:root {
  --bg: #f3eee3;
  --card: #fbf8f1;
  --ink: #1d2a33;
  --muted: #6c7780;
  --line: #ddd4c2;
  --grid: #e7dfcf;
  --land: #2f5d6b;
  --land-edge: #163a45;
  --vertex: #e0592a;
  --accent: #e0592a;
  --accent-ink: #fff;
  --good: #3c8a5d;
  --near: #d3a12a;
  --mid: #dd7a2f;
  --far: #c3473b;
  --map-land: #d3c6aa;
  --water: #dfe8e6;
  --map-guess: #e9b48a;
  --shadow: 0 1px 0 rgba(29, 42, 51, .06), 0 8px 24px -12px rgba(29, 42, 51, .25);
  --display: 'Fraunces', Georgia, serif;
  --sans: 'IBM Plex Sans', system-ui, sans-serif;
  --mono: 'IBM Plex Mono', ui-monospace, monospace;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #0f171d; --card: #16222b; --ink: #ece5d6; --muted: #8b98a2; --line: #26353f; --grid: #1c2a34;
    --land: #e7dcc3; --land-edge: #fff7e4; --vertex: #ff7a45; --accent: #ff7a45; --accent-ink: #1a0e08;
    --good: #5bb784; --near: #e3b544; --mid: #f08d45; --far: #e2675a;
    --map-land: #2a3a45; --water: #0b1318; --map-guess: #8a5a3c; --shadow: 0 8px 24px -12px rgba(0, 0, 0, .6);
    color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #0f171d; --card: #16222b; --ink: #ece5d6; --muted: #8b98a2; --line: #26353f; --grid: #1c2a34;
  --land: #e7dcc3; --land-edge: #fff7e4; --vertex: #ff7a45; --accent: #ff7a45; --accent-ink: #1a0e08;
  --good: #5bb784; --near: #e3b544; --mid: #f08d45; --far: #e2675a;
  --map-land: #2a3a45; --water: #0b1318; --map-guess: #8a5a3c; --shadow: 0 8px 24px -12px rgba(0, 0, 0, .6);
  color-scheme: dark;
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0; background: var(--bg); color: var(--ink);
  font: 16px/1.45 var(--sans);
  min-height: 100dvh;
}
button, input, select { font: inherit; color: inherit; }
a { color: inherit; }

.app {
  max-width: 520px; margin: 0 auto;
  padding: max(10px, env(safe-area-inset-top)) 16px max(16px, env(safe-area-inset-bottom));
  display: flex; flex-direction: column; min-height: 100dvh;
}
main { flex: 1; display: flex; flex-direction: column; gap: 12px; }

/* header */
.top { display: flex; align-items: center; justify-content: space-between; padding: 4px 0 10px; }
.logo { margin: 0; display: flex; align-items: center; gap: 9px; font: 800 26px/1 var(--display); letter-spacing: -.01em; }
.logo svg { width: 30px; height: 30px; }
.logo svg path { fill: var(--land); stroke: var(--land-edge); stroke-width: .8; stroke-linejoin: round; }
.logo svg circle { fill: var(--vertex); }
.tools { display: flex; gap: 2px; }
.icon { width: 40px; height: 40px; border: 0; background: none; border-radius: 10px; display: grid; place-items: center; cursor: pointer; }
.icon:hover { background: var(--grid); }
.icon svg { width: 22px; height: 22px; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; }
.icon svg .dot { fill: currentColor; }

.modes { display: grid; grid-template-columns: 1fr 1fr; background: var(--grid); border-radius: 12px; padding: 3px; margin-bottom: 12px; }
.modes button { border: 0; background: none; padding: 7px 0; border-radius: 9px; font-weight: 600; font-size: 14px; color: var(--muted); cursor: pointer; }
.modes button[aria-selected="true"] { background: var(--card); color: var(--ink); box-shadow: var(--shadow); }
#dayLabel { font-family: var(--mono); font-weight: 500; }

/* board */
.board {
  position: relative; background: var(--card); border: 1px solid var(--line); border-radius: 18px;
  box-shadow: var(--shadow); padding: 12px 12px 10px; overflow: hidden;
  background-image:
    linear-gradient(var(--grid) 1px, transparent 1px),
    linear-gradient(90deg, var(--grid) 1px, transparent 1px);
  background-size: 32px 32px; background-position: -1px -1px;
}
.board-head { display: flex; justify-content: space-between; align-items: baseline; gap: 10px; min-height: 30px; position: relative; z-index: 1; }
.angles { font-family: var(--display); font-weight: 600; font-size: 15px; color: var(--muted); }
.angles output { font: 800 28px/1 var(--display); color: var(--ink); font-variant-numeric: tabular-nums; }
.answer { font: 800 22px/1.1 var(--display); text-align: right; }
.answer.won { color: var(--good); }
.answer.lost { color: var(--far); }

.shape { display: block; width: 100%; aspect-ratio: 1 / .82; overflow: visible; }
.shape path { fill: var(--land); stroke: var(--land-edge); stroke-width: 1.5; stroke-linejoin: round; vector-effect: non-scaling-stroke; }
.shape .dots { fill: var(--vertex); stroke: var(--card); stroke-width: 1.2; }
.shape.loading path { opacity: .15; }
.shape.reveal path { animation: settle .6s ease-out; }
@keyframes settle { from { filter: brightness(1.25); } to { filter: none; } }

.ladder { list-style: none; margin: 8px 0 0; padding: 0; display: grid; grid-template-columns: repeat(6, 1fr); gap: 4px; }
.ladder li {
  text-align: center; font: 500 12px/1 var(--mono); padding: 6px 0; border-radius: 7px;
  background: var(--bg); color: var(--muted); border: 1px solid var(--line);
}
.ladder li.past { text-decoration: line-through; opacity: .55; }
.ladder li.now { background: var(--ink); color: var(--bg); border-color: var(--ink); }
.ladder li.win { background: var(--good); color: #fff; border-color: var(--good); text-decoration: none; opacity: 1; }

/* guess input */
.guess { display: flex; gap: 8px; }
.field { position: relative; flex: 1; }
.guess input {
  width: 100%; height: 48px; padding: 0 14px; border-radius: 12px; border: 1.5px solid var(--line);
  background: var(--card); outline: none; font-size: 17px;
}
.guess input:focus { border-color: var(--land); }
.guess.shake .field { animation: shake .35s; }
@keyframes shake { 20%, 60% { transform: translateX(-6px); } 40%, 80% { transform: translateX(6px); } }
.primary {
  height: 48px; padding: 0 20px; border: 0; border-radius: 12px; background: var(--accent); color: var(--accent-ink);
  font-weight: 600; cursor: pointer;
}
.primary:active { transform: translateY(1px); }
.ghost { height: 48px; padding: 0 18px; border-radius: 12px; border: 1.5px solid var(--line); background: var(--card); font-weight: 600; cursor: pointer; }
.suggest {
  position: absolute; left: 0; right: 0; bottom: calc(100% + 6px); margin: 0; padding: 4px; list-style: none; z-index: 5;
  background: var(--card); border: 1px solid var(--line); border-radius: 12px; box-shadow: var(--shadow);
}
.suggest li { padding: 9px 10px; border-radius: 8px; cursor: pointer; display: flex; justify-content: space-between; gap: 10px; }
.suggest li small { color: var(--muted); }
.suggest li[aria-selected="true"], .suggest li:hover { background: var(--grid); }
.suggest li.used { opacity: .45; }

/* guesses */
.guesses { list-style: none; margin: 0; padding: 0; display: grid; gap: 6px; }
.guesses li {
  display: grid; grid-template-columns: 1fr auto 28px 46px; align-items: center; gap: 10px;
  min-height: 42px; padding: 0 12px; border-radius: 11px; background: var(--card); border: 1px solid var(--line);
  font-size: 15px;
}
.guesses li.empty { background: none; border-style: dashed; color: var(--muted); font: 500 12px var(--mono); grid-template-columns: 1fr; }
.guesses li.new { animation: pop .35s ease-out; }
@keyframes pop { from { transform: scale(.97); opacity: 0; } }
.guesses .name { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.guesses .km { font: 500 13px var(--mono); color: var(--muted); text-align: right; }
.guesses .dir svg { width: 22px; height: 22px; display: block; margin: auto; }
.guesses .dir svg path { fill: var(--ink); }
.guesses .pct { font: 500 13px var(--mono); text-align: center; border-radius: 6px; padding: 3px 0; color: #fff; }
.guesses li.correct { border-color: var(--good); background: color-mix(in srgb, var(--good) 12%, var(--card)); }
.guesses li.correct .name { color: var(--good); }
.pct.near { background: var(--near); } .pct.mid { background: var(--mid); } .pct.far { background: var(--far); } .pct.hit { background: var(--good); }
.pct.hidden { background: var(--grid); color: var(--muted); }

/* result */
.result { background: var(--card); border: 1px solid var(--line); border-radius: 18px; padding: 14px; box-shadow: var(--shadow); display: grid; gap: 10px; }
.verdict { margin: 0; font: 600 18px/1.3 var(--display); text-align: center; }
.verdict b { font-weight: 800; }
.map-wrap { border-radius: 12px; overflow: hidden; background: var(--water); border: 1px solid var(--line); }
.minimap { display: block; width: 100%; aspect-ratio: 2 / 1; }
.minimap path { fill: var(--map-land); stroke: var(--card); stroke-width: .6; vector-effect: non-scaling-stroke; stroke-linejoin: round; }
.minimap path.guessed { fill: var(--map-guess); }
.minimap path.target { fill: var(--accent); }
.minimap circle { fill: none; stroke: var(--accent); stroke-width: 1.5; vector-effect: non-scaling-stroke; }
.result-actions { display: flex; gap: 8px; justify-content: center; }
.result-actions button { flex: 1; max-width: 200px; }
.countdown { margin: 0; text-align: center; font: 500 13px var(--mono); color: var(--muted); }

/* footer */
.foot { display: flex; justify-content: space-between; flex-wrap: wrap; gap: 6px; padding-top: 20px; font-size: 12px; color: var(--muted); }

/* toast */
.toast {
  position: fixed; left: 50%; top: 18px; transform: translate(-50%, -20px); opacity: 0; pointer-events: none;
  background: var(--ink); color: var(--bg); padding: 9px 16px; border-radius: 10px; font-weight: 500; font-size: 14px;
  transition: .2s; z-index: 20; max-width: calc(100vw - 32px);
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }

/* dialogs */
.dlg {
  width: min(440px, calc(100vw - 32px)); border: 1px solid var(--line); border-radius: 18px; padding: 20px 22px 22px;
  background: var(--card); color: var(--ink); box-shadow: 0 24px 60px -20px rgba(0, 0, 0, .45);
}
.dlg::backdrop { background: rgba(10, 16, 20, .45); }
.dlg h2 { margin: 0 0 12px; font: 800 24px/1.1 var(--display); }
.dlg h3 { margin: 18px 0 8px; font: 600 15px var(--display); }
.dlg p { margin: 0 0 10px; }
.dlg .fine { font-size: 13px; color: var(--muted); }
.dlg form { position: absolute; top: 10px; right: 10px; margin: 0; }
.close { width: 36px; height: 36px; border: 0; background: none; font-size: 26px; line-height: 1; cursor: pointer; color: var(--muted); border-radius: 9px; }
.close:hover { background: var(--grid); }
.help-ladder { list-style: none; display: flex; gap: 5px; padding: 0; margin: 0 0 12px; }
.help-ladder li { flex: 1; text-align: center; font: 500 13px var(--mono); padding: 6px 0; border-radius: 7px; border: 1px solid var(--line); background: var(--bg); }
.stat-row { display: grid; grid-template-columns: repeat(5, 1fr); gap: 6px; text-align: center; }
.stat-row b { display: block; font: 800 26px/1.1 var(--display); }
.stat-row span { font-size: 11px; color: var(--muted); line-height: 1.2; display: block; }
.dist { display: grid; gap: 5px; }
.dist .bar { display: grid; grid-template-columns: 44px 1fr; align-items: center; gap: 8px; font: 500 12px var(--mono); }
.dist .bar i { display: block; height: 22px; min-width: 24px; border-radius: 5px; background: var(--grid); color: var(--ink); font-style: normal; text-align: right; padding: 3px 7px; }
.dist .bar.hl i { background: var(--accent); color: var(--accent-ink); }
.setting { display: flex; justify-content: space-between; align-items: center; gap: 16px; padding: 12px 0; border-top: 1px solid var(--line); }
.setting small { display: block; color: var(--muted); font-size: 12.5px; }
.setting input[type="checkbox"] { width: 22px; height: 22px; accent-color: var(--accent); }
.setting select { padding: 7px 10px; border-radius: 9px; border: 1px solid var(--line); background: var(--bg); }

@media (max-width: 380px) {
  .guesses li { grid-template-columns: 1fr auto 24px 42px; gap: 7px; padding: 0 10px; font-size: 14px; }
  .stat-row b { font-size: 22px; }
}
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation: none !important; transition: none !important; }
}
