:root {
  --green: #1e7d45;
  --green-dark: #155f34;
  --green-text: #188038;
  --blue: #1a73e8;
  --orange: #d9600b;
  --red: #c5221f;
  --text: #202124;
  --muted: #5f6368;
  --line: #e3e5e8;
  --surface: #ffffff;
  --page: #f4f5f2;
  --chip: #eef1ec;
  --shadow: 0 2px 10px rgba(0, 0, 0, 0.18);
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --text: #e8eaed;
    --muted: #a8adb3;
    --line: #3a3d41;
    --surface: #202124;
    --page: #17181a;
    --chip: #2b2f2c;
    --green-text: #5bc27e;
    color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --text: #e8eaed;
  --muted: #a8adb3;
  --line: #3a3d41;
  --surface: #202124;
  --page: #17181a;
  --chip: #2b2f2c;
  --green-text: #5bc27e;
  color-scheme: dark;
}

* { box-sizing: border-box; }
html, body {
  margin: 0;
  height: 100%;
  overflow: hidden;
  background: var(--page);
  color: var(--text);
  font: 16px/1.4 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  -webkit-tap-highlight-color: transparent;
  -webkit-text-size-adjust: 100%;
}
button { font: inherit; color: inherit; }
.hidden { display: none !important; }

#map { position: fixed; inset: 0; }

/* Screens: only the current one shows. */
.screen, .screen-overlay { display: none; }
body[data-screen="home"] #home,
body[data-screen="overview"] #overview,
body[data-screen="nav"] #nav,
body[data-screen="explore"] #explore { display: block; }
.screen { position: fixed; inset: 0; background: var(--page); overflow-y: auto; -webkit-overflow-scrolling: touch; z-index: 5; }
.screen-overlay { position: fixed; inset: 0; pointer-events: none; z-index: 5; }
.screen-overlay > * { pointer-events: auto; }

/* Icons */
.i { display: inline-flex; width: 22px; height: 22px; }
.i svg, .icon-btn svg, .fab svg { width: 100%; height: 100%; }

/* Buttons */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  min-height: 46px; padding: 10px 18px; border-radius: 999px;
  border: 1px solid var(--line); background: var(--surface); color: var(--text);
  font-weight: 600; cursor: pointer; user-select: none; text-decoration: none;
}
.btn.primary { background: var(--green); border-color: var(--green); color: #fff; }
.btn.primary:active { background: var(--green-dark); }
.btn.big { min-height: 54px; font-size: 18px; flex: 1; }
.btn.small { min-height: 38px; padding: 6px 14px; font-size: 15px; }
.btn:disabled { opacity: 0.55; }
.link { background: none; border: 0; color: var(--blue); padding: 8px 0; cursor: pointer; font-weight: 600; }
.link.danger { color: var(--red); }
.icon-btn { width: 44px; height: 44px; padding: 10px; border: 0; background: none; color: var(--text); border-radius: 50%; cursor: pointer; }
.fab {
  width: 50px; height: 50px; padding: 13px; border-radius: 50%; border: 0;
  background: #fff; color: #202124; box-shadow: var(--shadow); cursor: pointer;
}

/* Home */
.home-head {
  position: sticky; top: 0; z-index: 2; display: flex; align-items: center; justify-content: space-between;
  padding: calc(var(--safe-top) + 10px) 16px 10px; background: var(--page);
}
.brand { display: flex; align-items: center; gap: 10px; }
.brand h1 { font-size: 24px; margin: 0; letter-spacing: -0.3px; }
.home-body { padding: 4px 16px calc(var(--safe-bottom) + 32px); max-width: 640px; margin: 0 auto; }
.home-actions { display: flex; gap: 10px; flex-wrap: wrap; margin: 6px 0 18px; }
.home-actions .btn { flex: 1 1 180px; }
.section-title { font-size: 15px; text-transform: uppercase; letter-spacing: 0.6px; color: var(--muted); margin: 18px 0 8px; }
.note { background: #fff4e0; color: #6b4300; border-radius: 12px; padding: 10px 14px; margin-bottom: 10px; font-size: 15px; }
.note.info { background: #e8f0fe; color: #174ea6; }
.note .progress { margin-top: 8px; }
.library-card { background: var(--surface); border: 1px solid var(--line); border-radius: 16px; padding: 14px 16px; margin-bottom: 14px; }
.library-card p { margin: 0 0 10px; }
.library-form { display: flex; gap: 8px; }
.library-form input { flex: 1; min-width: 0; font: inherit; padding: 9px 12px; border-radius: 10px; border: 1px solid var(--line); background: var(--page); color: var(--text); }
.field { display: grid; gap: 4px; margin: 6px 0 4px; font-weight: 500; }
.field select { font: inherit; padding: 8px 10px; border-radius: 10px; border: 1px solid var(--line); background: var(--page); color: var(--text); }
.voice-row { display: flex; align-items: center; gap: 10px; margin: 6px 0 10px; }
.group-head { display: flex; align-items: baseline; justify-content: space-between; gap: 8px; }
.group-head .fine { margin: 18px 0 8px; }
.saved-data { margin-top: 10px; font-size: 14.5px; }
.saved-data h3 { font-size: 14px; margin: 10px 0 4px; color: var(--muted); text-transform: uppercase; letter-spacing: 0.5px; }
.saved-data .row { display: flex; align-items: center; justify-content: space-between; gap: 8px; min-height: 36px; }
.saved-data .row .link { padding: 4px 0; }
.route-list { list-style: none; margin: 0; padding: 0; display: grid; gap: 10px; }
.route-card {
  display: flex; align-items: center; gap: 12px; padding: 14px; border-radius: 16px;
  background: var(--surface); border: 1px solid var(--line); cursor: pointer;
}
.route-card .rc-main { flex: 1; min-width: 0; }
.route-card .rc-name { font-weight: 700; font-size: 17px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.route-card .rc-meta { color: var(--muted); font-size: 14px; margin-top: 2px; }
.route-card .rc-thumb { width: 54px; height: 54px; flex: none; border-radius: 12px; background: var(--chip); color: var(--green-text); }
.pill { display: inline-flex; align-items: center; gap: 4px; font-size: 12.5px; font-weight: 700; padding: 2px 9px; border-radius: 999px; background: var(--chip); color: var(--muted); margin-top: 6px; }
.pill.ok { background: #e3f3e8; color: #17703a; }
.pill.warn { background: #fff1db; color: #8a5300; }
.pill .i { width: 14px; height: 14px; }
.empty { background: var(--surface); border: 1px dashed var(--line); border-radius: 16px; padding: 16px; }
.empty p { margin: 0 0 12px; }
.help { background: var(--surface); border: 1px solid var(--line); border-radius: 14px; padding: 12px 14px; margin-top: 10px; }
.help summary { font-weight: 600; cursor: pointer; }
.help ol, .help ul { padding-left: 20px; margin: 10px 0 2px; }
.help li { margin-bottom: 6px; }
.fine { color: var(--muted); font-size: 13.5px; }

/* Overview sheet */
.back-fab { position: absolute; top: calc(var(--safe-top) + 12px); left: 12px; }
.sheet {
  position: absolute; left: 0; right: 0; bottom: 0; max-height: 58%; overflow-y: auto;
  background: var(--surface); color: var(--text); border-radius: 20px 20px 0 0; box-shadow: 0 -2px 14px rgba(0,0,0,0.2);
  padding: 8px 16px calc(var(--safe-bottom) + 20px); -webkit-overflow-scrolling: touch;
}
.sheet.tall { max-height: 88%; }
.grabber { width: 40px; height: 5px; border-radius: 3px; background: var(--line); margin: 2px auto 10px; cursor: pointer; }
.ov-name { margin: 0 0 4px; font-size: 22px; line-height: 1.2; }
.stats { display: flex; flex-wrap: wrap; gap: 4px 16px; color: var(--muted); font-size: 15px; }
.stats b { color: var(--text); font-size: 18px; }
.profile { width: 100%; height: 72px; margin: 10px 0 4px; display: block; }
.profile .area { fill: rgba(30, 125, 69, 0.18); }
.profile .edge { fill: none; stroke: var(--green); stroke-width: 1.5; vector-effect: non-scaling-stroke; }
.offline-box { display: flex; align-items: center; gap: 10px; margin: 10px 0 14px; padding: 10px 12px; border-radius: 12px; background: var(--chip); font-size: 14.5px; }
.offline-box .grow { flex: 1; }
.offline-box.ok { background: #e3f3e8; color: #17532e; }
.offline-box.busy { background: #e8f0fe; color: #174ea6; }
.offline-box.warn { background: #fff1db; color: #6b4300; }
.progress { height: 4px; border-radius: 2px; background: rgba(23, 78, 166, 0.2); overflow: hidden; margin-top: 6px; }
.progress div { height: 100%; background: #1a73e8; width: 0; transition: width 0.3s; }
.ov-buttons { display: flex; gap: 10px; }
.cue-list { list-style: none; margin: 0; padding: 0; }
.cue-list li { display: flex; align-items: center; gap: 12px; padding: 9px 0; border-bottom: 1px solid var(--line); }
.cue-list .ci { width: 34px; height: 34px; flex: none; color: var(--green-text); }
.cue-list .ci svg { width: 100%; height: 100%; }
.cue-list .ct { flex: 1; font-size: 15.5px; }
.cue-list .cd { color: var(--muted); font-size: 13.5px; white-space: nowrap; }
.diff-dot { display: inline-block; width: 10px; height: 10px; border-radius: 50%; margin-right: 5px; vertical-align: 0; border: 1.5px solid #fff; box-shadow: 0 0 0 1px rgba(0,0,0,0.25); }
.diff-dot.dblack { box-shadow: 0 0 0 1px #000, 3px 0 0 -1px #000; }
.ov-footer { display: flex; justify-content: space-between; margin-top: 14px; }

/* Navigation */
.banner {
  position: absolute; top: calc(var(--safe-top) + 8px); left: 8px; right: 8px;
  display: flex; align-items: center; gap: 12px; padding: 12px 16px 12px 12px;
  background: var(--green); color: #fff; border-radius: 16px; box-shadow: var(--shadow);
  --arrow-bg: var(--green);
}
.banner.off { background: var(--orange); --arrow-bg: var(--orange); }
.banner.done { background: #1a73e8; --arrow-bg: #1a73e8; }
.b-icon { width: 60px; height: 60px; flex: none; }
.b-icon svg { width: 100%; height: 100%; }
.b-main { min-width: 0; flex: 1; }
.b-dist { font-size: 30px; font-weight: 800; line-height: 1.05; letter-spacing: -0.5px; }
.b-text { font-size: 19px; font-weight: 600; line-height: 1.2; margin-top: 2px; overflow-wrap: anywhere; }
.b-text .sub { display: block; font-size: 15px; font-weight: 500; opacity: 0.9; }
.then {
  position: absolute; top: calc(var(--safe-top) + 8px + var(--banner-h, 86px) + 4px); left: 8px;
  display: flex; align-items: center; gap: 6px; padding: 5px 12px 5px 14px; border-radius: 12px;
  background: var(--green-dark); color: #fff; font-weight: 600; font-size: 15px; box-shadow: var(--shadow);
}
.then svg { width: 26px; height: 26px; }
.nav-tools { position: absolute; right: 12px; top: calc(var(--safe-top) + var(--banner-h, 86px) + 24px); display: grid; gap: 10px; }
.recenter {
  position: absolute; left: 12px; bottom: calc(var(--safe-bottom) + 96px);
  display: inline-flex; align-items: center; gap: 6px; padding: 10px 16px; border-radius: 999px; border: 0;
  background: #fff; color: var(--blue); font-weight: 700; box-shadow: var(--shadow); cursor: pointer;
}
.recenter .i { width: 18px; height: 18px; }
.trail-chip {
  position: absolute; left: 50%; transform: translateX(-50%); bottom: calc(var(--safe-bottom) + 100px);
  max-width: calc(100% - 140px); white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
  padding: 6px 14px; border-radius: 999px; background: #fff; color: #202124; font-weight: 700; font-size: 15px; box-shadow: var(--shadow);
}
.nav-bottom {
  position: absolute; left: 0; right: 0; bottom: 0; display: flex; align-items: center; gap: 12px;
  padding: 12px 16px calc(var(--safe-bottom) + 12px); background: var(--surface); color: var(--text);
  border-radius: 20px 20px 0 0; box-shadow: 0 -2px 12px rgba(0,0,0,0.18);
}
.fab.exit { width: 46px; height: 46px; padding: 12px; box-shadow: none; background: var(--chip); color: var(--text); }
.nb-main { flex: 1; text-align: center; min-width: 0; }
.nb-time { font-size: 24px; font-weight: 800; color: var(--green-text); line-height: 1.1; }
.nb-sub { color: var(--muted); font-size: 15px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.nb-speed { width: 46px; text-align: center; font-size: 13px; color: var(--muted); line-height: 1.1; }
.nb-speed b { display: block; font-size: 19px; color: var(--text); }
.sim-bar {
  position: absolute; left: 50%; transform: translateX(-50%); bottom: calc(var(--safe-bottom) + 148px);
  display: flex; align-items: center; gap: 4px; padding: 5px; border-radius: 999px; background: rgba(32,33,36,0.88); color: #fff; box-shadow: var(--shadow);
  font-size: 14px;
}
.sim-bar .sim-label { padding: 0 8px; font-weight: 700; opacity: 0.8; }
.sim-bar button { border: 0; background: none; color: #fff; padding: 6px 10px; border-radius: 999px; cursor: pointer; font-weight: 600; }
.sim-bar button.on { background: rgba(255,255,255,0.22); }
.arrived { position: absolute; inset: 0; display: flex; align-items: flex-end; justify-content: center; background: rgba(0,0,0,0.25); padding: 16px 16px calc(var(--safe-bottom) + 16px); }
.arrived-card { width: 100%; max-width: 420px; background: var(--surface); color: var(--text); border-radius: 20px; padding: 22px; text-align: center; box-shadow: var(--shadow); }
.arrived-card h2 { margin: 6px 0; }
.arrived-icon { width: 56px; height: 56px; margin: 0 auto; color: var(--green); --arrow-bg: var(--surface); }

/* Position marker */
.puck { width: 64px; height: 64px; position: relative; pointer-events: none; }
.puck-dot { position: absolute; left: 50%; top: 50%; width: 22px; height: 22px; margin: -11px 0 0 -11px; border-radius: 50%; background: #1a73e8; border: 3.5px solid #fff; box-shadow: 0 1px 6px rgba(0,0,0,0.35); }
.puck-cone { position: absolute; left: 50%; top: 50%; width: 0; height: 0; margin-left: -16px; margin-top: -40px; border-left: 16px solid transparent; border-right: 16px solid transparent; border-bottom: 30px solid rgba(26,115,232,0.28); display: none; }
.puck.has-heading .puck-cone { display: block; }

/* Explore */
.ex-tools { position: absolute; right: 12px; top: calc(var(--safe-top) + 12px); display: grid; gap: 10px; }
.ex-bottom { position: absolute; left: 12px; right: 12px; bottom: calc(var(--safe-bottom) + 14px); text-align: center; }
.ex-bottom .btn { width: 100%; box-shadow: var(--shadow); }
.ex-bottom .fine { background: rgba(255,255,255,0.9); color: #3c4043; border-radius: 10px; padding: 4px 8px; display: inline-block; margin: 8px 0 0; }
.ex-bottom .fine:empty { display: none; }

/* Settings dialog */
.dialog { border: 0; border-radius: 20px; padding: 0; width: min(440px, calc(100% - 32px)); background: var(--surface); color: var(--text); box-shadow: 0 8px 40px rgba(0,0,0,0.35); }
.dialog::backdrop { background: rgba(0,0,0,0.4); }
.dialog form { padding: 20px; }
.dialog h2 { margin: 0 0 12px; }
.seg { border: 0; padding: 0; margin: 0 0 12px; display: grid; gap: 6px; }
.seg legend { font-weight: 700; margin-bottom: 6px; }
.seg label, .switch { display: flex; align-items: center; gap: 10px; min-height: 40px; }
.switch { font-weight: 500; }
.dialog input[type="checkbox"], .dialog input[type="radio"] { width: 22px; height: 22px; accent-color: var(--green); }
.credits { margin-top: 10px; font-size: 13.5px; color: var(--muted); }
.credits summary { cursor: pointer; font-weight: 600; color: var(--text); }
.dialog-buttons { display: flex; justify-content: flex-end; margin-top: 16px; }

/* Toast */
.toast {
  position: fixed; left: 50%; bottom: calc(var(--safe-bottom) + 24px); transform: translate(-50%, 20px);
  max-width: calc(100% - 32px); padding: 12px 18px; border-radius: 14px; background: #202124; color: #fff;
  font-size: 15px; box-shadow: var(--shadow); opacity: 0; pointer-events: none; transition: opacity 0.2s, transform 0.2s; z-index: 50;
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }
body[data-screen="nav"] .toast { bottom: calc(var(--safe-bottom) + 150px); }

/* MapLibre tweaks */
.maplibregl-ctrl-bottom-left { bottom: 0; }
body[data-screen="nav"] .maplibregl-ctrl-bottom-left,
body[data-screen="nav"] .maplibregl-ctrl-bottom-right { bottom: calc(var(--safe-bottom) + 80px); }
body[data-screen="overview"] .maplibregl-ctrl-bottom-left,
body[data-screen="overview"] .maplibregl-ctrl-bottom-right { bottom: var(--sheet-h, 0px); }
body[data-screen="explore"] .maplibregl-ctrl-bottom-left,
body[data-screen="explore"] .maplibregl-ctrl-bottom-right { bottom: calc(var(--safe-bottom) + 112px); }
.maplibregl-ctrl-attrib { font-size: 10px; }
