:root {
  --bg: #f4f5f2;
  --surface: #ffffff;
  --surface-2: #f7f8f6;
  --ink: #1d2733;
  --ink-2: #4b5664;
  --muted: #7b8591;
  --line: #e3e6e2;
  --line-2: #d3d8d2;
  --sea: #e9eff2;
  --accent: #243447;

  --origin: #3f72e0;
  --dest: #e0564f;
  --green: #3a9d5c;
  --yellow: #eebd2b;
  --brown: #9a6a43;
  --hollow: #7d8794;
  --route: #1d2733;
  --label-halo: rgba(255, 255, 255, 0.92);

  --radius: 14px;
  --shadow: 0 1px 2px rgba(20, 30, 40, .05), 0 4px 16px rgba(20, 30, 40, .05);
  --font: "Inter", ui-sans-serif, system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #12161b;
    --surface: #1a2027;
    --surface-2: #20272f;
    --ink: #e7ebef;
    --ink-2: #b8c0c9;
    --muted: #87919c;
    --line: #2a323b;
    --line-2: #37414c;
    --sea: #151b21;
    --accent: #e7ebef;

    --origin: #5d8cf0;
    --dest: #ee6b63;
    --green: #47b06c;
    --yellow: #f0c43c;
    --brown: #a97a52;
    --hollow: #95a0ab;
    --route: #f2f4f6;
    --label-halo: rgba(18, 22, 27, 0.9);
    --shadow: 0 1px 2px rgba(0, 0, 0, .3), 0 4px 16px rgba(0, 0, 0, .25);
    color-scheme: dark;
  }
}
:root[data-theme="dark"] {
    --bg: #12161b;
    --surface: #1a2027;
    --surface-2: #20272f;
    --ink: #e7ebef;
    --ink-2: #b8c0c9;
    --muted: #87919c;
    --line: #2a323b;
    --line-2: #37414c;
    --sea: #151b21;
    --accent: #e7ebef;

    --origin: #5d8cf0;
    --dest: #ee6b63;
    --green: #47b06c;
    --yellow: #f0c43c;
    --brown: #a97a52;
    --hollow: #95a0ab;
    --route: #f2f4f6;
    --label-halo: rgba(18, 22, 27, 0.9);
    --shadow: 0 1px 2px rgba(0, 0, 0, .3), 0 4px 16px rgba(0, 0, 0, .25);
    color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--ink);
  font-family: var(--font);
  font-size: 15px;
  line-height: 1.45;
  -webkit-font-smoothing: antialiased;
  display: flex;
  flex-direction: column;
}

button { font: inherit; color: inherit; }
svg { display: block; }

/* ------------------------------------------------------------ top bar */
.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 12px 20px;
  border-bottom: 1px solid var(--line);
  background: var(--surface);
}
.brand { display: flex; align-items: center; gap: 10px; font-weight: 700; font-size: 18px; letter-spacing: -0.01em; }
.logo { width: 30px; height: 30px; }
.logo rect { fill: var(--accent); }
.logo path { fill: none; stroke: var(--bg); stroke-width: 3; stroke-linecap: round; }
.logo .o { fill: var(--origin); }
.logo .d { fill: var(--dest); }
.actions { display: flex; align-items: center; gap: 6px; }

.icon-btn {
  width: 38px; height: 38px;
  display: inline-grid; place-items: center;
  border: 1px solid transparent;
  border-radius: 10px;
  background: transparent;
  cursor: pointer;
  color: var(--ink-2);
  transition: background .15s, color .15s;
}
.icon-btn:hover { background: var(--surface-2); color: var(--ink); }
.icon-btn svg { width: 20px; height: 20px; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; }
.icon-btn svg .fill { fill: currentColor; }
.icon-btn.small { width: 34px; height: 34px; background: var(--surface); border-color: var(--line); box-shadow: var(--shadow); }
.icon-btn.small svg { width: 18px; height: 18px; }

.btn {
  border-radius: 10px;
  padding: 8px 14px;
  font-weight: 600;
  font-size: 14px;
  border: 1px solid var(--line-2);
  background: var(--surface);
  cursor: pointer;
  transition: background .15s, border-color .15s, transform .05s;
  white-space: nowrap;
}
.btn:active { transform: translateY(1px); }
.btn-primary { background: var(--accent); color: var(--bg); border-color: var(--accent); }
.btn-primary:hover { opacity: .92; }
.btn-ghost:hover { background: var(--surface-2); }
.btn[disabled] { opacity: .45; cursor: default; }

/* ------------------------------------------------------------ layout */
.layout {
  flex: 1;
  min-height: 0;
  display: grid;
  grid-template-columns: minmax(0, 1fr) 360px;
  gap: 16px;
  padding: 16px;
}
.map-wrap {
  position: relative;
  min-height: 420px;
  border-radius: var(--radius);
  background: var(--sea);
  border: 1px solid var(--line);
  overflow: hidden;
}
#map { width: 100%; height: 100%; position: absolute; inset: 0; cursor: grab; touch-action: none; }
#map:active { cursor: grabbing; }

.map-tools { position: absolute; top: 12px; right: 12px; display: flex; flex-direction: column; gap: 6px; }

.legend {
  position: absolute; left: 12px; bottom: 12px;
  display: flex; flex-wrap: wrap; gap: 6px 14px;
  padding: 7px 12px;
  background: color-mix(in srgb, var(--surface) 88%, transparent);
  border: 1px solid var(--line);
  border-radius: 10px;
  font-size: 12px;
  color: var(--ink-2);
  backdrop-filter: blur(6px);
}
.legend span { display: inline-flex; align-items: center; gap: 6px; }

.sw { display: inline-block; width: 12px; height: 12px; border-radius: 3px; flex: none; }
.sw.green { background: var(--green); }
.sw.yellow { background: var(--yellow); }
.sw.brown { background: var(--brown); }
.sw.hollow { border: 1.5px solid var(--hollow); }
.sw.origin { background: var(--origin); }
.sw.dest { background: var(--dest); }

/* ------------------------------------------------------------ panel */
.panel { display: flex; flex-direction: column; gap: 12px; min-height: 0; }
.card {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 16px;
  box-shadow: var(--shadow);
}
.eyebrow {
  display: block;
  font-size: 11.5px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: .07em;
  color: var(--muted);
  margin: 0 0 8px;
}
.muted { color: var(--muted); }
.small { font-size: 13px; }

.journey {
  margin: 0;
  display: flex; flex-wrap: wrap; align-items: center; gap: 4px 10px;
  font-size: 22px; line-height: 1.2; font-weight: 750; letter-spacing: -0.015em;
}
.journey .arrow { width: 22px; height: 22px; fill: none; stroke: var(--muted); stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }
.place { display: inline-flex; align-items: center; gap: 8px; }
.place::before { content: ""; width: 12px; height: 12px; border-radius: 3px; flex: none; }
.place.origin::before { background: var(--origin); }
.place.dest::before { background: var(--dest); }
.sub { margin: 8px 0 14px; color: var(--ink-2); font-size: 13.5px; }
.t-origin { color: var(--origin); }
.t-dest { color: var(--dest); }

.stats-row { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.stat { background: var(--surface-2); border: 1px solid var(--line); border-radius: 10px; padding: 10px 12px; }
.stat .num { font-size: 22px; font-weight: 750; font-variant-numeric: tabular-nums; }
.stat .lbl { font-size: 12px; color: var(--muted); }

/* ------------------------------------------------------------ combobox */
.combo { position: relative; }
.combo input {
  width: 100%;
  padding: 11px 40px 11px 13px;
  border-radius: 10px;
  border: 1px solid var(--line-2);
  background: var(--surface-2);
  color: var(--ink);
  font: inherit;
  font-size: 15px;
  outline: none;
  transition: border-color .15s, box-shadow .15s;
}
.combo input:focus { border-color: var(--origin); box-shadow: 0 0 0 3px color-mix(in srgb, var(--origin) 22%, transparent); background: var(--surface); }
.combo input:disabled { opacity: .55; }
.combo-toggle {
  position: absolute; right: 4px; top: 50%; transform: translateY(-50%);
  width: 34px; height: 34px; border: 0; background: transparent; cursor: pointer; color: var(--muted);
  display: grid; place-items: center;
}
.combo-toggle svg { width: 18px; height: 18px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }
.combo-list {
  position: absolute; z-index: 20; left: 0; right: 0; top: calc(100% + 6px);
  margin: 0; padding: 6px; list-style: none;
  max-height: 280px; overflow-y: auto;
  background: var(--surface);
  border: 1px solid var(--line-2);
  border-radius: 12px;
  box-shadow: 0 10px 30px rgba(10, 20, 30, .14);
}
.combo-list li {
  padding: 8px 10px; border-radius: 8px; cursor: pointer;
  display: flex; justify-content: space-between; gap: 8px; align-items: baseline;
}
.combo-list li .alias { font-size: 12px; color: var(--muted); }
.combo-list li[aria-selected="true"] { background: var(--surface-2); outline: 1px solid var(--line-2); }
.combo-list li mark { background: none; color: inherit; font-weight: 700; }
.combo-list .none { color: var(--muted); cursor: default; }

.entry-msg { min-height: 20px; margin-top: 6px; font-size: 13px; color: var(--ink-2); }
.entry-msg.warn { color: var(--dest); }
.entry-actions { display: flex; justify-content: flex-end; gap: 8px; }

.shake { animation: shake .35s; }
@keyframes shake { 20%, 60% { transform: translateX(-5px); } 40%, 80% { transform: translateX(5px); } }

/* ------------------------------------------------------------ guess list */
.guesses { flex: 1; min-height: 140px; overflow: auto; }
.guess-list { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 6px; }
.guess-list li {
  display: grid; grid-template-columns: 22px 14px 1fr; align-items: center; gap: 10px;
  padding: 8px 10px; border-radius: 10px; background: var(--surface-2); border: 1px solid var(--line);
  animation: pop .25s ease-out;
}
.guess-list .n { font-size: 12px; color: var(--muted); font-variant-numeric: tabular-nums; text-align: right; }
.guess-list .name { font-weight: 600; }
.guess-list .note { display: block; font-size: 12px; color: var(--muted); font-weight: 400; }
.guess-list li.recent { border-color: var(--line-2); }
@keyframes pop { from { opacity: 0; transform: translateY(-4px); } to { opacity: 1; transform: none; } }
.empty { color: var(--muted); font-size: 13.5px; margin: 4px 0 0; }

/* ------------------------------------------------------------ map */
.country path { stroke-linejoin: round; transition: fill .4s, stroke .4s; }
.country.origin path { fill: var(--origin); stroke: color-mix(in srgb, var(--origin) 70%, black); stroke-width: .8; }
.country.dest path { fill: var(--dest); stroke: color-mix(in srgb, var(--dest) 70%, black); stroke-width: .8; }
.country.green path { fill: var(--green); stroke: color-mix(in srgb, var(--green) 70%, black); stroke-width: .8; }
.country.yellow path { fill: var(--yellow); stroke: color-mix(in srgb, var(--yellow) 70%, black); stroke-width: .8; }
.country.brown path { fill: var(--brown); stroke: color-mix(in srgb, var(--brown) 70%, black); stroke-width: .8; }
.country.hollow path { fill: transparent; stroke: var(--hollow); stroke-width: 1.4; }
.country.reveal path { fill: color-mix(in srgb, var(--green) 14%, transparent); stroke: var(--green); stroke-width: 1.4; stroke-dasharray: 4 3; }
.country.fresh path { animation: flash .9s ease-out; }
@keyframes flash { 0% { opacity: 0; } 100% { opacity: 1; } }

.trail-halo { fill: none; stroke: var(--label-halo); stroke-width: 7; stroke-linecap: round; stroke-linejoin: round; opacity: .8; }
.trail { fill: none; stroke: var(--route); stroke-width: 2.6; stroke-linecap: round; stroke-linejoin: round; }
.trail.complete { stroke-width: 3.2; }
.trail.reveal { stroke: var(--green); stroke-dasharray: 2 6; stroke-width: 2.6; }
.trail-dot { fill: var(--route); stroke: var(--label-halo); stroke-width: 2; }

.labels text {
  font-family: var(--font);
  font-size: 12.5px;
  font-weight: 650;
  fill: var(--ink);
  paint-order: stroke;
  stroke: var(--label-halo);
  stroke-width: 3.5px;
  stroke-linejoin: round;
  text-anchor: middle;
  dominant-baseline: middle;
  pointer-events: none;
}
.labels text.endpoint { font-size: 14.5px; font-weight: 750; }
.labels text.hollow { fill: var(--ink-2); font-weight: 550; }
.labels text.reveal { fill: var(--green); font-style: italic; }

/* ------------------------------------------------------------ modals */
.modal {
  border: 0; padding: 0; border-radius: 18px;
  width: min(520px, calc(100vw - 32px));
  max-height: calc(100vh - 40px);
  background: var(--surface); color: var(--ink);
  box-shadow: 0 24px 60px rgba(0, 0, 0, .25);
}
.modal::backdrop { background: rgba(14, 20, 27, .45); backdrop-filter: blur(2px); }
.modal[open] { animation: rise .2s ease-out; }
@keyframes rise { from { opacity: 0; transform: translateY(8px); } to { opacity: 1; transform: none; } }
.modal-inner { padding: 20px 22px 18px; }
.modal-inner > header { display: flex; justify-content: space-between; align-items: center; margin-bottom: 8px; }
.modal-inner h2 { margin: 0; font-size: 20px; letter-spacing: -0.01em; }
.modal-inner h3 { margin-top: 18px; }
.modal-inner p { color: var(--ink-2); }
.modal-actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 18px; flex-wrap: wrap; }
.key { list-style: none; padding: 0; margin: 0 0 12px; display: grid; gap: 8px; color: var(--ink-2); }
.key li { display: flex; gap: 10px; align-items: baseline; }
.key .sw { transform: translateY(1px); }

.seg { border: 0; padding: 0; margin: 4px 0 16px; display: grid; grid-template-columns: repeat(4, 1fr); gap: 6px; }
.seg legend { font-weight: 650; margin-bottom: 8px; padding: 0; }
.seg label { position: relative; }
.seg input { position: absolute; opacity: 0; }
.seg span {
  display: flex; flex-direction: column; align-items: center; gap: 2px;
  padding: 9px 4px; border: 1px solid var(--line-2); border-radius: 10px; cursor: pointer;
  font-weight: 650; font-size: 14px; text-align: center;
}
.seg small { font-weight: 450; font-size: 11.5px; color: var(--muted); }
.seg input:checked + span { border-color: var(--accent); box-shadow: inset 0 0 0 1px var(--accent); background: var(--surface-2); }
.seg input:focus-visible + span { outline: 2px solid var(--origin); outline-offset: 2px; }

.toggle { display: grid; grid-template-columns: 40px 1fr; gap: 12px; align-items: start; margin: 12px 0; cursor: pointer; position: relative; }
.toggle input { position: absolute; opacity: 0; }
.toggle .track { width: 40px; height: 24px; border-radius: 12px; background: var(--line-2); position: relative; transition: background .15s; margin-top: 2px; }
.toggle .track::after { content: ""; position: absolute; top: 3px; left: 3px; width: 18px; height: 18px; border-radius: 50%; background: #fff; transition: transform .15s; box-shadow: 0 1px 2px rgba(0,0,0,.2); }
.toggle input:checked + .track { background: var(--green); }
.toggle input:checked + .track::after { transform: translateX(16px); }
.toggle input:focus-visible + .track { outline: 2px solid var(--origin); outline-offset: 2px; }
.toggle small { display: block; color: var(--muted); font-size: 12.5px; }

.stat-grid { display: grid; grid-template-columns: repeat(4, 1fr); gap: 8px; margin: 8px 0 4px; }
.stat-grid .stat { text-align: center; }
.dist { display: grid; gap: 6px; }
.dist .row { display: grid; grid-template-columns: 110px 1fr; gap: 10px; align-items: center; font-size: 13px; }
.dist .bar { height: 22px; border-radius: 6px; background: var(--surface-2); position: relative; overflow: hidden; }
.dist .bar i { position: absolute; inset: 0 auto 0 0; background: var(--accent); opacity: .85; border-radius: 6px; min-width: 2px; }
.dist .bar b { position: absolute; right: 8px; top: 50%; transform: translateY(-50%); font-size: 12px; font-weight: 650; }

.result .rank-badge { display: flex; gap: 6px; margin: 2px 0 12px; }
.result .rank-badge i { width: 28px; height: 8px; border-radius: 4px; background: var(--line-2); }
.result .rank-badge i.on { background: var(--green); }
.res-summary { font-size: 15px; margin: 0 0 12px; }
.res-emoji { font-size: 22px; letter-spacing: 2px; margin-bottom: 14px; word-break: break-all; }
.res-route { background: var(--surface-2); border: 1px solid var(--line); border-radius: 12px; padding: 12px 14px; }
.res-route #res-route { display: flex; flex-wrap: wrap; gap: 4px 6px; align-items: center; font-weight: 600; }
.res-route .sep { color: var(--muted); font-weight: 400; }

.toast {
  position: fixed; left: 50%; bottom: 24px; transform: translate(-50%, 20px);
  background: var(--accent); color: var(--bg);
  padding: 10px 16px; border-radius: 10px; font-weight: 600; font-size: 14px;
  opacity: 0; pointer-events: none; transition: opacity .2s, transform .2s; z-index: 100;
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }

/* ------------------------------------------------------------ responsive */
@media (max-width: 900px) {
  body { height: auto; }
  .layout { grid-template-columns: 1fr; padding: 12px 16px 16px; }
  .layout { gap: 10px; }
  .map-wrap { min-height: 0; height: 52vh; order: 2; }
  .panel { display: contents; }
  .brief { order: 0; padding: 14px; }
  .brief .sub { display: none; }
  .brief .stats-row { margin-top: 12px; }
  .stat { padding: 8px 10px; }
  .stat .num { font-size: 19px; }
  .entry { order: 1; padding: 14px; position: relative; z-index: 5; }
  .entry-msg:empty { display: none; }
  .entry-actions { margin-top: 8px; }
  .guesses { order: 3; overflow: visible; }
  .topbar { padding: 10px 16px; }
  .brand span { display: none; }
  .journey { font-size: 19px; }
  .stat-grid { grid-template-columns: repeat(2, 1fr); }
  .seg { grid-template-columns: repeat(2, 1fr); }
  .legend { font-size: 11px; gap: 4px 10px; right: 12px; }
}
