:root {
  --bg: #03040a;
  --panel: #0a0d1c;
  --panel-2: #0f1328;
  --line: #1d2550;
  --text: #cfd8ff;
  --muted: #7680b0;
  --accent: #8a5cff;
  --gold: #ffd84a;
  --lives: #ff4f7b;
  --ok: #3dff8b;
  --warn: #ffd84a;
  --bad: #ff3d6e;
  --font: "JetBrains Mono", "Fira Code", ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  --chrome: 310px; /* vertical space taken by everything around the board */
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 13px/1.45 var(--font);
  -webkit-text-size-adjust: 100%;
}
body {
  padding: env(safe-area-inset-top) env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left);
}

button {
  font: inherit;
  color: var(--text);
  background: var(--panel-2);
  border: 1px solid var(--line);
  border-radius: 6px;
  padding: 6px 10px;
  cursor: pointer;
  transition: border-color 0.15s, background 0.15s, box-shadow 0.15s;
  touch-action: manipulation;
  -webkit-tap-highlight-color: transparent;
  -webkit-user-select: none;
  user-select: none;
}
button:hover:not(:disabled) { border-color: var(--accent); box-shadow: 0 0 10px rgba(138, 92, 255, 0.35); }
button:disabled { opacity: 0.4; cursor: not-allowed; }
button.primary { background: #2a1a66; border-color: var(--accent); color: #fff; }
button.ghost { background: transparent; }
button.wide { width: 100%; }
button.on { border-color: var(--accent); color: #fff; background: #1d1747; }

kbd {
  border: 1px solid var(--line);
  border-bottom-width: 2px;
  border-radius: 4px;
  padding: 0 4px;
  font-size: 11px;
  color: #fff;
}

.top {
  display: flex;
  align-items: center;
  gap: 20px;
  flex-wrap: wrap;
  padding: 12px 20px;
  border-bottom: 1px solid var(--line);
}
.logo {
  margin: 0;
  font-size: 20px;
  letter-spacing: 0.3em;
  color: #fff;
  text-shadow: 0 0 12px rgba(138, 92, 255, 0.9);
}
.logo span { color: var(--accent); }
.levels { display: flex; gap: 8px; flex-wrap: wrap; }
@media (max-width: 1100px) {
  .levels { flex-wrap: nowrap; overflow-x: auto; max-width: 100%; scrollbar-width: none; -webkit-overflow-scrolling: touch; }
  .levels button { flex: 0 0 auto; }
}
.levels button { display: flex; flex-direction: column; align-items: flex-start; padding: 6px 12px; }
.levels button small { color: var(--muted); font-size: 10px; }
.levels .badge { color: var(--gold); letter-spacing: 0.05em; }
.overlay .stars { font-size: 30px; color: var(--gold); letter-spacing: 0.2em; text-shadow: 0 0 14px rgba(255, 216, 74, 0.7); }
details > summary { cursor: pointer; color: var(--muted); font-size: 11px; letter-spacing: 0.1em; text-transform: uppercase; margin: 4px 0; }
details[open] > summary { color: var(--text); }
.hint p { margin-top: 6px; }
.mode-help { margin: 6px 0 0; }
.diff { text-transform: uppercase; letter-spacing: 0.08em; font-size: 9px; padding: 0 4px; border: 1px solid currentColor; border-radius: 3px; }
.diff.intro { color: var(--ok); }
.diff.hard { color: var(--warn); }
.diff.brutal { color: var(--bad); }

.layout {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 320px;
  gap: 16px;
  padding: 16px 20px 24px;
  max-width: 1400px;
  margin: 0 auto;
}
@media (max-width: 980px) {
  .layout { grid-template-columns: minmax(0, 1fr); padding: 12px 16px; }
}

.hud {
  display: flex;
  align-items: center;
  gap: 16px;
  flex-wrap: wrap;
  margin-bottom: 6px;
}
.hud-main, .hud-controls { display: flex; align-items: center; gap: 16px; flex-wrap: wrap; }
.hud-controls { gap: 8px; }
.phone-only { display: none; }
.stat { display: flex; flex-direction: column; }
.stat .label { font-size: 10px; letter-spacing: 0.15em; text-transform: uppercase; color: var(--muted); }
.stat .value { font-size: 20px; color: #fff; font-variant-numeric: tabular-nums; }
.stat .gold { color: var(--gold); text-shadow: 0 0 10px rgba(255, 216, 74, 0.5); }
.stat .lives { color: var(--lives); text-shadow: 0 0 10px rgba(255, 79, 123, 0.5); }
.hud-spacer { flex: 1; }
.speed { display: flex; gap: 4px; }
.incoming { color: var(--muted); min-height: 1.45em; margin-bottom: 8px; }
.incoming b { color: var(--text); font-weight: normal; }
.new { color: #000; background: var(--warn); border-radius: 3px; padding: 0 4px; font-size: 9px; letter-spacing: 0.08em; vertical-align: 1px; }
.ctxbar .tip { color: var(--text); border-left: 3px solid var(--creep); padding-left: 8px; display: block; }

.canvas-wrap {
  position: relative;
  border: 1px solid var(--line);
  border-radius: 10px;
  overflow: hidden;
  box-shadow: 0 0 40px rgba(60, 40, 160, 0.25);
  width: 100%;
  max-width: max(480px, calc((100dvh - var(--chrome)) * 4 / 3));
  margin-inline: auto;
}
canvas {
  display: block;
  cursor: crosshair;
  touch-action: none; /* taps and drags aim towers instead of scrolling or zooming */
  -webkit-tap-highlight-color: transparent;
  -webkit-touch-callout: none;
  -webkit-user-select: none;
  user-select: none;
}

.ctxbar {
  display: flex;
  align-items: center;
  gap: 6px;
  flex-wrap: wrap;
  min-height: 44px;
  margin-top: 8px;
  padding: 4px 8px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--panel);
}
.ctxbar .hint { flex: 1 1 160px; color: var(--muted); min-width: 0; }
.ctxbar .hint b { font-weight: bold; }
.toast {
  position: absolute;
  left: 50%;
  bottom: 12px;
  transform: translateX(-50%);
  max-width: calc(100% - 24px);
  padding: 5px 14px;
  border: 1px solid var(--bad);
  border-radius: 999px;
  background: rgba(20, 4, 14, 0.88);
  color: #fff;
  font-size: 12px;
  pointer-events: none;
  text-align: center;
}
.slider { display: flex; align-items: center; gap: 10px; margin: 10px 0 6px; color: var(--muted); }
.slider input { flex: 1; accent-color: var(--accent); }

.banner {
  position: absolute;
  top: 10px;
  left: 50%;
  transform: translateX(-50%);
  padding: 4px 14px;
  border: 1px solid var(--accent);
  border-radius: 999px;
  background: rgba(10, 6, 30, 0.8);
  color: #fff;
  font-size: 12px;
  letter-spacing: 0.1em;
  pointer-events: none;
  white-space: nowrap;
}
.overlay {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 12px;
  background: rgba(3, 4, 10, 0.78);
  text-align: center;
  padding: 16px;
}
.overlay h2 { margin: 0; font-size: 28px; letter-spacing: 0.25em; }
.overlay h2.win { color: var(--ok); text-shadow: 0 0 16px rgba(61, 255, 139, 0.8); }
.overlay h2.lose { color: var(--bad); text-shadow: 0 0 16px rgba(255, 61, 110, 0.8); }
.overlay p { margin: 0; color: var(--muted); max-width: 420px; }
.overlay .row { display: flex; gap: 8px; flex-wrap: wrap; justify-content: center; }
.hidden { display: none !important; }

.shop {
  display: grid;
  grid-template-columns: repeat(5, minmax(0, 1fr));
  gap: 8px;
  margin-top: 10px;
}
@media (max-width: 640px) { .shop { grid-template-columns: repeat(3, minmax(0, 1fr)); } }
.shop button {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  gap: 2px;
  text-align: left;
  padding: 8px 10px;
  border-left: 3px solid var(--tower);
}
.shop button.on { box-shadow: 0 0 14px var(--tower); border-color: var(--tower); }
.shop .name { color: var(--tower); font-weight: bold; }
.shop .cost { color: var(--gold); font-size: 12px; }
.shop .key { color: var(--muted); font-size: 10px; }

.panel { display: flex; flex-direction: column; gap: 12px; min-width: 0; }
.card {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 12px 14px;
}
.card h3 {
  margin: 0 0 8px;
  font-size: 11px;
  letter-spacing: 0.18em;
  text-transform: uppercase;
  color: var(--muted);
}
.card p { margin: 0 0 6px; }
.card .title { color: #fff; font-size: 15px; margin-bottom: 2px; }
.card .sub { color: var(--muted); font-size: 11px; margin-bottom: 8px; }
.actions { display: flex; flex-direction: column; gap: 6px; }
.keys p { color: var(--muted); line-height: 1.9; }

.kv { display: grid; grid-template-columns: auto 1fr; gap: 2px 12px; margin: 6px 0 10px; }
.kv dt { color: var(--muted); }
.kv dd { margin: 0; text-align: right; color: #fff; font-variant-numeric: tabular-nums; }
.kv dd .next { color: var(--ok); }
.btnrow { display: flex; gap: 6px; flex-wrap: wrap; }
.btnrow button { flex: 1; }
.modes { display: flex; gap: 4px; flex-wrap: wrap; margin-top: 8px; }
.modes button { padding: 3px 8px; font-size: 11px; }

.verdict {
  display: inline-block;
  padding: 1px 8px;
  border-radius: 999px;
  font-size: 11px;
  letter-spacing: 0.1em;
  border: 1px solid currentColor;
}
.verdict.PASS { color: var(--ok); }
.verdict.TIGHT { color: var(--warn); }
.verdict.FAIL { color: var(--bad); }
.meter { height: 6px; background: var(--panel-2); border-radius: 3px; overflow: hidden; margin: 8px 0; }
.meter > i { display: block; height: 100%; }
.fine { color: var(--muted); font-size: 11px; }

.matrix {
  background: var(--panel);
  color: var(--text);
  border: 1px solid var(--accent);
  border-radius: 12px;
  max-width: min(1100px, calc(100vw - 32px));
  max-height: calc(100vh - 64px);
  padding: 16px 20px;
}
.matrix::backdrop { background: rgba(0, 0, 0, 0.7); }
.matrix .close { float: right; }
.matrix h2 { margin: 0 0 6px; font-size: 16px; color: #fff; }
.matrix ul { margin: 6px 0 12px; padding-left: 18px; color: var(--muted); }
.matrix .scroll { overflow-x: auto; }
table { border-collapse: collapse; width: 100%; font-size: 12px; }
th, td { padding: 4px 8px; border-bottom: 1px solid var(--line); text-align: right; white-space: nowrap; }
th { color: var(--muted); font-weight: normal; }
td.l, th.l { text-align: left; }

/* ---- touch ------------------------------------------------------------------- */

@media (pointer: coarse) {
  button { min-height: 40px; }
  .shop .key, .keys { display: none; }
  .modes button { min-height: 36px; padding: 6px 10px; }
  .slider input { min-height: 32px; }
}

/* A game UI: no text selection, no long-press callouts (inputs keep working). */
body { -webkit-user-select: none; user-select: none; -webkit-touch-callout: none; }
input { -webkit-user-select: auto; user-select: auto; }

/* ---- wave / level title flash ------------------------------------------------ */

.flash {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 6px;
  pointer-events: none;
  opacity: 0;
  text-align: center;
  padding: 0 12px;
}
.flash b { font-size: clamp(24px, 9vmin, 56px); letter-spacing: 0.3em; color: #fff; text-shadow: 0 0 18px var(--flash, var(--accent)), 0 0 42px var(--flash, var(--accent)); }
.flash small { font-size: 13px; letter-spacing: 0.12em; color: var(--text); text-shadow: 0 0 10px #000; }
.flash.go { animation: flash-in 1.7s ease-out forwards; }
@keyframes flash-in {
  0% { opacity: 0; transform: scale(1.25); }
  12% { opacity: 1; transform: scale(1); }
  70% { opacity: 1; }
  100% { opacity: 0; transform: scale(0.98); }
}

/* ---- phone app shell: one screen, no scrolling, everything else in the menu sheet ---- */

.menu-btn, .sheet-head, .levels-card, .sheet-backdrop, .level-name { display: none; }
.menu-btn { font-size: 18px; line-height: 1; padding: 6px 12px; }
.sheet-head { align-items: center; justify-content: space-between; }
.sheet-head .logo { font-size: 15px; }
.levels-list { display: flex; flex-direction: column; gap: 6px; }
.levels-list button { display: flex; flex-direction: column; align-items: flex-start; text-align: left; }
.levels-list button small { color: var(--muted); font-size: 10px; }
.install-hint { color: var(--text); border-color: var(--accent); }
.stage { display: block; }

@media (max-width: 640px), (orientation: landscape) and (max-height: 540px) {
  html, body { height: 100%; overflow: hidden; overscroll-behavior: none; }
  body { display: flex; flex-direction: column; }
  .top { display: none; }
  .layout { flex: 1 1 0; min-height: 0; display: flex; flex-direction: column; padding: 6px 10px 6px; gap: 0; max-width: none; width: 100%; margin: 0; }

  /* The board takes whatever space the controls leave; JS fits the canvas into .stage. */
  .board { flex: 1 1 0; min-height: 0; display: grid; gap: 6px; }
  .hud { display: contents; }
  .hud-main { grid-area: main; gap: 6px 12px; flex-wrap: nowrap; }
  .hud-controls { grid-area: controls; gap: 6px; flex-wrap: nowrap; }
  .hud-controls > button { flex: 1 1 auto; min-width: 0; }
  .hud-spacer, .speed, #mute, #pause { display: none; }
  .phone-only, .menu-btn { display: inline-flex; align-items: center; justify-content: center; }
  .hud-main .menu-btn { order: -1; }
  .hud-main #speedCycle { margin-left: auto; }
  .hud button { min-height: 36px; padding: 4px 10px; }
  .stat { flex-direction: row; align-items: baseline; gap: 4px; }
  .stat .label { font-size: 9px; letter-spacing: 0.1em; }
  .stat .value { font-size: 17px; }
  #next { grid-area: next; min-height: 46px; font-size: 14px; letter-spacing: 0.06em; }
  .incoming { grid-area: incoming; margin: 0; font-size: 11px; min-height: 0; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
  .stage { grid-area: stage; min-height: 0; min-width: 0; display: flex; align-items: center; justify-content: center; overflow: hidden; }
  .canvas-wrap { width: auto; max-width: none; margin: 0; }
  .ctxbar { grid-area: ctx; margin: 0; min-height: 44px; padding: 4px 8px; }
  .ctxbar .hint { font-size: 12px; }
  .shop { grid-area: shop; margin: 0; grid-template-columns: repeat(5, minmax(0, 1fr)); gap: 4px; }
  .shop button { padding: 6px 4px; align-items: center; text-align: center; border-left-width: 1px; border-top: 3px solid var(--tower); min-height: 48px; }
  .shop .name { font-size: 11px; }
  .shop .key { display: none; }

  /* Menu sheet. */
  .sheet-head { display: flex; margin-bottom: 4px; }
  .levels-card { display: block; }
  .panel {
    position: fixed;
    z-index: 30;
    left: 0;
    right: 0;
    bottom: 0;
    max-height: 86dvh;
    overflow-y: auto;
    overscroll-behavior: contain;
    -webkit-overflow-scrolling: touch;
    background: var(--bg);
    border-top: 1px solid var(--accent);
    border-radius: 16px 16px 0 0;
    padding: 12px 12px calc(16px + env(safe-area-inset-bottom));
    transform: translateY(105%);
    transition: transform 0.22s ease-out;
    box-shadow: 0 -10px 40px rgba(60, 40, 160, 0.35);
  }
  body.sheet-open .panel { transform: none; }
  .sheet-backdrop { position: fixed; inset: 0; z-index: 29; background: rgba(0, 0, 0, 0.6); }
  body.sheet-open .sheet-backdrop { display: block; }
  .overlay h2 { font-size: 22px; }
}

/* Portrait phone: top bar, controls row, board filling the middle, actions + launch + shop at the thumb. */
@media (max-width: 640px) and (orientation: portrait) {
  /* Fixed-height, single-row action bar: selecting a tower must never resize the board. */
  .ctxbar { height: 58px; min-height: 0; flex-wrap: nowrap; overflow: hidden; gap: 5px; }
  .ctxbar .hint {
    flex: 1 1 0;
    min-width: 0;
    font-size: 11px;
    line-height: 1.25;
    display: -webkit-box;
    -webkit-line-clamp: 3;
    -webkit-box-orient: vertical;
    overflow: hidden;
  }
  .ctxbar .tip { display: inline; border-left: none; padding-left: 0; color: var(--creep); }
  .ctxbar button { flex: 0 0 auto; padding: 4px 9px; font-size: 13px; white-space: nowrap; min-height: 42px; }
  .board {
    grid-template-columns: minmax(0, 1fr);
    grid-template-rows: auto auto auto minmax(0, 1fr) auto auto auto;
    grid-template-areas: "main" "controls" "incoming" "stage" "ctx" "next" "shop";
  }
}

/* Landscape phone: board on the left at full height; HUD, actions, launch and shop on the right. */
@media (orientation: landscape) and (max-height: 540px) {
  .layout { padding: 6px 10px; }
  .board {
    grid-template-columns: auto minmax(0, 1fr);
    grid-template-rows: auto auto auto auto minmax(0, 1fr);
    grid-template-areas: "stage main" "stage controls" "stage next" "stage ctx" "stage shop";
    column-gap: 10px;
  }
  .stage { width: min(58vw, calc((100dvh - 12px - env(safe-area-inset-top) - env(safe-area-inset-bottom)) * 4 / 3)); height: 100%; }
  .incoming { display: none; }
  .hud-main { flex-wrap: wrap; row-gap: 4px; } /* narrow column next to the notch: speed / pause may wrap */
  .shop { grid-template-columns: repeat(3, minmax(0, 1fr)); align-content: start; }
  .shop button { min-height: 40px; }
  #next { min-height: 40px; }
  .panel {
    top: 0;
    left: auto;
    width: min(380px, 70vw);
    max-height: none;
    border-top: none;
    border-left: 1px solid var(--accent);
    border-radius: 16px 0 0 16px;
    padding: calc(12px + env(safe-area-inset-top)) calc(12px + env(safe-area-inset-right)) calc(16px + env(safe-area-inset-bottom)) 12px;
    transform: translateX(105%);
  }
}

/* ---- polish: level select, transitions, results, HUD juice ------------------------ */

.logo-btn { all: unset; cursor: pointer; }
.logo-btn span { color: var(--accent); }
button:active:not(:disabled) { transform: scale(0.97); }

.select {
  position: fixed;
  inset: 0;
  z-index: 40;
  overflow-y: auto;
  overscroll-behavior: contain;
  -webkit-overflow-scrolling: touch;
  background: radial-gradient(ellipse at 50% 0%, rgba(90, 50, 220, 0.25), transparent 60%), var(--bg);
  padding: calc(16px + env(safe-area-inset-top)) calc(16px + env(safe-area-inset-right)) calc(20px + env(safe-area-inset-bottom)) calc(16px + env(safe-area-inset-left));
  animation: select-in 0.3s ease-out;
}
@keyframes select-in { from { opacity: 0; } to { opacity: 1; } }
.select-inner { max-width: 1100px; margin: 0 auto; }
.select-head { text-align: center; margin: 8px 0 18px; }
.logo.big { font-size: clamp(26px, 8vw, 44px); letter-spacing: 0.35em; margin: 0 0 6px; }
.select-summary { color: var(--muted); margin: 0; }
.select-summary b { color: var(--gold); font-weight: normal; }
.select-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(220px, 1fr)); gap: 12px; }
.lvl-card {
  display: flex;
  flex-direction: column;
  align-items: stretch;
  gap: 8px;
  padding: 10px;
  text-align: left;
  border-radius: 12px;
  background: var(--panel);
  animation: card-in 0.35s ease-out backwards;
}
.lvl-card:hover:not(:disabled) { transform: translateY(-2px); }
.lvl-card.next { border-color: var(--accent); box-shadow: 0 0 18px rgba(138, 92, 255, 0.45); }
.lvl-card canvas { width: 100%; aspect-ratio: 4 / 3; border-radius: 8px; border: 1px solid var(--line); display: block; }
.lvl-card .row1 { display: flex; justify-content: space-between; align-items: baseline; gap: 8px; }
.lvl-card .num { color: var(--muted); font-size: 11px; }
.lvl-card .name { color: #fff; font-size: 14px; }
.lvl-card .stars { color: var(--gold); letter-spacing: 0.1em; white-space: nowrap; }
.lvl-card .stars.none { color: var(--line); }
.lvl-card .meta { color: var(--muted); font-size: 11px; display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
@keyframes card-in { from { opacity: 0; transform: translateY(12px); } to { opacity: 1; transform: none; } }

.fade { position: fixed; inset: 0; z-index: 50; background: var(--bg); opacity: 0; pointer-events: none; transition: opacity 0.18s ease; }
.fade.on { opacity: 1; pointer-events: auto; }

/* Results screen */
.overlay { overflow-y: auto; }
.overlay .stars { display: flex; gap: 10px; justify-content: center; font-size: 34px; }
.overlay .stars i { font-style: normal; color: var(--line); text-shadow: none; }
.overlay .stars i.on { color: var(--gold); text-shadow: 0 0 16px rgba(255, 216, 74, 0.8); animation: star-pop 0.45s cubic-bezier(0.2, 1.6, 0.4, 1) backwards; }
@keyframes star-pop { from { opacity: 0; transform: scale(2.4) rotate(-30deg); } to { opacity: 1; transform: none; } }
.results { display: grid; grid-template-columns: repeat(4, auto); gap: 4px 18px; justify-content: center; }
.results div { display: flex; flex-direction: column; align-items: center; }
.results b { color: #fff; font-size: 18px; font-weight: normal; font-variant-numeric: tabular-nums; }
.results small { color: var(--muted); font-size: 10px; letter-spacing: 0.12em; text-transform: uppercase; }
.dmg { width: min(320px, 100%); display: grid; grid-template-columns: auto 1fr auto; gap: 4px 8px; align-items: center; font-size: 11px; }
.dmg span { color: var(--tower); text-align: left; }
.dmg em { font-style: normal; color: var(--muted); text-align: right; font-variant-numeric: tabular-nums; }
.dmg i { display: block; height: 6px; border-radius: 3px; background: var(--tower); box-shadow: 0 0 8px var(--tower); transform-origin: left; animation: bar-grow 0.7s ease-out backwards; }
@keyframes bar-grow { from { transform: scaleX(0); } }

/* HUD juice */
.stat .value.hit { animation: hud-hit 0.6s ease-out; }
@keyframes hud-hit { 0% { transform: scale(1.6); color: #fff; } 100% { transform: none; } }
.stat .value.gain { animation: hud-gain 0.35s ease-out; }
@keyframes hud-gain { 0% { transform: scale(1.18); } 100% { transform: none; } }
.stat .value { display: inline-block; }

@media (max-width: 640px) {
  .select-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 8px; }
  .lvl-card { padding: 8px; gap: 6px; }
  .lvl-card .name { font-size: 12px; }
  .results { grid-template-columns: repeat(2, auto); }
}
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation-duration: 0.01ms !important; transition-duration: 0.01ms !important; }
}

.lvl-card.locked { opacity: 0.45; }
.lvl-card.locked canvas { filter: grayscale(1) brightness(0.6); }
.levels button:disabled, .levels-list button:disabled { opacity: 0.4; }
