/* e-Scooter PWA — mobile-first UI, light + dark aware */
:root {
  color-scheme: light dark;

  /* light theme (default) */
  --bg: #f4f6f9;
  --card: #ffffff;
  --card-2: #eef1f5;
  --fg: #111827;
  --muted: #667085;
  --border: #d9dee6;
  --accent: #16a34a;
  --accent-contrast: #ffffff;
  --accent-dim: #15803d;
  --accent-soft: rgba(22, 163, 74, 0.14);
  --danger: #dc2626;
  --warn: #c2740a;
  --overlay: rgba(15, 23, 42, 0.42);
  --appbar: rgba(244, 246, 249, 0.86);
  --log-bg: #f8fafc;
  --log-fg: #334155;
  --switch-knob: #ffffff;

  --radius: 14px;
  --tap: 46px;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0b0d10;
    --card: #151a21;
    --card-2: #1c232c;
    --fg: #e8edf3;
    --muted: #8b97a7;
    --border: #232b35;
    --accent: #37d67a;
    --accent-contrast: #06140c;
    --accent-dim: #1f7a48;
    --accent-soft: rgba(55, 214, 122, 0.16);
    --danger: #ff6b6b;
    --warn: #f2c14e;
    --overlay: rgba(0, 0, 0, 0.55);
    --appbar: rgba(11, 13, 16, 0.86);
    --log-bg: #0f1318;
    --log-fg: #a8b3c2;
    --switch-knob: #ffffff;
  }
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  background: var(--bg);
  color: var(--fg);
}

body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto,
    "Helvetica Neue", Arial, sans-serif;
  font-size: 16px;              /* >=16px stops iOS zoom-on-focus */
  line-height: 1.4;
  -webkit-text-size-adjust: 100%;
  -webkit-tap-highlight-color: transparent;
  min-height: 100dvh;
  display: flex;
  flex-direction: column;
  transition: background 0.2s, color 0.2s;
}

/* ---------------- app bar ---------------- */
.appbar {
  position: sticky;
  top: 0;
  z-index: 10;
  display: flex;
  align-items: center;
  gap: 8px;
  padding: calc(env(safe-area-inset-top) + 10px) 16px 10px;
  background: var(--appbar);
  backdrop-filter: blur(12px);
  border-bottom: 1px solid var(--border);
}
.appbar h1 {
  font-size: 1.05rem;
  font-weight: 650;
  margin: 0;
  flex: 1;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.device-name {
  flex: 0 1 auto;
  max-width: 44%;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  font-size: 0.8rem;
  color: var(--muted);
}

.icon-btn {
  flex: 0 0 auto;
  width: var(--tap);
  height: var(--tap);
  border-radius: 12px;
  border: 1px solid var(--border);
  background: var(--card);
  color: var(--fg);
  font-size: 1.25rem;
  display: grid;
  place-items: center;
  cursor: pointer;
}
.icon-btn:active { background: var(--card-2); }

/* version badge in the app bar */
.version {
  flex: 0 0 auto;
  font-size: 0.72rem;
  color: var(--muted);
  font-variant-numeric: tabular-nums;
  padding: 2px 6px;
  border: 1px solid var(--border);
  border-radius: 8px;
  white-space: nowrap;
}

/* ---------------- layout ---------------- */
main {
  flex: 1;
  width: 100%;
  max-width: 640px;
  margin: 0 auto;
  padding: 16px 16px 32px;
}

/* ---------------- status pill ---------------- */
.status {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 12px 14px;
  margin-bottom: 14px;
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  font-size: 0.95rem;
}
.status .dot {
  width: 10px;
  height: 10px;
  border-radius: 50%;
  background: var(--muted);
  flex: 0 0 auto;
}
.status.is-connected .dot { background: var(--accent); box-shadow: 0 0 0 4px var(--accent-soft); }
.status.is-busy .dot { background: var(--warn); animation: pulse 1s infinite; }
.status.is-error .dot { background: var(--danger); }
@keyframes pulse { 50% { opacity: 0.35; } }

/* ---------------- buttons ---------------- */
button { font-family: inherit; }

.primary {
  width: 100%;
  min-height: var(--tap);
  padding: 12px 16px;
  border: none;
  border-radius: var(--radius);
  background: var(--accent);
  color: var(--accent-contrast);
  font-size: 1rem;
  font-weight: 650;
  cursor: pointer;
}
.primary:active { background: var(--accent-dim); }
.primary.is-disconnect { background: var(--card-2); color: var(--fg); border: 1px solid var(--border); }
.primary:disabled { opacity: 0.5; }

.ghost {
  min-height: 40px;
  padding: 8px 14px;
  border-radius: 10px;
  border: 1px solid var(--border);
  background: transparent;
  color: var(--fg);
  font-size: 0.9rem;
  cursor: pointer;
}

/* ---------------- sections / cards ---------------- */
.section { margin-top: 22px; }
.section > h2 {
  font-size: 0.78rem;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--muted);
  margin: 0 0 10px 2px;
}
.card {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  overflow: hidden;
}

.row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 12px 14px;
  border-top: 1px solid var(--border);
}
.card > .row:first-child { border-top: none; }
.row-label {
  flex: 1 1 auto;
  min-width: 0;
  overflow-wrap: anywhere;
}
.row-label .ro {
  display: inline-block;
  margin-left: 6px;
  font-size: 0.68rem;
  color: var(--muted);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 1px 6px;
  vertical-align: middle;
}

/* status tiles */
.tiles {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(120px, 1fr));
  gap: 10px;
}
.tile {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 12px 14px;
}
.tile .tile-label {
  font-size: 0.75rem;
  color: var(--muted);
  margin-bottom: 4px;
  overflow-wrap: anywhere;
}
.tile .tile-meta {
  font-size: 0.66rem;
  color: var(--muted);
  opacity: 0.8;
  margin-bottom: 6px;
  overflow-wrap: anywhere;
}
.tile .tile-value {
  font-size: 1.15rem;
  font-weight: 600;
  overflow-wrap: anywhere;
}

/* ---------------- form controls ---------------- */
input[type="text"],
input[type="password"],
input[type="number"],
select {
  min-height: var(--tap);
  width: 100%;
  padding: 10px 12px;
  font-size: 16px;              /* keeps iOS from zooming */
  color: var(--fg);
  background: var(--card-2);
  border: 1px solid var(--border);
  border-radius: 10px;
}
select { cursor: pointer; }

.field { margin-bottom: 14px; }
.field label {
  display: block;
  font-size: 0.82rem;
  color: var(--muted);
  margin-bottom: 6px;
}

.inline { display: flex; gap: 8px; align-items: center; }
.inline input { flex: 1 1 auto; }
.inline .ghost { flex: 0 0 auto; }

/* toggle switch (checkbox + .slider) */
.switch {
  position: relative;
  display: inline-block;
  flex: 0 0 auto;
  width: 52px;
  height: 30px;
}
.switch input {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  margin: 0;
  opacity: 0;
  cursor: pointer;
}
.switch .slider {
  position: absolute;
  inset: 0;
  border-radius: 999px;
  background: var(--card-2);
  border: 1px solid var(--border);
  transition: background 0.15s, border-color 0.15s;
}
.switch .slider::before {
  content: "";
  position: absolute;
  top: 2px;
  left: 2px;
  width: 22px;
  height: 22px;
  border-radius: 50%;
  background: var(--muted);
  transition: transform 0.15s, background 0.15s;
}
.switch input:checked + .slider { background: var(--accent); border-color: var(--accent); }
.switch input:checked + .slider::before { transform: translateX(26px); background: var(--switch-knob); }
.switch input:disabled + .slider { opacity: 0.45; }
.switch input:disabled { cursor: not-allowed; }

/* interactive control tiles */
.tile-control { display: flex; flex-direction: column; gap: 8px; align-items: flex-start; }
.tile-control .tile-value { align-self: stretch; }
.control-select,
.control-num { min-height: 42px; }
.control-row,
.control-fields {
  display: flex;
  gap: 8px;
  align-items: center;
  flex-wrap: wrap;
  width: 100%;
}
.control-row .control-num { flex: 1 1 auto; }
.control-fields { margin-top: 4px; }
.control-fields .control-num.small { flex: 0 0 88px; width: 88px; }
.control-fields .control-select { flex: 0 0 auto; width: auto; }
.control-fields .control-num:not(.small) { flex: 1 1 120px; }
.manual-hint { margin-bottom: 8px; }
.ghost:disabled { opacity: 0.5; cursor: not-allowed; }

/* arm-switch tile spans the whole row */
.tile.arm { grid-column: 1 / -1; }
.tile.arm.on { border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft); }
.primary.danger { background: var(--danger); color: #fff; }
.primary.danger:active { filter: brightness(0.92); }

/* confirm dialog sits centred, not bottom-anchored */
#confirmOverlay { align-items: center; padding: 16px; }
.sheet.confirm { border-radius: 18px; max-width: 420px; }
.overlay .sheet.confirm { transform: translateY(24px) scale(0.98); }
.overlay.open .sheet.confirm { transform: none; }

/* segmented enum control */
.segment {
  display: flex;
  flex: 0 0 auto;
  flex-wrap: wrap;
  gap: 6px;
  justify-content: flex-end;
}
.segment button {
  min-height: 38px;
  padding: 6px 14px;
  border-radius: 999px;
  border: 1px solid var(--border);
  background: var(--card-2);
  color: var(--fg);
  font-size: 0.9rem;
  cursor: pointer;
}
.segment button[aria-pressed="true"] {
  background: var(--accent);
  border-color: var(--accent);
  color: var(--accent-contrast);
  font-weight: 650;
}

/* value stepper/slider */
.value-ctrl { flex: 0 0 46%; display: flex; align-items: center; gap: 10px; }
.value-ctrl input[type="range"] { flex: 1 1 auto; accent-color: var(--accent); min-width: 0; }
.value-ctrl input[type="number"] { width: 92px; min-height: 40px; text-align: right; }
.unit { color: var(--muted); min-width: 1.5em; }

/* ---------------- settings sheet ---------------- */
.overlay {
  position: fixed;
  inset: 0;
  z-index: 20;
  display: flex;
  align-items: flex-end;
  justify-content: center;
  background: var(--overlay);
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.2s;
}
.overlay[hidden] { display: none; }
.overlay.open { opacity: 1; pointer-events: auto; }
.sheet {
  width: 100%;
  max-width: 640px;
  max-height: 90dvh;
  overflow-y: auto;
  background: var(--card);
  border-radius: 20px 20px 0 0;
  padding: 20px 18px calc(env(safe-area-inset-bottom) + 20px);
  transform: translateY(100%);
  transition: transform 0.25s cubic-bezier(0.2, 0.8, 0.2, 1);
}
.overlay.open .sheet { transform: translateY(0); }
.sheet-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 8px;
}
.sheet-header h2 { font-size: 1.05rem; margin: 0; }
.sheet-hint { color: var(--muted); font-size: 0.82rem; margin: 0 0 16px; }
.sheet-actions { display: flex; gap: 10px; align-items: center; margin-top: 6px; }
.sheet-actions .primary { flex: 1 1 auto; }
.saved-note { color: var(--accent); font-size: 0.85rem; margin-left: 4px; }
.saved-note.error { color: var(--danger); }

/* settings is a full-screen view */
#settingsOverlay { align-items: stretch; background: var(--bg); }
#settingsOverlay .sheet {
  max-width: none;
  max-height: none;
  height: 100dvh;
  border-radius: 0;
  padding: calc(env(safe-area-inset-top) + 16px) 18px calc(env(safe-area-inset-bottom) + 24px);
}
.sheet-section-title {
  font-size: 0.72rem;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--muted);
  margin: 22px 0 10px;
  padding-bottom: 6px;
  border-bottom: 1px solid var(--border);
}
.sheet-hint.small { font-size: 0.8rem; margin-bottom: 8px; }
.sheet-hint a { color: var(--accent); }
.help { margin: 0 0 16px; font-size: 0.82rem; color: var(--muted); }
.help summary { cursor: pointer; color: var(--accent); }
.help ol { margin: 8px 0 0; padding-left: 20px; display: grid; gap: 6px; }

/* ---------------- diagnostics ---------------- */
.log-wrap {
  width: 100%;
  max-width: 640px;
  margin: 0 auto;
  padding: 0 16px calc(env(safe-area-inset-bottom) + 20px);
}
.log-wrap summary {
  color: var(--muted);
  font-size: 0.85rem;
  padding: 10px 2px;
  cursor: pointer;
}
.log-wrap .ghost { margin-bottom: 8px; }
.log {
  max-height: 40dvh;
  overflow: auto;
  margin: 0;
  padding: 10px;
  font-size: 12px;
  line-height: 1.45;
  background: var(--log-bg);
  color: var(--log-fg);
  border: 1px solid var(--border);
  border-radius: 10px;
  white-space: pre-wrap;
  overflow-wrap: anywhere;
}

.muted { color: var(--muted); }

.notice {
  margin-bottom: 14px;
  padding: 12px 14px;
  background: var(--card);
  border: 1px solid var(--border);
  border-left: 4px solid var(--warn);
  border-radius: 10px;
  color: var(--muted);
  font-size: 0.85rem;
  line-height: 1.45;
}
.empty {
  padding: 22px 16px;
  text-align: center;
  color: var(--muted);
}

/* ---------------- bottom tab bar ---------------- */
.tabbar {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 15;
  display: flex;
  gap: 6px;
  padding: 8px 12px calc(env(safe-area-inset-bottom) + 8px);
  background: var(--appbar);
  backdrop-filter: blur(12px);
  border-top: 1px solid var(--border);
}
.tab {
  flex: 1 1 0;
  min-height: 44px;
  border: 1px solid transparent;
  border-radius: 12px;
  background: transparent;
  color: var(--muted);
  font-size: 0.95rem;
  font-weight: 600;
  cursor: pointer;
}
.tab.is-active { background: var(--accent-soft); color: var(--accent); border-color: var(--accent); }

.toast {
  position: fixed;
  left: 50%;
  bottom: calc(84px + env(safe-area-inset-bottom));
  transform: translateX(-50%) translateY(12px);
  z-index: 30;
  max-width: 90vw;
  padding: 10px 16px;
  border-radius: 12px;
  background: var(--card-2);
  color: var(--fg);
  border: 1px solid var(--border);
  font-size: 0.85rem;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.28);
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.2s, transform 0.2s;
}
.toast.open { opacity: 1; transform: translateX(-50%) translateY(0); }
.toast[hidden] { display: none; }

main { padding-bottom: calc(84px + env(safe-area-inset-bottom)); }
.view[hidden] { display: none; }

/* ---------------- ride: speed hero ---------------- */
.unit-seg { display: flex; gap: 6px; justify-content: flex-end; margin-bottom: 10px; }
.seg-btn {
  min-height: 34px;
  padding: 4px 12px;
  border-radius: 999px;
  border: 1px solid var(--border);
  background: var(--card-2);
  color: var(--fg);
  font-size: 0.85rem;
  cursor: pointer;
}
.seg-btn.is-active {
  background: var(--accent);
  border-color: var(--accent);
  color: var(--accent-contrast);
  font-weight: 650;
}

.speed-hero {
  text-align: center;
  padding: 18px 14px 14px;
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  margin-bottom: 12px;
}
.speed-value { display: flex; align-items: baseline; justify-content: center; gap: 8px; line-height: 1; }
.speed-value #rideSpeed {
  font-size: 4.6rem;
  font-weight: 750;
  font-variant-numeric: tabular-nums;
  letter-spacing: -0.02em;
}
.speed-unit { font-size: 1.1rem; color: var(--muted); font-weight: 600; }
.speed-state { margin-top: 6px; font-size: 0.78rem; color: var(--muted); min-height: 1em; }
.speed-value #rideSpeed.is-warn { color: #ff4d4f; animation: warn-pulse 0.9s ease-in-out infinite; }
@keyframes warn-pulse { 0%, 100% { opacity: 1; } 50% { opacity: 0.45; } }
#warnUnit { opacity: 0.75; }

.mode-seg { display: grid; grid-template-columns: repeat(4, 1fr); gap: 6px; margin-bottom: 12px; }
.mode-btn {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
  padding: 8px 4px;
  border: 1px solid var(--border);
  border-radius: 12px;
  background: var(--card-2);
  color: var(--fg);
  font-size: 0.78rem;
  font-weight: 600;
  cursor: pointer;
}
.mode-btn .mode-emoji { font-size: 1.15rem; line-height: 1; }
.mode-btn.is-active { background: var(--accent); border-color: var(--accent); color: var(--accent-contrast); }

.lock-btn {
  width: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  padding: 12px;
  margin-bottom: 12px;
  border: 1px solid var(--border);
  border-radius: 12px;
  background: var(--card-2);
  color: var(--fg);
  font-size: 1rem;
  font-weight: 650;
  cursor: pointer;
}
.lock-btn .lock-icon { font-size: 1.25rem; line-height: 1; }
.lock-btn.is-locked { background: var(--danger); border-color: var(--danger); color: #fff; }
.lock-btn.is-unlocked { background: var(--accent); border-color: var(--accent); color: var(--accent-contrast); }
.lock-btn:disabled { opacity: 0.5; }

.ride-stats { display: grid; grid-template-columns: repeat(4, 1fr); gap: 8px; margin-bottom: 12px; }
.stat {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 10px 8px;
  text-align: center;
}
.stat .stat-label { font-size: 0.66rem; text-transform: uppercase; letter-spacing: 0.06em; color: var(--muted); }
.stat .stat-value { font-size: 1.05rem; font-weight: 650; margin-top: 2px; font-variant-numeric: tabular-nums; }
.stat .stat-unit { font-size: 0.66rem; color: var(--muted); margin-left: 3px; font-weight: 500; }

/* voice-announce controls */
.announce-row { display: flex; align-items: center; gap: 14px; flex-wrap: wrap; }
.announce-field { display: flex; align-items: center; gap: 6px; font-size: 0.82rem; color: var(--muted); }
.announce-field input { width: 76px; min-height: 40px; }
