:root {
  --bg: #f3f4f6;
  --card: #ffffff;
  --text: #1a1d21;
  --muted: #6b7280;
  --line: #e3e5e8;
  --accent: #1f5f8b;
  --accent-soft: #e6f0f7;
  --tobu: #1667b8;
  --tobu-soft: #e3eefa;
  --kk: #c0392b;
  --kk-soft: #fbe9e7;
  --go: #15803d;
  --go-soft: #e7f6ec;
  --soon: #b45309;
  --soon-soft: #fdf1e1;
  --now: #d92d20;
  --now-soft: #fde9e7;
  --miss: #9aa0a8;
  --shadow: 0 1px 2px rgba(0, 0, 0, .06), 0 2px 8px rgba(0, 0, 0, .04);
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #0f1114;
    --card: #1a1d22;
    --text: #e8eaed;
    --muted: #9aa0a6;
    --line: #2b3037;
    --accent: #6fb1e3;
    --accent-soft: #17293a;
    --tobu: #6aa9ef;
    --tobu-soft: #172a40;
    --kk: #f08a7e;
    --kk-soft: #3a1d1a;
    --go: #4ade80;
    --go-soft: #14301f;
    --soon: #fbbf24;
    --soon-soft: #3a2c10;
    --now: #ff7b6e;
    --now-soft: #3d1a17;
    --miss: #6b7178;
    --shadow: none;
    color-scheme: dark;
  }
}

:root[data-theme="dark"] {
  --bg: #0f1114;
  --card: #1a1d22;
  --text: #e8eaed;
  --muted: #9aa0a6;
  --line: #2b3037;
  --accent: #6fb1e3;
  --accent-soft: #17293a;
  --tobu: #6aa9ef;
  --tobu-soft: #172a40;
  --kk: #f08a7e;
  --kk-soft: #3a1d1a;
  --go: #4ade80;
  --go-soft: #14301f;
  --soon: #fbbf24;
  --soon-soft: #3a2c10;
  --now: #ff7b6e;
  --now-soft: #3d1a17;
  --miss: #6b7178;
  --shadow: none;
  color-scheme: dark;
}

* { box-sizing: border-box; }

html { -webkit-text-size-adjust: 100%; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: system-ui, -apple-system, "Hiragino Sans", "Hiragino Kaku Gothic ProN", "Yu Gothic UI", "Meiryo", sans-serif;
  font-size: 16px;
  line-height: 1.5;
  -webkit-tap-highlight-color: transparent;
}

[hidden] { display: none !important; }

a { color: var(--accent); }

button, input, select { font: inherit; color: inherit; }

svg { width: 20px; height: 20px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; flex: none; }

/* ---------- 上部バー ---------- */
.topbar {
  position: sticky;
  top: 0;
  z-index: 10;
  display: flex;
  align-items: center;
  gap: 8px;
  padding: calc(env(safe-area-inset-top) + 8px) 16px 8px;
  background: color-mix(in srgb, var(--bg) 88%, transparent);
  backdrop-filter: blur(10px);
  -webkit-backdrop-filter: blur(10px);
}
.topbar h1 { flex: 1; margin: 0; font-size: 20px; font-weight: 700; letter-spacing: .02em; }
#settingsView .topbar h1 { text-align: center; }
.topbar-actions { display: flex; gap: 4px; }
.topbar-spacer { width: 72px; }

.icon-btn {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  min-height: 40px;
  min-width: 40px;
  padding: 0 10px;
  border: 0;
  border-radius: 20px;
  background: transparent;
  color: var(--text);
  text-decoration: none;
  cursor: pointer;
}
.icon-btn:active { background: var(--line); }
.icon-btn.back { width: 72px; padding-left: 4px; color: var(--accent); }
.updated { font-size: 13px; color: var(--muted); font-variant-numeric: tabular-nums; }
.loading #refreshBtn svg { animation: spin 1s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

.content {
  max-width: 640px;
  margin: 0 auto;
  padding: 4px 16px calc(env(safe-area-inset-bottom) + 24px);
}

/* ---------- 起点の切り替え ---------- */
.origin-bar { margin-bottom: 12px; }
.seg {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  padding: 3px;
  border-radius: 10px;
  background: var(--line);
}
.seg button {
  min-height: 36px;
  border: 0;
  border-radius: 8px;
  background: transparent;
  font-size: 14px;
  cursor: pointer;
}
.seg button[aria-checked="true"] { background: var(--card); font-weight: 700; box-shadow: var(--shadow); }
.origin-note { margin: 6px 2px 0; font-size: 13px; color: var(--muted); }
.origin-note.warn { color: var(--soon); }

/* ---------- カード共通 ---------- */
.card {
  margin-bottom: 12px;
  padding: 16px;
  border-radius: 14px;
  background: var(--card);
  box-shadow: var(--shadow);
}
.card h2 { margin: 0 0 10px; font-size: 16px; }
.empty p { margin: 0 0 14px; color: var(--muted); }

/* ---------- いちばん上の「次に乗れるバス」 ---------- */
.hero {
  margin-bottom: 12px;
  padding: 16px;
  border-radius: 16px;
  background: var(--card);
  box-shadow: var(--shadow);
  border-left: 6px solid var(--accent);
}
.hero.st-go { border-left-color: var(--go); }
.hero.st-soon { border-left-color: var(--soon); }
.hero.st-now { border-left-color: var(--now); }
.hero-label { font-size: 13px; color: var(--muted); }
.hero-main { display: flex; align-items: baseline; gap: 6px; margin: 2px 0 6px; flex-wrap: wrap; }
.hero-num { font-size: 52px; line-height: 1; font-weight: 800; font-variant-numeric: tabular-nums; }
.hero-unit { font-size: 18px; font-weight: 700; }
.hero.st-go .hero-num, .hero.st-go .hero-unit { color: var(--go); }
.hero.st-soon .hero-num, .hero.st-soon .hero-unit { color: var(--soon); }
.hero.st-now .hero-num, .hero.st-now .hero-unit { color: var(--now); }
.hero-bus { font-size: 15px; font-weight: 600; }
.hero-sub { margin-top: 2px; font-size: 13px; color: var(--muted); }

/* ---------- 乗り場ごとの徒歩時間 ---------- */
.stop-chips { display: flex; flex-wrap: wrap; gap: 6px; margin: 0 0 10px; padding: 0; list-style: none; }
.stop-chips li {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  max-width: 100%;
  padding: 4px 10px 4px 4px;
  border-radius: 999px;
  background: var(--card);
  box-shadow: var(--shadow);
  font-size: 13px;
}
.stop-chips a { text-decoration: none; color: inherit; display: inline-flex; align-items: center; gap: 6px; min-width: 0; }
.stop-chips .chip-name { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.stop-chips .chip-walk { color: var(--muted); white-space: nowrap; }

.badge {
  display: inline-block;
  flex: none;
  padding: 1px 8px;
  border-radius: 999px;
  font-size: 12px;
  font-weight: 700;
  white-space: nowrap;
}
.badge.tobu { background: var(--tobu-soft); color: var(--tobu); }
.badge.kk { background: var(--kk-soft); color: var(--kk); }

/* ---------- お知らせ ---------- */
.alert {
  margin-bottom: 10px;
  padding: 10px 12px;
  border-radius: 10px;
  background: var(--soon-soft);
  color: var(--text);
  font-size: 14px;
}
.alert.err { background: var(--now-soft); }

/* ---------- バスの一覧 ---------- */
.bus-list { margin: 0; padding: 0; list-style: none; }
.bus {
  display: grid;
  grid-template-columns: 72px 1fr;
  gap: 12px;
  margin-bottom: 8px;
  padding: 12px;
  border-radius: 14px;
  background: var(--card);
  box-shadow: var(--shadow);
}
.leave {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 6px 2px;
  border-radius: 10px;
  text-align: center;
  background: var(--accent-soft);
  color: var(--accent);
}
.leave .num { font-size: 28px; font-weight: 800; line-height: 1.05; font-variant-numeric: tabular-nums; }
.leave .unit { font-size: 11px; font-weight: 700; line-height: 1.3; }
.leave .word { font-size: 15px; font-weight: 800; line-height: 1.2; }
.st-go .leave { background: var(--go-soft); color: var(--go); }
.st-soon .leave { background: var(--soon-soft); color: var(--soon); }
.st-now .leave { background: var(--now-soft); color: var(--now); }
.st-miss .leave, .st-far .leave { background: var(--line); color: var(--muted); }
.bus.st-miss { opacity: .55; padding-top: 8px; padding-bottom: 8px; }
.bus.st-miss .line3, .bus.st-miss .rt { display: none; }
.bus.st-miss .time { font-size: 17px; }
.bus.st-miss .leave .word { font-size: 12px; }

.info { min-width: 0; }
.line1 { display: flex; align-items: center; gap: 6px; min-width: 0; }
.route { font-weight: 700; white-space: nowrap; }
.dest { min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; color: var(--text); }
.line2 { display: flex; align-items: baseline; flex-wrap: wrap; gap: 4px 10px; margin-top: 2px; }
.time { font-size: 22px; font-weight: 800; font-variant-numeric: tabular-nums; }
.eta { font-size: 14px; color: var(--muted); font-variant-numeric: tabular-nums; }
.delay { font-size: 13px; font-weight: 700; color: var(--soon); }
.ontime { font-size: 13px; color: var(--go); }
.line3 { margin-top: 2px; font-size: 13px; color: var(--muted); }
.rt { display: inline-block; margin-top: 4px; padding: 1px 8px; border-radius: 6px; font-size: 12px; font-weight: 600; background: var(--line); color: var(--muted); }
.rt.live { background: var(--go-soft); color: var(--go); }
.rt.live::before { content: ""; display: inline-block; width: 7px; height: 7px; margin-right: 5px; border-radius: 50%; background: currentColor; vertical-align: 1px; animation: blink 1.6s ease-in-out infinite; }
@keyframes blink { 50% { opacity: .25; } }
.list-note { margin: 8px 2px 16px; font-size: 13px; color: var(--muted); }

.foot { margin-top: 24px; font-size: 12px; color: var(--muted); }

/* ---------- 設定画面 ---------- */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  min-height: 44px;
  padding: 0 16px;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: var(--card);
  color: var(--text);
  font-weight: 600;
  text-decoration: none;
  cursor: pointer;
}
.btn:active { background: var(--line); }
.btn.primary { border-color: var(--accent); background: var(--accent); color: #fff; }
.btn.ghost { background: transparent; }
.btn.small { min-height: 34px; padding: 0 12px; font-size: 14px; }
.btn.danger { color: var(--now); }
.btn[disabled] { opacity: .5; }
#nearbyBtn { width: 100%; margin-bottom: 10px; }
.btn-row { display: flex; flex-wrap: wrap; align-items: center; gap: 8px; margin-top: 8px; }

.search-row { display: flex; gap: 8px; }
.search-row input { flex: 1; min-width: 0; }
input[type="search"], input[type="text"], input[type="password"], input[type="number"], select {
  min-height: 44px;
  padding: 0 12px;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: var(--bg);
}
.field { display: flex; flex-direction: column; gap: 4px; margin-bottom: 10px; font-size: 14px; font-weight: 600; }
.hint { margin: 8px 0 0; font-size: 13px; color: var(--muted); }
.muted { color: var(--muted); }
.sub { margin-top: 10px; font-size: 14px; }
.sub summary { cursor: pointer; color: var(--accent); }

.results { margin-top: 10px; }
.results ul { margin: 0; padding: 0; list-style: none; }
.result-item {
  display: flex;
  align-items: center;
  gap: 8px;
  width: 100%;
  min-height: 48px;
  padding: 8px 4px;
  border: 0;
  border-bottom: 1px solid var(--line);
  background: transparent;
  text-align: left;
  cursor: pointer;
}
.result-item .r-name { flex: 1; min-width: 0; font-weight: 600; }
.result-item .r-dist { font-size: 13px; color: var(--muted); white-space: nowrap; }

.stop-detail:not(:empty) { margin-top: 14px; padding-top: 14px; border-top: 2px solid var(--line); }
.stop-detail h3 { display: flex; align-items: center; gap: 8px; margin: 0 0 4px; font-size: 17px; }
.pole { margin-top: 12px; }
.pole h4 { margin: 0 0 4px; font-size: 14px; color: var(--muted); }
.group-check {
  display: flex;
  align-items: flex-start;
  gap: 10px;
  padding: 10px 4px;
  border-bottom: 1px solid var(--line);
  cursor: pointer;
}
.group-check input { width: 22px; height: 22px; margin: 1px 0 0; flex: none; accent-color: var(--accent); }
.group-check .g-name { font-weight: 700; }
.group-check .g-dest { display: block; font-size: 14px; color: var(--muted); }
.detail-actions { display: flex; gap: 8px; margin-top: 12px; }
.detail-actions .btn { flex: 1; }

.entry-list { margin: 0; padding: 0; list-style: none; }
.entry { padding: 12px 0; border-bottom: 1px solid var(--line); }
.entry:last-child { border-bottom: 0; }
.entry-head { display: flex; align-items: center; gap: 8px; }
.entry-name { flex: 1; min-width: 0; font-weight: 700; }
.entry-groups { margin: 4px 0 8px; font-size: 13px; color: var(--muted); }
.entry-walk { display: flex; align-items: center; flex-wrap: wrap; gap: 6px; font-size: 14px; }
.entry-walk input { width: 76px; text-align: center; }
.test-result { font-size: 14px; }
.test-result.ok { color: var(--go); }
.test-result.ng { color: var(--now); }

@media (min-width: 600px) {
  .bus { grid-template-columns: 84px 1fr; }
  .leave .num { font-size: 32px; }
}
