/* SimovaTrack MQTT History — dashboard styles.
   Design tokens defined once on :root (light), overridden under prefers-color-scheme:
   dark. This is a LAN-only admin tool with no explicit theme toggle, so it just follows
   the OS setting.

   2026-08-18: hex values swapped to match simova-track-idf6/frontend/shared.css's
   palette exactly (--c-bg/--c-fg/--c-card-bg/--c-success-fg/etc.) — same device-UI
   family as the Snapshot panel's card system ported from that file. Variable *names*
   stay this app's own (--bg/--text/--surface/…, used throughout app.js's coloredText()/
   getSettings() already) — only the colors they resolve to changed. --accent-dim has no
   equivalent token over there (that repo doesn't have a tinted-accent background), kept
   as this app's own derived tint. */

:root {
  --bg: #f4f6f8;
  --surface: #ffffff;
  --surface-2: #eef1f4;
  --border: #dde2e7;
  --text: #1a222b;
  --text-dim: #5c6773;
  --text-faint: #75818c;
  --accent: #2563eb;
  --accent-dim: #dbe6fd;

  --ok: #059669;
  --ok-bg: #dcfce7;
  --warn: #b45309;
  --warn-bg: #fdecd2;
  --err: #dc2626;
  --err-bg: #fde2e1;
  --off: #75818c;
  --off-bg: #eef1f4;

  --mono: "SF Mono", "JetBrains Mono", ui-monospace, Menlo, Consolas, monospace;
  --sans: -apple-system, BlinkMacSystemFont, "Segoe UI", Inter, Roboto, sans-serif;

  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #101418;
    --surface: #171c22;
    --surface-2: #232c35;
    --border: #232a31;
    --text: #e8ecef;
    --text-dim: #8a97a3;
    --text-faint: #6c7883;
    --accent: #5b8def;
    --accent-dim: #1c2b4d;

    --ok: #35c98c;
    --ok-bg: #123a22;
    --warn: #e0a030;
    --warn-bg: #3a2e1a;
    --err: #f0685f;
    --err-bg: #3a1f1f;
    --off: #6c7883;
    --off-bg: #232c35;

    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: var(--sans);
  font-size: 14px;
  line-height: 1.45;
}
.hidden { display: none !important; }
a { color: inherit; text-decoration: none; }
code { font-family: var(--mono); }

/* ---------------------------------------------------------------- token gate */
.gate {
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px;
}
.gate-card {
  width: 100%;
  max-width: 380px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 28px;
  display: flex;
  flex-direction: column;
  gap: 12px;
}
.gate-card h1 { font-size: 17px; margin: 0; }
.gate-card p { margin: 0; color: var(--text-dim); font-size: 12.5px; }
.gate-card input {
  padding: 10px 12px;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: var(--surface-2);
  color: var(--text);
  font-family: var(--mono);
  font-size: 13px;
}
.gate-card button {
  padding: 10px 12px;
  border-radius: 8px;
  border: none;
  background: var(--accent);
  color: #fff;
  font-weight: 600;
  cursor: pointer;
}
.gate-error { color: var(--err); font-size: 12.5px; }

/* ---------------------------------------------------------------- shell */
.topbar {
  display: flex;
  align-items: center;
  gap: 20px;
  padding: 12px 20px;
  border-bottom: 1px solid var(--border);
  background: var(--surface);
  position: sticky;
  top: 0;
  z-index: 5;
}
.brand { font-weight: 700; font-size: 15px; white-space: nowrap; }
.brand span { font-weight: 400; color: var(--text-dim); }
.stats-strip {
  flex: 1;
  display: flex;
  gap: 16px;
  flex-wrap: wrap;
  color: var(--text-dim);
  font-size: 12px;
  font-family: var(--mono);
}
.stats-strip b { color: var(--text); font-weight: 600; }
.ghost-btn {
  background: none;
  border: 1px solid var(--border);
  color: var(--text-dim);
  padding: 6px 10px;
  border-radius: 6px;
  cursor: pointer;
  font-size: 12px;
  text-decoration: none;
  display: inline-flex;
  align-items: center;
  white-space: nowrap;
}
.ghost-btn:hover { color: var(--text); border-color: var(--text-faint); }

.topbar-right { display: flex; align-items: center; gap: 10px; }

.poll-control {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 12px;
  color: var(--text-dim);
  white-space: nowrap;
}
.poll-control select {
  background: var(--bg);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 4px 6px;
  font-size: 12px;
  font-family: inherit;
}
.poll-last {
  font-family: var(--mono);
  color: var(--text-faint);
  font-size: 11px;
  min-width: 3.5em;
}

/* Fluid up to a generous cap — 1200px left a lot of dead space on anything wider than a
   laptop, especially with the fleet/log tables now able to make use of it (more room
   for the Device/Health columns via their minWidth, see fleetColumns/logColumns). */
main#view { padding: 20px clamp(12px, 3vw, 32px); max-width: 1600px; margin: 0 auto; width: 100%; box-sizing: border-box; }

/* ---------------------------------------------------------------- filter bar */
.filter-bar {
  display: flex;
  gap: 10px;
  align-items: center;
  flex-wrap: wrap;
  margin-bottom: 16px;
}
.filter-bar input[type="search"] {
  flex: 1;
  min-width: 200px;
  padding: 8px 12px;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  font-size: 13px;
}
.seg {
  display: flex;
  border: 1px solid var(--border);
  border-radius: 8px;
  overflow: hidden;
}
.seg button {
  background: var(--surface);
  border: none;
  color: var(--text-dim);
  padding: 8px 12px;
  font-size: 12.5px;
  cursor: pointer;
  border-right: 1px solid var(--border);
}
.seg button:last-child { border-right: none; }
.seg button.active { background: var(--accent); color: #fff; }

/* ---------------------------------------------------------------- tabulator (fleet table, message log) */
/* Base layout/behavior CSS comes from tabulator.min.css (index.html, loaded before this
   file — see its comment); this re-skins it onto our --bg/--text/etc tokens so both
   tables follow the app's light/dark theme instead of Tabulator's bundled light theme.
   Selectors here deliberately mirror the nesting depth of the tabulator.css rule they're
   overriding (e.g. ".tabulator .tabulator-header .tabulator-col") — Tabulator's own CSS
   sets several of these backgrounds at 3-class specificity, so a shallower override here
   loses even with correct load order. Both renderFleet()'s fleet table and loadLog()'s
   message-log table share these rules. */
.tabulator {
  background: transparent;
  border: 1px solid var(--border);
  border-radius: 8px;
  font-family: inherit;
  font-size: 13px;
  color: var(--text);
}
.tabulator .tabulator-header {
  background-color: var(--surface);
  color: var(--text-faint);
  border-bottom: 1px solid var(--border);
}
.tabulator .tabulator-header .tabulator-col {
  background: var(--surface);
  border-right: 1px solid var(--border);
}
.tabulator .tabulator-header .tabulator-col .tabulator-col-content .tabulator-col-title {
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: .04em;
  color: var(--text-faint);
  font-weight: 600;
}
@media (hover: hover) and (pointer: fine) {
  .tabulator .tabulator-header .tabulator-col.tabulator-sortable.tabulator-col-sorter-element:hover {
    background-color: var(--surface-2);
  }
}
.tabulator .tabulator-header .tabulator-col.tabulator-sortable[aria-sort="none"] .tabulator-col-content .tabulator-col-sorter .tabulator-arrow {
  border-bottom-color: var(--text-faint);
}
.tabulator .tabulator-header .tabulator-col.tabulator-sortable[aria-sort="ascending"] .tabulator-col-content .tabulator-col-sorter .tabulator-arrow {
  border-bottom-color: var(--accent);
}
.tabulator .tabulator-header .tabulator-col.tabulator-sortable[aria-sort="descending"] .tabulator-col-content .tabulator-col-sorter .tabulator-arrow {
  border-top-color: var(--accent);
}
.tabulator .tabulator-header .tabulator-col .tabulator-header-filter input {
  background: var(--bg);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 5px;
  padding: 3px 6px;
  font-size: 12px;
  width: 100%;
  box-sizing: border-box;
}
.tabulator .tabulator-header .tabulator-col .tabulator-header-filter input::placeholder { color: var(--text-faint); }
.tabulator .tabulator-header .tabulator-col .tabulator-header-filter input:focus { outline: none; border-color: var(--accent); }
.tabulator .tabulator-col-resize-handle { background: transparent; }

.tabulator-row {
  background-color: var(--surface);
  border-bottom: 1px solid var(--border);
  color: var(--text);
  cursor: pointer;
}
.tabulator-row.tabulator-row-even { background-color: var(--surface); }
@media (hover: hover) and (pointer: fine) {
  .tabulator-row.tabulator-selectable:hover { background-color: var(--surface-2); }
}
.tabulator-row .tabulator-cell { border-right: none; padding: 10px; vertical-align: middle; }
.tabulator-row .tabulator-cell.tabulator-row-header { background: var(--surface); border-right: 1px solid var(--border); }
/* Collapsed-column disclosure (narrow viewports — see fleetColumns'/logColumns' `responsive`
   priorities and the responsiveLayout:"collapse" table option). */
.tabulator-row .tabulator-responsive-collapse {
  background: var(--surface-2);
  border-top: 1px solid var(--border);
  border-bottom: 1px solid var(--border);
  color: var(--text);
}
.tabulator-row .tabulator-responsive-collapse table tr td { color: var(--text-dim); padding: 3px 6px; }
.tabulator-row .tabulator-responsive-collapse table tr td:first-of-type { color: var(--text-faint); font-size: 11px; text-transform: uppercase; letter-spacing: .03em; }
.tabulator-row .tabulator-cell .tabulator-responsive-collapse-toggle { background: var(--accent); color: #fff; }

.tabulator .tabulator-tableholder .tabulator-placeholder .tabulator-placeholder-contents {
  color: var(--text-dim);
  font-weight: 500;
  font-size: 13px;
}
.tabulator .tabulator-footer {
  background-color: var(--surface);
  border-top: 1px solid var(--border);
  color: var(--text-faint);
}
.device-name { font-weight: 600; }
.device-mac { font-family: var(--mono); color: var(--text-dim); font-size: 12px; }
.empty-state {
  padding: 40px 20px;
  text-align: center;
  color: var(--text-dim);
}

.dot { width: 8px; height: 8px; border-radius: 50%; display: inline-block; margin-right: 6px; }
.dot-on { background: var(--ok); box-shadow: 0 0 0 3px var(--ok-bg); }
.dot-off { background: var(--off); }

.chip {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  padding: 2px 7px;
  border-radius: 5px;
  font-size: 10.5px;
  font-weight: 600;
  font-family: var(--mono);
  white-space: nowrap;
  /* Default = red. Anything that isn't explicitly "healthy" or "unavailable" reads as
     a problem, including a state string this UI doesn't recognize yet — silently
     falling back to unstyled text would hide a real fault. */
  color: var(--err);
  background: var(--err-bg);
}
/* Health-status -> palette-color mapping is configurable (Settings page); these are
   the 4 fixed palette slots it maps into, not one class per status literal anymore. */
.chip-ok { color: var(--ok); background: var(--ok-bg); }
.chip-warn { color: var(--warn); background: var(--warn-bg); }
.chip-err { color: var(--err); background: var(--err-bg); }
.chip-off { color: var(--off); background: var(--off-bg); }
.chip-row { display: flex; gap: 4px; flex-wrap: wrap; }

.icon-row { display: flex; gap: 8px; font-size: 12px; color: var(--text-faint); }
.icon-on { color: var(--accent); font-weight: 600; }

/* ---------------------------------------------------------------- detail page */
.back-link { color: var(--text-dim); font-size: 12.5px; margin-bottom: 10px; display: inline-block; }
.detail-header {
  display: flex;
  align-items: baseline;
  gap: 12px;
  flex-wrap: wrap;
  margin-bottom: 18px;
}
.detail-header h1 { font-size: 20px; margin: 0; }
.detail-header .device-mac { font-size: 13px; }

/* 2026-08-18: panels are now collapsible top-level cards, same mechanism as the
   Snapshot panel's inner .snap-card groups (and ultimately shared.css's own
   .card/.card-header/.card-body) — click the header, chevron rotates, state
   persists (see PANEL_COLLAPSE_KEY in app.js). overflow-x stays on .panel itself
   (not .panel-body) so a collapsed panel's border-radius doesn't get clipped by
   a scrolling child. */
.panel {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 16px;
  margin-bottom: 18px;
  overflow-x: auto;
}
/* Settings page's panels (app.js ~L266-305) still put a bare <h2> straight
   inside .panel, no header wrapper, no collapse — this rule is what styles
   those. Device-detail panels below use .panel-header h2 instead (its own
   rule, margin:0 — the header row itself carries the spacing). */
.panel > h2 {
  font-size: 12px;
  text-transform: uppercase;
  letter-spacing: .04em;
  color: var(--accent);
  margin: 0 0 12px 0;
}
.panel-header {
  display: flex; align-items: center; justify-content: space-between; gap: 10px;
  cursor: pointer; user-select: none; margin: 0 0 12px 0;
}
.panel.collapsed .panel-header { margin-bottom: 0; }
.panel-header h2 { font-size: 12px; text-transform: uppercase; letter-spacing: .04em; color: var(--accent); margin: 0; }
.panel-header .chevron { color: var(--text-faint); font-size: 11px; transition: transform .15s ease; flex-shrink: 0; }
.panel.collapsed .chevron { transform: rotate(-90deg); }
.panel.collapsed .panel-body { display: none; }

/* Snapshot panel — collapsible cards of stat tiles, one card per data group
   (Health/Device/Telemetry/GPS/…). Structure and class-shape ported from
   simova-track-idf6/frontend/shared.css's .card/.card-header/.status/
   .status-item (same collapsible-card + tile-grid system that repo's own
   device dashboard uses for its "Device Status"/"Live API Data" cards) —
   colors stay on *this* app's own --surface/--border/etc. tokens rather than
   forking a second palette. "snap-" prefix keeps these distinct from this
   app's existing .panel (the bigger Snapshot/Command/Chart sections these
   cards nest inside). 2026-08-18: replaces the old flat .kv-groups grid,
   which gave every group an equal-width column regardless of row count — a
   3-row group (Health) and a 12-row one (Device) ended up visually
   mismatched. Collapsing groups you don't need open fixes that directly.

   Chosen from a set of prototyped alternatives (masonry columns, dense flat
   table) — Italo's call: this dashboard already borrows can_simulator.html's
   AT-console conventions, and pulling in its collapsible-card system too
   keeps the two device UIs feeling like one family. */
.snap-cards { display: flex; flex-direction: column; gap: 10px; }
.snap-card { background: var(--surface-2); border: 1px solid var(--border); border-radius: 8px; overflow: hidden; }
.snap-card-header {
  display: flex; align-items: center; justify-content: space-between; gap: 10px;
  padding: 9px 12px; cursor: pointer; user-select: none;
}
.snap-card-header:hover { background: var(--border); }
.snap-card-header h3 {
  font-size: 11px; text-transform: uppercase; letter-spacing: .04em;
  color: var(--accent); margin: 0; font-weight: 700;
}
.snap-card-header .chevron { color: var(--text-faint); font-size: 10px; transition: transform .15s ease; flex-shrink: 0; }
.snap-card.collapsed .chevron { transform: rotate(-90deg); }
.snap-card-body { padding: 2px 12px 12px; }
.snap-card.collapsed .snap-card-body { display: none; }

.snap-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(130px, 1fr)); gap: 8px; }
.snap-tile { padding: 7px 10px; background: var(--surface); border: 1px solid var(--border); border-radius: 6px; overflow-wrap: anywhere; }
.snap-tile .k { font-size: 9.5px; text-transform: uppercase; letter-spacing: .03em; color: var(--text-faint); margin-bottom: 3px; }
.snap-tile .v { font-family: var(--mono); font-size: 12.5px; font-weight: 600; color: var(--text); word-break: break-word; }

.maps-btn {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  padding: 3px 9px;
  border-radius: 6px;
  background: var(--accent-dim);
  color: var(--accent);
  font-family: var(--sans);
  font-size: 11.5px;
  font-weight: 600;
  white-space: nowrap;
}
.maps-btn:hover { background: var(--accent); color: #fff; }

.chart-picker { display: flex; gap: 8px; flex-wrap: wrap; margin-bottom: 12px; }
.chart-picker button {
  background: var(--surface-2);
  border: 1px solid var(--border);
  color: var(--text-dim);
  padding: 5px 10px;
  border-radius: 6px;
  font-size: 11.5px;
  cursor: pointer;
  font-family: var(--mono);
}
.chart-picker button.active { background: var(--accent-dim); color: var(--accent); border-color: var(--accent); }
.chart-wrap { min-width: 480px; }
.chart-empty { color: var(--text-faint); font-size: 12.5px; padding: 30px 0; text-align: center; }

.log-payload {
  margin: 0;
  font-family: var(--mono);
  font-size: 11.5px;
  white-space: pre-wrap;
  word-break: break-word;
  background: var(--surface-2);
  border-radius: 6px;
  padding: 8px;
}
.kind-tag { font-family: var(--mono); font-size: 10.5px; padding: 1px 6px; border-radius: 4px; background: var(--surface-2); color: var(--text-dim); }

.cmd-hint { color: var(--text-dim); font-size: 12.5px; margin: 0 0 12px 0; }
.cmd-bar { display: flex; gap: 8px; margin-bottom: 12px; }
.cmd-bar input {
  flex: 1;
  background: var(--bg);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 8px 10px;
  font-family: var(--mono);
  font-size: 12.5px;
}
.cmd-bar input:focus { outline: none; border-color: var(--accent); }
.cmd-bar button {
  background: var(--accent);
  color: #fff;
  border: none;
  border-radius: 6px;
  padding: 8px 16px;
  font-weight: 600;
  font-size: 12.5px;
  cursor: pointer;
}
.cmd-bar button:disabled { opacity: .5; cursor: default; }

.cmd-log { display: flex; flex-direction: column; gap: 10px; max-height: 360px; overflow-y: auto; }
.cmd-entry { border: 1px solid var(--border); border-radius: 8px; padding: 8px 10px; background: var(--surface-2); }
.cmd-entry-req { font-family: var(--mono); font-size: 12.5px; margin-bottom: 4px; }
.cmd-entry-ts { color: var(--text-faint); font-size: 11px; }
.cmd-entry-lines { font-family: var(--mono); font-size: 12px; }
.cmd-line { white-space: pre-wrap; word-break: break-word; padding: 1px 0; }
.cmd-pending, .cmd-empty { color: var(--text-faint); }
.cmd-error { color: var(--err); }
.cmd-timeout { color: var(--warn); font-size: 11.5px; margin-top: 2px; }

/* ---------------------------------------------------------------- settings page */
.settings-table { width: 100%; border-collapse: collapse; font-size: 12.5px; margin-bottom: 10px; }
.settings-table td, .settings-table th { padding: 6px 8px; border-bottom: 1px solid var(--border); vertical-align: middle; }
.settings-table th { text-align: left; color: var(--text-faint); font-size: 10.5px; text-transform: uppercase; }
.settings-table select, .settings-table input {
  background: var(--bg);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 4px 6px;
  font-size: 12px;
  font-family: inherit;
}
.settings-table input.th-field { width: 100%; min-width: 220px; font-family: var(--mono); }
.settings-table input.th-value { width: 90px; }
.th-remove { color: var(--text-faint); border-color: var(--border); }
.th-remove:hover { color: var(--err); border-color: var(--err); }
#settings-save-btn {
  background: var(--accent);
  color: #fff;
  border: none;
  padding: 8px 16px;
  border-radius: 6px;
  cursor: pointer;
  font-size: 13px;
  margin-right: 8px;
}
#settings-saved-msg { color: var(--ok); margin-left: 4px; }

.load-more { text-align: center; margin-top: 10px; }
.load-more button {
  background: var(--surface-2);
  border: 1px solid var(--border);
  color: var(--text-dim);
  padding: 6px 14px;
  border-radius: 6px;
  cursor: pointer;
  font-size: 12px;
}

/* ---------------------------------------------------------------- small screens */
/* Everything above already flexes/wraps on its own (topbar, filter-bar, snap-cards'
   flex column + snap-grid's auto-fit tiles, Tabulator's own responsiveLayout:"collapse"
   — see fleetColumns'/logColumns' `responsive` priorities). This tightens up the parts
   that don't. */
@media (max-width: 720px) {
  .topbar { flex-wrap: wrap; row-gap: 8px; padding: 10px 12px; gap: 10px 14px; }
  /* Stats strip drops to its own full-width line below brand+actions instead of
     wrapping mid-flex (which used to squeeze/stack it beside them). `order` puts it
     last regardless of its place in the DOM (brand, stats-strip, topbar-right). */
  .stats-strip { order: 1; flex-basis: 100%; font-size: 11px; gap: 8px 14px; }
  main#view { padding: 14px 12px; }
  .filter-bar { flex-direction: column; align-items: stretch; }
  .filter-bar input[type="search"] { min-width: 0; }
  .seg { align-self: stretch; }
  .seg button { flex: 1; }
  .chart-wrap { min-width: 0; }
  .panel { padding: 14px; }
  .detail-header { gap: 6px 12px; }
}
