:root {
  --bg: #f4f6fa;
  --surface: #ffffff;
  --surface-2: #f0f3f8;
  --border: #dde3ec;
  --text: #17202e;
  --muted: #5d6b80;
  --accent: #2563eb;
  --accent-text: #ffffff;
  --accent-soft: #e5edff;
  --home: #2563eb;
  --dest: #ea580c;
  --best: #dcfce7;
  --best-text: #14532d;
  --warn-bg: #fff7e0;
  --warn-text: #7a5300;
  --error: #b42318;
  --error-bg: #fdecea;
  --gold: #f59e0b;
  --radius: 12px;
  --shadow: 0 1px 2px rgb(16 24 40 / 6%), 0 1px 3px rgb(16 24 40 / 8%);
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #0f141c;
    --surface: #171e29;
    --surface-2: #1f2836;
    --border: #2c3747;
    --text: #e7ecf3;
    --muted: #97a3b6;
    --accent: #5b8cff;
    --accent-text: #0b1220;
    --accent-soft: #1d2a4a;
    --home: #6b9bff;
    --dest: #fb8c4b;
    --best: #143d26;
    --best-text: #b7f0cb;
    --warn-bg: #3a2f12;
    --warn-text: #f5d58a;
    --error: #ff8a80;
    --error-bg: #3b1b1b;
    --shadow: none;
    color-scheme: dark;
  }
}

:root[data-theme="dark"] {
  --bg: #0f141c;
  --surface: #171e29;
  --surface-2: #1f2836;
  --border: #2c3747;
  --text: #e7ecf3;
  --muted: #97a3b6;
  --accent: #5b8cff;
  --accent-text: #0b1220;
  --accent-soft: #1d2a4a;
  --home: #6b9bff;
  --dest: #fb8c4b;
  --best: #143d26;
  --best-text: #b7f0cb;
  --warn-bg: #3a2f12;
  --warn-text: #f5d58a;
  --error: #ff8a80;
  --error-bg: #3b1b1b;
  --shadow: none;
  color-scheme: dark;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 15px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}

h2 { font-size: 1.05rem; margin: 0; }
h3 { font-size: 0.95rem; margin: 12px 0 6px; }
small { color: var(--muted); }
input, select, button { font: inherit; color: inherit; }

input, select {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 7px 10px;
  min-width: 0;
}
input:focus-visible, select:focus-visible, button:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 1px;
}
input:disabled, select:disabled { opacity: 0.55; }

/* ---------- Top bar ---------- */

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  flex-wrap: wrap;
  padding: 12px 24px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}
.brand {
  display: flex;
  align-items: center;
  gap: 10px;
  color: inherit;
  font-weight: 700;
  font-size: 1.1rem;
  text-decoration: none;
}
.account { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }
.account .email { color: var(--muted); font-size: 0.9rem; }

.badge {
  font-size: 0.75rem;
  font-weight: 700;
  padding: 2px 8px;
  border-radius: 999px;
  background: var(--surface-2);
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: 0.04em;
}
.badge.paid { background: var(--accent); color: var(--accent-text); }

/* ---------- Buttons ---------- */

.btn {
  border: 1px solid var(--border);
  background: var(--surface);
  border-radius: 8px;
  padding: 7px 14px;
  cursor: pointer;
  white-space: nowrap;
}
.btn:hover { background: var(--surface-2); }
a.btn { color: inherit; text-decoration: none; display: inline-block; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-text); font-weight: 600; }
.btn.primary:hover { filter: brightness(1.08); }
.btn.big { width: 100%; padding: 12px; font-size: 1rem; }
.btn.small { padding: 4px 10px; font-size: 0.85rem; }
.btn:disabled { opacity: 0.6; cursor: default; }

.icon-btn {
  border: none;
  background: none;
  color: var(--muted);
  cursor: pointer;
  width: 28px;
  height: 28px;
  border-radius: 6px;
  flex: none;
}
.icon-btn:hover:not(:disabled) { background: var(--surface-2); color: var(--text); }
.icon-btn:disabled { opacity: 0.3; cursor: default; }

.link {
  border: none;
  background: none;
  color: var(--accent);
  cursor: pointer;
  padding: 0;
  text-decoration: underline;
}

/* ---------- Layout ---------- */

.layout {
  display: grid;
  grid-template-columns: minmax(320px, 420px) minmax(0, 1fr);
  gap: 20px;
  padding: 20px 24px 40px;
  max-width: 1500px;
  margin: 0 auto;
  align-items: start;
}
.inputs, .results { display: flex; flex-direction: column; gap: 16px; min-width: 0; }

@media (max-width: 860px) {
  .layout { grid-template-columns: 1fr; padding: 16px; }
  .topbar { padding: 12px 16px; }
}

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 16px;
}
.card[hidden] { display: none; }
.card-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  margin-bottom: 10px;
}
.count { color: var(--muted); font-size: 0.85rem; }
.hint { color: var(--muted); font-size: 0.85rem; margin: 8px 0; }
.muted { color: var(--muted); }
.pro {
  font-size: 0.7rem;
  font-weight: 700;
  color: var(--accent);
  background: var(--accent-soft);
  padding: 2px 8px;
  border-radius: 999px;
  text-transform: uppercase;
  letter-spacing: 0.04em;
}

.row { display: flex; gap: 8px; align-items: center; margin: 8px 0; }
.row.wrap { flex-wrap: wrap; }
.inline-field { display: inline-flex; flex-direction: row; align-items: center; gap: 6px; font-size: 0.85rem; color: var(--muted); }
.inline-field input { width: 64px; }
.check { display: flex; gap: 8px; align-items: center; font-size: 0.9rem; }

/* ---------- Sidebar lists ---------- */

.place-list { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 6px; }
.place-list:not(:empty) { margin-bottom: 12px; }
.place {
  display: flex;
  align-items: center;
  gap: 4px;
  padding: 4px 6px 4px 4px;
  border-radius: 8px;
  background: var(--surface-2);
}
.place-main {
  display: flex;
  align-items: center;
  gap: 10px;
  flex: 1;
  min-width: 0;
  padding: 4px 6px;
  border: none;
  border-radius: 6px;
  background: none;
  text-align: left;
  cursor: pointer;
}
.place-main:hover { background: var(--accent-soft); }
.place-text { display: flex; flex-direction: column; min-width: 0; flex: 1; }
.place-text small { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.add-btn { width: 100%; }

.collapsible > summary { cursor: pointer; list-style: none; margin-bottom: 0; }
.collapsible > summary::-webkit-details-marker { display: none; }
.collapsible > summary h2::before { content: '▸'; display: inline-block; width: 1em; color: var(--muted); transition: transform 0.15s; }
.collapsible[open] > summary h2::before { transform: rotate(90deg); }
.collapsible[open] > summary { margin-bottom: 10px; }

.dot { width: 12px; height: 12px; border-radius: 50%; flex: none; }
.dot.home { background: var(--home); }
.dot.dest { background: var(--dest); }

.locked {
  margin-top: 10px;
  padding: 12px;
  border-radius: 8px;
  background: var(--accent-soft);
  display: flex;
  flex-direction: column;
  gap: 8px;
  align-items: flex-start;
}
.locked p { margin: 0; font-size: 0.88rem; }
.example {
  font-size: 0.9rem;
  padding: 8px 10px;
  border-radius: 8px;
  background: var(--surface-2);
  margin: 8px 0 0;
}

/* ---------- Trip editor ---------- */

.trip-form h3 { margin: 4px 0 -4px; }
.trip-form .hint { margin: 0; }
.trip-form .add-stop { align-self: flex-start; }
.stops { margin: 8px 0; padding-left: 22px; display: flex; flex-direction: column; gap: 6px; }
.stop { padding-left: 2px; }
.stop > * { vertical-align: middle; }
.stop select { max-width: 100%; width: 210px; }
.stop .inline-field { margin-left: 6px; }
.stop-actions { display: inline-flex; margin-left: 4px; }

.calc-bar .hint { text-align: center; }

/* ---------- Results ---------- */

.banner {
  background: var(--warn-bg);
  color: var(--warn-text);
  padding: 10px 24px;
  font-size: 0.9rem;
}
.banner.subtle { border-radius: var(--radius); padding: 10px 14px; }
.banner[hidden], .message[hidden], .toast[hidden] { display: none; }

.message { border-radius: var(--radius); padding: 10px 14px; font-size: 0.92rem; }
.message.error { background: var(--error-bg); color: var(--error); }
.error { color: var(--error); font-size: 0.88rem; }

.toast {
  position: fixed;
  left: 50%;
  bottom: 24px;
  transform: translateX(-50%);
  background: var(--text);
  color: var(--bg);
  padding: 10px 18px;
  border-radius: 999px;
  z-index: 2000;
  max-width: calc(100% - 32px);
}

.units { display: inline-flex; border: 1px solid var(--border); border-radius: 8px; overflow: hidden; }
.units button { border: none; background: none; padding: 3px 10px; cursor: pointer; font-size: 0.85rem; color: var(--muted); }
.units button.active { background: var(--accent); color: var(--accent-text); }

.empty { color: var(--muted); margin: 4px 0; }

.ranking { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 6px; }
.ranking li {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 10px 12px;
  border-radius: 10px;
  background: var(--surface-2);
}
.ranking li.winner { background: var(--best); color: var(--best-text); }
.ranking li.winner small { color: inherit; opacity: 0.85; }
.rank {
  width: 28px;
  height: 28px;
  border-radius: 50%;
  display: grid;
  place-items: center;
  font-weight: 700;
  background: var(--surface);
  flex: none;
}
.winner .rank { background: var(--gold); color: #1a1200; }
.rank-text { flex: 1; min-width: 0; display: flex; flex-direction: column; }
.rank-text small { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.rank-total { text-align: right; display: flex; flex-direction: column; flex: none; }
.rank-total strong { font-size: 1.1rem; font-variant-numeric: tabular-nums; }

.head-actions { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; justify-content: flex-end; }
.table-wrap { overflow-x: auto; }
.grid { border-collapse: separate; border-spacing: 0; width: 100%; font-variant-numeric: tabular-nums; }
.grid th, .grid td {
  padding: 0;
  border-bottom: 1px solid var(--border);
  text-align: center;
  white-space: nowrap;
}
.grid thead th { padding: 6px 10px; font-size: 0.85rem; vertical-align: bottom; }
.grid thead th small { display: block; font-weight: 400; }
.grid tbody th { text-align: left; padding: 8px 10px; font-weight: 600; max-width: 180px; overflow: hidden; text-overflow: ellipsis; }
/* The Home column stays in place while the rest of the table scrolls sideways. */
.grid thead th:first-child, .grid tbody th {
  position: sticky;
  left: 0;
  z-index: 1;
  background: var(--surface);
  box-shadow: inset -1px 0 0 var(--border);
}
.grid thead th:first-child .sort { align-items: flex-start; }
.sort {
  display: flex;
  flex-direction: column;
  align-items: center;
  width: 100%;
  padding: 0;
  border: none;
  background: none;
  font-weight: inherit;
  cursor: pointer;
}
.sort:hover { color: var(--accent); }
.sort-arrow { color: var(--muted); font-size: 0.75em; }
.sort.active .sort-arrow { color: var(--accent); }
.grid td.na, .grid td.weekly { padding: 8px 10px; color: var(--muted); }
.grid td.weekly { font-weight: 600; color: var(--text); }
.grid td.best { background: var(--best); }
.grid td.best .cell, .grid td.best small { color: var(--best-text); }
.grid td.selected { box-shadow: inset 0 0 0 2px var(--accent); }
.cell {
  display: flex;
  flex-direction: column;
  align-items: center;
  width: 100%;
  padding: 8px 10px;
  border: none;
  background: none;
  cursor: pointer;
}
.cell:hover { background: var(--accent-soft); }

.legs { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 4px; }
.legs li {
  display: grid;
  grid-template-columns: 72px 1fr auto auto;
  gap: 10px;
  align-items: baseline;
  padding: 6px 8px;
  border-radius: 6px;
  background: var(--surface-2);
}
.legs li.arrive { background: var(--accent-soft); }
.leg-time { color: var(--muted); font-size: 0.85rem; font-variant-numeric: tabular-nums; }
@media (max-width: 520px) {
  .legs li { grid-template-columns: 60px 1fr auto; }
  .legs li small { grid-column: 2 / -1; }
}

.map-card { padding: 0; overflow: hidden; }
#map { height: 420px; background: var(--surface-2); }
.leaflet-container path.marker-home { stroke: #fff; fill: var(--home); fill-opacity: 1; }
.leaflet-container path.marker-dest { stroke: #fff; fill: var(--dest); fill-opacity: 1; }
.leaflet-container path.route-line { stroke: var(--accent); stroke-opacity: 0.85; }

/* ---------- Dialogs ---------- */

dialog {
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
  color: var(--text);
  padding: 20px;
  width: min(420px, calc(100% - 32px));
}
dialog.wide { width: min(560px, calc(100% - 32px)); max-height: calc(100vh - 48px); }
dialog::backdrop { background: rgb(0 0 0 / 45%); }
dialog form, .upgrade { display: flex; flex-direction: column; gap: 12px; }
dialog label { display: flex; flex-direction: column; gap: 4px; font-size: 0.9rem; }
.dialog-actions { display: flex; gap: 8px; }
.btn.google { display: block; text-align: center; text-decoration: none; color: inherit; font-weight: 600; padding: 10px; }
.or { margin: 10px 0 0; text-align: center; font-size: 0.85rem; color: var(--muted); }
.price { font-size: 1.3rem; font-weight: 700; margin: 0; }
.features { margin: 0; padding-left: 18px; display: flex; flex-direction: column; gap: 6px; font-size: 0.92rem; }

/* ---------- Place editor ---------- */

.per-home { border: 1px solid var(--border); border-radius: 8px; padding: 10px 12px 12px; margin: 0; display: flex; flex-direction: column; gap: 8px; }
.per-home legend { font-size: 0.9rem; font-weight: 600; padding: 0 4px; }
.per-home .hint { margin: 0; }
.cell-where { display: block; color: var(--muted); font-size: 0.75rem; }

/* ---------- Address suggestions ---------- */

.suggest { position: relative; display: flex; flex-direction: column; }
.suggestions {
  position: fixed;
  z-index: 10;
  margin: 0;
  padding: 4px;
  list-style: none;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 8px;
  box-shadow: 0 6px 20px rgb(16 24 40 / 18%);
  overflow-y: auto;
}
.suggestions[hidden] { display: none; }
.suggestions [role=option] {
  display: flex;
  flex-direction: column;
  padding: 6px 8px;
  border-radius: 6px;
  cursor: pointer;
  font-size: 0.9rem;
}
.suggestions [role=option] small { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.suggestions [aria-selected=true] { background: var(--accent-soft); }
.suggestions-credit { padding: 4px 8px 2px; font-size: 0.7rem; color: var(--muted); }

.topbar-right { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }
.hint a, .locked a { color: var(--accent); }

/* ---------- Help page ---------- */

.help { max-width: 820px; margin: 0 auto; padding: 24px 24px 60px; display: flex; flex-direction: column; gap: 16px; }
.help h1 { font-size: 1.6rem; margin: 8px 0 0; }
.help .lede { font-size: 1.05rem; margin: 0; }
.help .card h2 { margin-bottom: 8px; display: flex; align-items: center; gap: 8px; }
.help .card p, .help .card li { line-height: 1.6; }
.help .card > :last-child { margin-bottom: 0; }
.help a { color: var(--accent); }
.help section { scroll-margin-top: 16px; }
.account-page { width: 100%; }
.account-page [hidden] { display: none !important; }
.account-page #account-message:empty { display: none; }
.account-page #account-content { display: flex; flex-direction: column; gap: 16px; }
.account-page #account-content[hidden] { display: none; }
.account-details { display: grid; grid-template-columns: minmax(100px, 1fr) 2fr; gap: 12px 20px; }
.account-details dt { color: var(--muted); }
.account-details dd { margin: 0; overflow-wrap: anywhere; }
.account-actions { display: flex; flex-wrap: wrap; gap: 8px; }
.toc ol { margin: 0; padding-left: 20px; columns: 2; column-gap: 24px; }
.plans td, .plans th { padding: 8px 10px; white-space: normal; }
.plans tbody th { max-width: none; }
@media (max-width: 600px) {
  .help { padding: 16px; }
  .toc ol { columns: 1; }
  .account-details { grid-template-columns: 1fr; gap: 4px; }
  .account-details dd { margin-bottom: 12px; }
}
