:root {
  --font-family: -apple-system, BlinkMacSystemFont, "SF Pro Text", "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  --font-mono: ui-monospace, "SF Mono", Menlo, Monaco, Consolas, monospace;

  /* Theme colors - Light */
  --bg-primary: #f8fafc;
  --bg-card: #ffffff;
  --bg-subtle: #f1f5f9;
  --bg-elevated: #ffffff;
  --border-color: #e2e8f0;
  --border-focus: #ba0c2f;
  --text-primary: #0f172a;
  --text-secondary: #475569;
  --text-muted: #64748b;
  --shadow-sm: 0 1px 3px rgba(0, 0, 0, 0.06), 0 1px 2px rgba(0, 0, 0, 0.04);
  --shadow-md: 0 4px 12px -2px rgba(0, 0, 0, 0.08), 0 2px 6px -1px rgba(0, 0, 0, 0.04);
  --shadow-lg: 0 12px 28px -4px rgba(0, 0, 0, 0.12), 0 4px 12px -2px rgba(0, 0, 0, 0.06);

  /* SF Muni Brand Colors */
  --muni-red: #ba0c2f;
  --muni-red-hover: #990012;
  --muni-red-light: #fee2e2;
  --muni-rapid: #ba0c2f;
  --muni-local: #2563eb;
  --muni-bus: #059669;
  --status-online: #10b981;
  --status-warning: #f59e0b;
  --status-error: #ef4444;

  --radius-sm: 8px;
  --radius-md: 14px;
  --radius-lg: 20px;
  --radius-pill: 9999px;
  --transition: all 0.2s cubic-bezier(0.4, 0, 0.2, 1);
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg-primary: #090d16;
    --bg-card: #131b2e;
    --bg-subtle: #1e293b;
    --bg-elevated: #1a233a;
    --border-color: #243048;
    --border-focus: #f43f5e;
    --text-primary: #f8fafc;
    --text-secondary: #cbd5e1;
    --text-muted: #94a3b8;
    --shadow-sm: 0 1px 3px rgba(0, 0, 0, 0.4);
    --shadow-md: 0 4px 12px -2px rgba(0, 0, 0, 0.4);
    --shadow-lg: 0 12px 28px -4px rgba(0, 0, 0, 0.5);
    --muni-red-light: rgba(186, 12, 47, 0.2);
  }
}

* {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
  -webkit-tap-highlight-color: transparent;
}

body {
  font-family: var(--font-family);
  background-color: var(--bg-primary);
  color: var(--text-primary);
  min-height: 100vh;
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
  padding-top: env(safe-area-inset-top);
  padding-bottom: env(safe-area-inset-bottom);
}

/* App Wrap */
.app-container {
  max-width: 840px;
  margin: 0 auto;
  padding: 16px 20px 48px;
}

/* Header */
.app-header {
  position: sticky;
  top: 0;
  z-index: 20;
  background-color: var(--bg-primary);
  padding: 12px 0 16px;
  border-bottom: 1px solid var(--border-color);
  margin-bottom: 20px;
  backdrop-filter: blur(12px);
  -webkit-backdrop-filter: blur(12px);
}

.header-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}

.brand-section {
  display: flex;
  align-items: center;
  gap: 12px;
}

.app-logo {
  width: 40px;
  height: 40px;
  border-radius: 10px;
  background: var(--muni-red);
  display: flex;
  align-items: center;
  justify-content: center;
  color: white;
  box-shadow: 0 2px 8px rgba(186, 12, 47, 0.35);
  flex-shrink: 0;
}

.app-logo svg {
  width: 26px;
  height: 26px;
  fill: currentColor;
}

.brand-title {
  font-size: 22px;
  font-weight: 750;
  letter-spacing: -0.02em;
  color: var(--text-primary);
  line-height: 1.2;
}

.brand-subtitle {
  font-size: 13px;
  color: var(--text-muted);
}

.header-actions {
  display: flex;
  align-items: center;
  gap: 8px;
}

.status-badge {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 5px 10px;
  border-radius: var(--radius-pill);
  font-size: 12px;
  font-weight: 550;
  background: var(--bg-subtle);
  color: var(--text-secondary);
  border: 1px solid var(--border-color);
}

.status-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background-color: var(--text-muted);
  transition: background-color 0.3s ease;
}

.status-dot.online {
  background-color: var(--status-online);
  box-shadow: 0 0 0 3px rgba(16, 185, 129, 0.2);
}

.status-dot.updating {
  background-color: var(--muni-red);
  animation: pulse 1s infinite alternate;
}

.status-dot.error {
  background-color: var(--status-error);
}

@keyframes pulse {
  0% { transform: scale(0.9); opacity: 0.6; }
  100% { transform: scale(1.15); opacity: 1; }
}

.icon-button {
  width: 38px;
  height: 38px;
  border-radius: 10px;
  border: 1px solid var(--border-color);
  background: var(--bg-card);
  color: var(--text-secondary);
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  transition: var(--transition);
}

.icon-button:hover {
  background: var(--bg-subtle);
  color: var(--text-primary);
  border-color: var(--text-muted);
}

.icon-button:active {
  transform: scale(0.94);
}

.icon-button svg {
  width: 18px;
  height: 18px;
  stroke: currentColor;
  stroke-width: 2;
  fill: none;
}

.spinning {
  animation: spin 0.8s linear infinite;
}

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

/* Progress bar for auto-refresh */
.refresh-progress-bar {
  position: absolute;
  bottom: 0;
  left: 0;
  right: 0;
  height: 2px;
  background: transparent;
  overflow: hidden;
}

.refresh-progress-fill {
  height: 100%;
  width: 0%;
  background: var(--muni-red);
  transition: width 1s linear;
}

/* Offline notice banner */
.offline-banner {
  background-color: #fffbeb;
  border: 1px solid #fde68a;
  color: #92400e;
  border-radius: var(--radius-md);
  padding: 10px 14px;
  font-size: 13px;
  margin-bottom: 16px;
  display: flex;
  align-items: center;
  gap: 10px;
}

@media (prefers-color-scheme: dark) {
  .offline-banner {
    background-color: rgba(245, 158, 11, 0.12);
    border-color: rgba(245, 158, 11, 0.3);
    color: #fcd34d;
  }
}

.hidden {
  display: none !important;
}

/* Stop Cards Grid */
.cards-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(250px, 1fr));
  gap: 16px;
}

.transit-card {
  background: var(--bg-card);
  border: 1px solid var(--border-color);
  border-radius: var(--radius-lg);
  padding: 20px;
  box-shadow: var(--shadow-sm);
  transition: var(--transition);
  position: relative;
  overflow: hidden;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
}

.transit-card:hover {
  box-shadow: var(--shadow-md);
  border-color: #cbd5e1;
}

@media (prefers-color-scheme: dark) {
  .transit-card:hover {
    border-color: #334155;
  }
}

/* Top bar inside card */
.card-header {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 10px;
  margin-bottom: 12px;
}

.card-title-group {
  display: flex;
  align-items: center;
  gap: 10px;
}

.route-badge {
  font-size: 20px;
  font-weight: 800;
  letter-spacing: -0.02em;
  padding: 4px 10px;
  border-radius: 8px;
  color: white;
  background: var(--muni-red);
  display: inline-block;
  min-width: 52px;
  text-align: center;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.12);
}

.route-badge.rapid {
  background: #ba0c2f;
}

.route-badge.local {
  background: #1e3a8a;
}

.route-badge.bus {
  background: #0f766e;
}

.route-direction {
  font-size: 13px;
  font-weight: 600;
  color: var(--text-secondary);
}

.stop-code-badge {
  font-size: 11px;
  font-family: var(--font-mono);
  background: var(--bg-subtle);
  color: var(--text-muted);
  padding: 2px 7px;
  border-radius: 6px;
  border: 1px solid var(--border-color);
}

.stop-label {
  font-size: 15px;
  font-weight: 600;
  color: var(--text-primary);
  margin-bottom: 16px;
  display: flex;
  align-items: center;
  gap: 6px;
}

.stop-label svg {
  width: 15px;
  height: 15px;
  color: var(--text-muted);
  flex-shrink: 0;
}

/* Primary Arrival section */
.arrival-display {
  display: flex;
  align-items: baseline;
  gap: 8px;
  min-height: 52px;
  margin-bottom: 14px;
}

.arrival-time {
  font-size: 42px;
  font-weight: 800;
  letter-spacing: -0.04em;
  line-height: 1;
  color: var(--text-primary);
}

.arrival-time.due {
  color: var(--muni-red);
  animation: pulse 1.2s infinite alternate;
}

.arrival-time.loading {
  color: var(--text-muted);
  opacity: 0.5;
}

.arrival-time.error {
  font-size: 20px;
  color: var(--status-error);
  font-weight: 600;
}

.arrival-unit {
  font-size: 16px;
  font-weight: 600;
  color: var(--text-muted);
}

.arrival-clock {
  font-size: 13px;
  color: var(--text-muted);
  margin-left: auto;
  align-self: center;
  background: var(--bg-subtle);
  padding: 3px 8px;
  border-radius: var(--radius-sm);
}

/* Next Arrivals list */
.upcoming-arrivals {
  border-top: 1px solid var(--border-color);
  padding-top: 12px;
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 6px;
  min-height: 32px;
}

.upcoming-pill {
  font-size: 12px;
  font-weight: 600;
  padding: 4px 9px;
  background: var(--bg-subtle);
  color: var(--text-secondary);
  border-radius: var(--radius-pill);
  border: 1px solid var(--border-color);
}

.upcoming-none {
  font-size: 12px;
  color: var(--text-muted);
  font-style: italic;
}

/* Modal and Drawers */
.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.55);
  backdrop-filter: blur(4px);
  -webkit-backdrop-filter: blur(4px);
  z-index: 50;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.25s ease;
}

.modal-overlay.active {
  opacity: 1;
  pointer-events: auto;
}

.modal-sheet {
  background: var(--bg-elevated);
  border: 1px solid var(--border-color);
  border-radius: var(--radius-lg);
  max-width: 580px;
  width: 100%;
  max-height: 88vh;
  display: flex;
  flex-direction: column;
  box-shadow: var(--shadow-lg);
  transform: translateY(16px);
  transition: transform 0.25s cubic-bezier(0.3, 1, 0.4, 1);
  overflow: hidden;
}

.modal-overlay.active .modal-sheet {
  transform: translateY(0);
}

.modal-header {
  padding: 16px 20px;
  border-bottom: 1px solid var(--border-color);
  display: flex;
  align-items: center;
  justify-content: space-between;
  background: var(--bg-card);
}

.modal-title {
  font-size: 18px;
  font-weight: 700;
  color: var(--text-primary);
}

.modal-body {
  padding: 20px;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
}

.modal-footer {
  padding: 14px 20px;
  border-top: 1px solid var(--border-color);
  display: flex;
  justify-content: flex-end;
  gap: 10px;
  background: var(--bg-card);
}

/* Settings Form Elements */
.section-title {
  font-size: 13px;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  font-weight: 700;
  color: var(--text-muted);
  margin-bottom: 12px;
  margin-top: 20px;
}

.section-title:first-child {
  margin-top: 0;
}

.form-group {
  margin-bottom: 16px;
}

label {
  display: block;
  font-size: 13px;
  font-weight: 600;
  color: var(--text-secondary);
  margin-bottom: 6px;
}

.input-with-button {
  display: flex;
  gap: 8px;
}

input[type="text"],
input[type="password"],
input[type="number"],
select {
  width: 100%;
  padding: 10px 12px;
  border: 1px solid var(--border-color);
  background: var(--bg-card);
  color: var(--text-primary);
  border-radius: var(--radius-sm);
  font-family: inherit;
  font-size: 14px;
  transition: var(--transition);
}

input:focus,
select:focus {
  outline: none;
  border-color: var(--muni-red);
  box-shadow: 0 0 0 3px rgba(186, 12, 47, 0.15);
}

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  padding: 9px 14px;
  border-radius: var(--radius-sm);
  font-family: inherit;
  font-size: 13px;
  font-weight: 600;
  border: 1px solid var(--border-color);
  background: var(--bg-card);
  color: var(--text-primary);
  cursor: pointer;
  transition: var(--transition);
  white-space: nowrap;
}

.btn:hover {
  background: var(--bg-subtle);
}

.btn:active {
  transform: scale(0.97);
}

.btn-primary {
  background: var(--muni-red);
  color: white;
  border-color: var(--muni-red);
}

.btn-primary:hover {
  background: var(--muni-red-hover);
  border-color: var(--muni-red-hover);
}

.btn-danger {
  color: var(--status-error);
  border-color: rgba(239, 68, 68, 0.3);
}

.btn-danger:hover {
  background: rgba(239, 68, 68, 0.08);
}

.helper-text {
  font-size: 12px;
  color: var(--text-muted);
  line-height: 1.4;
  margin-top: 6px;
}

/* Stop list in settings */
.stops-editor-list {
  display: flex;
  flex-direction: column;
  gap: 8px;
  margin-bottom: 12px;
}

.stop-editor-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 10px 12px;
  background: var(--bg-subtle);
  border: 1px solid var(--border-color);
  border-radius: var(--radius-sm);
}

.stop-editor-info {
  display: flex;
  align-items: center;
  gap: 10px;
}

.stop-editor-route {
  font-weight: 700;
  font-size: 14px;
  padding: 2px 7px;
  border-radius: 6px;
  background: var(--muni-red);
  color: white;
}

.stop-editor-details {
  font-size: 13px;
  color: var(--text-secondary);
}

/* Add stop form */
.add-stop-form {
  background: var(--bg-subtle);
  border: 1px solid var(--border-color);
  border-radius: var(--radius-sm);
  padding: 12px;
  margin-top: 10px;
}

.form-grid-2x2 {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
  margin-bottom: 10px;
}

/* Collapsible Debug Panel */
.debug-panel {
  margin-top: 18px;
  border: 1px solid var(--border-color);
  border-radius: var(--radius-md);
  background: var(--bg-subtle);
  overflow: hidden;
}

.debug-summary {
  padding: 10px 14px;
  cursor: pointer;
  font-weight: 600;
  font-size: 13px;
  color: var(--text-secondary);
  display: flex;
  align-items: center;
  justify-content: space-between;
  user-select: none;
}

.debug-summary:hover {
  background: var(--bg-card);
}

.debug-content {
  padding: 12px;
  border-top: 1px solid var(--border-color);
  background: var(--bg-card);
}

.debug-actions {
  display: flex;
  gap: 8px;
  margin-bottom: 10px;
}

.debug-log-view {
  font-family: var(--font-mono);
  font-size: 11px;
  line-height: 1.45;
  background: #090d16;
  color: #38bdf8;
  padding: 12px;
  border-radius: var(--radius-sm);
  max-height: 260px;
  overflow-y: auto;
  white-space: pre-wrap;
  word-break: break-all;
}

/* Footer */
.app-footer {
  margin-top: 36px;
  text-align: center;
  font-size: 12px;
  color: var(--text-muted);
  display: flex;
  flex-direction: column;
  gap: 4px;
}

/* Responsive adjustments */
@media (max-width: 600px) {
  .app-container {
    padding: 10px 14px 40px;
  }
  .brand-title {
    font-size: 19px;
  }
  .cards-grid {
    grid-template-columns: 1fr;
    gap: 12px;
  }
  .transit-card {
    padding: 16px;
  }
  .arrival-time {
    font-size: 38px;
  }
  .form-grid-2x2 {
    grid-template-columns: 1fr;
  }
}
