:root {
  --bg: #0b0f12;
  --panel: #141920;
  --panel-2: #1b222b;
  --line: #2a333f;
  --text: #e8edf2;
  --muted: #8d99a6;
  --accent: #5ec4b6;
  --gold: #ffd23f;
  --gold-dark: #8a6a00;
  --good: #5ee07a;
  --bad: #ff6b6b;
  --radius: 10px;
  color-scheme: dark;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font: 14px/1.35 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  -webkit-tap-highlight-color: transparent;
}

button { font: inherit; color: inherit; }

#app {
  display: grid;
  grid-template-columns: 210px minmax(0, 1fr) 330px;
  grid-template-rows: auto minmax(0, 1fr);
  grid-template-areas: "top top top" "stages board side";
  height: 100vh;
  height: 100dvh;
}

/* Header ---------------------------------------------------------------- */

.top {
  grid-area: top;
  display: flex;
  align-items: center;
  gap: 14px;
  padding: 8px 14px;
  background: var(--panel);
  border-bottom: 1px solid var(--line);
  min-width: 0;
}

.home {
  color: var(--muted);
  text-decoration: none;
  font-size: 20px;
  padding: 2px 6px;
  border-radius: 6px;
}
.home:hover { color: var(--text); background: var(--panel-2); }

.logo {
  margin: 0;
  font-size: 22px;
  font-weight: 800;
  letter-spacing: -0.5px;
}
.logo span { color: var(--accent); }

.wallet { margin-left: auto; text-align: right; line-height: 1.1; }
.money { font-size: 24px; font-weight: 800; font-variant-numeric: tabular-nums; }
.income { color: var(--good); font-size: 12px; font-variant-numeric: tabular-nums; }

.actions { display: flex; gap: 8px; align-items: center; }

.btn {
  position: relative;
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 8px 12px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--panel-2);
  cursor: pointer;
  white-space: nowrap;
  transition: background 0.15s, border-color 0.15s, transform 0.05s;
}
.btn:hover:not(:disabled) { background: #232c37; border-color: #3a4655; }
.btn:active:not(:disabled) { transform: translateY(1px); }
.btn:disabled { opacity: 0.45; cursor: default; }
.btn.small { padding: 5px 9px; font-size: 12px; }
.btn.icon { width: 36px; justify-content: center; padding: 8px 0; font-weight: 700; }
.btn.primary { background: #1f6f62; border-color: #2c9b89; }
.btn.primary:hover:not(:disabled) { background: #25836f; }
.btn.danger { background: #5a1f24; border-color: #8c2f37; }
.btn.gold { border-color: #6b5410; background: #2a2208; }
.btn.gold:hover:not(:disabled) { background: #3a2f0b; }
.btn.prestige { border-color: #6b5410; }
.btn.prestige b { color: var(--gold); }
.btn.prestige.ready { background: linear-gradient(180deg, #8a6a00, #5a4500); border-color: var(--gold); animation: glow 1.6s ease-in-out infinite; }
.btn.prestige.ready b { color: #fff6c8; }

@keyframes glow {
  0%, 100% { box-shadow: 0 0 0 0 rgba(255, 210, 63, 0.0); }
  50% { box-shadow: 0 0 14px 2px rgba(255, 210, 63, 0.35); }
}

.gb-dot {
  width: 14px;
  height: 14px;
  border-radius: 50%;
  background: radial-gradient(circle at 35% 35%, #fff6c8, var(--gold) 50%, var(--gold-dark));
  flex: none;
}
#gb { font-weight: 800; color: var(--gold); font-variant-numeric: tabular-nums; }

.badge {
  position: absolute;
  top: -4px;
  right: -4px;
  width: 10px;
  height: 10px;
  border-radius: 50%;
  background: var(--good);
  box-shadow: 0 0 6px var(--good);
}

/* Stage list ------------------------------------------------------------ */

.stages {
  grid-area: stages;
  display: flex;
  flex-direction: column;
  gap: 6px;
  padding: 10px;
  overflow-y: auto;
  background: var(--panel);
  border-right: 1px solid var(--line);
}

.stage {
  display: grid;
  grid-template-columns: 14px 1fr;
  gap: 2px 8px;
  align-items: center;
  width: 100%;
  padding: 8px 9px;
  text-align: left;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--panel-2);
  cursor: pointer;
}
.stage:hover:not(:disabled) { border-color: #3a4655; }
.stage.active { border-color: var(--accent); background: #18302d; }
.stage:disabled { cursor: default; opacity: 0.55; }
.stage .dot { width: 14px; height: 14px; border-radius: 50%; box-shadow: inset 0 0 0 2px rgba(255, 255, 255, 0.15); }
.stage .name { font-weight: 700; display: flex; justify-content: space-between; gap: 6px; }
.stage .name small { color: var(--muted); font-weight: 500; }
.stage .meta { grid-column: 2; color: var(--muted); font-size: 11px; }
.stage .bar { grid-column: 1 / -1; height: 4px; border-radius: 2px; background: #0d1116; overflow: hidden; margin-top: 3px; }
.stage .bar i { display: block; height: 100%; width: 0; background: var(--accent); }
.stage .bar.done i { background: var(--gold); }

/* Board ----------------------------------------------------------------- */

.board-wrap {
  grid-area: board;
  position: relative;
  min-height: 0;
  overflow: hidden;
}

#board {
  display: block;
  width: 100%;
  height: 100%;
  touch-action: none;
  cursor: crosshair;
}

.hud {
  position: absolute;
  left: 10px;
  top: 10px;
  padding: 7px 10px;
  border-radius: 8px;
  background: rgba(10, 13, 17, 0.72);
  border: 1px solid rgba(255, 255, 255, 0.07);
  font-size: 12px;
  pointer-events: none;
  max-width: calc(100% - 140px);
}
.hud b { font-size: 14px; }
.hud .row { color: var(--muted); }
.hud .row span { color: var(--text); }
.hud .row.fever, .hud .row.fever span { color: var(--gold); font-weight: 700; }
.hud .tags { display: flex; flex-wrap: wrap; gap: 4px; margin-top: 4px; }
.hud .tag { padding: 1px 6px; border-radius: 10px; background: #26303b; color: var(--text); font-size: 11px; }

.reset { position: absolute; right: 10px; top: 10px; opacity: 0.8; }

/* The Crunch button fills up like a meter and glows when ready. */
.crunch {
  position: absolute;
  right: 10px;
  bottom: 10px;
  width: 96px;
  height: 44px;
  overflow: hidden;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: rgba(10, 13, 17, 0.8);
  color: var(--muted);
  font: inherit;
  cursor: pointer;
  padding: 0;
}
.crunch .fill {
  position: absolute;
  inset: auto 0 0 0;
  height: 0;
  background: linear-gradient(180deg, rgba(255, 210, 63, 0.35), rgba(255, 140, 40, 0.45));
  transition: height 0.2s linear;
}
.crunch .label { position: relative; display: flex; flex-direction: column; align-items: center; line-height: 1.15; }
.crunch .label b { font-size: 14px; letter-spacing: 0.04em; }
.crunch .label small { font-size: 11px; font-variant-numeric: tabular-nums; }
.crunch.ready { color: #1a1300; border-color: var(--gold); animation: glow 1.2s ease-in-out infinite; }
.crunch.ready .fill { background: linear-gradient(180deg, #ffe27a, #ffb703); }
.crunch.auto small::after { content: ' · auto'; }

/* Side panel ------------------------------------------------------------ */

.side {
  grid-area: side;
  display: flex;
  flex-direction: column;
  min-height: 0;
  background: var(--panel);
  border-left: 1px solid var(--line);
}

.shop {
  display: flex;
  flex-direction: column;
  gap: 5px;
  padding: 10px;
  overflow-y: auto;
  flex: 1 1 auto;
  min-height: 0;
}

.tower-row {
  display: grid;
  grid-template-columns: 36px 1fr auto;
  align-items: center;
  gap: 9px;
  padding: 5px 6px 5px 6px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--panel-2);
  cursor: pointer;
  text-align: left;
  width: 100%;
}
.tower-row:hover { border-color: #3a4655; }
.tower-row.selected { border-color: var(--good); background: #183021; }
.tower-row.locked { opacity: 0.5; cursor: default; }
.tower-row .t-name { font-weight: 700; display: block; }
.tower-row .t-sub { font-size: 12px; color: var(--muted); font-variant-numeric: tabular-nums; }
.tower-row .t-sub .cant { color: var(--bad); }
.tower-row .t-sub .can { color: var(--good); }
.tree-btn {
  position: relative;
  padding: 6px 8px;
  border-radius: 7px;
  border: 1px solid var(--line);
  background: #11161c;
  font-size: 12px;
  cursor: pointer;
}
.tree-btn:hover { border-color: var(--accent); }
.tree-btn .badge { top: -3px; right: -3px; width: 8px; height: 8px; }

.info {
  flex: none;
  padding: 12px;
  border-top: 1px solid var(--line);
  min-height: 150px;
}
.info h3 { margin: 0 0 4px; font-size: 16px; display: flex; align-items: center; gap: 8px; }
.info p { margin: 0 0 8px; color: var(--muted); }
.info .buttons { display: flex; gap: 8px; flex-wrap: wrap; margin-top: 8px; }
.stats { display: grid; grid-template-columns: 1fr auto; gap: 2px 12px; font-size: 12px; margin: 6px 0; }
.stats dt { color: var(--muted); }
.stats dd { margin: 0; text-align: right; font-variant-numeric: tabular-nums; }
.tip { color: var(--muted); font-size: 12px; }
.tip kbd { background: #222a34; padding: 0 5px; border-radius: 4px; border: 1px solid var(--line); font-size: 11px; }

/* Modals ---------------------------------------------------------------- */

.modal-root:empty { display: none; }
.modal-root {
  position: fixed;
  inset: 0;
  z-index: 20;
  display: grid;
  place-items: center;
  background: rgba(3, 5, 8, 0.66);
  padding: 16px;
}

.modal {
  position: relative;
  width: min(760px, 100%);
  max-height: calc(100dvh - 32px);
  display: flex;
  flex-direction: column;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 14px;
  box-shadow: 0 20px 60px rgba(0, 0, 0, 0.5);
  overflow: hidden;
}
.modal.narrow { width: min(460px, 100%); }
.modal.wide { width: min(1100px, 100%); height: calc(100dvh - 32px); }
.modal header {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 12px 14px;
  border-bottom: 1px solid var(--line);
}
.modal header h2 { margin: 0; font-size: 18px; flex: 1; }
.modal .body { padding: 14px; overflow-y: auto; }
.modal .body p { margin: 0 0 10px; }
.modal .foot { display: flex; gap: 8px; justify-content: flex-end; padding: 12px 14px; border-top: 1px solid var(--line); }
.close { background: none; border: none; color: var(--muted); font-size: 22px; cursor: pointer; padding: 0 4px; }
.close:hover { color: var(--text); }

/* Tower tree */
.tree-wrap { display: grid; grid-template-columns: minmax(0, 1fr) 230px; gap: 14px; }
.tree {
  position: relative;
  aspect-ratio: 3 / 4.2;
  max-height: 62dvh;
  width: 100%;
  margin: 0 auto;
}
.tree svg, .gtree svg { position: absolute; inset: 0; width: 100%; height: 100%; pointer-events: none; }
.tree svg line, .gtree svg line { stroke: #2f3a47; stroke-width: 3; }
.tree svg line.on, .gtree svg line.on { stroke: var(--accent); }
.gtree svg line.on { stroke: var(--gold); }

.node {
  position: absolute;
  transform: translate(-50%, -50%);
  width: 29%;
  min-height: 52px;
  padding: 6px 6px;
  border-radius: 10px;
  border: 2px solid var(--line);
  background: #11161c;
  text-align: center;
  cursor: pointer;
  line-height: 1.15;
}
.node .n-name { font-weight: 700; font-size: 12px; display: block; }
.node .n-cost { font-size: 11px; color: var(--muted); font-variant-numeric: tabular-nums; }
.node.locked { color: #5d6670; border-color: #222a33; }
.node.locked .n-cost, .node.locked .n-rank { color: #4a525b; }
.node.open .n-cost { color: var(--bad); }
.node.ready { border-color: var(--good); box-shadow: 0 0 12px rgba(94, 224, 122, 0.25); }
.node.ready .n-cost { color: var(--good); }
.node.owned { border-color: var(--accent); background: #163a35; }
.node.owned .n-cost { color: var(--accent); }
.node.sel { outline: 2px solid #fff; outline-offset: 2px; }
.node.mastery { border-style: double; border-width: 4px; }

.detail { background: var(--panel-2); border: 1px solid var(--line); border-radius: var(--radius); padding: 12px; align-self: start; }
.detail h4 { margin: 0 0 4px; font-size: 15px; }
.detail p { color: var(--muted); margin: 0 0 8px; }
.detail .cost { font-weight: 700; margin-bottom: 8px; font-variant-numeric: tabular-nums; }
.detail .btn { width: 100%; justify-content: center; }

/* Golden tree */
.gtree-wrap { position: relative; flex: 1; min-height: 0; display: flex; }
.gtree-scroll { flex: 1; overflow: auto; position: relative; cursor: grab; }
.gtree-scroll.drag { cursor: grabbing; }
.gtree { position: relative; }
.gtree .node { width: 104px; min-height: 58px; }
.gtree .node.owned { border-color: var(--gold); background: #2c2408; }
.gtree .node.owned .n-cost { color: var(--gold); }
.gtree .node .n-rank { font-size: 10px; color: var(--muted); }
.gtree .node.cat-arsenal .n-name::before { content: "⚔ "; }
.gtree-detail { position: absolute; right: 12px; bottom: 12px; width: 250px; }
.legend { color: var(--muted); font-size: 12px; padding: 0 14px 10px; }

/* Lists */
.howto { padding-left: 18px; margin: 0 0 10px; }
.howto li { margin-bottom: 5px; }
.balls-legend { display: grid; grid-template-columns: auto 1fr; gap: 6px 10px; align-items: center; font-size: 13px; }
.swatch { width: 18px; height: 18px; border-radius: 50%; display: inline-block; }
.big { font-size: 28px; font-weight: 800; color: var(--gold); text-align: center; margin: 6px 0 12px; }
.kv { display: grid; grid-template-columns: 1fr auto; gap: 4px 14px; }
.kv dt { color: var(--muted); }
.kv dd { margin: 0; text-align: right; font-variant-numeric: tabular-nums; }
.setting { display: flex; justify-content: space-between; align-items: center; padding: 6px 0; border-bottom: 1px solid var(--line); }

/* Toasts ---------------------------------------------------------------- */

.toasts {
  position: fixed;
  left: 50%;
  top: 64px;
  transform: translateX(-50%);
  display: flex;
  flex-direction: column;
  gap: 6px;
  z-index: 30;
  pointer-events: none;
  width: min(420px, calc(100% - 32px));
}
.toast {
  padding: 9px 14px;
  border-radius: 10px;
  background: #1e2731;
  border: 1px solid #3a4655;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.4);
  text-align: center;
  animation: toast-in 0.25s ease-out;
}
.toast.gold { border-color: var(--gold); background: #2c2408; }
.toast.out { opacity: 0; transition: opacity 0.4s; }
@keyframes toast-in { from { transform: translateY(-8px); opacity: 0; } }

/* Narrow screens -------------------------------------------------------- */

@media (max-width: 1000px) {
  #app { grid-template-columns: 170px minmax(0, 1fr) 280px; }
  .actions .label { display: none; }
}

@media (max-width: 760px) {
  #app {
    grid-template-columns: minmax(0, 1fr);
    grid-template-rows: auto auto minmax(340px, 62dvh) auto;
    grid-template-areas: "top" "stages" "board" "side";
    height: auto;
    min-height: 100dvh;
  }
  .top { flex-wrap: wrap; gap: 8px; padding: 8px 12px; position: sticky; top: 0; z-index: 5; }
  .logo { font-size: 18px; }
  .money { font-size: 20px; }
  .actions { width: 100%; }
  .actions .btn.gold, .actions .btn.prestige { flex: 1; justify-content: center; }
  .stages { flex-direction: row; overflow-x: auto; border-right: none; border-bottom: 1px solid var(--line); padding: 8px 12px; }
  .stage { min-width: 150px; }
  .side { border-left: none; border-top: 1px solid var(--line); }
  .shop { display: grid; grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); overflow: visible; }
  .hud { font-size: 11px; padding: 5px 8px; left: 6px; top: 6px; }
  .hud b { font-size: 12px; }
  .hud .tags { display: none; }
  .reset { top: 6px; right: 6px; }
  .tree-wrap { grid-template-columns: 1fr; }
  .tree { max-height: none; }
  .node { width: 31%; padding: 4px 3px; min-height: 46px; }
  .node .n-name { font-size: 11px; }
  .gtree-detail { position: static; width: auto; margin: 10px; }
  .gtree-wrap { flex-direction: column; }
}
