:root {
  --bg: #0e0f13;
  --panel: #15171d;
  --panel-2: #1b1e26;
  --line: #262a34;
  --text: #e8eaf0;
  --muted: #8b91a3;
  --accent: #7aa2ff;
  --radius: 8px;
  --font: "SF Mono", "Cascadia Code", Consolas, "JetBrains Mono", Menlo, monospace;
}

* { box-sizing: border-box; }

/* The `hidden` attribute must beat the layout `display` rules below. The UA
   stylesheet's `[hidden] { display: none }` loses to any author rule, so
   `.overlay { display: flex }` kept the game-over screen on top of the board
   forever — `overlay.hidden = true` set the property and changed nothing. */
[hidden] { display: none !important; }

html, body {
  margin: 0;
  height: 100%;
}

body {
  display: grid;
  grid-template-columns: 260px minmax(0, 1fr);
  background: var(--bg);
  color: var(--text);
  font: 13px/1.5 var(--font);
  overflow: hidden;
}

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

.panel {
  background: var(--panel);
  border-right: 1px solid var(--line);
  padding: 18px 16px;
  overflow-y: auto;
}

.brand {
  margin: 0 0 8px;
  font-size: 26px;
  letter-spacing: 0.06em;
  color: var(--accent);
}

.intro { margin: 0 0 22px; }

.muted { color: var(--muted); }

.hint {
  margin-top: 22px;
  line-height: 1.8;
}

.status-line b { color: var(--text); }

/* ---------- scores ---------- */

.scores {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
  margin-bottom: 16px;
}

.score-box {
  background: var(--panel-2);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 8px 10px;
}

.score-box span {
  display: block;
  font-size: 10px;
  text-transform: uppercase;
  letter-spacing: 0.12em;
  color: var(--muted);
}

.score-box b {
  font-size: 20px;
  color: var(--text);
}

/* ---------- the blocks ---------- */

/* The block editor sits above the ladder in the panel, and the ladder is built
   from it — so it reads first, and each section keeps its own hairline top
   rather than relying on the one below it. */
.blocks {
  margin-top: 20px;
  padding-top: 16px;
  border-top: 1px solid var(--line);
}

.blocks-title {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 8px;
  margin: 0 0 10px;
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.14em;
  color: var(--muted);
}

.blocks-list {
  list-style: none;
  margin: 0;
  padding: 0;
  display: grid;
  gap: 6px;
}

/* one row per block: what it is, and how much of the spawn table it owns */
.block {
  padding: 6px 6px 5px;
  background: #12141a;
  border: 1px solid transparent;
  border-left: 2px solid #2c313d;
  border-radius: 4px;
}

/* a block the rules dropped — a value an earlier row already claims. Kept on
   screen rather than deleted, so nothing the player typed disappears. */
.block.inactive {
  opacity: 0.55;
  border-left-style: dashed;
  border-left-color: var(--line);
}

.block-top {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 58px 24px;
  align-items: center;
  gap: 6px;
}

.block-kind,
.block-amount {
  min-width: 0;
  padding: 3px 4px;
  background: var(--panel-2);
  color: var(--text);
  border: 1px solid var(--line);
  border-radius: 4px;
  font: inherit;
  font-size: 11px;
}

.block-amount { text-align: right; }

/* The native number spinners are light chrome that fights the panel, and at
   58px they crowd the value out. The arrow keys still step the field. */
.block-amount::-webkit-inner-spin-button,
.block-amount::-webkit-outer-spin-button {
  -webkit-appearance: none;
  appearance: none;
  margin: 0;
}

.block-kind:disabled,
.block-amount:disabled {
  background: #12141a;
  color: var(--muted);
}

/* the remove control: quiet until it is aimed at */
.btn-icon {
  width: 24px;
  height: 22px;
  padding: 0;
  font-size: 13px;
  line-height: 1;
  color: var(--muted);
}

.btn-icon:not(:disabled):hover { color: var(--text); }

/* the rarity control, with the share its weight actually buys */
.block-rarity {
  display: grid;
  grid-template-columns: 38px minmax(0, 1fr) 54px;
  align-items: center;
  gap: 6px;
  margin-top: 4px;
  font-size: 10px;
  color: var(--muted);
}

.block-rarity > span {
  text-transform: uppercase;
  letter-spacing: 0.1em;
}

/* A range input is the one control worth redrawing: the default track and
   thumb are bright, chunky chrome that reads as a different design language
   from the rest of the panel. */
.block-weight {
  -webkit-appearance: none;
  appearance: none;
  width: 100%;
  height: 14px;
  margin: 0;
  background: transparent;
  cursor: pointer;
}

.block-weight::-webkit-slider-runnable-track {
  height: 2px;
  border-radius: 1px;
  background: var(--line);
}

.block-weight::-webkit-slider-thumb {
  -webkit-appearance: none;
  appearance: none;
  width: 10px;
  height: 10px;
  margin-top: -4px;
  border-radius: 50%;
  background: var(--accent);
}

.block-weight::-moz-range-track {
  height: 2px;
  border-radius: 1px;
  background: var(--line);
}

.block-weight::-moz-range-thumb {
  width: 10px;
  height: 10px;
  border: none;
  border-radius: 50%;
  background: var(--accent);
}

.block-share {
  font-size: 10px;
  text-align: right;
  color: var(--text);
}

/* Scoped to this section: the AI controls use `.buttons` too and keep the
   layout they already had. */
.blocks .buttons {
  display: flex;
  gap: 6px;
  margin-top: 8px;
}

.blocks .buttons .btn {
  flex: 1;
  padding: 6px 8px;
  font-size: 11px;
}

/* ---------- the ladder ---------- */

.ladder {
  margin-top: 20px;
  padding-top: 16px;
  border-top: 1px solid var(--line);
}

.ladder-title {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 8px;
  margin: 0 0 10px;
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.14em;
  color: var(--muted);
}

.ladder-next {
  font-size: 10px;
  letter-spacing: 0.08em;
  color: var(--accent);
}

.milestones {
  list-style: none;
  margin: 0;
  padding: 0;
  display: grid;
  gap: 3px;
}

.rung {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 8px;
  padding: 4px 8px;
  border: 1px solid transparent;
  border-left: 2px solid #2c313d;
  border-radius: 4px;
  color: var(--muted);
  background: #12141a;
}

.rung-label {
  font-size: 10px;
  letter-spacing: 0.1em;
  text-transform: uppercase;
  opacity: 0.65;
}

.rung-value { font-size: 13px; }

/* reached in this run */
.rung.done {
  color: #0b0c10;
  background: var(--accent);
  border-left-color: var(--accent);
}

.rung.done .rung-label { opacity: 0.75; }

/* reached in an earlier run — a record to beat rather than a target */
.rung.ever {
  color: #cfd6e6;
  background: #1d2330;
  border-left-color: #4a5c8a;
}

/* the rung you are currently climbing */
.rung.next {
  color: var(--text);
  border-color: var(--accent);
  border-left-color: var(--accent);
}

.rung.pop { animation: rungpop 0.5s ease-out; }

@keyframes rungpop {
  0% { transform: translateX(0); }
  25% { transform: translateX(5px); }
  100% { transform: translateX(0); }
}

.ladder-note,
.blocks-note {
  margin: 10px 0 0;
  font-size: 11px;
  line-height: 1.6;
  min-height: 2.6em;
}

/* ---------- the AI's reasoning ---------- */

.chooser {
  list-style: none;
  margin: 14px 0 0;
  padding: 0;
  display: grid;
  gap: 3px;
}

/* one row per direction the AI considered */
.choice {
  display: grid;
  grid-template-columns: 18px minmax(0, 1fr) 52px;
  align-items: center;
  gap: 6px;
  padding: 3px 6px;
  border-left: 2px solid transparent;
  border-radius: 3px;
  background: #12141a;
  color: var(--muted);
}

/* a direction that would not change the board */
.choice.illegal { opacity: 0.4; }

.choice-dir {
  font-size: 14px;
  line-height: 1;
  color: var(--text);
}

.choice.illegal .choice-dir { color: var(--muted); }

.choice-bar {
  display: block;
  height: 4px;
  border-radius: 2px;
  background: #262a34;
  overflow: hidden;
}

.choice-bar i {
  display: block;
  height: 100%;
  background: #4a5c8a;
  transition: width 0.15s ease;
}

.choice-value {
  font-size: 11px;
  text-align: right;
  color: var(--muted);
}

/* the direction it picked */
.choice.chosen {
  border-left-color: var(--accent);
  background: #1d2330;
  color: var(--text);
}

.choice.chosen .choice-bar i { background: var(--accent); }
.choice.chosen .choice-value { color: var(--text); }

.ai-why {
  margin: 10px 0 0;
  font-size: 11px;
  line-height: 1.7;
}

.ai-why b { color: var(--text); }

.ai-stats {
  margin: 6px 0 0;
  font-size: 10px;
  letter-spacing: 0.04em;
  line-height: 1.7;
}

/* ---------- buttons ---------- */

.btn {
  background: var(--panel-2);
  color: var(--text);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 9px 12px;
  font: inherit;
  cursor: pointer;
}

.btn:hover { border-color: var(--accent); }

.btn:active { filter: brightness(0.94); }

/* A disabled button must not light up on hover — the rule above would
   otherwise still promise it does something. */
.btn:disabled {
  background: #12141a;
  color: var(--muted);
  border-color: var(--line);
  cursor: default;
}

.btn:disabled:hover { border-color: var(--line); }

.btn:disabled:active { filter: none; }

.btn-primary {
  width: 100%;
  background: var(--accent);
  border-color: var(--accent);
  color: #0b0c10;
  font-weight: 700;
}

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

/* ---------- stage / board ---------- */

.stage {
  display: flex;
  align-items: center;
  justify-content: center;
  min-width: 0;
  padding: 24px;
}

.board-wrap {
  position: relative;
  width: min(70vh, 100%);
  aspect-ratio: 1 / 1;
}

/* The AI's chosen move, shown on the board itself so the arrow reads as the
   thing thinking rather than a number in a sidebar. */
.ai-badge {
  position: absolute;
  top: 14px;
  left: 14px;
  z-index: 2;
  display: flex;
  align-items: baseline;
  gap: 8px;
  padding: 4px 9px;
  border: 1px solid var(--line);
  border-radius: 999px;
  background: rgba(11, 12, 16, 0.88);
  font-size: 11px;
  color: var(--muted);
  pointer-events: none;
}

.ai-badge b {
  font-size: 15px;
  line-height: 1;
  color: var(--accent);
}

.ai-badge.playing {
  border-color: var(--accent);
  color: var(--text);
}

.board {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  grid-template-rows: repeat(4, 1fr);
  gap: 8px;
  width: 100%;
  height: 100%;
  padding: 8px;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 12px;
}

.tile {
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 8px;
  background: var(--panel-2);
  color: var(--text);
  font-size: clamp(14px, 4.4vh, 30px);
  font-weight: 700;
  user-select: none;
}

/* value colours, from quiet grey through to warm gold */
.tile.v2    { background: #31384a; color: #e8eaf0; }
.tile.v4    { background: #3d4763; color: #e8eaf0; }
.tile.v8    { background: #4a5c8a; color: #f4f6fb; }
.tile.v16   { background: #5872b3; color: #0b0c10; }
.tile.v32   { background: #6b8fd6; color: #0b0c10; }
.tile.v64   { background: #7aa2ff; color: #0b0c10; }
.tile.v128  { background: #8fb4ff; color: #0b0c10; }
.tile.v256  { background: #a6c2ff; color: #0b0c10; }
.tile.v512  { background: #b7a6ff; color: #0b0c10; }
.tile.v1024 { background: #c9a6ff; color: #0b0c10; }
.tile.v2048 { background: #ffcf7a; color: #0b0c10; }
.tile.v4096 { background: #ffb35c; color: #0b0c10; }
.tile.v8192 { background: #ff8f6b; color: #0b0c10; }

/* An operator tile is a modifier, not a value, so it must not read as a rung
   of the ramp above: quiet panel fill, a hairline accent, and the symbol in
   the accent itself. It sits outside v2..v8192 on purpose. */
.tile.op {
  background: var(--panel-2);
  color: var(--accent);
  border: 1px solid var(--accent);
  font-size: clamp(13px, 3.6vh, 24px);
  letter-spacing: -0.02em;
}

@keyframes pop {
  0% { transform: scale(0.82); }
  60% { transform: scale(1.06); }
  100% { transform: scale(1); }
}

.tile.spawn { animation: pop 0.16s ease-out; }
.tile.merge { animation: pop 0.22s ease-out; }

/* ---------- overlay ---------- */

.overlay {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 16px;
  background: rgba(11, 12, 16, 0.82);
  border-radius: 12px;
}

.overlay p {
  margin: 0;
  font-size: 18px;
  color: var(--text);
  text-align: center;
}

.overlay-sub {
  font-size: 12px !important;
  color: var(--muted) !important;
  max-width: 260px;
}

.overlay-actions {
  display: flex;
  gap: 10px;
}

.overlay .btn { width: auto; }

@media (prefers-reduced-motion: reduce) {
  .tile.spawn,
  .tile.merge,
  .rung.pop { animation: none; }
  .choice-bar i { transition: none; }
}

/* ---------- focus ---------- */

.btn:focus-visible,
.check input:focus-visible,
.field input:focus-visible,
.block input:focus-visible,
.block select:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

/* ---------- narrow screens ---------- */

@media (max-width: 720px) {
  body {
    grid-template-columns: 1fr;
    grid-template-rows: auto minmax(0, 1fr);
  }

  .panel {
    border-right: none;
    border-bottom: 1px solid var(--line);
    max-height: 45vh;
  }
}
