:root {
  --bg: #15110e;
  --panel: #231c16;
  --panel-2: #2e251d;
  --line: #43372b;
  --text: #f3eadf;
  --muted: #b5a797;
  --accent: #ffb547;
  --accent-2: #ff7b3a;
  --good: #5fd35f;
  --bad: #e8483a;
  --blue: #2f7fd8;
  --radius: 10px;
  --hud-h: 46px;
  --bottom-h: 104px;
  --header-h: 48px;
  color-scheme: dark;
}

* {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: system-ui, -apple-system, 'Segoe UI', Roboto, Ubuntu, sans-serif;
  -webkit-text-size-adjust: 100%;
}

a {
  color: var(--accent);
}

button {
  font: inherit;
  color: inherit;
  touch-action: manipulation;
}

[hidden] {
  display: none !important;
}

.site-header {
  height: var(--header-h);
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 16px;
  border-bottom: 1px solid var(--line);
}

.site-logo {
  font-weight: 800;
  font-size: 20px;
  text-decoration: none;
  color: var(--text);
  letter-spacing: 0.5px;
}

.site-logo span {
  color: var(--accent);
  font-weight: 600;
  font-size: 13px;
  margin-left: 6px;
  text-transform: uppercase;
  letter-spacing: 2px;
}

.site-header nav a {
  color: var(--muted);
  text-decoration: none;
  font-size: 14px;
}

.page {
  padding: 8px max(8px, env(safe-area-inset-right)) 40px max(8px, env(safe-area-inset-left));
}

/* ---------- Game layout ---------- */

.game-root {
  position: relative;
  margin: 0 auto;
  width: 100%;
  max-width: min(1400px, calc((100vh - var(--header-h) - var(--hud-h) - var(--bottom-h) - 24px) * 2.6667));
  max-width: min(1400px, calc((100dvh - var(--header-h) - var(--hud-h) - var(--bottom-h) - 24px) * 2.6667));
  min-width: min(100%, 320px);
  background: #000;
  border-radius: var(--radius);
  overflow: hidden;
  border: 1px solid var(--line);
  user-select: none;
  -webkit-user-select: none;
  -webkit-tap-highlight-color: transparent;
}

.game-root.menu-mode {
  min-height: 420px;
}

.game-root:fullscreen {
  max-width: none;
  border: 0;
  border-radius: 0;
  display: flex;
  flex-direction: column;
  justify-content: center;
}

.stage {
  position: relative;
  width: 100%;
  aspect-ratio: 8 / 3;
}

.stage canvas {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  display: block;
  touch-action: manipulation;
}

.hud-top {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 6px 8px;
  min-height: var(--hud-h);
  background: var(--panel);
  border-bottom: 1px solid var(--line);
  flex-wrap: wrap;
}

.hud-gold {
  display: flex;
  align-items: center;
  gap: 6px;
  font-weight: 800;
  font-size: 20px;
  min-width: 92px;
  font-variant-numeric: tabular-nums;
}

.coin {
  width: 18px;
  height: 18px;
  border-radius: 50%;
  background: radial-gradient(circle at 35% 35%, #fff1b0, #ffcf33 40%, #c98f10);
  box-shadow: 0 0 0 2px #7a5608 inset;
  display: inline-block;
}

.hud-gold.pulse {
  animation: goldpulse 0.3s ease-out;
}

@keyframes goldpulse {
  0% {
    transform: scale(1.18);
    color: #ffe27a;
  }
  100% {
    transform: scale(1);
  }
}

.hud-xp {
  flex: 1 1 160px;
  min-width: 120px;
  max-width: 320px;
}

.xp-label {
  display: flex;
  justify-content: space-between;
  font-size: 12px;
  color: var(--muted);
  margin-bottom: 3px;
}

.xp-label span:first-child {
  color: var(--text);
  font-weight: 700;
}

.bar {
  height: 10px;
  background: #0d0a08;
  border-radius: 6px;
  overflow: hidden;
  border: 1px solid var(--line);
}

.bar-fill {
  height: 100%;
  width: 0%;
  background: linear-gradient(90deg, #7a5cff, #b58cff);
  transition: width 0.2s linear;
}

.bar-fill.full {
  background: linear-gradient(90deg, #ffb547, #ffe27a);
  animation: shimmer 1s linear infinite;
}

@keyframes shimmer {
  50% {
    filter: brightness(1.35);
  }
}

.hud-spacer {
  flex: 1;
}

.btn {
  background: var(--panel-2);
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 7px 12px;
  cursor: pointer;
  font-weight: 700;
  font-size: 14px;
  line-height: 1.1;
  position: relative;
  transition:
    transform 0.06s,
    background 0.15s,
    border-color 0.15s;
}

.btn:hover:not(:disabled) {
  background: #3a2f25;
  border-color: #6a5642;
}

.btn:active:not(:disabled) {
  transform: translateY(1px) scale(0.98);
}

.btn:disabled {
  opacity: 0.45;
  cursor: not-allowed;
}

.btn:focus-visible,
.tab:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

.btn-primary {
  background: linear-gradient(180deg, #ffbf5c, #f08a2c);
  color: #2a1606;
  border-color: #ffcf7a;
}

.btn-primary:hover:not(:disabled) {
  background: linear-gradient(180deg, #ffcf7a, #ff9a3a);
}

.btn-evolve:not(:disabled) {
  background: linear-gradient(180deg, #ffe27a, #ffb547);
  color: #2a1606;
  animation: glow 1s ease-in-out infinite;
}

@keyframes glow {
  50% {
    box-shadow: 0 0 16px 3px rgba(255, 200, 80, 0.8);
  }
}

.btn-special {
  overflow: hidden;
  min-width: 118px;
}

.btn-special .cd {
  position: absolute;
  left: 0;
  bottom: 0;
  height: 3px;
  background: var(--accent);
  width: 0%;
}

.btn-special:not(:disabled) {
  border-color: var(--accent-2);
  box-shadow: 0 0 10px rgba(255, 123, 58, 0.5);
}

.btn-stance.hold {
  background: #1d3550;
  border-color: var(--blue);
}

.btn-icon {
  min-width: 40px;
  padding: 7px 8px;
}

/* ---------- Bottom HUD ---------- */

.hud-bottom {
  background: var(--panel);
  border-top: 1px solid var(--line);
  padding: 6px 8px 8px;
  min-height: var(--bottom-h);
}

.tabs {
  display: flex;
  gap: 4px;
  margin-bottom: 6px;
}

.tab {
  background: transparent;
  border: 1px solid transparent;
  border-radius: 6px;
  padding: 3px 12px;
  color: var(--muted);
  font-size: 13px;
  font-weight: 700;
  cursor: pointer;
}

.tab.active {
  color: var(--text);
  background: var(--panel-2);
  border-color: var(--line);
}

.panel {
  display: flex;
  gap: 10px;
  align-items: stretch;
  flex-wrap: wrap;
}

.unit-buttons {
  display: flex;
  gap: 6px;
  flex-wrap: wrap;
}

.ubtn {
  display: flex;
  align-items: center;
  gap: 6px;
  min-width: 150px;
  padding: 4px 10px 4px 4px;
  text-align: left;
}

.ubtn canvas {
  width: 44px;
  height: 44px;
  background: #1a1511;
  border-radius: 6px;
  flex: none;
}

.ubtn .meta {
  display: flex;
  flex-direction: column;
  gap: 2px;
  min-width: 0;
}

.ubtn .name {
  font-size: 13px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.ubtn .cost {
  font-size: 13px;
  color: #ffd966;
  font-variant-numeric: tabular-nums;
}

.ubtn .cost.short {
  color: #ff8a7a;
}

.ubtn .key {
  position: absolute;
  top: 3px;
  right: 5px;
  font-size: 10px;
  color: var(--muted);
}

.ubtn .sub {
  font-size: 11px;
  color: var(--muted);
  font-weight: 500;
}

.queue {
  display: flex;
  gap: 4px;
  align-items: center;
  padding-left: 6px;
  border-left: 1px solid var(--line);
}

.qslot {
  width: 34px;
  height: 34px;
  border-radius: 6px;
  background: #1a1511;
  border: 1px dashed var(--line);
  position: relative;
  overflow: hidden;
  padding: 0;
  cursor: pointer;
}

.qslot canvas {
  width: 100%;
  height: 100%;
}

.qslot .prog {
  position: absolute;
  left: 0;
  bottom: 0;
  height: 4px;
  background: var(--good);
}

.slots {
  display: flex;
  gap: 6px;
  flex-wrap: nowrap;
  align-items: center;
  min-width: 0;
  max-width: 100%;
  overflow-x: auto;
  scrollbar-width: thin;
}

.slot {
  font-size: 12px;
  padding: 6px 8px;
  border-radius: 8px;
  background: #1a1511;
  border: 1px solid var(--line);
  display: flex;
  gap: 6px;
  align-items: center;
  min-height: 36px;
  flex: none;
  white-space: nowrap;
}

.slot .btn {
  padding: 4px 8px;
  font-size: 12px;
}

/* ---------- Overlay / menus ---------- */

.overlay {
  position: absolute;
  inset: 0;
  display: none;
  align-items: center;
  justify-content: center;
  background: radial-gradient(ellipse at center, rgba(20, 14, 10, 0.86), rgba(10, 7, 5, 0.96));
  z-index: 5;
  overflow: auto;
  padding: 16px;
}

.overlay.show {
  display: flex;
}

.menu {
  width: min(760px, 100%);
  max-height: 100%;
  margin: auto;
}

.menu h2 {
  margin: 0 0 12px;
  font-size: 26px;
}

.title {
  text-align: center;
}

.logo {
  font-size: clamp(34px, 7vw, 64px);
  font-weight: 900;
  letter-spacing: 2px;
  margin: 0;
  background: linear-gradient(180deg, #ffe7a3, #ff9a3a 60%, #c2501c);
  -webkit-background-clip: text;
  background-clip: text;
  color: transparent;
  text-shadow: 0 4px 24px rgba(255, 140, 40, 0.25);
}

.tagline {
  color: var(--muted);
  margin: 4px 0 18px;
}

.menu-buttons {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(170px, 1fr));
  gap: 10px;
  margin: 12px 0;
}

.menu-buttons .btn {
  padding: 12px 14px;
  font-size: 16px;
}

.menu-buttons .btn small {
  display: block;
  font-weight: 500;
  font-size: 12px;
  color: inherit;
  opacity: 0.8;
  margin-top: 3px;
}

.stat-row {
  display: flex;
  gap: 16px;
  justify-content: center;
  flex-wrap: wrap;
  color: var(--muted);
  font-size: 14px;
}

.stat-row b {
  color: var(--text);
}

.goal {
  margin: 14px auto 0;
  max-width: 420px;
  text-align: left;
  font-size: 13px;
  color: var(--muted);
}

.goal .bar {
  height: 12px;
  margin-top: 4px;
}

.goal .bar-fill {
  background: linear-gradient(90deg, #6fd0ff, #b0f0ff);
}

.cards {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
  gap: 10px;
}

.card {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 12px;
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.card h3 {
  margin: 0;
  font-size: 16px;
}

.card p {
  margin: 0;
  color: var(--muted);
  font-size: 13px;
}

.card.locked {
  opacity: 0.55;
}

.card.done {
  border-color: #6b8f3a;
}

.pips {
  display: flex;
  gap: 3px;
}

.pip {
  width: 12px;
  height: 6px;
  border-radius: 2px;
  background: #3a3027;
}

.pip.on {
  background: var(--accent);
}

.row {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 8px;
  flex-wrap: wrap;
}

.relics {
  color: #8fe3ff;
  font-weight: 800;
  font-variant-numeric: tabular-nums;
}

.relic-icon {
  display: inline-block;
  width: 12px;
  height: 12px;
  transform: rotate(45deg);
  background: linear-gradient(135deg, #d8f7ff, #4cc4ff);
  margin-right: 5px;
  border-radius: 2px;
}

.back {
  margin-top: 14px;
}

.results {
  text-align: center;
}

.results .big {
  font-size: clamp(36px, 6vw, 58px);
  font-weight: 900;
  margin: 0;
}

.results .big.win {
  color: #ffd966;
}

.results .big.lose {
  color: #ff8a7a;
}

.nearmiss {
  color: #ffcf7a;
  font-weight: 700;
  margin: 6px 0;
}

.results .grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(120px, 1fr));
  gap: 8px;
  margin: 14px 0;
}

.results .grid div {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 8px;
  font-size: 12px;
  color: var(--muted);
}

.results .grid b {
  display: block;
  font-size: 20px;
  color: var(--text);
}

.relic-gain {
  font-size: 28px;
  margin: 8px 0;
}

.ach-pop {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  background: #2b3a1f;
  border: 1px solid #6b8f3a;
  border-radius: 8px;
  padding: 6px 10px;
  margin: 4px;
  font-size: 13px;
  animation: popin 0.4s ease-out both;
}

/* Laid out from the start but hidden until revealed (see showResults). */
.ach-pop.waiting {
  visibility: hidden;
  animation: none;
}

@keyframes popin {
  0% {
    transform: scale(0.6);
    opacity: 0;
  }
  70% {
    transform: scale(1.08);
    opacity: 1;
  }
  100% {
    transform: scale(1);
  }
}

.diff-card .btn {
  margin-top: auto;
}

.stepper {
  display: flex;
  align-items: center;
  gap: 6px;
}

.stepper .btn {
  padding: 4px 10px;
}

.daily-mod {
  background: #2a2140;
  border: 1px solid #5a4a8a;
  border-radius: 8px;
  padding: 8px 10px;
  margin: 8px 0;
  font-size: 14px;
}

.toast {
  position: absolute;
  left: 50%;
  top: 12%;
  transform: translateX(-50%);
  background: rgba(20, 14, 10, 0.92);
  border: 1px solid var(--accent);
  border-radius: 10px;
  padding: 8px 14px;
  font-size: 14px;
  max-width: min(90%, 520px);
  text-align: center;
  pointer-events: none;
  opacity: 0;
  transition: opacity 0.25s;
  z-index: 3;
}

.toast.show {
  opacity: 1;
}

.rotate-hint {
  display: none;
  color: var(--muted);
  font-size: 13px;
  text-align: center;
  padding: 6px;
}

/* ---------- Ads & page ---------- */

.ad-slot {
  max-width: 970px;
  margin: 12px auto 0;
  min-height: 0;
}

.ad-slot[data-filled='true'] {
  min-height: 90px;
}

/* Room for the house ad (site.css .am-house: 72px thumbnail + padding) before it
   fills, so the results buttons above it do not jump when it appears. */
.ad-results {
  min-height: 94px;
}

.about {
  max-width: 820px;
  margin: 24px auto 0;
  color: var(--muted);
  line-height: 1.55;
  font-size: 15px;
}

.about h1,
.about h2 {
  color: var(--text);
}

kbd {
  background: var(--panel-2);
  border: 1px solid var(--line);
  border-radius: 4px;
  padding: 0 5px;
  font-size: 12px;
  color: var(--text);
}

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

@media (max-width: 720px) {
  :root {
    --bottom-h: 92px;
  }
  .hud-gold {
    font-size: 17px;
    min-width: 70px;
  }
  .btn {
    padding: 6px 8px;
    font-size: 13px;
  }
  .btn-special {
    min-width: 0;
  }
  .ubtn {
    min-width: 0;
    flex: 1 1 30%;
    padding-right: 6px;
  }
  .ubtn canvas {
    width: 36px;
    height: 36px;
  }
  .ubtn .sub {
    display: none;
  }
  .ubtn[data-forge] .sub,
  .ubtn[data-type] .sub {
    display: block;
    font-size: 10px;
  }
  .ubtn .key {
    display: none;
  }
  .unit-buttons {
    flex: 1 1 100%;
  }
  .ubtn,
  .btn-icon,
  .hud-top .btn {
    min-height: 40px;
  }
  .tab {
    padding: 6px 12px;
  }
  .queue {
    border-left: 0;
    padding-left: 0;
  }
  .hud-xp {
    order: 10;
    flex: 1 1 100%;
    max-width: none;
  }
}

@media (orientation: portrait) and (max-width: 720px) {
  .rotate-hint {
    display: block;
  }
}

@media (max-width: 420px) {
  .ubtn {
    gap: 3px;
    padding-left: 2px;
  }
  .ubtn canvas {
    width: 30px;
    height: 30px;
  }
  .ubtn .name,
  .ubtn .cost {
    font-size: 11px;
  }
}

/* Short landscape screens (phones held sideways): hide the site header while
   playing and squeeze the HUD into single rows so the battlefield gets the space. */
@media (max-height: 540px) and (orientation: landscape) {
  :root {
    --hud-h: 36px;
    --bottom-h: 58px;
  }
  body.playing .site-header {
    display: none;
  }
  body.playing .page {
    padding: 0 env(safe-area-inset-right) 0 env(safe-area-inset-left);
  }
  /* A drag on the battlefield or HUD must not scroll the page away mid-battle. */
  body.playing .game-root {
    touch-action: none;
  }
  body.playing .game-root {
    max-width: min(1400px, calc((100vh - var(--hud-h) - var(--bottom-h) - 4px) * 2.6667));
    max-width: min(1400px, calc((100dvh - var(--hud-h) - var(--bottom-h) - 4px) * 2.6667));
    border-radius: 0;
    border-width: 0;
  }
  .hud-top {
    flex-wrap: nowrap;
    gap: 4px;
    padding: 3px 4px;
  }
  .hud-gold {
    font-size: 15px;
    min-width: 58px;
    gap: 4px;
  }
  .coin {
    width: 13px;
    height: 13px;
  }
  .hud-xp {
    order: 0;
    flex: 1 1 60px;
    min-width: 50px;
  }
  .xp-label span:last-child {
    display: none;
  }
  .xp-label {
    font-size: 10px;
    margin-bottom: 1px;
  }
  .bar {
    height: 7px;
  }
  .hud-top .btn {
    padding: 4px 6px;
    font-size: 12px;
  }
  .btn-icon {
    min-width: 30px;
  }
  .hud-spacer {
    display: none;
  }
  .hud-bottom {
    display: flex;
    gap: 6px;
    padding: 3px 4px;
    align-items: center;
    height: var(--bottom-h);
    min-height: 0;
    overflow: hidden;
  }
  /* Tabs become three tall, narrow buttons with vertical labels, so each one
     is a comfortable touch target without making the HUD taller. */
  .tabs {
    flex-direction: row;
    margin: 0;
    gap: 3px;
    align-self: stretch;
  }
  .tab {
    padding: 0;
    width: 30px;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 11px;
  }
  .tab span {
    display: inline-block;
    writing-mode: vertical-rl;
    transform: rotate(180deg);
    white-space: nowrap;
  }
  .panel {
    flex-wrap: nowrap;
    gap: 6px;
    flex: 1;
    min-width: 0;
  }
  .unit-buttons {
    flex-wrap: nowrap;
    flex: 1 1 auto;
  }
  .ubtn {
    min-width: 0;
    flex: 1 1 0;
    padding: 2px 6px 2px 2px;
  }
  .ubtn canvas {
    width: 30px;
    height: 30px;
  }
  .ubtn .name,
  .ubtn .cost {
    font-size: 11px;
  }
  .ubtn .sub,
  .ubtn .key {
    display: none;
  }
  .ubtn[data-forge] .sub,
  .ubtn[data-type] .sub {
    display: block;
    font-size: 9px;
  }
  .ubtn {
    min-height: 0;
  }
  .btn-icon,
  .hud-top .btn {
    min-height: 28px;
  }
  .slots {
    flex: 1 1 auto;
  }
  .qslot {
    width: 26px;
    height: 26px;
  }
  .slot {
    min-height: 0;
    padding: 3px 6px;
    font-size: 11px;
  }
  .toast {
    font-size: 12px;
    top: 4%;
    padding: 5px 10px;
  }
}

@media (max-width: 720px) {
  .toast {
    font-size: 12px;
    top: 4%;
    padding: 5px 10px;
  }
}

@media (prefers-reduced-motion: reduce) {
  .btn-evolve:not(:disabled),
  .bar-fill.full {
    animation: none;
  }
}

.game-root.menu-mode .play-hint {
  display: none !important;
}
