:root {
  --primary: #00695c;
  --bg: #f0f3f4;
  --card: #ffffff;
  --text: #263238;
  --muted: #78909c;
  --accent: #ffb300;
  --sheet-peek: 214px;
  --sheet-half: 46vh;
  --sheet-full: 88vh;
}
* { box-sizing: border-box; margin: 0; padding: 0; }
html, body { height: 100%; overscroll-behavior: none; }
body {
  font-family: "Noto Sans TC", "Microsoft JhengHei", system-ui, sans-serif;
  color: var(--text);
  overflow: hidden;
}

/* ---------- 地圖 ---------- */
#map { position: fixed; inset: 0; z-index: 0; background: #e8eaed; }
.leaflet-container { font-family: inherit; background: #e8eaed; }

/* ---------- 頂部列 ---------- */
#topbar {
  position: fixed; top: 0; left: 0; right: 0; z-index: 1100;
  display: flex; justify-content: space-between; align-items: center;
  padding: calc(8px + env(safe-area-inset-top)) 14px 8px;
  pointer-events: none;
}
.brand-mini {
  background: var(--primary); color: #fff;
  font-size: 13px; font-weight: 700;
  padding: 7px 12px; border-radius: 999px;
  box-shadow: 0 2px 8px rgba(0,0,0,.25);
  pointer-events: auto;
}
.status {
  background: rgba(255,255,255,.92); color: var(--muted);
  font-size: 11px; text-align: right; line-height: 1.35;
  padding: 5px 10px; border-radius: 999px;
  box-shadow: 0 2px 8px rgba(0,0,0,.15);
  pointer-events: auto;
}

/* ---------- 底部面板 ---------- */
#sheet {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 1200;
  height: var(--sheet-full);
  background: var(--bg);
  border-radius: 18px 18px 0 0;
  box-shadow: 0 -4px 24px rgba(0,0,0,.25);
  transform: translateY(calc(var(--sheet-full) - var(--sheet-peek)));
  transition: transform .28s cubic-bezier(.3,.8,.3,1);
  display: flex; flex-direction: column;
  will-change: transform;
}
#sheet.dragging { transition: none; }
#sheetGrip {
  flex-shrink: 0;
  padding: 8px 16px 6px;
  cursor: grab;
  touch-action: none;
  background: var(--bg);
  border-radius: 18px 18px 0 0;
}
.grip { width: 40px; height: 5px; border-radius: 3px; background: #cfd8dc; margin: 0 auto 6px; }
.grip-row { display: flex; align-items: center; justify-content: space-between; }
.grip-title { font-size: 15px; font-weight: 800; color: var(--primary); }
#refreshBtn {
  border: 0;
  width: 38px; height: 38px; border-radius: 50%;
  background: linear-gradient(135deg, #26a69a 0%, #00796b 55%, #00695c 100%);
  color: #fff;
  display: flex; align-items: center; justify-content: center;
  box-shadow: 0 3px 10px rgba(0, 105, 92, .45), inset 0 1px 0 rgba(255, 255, 255, .35);
  cursor: pointer;
  flex-shrink: 0;
  transition: transform .15s ease, box-shadow .2s ease, filter .2s ease;
}
#refreshBtn:hover { filter: brightness(1.08); box-shadow: 0 4px 14px rgba(0, 105, 92, .55), inset 0 1px 0 rgba(255,255,255,.35); }
#refreshBtn:active { transform: scale(.88); box-shadow: 0 1px 4px rgba(0, 105, 92, .4); }
#refreshBtn svg { display: block; }
#refreshBtn.spinning svg { animation: spin .9s cubic-bezier(.45, .1, .55, .9) infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

#sheetBody {
  flex: 1; overflow-y: auto; -webkit-overflow-scrolling: touch;
  padding: 0 12px calc(30px + env(safe-area-inset-bottom));
}

/* ---------- 篩選（單行滑動） ---------- */
.filters {
  display: flex; gap: 6px;
  overflow-x: auto; -webkit-overflow-scrolling: touch;
  padding: 4px 0 10px;
  scrollbar-width: none;
}
.filters::-webkit-scrollbar { display: none; }
.seg { display: flex; background: #dfe5e7; border-radius: 10px; padding: 2px; flex-shrink: 0; }
.seg button {
  border: 0; background: transparent;
  padding: 7px 11px; font-size: 13px; font-weight: 700;
  color: var(--muted); border-radius: 8px; cursor: pointer;
  white-space: nowrap;
}
.seg button.active { background: var(--card); color: var(--primary); box-shadow: 0 1px 3px rgba(0,0,0,.15); }
.loc-btn {
  flex-shrink: 0;
  border: 0; background: var(--card); color: var(--primary);
  font-size: 16px; border-radius: 10px; padding: 5px 12px;
  box-shadow: 0 1px 3px rgba(0,0,0,.15); cursor: pointer;
}
.loc-btn:active { transform: scale(.92); }

/* ---------- 路線卡 ---------- */
.card {
  background: var(--card); border-radius: 14px;
  padding: 11px 13px; margin-bottom: 8px;
  box-shadow: 0 1px 3px rgba(0,0,0,.08);
  display: flex; align-items: center; gap: 11px;
  position: relative; overflow: hidden;
  cursor: pointer;
}
.card:active { background: #eef4f3; }
.card.best { outline: 2px solid var(--accent); outline-offset: -2px; padding-right: 12px; }
.best-tag {
  display: inline-block;
  font-size: 11.5px; font-weight: 800; color: #e65100;
  background: #fff3e0;
  padding: 2px 9px; border-radius: 999px;
  margin-bottom: 3px;
}
.card.active-route { outline: 2px solid var(--primary); outline-offset: -2px; }
.badge {
  min-width: 56px; max-width: 70px; text-align: center;
  color: #fff; border-radius: 12px; padding: 10px 5px;
  font-weight: 900; font-size: 17.5px; letter-spacing: .5px;
  flex-shrink: 0;
  background-image: linear-gradient(160deg, rgba(255,255,255,.28), rgba(255,255,255,0) 52%);
  box-shadow: inset 0 -2px 0 rgba(0,0,0,.18), 0 2px 6px rgba(0,0,0,.28);
  text-shadow: 0 1px 2px rgba(0,0,0,.25);
}
.eta-box {
  flex-shrink: 0;
  display: flex; flex-direction: column; align-items: flex-end; gap: 4px;
  min-width: 88px;
  text-align: right;
}
.eta-main { display: flex; justify-content: flex-end; }
.plate { display: inline-flex; align-items: center; gap: 4px; }
.arrive-line {
  display: flex; align-items: baseline; gap: 6px;
  margin: 3px 0 2px;
}
.arrive-word { font-size: 11px; color: var(--muted); font-weight: 700; }
.info i, .detail i { margin-right: 2px; }
.arrive-time { font-size: 19px; font-weight: 800; color: var(--primary); line-height: 1.1; white-space: nowrap; }
.arrive-time.dim { color: #b0bec5; }
.arrive-total { font-size: 11px; color: #e65100; font-weight: 800; white-space: nowrap; }
.info { flex: 1; min-width: 0; }
.trip { font-size: 12px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; margin-bottom: 3px; }
.trip b { color: var(--text); }
.eta-line { display: flex; align-items: center; gap: 6px; flex-wrap: wrap; }
.eta { font-size: 27px; font-weight: 800; color: var(--primary); line-height: 1.1; }
.eta .unit { font-size: 13px; font-weight: 600; color: var(--muted); margin-left: 2px; }
.eta.soon { color: #e65100; }
.eta.arriving { color: #c62828; font-size: 20px; animation: pulse 1.1s ease-in-out infinite; }
@keyframes pulse { 50% { opacity: .45; } }
.eta.alt { color: #b26a00; }
.eta.alt .sched, .eta .sched { font-size: 12px; color: var(--muted); font-weight: 600; }
.eta.none { color: var(--muted); font-size: 14.5px; font-weight: 600; }
.plate {
  font-size: 11.5px; font-weight: 700; color: #37474f;
  background: #eceff1; border-radius: 7px; padding: 3px 8px;
  display: inline-flex; align-items: center; gap: 5px;
}
.next-buses { font-size: 11.5px; color: var(--muted); font-weight: 600; }
.crowd { font-size: 10.5px; font-weight: 700; border-radius: 6px; padding: 1px 5px; }
.crowd.ok { color: #2e7d32; background: #e8f5e9; }
.crowd.bad { color: #c62828; background: #ffebee; }
.detail { display: flex; flex-wrap: wrap; gap: 4px; margin-top: 5px; }
.chip-lite {
  font-size: 11px; color: #455a64;
  background: #f1f4f5; border-radius: 6px; padding: 2px 7px;
}
.chip-total {
  font-size: 11px; color: #e65100; font-weight: 800;
  background: #fff3e0; border-radius: 6px; padding: 2px 7px;
}
.total { font-size: 12px; color: #e65100; font-weight: 700; }
.busid { font-size: 10.5px; color: var(--muted); }
.card.suspended { opacity: .55; }

/* ---------- 站點列 ---------- */
.sec-title { font-size: 14px; color: var(--primary); margin: 14px 2px 8px; }
#stops { display: flex; gap: 8px; overflow-x: auto; padding-bottom: 6px; -webkit-overflow-scrolling: touch; }
.stop-chip {
  flex-shrink: 0;
  background: var(--card); border-radius: 12px;
  padding: 9px 12px; min-width: 130px; max-width: 200px;
  box-shadow: 0 1px 3px rgba(0,0,0,.08);
  cursor: pointer;
}
.stop-chip:active { background: #eef4f3; }
.stop-chip .no {
  display: inline-flex; width: 20px; height: 20px; border-radius: 50%;
  align-items: center; justify-content: center;
  color: #fff; font-weight: 800; font-size: 12px; margin-right: 5px;
}
.stop-chip .nm { font-size: 13px; font-weight: 700; }
.stop-chip .w { font-size: 10.5px; color: var(--muted); margin-top: 2px; }
.stop-chip .r { font-size: 11px; color: var(--muted); margin-top: 3px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

.footnote { font-size: 11.5px; color: var(--muted); line-height: 1.6; padding: 12px 4px 8px; }
.footnote a { color: var(--primary); }

/* ---------- 地圖標記 ---------- */
.stop-marker, .bus-marker, .route-marker { background: transparent; border: 0; }
.stop-pin {
  width: 28px; height: 28px; border-radius: 50%;
  color: #fff; font-weight: 800; font-size: 14px;
  display: flex; align-items: center; justify-content: center;
  border: 2px solid #fff; box-shadow: 0 1px 5px rgba(0,0,0,.4);
  cursor: pointer;
}
.anchor-pin {
  width: 32px; height: 32px; border-radius: 50%;
  display: flex; align-items: center; justify-content: center;
  font-size: 13px; background: #fff;
  border: 2px solid var(--primary); box-shadow: 0 1px 5px rgba(0,0,0,.4);
  color: var(--primary);
}
.anchor-pin.station { color: #c62828; }
.anchor-pin.station { border-color: #c62828; }
.bus-pin {
  position: relative;
  color: #fff; font-weight: 800; font-size: 12px;
  border-radius: 999px; padding: 3px 10px;
  border: 2px solid #fff; box-shadow: 0 1px 5px rgba(0,0,0,.45);
  white-space: nowrap;
}
.bus-head {
  position: absolute;
  left: 50%; top: -26px;
  width: 23px; height: 23px;
  margin-left: -11.5px;
  transform-origin: 50% 37px; /* 車牌中心為軸心旋轉 */
  filter: drop-shadow(0 1.5px 2px rgba(0,0,0,.45));
  transition: transform .4s;
}
.bus-head svg { display: block; }
.route-tip {
  color: #fff; font-size: 11px; font-weight: 800;
  border-radius: 999px; padding: 2px 8px;
  border: 2px solid #fff; box-shadow: 0 1px 4px rgba(0,0,0,.4);
  white-space: nowrap;
}
.leaflet-popup-content { font-size: 12.5px; line-height: 1.5; }
.leaflet-popup-content b { color: var(--primary); }
.pop-chips { display: flex; flex-wrap: wrap; gap: 4px; margin-top: 4px; }
.pop-chip { color: #fff; font-size: 11px; font-weight: 700; border-radius: 6px; padding: 2px 7px; }

.offline {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 1300;
  background: #bf360c; color: #fff;
  font-size: 12.5px; padding: 8px 14px calc(8px + env(safe-area-inset-bottom));
  text-align: center;
}

/* 隔離模式提示 chip */
.iso-chip {
  position: fixed;
  left: 50%; transform: translateX(-50%);
  bottom: calc(var(--sheet-peek) + 20px + env(safe-area-inset-bottom));
  z-index: 1250;
  border: 0;
  background: #263238; color: #fff;
  font-size: 13px; font-weight: 700;
  padding: 10px 18px; border-radius: 999px;
  box-shadow: 0 3px 12px rgba(0,0,0,.35);
  cursor: pointer;
}
.iso-chip:active { background: #37474f; }

/* 站點資訊面板 */
.stop-panel {
  position: fixed;
  left: 10px; right: 10px;
  bottom: calc(var(--sheet-peek) + 16px + env(safe-area-inset-bottom));
  z-index: 1255;
  background: var(--card);
  border-radius: 16px;
  box-shadow: 0 6px 24px rgba(0,0,0,.28);
  max-height: 46vh;
  display: flex; flex-direction: column;
  overflow: hidden;
  animation: spIn .22s cubic-bezier(.3,.8,.3,1);
}
@keyframes spIn { from { opacity: 0; transform: translateY(14px); } }
.sp-head {
  display: flex; align-items: center; justify-content: space-between;
  padding: 12px 14px 8px;
  font-size: 15px; font-weight: 800; color: var(--primary);
}
.sp-walk { font-size: 11.5px; color: var(--muted); font-weight: 600; }
#stopPanelClose {
  border: 0; background: #eceff1; color: #455a64;
  width: 28px; height: 28px; border-radius: 50%;
  font-size: 13px; cursor: pointer; flex-shrink: 0;
}
#stopPanelBody { overflow-y: auto; padding: 0 10px 10px; }
.sp-row {
  display: flex; align-items: center; gap: 8px;
  padding: 9px 8px; border-radius: 10px; cursor: pointer;
}
.sp-row:active { background: #eef4f3; }
.sp-row .badge { min-width: 46px; padding: 6px 4px; font-size: 14px; border-radius: 8px; }
.sp-role { font-size: 11.5px; color: var(--muted); font-weight: 700; flex-shrink: 0; }
.sp-time { font-size: 13px; font-weight: 700; color: var(--text); margin-left: auto; text-align: right; }
.sp-next { display: block; font-size: 11px; color: var(--muted); font-weight: 600; }
.sp-empty { font-size: 13px; color: var(--muted); padding: 10px; }

/* Toast */
.toast {
  position: fixed;
  left: 50%; transform: translateX(-50%) translateY(-16px);
  top: calc(58px + env(safe-area-inset-top));
  z-index: 1400;
  max-width: 86vw;
  background: rgba(38, 50, 56, .95); color: #fff;
  font-size: 13px; line-height: 1.5;
  padding: 10px 16px; border-radius: 12px;
  box-shadow: 0 4px 16px rgba(0,0,0,.35);
  opacity: 0; pointer-events: none;
  transition: opacity .25s, transform .25s;
}
.toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }
.hidden { display: none !important; }
