:root {
  --normal: #2f7ed8;
  --left-one: #f5a524;
  --left-two: #e8590c;
  --complex: #9c36b5;
  --no-plan: #868e96;
  --anomaly: #e03131;

  --surface: #ffffff;
  --text: #1a1d21;
  --muted: #5c636a;
  --border: #dee2e6;
  --shadow: 0 2px 12px rgba(0, 0, 0, .18);
  --tap: 44px;
}

@media (prefers-color-scheme: dark) {
  :root {
    --surface: #1e2126;
    --text: #e9ecef;
    --muted: #adb5bd;
    --border: #343a40;
    --shadow: 0 2px 12px rgba(0, 0, 0, .5);
  }
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  overflow: hidden;                 /* 頁面本身不捲動，只有地圖與面板內部可捲 */
  font-family: system-ui, -apple-system, "Noto Sans TC", sans-serif;
  color: var(--text);
  background: var(--surface);
}

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

/* ---------- 頂欄 ---------- */
#topbar {
  position: absolute;
  top: 0; left: 0; right: 0;
  display: flex;
  align-items: center;
  gap: .75rem;
  padding: .6rem .9rem;
  padding-top: max(.6rem, env(safe-area-inset-top));
  background: color-mix(in srgb, var(--surface) 92%, transparent);
  backdrop-filter: blur(8px);
  border-bottom: 1px solid var(--border);
  z-index: 5;
}
#topbar h1 { margin: 0; font-size: 1rem; font-weight: 700; }
#count-badge { margin-left: auto; font-size: .8rem; color: var(--muted); }

/* ---------- 圖例 ---------- */
#legend-toggle {
  position: absolute;
  left: .75rem;
  bottom: max(.75rem, env(safe-area-inset-bottom));
  min-width: var(--tap);
  min-height: var(--tap);
  padding: 0 1rem;
  border: 1px solid var(--border);
  border-radius: 999px;
  background: var(--surface);
  color: var(--text);
  font-size: .9rem;
  box-shadow: var(--shadow);
  cursor: pointer;
  z-index: 6;
}

#legend {
  position: absolute;
  left: .75rem;
  bottom: calc(max(.75rem, env(safe-area-inset-bottom)) + var(--tap) + .5rem);
  width: min(19rem, calc(100vw - 1.5rem));
  max-height: 60vh;
  overflow-y: auto;
  padding: .9rem;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: .75rem;
  box-shadow: var(--shadow);
  z-index: 6;
}
#legend[hidden] { display: none; }

.legend-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin: .25rem 0 .5rem;
  font-size: .9rem;
}
.legend-head:not(:first-child) {
  margin-top: 1rem;
  padding-top: .75rem;
  border-top: 1px solid var(--border);
}
#legend-close {
  min-width: var(--tap); min-height: var(--tap);
  margin: calc(var(--tap) / -2 + .5rem) -.5rem;
  border: 0; background: none; color: var(--muted);
  font-size: 1rem; cursor: pointer;
}

#legend-items { list-style: none; margin: 0; padding: 0; }
#legend-items li {
  display: flex; align-items: center; gap: .6rem;
  min-height: 2rem; font-size: .85rem;
}
#legend-items .dot {
  width: .85rem; height: .85rem; border-radius: 50%;
  flex: 0 0 auto; border: 1px solid rgba(0, 0, 0, .25);
}
#legend-items .n { margin-left: auto; color: var(--muted); font-variant-numeric: tabular-nums; }

.switch {
  display: flex; align-items: center; gap: .6rem;
  min-height: var(--tap); font-size: .85rem; cursor: pointer;
}
.switch input { width: 1.15rem; height: 1.15rem; }

.hint { margin: .35rem 0 0; font-size: .75rem; color: var(--muted); line-height: 1.5; }
.hint.caveat {
  margin-top: 1rem; padding-top: .75rem;
  border-top: 1px solid var(--border);
}

/* ---------- 明細面板 ---------- */
#detail {
  position: absolute;
  background: var(--surface);
  border: 1px solid var(--border);
  box-shadow: var(--shadow);
  z-index: 7;
  overflow-y: auto;
  overscroll-behavior: contain;
}
#detail[hidden] { display: none; }

/* 桌面：右側面板 */
@media (min-width: 768px) {
  #detail {
    top: 3.2rem; right: .75rem; bottom: .75rem;
    width: 24rem;
    border-radius: .75rem;
    padding: 1rem 1.1rem;
  }
  #detail-handle { display: none; }
}

/* 行動裝置：底部抽屜 */
@media (max-width: 767px) {
  #detail {
    left: 0; right: 0; bottom: 0;
    max-height: 72vh;
    border-radius: 1rem 1rem 0 0;
    border-bottom: 0;
    padding: .4rem 1rem calc(1rem + env(safe-area-inset-bottom));
  }
  #detail-handle {
    width: 2.5rem; height: .25rem; margin: .35rem auto .6rem;
    border-radius: 999px; background: var(--border);
  }
}

#detail-close {
  position: absolute; top: .5rem; right: .5rem;
  min-width: var(--tap); min-height: var(--tap);
  border: 0; background: none; color: var(--muted);
  font-size: 1.1rem; cursor: pointer;
}

#detail h2 { margin: .2rem 2.5rem .15rem 0; font-size: 1.05rem; }
#detail .sub { margin: 0 0 .75rem; font-size: .8rem; color: var(--muted); }

.tag {
  display: inline-block; padding: .15rem .5rem;
  border-radius: 999px; font-size: .75rem; font-weight: 600;
  color: #fff;
}
/* 分類標籤文字本身已含「（推定）」，此處不再重複附加。 */

.kv { display: grid; grid-template-columns: auto 1fr; gap: .3rem .9rem; font-size: .85rem; margin: .75rem 0; }
.kv dt { color: var(--muted); }
.kv dd { margin: 0; font-variant-numeric: tabular-nums; }

.table-wrap { overflow-x: auto; margin: .5rem 0; }
table { border-collapse: collapse; width: 100%; font-size: .82rem; }
th, td { padding: .35rem .5rem; text-align: right; white-space: nowrap; }
th:first-child, td:first-child { text-align: left; }
thead th { border-bottom: 1px solid var(--border); color: var(--muted); font-weight: 600; }
tbody tr + tr td { border-top: 1px solid color-mix(in srgb, var(--border) 55%, transparent); }
tbody tr.placeholder td { color: var(--muted); font-style: italic; }
td.g { color: #1a7f37; } td.y { color: #b8860b; } td.r { color: #c92a2a; }

.flags { margin: .5rem 0 0; padding: 0; list-style: none; }
.flags li {
  font-size: .78rem; color: #9a3412;
  background: color-mix(in srgb, #f59e0b 15%, transparent);
  border-radius: .35rem; padding: .3rem .5rem; margin-bottom: .25rem;
}

.note { font-size: .75rem; color: var(--muted); line-height: 1.6; margin: .6rem 0 0; }

#detail .loading { padding: 1.5rem 0; text-align: center; color: var(--muted); font-size: .85rem; }
