:root {
  /* Viewport units corrected for the UI-size zoom (Settings), so layouts still fit the screen */
  --zdvh: calc(1dvh / var(--ui-zoom, 1));
  --zvh: calc(1vh / var(--ui-zoom, 1));
  --zvw: calc(1vw / var(--ui-zoom, 1));
  --bg: #12101a;
  --panel: #1d1a29;
  --text: #f1ece2;
  --muted: #a39cb3;
  --frame: #c9a861;
  --frame-dark: #6e5627;
  --atk: #e0533d;
  --hp: #3fae5a;
  --card-w: 180px;
}

* { box-sizing: border-box; }
body {
  margin: 0;
  background: radial-gradient(ellipse at top, #2a2140, var(--bg) 70%);
  color: var(--text);
  font-family: "Segoe UI", Tahoma, sans-serif;
  min-height: calc(100 * var(--zvh));
}
button {
  font: inherit;
  cursor: pointer;
  border: 1px solid var(--frame);
  background: var(--panel);
  color: var(--text);
  padding: 6px 14px;
  border-radius: 6px;
}
button:hover { background: #2c2740; }
button:disabled { opacity: .4; cursor: default; }

.topbar {
  display: flex; align-items: center; justify-content: space-between;
  padding: 10px 16px; border-bottom: 1px solid #2f2945;
}
.topbar h1 { margin: 0; font-size: 20px; letter-spacing: 1px; color: var(--frame); }
.topbar nav { display: flex; gap: 8px; }
.topbar nav button.active { background: var(--frame); color: #1a1408; }

main { padding: 12px 16px 40px; max-width: 1100px; margin: 0 auto; }

/* ---------- Heroes ---------- */
.hero {
  display: flex; align-items: center; gap: 14px; justify-content: center;
  padding: 6px;
}
.hero-name { font-weight: 600; }
.hero-hp {
  background: var(--hp); color: #fff; font-weight: 700;
  padding: 2px 12px; border-radius: 999px; min-width: 52px; text-align: center;
}
.hero-hp::before { content: "♥ "; }
.hero-deck { color: var(--muted); font-size: 13px; }

/* ---------- Board (4 slots per side) ---------- */
.board {
  background:
    linear-gradient(rgba(10,8,16,.55), rgba(10,8,16,.55)),
    url("assets/board/board.png") center/cover,
    linear-gradient(160deg, #2b3a2a, #1b2620 60%, #14201b);
  border: 3px solid var(--frame-dark);
  border-radius: 16px;
  padding: 14px;
  box-shadow: inset 0 0 40px rgba(0,0,0,.6);
}
.row {
  display: grid;
  --grave-w: calc(var(--card-w) * .8);
  grid-template-columns: repeat(4, var(--card-w)) var(--grave-w);
  justify-content: center;
  align-items: center;
  gap: 14px;
}
.slot {
  width: var(--card-w);
  aspect-ratio: 5 / 7;
  border: 2px dashed rgba(201,168,97,.35);
  border-radius: 12px;
  display: flex; align-items: center; justify-content: center;
  color: rgba(255,255,255,.25); font-size: 13px;
  position: relative;
}
.slot.droppable { border-color: var(--frame); background: rgba(201,168,97,.12); cursor: pointer; }
/* Graveyard pile (5th column of each row) */
.slot.grave {
  --card-w: var(--grave-w);
  width: var(--card-w);
  border: 2px solid rgba(120,110,140,.35);
  background: repeating-linear-gradient(45deg, rgba(255,255,255,.03) 0 6px, transparent 6px 12px), rgba(0,0,0,.35);
  margin-left: 10px;
}
.slot.grave .card { filter: grayscale(.85) brightness(.6); cursor: pointer; }
.slot.grave .card:hover { filter: grayscale(.5) brightness(.8); }
.slot.grave .tomb { font-size: 34px; color: rgba(255,255,255,.18); }
.grave-label {
  position: absolute; bottom: -9px; left: 50%; transform: translateX(-50%);
  background: #2a2435; border: 1px solid #5d5470; color: #d8d0e8;
  font-size: 11px; padding: 1px 8px; border-radius: 999px; white-space: nowrap; z-index: 5;
}
.grave-view { font-size: 18px; overflow: auto; padding: 30px 16px; justify-content: flex-start; }
.grave-title { font-size: 22px; color: var(--frame); }
.grave-list { display: flex; flex-wrap: wrap; gap: 18px; justify-content: center; --card-w: 170px; }

.divider { text-align: center; margin: 10px 0; color: var(--frame); font-weight: 600; }
.divider span { background: rgba(0,0,0,.5); padding: 3px 14px; border-radius: 999px; }

/* Hand */
.hint { text-align: center; color: var(--muted); font-size: 13px; margin: 10px 0 6px; }
.hand {
  display: flex; justify-content: center; flex-wrap: wrap; gap: 10px;
  --card-w: 150px;
}
.hand .card { cursor: pointer; }
.hand .card:hover { transform: translateY(-8px); }
.hand .card.selected { transform: translateY(-14px); box-shadow: 0 0 0 3px #fff, 0 0 22px var(--glow); }
.hand.locked .card { opacity: .55; cursor: default; }
/* Card in hand that would combo with your board */
.hand .card.synergy { box-shadow: 0 0 0 .12em #8dff9a, 0 0 1.2em rgba(120,255,150,.7); }
.hand .card.synergy::after {
  content: "SYNERGY"; position: absolute; top: -1.6em; left: 50%; transform: translateX(-50%);
  background: #1f6b2c; color: #c9ffd2; font: 700 .7em "Cinzel", serif; letter-spacing: .1em;
  padding: .15em .7em; border-radius: 999px; border: 1px solid #8dff9a; z-index: 6; white-space: nowrap;
}

.card.attacking { animation: lunge .35s ease; }
.card.hit { animation: shake .35s ease; }
@keyframes lunge { 50% { transform: translateY(var(--lunge, -18px)) scale(1.05); } }
@keyframes shake { 25% { transform: translateX(-5px); } 75% { transform: translateX(5px); } }

.log {
  margin: 16px auto 0; max-width: 640px; max-height: 160px; overflow: auto;
  background: var(--panel); border-radius: 8px; padding: 8px 8px 8px 30px;
  font-size: 13px; color: var(--muted);
}

.overlay {
  position: fixed; inset: 0; background: rgba(0,0,0,.75);
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 16px;
  font-size: 32px; font-weight: 700; z-index: 10;
}

/* Gallery */
.gallery {
  display: grid; gap: 18px; justify-content: center;
  grid-template-columns: repeat(auto-fill, 220px);
  --card-w: 220px;
}

@media (max-width: 600px), (max-height: 520px) {
  :root { --card-w: calc(18 * var(--zvw)); }
  .board { padding: 6px; border-width: 2px; }
  .row { --grave-w: calc(var(--card-w) * .5); }
  .slot.grave .tomb { font-size: 20px; }
  .grave-label { font-size: 9px; padding: 1px 5px; }
  /* Board cards are tiny on phones: hide the rules text and make ATK / HP readable. Tap a card to zoom. */
  .board .card .effect, .board .card .rarity-label, .board .card .sockets { display: none; }
  .board .card .stat { width: max(3.5em, 26px); height: max(3.5em, 26px); }
  .board .card .stat span { font-size: max(1.35em, 12px); }
  .board .card .status { font-size: max(.9em, 9px); }
  /* Hand: one swipeable row */
  .battle .hand { --card-w: calc(31 * var(--zvw)); flex-wrap: nowrap; overflow-x: auto; justify-content: flex-start;
    padding: 20px 4px 8px; scroll-snap-type: x mandatory; -webkit-overflow-scrolling: touch; }
  .battle .hand .card { flex: none; scroll-snap-align: start; }
  .battle .hint { display: none; }
  .row { gap: 6px; }
  .slot.grave { margin-left: 2px; }
  .hand { --card-w: calc(28 * var(--zvw)); }
  .gallery { grid-template-columns: repeat(auto-fill, calc(44 * var(--zvw))); --card-w: calc(44 * var(--zvw)); }
  main { padding: 10px 16px 30px; }
}

/* Short screens: shrink the battle so the board and hand fit without scrolling */
/* Battle scales with the screen height so it never needs scrolling */
/* Width limit: 4 slots + graveyard (0.8) + gaps must fit across the screen too (iPad portrait). */
.battle { --card-w: clamp(70px, min(calc((calc(100 * var(--zdvh)) - 290px) / 4.1), calc((calc(100 * var(--zvw)) - 130px) / 4.8)), 180px); padding-bottom: 8px; }
.battle .hand { --card-w: clamp(70px, min(calc((calc(100 * var(--zdvh)) - 290px) / 4.6), calc((calc(100 * var(--zvw)) - 60px) / 5.6)), 150px); }
/* Hand is always one row: centred when it fits, swipe sideways when it doesn't */
.battle .hand { flex-wrap: nowrap; overflow: auto hidden; scrollbar-width: thin; justify-content: flex-start; padding-top: 18px; }
.battle .hand .card { flex: none; }
.battle .hand .card:first-child { margin-left: auto; }
.battle .hand .card:last-child { margin-right: auto; }
.battle .hero { padding: 2px; }
.battle .hint { margin: 4px 0; }

/* Zoomed card (tap a board card / long-press a hand card) */
.inspect-overlay { z-index: 25; }
.inspect-overlay .modal-box { background: none; border: 0; box-shadow: none; display: flex; flex-direction: column; align-items: center; }
.inspect { --card-w: min(280px, calc(72 * var(--zvw))); display: flex; justify-content: center; padding-top: 14px; }
.board .slot .card { cursor: zoom-in; }

/* The log is only shown through the 📜 popup */
.battle > .log { display: none; }
.log.log-popup { display: block; max-height: calc(60 * var(--zvh)); margin: 10px 0 14px; }
#show-log { padding: 6px 10px; }

/* ---------- Phone in landscape: board on the left, heroes + hand on the right ---------- */
@media (orientation: landscape) and (max-height: 520px) {
  body[data-view="game"] .topbar { padding: 3px 10px; }
  body[data-view="game"] .topbar .logo { font-size: 14px; }
  body[data-view="game"] .pill { padding: 2px 8px; font-size: 12px; }
  .battle {
    max-width: none; padding: 6px 10px;
    display: grid; gap: 4px 12px;
    grid-template-columns: auto minmax(0, 1fr);
    grid-template-rows: auto auto minmax(0, 1fr);
    grid-template-areas: "board enemy" "board player" "board hand";
    --card-w: calc((calc(100 * var(--zdvh)) - 120px) / 2.9);
  }
  .battle .board { grid-area: board; padding: 5px; align-self: start; }
  .battle .row { gap: 5px; --grave-w: calc(var(--card-w) * .5); }
  .battle .divider { margin: 3px 0; font-size: 11px; }
  .battle .hero.enemy  { grid-area: enemy; justify-content: flex-start; flex-wrap: wrap; gap: 6px 10px; }
  .battle .hero.player { grid-area: player; justify-content: flex-start; flex-wrap: wrap; gap: 6px 10px; }
  .battle .hero-name { font-size: 13px; }
  .battle .hint { display: none; }
  .battle .hand {
    grid-area: hand; align-self: end;
    --card-w: min(calc((calc(100 * var(--zdvh)) - 195px) / 1.45), calc(30 * var(--zvw)));
    flex-wrap: nowrap; overflow: auto hidden; justify-content: flex-start; scrollbar-width: none;
    padding: 18px 2px 4px; scroll-snap-type: x mandatory;
  }
  .battle .hand .card { flex: none; scroll-snap-align: start; }
  /* Board cards: same compact look as portrait phones */
  .board .card .effect, .board .card .rarity-label, .board .card .sockets { display: none; }
  .board .card .stat { width: max(3.5em, 24px); height: max(3.5em, 24px); }
  .board .card .stat span { font-size: max(1.35em, 11px); }
  .slot { font-size: 10px; }
  .grave-label { font-size: 9px; padding: 1px 5px; }
  .slot.grave .tomb { font-size: 18px; }
  body[data-view="game"] .dev-panel { left: 50%; right: auto; bottom: auto; top: 4px; flex-direction: column; }
  body[data-view="game"] .dev-toggle { width: 30px; height: 30px; font-size: 14px; }
}

/* Phone portrait: limited by width (4 slots + graveyard) and by height (board + hand) */
@media (max-width: 600px) and (orientation: portrait) {
  .battle { --card-w: min(calc(18 * var(--zvw)), calc((calc(100 * var(--zdvh)) - 330px) / 4.1)); }
  .battle .hand { --card-w: min(calc(31 * var(--zvw)), calc((calc(100 * var(--zdvh)) - 410px) / 1.4)); }
}

/* Tablet portrait (iPad): use the extra height — bigger hand, battle centred vertically */
@media (orientation: portrait) and (min-width: 601px) {
  .battle { display: flex; flex-direction: column; justify-content: center; }
  .battle .hand { --card-w: clamp(120px, calc(22 * var(--zvw)), 190px); }
}
