:root {
  --bg: #f6f7f9;
  --surface: #ffffff;
  --border: #e4e7ec;
  --text: #1a1d23;
  --muted: #667085;
  --primary: #2f6df6;
  --primary-ink: #ffffff;
  --danger: #d64545;
  --accent: #f97316;
  --shadow: 0 1px 3px rgba(16, 24, 40, .08), 0 1px 2px rgba(16, 24, 40, .04);
  --radius: 12px;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f1216;
    --surface: #171b21;
    --border: #262b33;
    --text: #e7eaee;
    --muted: #99a2b0;
    --primary: #4d86ff;
    --shadow: 0 1px 3px rgba(0, 0, 0, .4);
  }
}

* { box-sizing: border-box; }

body {
  margin: 0;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  background: var(--bg);
  color: var(--text);
  line-height: 1.5;
}

.container { width: min(1040px, 92vw); margin: 0 auto; }

/* Header / footer */
.site-header {
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  position: sticky; top: 0; z-index: 10;
}
.header-inner { display: flex; align-items: center; justify-content: space-between; height: 64px; }
.brand { font-weight: 700; font-size: 1.15rem; text-decoration: none; color: var(--text); }
.nav { display: flex; align-items: center; gap: 1rem; }
.nav a { text-decoration: none; color: var(--muted); font-weight: 500; }
.nav a:hover { color: var(--text); }
.site-footer { margin: 4rem 0 2rem; color: var(--muted); font-size: .85rem; text-align: center; }

main.container { padding: 2rem 0 1rem; }

/* Buttons */
.btn {
  display: inline-block; padding: .55rem 1rem; border-radius: 8px;
  border: 1px solid var(--border); background: var(--surface); color: var(--text);
  font-weight: 600; font-size: .92rem; text-decoration: none; cursor: pointer;
  transition: transform .04s ease, box-shadow .15s ease;
}
.btn:hover { box-shadow: var(--shadow); }
.btn:active { transform: translateY(1px); }
.btn-primary { background: var(--primary); border-color: var(--primary); color: var(--primary-ink); }
.btn-danger { background: transparent; border-color: var(--danger); color: var(--danger); }
.btn-danger:hover { background: var(--danger); color: #fff; }

/* Headings */
.page-head { display: flex; align-items: flex-start; justify-content: space-between; gap: 1rem; margin-bottom: 1.5rem; }
.page-head h1 { margin: 0; font-size: 1.7rem; }
.subtitle { color: var(--muted); margin: .35rem 0 0; }
.back-link { color: var(--muted); text-decoration: none; font-size: .9rem; }
.badge {
  display: inline-block; padding: .1rem .5rem; border-radius: 999px;
  background: color-mix(in srgb, var(--primary) 15%, transparent);
  color: var(--primary); font-size: .72rem; font-weight: 700; letter-spacing: .03em;
}

/* Cards */
.card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); box-shadow: var(--shadow); padding: 1.5rem; margin-bottom: 1.5rem; }

/* Messages */
.messages { list-style: none; padding: 0; margin: 0 0 1.5rem; display: grid; gap: .5rem; }
.message { padding: .7rem 1rem; border-radius: 8px; border: 1px solid var(--border); }
.message-success { background: color-mix(in srgb, #12b76a 12%, transparent); border-color: #12b76a; }
.message-error { background: color-mix(in srgb, var(--danger) 12%, transparent); border-color: var(--danger); }

/* Ride grid */
.ride-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(240px, 1fr)); gap: 1rem; }
.ride-card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); box-shadow: var(--shadow); padding: 1.1rem; text-decoration: none; color: var(--text); transition: transform .08s ease, box-shadow .15s ease; }
.ride-card:hover { transform: translateY(-2px); box-shadow: 0 6px 18px rgba(16, 24, 40, .12); }
.ride-card-head { display: flex; justify-content: space-between; align-items: center; }
.ride-date { color: var(--muted); font-size: .8rem; }
.ride-name { font-size: 1.05rem; margin: .5rem 0 .9rem; }
.ride-stats { display: flex; gap: 1rem; }
.ride-stats div { display: flex; flex-direction: column; }
.ride-stats strong { font-size: 1.15rem; }
.ride-stats span { color: var(--muted); font-size: .72rem; }

/* Stat row (detail) */
.stat-row { display: grid; grid-template-columns: repeat(auto-fit, minmax(140px, 1fr)); gap: 1rem; margin-bottom: 1.5rem; }
.stat { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); box-shadow: var(--shadow); padding: 1rem 1.1rem; text-align: center; }
.stat-value { display: block; font-size: 1.6rem; font-weight: 700; }
.stat-label { color: var(--muted); font-size: .78rem; }

/* Charts */
.chart-title { margin: 0 0 1rem; font-size: 1.1rem; }
.chart-wrap { position: relative; height: 340px; }

/* Forms */
.form .field { margin-bottom: 1.25rem; display: flex; flex-direction: column; }
.form label { font-weight: 600; margin-bottom: .35rem; }
.form input[type=text], .form input[type=file] {
  padding: .6rem .75rem; border: 1px solid var(--border); border-radius: 8px;
  background: var(--bg); color: var(--text); font-size: .95rem;
}
.form small { color: var(--muted); font-size: .82rem; margin-top: .3rem; }
.form-actions { display: flex; gap: .75rem; align-items: center; }
.field-errors, .errorlist { color: var(--danger); font-size: .85rem; margin: .3rem 0 0; padding: 0; list-style: none; }

/* Empty + confirm */
.empty-state { text-align: center; padding: 4rem 1rem; }
.empty-emoji { font-size: 3rem; margin: 0; }
.confirm { max-width: 480px; }
.pagination { display: flex; gap: 1rem; align-items: center; justify-content: center; margin-top: 2rem; color: var(--muted); }
.pagination a { color: var(--primary); text-decoration: none; }

/* Auth */
.nav-user { color: var(--text); font-weight: 600; }
.logout-form { margin: 0; }
.linkish { background: none; border: none; color: var(--muted); font: inherit; cursor: pointer; padding: 0; }
.linkish:hover { color: var(--text); }
.auth-card { max-width: 420px; margin: 1rem auto; }
.auth-alt { color: var(--muted); font-size: .9rem; margin: 1rem 0 0; }
.auth-card .form input { width: 100%; }

/* Map */
.map-wrap { height: 380px; border-radius: 10px; overflow: hidden; }
.leaflet-container { background: var(--bg); }

/* Detail actions */
.detail-actions { display: flex; gap: .6rem; align-items: center; }

/* Map + graph overlay page */
.overlay-toggle { display: flex; border: 1px solid var(--border); border-radius: 8px; overflow: hidden; }
.toggle-btn {
  padding: .5rem .9rem; border: none; background: var(--surface); color: var(--muted);
  font: inherit; font-weight: 600; cursor: pointer;
}
.toggle-btn + .toggle-btn { border-left: 1px solid var(--border); }
.toggle-btn.is-active { background: var(--primary); color: #fff; }

.map-overlay-card { position: relative; padding: 0; overflow: hidden; }
.map-overlay-wrap { height: 78vh; min-height: 420px; border-radius: var(--radius); }
.map-overlay-chart {
  position: absolute; left: .75rem; right: .75rem; bottom: .75rem; z-index: 500;
  padding: .6rem .9rem calc(.5rem + env(safe-area-inset-bottom));
  border-radius: 12px;
  background: rgba(15, 18, 24, .05);
  -webkit-backdrop-filter: blur(2px) saturate(50%);
  backdrop-filter: blur(2px) saturate(50%);
  border: 1px solid rgba(255, 255, 255, .12);
  box-shadow: 0 8px 30px rgba(0, 0, 0, .35);
  pointer-events: none;
}
.map-overlay-chart.is-hidden { display: none; }
.map-overlay-chart .chart-wrap { position: relative; height: 150px; }
.overlay-canvas { position: absolute; inset: 0; pointer-events: auto; transition: opacity .25s ease; }
.overlay-canvas.is-hidden { opacity: 0; pointer-events: none; }

@media (max-width: 640px) {
  .map-overlay-wrap { height: 65vh; }
  .map-overlay-chart .chart-wrap { height: 120px; }
  .map-overlay-chart { left: .5rem; right: .5rem; bottom: .5rem; }
}

/* Live tracking */
.live-grid { display: grid; grid-template-columns: 1.6fr 1fr; gap: 1.5rem; align-items: start; }
.live-map-card { padding: .5rem; }
.live-map-card .map-wrap { height: 520px; }
.live-panel { display: flex; flex-direction: column; gap: 1.5rem; }
.live-panel .card { margin-bottom: 0; }
.live-stats { display: grid; grid-template-columns: repeat(2, 1fr); gap: .75rem; }
.live-stats .stat { padding: .7rem; }
.off-route { color: var(--danger); font-size: .85rem; margin: .8rem 0 0; }
.turn-list { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: .6rem; }
.turn { display: flex; align-items: center; gap: .75rem; padding: .5rem .6rem; border: 1px solid var(--border); border-radius: 10px; }
.turn-arrow { font-size: 1.6rem; line-height: 1; }
.turn-text { display: flex; flex-direction: column; }
.turn-text strong { text-transform: capitalize; }
.turn-away { color: var(--muted); font-size: .82rem; }
.turn-sharp .turn-arrow { color: var(--danger); }
.turn-turn .turn-arrow { color: var(--accent); }
.turn-slight .turn-arrow { color: var(--primary); }
.turn-list .muted { color: var(--muted); }

/* Landing */
.hero { text-align: center; padding: 2.5rem 0 1rem; max-width: 720px; margin: 0 auto; }
.hero h1 { font-size: 2.4rem; margin: 1rem 0 .5rem; line-height: 1.12; }
.hero-sub { color: var(--muted); font-size: 1.1rem; }
.hero-cta { display: flex; gap: .75rem; justify-content: center; margin-top: 1.5rem; flex-wrap: wrap; }
.feature-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(240px, 1fr)); gap: 1rem; margin: 1.5rem 0; }
.feature { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); box-shadow: var(--shadow); padding: 1.3rem; }
.feature-ico { font-size: 1.8rem; }
.feature h3 { margin: .5rem 0 .35rem; font-size: 1.1rem; }
.feature p { color: var(--muted); margin: 0; font-size: .92rem; }
.steps { text-align: center; }
.steps h2 { margin-top: 0; }
.step-list { display: inline-block; text-align: left; color: var(--muted); margin: 1rem auto 1.5rem; padding-left: 1.2rem; line-height: 1.9; }
.step-list strong { color: var(--text); }
@media (max-width: 860px) {
  .live-grid { grid-template-columns: 1fr; }
  .live-map-card .map-wrap { height: 340px; }
}

@media (max-width: 640px) {
  .container { width: 94vw; }

  /* Header wraps: brand on the first row, nav wraps onto the next */
  .header-inner { flex-wrap: wrap; height: auto; padding: .55rem 0; gap: .5rem .6rem; }
  .nav { flex: 1 1 100%; flex-wrap: wrap; gap: .5rem .75rem; }

  /* Bigger tap targets; 16px inputs stop iOS from zooming on focus */
  .btn { padding: .7rem 1.05rem; font-size: 1rem; }
  input[type=text], input[type=file], input[type=password],
  .form input { font-size: 16px; }

  .card { padding: 1.15rem; }
  .page-head { flex-direction: column; align-items: stretch; }
  .page-head h1 { font-size: 1.4rem; }

  /* Action buttons span the row and are easy to hit */
  .page-head > .btn { width: 100%; text-align: center; }
  .detail-actions { width: 100%; }
  .detail-actions .btn, .detail-actions form { flex: 1; }
  .detail-actions form .btn { width: 100%; }
  .form-actions { flex-wrap: wrap; }

  .chart-wrap { height: 260px; }
  .map-wrap { height: 300px; }

  .hero { padding: 1.5rem 0 .5rem; }
  .hero h1 { font-size: 1.9rem; }
  .hero-sub { font-size: 1rem; }
  .hero-cta .btn { width: 100%; }
}

@media (max-width: 460px) {
  .nav-user { display: none; }   /* username isn't essential on very small screens */
  .stat-value { font-size: 1.4rem; }
}
