:root {
  color-scheme: light dark;
  --bg: #f4f7f6;
  --surface: #ffffff;
  --surface-2: #eef3f2;
  --text: #15201e;
  --muted: #5b6b68;
  --line: #d9e2e0;
  --brand: #0f766e;
  --brand-ink: #ffffff;
  --brand-soft: #d7efec;
  --danger: #c0352b;
  --danger-soft: #fde7e5;
  --good: #1d7a3d;
  --good-soft: #def3e4;
  --shadow: 0 1px 2px rgba(0, 0, 0, .06), 0 4px 14px rgba(0, 0, 0, .05);
  --radius: 14px;
  --header-h: 60px;
  --safe-l: env(safe-area-inset-left, 0px);
  --safe-r: env(safe-area-inset-right, 0px);
  --safe-t: env(safe-area-inset-top, 0px);
  --safe-b: env(safe-area-inset-bottom, 0px);
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0e1513;
    --surface: #17211f;
    --surface-2: #1f2c29;
    --text: #e6eeec;
    --muted: #9fb1ad;
    --line: #2b3a37;
    --brand: #2bb3a3;
    --brand-ink: #062320;
    --brand-soft: #173b37;
    --danger: #ff6b5e;
    --danger-soft: #3a1714;
    --good: #5fd18a;
    --good-soft: #163522;
    --shadow: 0 1px 2px rgba(0, 0, 0, .4);
  }
  .leaflet-tile-pane { filter: invert(1) hue-rotate(180deg) brightness(.92) contrast(.9); }
  .leaflet-container { background: #1b2321; }
  .leaflet-bar a, .leaflet-control-attribution { background: var(--surface) !important; color: var(--text) !important; }
  .leaflet-control-attribution a { color: var(--brand) !important; }
}

* { box-sizing: border-box; }

html, body { margin: 0; height: 100%; }

body {
  background: var(--bg);
  color: var(--text);
  font: 16px/1.45 -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang TC", "PingFang SC", "Noto Sans TC", "Noto Sans SC", "Microsoft JhengHei", "Microsoft YaHei", sans-serif;
  -webkit-text-size-adjust: 100%;
  overflow-x: hidden;
}

button, input, select, textarea { font: inherit; color: inherit; }

.sr { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }
.muted { color: var(--muted); }
.small { font-size: .875rem; }
[hidden] { display: none !important; }

.app { min-height: 100dvh; display: flex; flex-direction: column; }

/* ---------- Header ---------- */
.top {
  position: sticky; top: 0; z-index: 1000;
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
  min-height: calc(var(--header-h) + var(--safe-t));
  padding: calc(8px + var(--safe-t)) calc(14px + var(--safe-r)) 8px calc(14px + var(--safe-l));
  background: #0f766e; color: #fff;
}
.brand { display: flex; align-items: center; gap: 10px; min-width: 0; }
.brand img { border-radius: 8px; flex: none; }
.brand > div { min-width: 0; }
.top .lang { flex: none; }
.brand h1 { margin: 0; font-size: 1.15rem; line-height: 1.2; }
.tagline { margin: 0; font-size: .8rem; opacity: .9; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.lang select {
  min-height: 44px; padding: 0 10px; border-radius: 10px;
  border: 1px solid rgba(255, 255, 255, .5); background: rgba(255, 255, 255, .14); color: #fff;
}
.lang select option { color: #111; }

/* ---------- Layout ---------- */
.layout { flex: 1; }
.panel {
  display: flex; flex-direction: column; gap: 12px;
  padding: 12px calc(12px + var(--safe-r)) calc(16px + var(--safe-b)) calc(12px + var(--safe-l));
  max-width: 720px; margin: 0 auto;
}

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

.label { display: block; font-weight: 650; margin-bottom: 8px; }

textarea, input[type="search"] {
  width: 100%; min-height: 44px; padding: 10px 12px; font-size: 16px;
  border: 1px solid var(--line); border-radius: 12px; background: var(--surface-2); resize: vertical;
}
textarea:focus, input:focus, select:focus, button:focus-visible, a:focus-visible {
  outline: 3px solid color-mix(in srgb, var(--brand) 55%, transparent); outline-offset: 2px;
}

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  min-height: 44px; min-width: 44px; padding: 0 16px;
  border-radius: 12px; border: 1px solid var(--line); background: var(--surface-2);
  font-weight: 600; text-decoration: none; cursor: pointer; color: var(--text);
}
.btn.primary { background: var(--brand); border-color: var(--brand); color: var(--brand-ink); }
.btn.wide { width: 100%; }
.link { background: none; border: 0; color: var(--brand); font-weight: 600; text-decoration: underline; min-height: 44px; cursor: pointer; padding: 0 4px; }

.ask-actions { display: flex; gap: 8px; margin-top: 10px; }
.ask-actions .primary { flex: 1; }

.chips-wrap { margin-top: 10px; }
.chips { display: flex; gap: 8px; overflow-x: auto; padding: 6px 0 2px; scrollbar-width: none; }
.chips::-webkit-scrollbar { display: none; }
.chip {
  flex: none; min-height: 44px; padding: 0 14px; border-radius: 999px;
  border: 1px solid var(--line); background: var(--surface); cursor: pointer;
}

/* ---------- Emergency banner ---------- */
.banner {
  background: var(--danger-soft); border: 2px solid var(--danger); border-radius: var(--radius);
  padding: 14px; color: var(--text);
}
.banner-text { margin: 0 0 10px; font-weight: 650; }
.banner-actions { display: flex; flex-wrap: wrap; gap: 8px; }
.call-big {
  display: inline-flex; align-items: center; justify-content: center; flex: 1 1 140px;
  min-height: 52px; padding: 0 18px; border-radius: 12px;
  background: var(--danger); color: #fff; font-size: 1.15rem; font-weight: 750; text-decoration: none;
}
.call-big.ghost { background: transparent; color: var(--danger); border: 2px solid var(--danger); }

/* ---------- Where ---------- */
.where { display: flex; flex-direction: column; gap: 10px; }
.search { display: flex; gap: 8px; }
.search input { flex: 1; min-width: 0; }
.results { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 6px; }
.results:empty { display: none; }
.result {
  width: 100%; text-align: left; min-height: 44px; padding: 8px 12px; border-radius: 10px;
  border: 1px solid var(--line); background: var(--surface-2); cursor: pointer; font-size: .95rem;
}
.status { margin: 0; color: var(--muted); font-size: .95rem; }
.status:empty { display: none; }
.status.error { color: var(--danger); }
.status.loading::before, .status.locating::before {
  content: ""; display: inline-block; width: 12px; height: 12px; margin-right: 8px; vertical-align: -1px;
  border: 2px solid var(--line); border-top-color: var(--brand); border-radius: 50%; animation: spin .8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }
@media (prefers-reduced-motion: reduce) { .status.loading::before, .status.locating::before { animation: none; } }

/* ---------- Care pills ---------- */
.pills { display: flex; gap: 8px; overflow-x: auto; padding: 2px 0 4px; scrollbar-width: none; }
.pills::-webkit-scrollbar { display: none; }
.pill {
  flex: none; min-height: 44px; padding: 0 14px; border-radius: 999px; cursor: pointer;
  border: 1px solid var(--line); background: var(--surface); font-weight: 600; white-space: nowrap;
}
.pill.on { background: var(--brand); border-color: var(--brand); color: var(--brand-ink); }
.pill.danger { color: var(--danger); border-color: color-mix(in srgb, var(--danger) 45%, var(--line)); }
.pill.danger.on { background: var(--danger); border-color: var(--danger); color: #fff; }

.advice h2 { margin: 0 0 4px; font-size: 1.05rem; color: var(--brand); }
.advice p { margin: 4px 0 0; }

/* ---------- Numbers ---------- */
.numbers h2 { margin: 0 0 6px; font-size: .9rem; color: var(--muted); font-weight: 600; }
.num-list { list-style: none; margin: 0; padding: 0; display: flex; gap: 8px; flex-wrap: wrap; }
.num-list li { flex: 1 1 100px; }
.num-list a {
  display: flex; flex-direction: column; justify-content: center; min-height: 52px; padding: 6px 12px;
  border-radius: 12px; border: 1px solid var(--line); background: var(--surface); text-decoration: none; color: var(--text);
}
.num-list a strong { font-size: 1.1rem; }
.num-list a.danger strong { color: var(--danger); }
.num-label { font-size: .78rem; color: var(--muted); }
.numbers > p { margin: 6px 0 0; }

/* ---------- Map ---------- */
.map-wrap { position: relative; }
.leaflet-touch .leaflet-bar a, .leaflet-bar a { width: 44px; height: 44px; line-height: 44px; font-size: 22px; }
.leaflet-control-attribution { font-size: 12px; }
.leaflet-control-attribution a { display: inline-block; padding: 6px 2px; }
.map {
  height: 46dvh; min-height: 260px; border-radius: var(--radius); overflow: hidden;
  border: 1px solid var(--line); z-index: 0;
}
.area-btn {
  position: absolute; top: 10px; left: 50%; transform: translateX(-50%); z-index: 500;
  min-height: 44px; padding: 0 16px; border-radius: 999px; border: 0; cursor: pointer;
  background: var(--surface); color: var(--text); font-weight: 650; box-shadow: 0 2px 10px rgba(0, 0, 0, .25);
}

/* ---------- List ---------- */
.list-head h2 { margin: 4px 0 0; font-size: 1.05rem; }
.list-head p { margin: 2px 0 0; }
.list { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 10px; }
.place {
  background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 4px 12px 12px;
  box-shadow: var(--shadow);
}
.place.fit { border-color: color-mix(in srgb, var(--brand) 45%, var(--line)); }
.place.selected { outline: 3px solid var(--brand); outline-offset: 0; }
.place-head {
  display: flex; align-items: center; gap: 10px; width: 100%; min-height: 48px;
  background: none; border: 0; padding: 6px 0; text-align: left; cursor: pointer;
}
.dot { flex: none; width: 12px; height: 12px; border-radius: 50%; box-shadow: 0 0 0 2px var(--surface), 0 0 0 3px var(--line); }
.place-title { flex: 1; min-width: 0; display: flex; flex-direction: column; }
.place-name { font-weight: 650; overflow-wrap: anywhere; }
.place-orig { font-size: .85rem; color: var(--muted); overflow-wrap: anywhere; }
.place-dist { flex: none; font-weight: 650; color: var(--brand); font-variant-numeric: tabular-nums; }
.place-meta { display: flex; flex-wrap: wrap; gap: 6px; }
.tag { font-size: .78rem; padding: 2px 8px; border-radius: 999px; background: var(--surface-2); color: var(--muted); }
.tag.danger { background: var(--danger-soft); color: var(--danger); }
.tag.good { background: var(--good-soft); color: var(--good); }
.place-line { margin: 6px 0 0; font-size: .9rem; overflow-wrap: anywhere; }
.place-actions { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 10px; }
.place-actions .btn { flex: 1 1 0; padding: 0 10px; }

.foot { font-size: .85rem; color: var(--muted); padding: 4px 2px; }
.foot p { margin: 4px 0; }

/* ---------- Wide screens: list on the left, map fills the right ---------- */
@media (min-width: 800px) {
  .panel { max-width: none; margin: 0; width: min(440px, 44vw); }
  .map-wrap {
    position: fixed; z-index: 1;
    top: calc(var(--header-h) + var(--safe-t)); bottom: 0;
    left: min(440px, 44vw); right: 0;
    padding: 12px calc(12px + var(--safe-r)) calc(12px + var(--safe-b)) 0;
  }
  .map { height: 100%; min-height: 0; }
  .area-btn { top: 22px; }
}

/* ---------- Phone in landscape: panel left, map right ---------- */
@media (orientation: landscape) and (max-height: 500px) {
  :root { --header-h: 48px; }
  .top { padding-top: calc(4px + var(--safe-t)); padding-bottom: 4px; }
  .tagline { display: none; }
  .brand img { width: 28px; height: 28px; }
  .panel { max-width: none; margin: 0; width: 50vw; gap: 10px; }
  .map-wrap {
    position: fixed; z-index: 1;
    top: calc(var(--header-h) + var(--safe-t)); bottom: 0; left: 50vw; right: 0;
    padding: 8px calc(8px + var(--safe-r)) calc(8px + var(--safe-b)) 0;
  }
  .map { height: 100%; min-height: 0; }
  textarea { min-height: 44px; }
}
