:root {
  --bg: #ffffff; --fg: #111827; --muted: #4b5563; --border: #d1d5db; --surface: #ffffff; --surface-2: #f3f4f6;
  --accent: #0f766e; --accent-fg: #ffffff; --road: #b45309; --ferry: #475569; --walk: #6d28d9; --overlay: #3f8f6a;
  --focus: #2563eb; --shadow: 0 2px 12px rgba(0,0,0,.18);
  --sat: env(safe-area-inset-top, 0px); --sab: env(safe-area-inset-bottom, 0px); --sal: env(safe-area-inset-left, 0px); --sar: env(safe-area-inset-right, 0px);
  --radius: 14px; --tap: 44px;
  color-scheme: light;
}
html.dark {
  --bg: #0f172a; --fg: #f1f5f9; --muted: #cbd5e1; --border: #334155; --surface: #1e293b; --surface-2: #273449;
  --accent: #2dd4bf; --accent-fg: #042f2e; --road: #fbbf24; --ferry: #94a3b8; --walk: #c4b5fd; --overlay: #6ee7b7; --focus: #93c5fd;
  color-scheme: dark;
}
* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { margin: 0; height: 100%; overflow: hidden; }
body { font: 16px/1.4 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif; color: var(--fg); background: var(--bg); -webkit-text-size-adjust: 100%; -webkit-tap-highlight-color: transparent; overscroll-behavior: none; }
#map { position: fixed; inset: 0; background: #e8ecef; }
html.dark #map { background: #0f172a; }
body.no-basemap #map::after { content: "Background map unavailable"; position: absolute; left: 50%; top: 50%; transform: translate(-50%, -50%); color: var(--muted); font-size: 14px; pointer-events: none; }
h2 { font-size: 20px; margin: 0 0 8px; } h3 { font-size: 15px; margin: 16px 0 6px; }
p { margin: 0 0 10px; }
a { color: var(--accent); text-underline-offset: 2px; }
html.dark a { color: #5eead4; }
.muted { color: var(--muted); }
.hint { color: var(--muted); font-size: 14px; }
hr { border: 0; border-top: 1px solid var(--border); margin: 16px 0; }
.visually-hidden { position: absolute !important; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }
.skip-link { position: absolute; left: 8px; top: -60px; z-index: 50; background: var(--surface); color: var(--fg); padding: 8px 12px; border-radius: 8px; }
.skip-link:focus { top: calc(var(--sat) + 8px); }
:focus-visible { outline: 3px solid var(--focus); outline-offset: 2px; }
button { font: inherit; color: inherit; }

/* Top bar: search + toggles (<= ~110px of a phone screen) */
.topbar { position: fixed; top: 0; left: 0; right: 0; padding: calc(var(--sat) + 8px) calc(var(--sar) + 8px) 0 calc(var(--sal) + 8px); z-index: 10; pointer-events: none; }
.topbar > * { pointer-events: auto; }
.search { position: relative; display: flex; background: var(--surface); border-radius: var(--radius); box-shadow: var(--shadow); align-items: center; }
.search input { flex: 1; min-width: 0; height: var(--tap); border: 0; background: transparent; padding: 0 14px; font-size: 16px; color: var(--fg); border-radius: var(--radius); }
.search input::placeholder { color: var(--muted); }
.search .btn-icon { width: var(--tap); height: var(--tap); }
.search-results { position: absolute; top: calc(100% + 4px); left: 0; right: 0; margin: 0; padding: 4px 0; list-style: none; background: var(--surface); border-radius: 12px; box-shadow: var(--shadow); max-height: 50vh; overflow: auto; }
.search-item { display: flex; flex-direction: column; width: 100%; text-align: left; border: 0; background: none; padding: 8px 14px; min-height: var(--tap); cursor: pointer; }
.search-item:hover, .search-item:focus { background: var(--surface-2); }
.search-label { font-weight: 600; } .search-sub { font-size: 13px; color: var(--muted); }
.kind-route .search-label::before { content: "↗ "; color: var(--accent); }
.search-note { padding: 10px 14px; color: var(--muted); font-size: 14px; }
.toggles { display: flex; gap: 8px; margin-top: 8px; }
.toggle-wrap { flex: 1; display: flex; background: var(--surface); border-radius: var(--radius); box-shadow: var(--shadow); overflow: hidden; }
.toggle { flex: 1; min-height: var(--tap); border: 0; background: transparent; display: flex; align-items: center; gap: 8px; padding: 0 10px; font-weight: 600; font-size: 14px; text-align: left; cursor: pointer; color: var(--muted); }
.toggle[aria-pressed="true"] { color: var(--fg); }
.toggle[aria-pressed="true"].toggle-offroad { box-shadow: inset 0 -3px 0 var(--accent); }
.toggle[aria-pressed="true"].toggle-mixed { box-shadow: inset 0 -3px 0 var(--road); }
.toggle[aria-pressed="false"] { text-decoration: line-through; text-decoration-color: var(--muted); text-decoration-thickness: 2px; }
.toggle[aria-pressed="false"] .swatch { opacity: .3; }
.swatch { width: 26px; height: 6px; border-radius: 3px; flex: none; }
.swatch-offroad { background: var(--accent); }
.swatch-mixed { background: repeating-linear-gradient(90deg, var(--road) 0 7px, transparent 7px 11px); }
.btn-help { width: 44px; min-height: var(--tap); border: 0; border-left: 1px solid var(--border); background: transparent; font-weight: 700; color: var(--muted); cursor: pointer; }

.data-pending { top: auto; bottom: calc(var(--sab) + 40px); transform: translateX(-50%); border-left: 4px solid var(--road); }
.empty-state { position: fixed; left: 50%; top: 45%; transform: translate(-50%, -50%); z-index: 9; background: var(--surface); padding: 16px; border-radius: var(--radius); box-shadow: var(--shadow); text-align: center; max-width: 320px; }

/* Floating buttons */
.fabs { position: fixed; right: calc(var(--sar) + 12px); bottom: calc(var(--sab) + 96px); display: flex; flex-direction: column; gap: 10px; z-index: 10; }
.fab { width: 48px; height: 48px; border-radius: 50%; border: 0; background: var(--surface); color: var(--fg); box-shadow: var(--shadow); display: grid; place-items: center; position: relative; cursor: pointer; }
.fab-small { width: 40px; height: 40px; font-weight: 700; font-style: italic; font-family: Georgia, serif; }
.fab.busy { animation: pulse 1s infinite; }
@keyframes pulse { 50% { opacity: .5; } }
.badge { position: absolute; top: -4px; right: -4px; min-width: 20px; height: 20px; border-radius: 10px; background: var(--road); color: #fff; font-size: 12px; font-weight: 700; display: grid; place-items: center; padding: 0 5px; }
html.dark .badge { color: #111; }
.maplibregl-ctrl-bottom-right { bottom: calc(var(--sab) + 4px); right: var(--sar); }
.maplibregl-ctrl-group { display: none; }
@media (min-width: 700px) { .maplibregl-ctrl-group { display: block; } .maplibregl-ctrl-bottom-right { bottom: calc(var(--sab) + 8px); } .fabs { bottom: calc(var(--sab) + 130px); } }
.maplibregl-ctrl-attrib, .maplibregl-ctrl-attrib-inner { font-size: 11px; background: var(--surface) !important; color: var(--fg); }
.maplibregl-ctrl-attrib a { color: var(--fg); }
.maplibregl-ctrl-attrib-button { filter: none; }
html.dark .maplibregl-ctrl-attrib-button { filter: invert(1); }

/* Buttons */
.btn { min-height: var(--tap); padding: 0 18px; border-radius: 12px; border: 0; background: var(--accent); color: var(--accent-fg); font-weight: 600; cursor: pointer; }
.btn-secondary { background: var(--surface-2); color: var(--fg); border: 1px solid var(--border); }
.btn-small { min-height: 36px; padding: 0 12px; border-radius: 10px; border: 1px solid var(--border); background: var(--surface-2); color: var(--fg); cursor: pointer; }
.btn-icon { width: var(--tap); height: var(--tap); border: 0; background: transparent; font-size: 26px; line-height: 1; cursor: pointer; border-radius: 50%; display: grid; place-items: center; color: var(--fg); }
.btn-link { background: none; border: 0; color: var(--accent); font-weight: 600; text-decoration: underline; cursor: pointer; min-height: 36px; }
.actions { display: flex; flex-wrap: wrap; gap: 8px; margin: 14px 0 8px; }
.list-btn { display: block; width: 100%; text-align: left; background: none; border: 0; padding: 10px 0; min-height: var(--tap); cursor: pointer; border-top: 1px solid var(--border); }
.plain-list { list-style: none; margin: 0; padding: 0; }
.saved-item { display: flex; align-items: center; gap: 8px; }
.saved-item .list-btn { flex: 1; }

/* Sheets */
.sheet { position: fixed; inset: 0; z-index: 20; background: rgba(0,0,0,.35); display: none; }
.sheet.open { display: block; }
/* Non-modal (route detail): no scrim, the map behind stays pannable while reading */
.sheet.nonmodal { background: transparent; pointer-events: none; }
.sheet.nonmodal .sheet-body { pointer-events: auto; }
.sheet-body { position: absolute; left: 0; right: 0; bottom: 0; max-height: calc(100% - var(--sat) - 40px); overflow: auto; background: var(--surface); border-radius: 18px 18px 0 0; padding: 14px 16px calc(var(--sab) + 16px); box-shadow: 0 -4px 24px rgba(0,0,0,.25); animation: up .18s ease-out; -webkit-overflow-scrolling: touch; }
@media (prefers-reduced-motion: reduce) { .sheet-body { animation: none; } }
@keyframes up { from { transform: translateY(40px); opacity: .6; } }
@media (min-width: 700px) { .sheet-body { left: 50%; transform: translateX(-50%); width: 520px; bottom: 24px; border-radius: 18px; } @keyframes up { from { transform: translate(-50%, 40px); } to { transform: translateX(-50%); } } }
.sheet-detail .sheet-body { max-height: 62%; }
.sheet-head { display: flex; justify-content: space-between; align-items: flex-start; gap: 8px; }
.sheet-head h2 { margin-bottom: 2px; }
fieldset { border: 0; padding: 0; margin: 0 0 12px; }
legend { font-weight: 700; padding: 0; margin-bottom: 6px; }
.row { display: flex; gap: 10px; align-items: center; flex-wrap: wrap; }
.row label { display: flex; align-items: center; gap: 6px; }
input[type=number] { width: 84px; min-height: 40px; border: 1px solid var(--border); border-radius: 8px; padding: 0 8px; font-size: 16px; background: var(--surface); color: var(--fg); }
.check { display: inline-flex; align-items: center; gap: 6px; min-height: 40px; padding: 0 12px; border: 1px solid var(--border); border-radius: 20px; cursor: pointer; background: var(--surface); }
.check:has(input:checked) { background: var(--accent); color: var(--accent-fg); border-color: var(--accent); }
.check input { position: absolute; opacity: 0; width: 1px; height: 1px; }
.check:has(input:focus-visible) { outline: 3px solid var(--focus); outline-offset: 2px; }

/* Detail */
.stats { display: grid; grid-template-columns: repeat(4, 1fr); gap: 6px; margin: 8px 0; }
.stats div { background: var(--surface-2); border-radius: 10px; padding: 8px 6px; text-align: center; }
.stats dt { font-size: 12px; color: var(--muted); } .stats dd { margin: 0; font-weight: 700; font-size: 16px; }
.badge-walk { background: var(--surface-2); border: 1px dashed var(--walk); border-radius: 10px; padding: 6px 10px; font-size: 14px; }
.badge-ferry { background: var(--surface-2); border: 1px dotted var(--ferry); border-radius: 10px; padding: 6px 10px; font-size: 14px; }
.terrain-bar { display: flex; height: 16px; border-radius: 8px; overflow: hidden; background: var(--surface-2); margin-top: 6px; }
.tb { display: block; height: 100%; }
.tb-offroad { background: var(--accent); }
.tb-walk { background: repeating-linear-gradient(45deg, var(--walk) 0 3px, transparent 3px 6px), repeating-linear-gradient(135deg, var(--walk) 0 3px, transparent 3px 6px); min-width: 4px; }
.tb-road { background: repeating-linear-gradient(135deg, var(--road) 0 4px, transparent 4px 8px); border-top: 2px solid var(--road); border-bottom: 2px solid var(--road); }
.legend-row { display: flex; gap: 14px; font-size: 13px; margin: 6px 0 0; }
.legend { padding-left: 34px; position: relative; white-space: nowrap; }
.legend::before { content: ""; position: absolute; left: 0; top: 50%; width: 28px; height: 5px; margin-top: -2px; border-radius: 2px; }
.legend-offroad::before { background: var(--accent); }
.legend-road::before { background: repeating-linear-gradient(90deg, var(--road) 0 7px, transparent 7px 11px); }
.legend-walk::before { background: repeating-linear-gradient(90deg, var(--walk) 0 4px, transparent 4px 8px); }
.legend-ferry::before { background: repeating-linear-gradient(90deg, var(--ferry) 0 3px, transparent 3px 7px); }
.legend-overlay::before { background: repeating-linear-gradient(90deg, var(--overlay) 0 3px, transparent 3px 7px); height: 3px; }
.legend-list li { margin: 4px 0; }
.profile { position: relative; }
.profile-svg { width: 100%; height: auto; display: block; touch-action: none; }
.profile-area { fill: var(--accent); opacity: .18; } .profile-line { fill: none; stroke: var(--accent); stroke-width: 2; }
.profile-axis { stroke: var(--border); } .profile-tick { font-size: 11px; fill: var(--muted); }
.profile-cursor { stroke: var(--road); stroke-width: 2; }
.profile-readout { font-size: 13px; color: var(--muted); min-height: 18px; }
.surfaces { list-style: none; padding: 0; margin: 0; }
.dot { display: inline-block; width: 12px; height: 12px; border-radius: 50%; margin-right: 6px; vertical-align: -1px; border: 1px solid var(--border); }
.dot-paved { background: #64748b; } .dot-gravel { background: #a16207; } .dot-unpaved { background: #65a30d; } .dot-unknown { background: transparent; }
.speed-row { margin-top: 12px; }

/* Toast */
.toast { position: fixed; left: 50%; bottom: calc(var(--sab) + 20px); transform: translateX(-50%); z-index: 30; background: #111827; color: #fff; padding: 10px 8px 10px 16px; border-radius: 12px; box-shadow: var(--shadow); display: flex; align-items: center; gap: 8px; width: min(92vw, 520px); font-size: 14px; }
.toast > span { flex: 1 1 auto; min-width: 0; }
.toast .btn-link { white-space: nowrap; flex: none; }
html.dark .toast { background: #f1f5f9; color: #111827; }
.toast .btn-link { color: #5eead4; } html.dark .toast .btn-link { color: #0f766e; }
.toast-close { width: 36px; height: 36px; font-size: 22px; color: inherit; }
