:root {
  --bg: #f6f1e7;
  --surface: #fffdf8;
  --surface-2: #f1eadc;
  --ink: #1d2621;
  --muted: #69705f;
  --line: #e4d9c5;
  --green: #1c5a43;
  --green-strong: #14402f;
  --green-soft: #dcebe2;
  --red: #b3332a;
  --red-soft: #f6e0dc;
  --gold: #c9952c;
  --gold-soft: #f6e8c6;
  --on-accent: #fffaf0;
  --shadow: 0 1px 2px rgb(40 30 10 / 6%), 0 6px 20px rgb(40 30 10 / 7%);
  --radius: 16px;
  --font: 'DM Sans', system-ui, -apple-system, 'Segoe UI', sans-serif;
  --display: 'Fraunces', Georgia, serif;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #0e1613;
    --surface: #16211c;
    --surface-2: #1d2a24;
    --ink: #ebe5d6;
    --muted: #98a196;
    --line: #2a3a33;
    --green: #5bb58e;
    --green-strong: #0b1a14;
    --green-soft: #1f3a2e;
    --red: #e46b5f;
    --red-soft: #3a211e;
    --gold: #e6bb5a;
    --gold-soft: #3a3020;
    --on-accent: #0e1613;
    --shadow: 0 1px 2px rgb(0 0 0 / 30%), 0 6px 20px rgb(0 0 0 / 25%);
    color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #0e1613; --surface: #16211c; --surface-2: #1d2a24; --ink: #ebe5d6; --muted: #98a196;
  --line: #2a3a33; --green: #5bb58e; --green-strong: #0b1a14; --green-soft: #1f3a2e; --red: #e46b5f;
  --red-soft: #3a211e; --gold: #e6bb5a; --gold-soft: #3a3020; --on-accent: #0e1613; color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  background: var(--bg);
  color: var(--ink);
  font: 15px/1.45 var(--font);
  -webkit-font-smoothing: antialiased;
  min-height: 100dvh;
}
h1, h2, h3 { font-family: var(--display); font-weight: 700; margin: 0; letter-spacing: -0.01em; }
a { color: var(--green); }
button { font: inherit; color: inherit; }

/* ---------- Top bar ---------- */
.topbar {
  position: sticky; top: 0; z-index: 1000;
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
  padding: calc(10px + env(safe-area-inset-top)) 16px 10px;
  background: var(--green-strong);
  color: #fbf4e4;
  box-shadow: 0 1px 0 rgb(0 0 0 / 15%);
}
.brand { display: flex; align-items: center; gap: 10px; min-width: 0; }
.brand h1 { font-size: 19px; line-height: 1.1; }
.brand p { margin: 1px 0 0; font-size: 12.5px; opacity: .75; }

/* ---------- Layout ---------- */
.layout { display: grid; grid-template-columns: 1fr; }
.mapwrap {
  position: sticky; top: calc(58px + env(safe-area-inset-top)); z-index: 1;
  height: 36vh; min-height: 220px;
  border-bottom: 1px solid var(--line);
}
#map { width: 100%; height: 100%; background: var(--surface-2); }
.panel { padding: 16px 16px calc(32px + env(safe-area-inset-bottom)); display: flex; flex-direction: column; gap: 14px; min-width: 0; }

@media (min-width: 900px) {
  .layout { grid-template-columns: minmax(380px, 460px) 1fr; height: calc(100dvh - 58px); }
  .mapwrap { order: 2; position: relative; top: 0; height: 100%; border-bottom: 0; border-left: 1px solid var(--line); }
  .panel { order: 1; overflow-y: auto; padding: 20px; }
}

/* ---------- Cards ---------- */
.card {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 16px;
}

.planner { padding: 0; }
.planner > summary {
  list-style: none; cursor: pointer;
  display: flex; align-items: baseline; justify-content: space-between; gap: 8px;
  padding: 14px 16px;
}
.planner > summary::-webkit-details-marker { display: none; }
.planner > summary::after { content: '▾'; color: var(--muted); transition: transform .2s; }
.planner:not([open]) > summary::after { transform: rotate(-90deg); }
.summary-title { font-family: var(--display); font-weight: 700; font-size: 18px; }
.summary-sub { color: var(--muted); font-size: 13px; margin-left: auto; text-align: right; }
.planner form { padding: 0 16px 16px; display: flex; flex-direction: column; gap: 14px; }

.grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.grid2 .span2 { grid-column: 1 / -1; }
.grid2 .field { min-width: 0; }
.field { display: flex; flex-direction: column; gap: 5px; font-size: 13px; color: var(--muted); font-weight: 500; }
.field > span { display: flex; justify-content: space-between; }
.field output { color: var(--ink); font-weight: 700; }
input[type="date"], input[type="time"], select {
  font: inherit; color: var(--ink);
  background: var(--bg); border: 1px solid var(--line); border-radius: 10px;
  padding: 9px 10px; min-height: 42px; width: 100%;
}
input:focus-visible, select:focus-visible, button:focus-visible { outline: 2px solid var(--gold); outline-offset: 2px; }
input[type="range"] { accent-color: var(--green); width: 100%; }

.row-between { display: flex; flex-wrap: wrap; align-items: center; justify-content: space-between; gap: 10px; }
.seg { display: inline-flex; align-self: flex-start; background: var(--surface-2); border-radius: 999px; padding: 3px; }
.seg label { cursor: pointer; }
.seg input { position: absolute; opacity: 0; pointer-events: none; }
.seg span { display: block; padding: 6px 12px; border-radius: 999px; font-size: 13px; font-weight: 500; color: var(--muted); }
.seg input:checked + span { background: var(--surface); color: var(--ink); box-shadow: var(--shadow); }
.seg input:focus-visible + span { outline: 2px solid var(--gold); }
.check { display: inline-flex; align-items: center; gap: 6px; font-size: 13.5px; cursor: pointer; }
.check input { accent-color: var(--green); width: 17px; height: 17px; }

.markets { border: 0; padding: 0; margin: 0; }
.markets legend { display: flex; width: 100%; justify-content: space-between; font-size: 13px; color: var(--muted); font-weight: 500; padding: 0; margin-bottom: 6px; }
.markets ul { list-style: none; margin: 0; padding: 0; border: 1px solid var(--line); border-radius: 12px; overflow: hidden; }
.markets li + li { border-top: 1px solid var(--line); }
.markets label { display: grid; grid-template-columns: auto 1fr auto; align-items: center; gap: 10px; padding: 9px 12px; cursor: pointer; }
.markets label:hover { background: var(--surface-2); }
.markets input { accent-color: var(--green); width: 17px; height: 17px; margin: 0; }
.markets .mname { font-weight: 500; font-size: 14px; line-height: 1.25; }
.markets .mplace { display: block; color: var(--muted); font-size: 12px; font-weight: 400; }
.markets .mhours { font-size: 12.5px; color: var(--muted); font-variant-numeric: tabular-nums; white-space: nowrap; }
.markets .closed .mhours { color: var(--red); }
.markets .closed .mname { color: var(--muted); }
.tag { display: inline-block; font-size: 10.5px; font-weight: 700; letter-spacing: .04em; text-transform: uppercase; color: var(--gold); margin-left: 4px; }

.linkbtn { background: none; border: 0; padding: 0; color: var(--green); font-size: 13px; font-weight: 500; cursor: pointer; }

/* ---------- Buttons ---------- */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  border-radius: 999px; border: 1px solid transparent;
  padding: 9px 16px; min-height: 40px; font-weight: 700; font-size: 14px; cursor: pointer;
  text-decoration: none; white-space: nowrap;
}
.btn-primary { background: var(--red); color: #fffaf0; }
.btn-primary:hover { filter: brightness(1.06); }
.btn-secondary { background: var(--surface-2); color: var(--ink); border-color: var(--line); }
.btn-secondary:hover { background: var(--line); }
.btn-ghost-light { background: transparent; color: #fbf4e4; border-color: rgb(255 255 255 / 35%); min-height: 34px; padding: 6px 12px; font-size: 13px; }
.btn-block { width: 100%; min-height: 48px; font-size: 15.5px; }
.btn-sm { min-height: 34px; padding: 6px 12px; font-size: 13px; }

/* ---------- Summary ---------- */
.summary h2 { font-size: 22px; }
.summary .sub { color: var(--muted); margin: 2px 0 12px; font-size: 13.5px; }
.stats { display: grid; grid-template-columns: repeat(4, 1fr); gap: 8px; }
.stat { background: var(--surface-2); border-radius: 12px; padding: 10px; }
.stat b { display: block; font-family: var(--display); font-size: 20px; line-height: 1.1; font-variant-numeric: tabular-nums; }
.stat span { font-size: 11.5px; color: var(--muted); }
.summary .actions { display: flex; gap: 8px; margin-top: 12px; flex-wrap: wrap; }
@media (max-width: 420px) { .stats { grid-template-columns: repeat(2, 1fr); } }

/* ---------- Rally ---------- */
.rally { display: flex; flex-direction: column; gap: 12px; background: linear-gradient(135deg, var(--green-soft), var(--surface) 70%); }
.rally-head { display: flex; align-items: center; gap: 14px; }
.ring { width: 64px; height: 64px; flex: none; }
.ring circle { fill: none; stroke-width: 7; }
.ring .track { stroke: var(--line); }
.ring .bar { stroke: var(--gold); stroke-linecap: round; transition: stroke-dashoffset .5s; transform: rotate(-90deg); transform-origin: 50% 50%; }
.ring text { font-family: var(--display); font-weight: 700; font-size: 17px; fill: var(--ink); }
.rally-head h3 { font-size: 17px; }
.rally-head p { margin: 2px 0 0; color: var(--muted); font-size: 13.5px; }
.rally-actions { display: flex; flex-wrap: wrap; gap: 8px; }
.stamps { display: flex; flex-wrap: wrap; gap: 5px; }
.stamps i { width: 22px; height: 22px; border-radius: 50%; display: grid; place-items: center; font-style: normal; font-size: 12px; background: var(--surface-2); color: var(--muted); border: 1px dashed var(--line); }
.stamps i.on { background: var(--gold); color: #3a2a06; border: 0; }

/* ---------- Timeline ---------- */
.timeline { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; }
.leg {
  display: flex; align-items: center; gap: 8px;
  margin-left: 21px; padding: 8px 0 8px 22px;
  border-left: 2px dashed var(--line);
  color: var(--muted); font-size: 13px;
}
.leg.transit { border-left-style: solid; border-left-color: var(--green); }
.leg svg { width: 16px; height: 16px; flex: none; }
.leg a { margin-left: auto; font-weight: 500; font-size: 12.5px; white-space: nowrap; }
.leg .wait { color: var(--gold); font-weight: 500; }

.stop {
  display: grid; grid-template-columns: 44px 1fr; gap: 12px;
  background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius);
  padding: 12px; box-shadow: var(--shadow); cursor: pointer;
  transition: border-color .2s, transform .2s;
}
.stop:hover { border-color: var(--green); }
.stop.focus { border-color: var(--gold); box-shadow: 0 0 0 3px var(--gold-soft), var(--shadow); }
.stop .num {
  width: 44px; height: 44px; border-radius: 50%;
  display: grid; place-items: center;
  background: var(--green); color: var(--on-accent);
  font-family: var(--display); font-weight: 700; font-size: 18px;
}
.stop.done .num { background: var(--gold); color: #3a2a06; }
.stop.next .num { background: var(--red); color: #fffaf0; box-shadow: 0 0 0 4px var(--red-soft); }
.stop h3 { font-size: 16.5px; line-height: 1.2; }
.stop .meta { color: var(--muted); font-size: 12.5px; margin: 2px 0 6px; }
.stop .times { font-variant-numeric: tabular-nums; font-size: 14px; display: flex; flex-wrap: wrap; gap: 4px 10px; align-items: baseline; }
.stop .times b { font-size: 17px; font-family: var(--display); }
.stop .warn { color: var(--red); font-size: 12.5px; font-weight: 500; }
.stop .tip { margin: 6px 0 0; font-size: 13.5px; color: var(--ink); opacity: .85; }
.stop .acts { display: flex; gap: 8px; margin-top: 10px; flex-wrap: wrap; }
.stamp-btn { border: 1.5px solid var(--gold); background: transparent; color: var(--ink); }
.stamp-btn[aria-pressed="true"] { background: var(--gold); color: #3a2a06; }
.punsch-btn { background: var(--surface-2); border-color: var(--line); }
.badge { font-size: 11px; font-weight: 700; letter-spacing: .04em; text-transform: uppercase; padding: 2px 7px; border-radius: 999px; }
.badge.next { background: var(--red-soft); color: var(--red); }
.badge.done { background: var(--gold-soft); color: var(--gold); }
.timeline .finish { margin-left: 21px; padding: 8px 0 0 22px; border-left: 2px dashed var(--line); color: var(--muted); font-size: 13px; }
.timeline .finish b { color: var(--ink); }

.skipped h3 { font-size: 15px; margin-bottom: 6px; }
.skipped .warn { color: var(--red); font-size: 13.5px; margin: 0 0 8px; }
.skipped ul { margin: 0; padding-left: 18px; font-size: 13.5px; }
.skipped li span { color: var(--muted); }

.disclaimer { color: var(--muted); font-size: 12px; margin: 4px 2px 0; }

/* ---------- Map markers ---------- */
.pin {
  width: 30px; height: 30px; border-radius: 50%;
  display: grid; place-items: center;
  background: #1c5a43; color: #fffaf0; border: 2px solid #fffaf0;
  font: 700 13px/1 var(--font); box-shadow: 0 2px 6px rgb(0 0 0 / 35%);
}
.pin.done { background: #c9952c; color: #3a2a06; }
.pin.next { background: #b3332a; }
.pin.off { background: #8d9289; opacity: .75; width: 22px; height: 22px; font-size: 0; }
.pin.start { background: #fffaf0; color: #14402f; border-color: #14402f; font-size: 14px; }
.pin.me { width: 18px; height: 18px; background: #2f7de1; border: 3px solid #fff; box-shadow: 0 0 0 6px rgb(47 125 225 / 25%); }
.leaflet-popup-content { font: 13.5px/1.4 var(--font); margin: 10px 14px; }
.leaflet-popup-content b { font-size: 14.5px; }
.leaflet-container { font-family: var(--font); }
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) .leaflet-tile-pane { filter: invert(1) hue-rotate(180deg) brightness(.9) contrast(.85) saturate(.6); }
}
:root[data-theme="dark"] .leaflet-tile-pane { filter: invert(1) hue-rotate(180deg) brightness(.9) contrast(.85) saturate(.6); }

/* ---------- Toast / dialog ---------- */
#toast {
  position: fixed; left: 50%; bottom: calc(20px + env(safe-area-inset-bottom)); z-index: 2000;
  transform: translate(-50%, 30px); opacity: 0; pointer-events: none;
  background: var(--ink); color: var(--bg); padding: 10px 16px; border-radius: 999px;
  font-size: 14px; font-weight: 500; transition: .25s; max-width: calc(100vw - 32px); text-align: center;
}
#toast.show { opacity: 1; transform: translate(-50%, 0); }

.done-dialog { border: 0; border-radius: 22px; padding: 0; max-width: min(380px, calc(100vw - 32px)); background: var(--surface); color: var(--ink); box-shadow: var(--shadow); }
.done-dialog::backdrop { background: rgb(10 20 15 / 55%); }
.done-inner { padding: 28px 24px 22px; text-align: center; }
.done-stars { color: var(--gold); font-size: 26px; letter-spacing: 6px; }
.done-inner h2 { font-size: 26px; margin: 6px 0 8px; }
.done-inner p { color: var(--muted); margin: 0 0 18px; }

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

@media (max-width: 480px) {
  .brand h1 { font-size: 17px; white-space: nowrap; }
  .brand p { font-size: 12px; }
  .brand img { width: 30px; height: 30px; }
  .topbar .btn-ghost-light { padding: 5px 10px; font-size: 12.5px; min-height: 30px; }
}
