/* The theme is set on <html data-theme> by a script in <head> (saved choice, else the system
   setting) and by the header switch, so only the attribute is needed here. */
:root {
  color-scheme: light;
  --bg: #f5f6f8;
  --surface: #ffffff;
  --text: #1a1d23;
  --muted: #6b7280;
  --border: #e3e5e9;
  --grid: rgba(0, 0, 0, 0.1);
  --switch-off: #b8bdc7;
  --good: #1a7f37;
  --bad: #cf222e;
  --accent: #d3396d;
  --accent-2: #2d6cdf;
}

:root[data-theme="dark"] {
  color-scheme: dark;
  --bg: #14161a;
  --surface: #1d2026;
  --text: #eceef1;
  --muted: #9aa1ad;
  --border: #2a2e36;
  --grid: rgba(255, 255, 255, 0.1);
  --switch-off: #4a505c;
  --good: #3fb950;
  --bad: #f85149;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
}

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-wrap: wrap;
  gap: 8px 24px;
  padding: 20px 24px;
  border-bottom: 1px solid var(--border);
}

.topbar h1 { font-size: 1.25rem; margin: 0; }
.topbar-right { display: flex; align-items: center; flex-wrap: wrap; gap: 8px 20px; }
.status { color: var(--muted); font-size: 0.85rem; }

.switch {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  position: relative;
  cursor: pointer;
  user-select: none;
  font-size: 0.85rem;
  color: var(--muted);
}
.switch input { position: absolute; opacity: 0; width: 0; height: 0; }
.switch .slider {
  position: relative;
  flex-shrink: 0;
  width: 38px;
  height: 22px;
  border-radius: 22px;
  background: var(--switch-off);
  transition: background 0.2s;
}
.switch .slider::after {
  content: '';
  position: absolute;
  top: 3px;
  left: 3px;
  width: 16px;
  height: 16px;
  border-radius: 50%;
  background: #fff;
  box-shadow: 0 1px 2px rgba(0, 0, 0, 0.3);
  transition: transform 0.2s;
}
.switch input:checked + .slider { background: var(--accent); }
.switch input:checked + .slider::after { transform: translateX(16px); }
.switch input:focus-visible + .slider { outline: 2px solid var(--accent-2); outline-offset: 2px; }

main {
  max-width: 960px;
  margin: 0 auto;
  padding: 24px;
}

.controls {
  display: flex;
  align-items: flex-end;
  gap: 16px;
  flex-wrap: wrap;
  margin-bottom: 8px;
}

.hint {
  font-size: 0.78rem;
  color: var(--muted);
  margin: 0 0 20px;
}

.control-group { display: flex; flex-direction: column; gap: 4px; }
.control-group label { font-size: 0.75rem; color: var(--muted); }
select {
  padding: 8px 10px;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
}

button {
  padding: 9px 16px;
  border-radius: 8px;
  border: none;
  background: var(--accent);
  color: white;
  font-weight: 600;
  cursor: pointer;
}
button:disabled { opacity: 0.6; cursor: default; }

button.secondary,
a.button.secondary {
  background: transparent;
  color: var(--text);
  border: 1px solid var(--border);
  padding: 7px 14px;
  font-size: 0.85rem;
}
a.button {
  display: inline-block;
  border-radius: 8px;
  font-weight: 600;
  text-decoration: none;
}

.cards {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(160px, 1fr));
  gap: 12px;
  margin-bottom: 24px;
}

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 16px;
}

.card-delta { margin-top: 4px; font-size: 0.78rem; color: var(--muted); }
.card-delta:empty { display: none; }
.delta { font-weight: 700; }
.delta.good { color: var(--good); }
.delta.bad { color: var(--bad); }
.compare-note { margin-top: -12px; }

.login {
  min-height: 100vh;
  display: grid;
  place-items: center;
  padding: 24px;
}
.login-card {
  width: min(100%, 360px);
  display: flex;
  flex-direction: column;
  gap: 14px;
  padding: 28px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
}
.login-card h1 { margin: 0 0 4px; font-size: 1.15rem; }
.login-card label { display: flex; flex-direction: column; gap: 4px; font-size: 0.8rem; color: var(--muted); }
.login-card input {
  padding: 10px 12px;
  font: inherit;
  color: var(--text);
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 8px;
}
.login-card input:focus-visible { outline: 2px solid var(--accent-2); outline-offset: 1px; }
.login-message { margin: 0; padding: 8px 10px; font-size: 0.85rem; color: var(--bad); border: 1px solid var(--bad); border-radius: 8px; }

.card-label { font-size: 0.75rem; color: var(--muted); margin-bottom: 6px; }
.card-value { font-size: 1.4rem; font-weight: 700; }

.chart-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 18px;
  margin-bottom: 20px;
}
.chart-card h2 { margin: 0 0 12px; font-size: 0.95rem; color: var(--muted); font-weight: 600; }

/* Charts sit in a grid: one column on phones, two side by side on wide screens, and three across
   when solar is shown on very wide ones. Each chart gets a height that scales with the screen. */
.charts {
  display: grid;
  grid-template-columns: minmax(0, 1fr);
  gap: 20px;
  margin-bottom: 20px;
}
.charts .chart-card { margin-bottom: 0; min-width: 0; }
.chart-box { position: relative; height: clamp(260px, 26vh, 520px); }

@media (min-width: 1400px) {
  main { max-width: 2560px; padding: 24px 32px; }
  .charts { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  /* With solar, the third chart spans both columns until there is room for three across. */
  .charts.has-solar .chart-card:nth-child(3) { grid-column: 1 / -1; }
}

@media (min-width: 2200px) {
  .charts.has-solar { grid-template-columns: repeat(3, minmax(0, 1fr)); }
  .charts.has-solar .chart-card:nth-child(3) { grid-column: auto; }
}

.table-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 12px;
}
.table-header h2 { margin: 0; }

.note {
  text-align: center;
  color: var(--muted);
  padding: 24px;
}

.table-wrap {
  max-height: clamp(300px, 29vh, 720px);
  overflow-y: auto;
  overflow-x: auto;
}

table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.85rem;
}

th, td {
  padding: 8px 10px;
  text-align: right;
  white-space: nowrap;
  border-bottom: 1px solid var(--border);
}

th:first-child, td:first-child { text-align: left; }

thead th {
  position: sticky;
  top: 0;
  background: var(--surface);
  color: var(--muted);
  font-weight: 600;
  vertical-align: bottom;
  line-height: 1.25;
}

/* The unit under a column name, e.g. "Imported" over "kWh". */
thead th small { display: block; font-size: 0.75em; font-weight: 400; opacity: 0.75; }

tbody tr:hover { background: var(--bg); }

.flag {
  margin-left: 6px;
  padding: 1px 6px;
  border-radius: 6px;
  font-size: 0.72rem;
  color: #b7791f;
  background: rgba(232, 179, 57, 0.15);
}

.flag.free {
  color: var(--good);
  background: rgba(63, 185, 80, 0.14);
}

.flag.outage {
  color: var(--accent-2);
  background: rgba(45, 108, 223, 0.14);
}

.special-lead { margin: 0 0 10px; }
.special-lead .info-btn { vertical-align: middle; margin-left: 4px; }
.special-help { margin: 0 0 12px; padding: 10px 14px; border-radius: 12px; background: var(--bg); border: 0.5px solid var(--border); font-size: 0.85rem; color: var(--text); }
.special-help ul { margin: 0; padding-left: 18px; }
.special-help li { margin-bottom: 6px; }
.special-help li:last-child { margin-bottom: 0; }
/* Special periods: one card per period (see renderSpecialPeriods). */
.sp-list { list-style: none; margin: 0; padding: 0; }
.sp-card { margin: 0 0 10px; padding: 12px 14px; border: 0.5px solid var(--border); border-radius: 16px; background: var(--surface); }
.sp-top { display: flex; align-items: center; justify-content: space-between; gap: 8px; }
.sp-chip { display: inline-block; padding: 3px 11px; border-radius: 999px; font-size: 0.78rem; font-weight: 600; }
.sp-card.kind-free .sp-chip { color: var(--good); background: var(--good-bg, transparent); }
.sp-card.kind-outage .sp-chip { color: var(--accent-2); background: var(--blue-bg, transparent); }
.sp-when { margin: 9px 0 2px; font-size: 1rem; font-weight: 600; }
.sp-when span { color: var(--muted); font-weight: 400; }
.sp-result { margin: 0; font-size: 0.88rem; color: var(--muted); }
.sp-result.pending::before { content: ''; display: inline-block; width: 7px; height: 7px; margin-right: 6px; border-radius: 50%; background: var(--warn, #b7791f); vertical-align: 1px; }
.sp-result.warn { margin-top: 8px; padding: 8px 10px; border-radius: 10px; color: var(--warn, #b7791f); background: var(--warn-bg, transparent); }
.sp-note { margin: 8px 0 0; font-size: 0.8rem; color: var(--muted); overflow-wrap: anywhere; }
.sp-empty { padding: 14px; text-align: center; color: var(--muted); font-size: 0.9rem; }

.free-dialog {
  width: min(94vw, 760px);
  max-height: 90vh;
  max-height: 90dvh;
  overflow: auto;
  padding: 20px;
  color: var(--text);
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
}
.free-dialog::backdrop { background: rgba(0, 0, 0, 0.55); }
/* Stays at the top of the dialog while its contents scroll, so Close is always reachable. */
.free-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  position: sticky;
  top: -20px;
  z-index: 2;
  margin: -20px -20px 0;
  padding: 14px 20px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}
.free-head h2 { margin: 0; font-size: 1.05rem; }
.free-form { display: flex; flex-wrap: wrap; align-items: flex-end; gap: 12px; margin: 14px 0 8px; }
.free-form label { display: flex; flex-direction: column; gap: 4px; font-size: 0.75rem; color: var(--muted); }
.free-form label.grow { flex: 1 1 160px; }
.free-form input {
  padding: 8px 10px;
  font: inherit;
  color: var(--text);
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 8px;
}
.free-message { margin: 0 0 8px; padding: 8px 10px; font-size: 0.85rem; color: var(--bad); border: 1px solid var(--bad); border-radius: 8px; }
.free-list { max-height: 40vh; }
.free-list td.note { white-space: normal; text-align: left; }
.free-list td.pending { color: var(--muted); }
button.link-danger { color: var(--bad); border-color: var(--bad); padding: 4px 10px; font-size: 0.78rem; }

.pw-controls { display: flex; flex-direction: column; gap: 10px; margin: 14px 0 8px; }
.pw-row {
  display: grid;
  grid-template-columns: 9.5rem minmax(7rem, 1fr) auto auto;
  align-items: center;
  gap: 10px 12px;
}
.pw-row label { font-weight: 600; }
.pw-now { color: var(--muted); font-size: 0.85rem; }
.pw-input { display: inline-flex; align-items: center; gap: 6px; }
.pw-input input, .pw-input select {
  padding: 8px 10px;
  font: inherit;
  color: var(--text);
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 8px;
}
.pw-input input { width: 5rem; }
.pw-log th, .pw-log td { text-align: left; }
.solar-headline { font-size: 1.05rem; margin: 10px 0 4px; }
.solar-headline strong { font-weight: 700; }
.solar-over { color: #3fb950; }
.solar-under { color: #d29922; }
.solar-sub { display: block; font-size: 0.8rem; font-weight: 400; color: var(--muted); }
.solar-today { color: #3fb950; font-weight: 600; }
.solar-fc, .solar-pct { font-weight: 600; white-space: nowrap; }
.solar-note { margin: 0 0 6px; }
.settings-form h3 { margin: 18px 0 6px; font-size: 0.95rem; color: var(--muted); font-weight: 600; border-bottom: 1px solid var(--border); padding-bottom: 4px; }
.settings-form h3:first-child { margin-top: 4px; }
.set-row { display: grid; gap: 4px; margin: 10px 0; }
.set-row label { font-size: 0.85rem; font-weight: 600; display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }
.set-row input, .set-row select {
  width: 100%;
  padding: 8px 10px;
  font: inherit;
  color: var(--text);
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 8px;
}
.set-row input[aria-invalid="true"], .set-row select[aria-invalid="true"] { border-color: var(--bad); }
.info-help { font-size: 0.75rem; color: var(--muted); }
.set-err { font-size: 0.8rem; color: var(--bad); }
.set-badge { font-weight: 400; font-size: 0.7rem; padding: 1px 7px; border-radius: 6px; color: var(--muted); border: 1px solid var(--border); }
.settings-actions { display: flex; gap: 8px; margin-top: 16px; }

/* Settings: one collapsed card per service, with a one-line status. */
.set-group { border: 1px solid var(--border); border-radius: 14px; margin: 10px 0; background: var(--bg); }
.set-group > summary { list-style: none; display: flex; align-items: center; gap: 12px; min-height: 56px; padding: 8px 14px; cursor: pointer; }
.set-group > summary::-webkit-details-marker { display: none; }
.set-ic { flex: none; width: 36px; height: 36px; border-radius: 10px; display: grid; place-items: center; background: var(--surface); border: 1px solid var(--border); color: var(--accent); }
.set-icsvg { width: 20px; height: 20px; }
.set-sumtext { flex: 1; min-width: 0; display: grid; }
.set-gname { font-weight: 600; }
.set-status { font-size: 0.8rem; color: var(--muted); }
.set-status.ok { color: var(--good); }
.set-status.warn { color: var(--warn); }
.set-flag { flex: none; font-size: 0.7rem; font-weight: 600; padding: 2px 8px; border-radius: 999px; color: var(--warn); border: 1px solid var(--warn); }
.set-chev { flex: none; font-size: 1.4rem; line-height: 1; color: var(--muted); transition: transform 0.15s; }
.set-group[open] > summary .set-chev { transform: rotate(90deg); }
.set-body { padding: 0 14px 12px; border-top: 1px solid var(--border); }
.set-secret input { flex: 1 1 100%; }
.info-btn { width: 28px; height: 28px; min-height: 0; padding: 0; border-radius: 50%; font: italic 700 0.8rem Georgia, serif; line-height: 1; color: var(--muted); background: transparent; border: 1px solid var(--border); cursor: pointer; position: relative; }
.info-btn::after { content: ''; position: absolute; inset: -8px; } /* a 44px touch target around the small circle */
.info-btn[aria-expanded="true"] { color: var(--on-accent); background: var(--accent); border-color: var(--accent); }
.set-secret { display: flex; flex-wrap: wrap; align-items: center; gap: 8px; }
.set-saved, .set-removing { flex: 1 1 100%; color: var(--muted); }
.set-removing { color: var(--warn); }
/* "Save your changes?" asked when Close is used with changes that are not saved. */
.set-leave { margin: 0 0 10px; padding: 12px; border-radius: 12px; background: var(--warn-bg, #fff2d4); color: var(--text); border: 1px solid var(--warn, #b86e00); }
.set-leave[hidden] { display: none; }
.set-leave p { margin: 0 0 10px; font-weight: 600; }
.set-leave-btns { display: flex; flex-wrap: wrap; gap: 8px; }
.set-small { padding: 3px 12px; font-size: 0.8rem; }
.set-danger { color: var(--bad); }
.pw-tools { display: inline-flex; gap: 8px; }
.pw-alert-note { display: flex; flex-wrap: wrap; align-items: center; gap: 8px; margin: 0 0 10px; }
.pw-alert-note button { padding: 3px 10px; font-size: 0.78rem; }
.pw-sched-form {
  display: flex;
  flex-wrap: wrap;
  align-items: flex-end;
  gap: 12px;
  margin: 0 0 12px;
  padding: 12px;
  border: 1px solid var(--border);
  border-radius: 10px;
}
.pw-sched-form[hidden] { display: none; } /* the flex above must not defeat the hidden attribute: the form opens with Add schedule */
.pw-sched-form label { display: flex; flex-direction: column; gap: 4px; font-size: 0.75rem; color: var(--muted); }
.pw-sched-form label.grow { flex: 1 1 200px; }
.switch input:disabled + .slider { opacity: 0.45; }
/* A slider switch inside a form (switchRow in app.js): the switch then its text on one line, not the stacked label of a field. */
.pw-sched-form label.switch { flex-direction: row; align-items: center; gap: 10px; min-height: 40px; font-size: 0.9rem; color: var(--text); }
/* A class that sets display (the flex on form labels) must not defeat the hidden attribute. */
.pw-sched-form label[hidden], .free-form label[hidden] { display: none; }
.pw-sched-form input[type="text"], .pw-sched-form input[type="time"], .pw-sched-form input[type="number"], .pw-sched-form select {
  padding: 8px 10px;
  font: inherit;
  color: var(--text);
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 8px;
}
.pw-sched-form input[type="number"] { width: 5rem; }
.pw-days { border: none; margin: 0; padding: 0; font-size: 0.75rem; color: var(--muted); }
.pw-days legend { padding: 0 0 4px; }
.pw-days label { display: inline-flex; flex-direction: row; align-items: center; gap: 4px; margin-right: 8px; color: var(--text); font-size: 0.85rem; }
.pw-sched-buttons { display: inline-flex; gap: 8px; }
.pw-log td .pw-row-buttons { display: inline-flex; flex-wrap: wrap; gap: 6px; min-width: 7rem; }
.pw-log td .sub { display: block; color: var(--muted); font-size: 0.78rem; }
.pw-log td { white-space: normal; }
.pw-log td button { padding: 4px 10px; font-size: 0.78rem; }
.pw-log tr.off td { opacity: 0.55; }
.pw-log tr.off td:last-child { opacity: 1; }
.free-message.ok { color: var(--good); border-color: var(--good); }
.free-message.warn { color: #b7791f; border-color: #b7791f; }
.free-list td.ok { color: var(--good); }
.free-list td.warn { color: #b7791f; }
.free-list td.fail { color: var(--bad); }
.free-list td.wrap { white-space: normal; text-align: left; }
@media (max-width: 560px) {
  .pw-row { grid-template-columns: 1fr auto; }
  .pw-now { grid-column: 1 / -1; }
}

#breakdownTable { font-variant-numeric: tabular-nums; }
table.hide-export .export-col { display: none; }
/* Total cost leads the table when there is no export data (Net cost does otherwise). */
table:not(.hide-export) .noexport-col { display: none; }
table.hide-solar .solar-col { display: none; }
table.hide-usage .usage-col { display: none; }
table.hide-lastyear .lastyear-col { display: none; }

/* Rates dialog: one bar per half hour. */
.rate-day h3 { margin: 14px 0 6px; font-size: 0.95rem; }
.rate-chart { display: flex; align-items: flex-end; gap: 1px; height: 120px; padding: 4px 0; border-bottom: 1px solid var(--border); }
.rate-bar { flex: 1 1 0; min-width: 2px; background: var(--muted); opacity: 0.55; border-radius: 2px 2px 0 0; }
.rate-bar.cheap { background: var(--good); opacity: 1; }
.rate-bar.dear { background: var(--bad); opacity: 1; }
.rate-bar.negative { background: var(--accent-2); opacity: 1; }
.rate-bar.none { height: 2px; background: var(--border); }
.rate-axis { display: flex; justify-content: space-between; font-size: 0.75rem; color: var(--muted); margin-top: 2px; }
