/* Two themes, set on <html data-theme> before first paint: ivory stone with
   gold by day, violet dusk with purple at night. The 3D colors that match
   these live in src/theme.js. */
:root {
  --bone: #f4efe3;
  --paper: #fbf8f1;
  --ink: #2a2418;
  --ink-2: #5b5140;
  --rule: rgba(42, 36, 24, 0.2);
  --accent: #c99a1e;
  --accent-hover: #b3870f;
  --on-accent: #2a2418;
  --accent-ink: #876210;
  --btn-hover: #463d2c;
  --ghost-hover: rgba(42, 36, 24, 0.07);
  --chip-hover: #efe7d5;
  --veil: rgba(244, 239, 227, 0.55);
  --veil-strong: rgba(244, 239, 227, 0.82);
  --stick-bg: rgba(251, 248, 241, 0.6);
  --sw-transfer: #e6dcc6;
  --sw-call: #c4b797;
  --sw-blob: #9f8d69;
  --sw-slab: #dacdb0;
  --sw-spring-base: #2a2418;
  --font: "Inter", system-ui, sans-serif;
  color-scheme: light;
}
:root[data-theme="dark"] {
  --bone: #17131f;
  --paper: #211c2d;
  --ink: #ece7f7;
  --ink-2: #b4aac8;
  --rule: rgba(236, 231, 247, 0.18);
  --accent: #a77bff;
  --accent-hover: #b994ff;
  --on-accent: #140f1f;
  --accent-ink: #c7a8ff;
  --btn-hover: #d4cbe9;
  --ghost-hover: rgba(236, 231, 247, 0.08);
  --chip-hover: #2d2640;
  --veil: rgba(23, 19, 31, 0.55);
  --veil-strong: rgba(23, 19, 31, 0.82);
  --stick-bg: rgba(33, 28, 45, 0.6);
  --sw-transfer: #4a4163;
  --sw-call: #352e4a;
  --sw-blob: #5d4b8c;
  --sw-slab: #3b3452;
  --sw-spring-base: #07060b;
  color-scheme: dark;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  overflow: hidden;
  background: var(--bone);
  color: var(--ink);
  font: 16px/1.5 var(--font);
  font-feature-settings: "cv11", "ss01";
  -webkit-font-smoothing: antialiased;
}

[hidden] { display: none !important; }

#scene {
  position: fixed;
  inset: 0;
  width: 100%;
  height: 100%;
  display: block;
  touch-action: none;
}

h1, h2 { font-weight: 700; margin: 0; line-height: 1; letter-spacing: -0.035em; }
h1 { font-size: clamp(2.6rem, 6vw, 4.2rem); }
h2 { font-size: 2rem; }
p { margin: 0; }

.mono { font-variant-numeric: tabular-nums slashed-zero; }
.small { font-size: 0.82rem; color: var(--ink-2); }
.label { font-size: 0.8rem; color: var(--ink-2); }

.sheet {
  background: var(--paper);
  border: 1px solid var(--ink);
  border-radius: 2px;
}

/* Title sheet: a ledger page pinned to the left, the tower on the right. */
#title {
  position: fixed;
  top: 20px;
  left: 20px;
  bottom: 20px;
  width: min(460px, calc(100vw - 40px));
  padding: 32px 32px 28px;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 20px;
}

.title-head { display: flex; flex-wrap: wrap; justify-content: space-between; align-items: center; gap: 12px; }
.title-head button { flex-shrink: 0; white-space: nowrap; }

.lede { font-size: 1.05rem; line-height: 1.5; }
.note { font-size: 0.92rem; color: var(--ink-2); }
/* Shown only on touch devices, where the keyboard-and-mouse climb is harder. */
.touch-note {
  font-size: 0.92rem;
  color: var(--ink-2);
  padding: 12px 14px;
  background: var(--bone);
  border: 1px solid var(--rule);
}
.touch-note strong { color: var(--ink); font-weight: 600; }

.ledger {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.9rem;
}
.ledger caption {
  text-align: left;
  font-weight: 600;
  font-size: 1.05rem;
  letter-spacing: -0.01em;
  padding-bottom: 6px;
  border-bottom: 1px solid var(--ink);
}
.ledger th, .ledger td {
  text-align: left;
  vertical-align: top;
  padding: 7px 8px 7px 0;
  border-bottom: 1px solid var(--rule);
  font-weight: 500;
}
.ledger td:last-child { font-weight: 400; color: var(--ink-2); padding-right: 0; }
.ledger td:first-child { width: 34px; }

.sw { display: inline-block; height: 12px; border: 1px solid var(--ink); margin-top: 4px; }
.sw-transfer { width: 28px; background: var(--sw-transfer); }
.sw-call { width: 14px; background: var(--sw-call); }
.sw-spring { width: 18px; background: var(--accent); border-bottom: 5px solid var(--sw-spring-base); height: 14px; }
.sw-blob { width: 18px; background: var(--sw-blob); }
.sw-slab { width: 30px; height: 14px; background: var(--sw-slab); }

.keys {
  margin: 0;
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 6px 16px;
  font-size: 0.85rem;
}
.keys div { display: flex; gap: 8px; border-top: 1px solid var(--rule); padding-top: 6px; }
.keys dt { font-weight: 600; min-width: 42px; }
.keys dd { margin: 0; color: var(--ink-2); }

.status { font-size: 0.85rem; font-variant-numeric: tabular-nums; }
.status.error { color: var(--accent-ink); }

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

button {
  font: 600 0.95rem/1 var(--font);
  min-height: 46px;
  padding: 0 18px;
  border: 1px solid var(--ink);
  border-radius: 2px;
  background: var(--ink);
  color: var(--paper);
  cursor: pointer;
}
button:hover:not(:disabled) { background: var(--btn-hover); }
#start, #again, #resume, #retry, #submit { background: var(--accent); border-color: var(--accent); color: var(--on-accent); }
#start:hover:not(:disabled), #again:hover, #resume:hover, #retry:hover, #submit:hover:not(:disabled) { background: var(--accent-hover); }
button.ghost { background: transparent; color: var(--ink); }
button.ghost:hover:not(:disabled) { background: var(--ghost-hover); }
button:disabled { opacity: 0.45; cursor: progress; }

:focus-visible { outline: 2px solid var(--accent-ink); outline-offset: 2px; }

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

/* HUD */
#hud { position: fixed; inset: 0; pointer-events: none; }
.panel {
  position: absolute;
  background: var(--paper);
  border: 1px solid var(--ink);
  border-radius: 2px;
  padding: 10px 14px 12px;
}
.hud-where { top: 16px; left: 16px; max-width: min(380px, calc(100vw - 180px)); }
.where { font-weight: 600; font-size: 1.3rem; letter-spacing: -0.02em; line-height: 1.15; margin: 2px 0 4px; font-variant-numeric: tabular-nums; }
.hud-gap { top: 16px; right: 16px; text-align: right; min-width: 150px; }
.big { font-weight: 700; font-size: 3.2rem; letter-spacing: -0.04em; line-height: 0.95; font-variant-numeric: tabular-nums; }
#line-info.warn { color: var(--accent-ink); font-weight: 500; }

.hud-bar {
  position: absolute;
  left: 16px;
  bottom: 16px;
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  align-items: center;
}
.chip, .chip-btn {
  background: var(--paper);
  color: var(--ink);
  border: 1px solid var(--ink);
  border-radius: 2px;
  padding: 0 12px;
  font-size: 0.85rem;
  min-height: 34px;
  display: inline-flex;
  align-items: center;
}
.chip-btn { pointer-events: auto; min-height: 44px; font-weight: 500; }
.chip-btn:hover { background: var(--chip-hover); }
#dash.spent { color: var(--ink-2); }
.chip:has(#dash.spent) { background: transparent; border-style: dashed; }
/* Hotkey hints on the bottom bar. Hidden on touch, where there's no keyboard. */
.hud-bar kbd {
  margin-left: 8px;
  padding: 1px 5px;
  border: 1px solid var(--rule);
  border-bottom-width: 2px;
  border-radius: 3px;
  font: 600 0.72rem/1.3 var(--font);
  color: var(--ink-2);
  background: var(--bone);
}
body.touch .hud-bar kbd { display: none; }
/* The on-screen controller (stick, Jump, Dash, Pause) is for touch only. Keyboards pause with P or Esc. */
body:not(.touch) #pause-btn { display: none; }

/* Desktop: the wrapper steps aside and its panels keep their own corners. */
.hud-left { display: contents; }
/* Touch: Standing on, then Pause and the run chips stacked in the top-left
   corner, clear of the stick. Sound and theme stay off the play screen. */
body.touch .hud-left {
  position: absolute;
  top: 16px;
  left: 16px;
  max-width: min(380px, calc(100vw - 180px));
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  gap: 8px;
}
body.touch .hud-where, body.touch .hud-bar { position: static; max-width: none; }
body.touch #mute, body.touch #theme-hud { display: none; }
body.touch #pause-btn { min-height: 48px; min-width: 48px; padding: 0 16px; font-weight: 600; }
#feed.warn { color: var(--accent-ink); }

#toast {
  position: fixed;
  left: 50%;
  bottom: 22px;
  transform: translate(-50%, 12px);
  background: var(--ink);
  color: var(--paper);
  font: 500 0.85rem var(--font);
  font-variant-numeric: tabular-nums;
  padding: 9px 14px;
  border-radius: 2px;
  opacity: 0;
  transition: opacity 0.25s, transform 0.25s;
  pointer-events: none;
  max-width: calc(100vw - 32px);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
/* The toast slides in so a new block registers even mid-jump. */
#toast.show { opacity: 1; transform: translate(-50%, 0); }

/* Overlays */
.overlay {
  position: fixed;
  inset: 0;
  display: grid;
  place-items: center;
  background: var(--veil);
  padding: 16px;
}
.small-sheet {
  width: min(520px, 100%);
  max-height: calc(100vh - 32px);
  overflow-y: auto;
  padding: 28px;
  display: flex;
  flex-direction: column;
  gap: 16px;
}
/* Loading: covers the title while the first blocks arrive; the tower
   stacks up faintly behind it. */
body.loading #title { visibility: hidden; }
#loading { background: var(--veil-strong); transition: opacity 0.3s ease; }
#loading.done { opacity: 0; pointer-events: none; }
.loading-sheet { width: min(420px, 100%); gap: 14px; }
.meter {
  position: relative;
  height: 10px;
  border: 1px solid var(--ink);
  background: var(--bone);
  overflow: hidden;
}
#loading-fill {
  position: absolute;
  inset: 0 auto 0 0;
  width: 0;
  background: var(--accent);
  transition: width 0.25s ease;
}
.meter.indeterminate #loading-fill { width: 30%; animation: meter-slide 1.2s ease-in-out infinite; }
@keyframes meter-slide {
  from { transform: translateX(-100%); }
  to { transform: translateX(340%); }
}

#submit-area { display: flex; flex-direction: column; gap: 10px; border-top: 1px solid var(--ink); padding-top: 16px; }
.name-form { display: flex; flex-direction: column; gap: 6px; }
.name-form label { font-size: 0.85rem; color: var(--ink-2); }
.name-row { display: flex; gap: 8px; }
.name-row input {
  flex: 1;
  min-width: 0;
  min-height: 44px;
  padding: 0 12px;
  font: inherit;
  color: var(--ink);
  background: var(--bone);
  border: 1px solid var(--ink);
  border-radius: 2px;
}
.name-row input:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; }

/* Leaderboard: same ruled-ledger table as the stone legend. */
.board { display: flex; flex-direction: column; gap: 8px; border-top: 1px solid var(--ink); padding-top: 16px; }
.board-head { display: flex; justify-content: space-between; align-items: center; gap: 12px; }
.board h2 { font-size: 1.35rem; letter-spacing: -0.02em; }
.small-btn { min-height: 44px; padding: 0 14px; font-size: 0.85rem; }
#board-status.error { color: var(--accent-ink); }
#board-table { font-variant-numeric: tabular-nums; }
#board-table thead th { font-size: 0.78rem; color: var(--ink-2); font-weight: 500; border-bottom: 1px solid var(--ink); }
#board-table td, #board-table th { font-weight: 400; color: var(--ink); }
#board-table td:first-child { width: 28px; }
#board-table tr.me td { font-weight: 600; }
#board-table td:last-child { color: var(--ink-2); }

/* Touch controls */
#touch {
  position: fixed; inset: 0; touch-action: none;
  -webkit-user-select: none; user-select: none; -webkit-touch-callout: none; -webkit-tap-highlight-color: transparent;
}
#stick-zone { position: absolute; left: 0; top: 0; bottom: 0; width: 45%; }
#look-zone { position: absolute; right: 0; top: 0; bottom: 0; width: 55%; }
#stick {
  position: absolute;
  left: calc(28px + env(safe-area-inset-left));
  bottom: calc(84px + env(safe-area-inset-bottom));
  opacity: 0.55;
  transition: opacity 0.15s;
  pointer-events: none;
  width: 128px;
  height: 128px;
  border: 1px solid var(--ink);
  border-radius: 50%;
  background: var(--stick-bg);
}
/* Under a thumb the stick sits wherever it was touched, fully opaque. */
#stick.active { opacity: 1; transition: none; }
#knob {
  position: absolute;
  left: 50%;
  top: 50%;
  width: 52px;
  height: 52px;
  margin: -26px 0 0 -26px;
  border-radius: 50%;
  background: var(--ink);
}
#t-jump, #t-dash {
  position: absolute;
  width: 78px;
  height: 78px;
  border-radius: 50%;
  padding: 0;
  touch-action: none;
  transition: transform 0.08s, background-color 0.15s;
}
#t-jump { right: calc(24px + env(safe-area-inset-right)); bottom: calc(88px + env(safe-area-inset-bottom)); width: 92px; height: 92px; font-size: 1.05rem; }
#t-dash { right: calc(128px + env(safe-area-inset-right)); bottom: calc(60px + env(safe-area-inset-bottom)); background: var(--paper); color: var(--ink); }
#t-jump.down, #t-dash.down { transform: scale(0.9); }
#t-jump:hover { background: var(--ink); }
#t-dash:hover { background: var(--paper); }
/* Dash is once per jump: the button goes hollow until you land. */
#t-dash.spent { background: transparent; border-style: dashed; color: var(--ink-2); }
#fs-btn { font-weight: 600; }

/* Portrait on a phone: cover everything and ask for landscape. */
#rotate { display: none; }
@media (orientation: portrait) {
  body.touch #rotate {
    position: fixed; inset: 0; z-index: 100;
    display: grid; place-items: center;
    background: var(--bone); color: var(--ink);
    padding: 24px; text-align: center;
  }
}
.rotate-box { display: flex; flex-direction: column; align-items: center; gap: 12px; max-width: 300px; }
.rotate-h { font-weight: 700; font-size: 1.4rem; letter-spacing: -0.02em; }
.rotate-phone {
  width: 46px; height: 78px; border: 3px solid var(--ink); border-radius: 8px; margin-bottom: 12px;
  animation: rotate-hint 2.2s ease-in-out infinite;
}
@keyframes rotate-hint { 0%, 25% { transform: rotate(0deg); } 55%, 85% { transform: rotate(-90deg); } 100% { transform: rotate(0deg); } }

/* Phones held sideways are short: smaller HUD, controls closer to the edges. */
@media (pointer: coarse) and (max-height: 520px) {
  body.touch .hud-left { top: calc(8px + env(safe-area-inset-top)); left: calc(8px + env(safe-area-inset-left)); gap: 6px; max-width: min(300px, 40vw); }
  body.touch .hud-where { padding: 6px 10px 7px; }
  body.touch .where { font-size: 1rem; margin: 0 0 2px; }
  body.touch #where-detail, body.touch #feed { display: none; }
  body.touch .hud-bar { gap: 6px; }
  body.touch .chip { min-height: 30px; font-size: 0.78rem; padding: 0 9px; }
  body.touch .chip-btn { min-height: 40px; }
  body.touch .hud-gap { top: calc(8px + env(safe-area-inset-top)); right: calc(8px + env(safe-area-inset-right)); padding: 6px 10px 7px; min-width: 0; }
  body.touch .big { font-size: 2.1rem; }
  body.touch #line-info { font-size: 0.75rem; }
  #stick { width: 116px; height: 116px; left: calc(36px + env(safe-area-inset-left)); bottom: calc(24px + env(safe-area-inset-bottom)); }
  #knob { width: 48px; height: 48px; margin: -24px 0 0 -24px; }
  #t-jump { width: 88px; height: 88px; right: calc(22px + env(safe-area-inset-right)); bottom: calc(24px + env(safe-area-inset-bottom)); }
  #t-dash { width: 66px; height: 66px; right: calc(124px + env(safe-area-inset-right)); bottom: calc(26px + env(safe-area-inset-bottom)); }
  .small-sheet { padding: 18px 20px; gap: 12px; max-height: calc(100vh - 24px); }
  .overlay { padding: 12px; }
  #title { top: calc(10px + env(safe-area-inset-top)); bottom: calc(10px + env(safe-area-inset-bottom)); left: calc(10px + env(safe-area-inset-left)); padding: 20px 22px 0; gap: 14px; }
  #title h1 { font-size: 2.4rem; }
  .lede { font-size: 0.95rem; }
  /* Start stays on screen while the rest of the ledger scrolls under it. */
  #start-actions, #error-actions {
    position: sticky; bottom: 0; z-index: 1;
    background: var(--paper); border-top: 1px solid var(--rule);
    margin: 0 -22px; padding: 10px 22px calc(10px + env(safe-area-inset-bottom));
  }
  #title .board { padding-bottom: 20px; }
}
body.touch #toast { bottom: auto; top: 16px; }

@media (max-width: 640px) {
  #title {
    top: auto;
    left: 12px;
    right: 12px;
    bottom: 12px;
    width: auto;
    max-height: 72vh;
    padding: 22px 18px;
    gap: 16px;
  }
  .keys { grid-template-columns: 1fr; }
  .hud-where { left: 10px; top: 10px; max-width: calc(60vw - 20px); padding: 8px 10px; }
  .where { font-size: 1.2rem; }
  .hud-gap { right: 10px; top: 10px; min-width: 0; max-width: calc(40vw - 10px); padding: 8px 10px; }
  .big { font-size: 2.6rem; }
  .hud-bar { left: 10px; right: 10px; }
  body.touch .hud-left { top: 10px; left: 10px; max-width: calc(60vw - 20px); }
  .actions button { flex: 1 1 auto; }
}

@media (prefers-reduced-motion: reduce) {
  #toast, #loading, #loading-fill { transition: none; }
  .meter.indeterminate #loading-fill { animation: none; width: 100%; opacity: 0.35; }
}
