:root {
  --teal: #0d9488;
  --teal-deep: #0b6e7c;
  --bg: #0f172a;
  --card: #1e293b;
  --card-2: #243248;
  --text: #e2e8f0;
  --muted: #94a3b8;
  --line: #334155;
  --warn: #f59e0b;
  --crit: #ef4444;
  --ok: #22c55e;
}

* { box-sizing: border-box; }
html, body { margin: 0; overflow-x: hidden; max-width: 100%; }
body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  background: var(--bg);
  color: var(--text);
  -webkit-font-smoothing: antialiased;
  padding-bottom: env(safe-area-inset-bottom);
}

.hidden { display: none !important; }

/* Top bar */
.topbar {
  position: sticky; top: 0; z-index: 5;
  display: flex; align-items: center; justify-content: space-between;
  padding: 12px 16px; padding-top: calc(12px + env(safe-area-inset-top));
  background: linear-gradient(180deg, var(--teal), var(--teal-deep));
  color: #fff;
}
.brand { display: flex; align-items: center; gap: 8px; font-weight: 700; font-size: 18px; }
.conn { display: flex; align-items: center; gap: 6px; font-size: 13px; opacity: .9; }
.conn .dot { width: 9px; height: 9px; border-radius: 50%; background: #cbd5e1; }
.conn.online .dot { background: var(--ok); }
.conn.offline .dot { background: var(--crit); }
.status-icons { display: flex; align-items: center; gap: 16px; }
.pump-ind.on .dot { background: var(--ok); }
.pump-ind.off .dot { background: var(--crit); }

main { max-width: 560px; margin: 0 auto; padding: 16px; display: flex; flex-direction: column; gap: 16px; }

.card { background: var(--card); border-radius: 16px; padding: 16px; box-shadow: 0 1px 0 rgba(255,255,255,.03) inset; }
.card-head { display: flex; align-items: center; justify-content: space-between; gap: 8px; margin-bottom: 8px; }
.head-actions { display: flex; gap: 6px; flex: none; }
h2 { font-size: 16px; margin: 0; display: inline; }
h3 { grid-column: 1 / -1; font-size: 13px; text-transform: uppercase; letter-spacing: .04em; color: var(--muted); margin: 8px 0 0; }

/* The ⓘ button next to settings section headers — taps open a descriptive popup. */
.info-btn {
  display: inline-flex; align-items: center; justify-content: center;
  width: 16px; height: 16px; margin-left: 6px; padding: 0; vertical-align: text-bottom;
  border: 1px solid var(--muted); border-radius: 50%; background: transparent;
  color: var(--muted); font: italic 700 11px/1 Georgia, serif; text-transform: none;
  cursor: pointer;
}
.info-btn:hover, .info-btn:focus-visible { color: var(--text); border-color: var(--text); }

/* Alert banner */
.banner { border-radius: 16px; padding: 14px 16px; display: flex; flex-direction: column; gap: 10px; }
.banner.warn { background: #422006; border: 1px solid var(--warn); }
.banner.crit { background: #450a0a; border: 1px solid var(--crit); }
.banner .b-title { font-weight: 700; }
.banner .b-msg { font-size: 14px; color: #fde68a; }
.banner.crit .b-msg { color: #fecaca; }
.banner .b-actions { display: flex; gap: 8px; flex-wrap: wrap; }

/* Live gauge */
.live { display: flex; flex-direction: column; align-items: center; gap: 14px; }
.gauge-wrap { position: relative; width: 220px; height: 220px; }
.gauge { width: 220px; height: 220px; transform: rotate(-90deg); }
.gauge-track { fill: none; stroke: var(--line); stroke-width: 16; }
.gauge-arc {
  fill: none; stroke: var(--teal); stroke-width: 16; stroke-linecap: round;
  transition: stroke-dashoffset .4s ease, stroke .3s ease;
}
.gauge-center {
  position: absolute; inset: 0; display: flex; flex-direction: column;
  align-items: center; justify-content: center;
}
.watts { font-size: 46px; font-weight: 800; line-height: 1; }
.watts.snoozed { font-size: 24px; color: var(--muted); letter-spacing: .01em; }
.watts-unit { color: var(--muted); font-size: 13px; margin-top: 4px; }

.live-meta { width: 100%; display: flex; flex-direction: column; align-items: center; gap: 12px; }
.pill { padding: 6px 16px; border-radius: 999px; font-weight: 700; font-size: 14px; }
.pill.idle { background: var(--card-2); color: var(--muted); }
.pill.running { background: rgba(13,148,136,.2); color: #5eead4; }
.pill.alert { background: rgba(239,68,68,.2); color: #fca5a5; }
.pill.snooze { background: rgba(129,140,248,.16); color: #a5b4fc; font-size: 13px; padding: 4px 14px; }

.readouts { display: flex; gap: 22px; flex-wrap: wrap; justify-content: center; }
.readouts > div { text-align: center; }
.readouts span { font-size: 18px; font-weight: 700; }
.readouts label { display: block; font-size: 11px; color: var(--muted); text-transform: uppercase; }

/* Range tabs */
.range-tabs { display: flex; gap: 4px; background: var(--card-2); border-radius: 10px; padding: 3px; }
.range-tabs button { border: 0; background: transparent; color: var(--muted); padding: 5px 12px; border-radius: 8px; font-size: 13px; cursor: pointer; }
.range-tabs button.active { background: var(--teal); color: #fff; }

.chart { width: 100%; display: block; }
.chart-legend { display: flex; gap: 16px; margin-top: 8px; font-size: 12px; color: var(--muted); }
.chart-legend .energy-total { margin-left: auto; color: var(--text); font-weight: 600; }
.chart-legend .sw { display: inline-block; width: 14px; height: 10px; vertical-align: middle; margin-right: 5px; border-radius: 2px; }
.chart-legend .sw.line { background: var(--teal); height: 3px; }
.chart-legend .sw.shade { background: rgba(13,148,136,.25); }

/* Settings */
summary { cursor: pointer; list-style: none; }
summary::-webkit-details-marker { display: none; }
summary::after { content: "▾"; float: right; color: var(--muted); }
details[open] summary::after { content: "▴"; }

.actions { display: flex; gap: 8px; margin: 14px 0 6px; }
.actions .btn { flex: 1; }
.grid { display: grid; grid-template-columns: 1fr 1fr; gap: 10px 12px; margin-top: 8px; }
/* Keep [hidden] hidden even where a display rule (e.g. .grid label flex) would otherwise win. */
[hidden] { display: none !important; }
.grid label { display: flex; flex-direction: column; font-size: 12px; color: var(--muted); gap: 4px; min-width: 0; }
.grid label.check { flex-direction: row; align-items: center; gap: 8px; color: var(--text); font-size: 14px; }
.grid input[type="number"], .grid input[type="text"], .grid input[type="time"] {
  background: var(--bg); border: 1px solid var(--line); color: var(--text);
  border-radius: 8px; padding: 8px 10px; font-size: 15px;
  width: 100%; min-width: 0; /* allow inputs to shrink inside the grid (no horizontal overflow) */
}
/* Narrow phones (e.g. Galaxy Z Flip): one column so the form never overflows the viewport. */
@media (max-width: 460px) {
  .grid { grid-template-columns: 1fr; }
}
.form-actions { grid-column: 1 / -1; display: flex; align-items: center; gap: 12px; margin-top: 6px; }

.btn { border: 1px solid var(--line); background: var(--card-2); color: var(--text); padding: 9px 14px; border-radius: 10px; font-size: 14px; font-weight: 600; cursor: pointer; }
.btn.primary { background: var(--teal); border-color: var(--teal); color: #fff; }
.btn.success { background: var(--ok); border-color: var(--ok); color: #052e16; }
.btn.danger { background: var(--crit); border-color: var(--crit); color: #fff; }
.btn:active { transform: translateY(1px); }
.btn:disabled { opacity: .5; }

.pump-toggle { width: 100%; padding: 11px 14px; font-size: 15px; margin-top: 4px; margin-bottom: 2px; }
.notif-head { margin-top: 20px; }

/* Alert history */
.alert-list { list-style: none; margin: 12px 0 0; padding: 0; display: flex; flex-direction: column; gap: 2px; }
.alert-row { display: flex; gap: 10px; padding: 10px 4px; border-bottom: 1px solid var(--line); }
.alert-row:last-child { border-bottom: 0; }
.alert-row.closed { opacity: .55; }
.alert-row .dot { width: 9px; height: 9px; border-radius: 50%; margin-top: 5px; flex: none; background: var(--muted); }
.alert-row .dot.sev-warning { background: var(--warn); }
.alert-row .dot.sev-critical { background: var(--crit); }
.alert-body { flex: 1; min-width: 0; }
.alert-top { display: flex; justify-content: space-between; gap: 8px; }
.alert-when { color: var(--muted); font-size: 12px; white-space: nowrap; }
.alert-msg { font-size: 13px; color: var(--text); margin: 2px 0; }
.alert-status { font-size: 11px; color: var(--muted); text-transform: uppercase; letter-spacing: .03em; }

.hint { color: var(--muted); font-size: 13px; margin: 6px 0 0; }
/* Away switch */
.switch-row { display: flex; align-items: center; justify-content: space-between; gap: 16px; }
.away-row { margin: 18px 0 14px; }
#snoozeBtn { width: 100%; margin-top: 6px; }
#snoozeBtn.snoozed { background: var(--teal-deep); border-color: var(--teal-deep); }
.switch-label { display: flex; flex-direction: column; gap: 6px; font-weight: 600; }
.switch-label small { font-weight: 400; color: var(--muted); font-size: 12px; line-height: 1.4; }
.switch-row input[type="checkbox"] {
  appearance: none; -webkit-appearance: none; flex: none;
  width: 50px; height: 28px; border-radius: 999px; background: var(--card-2);
  border: 1px solid var(--line); position: relative; cursor: pointer; transition: background .15s;
}
.switch-row input[type="checkbox"]::after {
  content: ""; position: absolute; top: 2px; left: 2px; width: 22px; height: 22px;
  border-radius: 50%; background: #cbd5e1; transition: transform .15s;
}
.switch-row input[type="checkbox"]:checked { background: var(--teal); border-color: var(--teal); }
.switch-row input[type="checkbox"]:checked::after { transform: translateX(22px); background: #fff; }
.control-buttons { display: flex; gap: 8px; margin-top: 14px; }
.control-buttons .btn { flex: 1; }

/* Custom confirm modal */
.modal {
  position: fixed; inset: 0; z-index: 50;
  display: flex; align-items: center; justify-content: center; padding: 24px;
  background: rgba(2, 6, 23, .65); -webkit-backdrop-filter: blur(2px); backdrop-filter: blur(2px);
}
.modal-box {
  background: var(--card); border: 1px solid var(--line); border-radius: 16px;
  padding: 20px; width: 100%; max-width: 360px;
  box-shadow: 0 16px 48px rgba(0, 0, 0, .55);
  animation: modal-in .14s ease-out;
}
@keyframes modal-in { from { opacity: 0; transform: translateY(8px) scale(.98); } to { opacity: 1; transform: none; } }
.modal-title { margin: 0 0 8px; font-size: 18px; }
.modal-body { margin: 0 0 18px; color: var(--muted); font-size: 14px; line-height: 1.45; }
.modal-actions { display: flex; justify-content: flex-end; gap: 8px; }
.modal-suppress { display: flex; align-items: center; gap: 8px; margin: 0 0 14px; color: var(--muted); font-size: 13px; cursor: pointer; }

/* Interim auth gate — opaque (it hides the app from logged-out eyes), above the confirm modal. */
/* Hide the app shell until init() finishes auth routing, so the dashboard never flashes before the
   login/onboarding overlay (or the real dashboard) appears. Overlays render on the dark bg meanwhile. */
body:not(.app-ready) .topbar,
body:not(.app-ready) main,
body:not(.app-ready) .foot { visibility: hidden; }

.modal.login { background: var(--bg); z-index: 60; }
.login-box { text-align: center; }
.login-form { display: flex; flex-direction: column; gap: 10px; margin-top: 4px; }
.login-form input {
  background: var(--bg); border: 1px solid var(--line); color: var(--text);
  border-radius: 8px; padding: 11px 12px; font-size: 15px; width: 100%;
}
.login-error { color: var(--crit); font-size: 13px; min-height: 16px; margin: 0; }
.login-divider { display: flex; align-items: center; gap: 10px; color: var(--muted); font-size: 12px; margin: 16px 0 2px; }
.login-divider::before, .login-divider::after { content: ""; flex: 1; height: 1px; background: var(--line); }
.setup-steps { text-align: left; margin: 8px 0 4px; padding-left: 20px; color: var(--text); font-size: 14px; line-height: 1.5; }
.setup-steps li { margin: 8px 0; }
.setup-steps a { color: #5eead4; }

.foot { text-align: center; color: var(--muted); font-size: 12px; padding: 18px; }
.foot a { color: #5eead4; }
.linkbtn { background: none; border: 0; color: #5eead4; font: inherit; cursor: pointer; padding: 0 4px; }

/* Subscribe / device-picker / admin screens */
.btn.ghost { background: transparent; }
.login-box > .btn { width: 100%; margin-top: 10px; }
.device-list { list-style: none; margin: 8px 0 0; padding: 0; display: flex; flex-direction: column; gap: 8px; text-align: left; }
/* Long device lists must scroll inside the dialog instead of overflowing the screen, with the title
   pinned above and the Cancel button pinned below so both stay reachable on a small phone. */
#devicePicker .modal-box { display: flex; flex-direction: column; max-height: calc(100dvh - 48px); }
#devicePicker .device-list { flex: 1 1 auto; min-height: 0; overflow-y: auto; -webkit-overflow-scrolling: touch; }
#deviceCancel { flex: 0 0 auto; margin-top: 12px; }
#deviceError { flex: 0 0 auto; }

/* Terms gate: cap the dialog at the viewport and scroll the terms text, keeping the agree checkbox
   and buttons pinned and reachable on a small phone (same approach as the device picker). */
#terms .modal-box { display: flex; flex-direction: column; max-height: calc(100dvh - 48px); text-align: left; }
.terms-body { flex: 1 1 auto; min-height: 0; overflow-y: auto; -webkit-overflow-scrolling: touch;
  border: 1px solid var(--line); border-radius: 10px; padding: 12px 14px; margin: 4px 0 12px;
  font-size: 13px; line-height: 1.5; color: var(--text); }
.terms-body h1 { font-size: 17px; margin: 0 0 8px; }
.terms-body h2 { font-size: 14px; margin: 16px 0 4px; }
.terms-body p { margin: 0 0 10px; color: var(--muted); }
.terms-body ul { margin: 0 0 10px; padding-left: 18px; color: var(--muted); }
.terms-body li { margin: 2px 0; }
.terms-agree { display: flex; align-items: center; gap: 8px; font-size: 13px; color: var(--text);
  margin: 0 0 12px; cursor: pointer; flex: 0 0 auto; }
#termsAccept, #termsSignOut { flex: 0 0 auto; }
.device-btn { width: 100%; display: flex; flex-direction: column; align-items: flex-start; gap: 2px; }
.device-btn small { color: var(--muted); font-weight: 400; }
.device-btn.selected { border-color: var(--teal); }
/* Wrap so the "Change plug" button is never pushed off-screen on a narrow phone; let a long
   device name shrink/break instead of forcing horizontal overflow. */
.device-current { align-items: center; flex-wrap: wrap; }
.device-current .hint { flex: 1 1 12ch; min-width: 0; margin: 0; overflow-wrap: anywhere; }
.device-current .btn { flex: 0 0 auto; }
.admin-box { width: 100%; max-width: 560px; max-height: 80vh; overflow-y: auto; text-align: left; }
.admin-users { display: flex; flex-direction: column; gap: 8px; margin-top: 8px; }
.admin-row { display: flex; align-items: center; justify-content: space-between; gap: 10px; border-bottom: 1px solid var(--line); padding-bottom: 8px; }
.admin-email { font-size: 14px; }
.admin-sub { font-size: 12px; color: var(--muted); }
