:root {
  --bg: #0d1418;
  --surface: #152129;
  --surface-2: #1c2c36;
  --border: #2d4250;
  --text: #e6f1f4;
  --muted: #a9bec8;
  --accent: #2ed3c6;
  --accent-text: #04201d;
  --danger: #ff8a80;
  --warn: #ffd166;
  --focus: #ffd166;
  --map-bg: #0a1014;
  --link: #ffd166;
  --link-current: #ff5ec4;
  --field: #2ed3c6;
  --portal: #e6f1f4;
  --portal-stroke: #0d1418;
  --radius: 10px;
  --shadow: 0 1px 2px rgb(0 0 0 / 0.35);
  color-scheme: dark;
}

@media (prefers-color-scheme: light) {
  :root:not([data-theme="dark"]) {
    --bg: #f4f7f9;
    --surface: #ffffff;
    --surface-2: #eaf0f3;
    --border: #b7c7d0;
    --text: #10202a;
    --muted: #46606e;
    --accent: #00756c;
    --accent-text: #ffffff;
    --danger: #b3261e;
    --warn: #7a5300;
    --focus: #1a5fd6;
    --map-bg: #dfe8ec;
    --link: #b06d00;
    --link-current: #c2187f;
    --field: #00756c;
    --portal: #10202a;
    --portal-stroke: #ffffff;
    --shadow: 0 1px 3px rgb(16 32 42 / 0.12);
    color-scheme: light;
  }
}

*, *::before, *::after { box-sizing: border-box; }

[hidden] { display: none !important; }

html { -webkit-text-size-adjust: 100%; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 1rem/1.5 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}

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

.visually-hidden {
  position: absolute !important;
  width: 1px; height: 1px;
  margin: -1px; padding: 0;
  overflow: hidden; clip: rect(0 0 0 0);
  white-space: nowrap; border: 0;
}

.skip-link {
  position: absolute;
  left: 1rem; top: -4rem;
  z-index: 10;
  padding: .6rem 1rem;
  background: var(--accent); color: var(--accent-text);
  border-radius: var(--radius);
  font-weight: 600;
}
.skip-link:focus { top: 1rem; }

h1, h2, h3 { line-height: 1.2; margin: 0; }
h2 { font-size: 1.15rem; display: flex; align-items: center; gap: .5rem; margin-bottom: .9rem; }
h3 { font-size: 1rem; margin: 1.25rem 0 .5rem; }
code { font-size: .9em; background: var(--surface-2); padding: .05em .3em; border-radius: 4px; }

.app-header {
  display: flex; align-items: center; gap: .9rem;
  padding: 1rem clamp(1rem, 3vw, 2rem);
  border-bottom: 1px solid var(--border);
}
.app-header h1 { font-size: 1.4rem; letter-spacing: .02em; }
.tagline { margin: 0; color: var(--muted); font-size: .9rem; }
.logo { width: 40px; height: 40px; color: var(--accent); flex: none; }
#install-btn { margin-left: auto; }

main { padding: 1rem clamp(1rem, 3vw, 2rem) 2rem; }
main:focus { outline: none; }

.layout {
  display: grid;
  gap: 1.25rem;
  grid-template-columns: minmax(0, 1fr);
}
@media (min-width: 64rem) {
  .layout { grid-template-columns: minmax(0, 1.5fr) minmax(22rem, 1fr); align-items: start; }
  .map-pane { position: sticky; top: 1rem; }
}

.card, .map-pane {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
}
.map-pane { padding: .75rem; min-width: 0; }
.panel { display: grid; gap: 1.25rem; min-width: 0; }
.card { padding: 1.1rem; min-width: 0; }

.num {
  display: inline-grid; place-items: center;
  width: 1.6rem; height: 1.6rem;
  border-radius: 50%;
  background: var(--accent); color: var(--accent-text);
  font-size: .9rem; font-weight: 700;
}
.badge {
  margin-left: auto;
  padding: .1rem .55rem;
  border-radius: 999px;
  background: var(--surface-2);
  font-size: .85rem; color: var(--muted);
}

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: .4rem;
  min-height: 2.75rem; min-width: 2.75rem;
  padding: .5rem 1rem;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface-2);
  color: var(--text);
  font: inherit; font-weight: 600;
  cursor: pointer;
}
.btn:hover:not(:disabled) { border-color: var(--accent); }
.btn:disabled { opacity: .5; cursor: not-allowed; }
.btn-primary { background: var(--accent); color: var(--accent-text); border-color: var(--accent); }
.btn-ghost { background: transparent; }
.btn-danger { color: var(--danger); }
.btn-icon { padding: .4rem; font-size: 1.1rem; }
.btn-group { display: inline-flex; gap: .25rem; }
.row { display: flex; gap: .6rem; align-items: center; margin-top: .9rem; }
.row.wrap { flex-wrap: wrap; }

.map-toolbar { display: flex; flex-wrap: wrap; gap: .6rem; align-items: center; margin-bottom: .6rem; }

.segmented {
  display: inline-flex; margin: 0; padding: 0; border: 0;
}
.segmented label {
  display: inline-flex; align-items: center; gap: .35rem;
  min-height: 2.75rem; padding: .4rem .8rem;
  border: 1px solid var(--border);
  background: var(--surface-2);
  cursor: pointer; font-weight: 600; font-size: .9rem;
}
.segmented label:first-of-type { border-radius: var(--radius) 0 0 var(--radius); }
.segmented label:last-of-type { border-radius: 0 var(--radius) var(--radius) 0; border-left: 0; }
.segmented label:has(input:checked) { background: var(--accent); color: var(--accent-text); border-color: var(--accent); }
.segmented label:has(input:focus-visible) { outline: 3px solid var(--focus); outline-offset: 2px; }
.segmented input { accent-color: var(--accent); }

.check { display: inline-flex; align-items: center; gap: .4rem; min-height: 2.75rem; cursor: pointer; }
.check input, .color-item input { width: 1.2rem; height: 1.2rem; accent-color: var(--accent); }

.map-scroll {
  position: relative;
  overflow: auto;
  max-height: 72vh;
  min-height: 16rem;
  background: var(--map-bg);
  border-radius: calc(var(--radius) - 2px);
  overscroll-behavior: contain;
}
#map { display: block; width: 100%; height: auto; touch-action: manipulation; }
#map.mode-pick { cursor: crosshair; }
#map.mode-edit { cursor: cell; }
.map-empty {
  position: absolute; inset: 0;
  display: grid; place-content: center; text-align: center;
  color: var(--muted); padding: 1rem;
}
.map-empty p { margin: .2rem 0; }

.map-fields polygon { fill: var(--field); fill-opacity: .14; stroke: none; }
.map-fields polygon.new { fill-opacity: .45; }
.map-links line { stroke: var(--link); stroke-linecap: round; }
.map-links line.future { stroke: var(--muted); stroke-dasharray: 4 6; opacity: .55; }
.map-links line.current { stroke: var(--link-current); }
.map-portals circle { fill: var(--portal); stroke: var(--portal-stroke); }
.map-portals circle.origin { fill: var(--link-current); }
.map-portals circle.dest { fill: var(--link); }
.map-portals text, .map-numbers text {
  fill: var(--text);
  stroke: var(--map-bg); paint-order: stroke;
  font-weight: 700; font-family: system-ui, sans-serif;
}
.map-numbers text { fill: var(--link); }
.map-numbers text.current { fill: var(--link-current); }

.hint { color: var(--muted); font-size: .88rem; margin: .5rem 0; }

.player { display: grid; gap: .5rem; margin-top: .6rem; grid-template-columns: auto 1fr; align-items: center; }
.step-range input { width: 100%; accent-color: var(--accent); min-height: 2.75rem; }
.step-text { grid-column: 1 / -1; margin: 0; font-weight: 600; min-height: 1.5em; }

.tabs { display: flex; gap: .25rem; border-bottom: 1px solid var(--border); margin-bottom: 1rem; }
.tabs [role="tab"] {
  min-height: 2.75rem; padding: .5rem 1rem;
  border: 0; border-bottom: 3px solid transparent;
  background: none; color: var(--muted);
  font: inherit; font-weight: 600; cursor: pointer;
}
.tabs [role="tab"][aria-selected="true"] { color: var(--text); border-bottom-color: var(--accent); }

.dropzone {
  display: grid; gap: .2rem; place-items: center; text-align: center;
  padding: 1.4rem 1rem;
  border: 2px dashed var(--border);
  border-radius: var(--radius);
  background: var(--surface-2);
  cursor: pointer;
}
.dropzone:hover, .dropzone.drag { border-color: var(--accent); }
.dropzone:has(input:focus-visible) { outline: 3px solid var(--focus); outline-offset: 2px; }
.dropzone-title { font-weight: 700; color: var(--accent); }
.dropzone-sub { font-size: .88rem; color: var(--muted); }

.fieldset { border: 1px solid var(--border); border-radius: var(--radius); margin: 1rem 0; padding: .6rem .9rem .3rem; }
.fieldset legend { font-weight: 600; padding: 0 .3rem; }
.color-list { display: flex; flex-wrap: wrap; gap: .2rem 1rem; }
.color-item { display: inline-flex; align-items: center; gap: .45rem; min-height: 2.75rem; cursor: pointer; }
.swatch { width: 1.1rem; height: 1.1rem; border-radius: 50%; border: 2px solid var(--text); flex: none; }
.chip { display: inline-flex; align-items: center; gap: .3rem; }
.chip .btn { min-height: 2.75rem; padding: .2rem .6rem; }

.details { margin: .75rem 0; }
.details summary { cursor: pointer; font-weight: 600; min-height: 2.75rem; display: flex; align-items: center; gap: .5rem; list-style: none; }
.details summary::-webkit-details-marker { display: none; }
.details summary::before { content: "▸"; color: var(--accent); transition: transform .15s; }
.details[open] > summary::before { transform: rotate(90deg); }
.grid-2 { display: grid; gap: .75rem; grid-template-columns: repeat(auto-fit, minmax(11rem, 1fr)); margin: .75rem 0; }
.full { grid-column: 1 / -1; }
.field { display: grid; gap: .3rem; font-weight: 600; font-size: .92rem; }
.field output { font-weight: 400; color: var(--muted); }

input[type="text"], input[type="url"], input[type="number"], select, textarea {
  width: 100%;
  min-height: 2.75rem;
  padding: .5rem .65rem;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--bg);
  color: var(--text);
  font: inherit; font-weight: 400;
}
textarea { font-family: ui-monospace, monospace; font-size: .88rem; resize: vertical; }
input[type="range"] { width: 100%; accent-color: var(--accent); }
input[aria-invalid="true"] { border-color: var(--danger); }

.table-wrap { overflow: auto; max-height: 22rem; border: 1px solid var(--border); border-radius: 8px; }
.table { width: 100%; border-collapse: collapse; font-size: .92rem; }
.table th, .table td { padding: .35rem .6rem; text-align: left; border-bottom: 1px solid var(--border); vertical-align: middle; }
.table thead th { position: sticky; top: 0; background: var(--surface-2); z-index: 1; }
.table td input { min-height: 2.5rem; padding: .3rem .5rem; }
.table .num-col { text-align: right; font-variant-numeric: tabular-nums; }
.table .pos { color: var(--muted); font-variant-numeric: tabular-nums; white-space: nowrap; font-size: .85rem; }
.table .btn { min-height: 2.5rem; padding: .2rem .6rem; font-size: .85rem; }
.empty-row td { color: var(--muted); text-align: center; padding: 1rem; }

.stats { display: grid; grid-template-columns: repeat(auto-fit, minmax(7.5rem, 1fr)); gap: .6rem; margin: 0; }
.stats div { background: var(--surface-2); border-radius: 8px; padding: .55rem .7rem; }
.stats dt { color: var(--muted); font-size: .8rem; }
.stats dd { margin: 0; font-size: 1.3rem; font-weight: 700; font-variant-numeric: tabular-nums; }

.warnings, .errors { padding-left: 1.2rem; margin: .8rem 0 0; }
.warnings li { color: var(--warn); }
.errors li { color: var(--danger); }
.warnings:empty, .errors:empty { display: none; }

.route { padding-left: 1.4rem; margin: 0; display: grid; gap: .5rem; }
.route > li::marker { font-weight: 700; color: var(--accent); }
.route .visit { font-weight: 700; }
.route .leg { color: var(--muted); font-weight: 400; font-size: .88rem; }
.route ul { list-style: none; padding: 0; margin: .25rem 0 0; display: grid; gap: .15rem; }
.route .link-btn {
  display: flex; gap: .5rem; width: 100%;
  min-height: 2.25rem; padding: .25rem .5rem;
  border: 1px solid transparent; border-radius: 6px;
  background: none; color: var(--text); font: inherit; text-align: left; cursor: pointer;
}
.route .link-btn:hover { border-color: var(--border); }
.route .link-btn[aria-current="step"] { border-color: var(--link-current); background: var(--surface-2); }
.route .step-no { color: var(--muted); font-variant-numeric: tabular-nums; min-width: 2.5ch; text-align: right; }
.route .gain { margin-left: auto; color: var(--accent); white-space: nowrap; }

progress { width: 100%; margin-top: .8rem; accent-color: var(--accent); height: .75rem; }

.toast {
  position: fixed; left: 50%; bottom: 1.25rem;
  transform: translateX(-50%);
  max-width: min(36rem, calc(100vw - 2rem));
  padding: .7rem 1.1rem;
  background: var(--text); color: var(--bg);
  border-radius: var(--radius);
  font-weight: 600;
  box-shadow: 0 4px 16px rgb(0 0 0 / .35);
  opacity: 0; pointer-events: none;
  transition: opacity .2s;
}
.toast.show { opacity: 1; }

.update-banner {
  display: flex; flex-wrap: wrap; align-items: center; justify-content: space-between; gap: .5rem 1rem;
  margin: 1rem clamp(1rem, 3vw, 2rem) 0;
  padding: .75rem 1rem;
  border: 2px solid var(--accent);
  border-radius: var(--radius);
  background: var(--surface);
}
.update-banner p { margin: 0; flex: 1 1 20rem; }
.update-banner .row { margin: 0; }

.app-footer { padding: 1rem clamp(1rem, 3vw, 2rem) 2rem; color: var(--muted); font-size: .85rem; }

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

@media (forced-colors: active) {
  .map-links line, .map-portals circle { forced-color-adjust: none; }
  .num { border: 1px solid CanvasText; }
}

@media print {
  .app-header button, .map-toolbar, .player .btn-group, .step-range, .panel > section:not(#result), .row, .skip-link, .toast, .hint { display: none !important; }
  body { background: #fff; color: #000; }
  .layout { display: block; }
  .map-pane, .card { border: 0; box-shadow: none; }
  .map-scroll { max-height: none; overflow: visible; }
  .table-wrap { max-height: none; overflow: visible; }
}
