:root {
  --bg: #0b0f17; --panel: #111827; --card: #1f2937; --line: #374151;
  --text: #f3f4f6; --muted: #9ca3af; --accent: #fbbf24;
  --green: #16a34a; --red: #dc2626; --blue: #2563eb;
  --safe-b: env(safe-area-inset-bottom, 0px);
}
html[data-theme="light"] {
  --bg: #f3f4f6; --panel: #ffffff; --card: #eef1f5; --line: #d1d5db;
  --text: #111827; --muted: #6b7280; --accent: #b45309;
}
html[data-theme="light"] .leaflet-container { background: #e5e7eb; }
html[data-theme="light"] .card:active { background: #dde1e6; }
* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
[hidden] { display: none !important; } /* 確保 hidden 屬性蓋過下面的 display 規則 */
html, body { margin: 0; height: 100%; }
body {
  background: var(--bg); color: var(--text);
  font-family: -apple-system, "PingFang HK", "Noto Sans TC", system-ui, sans-serif;
  overflow: hidden;
}

.view { display: none; flex-direction: column; height: 100vh; height: 100dvh; padding-bottom: calc(58px + var(--safe-b)); }
.view.active { display: flex; }

.topbar {
  display: flex; align-items: center; justify-content: space-between;
  padding: max(12px, env(safe-area-inset-top)) 16px 12px; flex: 0 0 auto;
}
.topbar h1 { font-size: 19px; margin: 0; }
.topbar-right { display: flex; align-items: center; gap: 10px; }
.icon-btn { background: none; border: none; font-size: 22px; padding: 4px; line-height: 1; }

/* 日/夜間 switch */
.switch { position: relative; display: inline-flex; align-items: center; cursor: pointer; }
.switch input { display: none; }
.switch-track {
  width: 50px; height: 28px; border-radius: 999px; background: #374151;
  position: relative; transition: background .2s; display: block;
}
.switch-thumb {
  position: absolute; top: 3px; left: 3px; width: 22px; height: 22px; border-radius: 50%;
  background: #1f2937; display: flex; align-items: center; justify-content: center;
  font-size: 12px; transition: transform .2s, background .2s;
}
.switch input:checked + .switch-track { background: #cbd5e1; }
.switch input:checked + .switch-track .switch-thumb { transform: translateX(22px); background: #fff; }

.pill { font-size: 13px; padding: 4px 12px; border-radius: 999px; font-weight: 600; }
.pill.idle { background: var(--line); color: var(--muted); }
.pill.rec { background: var(--red); color: #fff; animation: pulse 1.4s infinite; }
@keyframes pulse { 50% { opacity: .55; } }

/* 記錄頁 */
#map { flex: 1 1 auto; min-height: 180px; margin: 0 12px; border-radius: 16px; overflow: hidden; background: #1a2230; }

.stats { display: flex; gap: 10px; padding: 14px 12px 6px; flex: 0 0 auto; }
.stat { flex: 1; background: var(--panel); border-radius: 14px; padding: 10px 12px; text-align: center; }
.stat-label { font-size: 12px; color: var(--muted); }
.stat-value { font-size: 22px; font-weight: 700; margin-top: 2px; font-variant-numeric: tabular-nums; }
.stat-value small { font-size: 13px; color: var(--muted); font-weight: 500; }
.stat-value.gps { font-size: 16px; }
.gps.good { color: #4ade80; } .gps.ok { color: var(--accent); } .gps.poor { color: #f87171; }

.controls { padding: 8px 16px 4px; flex: 0 0 auto; }
.big-btn { width: 100%; border: none; border-radius: 18px; padding: 20px; font-size: 22px; font-weight: 800; color: #fff; }
.big-btn.start { background: var(--green); }
.big-btn.end { background: var(--red); }
.big-btn:active { transform: scale(.98); }
.hint { color: var(--muted); font-size: 13px; text-align: center; padding: 6px 24px 0; margin: 0; }

/* 清單 */
.list { flex: 1 1 auto; overflow-y: auto; padding: 4px 12px 16px; -webkit-overflow-scrolling: touch; }
.card {
  display: flex; align-items: center; gap: 12px; background: var(--card);
  border-radius: 14px; padding: 14px; margin-bottom: 10px;
}
.card:active { background: #283446; }
.card-main { flex: 1; min-width: 0; }
.card-top { font-size: 15px; font-weight: 600; }
.card-route { font-size: 13px; color: var(--text); margin-top: 2px; opacity: .85; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.card-sub { font-size: 13px; color: var(--muted); margin-top: 3px; }
.card-right { font-size: 17px; font-weight: 700; color: var(--accent); white-space: nowrap; }
.card-del { background: none; border: none; font-size: 18px; padding: 6px 2px 6px 10px; color: var(--muted); flex: 0 0 auto; }
.cost-card { border-left: 3px solid var(--red); }
.card-del:active { color: var(--red); }
.tag { background: var(--red); color: #fff; font-size: 13px; padding: 4px 10px; border-radius: 8px; }
.empty { color: var(--muted); text-align: center; padding: 40px 0; }
.summary-bar { margin: 0 12px 8px; padding: 12px 14px; background: var(--panel); border-radius: 12px; font-size: 14px; color: var(--muted); }
.summary-bar b { color: var(--text); font-size: 16px; }

/* 底部導覽 */
.tabbar {
  position: fixed; bottom: 0; left: 0; right: 0; display: flex;
  background: var(--panel); border-top: 1px solid var(--line);
  padding-bottom: var(--safe-b); z-index: 500;
}
.tab {
  flex: 1; background: none; border: none; color: var(--muted);
  padding: 12px 0 14px; font-size: 14px; font-weight: 600;
}
.tab.active { color: var(--accent); }
.badge { background: var(--red); color: #fff; border-radius: 999px; font-size: 11px; padding: 1px 7px; margin-left: 2px; }

/* 表單彈窗 */
.sheet {
  position: fixed; inset: 0; background: rgba(0,0,0,.55); z-index: 1000;
  display: flex; align-items: flex-end;
}
.sheet-card {
  width: 100%; max-height: 92vh; overflow-y: auto; background: var(--panel);
  border-radius: 22px 22px 0 0; padding: 20px 18px calc(20px + var(--safe-b));
  animation: slideup .2s ease;
}
@keyframes slideup { from { transform: translateY(100%); } }
.sheet-head { margin-bottom: 8px; }
.sheet-head h2 { margin: 0 0 4px; font-size: 20px; }
.form-meta { color: var(--muted); font-size: 13px; }

.field { margin-top: 14px; }
.field > label { display: block; font-size: 13px; color: var(--muted); margin-bottom: 7px; }
.field-row { display: flex; gap: 10px; }
.field-row .field { flex: 1; }
input[type=number], input[type=text], input[type=date] {
  width: 100%; background: var(--card); border: 1px solid var(--line); color: var(--text);
  border-radius: 12px; padding: 13px; font-size: 17px;
}
input:focus { outline: none; border-color: var(--accent); }

.chip-group { display: flex; flex-wrap: wrap; gap: 8px; }
.chip {
  background: var(--card); border: 1px solid var(--line); color: var(--text);
  border-radius: 999px; padding: 9px 14px; font-size: 14px;
}
.chip.sel { background: var(--accent); color: #111; border-color: var(--accent); font-weight: 700; }

.checkrow { display: flex; align-items: center; gap: 10px; margin-top: 16px; font-size: 14px; }
.checkrow input { width: 22px; height: 22px; accent-color: var(--accent); }

/* 起訖點搜尋 */
.loc-field { position: relative; }
.loc-input-row { display: flex; gap: 8px; }
.loc-input-row input { flex: 1; }
.loc-gps-btn { background: var(--card); border: 1px solid var(--line); border-radius: 12px; padding: 0 14px; font-size: 18px; color: var(--text); }
.loc-gps-btn:active { background: var(--line); }
.suggest-list {
  position: absolute; left: 0; right: 0; top: calc(100% + 4px); z-index: 50;
  background: var(--card); border: 1px solid var(--line); border-radius: 12px;
  max-height: 220px; overflow-y: auto; box-shadow: 0 8px 20px rgba(0,0,0,.35);
}
.suggest-item { padding: 11px 14px; font-size: 14px; border-bottom: 1px solid var(--line); }
.suggest-item:last-child { border-bottom: none; }
.suggest-item:active { background: var(--line); }

/* 確認對話框 */
.confirm-overlay { align-items: center; justify-content: center; }
.confirm-box { width: calc(100% - 48px); max-width: 360px; border-radius: 18px; animation: none; padding: 22px 18px; }
.confirm-msg { font-size: 15px; line-height: 1.5; margin: 4px 0 18px; }
.btn.danger { background: var(--red); color: #fff; }

.sheet-actions { display: flex; gap: 10px; margin-top: 22px; }
.btn { flex: 1; border: none; border-radius: 14px; padding: 16px; font-size: 16px; font-weight: 700; }
.btn.primary { background: var(--accent); color: #111; }
.btn.ghost { background: var(--card); color: var(--text); border: 1px solid var(--line); }

/* Toast */
#toast {
  position: fixed; left: 50%; bottom: calc(80px + var(--safe-b)); transform: translateX(-50%) translateY(20px);
  background: #000; color: #fff; padding: 12px 20px; border-radius: 12px; font-size: 14px;
  opacity: 0; pointer-events: none; transition: .25s; z-index: 2000; white-space: nowrap;
}
#toast.show { opacity: .95; transform: translateX(-50%) translateY(0); }

.leaflet-container { background: #1a2230; }

/* 分析頁 */
.seg-group { display: flex; gap: 6px; margin: 0 12px 14px; background: var(--panel); border-radius: 12px; padding: 4px; flex: 0 0 auto; }
.seg { flex: 1; background: none; border: none; color: var(--muted); padding: 9px 0; border-radius: 9px; font-size: 13px; font-weight: 600; }
.seg.active { background: var(--accent); color: #111; }

#analytics-content { flex: 1 1 auto; overflow-y: auto; padding: 0 0 24px; -webkit-overflow-scrolling: touch; }

.stat-grid { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; margin: 0 12px 6px; }
.stat-grid.two { grid-template-columns: repeat(2, 1fr); }
.a-card { background: var(--panel); border-radius: 14px; padding: 12px 8px; text-align: center; }
.a-label { font-size: 11px; color: var(--muted); }
.a-value { font-size: 18px; font-weight: 700; margin-top: 4px; }
.a-value.accent { color: var(--accent); }
.a-value.good { color: #22c55e; }
.a-value.bad { color: var(--red); }
.a-sub { font-size: 12px; color: var(--muted); margin-top: 2px; }

.section-title { margin: 22px 12px 10px; font-size: 15px; font-weight: 700; }

.hour-chart { display: flex; align-items: flex-end; gap: 2px; height: 70px; margin: 0 12px; background: var(--panel); border-radius: 12px; padding: 8px 6px 4px; }
.hour-bar { flex: 1; background: var(--accent); border-radius: 3px 3px 0 0; min-height: 2px; }
.hour-axis { display: flex; justify-content: space-between; margin: 4px 16px 0; font-size: 11px; color: var(--muted); }
.top-hours { margin: 10px 12px 0; display: flex; flex-direction: column; gap: 6px; }
.top-hour-item { background: var(--panel); border-radius: 10px; padding: 9px 12px; font-size: 13px; }

.hint-sm { color: var(--muted); font-size: 12px; margin: 6px 12px 0; }
.empty-note { color: var(--muted); font-size: 13px; margin: 0 12px; padding: 14px; background: var(--panel); border-radius: 12px; }

.bars-list { margin: 0 12px; display: flex; flex-direction: column; gap: 10px; }
.bar-row { display: flex; align-items: center; gap: 8px; }
.bar-label { width: 86px; font-size: 13px; flex-shrink: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.bar-track { flex: 1; height: 18px; background: var(--panel); border-radius: 9px; overflow: hidden; }
.bar-fill { height: 100%; background: var(--accent); border-radius: 9px; }
.bar-value { font-size: 12px; color: var(--muted); white-space: nowrap; flex-shrink: 0; }
.bar-value small { font-size: 11px; }

.rank-list { margin: 0 12px; display: flex; flex-direction: column; gap: 8px; }
.rank-row { display: flex; align-items: center; gap: 10px; background: var(--panel); border-radius: 10px; padding: 10px 12px; }
.rank-num { font-weight: 700; color: var(--accent); width: 18px; flex-shrink: 0; }
.rank-label { flex: 1; font-size: 13px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.rank-meta { font-size: 12px; color: var(--muted); white-space: nowrap; }

.trend-list { margin: 0 12px 4px; display: flex; flex-direction: column; gap: 6px; }
.trend-row { display: flex; align-items: center; gap: 8px; }
.trend-date { width: 38px; font-size: 12px; color: var(--muted); flex-shrink: 0; }
.trend-track { flex: 1; height: 14px; background: var(--panel); border-radius: 7px; overflow: hidden; }
.trend-fill { height: 100%; border-radius: 7px; }
.trend-fill.pos { background: #22c55e; }
.trend-fill.neg { background: var(--red); }
.trend-val { width: 60px; text-align: right; font-size: 12px; font-weight: 600; flex-shrink: 0; }
.trend-val.good { color: #22c55e; }
.trend-val.bad { color: var(--red); }
