/* =========================================================================
   CabSee Companion — styles.css
   Mobile-first, dark-mode aware (prefers-color-scheme), system font stack.
   No JS-injected styles: every visual is keyed off ids/classes/data-attrs
   from the frozen interface. map.js emits the .cs-marker divIcon html and
   sets --heading + data-status; this file does all the painting.
   ========================================================================= */

/* ---- Design tokens -------------------------------------------------------
   Light defaults; dark overrides live in the prefers-color-scheme block.
   Status hexes are the frozen contract values and never change by theme. */
:root {
  /* Frozen status colors (must match STATUS_COLORS in config.js) */
  --status-idle: #3cae50;
  --status-onTrip: #f5a623;
  --status-offDuty: #888a8c;

  /* Surfaces & text (light) */
  --bg: #f2f3f5;
  --surface: #ffffff;
  --surface-2: #f7f8fa;
  --text: #15181c;
  --text-dim: #5c636e;
  --text-faint: #8b929c;
  --border: rgba(20, 24, 30, 0.1);
  --border-strong: rgba(20, 24, 30, 0.16);

  /* Accent (interactive) */
  --accent: #2f6df6;
  --accent-text: #ffffff;
  --danger: #d9434e;

  /* Glass: translucent layer + blur for badge / sheet / tab bar */
  --glass-bg: rgba(255, 255, 255, 0.66);
  --glass-border: rgba(255, 255, 255, 0.55);
  --glass-blur: saturate(180%) blur(18px);
  --shadow-lg: 0 8px 30px rgba(15, 20, 30, 0.16);
  --shadow-sm: 0 2px 10px rgba(15, 20, 30, 0.1);

  /* Geometry */
  --radius: 16px;
  --radius-sm: 10px;
  --tap: 44px; /* minimum accessible hit target */

  /* iOS safe-area insets (0 on non-notched / non-iOS) */
  --sat: env(safe-area-inset-top, 0px);
  --sar: env(safe-area-inset-right, 0px);
  --sab: env(safe-area-inset-bottom, 0px);
  --sal: env(safe-area-inset-left, 0px);

  /* Layout heights used to keep map controls clear of chrome */
  --tabbar-h: 56px;
  --statusbar-h: 56px;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0c0e11;
    --surface: #16191e;
    --surface-2: #1d2127;
    --text: #f3f5f7;
    --text-dim: #aab1bc;
    --text-faint: #717a86;
    --border: rgba(255, 255, 255, 0.1);
    --border-strong: rgba(255, 255, 255, 0.18);

    --accent: #5a8bff;
    --accent-text: #0c0e11;
    --danger: #ff6b73;

    --glass-bg: rgba(22, 25, 30, 0.62);
    --glass-border: rgba(255, 255, 255, 0.12);
    --glass-blur: saturate(160%) blur(18px);
    --shadow-lg: 0 10px 34px rgba(0, 0, 0, 0.5);
    --shadow-sm: 0 2px 12px rgba(0, 0, 0, 0.4);
  }
}

/* ---- Reset / base -------------------------------------------------------- */
*,
*::before,
*::after {
  box-sizing: border-box;
}

html,
body {
  height: 100%;
}

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", system-ui, Roboto,
    Helvetica, Arial, sans-serif;
  font-size: 16px;
  line-height: 1.4;
  -webkit-font-smoothing: antialiased;
  text-rendering: optimizeLegibility;
  /* Lock the app to the viewport; the map scrolls/zooms internally. */
  overflow: hidden;
  overscroll-behavior: none;
  -webkit-tap-highlight-color: transparent;
}

button {
  font: inherit;
  color: inherit;
  cursor: pointer;
}

/* Shared focus ring for keyboard accessibility. */
:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
  border-radius: 8px;
}

/* Generic hidden toggle (app.js flips .hidden on screens/panels). */
.hidden {
  display: none !important;
}

/* =========================================================================
   PAIRING SCREEN
   ========================================================================= */
#pairing-screen {
  position: fixed;
  inset: 0;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  gap: 22px;
  /* Respect notches + give breathing room. */
  padding: calc(var(--sat) + 28px) calc(var(--sar) + 24px)
    calc(var(--sab) + 28px) calc(var(--sal) + 24px);
  text-align: center;
  background: radial-gradient(
      130% 90% at 50% -10%,
      color-mix(in srgb, var(--accent) 16%, var(--bg)),
      var(--bg) 60%
    );
}

#pairing-screen h1 {
  margin: 0;
  font-size: 1.65rem;
  font-weight: 700;
  letter-spacing: -0.02em;
}

#pairing-screen p {
  margin: 0;
  max-width: 30ch;
  color: var(--text-dim);
  font-size: 1rem;
}

/* Large monospaced code field. Big tracking reads as discrete digits. */
#code-input {
  width: min(320px, 80vw);
  padding: 18px 16px;
  border: 1.5px solid var(--border-strong);
  border-radius: var(--radius);
  background: var(--surface);
  color: var(--text);
  font-family: ui-monospace, "SF Mono", SFMono-Regular, Menlo, Consolas,
    "Liberation Mono", monospace;
  font-size: 2rem;
  font-weight: 600;
  letter-spacing: 0.42em;
  /* letter-spacing pushes content right; nudge so it optically centers. */
  text-indent: 0.42em;
  text-align: center;
  caret-color: var(--accent);
  transition: border-color 0.15s ease, box-shadow 0.15s ease;
}

#code-input::placeholder {
  color: var(--text-faint);
  letter-spacing: 0.12em;
  text-indent: 0;
  font-weight: 500;
}

#code-input:focus {
  outline: none;
  border-color: var(--accent);
  box-shadow: 0 0 0 4px color-mix(in srgb, var(--accent) 22%, transparent);
}

/* Error message under the field; reserve no space until populated. */
#pair-error {
  min-height: 1.2em;
  margin: 0;
  color: var(--danger);
  font-size: 0.9rem;
  font-weight: 500;
}

#connect-btn {
  width: min(320px, 80vw);
  min-height: var(--tap);
  padding: 15px 20px;
  border: none;
  border-radius: var(--radius);
  background: var(--accent);
  color: var(--accent-text);
  font-size: 1.05rem;
  font-weight: 700;
  box-shadow: var(--shadow-sm);
  transition: transform 0.08s ease, filter 0.15s ease, opacity 0.15s ease;
}

#connect-btn:active {
  transform: scale(0.98);
  filter: brightness(0.95);
}

#connect-btn:disabled {
  opacity: 0.55;
  cursor: default;
}

/* =========================================================================
   APP SHELL — full-screen map + floating chrome + bottom tab bar
   ========================================================================= */
#app-shell {
  position: fixed;
  inset: 0;
  display: flex;
  flex-direction: column;
}

/* Tab panels fill everything above the tab bar. */
[data-tab-panel] {
  position: absolute;
  inset: 0 0 calc(var(--tabbar-h) + var(--sab)) 0;
  overflow: hidden;
}

/* History / settings scroll; live (map) does not. */
[data-tab-panel="history"],
[data-tab-panel="settings"] {
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  background: var(--bg);
  padding: calc(var(--sat) + 16px) calc(var(--sar) + 16px) 24px
    calc(var(--sal) + 16px);
}

/* ---- Map (full-screen) --------------------------------------------------- */
#map {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  background: var(--surface-2);
  z-index: 0;
}

/* Subtle Leaflet attribution that doesn't fight the dark theme. */
.leaflet-control-attribution {
  background: var(--glass-bg) !important;
  color: var(--text-faint) !important;
  backdrop-filter: var(--glass-blur);
  -webkit-backdrop-filter: var(--glass-blur);
}
.leaflet-control-attribution a {
  color: var(--text-dim) !important;
}

/* In dark mode, gently tone the raster tiles toward the UI palette. */
@media (prefers-color-scheme: dark) {
  .leaflet-tile {
    filter: brightness(0.78) contrast(1.05) saturate(0.9);
  }
}

/* =========================================================================
   STATUS BADGE — glassmorphic, breathing animation keyed off [data-status]
   ========================================================================= */
#status-badge {
  position: absolute;
  top: calc(var(--sat) + 12px);
  left: 50%;
  transform: translateX(-50%);
  z-index: 600; /* above Leaflet panes/controls */

  display: flex;
  align-items: center;
  gap: 10px;
  max-width: calc(100vw - var(--sal) - var(--sar) - 24px);
  padding: 9px 16px;
  border-radius: 999px;
  border: 1px solid var(--glass-border);
  background: var(--glass-bg);
  backdrop-filter: var(--glass-blur);
  -webkit-backdrop-filter: var(--glass-blur);
  box-shadow: var(--shadow-lg);
  /* The "breathing" glow — color injected per-status below. */
  animation: badge-breathe 3.4s ease-in-out infinite;
}

/* Status dot mirrors the live status color; itself pulses. */
#status-dot {
  flex: 0 0 auto;
  width: 11px;
  height: 11px;
  border-radius: 50%;
  background: var(--status-offDuty);
  box-shadow: 0 0 0 0 currentColor;
}

#status-text {
  font-weight: 700;
  font-size: 0.95rem;
  letter-spacing: -0.01em;
  white-space: nowrap;
}

#status-updated {
  color: var(--text-dim);
  font-size: 0.8rem;
  white-space: nowrap;
}

/* --- Per-status theming. The badge carries data-status; we set a local
   --sc (status color) used by the breathing glow and the dot pulse. --- */
#status-badge[data-status="idle"] {
  --sc: var(--status-idle);
}
#status-badge[data-status="onTrip"] {
  --sc: var(--status-onTrip);
}
#status-badge[data-status="offDuty"] {
  --sc: var(--status-offDuty);
}

#status-badge[data-status="idle"] #status-dot {
  background: var(--status-idle);
  color: var(--status-idle);
}
#status-badge[data-status="onTrip"] #status-dot {
  background: var(--status-onTrip);
  color: var(--status-onTrip);
}
#status-badge[data-status="offDuty"] #status-dot {
  background: var(--status-offDuty);
  color: var(--status-offDuty);
}

/* Dot pulse: lively for active states, calm when off duty. */
#status-badge[data-status="idle"] #status-dot,
#status-badge[data-status="onTrip"] #status-dot {
  animation: dot-pulse 1.9s ease-out infinite;
}
#status-badge[data-status="offDuty"] #status-dot {
  animation: dot-pulse 3.4s ease-out infinite;
}

/* Glow ring breathes using the current status color (falls back to dim). */
@keyframes badge-breathe {
  0%,
  100% {
    box-shadow: var(--shadow-lg),
      0 0 0 0 color-mix(in srgb, var(--sc, var(--text-faint)) 38%, transparent);
  }
  50% {
    box-shadow: var(--shadow-lg),
      0 0 18px 3px
        color-mix(in srgb, var(--sc, var(--text-faint)) 30%, transparent);
  }
}

@keyframes dot-pulse {
  0% {
    box-shadow: 0 0 0 0 color-mix(in srgb, currentColor 55%, transparent);
  }
  70% {
    box-shadow: 0 0 0 9px color-mix(in srgb, currentColor 0%, transparent);
  }
  100% {
    box-shadow: 0 0 0 0 color-mix(in srgb, currentColor 0%, transparent);
  }
}

/* =========================================================================
   RECENTER BUTTON — floating, sits above the bottom sheet
   ========================================================================= */
#recenter-btn {
  position: absolute;
  right: calc(var(--sar) + 16px);
  /* Clear the bottom sheet's collapsed height + tab bar. */
  bottom: calc(var(--tabbar-h) + var(--sab) + 150px);
  z-index: 600;

  width: var(--tap);
  height: var(--tap);
  display: grid;
  place-items: center;
  border: 1px solid var(--glass-border);
  border-radius: 50%;
  background: var(--glass-bg);
  backdrop-filter: var(--glass-blur);
  -webkit-backdrop-filter: var(--glass-blur);
  box-shadow: var(--shadow-lg);
  color: var(--text);
  font-size: 1.25rem;
  transition: transform 0.08s ease;
}

#recenter-btn:active {
  transform: scale(0.92);
}

/* =========================================================================
   BOTTOM SHEET — glassmorphic stats + check-in, docked above the tab bar
   ========================================================================= */
.bottom-sheet {
  position: absolute;
  left: calc(var(--sal) + 10px);
  right: calc(var(--sar) + 10px);
  bottom: calc(var(--tabbar-h) + var(--sab) + 10px);
  z-index: 500;

  padding: 16px 18px;
  border-radius: 22px;
  border: 1px solid var(--glass-border);
  background: var(--glass-bg);
  backdrop-filter: var(--glass-blur);
  -webkit-backdrop-filter: var(--glass-blur);
  box-shadow: var(--shadow-lg);
}

/* Stat row: three equal columns (Trips / km / Duration). */
.stats {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 4px;
  margin-bottom: 14px;
}

.stat {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
  text-align: center;
}

.stat + .stat {
  border-left: 1px solid var(--border);
}

/* Large numbers are tabular so they don't jitter as values update. */
#stat-trips,
#stat-km,
#stat-duration {
  font-size: 1.5rem;
  font-weight: 700;
  letter-spacing: -0.02em;
  font-variant-numeric: tabular-nums;
  font-feature-settings: "tnum" 1;
}

.stat__label {
  font-size: 0.72rem;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--text-dim);
}

/* Check-in button: full width, prominent. */
#checkin-btn {
  width: 100%;
  min-height: var(--tap);
  padding: 13px 18px;
  border: none;
  border-radius: 14px;
  background: var(--accent);
  color: var(--accent-text);
  font-size: 1rem;
  font-weight: 700;
  transition: transform 0.08s ease, opacity 0.2s ease, filter 0.15s ease;
}

#checkin-btn:active {
  transform: scale(0.98);
}

/* Cooldown: app.js disables the button for CHECKIN_COOLDOWN_MS. */
#checkin-btn:disabled {
  opacity: 0.5;
  cursor: default;
}

/* Confirmation line ("Sent {x}"). Reserve no height when empty. */
#checkin-result {
  min-height: 1.1em;
  margin-top: 8px;
  text-align: center;
  font-size: 0.85rem;
  font-weight: 600;
  color: var(--status-idle);
}

/* =========================================================================
   MAP WAITING OVERLAY — shown until hasAutoCentered()
   ========================================================================= */
#map-waiting {
  position: absolute;
  inset: 0;
  z-index: 400; /* above map, below badge/sheet/recenter */
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 16px;
  padding: 24px;
  text-align: center;
  color: var(--text-dim);
  background: color-mix(in srgb, var(--bg) 72%, transparent);
  backdrop-filter: blur(3px);
  -webkit-backdrop-filter: blur(3px);
}

/* Spinner — pure CSS, no asset needed. */
#map-waiting::before {
  content: "";
  width: 34px;
  height: 34px;
  border-radius: 50%;
  border: 3px solid var(--border-strong);
  border-top-color: var(--accent);
  animation: spin 0.9s linear infinite;
}

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

/* =========================================================================
   HISTORY TAB
   ========================================================================= */
#history-list {
  display: flex;
  flex-direction: column;
  gap: 10px;
  max-width: 640px;
  margin: 0 auto;
  list-style: none;
  padding: 0;
}

/* Each trip row: app.js appends [time, route, km] as flat spans. */
#history-list .history-row,
#history-list > li {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 14px 16px;
  border-radius: var(--radius);
  background: var(--surface);
  border: 1px solid var(--border);
  box-shadow: var(--shadow-sm);
}

.history-row__time {
  flex: 0 0 auto;
  font-size: 0.82rem;
  color: var(--text-dim);
  font-variant-numeric: tabular-nums;
  font-feature-settings: "tnum" 1;
}

.history-row__route {
  flex: 1 1 auto;
  min-width: 0;
  font-weight: 600;
  font-size: 0.98rem;
  /* Long origin → dest strings truncate gracefully. */
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.history-row__km {
  flex: 0 0 auto;
  text-align: right;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
  font-feature-settings: "tnum" 1;
}

/* Empty state ("No trips yet today"). */
.history-empty,
#history-list .history-empty {
  margin: 48px auto 0;
  max-width: 26ch;
  text-align: center;
  color: var(--text-faint);
  font-size: 0.95rem;
  list-style: none;
}

/* =========================================================================
   SETTINGS TAB
   ========================================================================= */
[data-tab-panel="settings"] {
  display: flex;
  flex-direction: column;
}

#connection-state {
  max-width: 640px;
  width: 100%;
  margin: 0 auto 18px;
  padding: 16px 18px;
  border-radius: var(--radius);
  background: var(--surface);
  border: 1px solid var(--border);
  box-shadow: var(--shadow-sm);
  font-weight: 600;
  display: flex;
  align-items: center;
  gap: 10px;
}

/* Optional leading dot if app.js sets a data-state; harmless if unused. */
#connection-state::before {
  content: "";
  width: 9px;
  height: 9px;
  border-radius: 50%;
  background: var(--status-idle);
  flex: 0 0 auto;
}
#connection-state[data-state="notPaired"]::before {
  background: var(--text-faint);
}

#unpair-btn {
  max-width: 640px;
  width: 100%;
  margin: 0 auto;
  min-height: var(--tap);
  padding: 14px 18px;
  border: 1px solid color-mix(in srgb, var(--danger) 45%, transparent);
  border-radius: var(--radius);
  background: color-mix(in srgb, var(--danger) 10%, var(--surface));
  color: var(--danger);
  font-size: 1rem;
  font-weight: 700;
  transition: transform 0.08s ease, filter 0.15s ease;
}

#unpair-btn:active {
  transform: scale(0.99);
  filter: brightness(0.97);
}

/* =========================================================================
   BOTTOM TAB BAR — glassmorphic, fixed, safe-area aware
   ========================================================================= */
.tab-bar,
#tab-bar {
  position: absolute;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 700; /* above sheet + map chrome */

  display: flex;
  height: calc(var(--tabbar-h) + var(--sab));
  padding-bottom: var(--sab);
  padding-left: var(--sal);
  padding-right: var(--sar);

  border-top: 1px solid var(--glass-border);
  background: var(--glass-bg);
  backdrop-filter: var(--glass-blur);
  -webkit-backdrop-filter: var(--glass-blur);
}

/* Tab buttons are identified by [data-tab]; app.js toggles .active / aria. */
[data-tab] {
  flex: 1 1 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 3px;
  min-height: var(--tap);
  padding: 6px 4px;
  border: none;
  background: transparent;
  color: var(--text-faint);
  font-size: 0.72rem;
  font-weight: 600;
  letter-spacing: 0.01em;
  transition: color 0.15s ease;
}

/* Active tab — supports either .active class or aria-selected. */
[data-tab].active,
[data-tab][aria-selected="true"],
[data-tab][aria-current="page"] {
  color: var(--accent);
}

[data-tab] svg,
[data-tab] .tab__icon {
  width: 24px;
  height: 24px;
}

[data-tab]:active {
  opacity: 0.7;
}

/* =========================================================================
   DRIVER MARKER — class contract emitted verbatim by map.js
   Structure (do not change):
     .cs-marker  (Leaflet wrapper, 44x44, anchor 22,22)
       .cs-marker__inner .cs-marker--<status> [.cs-marker--moving]
                         data-status=<status> style="--heading:<deg>deg"
         .cs-marker__arrow   <- rotated by var(--heading), shown when moving
         .cs-marker__dot     <- shown when NOT moving
         .cs-marker__pulse   <- expanding pulse ring
   ========================================================================= */

/* Leaflet sets position on .cs-marker; we only need a clean stacking box. */
.cs-marker {
  background: transparent;
  border: none;
}

.cs-marker__inner {
  position: relative;
  width: 44px;
  height: 44px;
  /* Status color is resolved per modifier into --mc (marker color). */
  --mc: var(--status-offDuty);
  color: var(--mc);
}

/* Map status modifiers to the frozen hexes. */
.cs-marker--idle {
  --mc: var(--status-idle);
}
.cs-marker--onTrip {
  --mc: var(--status-onTrip);
}
.cs-marker--offDuty {
  --mc: var(--status-offDuty);
}

/* --- Pulse ring: always present, expands and fades from the center. --- */
.cs-marker__pulse {
  position: absolute;
  top: 50%;
  left: 50%;
  width: 20px;
  height: 20px;
  margin: -10px 0 0 -10px;
  border-radius: 50%;
  background: var(--mc);
  opacity: 0.45;
  /* Sits behind dot/arrow. */
  z-index: 0;
  animation: marker-pulse 2.2s ease-out infinite;
  pointer-events: none;
}

@keyframes marker-pulse {
  0% {
    transform: scale(0.6);
    opacity: 0.5;
  }
  70% {
    opacity: 0;
  }
  100% {
    transform: scale(2.6);
    opacity: 0;
  }
}

/* --- Dot: shown when stationary (default state). --- */
.cs-marker__dot {
  position: absolute;
  top: 50%;
  left: 50%;
  width: 18px;
  height: 18px;
  margin: -9px 0 0 -9px;
  border-radius: 50%;
  background: var(--mc);
  border: 3px solid #fff;
  box-shadow: 0 1px 4px rgba(0, 0, 0, 0.4);
  z-index: 2;
}

/* --- Arrow: shown when moving; rotated to the heading via --heading. ---
   A CSS triangle pointing "up" (north) by default; --heading rotates it
   clockwise to match compass bearing (0deg = north). */
.cs-marker__arrow {
  position: absolute;
  top: 50%;
  left: 50%;
  width: 0;
  height: 0;
  /* Triangle: 11px half-width base, 18px tall, color = marker color. */
  border-left: 11px solid transparent;
  border-right: 11px solid transparent;
  border-bottom: 20px solid var(--mc);
  /* Anchor the triangle's centroid at the marker center, then rotate. */
  margin: -13px 0 0 -11px;
  transform-origin: 11px 13px;
  transform: rotate(var(--heading, 0deg));
  filter: drop-shadow(0 1px 2px rgba(0, 0, 0, 0.45));
  z-index: 2;
  /* Hidden by default; the --moving modifier reveals it. */
  display: none;
}

/* --- Moving vs. stationary swap.
   When .cs-marker--moving is present: show arrow, hide dot.
   Otherwise: show dot, hide arrow (the default above). --- */
.cs-marker--moving .cs-marker__arrow {
  display: block;
}
.cs-marker--moving .cs-marker__dot {
  display: none;
}

/* =========================================================================
   THEME-COLOR META is non-visual (app.js updates #theme-color-meta content);
   no styling needed here.

   REDUCED MOTION — honor user preference: kill looping animations but keep
   layouts intact. Functional state (which element is visible) is unaffected.
   ========================================================================= */
@media (prefers-reduced-motion: reduce) {
  #status-badge,
  #status-dot,
  .cs-marker__pulse,
  #map-waiting::before {
    animation: none !important;
  }
  * {
    transition-duration: 0.001ms !important;
  }
}
