/* Typography carried over from ~/iot-dashboard/frontend/css/dashboard.css
   (bold monospace, rounded cards). The palette is pure black with a neutral
   grey surface ramp — the surfaces are greys rather than the original browns
   so nothing reads warm against a #000 page. */

:root {
  --bg: #000000;
  --bg-card: #0d0d0d;
  --bg-card-hi: #181818;
  --border: #2a2a2a;
  --fg: #ffffff;
  /* Element text — titles, captions, meta lines, table headers. Brightened
     from the original warm greys. The plots keep their own axis and grid colours
     hardcoded in js/widgets.js, so this does not touch the graphs. */
  --fg-dim: #d6d6d6;
  --fg-faint: #9a9a9a;
  /* 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;

  /* Makes the browser render its own widgets dark — the native
     datetime-local calendar popup, select dropdowns, scrollbars and focus
     rings. Without it the date picker opens as a white panel over a dark
     dashboard, which no amount of CSS on the input itself can reach. */
  color-scheme: dark;
}

* { 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: #000; }
::-webkit-scrollbar-thumb { background: #2a2a2a; border-radius: 6px; }
::-webkit-scrollbar-thumb:hover { background: #3d3d3d; }
* { scrollbar-width: thin; scrollbar-color: #2a2a2a #000; }

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

header {
  position: sticky;
  top: 0;
  z-index: 100;
  display: flex;
  align-items: center;
  gap: 0.5em;
  padding: 0.45em 0.8em;
  /* body is bold; the header reads lighter than the cards it sits above */
  font-weight: 500;
  background: rgba(0, 0, 0, 0.92);
  backdrop-filter: blur(6px);
}

.brand {
  font-size: 1.02em;
  font-weight: 500;
  color: var(--accent);
  white-space: nowrap;
  letter-spacing: 0.02em;
  flex: 0 0 auto;
}
.brand-link {
  color: inherit;
  text-decoration: none;
  display: inline-flex;
  align-items: center;
}

/* Scale a tenant logo to the header rather than the other way round. */
.brand.has-logo {
  display: flex;
  align-items: center;
  /* on top of the header's own gap, so the tabs do not crowd the mark */
  margin-right: 0.9em;
}
.brand-link img {
  height: 30px;
  width: auto;
  max-width: 220px;
  object-fit: contain;
  display: block;
}
@media (max-width: 820px) {
  .brand-link img { height: 24px; max-width: 150px; }
}

#tabs {
  display: flex;
  gap: 0.3em;
  overflow-x: auto;
  flex: 1;
  margin-right: 0.5em;
  scrollbar-width: none;
  scroll-behavior: smooth;
  scroll-snap-type: x proximity;
}
#tabs::-webkit-scrollbar { display: none; }
#tabs .tab { scroll-snap-align: start; }

/* Fade the edge the strip can still scroll towards, so it is visible that
   there are more tabs than fit. Applied only when it actually overflows —
   the classes come from the dashboard as it measures the strip. */
#tabs.can-left {
  mask-image: linear-gradient(to right, transparent 0, #000 26px);
  -webkit-mask-image: linear-gradient(to right, transparent 0, #000 26px);
}
#tabs.can-right {
  mask-image: linear-gradient(to left, transparent 0, #000 26px);
  -webkit-mask-image: linear-gradient(to left, transparent 0, #000 26px);
}
#tabs.can-left.can-right {
  mask-image: linear-gradient(to right, transparent 0, #000 26px,
              #000 calc(100% - 26px), transparent 100%);
  -webkit-mask-image: linear-gradient(to right, transparent 0, #000 26px,
              #000 calc(100% - 26px), transparent 100%);
}

/* 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;
  font-weight: 500;
  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); }

/* Only an admin ever sees these, so mark them as not public. */
.tab-hidden { border-style: dashed; border-color: var(--fg-faint); opacity: 0.75; }
.tab-hidden::after { content: ' ·'; }

.header-right {
  display: flex;
  align-items: center;
  gap: 0.4em;
  font-size: 1em;
  color: var(--fg-faint);
  white-space: nowrap;
}

/* ── time range picker ── */

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

.timepicker button,
.timepicker select,
.hdr-btn {
  font: inherit;
  font-size: 0.88em;
  font-weight: 500;
  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,
.hdr-btn:hover { background: var(--bg-card-hi); color: var(--fg); }

.hdr-btn {
  text-decoration: none;
  display: inline-flex;
  align-items: center;
  gap: 0.35em;
}

.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; }


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

.tp-refresh {
  display: inline-flex;
  align-items: center;
  gap: 0.35em;
  min-width: 4.6em;
  justify-content: center;
}
.tp-refresh-icon { display: inline-block; line-height: 1; }

/* One turn per refresh tick — enough to notice, not enough to nag. */
.tp-refresh-icon.spin { animation: tp-spin 0.7s ease-in-out; }
@keyframes tp-spin {
  from { transform: rotate(0deg); }
  to   { transform: rotate(360deg); }
}
@media (prefers-reduced-motion: reduce) {
  .tp-refresh-icon.spin { animation: none; }
}

.tp-panel {
  position: absolute;
  top: calc(100% + 6px);
  right: 0;
  z-index: 300;
  color-scheme: dark;
  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.78em;
  color: var(--fg);
  background: var(--bg-card-hi);
  border: 1px solid var(--border);
  border-radius: 0.3em;
  padding: 0.5em 0.55em;
  outline: none;
  transition: border-color 0.15s, background 0.15s;
}
.tp-field input:hover { background: #202020; }
.tp-field input:focus { border-color: var(--fg-dim); background: var(--bg); }
.tp-field input::-webkit-calendar-picker-indicator {
  cursor: pointer;
  opacity: 0.55;
  transition: opacity 0.15s;
}
.tp-field input::-webkit-calendar-picker-indicator:hover { opacity: 1; }
/* the spin/segment highlight the native control paints while editing */
.tp-field input::-webkit-datetime-edit-fields-wrapper { padding: 0; }
.tp-field input::selection { background: rgba(255, 255, 255, 0.22); }

/* Sits inside the panel with the preset buttons, so it reads as one of them
   with emphasis rather than a bright slab pasted on top. */
.tp-apply {
  width: 100%;
  font: inherit;
  font-size: 0.78em;
  color: var(--fg) !important;
  background: var(--bg-card-hi) !important;
  border: 1px solid var(--fg-faint) !important;
  border-radius: 0.3em;
  padding: 0.5em;
  cursor: pointer;
  transition: background 0.15s, border-color 0.15s;
}
.tp-apply:hover {
  background: #242424 !important;
  border-color: var(--fg-dim) !important;
}
.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: #3a3a3a; }
.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: 1em;
  color: var(--fg);
  margin-bottom: 0.55em;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  letter-spacing: 0.02em;
}
.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.stale { color: var(--warn); }
.stat-meta {
  margin-top: 0.5em;
  font-size: 0.68em;
  color: var(--fg-faint);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

/* ── climate ── */

.climate {
  display: flex;
  align-items: baseline;
  gap: 0.9em;
  flex-wrap: wrap;
  line-height: 1;
  /* One size for the whole row — heading, emoji, figures and units all
     inherit it, so there is a single number to tune. */
  font-size: 1.25em;
}
/* The card's heading sits on the same row as the readings rather than
   costing a line of its own. */
.climate-title {
  color: var(--fg);
  letter-spacing: 0.02em;
  margin-right: 0.15em;
}
.climate-metric {
  display: flex;
  align-items: baseline;
  gap: 0.22em;
  min-width: 0;
}
.climate-icon { opacity: 0.9; }

.climate-chart { margin-top: 0.7em; }
.climate-chart-label {
  display: flex;
  align-items: baseline;
  gap: 1.15em;
  font-size: 0.62em;
  color: var(--fg-dim);
  letter-spacing: 0.04em;
  margin-bottom: 0.15em;
}
.climate-chart .widget-empty { padding: 0.4em 0; font-size: 0.7em; }

.climate-meta {
  margin-top: 0.6em;
  font-size: 0.68em;
  color: var(--fg-faint);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.climate-meta.stale { color: var(--warn); }

/* ── gauge ── */

.gauge-svg { width: 100%; max-height: 90px; display: block; }
.gauge-track {
  fill: none;
  stroke: #2a2a2a;
  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%; position: relative; }

.uplot-tooltip {
  position: absolute;
  z-index: 20;
  pointer-events: none;
  background: rgba(0, 0, 0, 0.94);
  border: 1px solid var(--border);
  border-radius: 0.35em;
  padding: 0.4em 0.55em;
  font-size: 0.68em;
  line-height: 1.45;
  white-space: nowrap;
  box-shadow: 0 4px 14px rgba(0, 0, 0, 0.5);
}
.uplot-tooltip .tt-time { color: var(--fg-faint); margin-bottom: 0.2em; }
.uplot-tooltip .tt-row { display: flex; align-items: center; gap: 0.4em; }
.uplot-tooltip .tt-dot {
  width: 7px; height: 7px; border-radius: 50%; flex: 0 0 auto;
}
.uplot-tooltip .tt-name { color: var(--fg-dim); }
.uplot-tooltip .tt-val { color: var(--fg); margin-left: auto; padding-left: 0.7em; }
.chart-meta { font-size: 0.65em; color: var(--fg-faint); margin-top: 0.3em; }

/* max / mean / min under a plot */
/* A grid rather than a run of text: one row per series, one column per
   figure, so now/max/mean/min line up down the legend and can be compared by
   eye. The rows are display:contents so their cells are the grid's items. */
.chart-stats {
  display: grid;
  grid-template-columns: max-content repeat(var(--cs-cols, 4), max-content);
  gap: 0.3em 1.8em;
  justify-content: start;
  font-size: 0.65em;
  color: var(--fg-dim);
  margin: 0 0 0.4em;
}
/* below the plot it needs the gap on the other side */
.chart-holder + .chart-stats { margin: 0.35em 0 0; }
.chart-stats .cs-row { display: contents; }
.chart-stats .cs-label {
  display: flex;
  align-items: center;
  gap: 0.45em;
  color: var(--fg);
  min-width: 0;
}
/* caption left, figure right, so both edges line up column to column */
.chart-stats .cs-stat {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 0.55em;
  white-space: nowrap;
}
.chart-stats .cs-cap { color: var(--fg-dim); }
.chart-stats .cs-row.is-clickable > * { cursor: pointer; }
.chart-stats .cs-row.is-clickable:hover .cs-name { text-decoration: underline; }
/* a series the reader has switched off */
.chart-stats .cs-row.is-off > * { opacity: 0.35; }
.chart-stats .cs-dot {
  width: 6px; height: 6px; border-radius: 50%; flex: 0 0 auto;
}
.chart-stats .cs-name {
  color: var(--fg);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
/* the figures read brighter than their labels */
.chart-stats b { color: var(--fg); font-weight: inherit; }
.climate-chart-label .chart-stats { margin-top: 0; font-size: 1em; }

.uplot, .uplot * { font-family: ui-monospace, Menlo, monospace !important; }
.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); }

/* ── heading ── */

/* A section divider, not a card: no surface of its own. */
.widget[data-type="heading"] {
  background: none;
  border: none;
  padding: 1.1em 0.2em 0;
}
.widget[data-type="heading"]:first-child { padding-top: 0.2em; }

.heading {
  display: flex;
  align-items: baseline;
  gap: 0.7em;
  border-bottom: 1px solid var(--border);
  padding-bottom: 0.45em;
}
.heading-title {
  font-size: 0.95em;
  color: var(--fg);
  letter-spacing: 0.14em;
  text-transform: uppercase;
}
.heading-sub {
  font-size: 0.7em;
  color: var(--fg-faint);
  font-weight: normal;
}

/* ── 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%;
  }

  /* Two rows: brand and time controls first, the tab strip beneath. Keeping
     them on one line either squeezed the picker to nothing or pushed the tabs
     off screen. */
  header {
    flex-wrap: wrap;
    gap: 0.4em 0.5em;
    padding: 0.45em 0.6em;
  }
  .brand { order: 1; }
  .timepicker { order: 2; margin-left: auto; }
  .header-right { order: 3; }
  #tabs {
    order: 4;
    flex: 1 0 100%;
    margin: 0;
    /* the strip scrolls sideways when the tabs outgrow the screen */
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    scroll-snap-type: x proximity;
    padding-bottom: 0.15em;
  }
  #tabs .tab { scroll-snap-align: start; }

  /* the admin entry point is not for a phone */
  .header-right { display: none; }

  /* The picker sits right-aligned now, so anchoring the panel to it pushed the
     panel off screen. Pin it to the viewport instead. */
  .tp-panel {
    position: fixed;
    left: 0.6em;
    right: 0.6em;
    /* top is set when the panel opens, from the header's actual height */
    width: auto;
    min-width: 0;
    max-height: calc(100vh - 7.5em);
    overflow-y: auto;
  }
  .tp-cols { flex-direction: column; gap: 0.8em; }
  .tp-list { max-height: 40vh; }

  .timepicker button, .timepicker select { font-size: 0.8em; padding: 0.45em 0.55em; }

  /* the range itself is what gets read, so it takes the space */
  .tp-button { min-width: 0; font-size: 0.92em; padding: 0.5em 0.7em; }
  .tp-label { max-width: 11em; }

  /* still a comfortable target, just not competing with the label */
  .timepicker .tp-arrow {
    font-size: 1em;
    line-height: 1;
    padding: 0.3em 0.5em;
    min-width: 1.8em;
  }
  .tp-field input { font-size: 0.9em; padding: 0.55em; }
  .tp-label { max-width: 9.5em; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
  .brand.has-logo img { height: 22px; max-width: 130px; }
}
