:root {
  --sbb-red: #d52b1e;
  --sbb-red-dark: #b21d12;
  --bg: #f4f5f6;
  --surface: #ffffff;
  --surface-2: #f0f1f3;
  --ink: #1c1f23;
  --ink-soft: #51585f;
  --ink-faint: #828a92;
  --line: #d9dde1;
  --line-soft: #e7eaed;
  --accent: var(--sbb-red);
  --ok: #1a8f3c;
  --warn: #c8741a;
  --late: #d52b1e;
  --shadow: 0 1px 3px rgba(0, 0, 0, 0.10), 0 4px 14px rgba(0, 0, 0, 0.05);
  --radius: 12px;
  --radius-sm: 8px;
  --focus: 0 0 0 3px rgba(213, 43, 30, 0.40);
  --maxw: 720px;
}

[data-theme="dark"] {
  --bg: #15171a;
  --surface: #1f2227;
  --surface-2: #272b31;
  --ink: #eef1f4;
  --ink-soft: #b6bdc5;
  --ink-faint: #828a92;
  --line: #353a41;
  --line-soft: #2c3036;
  --accent: #ff5648;
  --ok: #4ec06f;
  --warn: #e0973f;
  --late: #ff5648;
  --shadow: 0 1px 3px rgba(0, 0, 0, 0.4), 0 6px 18px rgba(0, 0, 0, 0.35);
  --focus: 0 0 0 3px rgba(255, 86, 72, 0.45);
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  background: var(--bg);
  color: var(--ink);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  font-size: 16px;
  line-height: 1.45;
  -webkit-text-size-adjust: 100%;
}

body {
  min-height: 100vh;
  overflow-x: hidden;
}

.skip-link {
  position: absolute;
  left: -999px;
  top: 0;
  background: var(--sbb-red);
  color: #fff;
  padding: 10px 16px;
  border-radius: 0 0 var(--radius-sm) 0;
  z-index: 100;
}
.skip-link:focus { left: 0; }

.hidden { display: none !important; }

/* Header */
.app-header {
  background: var(--sbb-red);
  color: #fff;
  position: sticky;
  top: 0;
  z-index: 30;
  padding-top: env(safe-area-inset-top);
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.18);
}
.header-inner {
  max-width: var(--maxw);
  margin: 0 auto;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 12px 16px;
}
.brand { display: flex; align-items: center; gap: 10px; min-width: 0; }
.brand-mark { display: inline-flex; flex: 0 0 auto; }
.brand-text {
  font-weight: 800;
  font-size: 1.15rem;
  letter-spacing: 0.2px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.header-actions { display: flex; gap: 6px; flex: 0 0 auto; }

.icon-btn {
  background: transparent;
  border: none;
  color: inherit;
  width: 42px;
  height: 42px;
  border-radius: 50%;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  transition: background 0.15s ease;
}
.icon-btn:hover { background: rgba(255, 255, 255, 0.16); }
.app-main .icon-btn { color: var(--ink-soft); }
.app-main .icon-btn:hover { background: var(--surface-2); }

.icon-btn:focus-visible,
.btn:focus-visible,
.text-input:focus-visible,
.chip:focus-visible,
.suggest-item:focus-visible,
.skip-link:focus-visible {
  outline: none;
  box-shadow: var(--focus);
}

/* theme icon toggle */
.ico-moon { display: none; }
[data-theme="dark"] .ico-sun { display: none; }
[data-theme="dark"] .ico-moon { display: inline; }

/* star toggle */
.ico-star-f { display: none; }
.is-saved .ico-star-o { display: none; }
.is-saved .ico-star-f { display: inline; color: var(--sbb-red); }

/* Main */
.app-main {
  max-width: var(--maxw);
  margin: 0 auto;
  padding: 16px 16px calc(40px + env(safe-area-inset-bottom));
  display: flex;
  flex-direction: column;
  gap: 16px;
}

/* Search */
.search-section {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 16px;
}
.search-form { display: flex; flex-direction: column; gap: 12px; }
.search-field { position: relative; }
.field-label {
  display: block;
  font-size: 0.82rem;
  font-weight: 700;
  color: var(--ink-soft);
  margin-bottom: 6px;
  text-transform: uppercase;
  letter-spacing: 0.4px;
}
.input-wrap { position: relative; }
.text-input {
  width: 100%;
  padding: 13px 40px 13px 14px;
  font-size: 1rem;
  border: 2px solid var(--line);
  border-radius: var(--radius-sm);
  background: var(--surface);
  color: var(--ink);
  transition: border-color 0.15s ease;
}
.text-input::placeholder { color: var(--ink-faint); }
.text-input:focus { border-color: var(--accent); outline: none; }

.clear-btn {
  position: absolute;
  right: 6px;
  top: 50%;
  transform: translateY(-50%);
  width: 30px;
  height: 30px;
  border: none;
  background: transparent;
  color: var(--ink-faint);
  cursor: pointer;
  border-radius: 50%;
  display: inline-flex;
  align-items: center;
  justify-content: center;
}
.clear-btn:hover { background: var(--surface-2); color: var(--ink); }

.suggest-list {
  position: absolute;
  z-index: 20;
  left: 0;
  right: 0;
  margin: 6px 0 0;
  padding: 4px;
  list-style: none;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius-sm);
  box-shadow: var(--shadow);
  max-height: 300px;
  overflow-y: auto;
}
.suggest-item {
  padding: 11px 12px;
  border-radius: 6px;
  cursor: pointer;
  display: flex;
  align-items: center;
  gap: 10px;
  color: var(--ink);
}
.suggest-item:hover,
.suggest-item.active { background: var(--surface-2); }
.suggest-item .si-icon { color: var(--sbb-red); flex: 0 0 auto; }
.suggest-item .si-name { font-weight: 600; word-break: break-word; }

.search-buttons { display: flex; gap: 10px; flex-wrap: wrap; }

.btn {
  font: inherit;
  font-weight: 700;
  border: 2px solid transparent;
  border-radius: var(--radius-sm);
  padding: 12px 18px;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  gap: 8px;
  transition: background 0.15s ease, border-color 0.15s ease, color 0.15s ease;
}
.btn-primary {
  background: var(--sbb-red);
  color: #fff;
  flex: 1 1 auto;
  justify-content: center;
}
.btn-primary:hover { background: var(--sbb-red-dark); }
.btn-ghost {
  background: var(--surface);
  color: var(--ink);
  border-color: var(--line);
}
.btn-ghost:hover { background: var(--surface-2); border-color: var(--ink-faint); }
.btn-link {
  background: transparent;
  color: var(--accent);
  border: none;
  padding: 12px 6px;
}
.btn-link:hover { text-decoration: underline; }

.hint { margin: 0; font-size: 0.82rem; color: var(--ink-faint); }

/* Coord panel */
.coord-panel {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 16px;
}
.panel-title { margin: 0 0 4px; font-size: 1.05rem; }
.panel-sub { margin: 0 0 12px; font-size: 0.85rem; color: var(--ink-soft); }
.coord-form { display: flex; flex-direction: column; gap: 12px; }
.coord-inputs { display: flex; gap: 10px; flex-wrap: wrap; }
.coord-label {
  flex: 1 1 130px;
  font-size: 0.8rem;
  font-weight: 700;
  color: var(--ink-soft);
  display: flex;
  flex-direction: column;
  gap: 6px;
}
.coord-buttons { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; }

.nearby-list { list-style: none; margin: 14px 0 0; padding: 0; display: flex; flex-direction: column; gap: 8px; }
.nearby-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 12px;
  border: 1px solid var(--line);
  border-radius: var(--radius-sm);
  background: var(--surface);
  cursor: pointer;
  text-align: left;
  width: 100%;
  color: var(--ink);
  font: inherit;
}
.nearby-item:hover { background: var(--surface-2); }
.nearby-item .nb-name { font-weight: 600; word-break: break-word; }
.nearby-item .nb-dist { color: var(--ink-faint); font-size: 0.85rem; white-space: nowrap; flex: 0 0 auto; }

/* Chips */
.chips-section { display: flex; flex-direction: column; gap: 12px; }
.chips-group {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 14px 16px;
  box-shadow: var(--shadow);
}
.chips-title {
  margin: 0 0 10px;
  font-size: 0.78rem;
  text-transform: uppercase;
  letter-spacing: 0.5px;
  color: var(--ink-soft);
}
.chips { display: flex; flex-wrap: wrap; gap: 8px; }
.chip {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  background: var(--surface-2);
  border: 1px solid var(--line);
  border-radius: 999px;
  padding: 8px 6px 8px 14px;
  font: inherit;
  font-size: 0.92rem;
  color: var(--ink);
  cursor: pointer;
  max-width: 100%;
}
.chip:hover { border-color: var(--accent); }
.chip .chip-label { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; max-width: 200px; }
.chip .chip-x {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 24px;
  height: 24px;
  border-radius: 50%;
  border: none;
  background: transparent;
  color: var(--ink-faint);
  cursor: pointer;
  flex: 0 0 auto;
}
.chip .chip-x:hover { background: var(--surface); color: var(--late); }

/* Board */
.board-section {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  overflow: hidden;
}
.board-head {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 12px;
  padding: 16px;
  background: var(--sbb-red);
  color: #fff;
}
.board-head-text { min-width: 0; }
.board-station {
  margin: 0;
  font-size: 1.25rem;
  font-weight: 800;
  word-break: break-word;
}
.board-meta { margin: 4px 0 0; font-size: 0.82rem; opacity: 0.9; }
.board-head-actions { display: flex; gap: 4px; flex: 0 0 auto; }
.board-head .icon-btn { color: #fff; }
.board-head .icon-btn:hover { background: rgba(255, 255, 255, 0.18); }
.board-head .icon-btn.is-saved { color: #fff; }
.board-head .icon-btn.is-saved .ico-star-f { color: #fff; }

.offline-banner {
  background: #fff4e5;
  color: #8a4b00;
  padding: 10px 16px;
  font-size: 0.85rem;
  font-weight: 600;
  border-bottom: 1px solid var(--line-soft);
}
[data-theme="dark"] .offline-banner { background: #3a2a12; color: #f1c187; }

.board-loading { padding: 18px 16px; color: var(--ink-soft); }
.board-empty { padding: 18px 16px; color: var(--ink-soft); margin: 0; }

.departures { list-style: none; margin: 0; padding: 0; }
.dep {
  display: grid;
  grid-template-columns: auto 1fr auto;
  gap: 12px;
  align-items: center;
  padding: 13px 16px;
  border-bottom: 1px solid var(--line-soft);
}
.dep:last-child { border-bottom: none; }

.dep-line {
  flex: 0 0 auto;
  min-width: 52px;
  text-align: center;
  font-weight: 800;
  font-size: 0.92rem;
  color: #fff;
  background: var(--sbb-red);
  border-radius: 6px;
  padding: 6px 8px;
  white-space: nowrap;
}
.dep-line.cat-bus { background: #2b6fb5; }
.dep-line.cat-tram { background: #1a8f3c; }
.dep-line.cat-ship { background: #0a8aa0; }
.dep-line.cat-funicular { background: #7a4ba0; }
.dep-line.cat-train { background: var(--sbb-red); }

.dep-mid { min-width: 0; }
.dep-to {
  font-weight: 700;
  font-size: 1rem;
  word-break: break-word;
  line-height: 1.25;
}
.dep-sub {
  margin-top: 3px;
  font-size: 0.82rem;
  color: var(--ink-soft);
  display: flex;
  flex-wrap: wrap;
  gap: 4px 10px;
}
.dep-platform { display: inline-flex; align-items: center; gap: 4px; }
.dep-platform strong { color: var(--ink); }

.dep-right { text-align: right; flex: 0 0 auto; }
.dep-time {
  font-weight: 800;
  font-size: 1.05rem;
  font-variant-numeric: tabular-nums;
}
.dep-time.has-delay { color: var(--ink-faint); text-decoration: line-through; font-size: 0.92rem; }
.dep-realtime { color: var(--late); font-weight: 800; font-size: 1.05rem; font-variant-numeric: tabular-nums; }
.dep-countdown {
  margin-top: 2px;
  font-size: 0.82rem;
  font-weight: 700;
  color: var(--ok);
  font-variant-numeric: tabular-nums;
}
.dep-countdown.soon { color: var(--late); }
.dep-delay {
  display: inline-block;
  margin-top: 2px;
  font-size: 0.74rem;
  font-weight: 800;
  color: var(--late);
}

/* Source note */
.source-note {
  font-size: 0.78rem;
  color: var(--ink-faint);
  line-height: 1.5;
  text-align: center;
  padding: 0 4px;
}
.source-note strong { color: var(--ink-soft); }

/* Toast */
.toast {
  position: fixed;
  left: 50%;
  bottom: calc(20px + env(safe-area-inset-bottom));
  transform: translateX(-50%) translateY(20px);
  background: #2a2d31;
  color: #fff;
  padding: 12px 18px;
  border-radius: 999px;
  font-size: 0.9rem;
  font-weight: 600;
  box-shadow: 0 6px 20px rgba(0, 0, 0, 0.3);
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.25s ease, transform 0.25s ease;
  z-index: 200;
  max-width: calc(100vw - 32px);
  text-align: center;
}
.toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }
.toast.error { background: var(--sbb-red); }

@media (max-width: 400px) {
  .search-buttons { flex-direction: column; }
  .btn-ghost { justify-content: center; }
  .dep { grid-template-columns: auto 1fr; }
  .dep-right { grid-column: 1 / -1; text-align: left; display: flex; gap: 12px; align-items: baseline; }
}

@media (prefers-reduced-motion: reduce) {
  * { transition: none !important; animation: none !important; }
  .toast { transition: opacity 0.01s; }
}
