// lineup-slots.jsx — Empty slot placeholder + slot wrapper + position icons.

// ─── Position icons (SVG, simple geometric) ──────────────────────────────────
// Inspired by tactical board notation — circles for outfield, gloves for GK.
function PosIcon({ position, size = 28 }) {
  const stroke = 'currentColor';
  const sw = 1.6;
  switch (position) {
    case 'GK':
      // Glove silhouette — palm + 4 fingers
      return (
        <svg width={size} height={size} viewBox="0 0 32 32" fill="none" aria-hidden="true">
          <path d="M9 14 V8.5 a1.5 1.5 0 0 1 3 0 V14
                   M12 14 V6.5 a1.5 1.5 0 0 1 3 0 V14
                   M15 14 V7.5 a1.5 1.5 0 0 1 3 0 V14
                   M18 14 V9.5 a1.5 1.5 0 0 1 3 0 V14
                   M9 14 V18 a7 7 0 0 0 7 7 h2 a3 3 0 0 0 3 -3 V14"
                stroke={stroke} strokeWidth={sw} strokeLinecap="round" strokeLinejoin="round"/>
        </svg>
      );
    case 'DEF':
      // Shield
      return (
        <svg width={size} height={size} viewBox="0 0 32 32" fill="none" aria-hidden="true">
          <path d="M16 5 L26 9 V16 c0 6 -4 10 -10 12 -6 -2 -10 -6 -10 -12 V9 Z"
                stroke={stroke} strokeWidth={sw} strokeLinejoin="round"/>
          <path d="M11 15 H21" stroke={stroke} strokeWidth={sw} strokeLinecap="round"/>
        </svg>
      );
    case 'MID':
      // Diamond (playmaker)
      return (
        <svg width={size} height={size} viewBox="0 0 32 32" fill="none" aria-hidden="true">
          <path d="M16 5 L26 16 L16 27 L6 16 Z"
                stroke={stroke} strokeWidth={sw} strokeLinejoin="round"/>
          <path d="M11 16 H21 M16 11 V21" stroke={stroke} strokeWidth={sw} strokeLinecap="round" opacity="0.5"/>
        </svg>
      );
    case 'FWD':
      // Upward arrow / chevron
      return (
        <svg width={size} height={size} viewBox="0 0 32 32" fill="none" aria-hidden="true">
          <path d="M16 5 L26 18 H21 V27 H11 V18 H6 Z"
                stroke={stroke} strokeWidth={sw} strokeLinejoin="round"/>
        </svg>
      );
    default:
      return null;
  }
}

// Plus icon for "add" affordance
function IconPlus({ size = 18 }) {
  return (
    <svg width={size} height={size} viewBox="0 0 18 18" fill="none" aria-hidden="true">
      <path d="M9 3 V15 M3 9 H15" stroke="currentColor" strokeWidth="1.6" strokeLinecap="round"/>
    </svg>
  );
}

// X / close
function IconX({ size = 14 }) {
  return (
    <svg width={size} height={size} viewBox="0 0 18 18" fill="none" aria-hidden="true">
      <path d="M4 4 L14 14 M14 4 L4 14" stroke="currentColor" strokeWidth="1.8" strokeLinecap="round"/>
    </svg>
  );
}

// ─── Empty slot placeholder ──────────────────────────────────────────────────
// Dashed outline matching compact card aspect (200×310 → here scaled).
// Shows position icon, label, and a small + affordance.
function EmptySlot({ position, locale = 'fa', scale = 1, onClick, label, invalid = false }) {
  const ll = LU_I18N[locale];
  const w = 200 * scale;
  const h = 310 * scale;
  const tint = invalid ? 'var(--danger)' : 'var(--text-tertiary)';
  return (
    <button className={`lu-empty ${invalid ? 'lu-empty--invalid' : ''}`}
            onClick={onClick}
            type="button"
            aria-label={`${ll.add} · ${ll.pos[position]}`}
            style={{ width: w, height: h, color: tint }}>
      <div className="lu-empty-stripes" aria-hidden="true"></div>
      <div className="lu-empty-icon"><PosIcon position={position} size={36 * scale} /></div>
      <div className="lu-empty-pos">{ll.posShort[position]}</div>
      <div className="lu-empty-label">{label ?? ll.pos[position]}</div>
      <div className="lu-empty-plus" aria-hidden="true">
        <IconPlus size={16 * scale} />
      </div>
    </button>
  );
}

// ─── Special empty slot (Fan / Coach) ────────────────────────────────────────
// Smaller variant for the special bar.
function SpecialEmptySlot({ kind, locale = 'fa', onClick, scale = 1, compact = false }) {
  const ll = LU_I18N[locale];
  const isFan = kind === 'fan';
  // Brighter, more saturated tints so the slot reads as a tinted card rather
  // than a faint outline. These match the danger/success accents but pushed
  // toward chromatic display values.
  const tint = isFan ? '#EF4444' : '#22C55E';
  const label = isFan ? ll.fanSlot : ll.coachSlot;
  const sub = isFan ? ll.fanEmpty : ll.coachEmpty;
  return (
    <button className={`lu-special-empty ${compact ? 'lu-special-empty--compact' : ''}`}
            onClick={onClick}
            type="button"
            aria-label={`${ll.add} · ${label}`}
            style={{ '--tint': tint }}>
      <div className="lu-special-empty-stripes" aria-hidden="true"></div>
      <div className="lu-special-empty-icon" aria-hidden="true">
        {isFan ? (
          // small "+" inside a flag-like rect
          <svg width={compact ? 16 : 20} height={compact ? 16 : 20} viewBox="0 0 22 22" fill="none">
            <path d="M5 4 V18 M5 4 H17 L14 9 L17 14 H5"
                  stroke="currentColor" strokeWidth="1.6" strokeLinejoin="round" strokeLinecap="round"/>
          </svg>
        ) : (
          // whistle for coach
          <svg width={compact ? 16 : 20} height={compact ? 16 : 20} viewBox="0 0 22 22" fill="none">
            <circle cx="13" cy="13" r="5.5" stroke="currentColor" strokeWidth="1.6"/>
            <path d="M7.5 11 L3 8 L5 6 L8.5 9" stroke="currentColor" strokeWidth="1.6" strokeLinecap="round" strokeLinejoin="round"/>
            <circle cx="13" cy="13" r="1.5" fill="currentColor"/>
          </svg>
        )}
      </div>
      <div className="lu-special-empty-text">
        <div className="lu-special-empty-label">{label}</div>
        <div className="lu-special-empty-sub">{sub}</div>
      </div>
      <div className="lu-special-empty-plus" aria-hidden="true">
        <IconPlus size={compact ? 14 : 16} />
      </div>
    </button>
  );
}

// ─── Filled slot wrapper ─────────────────────────────────────────────────────
// Wraps a PlayerCard compact in a positioning frame. Supports invalid styling +
// "captain"/"selected" markers if needed later. For now: simple wrapper.
function FilledSlot({ children, scale = 1, invalid = false, onRemove, onClick, locale = 'fa' }) {
  const ll = LU_I18N[locale];
  return (
    <div className={`lu-filled ${invalid ? 'lu-filled--invalid' : ''}`}
         style={{ '--slot-scale': scale }}
         onClick={onClick}>
      <div className="lu-filled-scaler">{children}</div>
      {invalid && (
        <div className="lu-filled-warn" role="alert">
          <span>⚠</span>
          <span>{ll.invalidPosition}</span>
        </div>
      )}
      {onRemove && (
        <button className="lu-filled-remove"
                onClick={(e) => { e.stopPropagation(); onRemove(); }}
                type="button"
                aria-label={ll.remove}>
          <IconX size={12} />
        </button>
      )}
    </div>
  );
}

// ─── styles ─────────────────────────────────────────────────────────────────
const SLOT_CSS = `
  /* ──── Empty slot (player) ──── */
  .lu-empty {
    position: relative;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 6px;
    padding: 14px 10px;
    border-radius: 18px;
    border: 1.5px dashed rgba(255,255,255,0.12);
    background:
      radial-gradient(ellipse 80% 60% at 50% 40%, rgba(255,255,255,0.025) 0%, transparent 80%),
      rgba(255,255,255,0.012);
    color: var(--text-tertiary);
    font-family: inherit;
    cursor: pointer;
    transition: border-color .15s, background .15s, transform .12s, color .15s;
    overflow: hidden;
    isolation: isolate;
  }
  .lu-empty:hover {
    border-color: rgba(255,255,255,0.30);
    background: rgba(255,255,255,0.035);
    color: var(--text-secondary);
  }
  .lu-empty:active { transform: scale(0.98); }
  .lu-empty--invalid {
    border-color: var(--danger);
    background: rgba(251,113,133,0.06);
  }
  .lu-empty-stripes {
    position: absolute;
    inset: 0;
    background-image: repeating-linear-gradient(
      135deg,
      transparent 0 14px,
      rgba(255,255,255,0.02) 14px 15px
    );
    pointer-events: none;
    z-index: 0;
  }
  .lu-empty-icon {
    position: relative;
    z-index: 1;
    opacity: 0.6;
    margin-bottom: 4px;
  }
  .lu-empty-pos {
    position: relative;
    z-index: 1;
    font-family: 'JetBrains Mono', ui-monospace, monospace;
    font-size: 11px;
    letter-spacing: 0.18em;
    font-weight: 700;
    color: var(--text-tertiary);
    opacity: 0.7;
  }
  .lu-empty-label {
    position: relative;
    z-index: 1;
    font-size: 13px;
    font-weight: 600;
    color: var(--text-secondary);
    margin-top: 2px;
  }
  .lu-empty-plus {
    position: relative;
    z-index: 1;
    margin-top: 6px;
    width: 28px;
    height: 28px;
    border-radius: 50%;
    border: 1px dashed rgba(255,255,255,0.20);
    display: flex; align-items: center; justify-content: center;
    color: var(--text-secondary);
    background: rgba(255,255,255,0.03);
  }
  .lu-empty:hover .lu-empty-plus {
    border-color: var(--accent);
    color: var(--accent);
    background: rgba(113,99,217,0.10);
  }

  /* ──── Filled slot wrapper ──── */
  .lu-filled {
    position: relative;
    width: calc(200px * var(--slot-scale, 1));
    height: calc(310px * var(--slot-scale, 1));
    cursor: pointer;
  }
  .lu-filled-scaler {
    position: absolute;
    top: 0; left: 0;
    width: 200px;
    height: 310px;
    transform-origin: top left;
    transform: scale(var(--slot-scale, 1));
  }
  /* in RTL the visual origin of the lineup should still anchor consistently;
     since we scale uniformly and parent has fixed scaled dims, no flip needed */
  .lu-filled:hover .lu-filled-scaler { filter: brightness(1.04); }
  .lu-filled--invalid::after {
    content: '';
    position: absolute;
    inset: -3px;
    border-radius: 22px;
    border: 2px solid var(--danger);
    box-shadow: 0 0 16px rgba(251,113,133,0.30);
    pointer-events: none;
  }
  .lu-filled-warn {
    position: absolute;
    top: -10px;
    left: 50%;
    transform: translateX(-50%);
    background: var(--danger);
    color: #2A0A12;
    padding: 3px 10px;
    border-radius: 999px;
    font-size: 11px;
    font-weight: 700;
    display: flex; align-items: center; gap: 4px;
    z-index: 10;
    box-shadow: 0 4px 10px rgba(251,113,133,0.4);
    white-space: nowrap;
  }
  .lu-filled-remove {
    position: absolute;
    top: -8px;
    right: -8px;
    width: 26px;
    height: 26px;
    border-radius: 50%;
    border: 1px solid rgba(255,255,255,0.18);
    background: rgba(20,20,28,0.92);
    color: var(--text-secondary);
    display: flex; align-items: center; justify-content: center;
    cursor: pointer;
    z-index: 11;
    transition: background .12s, color .12s, transform .12s;
    backdrop-filter: blur(8px);
    -webkit-backdrop-filter: blur(8px);
    box-shadow: 0 2px 6px rgba(0,0,0,0.4);
  }
  html[dir="rtl"] .lu-filled-remove { right: auto; left: -8px; }
  .lu-filled-remove:hover {
    background: var(--danger);
    color: white;
  }
  .lu-filled-remove:active { transform: scale(0.92); }

  /* ──── Special empty slot ──── */
  /* Hero look — colored tint background + glow + filled plus chip, so users
     immediately see these are actionable cards. */
  .lu-special-empty {
    position: relative;
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 14px 14px;
    border-radius: 16px;
    border: 1.5px solid color-mix(in oklab, var(--tint) 55%, transparent);
    background:
      linear-gradient(135deg,
        color-mix(in oklab, var(--tint) 22%, transparent) 0%,
        color-mix(in oklab, var(--tint) 8%, rgba(20,20,28,0.45)) 100%);
    color: var(--text-primary);
    font-family: inherit;
    cursor: pointer;
    transition: border-color .15s, background .15s, transform .12s, box-shadow .15s;
    overflow: hidden;
    text-align: start;
    width: 100%;
    box-shadow:
      0 0 0 1px color-mix(in oklab, var(--tint) 25%, transparent) inset,
      0 6px 18px -6px color-mix(in oklab, var(--tint) 50%, transparent);
    isolation: isolate;
  }
  .lu-special-empty::before {
    content: '';
    position: absolute;
    inset: 0;
    background: radial-gradient(ellipse 70% 100% at 100% 0%,
      color-mix(in oklab, var(--tint) 35%, transparent) 0%, transparent 70%);
    opacity: 0.7;
    pointer-events: none;
    z-index: -1;
  }
  .lu-special-empty--compact {
    padding: 10px 12px;
    gap: 10px;
    border-radius: 12px;
  }
  .lu-special-empty:hover {
    border-color: var(--tint);
    transform: translateY(-1px);
    box-shadow:
      0 0 0 1px color-mix(in oklab, var(--tint) 50%, transparent) inset,
      0 10px 26px -6px color-mix(in oklab, var(--tint) 70%, transparent);
  }
  .lu-special-empty:active { transform: translateY(0); }
  .lu-special-empty-stripes {
    position: absolute;
    inset: 0;
    background-image: repeating-linear-gradient(
      135deg,
      transparent 0 14px,
      color-mix(in oklab, var(--tint) 8%, transparent) 14px 15px
    );
    pointer-events: none;
    z-index: 0;
  }
  .lu-special-empty-icon {
    position: relative;
    z-index: 1;
    width: 40px;
    height: 40px;
    border-radius: 10px;
    background: color-mix(in oklab, var(--tint) 28%, rgba(0,0,0,0.35));
    border: 1px solid color-mix(in oklab, var(--tint) 50%, transparent);
    display: flex; align-items: center; justify-content: center;
    color: #fff;
    flex-shrink: 0;
    box-shadow: 0 4px 12px -4px color-mix(in oklab, var(--tint) 60%, transparent);
  }
  .lu-special-empty--compact .lu-special-empty-icon {
    width: 32px; height: 32px;
    border-radius: 8px;
  }
  .lu-special-empty-text {
    position: relative;
    z-index: 1;
    display: flex; flex-direction: column;
    line-height: 1.2;
    min-width: 0;
    flex: 1;
  }
  .lu-special-empty-label {
    font-size: 13px;
    font-weight: 800;
    color: var(--text-primary);
  }
  .lu-special-empty--compact .lu-special-empty-label { font-size: 12px; }
  .lu-special-empty-sub {
    font-size: 10px;
    color: color-mix(in oklab, var(--tint) 85%, white);
    margin-top: 3px;
    font-family: 'JetBrains Mono', ui-monospace, monospace;
    letter-spacing: 0.06em;
    text-transform: uppercase;
    font-weight: 700;
  }
  .lu-special-empty--compact .lu-special-empty-sub { font-size: 9px; margin-top: 1px; }
  html[lang="fa"] .lu-special-empty-sub,
  html[lang="ar"] .lu-special-empty-sub {
    font-family: inherit;
    letter-spacing: 0;
    text-transform: none;
    font-weight: 600;
  }
  .lu-special-empty-plus {
    position: relative;
    z-index: 1;
    width: 28px;
    height: 28px;
    border-radius: 50%;
    border: 0;
    display: flex; align-items: center; justify-content: center;
    color: #fff;
    background: var(--tint);
    box-shadow: 0 4px 12px -2px color-mix(in oklab, var(--tint) 70%, transparent);
    flex-shrink: 0;
  }
  .lu-special-empty--compact .lu-special-empty-plus {
    width: 24px; height: 24px;
  }
`;

const __luSlotStyle = document.createElement('style');
__luSlotStyle.textContent = SLOT_CSS;
document.head.appendChild(__luSlotStyle);

Object.assign(window, { PosIcon, IconPlus, IconX, EmptySlot, SpecialEmptySlot, FilledSlot });
