/* Heartbeat dashboard — design tokens + layout. Vanilla CSS, no build step.
   Theme is driven by [data-theme="dark"|"light"] on <html>; when unset we fall
   back to prefers-color-scheme. Chart series colours live here too and are read
   by hb-charts.js via getComputedStyle so both themes render correctly. */

:root,
[data-theme="dark"] {
  --bg: #0f1115;
  --page: #0d0d0d;
  --panel: #1a1d24;
  --panel-2: #21252e;
  --border: #262a33;
  --border-strong: #333844;
  --fg: #e6e8eb;
  --muted: #8a93a2;
  --muted-2: #6b7280;
  --accent: #5eead4;
  --accent-ink: #0f1115;

  /* Status (dataviz status palette, dark-surface steps). Icon + label always. */
  --ok: #0ca30c;
  --warn: #fab219;
  --serious: #ec835a;
  --bad: #d03b3b;

  /* Chart chrome */
  --grid: #262a33;
  --axis: #383835;
  --tick: #8a93a2;

  /* Categorical series 1-8 (dataviz reference, dark steps) + Other */
  --series-1: #3987e5;
  --series-2: #199e70;
  --series-3: #c98500;
  --series-4: #008300;
  --series-5: #9085e9;
  --series-6: #e66767;
  --series-7: #d55181;
  --series-8: #d95926;
  --series-other: #8a93a2;
}

[data-theme="light"] {
  --bg: #f9f9f7;
  --page: #f0efec;
  --panel: #ffffff;
  --panel-2: #f5f5f3;
  --border: #e5e7eb;
  --border-strong: #d3d6db;
  --fg: #1f2328;
  --muted: #6b7280;
  --muted-2: #898781;
  --accent: #0d9488;
  --accent-ink: #ffffff;

  --ok: #0ca30c;
  --warn: #fab219;
  --serious: #ec835a;
  --bad: #d03b3b;

  --grid: #e1e0d9;
  --axis: #c3c2b7;
  --tick: #898781;

  /* Categorical series (dataviz reference, light steps) */
  --series-1: #2a78d6;
  --series-2: #1baf7a;
  --series-3: #eda100;
  --series-4: #008300;
  --series-5: #4a3aa7;
  --series-6: #e34948;
  --series-7: #e87ba4;
  --series-8: #eb6834;
  --series-other: #898781;
}

/* Fall back to system preference only when no explicit choice is stored. */
@media (prefers-color-scheme: light) {
  :root:not([data-theme]) {
    --bg: #f9f9f7;
    --page: #f0efec;
    --panel: #ffffff;
    --panel-2: #f5f5f3;
    --border: #e5e7eb;
    --border-strong: #d3d6db;
    --fg: #1f2328;
    --muted: #6b7280;
    --muted-2: #898781;
    --accent: #0d9488;
    --accent-ink: #ffffff;
    --grid: #e1e0d9;
    --axis: #c3c2b7;
    --tick: #898781;
    --series-1: #2a78d6;
    --series-2: #1baf7a;
    --series-3: #eda100;
    --series-4: #008300;
    --series-5: #4a3aa7;
    --series-6: #e34948;
    --series-7: #e87ba4;
    --series-8: #eb6834;
    --series-other: #898781;
  }
}

* { box-sizing: border-box; }

[hidden] { display: none !important; }

html, body {
  margin: 0;
  padding: 0;
  background: var(--bg);
  color: var(--fg);
  font-family: system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  font-size: 14px;
  line-height: 1.45;
}

@media (prefers-reduced-motion: reduce) {
  * { transition: none !important; animation: none !important; }
}

/* ---------------------------------------------------------------- header --- */

.hb-header {
  position: sticky;
  top: 0;
  z-index: 10;
  background: var(--bg);
  padding: 14px 28px 0;
  border-bottom: 1px solid var(--border);
}

.hb-header-top {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  gap: 16px;
}

.hb-header h1 {
  margin: 0;
  font-size: 1.15rem;
  font-weight: 600;
}

.hb-sub {
  color: var(--muted);
  font-weight: 400;
  font-size: 0.85rem;
  margin-left: 8px;
}

.hb-header-right {
  display: flex;
  align-items: center;
  gap: 14px;
}

.hb-meta {
  color: var(--muted);
  font-size: 0.8rem;
  white-space: nowrap;
}

.hb-icon-btn {
  background: transparent;
  border: 1px solid var(--border);
  border-radius: 6px;
  color: var(--muted);
  cursor: pointer;
  font-size: 1rem;
  line-height: 1;
  padding: 6px 9px;
}
.hb-icon-btn:hover { color: var(--fg); border-color: var(--border-strong); }

/* Tabs */
.hb-tabs {
  display: flex;
  gap: 4px;
  margin-top: 12px;
  overflow-x: auto;
  scrollbar-width: none;
}
.hb-tabs::-webkit-scrollbar { display: none; }

.hb-tab {
  background: transparent;
  border: 0;
  border-bottom: 2px solid transparent;
  color: var(--muted);
  cursor: pointer;
  font: inherit;
  font-weight: 500;
  padding: 8px 14px;
  white-space: nowrap;
}
.hb-tab:hover { color: var(--fg); }
.hb-tab[aria-selected="true"] {
  color: var(--fg);
  border-bottom-color: var(--accent);
}

/* --------------------------------------------------------------- filter --- */

main { padding: 18px 28px 40px; max-width: 1400px; }

.hb-filter {
  display: flex;
  gap: 16px;
  align-items: center;
  flex-wrap: wrap;
  margin-bottom: 18px;
  color: var(--muted);
  font-size: 0.85rem;
}

.hb-filter label { display: inline-flex; align-items: center; gap: 6px; }

.hb-filter select,
.hb-filter input[type="datetime-local"],
.hb-controls select,
.hb-controls input[type="datetime-local"] {
  background: var(--panel);
  color: var(--fg);
  border: 1px solid var(--border);
  padding: 5px 8px;
  border-radius: 5px;
  font: inherit;
}

/* --------------------------------------------------------------- panels --- */

.hb-panel {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 16px 20px;
  margin-bottom: 18px;
}

.hb-panel h2 {
  font-size: 0.8rem;
  font-weight: 600;
  margin: 0 0 12px 0;
  color: var(--muted);
  letter-spacing: 0.04em;
  text-transform: uppercase;
}

.hb-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
  flex-wrap: wrap;
  gap: 12px;
  margin-bottom: 12px;
}

.hb-controls {
  display: flex;
  gap: 12px;
  align-items: center;
  font-size: 0.85rem;
  color: var(--muted);
}
.hb-controls label { display: inline-flex; align-items: center; gap: 6px; }

.hb-inline-check { display: inline-flex; align-items: center; gap: 4px; cursor: pointer; }
.hb-inline-check input { margin: 0 4px 0 0; accent-color: var(--accent); }

/* Refresh-in-flight: hold prior render, dim slightly (no skeleton flash). */
.hb-fetching { opacity: 0.55; transition: opacity 0.15s; }

/* --------------------------------------------------------------- KPI row --- */

.hb-kpis {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 14px;
  margin-bottom: 18px;
}

.hb-kpi {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 14px 16px;
  display: flex;
  flex-direction: column;
  gap: 4px;
  min-height: 92px;
}

.hb-kpi-label {
  color: var(--muted);
  font-size: 0.72rem;
  text-transform: uppercase;
  letter-spacing: 0.05em;
}

.hb-kpi-value {
  font-size: 1.9rem;
  font-weight: 600;
  line-height: 1.1;
}

.hb-kpi-sub { color: var(--muted); font-size: 0.78rem; }

.hb-kpi-spark { height: 26px; margin-top: 2px; }

/* --------------------------------------------------------------- charts --- */

.hb-chart-wrap { position: relative; height: 300px; width: 100%; }

.hb-note { color: var(--muted); font-size: 0.8rem; margin: 8px 0 0 0; }

/* --------------------------------------------------------------- tables --- */

.hb-table-wrap { overflow-x: auto; }

.hb-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.88rem;
}

.hb-table th, .hb-table td {
  padding: 7px 10px;
  text-align: left;
  border-bottom: 1px solid var(--border);
  white-space: nowrap;
}

.hb-table th {
  color: var(--muted);
  font-weight: 500;
  text-transform: uppercase;
  font-size: 0.72rem;
  letter-spacing: 0.04em;
  position: sticky;
  top: 0;
  background: var(--panel);
}

.hb-table td.hb-num, .hb-table th.hb-num { text-align: right; font-variant-numeric: tabular-nums; }
.hb-table tbody tr[data-id]:hover,
.hb-table tbody tr[data-lic]:hover { background: var(--panel-2); cursor: pointer; }

.hb-empty { color: var(--muted); padding: 14px 10px; font-size: 0.85rem; }

/* Sortable headers */
.hb-table th.hb-sortable { cursor: pointer; user-select: none; }
.hb-table th.hb-sortable:hover { color: var(--fg); }
.hb-table th.hb-sortable::after { content: "⇅"; opacity: 0.3; margin-left: 4px; font-size: 0.85em; }
.hb-table th.hb-sortable[data-dir="asc"]::after { content: "▲"; opacity: 0.9; }
.hb-table th.hb-sortable[data-dir="desc"]::after { content: "▼"; opacity: 0.9; }

/* Table search box */
.hb-search {
  background: var(--panel);
  color: var(--fg);
  border: 1px solid var(--border);
  border-radius: 5px;
  padding: 5px 9px;
  font: inherit;
  font-size: 0.82rem;
  min-width: 180px;
}
.hb-search::placeholder { color: var(--muted); }

/* Segmented control (Open / Closed / All) */
.hb-seg { display: inline-flex; border: 1px solid var(--border); border-radius: 6px; overflow: hidden; }
.hb-seg button {
  background: transparent; border: 0; color: var(--muted);
  cursor: pointer; font: inherit; padding: 5px 12px;
}
.hb-seg button + button { border-left: 1px solid var(--border); }
.hb-seg button[aria-pressed="true"] { background: var(--panel-2); color: var(--fg); }

/* --------------------------------------------------------------- badges --- */

.hb-badge {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  padding: 2px 8px;
  border-radius: 10px;
  font-size: 0.72rem;
  font-weight: 600;
}
.hb-badge::before { content: ""; width: 6px; height: 6px; border-radius: 50%; background: currentColor; }
.hb-badge.live { color: var(--ok); background: color-mix(in srgb, var(--ok) 15%, transparent); }
.hb-badge.stale { color: var(--warn); background: color-mix(in srgb, var(--warn) 15%, transparent); }
.hb-badge.closed { color: var(--muted); background: color-mix(in srgb, var(--muted) 14%, transparent); }
.hb-badge.revoked { color: var(--bad); background: color-mix(in srgb, var(--bad) 15%, transparent); }

/* --------------------------------------------------------------- labels --- */

.hb-label { color: var(--fg); }
.hb-mono { font-family: ui-monospace, "SF Mono", Menlo, Consolas, monospace; }

.hb-edit {
  background: transparent; border: 0; color: var(--muted);
  cursor: pointer; font-size: 0.85rem; padding: 0 2px; margin-left: 4px; opacity: 0.55;
}
.hb-edit:hover { color: var(--accent); opacity: 1; }

.hb-muted { color: var(--muted); font-size: 0.8rem; }

/* buttons */
.hb-btn, .hb-controls button, .hb-form-actions button, .hb-key-copy, .hb-pager button {
  background: var(--panel-2);
  color: var(--fg);
  border: 1px solid var(--border);
  border-radius: 5px;
  padding: 5px 12px;
  font: inherit;
  font-size: 0.82rem;
  cursor: pointer;
}
.hb-btn:hover, .hb-controls button:hover, .hb-form-actions button:hover,
.hb-key-copy:hover, .hb-pager button:hover { border-color: var(--accent); }
.hb-btn-primary { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); font-weight: 600; }

.hb-pager { margin-top: 10px; display: flex; align-items: center; gap: 12px; }
.hb-pager:empty { display: none; }
.hb-pager button[disabled] { opacity: 0.4; cursor: default; border-color: var(--border); }
.hb-copy-row { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }

/* --------------------------------------------------------- integrate tab --- */

.hb-guide-vars {
  display: flex;
  align-items: center;
  gap: 16px;
  flex-wrap: wrap;
  margin-bottom: 14px;
  color: var(--muted);
  font-size: 0.85rem;
}
.hb-guide-vars label { display: inline-flex; align-items: center; gap: 6px; }
.hb-guide-vars input {
  background: var(--bg);
  color: var(--fg);
  border: 1px solid var(--border);
  border-radius: 5px;
  padding: 5px 8px;
  font: inherit;
  min-width: 260px;
}

.hb-guide-block { margin-bottom: 16px; }
.hb-guide-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 6px;
}
.hb-guide-title { color: var(--muted); font-size: 0.8rem; font-weight: 600; }

.hb-code {
  margin: 0;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 12px 14px;
  overflow-x: auto;
  font-family: ui-monospace, "SF Mono", Menlo, Consolas, monospace;
  font-size: 0.8rem;
  line-height: 1.5;
  white-space: pre;
  tab-size: 4;
}
.hb-code code { color: var(--fg); }

/* Rendered directive (marked output) */
.hb-directive-summary {
  cursor: pointer;
  font-size: 0.8rem;
  font-weight: 600;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: 0.04em;
}
.hb-directive-actions { display: flex; gap: 10px; margin: 12px 0; flex-wrap: wrap; }

.hb-md { font-size: 0.9rem; line-height: 1.6; }
.hb-md h1 { font-size: 1.35rem; margin: 18px 0 10px; }
.hb-md h2 { font-size: 1.1rem; margin: 22px 0 8px; border-bottom: 1px solid var(--border); padding-bottom: 4px; }
.hb-md h3 { font-size: 0.98rem; margin: 16px 0 6px; }
.hb-md h4 { font-size: 0.9rem; margin: 12px 0 6px; color: var(--muted); }
.hb-md p { margin: 8px 0; }
.hb-md ul, .hb-md ol { margin: 8px 0; padding-left: 22px; }
.hb-md li { margin: 3px 0; }
.hb-md a { color: var(--accent); }
.hb-md code {
  font-family: ui-monospace, "SF Mono", Menlo, Consolas, monospace;
  font-size: 0.85em;
  background: var(--panel-2);
  border: 1px solid var(--border);
  border-radius: 4px;
  padding: 1px 5px;
}
.hb-md pre {
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 12px 14px;
  overflow-x: auto;
}
.hb-md pre code { background: none; border: 0; padding: 0; font-size: 0.8rem; line-height: 1.5; }
.hb-md table { border-collapse: collapse; width: 100%; margin: 10px 0; font-size: 0.85rem; }
.hb-md th, .hb-md td { border: 1px solid var(--border); padding: 6px 10px; text-align: left; }
.hb-md th { background: var(--panel-2); color: var(--muted); }
.hb-md blockquote { margin: 8px 0; padding: 4px 12px; border-left: 3px solid var(--border); color: var(--muted); }
.hb-md hr { border: 0; border-top: 1px solid var(--border); margin: 16px 0; }

/* --------------------------------------------------------------- drawer --- */

.hb-drawer {
  position: fixed; top: 0; right: 0;
  width: min(520px, 92vw); height: 100vh;
  background: var(--panel);
  border-left: 1px solid var(--border);
  padding: 24px; overflow-y: auto;
  box-shadow: -6px 0 24px rgba(0, 0, 0, 0.35);
  z-index: 30;
}

.hb-close {
  position: absolute; top: 12px; right: 16px;
  background: transparent; border: 0; color: var(--muted);
  font-size: 1.5rem; cursor: pointer; line-height: 1;
}
.hb-close:hover { color: var(--fg); }

.hb-drawer pre {
  white-space: pre-wrap; word-break: break-word;
  background: var(--bg); border: 1px solid var(--border);
  padding: 12px; border-radius: 4px; font-size: 0.8rem;
}
.hb-drawer h4 { margin: 16px 0 6px; font-size: 0.85rem; }
.hb-drawer ul { margin: 0; padding-left: 18px; font-size: 0.82rem; }

/* ---------------------------------------------------------------- modal --- */

.hb-modal-overlay {
  position: fixed; inset: 0;
  background: rgba(0, 0, 0, 0.55);
  display: flex; align-items: center; justify-content: center;
  z-index: 40;
}

.hb-modal {
  position: relative;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 24px;
  width: min(560px, 94vw);
  max-height: 90vh; overflow-y: auto;
  box-shadow: 0 12px 40px rgba(0, 0, 0, 0.4);
}

.hb-form { display: grid; gap: 12px; margin-top: 12px; }
.hb-form label { display: flex; flex-direction: column; gap: 4px; font-size: 0.8rem; color: var(--muted); }
.hb-form input, .hb-form select {
  background: var(--bg); border: 1px solid var(--border); border-radius: 5px;
  padding: 6px 8px; color: var(--fg); font-size: 0.85rem;
}
.hb-form-actions { display: flex; align-items: center; gap: 12px; margin-top: 8px; }
.hb-form-err { color: var(--bad); font-size: 0.8rem; }
#hb-token-text {
  width: 100%; box-sizing: border-box; resize: vertical; margin-top: 8px;
  background: var(--bg); border: 1px solid var(--border); border-radius: 5px;
  padding: 6px 8px; color: var(--fg); word-break: break-all;
  font-family: ui-monospace, "SF Mono", Menlo, Consolas, monospace; font-size: 0.72rem;
}
.hb-key { word-break: break-all; font-size: 0.72rem; }
.hb-events { font-size: 0.75rem; color: var(--muted); }

.hb-lic-signing { margin-top: 12px; }

.hb-explain {
  background: var(--panel-2);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 14px 18px;
  margin-bottom: 16px;
  font-size: 0.86rem;
}
.hb-explain strong { display: block; margin-bottom: 6px; }
.hb-explain ol { margin: 0 0 6px; padding-left: 20px; }
.hb-explain li { margin: 3px 0; }
.hb-explain .hb-note { margin-top: 8px; }
input[type="file"] { color: var(--fg); font: inherit; }

/* ------------------------------------------------------------- a11y/focus - */

:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
.hb-close:focus-visible, .hb-tab:focus-visible { outline-offset: -2px; }

/* -------------------------------------------------------------- responsive - */

@media (max-width: 880px) {
  .hb-header, main { padding-left: 16px; padding-right: 16px; }
  .hb-kpis { grid-template-columns: repeat(2, 1fr); }
  .hb-header-top { flex-direction: column; align-items: flex-start; gap: 6px; }
  .hb-drawer { width: 100vw; }
  .hb-col-hide-sm { display: none; }
}

@media (max-width: 480px) {
  .hb-kpis { grid-template-columns: 1fr; }
}

/* ------------------------------------------------------------ login --- */

.hb-login {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px 16px;
  max-width: none;
}
.hb-login-card { width: 100%; max-width: 420px; }
.hb-login-card h1 { font-size: 1.15rem; margin: 0 0 4px 0; }
.hb-login-card h2 { text-transform: none; letter-spacing: 0; font-size: 1rem; color: var(--fg); }
.hb-qr {
  background: #ffffff;  /* QR needs a light quiet zone in both themes */
  border-radius: 8px;
  padding: 8px;
  width: 220px;
  height: 220px;
  margin: 8px auto;
}
.hb-qr svg { width: 100%; height: 100%; display: block; }
.hb-recovery {
  font-size: 0.95rem;
  line-height: 1.7;
  letter-spacing: 0.04em;
  user-select: all;
}
