:root {
  /* Layout: a phone app. Home, trips and notes are one narrow column; the drive screen is a dark full-screen lamp. */
  --bg: #EEF1F3;
  --surface: #FFFFFF;
  --line: #C9D1D8;
  --fg: #14191E;
  --muted: #56616B;
  --accent: #1C4E8F;
  --accent-fg: #FFFFFF;
  --ok: #1F9D58;
  --warn: #E3A008;
  --stop: #D62839;
  --idle: #5E6A75;
  --night: #0B0E11;
  --night-2: #161B21;
  --night-line: #28313A;
  --night-fg: #EEF1F4;
  --night-muted: #9AA6B1;
  --sign-bg: #FFFFFF;
  --sign-ring: #C8102E;
  --sign-fg: #111417;
  --font: system-ui, -apple-system, "Segoe UI", Roboto, "Noto Sans", "Helvetica Neue", Arial, sans-serif;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #101418;
    --surface: #191E24;
    --line: #313A43;
    --fg: #E6EAEE;
    --muted: #9AA5AF;
    --accent: #7AA5E6;
    --accent-fg: #0B1422;
    --ok: #33C47A;
    --warn: #F0B429;
    --stop: #F2495C;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body { margin: 0; background: var(--bg); color: var(--fg); font-family: var(--font); font-size: 16px; line-height: 1.5; }
body[data-view="drive"] { background: var(--night); }
button, select, input { font: inherit; }
h1, h2, h3, p { margin: 0; }
h1, h2, h3 { line-height: 1.15; text-wrap: balance; }
a { color: var(--accent); }
:focus-visible { outline: 3px solid var(--accent); outline-offset: 2px; }
[hidden] { display: none !important; }

/* Ordinary screens */
.view {
  max-width: 560px;
  margin: 0 auto;
  display: grid;
  gap: 16px;
  align-content: start;
  padding-top: max(20px, env(safe-area-inset-top, 0px));
  padding-bottom: max(28px, env(safe-area-inset-bottom, 0px));
  padding-left: max(16px, env(safe-area-inset-left, 0px));
  padding-right: max(16px, env(safe-area-inset-right, 0px));
}
.app-head { display: grid; gap: 4px; }
.app-head h1 { font-size: 2.2rem; font-weight: 800; letter-spacing: -0.01em; }
.view h2 { font-size: 1.45rem; font-weight: 800; }
.sub, .muted, .safety, .foot, .hint { color: var(--muted); }
.safety { font-size: .92rem; }
.hint { font-size: .85rem; }
.foot { font-size: .8rem; }
.card { background: var(--surface); border: 1px solid var(--line); border-radius: 16px; padding: 16px; }
.total { display: grid; gap: 2px; }
.total .label { font-size: .9rem; color: var(--muted); }
.total .big { font-size: 3.4rem; font-weight: 800; line-height: 1.05; color: var(--accent); font-variant-numeric: tabular-nums; }
.total .small { font-size: .98rem; }
.total .hint { margin-top: 6px; }
.notice { background: var(--surface); border: 1px solid var(--line); border-radius: 12px; padding: 10px 12px; font-size: .92rem; }

.primary {
  width: 100%;
  min-height: 56px;
  border: 0;
  border-radius: 14px;
  background: var(--accent);
  color: var(--accent-fg);
  font-size: 1.15rem;
  font-weight: 700;
  cursor: pointer;
}
.ghost {
  min-height: 44px;
  padding: 0 14px;
  border-radius: 12px;
  border: 1.5px solid var(--line);
  background: var(--surface);
  color: var(--fg);
  font-weight: 600;
  cursor: pointer;
}
.ghost:hover { border-color: var(--muted); }
.ghost.danger { color: var(--stop); justify-self: start; }
.ghost.danger[data-armed] { background: var(--stop); border-color: var(--stop); color: #fff; }
.links { display: flex; flex-wrap: wrap; gap: 10px; }

.view-head { display: flex; align-items: center; gap: 6px; }
.back { min-height: 44px; padding: 0 10px; margin-left: -10px; border: 0; background: transparent; color: var(--accent); font-weight: 600; cursor: pointer; }

/* Drive screen: always dark, so it does not dazzle at night */
.drive { position: fixed; inset: 0; background: var(--night); color: var(--night-fg); --lamp: var(--idle); }
.drive[data-lamp="ok"] { --lamp: var(--ok); }
.drive[data-lamp="over"] { --lamp: var(--warn); }
.drive[data-lamp="red"] { --lamp: var(--stop); }
.drive-main {
  height: 100%;
  display: grid;
  grid-template-columns: minmax(0, 1fr);
  grid-template-rows: auto minmax(0, 1fr) auto auto auto auto auto;
  grid-template-areas: "top" "lamp" "read" "status" "stats" "stop" "attr";
  justify-items: center;
  align-items: center;
  row-gap: 10px;
  padding-top: max(12px, env(safe-area-inset-top, 0px));
  padding-bottom: max(12px, env(safe-area-inset-bottom, 0px));
  padding-left: max(16px, env(safe-area-inset-left, 0px));
  padding-right: max(16px, env(safe-area-inset-right, 0px));
  background: var(--night);
  background: radial-gradient(circle at 50% 32%, color-mix(in srgb, var(--lamp) 16%, var(--night)) 0%, var(--night) 62%);
}
.drive-top { grid-area: top; width: 100%; display: flex; justify-content: space-between; align-items: center; gap: 12px; font-size: .95rem; color: var(--night-muted); }
.road { min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.gps { flex: none; font-size: .72rem; font-weight: 700; letter-spacing: .08em; padding: 2px 8px; border-radius: 999px; border: 1px solid var(--night-line); color: var(--night-muted); }
.gps[data-q="good"] { color: var(--ok); border-color: currentColor; }
.gps[data-q="ok"] { color: var(--warn); border-color: currentColor; }
.gps[data-q="weak"], .gps[data-q="none"] { color: var(--stop); border-color: currentColor; }
.lamp-wrap { grid-area: lamp; position: relative; display: grid; place-items: center; --ls: min(70vw, 34vh, 340px); }
.lamp {
  width: var(--ls);
  aspect-ratio: 1;
  border-radius: 50%;
  background: var(--lamp);
  background: radial-gradient(circle at 50% 36%, color-mix(in srgb, var(--lamp) 68%, #fff) 0%, var(--lamp) 50%, color-mix(in srgb, var(--lamp) 60%, #000) 100%);
  box-shadow: 0 0 48px 8px color-mix(in srgb, var(--lamp) 45%, transparent);
  transition: box-shadow .3s ease, opacity .3s ease;
}
.drive[data-lamp="wait"] .lamp { opacity: .45; box-shadow: none; }
.readout { grid-area: read; display: flex; align-items: center; justify-content: center; gap: 22px; }
.speed { display: flex; align-items: baseline; gap: 6px; font-weight: 800; font-size: min(20vw, 11vh, 104px); line-height: 1; font-variant-numeric: tabular-nums; letter-spacing: -0.02em; }
.speed small { font-size: 1rem; font-weight: 600; color: var(--night-muted); letter-spacing: 0; }
.sign-wrap { display: grid; justify-items: center; gap: 4px; }
.sign {
  --s: min(18vw, 10vh, 92px);
  width: var(--s);
  height: var(--s);
  border-radius: 50%;
  background: var(--sign-bg);
  border: calc(var(--s) * 0.12) solid var(--sign-ring);
  color: var(--sign-fg);
  display: grid;
  place-items: center;
  font-weight: 800;
  font-size: calc(var(--s) * 0.36);
  line-height: 1;
  font-variant-numeric: tabular-nums;
}
.est { font-size: .78rem; color: var(--night-muted); min-height: 1.2em; }
/* In small windows the speed sits inside the lamp and the sign on its corner */
.lamp-speed {
  position: absolute;
  inset: 0;
  display: none;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  color: var(--night);
  font-weight: 800;
  line-height: 1;
  font-variant-numeric: tabular-nums;
  pointer-events: none;
}
.lamp-speed span { font-size: calc(var(--ls) * 0.36); letter-spacing: -0.02em; }
.lamp-speed small { font-size: calc(var(--ls) * 0.08); font-weight: 700; }
.drive[data-lamp="wait"] .lamp-speed { color: var(--night-fg); }
.sign.sign-mini { --s: calc(var(--ls) * 0.34); position: absolute; top: 0; right: 0; display: none; }
.status { grid-area: status; font-size: 1.15rem; font-weight: 600; text-align: center; min-height: 2.6em; max-width: 30ch; display: flex; align-items: center; justify-content: center; }
.trip-stats { grid-area: stats; width: 100%; max-width: 480px; display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 8px; }
.trip-stats div, .totals div { display: grid; justify-items: center; }
.trip-stats .v { font-size: 1.5rem; font-weight: 800; font-variant-numeric: tabular-nums; }
.trip-stats .l { font-size: .78rem; color: var(--night-muted); text-align: center; }
.stop { grid-area: stop; width: 100%; max-width: 480px; min-height: 52px; border-radius: 14px; border: 1px solid var(--night-line); background: var(--night-2); color: var(--night-fg); font-weight: 700; cursor: pointer; }
.stop[data-armed] { background: var(--stop); border-color: var(--stop); color: #fff; }
.attr { grid-area: attr; font-size: .7rem; color: var(--night-muted); }
.drive-error { height: 100%; max-width: 480px; margin: 0 auto; display: grid; align-content: center; gap: 18px; padding: 24px max(16px, env(safe-area-inset-right, 0px)) 24px max(16px, env(safe-area-inset-left, 0px)); font-size: 1.05rem; text-align: center; }

@media (orientation: landscape) and (max-height: 560px) {
  .drive-main {
    grid-template-columns: auto minmax(0, 1fr);
    grid-template-rows: auto minmax(0, 1fr) auto auto auto auto;
    grid-template-areas: "top top" "lamp read" "lamp status" "lamp stats" "lamp stop" "lamp attr";
    column-gap: 28px;
    row-gap: 6px;
  }
  .lamp-wrap { --ls: min(42vw, 68vh, 320px); }
  .speed { font-size: min(11vw, 20vh, 96px); }
  .sign { --s: min(10vw, 18vh, 84px); }
  .status { min-height: 0; }
}
/* Half of the screen beside Google Maps, or a pop-up window */
@media (max-width: 520px) and (max-height: 480px) {
  .drive-main {
    grid-template-columns: auto minmax(0, 1fr);
    grid-template-rows: auto minmax(0, 1fr) auto auto;
    grid-template-areas: "top top" "lamp status" "lamp stats" "lamp stop";
    column-gap: 14px;
    row-gap: 6px;
    justify-items: stretch;
  }
  .readout, .attr { display: none; }
  .lamp-wrap { --ls: min(48vw, 62vh, 260px); }
  .lamp-speed { display: flex; }
  .sign.sign-mini { display: grid; }
  .status { font-size: .95rem; min-height: 0; justify-content: flex-start; text-align: left; }
  .trip-stats { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .trip-stats div:nth-child(2) { display: none; }
  .trip-stats .v { font-size: 1.1rem; }
  .trip-stats .l { font-size: .72rem; white-space: nowrap; }
  .stop { min-height: 40px; }
}
/* A small bubble: only the lamp, the speed and the limit */
@media (max-width: 360px) and (max-height: 360px) {
  .drive-main {
    grid-template-columns: minmax(0, 1fr);
    grid-template-rows: minmax(0, 1fr);
    grid-template-areas: "lamp";
    justify-items: center;
    padding: 6px;
    background: var(--night);
  }
  .drive-top, .readout, .status, .trip-stats, .stop, .attr { display: none; }
  .lamp-wrap { --ls: min(86vw, 86vh); }
  .lamp-speed { display: flex; }
  .sign.sign-mini { display: grid; }
}
@media (prefers-reduced-motion: no-preference) {
  .drive[data-lamp="red"] .lamp { animation: pulse 1s ease-in-out infinite; }
}
@media (prefers-reduced-motion: reduce) {
  .lamp { transition: none; }
}
@keyframes pulse {
  50% { box-shadow: 0 0 72px 18px color-mix(in srgb, var(--lamp) 65%, transparent); }
}

/* End of a drive */
.ticket { background: var(--surface); border: 1.5px dashed var(--line); border-radius: 12px; padding: 14px 16px; display: grid; gap: 10px; }
.ticket-h { font-weight: 600; }
.ticket dl { margin: 0; display: grid; gap: 6px; }
.ticket dl div { display: flex; justify-content: space-between; gap: 12px; border-bottom: 1px dotted var(--line); padding-bottom: 6px; }
.ticket dt { color: var(--muted); }
.ticket dd { margin: 0; font-weight: 600; font-variant-numeric: tabular-nums; }
.ticket-f { font-size: .88rem; color: var(--muted); }

/* Trips */
.totals { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 8px; }
.totals .v { font-size: 1.6rem; font-weight: 800; font-variant-numeric: tabular-nums; }
.totals .l { font-size: .8rem; color: var(--muted); text-align: center; }
.trip-list { list-style: none; margin: 0; padding: 0; display: grid; }
.trip-list li { display: flex; flex-wrap: wrap; justify-content: space-between; gap: 4px 12px; padding: 10px 0; border-bottom: 1px solid var(--line); }
.trip-list .when { color: var(--muted); }
.trip-list .what { font-weight: 600; font-variant-numeric: tabular-nums; }

/* How it works */
#about article { display: grid; gap: 8px; }
#about h3 { font-size: 1.1rem; font-weight: 700; }
#about ul { margin: 0; padding-left: 1.2em; display: grid; gap: 4px; }
.setting { display: flex; justify-content: space-between; align-items: center; gap: 12px; padding: 10px 0; border-bottom: 1px solid var(--line); }
.setting input[type="checkbox"] { width: 24px; height: 24px; accent-color: var(--accent); }
.setting select { min-height: 44px; max-width: 60%; padding: 0 10px; border-radius: 10px; border: 1.5px solid var(--line); background: var(--surface); color: var(--fg); }
