:root {
  --bg: #f6f3ee;
  --surface: #ffffff;
  --surface-2: #efebe4;
  --text: #1d1b18;
  --muted: #6d675e;
  --line: #d9d3c9;
  --accent: #3b5bdb;
  --accent-text: #ffffff;
  --good: #2b8a3e;
  --good-bg: #e6f4ea;
  --warn: #b35c00;
  --bad: #c92a2a;
  --endpoint: #1d1b18;
  --endpoint-text: #f6f3ee;
  --radius: 14px;
  --shadow: 0 1px 2px rgb(0 0 0 / 6%), 0 4px 14px rgb(0 0 0 / 5%);
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #16171b; --surface: #202227; --surface-2: #2a2d33; --text: #ecebe8; --muted: #9a978f;
    --line: #3a3d44; --accent: #7b93ff; --accent-text: #0f1220; --good: #69db7c; --good-bg: #1f3325;
    --warn: #ffa94d; --bad: #ff8787; --endpoint: #ecebe8; --endpoint-text: #16171b;
    --shadow: 0 1px 2px rgb(0 0 0 / 30%);
    color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #16171b; --surface: #202227; --surface-2: #2a2d33; --text: #ecebe8; --muted: #9a978f;
  --line: #3a3d44; --accent: #7b93ff; --accent-text: #0f1220; --good: #69db7c; --good-bg: #1f3325;
  --warn: #ffa94d; --bad: #ff8787; --endpoint: #ecebe8; --endpoint-text: #16171b;
  --shadow: 0 1px 2px rgb(0 0 0 / 30%);
  color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body {
  background: var(--bg);
  color: var(--text);
  font: 16px/1.45 Inter, system-ui, sans-serif;
  -webkit-font-smoothing: antialiased;
  -webkit-tap-highlight-color: transparent;
}
button, input { font: inherit; color: inherit; }

.app {
  max-width: 480px;
  margin: 0 auto;
  min-height: 100%;
  display: flex;
  flex-direction: column;
  /* The host pads :root by the phone's safe-area insets, so plain padding here. */
  padding: 16px;
}
.loading { margin: auto; color: var(--muted); }

h1, h2 { font-family: Fraunces, Georgia, serif; letter-spacing: -0.01em; margin: 0; }

/* ---------- Home ---------- */
.home { display: flex; flex-direction: column; gap: 20px; margin: auto 0; }
.logo { font-size: clamp(2.4rem, 11vw, 3.2rem); font-weight: 800; line-height: 1; }
.logo span { color: var(--accent); }
.tagline { color: var(--muted); margin: 8px 0 0; }
.demo { display: flex; align-items: center; gap: 6px; flex-wrap: wrap; font-weight: 600; }
.demo .chip { padding: 4px 10px; border-radius: 999px; background: var(--endpoint); color: var(--endpoint-text); }
.demo .chip.mid { background: var(--good-bg); color: var(--good); }
.demo .arrow { color: var(--muted); }

.menu { display: grid; gap: 10px; }
.menu-btn {
  display: flex; justify-content: space-between; align-items: center; gap: 12px;
  text-align: left; padding: 16px 18px; border-radius: var(--radius);
  background: var(--surface); border: 1px solid var(--line); box-shadow: var(--shadow); cursor: pointer;
}
.menu-btn strong { display: block; font-size: 1.05rem; }
.menu-btn small { color: var(--muted); }
.menu-btn.primary { background: var(--accent); color: var(--accent-text); border-color: transparent; }
.menu-btn.primary small { color: inherit; opacity: 0.8; }
.menu-btn .meta { font-size: 0.85rem; white-space: nowrap; opacity: 0.85; }

.diff-picker { display: grid; grid-template-columns: repeat(5, 1fr); gap: 8px; }
.diff-picker button {
  padding: 12px 0; border-radius: 10px; border: 1px solid var(--line);
  background: var(--surface); cursor: pointer; font-weight: 600;
}
.how { color: var(--muted); font-size: 0.9rem; margin: 0; }

/* ---------- Game ---------- */
.topbar { display: flex; align-items: center; justify-content: space-between; gap: 12px; margin-bottom: 12px; }
.icon-btn {
  width: 40px; height: 40px; border-radius: 10px; border: 1px solid var(--line);
  background: var(--surface); cursor: pointer; font-size: 1.1rem; line-height: 1;
}
.status { display: flex; gap: 14px; align-items: center; font-size: 0.9rem; color: var(--muted); }
.status b { color: var(--text); }
.dots { display: inline-flex; gap: 3px; vertical-align: middle; }
.dots i { width: 7px; height: 7px; border-radius: 50%; background: var(--line); }
.dots i.on { background: var(--accent); }
.lives { letter-spacing: 1px; color: var(--bad); }

.board { display: flex; flex-direction: column; align-items: stretch; gap: 0; margin: auto 0; padding: 8px 0; }
.node {
  position: relative; text-align: center; padding: 14px 16px; border-radius: var(--radius);
  font-weight: 600; font-size: 1.15rem; min-height: 54px;
  display: flex; align-items: center; justify-content: center;
}
.node + .node { margin-top: 22px; }
.node + .node::before {
  content: ""; position: absolute; top: -22px; left: 50%; width: 2px; height: 22px;
  background: var(--line); transform: translateX(-50%);
}
.node.endpoint {
  background: var(--endpoint); color: var(--endpoint-text);
  font-family: Fraunces, Georgia, serif; font-size: 1.5rem; text-transform: capitalize;
}
.node.solved { background: var(--good-bg); color: var(--good); text-transform: capitalize; }
.node.solved.just { animation: pop 0.35s ease-out; }
.node.solved.revealed { color: var(--warn); background: var(--surface-2); }
.node.open { background: var(--surface); border: 2px solid var(--line); color: var(--muted); }
.node.open.hinted { border-color: var(--accent); color: var(--text); }
.node.locked { background: transparent; border: 2px dashed var(--line); color: var(--muted); }
.pattern { font-family: ui-monospace, Menlo, monospace; letter-spacing: 0.25em; text-transform: uppercase; }

.feedback { min-height: 1.5em; text-align: center; font-size: 0.95rem; margin: 10px 0 6px; }
.feedback.good { color: var(--good); }
.feedback.warn { color: var(--warn); }
.feedback.bad { color: var(--bad); }

.controls { position: sticky; bottom: 0; background: var(--bg); padding-top: 6px; }
.guess-form { display: flex; gap: 8px; }
.guess-form input {
  flex: 1; min-width: 0; padding: 14px 16px; border-radius: var(--radius);
  border: 2px solid var(--line); background: var(--surface); font-size: 1.1rem;
}
.guess-form input:focus { outline: none; border-color: var(--accent); }
.btn {
  padding: 12px 18px; border-radius: var(--radius); border: 1px solid var(--line);
  background: var(--surface); cursor: pointer; font-weight: 600;
}
.btn.primary { background: var(--accent); color: var(--accent-text); border-color: transparent; }
.btn:disabled { opacity: 0.45; cursor: default; }
.helpers { display: grid; grid-template-columns: 1fr 1fr 1fr; gap: 8px; margin-top: 8px; }
.helpers .btn { padding: 10px 6px; font-size: 0.9rem; font-weight: 500; }
.helpers .cost { display: block; font-size: 0.72rem; color: var(--muted); font-weight: 400; }

.shake { animation: shake 0.35s; }
@keyframes shake { 20%, 60% { transform: translateX(-6px); } 40%, 80% { transform: translateX(6px); } }
@keyframes pop { 0% { transform: scale(0.9); opacity: 0.3; } 60% { transform: scale(1.04); } 100% { transform: scale(1); opacity: 1; } }

/* ---------- Result panel ---------- */
.result { text-align: center; display: grid; gap: 10px; padding: 18px; border-radius: var(--radius); background: var(--surface); box-shadow: var(--shadow); margin-top: 8px; }
.result h2 { font-size: 1.6rem; }
.result .points { font-size: 1.1rem; font-weight: 600; color: var(--accent); }
.result .alt { color: var(--muted); font-size: 0.9rem; margin: 0; }
.row { display: flex; gap: 8px; justify-content: center; }
.stars { font-size: 1.5rem; letter-spacing: 4px; }

/* ---------- Run HUD ---------- */
.status { gap: 10px; }
.clock {
  font-variant-numeric: tabular-nums; font-weight: 700; color: var(--text);
  padding: 2px 8px; border-radius: 8px; background: var(--surface-2);
}
.clock.low { color: var(--bad); animation: pulse 1s infinite; }
@keyframes pulse { 50% { opacity: 0.55; } }
.tokens { color: var(--text); font-weight: 600; }

.name-form { display: grid; gap: 8px; text-align: left; }
.name-form label { font-weight: 600; font-size: 0.95rem; }

/* ---------- Leaderboard ---------- */
.link-btn { background: none; border: none; color: var(--accent); font-weight: 600; cursor: pointer; padding: 4px 0; justify-self: start; }
.screen-title { font-size: 1.4rem; }
.tabs { display: grid; grid-template-columns: 1fr 1fr; gap: 4px; padding: 4px; background: var(--surface-2); border-radius: 12px; margin-bottom: 14px; }
.tabs button { padding: 10px; border: none; border-radius: 9px; background: transparent; font-weight: 600; cursor: pointer; color: var(--muted); }
.tabs button[aria-selected="true"] { background: var(--surface); color: var(--text); box-shadow: var(--shadow); }
.scores { list-style: none; margin: 0; padding: 0; display: grid; gap: 6px; }
.scores li {
  display: grid; grid-template-columns: 28px 1fr auto 56px; gap: 10px; align-items: center;
  padding: 12px 14px; border-radius: 12px; background: var(--surface); border: 1px solid var(--line);
}
.scores li.me { border-color: var(--accent); box-shadow: 0 0 0 2px var(--accent) inset; }
.scores li.empty { display: block; text-align: center; color: var(--muted); }
.scores .rank { font-weight: 700; color: var(--muted); }
.scores .name { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.scores .rounds { color: var(--muted); font-size: 0.85rem; }
.scores .score { font-weight: 700; text-align: right; font-variant-numeric: tabular-nums; }

@media (prefers-reduced-motion: reduce) { * { animation: none !important; } }
