:root {
  color-scheme: light;
  --page: #f9f9f7;
  --surface: #fcfcfb;
  --surface-2: #f3f2ee;
  --ink: #0b0b0b;
  --ink-2: #52514e;
  --muted: #6c6b66;
  --grid: #e1e0d9;
  --axis: #c3c2b7;
  --border: rgba(11, 11, 11, 0.10);
  --accent: #2a78d6;
  --accent-ink: #ffffff;
  --series-1: #2a78d6;
  --series-2: #eb6834;
  --series-3: #1baf7a;
  --band: rgba(12, 163, 12, 0.08);
  --rain: #4a3aa7;
  --good: #0ca30c;
  --good-ink: #006300;
  --warning: #fab219;
  --warning-ink: #8a5a00;
  --critical: #d03b3b;
  --radius: 10px;
  font-family: system-ui, -apple-system, "Segoe UI", sans-serif;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    color-scheme: dark;
    --page: #0d0d0d;
    --surface: #1a1a19;
    --surface-2: #232321;
    --ink: #ffffff;
    --ink-2: #c3c2b7;
    --muted: #898781;
    --grid: #2c2c2a;
    --axis: #383835;
    --border: rgba(255, 255, 255, 0.10);
    --accent: #3987e5;
    --series-1: #3987e5;
    --series-2: #d95926;
    --series-3: #199e70;
    --band: rgba(12, 163, 12, 0.14);
    --rain: #9085e9;
    --good-ink: #0ca30c;
    --warning-ink: #fab219;
  }
}
:root[data-theme="dark"] {
  color-scheme: dark;
  --page: #0d0d0d;
  --surface: #1a1a19;
  --surface-2: #232321;
  --ink: #ffffff;
  --ink-2: #c3c2b7;
  --muted: #898781;
  --grid: #2c2c2a;
  --axis: #383835;
  --border: rgba(255, 255, 255, 0.10);
  --accent: #3987e5;
  --series-1: #3987e5;
  --series-2: #d95926;
  --series-3: #199e70;
  --band: rgba(12, 163, 12, 0.14);
  --rain: #9085e9;
  --good-ink: #0ca30c;
  --warning-ink: #fab219;
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html { -webkit-text-size-adjust: 100%; }
body { margin: 0; background: var(--page); color: var(--ink); line-height: 1.45; }
button { font: inherit; }

.btn:focus-visible, .seg button:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

/* ---------- Buttons ---------- */
.btn {
  display: inline-block; text-decoration: none;
  border: 1px solid var(--border); background: var(--surface); color: var(--ink);
  padding: 7px 14px; border-radius: 8px; cursor: pointer; font-size: 14px;
}
.btn:hover { background: var(--surface-2); }
.btn.primary { background: var(--accent); color: var(--accent-ink); border-color: transparent; padding: 10px 14px; }
.btn.primary:hover { filter: brightness(1.06); }
.btn[disabled] { opacity: .55; cursor: default; }

/* ---------- Layout ---------- */
header.top {
  display: flex; align-items: center; justify-content: space-between; gap: 12px; flex-wrap: wrap;
  max-width: 1080px; margin: 0 auto; padding: 20px 16px 8px;
}
header.top h1 { font-size: 20px; margin: 0; }
header.top .sub { color: var(--muted); font-size: 13px; }
header.top .actions { display: flex; gap: 8px; align-items: center; }
header.top .who { color: var(--muted); font-size: 13px; margin-right: 4px; }
@media (max-width: 520px) { header.top .who { display: none; } }
main { max-width: 1080px; margin: 0 auto; padding: 8px 16px 48px; }
section { margin-top: 28px; }
section > h2 { font-size: 15px; font-weight: 600; margin: 0 0 12px; color: var(--ink); }
.section-head { display: flex; align-items: center; justify-content: space-between; gap: 12px; flex-wrap: wrap; margin-bottom: 12px; }
.section-head h2 { font-size: 15px; font-weight: 600; margin: 0; }
.card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 16px; }
.grid-3 { display: grid; grid-template-columns: repeat(auto-fit, minmax(240px, 1fr)); gap: 12px; }
.grid-2 { display: grid; grid-template-columns: repeat(auto-fit, minmax(300px, 1fr)); gap: 12px; }
.note { color: var(--muted); font-size: 13px; margin: 8px 0 0; }

/* ---------- Stat tiles ---------- */
.tile .label { display: flex; align-items: center; gap: 8px; font-size: 13px; color: var(--ink-2); }
.swatch { width: 10px; height: 10px; border-radius: 3px; flex: none; }
.tile .value { font-size: 34px; font-weight: 600; margin: 6px 0 2px; letter-spacing: -0.01em; }
.tile .value small { font-size: 16px; font-weight: 500; color: var(--ink-2); margin-left: 2px; }
.tile .meta { font-size: 13px; color: var(--ink-2); }
.badge { display: inline-flex; align-items: center; gap: 5px; font-size: 12px; font-weight: 600; margin-top: 10px; white-space: nowrap; }
.badge .dot { width: 8px; height: 8px; border-radius: 50%; }
.badge.good { color: var(--good-ink); } .badge.good .dot { background: var(--good); }
.badge.warn { color: var(--warning-ink); } .badge.warn .dot { background: var(--warning); }
.totals { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 16px; }
.totals .value { font-size: 24px; font-weight: 600; }
.totals .label { font-size: 13px; color: var(--ink-2); }

/* ---------- Hubs ---------- */
.hub h3 { margin: 0; font-size: 15px; display: flex; align-items: center; justify-content: space-between; }
.status-pill { font-size: 12px; font-weight: 600; display: inline-flex; align-items: center; gap: 5px; color: var(--ink-2); }
.status-pill .dot { width: 8px; height: 8px; border-radius: 50%; background: var(--muted); }
.status-pill.on .dot { background: var(--good); }
.status-pill.off .dot { background: var(--critical); }
.zone { border-top: 1px solid var(--grid); margin-top: 12px; padding-top: 12px; }
.zone .area { display: flex; align-items: center; gap: 8px; font-weight: 600; font-size: 14px; }
.zone dl { display: grid; grid-template-columns: auto 1fr; gap: 4px 12px; margin: 8px 0 0; font-size: 13px; }
.zone dt { color: var(--muted); }
.zone dd { margin: 0; color: var(--ink); }
.running { color: var(--accent); font-weight: 600; }

/* ---------- Chart ---------- */
.seg { display: inline-flex; border: 1px solid var(--border); border-radius: 8px; overflow: hidden; }
.seg button { border: 0; background: var(--surface); color: var(--ink-2); padding: 6px 12px; font-size: 13px; cursor: pointer; }
.seg button[aria-pressed="true"] { background: var(--surface-2); color: var(--ink); font-weight: 600; }
.legend { display: flex; gap: 16px; flex-wrap: wrap; font-size: 13px; color: var(--ink-2); margin-bottom: 8px; }
.legend span { display: inline-flex; align-items: center; gap: 6px; }
.chart-wrap { position: relative; }
.chart-wrap svg { display: block; width: 100%; height: auto; overflow: visible; }
.chart-wrap .tick { fill: var(--muted); font-size: 11px; font-variant-numeric: tabular-nums; }
.chart-wrap .gridline { stroke: var(--grid); stroke-width: 1; }
.chart-wrap .baseline { stroke: var(--axis); stroke-width: 1; }
.chart-wrap .band { fill: var(--band); }
.chart-wrap .band-label { fill: var(--good-ink); font-size: 11px; }
.chart-wrap .hit { fill: transparent; cursor: default; }
.chart-wrap .bar.dim { opacity: .35; }
.tooltip {
  position: absolute; pointer-events: none; background: var(--surface); border: 1px solid var(--border);
  border-radius: 8px; padding: 8px 10px; font-size: 12px; color: var(--ink); box-shadow: 0 4px 16px rgba(0,0,0,.12);
  white-space: nowrap; opacity: 0; transition: opacity .1s;
}
.tooltip.show { opacity: 1; }
.tooltip .t-head { color: var(--ink-2); margin-bottom: 4px; }
.tooltip .t-row { display: flex; align-items: center; gap: 6px; }
.empty { color: var(--muted); font-size: 14px; padding: 24px 0; text-align: center; }

/* ---------- Table ---------- */
.table-scroll { overflow-x: auto; }
table { width: 100%; border-collapse: collapse; font-size: 13px; }
th, td { text-align: left; padding: 8px 10px; border-bottom: 1px solid var(--grid); white-space: nowrap; }
th { color: var(--muted); font-weight: 500; }
td.num, th.num { text-align: right; font-variant-numeric: tabular-nums; }

/* ---------- Series colours (classes, since the CSP blocks inline styles) ---------- */
.swatch.s1 { background: var(--series-1); } .swatch.s2 { background: var(--series-2); } .swatch.s3 { background: var(--series-3); }
.swatch.muted { background: var(--muted); }
.swatch.target { background: var(--band); outline: 1px solid var(--good); }
.chart-wrap .bar.s1 { fill: var(--series-1); } .chart-wrap .bar.s2 { fill: var(--series-2); } .chart-wrap .bar.s3 { fill: var(--series-3); }
.tile .meta.empty-tile { margin-top: 10px; }
.area-cell { display: inline-flex; align-items: center; gap: 6px; }

/* ---------- Login ---------- */
body.auth {
  min-height: 100vh; min-height: 100dvh;
  background:
    radial-gradient(1200px 600px at 50% -10%, color-mix(in srgb, var(--series-3) 14%, transparent), transparent 70%),
    var(--page);
}
.auth-wrap { min-height: 100vh; min-height: 100dvh; display: flex; flex-direction: column; align-items: center; justify-content: center; padding: 32px 16px; gap: 20px; }
.auth-brand { display: flex; align-items: center; gap: 12px; width: 100%; max-width: 380px; }
.auth-mark { width: 40px; height: 40px; flex: none; }
.auth-mark path { fill: var(--accent); }
.auth-mark .shine { fill: none; stroke: rgba(255, 255, 255, .7); stroke-width: 1.8; stroke-linecap: round; }
.auth-name { font-size: 19px; font-weight: 650; letter-spacing: -0.01em; }
.auth-tag { font-size: 13px; color: var(--muted); }
.auth-card {
  width: 100%; max-width: 380px; background: var(--surface); border: 1px solid var(--border);
  border-radius: 16px; padding: 28px 24px 24px; box-shadow: 0 1px 2px rgba(0,0,0,.04), 0 12px 32px rgba(0,0,0,.06);
}
.auth-card h1 { font-size: 22px; font-weight: 650; margin: 0 0 18px; letter-spacing: -0.01em; }
.auth-card label { display: block; font-size: 13px; font-weight: 500; color: var(--ink-2); margin: 14px 0 6px; }
.auth-card h1 + label { margin-top: 0; }
.auth-card input {
  width: 100%; font: inherit; font-size: 16px; padding: 11px 12px; border-radius: 10px;
  border: 1px solid var(--axis); background: var(--page); color: var(--ink); transition: border-color .15s, box-shadow .15s;
}
.auth-card input:focus { outline: none; border-color: var(--accent); box-shadow: 0 0 0 3px color-mix(in srgb, var(--accent) 22%, transparent); }
.auth-card .code-input { font-size: 26px; letter-spacing: 10px; text-align: center; font-variant-numeric: tabular-nums; padding: 10px 12px; }
.auth-card .btn.primary { width: 100%; margin-top: 20px; padding: 11px 14px; font-weight: 600; border-radius: 10px; }
.auth-help { margin: -8px 0 16px; font-size: 14px; color: var(--ink-2); }
.auth-help b { color: var(--ink); font-weight: 600; }
.auth-error { color: var(--critical); font-size: 14px; min-height: 20px; margin: 12px 0 0; }
.auth-error:empty { min-height: 0; margin: 0; }
.auth-links { display: flex; justify-content: space-between; gap: 12px; flex-wrap: wrap; margin-top: 16px; }
.link { background: none; border: 0; padding: 0; color: var(--accent); font-size: 14px; cursor: pointer; }
.link:hover { text-decoration: underline; }
.link[disabled] { opacity: .5; cursor: default; }
.link:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; border-radius: 4px; }
.auth-foot { font-size: 12px; color: var(--muted); margin: 0; }

/* ---------- Status: next watering + alerts ---------- */
.status { display: grid; grid-template-columns: minmax(0, 1fr) minmax(0, 1fr); gap: 12px 32px; align-items: start; }
.status-label { font-size: 13px; color: var(--ink-2); }
.status-next { font-size: 24px; font-weight: 650; letter-spacing: -0.01em; margin: 2px 0 8px; }
.status-order { list-style: none; margin: 0; padding: 0; display: grid; gap: 4px; font-size: 13px; }
.status-order li { display: flex; align-items: center; gap: 8px; }
.status-order .t { margin-left: auto; color: var(--ink-2); font-variant-numeric: tabular-nums; white-space: nowrap; }
.alerts { list-style: none; margin: 0; padding: 0; display: grid; gap: 8px; }
.alert { display: flex; gap: 8px; align-items: flex-start; font-size: 13px; line-height: 1.4; padding: 8px 10px; border-radius: 8px; background: var(--surface-2); }
.alert .dot, .all-good .dot { width: 8px; height: 8px; border-radius: 50%; flex: none; margin-top: 5px; }
.alert.warn .dot { background: var(--warning); }
.alert.critical .dot { background: var(--critical); }
.alert.info .dot { background: var(--muted); }
.all-good { display: flex; align-items: center; gap: 8px; font-size: 13px; color: var(--good-ink); font-weight: 600; align-self: center; }
.all-good .dot { background: var(--good); margin-top: 0; }

/* ---------- Missed runs ---------- */
.swatch.missed { background: var(--warning); }
.chart-wrap .missed-mark { fill: var(--warning); }

/* ---------- History ---------- */
.run-area { display: flex; align-items: center; gap: 6px; }
.run-time { color: var(--ink-2); font-size: 12px; margin: 2px 0 0 16px; }

/* ---------- Phones ---------- */
@media (max-width: 560px) {
  .status { grid-template-columns: 1fr; }
  .status-next { font-size: 22px; }

  /* Last session: one compact row per area instead of three tall cards */
  .grid-3 { gap: 8px; }
  .tile { display: grid; grid-template-columns: minmax(0, 1fr) auto; grid-template-areas: "label value" "meta badge"; align-items: center; gap: 2px 12px; padding: 12px 14px; }
  .tile .label { grid-area: label; }
  .tile .value { grid-area: value; font-size: 24px; margin: 0; text-align: right; }
  .tile .value small { font-size: 13px; }
  .tile .meta { grid-area: meta; font-size: 12px; }
  .tile .badge { grid-area: badge; margin: 0; justify-self: end; }
  .tile .meta.empty-tile { margin: 0; grid-column: 1 / -1; }

  .totals { gap: 8px; }
  .totals .value { font-size: 19px; }
  .totals .label { font-size: 12px; }

  /* History: drop minutes so area, litres and mm fit without sideways scrolling */
  .col-min { display: none; }
  th, td { padding: 8px 6px; }
  .run-area { white-space: normal; }
}

/* ---------- Rain delay controls ---------- */
.seg-sm button { padding: 4px 10px; font-size: 12px; }
.seg button[disabled] { cursor: default; opacity: .6; }
.delay-seg.saving { opacity: .6; }
.delay-all { display: flex; align-items: center; justify-content: space-between; gap: 12px; flex-wrap: wrap; margin-top: 14px; padding-top: 12px; border-top: 1px solid var(--grid); }
.zone dd .delay-seg { vertical-align: middle; }
.delay-end { font-size: 12px; color: var(--ink-2); margin-top: 4px; }
.swatch.delayed { background: var(--muted); }
.chart-wrap .delayed-mark { fill: var(--muted); }

/* ---------- Tuya access notice ---------- */
.notice { background: var(--surface); border: 1px solid var(--border); border-left: 4px solid var(--good); border-radius: var(--radius); padding: 12px 16px; }
.notice.warn { border-left-color: var(--warning); }
.notice.critical { border-left-color: var(--critical); }
.notice-row { display: flex; align-items: center; gap: 12px; }
.notice-row .dot { width: 8px; height: 8px; border-radius: 50%; flex: none; background: var(--good); align-self: flex-start; margin-top: 7px; }
.notice.warn .notice-row .dot { background: var(--warning); }
.notice.critical .notice-row .dot { background: var(--critical); }
.notice-text { flex: 1; font-size: 14px; min-width: 0; }
.notice-sub { font-size: 12px; color: var(--ink-2); margin: 4px 0 0; }
.notice-sub a { color: var(--accent); }
.expiry-form { display: flex; flex-wrap: wrap; align-items: center; gap: 8px 10px; }
.expiry-form label { font-size: 14px; font-weight: 500; }
.expiry-form input { font: inherit; font-size: 15px; padding: 6px 10px; border-radius: 8px; border: 1px solid var(--axis); background: var(--page); color: var(--ink); }
.expiry-form .btn.primary { padding: 7px 14px; }
.expiry-form .notice-sub, .expiry-form .auth-error { flex-basis: 100%; }
section:has(> .notice[hidden]) { display: none; }
@media (max-width: 560px) {
  .notice-row { flex-wrap: wrap; }
  .notice-text { flex: 1 1 calc(100% - 20px); }
  .notice-row .btn { margin-left: 20px; }
}

/* ---------- Rain (BoM) ---------- */
.rain-stats { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); gap: 16px; margin-bottom: 18px; }
.rain-stats .value { font-size: 24px; font-weight: 600; }
.rain-stats .value small { font-size: 13px; font-weight: 500; color: var(--ink-2); margin-left: 2px; }
.rain-stats .label { font-size: 13px; color: var(--ink-2); }
.rain-stats .label + .label { font-size: 12px; color: var(--muted); }
.forecast-head { display: flex; justify-content: space-between; align-items: center; flex-wrap: wrap; gap: 4px 12px; margin-bottom: 8px; }
.forecast-key { white-space: nowrap; display: inline-flex; align-items: center; gap: 6px; font-size: 12px; color: var(--ink-2); }
.water-dot { width: 7px; height: 7px; border-radius: 50%; background: var(--series-1); display: inline-block; flex: none; }
.forecast { display: grid; grid-template-columns: repeat(7, minmax(0, 1fr)); gap: 6px; }
.fc { background: var(--surface-2); border-radius: 8px; padding: 8px 8px 10px; text-align: center; min-width: 0; }
.fc.heavy { outline: 2px solid var(--rain); outline-offset: -2px; }
.fc-day { font-size: 13px; font-weight: 600; display: flex; align-items: center; justify-content: center; gap: 4px; }
.fc-date { font-size: 11px; color: var(--muted); margin-bottom: 6px; }
.fc-meter { display: block; width: 100%; height: 6px; }
.fc-meter .track { fill: var(--grid); }
.fc-meter .fill { fill: var(--rain); }
.fc-chance { font-size: 16px; font-weight: 600; margin-top: 6px; font-variant-numeric: tabular-nums; }
.fc-mm { font-size: 12px; color: var(--ink-2); white-space: nowrap; font-variant-numeric: tabular-nums; }
.rain .note a, .fva a { color: var(--accent); }
.warn-text { color: var(--warning-ink); }
.muted { color: var(--muted); }
.fva { margin-top: 12px; }
.fva summary { cursor: pointer; font-size: 14px; font-weight: 600; }
.fva[open] summary { margin-bottom: 8px; }
.swatch.rain { background: var(--rain); }
.chart-wrap .bar.rain { fill: var(--rain); }
.chart-wrap .panel-label { fill: var(--ink-2); font-size: 12px; font-weight: 600; }
.alert.rain .dot { background: var(--rain); }
.alert-actions { display: flex; align-items: center; gap: 12px; margin-top: 8px; flex-wrap: wrap; }
.alert.rain > span:last-child { display: block; }
.btn.btn-sm { padding: 5px 10px; font-size: 13px; }
@media (max-width: 560px) {
  .rain-stats { grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 12px; }
  .rain-stats .value { font-size: 19px; }
  .forecast { gap: 3px; }
  .fc { padding: 6px 2px 8px; }
  .fc-day { font-size: 12px; }
  .fc-chance { font-size: 14px; }
  .fc-mm { font-size: 11px; }
  .fc-unit { display: none; } /* the key says the figures are mm */
}
.fc-unit-note { display: none; }
@media (max-width: 560px) { .fc-unit-note { display: inline; } }

/* ---------- Chart values and daily table ---------- */
.chart-controls { display: flex; gap: 8px; flex-wrap: wrap; }
.chart-wrap .bar-label { fill: var(--ink-2); font-size: 11px; font-variant-numeric: tabular-nums; pointer-events: none; }
.daily th { vertical-align: bottom; }
.th-sub { display: block; font-size: 11px; color: var(--muted); font-weight: 400; }
.daily td.num { font-variant-numeric: tabular-nums; }
.tag { font-size: 11px; color: var(--ink-2); background: var(--surface-2); border-radius: 4px; padding: 1px 5px; }
.tag.missed { color: var(--warning-ink); }
@media (max-width: 560px) {
  .col-fc { display: none; }
}
.chart-wrap .bar-label.tight { font-size: 9.5px; letter-spacing: -0.02em; }

/* ---------- Tuya access problem (status card) ---------- */
.status:has(.problem) { display: block; }
.problem p, .problem ol { font-size: 14px; color: var(--ink-2); margin: 8px 0 0; }
.problem ol { padding-left: 20px; display: grid; gap: 4px; }
.problem b { color: var(--ink); }
.problem a { color: var(--accent); }
.problem .problem-detail { font-size: 12px; color: var(--muted); }

/* ---------- Arrange: reorder sections ---------- */
.arrange-help { display: flex; justify-content: space-between; align-items: center; gap: 8px 16px; flex-wrap: wrap; margin-top: 28px; padding: 12px 16px; border-radius: var(--radius); background: var(--surface-2); font-size: 14px; color: var(--ink-2); }
.arrange-help b { color: var(--ink); }
body.arranging #sections > section { margin-top: 8px; }
body.arranging #sections > section > :not(.arrange-bar) { display: none !important; }
body.arranging #sections > section[hidden] { display: block !important; } /* show Rain even before its data loads */
.arrange-bar { display: flex; align-items: center; gap: 8px; background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 10px 12px 10px 16px; }
.arrange-name { flex: 1; font-size: 15px; font-weight: 600; min-width: 0; }
.arrange-bar .btn { min-width: 44px; min-height: 36px; }
#arrange[aria-pressed="true"] { background: var(--accent); color: var(--accent-ink); border-color: transparent; }
