/* Arcana Frontier â€” mobile-first styles. */
:root {
  --bg: #0f1220;
  --bg2: #171a2e;
  --panel: #1f2440;
  --ink: #eef1ff;
  --muted: #9aa3c7;
  --accent: #e8c84a;
  --player: #3ba55d;
  --opponent: #e0563b;
  --cell: #262b4a;
  --cell2: #2f355c;
  --radius: 12px;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }

html, body {
  margin: 0; padding: 0;
  background: var(--bg);
  color: var(--ink);
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  -webkit-user-select: none; user-select: none;
  overscroll-behavior: none;
  touch-action: manipulation;
}

.app { min-height: 100dvh; display: flex; flex-direction: column; }

.screen {
  padding: calc(12px + var(--safe-top)) 12px calc(16px + var(--safe-bottom));
  max-width: 520px; margin: 0 auto; width: 100%;
  flex: 1; display: flex; flex-direction: column;
}

/* ---------- buttons ---------- */
.btn {
  appearance: none; border: 1px solid var(--cell2); background: var(--panel); color: var(--ink);
  padding: 12px 16px; border-radius: var(--radius); font-size: 16px; font-weight: 600;
  min-height: 48px; cursor: pointer; width: 100%;
}
.btn.primary { background: var(--accent); color: #1a1400; border-color: var(--accent); }
.btn.ghost { background: transparent; }
.btn.small { width: auto; padding: 8px 12px; min-height: 40px; font-size: 14px; }
.btn.back { align-self: flex-start; margin-bottom: 8px; }
.btn:disabled { opacity: 0.45; }

/* ---------- home ---------- */
.home { justify-content: center; align-items: stretch; gap: 16px; }
.brand { text-align: center; margin-bottom: 8px; }
.brand h1 { margin: 0; font-size: 30px; letter-spacing: 0.5px; color: var(--accent); }
.brand .subtitle { margin: 4px 0 0; color: var(--muted); font-size: 14px; }
.menu { display: flex; flex-direction: column; gap: 10px; }
.stats { text-align: center; color: var(--muted); font-size: 13px; margin: 4px 0 0; }
.badge { display: inline-block; background: var(--cell2); border-radius: 999px; padding: 1px 8px; font-size: 12px; color: var(--muted); margin-left: 6px; }

/* ---------- modes ---------- */
.modes { display: flex; flex-direction: column; gap: 12px; margin-top: 8px; }
.mode-card {
  text-align: left; background: var(--panel); border: 1px solid var(--cell2); border-radius: var(--radius);
  padding: 14px; color: var(--ink); cursor: pointer;
}
.mode-card h3 { margin: 0 0 4px; color: var(--accent); }
.mode-card .tagline { margin: 0 0 6px; color: var(--muted); font-size: 13px; }
.mode-card .blurb { margin: 0; font-size: 13px; color: #c7cef0; }
.mode-actions { display: flex; gap: 8px; margin-top: 10px; }
.mode-actions .btn { width: auto; }

/* ---------- deck editor ---------- */
.deck-slots {
  display: flex; gap: 6px; overflow-x: auto; scroll-snap-type: x proximity;
  padding: 6px; background: var(--bg2); border-radius: var(--radius); margin-top: 8px;
}
.deck-slots::-webkit-scrollbar { height: 4px; }
.deck-slots::-webkit-scrollbar-thumb { background: var(--cell2); border-radius: 2px; }
.deck-slot { flex: 0 0 auto; width: 64px; height: 88px; border-radius: 10px; scroll-snap-align: center; }
.deck-slot .card { border-radius: 10px; }
.deck-slot .card .card-name { display: block; position: absolute; bottom: 1px; left: 2px; right: 2px; font-size: 8px; text-align: center; background: rgba(0,0,0,0.55); padding: 1px; border-radius: 3px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.deck-slot.empty { border: 2px dashed var(--cell2); display: flex; align-items: center; justify-content: center; }
.deck-slot .slot-empty { color: var(--muted); font-size: 22px; }
.deck-slot.filled { cursor: pointer; }
.deck-actions { display: flex; gap: 8px; justify-content: center; margin-top: 10px; }
button.col-item { background: transparent; border: 0; padding: 0; color: var(--ink); cursor: pointer; }
.col-item.in-deck .card { outline: 2px solid var(--accent); outline-offset: 1px; }
.col-item.in-deck .count { color: var(--accent); font-weight: 700; }

/* ---------- match ---------- */
.match { padding-left: 8px; padding-right: 8px; }
.match-bar { display: flex; align-items: center; justify-content: space-between; gap: 8px; margin-bottom: 4px; }
.scores { display: flex; gap: 8px; font-size: 13px; }
.score-p { color: var(--player); }
.score-o { color: var(--opponent); }
.turn-indicator { text-align: center; color: var(--muted); font-size: 13px; min-height: 18px; margin: 4px 0; }

.opp-hand { display: flex; gap: 4px; justify-content: center; align-items: center; min-height: 44px; flex-wrap: nowrap; overflow-x: auto; }
.hand-count { font-size: 12px; color: var(--muted); }

/* grid board — cells share the card's portrait 5:7 proportion (board = 4 cards wide, 4 tall) */
.grid-board {
  display: grid; grid-template-columns: repeat(4, 1fr); gap: 4px;
  background: var(--bg2); padding: 6px; border-radius: var(--radius);
  margin: 6px auto; width: 100%; max-width: 340px; aspect-ratio: 5 / 7;
}
.cell {
  background: var(--cell); border: 1px solid var(--cell2); border-radius: 8px; padding: 0;
  position: relative; min-width: 0; min-height: 0; cursor: pointer; overflow: hidden;
}
.cell.empty { background: var(--cell); }
.cell.placeable { outline: 2px solid var(--accent); outline-offset: -2px; }
.cell.filled { padding: 0; }
.cell:disabled { cursor: default; }
.cell-el { position: absolute; top: 2px; left: 2px; font-size: 10px; opacity: 0.85; z-index: 2; }

/* ----------------------------------------------------------------------
   Arcana Frontier card â€” ORIGINAL design, FF7-*inspired* (not a copy).
   Midgar slate body + Mako-teal glow + brass-gold double frame + art-deco
   ticks + scanlines + rarity gem + element badge. Element accent = --el.
   ---------------------------------------------------------------------- */
.card {
  --el: #8a8f98; --rarity: #9aa3b5;
  --slate: #1b212b; --slate2: #2a3340; --mako: #2fbfa4; --gold: #d8b24a; --red: #c0322e;
  position: relative; width: 100%; height: 100%;
  background:
    radial-gradient(130% 95% at 50% -12%, color-mix(in srgb, var(--el) 38%, transparent), transparent 62%),
    linear-gradient(165deg, var(--slate2), var(--slate) 78%);
  border: 1px solid var(--gold); border-radius: 9px;
  color: #fff; overflow: hidden; padding: 3px;
  display: flex; align-items: center; justify-content: center;
  box-shadow: inset 0 0 0 1px rgba(0,0,0,0.65), inset 0 0 0 2px rgba(216,178,74,0.22), 0 2px 7px rgba(0,0,0,0.45);
}
/* inner hairline gold frame */
.card::before { content: ""; position: absolute; inset: 3px; border: 1px solid rgba(216,178,74,0.4); border-radius: 6px; pointer-events: none; z-index: 0; }
/* CRT scanlines + Mako vignette */
.card::after {
  content: ""; position: absolute; inset: 0; pointer-events: none; z-index: 0;
  background:
    repeating-linear-gradient(0deg, rgba(255,255,255,0.025) 0 1px, transparent 1px 3px),
    radial-gradient(120% 120% at 50% 50%, transparent 55%, rgba(0,0,0,0.45));
}
.card .art-window { position: absolute; inset: 0; z-index: 0; }
.card .card-icon {
  position: absolute; left: 50%; top: 46%; transform: translate(-50%, -50%);
  width: 52%; height: 52%; color: var(--el); opacity: 0.55;
  filter: drop-shadow(0 0 6px color-mix(in srgb, var(--el) 55%, transparent));
}
.card .card-icon svg { width: 100%; height: 100%; display: block; }
.card.mini .card-icon { width: 60%; height: 60%; opacity: 0.5; }
/* (name hidden on tiny mini cells below; shown in name plate otherwise) */

/* art-deco corner ticks (geometric FF7/Midgar nod) */
.tick { position: absolute; width: 7px; height: 7px; border: 1px solid var(--gold); z-index: 2; pointer-events: none; }
.tick.tl { top: 2px; left: 2px; border-right: 0; border-bottom: 0; }
.tick.tr { top: 2px; right: 2px; border-left: 0; border-bottom: 0; }
.tick.bl { bottom: 2px; left: 2px; border-right: 0; border-top: 0; }
.tick.br { bottom: 2px; right: 2px; border-left: 0; border-top: 0; }

/* rarity gem (top-right) */
.rarity-gem {
  position: absolute; top: 4px; right: 4px; width: 7px; height: 7px; z-index: 3;
  background: var(--rarity); transform: rotate(45deg);
  box-shadow: 0 0 4px var(--rarity), inset 0 0 0 1px rgba(0,0,0,0.5);
}
/* element badge (top-left) */
.elem-badge {
  position: absolute; top: 3px; left: 3px; width: 13px; height: 13px; z-index: 3;
  color: var(--el); background: rgba(0,0,0,0.5); border-radius: 50%;
  padding: 1px; display: flex; align-items: center; justify-content: center;
  box-shadow: 0 0 0 1px rgba(216,178,74,0.4);
}
.elem-badge svg { width: 100%; height: 100%; display: block; }

/* name plate (bottom bar) â€” visible name */
.card .card-name {
  display: block;
  position: absolute; left: 3px; right: 3px; bottom: 3px; z-index: 2;
  font-size: 8px; font-weight: 700; letter-spacing: 0.2px; color: #e9ecf3;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis; text-shadow: 0 1px 2px rgba(0,0,0,0.9);
  background: linear-gradient(180deg, rgba(0,0,0,0.1), rgba(0,0,0,0.7));
  border-top: 1px solid rgba(216,178,74,0.45); border-radius: 0 0 5px 5px; padding: 1px 3px;
}
/* mini-cell name hiding is handled per-container in the final override block */

/* triad numbers */
.tri { position: absolute; font-weight: 800; font-size: 11px; line-height: 1; color: #fff; text-shadow: 0 1px 2px rgba(0,0,0,0.8); z-index: 1; }
.tri.t { top: 2px; left: 50%; transform: translateX(-50%); }
.tri.b { bottom: 2px; left: 50%; transform: translateX(-50%); }
.tri.l { left: 2px; top: 50%; transform: translateY(-50%); }
.tri.r { right: 2px; top: 50%; transform: translateY(-50%); }

/* tetra */
.tri.attacktype { top: 2px; right: 3px; font-size: 10px; color: var(--accent); }
.tri.defense { bottom: 2px; right: 3px; font-size: 10px; }
.tri.defense em { font-style: normal; opacity: 0.8; }
.tri.t { }
.arrows { position: absolute; inset: 18%; display: grid; grid-template-columns: repeat(3,1fr); grid-template-rows: repeat(3,1fr); gap: 1px; opacity: 0.5; pointer-events: none; }
.arrows i { width: 3px; height: 3px; border-radius: 50%; background: #fff; margin: auto; }
.arrows i.on { background: var(--accent); width: 4px; height: 4px; }
.arrows i.center { background: transparent; }

/* blood — Queen's Blood card layout: rank pips top-left, power top-right, class icon bottom-left, range icon bottom-right */
.blood-rank { position: absolute; top: 2px; left: 4px; display: flex; align-items: center; gap: 2px; color: var(--accent); font-size: 11px; font-weight: 800; text-shadow: 0 1px 2px rgba(0,0,0,0.6); }
.blood-rank i.pawn { width: 5px; height: 5px; border-radius: 50%; background: var(--accent); display: inline-block; }
.blood-power { position: absolute; top: 2px; right: 4px; font-size: 15px; font-weight: 800; color: var(--accent); text-shadow: 0 1px 2px rgba(0,0,0,0.6); }
.blood-class { position: absolute; bottom: 3px; left: 4px; width: 16px; height: 16px; color: var(--accent); }
.blood-class svg { width: 100%; height: 100%; }
/* Queen's Blood card-face range diagram: white center = the card's own space,
   gold squares = spaces that gain a pawn, red squares = spaces the ability
   reaches (solid red = pawn + ability, red outline = ability only, no pawn),
   gold center = a replace card (destroys the card it lands on). */
.range-diagram {
  position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%);
  width: 44%; aspect-ratio: 1; z-index: 1; pointer-events: none;
  display: grid; grid-template-columns: repeat(var(--grid), 1fr); gap: 1px;
}
.range-diagram i { border-radius: 1px; }
.range-diagram i.center { background: #f2f4f7; box-shadow: 0 0 0 1px rgba(0,0,0,0.45); }
.range-diagram i.center-replace { background: var(--gold); box-shadow: 0 0 0 1px rgba(0,0,0,0.45), 0 0 4px rgba(216,178,74,0.7); }
.range-diagram i.pawn { background: var(--gold); }
.range-diagram i.ability { background: rgba(192,50,46,0.12); border: 1.5px solid var(--red); }
.range-diagram i.both { background: var(--red); box-shadow: 0 0 3px rgba(192,50,46,0.7); }
.range-diagram i.blank { background: transparent; }
/* ability badge (Capture / Replace) sits bottom-right */
.blood-ability { position: absolute; bottom: 3px; right: 4px; width: 16px; height: 16px; background: rgba(0,0,0,0.5); border-radius: 4px; padding: 1px; }
.blood-ability svg { width: 100%; height: 100%; }
.blood-ability.ab-capture { color: #ff8074; }
.blood-ability.ab-replace { color: var(--accent); }

/* ownership tint */
.owned-player { box-shadow: inset 0 0 0 2px var(--player); }
.owned-opponent { box-shadow: inset 0 0 0 2px var(--opponent); }

/* rarity */
.rarity-rare { border-color: #62b6e8; }
.rarity-epic { border-color: #7a5aa8; }
.rarity-legendary { border-color: var(--accent); box-shadow: 0 0 8px rgba(232,200,74,0.5), inset 0 0 0 1px rgba(0,0,0,0.25); }

/* face down */
.card.facedown { background: repeating-linear-gradient(45deg, #222a4d, #222a4d 6px, #1a2040 6px, #1a2040 12px); }
.card-back { color: var(--accent); opacity: 0.6; }

/* hand — single row, scrolls horizontally (no wrap) */
.hand {
  display: flex; gap: 6px; justify-content: flex-start; margin-top: 8px;
  flex-wrap: nowrap; overflow-x: auto; padding: 10px 8px 6px;
  scroll-snap-type: x proximity; -webkit-overflow-scrolling: touch;
  scrollbar-width: thin; scrollbar-color: var(--cell2) transparent;
}
.hand::-webkit-scrollbar { height: 4px; }
.hand::-webkit-scrollbar-thumb { background: var(--cell2); border-radius: 2px; }
.hand-card { background: transparent; border: 0; padding: 0; width: 84px; height: 118px; cursor: pointer; flex: 0 0 auto; scroll-snap-align: center; }
.hand-card .card { border-radius: 10px; }
.hand-card .card.selected { outline: 3px solid var(--accent); outline-offset: 1px; transform: translateY(-6px); }
.hand-card .card .card-name { display: block; position: absolute; bottom: 1px; left: 2px; right: 2px; font-size: 8px; text-align: center; background: rgba(0,0,0,0.55); padding: 1px; border-radius: 3px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.hand-card:disabled { opacity: 0.5; }
.opp-hand .card.mini { width: 42px; height: 58px; }

/* blood board — shared 5x3 Queen's Blood board; score zones at the lane ends */
.blood-board {
  width: 100%; max-width: 380px; margin: 6px auto;
  display: flex; flex-direction: column; gap: 4px;
  background: var(--bg2); padding: 6px; border-radius: var(--radius);
}
.blood-lane { display: grid; grid-template-columns: 26px repeat(5, 1fr) 26px; gap: 4px; }
.blood-lane .cell { aspect-ratio: 5 / 7; }
.blood-header { display: grid; grid-template-columns: 26px 1fr 26px; gap: 4px; margin-bottom: 2px; }
.lane-label { font-size: 10px; font-weight: 700; text-align: center; padding: 2px 0; border-radius: 4px; }
.lane-label.you { background: rgba(47,191,164,.14); color: var(--mako); }
.lane-label.opp { background: rgba(192,50,46,.14); color: #ff8074; }
.lane-label.center { grid-column: 2; color: var(--muted); font-size: 11px; }
.lane-score { display: flex; align-items: center; justify-content: center; border-radius: 8px; font-weight: 700; font-size: 13px; }
.lane-score.you { background: rgba(47,191,164,.14); color: var(--mako); border: 1px solid rgba(47,191,164,.45); }
.lane-score.opp { background: rgba(192,50,46,.14); color: #ff8074; border: 1px solid rgba(192,50,46,.45); }
/* Queen's Blood preview: tiles whose enemy pawns the selected card will capture glow red */
.cell.preview-flip { box-shadow: inset 0 0 0 2px var(--opponent), 0 0 10px rgba(192,50,46,.6); animation: previewPulse 1s ease-in-out infinite; }
@keyframes previewPulse { 0%, 100% { box-shadow: inset 0 0 0 2px var(--opponent), 0 0 6px rgba(192,50,46,.4); } 50% { box-shadow: inset 0 0 0 3px var(--opponent), 0 0 14px rgba(192,50,46,.8); } }
/* Queen's Blood preview: tiles the selected card will spread a pawn onto glow teal */
.cell.preview-spread { box-shadow: inset 0 0 0 2px rgba(47,191,164,.75), 0 0 9px rgba(47,191,164,.45); animation: previewSpread 1s ease-in-out infinite; }
@keyframes previewSpread { 0%, 100% { box-shadow: inset 0 0 0 2px rgba(47,191,164,.55), 0 0 5px rgba(47,191,164,.3); } 50% { box-shadow: inset 0 0 0 3px rgba(47,191,164,.9), 0 0 12px rgba(47,191,164,.6); } }
/* Queen's Blood preview: enemy pawn spaces the selected card will convert glow red */
.cell.preview-convert { box-shadow: inset 0 0 0 2px rgba(192,50,46,.7), 0 0 9px rgba(192,50,46,.4); animation: previewSpread 1s ease-in-out infinite; }
/* Queen's Blood preview: Replace cards mark every occupied card they can destroy */
.cell.preview-replace { outline: 2px dashed var(--accent); outline-offset: -2px; animation: previewReplace 1s ease-in-out infinite; }
@keyframes previewReplace { 0%, 100% { box-shadow: 0 0 4px rgba(216,178,74,.3); } 50% { box-shadow: 0 0 12px rgba(216,178,74,.7); } }
/* pawns on board cells: 1-3 dots, teal for 1P, red for CPU */
.pawns { position: absolute; top: 3px; left: 0; right: 0; display: flex; justify-content: center; gap: 3px; pointer-events: none; }
.pawns i { width: 7px; height: 7px; border-radius: 50%; }
.pawns.you i { background: #2fbfa4; box-shadow: 0 0 4px rgba(47,191,164,.7); }
.pawns.opp i { background: #c0322e; box-shadow: 0 0 4px rgba(192,50,46,.7); }
.blood-lane .cell.filled .pawns { top: 1px; }
.blood-lane .cell.filled .pawns i { width: 5px; height: 5px; }
.field-note { text-align: center; color: var(--muted); font-size: 12px; min-height: 16px; }

/* ---------- collection ---------- */
.grid-collection { display: grid; grid-template-columns: repeat(auto-fill, minmax(96px, 1fr)); gap: 8px; margin-top: 10px; }
.col-item { display: flex; flex-direction: column; align-items: center; gap: 2px; }
.col-item .card { width: 96px; height: 128px; border-radius: 10px; }
.col-item .count { font-size: 12px; color: var(--muted); }
.col-item.locked { opacity: 0.28; filter: grayscale(0.7); }
.col-item .card-name { display: block; position: absolute; bottom: 1px; left: 2px; right: 2px; font-size: 8px; text-align: center; background: rgba(0,0,0,0.55); padding: 1px; border-radius: 3px; }

/* ---------- overlays ---------- */
.overlay { position: fixed; inset: 0; background: rgba(5,7,15,0.82); display: flex; align-items: center; justify-content: center; padding: 20px; z-index: 50; }
.end-card { background: var(--panel); border: 1px solid var(--cell2); border-radius: 16px; padding: 22px; width: 100%; max-width: 380px; text-align: center; max-height: 86dvh; overflow: auto; }
.end-card h2 { margin: 0 0 12px; }
.end-card.player h2 { color: var(--player); }
.end-card.opponent h2 { color: var(--opponent); }
.end-actions { display: flex; flex-direction: column; gap: 8px; margin-top: 16px; }
.reward p { color: var(--muted); margin: 4px 0 8px; }
.reward .card { width: 96px; height: 128px; margin: 0 auto; border-radius: 10px; }
.reward .card-name { display: block; position: absolute; bottom: 1px; left: 2px; right: 2px; font-size: 8px; background: rgba(0,0,0,0.55); border-radius: 3px; }
.rules-body { text-align: left; font-size: 14px; color: #d3d8f5; }
.rules-body h3 { margin: 12px 0 4px; color: var(--accent); }
.rules-body h3 small { color: var(--muted); font-weight: 400; }
.fine { color: var(--muted); font-size: 12px; margin-top: 12px; }
/* card-face diagram key in the rules overlay */
.diag-key { display: inline-block; width: 11px; height: 11px; border-radius: 2px; vertical-align: middle; margin: 0 2px; }
.diag-key.center { background: #f2f4f7; box-shadow: 0 0 0 1px rgba(0,0,0,0.45); }
.diag-key.pawn { background: var(--gold); }
.diag-key.ability { background: rgba(192,50,46,0.12); border: 1.5px solid var(--red); }
.diag-key.both { background: var(--red); }
.diag-key.replace { background: var(--gold); box-shadow: 0 0 0 1px rgba(0,0,0,0.45), 0 0 4px rgba(216,178,74,0.7); }

.toast { position: fixed; bottom: calc(24px + var(--safe-bottom)); left: 50%; transform: translateX(-50%); background: var(--accent); color: #1a1400; padding: 10px 18px; border-radius: 999px; font-weight: 700; z-index: 60; }

/* small screens */
@media (max-width: 380px) {
  .hand-card { width: 72px; height: 100px; }
  .tri { font-size: 10px; }
}

/* ---- card-name visibility (final override) ---- */
.card .card-name { display: block; }
.grid-board .card-name, .blood-board .card-name, .opp-hand .card-name { display: none; }
.hand-card .card-name, .col-item .card-name, .reward .card-name { display: block; }

/* ---- lift bottom stats above the visible name plate ---- */
.hand-card .tri.b, .hand-card .tri.defense, .hand-card .tri.effect,
.col-item .tri.b, .col-item .tri.defense, .col-item .tri.effect,
.reward .tri.b, .reward .tri.defense, .reward .tri.effect { bottom: 17px; }
.hand-card .blood-class, .hand-card .blood-ability,
.col-item .blood-class, .col-item .blood-ability,
.reward .blood-class, .reward .blood-ability { bottom: 17px; }
