:root {
  --bg0: #070b12;
  --ice: #9fd4e8;
  --ice-dim: #6a9cb4;
  --gold: #f0d48a;
  --gold-deep: #a87830;
  --wood: #3a2818;
  --wood-deep: #1a120c;
  --wood-hi: #5c4030;
  --line: #8a6844;
  --text: #f6f0e4;
  --foam: #f3ead4;
  --muted: #b7c3cc;
  --danger: #e05050;
  --hud: rgba(16, 12, 8, 0.78);
  --shadow: 0 18px 50px rgba(0, 0, 0, 0.55);
  --pixel: "Press Start 2P", monospace;
  --sans: Manrope, "Segoe UI", sans-serif;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  height: 100dvh;
  overflow: hidden;
  font-family: var(--sans);
  color: var(--text);
  background: #05080c;
  overscroll-behavior: none;
}

button, input { font: inherit; }

[hidden] { display: none !important; }

#app {
  height: 100%;
  height: 100dvh;
  width: 100%;
  max-width: none;
  display: flex;
  flex-direction: column;
  background:
    radial-gradient(ellipse 90% 40% at 50% -10%, rgba(159, 212, 232, 0.16), transparent 55%),
    linear-gradient(180deg, #122033 0%, #070d14 42%, #090c10 100%);
}

/* ---------- thin tavern header ---------- */
.top {
  flex: 0 0 auto;
  display: flex;
  align-items: center;
  gap: 12px;
  min-height: 40px;
  padding: 6px 14px;
  padding-left: max(14px, env(safe-area-inset-left));
  padding-right: max(14px, env(safe-area-inset-right));
  color: var(--foam);
  background:
    repeating-linear-gradient(90deg, rgba(0, 0, 0, 0.18) 0 2px, transparent 2px 11px),
    linear-gradient(180deg, #4a3422 0%, #2a1c12 55%, #140e0a 100%);
  border-bottom: 3px solid var(--gold);
  box-shadow: inset 0 -4px 0 #5c3e1e, 0 6px 16px rgba(0, 0, 0, 0.35);
  z-index: 5;
}

.lab-back {
  color: var(--muted, #8aa0b8);
  text-decoration: none;
  font-size: 0.78rem;
  font-weight: 700;
  margin-right: 4px;
}
.lab-back:hover { color: var(--ice, #c8e4f8); }

.brand {
  display: flex;
  align-items: baseline;
  gap: 10px;
  flex-wrap: wrap;
  min-width: 0;
}

.brand strong {
  font-family: var(--pixel);
  font-size: clamp(0.48rem, 1.5vw, 0.62rem);
  letter-spacing: 0.02em;
  color: var(--gold);
  text-shadow: 2px 2px 0 #000;
}

.brand .sub {
  color: var(--ice);
  font-size: 0.82rem;
  font-weight: 700;
  white-space: nowrap;
}

.coins {
  margin-left: auto;
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 4px 8px;
  border: 2px solid var(--gold-deep);
  background: rgba(0, 0, 0, 0.28);
  font-family: var(--pixel);
  font-size: 0.55rem;
  color: var(--gold);
  white-space: nowrap;
}

/* ---------- screens ---------- */
.screen {
  flex: 1 1 auto;
  min-height: 0;
  width: 100%;
  max-width: none;
  overflow: auto;
  padding: 22px 18px 28px;
  padding-bottom: max(28px, env(safe-area-inset-bottom));
}

.screen:not(.run) {
  display: flex;
  flex-direction: column;
  justify-content: safe center;
  background:
    radial-gradient(ellipse 80% 36% at 50% 112%, rgba(159, 212, 232, 0.14), transparent 58%);
}

.screen.run {
  padding: 0;
  overflow: hidden;
  display: flex;
  flex-direction: column;
  background: #000;
}

.menu-stage {
  width: min(1080px, 100%);
  margin: 0 auto;
  display: grid;
  grid-template-columns: minmax(0, 1.15fr) minmax(260px, 0.85fr);
  gap: 18px;
  align-items: stretch;
}

.sheet {
  width: min(1100px, 100%);
  margin: 0 auto;
}

.sheet-head { margin-bottom: 14px; }

.panel,
.hero-panel,
.board,
.sheet {
  position: relative;
}

.hero-panel,
.board {
  background:
    linear-gradient(180deg, rgba(255, 226, 170, 0.06), transparent 28%),
    repeating-linear-gradient(90deg, #2c1e14 0 4px, #3d2a1c 4px 8px, #24180f 8px 13px);
  border: 3px solid var(--gold);
  box-shadow:
    inset 0 0 0 5px rgba(36, 22, 12, 0.9),
    inset 0 0 0 7px rgba(168, 120, 48, 0.85),
    var(--shadow);
  padding: 26px 24px 22px;
}

.hero-panel::before,
.hero-panel::after,
.board::before,
.board::after {
  content: "";
  position: absolute;
  width: 8px;
  height: 8px;
  background: var(--gold);
  transform: rotate(45deg);
}

.hero-panel::before, .board::before { top: 10px; left: 10px; }
.hero-panel::after, .board::after { top: 10px; right: 10px; }

.ice-ornament {
  height: 4px;
  margin: 4px 12px 16px;
  background: linear-gradient(90deg, transparent, var(--ice), var(--gold), var(--ice), transparent);
  box-shadow: 0 0 14px rgba(159, 212, 232, 0.75);
}

.eyebrow {
  font-family: var(--pixel);
  font-size: 0.56rem;
  line-height: 1.7;
  color: var(--ice);
  margin: 0 0 10px;
}

h1 {
  font-family: var(--pixel);
  font-size: clamp(1.05rem, 3.2vw, 1.55rem);
  line-height: 1.5;
  margin: 0 0 14px;
  color: #ffe7a8;
  text-shadow: 0 2px 0 #6a4010, 4px 5px 0 #000;
}

h2 {
  font-family: var(--pixel);
  font-size: 0.72rem;
  line-height: 1.6;
  color: var(--gold);
  margin: 0 0 8px;
  text-shadow: 2px 2px 0 #000;
}

.lede, .note, p { color: var(--muted); line-height: 1.5; }

.hero-panel .lede { color: var(--foam); font-size: 1.02rem; max-width: 38ch; }

.note { font-size: 0.92rem; margin: 14px 0 0; }

.stack {
  display: flex;
  flex-direction: column;
  gap: 10px;
  max-width: 320px;
  margin-top: 18px;
}

.row {
  display: flex;
  gap: 10px;
  flex-wrap: wrap;
  margin-top: 16px;
}

.keys {
  list-style: none;
  margin: 12px 0 0;
  padding: 0;
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.keys li {
  display: flex;
  align-items: center;
  gap: 10px;
  color: var(--foam);
  font-weight: 700;
}

.keyrow { display: inline-flex; gap: 4px; }

kbd {
  font-family: var(--pixel);
  font-size: 0.48rem;
  color: #1a1008;
  background: linear-gradient(180deg, #f3e2a8, #c9a24a);
  border: 2px solid #6a4010;
  padding: 5px 6px 4px;
  box-shadow: 0 2px 0 #3a2208;
}

.btn {
  border: 2px solid var(--line);
  background:
    linear-gradient(180deg, rgba(255, 220, 160, 0.08), transparent 40%),
    #2a1e14;
  color: var(--foam);
  border-radius: 2px;
  padding: 11px 14px;
  min-height: 44px;
  cursor: pointer;
  font-weight: 800;
  letter-spacing: 0.01em;
  box-shadow: inset 0 0 0 1px rgba(0, 0, 0, 0.35), 0 2px 0 #120c08;
}

.btn:hover { border-color: var(--gold); color: #fff; }

.btn:focus-visible,
.pick:focus-visible,
.card-btn:focus-visible {
  outline: 2px solid var(--ice);
  outline-offset: 2px;
}

.btn.primary {
  background: linear-gradient(180deg, #ffe7a8 0%, #e0b04a 42%, #8a5820 100%);
  color: #1a1008;
  border-color: #fff1c4;
  font-family: var(--pixel);
  font-size: 0.58rem;
  line-height: 1.45;
  padding: 14px 16px;
  text-shadow: 0 1px 0 rgba(255, 255, 255, 0.35);
}

.btn.primary:hover { filter: brightness(1.06); }

.btn.tiny {
  min-height: 36px;
  padding: 6px 10px;
  font-size: 0.78rem;
  background: rgba(28, 20, 12, 0.92);
}

.btn:disabled { opacity: 0.42; cursor: not-allowed; filter: grayscale(0.3); }

.meta-stats {
  grid-column: 1 / -1;
  padding: 12px 14px;
  margin-bottom: 4px;
  background: rgba(12, 16, 22, 0.55);
  border: 1px solid rgba(240, 212, 138, 0.35);
}
.meta-stats p { margin: 0 0 6px; }
.meta-stats .note { color: var(--muted); font-size: 0.86rem; }
.meta-group {
  grid-column: 1 / -1;
  margin: 10px 0 0;
  font-family: var(--pixel);
  font-size: 0.55rem;
  color: var(--gold);
  letter-spacing: 0.04em;
}

.grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(230px, 1fr));
  gap: 12px;
}

.pick,
.card-btn {
  text-align: left;
  width: 100%;
  border: 2px solid var(--line);
  background:
    linear-gradient(180deg, rgba(159, 212, 232, 0.08), transparent 34%),
    linear-gradient(180deg, #2a2016, #120e0a);
  color: inherit;
  border-radius: 2px;
  padding: 12px;
  cursor: pointer;
  box-shadow: inset 0 0 0 1px rgba(240, 212, 138, 0.18), 0 8px 18px rgba(0, 0, 0, 0.28);
}

.pick.selected,
.pick:hover,
.card-btn:hover {
  border-color: var(--gold);
  box-shadow:
    0 0 0 1px var(--gold),
    inset 0 0 24px rgba(159, 212, 232, 0.12),
    0 10px 22px rgba(0, 0, 0, 0.35);
}

.pick h3, .card-btn h3 {
  margin: 0 0 6px;
  color: var(--foam);
  font-size: 1.05rem;
}

.pick p, .card-btn p { margin: 0; font-size: 0.9rem; }

.tag {
  display: inline-block;
  margin-top: 8px;
  font-size: 0.78rem;
  font-weight: 800;
  letter-spacing: 0.03em;
  line-height: 1.35;
  color: var(--ice);
}

.portrait-frame {
  display: grid;
  place-items: center;
  height: 108px;
  margin: -4px -4px 10px;
  background:
    radial-gradient(ellipse at 50% 80%, color-mix(in srgb, var(--c, #c8a060) 70%, transparent), transparent 62%),
    linear-gradient(180deg, #163044, #071018);
  border: 2px solid var(--ice-dim);
  box-shadow: inset 0 0 0 1px rgba(0, 0, 0, 0.45);
}

.portrait {
  image-rendering: pixelated;
  display: block;
  width: 72px;
  height: 80px;
  object-fit: contain;
  background: transparent;
}

/* ---------- full-bleed run ---------- */
.stage {
  position: relative;
  flex: 1 1 auto;
  min-height: 0;
  width: 100%;
  height: 100%;
  max-width: none;
  background: #000;
  overflow: hidden;
  container-type: size;
}

/* Element box IS the painted canvas, so pointer mapping in game.js stays true.
   Container query keeps 16:9 and fills the stage on the limiting side.
   Width/height 100% is the fallback and the portrait-phone fill. */
#game {
  position: absolute;
  inset: 0;
  display: block;
  width: 100%;
  height: 100%;
  max-width: none;
  max-height: none;
  background: #0a1018;
  image-rendering: pixelated;
  cursor: crosshair;
  touch-action: none;
}

/* Integer CSS scale is applied from main.js (fitCanvas). Fallback keep 16:9. */
@supports (width: 1cqw) {
  #game:not(.fit-int) {
    inset: auto;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    width: min(100cqw, calc(100cqh * 16 / 9));
    height: min(100cqh, calc(100cqw * 9 / 16));
  }
}

#game.fit-int {
  inset: auto;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  max-width: none;
  max-height: none;
}

.stage::after {
  content: "";
  position: absolute;
  inset: 0;
  pointer-events: none;
  z-index: 1;
  background:
    linear-gradient(180deg, rgba(0, 0, 0, 0.5) 0%, transparent 16%, transparent 78%, rgba(0, 0, 0, 0.55) 100%);
}

.hud-overlay {
  position: absolute;
  inset: 0;
  z-index: 2;
  pointer-events: none;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  gap: 8px;
  padding:
    max(8px, env(safe-area-inset-top))
    max(10px, env(safe-area-inset-right))
    max(8px, env(safe-area-inset-bottom))
    max(10px, env(safe-area-inset-left));
}

.hud-overlay .btn,
#weapons { pointer-events: auto; }

.hud-top {
  display: grid;
  grid-template-columns: minmax(190px, 280px) minmax(120px, 1fr) auto;
  gap: 10px 14px;
  align-items: center;
  padding: 8px 10px;
  background:
    linear-gradient(180deg, rgba(74, 52, 34, 0.55), rgba(12, 10, 8, 0.2) 28%, transparent 28%),
    var(--hud);
  border: 2px solid rgba(240, 212, 138, 0.78);
  box-shadow: inset 0 0 0 1px rgba(92, 62, 30, 0.8), 0 10px 28px rgba(0, 0, 0, 0.35);
  backdrop-filter: blur(3px);
}

.hud-left { min-width: 0; }

.bars { display: flex; flex-direction: column; gap: 4px; margin-top: 5px; }

.hp-text {
  font-size: 0.72rem;
  font-weight: 800;
  color: var(--foam);
  text-shadow: 1px 1px 0 #000;
  margin-top: 2px;
}

.hud-bottom .bar.xp {
  height: 14px;
}

.hud-mid {
  text-align: center;
  font-family: var(--pixel);
}

#hud-time {
  display: block;
  font-size: clamp(0.72rem, 2vw, 0.95rem);
  color: var(--gold);
  text-shadow: 2px 2px 0 #000;
}

#hud-lv, #hud-map {
  display: block;
  margin-top: 5px;
  font-size: 0.55rem;
  line-height: 1.55;
  color: var(--ice);
}

#hud-name {
  font-weight: 800;
  color: var(--foam);
  font-size: 0.98rem;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.hud-right {
  display: flex;
  gap: 8px;
  align-items: flex-end;
  justify-content: flex-end;
}

#hud-kills {
  align-self: center;
  font-size: 0.82rem;
  font-weight: 800;
  color: var(--foam);
  white-space: nowrap;
  padding: 4px 6px;
  background: rgba(0, 0, 0, 0.28);
  border: 1px solid rgba(240, 212, 138, 0.35);
}

.cd-stack {
  display: flex;
  flex-direction: column;
  gap: 3px;
  min-width: 64px;
}

.cd-name {
  font-family: var(--pixel);
  font-size: 0.48rem;
  color: var(--muted);
}

.cd-stack:has(#berserk[hidden]) { display: none; }

.bar {
  width: 100%;
  height: 12px;
  background: #05080c;
  border: 2px solid var(--line);
  overflow: hidden;
}

.bar i { display: block; height: 100%; width: 0%; }
.bar.hp i { width: 100%; background: linear-gradient(90deg, #6e2424, var(--danger)); }
.bar.xp i { background: linear-gradient(90deg, #2a628f, var(--ice)); }
.bar.dash, .bar.berserk { width: 72px; height: 10px; }
.bar.dash i { background: linear-gradient(90deg, #4a78a8, #d5ecff); }
.bar.berserk i { background: linear-gradient(90deg, #8a5820, var(--gold)); }
.bar.ready { border-color: var(--gold); box-shadow: 0 0 8px rgba(240, 212, 138, 0.65); }

.hud-bottom {
  display: flex;
  flex-direction: column;
  justify-content: flex-end;
  align-items: stretch;
  gap: 6px;
  min-width: 0;
  max-width: min(100%, 980px);
}

#weapons {
  width: 100%;
  padding: 7px 8px 8px;
  background: var(--hud);
  border: 2px solid rgba(240, 212, 138, 0.72);
  box-shadow: 0 10px 24px rgba(0, 0, 0, 0.35);
  backdrop-filter: blur(3px);
}

.hud-bottom .bar.xp {
  width: 100%;
  height: 10px;
  border: 2px solid rgba(240, 212, 138, 0.72);
  background: rgba(5, 8, 12, 0.88);
  box-shadow: 0 8px 18px rgba(0, 0, 0, 0.3);
}

.loadout-label {
  font-family: var(--pixel);
  font-size: 0.5rem;
  color: var(--gold);
  margin-bottom: 6px;
}

.loadout-row {
  display: flex;
  flex-wrap: nowrap;
  gap: 6px;
  align-items: stretch;
  overflow-x: auto;
  overscroll-behavior-x: contain;
  scrollbar-width: thin;
  max-width: 100%;
}

.loadout-split {
  flex: 0 0 2px;
  align-self: stretch;
  margin: 0 2px;
  background: linear-gradient(180deg, transparent, var(--gold), transparent);
}

.slot {
  position: relative;
  flex: 0 0 auto;
  width: 84px;
  min-height: 74px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
  padding: 4px 3px 3px;
  border: 2px solid var(--line);
  background: #12100c;
  box-shadow: inset 0 -3px 0 var(--c, transparent);
}

.slot.pas { border-color: #3d6274; }

.slot-icon {
  width: 36px;
  height: 36px;
  display: grid;
  place-items: center;
  background: radial-gradient(circle, rgba(255, 255, 255, 0.06), transparent 70%);
}

.slot img {
  width: 34px;
  height: 34px;
  image-rendering: pixelated;
  object-fit: contain;
}

.slot b {
  position: absolute;
  right: 2px;
  top: 2px;
  font-family: var(--pixel);
  font-size: 0.4rem;
  color: #1a1008;
  background: var(--gold);
  padding: 2px 3px 1px;
}

.slot em {
  font-style: normal;
  font-size: 0.68rem;
  font-weight: 800;
  line-height: 1.15;
  color: var(--foam);
  text-align: center;
  max-width: 100%;
  overflow: hidden;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
}

.slot-fallback {
  width: 34px;
  height: 34px;
  display: grid;
  place-items: center;
  font-family: var(--pixel);
  font-size: 0.62rem;
  color: var(--gold);
}

.muted-hint {
  color: var(--muted);
  font-size: 0.8rem;
  align-self: center;
  padding: 8px 4px;
}

/* ---------- level-up / result ---------- */
.overlay {
  position: fixed;
  inset: 0;
  z-index: 30;
  display: flex;
  align-items: center;
  justify-content: center;
  overflow: auto;
  padding: 16px;
  padding-top: max(16px, env(safe-area-inset-top));
  padding-bottom: max(16px, env(safe-area-inset-bottom));
  background:
    radial-gradient(ellipse at 50% 30%, rgba(159, 212, 232, 0.12), transparent 46%),
    rgba(4, 8, 12, 0.8);
}

.card.wood {
  width: min(860px, 100%);
  max-height: min(92dvh, 920px);
  overflow: auto;
  padding: 20px 18px 16px;
  background:
    linear-gradient(180deg, rgba(255, 226, 170, 0.07), transparent 22%),
    repeating-linear-gradient(90deg, #2a1c12 0 4px, #3c2a1c 4px 9px, #1c130c 9px 14px);
  border: 3px solid var(--gold);
  box-shadow:
    inset 0 0 0 5px rgba(28, 18, 10, 0.92),
    inset 0 0 0 7px rgba(168, 120, 48, 0.8),
    var(--shadow);
}

.card.wood h2 { font-size: 0.78rem; }

.overlay .grid { margin-top: 12px; }

.pick-card {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  min-height: 168px;
}

.pick-icon {
  width: 56px;
  height: 56px;
  margin-bottom: 8px;
  display: grid;
  place-items: center;
  border: 2px solid var(--line);
  background:
    radial-gradient(circle at 50% 40%, rgba(159, 212, 232, 0.2), transparent 70%),
    #0c1218;
  color: var(--gold);
  font-size: 1.4rem;
}

.pick-icon img {
  width: 44px;
  height: 44px;
  image-rendering: pixelated;
  object-fit: contain;
}

@media (max-aspect-ratio: 3/4) {
  #game {
    inset: 0;
    top: 0;
    left: 0;
    transform: none;
    width: 100%;
    height: 100%;
  }
}

@media (max-width: 860px) {
  .menu-stage { grid-template-columns: 1fr; }
  .brand .sub { display: none; }
  .hud-top {
    grid-template-columns: 1fr auto;
    grid-template-areas:
      "left right"
      "mid mid";
  }
  .hud-left { grid-area: left; }
  .hud-right { grid-area: right; }
  .hud-mid { grid-area: mid; }
  #hud-map { display: none; }
  .slot { width: 72px; min-height: 68px; }
}

@media (max-width: 560px) {
  .top { padding: 5px 8px; min-height: 34px; }
  .screen { padding: 14px 10px 18px; }
  .hero-panel, .board { padding: 18px 14px 14px; }
  h1 { font-size: 0.92rem; }
  .hud-overlay { gap: 6px; padding: 6px; }
  .hud-top {
    grid-template-columns: 1fr auto;
    grid-template-areas:
      "name name"
      "mid right";
    padding: 6px;
    gap: 6px;
    align-items: center;
  }
  .hud-left { display: contents; }
  #hud-name {
    grid-area: name;
    white-space: normal;
    font-size: 0.88rem;
  }
  .hud-mid { grid-area: mid; text-align: left; }
  .hud-right { grid-area: right; gap: 4px; }
  #hud-kills { font-size: 0.72rem; }
  .cd-name { display: none; }
  .bar.dash, .bar.berserk { width: 46px; }
  .slot { width: 64px; min-height: 58px; }
  .slot em { font-size: 0.6rem; -webkit-line-clamp: 1; }
  .pick-card { min-height: 0; }
}
