/* ========================================
   When To Leave - Styles
   ======================================== */

*, *::before, *::after {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

/* --- Theme Variables --- */
:root {
  --orange: #ED8B00;
  --orange-dim: #b36800;
  --green: #2ecc71;
  --yellow: #f1c40f;
  --red: #e74c3c;
  --gray: #888;
}

[data-theme="dark"] {
  --bg: #121212;
  --bg-card: #1e1e1e;
  --bg-input: #2a2a2a;
  --text: #f0f0f0;
  --text-muted: #999;
  --border: #333;
  --shadow: rgba(0,0,0,0.4);
}

[data-theme="light"] {
  --bg: #f5f5f5;
  --bg-card: #ffffff;
  --bg-input: #eee;
  --text: #1a1a1a;
  --text-muted: #666;
  --border: #ddd;
  --shadow: rgba(0,0,0,0.1);
}

html {
  font-size: 16px;
}

body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
  min-height: 100vh;
  transition: background 0.3s, color 0.3s;
}

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

/* --- Error Toast --- */
.error-toast {
  position: fixed;
  top: 1rem;
  left: 50%;
  transform: translateX(-50%);
  z-index: 2000;
  background: var(--red);
  color: #fff;
  padding: 0.6rem 1.25rem;
  border-radius: 8px;
  font-size: 0.85rem;
  font-weight: 600;
  box-shadow: 0 4px 16px rgba(0,0,0,0.3);
  transition: opacity 0.3s, transform 0.3s;
  max-width: 90vw;
  text-align: center;
}
.error-toast.hidden {
  opacity: 0;
  pointer-events: none;
  transform: translateX(-50%) translateY(-1rem);
}

/* --- App Shell --- */
.app {
  max-width: 1100px;
  margin: 0 auto;
  padding: 1rem;
  display: flex;
  flex-direction: column;
  min-height: 100vh;
}

/* --- Header --- */
.header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding-bottom: 0.75rem;
  border-bottom: 2px solid var(--orange);
  margin-bottom: 1rem;
}

.brand {
  display: flex;
  align-items: center;
  gap: 0.5rem;
}

.brand h1 {
  font-size: 1.25rem;
  font-weight: 700;
  letter-spacing: -0.02em;
}

.brand-icon {
  flex-shrink: 0;
}

.header-actions {
  display: flex;
  align-items: center;
  gap: 0.25rem;
}

.icon-btn {
  background: none;
  border: none;
  color: var(--text);
  cursor: pointer;
  padding: 0.4rem;
  border-radius: 6px;
  display: flex;
  align-items: center;
  justify-content: center;
}
.icon-btn:hover {
  background: var(--bg-input);
}

/* Refresh button spin */
.icon-btn.refreshing svg {
  animation: spin 0.8s linear infinite;
}
@keyframes spin {
  from { transform: rotate(0deg); }
  to { transform: rotate(360deg); }
}

/* --- Offline Banner --- */
.offline-banner {
  background: var(--orange);
  color: #000;
  text-align: center;
  padding: 0.4rem 1rem;
  border-radius: 6px;
  font-size: 0.85rem;
  font-weight: 600;
  margin-bottom: 1rem;
  display: flex;
  justify-content: space-between;
}
.offline-banner.hidden { display: none; }

/* --- Main Grid --- */
.main-grid {
  flex: 1;
  display: grid;
  gap: 1rem;
  grid-template-columns: 1fr;
}

@media (min-width: 700px) {
  .main-grid {
    grid-template-columns: 1.4fr 1fr;
    align-content: start;
  }
}

/* --- Hero Countdown --- */
.hero {
  background: var(--bg-card);
  border-radius: 12px;
  padding: 2rem 1.5rem;
  text-align: center;
  box-shadow: 0 2px 12px var(--shadow);
  border-left: 5px solid var(--gray);
  transition: border-color 0.3s;
}

.hero[data-status="PLENTY_OF_TIME"] { border-left-color: var(--green); }
.hero[data-status="LEAVE_SOON"]     { border-left-color: var(--yellow); }
.hero[data-status="GO_NOW"]         { border-left-color: var(--red); }
.hero[data-status="NO_TRAINS"]      { border-left-color: var(--gray); }

.hero-label {
  font-size: 0.9rem;
  text-transform: uppercase;
  letter-spacing: 0.1em;
  color: var(--text-muted);
  margin-bottom: 0.25rem;
}

.hero-time {
  font-size: clamp(2rem, 8vw, 5rem);
  font-weight: 800;
  line-height: 1.1;
  font-variant-numeric: tabular-nums;
}

.hero[data-status="PLENTY_OF_TIME"] .hero-time { color: var(--green); }
.hero[data-status="LEAVE_SOON"] .hero-time     { color: var(--yellow); }
.hero[data-status="GO_NOW"] .hero-time         { color: var(--red); }
.hero[data-status="NO_TRAINS"] .hero-time      { color: var(--gray); }

.hero[data-status="GO_NOW"] {
  animation: pulse 1s ease-in-out infinite;
}

@keyframes pulse {
  0%, 100% { box-shadow: 0 2px 12px var(--shadow); }
  50%      { box-shadow: 0 2px 24px rgba(231,76,60,0.5); }
}

.hero-sub {
  font-size: 0.9rem;
  color: var(--text-muted);
  margin-top: 0.5rem;
}

/* --- Train Tracker Strip Map --- */
.tracker-section {
  background: var(--bg-card);
  border-radius: 12px;
  padding: 1rem 1.25rem;
  box-shadow: 0 2px 12px var(--shadow);
}

.tracker-strip {
  position: relative;
  height: 60px;
  margin: 0.5rem 1rem;
}

.tracker-rail {
  position: absolute;
  top: 20px;
  left: 0;
  right: 0;
  height: 4px;
  background: var(--border);
  border-radius: 2px;
}

.tracker-active-segment {
  position: absolute;
  top: 0;
  height: 100%;
  background: var(--orange);
  border-radius: 2px;
  transition: left 0.6s ease, width 0.6s ease;
}

.tracker-stop-dot {
  position: absolute;
  top: 14px;
  width: 14px;
  height: 14px;
  border-radius: 50%;
  background: var(--border);
  border: 2px solid var(--bg-card);
  transform: translateX(-50%);
  z-index: 2;
  transition: left 0.6s ease;
}

.tracker-stop-dot.user-stop {
  width: 18px;
  height: 18px;
  top: 12px;
  background: var(--orange);
  box-shadow: 0 0 8px rgba(237, 139, 0, 0.5);
}

.tracker-train-dot {
  position: absolute;
  top: 15px;
  width: 12px;
  height: 12px;
  border-radius: 50%;
  background: var(--text-muted);
  border: 2px solid var(--bg-card);
  transform: translateX(-50%);
  z-index: 3;
  transition: left 0.6s ease;
}

.tracker-train-dot.target-train {
  width: 16px;
  height: 16px;
  top: 13px;
  background: var(--orange);
  animation: tracker-pulse 1.5s ease-in-out infinite;
}

@keyframes tracker-pulse {
  0%, 100% { box-shadow: 0 0 0 0 rgba(237, 139, 0, 0.4); }
  50% { box-shadow: 0 0 0 6px rgba(237, 139, 0, 0); }
}

.tracker-label {
  position: absolute;
  top: 36px;
  transform: translateX(-50%);
  font-size: 0.65rem;
  color: var(--text-muted);
  white-space: nowrap;
  transition: left 0.6s ease;
}

.tracker-label.user-label {
  color: var(--orange);
  font-weight: 700;
  font-size: 0.7rem;
}

.tracker-label.train-label {
  top: -2px;
  font-size: 0.6rem;
  color: var(--orange);
  font-weight: 600;
}

.tracker-summary {
  text-align: center;
  font-size: 0.8rem;
  color: var(--text-muted);
  margin-top: 0.25rem;
  min-height: 1.2em;
}

.tracker-summary .tracker-highlight {
  color: var(--orange);
  font-weight: 600;
}

.tracker-section.no-trains .tracker-strip {
  opacity: 0.4;
}

@media (max-width: 699px) {
  .tracker-strip {
    margin: 0.25rem 0;
  }

  .tracker-stop-dot {
    width: 10px;
    height: 10px;
    top: 16px;
  }

  .tracker-stop-dot.user-stop {
    width: 14px;
    height: 14px;
    top: 14px;
  }

  .tracker-train-dot {
    width: 10px;
    height: 10px;
    top: 16px;
  }

  .tracker-train-dot.target-train {
    width: 14px;
    height: 14px;
    top: 14px;
  }

  .tracker-label {
    font-size: 0.6rem;
  }
}

@media (prefers-reduced-motion: reduce) {
  .tracker-train-dot.target-train {
    animation: none;
  }

  .tracker-active-segment,
  .tracker-stop-dot,
  .tracker-train-dot,
  .tracker-label {
    transition: none;
  }
}

/* --- Trains Table --- */
.trains-section, .alerts-section {
  background: var(--bg-card);
  border-radius: 12px;
  padding: 1rem 1.25rem;
  box-shadow: 0 2px 12px var(--shadow);
}

.section-title {
  font-size: 0.85rem;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--text-muted);
  margin-bottom: 0.75rem;
}

.trains-table-wrap {
  overflow-x: auto;
}

.trains-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.9rem;
}

.trains-table th {
  text-align: left;
  font-weight: 600;
  font-size: 0.75rem;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--text-muted);
  padding: 0.4rem 0.5rem;
  border-bottom: 1px solid var(--border);
}

.trains-table td {
  padding: 0.5rem;
  border-bottom: 1px solid var(--border);
}

.trains-table tbody tr:last-child td {
  border-bottom: none;
}

.trains-table .target-row {
  background: rgba(237, 139, 0, 0.1);
  font-weight: 600;
  position: relative;
}

.trains-table .target-row td:first-child {
  border-left: 3px solid var(--orange);
  padding-left: calc(0.5rem - 3px);
}

.trains-table .target-row td:last-child::after {
  content: '\2190';
  margin-left: 0.4rem;
  color: var(--orange);
  font-weight: 700;
}

.empty-row {
  color: var(--text-muted);
  text-align: center;
  padding: 1.5rem 0;
  font-style: italic;
}

/* --- Skeleton Loading --- */
@keyframes shimmer {
  0% { background-position: -200px 0; }
  100% { background-position: 200px 0; }
}

.skeleton-line {
  height: 0.75rem;
  border-radius: 4px;
  background: linear-gradient(90deg, var(--bg-input) 25%, var(--border) 50%, var(--bg-input) 75%);
  background-size: 400px 100%;
  animation: shimmer 1.5s ease-in-out infinite;
  margin-bottom: 0.5rem;
  width: 80%;
}

.skeleton-line.short {
  width: 50%;
  margin-bottom: 0;
}

.skeleton-card {
  padding: 0.75rem 1rem;
  border-radius: 8px;
  background: var(--bg-input);
}

/* --- Alerts --- */
.alerts-count {
  font-size: 0.7rem;
  background: var(--orange);
  color: #000;
  border-radius: 10px;
  padding: 0.1rem 0.45rem;
  font-weight: 700;
  vertical-align: middle;
  margin-left: 0.35rem;
}

.alerts-count:empty {
  display: none;
}

.alert-card {
  border-left: 4px solid var(--orange);
  padding: 0.75rem 1rem;
  margin-bottom: 0.75rem;
  border-radius: 0 8px 8px 0;
  background: var(--bg-input);
  cursor: pointer;
}

.alert-card:last-child { margin-bottom: 0; }

.alert-card[data-severity="high"] { border-left-color: var(--red); }
.alert-card[data-severity="medium"] { border-left-color: var(--yellow); }
.alert-card[data-severity="low"] { border-left-color: var(--green); }

.alert-header {
  font-weight: 600;
  font-size: 0.85rem;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.alert-effect {
  font-size: 0.7rem;
  text-transform: uppercase;
  color: var(--text-muted);
  letter-spacing: 0.05em;
}

.alert-desc {
  font-size: 0.8rem;
  color: var(--text-muted);
  margin-top: 0.5rem;
  display: none;
  line-height: 1.4;
}

.alert-card.expanded .alert-desc {
  display: block;
}

.alert-toggle {
  font-size: 0.7rem;
  color: var(--text-muted);
  margin-left: 0.5rem;
}

.alert-card.expanded .alert-toggle::after { content: '\25B2'; }
.alert-card:not(.expanded) .alert-toggle::after { content: '\25BC'; }

/* --- Footer --- */
.footer {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding-top: 0.75rem;
  margin-top: 1rem;
  border-top: 1px solid var(--border);
  font-size: 0.75rem;
  color: var(--text-muted);
}

/* --- Overlays --- */
.overlay {
  position: fixed;
  inset: 0;
  z-index: 1000;
  background: rgba(0,0,0,0.8);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 1rem;
}
.overlay.hidden { display: none; }

.overlay-card {
  background: var(--bg-card);
  border-radius: 12px;
  padding: 2rem;
  max-width: 400px;
  width: 100%;
  text-align: center;
}

.overlay-card h2 {
  margin-bottom: 0.75rem;
  color: var(--orange);
}

.overlay-card p {
  font-size: 0.9rem;
  color: var(--text-muted);
  margin-bottom: 1.25rem;
  line-height: 1.5;
}

.overlay-card input[type="text"],
.overlay-card input[type="password"] {
  width: 100%;
  padding: 0.6rem 0.8rem;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--bg-input);
  color: var(--text);
  font-size: 0.95rem;
  margin-bottom: 1rem;
}

.overlay-card select {
  width: 100%;
  padding: 0.6rem 0.8rem;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--bg-input);
  color: var(--text);
  font-size: 0.95rem;
  appearance: none;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='12' viewBox='0 0 12 12'%3E%3Cpath fill='%23888' d='M6 8L1 3h10z'/%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: right 0.75rem center;
  cursor: pointer;
}

.overlay-card select:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

.overlay-card .route-picker {
  text-align: left;
  margin-bottom: 1rem;
}

.overlay-card button:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

.overlay-card button {
  background: var(--orange);
  color: #fff;
  border: none;
  padding: 0.6rem 2rem;
  border-radius: 8px;
  font-size: 1rem;
  font-weight: 600;
  cursor: pointer;
}
.overlay-card button:hover { background: var(--orange-dim); }

/* --- Settings Panel --- */
.settings-backdrop {
  position: fixed;
  inset: 0;
  z-index: 900;
  background: rgba(0,0,0,0.5);
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.2s;
}
.settings-backdrop.open {
  opacity: 1;
  pointer-events: auto;
}

.settings-panel {
  position: fixed;
  top: 0;
  right: 0;
  bottom: 0;
  width: 320px;
  max-width: 90vw;
  z-index: 950;
  background: var(--bg-card);
  box-shadow: -4px 0 20px var(--shadow);
  transform: translateX(100%);
  transition: transform 0.25s ease;
  display: flex;
  flex-direction: column;
}
.settings-panel.open {
  transform: translateX(0);
}

.settings-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 1rem 1.25rem;
  border-bottom: 1px solid var(--border);
}

.settings-header h2 { font-size: 1.1rem; }
.settings-header .icon-btn { font-size: 1.5rem; }

.settings-body {
  padding: 1.25rem;
  display: flex;
  flex-direction: column;
  gap: 1.25rem;
  overflow-y: auto;
}

.settings-body label {
  display: flex;
  flex-direction: column;
  gap: 0.4rem;
  font-size: 0.85rem;
}

.settings-body label span {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
}

.settings-body input[type="text"],
.settings-body input[type="password"] {
  padding: 0.5rem 0.7rem;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--bg-input);
  color: var(--text);
  font-size: 0.9rem;
}

.settings-body input[type="range"] {
  accent-color: var(--orange);
  width: 100%;
}

/* --- Route Picker --- */
.route-picker {
  border: none;
  padding: 0;
  margin: 0;
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
}

.route-picker legend {
  font-size: 0.85rem;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--text-muted);
  margin-bottom: 0.25rem;
  font-weight: 600;
}

.settings-body select {
  padding: 0.5rem 0.7rem;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--bg-input);
  color: var(--text);
  font-size: 0.9rem;
  appearance: none;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='12' viewBox='0 0 12 12'%3E%3Cpath fill='%23888' d='M6 8L1 3h10z'/%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: right 0.6rem center;
  cursor: pointer;
  width: 100%;
}

.settings-body select:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

.range-val {
  font-weight: 600;
  color: var(--orange);
  font-size: 0.8rem;
}

.toggle-label {
  flex-direction: row !important;
  justify-content: space-between;
  align-items: center;
}

.toggle-label input[type="checkbox"] {
  accent-color: var(--orange);
  width: 18px;
  height: 18px;
}

.setting-help {
  font-size: 0.75rem;
  color: var(--text-muted);
  margin-top: -0.5rem;
  min-height: 0;
}

.setting-help:empty {
  display: none;
}

/* --- Mobile: alerts accordion + full-screen settings --- */
@media (max-width: 699px) {
  .settings-panel {
    width: 100vw;
    max-width: 100vw;
  }

  .alerts-section .section-title {
    cursor: pointer;
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 0;
    user-select: none;
  }

  .alerts-section .section-title::after {
    content: '\25BC';
    font-size: 0.6rem;
    color: var(--text-muted);
    transition: transform 0.2s;
  }

  .alerts-section.expanded .section-title {
    margin-bottom: 0.75rem;
  }

  .alerts-section.expanded .section-title::after {
    content: '\25B2';
  }

  .alerts-section:not(.expanded) #alerts-container {
    display: none;
  }
}

/* --- Pi / Desktop tweaks --- */
@media (min-width: 700px) {
  .app {
    padding: 1.25rem 2rem;
  }

  .col-left {
    display: flex;
    flex-direction: column;
    gap: 1rem;
  }

  .col-right {
    display: flex;
    flex-direction: column;
    gap: 1rem;
  }
}
