/* ==========================================================================
 * 大埔仔 巴士預計到站 — 樣式
 * 手機優先 → 桌面雙欄（≥1080px）
 * 淺色為預設；深色由 <html data-theme="dark"> 觸發
 * ========================================================================== */

/* ---------- 版面寬度 ---------- */
:root {
  --radius-lg: 18px;
  --radius-md: 14px;
  --radius-sm: 10px;

  --font: -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang HK",
          "PingFang TC", "Microsoft JhengHei", "Noto Sans TC", "Helvetica Neue",
          Arial, sans-serif;

  --page-pad: 18px;
  --page-max: 620px;
  --content-max: calc(var(--page-max) - 2 * var(--page-pad));
}

@media (min-width: 700px) {
  :root { --page-pad: 26px; --page-max: 740px; }
}

/* ---------- 淺色（預設）---------- */
:root,
:root[data-theme="light"] {
  color-scheme: light;

  --bg: #f1f3f6;
  --bg-glow: radial-gradient(120% 60% at 50% 0%, rgba(214, 30, 40, .09), transparent 70%);
  --surface: #ffffff;
  --surface-2: #f5f7fa;
  --line: rgba(16, 20, 26, .09);
  --line-strong: rgba(16, 20, 26, .16);

  --text: #0f141a;
  --text-dim: #3b4654;
  --muted: #6b7787;

  --accent: #d32029;
  --accent-soft: rgba(211, 32, 41, .10);
  --accent-ink: #ffffff;

  --soon: #b06f00;
  --now: #0f7c43;
  --danger: #c6202a;

  --shadow: 0 1px 2px rgba(16, 20, 26, .05), 0 6px 20px rgba(16, 20, 26, .06);
  --shadow-lift: 0 2px 4px rgba(16, 20, 26, .06), 0 12px 28px rgba(16, 20, 26, .09);

  --header-bg: rgba(241, 243, 246, .82);
}

/* ---------- 深色 ---------- */
:root[data-theme="dark"] {
  color-scheme: dark;

  --bg: #0a0d11;
  --bg-glow: radial-gradient(120% 60% at 50% 0%, rgba(214, 30, 40, .16), transparent 70%);
  --surface: #141a21;
  --surface-2: #1b232c;
  --line: rgba(255, 255, 255, .075);
  --line-strong: rgba(255, 255, 255, .14);

  --text: #eef2f6;
  --text-dim: #b6c0cb;
  --muted: #7f8c9a;

  --accent: #ff4d57;
  --accent-soft: rgba(255, 77, 87, .14);
  --accent-ink: #ffffff;

  --soon: #ffb020;
  --now: #35d07f;
  --danger: #ff6b6b;

  --shadow: 0 1px 2px rgba(0, 0, 0, .3), 0 8px 24px rgba(0, 0, 0, .3);
  --shadow-lift: 0 2px 4px rgba(0, 0, 0, .35), 0 14px 32px rgba(0, 0, 0, .4);

  --header-bg: rgba(10, 13, 17, .82);
}

* { box-sizing: border-box; }

html {
  -webkit-text-size-adjust: 100%;
  scrollbar-gutter: stable; /* 桌面：避免內容因滾動條出現而左右跳動 */
}

body {
  margin: 0;
  min-height: 100dvh;
  display: flex;
  flex-direction: column;
  font-family: var(--font);
  font-size: 17px;
  line-height: 1.45;
  color: var(--text);
  background: var(--bg-glow), var(--bg);
  background-attachment: fixed;
  -webkit-font-smoothing: antialiased;
  text-rendering: optimizeLegibility;
  padding-bottom: env(safe-area-inset-bottom);
  transition: background-color .18s ease, color .18s ease;
}

/* ==========================================================================
 * 頁首
 * ========================================================================== */

.app-header {
  position: sticky;
  top: 0;
  z-index: 30;
  padding: calc(env(safe-area-inset-top) + 14px) var(--page-pad) 12px;
  background: var(--header-bg);
  backdrop-filter: saturate(140%) blur(14px);
  -webkit-backdrop-filter: saturate(140%) blur(14px);
  border-bottom: 1px solid var(--line);
}

.header-inner {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  max-width: var(--content-max);
  margin: 0 auto;
}

.header-title {
  display: flex;
  align-items: baseline;
  flex-wrap: wrap;
  gap: 8px;
  min-width: 0;
}

.header-title h1 {
  margin: 0;
  font-size: 1.5rem;
  font-weight: 800;
  letter-spacing: -.02em;
  white-space: nowrap;
}

.stop-code {
  font-size: .8rem;
  font-weight: 700;
  letter-spacing: .06em;
  color: var(--accent);
  background: var(--accent-soft);
  border: 1px solid color-mix(in srgb, var(--accent) 30%, transparent);
  padding: 2px 8px;
  border-radius: 999px;
  white-space: nowrap;
}

.header-meta {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-shrink: 0;
}

.updated {
  font-size: .82rem;
  color: var(--muted);
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
  margin-right: 2px;
}

.updated.is-stale { color: var(--danger); }

/* ---------- 圓形圖示按鈕（刷新 / 主題）---------- */

.icon-btn {
  display: grid;
  place-items: center;
  width: 40px;
  height: 40px;
  padding: 0;
  border: 1px solid var(--line-strong);
  border-radius: 50%;
  background: var(--surface);
  color: var(--text-dim);
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
  transition: transform .15s ease, color .15s ease, border-color .15s ease,
              background-color .15s ease;
}

.icon-btn:hover { color: var(--text); border-color: var(--accent); }
.icon-btn:active { transform: scale(.93); }
.icon-btn:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
.icon-btn svg { width: 20px; height: 20px; }

.icon-btn svg [fill="currentColor"] { transition: none; }

#refresh.is-busy { color: var(--accent); }
#refresh.is-busy svg { animation: spin .9s linear infinite; }

@keyframes spin { to { transform: rotate(360deg); } }

/* 主題圖示：淺色顯示月亮（可切到深色），深色顯示太陽 */
.theme-icon-sun { display: none; }
:root[data-theme="dark"] .theme-icon-sun { display: block; }
:root[data-theme="dark"] .theme-icon-moon { display: none; }

/* 距離下次輪詢的進度條 */
.poll-progress {
  max-width: var(--content-max);
  margin: 10px auto 0;
  height: 2px;
  border-radius: 2px;
  background: var(--line);
  overflow: hidden;
}

.poll-progress > div {
  height: 100%;
  width: 100%;
  transform: scaleX(0);
  transform-origin: left center;
  background: linear-gradient(90deg, var(--accent), var(--soon));
  opacity: .75;
}

/* ==========================================================================
 * 提示條
 * ========================================================================== */

.notice {
  max-width: var(--content-max);
  width: 100%; /* 同上：flex item 需要明確寬度 */
  margin: 14px auto 0;
  padding: 10px 14px;
  border-radius: var(--radius-sm);
  font-size: .88rem;
  font-weight: 600;
  line-height: 1.4;
}

.notice.is-error {
  color: var(--danger);
  background: color-mix(in srgb, var(--danger) 10%, transparent);
  border: 1px solid color-mix(in srgb, var(--danger) 32%, transparent);
}

/* ==========================================================================
 * 主體
 * ========================================================================== */

#app {
  max-width: var(--page-max);
  /* body 是 flex column：auto margin 會讓 flex item 變成 fit-content，
     所以必須明確給 width:100%，否則卡片會被縮到內容寬度 */
  width: 100%;
  margin: 0 auto;
  padding: 16px var(--page-pad) 8px;
  flex: 1 0 auto; /* 內容比螢幕短時，把頁腳推到底部 */
}

.stop-section + .stop-section { margin-top: 24px; }

.stop-heading {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  padding: 0 4px 9px;
  margin-bottom: 11px;
  border-bottom: 1px solid var(--line);
}

.stop-heading-name {
  display: flex;
  align-items: center;
  font-size: 1.02rem;
  font-weight: 800;
  letter-spacing: -.01em;
}

.stop-heading-name::before {
  content: "";
  flex: none;
  width: 3px;
  height: 13px;
  margin-right: 8px;
  border-radius: 2px;
  background: var(--accent);
}

.stop-heading-code {
  flex: none;
  font-size: .7rem;
  font-weight: 700;
  letter-spacing: .07em;
  color: var(--muted);
}

.route-list {
  list-style: none;
  margin: 0;
  padding: 0;
  display: grid;
  gap: 10px;
}

.state-line {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 28px 16px;
  justify-content: center;
  color: var(--muted);
  font-size: .95rem;
}

.spinner {
  width: 16px;
  height: 16px;
  border-radius: 50%;
  border: 2px solid var(--line-strong);
  border-top-color: var(--accent);
  animation: spin .8s linear infinite;
}

/* ---------- 路線卡片 ---------- */

.route-card {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius-lg);
  padding: 14px 16px 15px;
  box-shadow: var(--shadow);
  transition: box-shadow .18s ease, border-color .18s ease;
}

@media (hover: hover) {
  .route-card:hover {
    box-shadow: var(--shadow-lift);
    border-color: var(--line-strong);
  }
}

.card-head {
  display: flex;
  align-items: center;
  gap: 12px;
}

.route-no {
  flex-shrink: 0;
  min-width: 60px;
  padding: 5px 10px;
  text-align: center;
  background: var(--accent);
  color: var(--accent-ink);
  border-radius: var(--radius-sm);
  font-size: 1.32rem;
  font-weight: 800;
  letter-spacing: -.01em;
  font-variant-numeric: tabular-nums;
}

.dest-wrap {
  display: flex;
  align-items: baseline;
  gap: 6px;
  min-width: 0;
  flex: 1;
}

.dest-label {
  font-size: .8rem;
  color: var(--muted);
  flex-shrink: 0;
}

.dest-name {
  font-size: 1.12rem;
  font-weight: 700;
  letter-spacing: -.01em;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  min-width: 0;
}

/* 城巴：品牌黃底黑字，和九巴的紅色一眼分得開 */
.route-no[data-op="CTB"] {
  background: #f7c600;
  color: #1a1400;
}

/* 營運商標籤 */
.op-tag {
  flex-shrink: 0;
  font-size: .66rem;
  font-weight: 700;
  letter-spacing: .03em;
  padding: 2px 7px;
  border-radius: 999px;
  white-space: nowrap;
  color: var(--accent);
  background: var(--accent-soft);
  border: 1px solid color-mix(in srgb, var(--accent) 30%, transparent);
}

.op-tag[data-op="CTB"] {
  color: #7d6000;
  background: rgba(247, 198, 0, .18);
  border-color: rgba(190, 152, 0, .45);
}

:root[data-theme="dark"] .op-tag[data-op="CTB"] {
  color: #f5c518;
  background: rgba(245, 197, 24, .14);
  border-color: rgba(245, 197, 24, .38);
}

.sched-badge {
  flex-shrink: 0;
  font-size: .68rem;
  font-weight: 700;
  letter-spacing: .04em;
  color: var(--muted);
  border: 1px solid var(--line-strong);
  border-radius: 999px;
  padding: 2px 7px;
}

/* ---------- 倒數 chip ---------- */

.card-times {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 8px;
  margin-top: 12px;
}

.chip {
  position: relative;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 1px;
  padding: 9px 4px 7px;
  border-radius: var(--radius-md);
  background: var(--surface-2);
  border: 1px solid transparent;
  min-width: 0;
}

.chip.is-next {
  border-color: color-mix(in srgb, var(--accent) 42%, transparent);
  background: color-mix(in srgb, var(--accent) 8%, var(--surface-2));
}

.chip-value {
  font-size: 1.16rem;
  font-weight: 800;
  letter-spacing: -.02em;
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
  color: var(--text);
}

.chip-value[data-tone="soon"] { color: var(--soon); }
.chip-value[data-tone="normal"] { color: var(--text-dim); }

.chip-value[data-tone="now"] {
  color: var(--now);
  animation: pulse 1.6s ease-in-out infinite;
}

@keyframes pulse {
  0%, 100% { opacity: 1; }
  50% { opacity: .55; }
}

.chip-foot {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 5px;
  flex-wrap: wrap;
  min-width: 0;
}

.chip-clock {
  font-size: .72rem;
  color: var(--muted);
  font-variant-numeric: tabular-nums;
  letter-spacing: .01em;
}

.chip-tag {
  font-size: .6rem;
  font-weight: 700;
  line-height: 1;
  color: var(--muted);
  border: 1px solid var(--line-strong);
  border-radius: 4px;
  padding: 2px 3px;
  white-space: nowrap;
}

/* ---------- 無班次：緊湊單行 ---------- */

.route-card.is-idle { padding: 11px 14px; box-shadow: none; }

.route-card.is-idle:hover { box-shadow: none; }

.idle-row {
  display: flex;
  align-items: center;
  gap: 11px;
}

.route-no-sm {
  min-width: 46px;
  padding: 3px 8px;
  font-size: 1rem;
  background: var(--surface-2);
  color: var(--text-dim);
  border: 1px solid var(--line-strong);
}

.route-no-sm[data-op="CTB"] {
  background: #f7c600;
  color: #1a1400;
  border-color: rgba(26, 20, 0, .22);
}

.idle-body {
  display: flex;
  flex-direction: column;
  gap: 1px;
  min-width: 0;
}

.idle-dest {
  font-size: .98rem;
  font-weight: 700;
  color: var(--text-dim);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.idle-remark {
  font-size: .82rem;
  line-height: 1.35;
  color: var(--muted);
}

.empty-card {
  list-style: none;
  text-align: center;
  padding: 34px 20px;
  border: 1px dashed var(--line-strong);
  border-radius: var(--radius-lg);
  background: var(--surface);
}

.empty-title {
  margin: 0 0 6px;
  font-size: 1rem;
  font-weight: 700;
  color: var(--text-dim);
}

.empty-hint {
  margin: 0;
  font-size: .85rem;
  color: var(--muted);
}

/* ==========================================================================
 * 頁腳（4.4 數據署名）
 * ========================================================================== */

.app-footer {
  max-width: var(--page-max);
  width: 100%; /* 同上：flex item 需要明確寬度 */
  margin: auto auto 0;
  padding: 22px var(--page-pad) calc(26px + env(safe-area-inset-bottom));
  text-align: center;
  font-size: .78rem;
  line-height: 1.6;
  color: var(--muted);
}

.app-footer p { margin: 0; }
.app-footer p + p { margin-top: 4px; }

.app-footer a {
  color: var(--text-dim);
  text-decoration: none;
  border-bottom: 1px solid var(--line-strong);
}

.app-footer a:hover { color: var(--accent); border-color: var(--accent); }

.app-footer a:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
  border-radius: 2px;
}

.disclaimer { opacity: .8; }

/* 頁腳圖例：紅＝九巴、黃＝城巴（卡片上的路線牌用同一組顏色） */
.legend {
  display: flex;
  align-items: center;
  justify-content: center;
  flex-wrap: wrap;
  gap: 6px;
  margin-bottom: 8px;
  font-size: .74rem;
  color: var(--muted);
}

.legend-chip {
  display: inline-block;
  padding: 1px 7px;
  border-radius: 5px;
  font-size: .68rem;
  font-weight: 800;
  letter-spacing: .02em;
  background: var(--accent);
  color: var(--accent-ink);
}

.legend-chip[data-op="CTB"] {
  background: #f7c600;
  color: #1a1400;
}

/* ==========================================================================
 * 平板（700px+）：仍然單欄，放大間距
 * ========================================================================== */

@media (min-width: 700px) {
  body { font-size: 17.5px; }
  .header-title h1 { font-size: 1.62rem; }
  .updated { font-size: .84rem; }
  .route-no { font-size: 1.4rem; min-width: 66px; }
  .dest-name { font-size: 1.16rem; }
  .chip-value { font-size: 1.24rem; }
  .route-card { padding: 16px 20px 17px; }
  .route-list { gap: 11px; }
}

/* ==========================================================================
 * 桌面（1200px+）：兩個方向並排，卡片改成單行
 * 斷點取 1200 而非 1080：再窄的話每欄只剩 ~490px，
 * 「坪石 / 彩虹站」這種長目的地會被省略號截掉。
 * ========================================================================== */

@media (min-width: 1200px) {
  :root { --page-pad: 32px; --page-max: 1200px; }

  .app-header {
    padding-top: calc(env(safe-area-inset-top) + 18px);
    padding-bottom: 14px;
  }

  #app {
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    align-items: start;
    gap: 0 36px;
    padding-top: 26px;
    padding-bottom: 20px;
  }

  /* 兩欄時「+ margin-top」會把右欄推低 */
  .stop-section + .stop-section { margin-top: 0; }

  .stop-heading { padding-bottom: 10px; margin-bottom: 13px; }

  .stop-heading-name { font-size: 1.08rem; }

  /* 卡片：路線 + 目的地 | 三個倒數 */
  .route-card {
    display: flex;
    align-items: center;
    gap: 20px;
    padding: 13px 18px;
    border-radius: var(--radius-md);
  }

  .card-head {
    flex: 1 1 auto;
    min-width: 0;
  }

  .card-times {
    flex: 0 0 auto;
    margin-top: 0;
    grid-template-columns: repeat(3, 88px);
    gap: 7px;
  }

  .chip {
    padding: 7px 4px 5px;
  }

  .chip-foot { gap: 4px; }

  .chip-value { font-size: 1.04rem; }
  .chip-clock { font-size: .68rem; }
  .chip-tag { font-size: .58rem; padding: 1px 2px; }

  .route-no { font-size: 1.2rem; min-width: 56px; padding: 4px 8px; }

  /* 單行卡片寬度有限：目的地容許換行（否則「坪石 / 彩虹站」會被截斷），
     營運商改由路線牌底色＋頁腳圖例表達，省下標籤的寬度 */
  .dest-name {
    font-size: 1.05rem;
    white-space: normal;
    overflow: hidden;
    text-overflow: clip;
    line-height: 1.25;
  }

  .op-tag { display: none; }

  .route-list { gap: 9px; }

  .empty-card { padding: 40px 20px; }

  .app-footer {
    padding-top: 18px;
    font-size: .8rem;
  }
}

/* 超寬螢幕：再放寬一點，但別讓單行卡片拉太長 */
@media (min-width: 1560px) {
  :root { --page-max: 1320px; }
  .card-times { grid-template-columns: repeat(3, 100px); }
}

/* 橫向矮螢幕（例如桌面小窗）：縮一點頁首 */
@media (min-width: 1200px) and (max-height: 620px) {
  .app-header { padding-top: calc(env(safe-area-inset-top) + 10px); padding-bottom: 10px; }
  #app { padding-top: 16px; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: .001ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: .001ms !important;
  }
}
