/* === Reset & Base === */
* { margin: 0; padding: 0; box-sizing: border-box; }
body { font-family: -apple-system, system-ui, 'Segoe UI', sans-serif; background: #0f172a; color: #e2e8f0; }
a { color: #3b82f6; text-decoration: none; }
a:hover { text-decoration: underline; }

.skip-link {
  position: absolute;
  left: -9999px;
  top: 0;
  background: #f8fafc;
  color: #0f172a;
  padding: 0.65rem 0.9rem;
  border-radius: 0 0 8px 0;
  z-index: 1000;
  font-weight: 700;
}
.skip-link:focus { left: 0; }

/* === Nav === */
.nav { background: #1e293b; border-bottom: 1px solid #334155; padding: 0 1rem; display: flex; align-items: center; height: 48px; gap: 1rem; position: sticky; top: 0; z-index: 100; }
.nav-brand { font-weight: 700; font-size: 0.95rem; color: #f8fafc; white-space: nowrap; }
.nav-links { display: flex; gap: 0.25rem; }
.nav-links a { color: #94a3b8; font-size: 0.85rem; padding: 0.35rem 0.7rem; border-radius: 6px; text-decoration: none; transition: background 0.15s, color 0.15s; }
.nav-links a:hover { background: #334155; color: #e2e8f0; text-decoration: none; }
.nav-links a.active { background: #3b82f6; color: #fff; }
.nav-spacer { flex: 1; }
.nav-status { display: flex; align-items: center; gap: 6px; font-size: 0.75rem; color: #64748b; }
.nav-status .pulse { width: 8px; height: 8px; border-radius: 50%; background: #22c55e; }
.nav-status .pulse.warn { background: #eab308; }
.nav-status .pulse.dead { background: #ef4444; }
@keyframes pulse-glow { 0%, 100% { opacity: 1; } 50% { opacity: 0.4; } }
.nav-status .pulse.live { animation: pulse-glow 2s ease-in-out infinite; }

/* === Layout === */
.container { padding: 1rem; max-width: 1400px; margin: 0 auto; }
.container.narrow { max-width: 800px; }
h1 { font-size: 1.2rem; color: #94a3b8; margin-bottom: 1rem; }
h2 { font-size: 1rem; color: #94a3b8; margin: 1.5rem 0 0.8rem; }

/* === Cards === */
.grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(280px, 1fr)); gap: 1rem; margin-bottom: 1.5rem; }
.card { background: #1e293b; border-radius: 12px; padding: 1.2rem; border: 1px solid #334155; cursor: pointer; transition: border-color 0.15s; }
.card:hover { border-color: #3b82f6; }
.card-header { display: flex; justify-content: space-between; align-items: center; margin-bottom: 0.5rem; }
.device-name { font-size: 0.85rem; color: #e2e8f0; font-weight: 600; }
.device-id { font-size: 0.7rem; color: #64748b; font-family: monospace; }
.device-site { font-size: 0.7rem; color: #64748b; }
.temp { font-size: 2.4rem; font-weight: 700; color: #f8fafc; line-height: 1; }
.temp .unit { font-size: 1rem; color: #64748b; }
.pressure { font-size: 0.85rem; color: #94a3b8; margin-top: 0.2rem; }
.card-stats { display: flex; gap: 0.8rem; margin-top: 0.5rem; font-size: 0.75rem; color: #64748b; }
.card-stats .lo { color: #38bdf8; }
.card-stats .hi { color: #f87171; }
.card-sparkline { margin-top: 0.5rem; height: 30px; }
.card-sparkline canvas { width: 100%; height: 100%; }
.meta { font-size: 0.72rem; color: #475569; margin-top: 0.5rem; display: flex; justify-content: space-between; }

/* Status dot */
.status { display: inline-block; width: 8px; height: 8px; border-radius: 50%; margin-right: 5px; vertical-align: middle; }
.status.online { background: #22c55e; }
.status.stale { background: #eab308; }
.status.offline { background: #ef4444; }

/* === Controls === */
.controls { display: flex; gap: 0.5rem; margin-bottom: 1rem; flex-wrap: wrap; align-items: center; }
.controls button { background: #334155; color: #e2e8f0; border: none; padding: 0.4rem 0.8rem; border-radius: 6px; cursor: pointer; font-size: 0.8rem; transition: background 0.15s; }
.controls button:hover { background: #475569; }
.controls button.active { background: #3b82f6; }
.controls button.secondary.active { background: #3b82f6; color: #fff; }
.controls select { background: #334155; color: #e2e8f0; border: 1px solid #475569; padding: 0.4rem 0.8rem; border-radius: 6px; font-size: 0.8rem; }
.controls .sep { width: 1px; height: 24px; background: #334155; }

/* === Chart === */
.chart-section { background: #1e293b; border-radius: 12px; padding: 1.2rem; border: 1px solid #334155; margin-bottom: 1.5rem; }
.chart-section h2 { font-size: 0.9rem; color: #94a3b8; margin-bottom: 0.5rem; }
.chart-wrap { position: relative; }
.chart-wrap canvas { width: 100%; height: 250px; }
.chart-tooltip { position: absolute; pointer-events: none; background: #0f172a; border: 1px solid #475569; border-radius: 6px; padding: 0.5rem 0.7rem; font-size: 0.75rem; display: none; z-index: 10; white-space: nowrap; }
.chart-tooltip .tt-time { color: #94a3b8; margin-bottom: 0.25rem; }
.chart-tooltip .tt-row { display: flex; align-items: center; gap: 0.4rem; }
.chart-tooltip .tt-dot { width: 8px; height: 8px; border-radius: 50%; }
.chart-legend { display: flex; gap: 1rem; flex-wrap: wrap; margin-top: 0.6rem; }
.chart-legend-item { display: flex; align-items: center; gap: 0.35rem; font-size: 0.78rem; color: #94a3b8; cursor: pointer; user-select: none; }
.chart-legend-item.hidden { opacity: 0.35; }
.chart-legend-item .dot { width: 10px; height: 10px; border-radius: 50%; }

/* === Stats row === */
.latest-reading { display: flex; align-items: center; justify-content: space-between; background: #1e293b; border-radius: 12px; padding: 1rem 1.5rem; border: 1px solid #334155; margin-bottom: 1.5rem; }
.latest-reading.stale { opacity: 0.45; border-color: #475569; }
.latest-values { display: flex; align-items: baseline; gap: 1.5rem; }
.latest-temp { font-size: 2rem; font-weight: 700; color: #f8fafc; }
.latest-reading.stale .latest-temp { color: #64748b; }
.latest-pressure { font-size: 1rem; color: #94a3b8; }
.latest-age { font-size: 0.8rem; color: #64748b; }
.stat-when { font-size: 0.7rem; color: #94a3b8; margin-top: 0.15rem; }
.stats-row { display: flex; gap: 1rem; flex-wrap: wrap; margin-bottom: 1.5rem; }
.stat { background: #1e293b; border-radius: 10px; padding: 0.8rem 1.2rem; border: 1px solid #334155; min-width: 140px; }
.stat-label { font-size: 0.7rem; color: #64748b; text-transform: uppercase; letter-spacing: 0.05em; }
.stat-value { font-size: 1.5rem; font-weight: 700; color: #f8fafc; margin-top: 0.15rem; }
.stat-value .unit { font-size: 0.75rem; color: #64748b; }
.stat-value.sm { font-size: 1rem; }

/* === Table === */
.table-wrap { overflow-x: auto; -webkit-overflow-scrolling: touch; }
table { width: 100%; border-collapse: collapse; background: #1e293b; border-radius: 10px; overflow: hidden; border: 1px solid #334155; }
thead th { background: #334155; padding: 0.6rem 1rem; text-align: left; font-size: 0.72rem; color: #94a3b8; text-transform: uppercase; letter-spacing: 0.05em; }
tbody td { padding: 0.5rem 1rem; border-top: 1px solid #263348; font-size: 0.85rem; font-variant-numeric: tabular-nums; white-space: nowrap; }
tbody tr:hover { background: #263348; }

/* === Pagination === */
.pagination { display: flex; gap: 0.4rem; align-items: center; justify-content: center; margin-top: 1rem; flex-wrap: wrap; }
.pagination button { background: #334155; color: #e2e8f0; border: none; padding: 0.4rem 0.75rem; border-radius: 6px; cursor: pointer; font-size: 0.8rem; min-width: 2.2rem; }
.pagination button.active { background: #3b82f6; }
.pagination button:disabled { opacity: 0.4; cursor: default; }
.page-info { font-size: 0.8rem; color: #64748b; margin: 0 0.5rem; }

/* === Form (admin) === */
.section { background: #1e293b; border-radius: 10px; padding: 1.2rem; border: 1px solid #334155; margin-bottom: 1rem; }
input[type="text"], input[type="password"] { background: #0f172a; color: #e2e8f0; border: 1px solid #475569; border-radius: 6px; padding: 0.4rem 0.6rem; width: 100%; margin-bottom: 0.5rem; font-size: 0.85rem; }
button, .btn { background: #3b82f6; color: #fff; border: none; padding: 0.5rem 1rem; border-radius: 6px; cursor: pointer; font-size: 0.85rem; transition: background 0.15s; display: inline-flex; align-items: center; gap: 0.35rem; }
button:hover, .btn:hover { background: #2563eb; }
button.danger { background: #ef4444; }
button.danger:hover { background: #dc2626; }
button.secondary { background: #334155; color: #e2e8f0; }
button.secondary:hover { background: #475569; }
.form-row { display: flex; gap: 0.5rem; align-items: end; margin-bottom: 0.5rem; }
.form-row input { margin-bottom: 0; }
label { font-size: 0.75rem; color: #64748b; display: block; margin-bottom: 0.2rem; }
.mono { font-family: monospace; font-size: 0.8rem; color: #94a3b8; }
.key-display { background: #0f172a; padding: 0.5rem; border-radius: 6px; font-family: monospace; font-size: 0.8rem; word-break: break-all; color: #22c55e; margin-top: 0.5rem; }
.msg { padding: 0.5rem; border-radius: 6px; margin-top: 0.5rem; font-size: 0.85rem; }
.msg.ok { background: #14532d; color: #86efac; }
.msg.err { background: #450a0a; color: #fca5a5; }
.hidden { display: none; }

/* === Utility === */
.empty { text-align: center; color: #475569; padding: 3rem; }
.export-btn { margin-left: auto; }

/* === Dashboard extras === */
.dash-header { display: flex; justify-content: space-between; align-items: center; margin-bottom: 1rem; }
.dash-header h1 { margin-bottom: 0; }
.dash-header-actions { display: flex; align-items: center; gap: 0.5rem; }
.auto-refresh-indicator { font-size: 1.2rem; color: #64748b; cursor: default; transition: color 0.3s; }
.auto-refresh-indicator.spinning { color: #3b82f6; animation: spin 0.8s ease-in-out; }
@keyframes spin { from { transform: rotate(0deg); } to { transform: rotate(360deg); } }
.sort-label { font-size: 0.78rem; color: #64748b; margin-bottom: 0; white-space: nowrap; }
.group-header { font-size: 0.85rem; font-weight: 600; color: #94a3b8; padding: 0.6rem 0 0.3rem; border-bottom: 1px solid #334155; margin-top: 1rem; margin-bottom: 0.5rem; }
.group-header:first-child { margin-top: 0; }
.theme-toggle { background: none; border: none; cursor: pointer; font-size: 1.1rem; padding: 0.25rem 0.4rem; border-radius: 6px; line-height: 1; margin-right: 0.5rem; }
.theme-toggle:hover { background: #334155; }

.page-top-actions { margin-bottom: 0.8rem; }

.site-footer {
  border-top: 1px solid #334155;
  padding: 0.65rem 1rem;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.8rem;
  color: #94a3b8;
}

.footer-links {
  display: flex;
  align-items: center;
  gap: 0.8rem;
  flex-wrap: wrap;
}

.footer-links a {
  color: #94a3b8;
  font-size: 0.9rem;
  text-decoration: none;
}

.footer-links a:hover { color: #e2e8f0; text-decoration: underline; }

.sensor-top-layout {
  display: grid;
  grid-template-columns: minmax(360px, 440px) 1fr;
  gap: 1rem;
  align-items: start;
}

.sensor-topbar {
  display: flex;
  gap: 0.7rem;
  align-items: center;
  flex-wrap: wrap;
  margin-bottom: 0.8rem;
}

.sensor-topbar #chart-controls {
  margin-bottom: 0;
  flex: 1;
}

.sensor-left-col .latest-reading {
  margin-bottom: 0.8rem;
  padding: 0.9rem 1rem;
}

.sensor-left-col .controls {
  margin-bottom: 0.8rem;
}

.sensor-left-col .stats-row {
  margin-bottom: 0;
}

.sensor-left-col .stats-triplet {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 0.6rem;
}

.sensor-left-col .stats-secondary {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 0.6rem;
  margin-top: 0.6rem;
}

.sensor-left-col .stat {
  width: 100%;
  min-width: 0;
}

.sensor-right-col .chart-section {
  margin-bottom: 0;
}

.sensor-right-col .chart-wrap canvas {
  min-height: 360px;
}

/* === Card flash animation === */
@keyframes card-flash { 0% { border-color: #3b82f6; box-shadow: 0 0 8px rgba(59,130,246,0.3); } 100% { border-color: #334155; box-shadow: none; } }
.card-flash { animation: card-flash 1.5s ease-out; }

/* === Mobile === */
@media (max-width: 640px) {
  .nav { padding: 0 0.5rem; gap: 0.5rem; }
  .nav-brand { font-size: 0.85rem; }
  .nav-links a { font-size: 0.78rem; padding: 0.3rem 0.5rem; }
  .nav-status span:not(.pulse) { display: none; }
  .container { padding: 0.75rem; }
  .grid { grid-template-columns: 1fr; }
  .chart-wrap canvas { height: 300px; }
  .stats-row { gap: 0.5rem; }
  .stat { min-width: 0; flex: 1; padding: 0.6rem 0.8rem; }
  .stat-value { font-size: 1.2rem; }
  .controls { gap: 0.3rem; }
  .controls button { padding: 0.35rem 0.6rem; font-size: 0.75rem; }
  .controls select { font-size: 0.75rem; padding: 0.35rem 0.5rem; }
  .sort-label { display: none; }
  .dash-header { flex-direction: row; }
  .card .temp { font-size: 2rem; }
  .card { padding: 1rem; }
  .group-header { font-size: 0.8rem; }
  .theme-toggle { font-size: 0.95rem; padding: 0.2rem; }
  .site-footer { padding: 0.6rem 0.75rem; }
  .footer-links { gap: 0.55rem; }
  .footer-links a { font-size: 0.82rem; }
  .sensor-top-layout { grid-template-columns: 1fr; }
  .sensor-right-col .chart-wrap canvas { min-height: 280px; }
  .sensor-topbar { align-items: stretch; }
  .sensor-topbar > .btn { width: 100%; justify-content: center; }
  .sensor-topbar #chart-controls { width: 100%; }
}
[data-theme="light"] .skip-link {
  background: #0f172a;
  color: #f8fafc;
}

@media (max-width: 380px) {
  .grid { gap: 0.6rem; }
  .card .temp { font-size: 1.7rem; }
  .card-sparkline { height: 24px; }
  .nav-links { gap: 0; }
  .nav-brand { display: none; }
}

/* === Light theme === */
[data-theme="light"] body { background: #f1f5f9; color: #1e293b; }
[data-theme="light"] .nav { background: #ffffff; border-bottom-color: #e2e8f0; }
[data-theme="light"] .nav-brand { color: #0f172a; }
[data-theme="light"] .nav-links a { color: #64748b; }
[data-theme="light"] .nav-links a:hover { background: #f1f5f9; color: #1e293b; }
[data-theme="light"] .nav-links a.active { background: #3b82f6; color: #fff; }
[data-theme="light"] .nav-status { color: #94a3b8; }
[data-theme="light"] .card { background: #ffffff; border-color: #e2e8f0; }
[data-theme="light"] .card:hover { border-color: #3b82f6; }
[data-theme="light"] .temp { color: #0f172a; }
[data-theme="light"] .temp .unit { color: #94a3b8; }
[data-theme="light"] .pressure { color: #64748b; }
[data-theme="light"] .device-name { color: #1e293b; }
[data-theme="light"] .device-site, [data-theme="light"] .device-id { color: #94a3b8; }
[data-theme="light"] .card-stats { color: #94a3b8; }
[data-theme="light"] .meta { color: #94a3b8; }
[data-theme="light"] h1, [data-theme="light"] h2 { color: #64748b; }
[data-theme="light"] .controls button { background: #e2e8f0; color: #1e293b; }
[data-theme="light"] .controls button:hover { background: #cbd5e1; }
[data-theme="light"] .controls button.active { background: #3b82f6; color: #fff; }
[data-theme="light"] .controls button.secondary.active { background: #3b82f6; color: #fff; }
[data-theme="light"] .controls button.secondary { background: #e2e8f0; color: #475569; }
[data-theme="light"] .controls button.secondary:hover { background: #cbd5e1; }
[data-theme="light"] .controls select { background: #ffffff; color: #1e293b; border-color: #e2e8f0; }
[data-theme="light"] .chart-section { background: #ffffff; border-color: #e2e8f0; }
[data-theme="light"] .chart-section h2 { color: #64748b; }
[data-theme="light"] .chart-tooltip { background: #ffffff; border-color: #e2e8f0; color: #1e293b; }
[data-theme="light"] .chart-tooltip .tt-time { color: #64748b; }
[data-theme="light"] .chart-legend-item { color: #64748b; }
[data-theme="light"] .stat { background: #ffffff; border-color: #e2e8f0; }
[data-theme="light"] .stat-label { color: #94a3b8; }
[data-theme="light"] .stat-value { color: #0f172a; }
[data-theme="light"] .stat-value .unit { color: #94a3b8; }
[data-theme="light"] .latest-reading { background: #ffffff; border-color: #e2e8f0; }
[data-theme="light"] .latest-temp { color: #0f172a; }
[data-theme="light"] .latest-pressure { color: #64748b; }
[data-theme="light"] .latest-age { color: #94a3b8; }
[data-theme="light"] .stat-when { color: #94a3b8; }
[data-theme="light"] .section { background: #ffffff; border-color: #e2e8f0; }
[data-theme="light"] input[type="text"], [data-theme="light"] input[type="password"] { background: #f8fafc; color: #1e293b; border-color: #e2e8f0; }
[data-theme="light"] table { background: #ffffff; border-color: #e2e8f0; }
[data-theme="light"] thead th { background: #f1f5f9; color: #64748b; }
[data-theme="light"] tbody td { border-top-color: #f1f5f9; color: #1e293b; }
[data-theme="light"] tbody tr:hover { background: #f8fafc; }
[data-theme="light"] .empty { color: #94a3b8; }
[data-theme="light"] .group-header { color: #64748b; border-bottom-color: #e2e8f0; }
[data-theme="light"] .theme-toggle:hover { background: #f1f5f9; }
[data-theme="light"] .site-footer { border-top-color: #e2e8f0; color: #64748b; }
[data-theme="light"] .footer-links a { color: #64748b; }
[data-theme="light"] .footer-links a:hover { color: #1e293b; }
[data-theme="light"] .auto-refresh-indicator { color: #94a3b8; }
[data-theme="light"] .controls .sep { background: #e2e8f0; }
[data-theme="light"] .sort-label { color: #94a3b8; }
[data-theme="light"] .pagination button { background: #e2e8f0; color: #1e293b; }
[data-theme="light"] .pagination button.active { background: #3b82f6; color: #fff; }
[data-theme="light"] .page-info { color: #94a3b8; }
[data-theme="light"] .toggle .slider { background: #cbd5e1; }
[data-theme="light"] .toggle .slider::before { background: #64748b; }
@keyframes card-flash-light { 0% { border-color: #3b82f6; box-shadow: 0 0 8px rgba(59,130,246,0.2); } 100% { border-color: #e2e8f0; box-shadow: none; } }
[data-theme="light"] .card-flash { animation: card-flash-light 1.5s ease-out; }

/* === Toggle switch === */
.toggle { position: relative; display: inline-block; width: 36px; height: 20px; }
.toggle input { display: none; }
.toggle .slider { position: absolute; cursor: pointer; inset: 0; background: #334155; border-radius: 20px; transition: background 0.2s; }
.toggle .slider::before { content: ''; position: absolute; width: 14px; height: 14px; left: 3px; top: 3px; background: #94a3b8; border-radius: 50%; transition: transform 0.2s, background 0.2s; }
.toggle input:checked + .slider { background: #3b82f6; }
.toggle input:checked + .slider::before { transform: translateX(16px); background: #fff; }

/* === Selected table rows === */
.selected-row { background: rgba(59, 130, 246, 0.1); }
table input[type="checkbox"] { accent-color: #3b82f6; cursor: pointer; }
