:root {
  --bg: #0a0e14;
  --panel: #12171f;
  --fg: #eef2f6;
  --muted: #8b98a8;
  --warn: #f5a623;
  --danger: #e8433a;
  --accent: #4da3ff;
  --radius: 18px;
}

* { box-sizing: border-box; }

html, body {
  height: 100%;
  margin: 0;
  background: var(--bg);
  color: var(--fg);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  -webkit-tap-highlight-color: transparent;
  overscroll-behavior: none;
  user-select: none;
  overflow: hidden;
}

/* Pin the page so a rotation can't leave it scrolled/offset ("wobbly"). */
body {
  position: fixed;
  inset: 0;
  width: 100%;
}

button {
  font-family: inherit;
  border: none;
  cursor: pointer;
}

.btn {
  background: var(--panel);
  color: var(--fg);
  border-radius: 12px;
  padding: 0.8rem 1.4rem;
  font-size: 1rem;
  font-weight: 600;
}

.btn.primary { background: var(--accent); color: #05131f; }
.btn.small { padding: 0.55rem 0.9rem; font-size: 0.9rem; }
.btn.danger { color: var(--danger); }
.btn:disabled { opacity: 0.35; cursor: not-allowed; }
.btn.icon-btn {
  width: 3rem;
  height: 3rem;
  border-radius: 50%;
  font-size: 1.3rem;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 0;
}

.build-version {
  color: #46505e;
  font-size: 0.7rem;
  text-align: center;
}

/* ---------- Main view ---------- */

.main-view {
  height: 100%;
  overflow: hidden;
  touch-action: manipulation;
  display: flex;
  flex-direction: column;
  /* Extra bottom padding compresses the status/sign area upward, pulling
     everything below the top bar up off the bottom edge of the screen. */
  padding: env(safe-area-inset-top) 1rem calc(env(safe-area-inset-bottom) + 50px);
}

.top-bar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0.7rem 0.2rem;
}

.top-bar-center {
  flex: 1;
  min-width: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 0.1rem;
  padding: 0 0.5rem;
}

.gps-info {
  color: var(--muted);
  font-size: 0.75rem;
  font-variant-numeric: tabular-nums;
}

.status-panel {
  flex: 1;
  min-height: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  /* flex-start + weighted spacers (1:3) instead of justify-content:center,
     so the gap above the speed readout is half the gap below it - i.e.
     half of what an even (1:1) centered split would give. */
  justify-content: flex-start;
  border-radius: var(--radius);
  padding: 1rem;
  transition: background-color 0.25s ease;
  background: var(--panel);
}

.status-panel::before {
  content: "";
  flex: 1 0 0;
}

.status-panel::after {
  content: "";
  flex: 3 0 0;
}

.status-panel[data-status="exceeding"] { background: color-mix(in srgb, var(--danger) 28%, var(--panel)); }
.status-panel[data-status="exceeding"] { animation: pulse-bg 1s ease-in-out infinite; }

@keyframes pulse-bg {
  0%, 100% { background-color: color-mix(in srgb, var(--danger) 28%, var(--panel)); }
  50% { background-color: color-mix(in srgb, var(--danger) 55%, var(--panel)); }
}

.speed-readout {
  display: flex;
  align-items: baseline;
  gap: 0.5rem;
  font-variant-numeric: tabular-nums;
}

#speedValue {
  font-size: clamp(5rem, 30vw, 9rem);
  font-weight: 700;
  line-height: 1;
}

.actual-speed {
  color: var(--muted);
  font-size: 1rem;
  font-weight: 600;
}

.status-message {
  margin: 0;
  max-width: 100%;
  color: var(--muted);
  font-size: 0.75rem;
  text-align: center;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  min-height: 1em;
}

.main-view[data-status="exceeding"] .status-message { color: #ffd7d2; font-weight: 700; }

.manual-entry {
  flex: 2;
  min-height: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 0.8rem;
  padding: 0.5rem 0.2rem;
}

.speed-signs {
  display: grid;
  grid-template-columns: repeat(2, min-content);
  grid-template-rows: repeat(3, min-content);
  justify-content: center;
  align-content: center;
  gap: 3.5vh 6vw;
  min-height: 0;
  flex: 1;
}

.speed-sign {
  width: min(38vw, 20vh);
  aspect-ratio: 1;
  border-radius: 50%;
  background: #fff;
  border: 11px solid var(--danger);
  color: #111;
  font-weight: 800;
  font-size: clamp(2.6rem, 15vw, 4.6rem);
  font-variant-numeric: tabular-nums;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 0;
  transition: transform 0.12s ease, box-shadow 0.12s ease, background-color 0.12s ease, color 0.12s ease;
}

.speed-sign:active { transform: scale(0.94); }

.speed-sign[data-active="true"] {
  background: #1a2029;
  color: #fff;
}

.test-controls {
  display: flex;
  flex-direction: column;
  gap: 0.6rem;
  padding: 0.2rem 0.2rem 0.9rem;
}

.test-controls-label {
  color: var(--warn);
  font-size: 0.78rem;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  font-weight: 700;
}

.test-presets {
  display: flex;
  gap: 0.5rem;
}

.test-presets .btn { flex: 1; }

.gps-info.test-mode { color: var(--warn); font-weight: 700; }

/* ---------- Settings ---------- */

.settings-panel {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.55);
  display: flex;
  align-items: flex-end;
  z-index: 20;
}

.settings-sheet {
  background: var(--panel);
  width: 100%;
  border-radius: 20px 20px 0 0;
  padding: 1.4rem 1.4rem calc(1.4rem + env(safe-area-inset-bottom));
  display: flex;
  flex-direction: column;
  gap: 1.1rem;
  max-height: 92vh;
  overflow-y: auto;
}

.settings-sheet h2 { margin: 0 0 0.2rem; }

.setting-row {
  display: flex;
  flex-direction: column;
  gap: 0.4rem;
}

.setting-row.checkbox-row {
  flex-direction: row;
  align-items: center;
  justify-content: space-between;
}

.setting-label {
  font-size: 0.92rem;
  font-weight: 600;
}

.setting-value {
  color: var(--accent);
  font-weight: 600;
}

.setting-help {
  color: var(--muted);
  font-size: 0.78rem;
}

.setting-row input[type="range"] { width: 100%; }
.setting-row input[type="checkbox"] { width: 1.3rem; height: 1.3rem; }

.settings-actions {
  margin-top: 0.4rem;
}

.settings-actions .btn { width: 100%; }

.voice-rows { display: flex; flex-direction: column; gap: 0.6rem; }
.voice-row { display: flex; align-items: center; gap: 0.4rem; flex-wrap: wrap; }
.voice-row .voice-speed { width: 2.6rem; font-weight: 700; }
.voice-row .voice-kind { width: 4.2rem; font-size: 0.8rem; color: var(--muted); }
.voice-row .btn.small { padding: 0.4rem 0.6rem; font-size: 0.8rem; }
.voice-row .btn.recording { background: var(--danger); color: #fff; }
.voice-row .voice-has { font-size: 0.75rem; color: var(--muted); }

[hidden] { display: none !important; }

/* ---------- Rotated (landscape) ---------- */

@media (orientation: landscape) and (max-height: 600px) {
  .main-view {
    display: grid;
    grid-template-columns: 1fr 1.7fr;
    grid-template-rows: auto minmax(0, 1fr) auto;
    column-gap: 1rem;
    padding-bottom: calc(env(safe-area-inset-bottom) + 0.5rem);
    padding-left: calc(env(safe-area-inset-left) + 1rem);
    padding-right: calc(env(safe-area-inset-right) + 1rem);
  }

  .top-bar { grid-column: 1 / -1; padding: 0.3rem 0.2rem; }
  .status-panel { grid-column: 1; grid-row: 2; }
  .manual-entry { grid-column: 2; grid-row: 2; padding: 0; }
  .test-controls { grid-column: 1 / -1; grid-row: 3; padding-bottom: 0.3rem; }

  #speedValue { font-size: clamp(4rem, 28vh, 9rem); }

  .speed-signs {
    grid-template-columns: repeat(3, min-content);
    grid-template-rows: repeat(2, min-content);
    gap: 3vh 3vw;
  }

  /* Same size as portrait (38 of the short screen side), only shrinking
     if two rows would not otherwise fit the available height. */
  .speed-sign {
    width: min(38vmin, calc((100dvh - 7rem - 3vh) / 2));
    border-width: 11px;
    font-size: clamp(2.6rem, 15vmin, 4.6rem);
  }
}

.speed-signs.indicator-only { pointer-events: none; }

.speed-sign[data-flash="true"] { animation: sign-flash 0.7s steps(1) infinite; }

@keyframes sign-flash {
  50% { background: #fff; color: #111; }
}
