/* Material 3, hand-rolled.
 *
 * Deliberately not @material/web: those components need a bundler, and
 * vercel.json sets buildCommand: null. Tokens and plain CSS get the same look
 * with no build step and nothing to keep up to date.
 *
 * Density is M3's compact end throughout. The canonical 48px data-table row and
 * 56px list item are built for a phone in one hand; this is a dashboard read on
 * a desktop, and at full spec roughly a quarter less fits on screen. Shape,
 * colour and type are M3 — the spacing is tightened on purpose.
 */

:root {
  color-scheme: light dark;

  /* ── Colour roles ──────────────────────────────────────────
   * Baseline M3 blue. Surfaces are the tonal container ramp rather than
   * white-on-grey-with-a-border: elevation is expressed as tone first, shadow
   * second, which is the main thing that makes M3 read as M3.
   */
  --md-primary: #0b57d0;
  --md-on-primary: #ffffff;
  --md-primary-container: #d3e3fd;
  --md-on-primary-container: #041e49;
  --md-secondary-container: #c2e7ff;
  --md-on-secondary-container: #001d35;

  --md-surface: #f8fafd;
  --md-surface-container-lowest: #ffffff;
  --md-surface-container-low: #f8fafd;
  --md-surface-container: #f0f4f9;
  --md-surface-container-high: #e9eef6;
  --md-on-surface: #1f1f1f;
  --md-on-surface-variant: #444746;

  --md-outline: #747775;
  --md-outline-variant: #c4c7c5;

  --md-error: #b3261e;
  --md-success: #146c2e;
  --md-warning: #a8500a;

  /* Shape scale */
  --md-r-xs: 4px;
  --md-r-sm: 8px;
  --md-r-md: 12px;
  --md-r-lg: 16px;
  --md-r-xl: 28px;
  --md-r-full: 999px;

  /* Elevation 1 and 2 */
  --md-e1: 0 1px 2px rgba(0, 0, 0, .3), 0 1px 3px 1px rgba(0, 0, 0, .15);
  --md-e2: 0 1px 2px rgba(0, 0, 0, .3), 0 2px 6px 2px rgba(0, 0, 0, .15);

  /* Standard easing and duration */
  --md-ease: cubic-bezier(.2, 0, 0, 1);
  --md-dur: 180ms;

  /* ── Names the markup already uses, mapped onto the roles above ──
   * Every selector in this file and every class in app.js predates the
   * restyle; keeping the old variable names means the reskin touched no JS.
   */
  --bg: var(--md-surface-container);
  --surface: var(--md-surface-container-lowest);
  --surface-2: var(--md-surface-container-high);
  --ink: var(--md-on-surface);
  --ink-2: var(--md-on-surface-variant);
  --ink-3: var(--md-outline);
  --line: var(--md-outline-variant);
  --accent: var(--md-primary);
  --accent-soft: var(--md-secondary-container);
  --good: var(--md-success);
  --warn: var(--md-warning);
  --bad: var(--md-error);
  --radius: var(--md-r-md);
  --sidebar: 232px;

  /* State-layer opacities, straight from the spec. */
  --state-hover: .08;
  --state-press: .12;
}

@media (prefers-color-scheme: dark) {
  :root {
    --md-primary: #a8c7fa;
    --md-on-primary: #062e6f;
    --md-primary-container: #0842a0;
    --md-on-primary-container: #d3e3fd;
    --md-secondary-container: #004a77;
    --md-on-secondary-container: #c2e7ff;

    --md-surface: #131314;
    --md-surface-container-lowest: #0e0e0e;
    --md-surface-container-low: #1b1b1b;
    --md-surface-container: #1e1f20;
    --md-surface-container-high: #282a2c;
    --md-on-surface: #e3e3e3;
    --md-on-surface-variant: #c4c7c5;

    --md-outline: #8e918f;
    --md-outline-variant: #444746;

    --md-error: #f2b8b5;
    --md-success: #6dd58c;
    --md-warning: #fdd663;

    --md-e1: 0 1px 2px rgba(0, 0, 0, .6), 0 1px 3px 1px rgba(0, 0, 0, .3);
    --md-e2: 0 1px 2px rgba(0, 0, 0, .6), 0 2px 6px 2px rgba(0, 0, 0, .3);

    /* Dark surfaces read as elevated by getting lighter, not by casting a
     * bigger shadow — so the card sits above the page rather than below it. */
    --bg: var(--md-surface);
    --surface: var(--md-surface-container-low);
    --surface-2: var(--md-surface-container-high);
  }
}

* { box-sizing: border-box; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--ink);
  font: 400 14px/1.5 "Roboto Flex", Roboto, "Google Sans Text", system-ui, -apple-system, sans-serif;
  -webkit-font-smoothing: antialiased;
}

.hidden { display: none !important; }
.muted { color: var(--ink-3); }
.small { font-size: 12.5px; }

/* Type scale: headline-small, title-medium, title-small. */
h1 { font-size: 24px; font-weight: 400; margin: 0; letter-spacing: 0; }
h2 { font-size: 16px; font-weight: 500; margin: 0 0 12px; letter-spacing: .15px; }
h3 { font-size: 14px; font-weight: 500; margin: 16px 0 6px; letter-spacing: .1px; }

/* ── Shell: navigation drawer + content ───────────────────── */
.shell { display: grid; grid-template-columns: var(--sidebar) 1fr; min-height: 100vh; }

.sidebar {
  background: var(--bg);
  padding: 18px 12px;
  display: flex; flex-direction: column; gap: 20px;
  position: sticky; top: 0; height: 100vh;
}
.brand {
  font-size: 22px; font-weight: 400; letter-spacing: 0;
  padding: 4px 12px 0; color: var(--ink);
}

/* Drawer destinations: full-pill, active one filled with the secondary
 * container. The pill indicator is the single most recognisable M3 signature
 * available without changing the markup. */
.nav { display: flex; flex-direction: column; gap: 4px; }
.nav button {
  text-align: left; font: inherit; font-size: 14px; font-weight: 500;
  letter-spacing: .1px; color: var(--md-on-surface-variant);
  background: none; border: 0; padding: 0 24px; height: 48px;
  border-radius: var(--md-r-full); cursor: pointer;
  transition: background var(--md-dur) var(--md-ease), color var(--md-dur) var(--md-ease);
}
.nav button:hover {
  background: color-mix(in srgb, var(--md-on-surface) calc(var(--state-hover) * 100%), transparent);
  color: var(--ink);
}
.nav button:active {
  background: color-mix(in srgb, var(--md-on-surface) calc(var(--state-press) * 100%), transparent);
}
.nav button.active {
  background: var(--md-secondary-container);
  color: var(--md-on-secondary-container);
}
.sidebar-foot { margin-top: auto; display: flex; flex-direction: column; gap: 10px; }

#main { padding: 24px 28px 60px; max-width: 1440px; }
.page-head {
  display: flex; align-items: center; justify-content: space-between;
  gap: 16px; margin-bottom: 20px; flex-wrap: wrap;
}
.row { display: flex; align-items: center; gap: 8px; }
.row.wrap { flex-wrap: wrap; }

/* ── Grid ─────────────────────────────────────────────────── */
.grid { display: grid; gap: 16px; margin-bottom: 16px; }
.cols-2 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
.cols-3 { grid-template-columns: repeat(3, minmax(0, 1fr)); }
@media (max-width: 1100px) { .cols-3 { grid-template-columns: repeat(2, minmax(0,1fr)); } }
@media (max-width: 860px) {
  .shell { grid-template-columns: 1fr; }
  .sidebar {
    position: static; height: auto; flex-direction: row; align-items: center;
    gap: 8px; padding: 8px 12px;
  }
  .nav { flex-direction: row; flex-wrap: wrap; }
  .nav button { height: 40px; padding: 0 16px; }
  .sidebar-foot { margin: 0 0 0 auto; flex-direction: row; align-items: center; gap: 8px; }
  .sidebar-foot .pill { white-space: nowrap; padding: 6px 10px; }
  .sidebar-foot .btn.small { padding: 0 12px; }
  .cols-2, .cols-3 { grid-template-columns: 1fr; }
  #main { padding: 16px 12px 40px; }
}

/* ── Cards ────────────────────────────────────────────────── */
/* M3 filled card: a tonal surface, no outline. The old 1px border is gone —
 * separation comes from the tone step against the page background. */
.panel, .stat {
  background: var(--surface); border: 0;
  border-radius: var(--radius); padding: 16px 18px;
}
.panel figcaption {
  display: flex; justify-content: space-between; align-items: baseline;
  font-size: 14px; font-weight: 500; letter-spacing: .1px;
  margin-bottom: 14px; gap: 12px;
}
.panel { margin: 0; }
.stat-label {
  font-size: 11px; color: var(--ink-2); text-transform: uppercase;
  letter-spacing: .5px; font-weight: 500;
}
.stat-value {
  font-size: 32px; font-weight: 400; letter-spacing: 0;
  font-variant-numeric: tabular-nums; margin: 6px 0 2px;
}
.stat-sub { font-size: 12.5px; color: var(--ink-2); font-variant-numeric: tabular-nums; }
.delta { font-weight: 500; }
.delta.up { color: var(--bad); }
.delta.down { color: var(--good); }

.kv { display: grid; grid-template-columns: auto 1fr; gap: 6px 20px; font-size: 13.5px; }
.kv dt { color: var(--ink-2); }
.kv dd { margin: 0; text-align: right; font-variant-numeric: tabular-nums; }

.banner {
  border-radius: var(--md-r-md); padding: 12px 16px; font-size: 13px; margin-bottom: 16px;
  background: color-mix(in srgb, var(--warn) 14%, var(--surface)); color: var(--warn);
}
.notice {
  border-radius: var(--md-r-md); padding: 14px 16px; font-size: 13.5px;
  background: var(--surface-2); color: var(--ink-2); line-height: 1.55;
}

/* ── Charts ───────────────────────────────────────────────── */
/* On a tinted container surface a single accent at low opacity can fall under
 * 3:1, so these are picked against the container tone rather than inherited
 * from the link colour. */
.chart { position: relative; }
.chart svg { display: block; width: 100%; height: auto; }
.chart .bar { fill: var(--md-primary); transition: fill var(--md-dur) var(--md-ease); }
.chart .bar.zero { fill: var(--md-outline-variant); }
.chart .bar.alt { fill: var(--warn); }
.chart .bar.over { fill: var(--md-error); }
.chart .bar.under { fill: var(--md-success); }
.chart .bar:hover { fill: color-mix(in srgb, var(--md-primary) 82%, var(--md-on-surface)); }
.chart .bar.over:hover { fill: color-mix(in srgb, var(--md-error) 82%, var(--md-on-surface)); }
.chart .bar.under:hover { fill: color-mix(in srgb, var(--md-success) 82%, var(--md-on-surface)); }
.chart .line { fill: none; stroke: var(--md-primary); stroke-width: 2; stroke-linejoin: round; }
.chart .area { fill: var(--md-primary); opacity: .12; }
.chart .grid { stroke: var(--md-outline-variant); stroke-width: 1; }
.chart .grid.zero-line { stroke: var(--md-outline); }
.chart .tick { fill: var(--ink-2); font-size: 10px; }
/* Printed on the bar itself: the figure should be readable without a pointer. */
.chart .val { fill: var(--ink-2); font-size: 10px; font-weight: 500; font-variant-numeric: tabular-nums; }
.chart .dot { fill: var(--md-primary); }
/* Heatmap cells carry intensity as opacity over one themed colour, so the same
 * markup reads correctly in light and dark. */
.chart .cell { fill: var(--md-primary); shape-rendering: crispEdges; }
.chart .cell:hover { fill: var(--md-error); fill-opacity: 1 !important; }
.legend {
  display: flex; align-items: center; gap: 8px; margin-top: 10px;
  font-size: 11.5px; color: var(--ink-2);
}
.legend .ramp {
  flex: 0 0 120px; height: 8px; border-radius: var(--md-r-full);
  background: linear-gradient(to right,
    color-mix(in srgb, var(--md-primary) 4%, transparent), var(--md-primary));
}
.tip {
  position: absolute; pointer-events: none; transform: translate(-50%, -100%);
  background: var(--md-on-surface); color: var(--md-surface-container-lowest);
  font-size: 12px; padding: 6px 10px; border-radius: var(--md-r-xs);
  white-space: nowrap; font-variant-numeric: tabular-nums; z-index: 5;
  box-shadow: var(--md-e1);
}

/* ── Tables and lists ─────────────────────────────────────── */
.table-wrap { overflow-x: auto; }
table { width: 100%; border-collapse: collapse; font-size: 13px; }
th {
  text-align: left; font-weight: 500; color: var(--ink-2); font-size: 11.5px;
  text-transform: uppercase; letter-spacing: .5px; padding: 8px 12px;
  border-bottom: 1px solid var(--line); white-space: nowrap;
}
td { padding: 9px 12px; border-bottom: 1px solid var(--line); white-space: nowrap; }
td.num, th.num { text-align: right; font-variant-numeric: tabular-nums; }
tbody tr { transition: background var(--md-dur) var(--md-ease); }
tbody tr:hover {
  background: color-mix(in srgb, var(--md-on-surface) calc(var(--state-hover) * 100%), transparent);
}
tbody tr.clickable { cursor: pointer; }

.list { display: flex; flex-direction: column; gap: 8px; }
.item {
  display: flex; align-items: center; justify-content: space-between; gap: 14px;
  background: var(--surface-2); border: 0; border-radius: var(--md-r-md);
  padding: 12px 16px; text-align: left; font: inherit; color: inherit; width: 100%;
  transition: background var(--md-dur) var(--md-ease);
}
button.item, .item.clickable { cursor: pointer; }
.item:hover {
  background: color-mix(in srgb, var(--md-on-surface) calc(var(--state-hover) * 100%), var(--surface-2));
}
.item:active {
  background: color-mix(in srgb, var(--md-on-surface) calc(var(--state-press) * 100%), var(--surface-2));
}
.item .lead { display: block; font-weight: 500; }
.item .sub { display: block; font-size: 12px; color: var(--ink-2); font-variant-numeric: tabular-nums; }
.item .amt { font-weight: 500; font-variant-numeric: tabular-nums; text-align: right; }
.empty { color: var(--ink-3); text-align: center; padding: 28px 0; }

/* ── Controls ─────────────────────────────────────────────── */
/* Outlined text field, compact. True floating labels need markup changes and
 * are deliberately left out of this pass. */
.control, input, select {
  font: inherit; font-size: 14px; color: var(--ink);
  background: transparent;
  border: 1px solid var(--md-outline); border-radius: var(--md-r-xs);
  padding: 9px 14px;
  transition: border-color var(--md-dur) var(--md-ease), box-shadow var(--md-dur) var(--md-ease);
}
input, select { width: 100%; }
input:hover, select:hover, .control:hover { border-color: var(--md-on-surface); }
input:focus, select:focus, .control:focus-visible {
  outline: none;
  border-color: var(--md-primary);
  box-shadow: inset 0 0 0 1px var(--md-primary);
}

/* Buttons: pill, 40px, with a state layer. */
.btn {
  font: inherit; font-size: 14px; font-weight: 500; letter-spacing: .1px;
  cursor: pointer; text-align: center; text-decoration: none;
  display: inline-flex; align-items: center; justify-content: center;
  width: auto; height: 40px; padding: 0 24px; white-space: nowrap;
  border-radius: var(--md-r-full);
  border: 1px solid var(--md-outline);
  background: transparent; color: var(--md-primary);
  transition: background var(--md-dur) var(--md-ease), box-shadow var(--md-dur) var(--md-ease);
}
.btn:hover {
  background: color-mix(in srgb, var(--md-primary) calc(var(--state-hover) * 100%), transparent);
}
.btn:active {
  background: color-mix(in srgb, var(--md-primary) calc(var(--state-press) * 100%), transparent);
}
.btn.primary {
  background: var(--md-primary); border-color: transparent; color: var(--md-on-primary);
}
.btn.primary:hover { box-shadow: var(--md-e1); background: var(--md-primary); filter: brightness(1.06); }
.btn.primary:active { box-shadow: none; filter: brightness(.96); }
.btn.small { height: 32px; padding: 0 16px; font-size: 13px; }
.btn.danger { color: var(--md-error); border-color: var(--md-outline); }
.btn.danger:hover {
  background: color-mix(in srgb, var(--md-error) calc(var(--state-hover) * 100%), transparent);
}
a.control { text-decoration: none; display: inline-flex; align-items: center; }
label { display: flex; flex-direction: column; gap: 6px; font-size: 12px; color: var(--ink-2); }
form.grid { margin-bottom: 0; align-items: end; }

/* Chips */
.pill {
  font-size: 12px; font-weight: 500; letter-spacing: .1px;
  padding: 6px 12px; border-radius: var(--md-r-sm);
  background: var(--surface-2); color: var(--ink-2); text-align: center;
}
.pill.charging { background: var(--md-secondary-container); color: var(--md-on-secondary-container); }
.pill.error { background: color-mix(in srgb, var(--bad) 16%, var(--surface)); color: var(--bad); }
.tag {
  font-size: 11px; font-weight: 500; letter-spacing: .1px;
  padding: 3px 8px; border-radius: var(--md-r-sm);
  background: var(--surface-2); color: var(--ink-2);
}
.tag.live { background: var(--md-secondary-container); color: var(--md-on-secondary-container); }
.tag.off { background: color-mix(in srgb, var(--warn) 16%, var(--surface)); color: var(--warn); }

/* ── Standing-load leaderboard ────────────────────────────── */
.lb { display: flex; flex-direction: column; gap: 14px; }
.lb-head { display: flex; justify-content: space-between; align-items: baseline; gap: 12px; }
.lb-name { font-weight: 500; }
.lb-amt { font-variant-numeric: tabular-nums; font-weight: 500; }
.lb-track {
  height: 8px; border-radius: var(--md-r-full); margin: 6px 0 4px;
  background: color-mix(in srgb, var(--md-on-surface) 8%, transparent);
  overflow: hidden;
}
.lb-fill { height: 100%; border-radius: var(--md-r-full); background: var(--md-primary); }
/* The remainder is the target, not an achievement — it reads in the error
 * colour so the eye lands on the thing worth shrinking. */
.lb-row.unexplained .lb-fill { background: var(--md-error); }
.lb-row.unexplained .lb-amt { color: var(--md-error); }
.lb-row.pending .lb-name, .lb-row.pending .lb-sub { opacity: .72; }
.lb-fill.pending {
  width: 100%; opacity: .45;
  background: repeating-linear-gradient(135deg,
    color-mix(in srgb, var(--md-on-surface) 22%, transparent) 0 5px,
    transparent 5px 10px);
}
.lb-sub { font-variant-numeric: tabular-nums; }

/* ── Sign-in gate ─────────────────────────────────────────── */
.gate {
  position: fixed; inset: 0; z-index: 30; display: flex;
  align-items: center; justify-content: center; background: var(--bg); padding: 24px;
}
.gate-card { text-align: center; display: flex; flex-direction: column; align-items: center; gap: 14px; }
.gate-title { font-size: 32px; font-weight: 400; letter-spacing: 0; margin: 0; }
.gate-sub { color: var(--ink-2); margin: 0; }

/* ── Dialog ───────────────────────────────────────────────── */
.sheet {
  position: fixed; inset: 0; background: rgba(0,0,0,.32);
  display: flex; align-items: center; justify-content: center; z-index: 20; padding: 24px;
}
.sheet-inner {
  background: var(--md-surface-container-high); width: 100%; max-width: 720px;
  border-radius: var(--md-r-xl);
  padding: 24px 26px; max-height: 86vh; overflow: auto; position: relative;
  border: 0; box-shadow: var(--md-e2);
}
.sheet-close {
  position: absolute; top: 12px; right: 12px; background: none; border: 0;
  font-size: 22px; line-height: 1; color: var(--ink-2); cursor: pointer;
  width: 40px; height: 40px; border-radius: var(--md-r-full);
  transition: background var(--md-dur) var(--md-ease);
}
.sheet-close:hover {
  background: color-mix(in srgb, var(--md-on-surface) calc(var(--state-hover) * 100%), transparent);
}

/* ── AI insight cards ─────────────────────────────────────── */
/* The one place a coloured container is used to mark authorship: everything in
 * here is model-written prose, and nothing in it is a dollar figure the model
 * worked out. */
.insight {
  margin-bottom: 16px; border-left: 0;
  background: var(--md-primary-container); color: var(--md-on-primary-container);
}
.insight-head { display: flex; justify-content: space-between; align-items: center; margin-bottom: 10px; }
.insight-tag {
  font-size: 11px; font-weight: 500; letter-spacing: .5px; text-transform: uppercase;
  color: var(--md-on-primary-container);
}
.insight .btn {
  color: var(--md-on-primary-container);
  border-color: color-mix(in srgb, var(--md-on-primary-container) 40%, transparent);
}
.insight .btn:hover {
  background: color-mix(in srgb, var(--md-on-primary-container) calc(var(--state-hover) * 100%), transparent);
}
.insight h3 { margin: 0 0 4px; font-size: 14px; }
.insight .headline { font-size: 16px; font-weight: 400; margin: 0 0 14px; line-height: 1.45; }
.insight-item {
  padding: 10px 0;
  border-top: 1px solid color-mix(in srgb, var(--md-on-primary-container) 20%, transparent);
}
.insight-item:first-of-type { border-top: 0; }
.insight-item .detail { color: inherit; opacity: .86; margin: 2px 0 0; font-size: 13px; }
.insight-item .act { color: inherit; font-weight: 500; font-size: 13px; margin-top: 4px; }
.insight-item .worth { font-variant-numeric: tabular-nums; font-weight: 500; }
.insight-foot { margin-top: 12px; font-size: 12.5px; opacity: .74; }
.insight .muted { color: inherit; opacity: .74; }

/* Device setup */
.setup-btn { margin-top: 8px; align-self: flex-start; }
.add-summary { cursor: pointer; font-weight: 500; color: var(--md-primary); margin-top: 6px; }
#add-device[open] .add-summary { margin-bottom: 10px; }
#device-form { margin-top: 8px; gap: 10px; }
.setup-card {
  margin-top: 14px; padding: 14px 16px; border: 0;
  border-radius: var(--md-r-md); background: var(--md-secondary-container);
  color: var(--md-on-secondary-container);
}
.setup-card .lead { font-weight: 500; margin-bottom: 6px; }
.setup-card p { margin: 6px 0; }
.code {
  display: block; overflow-x: auto; white-space: pre; margin: 6px 0;
  padding: 12px 14px; border-radius: var(--md-r-sm);
  background: color-mix(in srgb, var(--md-on-surface) 8%, var(--surface));
  border: 0; font-family: "Roboto Mono", ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: 12px; line-height: 1.5; color: var(--ink);
}
.setup-card code { font-family: "Roboto Mono", ui-monospace, SFMono-Regular, Menlo, monospace; font-size: 12px; }

@media (prefers-reduced-motion: reduce) {
  * { transition: none !important; }
}
