/* ==============================================================================================
   RCPS Refrigeration Monitoring

   Palette roles follow the house data-viz system. Light values live on bare :root; the dark
   block is redefined twice so the OS setting and an explicit theme stamp both win in the right
   direction. Status colors are fixed in both modes and always ship with an icon and a text
   label, never as color alone.
   ============================================================================================== */

:root {
  color-scheme: light;

  --page-plane:      #f9f9f7;
  --surface-1:       #fcfcfb;
  --surface-2:       #f2f2ee;
  --text-primary:    #0b0b0b;
  --text-secondary:  #52514e;
  --text-muted:      #898781;
  --gridline:        #e1e0d9;
  --axis:            #c3c2b7;
  --border:          rgba(11, 11, 11, 0.10);
  --shadow:          0 1px 2px rgba(11, 11, 11, 0.05), 0 4px 12px rgba(11, 11, 11, 0.04);

  /* Single-series line: categorical slot 1. */
  --series-1:        #2a78d6;

  /* Status palette -- fixed, never themed. */
  --status-good:     #0ca30c;
  --status-warning:  #fab219;
  --status-serious:  #ec835a;
  --status-critical: #d03b3b;
  --status-offline:  #898781;

  /* Threshold washes on the charts. Kept faint so the data line stays dominant. */
  --wash-warning:    rgba(250, 178, 25, 0.13);
  --wash-critical:   rgba(208, 59, 59, 0.11);
  --wash-good:       rgba(12, 163, 12, 0.07);

  --radius:          10px;
  --radius-sm:       6px;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    color-scheme: dark;

    --page-plane:     #0d0d0d;
    --surface-1:      #1a1a19;
    --surface-2:      #232322;
    --text-primary:   #ffffff;
    --text-secondary: #c3c2b7;
    --text-muted:     #898781;
    --gridline:       #2c2c2a;
    --axis:           #383835;
    --border:         rgba(255, 255, 255, 0.10);
    --shadow:         0 1px 2px rgba(0, 0, 0, 0.4), 0 4px 12px rgba(0, 0, 0, 0.3);

    --series-1:       #3987e5;

    --wash-warning:   rgba(250, 178, 25, 0.16);
    --wash-critical:  rgba(208, 59, 59, 0.16);
    --wash-good:      rgba(12, 163, 12, 0.10);
  }
}

:root[data-theme="dark"] {
  color-scheme: dark;

  --page-plane:     #0d0d0d;
  --surface-1:      #1a1a19;
  --surface-2:      #232322;
  --text-primary:   #ffffff;
  --text-secondary: #c3c2b7;
  --text-muted:     #898781;
  --gridline:       #2c2c2a;
  --axis:           #383835;
  --border:         rgba(255, 255, 255, 0.10);
  --shadow:         0 1px 2px rgba(0, 0, 0, 0.4), 0 4px 12px rgba(0, 0, 0, 0.3);

  --series-1:       #3987e5;

  --wash-warning:   rgba(250, 178, 25, 0.16);
  --wash-critical:  rgba(208, 59, 59, 0.16);
  --wash-good:      rgba(12, 163, 12, 0.10);
}

* { box-sizing: border-box; }

body {
  margin: 0;
  padding: 0 clamp(12px, 3vw, 32px) 48px;
  background: var(--page-plane);
  color: var(--text-primary);
  font-family: system-ui, -apple-system, "Segoe UI", sans-serif;
  font-size: 16px;
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
}

.skip-link {
  position: absolute;
  left: -9999px;
}
.skip-link:focus {
  left: 12px;
  top: 12px;
  z-index: 20;
  padding: 8px 14px;
  background: var(--surface-1);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
}

/* ---------------------------------------------------------------------------- masthead ----- */

.masthead {
  display: flex;
  flex-wrap: wrap;
  align-items: flex-end;
  justify-content: space-between;
  gap: 16px;
  padding: 28px 0 20px;
}

.masthead h1 {
  margin: 0;
  font-size: clamp(1.5rem, 1.1rem + 1.6vw, 2.1rem);
  font-weight: 650;
  letter-spacing: -0.02em;
}

.masthead-sub {
  margin: 4px 0 0;
  color: var(--text-secondary);
  font-size: 0.95rem;
}

.masthead-actions {
  display: flex;
  align-items: center;
  gap: 14px;
}

.freshness {
  margin: 0;
  color: var(--text-secondary);
  font-size: 0.85rem;
  text-align: right;
}
.freshness .dot {
  display: inline-block;
  width: 8px;
  height: 8px;
  margin-right: 6px;
  border-radius: 50%;
  background: var(--status-good);
  vertical-align: 1px;
}
.freshness.is-stale .dot { background: var(--status-critical); }

.ghost-button {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 7px 12px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: var(--surface-1);
  color: var(--text-secondary);
  font: inherit;
  font-size: 0.85rem;
  cursor: pointer;
}
.ghost-button:hover { color: var(--text-primary); }

/* ------------------------------------------------------------------------ alert banner ----- */

.alert-banner {
  display: flex;
  align-items: flex-start;
  gap: 12px;
  margin-bottom: 20px;
  padding: 14px 18px;
  border: 1px solid var(--border);
  border-left: 4px solid var(--status-critical);
  border-radius: var(--radius);
  background: var(--surface-1);
  box-shadow: var(--shadow);
}
.alert-banner.level-warning { border-left-color: var(--status-warning); }
.alert-banner.level-offline { border-left-color: var(--status-offline); }

.alert-banner .alert-icon {
  flex: none;
  font-size: 1.2rem;
  line-height: 1.3;
}
.alert-banner h2 {
  margin: 0 0 2px;
  font-size: 1rem;
  font-weight: 650;
}
.alert-banner p {
  margin: 0;
  color: var(--text-secondary);
  font-size: 0.9rem;
}

/* ----------------------------------------------------------------------------- summary ----- */

.summary { margin-bottom: 18px; }

.summary-list {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
  margin: 0;
  padding: 0;
  list-style: none;
}

.summary-list li {
  display: flex;
  align-items: baseline;
  gap: 8px;
  padding: 9px 15px;
  border: 1px solid var(--border);
  border-radius: 999px;
  background: var(--surface-1);
  font-size: 0.88rem;
  color: var(--text-secondary);
}
.summary-list .count {
  font-size: 1.05rem;
  font-weight: 650;
  color: var(--text-primary);
}
.summary-list .swatch {
  width: 9px;
  height: 9px;
  border-radius: 50%;
  background: var(--status-offline);
}
.summary-list li.s-ok       .swatch { background: var(--status-good); }
.summary-list li.s-warn     .swatch { background: var(--status-warning); }
.summary-list li.s-alarm    .swatch { background: var(--status-critical); }
.summary-list li.s-fault    .swatch { background: var(--status-serious); }

/* ----------------------------------------------------------------------------- toolbar ----- */

.toolbar {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 22px;
  margin-bottom: 22px;
}

.range-control,
.view-control {
  display: flex;
  align-items: center;
  gap: 10px;
}

.control-label {
  color: var(--text-secondary);
  font-size: 0.82rem;
}

.segmented {
  display: inline-flex;
  padding: 3px;
  border: 1px solid var(--border);
  border-radius: 999px;
  background: var(--surface-2);
}
.segmented button {
  padding: 6px 14px;
  border: 0;
  border-radius: 999px;
  background: transparent;
  color: var(--text-secondary);
  font: inherit;
  font-size: 0.85rem;
  cursor: pointer;
}
.segmented button[aria-checked="true"] {
  background: var(--surface-1);
  color: var(--text-primary);
  font-weight: 600;
  box-shadow: var(--shadow);
}

/* ------------------------------------------------------------------------------- sites ----- */

.site-section { margin-bottom: 34px; }

.site-heading {
  display: flex;
  align-items: baseline;
  gap: 12px;
  margin: 0 0 14px;
  font-size: 1.15rem;
  font-weight: 650;
  letter-spacing: -0.01em;
}
.site-heading .site-meta {
  color: var(--text-muted);
  font-size: 0.82rem;
  font-weight: 400;
}

.tile-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(330px, 1fr));
  gap: 16px;
}

/* -------------------------------------------------------------------------------- tile ----- */

.tile {
  display: flex;
  flex-direction: column;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface-1);
  box-shadow: var(--shadow);
  overflow: hidden;
}

/* A 3px rail carries status redundantly with the pill's icon and word. */
.tile::before {
  content: "";
  display: block;
  height: 3px;
  background: var(--status-offline);
}
.tile.s-ok::before      { background: var(--status-good); }
.tile.s-warn::before    { background: var(--status-warning); }
.tile.s-alarm::before   { background: var(--status-critical); }
.tile.s-fault::before   { background: var(--status-serious); }

.tile-head {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 12px;
  padding: 15px 18px 0;
}

.tile-name {
  margin: 0;
  font-size: 1rem;
  font-weight: 600;
}
.tile-sub {
  margin: 2px 0 0;
  color: var(--text-muted);
  font-size: 0.78rem;
}

.status-pill {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  flex: none;
  padding: 4px 10px;
  border-radius: 999px;
  background: var(--surface-2);
  color: var(--text-secondary);
  font-size: 0.74rem;
  font-weight: 650;
  letter-spacing: 0.04em;
  text-transform: uppercase;
  white-space: nowrap;
}
.status-pill .pill-icon { font-size: 0.85rem; line-height: 1; }
.s-ok    .status-pill { color: #0a7c0a; }
.s-warn  .status-pill { color: #8a5c00; }
.s-alarm .status-pill { color: #a32b2b; }
.s-fault .status-pill { color: #9a4a26; }

:root[data-theme="dark"] .s-ok    .status-pill { color: var(--status-good); }
:root[data-theme="dark"] .s-warn  .status-pill { color: var(--status-warning); }
:root[data-theme="dark"] .s-alarm .status-pill { color: var(--status-critical); }
:root[data-theme="dark"] .s-fault .status-pill { color: var(--status-serious); }

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) .s-ok    .status-pill { color: var(--status-good); }
  :root:not([data-theme="light"]) .s-warn  .status-pill { color: var(--status-warning); }
  :root:not([data-theme="light"]) .s-alarm .status-pill { color: var(--status-critical); }
  :root:not([data-theme="light"]) .s-fault .status-pill { color: var(--status-serious); }
}

.tile-reading {
  display: flex;
  align-items: baseline;
  gap: 10px;
  padding: 6px 18px 0;
}
.tile-value {
  font-size: 3rem;
  font-weight: 620;
  letter-spacing: -0.03em;
  line-height: 1.05;
}
.tile-unit {
  color: var(--text-secondary);
  font-size: 1.1rem;
  font-weight: 500;
}
.tile-value.is-missing {
  color: var(--text-muted);
  font-size: 2rem;
}

.tile-note {
  margin: 6px 18px 0;
  font-size: 0.82rem;
  color: var(--text-secondary);
}
.tile-note.is-urgent { color: var(--status-critical); font-weight: 600; }

.tile-spark {
  padding: 10px 12px 0;
}
.tile-spark svg { display: block; width: 100%; height: 54px; overflow: visible; }

.tile-stats {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 2px;
  margin: 12px 18px 0;
  padding-top: 12px;
  border-top: 1px solid var(--gridline);
}
.tile-stats div { text-align: left; }
.tile-stats dt {
  margin: 0;
  color: var(--text-muted);
  font-size: 0.7rem;
  text-transform: uppercase;
  letter-spacing: 0.05em;
}
.tile-stats dd {
  margin: 1px 0 0;
  font-size: 0.95rem;
  font-weight: 600;
  font-variant-numeric: tabular-nums;
}

.tile-actions {
  display: flex;
  gap: 8px;
  padding: 12px 18px 15px;
}
.tile-actions button {
  padding: 6px 11px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: transparent;
  color: var(--text-secondary);
  font: inherit;
  font-size: 0.8rem;
  cursor: pointer;
}
.tile-actions button:hover { color: var(--text-primary); background: var(--surface-2); }

.tile-chart {
  padding: 0 12px 14px;
  border-top: 1px solid var(--gridline);
}
.tile-chart[hidden] { display: none; }
.tile-chart svg { display: block; width: 100%; height: 230px; overflow: visible; }
.chart-caption {
  margin: 6px 6px 0;
  color: var(--text-muted);
  font-size: 0.74rem;
}

/* -------------------------------------------------------------------------- chart parts ----- */

.grid-line   { stroke: var(--gridline); stroke-width: 1; }
.axis-line   { stroke: var(--axis); stroke-width: 1; }
.axis-text   { fill: var(--text-muted); font-size: 11px; font-variant-numeric: tabular-nums; }
.series-line { fill: none; stroke: var(--series-1); stroke-width: 2; stroke-linejoin: round; stroke-linecap: round; }
.series-dot  { fill: var(--series-1); stroke: var(--surface-1); stroke-width: 2; }
.band-good     { fill: var(--wash-good); }
.band-warning  { fill: var(--wash-warning); }
.band-critical { fill: var(--wash-critical); }
.crosshair   { stroke: var(--text-muted); stroke-width: 1; stroke-dasharray: 3 3; }

.chart-tooltip {
  position: fixed;
  z-index: 30;
  padding: 7px 10px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: var(--surface-1);
  box-shadow: var(--shadow);
  color: var(--text-primary);
  font-size: 0.8rem;
  pointer-events: none;
  white-space: nowrap;
}
.chart-tooltip .tt-value {
  font-weight: 650;
  font-variant-numeric: tabular-nums;
}
.chart-tooltip .tt-time {
  display: block;
  color: var(--text-muted);
  font-size: 0.72rem;
}

.empty-chart {
  padding: 28px 8px;
  color: var(--text-muted);
  font-size: 0.85rem;
  text-align: center;
}

/* ---------------------------------------------------------------------------- table view ---- */

.table-view {
  margin-bottom: 34px;
  padding: 4px 18px 18px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface-1);
  box-shadow: var(--shadow);
  overflow-x: auto;
}
.readings-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.88rem;
}
.readings-table caption {
  padding: 14px 0 10px;
  color: var(--text-secondary);
  font-size: 0.82rem;
  text-align: left;
}
.readings-table th,
.readings-table td {
  padding: 9px 12px 9px 0;
  border-bottom: 1px solid var(--gridline);
  text-align: left;
  vertical-align: baseline;
}
.readings-table th {
  color: var(--text-muted);
  font-size: 0.74rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.05em;
}
.readings-table .num { text-align: right; font-variant-numeric: tabular-nums; }
.readings-table td.num { font-weight: 600; }

/* ------------------------------------------------------------------------------ devices ----- */

.devices h2 {
  margin: 0 0 14px;
  font-size: 1.15rem;
  font-weight: 650;
}

.device-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(270px, 1fr));
  gap: 14px;
}

.device-card {
  padding: 14px 16px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface-1);
  box-shadow: var(--shadow);
  font-size: 0.83rem;
}
.device-card h3 {
  display: flex;
  align-items: center;
  gap: 7px;
  margin: 0 0 8px;
  font-size: 0.92rem;
  font-weight: 620;
}
.device-card .conn {
  width: 9px;
  height: 9px;
  border-radius: 50%;
  background: var(--status-good);
  flex: none;
}
.device-card.is-offline .conn { background: var(--status-critical); }

.device-card dl {
  display: grid;
  grid-template-columns: auto 1fr;
  gap: 3px 12px;
  margin: 0;
}
.device-card dt { color: var(--text-muted); }
.device-card dd { margin: 0; color: var(--text-secondary); }

/* ---------------------------------------------------------------------------- colophon ----- */

.colophon {
  max-width: 78ch;
  margin-top: 40px;
  padding-top: 18px;
  border-top: 1px solid var(--gridline);
  color: var(--text-muted);
  font-size: 0.8rem;
}
.colophon p { margin: 0 0 6px; }
.colophon code {
  padding: 1px 4px;
  border-radius: 4px;
  background: var(--surface-2);
  font-size: 0.95em;
}

/* -------------------------------------------------------------------- wall-display mode ----- */

/* A kitchen office monitor is read from across the room, so above 1800px everything grows. */
@media (min-width: 1800px) {
  body { font-size: 18px; }
  .tile-grid { grid-template-columns: repeat(auto-fill, minmax(420px, 1fr)); }
  .tile-value { font-size: 4rem; }
}

@media (max-width: 520px) {
  .tile-grid { grid-template-columns: 1fr; }
  .tile-value { font-size: 2.6rem; }
  .masthead { padding-top: 20px; }
  .toolbar { gap: 14px; }
}

@media print {
  body { background: #fff; color: #000; padding: 0; }
  .toolbar, .tile-actions, .masthead-actions, .skip-link { display: none; }
  .tile, .device-card, .table-view { box-shadow: none; break-inside: avoid; }
}

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