:root {
  color-scheme: light;
  --page: #f2f4f7;
  --surface: #ffffff;
  --surface-soft: #f8fafb;
  --ink: #17202a;
  --muted: #66717e;
  --line: #d9dee5;
  --brand: #b4232c;
  --brand-dark: #861b22;
  --green: #18794e;
  --green-soft: #e9f6ef;
  --amber: #9a6700;
  --amber-soft: #fff5d6;
  --blue: #1769aa;
  --blue-soft: #eaf4fb;
  --red-soft: #fbeaec;
  --shadow: 0 10px 28px rgba(23, 32, 42, 0.08);
}

:root[data-theme="dark"] {
  color-scheme: dark;
  --page: #101317;
  --surface: #1a1f25;
  --surface-soft: #222830;
  --ink: #f2f5f7;
  --muted: #aab4bf;
  --line: #343c45;
  --green: #4fbc84;
  --green-soft: #173a2a;
  --amber: #e3b341;
  --amber-soft: #3b3015;
  --blue-soft: #17364d;
  --red-soft: #452127;
  --shadow: 0 10px 28px rgba(0, 0, 0, 0.28);
}

:root[data-theme="dark"] .topbar,
:root[data-theme="dark"] .bottom-nav { background: rgba(26, 31, 37, 0.96); }
:root[data-theme="dark"] label,
:root[data-theme="dark"] legend { color: #c1c9d1; }
:root[data-theme="dark"] input,
:root[data-theme="dark"] select,
:root[data-theme="dark"] textarea { color: var(--ink); background: #14191e; border-color: #46505b; }
:root[data-theme="dark"] input:disabled,
:root[data-theme="dark"] select:disabled,
:root[data-theme="dark"] textarea:disabled { background: #252b32; }
:root[data-theme="dark"] .secondary,
:root[data-theme="dark"] .badge.gray,
:root[data-theme="dark"] .segmented { background: #29313a; }
:root[data-theme="dark"] .segmented input:checked + span { background: #3a444f; box-shadow: none; }
:root[data-theme="dark"] .response-actions .self { color: #b8def5; border-color: #315a72; }
:root[data-theme="dark"] .response-actions .decline { color: #ffc7ce; border-color: #69353c; }
:root[data-theme="dark"] .privacy-note { color: #f2d27b; }

* { box-sizing: border-box; }

html { min-width: 320px; min-height: 100%; background: var(--page); }

body {
  min-height: 100%;
  margin: 0;
  color: var(--ink);
  background: var(--page);
  background-image: linear-gradient(rgba(242, 244, 247, 0.88), rgba(242, 244, 247, 0.88)), var(--custom-background, none);
  background-size: cover;
  background-position: center;
  background-attachment: fixed;
  font-family: system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  font-size: 16px;
  letter-spacing: 0;
}

:root[data-theme="dark"] body {
  background-image: linear-gradient(rgba(16, 19, 23, 0.88), rgba(16, 19, 23, 0.88)), var(--custom-background, none);
}

button, input, select, textarea { font: inherit; letter-spacing: 0; }
button, summary, select, input[type="checkbox"], input[type="radio"] { cursor: pointer; }
button:focus-visible, input:focus-visible, select:focus-visible, textarea:focus-visible, summary:focus-visible {
  outline: 3px solid rgba(23, 105, 170, 0.28);
  outline-offset: 2px;
}

h1, h2, h3, p { margin: 0; }
h1 { font-size: 1.03rem; line-height: 1.2; }
h2 { font-size: 1.22rem; line-height: 1.25; }
h3 { font-size: 0.98rem; line-height: 1.3; }

.app-shell { min-height: 100vh; display: grid; grid-template-rows: auto 1fr auto; }

.topbar {
  position: sticky;
  top: 0;
  z-index: 20;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  min-height: 68px;
  padding: 10px max(14px, env(safe-area-inset-left)) 10px max(14px, env(safe-area-inset-right));
  background: rgba(255, 255, 255, 0.96);
  border-bottom: 1px solid var(--line);
  backdrop-filter: blur(12px);
}

.brand { display: flex; align-items: center; gap: 10px; min-width: 0; }
.brand img { width: 44px; height: 44px; object-fit: contain; flex: 0 0 auto; }
.brand div { min-width: 0; }
.brand p { margin-top: 2px; color: var(--muted); font-size: 0.78rem; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.session-tools { display: flex; align-items: center; gap: 8px; flex: 0 0 auto; }
.chat-toggle { position: relative; }
.session-name { display: none; color: var(--muted); font-size: 0.88rem; font-weight: 700; }

.main-content { width: min(900px, 100%); margin: 0 auto; padding: 18px 14px 96px; }
.screen { display: grid; gap: 14px; }
.screen.narrow { width: min(520px, 100%); margin: 0 auto; }

.panel {
  padding: 16px;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 8px;
  box-shadow: var(--shadow);
}
.panel.soft { background: var(--surface-soft); box-shadow: none; }
.stack { display: grid; gap: 14px; }
.compact { gap: 9px; }
.center { text-align: center; justify-items: center; }
.split { display: flex; align-items: center; justify-content: space-between; gap: 12px; }
.section-heading { display: grid; gap: 4px; }
.muted { color: var(--muted); }
.fine-print { color: var(--muted); font-size: 0.84rem; line-height: 1.45; }
.eyebrow { color: var(--muted); font-size: 0.75rem; font-weight: 800; text-transform: uppercase; }

.loading { min-height: 55vh; display: grid; place-content: center; justify-items: center; gap: 12px; color: var(--muted); }
.loading span { width: 28px; height: 28px; border: 3px solid var(--line); border-top-color: var(--brand); border-radius: 50%; animation: spin 0.8s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

.login { min-height: calc(100vh - 190px); display: grid; place-content: center; gap: 16px; text-align: center; }
.login > img { width: 90px; height: 90px; margin: 0 auto; object-fit: contain; }
.login > p { color: var(--muted); }
.login .panel { width: min(400px, calc(100vw - 28px)); text-align: left; }

label { display: grid; gap: 7px; color: #4f5b67; font-size: 0.88rem; font-weight: 750; }
.optional-label { color: var(--muted); font-size: 0.78rem; font-weight: 600; }
fieldset { min-width: 0; margin: 0; padding: 0; border: 0; }
legend { margin-bottom: 7px; color: #4f5b67; font-size: 0.88rem; font-weight: 750; }
input, select, textarea {
  width: 100%;
  min-height: 46px;
  padding: 11px 12px;
  color: var(--ink);
  background: var(--surface);
  border: 1px solid #c9d0d8;
  border-radius: 7px;
}
textarea { min-height: 90px; resize: vertical; }
input:disabled, select:disabled, textarea:disabled { color: var(--muted); background: #eef1f4; }

button {
  min-height: 46px;
  padding: 0 15px;
  border: 0;
  border-radius: 7px;
  font-weight: 800;
}
button:disabled { opacity: 0.45; cursor: not-allowed; }
.primary { color: #fff; background: var(--brand); }
.primary:hover { background: var(--brand-dark); }
.secondary { color: var(--ink); background: #e8edf2; }
.outline { color: var(--ink); background: transparent; border: 1px solid var(--line); }
.danger { color: #fff; background: #9f2430; }
.full { width: 100%; }
.small { min-height: 36px; padding: 0 11px; font-size: 0.84rem; }
.link-button { min-height: 36px; padding: 0 4px; color: var(--brand); background: transparent; }

.check { display: flex; align-items: center; gap: 10px; color: var(--ink); }
.check input { width: 21px; min-height: 21px; height: 21px; margin: 0; }
.check-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); gap: 8px; }
.crew-select { padding: 12px; border: 1px solid var(--line); border-radius: 8px; background: var(--surface-soft); }

.badge { display: inline-flex; align-items: center; justify-content: center; gap: 6px; min-height: 27px; padding: 4px 9px; border-radius: 999px; font-size: 0.76rem; font-weight: 850; white-space: nowrap; }
.badge::before { content: ""; width: 7px; height: 7px; border-radius: 50%; background: currentColor; }
.badge.green { color: var(--green); background: var(--green-soft); }
.badge.amber { color: var(--amber); background: var(--amber-soft); }
.badge.red { color: var(--brand); background: var(--red-soft); }
.badge.blue { color: var(--blue); background: var(--blue-soft); }
.badge.gray { color: var(--muted); background: #edf0f3; }

.quiet-state { min-height: 210px; align-content: center; }
.state-mark { width: 54px; height: 54px; display: grid; place-items: center; border-radius: 50%; color: var(--green); background: var(--green-soft); font-size: 1.5rem; font-weight: 900; }
.vehicle-line { display: flex; align-items: center; justify-content: space-between; gap: 14px; }
.vehicle-name { font-size: 1.12rem; font-weight: 850; }

.alarm-banner { display: grid; gap: 12px; padding: 18px; color: #fff; background: var(--brand); border-radius: 8px; box-shadow: var(--shadow); }
.alarm-banner.critical { background: #8e101c; }
.alarm-banner.medium { background: #a82a20; }
.alarm-banner.other { background: var(--blue); }
.alarm-banner .eyebrow, .alarm-banner .muted { color: rgba(255, 255, 255, 0.82); }
.alarm-banner h2 { font-size: 1.65rem; }
.alarm-note { padding-top: 10px; border-top: 1px solid rgba(255, 255, 255, 0.28); font-size: 1.03rem; line-height: 1.45; white-space: pre-wrap; }
.alarm-meta { display: flex; justify-content: space-between; gap: 12px; font-size: 0.86rem; font-weight: 750; }
.alarm-banner .outline { color: #fff; border-color: rgba(255, 255, 255, 0.52); }

.response-actions { display: grid; gap: 9px; }
.response-actions button { min-height: 54px; text-align: left; }
.response-actions button span { display: block; font-size: 0.8rem; font-weight: 600; opacity: 0.78; margin-top: 2px; }
.response-actions .pickup { color: #fff; background: var(--green); }
.response-actions .self { color: #0f527e; background: var(--blue-soft); border: 1px solid #b9d9eb; }
.response-actions .decline { color: #842029; background: var(--red-soft); border: 1px solid #efc5ca; }

.segmented { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 6px; padding: 5px; background: #edf1f4; border-radius: 8px; }
.segmented.two { grid-template-columns: repeat(2, minmax(0, 1fr)); }
.segmented.priority-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); }
.segmented label { display: block; position: relative; color: var(--muted); text-align: center; }
.segmented input { position: absolute; width: 1px; height: 1px; min-height: 0; margin: 0; opacity: 0; pointer-events: none; }
.segmented span { display: grid; place-items: center; min-height: 40px; padding: 6px; border-radius: 6px; font-size: 0.82rem; font-weight: 800; }
.segmented.priority-grid span { min-height: 54px; }
.segmented.priority-grid small { color: var(--muted); font-size: 0.72rem; font-weight: 650; }
.segmented input:checked + span { color: var(--ink); background: #fff; box-shadow: 0 1px 4px rgba(23, 32, 42, 0.12); }

.status-choices { display: grid; gap: 8px; }
.status-choice { display: grid; grid-template-columns: 15px 1fr; align-items: center; gap: 10px; min-height: 58px; padding: 10px 12px; color: var(--ink); background: var(--surface-soft); border: 1px solid var(--line); text-align: left; }
.status-choice::before { content: ""; width: 11px; height: 11px; border-radius: 50%; background: var(--muted); }
.status-choice.ready::before { background: var(--green); }
.status-choice.away::before { background: var(--amber); }
.status-choice.unavailable::before { background: var(--brand); }
.status-choice.selected { border-color: var(--blue); background: var(--blue-soft); }
.status-choice small { display: block; margin-top: 2px; color: var(--muted); font-weight: 600; }

.stats { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 8px; }
.stat { min-height: 70px; display: grid; align-content: center; justify-items: center; padding: 9px; background: var(--surface-soft); border: 1px solid var(--line); border-radius: 7px; }
.stat strong { font-size: 1.35rem; }
.stat span { color: var(--muted); font-size: 0.78rem; font-weight: 750; }

.response-group { display: grid; gap: 8px; }
.person { display: grid; grid-template-columns: 1fr auto; gap: 4px 12px; align-items: center; min-height: 58px; padding: 11px 12px; background: var(--surface-soft); border: 1px solid var(--line); border-radius: 7px; }
.person-details { grid-column: 1 / -1; display: grid; gap: 3px; color: var(--muted); font-size: 0.88rem; }
.person-details a { color: var(--blue); text-decoration: none; font-weight: 750; }
.picked { opacity: 0.62; }

.inline { display: grid; grid-template-columns: 1fr auto; gap: 8px; align-items: end; }
.form-grid { display: grid; gap: 12px; }
.admin-list { display: grid; gap: 8px; }
.admin-user { background: var(--surface); border: 1px solid var(--line); border-radius: 7px; overflow: clip; }
.admin-user summary { display: flex; align-items: center; justify-content: space-between; gap: 10px; min-height: 58px; padding: 12px; list-style: none; }
.admin-user summary::-webkit-details-marker { display: none; }
.admin-user summary::after { content: "+"; color: var(--muted); font-size: 1.2rem; font-weight: 500; }
.admin-user[open] summary::after { content: "−"; }
.admin-user form { padding: 0 12px 12px; border-top: 1px solid var(--line); }
.button-row { display: flex; flex-wrap: wrap; gap: 8px; }
.button-row > * { flex: 1 1 130px; }

.log-list { display: grid; }
.log-item { display: grid; gap: 3px; padding: 10px 0; border-bottom: 1px solid var(--line); }
.log-item:last-child { border-bottom: 0; }
.log-item time { color: var(--muted); font-size: 0.8rem; }

.privacy-note { padding: 11px 12px; color: #6e4f00; background: var(--amber-soft); border-left: 4px solid var(--amber); border-radius: 7px; font-size: 0.86rem; line-height: 1.45; }
.document-list { display: grid; gap: 8px; }
.document-entry { background: var(--surface-soft); border: 1px solid var(--line); border-radius: 7px; overflow: clip; }
.document-entry > summary { display: flex; align-items: center; justify-content: space-between; gap: 10px; min-height: 64px; padding: 12px; list-style: none; }
.document-entry > summary::-webkit-details-marker { display: none; }
.document-entry > summary small { color: var(--muted); font-size: 0.8rem; }
.document-entry > summary .badge { max-width: 42%; overflow: hidden; text-overflow: ellipsis; }
.document-body { padding: 12px; border-top: 1px solid var(--line); background: var(--surface); }
.document-preview { margin: 0; padding: 12px; overflow: auto; color: var(--ink); background: var(--surface-soft); border: 1px solid var(--line); border-radius: 7px; font: inherit; font-size: 0.9rem; line-height: 1.55; white-space: pre-wrap; overflow-wrap: anywhere; }
.document-meta { color: var(--muted); font-size: 0.8rem; }
.edit-document { border-top: 1px solid var(--line); padding-top: 10px; }
.edit-document > summary { color: var(--blue); font-weight: 800; }
.edit-document form { margin-top: 12px; }

.bottom-nav { position: fixed; left: 0; right: 0; bottom: 0; z-index: 30; display: grid; grid-auto-flow: column; gap: 6px; padding: 8px 14px calc(8px + env(safe-area-inset-bottom)); background: rgba(255, 255, 255, 0.97); border-top: 1px solid var(--line); backdrop-filter: blur(12px); }
.bottom-nav[hidden] { display: none; }
.bottom-nav button { min-height: 48px; color: var(--muted); background: transparent; }
.bottom-nav button.active { color: var(--brand); background: var(--red-soft); }

.toast { position: fixed; left: 50%; bottom: calc(82px + env(safe-area-inset-bottom)); z-index: 50; width: min(440px, calc(100% - 28px)); transform: translateX(-50%); padding: 12px 14px; color: #fff; background: #26323e; border-radius: 7px; box-shadow: var(--shadow); font-weight: 750; text-align: center; }
.toast.error { background: #8d1f29; }

.chat-drawer {
  position: fixed;
  inset: 0;
  z-index: 45;
  display: none;
  justify-content: flex-end;
  background: rgba(23, 32, 42, 0.22);
}
.chat-drawer.open { display: flex; }
.chat-panel {
  width: min(420px, 100%);
  min-height: 100%;
  display: grid;
  grid-template-rows: auto 1fr auto;
  gap: 12px;
  padding: 16px;
  background: var(--surface);
  border-left: 1px solid var(--line);
  box-shadow: var(--shadow);
}
.chat-list { min-height: 0; display: grid; align-content: start; gap: 9px; overflow: auto; padding: 4px; }
.chat-message { max-width: 86%; display: grid; gap: 4px; justify-self: start; padding: 10px 12px; background: var(--surface-soft); border: 1px solid var(--line); border-radius: 8px; }
.chat-message.own { justify-self: end; color: #fff; background: var(--blue); border-color: var(--blue); }
.chat-message p { line-height: 1.35; overflow-wrap: anywhere; }
.chat-message time { color: var(--muted); font-size: 0.72rem; }
.chat-message.own time { color: rgba(255, 255, 255, 0.72); }
.chat-form { display: grid; grid-template-columns: 1fr auto; gap: 8px; }

:root[data-motion="full"] .panel,
:root[data-motion="full"] .alarm-banner,
:root[data-motion="full"] .chat-panel,
:root[data-motion="full"] .toast {
  animation: rise-in 180ms ease-out both;
}
:root[data-motion="full"] button,
:root[data-motion="full"] .status-choice,
:root[data-motion="full"] .person {
  transition: transform 140ms ease, background-color 140ms ease, border-color 140ms ease;
}
:root[data-motion="full"] button:enabled:active,
:root[data-motion="full"] .status-choice:active { transform: scale(0.98); }
:root[data-motion="reduced"] .panel,
:root[data-motion="reduced"] .chat-panel { animation: fade-in 120ms ease-out both; }
:root[data-motion="off"] *,
:root[data-motion="off"] *::before,
:root[data-motion="off"] *::after {
  animation: none !important;
  transition: none !important;
}
@keyframes rise-in {
  from { opacity: 0; transform: translateY(8px); }
  to { opacity: 1; transform: translateY(0); }
}
@keyframes fade-in {
  from { opacity: 0; }
  to { opacity: 1; }
}

@media (min-width: 720px) {
  .session-name { display: inline; }
  .main-content { padding-top: 26px; }
  .form-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .form-grid .wide { grid-column: 1 / -1; }
  .stats { grid-template-columns: repeat(4, minmax(0, 1fr)); }
  .admin-layout { grid-template-columns: minmax(310px, 0.8fr) minmax(380px, 1.2fr); align-items: start; }
  .admin-layout .logs-panel { grid-column: 1 / -1; }
  .documentation-layout { grid-template-columns: minmax(340px, 0.9fr) minmax(380px, 1.1fr); align-items: start; }
}

@media (max-width: 420px) {
  .topbar { gap: 7px; }
  .brand { gap: 8px; }
  .brand img { width: 38px; height: 38px; }
  .brand p { max-width: 45vw; }
  .session-tools .badge { display: none; }
  .main-content { padding-left: 10px; padding-right: 10px; }
  .panel { padding: 14px; }
}

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