:root {
  color-scheme: light;
  --bg: #f4f6f8;
  --panel: #ffffff;
  --panel-2: #f5f7f9;
  --border: #dbe2e8;
  --border-strong: #b9c3cc;
  --text: #18212b;
  --muted: #627180;
  --primary: #165a72;
  --primary-text: #ffffff;
  --ok-bg: #e8f6ec; --ok-border: #a9d8b5; --ok-text: #1c6b33;
  --warn-bg: #fff9df; --warn-border: #ead48a; --warn-text: #806100;
  --bad-bg: #fff5f5; --bad-border: #e5a7a7; --bad-text: #9d1f1f;
  --info-bg: #eaf3f7; --info-border: #b5d3df; --info-text: #165a72;
  --alarm: #c62828;
  --overdue: #d98b00;
  --series: #2a78d6;
  /* compare-graph lines, in fixed order (validated for color-blind separation) */
  --s1: #2a78d6; --s2: #eb6834; --s3: #1baf7a; --s4: #eda100; --s5: #e87ba4; --s6: #008300; --s7: #4a3aa7;
  --critical: #d03b3b;
  --band: rgba(22, 90, 114, 0.09);
  --band-line: #7fa7b6;
  --grid: #e6ebef;
  --shadow: 0 12px 32px rgba(24, 33, 43, 0.12);
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, Arial, sans-serif;
}

:root[data-theme="dark"] {
  color-scheme: dark;
  --bg: #11161b;
  --panel: #1a2128;
  --panel-2: #222b33;
  --border: #2e3943;
  --border-strong: #46535f;
  --text: #e8edf2;
  --muted: #9aa8b5;
  --primary: #2a87a8;
  --primary-text: #ffffff;
  --ok-bg: #16301f; --ok-border: #2d6a3f; --ok-text: #8fe0a6;
  --warn-bg: #302914; --warn-border: #8a6f1f; --warn-text: #ffe084;
  --bad-bg: #351d1d; --bad-border: #7d2b2b; --bad-text: #ffb1b1;
  --info-bg: #15303b; --info-border: #2c5f72; --info-text: #9fd3e6;
  --alarm: #e66767;
  --overdue: #f0a830;
  --series: #3987e5;
  --s1: #3987e5; --s2: #d95926; --s3: #199e70; --s4: #c98500; --s5: #d55181; --s6: #008300; --s7: #9085e9;
  --critical: #e66767;
  --band: rgba(90, 170, 200, 0.12);
  --band-line: #4f8597;
  --grid: #27313a;
  --shadow: 0 12px 32px rgba(0, 0, 0, 0.4);
}

* { box-sizing: border-box; }
html, body { margin: 0; background: var(--bg); color: var(--text); }
body { font-size: 15px; line-height: 1.4; }
h1, h2, h3 { margin: 0; line-height: 1.2; }
h1 { font-size: 26px; }
h2 { font-size: 22px; }
h3 { font-size: 16px; }
a { color: var(--primary); }
button, input, select, textarea { font: inherit; color: inherit; }
.muted { color: var(--muted); }
.small { font-size: 13px; }
.hidden { display: none !important; }

/* ---------- buttons, pills, inputs (DMS look) ---------- */
.btn {
  border: 1px solid var(--border-strong);
  background: var(--panel);
  border-radius: 6px;
  padding: 8px 12px;
  font-weight: 700;
  cursor: pointer;
  text-decoration: none;
  color: var(--text);
  display: inline-flex; align-items: center; gap: 6px; justify-content: center;
}
.btn:hover { filter: brightness(0.96); }
.btn.primary, .btn.active { background: var(--primary); border-color: var(--primary); color: var(--primary-text); }
.btn.danger { background: var(--bad-bg); border-color: var(--bad-border); color: var(--bad-text); }
.btn.small { padding: 5px 9px; font-size: 13px; }
.btn:disabled { opacity: .55; cursor: not-allowed; }

.pill {
  display: inline-flex; align-items: center; gap: 5px;
  border-radius: 999px; padding: 4px 10px; font-size: 12px; font-weight: 800;
  white-space: nowrap; border: 1px solid transparent;
}
.pill.ok { background: var(--ok-bg); color: var(--ok-text); border-color: var(--ok-border); }
.pill.warn { background: var(--warn-bg); color: var(--warn-text); border-color: var(--warn-border); }
.pill.bad { background: var(--bad-bg); color: var(--bad-text); border-color: var(--bad-border); }
.pill.info { background: var(--info-bg); color: var(--info-text); border-color: var(--info-border); }
.pill.muted { background: var(--panel-2); color: var(--muted); border-color: var(--border); }

.field { display: grid; gap: 5px; font-size: 13px; font-weight: 700; color: var(--muted); }
input[type=text], input[type=number], input[type=password], input[type=datetime-local], input[type=time],
input[type=date], select, textarea {
  width: 100%;
  border: 1px solid var(--border-strong);
  background: var(--panel);
  border-radius: 6px;
  padding: 8px 10px;
  color: var(--text);
}
textarea { min-height: 70px; resize: vertical; }

.panel {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 14px;
}

/* ---------- app shell ---------- */
.app { max-width: 1400px; margin: 0 auto; padding: 18px; }
.topbar { display: flex; justify-content: space-between; gap: 18px; align-items: flex-start; margin-bottom: 18px; flex-wrap: wrap; }
.eyebrow { font-size: 12px; text-transform: uppercase; letter-spacing: .06em; color: var(--muted); margin-bottom: 3px; }
.top-actions { display: flex; gap: 10px; align-items: center; flex-wrap: wrap; justify-content: flex-end; }
.nav { display: flex; gap: 8px; flex-wrap: wrap; margin-bottom: 20px; }
.menu { position: relative; }
.menu-list {
  position: absolute; top: calc(100% + 6px); left: 0; z-index: 20;
  min-width: 210px; display: grid; gap: 2px; padding: 6px;
  background: var(--panel); border: 1px solid var(--border); border-radius: 8px; box-shadow: var(--shadow);
}
.menu-list button { text-align: left; border: 0; background: none; padding: 8px 10px; border-radius: 6px; cursor: pointer; font-weight: 600; }
.menu-list button:hover { background: var(--panel-2); }

.theme-toggle { display: inline-flex; align-items: center; gap: 8px; font-size: 13px; font-weight: 700; color: var(--muted); }
.switch { width: 40px; height: 22px; border-radius: 999px; border: 1px solid var(--border-strong); background: var(--panel-2); position: relative; cursor: pointer; padding: 0; }
.switch::after { content: ""; position: absolute; top: 2px; left: 2px; width: 16px; height: 16px; border-radius: 50%; background: var(--muted); transition: left .15s; }
:root[data-theme="dark"] .switch::after { left: 20px; background: var(--primary); }

.summary-grid { display: grid; gap: 10px; grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); margin-bottom: 20px; }
.summary-item { background: var(--panel); border: 1px solid var(--border); border-radius: 8px; padding: 11px 13px; }
.summary-value { display: block; font-size: 26px; font-weight: 800; }
.summary-label { color: var(--muted); font-size: 13px; }
.summary-item.bad .summary-value { color: var(--alarm); }
.summary-item.warn .summary-value { color: var(--overdue); }

.section-heading { display: flex; justify-content: space-between; gap: 14px; align-items: end; margin-bottom: 12px; flex-wrap: wrap; }
.filters { display: flex; gap: 10px; align-items: end; flex-wrap: wrap; }
.filters label.field { min-width: 180px; }

/* ---------- unit tiles ---------- */
.tile-grid { display: grid; gap: 14px; grid-template-columns: repeat(auto-fill, minmax(270px, 1fr)); }
.tile {
  background: var(--panel); border: 2px solid var(--border); border-radius: 10px; padding: 14px;
  display: grid; gap: 10px; align-content: start;
}
.tile.excursion { border-color: var(--alarm); }
.tile.overdue { border-color: var(--overdue); }
.tile-head { display: flex; justify-content: space-between; gap: 8px; align-items: flex-start; }
.tile-kind { font-size: 13px; color: var(--muted); }
.tile-name { font-size: 19px; font-weight: 800; margin-top: 2px; }
.tile-value { font-size: 40px; font-weight: 800; letter-spacing: -.01em; line-height: 1; }
.tile-value small { font-size: 22px; font-weight: 700; }
.tile.excursion .tile-value { color: var(--alarm); }
.tile-value.empty { font-size: 22px; color: var(--muted); }
.meta-grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 8px; margin: 0; }
.meta-grid div { background: var(--panel-2); border-radius: 6px; padding: 7px 9px; min-width: 0; }
.meta-grid dt { font-size: 12px; color: var(--muted); }
.meta-grid dd { margin: 2px 0 0; font-weight: 800; font-size: 14px; overflow-wrap: anywhere; }
.tile-note { border-radius: 6px; padding: 8px 10px; font-size: 13px; }
.tile-note.bad { background: var(--bad-bg); border: 1px solid var(--bad-border); color: var(--bad-text); }
.tile-note.warn { background: var(--warn-bg); border: 1px solid var(--warn-border); color: var(--warn-text); }
.tile-note.ok { background: var(--ok-bg); border: 1px solid var(--ok-border); color: var(--ok-text); }
.tile-note.bad { background: var(--bad-bg); border: 1px solid var(--bad-border); color: var(--bad-text); }
.tile-note.info { background: var(--info-bg); border: 1px solid var(--info-border); color: var(--info-text); }
.tile-range { font-size: 13px; color: var(--muted); }
.tile-actions { display: flex; gap: 8px; flex-wrap: wrap; }

/* ---------- tables ---------- */
.table-wrap { overflow-x: auto; background: var(--panel); border: 1px solid var(--border); border-radius: 8px; }
table { width: 100%; border-collapse: collapse; font-size: 14px; }
th, td { text-align: left; padding: 9px 11px; border-bottom: 1px solid var(--border); vertical-align: top; }
th { font-size: 12px; color: var(--muted); text-transform: uppercase; letter-spacing: .04em; background: var(--panel-2); white-space: nowrap; }
tr:last-child td { border-bottom: 0; }
td.num { font-variant-numeric: tabular-nums; white-space: nowrap; }
tr.row-bad td { background: var(--bad-bg); }
.thumb { width: 44px; height: 44px; object-fit: cover; border-radius: 4px; border: 1px solid var(--border); cursor: zoom-in; }
.empty-state { padding: 26px; border: 1px dashed var(--border-strong); border-radius: 8px; color: var(--muted); text-align: center; }

/* ---------- chart ---------- */
.chart-card { background: var(--panel); border: 1px solid var(--border); border-radius: 8px; padding: 14px 14px 6px; margin-bottom: 14px; }
.chart-title { display: flex; justify-content: space-between; gap: 10px; flex-wrap: wrap; align-items: baseline; margin-bottom: 6px; }
.chart { position: relative; }
.chart svg { display: block; width: 100%; overflow: visible; }
.chart .axis text { fill: var(--muted); font-size: 11px; }
.chart .gridline { stroke: var(--grid); stroke-width: 1; }
.chart .band { fill: var(--band); }
.chart .band-edge { stroke: var(--band-line); stroke-width: 1; stroke-dasharray: 4 4; }
.chart .band-label { fill: var(--muted); font-size: 11px; }
.chart .series { fill: none; stroke: var(--series); stroke-width: 2; stroke-linejoin: round; }
.chart .dot { fill: var(--series); stroke: var(--panel); stroke-width: 2; }
.chart .dot.bad { fill: var(--critical); }
.chart .crosshair { stroke: var(--muted); stroke-width: 1; stroke-dasharray: 3 3; }
.chart .hover-dot { fill: var(--panel); stroke: var(--series); stroke-width: 2.5; }
.chart .dot.ring-bad { stroke: var(--critical); stroke-width: 2.5; }
.chart .line-label { fill: var(--text); font-size: 12px; font-weight: 600; }
.legend { display: flex; flex-wrap: wrap; gap: 6px 16px; margin: 2px 0 8px; font-size: 13px; }
.legend span, .swatch-name { display: inline-flex; align-items: center; gap: 6px; }
.swatch { display: inline-block; width: 14px; height: 4px; border-radius: 2px; flex: none; }
.ring-key { display: inline-block; width: 9px; height: 9px; border-radius: 50%; border: 2.5px solid var(--critical); flex: none; }
.chart-tip .tip-row { display: flex; align-items: center; gap: 6px; font-size: 13px; }
.chart-tip .tip-row b { margin-left: auto; padding-left: 10px; }
.compare-picker { display: flex; flex-wrap: wrap; gap: 6px; }
.compare-picker label { display: inline-flex; align-items: center; gap: 6px; border: 1px solid var(--border); border-radius: 99px;
  padding: 5px 11px; font-size: 13px; cursor: pointer; background: var(--panel); }
.compare-picker label.on { border-color: var(--primary); background: var(--info-bg); }
.compare-picker label.off { opacity: .45; cursor: not-allowed; }
.compare-picker input { margin: 0; }
.chart-tip {
  position: absolute; pointer-events: none; z-index: 5; min-width: 170px;
  background: var(--panel); border: 1px solid var(--border); border-radius: 8px; box-shadow: var(--shadow);
  padding: 8px 10px; font-size: 13px;
}
.chart-tip .tip-value { font-size: 18px; font-weight: 800; }
.chart-tip .tip-bad { color: var(--bad-text); font-weight: 700; }

/* ---------- dialog ---------- */
dialog { border: 1px solid var(--border); border-radius: 10px; background: var(--panel); color: var(--text); box-shadow: var(--shadow); padding: 18px; width: min(560px, calc(100vw - 32px)); }
dialog::backdrop { background: rgba(10, 16, 22, .45); }
.form-grid { display: grid; gap: 12px; grid-template-columns: 1fr 1fr; }
.form-grid .full { grid-column: 1 / -1; }
.check { display: flex; gap: 8px; align-items: center; font-weight: 600; }
.dialog-actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 16px; }
.form-message { min-height: 18px; color: var(--bad-text); font-weight: 700; font-size: 14px; }
.photo-view { max-width: 100%; max-height: 70vh; display: block; margin: 0 auto; border-radius: 6px; }

/* ---------- print (reports) ---------- */
.print-only { display: none; }
.print-head { display: flex; justify-content: space-between; align-items: flex-start; gap: 16px;
  border-bottom: 2px solid #165a72; padding-bottom: 10px; margin-bottom: 14px; }
.print-head img { max-height: 56px; max-width: 200px; object-fit: contain; }
.print-head h2 { font-size: 20px; margin: 2px 0 4px; }
.print-head p { margin: 2px 0; }
.signoff { margin-top: 26px; display: grid; gap: 18px; break-inside: avoid; font-size: 12px; }
.signoff div { display: grid; grid-template-columns: 1.4fr 1fr 1.4fr; gap: 16px; }
.signoff span { border-bottom: 1px solid #333; padding: 14px 2px 3px; }
.signoff span b { font-weight: 700; }
@page { margin: 14mm; }
@media print {
  :root { --bg: #fff; --panel: #fff; --panel-2: #f3f5f7; --text: #111; --muted: #555; --border: #ccc; }
  body { font-size: 12px; }
  .no-print, .nav, .topbar, .filters, .tile-actions, [data-view] > .section-heading { display: none !important; }
  .print-only { display: block; }
  .app { padding: 0; max-width: none; }
  .table-wrap { border-color: #ccc; overflow: visible; }
  .chart-card, .panel { border-color: #ccc; break-inside: avoid; }
  thead { display: table-header-group; }  /* repeat column headings on every page */
  tr { break-inside: avoid; }
}

.pick-list { border: 1px solid var(--border); border-radius: 6px; max-height: 260px; overflow-y: auto; padding: 4px 8px; background: var(--panel); }
.pick-list .check { padding: 6px 2px; font-weight: 500; color: var(--text); border-bottom: 1px solid var(--border); }
.pick-list .check:last-child { border-bottom: 0; }
.pick-list .check .small { margin-left: auto; }
.chips { display: flex; flex-wrap: wrap; gap: 6px; }
.term { cursor: pointer; text-align: left; border: 2px solid var(--border); }
.term:hover { border-color: var(--primary); }
.term .summary-value { font-size: 22px; }
.term .save { color: var(--ok-text); font-weight: 700; font-size: 12px; }
.tile.disabled { opacity: .72; }
.tile.disabled::before { background: var(--border-strong); }
.banner { display: flex; gap: 10px; align-items: center; justify-content: space-between; margin-bottom: 14px; }
.phone-only { display: none; }

/* ---------- phone layout (switches on automatically on small screens) ---------- */
@media (max-width: 640px) {
  .app { padding: 12px 12px 24px; }
  h1 { font-size: 20px; }
  h2 { font-size: 19px; }
  .form-grid { grid-template-columns: 1fr; }
  .phone-only { display: inline; }
  .desktop-only { display: none; }

  /* Header: logo + name on one line, controls on the next */
  .topbar { flex-direction: column; align-items: stretch; gap: 10px; margin-bottom: 12px; }
  .brand-row { gap: 10px; align-items: center; }
  .brand-mark { width: 36px; height: 36px; font-size: 18px; border-radius: 8px; }
  .brand-mark.has-logo { max-width: 110px; padding: 2px; }
  .brand-mark.has-logo img { height: 30px; max-width: 104px; }
  .top-actions { justify-content: space-between; gap: 8px; flex-wrap: nowrap; }
  .tt-label, #last-refresh { display: none; }
  #user-badge { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; justify-content: center; }

  /* Menu: one row that scrolls sideways; dropdowns open below it, full width */
  .nav { flex-wrap: nowrap; overflow-x: auto; margin: 0 -12px 14px; padding: 0 12px 4px; scrollbar-width: none; }
  .nav::-webkit-scrollbar { display: none; }
  .nav > .btn, .nav .menu > .btn { white-space: nowrap; flex: none; }
  .menu { position: static; }
  .menu-list { position: fixed; left: 12px; right: 12px; min-width: 0; }
  .menu-list button { padding: 12px; }

  .summary-grid { grid-template-columns: 1fr 1fr; gap: 8px; margin-bottom: 14px; }
  .summary-item { padding: 9px 11px; }
  .summary-value { font-size: 21px; }
  .summary-label { font-size: 12px; }

  .banner { flex-direction: column; align-items: stretch; text-align: left; }
  .section-heading { align-items: stretch; }
  .section-heading > .filters, .section-heading > .btn { width: 100%; }
  .filters { gap: 8px; }
  .filters label.field, .filters .field { min-width: 0; flex: 1 1 140px; }
  .panel { padding: 12px; }

  .tile-grid { grid-template-columns: 1fr; gap: 12px; }
  .tile-value { font-size: 34px; }
  .tile-actions .btn { flex: 1; }

  th, td { padding: 8px 9px; }
  .chart-card { padding: 10px 8px 4px; }
  dialog { padding: 14px; width: calc(100vw - 16px) !important; max-height: calc(100vh - 16px); }
  .dialog-actions { flex-wrap: wrap; }
  .dialog-actions .btn { flex: 1; }
}

/* ---------- refresh: brand, tiles with status stripe, sparklines ---------- */
.brand-row { display: flex; align-items: center; gap: 12px; }
.brand-mark {
  width: 42px; height: 42px; border-radius: 10px; display: grid; place-items: center; flex: none;
  background: linear-gradient(135deg, var(--primary), #2a9bbd); color: #fff; font-size: 22px; font-weight: 800;
}
.brand-mark.has-logo { background: #fff; border: 1px solid var(--border); width: auto; min-width: 42px; max-width: 160px; padding: 3px; }
.brand-mark.has-logo img { height: 34px; max-width: 150px; object-fit: contain; display: block; }
.tile { position: relative; overflow: hidden; border-width: 1px; padding-top: 18px; box-shadow: 0 1px 2px rgba(24, 33, 43, .05); }
.tile::before { content: ""; position: absolute; inset: 0 0 auto 0; height: 5px; background: var(--ok-border); }
.tile.excursion { border-color: var(--bad-border); }
.tile.excursion::before { background: var(--alarm); }
.tile.overdue { border-color: var(--warn-border); }
.tile.overdue::before { background: var(--overdue); }
.tile.due::before { background: var(--info-border); }
.tile.no-data::before { background: var(--border-strong); }
.spark { width: 100%; height: 44px; display: block; }
.spark.empty { height: auto; padding: 12px 0; }
.excursion-body { display: flex; justify-content: space-between; gap: 14px; margin: 10px 0 14px; }
.excursion-body p { margin: 0 0 6px; }
.excursion-card { border-left: 5px solid var(--alarm); }
