/* Palette and typography carried over from ~/iot-dashboard/frontend/css/dashboard.css
   (background #180e0a, bold monospace, rounded cards) so the two dashboards
   look like siblings. */

:root {
  --bg: #180e0a;
  --bg-card: #241812;
  --bg-card-hi: #2e201a;
  --border: #3a2a22;
  --fg: #ffffff;
  --fg-dim: #a8968c;
  --fg-faint: #6f615a;
  /* UI chrome accent. Chart series colours are deliberately NOT derived from
     this — plot colours live in js/fields.js and keep their own palette. */
  --accent: #ffffff;
  --danger: #ff5c5c;
  --warn: #ffb454;
  --radius: 0.5em;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--fg);
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-weight: bold;
  font-size: 15px;
  -webkit-font-smoothing: antialiased;
}

::-webkit-scrollbar { width: 12px; height: 12px; }
::-webkit-scrollbar-track { background: #100906; }
::-webkit-scrollbar-thumb { background: #3a2a22; border-radius: 6px; }
::-webkit-scrollbar-thumb:hover { background: #554034; }
* { scrollbar-width: thin; scrollbar-color: #3a2a22 #100906; }

/* ── compact header with tabs ── */

header {
  position: sticky;
  top: 0;
  z-index: 100;
  display: flex;
  align-items: center;
  gap: 1em;
  padding: 0.45em 0.8em;
  background: rgba(24, 14, 10, 0.96);
  backdrop-filter: blur(6px);
}

.brand {
  font-size: 1.02em;
  color: var(--accent);
  white-space: nowrap;
  letter-spacing: 0.02em;
}

#tabs {
  display: flex;
  gap: 0.3em;
  overflow-x: auto;
  flex: 1;
  scrollbar-width: none;
}
#tabs::-webkit-scrollbar { display: none; }

/* Header controls share one height so the bar reads as a single row rather
   than a set of differently sized chips. */
.tab {
  flex: 0 0 auto;
  padding: 0.5em 1em;
  font: inherit;
  font-size: 0.92em;
  line-height: 1.2;
  color: var(--fg-dim);
  background: transparent;
  border: 1px solid transparent;
  border-radius: var(--radius);
  cursor: pointer;
  white-space: nowrap;
  transition: background 0.15s, color 0.15s;
}
.tab:hover { background: var(--bg-card); color: var(--fg); }
.tab.active {
  background: var(--bg-card-hi);
  color: var(--accent);
  border-color: var(--border);
}
.tab-error { color: var(--danger); }

.header-right {
  display: flex;
  align-items: center;
  gap: 0.8em;
  font-size: 0.88em;
  color: var(--fg-faint);
  white-space: nowrap;
}
.header-right a { color: var(--fg-dim); text-decoration: none; }
.header-right a:hover { color: var(--accent); }

/* ── time range picker ── */

.timepicker {
  display: flex;
  align-items: center;
  gap: 2px;
  position: relative;
  flex: 0 0 auto;
}

.timepicker button,
.timepicker select {
  font: inherit;
  font-size: 0.88em;
  line-height: 1.2;
  color: var(--fg-dim);
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: 0.35em;
  padding: 0.5em 0.7em;
  cursor: pointer;
  white-space: nowrap;
  transition: background 0.15s, color 0.15s;
}
.timepicker button:hover,
.timepicker select:hover { background: var(--bg-card-hi); color: var(--fg); }

.tp-button {
  display: flex;
  align-items: center;
  gap: 0.45em;
  min-width: 11.5em;
  justify-content: center;
}
.tp-button.active { border-color: var(--accent); color: var(--accent); }
.tp-icon { opacity: 0.8; }

/* An absolute window is not following "now" — make that visible at a glance. */
.timepicker.is-absolute .tp-button {
  color: var(--warn);
  border-color: rgba(255, 180, 84, 0.5);
}

.tp-arrow { padding: 0.5em 0.6em; }
.tp-refresh { padding-right: 0.4em; }

.tp-panel {
  position: absolute;
  top: calc(100% + 6px);
  right: 0;
  z-index: 300;
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.55);
  padding: 0.8em;
  min-width: 430px;
}
.tp-cols { display: flex; gap: 1.2em; align-items: flex-start; }
.tp-abs { flex: 1 1 45%; min-width: 0; }
.tp-quick { flex: 1 1 55%; min-width: 0; }

.tp-heading {
  font-size: 0.62em;
  color: var(--fg-faint);
  text-transform: uppercase;
  letter-spacing: 0.08em;
  margin-bottom: 0.6em;
}

.tp-field { display: block; margin-bottom: 0.5em; }
.tp-field span {
  display: block;
  font-size: 0.62em;
  color: var(--fg-dim);
  margin-bottom: 0.2em;
}
.tp-field input {
  width: 100%;
  font: inherit;
  font-size: 0.72em;
  color: var(--fg);
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 0.3em;
  padding: 0.4em 0.5em;
  outline: none;
}
.tp-field input:focus { border-color: var(--accent); }
.tp-field input::-webkit-calendar-picker-indicator { filter: invert(0.7); cursor: pointer; }

.tp-apply {
  width: 100%;
  font: inherit;
  font-size: 0.72em;
  color: var(--bg) !important;
  background: var(--accent) !important;
  border: none;
  border-radius: 0.3em;
  padding: 0.45em;
  cursor: pointer;
}
.tp-apply:hover { filter: brightness(1.1); }
.tp-err { color: var(--danger); font-size: 0.62em; min-height: 1em; margin-top: 0.3em; }

.tp-list { display: flex; flex-direction: column; max-height: 300px; overflow-y: auto; }
.tp-preset {
  text-align: left;
  border: none !important;
  background: transparent !important;
  padding: 0.35em 0.5em !important;
  border-radius: 0.3em !important;
}
.tp-preset:hover { background: var(--bg-card-hi) !important; color: var(--fg) !important; }
.tp-preset.active { color: var(--accent) !important; background: var(--bg-card-hi) !important; }

@media (max-width: 820px) {
  .tp-panel { min-width: min(92vw, 430px); right: auto; left: 0; }
  .tp-cols { flex-direction: column; gap: 0.8em; }
  .tp-button { min-width: 7em; }
  .timepicker .tp-arrow { padding: 0.35em 0.35em; }
}

/* ── widget grid: widgets flow in the order they appear in the JSON ── */

#grid {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
  padding: 10px;
  align-content: flex-start;
}

.widget {
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 0.7em 0.8em;
  min-width: 0;
  display: flex;
  flex-direction: column;
  transition: border-color 0.2s;
}
.widget.loading { border-color: #4a3a30; }
.widget.error { border-color: var(--danger); }

.w-quarter       { flex: 1 1 calc(25%  - 10px); max-width: calc(25%  - 10px); }
.w-third         { flex: 1 1 calc(33.333% - 10px); max-width: calc(33.333% - 10px); }
.w-half          { flex: 1 1 calc(50%  - 10px); max-width: calc(50%  - 10px); }
.w-twothirds     { flex: 1 1 calc(66.666% - 10px); max-width: calc(66.666% - 10px); }
.w-threequarters { flex: 1 1 calc(75%  - 10px); max-width: calc(75%  - 10px); }
.w-full          { flex: 1 1 100%; max-width: 100%; }

.widget-title {
  font-size: 0.75em;
  color: var(--fg-dim);
  margin-bottom: 0.5em;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  letter-spacing: 0.03em;
}
.widget-body { flex: 1; min-width: 0; }

.widget-error { color: var(--danger); font-size: 0.8em; word-break: break-word; }
.widget-warn  { color: var(--warn);   font-size: 0.7em; margin-bottom: 0.4em; }
.widget-empty { color: var(--fg-faint); font-size: 0.8em; padding: 0.8em 0; }

/* ── stat ── */

.stat-value { display: flex; align-items: baseline; gap: 0.25em; line-height: 1; }
.stat-number { font-size: 2.1em; }
.stat-unit { font-size: 0.9em; opacity: 0.75; }
.stat-meta {
  margin-top: 0.5em;
  font-size: 0.68em;
  color: var(--fg-faint);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

/* ── gauge ── */

.gauge-svg { width: 100%; max-height: 90px; display: block; }
.gauge-track {
  fill: none;
  stroke: #3a2a22;
  stroke-width: 10;
  stroke-linecap: round;
}
.gauge-fill {
  fill: none;
  stroke-width: 10;
  stroke-linecap: round;
  transition: stroke-dasharray 0.4s ease;
}
.gauge-readout { text-align: center; font-size: 1.4em; margin-top: -0.4em; }
.gauge-scale {
  display: flex;
  justify-content: space-between;
  font-size: 0.62em;
  color: var(--fg-faint);
  margin-top: 0.3em;
}

/* ── chart ── */

.chart-holder { width: 100%; }
.chart-meta { font-size: 0.65em; color: var(--fg-faint); margin-top: 0.3em; }

.uplot, .uplot * { font-family: ui-monospace, Menlo, monospace !important; }
.u-legend { font-size: 0.7em; color: var(--fg-dim); }
.u-legend .u-marker { border-width: 3px; }
.u-select { background: rgba(99, 255, 132, 0.12); }

/* ── table ── */

.table-wrap { overflow-x: auto; max-height: 340px; overflow-y: auto; }
.data-table { width: 100%; border-collapse: collapse; font-size: 0.72em; }
.data-table th {
  position: sticky;
  top: 0;
  background: var(--bg-card-hi);
  color: var(--fg-dim);
  text-align: left;
  padding: 0.4em 0.6em;
  font-weight: bold;
  white-space: nowrap;
}
.data-table td {
  padding: 0.32em 0.6em;
  border-top: 1px solid rgba(255, 255, 255, 0.05);
  white-space: nowrap;
}
.data-table td.ts { color: var(--fg-faint); }
.data-table tbody tr:hover { background: rgba(255, 255, 255, 0.03); }

/* ── status ── */

.status-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(140px, 1fr));
  gap: 6px;
}
.status-item {
  display: flex;
  align-items: center;
  gap: 0.5em;
  padding: 0.4em 0.6em;
  background: var(--bg-card-hi);
  border-radius: 0.35em;
  border-left: 3px solid var(--accent);
  min-width: 0;
}
.status-item.stale { border-left-color: var(--warn); }
.status-item.unknown { border-left-color: var(--danger); opacity: 0.65; }
.status-dot {
  width: 8px; height: 8px; border-radius: 50%;
  background: var(--accent);
  flex: 0 0 auto;
  box-shadow: 0 0 6px rgba(255, 255, 255, 0.5);
}
.status-item.stale .status-dot { background: var(--warn); box-shadow: 0 0 6px var(--warn); }
.status-item.unknown .status-dot { background: var(--danger); box-shadow: none; }
.status-info { min-width: 0; }
.status-name {
  font-size: 0.72em;
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.status-age { font-size: 0.62em; color: var(--fg-faint); }

/* ── text ── */

.text-widget { font-size: 0.8em; color: var(--fg-dim); font-weight: normal; }
.text-widget p { margin: 0 0 0.4em; }
.text-widget p:empty { height: 0.5em; }

/* ── page-level messages ── */

.page-error, .page-empty {
  flex: 1 1 100%;
  padding: 1.2em;
  border-radius: var(--radius);
  font-size: 0.85em;
}
.page-error { background: rgba(255, 92, 92, 0.1); color: var(--danger); border: 1px solid var(--danger); }
.page-empty { background: var(--bg-card); color: var(--fg-faint); border: 1px dashed var(--border); }

/* ── mobile ── */

@media (max-width: 820px) {
  body { font-size: 14px; }
  .w-quarter, .w-third, .w-half, .w-twothirds, .w-threequarters {
    flex-basis: 100%;
    max-width: 100%;
  }
  header { flex-wrap: wrap; gap: 0.5em; }
  .header-right { font-size: 0.7em; }
}
