:root {
  color-scheme: dark;
  --bg: #0b0d18;
  --panel: #1b2036;
  --accent: #5865f2;
  --gold: #f2c14e;
  --text: #e8e9f3;
  --text-dim: #9aa0c0;
  --blue: #3d7bff;
  --red: #ff4d4d;
  --card-w: 84;
  --card-h: 102;
}

* { box-sizing: border-box; }

html, body {
  margin: 0; height: 100%; height: 100dvh;
  background: radial-gradient(ellipse at top, #151a33 0%, var(--bg) 60%);
  color: var(--text);
  font-family: system-ui, -apple-system, "Segoe UI", sans-serif;
  overscroll-behavior: none;
  -webkit-user-select: none; user-select: none;
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation; /* no double-tap zoom on iOS */
}

#app {
  height: 100%; display: flex; flex-direction: column;
  padding-top: env(safe-area-inset-top);
  padding-bottom: env(safe-area-inset-bottom);
}
#boot-fallback { margin: auto; color: var(--text-dim); }

/* --- Auth ----------------------------------------------------------- */

.auth-screen {
  margin: auto; width: min(360px, 90vw);
  background: var(--panel); border-radius: 16px; padding: 2rem;
  box-shadow: 0 20px 60px rgba(0,0,0,.4);
}
.auth-screen h1 { margin: 0 0 .25rem; font-size: 1.5rem; color: var(--gold); }
.auth-screen p.subtitle { margin: 0 0 1.5rem; color: var(--text-dim); font-size: .9rem; }
.auth-screen input {
  width: 100%; padding: .75rem; border-radius: 8px; border: 1px solid #333a5c;
  background: #10132200; color: var(--text); font-size: 1rem; margin-bottom: 1rem;
}
.auth-screen button {
  width: 100%; padding: .75rem; border-radius: 8px; border: none;
  background: var(--accent); color: #fff; font-size: 1rem; font-weight: 600;
  cursor: pointer; margin-bottom: .5rem;
}
.auth-screen button.secondary { background: transparent; border: 1px solid #333a5c; color: var(--text-dim); }
.auth-screen .error { color: #f26d6d; font-size: .85rem; min-height: 1.2em; margin-bottom: .5rem; }

/* --- Topbar / layout ------------------------------------------------ */

.topbar {
  display: flex; align-items: center; gap: 1rem;
  padding: .5rem 1rem; background: var(--panel);
  font-size: .9rem; color: var(--text-dim);
}
.topbar button {
  background: transparent; border: 1px solid #333a5c; color: var(--text-dim);
  border-radius: 6px; padding: .3rem .7rem; cursor: pointer; font-size: .85rem;
}
.topbar .spacer { flex: 1; }

.page { flex: 1; overflow-y: auto; padding: 1rem; max-width: 900px; margin: 0 auto; width: 100%; }
.section-title {
  font-size: .95rem; color: var(--gold); margin: 1.5rem 0 .5rem;
  text-transform: uppercase; letter-spacing: .04em;
}
.section-title:first-child { margin-top: 0; }
.empty-note { color: var(--text-dim); font-size: .9rem; }

.list-row {
  display: flex; align-items: center; justify-content: space-between;
  background: var(--panel); border-radius: 10px; padding: .7rem 1rem; margin-bottom: .5rem;
}
.list-row button {
  background: var(--accent); border: none; color: #fff; border-radius: 6px;
  padding: .4rem .9rem; cursor: pointer; font-weight: 600;
}
.list-row .badge { font-size: .75rem; color: var(--gold); margin-left: .5rem; }
.list-row .opp { display: flex; flex-direction: column; gap: .15rem; }
.list-row .opp-name { font-weight: 600; }
.list-row .tier { color: var(--gold); font-size: .75rem; margin-left: .6rem; letter-spacing: .1em; }
.list-row .opp-title { font-size: .8rem; color: var(--text-dim); }

/* --- Cards ------------------------------------------------------------ */

.card {
  position: relative;
  width: calc(var(--card-w) * 1px);
  height: calc(var(--card-h) * 1px);
  flex-shrink: 0;
  perspective: 600px;
  transition: transform .18s ease;
}
.card-inner {
  position: absolute; inset: 0;
  transform-style: preserve-3d;
  transition: transform .5s cubic-bezier(.4,.1,.2,1);
  border-radius: 6px;
  box-shadow: 0 4px 12px rgba(0,0,0,.5);
}
.card-face {
  position: absolute; inset: 0; border-radius: 6px; overflow: hidden;
  backface-visibility: hidden; background: #1b2b5a;
}
.card-face-back {
  background: repeating-linear-gradient(135deg, #1c2450 0 6px, #232d63 6px 12px);
  border: 2px solid #3a4590;
}
.card-art {
  width: 100%; height: 100%; display: block; object-fit: cover;
  image-rendering: pixelated;
}
.card-clickable { cursor: pointer; }
.card-clickable:hover { transform: translateY(-6px) scale(1.04); }
.card-selected { transform: translateY(-12px) scale(1.06); }
.card-selected .card-inner { box-shadow: 0 0 0 3px var(--gold), 0 8px 20px rgba(0,0,0,.6); }

/* Owner tint: a colored border + a soft inner glow, blue=you, red=them */
.card-mine .card-inner   { box-shadow: 0 0 0 3px var(--blue), 0 0 14px 2px rgba(61,123,255,.55); }
.card-theirs .card-inner { box-shadow: 0 0 0 3px var(--red),  0 0 14px 2px rgba(255,77,77,.55); }
.card-mine .card-face::after, .card-theirs .card-face::after {
  content: ""; position: absolute; inset: 0; pointer-events: none; mix-blend-mode: overlay;
}
.card-mine .card-face::after   { background: rgba(61,123,255,.35); }
.card-theirs .card-face::after { background: rgba(255,77,77,.35); }

/* Arrows: gold wedges at the 8 positions */
.card-arrows { position: absolute; inset: 0; pointer-events: none; }
.arrow { position: absolute; width: 0; height: 0; opacity: 0; }
.arrow.on { opacity: 1; filter: drop-shadow(0 0 2px #000); }
.arrow-N  { top: 3px; left: 50%; margin-left: -6px; border-left: 6px solid transparent; border-right: 6px solid transparent; border-bottom: 9px solid var(--gold); }
.arrow-S  { bottom: 3px; left: 50%; margin-left: -6px; border-left: 6px solid transparent; border-right: 6px solid transparent; border-top: 9px solid var(--gold); }
.arrow-W  { left: 3px; top: 50%; margin-top: -6px; border-top: 6px solid transparent; border-bottom: 6px solid transparent; border-right: 9px solid var(--gold); }
.arrow-E  { right: 3px; top: 50%; margin-top: -6px; border-top: 6px solid transparent; border-bottom: 6px solid transparent; border-left: 9px solid var(--gold); }
.arrow-NE { top: 3px; right: 3px; border-top: 11px solid var(--gold); border-left: 11px solid transparent; }
.arrow-NW { top: 3px; left: 3px; border-top: 11px solid var(--gold); border-right: 11px solid transparent; }
.arrow-SE { bottom: 3px; right: 3px; border-bottom: 11px solid var(--gold); border-left: 11px solid transparent; }
.arrow-SW { bottom: 3px; left: 3px; border-bottom: 11px solid var(--gold); border-right: 11px solid transparent; }

.card-stats {
  position: absolute; bottom: 4px; left: 50%; transform: translateX(-50%);
  font: 700 11px/1 "Courier New", monospace; letter-spacing: 1px;
  color: #fff; text-shadow: 0 0 3px #000, 0 0 3px #000, 1px 1px 0 #000;
  background: rgba(0,0,0,.45); padding: 2px 5px; border-radius: 3px;
}

.hand-row {
  display: flex; gap: .6rem; overflow-x: auto; padding: 1rem;
  justify-content: center; min-height: calc(var(--card-h) * 1px + 2.4rem);
}
.card-grid { display: flex; flex-wrap: wrap; gap: .6rem; }

/* --- Match board -------------------------------------------------------- */

.match-shell { display: flex; flex-direction: column; height: 100%; position: relative; }
.match-status {
  display: flex; justify-content: space-between; align-items: center;
  padding: .5rem 1rem; background: var(--panel); font-size: .9rem;
}
.match-status .score { font-weight: 700; }
.match-status .score .you-n { color: var(--blue); }
.match-status .score .them-n { color: var(--red); }
.match-status .turn.you-turn { color: #7CFC9E; }
.match-status .turn.their-turn { color: var(--text-dim); }
.match-status .turn.finished { color: var(--gold); font-weight: 700; }

.opp-hand {
  display: flex; gap: .4rem; justify-content: center; padding: .6rem;
  --card-w: 42; --card-h: 51;
}

.board {
  display: grid; grid-template-columns: repeat(4, 1fr); grid-template-rows: repeat(4, 1fr);
  gap: 7px; aspect-ratio: 1; margin: .25rem auto;
  width: min(94vw, 440px, calc(100dvh - 330px));
  padding: 12px; border-radius: 14px;
  background:
    radial-gradient(ellipse at 30% 20%, rgba(255,255,255,.06), transparent 60%),
    linear-gradient(160deg, #262a4a, #12152a);
  border: 3px solid #4a4160;
  box-shadow: 0 0 0 2px #1b1a2c, 0 0 0 5px #8a7a4a, 0 18px 40px rgba(0,0,0,.7), inset 0 0 40px rgba(0,0,0,.6);
}
.cell {
  border-radius: 6px; position: relative; overflow: visible;
  display: flex; align-items: center; justify-content: center;
  background:
    linear-gradient(135deg, rgba(255,255,255,.07), transparent 40%, rgba(0,0,0,.25)),
    #2a2f52;
  border: 1px solid #3a4070;
  box-shadow: inset 0 2px 4px rgba(255,255,255,.08), inset 0 -3px 6px rgba(0,0,0,.45);
  transition: background .2s, transform .2s;
}
.board.intro .cell { animation: tileIn .4s ease-out backwards; }
.board.intro .cell:nth-child(n)  { animation-delay: calc(var(--i, 0) * 0s); }
@keyframes tileIn { from { opacity: 0; transform: scale(.6); } }

.cell.blocked {
  background:
    repeating-linear-gradient(45deg, rgba(255,255,255,.03) 0 3px, transparent 3px 8px),
    radial-gradient(ellipse at 40% 30%, #6b6478, #3a3547 70%);
  border: 2px solid #7a7290;
  box-shadow: inset 0 3px 6px rgba(255,255,255,.18), inset 0 -6px 10px rgba(0,0,0,.6), 0 6px 10px rgba(0,0,0,.6);
}
.cell.blocked::before {
  content: ""; position: absolute; inset: 22%; border-radius: 4px;
  background: linear-gradient(135deg, #8d8599, #4d4759);
  box-shadow: inset 0 2px 3px rgba(255,255,255,.25), inset 0 -3px 4px rgba(0,0,0,.5);
  transform: rotate(45deg) scale(.8);
}
.cell.blocked.slam { animation: slam .32s cubic-bezier(.2,1.2,.3,1); }
@keyframes slam {
  0%   { transform: scale(1.8) translateY(-40px); opacity: 0; }
  70%  { transform: scale(.95) translateY(2px); opacity: 1; }
  100% { transform: scale(1) translateY(0); }
}

.cell.empty.placeable { cursor: pointer; border-color: var(--accent); background: #2f3a6e; }
.cell.empty.placeable::after {
  content: ""; position: absolute; inset: 6px; border-radius: 5px;
  border: 2px dashed rgba(120,140,255,.7); animation: pulse 1.2s ease-in-out infinite;
}
.cell.empty.placeable:hover { background: #3a4a8a; transform: scale(1.03); }
@keyframes pulse { 50% { opacity: .35; } }

.cell .card { width: 100%; height: 100%; --card-w: 0; --card-h: 0; }
.cell .card .card-inner { border-radius: 5px; }

.cell.choose { cursor: pointer; z-index: 5; }
.cell.choose .card .card-inner { animation: chooseGlow .8s ease-in-out infinite alternate; }
.cell.choose::after {
  content: "FIGHT"; position: absolute; top: -10px; left: 50%; transform: translateX(-50%);
  font: 900 10px/1 system-ui; letter-spacing: .1em; color: #fff; background: var(--red);
  padding: 3px 6px; border-radius: 4px; box-shadow: 0 2px 6px rgba(0,0,0,.6); pointer-events: none;
}
@keyframes chooseGlow {
  from { box-shadow: 0 0 0 3px var(--red), 0 0 10px 2px rgba(255,77,77,.5); }
  to   { box-shadow: 0 0 0 4px #fff, 0 0 24px 8px rgba(255,120,120,.9); transform: scale(1.06); }
}

/* --- Hand setup --------------------------------------------------------- */

.setup { flex: 1; overflow-y: auto; padding: 1rem; max-width: 900px; margin: 0 auto; width: 100%; }
.setup-head { text-align: center; margin-bottom: .75rem; }
.setup-vs { font-size: 1.1rem; }
.setup-hint { color: var(--text-dim); font-size: .85rem; }
.slots {
  display: flex; gap: .6rem; justify-content: center; padding: .8rem;
  background: linear-gradient(160deg, #262a4a, #12152a); border: 2px solid #8a7a4a; border-radius: 12px;
  box-shadow: inset 0 0 30px rgba(0,0,0,.6);
}
.slot-empty {
  width: calc(var(--card-w) * 1px); height: calc(var(--card-h) * 1px);
  border: 2px dashed #4a5080; border-radius: 6px; color: #4a5080;
  display: flex; align-items: center; justify-content: center; font-weight: 700;
}
.setup-actions { display: flex; gap: .6rem; justify-content: center; margin: .8rem 0 1rem; }
.setup-actions button {
  background: var(--accent); border: none; color: #fff; border-radius: 8px;
  padding: .6rem 1.4rem; cursor: pointer; font-weight: 700; font-size: 1rem;
}
.setup-actions button:disabled { opacity: .45; cursor: default; }
.setup-actions button.secondary { background: transparent; border: 1px solid #333a5c; color: var(--text-dim); }
.collection { display: flex; flex-wrap: wrap; gap: .7rem; justify-content: center; }
.coll-item { display: flex; flex-direction: column; align-items: center; gap: .25rem; }
.coll-name { font-size: .7rem; color: var(--text-dim); max-width: 84px; text-align: center; }
.card-dim { opacity: .4; }

/* Flip on capture: rotate the inner; the owner tint swaps at the halfway mark (JS) */
.card.flipping .card-inner { animation: flipY .6s cubic-bezier(.4,.1,.2,1); }
@keyframes flipY {
  0%   { transform: rotateY(0); }
  50%  { transform: rotateY(90deg) scale(1.1); }
  100% { transform: rotateY(0); }
}

/* Landing bump after a card flies onto a cell */
.card.landed .card-inner { animation: land .35s ease-out; }
@keyframes land {
  0%   { transform: scale(1.25); box-shadow: 0 20px 30px rgba(0,0,0,.7); }
  60%  { transform: scale(.96); }
  100% { transform: scale(1); }
}

/* Attack pulse on the attacker */
.card.attacking .card-inner { animation: attack .45s ease-in-out; }
@keyframes attack {
  0%,100% { transform: scale(1); }
  40% { transform: scale(1.12); box-shadow: 0 0 0 3px var(--gold), 0 0 24px 6px rgba(242,193,78,.7); }
}

/* Flying clone (position:fixed, moved via transform by JS) */
.flying {
  position: fixed; z-index: 50; pointer-events: none; margin: 0;
  transition: transform .55s cubic-bezier(.3,.9,.3,1), width .55s, height .55s;
  will-change: transform;
}
.flying .card-inner { box-shadow: 0 25px 40px rgba(0,0,0,.7); }

/* Battle numbers */
.battle-num {
  position: absolute; left: 50%; top: 50%; transform: translate(-50%,-50%);
  z-index: 40; pointer-events: none;
  font: 900 30px/1 "Courier New", monospace; color: #fff;
  text-shadow: 0 0 6px #000, 0 0 12px #000, 2px 2px 0 #000;
  background: rgba(0,0,0,.55); padding: 6px 10px; border-radius: 8px;
  animation: numIn .2s ease-out;
}
.battle-num.win  { color: #7CFC9E; }
.battle-num.lose { color: #ff7a7a; }
@keyframes numIn { from { transform: translate(-50%,-50%) scale(.4); opacity: 0; } }

.banner {
  position: absolute; left: 50%; top: 42%; transform: translate(-50%,-50%);
  z-index: 45; pointer-events: none; text-align: center;
  font: 900 clamp(28px, 8vw, 54px)/1 system-ui, sans-serif; letter-spacing: .06em;
  color: var(--gold); text-shadow: 0 0 10px #000, 0 0 30px rgba(242,193,78,.8), 3px 3px 0 #000;
  animation: bannerIn .5s cubic-bezier(.2,1.4,.4,1);
}
.banner.combo { color: #ff9a3c; }
.banner.lose  { color: #ff6b6b; }
.banner small { display: block; font-size: .4em; color: #fff; letter-spacing: .02em; margin-top: .3em; }
@keyframes bannerIn { from { transform: translate(-50%,-50%) scale(.3); opacity: 0; } }
.banner.out { animation: bannerOut .4s ease-in forwards; }
@keyframes bannerOut { to { transform: translate(-50%,-80%) scale(1.1); opacity: 0; } }

.shake { animation: shake .4s; }
@keyframes shake {
  0%,100% { transform: translateX(0); } 20% { transform: translateX(-6px); }
  40% { transform: translateX(6px); } 60% { transform: translateX(-4px); } 80% { transform: translateX(4px); }
}

/* --- Trade / result modal ----------------------------------------------- */

.modal-backdrop {
  position: fixed; inset: 0; background: rgba(0,0,0,.7); z-index: 60;
  display: flex; align-items: center; justify-content: center;
  animation: fadeIn .3s;
}
@keyframes fadeIn { from { opacity: 0; } }
.modal {
  background: var(--panel); border-radius: 14px; padding: 1.5rem;
  max-width: 520px; width: 92vw; text-align: center; max-height: 90vh; overflow-y: auto;
  box-shadow: 0 30px 80px rgba(0,0,0,.8);
}
.modal h2 { color: var(--gold); margin: 0 0 .4rem; }
.modal .card-grid { justify-content: center; margin: 1rem 0; }
.modal button {
  background: var(--accent); border: none; color: #fff; border-radius: 8px;
  padding: .6rem 1.2rem; cursor: pointer; font-weight: 600; font-size: 1rem; margin-top: .5rem;
}
.modal .won-label { color: var(--gold); font-weight: 700; margin-top: .5rem; }

/* --- Small screens --------------------------------------------------- */

@media (max-width: 480px) {
  :root { --card-w: 66; --card-h: 80; }
  .topbar { padding: .4rem .7rem; font-size: .8rem; gap: .5rem; }
  .topbar button { padding: .25rem .5rem; font-size: .75rem; }
  .match-status { font-size: .8rem; padding: .4rem .7rem; }
  .opp-hand { --card-w: 32; --card-h: 39; padding: .4rem; }
  .board { gap: 5px; padding: 8px; width: min(96vw, calc(100dvh - 290px)); }
  .hand-row { gap: .4rem; padding: .6rem .5rem .8rem; min-height: calc(var(--card-h) * 1px + 1.6rem); }
  .card-stats { font-size: 9px; bottom: 3px; padding: 1px 4px; }
  .arrow-N, .arrow-S { border-left-width: 5px; border-right-width: 5px; }
  .arrow-N { border-bottom-width: 7px; } .arrow-S { border-top-width: 7px; }
  .arrow-W, .arrow-E { border-top-width: 5px; border-bottom-width: 5px; }
  .arrow-W { border-right-width: 7px; } .arrow-E { border-left-width: 7px; }
  .arrow-NE, .arrow-NW { border-top-width: 9px; } .arrow-SE, .arrow-SW { border-bottom-width: 9px; }
  .arrow-NE, .arrow-SE { border-left-width: 9px; } .arrow-NW, .arrow-SW { border-right-width: 9px; }
  .battle-num { font-size: 22px; padding: 4px 7px; }
  .setup { padding: .6rem; }
  .slots { gap: .35rem; padding: .5rem; }
  .collection { gap: .45rem; }
  .coll-name { font-size: .62rem; max-width: 66px; }
  .list-row { padding: .55rem .7rem; }
  .modal { padding: 1rem; }
}
@media (max-height: 700px) and (max-width: 480px) {
  .opp-hand { display: none; }
  .board { width: min(96vw, calc(100dvh - 240px)); }
}
