/* 城市交通經理 —— 乾淨、卡片式、手機優先 */

:root {
  --bg: #0f1720;
  --panel: #ffffff;
  --panel-alt: #f5f7fa;
  --ink: #16202b;
  --ink-soft: #5b6b7c;
  --line: #e2e8f0;
  --accent: #2f7fd1;
  --accent-ink: #ffffff;
  --good: #2f9e5f;
  --warn: #e0a02a;
  --bad: #d1493f;
  --rail: #c8102e;
  --radius: 12px;
  --shadow: 0 6px 24px rgba(15, 23, 32, 0.18);
  --safe-t: env(safe-area-inset-top, 0px);
  --safe-b: env(safe-area-inset-bottom, 0px);
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }

html, body {
  margin: 0;
  padding: 0;
  height: 100%;
  overflow: hidden;
  font-family: -apple-system, BlinkMacSystemFont, "Noto Sans HK", "PingFang HK",
    "Microsoft JhengHei", "Helvetica Neue", sans-serif;
  color: var(--ink);
  background: var(--bg);
  -webkit-text-size-adjust: 100%;
}

#app { position: fixed; inset: 0; }

#map {
  position: absolute;
  inset: 0;
  touch-action: none;
}
#map canvas { display: block; width: 100%; height: 100%; }

/* ─────────── 頂部工具列 ─────────── */

#topbar {
  position: absolute;
  top: 0; left: 0; right: 0;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  padding: calc(var(--safe-t) + 10px) 12px 10px;
  background: linear-gradient(180deg, rgba(15,23,32,0.88), rgba(15,23,32,0.35) 75%, transparent);
  pointer-events: none;
  z-index: 20;
}
#topbar > * { pointer-events: auto; }

.brand { display: flex; align-items: center; gap: 9px; }
.logo {
  width: 34px; height: 34px;
  display: grid; place-items: center;
  background: var(--accent);
  border-radius: 9px;
  font-size: 19px;
}
.brand-text { display: flex; flex-direction: column; line-height: 1.2; }
.brand-text strong { color: #fff; font-size: 15px; letter-spacing: 0.02em; }
.badge {
  color: rgba(255,255,255,0.72);
  font-size: 11px;
}
.top-actions { display: flex; gap: 8px; }

/* ─────────── 按鈕 ─────────── */

.btn {
  appearance: none;
  border: 1px solid transparent;
  border-radius: 10px;
  padding: 9px 14px;
  font-size: 14px;
  font-weight: 600;
  font-family: inherit;
  cursor: pointer;
  transition: filter 0.12s ease, transform 0.08s ease;
  min-height: 40px;
}
.btn:active { transform: scale(0.97); }
.btn-primary { background: var(--accent); color: var(--accent-ink); }
.btn-primary:hover { filter: brightness(1.07); }
.btn-ghost {
  background: rgba(255,255,255,0.92);
  color: var(--ink);
  border-color: var(--line);
}
.btn-ghost:hover { background: #fff; }
.btn-danger { background: #fdecea; color: var(--bad); border-color: #f5c6c2; }
.btn-block { display: block; width: 100%; margin-top: 10px; }
.btn-row { display: flex; flex-wrap: wrap; gap: 8px; }
.btn-row .btn { flex: 1 1 auto; }

.btn:disabled { opacity: 0.45; cursor: not-allowed; }

/* ─────────── 側面板 ─────────── */

.panel {
  position: absolute;
  top: 62px;
  right: 12px;
  bottom: calc(var(--safe-b) + 12px);
  width: 310px;
  max-width: calc(100vw - 24px);
  z-index: 15;
  transition: transform 0.22s ease, opacity 0.22s ease;
}
.panel.hidden { transform: translateX(calc(100% + 20px)); opacity: 0; pointer-events: none; }

.panel-scroll {
  height: 100%;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  display: flex;
  flex-direction: column;
  gap: 10px;
  padding-right: 2px;
}

.card {
  background: var(--panel);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 13px 14px;
}
.card h2 {
  margin: 0 0 10px;
  font-size: 13px;
  font-weight: 700;
  letter-spacing: 0.04em;
  color: var(--ink-soft);
  text-transform: uppercase;
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 8px;
}

/* ─────────── KPI ─────────── */

.kpi-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
}
.kpi {
  background: var(--panel-alt);
  border-radius: 9px;
  padding: 9px 10px;
  display: flex;
  flex-direction: column;
  gap: 3px;
}
.kpi-label { font-size: 11px; color: var(--ink-soft); }
.kpi-value { font-size: 19px; font-weight: 700; font-variant-numeric: tabular-nums; }

.clock-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  margin-top: 10px;
}
.clock {
  font-size: 15px;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
  color: var(--ink);
}
.speed-controls { display: flex; gap: 4px; }
.chip {
  border: 1px solid var(--line);
  background: var(--panel-alt);
  border-radius: 7px;
  padding: 5px 9px;
  font-size: 12px;
  font-weight: 600;
  cursor: pointer;
  font-family: inherit;
  min-height: 32px;
}
.chip.active { background: var(--accent); color: #fff; border-color: var(--accent); }

/* ─────────── 路線列表 ─────────── */

.route-list { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 7px; }
.route-item {
  border: 1px solid var(--line);
  border-radius: 9px;
  padding: 9px 10px;
  display: flex;
  flex-direction: column;
  gap: 6px;
}
.route-head { display: flex; align-items: center; gap: 8px; }
.route-swatch { width: 15px; height: 15px; border-radius: 4px; flex: 0 0 auto; }
.route-name { font-weight: 700; font-size: 14px; flex: 1; }
/* 路線類型標記：巴士（地面層）/ 地鐵（地下層）/ 原有 */
.route-badge {
  font-size: 10px;
  font-weight: 700;
  padding: 2px 6px;
  border-radius: 999px;
  background: var(--panel-alt);
  color: var(--ink-soft);
  border: 1px solid var(--line);
  white-space: nowrap;
}
.route-badge.bus { background: #e8f0fb; color: #2f6bb5; border-color: #cfe0f7; }
.route-badge.rail { background: #fde8ec; color: #a8203a; border-color: #f7ccd5; }
.route-meta { font-size: 11.5px; color: var(--ink-soft); display: flex; flex-wrap: wrap; gap: 8px; }
.route-actions { display: flex; gap: 6px; }
.route-actions .btn { flex: 1; padding: 6px 8px; min-height: 34px; font-size: 12.5px; }

/* ─────────── 小列表 ─────────── */

.mini-list { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 5px; font-size: 12.5px; }
.mini-list li { display: flex; justify-content: space-between; gap: 8px; }
.feeder-list { display: flex; flex-direction: column; gap: 7px; }
.feeder-row {
  display: flex; align-items: center; gap: 7px;
  font-size: 12.5px;
}
.feeder-row select { flex: 1; font-size: 12px; padding: 4px 6px; border-radius: 6px; border: 1px solid var(--line); font-family: inherit; min-height: 32px; }

.muted { color: var(--ink-soft); }
.small { font-size: 12px; }

/* ─────────── 工具列（揀選 / 畫巴士線 / 畫地鐵線 / 落站 / 刪除） ─────────── */

.toolbar {
  position: absolute;
  left: 50%;
  transform: translateX(-50%);
  bottom: calc(var(--safe-b) + 12px);
  z-index: 19;
  display: flex;
  gap: 6px;
  padding: 6px;
  background: var(--panel);
  border-radius: 14px;
  box-shadow: var(--shadow);
  max-width: calc(100vw - 20px);
  overflow-x: auto;
  scrollbar-width: none;
}
.toolbar::-webkit-scrollbar { display: none; }

.tool {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 1px;
  min-width: 60px;
  min-height: 54px;
  padding: 6px 9px;
  border: 1px solid transparent;
  border-radius: 10px;
  background: transparent;
  color: var(--ink);
  font-family: inherit;
  font-size: 11px;
  font-weight: 600;
  cursor: pointer;
  touch-action: manipulation;
  transition: background 0.12s, transform 0.08s;
}
.tool:active { transform: scale(0.95); }
.tool:hover { background: var(--panel-alt); }
.tool.active {
  background: var(--accent);
  color: var(--accent-ink);
  border-color: var(--accent);
}
.tool-danger.active { background: var(--bad); border-color: var(--bad); }
.tool-ico { font-size: 19px; line-height: 1.1; }
.tool-label { white-space: nowrap; }

/* 準則切換 */
.criteria-row {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-top: 10px;
  flex-wrap: wrap;
}

/* 需求 */
.demand-add {
  display: flex;
  align-items: center;
  gap: 6px;
  margin-top: 8px;
  flex-wrap: wrap;
}
.mini-select {
  flex: 1 1 90px;
  min-width: 0;
  font-size: 12px;
  padding: 5px 6px;
  border-radius: 6px;
  border: 1px solid var(--line);
  font-family: inherit;
  min-height: 34px;
}
.demand-row {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 12px;
}
.demand-row input {
  width: 62px;
  font-size: 12px;
  padding: 4px 6px;
  border-radius: 6px;
  border: 1px solid var(--line);
  font-family: inherit;
  min-height: 32px;
}

/* ─────────── 畫路線工具列 ─────────── */

.draw-bar {
  position: absolute;
  left: 12px;
  right: 12px;
  bottom: calc(var(--safe-b) + 12px);
  z-index: 18;
  background: var(--panel);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 11px 13px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  flex-wrap: wrap;
}
.draw-bar.hidden { display: none; }
.draw-info { display: flex; flex-direction: column; gap: 2px; min-width: 140px; }
.draw-info strong { font-size: 14px; }
.draw-actions { display: flex; gap: 7px; flex-wrap: wrap; }
.draw-actions .btn { min-height: 38px; }

/* ─────────── 對話框 ─────────── */

.modal {
  position: absolute;
  inset: 0;
  z-index: 40;
  background: rgba(15, 23, 32, 0.55);
  display: grid;
  place-items: center;
  padding: 16px;
  backdrop-filter: blur(2px);
}
.modal.hidden { display: none; }
.modal-box {
  background: var(--panel);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 16px;
  width: 100%;
  max-width: 380px;
  max-height: 86vh;
  overflow-y: auto;
}
.modal-box.wide { max-width: 520px; }
.modal-box h2 { margin: 0 0 12px; font-size: 16px; }
.modal-body { display: flex; flex-direction: column; gap: 11px; }
.modal-actions { display: flex; gap: 8px; justify-content: flex-end; margin-top: 14px; }

.field { display: flex; flex-direction: column; gap: 5px; }
.field label { font-size: 12px; color: var(--ink-soft); font-weight: 600; }
.field input, .field select {
  font-family: inherit;
  font-size: 14px;
  padding: 9px 10px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fff;
  color: var(--ink);
  min-height: 42px;
}
.field .hint { font-size: 11.5px; color: var(--ink-soft); }

.stat-table { width: 100%; border-collapse: collapse; font-size: 13px; }
.stat-table td { padding: 6px 0; border-bottom: 1px solid var(--line); }
.stat-table td:last-child { text-align: right; font-variant-numeric: tabular-nums; font-weight: 600; }
.stat-table tr.total td { border-bottom: none; border-top: 2px solid var(--ink); padding-top: 9px; font-size: 15px; }
.stat-table tr.total td:last-child { font-weight: 800; }
.pos { color: var(--good); }
.neg { color: var(--bad); }

/* ─────────── 載入遮罩 ─────────── */

.loading {
  position: absolute; inset: 0;
  z-index: 60;
  background: var(--bg);
  display: grid; place-items: center;
  transition: opacity 0.3s ease;
}
.loading.hidden { opacity: 0; pointer-events: none; }
.loading-box { text-align: center; color: #cbd5e1; }
.loading-box p { margin-top: 14px; font-size: 13px; }

/* 資料來源標示（ODbL 1.0 要求） */
.attribution {
  margin: 4px 2px 8px;
  font-size: 11px;
  line-height: 1.5;
  color: var(--ink-soft);
  opacity: 0.85;
}
.attribution a { color: var(--ink-soft); text-decoration: underline; }
.attribution a:hover { color: var(--ink); }
.spinner {
  width: 34px; height: 34px; margin: 0 auto;
  border: 3px solid rgba(255,255,255,0.18);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 0.85s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* ─────────── 手機版 ─────────── */

@media (max-width: 760px) {
  .panel {
    top: auto;
    left: 0;
    right: 0;
    /* 工具列喺最底（永遠可撳），面板喺工具列上面，唔會互相遮住 */
    bottom: calc(var(--safe-b) + 72px);
    width: 100%;
    max-width: 100%;
    max-height: 58vh;
    padding: 0 10px 10px;
  }
  .panel.hidden { transform: translateY(calc(100% + 20px)); }
  .panel-scroll { padding-bottom: 6px; }
  .kpi-value { font-size: 17px; }
  .draw-bar { flex-direction: column; align-items: stretch; }
  .draw-actions .btn { flex: 1; }
  .brand-text strong { font-size: 14px; }
  /* 工具列喺手機版要細啲，但仍然係大目標（觸控友善） */
  .toolbar { bottom: calc(var(--safe-b) + 8px); gap: 4px; padding: 5px; }
  .tool { min-width: 54px; min-height: 50px; padding: 5px 7px; font-size: 10px; }
  .tool-ico { font-size: 17px; }
  /* 畫路線時工具列讓位畀 draw-bar */
  .toolbar.drawing { bottom: calc(var(--safe-b) + 148px); }
  /* 畫路線工具列喺手機版靠上，唔好遮住工具列 */
  .draw-bar { bottom: calc(var(--safe-b) + 74px); }
}

@media (min-width: 761px) {
  .panel { display: block !important; }
  /* 桌面版：面板喺右邊，所以工具列要喺「面板以外」嘅範圍居中 */
  .toolbar {
    left: calc(50% - 161px);
    transform: translateX(-50%);
  }
}

/* 減少動態偏好 */
@media (prefers-reduced-motion: reduce) {
  * { animation-duration: 0.01ms !important; transition-duration: 0.01ms !important; }
}
