:root {
  color-scheme: light;
  font-family: "DM Sans", "Atkinson Hyperlegible Next", "Microsoft YaHei UI", sans-serif;
  color: #202825;
  background: #f4f6f5;
  font-synthesis: none;
  text-rendering: optimizeLegibility;
  --ink: #202825;
  --muted: #6f7975;
  --line: #dfe5e2;
  --paper: #ffffff;
  --green: #146d59;
  --green-pale: #e5f2ed;
  --amber: #b7671b;
  --amber-pale: #fff1dd;
  --red: #bd493d;
  --red-pale: #fbe9e6;
  --blue: #386c8b;
  --blue-pale: #e8f1f6;
  --radius: 8px;
}

* { box-sizing: border-box; }

body {
  min-width: 320px;
  min-height: 100vh;
  margin: 0;
  background: var(--paper);
}

button, input { font: inherit; }

button { color: inherit; }

.app-shell {
  display: flex;
  min-height: 100vh;
  flex-direction: column;
}

.topbar {
  display: flex;
  min-height: 64px;
  align-items: center;
  justify-content: space-between;
  padding: 0 max(24px, calc((100vw - 1040px) / 2));
  border-bottom: 1px solid var(--line);
  background: var(--paper);
}

.wordmark {
  display: inline-flex;
  align-items: center;
  gap: 10px;
  color: var(--ink);
  font-size: 15px;
  font-weight: 700;
  text-decoration: none;
}

.brand-mark {
  width: 12px;
  height: 12px;
  border: 3px solid var(--green);
  border-radius: 50%;
  box-shadow: inset 0 0 0 2px var(--paper);
  background: var(--amber);
}

.topbar-actions { display: flex; align-items: center; gap: 20px; }
.admin-link { display: inline-flex; min-height: 44px; min-width: 32px; align-items: center; justify-content: center; color: var(--green); font-size: 12px; text-decoration: none; }
.admin-link:hover { text-decoration: underline; }
.login-error { margin: 0; color: var(--red); font-size: 13px; line-height: 1.5; }
.login-error:empty { display: none; }
.guest-button { min-height: 48px; padding: 8px 16px; border: 1px solid var(--green); border-radius: 6px; background: var(--paper); color: var(--green); font-weight: 700; cursor: pointer; }
.guest-button:hover { background: var(--green-pale); }

.connection {
  display: inline-flex;
  min-height: 32px;
  align-items: center;
  gap: 8px;
  color: var(--muted);
  font-size: 12px;
  white-space: nowrap;
}

.connection-dot, .status-light {
  width: 8px;
  height: 8px;
  flex: 0 0 auto;
  border-radius: 50%;
  background: #a1aaa6;
}

.connection[data-state="online"] .connection-dot { background: #2f9477; }
.connection[data-state="pending"] .connection-dot { background: var(--amber); }
.connection[data-state="offline"] .connection-dot { background: var(--red); }

.text-button, .close-button {
  min-height: 40px;
  padding: 0 10px;
  border: 0;
  border-radius: 6px;
  background: transparent;
  color: #46514c;
  cursor: pointer;
}

.text-button:hover, .close-button:hover { background: #f0f3f1; }

main {
  width: min(100% - 48px, 920px);
  flex: 1;
  margin: 0 auto;
}

.setup-view {
  width: min(100%, 520px);
  margin: clamp(72px, 15vh, 150px) auto 80px;
}

.intro-copy { margin-bottom: 34px; }

.eyebrow {
  margin: 0 0 8px;
  color: var(--green);
  font-size: 11px;
  font-weight: 700;
  letter-spacing: 0.08em;
  text-transform: uppercase;
}

h1, h2, p { margin-top: 0; }

h1 {
  margin-bottom: 8px;
  font-size: 30px;
  line-height: 1.2;
}

.subtle { margin: 0; color: var(--muted); font-size: 14px; }

.vehicle-form {
  display: grid;
  gap: 10px;
}

.vehicle-form label, .settings-dialog label {
  color: #46514c;
  font-size: 13px;
  font-weight: 600;
}

input {
  width: 100%;
  min-height: 50px;
  padding: 0 14px;
  border: 1px solid #cfd8d3;
  border-radius: 6px;
  outline: none;
  background: var(--paper);
  color: var(--ink);
}

input:focus { border-color: var(--green); box-shadow: 0 0 0 3px #146d591b; }
input::placeholder { color: #9ba49f; }

.primary-button {
  min-height: 48px;
  padding: 0 18px;
  border: 1px solid var(--green);
  border-radius: 6px;
  background: var(--green);
  color: #fff;
  font-weight: 700;
  cursor: pointer;
}

.primary-button:hover { background: #0f5b49; }
.primary-button:focus-visible, button:focus-visible { outline: 3px solid #5eaa95; outline-offset: 2px; }

.vehicle-form .primary-button { margin-top: 8px; }

.trip-history-section {
  margin-top: 54px;
  padding-top: 23px;
  border-top: 1px solid var(--line);
}

.trip-list { display: grid; gap: 8px; }

.trip-item {
  overflow: hidden;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--paper);
}

.trip-summary {
  display: grid;
  min-height: 66px;
  grid-template-columns: minmax(0, 1fr) auto 14px;
  grid-template-areas: "vehicle count more" "meta meta more";
  align-items: center;
  gap: 8px 16px;
  padding: 11px 14px;
  cursor: pointer;
  list-style: none;
}

.trip-summary::-webkit-details-marker { display: none; }
.trip-summary::after { grid-area: more; color: #74817a; content: "+"; font-size: 19px; }
.trip-item[open] .trip-summary::after { content: "−"; }
.trip-vehicle { grid-area: vehicle; overflow: hidden; font-size: 14px; font-weight: 700; text-overflow: ellipsis; white-space: nowrap; }
.trip-meta { grid-area: meta; overflow-wrap: anywhere; color: var(--muted); font-size: 11px; line-height: 1.4; }
.trip-count { grid-area: count; color: var(--green); font-size: 11px; font-weight: 600; white-space: nowrap; }
.trip-contents { padding: 0 14px 4px; }
.trip-contents .history-item:last-child { border-bottom: 0; }
.trip-contents .empty-state { padding: 16px 0; }
.trip-history-section > .empty-state { padding: 22px 0; }

.record-view { padding: 48px 0 64px; }

.record-heading {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 18px;
  padding-bottom: 25px;
  border-bottom: 1px solid var(--line);
}

.record-heading h1 { margin: 0; overflow-wrap: anywhere; }

.ignition-status {
  display: inline-flex;
  min-height: 36px;
  align-items: center;
  gap: 9px;
  padding: 0 12px;
  border: 1px solid var(--line);
  border-radius: 999px;
  color: var(--muted);
  font-size: 12px;
  white-space: nowrap;
}

.ignition-status[data-on="true"] { border-color: #b5d7cb; background: var(--green-pale); color: #155f4d; }
.ignition-status[data-on="true"] .status-light { background: #2f9477; }

.finish-button {
  min-height: 36px;
  padding: 0 11px;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: var(--paper);
  color: #59645f;
  font-size: 12px;
  cursor: pointer;
}

.finish-button:hover:not(:disabled) { border-color: #aebdb6; background: #f8faf9; }
.finish-button:disabled { color: #a2aaa6; cursor: not-allowed; }

.controls-section { padding: 24px 0 28px; border-bottom: 1px solid var(--line); }

.section-heading {
  display: flex;
  min-height: 28px;
  align-items: center;
  justify-content: space-between;
  gap: 14px;
  margin-bottom: 14px;
}

.section-heading h2 { margin: 0; font-size: 16px; }
.section-heading > span { color: var(--muted); font-size: 12px; }

.action-grid {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 10px;
}

.action-button {
  display: flex;
  min-width: 0;
  min-height: 76px;
  flex-direction: column;
  align-items: flex-start;
  justify-content: center;
  gap: 5px;
  padding: 12px 15px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--paper);
  font-size: 14px;
  font-weight: 700;
  text-align: left;
  cursor: pointer;
  transition: border-color 120ms ease, background 120ms ease, transform 120ms ease;
}

.action-button:hover:not(:disabled) { transform: translateY(-1px); border-color: #aebdb6; }
.action-button:active:not(:disabled) { transform: translateY(0); }
.action-button:disabled { color: #a2aaa6; cursor: not-allowed; background: #fafbfa; }
.action-button small { margin-left: 22px; color: var(--muted); font-size: 11px; font-weight: 500; }

.action-ignition:not(:disabled) { border-color: #b5d7cb; background: var(--green-pale); color: #155f4d; }
.action-driving:not(:disabled), .action-deceleration:not(:disabled) { border-color: #f1d2aa; background: var(--amber-pale); color: #8f4d11; }
.action-turn:not(:disabled), .action-off:not(:disabled) { border-color: #ecc2bc; background: var(--red-pale); color: #a63f35; }
.action-stop:not(:disabled) { border-color: #c2d7e2; background: var(--blue-pale); color: #315e78; }
.action-speed-low:not(:disabled) { border-color: #c2d7e2; background: var(--blue-pale); color: #315e78; }
.action-speed-medium:not(:disabled) { border-color: #b5d7cb; background: var(--green-pale); color: #155f4d; }
.action-speed-high:not(:disabled) { border-color: #f1d2aa; background: var(--amber-pale); color: #8f4d11; }

.action-symbol {
  position: relative;
  display: block;
  width: 14px;
  height: 14px;
  flex: 0 0 14px;
  margin-bottom: 1px;
  color: currentColor;
}

.symbol-start { border: 2px solid currentColor; border-radius: 50%; }
.symbol-start::after { position: absolute; top: 3px; left: 3px; width: 4px; height: 4px; border-radius: 50%; background: currentColor; content: ""; }
.symbol-driving::before { position: absolute; top: 5px; left: 1px; width: 11px; border-top: 2px solid currentColor; content: ""; }
.symbol-driving::after { position: absolute; top: 2px; right: 0; width: 6px; height: 6px; border-top: 2px solid currentColor; border-right: 2px solid currentColor; transform: rotate(45deg); content: ""; }
.symbol-deceleration::before { position: absolute; top: 5px; right: 1px; width: 11px; border-top: 2px solid currentColor; content: ""; }
.symbol-deceleration::after { position: absolute; top: 2px; left: 0; width: 6px; height: 6px; border-bottom: 2px solid currentColor; border-left: 2px solid currentColor; transform: rotate(45deg); content: ""; }
.symbol-turn { border: 2px solid currentColor; border-right-color: transparent; border-radius: 50%; transform: rotate(-35deg); }
.symbol-turn::after { position: absolute; top: -2px; right: -2px; width: 5px; height: 5px; border-top: 2px solid currentColor; border-right: 2px solid currentColor; content: ""; }
.symbol-stop { border: 2px solid currentColor; border-radius: 3px; }
.symbol-stop::after { position: absolute; inset: 3px; background: currentColor; content: ""; }
.symbol-off { border: 2px solid currentColor; border-radius: 50%; }
.symbol-off::after { position: absolute; top: -2px; left: 4px; width: 2px; height: 7px; background: currentColor; content: ""; }
.symbol-speed-low, .symbol-speed-medium, .symbol-speed-high { border-bottom: 2px solid currentColor; }
.symbol-speed-low::before, .symbol-speed-medium::before, .symbol-speed-high::before,
.symbol-speed-low::after, .symbol-speed-medium::after, .symbol-speed-high::after {
  position: absolute;
  bottom: 1px;
  width: 3px;
  background: currentColor;
  content: "";
}
.symbol-speed-low::before { left: 1px; height: 4px; }
.symbol-speed-low::after { left: 7px; height: 6px; }
.symbol-speed-medium::before { left: 1px; height: 6px; }
.symbol-speed-medium::after { left: 7px; height: 9px; }
.symbol-speed-high::before { left: 1px; height: 8px; }
.symbol-speed-high::after { left: 7px; height: 12px; }

.undo-row {
  display: flex;
  min-height: 48px;
  align-items: center;
  justify-content: space-between;
  gap: 14px;
  margin-top: 8px;
}

.undo-button {
  display: inline-flex;
  min-height: 42px;
  align-items: center;
  gap: 8px;
  padding: 0 11px;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: var(--paper);
  color: #49544f;
  font-size: 13px;
  cursor: pointer;
}

.undo-button > span:first-child { font-size: 18px; line-height: 1; }
.undo-button:hover:not(:disabled) { border-color: #aebdb6; background: #f8faf9; }
.undo-button:disabled { color: #a2aaa6; cursor: not-allowed; }
#syncNote { color: var(--muted); font-size: 11px; text-align: right; }

.history-section { padding-top: 25px; }
.history-section .section-heading { margin-bottom: 4px; }
.history-list { margin: 0; padding: 0; list-style: none; }

.history-item {
  display: grid;
  min-height: 56px;
  grid-template-columns: 13px minmax(0, 1fr) auto;
  align-items: center;
  gap: 12px;
  border-bottom: 1px solid #edf0ee;
}

.history-marker { width: 8px; height: 8px; border-radius: 50%; background: #7b8982; }
.history-marker[data-kind="start"] { background: var(--green); }
.history-marker[data-kind="event"] { background: var(--amber); }
.history-marker[data-kind="stop"] { background: var(--blue); }
.history-marker[data-kind="off"] { background: var(--red); }
.history-label { overflow: hidden; font-size: 13px; text-overflow: ellipsis; white-space: nowrap; }
.history-time { color: var(--muted); font-size: 12px; font-variant-numeric: tabular-nums; white-space: nowrap; }
.empty-state { padding: 30px 0; color: #89938e; font-size: 13px; text-align: center; }

.app-footer {
  display: flex;
  min-height: 54px;
  align-items: center;
  justify-content: space-between;
  padding: 0 max(24px, calc((100vw - 1040px) / 2));
  border-top: 1px solid var(--line);
  color: #8a948f;
  font-size: 11px;
}

.settings-dialog {
  width: min(100% - 32px, 440px);
  padding: 22px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--paper);
  color: var(--ink);
  box-shadow: 0 18px 60px #11251b2b;
}

.safety-dialog {
  width: min(100% - 32px, 480px);
  max-height: calc(100% - 32px);
  padding: 24px;
  overflow: auto;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--paper);
  color: var(--ink);
  box-shadow: 0 18px 60px #11251b2b;
}

.safety-dialog::backdrop { background: #19231f80; }
.safety-dialog form { display: grid; gap: 13px; }
.safety-dialog h2 { margin: 0; font-size: 21px; }
.safety-copy { margin: 0 0 6px; color: #46514c; font-size: 15px; line-height: 1.75; }
.safety-dialog .primary-button { width: 100%; }

.settings-dialog::backdrop { background: #19231f70; }
.settings-dialog form { display: grid; gap: 10px; }
.dialog-heading { display: flex; justify-content: space-between; align-items: flex-start; margin-bottom: 12px; }
.dialog-heading h2 { margin: 0; font-size: 20px; }
.dialog-heading .eyebrow { margin-bottom: 6px; }
.close-button { min-width: 38px; padding: 0; font-size: 25px; line-height: 1; }
.optional-label { padding-left: 4px; color: var(--muted); font-size: 11px; font-weight: 400; }
.dialog-note { margin: 0; color: var(--muted); font-size: 12px; line-height: 1.5; }
.dialog-actions { display: flex; justify-content: flex-end; gap: 9px; margin-top: 14px; }
.dialog-actions .primary-button { min-height: 42px; }

[hidden] { display: none !important; }

@media (max-width: 620px) {
  .topbar { min-height: 58px; padding: 0 18px; }
  .topbar-actions { gap: 10px; }
  .topbar .settings-button, #recordAccountButton { min-width: 40px; min-height: 44px; padding: 0 3px; font-size: 12px; }
  #connectionText { display: none; }
  main { width: calc(100% - 36px); }
  .setup-view { margin-top: 88px; }
  .trip-history-section { margin-top: 42px; }
  .record-view { padding-top: 31px; }
  .record-heading { align-items: flex-start; padding-bottom: 20px; }
  .record-heading h1 { max-width: 62vw; font-size: 26px; }
  .action-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 8px; }
  .action-button { min-height: 72px; padding: 11px 12px; }
  .history-item { grid-template-columns: 10px minmax(0, 1fr); grid-template-areas: "marker label" "marker time"; gap: 5px 10px; padding: 12px 0; }
  .history-marker { grid-area: marker; align-self: start; margin-top: 4px; }
  .history-label { grid-area: label; overflow: visible; overflow-wrap: anywhere; white-space: normal; line-height: 1.5; }
  .history-time { grid-area: time; }
  .undo-row { align-items: flex-start; flex-direction: column; gap: 5px; padding-top: 4px; }
  #syncNote { padding-left: 2px; text-align: left; }
  .app-footer { min-height: 48px; padding: 0 18px; }
}

@media (max-width: 360px) {
  .connection { gap: 5px; font-size: 11px; }
  .wordmark { gap: 7px; font-size: 14px; }
  .topbar-actions { gap: 5px; }
  .history-item { grid-template-columns: 10px minmax(0, 1fr); gap: 5px 8px; }
  .history-label { font-size: 12px; }
  .history-time { font-size: 11px; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { scroll-behavior: auto !important; transition-duration: 0.01ms !important; }
}
