:root {
  --surface: #fcfcfb;
  --surface-2: #f3f2ef;
  --sea: #eef3f8;
  --land: #dcdad3;
  --border-land: #fcfcfb;
  --border: #e3e1db;
  --text: #0b0b0b;
  --text-2: #52514e;
  --muted: #74736e;
  --accent: #2a78d6;
  --accent-ink: #fff;
  --danger: #c43c3b;
  --correct: #008300;
  /* Heat ramp, cold → hot (validated ordinal ramp; light end clears 2:1). */
  --t0: #ec9a68; --t1: #e57a42; --t2: #d95f28; --t3: #b0461a; --t4: #7e2c0c;
  /* Arrow ink on each heat colour. */
  --i0: #1a1a19; --i1: #1a1a19; --i2: #fff; --i3: #fff; --i4: #fff;
  --radius: 12px;
  --shadow: 0 1px 2px rgb(0 0 0 / 6%), 0 6px 24px rgb(0 0 0 / 8%);
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --surface: #1a1a19;
    --surface-2: #242422;
    --sea: #141c26;
    --land: #3a3936;
    --border-land: #1a1a19;
    --border: #34332f;
    --text: #f0efec;
    --text-2: #c3c2b7;
    --muted: #9b9a92;
    --accent: #3987e5;
    --danger: #e66767;
    --correct: #3fbf5f;
    /* On dark, hotter = brighter. */
    --t0: #8a2f0d; --t1: #c24a1c; --t2: #eb6834; --t3: #ee8c56; --t4: #f2b48a;
    --i0: #fff; --i1: #fff; --i2: #1a1a19; --i3: #1a1a19; --i4: #1a1a19;
    --shadow: 0 1px 2px rgb(0 0 0 / 30%), 0 8px 28px rgb(0 0 0 / 40%);
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body {
  background: var(--surface);
  color: var(--text);
  font: 16px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  -webkit-font-smoothing: antialiased;
  display: flex;
  flex-direction: column;
}
button { font: inherit; color: inherit; cursor: pointer; }
[hidden] { display: none !important; }
.sr-only { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }
.sprite { position: absolute; }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

/* Header */
.bar {
  display: flex; align-items: center; gap: 12px;
  padding: 10px max(16px, env(safe-area-inset-left)) 10px max(16px, env(safe-area-inset-right));
  border-bottom: 1px solid var(--border);
}
.brand { display: flex; align-items: center; gap: 8px; margin: 0; font-size: 20px; font-weight: 750; letter-spacing: -0.02em; }
.brand-icon { width: 24px; height: 24px; color: var(--accent); }
.mode { display: flex; background: var(--surface-2); border-radius: 999px; padding: 3px; margin-left: auto; }
.mode button { border: 0; background: none; padding: 5px 12px; border-radius: 999px; font-size: 14px; color: var(--text-2); }
.mode button[aria-pressed="true"] { background: var(--surface); color: var(--text); font-weight: 600; box-shadow: 0 1px 3px rgb(0 0 0 / 12%); }
.tools { display: flex; gap: 2px; }
.icon-btn {
  display: inline-grid; place-items: center; width: 38px; height: 38px;
  border: 0; border-radius: 10px; background: none; color: var(--text-2); font-size: 20px; line-height: 1;
}
.icon-btn:hover { background: var(--surface-2); color: var(--text); }
.icon-btn svg { width: 22px; height: 22px; }

/* Layout */
.layout { flex: 1; display: grid; grid-template-columns: 1fr minmax(320px, 400px); min-height: 0; }
.map-wrap { position: relative; background: var(--sea); overflow: hidden; min-height: 0; }
.map { width: 100%; height: 100%; display: block; touch-action: none; cursor: grab; user-select: none; }
.map.dragging { cursor: grabbing; }
.map .country { fill: var(--land); stroke: var(--border-land); stroke-width: 0.6px; vector-effect: non-scaling-stroke; }
.map .country.answer { fill: color-mix(in srgb, var(--correct) 35%, var(--land)); }
.panel { overflow-y: auto; padding: 20px 20px 32px; border-left: 1px solid var(--border); }

/* Map pins */
.pin circle { stroke: var(--surface); stroke-width: 2px; vector-effect: non-scaling-stroke; }
.pin .arrow { stroke: var(--text); stroke-width: 2px; vector-effect: non-scaling-stroke; fill: none; stroke-linecap: round; stroke-linejoin: round; }
.pin text { fill: var(--text); paint-order: stroke; stroke: var(--surface); stroke-width: 3px; stroke-linejoin: round; font-weight: 600; }
.pin.latest circle { stroke: var(--text); }
.pin.target circle { fill: var(--correct); }
.t0 { fill: var(--t0); background: var(--t0); }
.t1 { fill: var(--t1); background: var(--t1); }
.t2 { fill: var(--t2); background: var(--t2); }
.t3 { fill: var(--t3); background: var(--t3); }
.t4 { fill: var(--t4); background: var(--t4); }
.t5 { fill: var(--correct); background: var(--correct); }

.zoom { position: absolute; right: 12px; bottom: 12px; display: flex; flex-direction: column; background: var(--surface); border-radius: 10px; box-shadow: var(--shadow); }
.legend {
  position: absolute; left: 12px; bottom: 12px; margin: 0; padding: 6px 10px; list-style: none;
  display: flex; gap: 10px; font-size: 12px; color: var(--text-2);
  background: color-mix(in srgb, var(--surface) 88%, transparent); border-radius: 8px;
}
.legend li { display: flex; align-items: center; gap: 4px; }
.legend i, .how-heat i, .swatch { display: inline-block; width: 10px; height: 10px; border-radius: 50%; }

/* Guess form */
.puzzle-label { margin: 0 0 10px; font-size: 13px; font-weight: 600; letter-spacing: 0.04em; text-transform: uppercase; color: var(--muted); }
.guess-form { display: flex; gap: 8px; }
.combo { position: relative; flex: 1; }
.combo input {
  width: 100%; height: 46px; padding: 0 14px; font-size: 17px;
  border: 1.5px solid var(--border); border-radius: var(--radius); background: var(--surface); color: var(--text);
}
.combo input:focus { outline: none; border-color: var(--accent); }
.suggestions {
  position: absolute; z-index: 5; top: calc(100% + 4px); left: 0; right: 0; margin: 0; padding: 4px; list-style: none;
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); box-shadow: var(--shadow);
}
.suggestions li { display: flex; gap: 10px; align-items: baseline; padding: 8px 10px; border-radius: 8px; cursor: pointer; }
.suggestions li[aria-selected="true"], .suggestions li:hover { background: var(--surface-2); }
.suggestions .flag { font-size: 18px; }
.suggestions .sub { color: var(--muted); font-size: 14px; margin-left: auto; text-align: right; }
.suggestions li.used { opacity: 0.45; }

.btn {
  height: 46px; padding: 0 16px; border-radius: var(--radius);
  border: 1.5px solid var(--border); background: var(--surface); font-weight: 600;
}
.btn:hover { background: var(--surface-2); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
.btn.primary:hover { filter: brightness(1.08); }
.btn.danger { background: var(--danger); border-color: var(--danger); color: #fff; }
.btn.wide { width: 100%; margin-top: 8px; }
.btn:disabled { opacity: 0.45; cursor: default; }
.link-btn { border: 0; background: none; color: var(--muted); font-size: 14px; text-decoration: underline; padding: 6px 0; margin-top: 12px; }
.form-msg:empty { min-height: 8px; }
.form-msg { min-height: 20px; margin: 6px 2px 0; font-size: 14px; color: var(--danger); }

/* Hints */
.hints { display: flex; flex-wrap: wrap; align-items: center; gap: 8px; margin: 6px 0 16px; }
.hints .btn { height: 34px; font-size: 14px; padding: 0 12px; }
.cost { color: var(--muted); font-weight: 500; margin-left: 2px; }
.hint-list { display: contents; list-style: none; }
.hint-list li { padding: 5px 10px; border-radius: 999px; background: var(--surface-2); font-size: 14px; }
.hint-list b { color: var(--muted); font-weight: 500; margin-right: 4px; }

/* Guess list */
.guesses { list-style: none; margin: 0; padding: 0; display: grid; gap: 6px; }
.guess {
  display: grid; grid-template-columns: 34px 1fr auto; align-items: center; gap: 10px;
  padding: 8px 12px; border-radius: var(--radius); background: var(--surface-2);
}
.guess .dir { width: 34px; height: 34px; border-radius: 50%; display: grid; place-items: center; color: #fff; }
.guess .dir svg { width: 20px; height: 20px; }
.guess .dir.t0 { color: var(--i0); } .guess .dir.t1 { color: var(--i1); } .guess .dir.t2 { color: var(--i2); }
.guess .dir.t3 { color: var(--i3); } .guess .dir.t4 { color: var(--i4); }
.guess .name { font-weight: 600; line-height: 1.2; }
.guess .country { display: block; font-weight: 400; font-size: 13px; color: var(--muted); }
.guess .nums { text-align: right; font-variant-numeric: tabular-nums; line-height: 1.2; }
.guess .km { font-weight: 600; }
.guess .meta { display: block; font-size: 13px; color: var(--muted); }
.guess.fresh { animation: pop 0.35s ease-out; }
@keyframes pop { from { transform: translateY(-6px); opacity: 0; } }
.empty { color: var(--muted); font-size: 15px; }

/* Result card */
.result { border: 1.5px solid var(--border); border-radius: var(--radius); padding: 16px; margin-bottom: 16px; }
.result h2 { margin: 0 0 2px; font-size: 22px; letter-spacing: -0.01em; }
.result .verdict { margin: 0 0 12px; color: var(--text-2); }
.result .flag-big { font-size: 34px; float: right; line-height: 1; }
.facts { display: grid; grid-template-columns: auto 1fr; gap: 4px 14px; margin: 0 0 14px; font-size: 14px; }
.facts dt { color: var(--muted); }
.facts dd { margin: 0; }
.result .row { display: flex; gap: 8px; flex-wrap: wrap; }
.result .row.more { margin-top: 8px; }
.result .row .btn { flex: 1; height: 42px; font-size: 15px; padding: 0 10px; white-space: nowrap; }
.result .next { margin: 12px 0 0; font-size: 14px; color: var(--muted); }
.result a { color: var(--accent); }

/* Dialogs */
.modal {
  width: min(420px, calc(100vw - 32px)); border: 0; border-radius: 16px; padding: 22px;
  background: var(--surface); color: var(--text); box-shadow: var(--shadow);
}
.modal::backdrop { background: rgb(0 0 0 / 45%); }
.modal h2 { margin: 0 0 10px; font-size: 20px; }
.modal .row { display: flex; gap: 8px; margin-top: 12px; }
.modal .row .btn { flex: 1; }
.muted { color: var(--muted); }
.small { font-size: 13px; }
.how { list-style: none; padding: 0; margin: 14px 0; display: grid; gap: 12px; }
.how li { display: grid; grid-template-columns: 44px 1fr; gap: 10px; align-items: center; }
.how-arrow, .how-heat, .how-hint { display: grid; place-items: center; height: 36px; border-radius: 10px; background: var(--surface-2); }
.how-arrow svg { width: 20px; height: 20px; transform: rotate(45deg); color: var(--accent); }
.how-heat { display: flex; justify-content: center; gap: 2px; }
.how-hint { font-weight: 700; color: var(--text-2); }
.setting { border: 0; padding: 0; margin: 0 0 12px; display: flex; gap: 18px; flex-wrap: wrap; }
.setting legend { font-weight: 600; margin-bottom: 6px; }

/* Stats */
.tiles { display: grid; grid-template-columns: repeat(4, 1fr); gap: 8px; margin-bottom: 18px; }
.tile { text-align: center; }
.tile b { display: block; font-size: 26px; font-variant-numeric: tabular-nums; line-height: 1.1; }
.tile span { font-size: 12px; color: var(--muted); }
.dist-title { font-size: 14px; font-weight: 600; margin: 0 0 8px; }
.dist { display: grid; grid-template-columns: 34px 1fr; gap: 4px 8px; align-items: center; font-size: 13px; font-variant-numeric: tabular-nums; }
.dist .lbl { color: var(--text-2); text-align: right; }
.dist .track { display: flex; align-items: center; gap: 6px; }
.dist .fill { height: 18px; min-width: 2px; border-radius: 0 4px 4px 0; background: var(--accent); opacity: 0.55; }
.dist .fill.today { opacity: 1; }
.dist .val { color: var(--text-2); }

.toast {
  position: fixed; left: 50%; top: 70px; transform: translateX(-50%); z-index: 20;
  background: var(--text); color: var(--surface); padding: 8px 16px; border-radius: 999px; font-weight: 600; font-size: 14px;
}

@media (max-width: 760px) {
  body { height: auto; min-height: 100%; }
  .layout { display: flex; flex-direction: column; }
  .map-wrap { height: min(46vh, 62vw); min-height: 200px; flex: none; }
  .panel { border-left: 0; padding: 16px 16px 40px; overflow: visible; }
  .brand { font-size: 18px; }
  .legend { gap: 6px; font-size: 11px; padding: 4px 8px; left: 8px; bottom: 8px; }
  .zoom { right: 8px; bottom: 8px; }
  .zoom .icon-btn { width: 34px; height: 34px; }
}
@media (max-width: 420px) {
  .bar { gap: 6px; }
  .brand span, .brand { font-size: 0; }
  .brand-icon { width: 26px; height: 26px; }
  .legend li:nth-child(2), .legend li:nth-child(4) { display: none; }
}
@media (prefers-reduced-motion: reduce) {
  * { animation: none !important; transition: none !important; }
}
