/* TrailMaps — warm paper, forest ink, terracotta accent. */

:root {
  --tm-paper: #f3efe4;
  --tm-surface: #fffdf8;
  --tm-glass: rgba(252, 250, 244, 0.86);
  --tm-ink: #1f2a24;
  --tm-ink-2: #3b4640;
  --tm-muted: #6b756f;
  --tm-faint: #9aa29d;
  --tm-line: rgba(31, 42, 36, 0.09);
  --tm-line-2: rgba(31, 42, 36, 0.14);
  --tm-forest: #2f5d46;
  --tm-forest-2: #3d7559;
  --tm-forest-soft: rgba(47, 93, 70, 0.1);
  --tm-accent: #d4683c;
  --tm-accent-soft: rgba(212, 104, 60, 0.12);
  --tm-danger: #b9442f;
  --tm-blue: #2f7fd8;
  --tm-radius: 18px;
  --tm-shadow: 0 1px 0 rgba(255, 255, 255, 0.75) inset, 0 12px 32px -10px rgba(31, 42, 36, 0.32), 0 2px 8px rgba(31, 42, 36, 0.08);
  --safe-t: env(safe-area-inset-top, 0px);
  --safe-b: env(safe-area-inset-bottom, 0px);
  --safe-l: env(safe-area-inset-left, 0px);
  --safe-r: env(safe-area-inset-right, 0px);
  --dock-h: 64px;
  --ease: cubic-bezier(0.2, 0.8, 0.2, 1);
  color-scheme: light;
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }

html, body {
  margin: 0; height: 100%; overflow: hidden;
  background: var(--tm-paper); color: var(--tm-ink);
  font: 15px/1.4 -apple-system, BlinkMacSystemFont, "SF Pro Text", "Segoe UI", Inter, Roboto, system-ui, sans-serif;
  -webkit-font-smoothing: antialiased;
  overscroll-behavior: none;
}

button { font: inherit; color: inherit; border: 0; background: none; cursor: pointer; padding: 0; }
button:disabled { opacity: 0.45; cursor: default; }
:focus-visible { outline: 2px solid var(--tm-accent); outline-offset: 2px; }
.ic { display: block; flex: none; }
[hidden] { display: none !important; }

#map { position: fixed; inset: 0; }

.glass {
  background: var(--tm-glass);
  -webkit-backdrop-filter: blur(18px) saturate(1.5);
  backdrop-filter: blur(18px) saturate(1.5);
  border: 1px solid rgba(255, 255, 255, 0.65);
  box-shadow: var(--tm-shadow);
}

/* ---- Top bar ---------------------------------------------------------------- */

.topbar {
  position: fixed; z-index: 5;
  top: calc(var(--safe-t) + 10px); left: calc(var(--safe-l) + 12px);
  pointer-events: none;
}
.brand {
  pointer-events: auto;
  display: flex; align-items: center; gap: 8px;
  height: 42px; padding: 0 8px 0 10px; border-radius: 14px;
}
.brand-mark { width: 26px; height: 26px; }
.brand-name { font-weight: 720; font-size: 16px; letter-spacing: -0.02em; }
.net {
  display: inline-flex; align-items: center; gap: 6px;
  height: 26px; padding: 0 10px 0 9px; border-radius: 99px;
  font-size: 12px; font-weight: 600; color: var(--tm-forest);
  background: var(--tm-forest-soft);
}
.net i { width: 7px; height: 7px; border-radius: 50%; background: currentColor; }
.net b { font-weight: 600; }
.net[data-state="offline"] { color: #8a5a12; background: rgba(217, 154, 43, 0.16); }

/* ---- Floating buttons --------------------------------------------------------- */

.fabs {
  position: fixed; z-index: 5;
  top: calc(var(--safe-t) + 10px); right: calc(var(--safe-r) + 12px);
  display: flex; flex-direction: column; gap: 10px;
}
.fab {
  width: 46px; height: 46px; border-radius: 14px;
  display: grid; place-items: center; color: var(--tm-ink-2);
  transition: transform 0.15s var(--ease), color 0.2s;
}
.fab:active { transform: scale(0.94); }
.fab.on { color: var(--tm-blue); }
.fab.follow { color: #fff; background: var(--tm-blue); border-color: transparent; }
.fab.waiting .ic { animation: pulse 1.4s ease-in-out infinite; }
#btn-north .ic { transition: transform 0.1s linear; color: var(--tm-accent); }

@keyframes pulse { 50% { opacity: 0.35; } }

/* Scale bar sits under the brand pill */
.maplibregl-ctrl-top-left { top: calc(var(--safe-t) + 58px); left: calc(var(--safe-l) + 4px); }
.maplibregl-ctrl-scale {
  background: rgba(252, 250, 244, 0.7); border-color: var(--tm-ink-2); color: var(--tm-ink-2);
  font-size: 10px; font-weight: 600; border-radius: 0 0 3px 3px;
}
.maplibregl-ctrl-bottom-right { bottom: calc(var(--safe-b) + var(--dock-h) + 18px); }
.maplibregl-ctrl-attrib.maplibregl-compact { background: rgba(252, 250, 244, 0.8); }

/* ---- Dock --------------------------------------------------------------------- */

.dock {
  position: fixed; z-index: 11; /* stays above an open sheet, like an iOS tab bar */
  left: 50%; transform: translateX(-50%);
  bottom: calc(var(--safe-b) + 10px);
  width: min(440px, calc(100% - 24px));
  height: var(--dock-h); border-radius: 22px; padding: 6px;
  display: grid; grid-template-columns: repeat(4, 1fr); gap: 4px;
}
.dock button {
  border-radius: 16px;
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 3px;
  font-size: 11px; font-weight: 600; color: var(--tm-muted); letter-spacing: 0.01em;
  transition: background 0.2s, color 0.2s;
}
.dock button.active { color: var(--tm-forest); background: var(--tm-forest-soft); }
.dock-ic .ic { width: 22px; height: 22px; }

/* ---- Sheet -------------------------------------------------------------------- */

.sheet {
  position: fixed; z-index: 10;
  left: 0; right: 0; bottom: 0;
  max-height: min(78dvh, 720px);
  display: flex; flex-direction: column;
  background: var(--tm-surface);
  border-radius: 24px 24px 0 0;
  box-shadow: 0 -12px 40px -12px rgba(31, 42, 36, 0.35);
  transform: translateY(105%); visibility: hidden;
  transition: transform 0.36s var(--ease), visibility 0s linear 0.36s;
  padding-bottom: calc(var(--safe-b) + var(--dock-h) + 14px);
}
.sheet.open { transform: none; visibility: visible; transition: transform 0.36s var(--ease); }
.sheet-grip { width: 38px; height: 5px; border-radius: 3px; background: var(--tm-line-2); margin: 8px auto 0; flex: none; }
.sheet-head {
  display: flex; align-items: center; gap: 8px;
  padding: 8px 14px 10px 20px; flex: none;
}
.sheet-head h2 { margin: 0; font-size: 22px; font-weight: 750; letter-spacing: -0.025em; flex: 1; }
.sheet-actions { display: flex; gap: 6px; }
.sheet-body { overflow-y: auto; -webkit-overflow-scrolling: touch; padding: 0 16px 20px; overscroll-behavior: contain; }

.icon-btn {
  width: 36px; height: 36px; border-radius: 50%;
  display: grid; place-items: center; color: var(--tm-muted);
  background: rgba(31, 42, 36, 0.06);
}
.icon-btn:active { background: rgba(31, 42, 36, 0.12); }
.icon-btn .ic { width: 18px; height: 18px; }

@media (min-width: 900px) {
  .sheet {
    left: calc(var(--safe-l) + 12px); right: auto; bottom: 12px; top: calc(var(--safe-t) + 64px);
    width: 400px; max-height: none; border-radius: 22px;
    transform: translateX(calc(-100% - 24px));
    box-shadow: var(--tm-shadow);
    padding-bottom: 0;
  }
  .sheet-grip { display: none; }
  .sheet-head { padding-top: 16px; }
}

/* ---- Buttons & fields ---------------------------------------------------------- */

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  height: 46px; padding: 0 18px; border-radius: 14px;
  font-size: 15px; font-weight: 650; letter-spacing: -0.005em;
  transition: transform 0.12s var(--ease), background 0.2s, box-shadow 0.2s;
  white-space: nowrap;
}
.btn:active { transform: scale(0.97); }
.btn .ic { width: 19px; height: 19px; }
.btn.primary { background: var(--tm-forest); color: #fff; box-shadow: 0 6px 16px -6px rgba(47, 93, 70, 0.6); }
.btn.accent { background: var(--tm-accent); color: #fff; box-shadow: 0 6px 16px -6px rgba(212, 104, 60, 0.6); }
.btn.soft { background: var(--tm-forest-soft); color: var(--tm-forest); }
.btn.ghost { background: rgba(31, 42, 36, 0.06); color: var(--tm-ink-2); }
.btn.danger { background: var(--tm-danger); color: #fff; }
.btn.small { height: 36px; padding: 0 12px; font-size: 14px; border-radius: 11px; }
.btn.block { width: 100%; }
.btn-row { display: flex; gap: 10px; }
.btn-row > .btn { flex: 1; }

.field {
  width: 100%; height: 46px; padding: 0 14px;
  border-radius: 13px; border: 1px solid var(--tm-line-2);
  background: #fff; color: var(--tm-ink);
  font: inherit; font-size: 16px; /* ≥16px: no iOS focus zoom */
}
.field:focus { outline: none; border-color: var(--tm-forest-2); box-shadow: 0 0 0 3px var(--tm-forest-soft); }

/* ---- Lists & cards ---------------------------------------------------------------- */

.section-label {
  margin: 18px 4px 8px; font-size: 12px; font-weight: 700; letter-spacing: 0.06em;
  text-transform: uppercase; color: var(--tm-faint);
}
.list { display: flex; flex-direction: column; gap: 10px; }
.card {
  background: #fff; border: 1px solid var(--tm-line); border-radius: var(--tm-radius);
  padding: 14px; box-shadow: 0 1px 2px rgba(31, 42, 36, 0.04);
}
.card.tappable { cursor: pointer; transition: transform 0.12s var(--ease), box-shadow 0.2s; }
.card.tappable:active { transform: scale(0.985); }
.card.active { border-color: rgba(47, 93, 70, 0.45); box-shadow: 0 0 0 3px var(--tm-forest-soft); }
.card-top { display: flex; align-items: flex-start; gap: 12px; }
.card-title { font-weight: 680; font-size: 16px; letter-spacing: -0.012em; line-height: 1.25; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.card-sub { color: var(--tm-muted); font-size: 13px; margin-top: 2px; }
.card-main { flex: 1; min-width: 0; }
.card-actions { display: flex; gap: 8px; margin-top: 12px; flex-wrap: wrap; }
.card-actions .btn { flex: 1 1 auto; }

.chips { display: flex; flex-wrap: wrap; gap: 6px 14px; margin-top: 8px; font-size: 13px; color: var(--tm-ink-2); font-variant-numeric: tabular-nums; }
.chips span { display: inline-flex; align-items: center; gap: 5px; white-space: nowrap; }
.chips .ic { width: 14px; height: 14px; color: var(--tm-faint); }

.spark { flex: none; margin-top: 2px; }
.spark-fill { fill: rgba(47, 93, 70, 0.12); }
.spark-line { fill: none; stroke: var(--tm-forest); stroke-width: 1.6; stroke-linejoin: round; }

.badge {
  display: inline-flex; align-items: center; gap: 4px; height: 22px; padding: 0 8px; border-radius: 99px;
  font-size: 11.5px; font-weight: 650; background: var(--tm-forest-soft); color: var(--tm-forest); white-space: nowrap;
}
.badge.warn { background: rgba(217, 154, 43, 0.16); color: #8a5a12; }
.badge.accent { background: var(--tm-accent-soft); color: #a3482a; }

.empty {
  text-align: center; color: var(--tm-muted); padding: 26px 18px 10px; font-size: 14px;
}
.empty .ic { margin: 0 auto 10px; color: var(--tm-faint); width: 34px; height: 34px; }
.empty b { display: block; color: var(--tm-ink); font-size: 16px; margin-bottom: 4px; }

/* Settings rows */
.rows { background: #fff; border: 1px solid var(--tm-line); border-radius: var(--tm-radius); overflow: hidden; }
.row {
  display: flex; align-items: center; gap: 12px; min-height: 54px; padding: 10px 14px;
  border-top: 1px solid var(--tm-line);
}
.row:first-child { border-top: 0; }
.row-ic {
  width: 32px; height: 32px; border-radius: 10px; display: grid; place-items: center; flex: none;
  background: var(--tm-forest-soft); color: var(--tm-forest);
}
.row-ic .ic { width: 18px; height: 18px; }
.row-main { flex: 1; min-width: 0; }
.row-title { font-weight: 600; }
.row-sub { font-size: 12.5px; color: var(--tm-muted); margin-top: 1px; }

/* iOS-style switch */
.switch { position: relative; width: 51px; height: 31px; flex: none; }
.switch input { position: absolute; opacity: 0; inset: 0; margin: 0; cursor: pointer; z-index: 1; }
.switch span {
  position: absolute; inset: 0; border-radius: 99px; background: rgba(31, 42, 36, 0.16);
  transition: background 0.22s var(--ease);
}
.switch span::after {
  content: ""; position: absolute; top: 2px; left: 2px; width: 27px; height: 27px; border-radius: 50%;
  background: #fff; box-shadow: 0 2px 6px rgba(0, 0, 0, 0.2);
  transition: transform 0.22s var(--ease);
}
.switch input:checked + span { background: var(--tm-forest-2); }
.switch input:checked + span::after { transform: translateX(20px); }

/* Segmented control */
.seg { display: inline-flex; background: rgba(31, 42, 36, 0.07); border-radius: 10px; padding: 2px; }
.seg button { height: 30px; padding: 0 12px; border-radius: 8px; font-size: 13px; font-weight: 600; color: var(--tm-muted); }
.seg button.on { background: #fff; color: var(--tm-ink); box-shadow: 0 1px 3px rgba(0, 0, 0, 0.12); }

/* Layer tiles */
.layer-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.layer-tile {
  position: relative; text-align: left; padding: 12px; border-radius: 16px; min-height: 92px;
  background: #fff; border: 1px solid var(--tm-line);
  display: flex; flex-direction: column; justify-content: space-between; gap: 10px;
  transition: border-color 0.2s, box-shadow 0.2s;
}
.layer-tile .swatch { width: 38px; height: 38px; border-radius: 11px; display: grid; place-items: center; }
.layer-tile .lt-name { font-weight: 650; font-size: 14px; }
.layer-tile .lt-sub { font-size: 12px; color: var(--tm-muted); }
.layer-tile .tick {
  position: absolute; top: 10px; right: 10px; width: 22px; height: 22px; border-radius: 50%;
  border: 1.5px solid var(--tm-line-2); display: grid; place-items: center; color: transparent;
}
.layer-tile .tick .ic { width: 13px; height: 13px; }
.layer-tile.on { border-color: rgba(47, 93, 70, 0.45); box-shadow: 0 0 0 3px var(--tm-forest-soft); }
.layer-tile.on .tick { background: var(--tm-forest); border-color: var(--tm-forest); color: #fff; }

/* Progress */
.progress { height: 8px; border-radius: 99px; background: rgba(31, 42, 36, 0.08); overflow: hidden; }
.progress i { display: block; height: 100%; width: 0; border-radius: inherit; background: linear-gradient(90deg, var(--tm-forest-2), var(--tm-forest)); transition: width 0.25s linear; }

.meter { display: flex; justify-content: space-between; font-size: 12.5px; color: var(--tm-muted); margin-top: 6px; font-variant-numeric: tabular-nums; }

/* ---- HUD (active route) ------------------------------------------------------------ */

.hud, .drawbar {
  position: fixed; z-index: 7;
  left: 50%; transform: translateX(-50%);
  bottom: calc(var(--safe-b) + var(--dock-h) + 20px);
  width: min(560px, calc(100% - 24px));
  border-radius: 22px; padding: 12px 14px 10px;
}
body[data-mode="draw"] .dock, body[data-mode="download"] .dock { display: none; }
body[data-mode="draw"] .drawbar, body[data-mode="download"] .drawbar { bottom: calc(var(--safe-b) + 12px); }
body[data-mode="draw"] .hud, body[data-mode="download"] .hud { display: none; }
body[data-mode="draw"] .maplibregl-ctrl-bottom-right,
body[data-mode="download"] .maplibregl-ctrl-bottom-right { display: none; }
body[data-sheet] .hud { opacity: 0; pointer-events: none; }

.hud-head { display: flex; align-items: center; gap: 10px; }
.hud-title { flex: 1; min-width: 0; font-weight: 700; font-size: 15px; letter-spacing: -0.015em; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.hud-state { font-size: 12px; font-weight: 600; color: var(--tm-muted); white-space: nowrap; }
.hud-state.off { color: var(--tm-danger); }
.hud-state.on { color: var(--tm-forest); }
.hud .icon-btn { width: 32px; height: 32px; }

.stats { display: grid; grid-template-columns: repeat(4, 1fr); gap: 4px; margin: 10px 0 6px; }
.stat { min-width: 0; }
.stat-v { font-size: 21px; font-weight: 720; letter-spacing: -0.03em; font-variant-numeric: tabular-nums; line-height: 1.1; white-space: nowrap; }
.stat-v small { font-size: 12px; font-weight: 600; color: var(--tm-muted); letter-spacing: 0; margin-left: 2px; }
.stat-k { font-size: 11px; font-weight: 650; text-transform: uppercase; letter-spacing: 0.06em; color: var(--tm-faint); margin-top: 2px; white-space: nowrap; }
.hud-profile { height: 118px; margin: 0 -4px; }
.hud.collapsed .hud-profile { display: none; }
.hud.collapsed .stats { margin-bottom: 0; }

/* Draw bar */
.draw-hint { font-size: 13px; color: var(--tm-muted); display: flex; align-items: center; gap: 8px; margin-bottom: 8px; }
.draw-hint b { color: var(--tm-ink); font-weight: 650; }
.draw-tools { display: flex; align-items: center; gap: 8px; margin-top: 10px; }
.draw-tools .btn.small { flex: none; }
.draw-tools .spacer { flex: 1; }
.tool { height: 40px; min-width: 40px; padding: 0 10px; border-radius: 12px; display: inline-flex; align-items: center; justify-content: center; gap: 6px; background: rgba(31, 42, 36, 0.06); font-size: 13px; font-weight: 650; color: var(--tm-ink-2); }
.tool .ic { width: 18px; height: 18px; }
.tool.on { background: var(--tm-accent-soft); color: #a3482a; }
.drawbar .hud-profile { height: 96px; }

/* ---- Download frame ------------------------------------------------------------------ */

#frame { position: fixed; inset: 0; z-index: 4; pointer-events: none; display: none; }
body[data-mode="download"] #frame { display: block; }
.frame-box {
  position: absolute;
  top: calc(var(--safe-t) + 112px); left: 26px; right: 26px; bottom: calc(var(--safe-b) + 250px);
  border-radius: 18px;
  border: 2px solid #fff;
  box-shadow: 0 0 0 4px rgba(212, 104, 60, 0.4), 0 0 0 200vmax rgba(24, 32, 28, 0.45), 0 0 0 1px rgba(31, 42, 36, 0.25) inset;
}
.frame-size {
  position: absolute; top: 10px; left: 50%; transform: translateX(-50%);
  background: rgba(24, 32, 28, 0.72); color: #fff; font-size: 12.5px; font-weight: 650;
  padding: 5px 11px; border-radius: 99px; white-space: nowrap; font-variant-numeric: tabular-nums;
}

/* ---- Toast & dialog -------------------------------------------------------------------- */

.toast {
  position: fixed; z-index: 30; left: 50%; top: calc(var(--safe-t) + 62px);
  transform: translate(-50%, -12px); opacity: 0; pointer-events: none;
  max-width: calc(100% - 32px);
  background: rgba(31, 42, 36, 0.92); color: #fff; font-size: 14px; font-weight: 550;
  padding: 10px 16px; border-radius: 14px; box-shadow: 0 10px 30px -8px rgba(0, 0, 0, 0.4);
  transition: opacity 0.25s, transform 0.3s var(--ease);
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }
.toast[data-kind="error"] { background: rgba(160, 52, 34, 0.95); }
.toast[data-kind="ok"] { background: rgba(37, 82, 60, 0.95); }

.dialog-backdrop {
  position: fixed; inset: 0; z-index: 40; display: grid; place-items: center; padding: 24px;
  background: rgba(20, 28, 24, 0); transition: background 0.2s;
}
.dialog-backdrop.show { background: rgba(20, 28, 24, 0.4); }
.dialog {
  width: min(360px, 100%); background: var(--tm-surface); border-radius: 22px; padding: 20px;
  box-shadow: 0 24px 60px -12px rgba(0, 0, 0, 0.45);
  transform: scale(0.94); opacity: 0; transition: transform 0.22s var(--ease), opacity 0.2s;
}
.dialog-backdrop.show .dialog { transform: none; opacity: 1; }
.dialog h3 { margin: 0 0 6px; font-size: 18px; letter-spacing: -0.02em; }
.dialog p { margin: 0 0 14px; color: var(--tm-muted); font-size: 14px; }
.dialog .field { margin: 6px 0 4px; }
.dialog-actions { display: flex; gap: 10px; margin-top: 14px; }
.dialog-actions .btn { flex: 1; }

.note { font-size: 12.5px; color: var(--tm-muted); margin: 10px 4px 0; line-height: 1.45; }
.note a { color: var(--tm-forest); }

/* Methodology page */
.doc { font-size: 14px; line-height: 1.55; color: var(--tm-ink-2); }
.doc h1 { font-size: 20px; margin: 4px 0 8px; color: var(--tm-ink); letter-spacing: -0.02em; }
.doc h2 { font-size: 16px; margin: 18px 0 6px; color: var(--tm-ink); }
.doc h3 { font-size: 14px; margin: 14px 0 4px; color: var(--tm-ink); }
.doc p, .doc ul { margin: 0 0 10px; }
.doc ul { padding-left: 20px; }
.doc code { background: rgba(31, 42, 36, 0.06); padding: 1px 5px; border-radius: 5px; font-size: 12.5px; }
.doc a { color: var(--tm-forest); }

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