:root {
  --bg: #0f1720;
  --panel: #17212b;
  --border: #24303d;
  --text: #e6edf3;
  --muted: #8b98a5;
  --accent: #3fb950;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
}

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-wrap: wrap;
  gap: 1rem;
  padding: 1rem 1.5rem;
  border-bottom: 1px solid var(--border);
  background: var(--panel);
}

.topbar h1 {
  font-size: 1.15rem;
  margin: 0;
  letter-spacing: 0.02em;
}

.controls {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  flex-wrap: wrap;
}

.controls label,
.controls .field {
  display: flex;
  flex-direction: column;
  font-size: 0.7rem;
  color: var(--muted);
  gap: 0.2rem;
}

.controls input,
.controls select {
  background: var(--bg);
  border: 1px solid var(--border);
  color: var(--text);
  /* Extra right padding so the native dropdown arrow isn't flush to the border. */
  padding: 0.35rem 1.6rem 0.35rem 0.5rem;
  border-radius: 6px;
  font-size: 0.85rem;
}

.controls button {
  align-self: flex-end;
  background: var(--accent);
  color: #05230d;
  border: 0;
  padding: 0.45rem 0.9rem;
  border-radius: 6px;
  font-weight: 600;
  cursor: pointer;
}

.status { font-size: 0.75rem; color: var(--muted); }

.cards {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(160px, 1fr));
  gap: 1rem;
  padding: 1.5rem;
}

.card {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 1.1rem 1.25rem;
  display: flex;
  flex-direction: column;
  gap: 0.15rem;
}

.card .label { font-size: 0.75rem; color: var(--muted); text-transform: uppercase; letter-spacing: 0.05em; }
.card .value { font-size: 2rem; font-weight: 700; }
.card .unit { font-size: 0.8rem; color: var(--muted); }

.charts {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(min(320px, 100%), 1fr));
  gap: 1rem;
  padding: 0 1.5rem 1.5rem;
}

.chart-wrap {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 1rem;
  height: 260px;
}

.footer {
  padding: 1rem 1.5rem;
  border-top: 1px solid var(--border);
  color: var(--muted);
  font-size: 0.75rem;
}

/* Navigation between pages */
.nav {
  display: flex;
  gap: 1rem;
}

.nav a {
  color: var(--muted);
  text-decoration: none;
  font-size: 0.85rem;
  padding: 0.3rem 0.1rem;
  border-bottom: 2px solid transparent;
}

.nav a:hover {
  color: var(--text);
}

.nav a.active {
  color: var(--text);
  border-bottom-color: var(--accent);
}

/* Readings table */
.table-wrap {
  margin: 1.5rem;
  max-height: calc(100vh - 220px);
  overflow: auto;
  border: 1px solid var(--border);
  border-radius: 10px;
}

.readings-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.85rem;
}

.readings-table thead th {
  position: sticky;
  top: 0;
  background: var(--panel);
  color: var(--muted);
  text-align: left;
  font-weight: 600;
  text-transform: uppercase;
  font-size: 0.7rem;
  letter-spacing: 0.05em;
  padding: 0.6rem 0.9rem;
  border-bottom: 1px solid var(--border);
}

.readings-table tbody td {
  padding: 0.5rem 0.9rem;
  border-bottom: 1px solid var(--border);
}

.readings-table tbody tr:hover {
  background: rgba(255, 255, 255, 0.03);
}

.readings-table .empty {
  text-align: center;
  color: var(--muted);
  padding: 1.5rem;
}

.pager {
  display: flex;
  align-items: center;
  gap: 1rem;
}

.pager button {
  background: var(--bg);
  border: 1px solid var(--border);
  color: var(--text);
  padding: 0.4rem 0.8rem;
  border-radius: 6px;
  cursor: pointer;
}

.pager button:disabled {
  opacity: 0.4;
  cursor: not-allowed;
}

/* Usage page */
.usage-grid {
  display: grid;
  /* min(360px, 100%) caps the track minimum at the container width so the
     column shrinks to fit on a narrow phone instead of forcing a 360px track
     that overflows the viewport to the right. */
  grid-template-columns: repeat(auto-fit, minmax(min(360px, 100%), 1fr));
  gap: 1rem;
  padding: 1.5rem;
  align-items: start;
}

.usage-card h2 {
  margin: 0 0 0.5rem;
  font-size: 1rem;
}

.usage-card .hint {
  color: var(--muted);
  font-size: 0.8rem;
  margin: 0 0 1rem;
  line-height: 1.5;
}

.tabs {
  display: flex;
  gap: 0.4rem;
  margin-bottom: 0.75rem;
  flex-wrap: wrap;
}

.tabs button {
  background: var(--bg);
  border: 1px solid var(--border);
  color: var(--muted);
  padding: 0.35rem 0.7rem;
  border-radius: 6px;
  font-size: 0.8rem;
  cursor: pointer;
}

.tabs button.active {
  color: var(--text);
  border-color: var(--accent);
}

.sample {
  background: #0b1017;
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 0.9rem;
  overflow-x: auto;
  font-size: 0.78rem;
  line-height: 1.5;
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  color: #cdd9e5;
  white-space: pre;
  margin: 0;
}

/* Grouped chart panels (schema-driven dashboard) */
#groups {
  display: grid;
  /* min(420px, 100%) caps the track minimum at the container width so on a
     narrow phone the column shrinks to fit instead of forcing a 420px track
     that overflows the viewport. */
  grid-template-columns: repeat(auto-fit, minmax(min(420px, 100%), 1fr));
  gap: 1rem;
  padding: 0 1.5rem 1.5rem;
}

.group {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 1rem 1.1rem 1.25rem;
}

.group-title {
  margin: 0 0 0.75rem;
  font-size: 0.8rem;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: 0.05em;
}

/* Inside a group the chart fills the panel; override the standalone card style */
.group .chart-wrap {
  background: transparent;
  border: 0;
  border-radius: 0;
  padding: 0;
  height: 280px;
}

/* Grafana-style refresh control: icon button + auto-refresh interval select */
.refresh-control {
  display: inline-flex;
}

.refresh-control #refresh {
  align-self: auto;
  background: var(--bg);
  color: var(--text);
  border: 1px solid var(--border);
  border-right: 0;
  border-radius: 6px 0 0 6px;
  padding: 0.35rem 0.6rem;
  font-size: 1rem;
  line-height: 1;
  font-weight: 400;
}

.refresh-control #refresh:hover {
  color: var(--accent);
}

.refresh-control #auto-refresh {
  border-radius: 0 6px 6px 0;
  border-color: var(--border);
}

/* Spin only the refresh glyph (not the button box) while a refresh is in flight. */
@keyframes onair-spin {
  to {
    transform: rotate(360deg);
  }
}
.refresh-icon {
  display: inline-block;
  line-height: 1;
}
.refresh-control #refresh.spinning {
  color: var(--accent);
}
.refresh-control #refresh.spinning .refresh-icon {
  animation: onair-spin 0.7s linear infinite;
}

/* ===== Real-time Dashboard ===== */

/* Connection status badge (top-right of header) */
.conn-badge {
  display: inline-flex;
  align-items: center;
  gap: 0.4rem;
  font-size: 0.75rem;
  padding: 0.3rem 0.7rem;
  border-radius: 999px;
  border: 1px solid var(--border);
}
.conn-badge .conn-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
}
.conn-ok {
  color: #3fb950;
  background: rgba(63, 185, 80, 0.12);
}
.conn-ok .conn-dot {
  background: #3fb950;
  box-shadow: 0 0 6px #3fb950;
}
.conn-bad {
  color: #f2545b;
  background: rgba(242, 84, 91, 0.12);
}
.conn-bad .conn-dot {
  background: #f2545b;
}

/* 2x2 panel grid */
.rt-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 1rem;
  padding: 1.5rem;
}
.rt-panel {
  min-height: 0;
}
.panel-title {
  margin: 0 0 1rem;
  font-size: 0.8rem;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: 0.05em;
}

/* Upper-left: AQI gauge + pollutant bars side by side */
.rt-panel:first-child {
  display: grid;
  grid-template-columns: auto 1fr;
  gap: 1.5rem;
  align-items: center;
}
.aqi-block {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 0.5rem;
}
.aqi-gauge {
  position: relative;
  width: 200px;
  height: 200px;
}
.aqi-center {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
}
.aqi-value {
  font-size: 2.6rem;
  font-weight: 700;
  line-height: 1;
}
.aqi-label {
  font-size: 0.75rem;
  color: var(--muted);
  letter-spacing: 0.1em;
}
.aqi-category {
  font-size: 0.95rem;
  font-weight: 600;
  text-align: center;
}
.aqi-dominant {
  font-size: 0.72rem;
  color: var(--muted);
}

/* Pollutant bars */
.pollutant-bars {
  display: flex;
  flex-direction: column;
  gap: 0.6rem;
  width: 100%;
}
.bar-row {
  display: grid;
  /* Fixed label + value columns so the track has the same start/end on every
     row and the readings line up in a column. A variable (auto) value column
     made each row's track a different width, so they looked misaligned. */
  grid-template-columns: 54px 1fr 76px;
  align-items: center;
  gap: 0.6rem;
  font-size: 0.8rem;
}
.bar-label {
  color: var(--muted);
}
.bar-track {
  background: var(--bg);
  border-radius: 999px;
  height: 10px;
  overflow: hidden;
  width: 100%;
}
.bar-fill {
  display: block;
  height: 100%;
  border-radius: 999px;
  transition: width 0.4s ease;
}
.bar-value {
  display: flex;
  align-items: baseline;
  gap: 0.25rem;
  /* Left-aligned column immediately right of the track. */
  justify-content: flex-start;
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
}
.bar-unit {
  color: var(--muted);
  font-size: 0.7rem;
}

/* Upper-right: environment stack */
.env-stack {
  display: flex;
  flex-direction: column;
  gap: 0.75rem;
}
.env-row {
  display: grid;
  grid-template-columns: 2rem 1fr auto;
  align-items: center;
  gap: 0.75rem;
  padding: 0.75rem;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 8px;
}
.env-icon {
  font-size: 1.4rem;
  text-align: center;
}
.env-label {
  color: var(--muted);
  font-size: 0.85rem;
}
.env-value {
  font-size: 1.3rem;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
}
.env-unit {
  font-size: 0.75rem;
  color: var(--muted);
  margin-left: 0.25rem;
  font-weight: 400;
}

/* Lower-left: AQI trend chart */
.trend-wrap {
  height: 260px;
}

/* Lower-right: live-data list */
.live-list {
  list-style: none;
  margin: 0;
  padding: 0;
  display: flex;
  flex-direction: column;
}
.live-item {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0.5rem 0;
  border-bottom: 1px solid var(--border);
  font-size: 0.85rem;
}
.live-item:last-child {
  border-bottom: 0;
}
.live-left {
  display: inline-flex;
  align-items: center;
  gap: 0.6rem;
  color: var(--muted);
}
.live-dot {
  width: 9px;
  height: 9px;
  border-radius: 50%;
}
.live-right {
  font-variant-numeric: tabular-nums;
}
.live-unit {
  color: var(--muted);
  font-size: 0.72rem;
}

@media (max-width: 820px) {
  .rt-grid {
    grid-template-columns: 1fr;
  }
  .rt-panel:first-child {
    grid-template-columns: 1fr;
    justify-items: center;
  }
  /* The gauge/category block centers, but the bars must span the full panel
     width so their tracks stay fixed and aligned (justify-items: center would
     otherwise shrink them to content width). */
  .rt-panel:first-child .pollutant-bars {
    justify-self: stretch;
  }
}

/* Phones (e.g. iPhone 16 ~393px): trim the side gutters so the chart panels
   get more usable width while still keeping a small margin from the edges. */
@media (max-width: 480px) {
  .charts,
  #groups,
  .cards,
  .rt-grid,
  .usage-grid {
    padding-left: 0.75rem;
    padding-right: 0.75rem;
  }
}

/* ===== Usage: AQI methodology card ===== */

/* A full-width single-column section. */
.usage-grid.aqi-method {
  grid-template-columns: 1fr;
}

/* Only collapse the top padding when it follows another section (e.g. the
   usage page, where it sits directly below the two-up grid). As a first/
   standalone section (e.g. the Admin page's Delete card) it keeps the normal
   top padding so it isn't glued to the nav header. */
.usage-grid + .usage-grid.aqi-method {
  padding-top: 0;
}

/* A grid item defaults to min-width: auto, so the 4-column breakpoint table's
   min-content width stretched the card's track past the container, making this
   card a touch wider than the cards above. min-width: 0 lets the track hold the
   container width; the table (fixed layout below) then wraps to fit. */
.aqi-card {
  min-width: 0;
}

/* Fixed layout + percentage columns so the breakpoint table always fits the
   card width: columns are sized by the percentages below and long cells wrap
   instead of forcing the table wider (which previously caused a scrollbar). */
.aqi-card .fields-table {
  table-layout: fixed;
  width: 100%;
}

.aqi-card .fields-table th,
.aqi-card .fields-table td {
  padding: 0.5rem 0.6rem;
  /* Wrap only between words, never mid-word (which produced "POLLUTAN|T").
     Hyphenated terms like "8-hour" may still wrap at the hyphen, which reads
     fine. */
  overflow-wrap: normal;
  word-break: normal;
}

.aqi-card .fields-table th:nth-child(1),
.aqi-card .fields-table td:nth-child(1) {
  width: 26%;
}
.aqi-card .fields-table th:nth-child(2),
.aqi-card .fields-table td:nth-child(2) {
  width: 24%;
}
.aqi-card .fields-table th:nth-child(3),
.aqi-card .fields-table td:nth-child(3) {
  width: 20%;
}
.aqi-card .fields-table th:nth-child(4),
.aqi-card .fields-table td:nth-child(4) {
  width: 30%;
}

/* On narrow phones the non-breaking unit tokens (e.g. "µg/m³ → ppm") are the
   limiting factor; nudge the table font down so they fit their column. */
@media (max-width: 480px) {
  .aqi-card .fields-table {
    font-size: 0.78rem;
  }
}

.aqi-card h3 {
  margin: 1.25rem 0 0.4rem;
  font-size: 0.85rem;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--muted);
}

.aqi-card .sample {
  white-space: pre-wrap;
  margin-bottom: 1rem;
}

.aqi-card a {
  color: var(--accent);
}

/* Category legend: color swatch + label per band. */
.aqi-legend {
  list-style: none;
  margin: 0 0 1rem;
  padding: 0;
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(260px, 1fr));
  gap: 0.4rem;
  font-size: 0.82rem;
  color: var(--text);
}

.aqi-legend .swatch {
  display: inline-block;
  width: 0.85rem;
  height: 0.85rem;
  border-radius: 3px;
  margin-right: 0.5rem;
  vertical-align: -1px;
}

.aqi-links {
  margin: 0;
  padding-left: 1.2rem;
  font-size: 0.82rem;
  line-height: 1.7;
}

/* Caveat callout (e.g. the CO unit mismatch). */
.aqi-note {
  border-left: 3px solid var(--accent);
  padding-left: 0.75rem;
  margin-top: 0.5rem;
}

.aqi-note strong {
  color: var(--text);
}

/* ===== Admin: delete-readings form ===== */
.admin-form {
  display: flex;
  flex-direction: column;
  gap: 0.9rem;
}

.admin-mode {
  display: flex;
  flex-wrap: wrap;
  gap: 0.9rem;
}

.admin-field {
  display: flex;
  flex-direction: column;
  gap: 0.3rem;
  font-size: 0.8rem;
  color: var(--muted);
}

.admin-field select,
.admin-field input {
  background: var(--bg);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 0.4rem 0.5rem;
  font-size: 0.85rem;
}

/* Extra right padding on dropdowns so the arrow clears the border. */
.admin-field select {
  padding-right: 1.6rem;
}

.admin-actions {
  display: flex;
  gap: 0.6rem;
  margin-top: 0.3rem;
}

.admin-actions button {
  background: var(--bg);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 0.45rem 1rem;
  font-size: 0.85rem;
  cursor: pointer;
}

.admin-actions button:hover:not(:disabled) {
  border-color: var(--accent);
}

.admin-actions button:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

/* Destructive action: red outline, filled red on hover. */
.admin-actions button.danger {
  color: #f2545b;
  border-color: #f2545b;
}

.admin-actions button.danger:hover:not(:disabled) {
  background: #f2545b;
  color: #fff;
  border-color: #f2545b;
}

/* AQI config rows (pollutant on/off + averaging window) */
.aqi-config {
  display: flex;
  flex-direction: column;
  gap: 0.4rem;
  margin-bottom: 0.8rem;
}

.aqi-config-row {
  display: grid;
  grid-template-columns: auto 4rem 1fr;
  align-items: center;
  gap: 0.6rem;
  font-size: 0.85rem;
}

.aqi-config-row input[type="checkbox"] {
  accent-color: var(--accent);
}

.aqi-config-name {
  font-weight: 600;
}

.aqi-config-window {
  color: var(--muted);
}

/* Read-only EPA breakpoint reference tables on the admin page */
#breakpoints {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(min(260px, 100%), 1fr));
  gap: 1rem 1.5rem;
}

.bp-table h3 {
  margin: 0 0 0.4rem;
  font-size: 0.9rem;
}

.bp-table .bp-basis {
  color: var(--muted);
  font-size: 0.72rem;
  font-weight: 400;
  text-transform: none;
  letter-spacing: 0;
}

.bp-table table {
  font-size: 0.8rem;
}

/* Admin browse & delete grid */
.admin-filters {
  flex-direction: row;
  flex-wrap: wrap;
  align-items: flex-end;
  gap: 0.9rem;
}

.admin-field-action button {
  background: var(--bg);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 0.4rem 0.9rem;
  font-size: 0.85rem;
  cursor: pointer;
}

.admin-field-action button:hover {
  border-color: var(--accent);
}

.admin-table-wrap {
  margin-top: 1rem;
  margin-bottom: 0.6rem;
  max-height: 70vh;
  min-height: 18rem;
  overflow: auto;
  border: 1px solid var(--border);
  border-radius: 8px;
}

.admin-browse .readings-table .check-col {
  width: 2.2rem;
  text-align: center;
  padding-left: 0.6rem;
  padding-right: 0.6rem;
}

.admin-browse .readings-table input {
  accent-color: var(--accent);
  cursor: pointer;
}

.admin-pager {
  gap: 1rem;
  flex-wrap: wrap;
}

.admin-pager button {
  background: var(--bg);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 0.35rem 0.8rem;
  font-size: 0.85rem;
  cursor: pointer;
}

.admin-pager button:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

/* Reading validation rules editor */
.validation-rules {
  display: flex;
  flex-direction: column;
  gap: 0.4rem;
  margin-bottom: 0.8rem;
}

.validation-row {
  display: grid;
  grid-template-columns: 1fr 5rem 5rem;
  align-items: center;
  gap: 0.6rem;
  font-size: 0.85rem;
}

.validation-name {
  color: var(--text);
}

.validation-row input {
  width: 100%;
  background: var(--bg);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 0.3rem 0.4rem;
  font-size: 0.82rem;
}
