/* Home Energy Automation Manager - UI styles (light default, dark variant).
   Theme is switched by [data-theme] on <html>; colours come from CSS vars. */

:root, [data-theme="light"] {
  --bg: #f4f6fa;
  --panel: #ffffff;
  --card: #f0f3f8;
  --text: #1a2230;
  --muted: #5e6b7e;
  --border: #e1e6ee;
  --accent: #2563eb;
  --good: #059669;
  --warn: #d97706;
  --danger: #dc2626;
  --unavail: #9aa6b5;
  --shadow: 0 1px 3px rgba(20,30,50,.08);
  --plot-bg: #ffffff;       /* chart background */
  --plot-grid: #e3e8ef;     /* chart grid lines / axis ticks: light, so lines stand out */
}
[data-theme="dark"] {
  --bg: #0f1620;
  --panel: #1a2330;
  --card: #222d3d;
  --text: #e6edf3;
  --muted: #8b98a9;
  --border: #2a3647;
  --accent: #3b82f6;
  --good: #34d399;
  --warn: #fbbf24;
  --danger: #f87171;
  --unavail: #5a6675;
  --shadow: 0 1px 3px rgba(0,0,0,.4);
  --plot-bg: #0a1017;       /* darker than the page so coloured lines pop */
  --plot-grid: #202c3b;
}

* { box-sizing: border-box; }
body {
  margin: 0; background: var(--bg); color: var(--text);
  font-family: "Segoe UI", Roboto, system-ui, sans-serif; font-size: 14px;
}
.muted { color: var(--muted); }
.small { font-size: 12px; }
.hidden { display: none !important; }
.error { color: #dc2626; }

/* top bar */
.topbar {
  display: flex; align-items: center; gap: 20px;
  background: var(--panel); border-bottom: 1px solid var(--border);
  padding: 10px 20px; box-shadow: var(--shadow);
}
.brand { font-weight: 600; font-size: 16px; display: flex; align-items: center; gap: 8px; }
.brand.big { font-size: 20px; }
.brand-dot { width: 12px; height: 12px; border-radius: 50%;
  background: linear-gradient(#2b6cb0,#2c5282); display: inline-block; }
.mainnav { display: flex; gap: 6px; }
.mainnav a {
  color: var(--muted); text-decoration: none; padding: 6px 12px; border-radius: 8px;
}
.mainnav a.active, .mainnav a:hover { color: var(--text); background: var(--card); }
.topright { margin-left: auto; display: flex; align-items: center; gap: 12px; }
.live-dot { width: 9px; height: 9px; border-radius: 50%; background: var(--good); }
.live-dot.stale { background: var(--warn); }
.icon-btn {
  background: var(--card); border: 1px solid var(--border); color: var(--text);
  border-radius: 8px; cursor: pointer; padding: 4px 9px; font-size: 14px;
}
/* Every page uses the same, wide content area (dashboard and graphs alike). */
.content { padding: 20px; max-width: min(1900px, 100%); margin: 0 auto; }

.page-head { display: flex; align-items: baseline; gap: 14px; margin: 4px 0 16px; }
.page-head h1 { font-size: 20px; margin: 0; }

/* dashboard groups + cards */
.groups { display: flex; flex-direction: column; gap: 20px; }
.group-title { color: var(--accent); font-weight: 600; font-size: 12px;
  letter-spacing: .04em; text-transform: uppercase; margin: 2px 0 8px; }
.cards { display: grid; grid-template-columns: repeat(auto-fill, minmax(180px,1fr)); gap: 12px; }
.card {
  position: relative;
  background: var(--panel); border: 1px solid var(--border); border-radius: 12px;
  padding: 12px 14px; box-shadow: var(--shadow); cursor: grab;
}
.card:active { cursor: grabbing; }
.card.dragging { opacity: .45; }
.card .k { color: var(--muted); font-size: 12px; padding-right: 16px; }
.card .k .ku { opacity: .75; font-weight: 400; }
.card .v { font-size: 26px; font-weight: 700; margin-top: 4px; }
.card .u { color: var(--muted); font-size: 13px; font-weight: 400; }
.card .v.unavail { color: var(--unavail); font-size: 18px; font-weight: 600; }
.card .v.on { color: var(--good); }
.card .ts { color: var(--muted); font-size: 11px; margin-top: 4px; }

/* graph controls */
.graph-controls { display: flex; flex-wrap: wrap; gap: 12px; align-items: center; margin-bottom: 14px; }
.chip, .chips span {
  background: var(--card); border: 1px solid var(--border); color: var(--text);
  border-radius: 16px; padding: 5px 12px; font-size: 13px; cursor: pointer;
}
.chips span { display: inline-flex; align-items: center; gap: 6px; }
.chips span .x { color: var(--muted); cursor: pointer; }
.metric-picker { position: relative; display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.metric-menu {
  position: absolute; top: 110%; left: 0; z-index: 10; max-height: 320px; overflow: auto;
  background: var(--panel); border: 1px solid var(--border); border-radius: 10px;
  box-shadow: var(--shadow); padding: 6px; min-width: 260px;
}
.metric-menu .opt {
  padding: 6px 10px; border-radius: 6px; cursor: pointer;
  display: grid; grid-template-columns: 18px 1fr auto; align-items: center; gap: 6px;
}
.metric-menu .opt:hover { background: var(--card); }
.metric-menu .opt.sel { background: color-mix(in srgb, var(--accent) 14%, transparent); }
.metric-menu .opt.dim .nm { color: var(--unavail); }
.metric-menu .opt .chk { color: var(--accent); font-weight: 700; text-align: center; }
.metric-menu .opt .cv { white-space: nowrap; }
.metric-menu .grp { color: var(--accent); font-size: 11px; text-transform: uppercase;
  padding: 8px 10px 2px; }
.metric-menu .menu-hint { padding: 4px 10px 8px; border-bottom: 1px solid var(--border); margin-bottom: 4px; }
.metric-menu .menu-tools { display: flex; align-items: center; gap: 8px; padding: 2px 10px 8px;
  border-bottom: 1px solid var(--border); margin-bottom: 4px; }

.dash-toolbar { display: flex; align-items: center; gap: 12px; margin: 0 0 14px; }
.range-seg { display: inline-flex; background: var(--card); border: 1px solid var(--border);
  border-radius: 10px; overflow: hidden; }
.range-seg button {
  background: transparent; border: none; color: var(--muted); padding: 6px 12px;
  cursor: pointer; font-size: 13px;
}
.range-seg button.on { background: var(--accent); color: #fff; }
.bucket-sel { color: var(--muted); font-size: 13px; display: inline-flex; gap: 6px; align-items: center; }
.bucket-sel select { background: var(--card); color: var(--text); border: 1px solid var(--border);
  border-radius: 8px; padding: 5px 8px; }

/* The container grows with its content: uPlot draws the legend BELOW the plot,
   outside the plot height, so a fixed container height let a wrapped legend
   spill over the statistics underneath. */
.chart { width: 100%; min-height: 300px; height: auto; background: var(--plot-bg);
  border: 1px solid var(--border); border-radius: 12px; padding: 8px;
  box-shadow: var(--shadow); margin-bottom: 18px; overflow: hidden; }
.chart .u-legend { max-width: 100%; text-align: left; }
.chart .u-legend .u-series { padding: 1px 6px; }

.stats-head h2 { font-size: 15px; margin: 0 0 8px; }
/* Statistics: one compact line per metric rather than a card each. */
.stats { display: flex; flex-direction: column; gap: 6px; }
.stat {
  display: flex; flex-wrap: wrap; align-items: baseline; gap: 2px 14px;
  background: var(--panel); border: 1px solid var(--border); border-radius: 8px;
  padding: 6px 12px;
}
.stat .swatch { width: 10px; height: 10px; border-radius: 2px; display: inline-block;
  align-self: center; flex: none; }
.stat .sname { font-weight: 600; flex: 1 1 220px; min-width: 0; overflow-wrap: anywhere; }
.stat .svals { color: var(--muted); font-size: 12px; flex: 0 1 auto; }
.stat .svals b { color: var(--text); font-weight: 600; }

/* login */
.login-wrap { min-height: 90vh; display: flex; align-items: center; justify-content: center; }
.login-card { background: var(--panel); border: 1px solid var(--border); border-radius: 16px;
  padding: 28px 32px; width: 340px; box-shadow: var(--shadow); }
.login-card form { display: flex; flex-direction: column; gap: 12px; margin-top: 16px; }
.login-card label { display: flex; flex-direction: column; gap: 4px; font-size: 13px; color: var(--muted); }
.login-card input {
  background: var(--bg); border: 1px solid var(--border); color: var(--text);
  border-radius: 8px; padding: 9px 11px; font-size: 14px;
}
button.primary { background: var(--accent); color: #fff; border: none; border-radius: 8px;
  padding: 10px; font-size: 14px; cursor: pointer; font-weight: 600; }

/* settings page */
.settings-form { display: flex; flex-direction: column; gap: 18px; max-width: 760px; }
.card-panel { background: var(--panel); border: 1px solid var(--border); border-radius: 14px;
  padding: 16px 18px; box-shadow: var(--shadow); }
.card-panel legend { font-weight: 600; color: var(--accent); padding: 0 6px; font-size: 13px;
  text-transform: uppercase; letter-spacing: .04em; }
.card-panel h3 { font-size: 13px; margin: 2px 0 10px; color: var(--muted);
  text-transform: uppercase; letter-spacing: .03em; }
.field { display: flex; flex-direction: column; gap: 4px; font-size: 13px; color: var(--muted);
  margin-top: 10px; }
.field.field-inline { flex-direction: row; align-items: center; gap: 10px; }
.field input { background: var(--bg); border: 1px solid var(--border); color: var(--text);
  border-radius: 8px; padding: 8px 10px; font-size: 14px; max-width: 220px; }
.field input:disabled { opacity: .6; cursor: not-allowed; }
.tariff-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(220px,1fr)); gap: 18px;
  margin-top: 12px; }
.tariff-season { border: 1px solid var(--border); border-radius: 10px; padding: 10px 14px 14px; }
.form-actions { display: flex; align-items: center; gap: 12px; }

/* advisory banner strip (Phase 5a) */
.advisories { display: flex; flex-direction: column; gap: 10px; margin: 0 0 16px; }
.advisories:empty { display: none; }
.advisory { display: flex; align-items: flex-start; gap: 10px; padding: 12px 14px;
  border: 1px solid var(--border); border-left-width: 4px; border-radius: 10px;
  background: var(--panel); box-shadow: var(--shadow); }
.advisory .adv-icon { font-size: 18px; line-height: 1.3; }
.advisory .adv-title { font-weight: 600; margin-bottom: 2px; }
.advisory .adv-msg { color: var(--muted); font-size: 13px; }
.advisory.warning { border-left-color: var(--warn);
  background: color-mix(in srgb, var(--warn) 10%, var(--panel)); }
.advisory.warning .adv-title { color: var(--warn); }
.advisory.critical { border-left-color: var(--danger);
  background: color-mix(in srgb, var(--danger) 12%, var(--panel)); }
.advisory.critical .adv-title { color: var(--danger); }
.advisory.info { border-left-color: var(--accent); }

/* metric labels editor */
.labels-toolbar { display: flex; align-items: center; gap: 14px; margin: 0 0 12px; flex-wrap: wrap; }
.label-filter { background: var(--bg); border: 1px solid var(--border); color: var(--text);
  border-radius: 8px; padding: 7px 10px; font-size: 13px; min-width: 240px; }
.chk-inline { color: var(--muted); font-size: 13px; display: inline-flex; gap: 6px; align-items: center; }
.labels-table-wrap { overflow-x: auto; border: 1px solid var(--border); border-radius: 12px;
  background: var(--panel); box-shadow: var(--shadow); }
.labels-table { width: 100%; border-collapse: collapse; font-size: 13px; }
.labels-table th, .labels-table td { text-align: left; padding: 8px 12px;
  border-bottom: 1px solid var(--border); vertical-align: middle; }
.labels-table th { color: var(--muted); font-weight: 600; font-size: 12px; text-transform: uppercase;
  letter-spacing: .03em; position: sticky; top: 0; background: var(--panel); }
.labels-table tr:last-child td { border-bottom: none; }
.labels-table tr.dim .raw code { color: var(--unavail); }
.labels-table code { font-size: 12px; color: var(--muted); }
.labels-table input { background: var(--bg); border: 1px solid var(--border); color: var(--text);
  border-radius: 6px; padding: 6px 8px; font-size: 13px; width: 100%; max-width: 220px; }
.labels-table input:disabled { opacity: .6; cursor: not-allowed; }
.labels-table td.latest { white-space: nowrap; font-weight: 600; }
.save-btn { background: var(--accent); color: #fff; border: none; border-radius: 6px;
  padding: 6px 12px; font-size: 13px; cursor: pointer; }

/* dashboard: grouping, collapse, card controls (curated/reorderable) */
.dgroup { margin-bottom: 18px; }
.group-title.collapser, .sub-title.collapser { cursor: pointer; user-select: none; display: flex;
  align-items: center; gap: 6px; }
.group-title .caret, .sub-title .caret { display: inline-block; width: 1em; color: var(--muted); }
.dsub { margin: 4px 0 12px; }
.sub-title { color: var(--text); font-weight: 600; font-size: 12px; text-transform: none;
  letter-spacing: 0; margin: 6px 0; opacity: .85; }
.add-btn { margin-left: 8px; background: transparent; border: 1px dashed var(--border);
  color: var(--muted); border-radius: 12px; padding: 1px 8px; font-size: 11px; cursor: pointer; }
.add-btn:hover { color: var(--accent); border-color: var(--accent); }
.card-x { position: absolute; top: 6px; right: 6px; width: 18px; height: 18px; line-height: 16px;
  text-align: center; border: none; background: transparent; color: var(--unavail);
  border-radius: 4px; cursor: pointer; font-size: 12px; }
.card-x:hover { color: var(--danger); background: color-mix(in srgb, var(--danger) 12%, transparent); }
.add-menu .opt { grid-template-columns: 1fr; }

/* dashboard card -> graph drill-in link */
.card-link { display: block; color: inherit; text-decoration: none; }
.card-link:hover .k { color: var(--accent); }

/* graphs: saved-views bar */
.views-bar { display: flex; align-items: center; gap: 10px; margin: 0 0 12px; flex-wrap: wrap; }
.views-sel { color: var(--muted); font-size: 13px; display: inline-flex; gap: 6px; align-items: center; }
.views-sel select { background: var(--card); color: var(--text); border: 1px solid var(--border);
  border-radius: 8px; padding: 5px 8px; max-width: 220px; }

/* weather-for-today strip (top of dashboard) */
.weather-today { margin: 0 0 16px; background: var(--panel); border: 1px solid var(--border);
  border-radius: 14px; padding: 12px 16px; box-shadow: var(--shadow); }
.weather-today:empty, .weather-today.hidden { display: none; }
.weather-today .wt-head { color: var(--accent); font-weight: 600; font-size: 12px;
  text-transform: uppercase; letter-spacing: .04em; margin-bottom: 10px; }
.weather-today .wt-tiles { display: grid; grid-template-columns: repeat(auto-fit, minmax(120px, 1fr));
  gap: 10px; }
.weather-today .wt-tile { background: var(--card); border: 1px solid var(--border);
  border-radius: 10px; padding: 10px 12px; }
.weather-today .wt-val { font-size: 22px; font-weight: 700; }
.weather-today .wt-val.wt-cond { font-size: 18px; text-transform: capitalize; }
.weather-today .wt-u { color: var(--muted); font-size: 13px; font-weight: 400; }
.weather-today .wt-lbl { color: var(--muted); font-size: 12px; margin-top: 2px; }

/* weather strip: wind tile, hour-by-hour rows (today / tomorrow) */
.weather-today .wt-arrow { display: inline-block; font-size: .9em; line-height: 1; }
.weather-today .wt-day { margin-top: 12px; }
.weather-today .wt-dayhead { font-weight: 600; font-size: 13px; margin-bottom: 6px; }
.weather-today .wt-hours { display: flex; gap: 6px; overflow-x: auto; position: relative;
  padding-bottom: 6px; -webkit-overflow-scrolling: touch; }
.weather-today .wt-hour { flex: 0 0 56px; text-align: center; background: var(--card);
  border: 1px solid var(--border); border-radius: 10px; padding: 6px 2px; font-size: 12px; }
.weather-today .wt-hour.past { opacity: .5; }
.weather-today .wt-hour.now { border-color: var(--accent);
  box-shadow: 0 0 0 1px var(--accent); }
.weather-today .wt-hh { color: var(--muted); font-size: 11px; }
.weather-today .wt-hi { font-size: 20px; line-height: 1.5; }
.weather-today .wt-ht { font-weight: 700; font-size: 14px; }
.weather-today .wt-hw { margin-top: 3px; }
.weather-today .wt-hd { color: var(--muted); font-size: 11px; }
.weather-today .wt-hr { color: var(--accent); font-size: 11px; margin-top: 2px; }
.weather-today .wt-foot { margin-top: 8px; }

/* ---- small screens ----------------------------------------------------- */
@media (max-width: 760px) {
  body { font-size: 13px; }
  .topbar { flex-wrap: wrap; gap: 8px 12px; padding: 8px 12px; }
  .mainnav { order: 3; width: 100%; overflow-x: auto; }
  .topright { gap: 8px; }
  .content { padding: 12px; }
  .page-head { flex-wrap: wrap; gap: 4px 10px; margin-bottom: 12px; }
  .cards { grid-template-columns: repeat(auto-fill, minmax(140px, 1fr)); gap: 8px; }
  .card { padding: 10px 12px; }
  .card .v { font-size: 22px; }
  .graph-controls { gap: 8px; }
  .range-seg { max-width: 100%; overflow-x: auto; }
  .range-seg button { padding: 6px 9px; }
  .metric-menu { max-width: calc(100vw - 24px); min-width: 0; width: 320px; }
  .chart { padding: 4px; min-height: 240px; }
  .stat .sname { flex-basis: 100%; }
  .weather-today { padding: 10px 12px; }
  .weather-today .wt-tiles { grid-template-columns: repeat(auto-fit, minmax(100px, 1fr)); }
  .weather-today .wt-val { font-size: 19px; }
  .settings-form { max-width: 100%; }
  .labels-toolbar .label-filter { min-width: 0; width: 100%; }
}
@media (max-width: 420px) {
  .brand { font-size: 14px; }
  #userLabel { display: none; }
}

/* graphs: custom range row */
.range-row { display: flex; flex-wrap: wrap; align-items: center; gap: 8px 14px; margin: 0 0 12px;
  color: var(--muted); font-size: 13px; }
.range-row label { display: inline-flex; align-items: center; gap: 6px; }
.range-row input[type=datetime-local] { background: var(--card); color: var(--text);
  border: 1px solid var(--border); border-radius: 8px; padding: 5px 8px; font-size: 13px; }

/* graphs: notes and logged baths in the shown period */
.notes-section { margin: 0 0 18px; }
.notes-section h2 { font-size: 15px; margin: 0 0 8px; }
.notes-list { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 6px; }
.notes-list li { display: flex; flex-wrap: wrap; align-items: baseline; gap: 4px 12px;
  background: var(--panel); border: 1px solid var(--border); border-radius: 8px; padding: 6px 12px; }
.nbadge { display: inline-flex; align-items: center; justify-content: center; width: 20px; height: 20px;
  border-radius: 50%; color: #fff; font-size: 11px; font-weight: 700; align-self: center; flex: none; }
.nbadge.bath { background: #0ea5e9; }
.nbadge.note { background: #f59e0b; }
.notes-list .nwhen { font-variant-numeric: tabular-nums; color: var(--muted); }
.notes-list .nkind { font-weight: 600; }
.notes-list .ntext { flex: 1 1 240px; min-width: 0; overflow-wrap: anywhere; }
.notes-list .nx { background: transparent; border: none; color: var(--unavail); cursor: pointer;
  border-radius: 4px; padding: 2px 6px; margin-left: auto; }
.notes-list .nx:hover { color: var(--danger); background: color-mix(in srgb, var(--danger) 12%, transparent); }

/* add-a-bath / add-a-note dialog and its confirmation toast */
.note-dialog { border: 1px solid var(--border); border-radius: 14px; background: var(--panel);
  color: var(--text); padding: 18px 20px; width: min(420px, 92vw); box-shadow: 0 10px 40px rgba(0,0,0,.35); }
.note-dialog::backdrop { background: rgba(0,0,0,.45); }
.note-dialog h3 { margin: 0 0 10px; font-size: 16px; }
.note-kind { display: flex; gap: 18px; margin-bottom: 4px; }
.note-kind label { display: inline-flex; align-items: center; gap: 6px; cursor: pointer; }
.note-dialog .field input, .note-dialog .field textarea { max-width: none; width: 100%; }
.field textarea { background: var(--bg); border: 1px solid var(--border); color: var(--text);
  border-radius: 8px; padding: 8px 10px; font-size: 14px; font-family: inherit; resize: vertical; }
.note-dialog .error { min-height: 1.2em; margin: 8px 0 0; }
.note-actions { display: flex; justify-content: flex-end; gap: 10px; margin-top: 12px; }
.note-actions .primary { padding: 7px 18px; }
.toast { position: fixed; left: 50%; bottom: 22px; transform: translateX(-50%); z-index: 50;
  background: var(--text); color: var(--bg); border-radius: 10px; padding: 9px 16px; font-size: 13px;
  box-shadow: 0 4px 16px rgba(0,0,0,.3); }
