:root {
  --bg: #f4f6f8;
  --surface: #ffffff;
  --surface-alt: #f8fafc;
  --border: #e2e8f0;
  --text: #1e293b;
  --text-muted: #64748b;
  /* Decorative-only tone — do NOT use for text that conveys information;
     at this lightness it fails WCAG 1.4.3 (2.56:1 on white). Use
     --text-muted (4.76:1) for any real content. */
  --text-faint: #94a3b8;
  --accent: #00728f;              /* 5.52:1 on white — used for text/links/focus/active-tab */
  --accent-hover: #005b73;
  --accent-contrast: #00728f;     /* button backgrounds under white text — same as --accent in light mode */
  --accent-contrast-hover: #005b73;
  --accent-soft: rgba(0, 114, 143, 0.12);
  --success: #15803d;             /* 5.02:1 on white */
  --warning: #d97706;
  --danger: #dc2626;
  --shadow-sm: 0 1px 2px rgba(15, 23, 42, 0.06);
  --shadow-md: 0 4px 16px rgba(15, 23, 42, 0.08);
  --radius: 10px;
  --font: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  color-scheme: light;
}

:root[data-theme="dark"] {
  --bg: #0f1620;
  --surface: #161f2b;
  --surface-alt: #1b2531;
  --border: #2a3644;
  --text: #e5edf5;
  --text-muted: #94a5b8;
  --text-faint: #64758a;
  --accent: #24b6e0;              /* 6.99:1 on dark surface as text — unchanged, already compliant */
  --accent-hover: #4cc9ec;
  --accent-contrast: #0e7a95;     /* 4.96:1 with white text — accent itself only hits 2.38:1 as a button fill */
  --accent-contrast-hover: #0c6980;
  --accent-soft: rgba(36, 182, 224, 0.15);
  --success: #22c55e;             /* 7.28:1 on dark surface */
  --shadow-sm: 0 1px 2px rgba(0, 0, 0, 0.3);
  --shadow-md: 0 8px 24px rgba(0, 0, 0, 0.4);
  color-scheme: dark;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #0f1620;
    --surface: #161f2b;
    --surface-alt: #1b2531;
    --border: #2a3644;
    --text: #e5edf5;
    --text-muted: #94a5b8;
    --text-faint: #64758a;
    --accent: #24b6e0;
    --accent-hover: #4cc9ec;
    --accent-contrast: #0e7a95;
    --accent-contrast-hover: #0c6980;
    --accent-soft: rgba(36, 182, 224, 0.15);
    --success: #22c55e;
    --shadow-sm: 0 1px 2px rgba(0, 0, 0, 0.3);
    --shadow-md: 0 8px 24px rgba(0, 0, 0, 0.4);
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }

.icon { display: inline-block; vertical-align: middle; flex: none; }

html, body {
  margin: 0;
  padding: 0;
  background: var(--bg);
  color: var(--text);
  font-family: var(--font);
  font-size: 14px;
  line-height: 1.5;
}

.app-shell {
  min-height: 100vh;
  display: flex;
  flex-direction: column;
}

/* ---------- Header ---------- */
.app-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  padding: 16px 24px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  position: sticky;
  top: 0;
  z-index: 20;
}
.brand { display: flex; align-items: center; gap: 12px; }
.brand-icon {
  font-size: 26px;
  width: 44px; height: 44px;
  display: flex; align-items: center; justify-content: center;
  background: var(--accent-soft);
  border-radius: 12px;
  color: var(--accent);
}
.brand h1 { font-size: 17px; margin: 0; font-weight: 700; }
.brand .subtitle { margin: 2px 0 0; font-size: 12px; color: var(--text-muted); }
.header-actions { display: flex; flex-wrap: wrap; align-items: center; gap: 8px; }

/* ---------- Tabs ---------- */
.tab-bar {
  display: flex;
  gap: 4px;
  padding: 0 24px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  overflow-x: auto;
}
.tab-btn {
  appearance: none;
  border: none;
  background: transparent;
  color: var(--text-muted);
  font: inherit;
  font-weight: 600;
  padding: 12px 16px;
  cursor: pointer;
  border-bottom: 2px solid transparent;
  white-space: nowrap;
  transition: color .15s, border-color .15s;
}
.tab-btn:hover { color: var(--text); }
.tab-btn.active { color: var(--accent); border-bottom-color: var(--accent); }

.tab-content { flex: 1; padding: 20px 24px 60px; max-width: 1400px; width: 100%; margin: 0 auto; }
.tab-pane { display: none; flex-direction: column; gap: 16px; animation: fadein .15s ease; }
.tab-pane.active { display: flex; }
@keyframes fadein { from { opacity: 0; transform: translateY(4px);} to { opacity: 1; transform: translateY(0);} }

/* ---------- Cards ---------- */
.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 18px 20px;
  box-shadow: var(--shadow-sm);
}
.card-title {
  margin: 0 0 14px;
  font-size: 15px;
  font-weight: 700;
  color: var(--text);
}
/* Collapsible cards: title becomes a toggle for everything below it (see
   initCollapsibleCards in ui.js). */
.card-title-toggle {
  display: flex; align-items: center; justify-content: space-between;
  cursor: pointer; user-select: none; margin-bottom: 0;
  padding-bottom: 14px;
}
.card.collapsed .card-title-toggle { padding-bottom: 0; }
.card-chevron { transition: transform .15s ease; color: var(--text-faint); flex: none; }
.card.collapsed .card-chevron { transform: rotate(-90deg); }
.card-body { display: flex; flex-direction: column; gap: 16px; overflow: hidden; }
.card.collapsed .card-body { display: none; }
/* Muted, upright helper copy — the old accent-colored italic read as links. */
.hint-text {
  margin: 0;
  color: var(--text-muted);
  font-size: 13px;
  max-width: 80ch;
}
.empty-note { color: var(--text-muted); }

/* ---------- Forms ---------- */
.field-row {
  display: flex;
  flex-wrap: wrap;
  gap: 14px;
  align-items: flex-end;
  margin-bottom: 4px;
}
.field-row label {
  min-width: 0;
  display: flex;
  flex-direction: column;
  gap: 6px;
  font-size: 12px;
  font-weight: 600;
  color: var(--text-muted);
}
input[type="text"], input[type="number"], input[type="date"], select, textarea {
  font: inherit;
  padding: 8px 10px;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: var(--surface-alt);
  color: var(--text);
  min-width: 140px;
}
input:focus, select:focus, textarea:focus {
  outline: none;
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-soft);
}
input:disabled, select:disabled, textarea:disabled {
  opacity: .5;
  cursor: not-allowed;
}
.radio-row { display: flex; flex-direction: column; gap: 10px; }
.radio-row label { display: flex; align-items: center; gap: 8px; font-size: 13px; cursor: pointer; min-height: 24px; }

/* Consistent, visible keyboard-focus ring for anything not covered by the
   input/select/textarea rule above (buttons, checkboxes, radios, links,
   table rows). Uses :focus-visible so mouse clicks don't show a ring. */
button:focus-visible, a:focus-visible, [tabindex]:focus-visible,
input[type="checkbox"]:focus-visible, input[type="radio"]:focus-visible,
tr.selectable-row:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}
input[type="checkbox"], input[type="radio"] {
  width: 16px; height: 16px;
  accent-color: var(--accent);
}

/* ---------- Buttons ----------
   Two consistent size tiers only: the base .btn (every standard action) and
   .btn-lg (exactly one "hero" action per section — Search, Process, Import,
   Run). Nothing in between, so sizing reads as deliberate, not random. */
.btn {
  appearance: none;
  border: 1px solid transparent;
  border-radius: 9px;
  padding: 10px 16px;
  font: inherit;
  font-weight: 600;
  font-size: 13.5px;
  cursor: pointer;
  transition: background .15s, border-color .15s, transform .05s, box-shadow .15s;
  display: inline-flex;
  align-items: center;
  gap: 7px;
  white-space: nowrap;
  min-height: 40px;
  line-height: 1;
}
.btn:active { transform: scale(0.98); }
.btn:disabled { opacity: .5; cursor: not-allowed; }
.btn-lg { padding: 13px 22px; font-size: 14.5px; min-height: 48px; border-radius: 10px; }
.btn-primary { background: var(--accent-contrast); color: #fff; }
.btn-primary:hover:not(:disabled) { background: var(--accent-contrast-hover); box-shadow: var(--shadow-sm); }
.btn-accent { background: var(--accent-contrast); color: #fff; }
.btn-accent:hover:not(:disabled) { background: var(--accent-contrast-hover); box-shadow: var(--shadow-sm); }
.btn-secondary { background: var(--surface-alt); color: var(--text); border-color: var(--border); }
.btn-secondary:hover:not(:disabled) { background: var(--accent-soft); border-color: var(--accent); }
.btn-ghost { background: transparent; color: var(--text-muted); }
.btn-ghost:hover:not(:disabled) { color: var(--text); background: var(--surface-alt); }
.btn-icon { width: 40px; height: 40px; padding: 0; justify-content: center; border-radius: 50%; min-height: 0; }
.btn-block { width: 100%; justify-content: center; margin-top: 10px; }
.btn-row { display: flex; flex-wrap: wrap; gap: 10px; margin: 10px 0; align-items: center; }
.btn-danger { background: var(--danger); color: #fff; }
.btn .icon { flex: none; }

/* Text-style action for secondary/help links that sit inside copy
   (template download, help, select/deselect all) — not a full button. */
.btn-link {
  appearance: none; border: none; background: none; padding: 4px 0;
  display: inline-flex; align-items: center; gap: 6px;
  font: inherit; font-size: 13px; font-weight: 600;
  color: var(--accent); cursor: pointer; min-height: 24px;
  text-decoration: underline; text-underline-offset: 3px; text-decoration-thickness: 1px;
}
.btn-link:hover:not(:disabled) { color: var(--accent-hover); text-decoration-thickness: 2px; }
.btn-link:disabled { color: var(--text-muted); text-decoration: none; opacity: .6; cursor: not-allowed; }
.link-row { display: flex; flex-wrap: wrap; gap: 6px 20px; align-items: center; }
.btn-push-right { margin-left: auto; }

/* The EPW path lives inside the Location card as the alternative weather source */
.alt-source { border-top: 1px solid var(--border); padding-top: 16px; display: flex; flex-direction: column; gap: 4px; }

/* Regression Setup: one card, three labelled groups, one Run button */
.setup-group { border: none; margin: 4px 0 0; padding: 0; display: flex; flex-direction: column; gap: 10px; min-width: 0; }
.setup-group legend { padding: 0; margin-bottom: 10px; font-size: 13px; font-weight: 700; color: var(--text); }

/* ---------- Tables ---------- */
.table-wrap { overflow-x: auto; border: 1px solid var(--border); border-radius: 8px; margin: 10px 0; }
.data-table { width: 100%; border-collapse: collapse; font-size: 13px; }
.data-table th {
  text-align: left;
  background: var(--surface-alt);
  color: var(--text-muted);
  font-weight: 700;
  padding: 10px 12px;
  border-bottom: 1px solid var(--border);
  white-space: nowrap;
  position: sticky; top: 0;
}
.data-table td { padding: 8px 12px; border-bottom: 1px solid var(--border); white-space: nowrap; }
.data-table tbody tr:last-child td { border-bottom: none; }
.data-table.selectable tbody tr { cursor: pointer; }
.data-table.selectable tbody tr:hover { background: var(--surface-alt); }
/* Selection is conveyed by more than color alone (border + weight), per WCAG 1.4.1 */
.data-table.selectable tbody tr.selected { background: var(--accent-soft); box-shadow: inset 3px 0 0 var(--accent); font-weight: 600; }
.data-table tbody tr.empty-row td { color: var(--text-muted); font-style: italic; cursor: default; white-space: normal; }
.data-table tbody tr.empty-row:hover { background: transparent; }

/* ---------- Misc widgets ---------- */
.info-box {
  background: var(--surface-alt);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 10px 12px;
  font-size: 12.5px;
  color: var(--text-muted);
  white-space: pre-wrap;
  margin-top: 10px;
}
.progress-track {
  height: 8px;
  border-radius: 4px;
  background: var(--surface-alt);
  overflow: hidden;
  margin-top: 10px;
}
.progress-fill {
  height: 100%;
  background: var(--accent);
  border-radius: 4px;
  transition: width .3s ease;
}
.status-chips { display: flex; flex-wrap: wrap; gap: 10px; margin-bottom: 6px; }
.chip {
  appearance: none;
  font: inherit;
  font-size: 12px;
  font-weight: 600;
  padding: 5px 10px;
  border-radius: 999px;
  background: var(--surface-alt);
  border: 1px solid var(--border);
  color: var(--text-muted);
  cursor: not-allowed;
  min-height: 24px;
}
.chip.ready { color: var(--success); border-color: var(--success); background: rgba(21,128,61,0.08); cursor: pointer; }
.chip.ready:hover { background: rgba(21,128,61,0.16); }
.chip:disabled { opacity: .7; }
.status-log {
  background: #0d1420;
  color: #b7c4d6;
  border-radius: 8px;
  padding: 12px 14px;
  font-family: "SF Mono", Consolas, Menlo, monospace;
  font-size: 12px;
  max-height: 220px;
  overflow-y: auto;
  white-space: pre-wrap;
  margin: 0;
}

/* Single-line "what's happening right now" banner — screen-reader users get
   this announced via aria-live; sighted users get it without hunting for
   the status-log pane at the bottom of the tab. */
.live-status {
  display: none;
  align-items: center;
  gap: 8px;
  padding: 8px 14px;
  border-radius: 8px;
  background: var(--accent-soft);
  color: var(--text);
  font-size: 12.5px;
  font-weight: 600;
}
.live-status.visible { display: flex; }
.live-status .spinner {
  width: 13px; height: 13px;
  border-radius: 50%;
  border: 2px solid var(--accent);
  border-top-color: transparent;
  animation: spin .7s linear infinite;
  flex: none;
}
@keyframes spin { to { transform: rotate(360deg); } }

.year-checks { display: flex; flex-wrap: wrap; gap: 12px; color: var(--text-muted); font-size: 13px; }
.year-checks label { display: flex; align-items: center; gap: 6px; background: var(--surface-alt); border: 1px solid var(--border); padding: 5px 10px; border-radius: 8px; cursor: pointer; min-height: 24px; }

.plots-container { display: flex; flex-direction: column; gap: 16px; }
.plot-grid { display: flex; flex-direction: column; gap: 14px; }
.plot-box { background: var(--surface-alt); border-radius: 8px; padding: 10px; border: 1px solid var(--border); }
.plot-box .chart-wrap { position: relative; height: 400px; width: 100%; }

/* Plotly-modebar-style toolbar: small icon-button row, top-right of each
   chart, subtle until hovered so it doesn't fight the chart for attention. */
.chart-modebar {
  position: absolute; top: 2px; right: 4px; z-index: 5;
  display: flex; gap: 1px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 2px;
  opacity: .45;
  transition: opacity .15s ease;
}
.chart-wrap:hover .chart-modebar, .chart-modebar:focus-within { opacity: 1; }
.chart-modebar-btn {
  appearance: none; border: none; background: transparent;
  width: 24px; height: 24px; padding: 0;
  display: flex; align-items: center; justify-content: center;
  border-radius: 4px; cursor: pointer; color: var(--text-muted);
}
.chart-modebar-btn:hover { background: var(--accent-soft); color: var(--text); }
.chart-modebar-btn.active { background: var(--accent-contrast); color: #fff; }
.plot-box .plot-caption { font-size: 11px; color: var(--text-muted); text-align: center; margin-top: 6px; }

/* ---------- Chart selector (fuel pills + view toggle) ---------- */
.chart-selector { display: flex; flex-wrap: wrap; align-items: center; gap: 12px; margin-bottom: 16px; }
.fuel-pills { display: flex; flex-wrap: wrap; gap: 6px; }
.fuel-pill {
  appearance: none; border: 1px solid var(--border); background: var(--surface-alt);
  color: var(--text); font: inherit; font-size: 13px; font-weight: 600;
  padding: 8px 14px; border-radius: 999px; cursor: pointer; min-height: 36px;
  transition: background .15s, border-color .15s, color .15s;
}
.fuel-pill:hover { border-color: var(--accent); }
.fuel-pill.active { background: var(--accent-contrast); border-color: var(--accent-contrast); color: #fff; }

.view-toggle {
  display: inline-flex; background: var(--surface-alt); border: 1px solid var(--border);
  border-radius: 9px; padding: 3px; gap: 2px;
}
.view-toggle-btn {
  appearance: none; border: none; background: transparent;
  display: inline-flex; align-items: center; gap: 6px;
  font: inherit; font-size: 12.5px; font-weight: 600; color: var(--text-muted);
  padding: 7px 12px; border-radius: 7px; cursor: pointer; min-height: 32px;
  transition: background .15s, color .15s;
}
.view-toggle-btn:hover { color: var(--text); }
.view-toggle-btn.active { background: var(--surface); color: var(--accent); box-shadow: var(--shadow-sm); }
.chart-selector .btn { margin-left: auto; }

/* Secondary filter row: Year pills + HDD/CDD pills. Each group collapses to
   nothing (no gap/border) when empty — e.g. year pills only appear once
   there's more than one analysis to choose between ("if applicable"). */
.chart-subselector { display: flex; flex-wrap: wrap; gap: 16px; margin: -6px 0 16px; }
.chart-subselector .fuel-pills:empty { display: none; }
.chart-subselector .fuel-pill { font-size: 12px; padding: 6px 12px; min-height: 30px; }

.app-footer {
  text-align: right;
  padding: 10px 24px;
  color: var(--text-muted);
  font-size: 11px;
}

/* ---------- Modal ---------- */
.modal-backdrop {
  position: fixed; inset: 0;
  background: rgba(15, 23, 42, 0.5);
  display: flex; align-items: center; justify-content: center;
  z-index: 100;
  padding: 24px;
}
.modal {
  background: var(--surface);
  border-radius: var(--radius);
  box-shadow: var(--shadow-md);
  max-width: 900px; width: 100%;
  max-height: 85vh;
  display: flex; flex-direction: column;
  overflow: hidden;
}
.modal-header {
  display: flex; align-items: center; justify-content: space-between;
  padding: 16px 20px;
  border-bottom: 1px solid var(--border);
}
.modal-header h3 { margin: 0; font-size: 15px; }
.modal-body { padding: 18px 20px; overflow-y: auto; }
.modal-footer { padding: 12px 20px; border-top: 1px solid var(--border); display: flex; justify-content: flex-end; gap: 8px; }
.modal-body pre.report { white-space: pre-wrap; font-family: "SF Mono", Consolas, Menlo, monospace; font-size: 12.5px; line-height: 1.6; }

/* Severity tint so warning/error dialogs are visually distinct from plain info, not just by wording */
.modal[data-variant="danger"] .modal-header { border-bottom: 2px solid var(--danger); }
.modal[data-variant="warning"] .modal-header { border-bottom: 2px solid var(--warning); }
.modal[data-variant="success"] .modal-header { border-bottom: 2px solid var(--success); }

.sheet-row { display: grid; grid-template-columns: 1fr 90px 90px 220px; gap: 10px; align-items: center; padding: 6px 0; border-bottom: 1px solid var(--border); }
.sheet-row.header-row { font-weight: 700; color: var(--text-muted); font-size: 12px; }
/* The checkbox + sheet name are wrapped in one label so the whole row (not
   just the tiny native checkbox) is a valid click/tap target — WCAG 2.5.8 */
.sheet-import-label { display: flex; align-items: center; gap: 8px; cursor: pointer; min-height: 24px; }

@media (max-width: 720px) {
  .app-header { flex-direction: column; align-items: flex-start; padding: 12px 16px; position: static; }
  .brand .subtitle { display: none; }
  .tab-bar { padding: 0 4px; scrollbar-width: none; }
  .tab-btn { padding: 12px 10px; }
  .tab-content { padding: 16px 16px 48px; }
  .card { padding: 14px 16px; }
  /* Fields stack two-up / full-width instead of overflowing the viewport */
  .field-row label { flex: 1 1 140px; }
  .field-row input, .field-row select { width: 100%; min-width: 0; }
  .field-row > .btn { width: 100%; justify-content: center; }
  .chart-selector .btn { margin-left: 0; }
  .view-toggle { flex-wrap: wrap; }
  .sheet-row { grid-template-columns: 1fr; row-gap: 4px; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation-duration: 0s !important; transition-duration: 0s !important; }
  .live-status .spinner { animation: spin 1.5s linear infinite !important; }
}
