:root {
  color-scheme: light;
  --ink: #10212c;
  --ink-soft: #53636d;
  --navy: #0b2638;
  --navy-hover: #123b54;
  --paper: #f4f1e8;
  --surface: #fffdf7;
  --line: #d8d3c7;
  --amber: #f4b942;
  --amber-dark: #6f4b00;
  --green: #176b4d;
  --red: #a7372a;
  --warning: #fff0c8;
  --radius: 10px;
  --shadow: 0 8px 24px rgb(16 33 44 / 8%);
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  min-width: 300px;
  background: var(--paper);
  color: var(--ink);
  font-size: 16px;
  line-height: 1.45;
}

button, input, select, textarea { font: inherit; }
button, select { min-height: 46px; }
button { cursor: pointer; }
button:disabled { cursor: not-allowed; opacity: .55; }

a { color: inherit; }

h1, h2, h3, p { margin-top: 0; }
h1 { margin-bottom: .35rem; font-size: clamp(1.55rem, 5vw, 2.2rem); line-height: 1.12; }
h2 { margin-bottom: 1rem; font-size: 1.35rem; }
h3 { margin: 0; font-size: 1rem; }

.app-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  min-height: 72px;
  padding: 10px max(16px, env(safe-area-inset-left)) 10px max(16px, env(safe-area-inset-right));
  background: var(--navy);
  color: white;
}

.brand {
  display: flex;
  align-items: center;
  gap: 10px;
  color: white;
  text-decoration: none;
}

.brand strong, .brand small { display: block; }
.brand strong { font-size: 1rem; letter-spacing: .01em; }
.brand small { margin-top: 1px; color: #b9cad4; font-size: .72rem; }

.brand-mark {
  display: grid;
  width: 40px;
  height: 40px;
  place-items: center;
  flex: 0 0 auto;
  border-radius: 7px;
  background: var(--amber);
  color: var(--navy);
  font-size: 1.5rem;
  font-weight: 900;
}

.header-link {
  padding: 9px 7px;
  color: #e9f1f5;
  font-size: .9rem;
  font-weight: 700;
  text-underline-offset: 4px;
  white-space: nowrap;
}

.page-shell {
  width: min(100% - 24px, 1120px);
  margin: 0 auto;
  padding: 14px 0 36px;
}

.connection-strip {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  min-height: 32px;
  margin-bottom: 10px;
  font-size: .78rem;
}

.status-dot::before {
  display: inline-block;
  width: 8px;
  height: 8px;
  margin-right: 7px;
  border-radius: 50%;
  background: #8a969d;
  content: "";
}
.status-dot.online::before { background: var(--green); }
.status-dot.offline::before { background: var(--red); }

.panel {
  padding: 20px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--surface);
  box-shadow: var(--shadow);
}

.picker-panel {
  display: grid;
  gap: 16px;
  max-width: 1120px;
  margin: 2vh auto 0;
}

.picker-content {
  display: grid;
  grid-template-columns: minmax(0, 1.7fr) minmax(280px, .8fr);
  gap: 16px;
  align-items: start;
}

.map-section { min-width: 0; }
.stops-map {
  width: 100%;
  height: clamp(360px, 56vh, 580px);
  overflow: hidden;
  border: 1px solid var(--line);
  border-radius: 7px;
  background: #dce6e7;
}
.map-status { min-height: 1.25rem; margin: 7px 2px 0; }
.stops-map .maplibregl-ctrl button { min-height: 29px; }
.stop-map-marker {
  width: 36px;
  min-height: 36px;
  padding: 0;
  border: 3px solid white;
  border-radius: 50%;
  background: var(--navy);
  box-shadow: 0 2px 8px rgb(16 33 44 / 32%);
}
.stop-map-marker::after {
  display: block;
  width: 10px;
  height: 10px;
  margin: auto;
  border-radius: 50%;
  background: var(--amber);
  content: "";
}
.stop-map-marker:hover { background: var(--navy-hover); }
.stop-map-marker:focus-visible { outline: 4px solid var(--amber); outline-offset: 3px; }
.stop-map-marker.selected {
  border-color: var(--navy);
  background: var(--amber);
  box-shadow: 0 0 0 4px rgb(244 185 66 / 34%), 0 2px 8px rgb(16 33 44 / 32%);
}
.stop-map-marker.selected::after { background: var(--navy); }
.stop-map-popup { display: grid; gap: 4px; min-width: 150px; color: var(--ink); }
.stop-map-popup a { color: var(--navy); font-weight: 850; text-underline-offset: 3px; }
.stop-map-popup small { color: var(--ink-soft); }
.stops-map .maplibregl-popup-content { border-radius: 7px; padding: 12px 14px; }

.stop-links { display: grid; gap: 8px; }
.stop-option {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  gap: 8px;
  min-width: 0;
  padding: 5px;
  border: 1px solid var(--line);
  border-radius: 7px;
  background: #f8f6f0;
}
.stop-option:hover { border-color: var(--navy); background: #f1eee5; }
.stop-option.selected { border-color: var(--navy); box-shadow: inset 4px 0 var(--amber); }
.stop-focus-button {
  display: flex;
  min-width: 0;
  flex-direction: column;
  align-items: flex-start;
  justify-content: space-between;
  gap: 3px;
  padding: 7px 8px;
  border: 0;
  border-radius: 5px;
  background: transparent;
  color: var(--ink);
  text-align: left;
}
.stop-focus-button strong { overflow-wrap: anywhere; }
.stop-option-details { display: flex; flex-wrap: wrap; gap: 4px 8px; color: var(--ink-soft); font-size: .75rem; }
.stop-selected-label { display: none; color: var(--green); font-weight: 850; }
.stop-option.selected .stop-selected-label { display: inline; }
.stop-forecast-button {
  display: inline-flex;
  min-height: 46px;
  align-items: center;
  align-self: stretch;
  padding: 8px 10px;
  border-radius: 6px;
  background: var(--navy);
  color: white;
  font-size: .82rem;
  font-weight: 850;
  text-decoration: none;
}
.stop-forecast-button:hover { background: var(--navy-hover); }

.eyebrow {
  margin-bottom: .35rem;
  color: var(--amber-dark);
  font-size: .72rem;
  font-weight: 900;
  letter-spacing: .12em;
  text-transform: uppercase;
}

.muted { color: var(--ink-soft); font-size: .86rem; }

label { font-size: .87rem; font-weight: 800; }

input, select, textarea {
  width: 100%;
  border: 1px solid #a9b0b3;
  border-radius: 7px;
  background: white;
  color: var(--ink);
}
input, select { padding: 10px 12px; }
textarea { padding: 10px 12px; resize: vertical; }
input:focus, select:focus, textarea:focus, button:focus-visible, a:focus-visible {
  outline: 3px solid rgb(244 185 66 / 55%);
  outline-offset: 2px;
}

.primary-button, .secondary-button, .danger-button, .text-button {
  border-radius: 7px;
  font-weight: 850;
}
.primary-button {
  border: 1px solid var(--navy);
  background: var(--navy);
  color: white;
  padding: 11px 16px;
}
.primary-button:hover { background: var(--navy-hover); }
.secondary-button {
  border: 2px solid var(--navy);
  background: transparent;
  color: var(--navy);
  padding: 11px 16px;
}
.danger-button {
  border: 1px solid #d4a29c;
  background: #fff5f3;
  color: var(--red);
  padding: 10px 14px;
}
.text-button {
  border: 0;
  background: transparent;
  color: var(--navy);
  padding: 8px;
  text-decoration: underline;
  text-underline-offset: 4px;
}
.compact { min-height: 40px; padding: 8px 12px; }
.full-width { width: 100%; margin-top: 14px; }

.workspace-heading, .section-heading {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 14px;
}
.workspace-heading { margin: 10px 2px 14px; }
.section-heading { align-items: center; margin-bottom: 14px; }
.section-heading h1, .section-heading h2 { margin-bottom: 0; }

.notice {
  margin-bottom: 12px;
  padding: 11px 13px;
  border-radius: 7px;
  font-size: .87rem;
}
.notice.warning { border: 1px solid #e2c373; background: var(--warning); color: #4f3b0a; }

.table-wrap {
  overflow: hidden;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--surface);
  box-shadow: var(--shadow);
}

table { width: 100%; border-collapse: collapse; }
th {
  padding: 11px 10px;
  background: #e9e5db;
  color: #46545c;
  font-size: .72rem;
  letter-spacing: .055em;
  text-align: left;
  text-transform: uppercase;
}
td { padding: 13px 10px; border-top: 1px solid var(--line); vertical-align: middle; }
tbody tr:first-child td { border-top: 0; }
tbody tr.recorded { background: #edf8f2; }

.route-number { font-size: 1.35rem; font-weight: 950; }
.registration-number { display: block; font-size: 1.06rem; font-weight: 950; letter-spacing: .035em; }
.vehicle-id { font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-weight: 850; }
.cell-secondary { display: block; margin-top: 2px; color: var(--ink-soft); font-size: .72rem; }
.vehicle-details { display: flex; flex-wrap: wrap; gap: 2px 8px; }
.time-value { font-variant-numeric: tabular-nums; font-weight: 800; }
.countdown { min-width: 108px; font-variant-numeric: tabular-nums; font-weight: 900; }
.countdown.overdue { color: var(--red); }

.source-badge {
  display: inline-flex;
  align-items: center;
  min-height: 28px;
  padding: 3px 8px;
  border-radius: 999px;
  background: #e8f3ed;
  color: var(--green);
  font-size: .72rem;
  font-weight: 850;
}
.source-badge.stale { background: #fff0c8; color: #76520a; }
.source-badge.lost { background: #f9e3df; color: var(--red); }

.arrival-actions { display: grid; min-width: 112px; gap: 6px; }
.arrival-button, .not-arrived-button {
  width: 100%;
  min-width: 106px;
  border-radius: 7px;
  padding: 10px 12px;
  font-weight: 950;
  text-transform: uppercase;
}
.arrival-button { border: 0; background: var(--amber); color: #2d2105; }
.arrival-button:hover { background: #ffc951; }
.arrival-button.recorded { background: #d7ece1; color: var(--green); }
.not-arrived-button { border: 2px solid #b85a4e; background: #fff8f6; color: var(--red); }
.not-arrived-button:hover { background: #f9e3df; }

.table-state { padding: 28px 16px; color: var(--ink-soft); text-align: center; }
.table-state.error { color: var(--red); }

.toast {
  position: fixed;
  z-index: 20;
  right: 16px;
  bottom: max(16px, env(safe-area-inset-bottom));
  left: 16px;
  max-width: 520px;
  margin: auto;
  padding: 13px 16px;
  border-radius: 7px;
  background: var(--navy);
  box-shadow: var(--shadow);
  color: white;
  font-weight: 750;
}

dialog {
  width: min(100% - 24px, 500px);
  max-height: calc(100dvh - 24px);
  padding: 0;
  border: 0;
  border-radius: var(--radius);
  box-shadow: 0 24px 70px rgb(0 0 0 / 30%);
}
dialog::backdrop { background: rgb(5 21 30 / 66%); }
.dialog-content { display: grid; gap: 10px; padding: 22px; }
.dialog-content h2, .dialog-content p { margin-bottom: 0; }
.dialog-actions { display: grid; gap: 9px; margin-top: 6px; }
.dialog-actions.horizontal { grid-template-columns: 1fr auto; align-items: center; }
.result-summary { padding: 12px; border-left: 4px solid var(--amber); background: #f6f2e9; }
.form-error { margin: 0; color: var(--red); font-size: .84rem; font-weight: 750; }

.admin-layout { display: grid; gap: 14px; }
.stop-list { display: grid; gap: 8px; }
.stop-item {
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 8px 12px;
  padding: 12px;
  border: 1px solid var(--line);
  border-radius: 7px;
}
.stop-item p { grid-column: 1 / -1; margin: 0; color: var(--ink-soft); font-size: .8rem; }
.stop-item.archived { opacity: .6; }
.stack-form { display: grid; gap: 9px; }

.facts-page { display: grid; gap: 14px; }
.facts-heading { display: flex; align-items: end; justify-content: space-between; margin-top: 6px; }
.facts-heading h1, .facts-heading p { margin-bottom: 0; }
.facts-filters {
  display: grid;
  grid-template-columns: repeat(2, minmax(130px, .75fr)) minmax(190px, 1.35fr) minmax(130px, .8fr) minmax(150px, .9fr) auto;
  gap: 12px;
  align-items: end;
  padding: 16px;
}
.filter-field { display: grid; gap: 5px; min-width: 0; }
.facts-filters input,
.facts-filters select,
.facts-filter-actions button {
  height: 48px;
  min-height: 48px;
}
.facts-filters input, .facts-filters select { min-width: 0; }
.facts-filters select {
  appearance: none;
  padding-right: 44px;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='8' viewBox='0 0 12 8'%3E%3Cpath d='m1 1 5 5 5-5' fill='none' stroke='%2310212c' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'/%3E%3C/svg%3E");
  background-position: right 16px center;
  background-repeat: no-repeat;
  background-size: 12px 8px;
}
.facts-filter-actions { display: flex; align-items: center; gap: 6px; }
.facts-filter-actions .primary-button { white-space: nowrap; }
.facts-results { min-width: 0; }
.facts-table { font-size: .87rem; }
.facts-table th, .facts-table td { padding-right: 9px; padding-left: 9px; }
.facts-table .fact-time { min-width: 142px; font-variant-numeric: tabular-nums; font-weight: 750; }
.facts-table .fact-route { font-size: 1.15rem; font-weight: 950; }
.facts-table .fact-vehicle { min-width: 126px; }
.facts-table .fact-deviation { min-width: 130px; font-weight: 750; }
.facts-table .fact-comment { max-width: 220px; overflow-wrap: anywhere; }
.facts-table tr.canceled { color: var(--ink-soft); background: #f1efeb; }
.fact-status {
  display: inline-flex;
  min-height: 28px;
  align-items: center;
  padding: 3px 8px;
  border-radius: 999px;
  background: #e8f3ed;
  color: var(--green);
  font-size: .72rem;
  font-weight: 850;
}
.fact-status.canceled { background: #f9e3df; color: var(--red); }
.fact-status.not-arrived { background: var(--warning); color: #76520a; }
.facts-pagination {
  display: grid;
  grid-template-columns: minmax(100px, auto) minmax(0, 1fr) minmax(100px, auto);
  gap: 12px;
  align-items: center;
  margin-top: 12px;
}
.facts-pagination span { color: var(--ink-soft); text-align: center; font-size: .86rem; }

.visually-hidden {
  position: absolute !important;
  width: 1px !important;
  height: 1px !important;
  padding: 0 !important;
  margin: -1px !important;
  overflow: hidden !important;
  clip: rect(0, 0, 0, 0) !important;
  white-space: nowrap !important;
  border: 0 !important;
}

[hidden] { display: none !important; }

@media (max-width: 1000px) {
  .facts-filters { grid-template-columns: repeat(3, minmax(0, 1fr)); }
  .facts-filter-actions { grid-column: 1 / -1; }
}

@media (max-width: 700px) {
  .page-shell { width: min(100% - 20px, 660px); padding-top: 8px; }
  .panel { padding: 16px; }
  .picker-content { grid-template-columns: 1fr; }
  .stops-map { height: min(330px, 48vh); }
  .connection-strip { align-items: flex-start; flex-direction: column; gap: 2px; }
  .brand small { display: none; }
  .app-header { min-height: 62px; }
  .table-wrap { overflow: visible; border: 0; background: transparent; box-shadow: none; }
  table, tbody { display: block; }
  thead { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0, 0, 0, 0); }
  tbody { display: grid; gap: 10px; }
  tbody tr {
    display: grid;
    grid-template-columns: 68px minmax(0, 1fr) auto;
    grid-template-areas:
      "route vehicle vehicle"
      "forecast countdown countdown"
      "source source source"
      "action action action";
    gap: 7px 10px;
    padding: 13px;
    border: 1px solid var(--line);
    border-radius: var(--radius);
    background: var(--surface);
    box-shadow: var(--shadow);
  }
  td { display: block; min-width: 0; padding: 0; border: 0; }
  td:nth-child(1) { grid-area: route; }
  td:nth-child(2) { grid-area: vehicle; }
  td:nth-child(3) { grid-area: forecast; }
  td:nth-child(4) { grid-area: countdown; }
  td:nth-child(5) { grid-area: source; }
  td:nth-child(6) { display: block; grid-area: action; }
  .arrival-actions { grid-template-columns: 1fr 1fr; min-width: 0; }
  .arrival-button, .not-arrived-button { min-width: 0; min-height: 44px; padding: 8px; }
  .route-number { font-size: 1.55rem; }
  .registration-number { font-size: 1.08rem; }
  .countdown { min-width: 0; font-size: .86rem; }
  .time-value { font-size: .86rem; }
  .source-badge { min-height: 24px; }
  .table-state { border: 1px solid var(--line); border-radius: var(--radius); background: var(--surface); }
  .dialog-content { padding: 18px; }
  .facts-page { gap: 10px; }
  .facts-filters { grid-template-columns: 1fr 1fr; gap: 10px; padding: 13px; }
  .facts-filters .filter-field:nth-child(n + 3), .facts-filter-actions { grid-column: 1 / -1; }
  .facts-filter-actions { display: grid; grid-template-columns: minmax(0, 1fr) auto; }
  .facts-filter-actions .primary-button { padding-right: 12px; padding-left: 12px; }
  .facts-filter-actions .text-button { padding-right: 4px; padding-left: 4px; font-size: .86rem; }
  .facts-table tbody tr {
    grid-template-columns: minmax(0, 1fr) minmax(0, 1fr);
    grid-template-areas:
      "fact fact"
      "stop stop"
      "route vehicle"
      "forecast deviation"
      "comment comment"
      "status status";
    gap: 10px 14px;
  }
  .facts-table td { display: grid; grid-template-rows: auto 1fr; gap: 2px; align-content: start; }
  .facts-table td::before {
    color: var(--ink-soft);
    content: attr(data-label);
    font-size: .65rem;
    font-weight: 800;
    letter-spacing: .055em;
    text-transform: uppercase;
  }
  .facts-table td:nth-child(1) { grid-area: fact; }
  .facts-table td:nth-child(2) { grid-area: stop; }
  .facts-table td:nth-child(3) { grid-area: route; }
  .facts-table td:nth-child(4) { grid-area: vehicle; }
  .facts-table td:nth-child(5) { grid-area: forecast; }
  .facts-table td:nth-child(6) { grid-area: deviation; }
  .facts-table td:nth-child(7) { grid-area: comment; }
  .facts-table td:nth-child(8) { grid-area: status; }
  .facts-table .fact-time, .facts-table .fact-vehicle, .facts-table .fact-deviation { min-width: 0; }
  .facts-table .fact-comment { max-width: none; }
  .facts-pagination { grid-template-columns: 1fr 1fr; gap: 8px; }
  .facts-pagination span { grid-column: 1 / -1; grid-row: 1; }
}

@media (min-width: 900px) {
  .admin-layout { grid-template-columns: minmax(320px, .8fr) minmax(420px, 1.2fr); }
}

@media (prefers-reduced-motion: no-preference) {
  .toast { animation: toast-in .18s ease-out; }
  @keyframes toast-in { from { transform: translateY(10px); opacity: 0; } }
}
