:root {
  color-scheme: light;
  --bg: #eef2ef;
  --bg-elev: #ffffff;
  --ink: #14211b;
  --muted: #5c6d65;
  --line: #e1e8e3;
  --accent: #0f766e;
  --accent-ink: #ffffff;
  --accent-soft: #e5f4f1;
  --ok: #067647;
  --ok-soft: #e8f6ee;
  --warn: #9a6700;
  --warn-soft: #fff5df;
  --bad: #b42318;
  --bad-soft: #fdeceb;
  --info: #175cd3;
  --info-soft: #eef4ff;
  --shadow: 0 1px 2px rgba(16, 24, 20, 0.04), 0 12px 32px rgba(16, 24, 20, 0.06);
  --radius: 18px;
  --sans: "PingFang HK", "PingFang TC", "Noto Sans TC", "Heiti TC", "Microsoft JhengHei", system-ui, sans-serif;
}

:root[data-theme="dark"] {
  color-scheme: dark;
  --bg: #0c1210;
  --bg-elev: #171f1c;
  --ink: #e8f2ed;
  --muted: #9aafa6;
  --line: #2a3832;
  --accent: #2dd4bf;
  --accent-ink: #042f2c;
  --accent-soft: #12332e;
  --ok: #6ee7b7;
  --ok-soft: #123128;
  --warn: #fbbf24;
  --warn-soft: #2d2414;
  --bad: #fda4af;
  --bad-soft: #3b171b;
  --info: #93c5fd;
  --info-soft: #172554;
  --shadow: 0 1px 0 rgba(255, 255, 255, 0.03);
}

* { box-sizing: border-box; }
html { height: 100%; }
body {
  margin: 0;
  min-height: 100%;
  font-family: var(--sans);
  background: var(--bg);
  color: var(--ink);
  line-height: 1.45;
  text-rendering: optimizeLegibility;
}
body.sheet-open { overflow: hidden; }
button, input { font: inherit; color: inherit; }
button { touch-action: manipulation; -webkit-tap-highlight-color: transparent; }
/* Safari 撳到按鈕入面嘅 SVG 唔會冒泡 click，要由按鈕本身接收。 */
button svg, button svg * { pointer-events: none; }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
[hidden] { display: none !important; }

.app {
  width: min(640px, 100%);
  margin: 0 auto;
  padding: 12px 16px calc(28px + env(safe-area-inset-bottom));
}
body.is-editing .app { padding-bottom: calc(92px + env(safe-area-inset-bottom)); }

.topbar {
  position: sticky;
  top: 0;
  z-index: 10;
  isolation: isolate;
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-wrap: wrap;
  gap: 12px;
  margin: 0 -16px;
  padding: calc(10px + env(safe-area-inset-top)) 16px 12px;
}
/* 模糊放喺背後層。直接加喺頂欄上，Safari 有時會令入面嘅掣接收唔到點擊。 */
.topbar::before, .edit-bar::before {
  content: "";
  position: absolute;
  inset: 0;
  z-index: -1;
  pointer-events: none;
  background: var(--bg);
  background: color-mix(in srgb, var(--bg) 88%, transparent);
  -webkit-backdrop-filter: blur(14px);
  backdrop-filter: blur(14px);
}
.brand { display: flex; align-items: center; gap: 10px; min-width: 0; }
.brand h1 { margin: 0; font-size: 1.25rem; letter-spacing: -0.02em; }
.brand p { margin: 0; color: var(--muted); font-size: 0.78rem; }
.mark {
  width: 40px; height: 40px;
  object-fit: contain;
  display: block;
  flex: none;
}
.tools { display: flex; gap: 6px; flex-wrap: wrap; justify-content: flex-end; }

.icon-btn, .btn, .text-btn {
  border: 1px solid var(--line);
  background: var(--bg-elev);
  border-radius: 999px;
  min-height: 36px;
  padding: 0 12px;
  cursor: pointer;
}
.icon-btn {
  width: 36px; padding: 0;
  display: grid; place-items: center;
}
.icon-btn svg, .card-tools svg { width: 18px; height: 18px; display: block; }
.btn:active, .icon-btn:active, .choice:active, .text-btn:active { background: var(--accent-soft); }
.btn.primary, .icon-btn.primary {
  background: var(--accent); color: var(--accent-ink); border-color: transparent;
}
.btn.ghost, .text-btn { background: transparent; }
.text-btn { border: 0; color: var(--accent); padding: 0 4px; min-height: 32px; }
.btn:disabled { opacity: 0.45; cursor: default; }

.loc-pill { max-width: 46vw; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

.banner, .card, .sheet-panel {
  background: var(--bg-elev);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
}
.banner { padding: 14px; margin-bottom: 12px; }
.banner p { margin: 0 0 10px; }
.row-btns, .sheet-actions { display: flex; flex-wrap: wrap; gap: 8px; }

.board { display: grid; gap: 12px; }
.card { padding: 14px 14px 12px; min-width: 0; }
.card.is-editing { border-style: dashed; }
.card-head { display: flex; align-items: flex-start; gap: 8px; }
.card-title { min-width: 0; flex: 1; }
.card-title h2 { margin: 0; font-size: 1rem; letter-spacing: -0.01em; }
.card-meta { margin: 2px 0 0; color: var(--muted); font-size: 0.78rem; }
.card-tools { display: flex; gap: 4px; align-items: center; }
.card-body { margin-top: 12px; }
.handle {
  width: 22px; color: var(--muted); cursor: grab; border: 0; background: transparent; padding: 4px 0;
}
.kicker { margin: 0; color: var(--muted); font-size: 0.86rem; }
.wx-now { display: flex; align-items: center; gap: 8px; margin: 0; color: var(--muted); font-size: 0.95rem; }
.wx-ico { font-size: 1.65rem; line-height: 1; }
.hero { margin: 2px 0 0; display: flex; align-items: baseline; gap: 4px; }
.num, .hero-text {
  font-size: 2.6rem; font-weight: 650; letter-spacing: -0.04em; line-height: 1;
  font-variant-numeric: tabular-nums;
}
.unit { color: var(--muted); font-size: 1rem; }
.place, .sub, .addr, .note, .empty { color: var(--muted); margin: 6px 0 0; }
.stats { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 12px; }
.stat {
  background: var(--bg); border-radius: 12px; padding: 8px 10px; min-width: 92px;
}
.stat b { display: block; font-size: 1rem; }
.stat span { color: var(--muted); font-size: 0.75rem; }
.forecast { margin: 12px 0 0; }
.outlook { margin: 6px 0 0; color: var(--muted); font-size: 0.92rem; }

.chips { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 12px; }
.chip, .tag {
  display: inline-flex; align-items: center; border-radius: 999px;
  padding: 3px 8px; font-size: 0.78rem; font-weight: 600;
}
.chip.bad, .tag.bad, .tone-bad { background: var(--bad-soft); color: var(--bad); }
.chip.warn, .tag.warn, .tone-warn { background: var(--warn-soft); color: var(--warn); }
.chip.ok, .tone-ok { background: var(--ok-soft); color: var(--ok); }
.chip.info, .tone-info { background: var(--info-soft); color: var(--info); }
.chip.muted, .tone-muted, .tag { background: var(--bg); color: var(--muted); }
.messages { margin: 8px 0 0; padding-left: 1.1rem; }
.messages li { margin: 4px 0; }

.aqhi-grid, .border-list { display: grid; gap: 8px; }
.aqhi-grid { grid-template-columns: 1fr 1fr; }
.aqhi-box, .border-row, .stop-block, .park-row, .ae-block {
  border-top: 1px solid var(--line); padding-top: 10px; margin-top: 10px;
}
.aqhi-box { border: 0; margin: 0; padding: 10px; border-radius: 14px; background: var(--bg); }
.aqhi-box strong, .mins, .vac-num { font-variant-numeric: tabular-nums; }
.station-list { list-style: none; margin: 8px 0 0; padding: 0; }
.station-list li, .eta-list li, .park-row, .border-row {
  display: grid; gap: 2px 8px; align-items: center;
}
.station-list li {
  grid-template-columns: 1fr auto auto;
  padding: 7px 0; border-top: 1px solid var(--line);
}
.eta-list { list-style: none; margin: 8px 0 0; padding: 0; }
.eta-list li {
  grid-template-columns: auto 1fr auto;
  padding: 8px 0;
  border-top: 1px solid var(--line);
}
.route { font-weight: 700; }
.dest { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.mins { font-weight: 700; }
.mins.soon { color: var(--ok); }
.mins.is-quiet { font-size: 0.82rem; font-weight: 650; white-space: nowrap; }
.rmk, .co { grid-column: 1 / -1; color: var(--muted); font-size: 0.78rem; }
.stop-head, .park-head, .ae-head, .border-head {
  display: flex; justify-content: space-between; gap: 8px; align-items: center;
}
.stop-head h3, .park-head h3, .ae-head h3 { margin: 0; font-size: 0.98rem; min-width: 0; }
.dist { color: var(--muted); font-size: 0.78rem; white-space: nowrap; flex: none; display: inline-flex; align-items: center; gap: 2px; }
.dist .text-btn { font-size: 0.92rem; }
.place-actions { margin: 0 0 4px; }
.border-row strong { display: flex; flex-wrap: wrap; align-items: center; gap: 2px; min-width: 0; }
.vac-num { font-size: 1.8rem; font-weight: 650; letter-spacing: -0.03em; line-height: 1; }
.alert {
  margin: 8px 0 0; padding: 8px 10px; border-radius: 12px;
  background: var(--bad-soft); color: var(--bad); font-weight: 650;
}
.seg { display: flex; gap: 6px; margin-bottom: 4px; }
.seg button[aria-pressed="true"] { background: var(--accent); color: var(--accent-ink); border-color: transparent; }
.border-row { grid-template-columns: 1.1fr 1fr 1fr; padding-bottom: 8px; }
.border-row span { font-size: 0.92rem; }
.legend { display: flex; gap: 10px; color: var(--muted); font-size: 0.75rem; margin-top: 8px; }
.install-steps { margin: 8px 0 0; padding-left: 1.25em; }
.install-steps li { margin: 8px 0; }

.edit-only { display: none; }
body.is-editing .edit-only { display: inline; }
.choice:disabled { opacity: 0.55; cursor: default; }

.skeleton {
  height: 72px; border-radius: 12px;
  background: linear-gradient(90deg, var(--bg), color-mix(in srgb, var(--line) 70%, var(--bg)), var(--bg));
  background-size: 200% 100%;
  animation: shimmer 1.2s ease-in-out infinite;
}
.error-box { padding: 4px 0 2px; }
.error-title { margin: 0; font-weight: 700; }
.error-detail { margin: 4px 0 10px; color: var(--muted); font-size: 0.9rem; }
.need-loc p { margin-top: 0; }

.card.is-loading .icon-refresh { animation: spin 0.8s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }
@keyframes shimmer { to { background-position: -200% 0; } }
@media (prefers-reduced-motion: reduce) {
  .skeleton, .card.is-loading .icon-refresh { animation: none; }
}

.edit-bar {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 12;
  isolation: isolate;
  display: flex; justify-content: center; gap: 8px;
  padding: 10px 16px calc(10px + env(safe-area-inset-bottom));
  border-top: 1px solid var(--line);
}

.sheet {
  position: fixed; inset: 0; z-index: 40;
  display: grid; align-items: end; justify-items: center;
}
/* hidden 被上面嘅 display 蓋過時，全屏層會食晒所有點擊。 */
.sheet[hidden] { display: none !important; pointer-events: none !important; }
.sheet-backdrop { position: absolute; inset: 0; background: rgba(0, 0, 0, 0.45); }
.sheet-panel {
  position: relative; width: min(560px, 100%);
  max-height: min(88vh, 760px); overflow: auto;
  border-radius: 20px 20px 0 0; padding: 8px 16px calc(16px + env(safe-area-inset-bottom));
  box-shadow: var(--shadow);
}
.sheet-head { display: flex; align-items: center; justify-content: space-between; gap: 8px; position: sticky; top: 0; background: var(--bg-elev); padding: 8px 0; }
.sheet-head h2 { margin: 0; font-size: 1.05rem; }
.sheet-body { padding-bottom: 8px; }
.search {
  width: 100%; min-height: 44px; border-radius: 12px; border: 1px solid var(--line);
  background: var(--bg); padding: 0 12px; margin: 4px 0 10px;
}
.choice, .check-row {
  width: 100%; text-align: left; border: 1px solid var(--line); background: var(--bg);
  border-radius: 14px; padding: 10px 12px; margin-bottom: 8px; cursor: pointer;
}
.choice small, .hint { display: block; color: var(--muted); font-size: 0.8rem; margin-top: 2px; }
.choice[aria-pressed="true"], .check-row:has(input:checked) {
  border-color: var(--accent); background: var(--accent-soft);
}
.district-grid { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; }
.district-grid button { min-height: 44px; }
.check-row { display: flex; align-items: center; gap: 8px; }
.check-row input { width: 18px; height: 18px; }

.toast {
  position: fixed; left: 50%; bottom: calc(20px + env(safe-area-inset-bottom));
  transform: translateX(-50%);
  background: var(--ink); color: var(--bg); border-radius: 999px;
  padding: 8px 14px; z-index: 50; max-width: min(90vw, 420px); text-align: center;
}
body.is-editing .toast { bottom: calc(78px + env(safe-area-inset-bottom)); }

.card-actions { margin: 12px 0 0; }
.card-actions .btn { width: 100%; }
.sheet-panel.is-map {
  display: flex;
  flex-direction: column;
  height: min(92vh, 860px);
  overflow: hidden;
}
.sheet-panel.is-map .sheet-body {
  display: flex;
  flex-direction: column;
  gap: 8px;
  min-height: 0;
  flex: 1;
  overflow: hidden;
}
.bus-search { position: relative; z-index: 2; }
.bus-tools { display: flex; gap: 8px; align-items: center; }
.bus-tools .search { flex: 1; margin: 0; min-width: 0; }
#bus-suggest, #meter-suggest, #park-suggest {
  position: absolute;
  z-index: 5;
  left: 0; right: 0; top: calc(100% + 4px);
  max-height: 200px;
  overflow: auto;
  background: var(--bg-elev);
  border: 1px solid var(--line);
  border-radius: 12px;
  box-shadow: var(--shadow);
}
#bus-suggest:empty, #meter-suggest:empty, #park-suggest:empty { display: none; }
#bus-suggest .choice, #meter-suggest .choice, #park-suggest .choice { margin: 0; border: 0; border-radius: 0; border-bottom: 1px solid var(--line); }
.bus-map {
  position: relative;
  z-index: 1;
  height: min(42vh, 380px);
  min-height: 220px;
  border-radius: 16px;
  overflow: hidden;
  border: 1px solid var(--line);
  background: #d7e0ea;
  touch-action: none;
}
.bus-map.is-place { flex: 1; height: auto; min-height: 280px; }
#place-map-note, #place-nav { flex: none; }
a.btn { text-decoration: none; display: inline-flex; align-items: center; justify-content: center; }
#place-nav { width: 100%; }
.bus-pick { flex: 1; min-height: 88px; overflow: auto; }
.route-picks { display: flex; flex-wrap: wrap; gap: 6px; }
.route-picks .choice { width: auto; margin: 0; min-height: 40px; }
.leaflet-control-attribution { font-size: 10px; max-width: 70%; }
.footer { margin-top: 22px; color: var(--muted); font-size: 0.78rem; }
.footer a { color: inherit; }
.footer p { margin: 6px 0; }

@media (min-width: 720px) {
  .app { padding-top: 18px; }
  .sheet { align-items: center; }
  .sheet-panel { border-radius: 20px; margin: 24px; }
}
