@import url('https://fonts.googleapis.com/css2?family=IBM+Plex+Mono:wght@400;500;600&family=Manrope:wght@400;500;600;700;800&display=swap');

:root {
  --canvas: #f3f1e9;
  --surface: #fffef8;
  --surface-raised: #ffffff;
  --surface-sunken: #e7e6dd;
  --surface-selected: #e7f0c5;
  --text-primary: #111612;
  --text-secondary: #404b44;
  --text-muted: #58635b;
  --border-subtle: #c5c9c1;
  --border-strong: #69736c;
  --interactive: #173d34;
  --interactive-hover: #0f3029;
  --interactive-pressed: #0b2721;
  --on-interactive: #ffffff;
  --focus-ring: #2f6fff;
  --selection-indicator: #b8d936;
  --status-positive: #164f3f;
  --status-positive-fill: #dcefe7;
  --status-caution: #694300;
  --status-caution-fill: #f7e7bd;
  --status-critical: #7b2525;
  --status-critical-fill: #f6dede;
  --status-neutral: #3d4942;
  --status-neutral-fill: #e3e7e3;
  --chart-solar: #00785d;
  --chart-load: #b94a00;
  --chart-soc: #111612;
  --chart-baseline: #68726b;
  --accent: #d8f763;
  --white: #fffef8;
  --radius-sm: 3px;
  --space-1: 6px;
  --space-2: 10px;
  --space-3: 14px;
  --space-4: 18px;
  --space-5: 24px;
  --space-6: 32px;
}

* { box-sizing: border-box; }

html { scroll-behavior: smooth; }

body {
  margin: 0;
  min-width: 320px;
  min-height: 100vh;
  color: var(--text-primary);
  background: var(--canvas);
  font-family: "Manrope", "Avenir Next", sans-serif;
}

button, input, select { font: inherit; }

a, button, input, select, summary { -webkit-tap-highlight-color: transparent; }

:focus-visible {
  outline: 2px solid var(--white);
  box-shadow: 0 0 0 4px var(--focus-ring);
}

.masthead {
  position: sticky;
  z-index: 10;
  top: 0;
  min-height: 70px;
  padding: 12px 22px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  color: var(--white);
  background: rgba(17, 22, 18, .97);
  border-bottom: 1px solid #49534d;
  backdrop-filter: blur(14px);
}

.brand { display: flex; gap: 13px; align-items: center; color: inherit; text-decoration: none; }
.brand strong { display: block; font-size: 15px; letter-spacing: .12em; }
.brand small { display: block; margin-top: 2px; color: #b8c0bb; font: 11px "IBM Plex Mono", monospace; letter-spacing: .06em; }
.brand-mark { width: 36px; height: 36px; display: grid; place-content: center; gap: 3px; border: 1px solid #819088; transform: rotate(-8deg); }
.brand-mark i { width: 18px; height: 2px; display: block; background: var(--accent); }
.brand-mark i:nth-child(2) { width: 12px; }
.brand-mark i:nth-child(3) { width: 7px; }
.mast-status { display: flex; gap: 18px; align-items: center; font: 11px "IBM Plex Mono", monospace; color: #c5ccc8; }
.home-setup-link { min-height: 44px; display: inline-flex; align-items: center; color: var(--accent); text-underline-offset: 4px; }
.mode-badge { padding: 8px 10px; color: var(--accent); border: 1px solid #667369; font-weight: 600; letter-spacing: .04em; }

.workspace {
  display: grid;
  grid-template-columns: minmax(330px, 390px) minmax(0, 1fr);
  min-height: calc(100vh - 70px);
}

.controls {
  padding: 30px 24px 70px;
  background: var(--surface-sunken);
  border-right: 1px solid var(--border-subtle);
}

.panel-intro { padding-bottom: 22px; }
.eyebrow { display: block; color: var(--text-muted); font: 600 11px "IBM Plex Mono", monospace; letter-spacing: .1em; }
h1, h2, h3 { text-wrap: balance; }
.panel-intro h2 { margin: 10px 0 9px; font-size: clamp(25px, 2.5vw, 35px); line-height: 1.02; letter-spacing: -.04em; }
.panel-intro p, .microcopy, .source-note, .section-note, .chart-description { color: var(--text-muted); font-size: 12px; line-height: 1.55; }

.scenario-disclosure { border-top: 1px solid var(--border-subtle); }
.scenario-disclosure > summary {
  min-height: 58px;
  padding: 12px 0;
  display: grid;
  align-content: center;
  cursor: pointer;
  list-style: none;
  font-size: 13px;
  font-weight: 800;
}
.scenario-disclosure > summary::-webkit-details-marker, .advanced > summary::-webkit-details-marker { display: none; }
.scenario-disclosure > summary::after, .advanced > summary::after { content: "+"; grid-column: 2; grid-row: 1 / span 2; align-self: center; font: 600 18px "IBM Plex Mono", monospace; }
.scenario-disclosure[open] > summary::after, .advanced[open] > summary::after { content: "−"; }
.scenario-disclosure > summary small { margin-top: 4px; color: var(--text-muted); font-size: 11px; font-weight: 500; }

fieldset { margin: 0; padding: 21px 0; border: 0; border-top: 1px solid var(--border-subtle); }
legend { padding: 0; margin-bottom: 14px; font-size: 13px; font-weight: 800; }
label { position: relative; display: grid; gap: 7px; color: var(--text-secondary); font: 500 11px "IBM Plex Mono", monospace; text-transform: uppercase; letter-spacing: .025em; }
.unit { position: absolute; top: 0; right: 0; color: var(--text-muted); font-weight: 400; text-transform: none; }
input, select {
  width: 100%;
  min-width: 0;
  height: 46px;
  padding: 0 11px;
  border: 1px solid var(--border-strong);
  border-radius: var(--radius-sm);
  outline: 0;
  color: var(--text-primary);
  background: var(--surface);
  font-family: "Manrope", sans-serif;
  font-size: 14px;
  text-transform: none;
}
input:hover, select:hover { border-color: var(--text-primary); }
input:focus, select:focus { border-color: var(--text-primary); }
input[data-source="live"] { border-left: 4px solid var(--status-positive); background: var(--status-positive-fill); }
input[readonly] { color: var(--text-primary); cursor: default; }
.input-source { min-height: 28px; color: var(--text-muted); font: 9px/1.4 "IBM Plex Mono", monospace; text-transform: none; letter-spacing: 0; }
.input-source[data-source="live"] { color: var(--status-positive); font-weight: 600; }
.field-pair, .field-triple { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 13px; }
.field-triple { grid-template-columns: repeat(3, minmax(0, 1fr)); }
.compact { margin-top: 13px; }

.segmented { display: grid; grid-template-columns: 1fr 1fr; padding: 3px; background: #ccd0c9; border: 1px solid var(--border-strong); border-radius: var(--radius-sm); }
.segmented label { display: block; }
.segmented input { position: absolute; width: 1px; height: 1px; opacity: 0; }
.segmented span { display: grid; min-height: 44px; place-items: center; color: var(--text-secondary); cursor: pointer; }
.segmented input:checked + span { color: var(--white); background: var(--interactive); font-weight: 700; }
.segmented input:focus-visible + span { outline: 2px solid var(--white); box-shadow: 0 0 0 4px var(--focus-ring); }

.live-connection { margin-top: 13px; padding: 14px; border: 1px solid var(--border-strong); background: var(--surface); }
.live-connection > label + .field-pair, .provider-extra { margin-top: 13px; }
.live-connection .button { margin-top: 12px; }
.live-connection .microcopy[data-state="loading"] { color: var(--status-caution); }
.live-connection .microcopy[data-state="success"] { color: var(--status-positive); }
.live-connection .microcopy[data-state="error"] { color: var(--status-critical); font-weight: 600; }
.provider-extra { padding-top: 13px; border-top: 1px solid var(--border-subtle); }
.provider-extra .field-pair + .field-pair { margin-top: 13px; }
.connector-diagnostics { margin-top: 10px; border: 1px solid var(--border-subtle); background: var(--surface-sunken); }
.connector-diagnostics summary { min-height: 38px; padding: 0 10px; display: flex; align-items: center; cursor: pointer; color: var(--text-secondary); font: 700 11px "IBM Plex Mono", monospace; text-transform: uppercase; }
.connector-diagnostics pre { max-height: 240px; margin: 0; padding: 10px; overflow: auto; border-top: 1px solid var(--border-subtle); color: var(--text-secondary); font: 10px/1.45 "IBM Plex Mono", monospace; white-space: pre-wrap; overflow-wrap: anywhere; }
.live-readings { margin-top: 14px; padding-top: 13px; border-top: 1px solid var(--border-subtle); }
.live-readings-head { display: flex; justify-content: space-between; gap: 10px; align-items: baseline; }
.live-readings-head strong { font-size: 12px; }
.live-readings-head small { color: var(--text-muted); font: 9px "IBM Plex Mono", monospace; text-align: right; }
.live-readings dl { margin: 11px 0 0; display: grid; grid-template-columns: 1fr 1fr; gap: 1px; border: 1px solid var(--border-subtle); background: var(--border-subtle); }
.live-readings dl div { min-width: 0; padding: 10px; background: var(--status-positive-fill); }
.live-readings dt { color: var(--text-muted); font: 9px "IBM Plex Mono", monospace; text-transform: uppercase; }
.live-readings dd { margin: 5px 0 0; font-size: 15px; font-weight: 800; }
.check { min-height: 44px; margin-top: 10px; display: flex; align-items: center; gap: 10px; color: var(--text-primary); font-family: "Manrope", sans-serif; font-size: 13px; text-transform: none; }
.check > input { width: 19px; height: 19px; flex: 0 0 auto; accent-color: var(--interactive); }
.inline-input { display: inline-block; width: 70px; height: 34px; margin: 0 3px; padding: 0 6px; }

.advanced { margin: 18px 0 0; border: 1px solid var(--border-strong); background: rgba(255, 254, 248, .42); }
.advanced > summary { min-height: 50px; padding: 0 13px; display: grid; grid-template-columns: 1fr auto; align-items: center; cursor: pointer; list-style: none; font-size: 12px; font-weight: 800; }
.advanced fieldset { padding-left: 13px; padding-right: 13px; }
.advanced label + label:not(.check) { margin-top: 13px; }

.button { min-height: 44px; border: 0; border-radius: var(--radius-sm); cursor: pointer; font-weight: 800; }
.button:disabled { opacity: .65; cursor: wait; }
.button.secondary { padding: 0 14px; color: var(--on-interactive); background: var(--interactive); }
.button.secondary:hover { background: var(--interactive-hover); }
.form-action { position: sticky; bottom: 0; z-index: 2; padding: 18px 0 max(10px, env(safe-area-inset-bottom)); background: linear-gradient(to bottom, rgba(231,230,221,0), var(--surface-sunken) 20%); }
.button.primary { width: 100%; min-height: 54px; padding: 0 18px; color: var(--on-interactive); background: var(--interactive); transition: background .15s ease; }
.button.primary:hover { background: var(--interactive-hover); }
.button.primary:active { background: var(--interactive-pressed); }
.form-action p { margin: 8px 0 0; color: var(--text-muted); text-align: center; font: 10px "IBM Plex Mono", monospace; }
.form-error { min-height: 18px; margin: 0; color: var(--status-critical); font-size: 12px; }

.results { min-width: 0; padding: 36px clamp(20px, 4vw, 58px) 80px; }
.results-head { display: flex; justify-content: space-between; gap: 22px; align-items: end; margin-bottom: 22px; }
.results h1 { margin: 10px 0 0; font-size: clamp(31px, 4.2vw, 54px); line-height: .99; letter-spacing: -.055em; }
.results h2 { margin: 7px 0 0; font-size: 20px; letter-spacing: -.025em; }
.run-meta { max-width: 220px; text-align: right; font-family: "IBM Plex Mono", monospace; }
.run-meta span { display: block; font-size: 11px; font-weight: 600; letter-spacing: .06em; }
.run-meta small { display: block; margin-top: 6px; color: var(--text-muted); font-size: 11px; line-height: 1.45; }

.context-strip { margin-bottom: 18px; padding: 12px 0; display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); border-top: 1px solid var(--border-strong); border-bottom: 1px solid var(--border-subtle); }
.context-strip > span { min-width: 0; padding: 0 13px; border-left: 1px solid var(--border-subtle); }
.context-strip > span:first-child { padding-left: 0; border-left: 0; }
.context-strip small, .context-strip strong { display: block; }
.context-strip small { color: var(--text-muted); font: 10px "IBM Plex Mono", monospace; text-transform: uppercase; }
.context-strip strong { margin-top: 5px; overflow-wrap: anywhere; font-size: 12px; }

.loading { min-height: 340px; display: grid; place-content: center; justify-items: center; gap: 18px; color: var(--text-muted); font: 12px "IBM Plex Mono", monospace; }
.loading i { width: 54px; height: 54px; border: 2px solid var(--border-subtle); border-top-color: var(--interactive); border-radius: 50%; animation: spin .8s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

.decision-gate { margin-bottom: 18px; padding: 22px; display: grid; grid-template-columns: 130px minmax(240px, 1fr) minmax(190px, .6fr); gap: 22px; align-items: center; border: 1px solid var(--border-strong); border-left-width: 6px; background: var(--status-neutral-fill); }
.decision-gate[data-state="ready"] { border-left-color: var(--status-positive); background: var(--status-positive-fill); }
.decision-gate[data-state="review"] { border-left-color: var(--status-caution); background: var(--status-caution-fill); }
.decision-gate[data-state="blocked"] { border-left-color: var(--status-critical); background: var(--status-critical-fill); }
.gate-label span, .next-action span { display: block; color: var(--text-muted); font: 600 10px "IBM Plex Mono", monospace; letter-spacing: .08em; }
.gate-label strong { display: block; margin-top: 6px; font: 700 13px "IBM Plex Mono", monospace; }
.decision-gate[data-state="ready"] .gate-label strong { color: var(--status-positive); }
.decision-gate[data-state="review"] .gate-label strong { color: var(--status-caution); }
.decision-gate[data-state="blocked"] .gate-label strong { color: var(--status-critical); }
.decision-gate h2 { margin: 0; font-size: clamp(22px, 2.4vw, 31px); line-height: 1.05; }
.decision-gate p { margin: 7px 0 0; color: var(--text-secondary); font-size: 13px; line-height: 1.5; }
.next-action { padding-left: 20px; border-left: 1px solid var(--border-strong); }
.next-action strong { display: block; margin-top: 7px; font-size: 13px; line-height: 1.45; }

.metric-grid { display: grid; grid-template-columns: 1.55fr repeat(3, 1fr); border: 1px solid var(--border-strong); background: var(--border-strong); gap: 1px; }
.metric { min-height: 140px; padding: 18px; background: var(--surface); }
.metric span { display: block; color: var(--text-muted); font: 10px "IBM Plex Mono", monospace; text-transform: uppercase; line-height: 1.4; }
.metric strong { display: block; margin-top: 25px; font-size: clamp(22px, 2.2vw, 34px); line-height: 1; letter-spacing: -.045em; }
.metric small { display: block; margin-top: 8px; color: var(--text-muted); font-size: 11px; line-height: 1.4; }
.hero-metric { color: var(--accent); background: var(--text-primary); }
.hero-metric span, .hero-metric small { color: #c4cbc6; }

.reserve-card { margin-top: 18px; padding: 24px; display: grid; grid-template-columns: 1.1fr 1fr; gap: 40px; align-items: center; border: 1px solid var(--border-subtle); background: var(--surface-sunken); }
.reserve-card p { max-width: 640px; margin: 9px 0 0; color: var(--text-secondary); font-size: 12px; line-height: 1.55; }
.reserve-card .reserve-rule { padding-left: 10px; border-left: 3px solid var(--border-strong); color: var(--text-muted); }
.gauge-track { position: relative; height: 16px; overflow: visible; background: #b7beb7; border: 1px solid var(--border-strong); }
.gauge-track i { display: block; width: 0; height: 100%; background: var(--status-positive); transition: width .5s ease; }
.gauge-track b { position: absolute; top: -7px; width: 3px; height: 28px; background: var(--text-primary); transition: left .5s ease; }
.gauge-track b::after { content: "START"; position: absolute; top: -18px; left: 50%; transform: translateX(-50%); color: var(--text-primary); font: 9px "IBM Plex Mono", monospace; }
.gauge-labels { display: flex; justify-content: space-between; margin-top: 11px; color: var(--text-muted); font: 10px "IBM Plex Mono", monospace; }
.gauge-labels strong { color: var(--text-primary); }

.chart-card, .decision-card, .audit-card { min-width: 0; margin-top: 18px; border: 1px solid var(--border-subtle); background: var(--surface); }
.chart-card { padding: 22px; }
.card-head { display: flex; justify-content: space-between; align-items: end; gap: 18px; }
.legend { display: flex; flex-wrap: wrap; justify-content: flex-end; gap: 12px; color: var(--text-secondary); font: 10px "IBM Plex Mono", monospace; }
.legend span::before { content: ""; width: 18px; height: 3px; margin-right: 6px; display: inline-block; vertical-align: middle; background: var(--chart-soc); }
.legend .solar-key::before { background: var(--chart-solar); }
.legend .load-key::before { background: var(--chart-load); }
.legend .soc-key::before { background: var(--chart-soc); }
.legend .base-key::before { height: 2px; background: repeating-linear-gradient(90deg, var(--chart-baseline) 0 5px, transparent 5px 8px); }
.chart-description { margin: 10px 0 0; }
.chart-wrap { position: relative; width: 100%; margin-top: 12px; }
#energyChart { display: block; width: 100%; height: 390px; }
.chart-note { display: flex; flex-wrap: wrap; gap: 18px; color: var(--text-muted); font: 10px "IBM Plex Mono", monospace; }
.chart-note i { display: inline-block; width: 12px; height: 12px; margin-right: 5px; vertical-align: -2px; border: 1px solid var(--border-strong); }
.peak-swatch { background: #f3d7c5; }
.outage-swatch { background: #efd0d0; }

.detail-grid { display: grid; grid-template-columns: minmax(0, 1.4fr) minmax(270px, .6fr); gap: 18px; }
.decision-card, .audit-card { padding: 22px; }
.section-note { margin: 8px 0 16px; }
.table-wrap { width: 100%; max-width: 100%; overflow-x: auto; border-top: 1px solid var(--border-strong); }
table { width: 100%; min-width: 700px; border-collapse: collapse; font-size: 11px; }
th { padding: 10px 8px; color: var(--text-muted); text-align: left; font: 600 10px "IBM Plex Mono", monospace; text-transform: uppercase; }
td { padding: 12px 8px; border-top: 1px solid var(--border-subtle); vertical-align: top; line-height: 1.45; }
td:nth-child(-n+5) { white-space: nowrap; }
td time, td .mono { font-family: "IBM Plex Mono", monospace; }
td .control-label { font-weight: 800; }

.audit-card dl { margin: 18px 0; }
.audit-card dl div { padding: 11px 0; display: flex; justify-content: space-between; gap: 18px; border-bottom: 1px solid var(--border-subtle); }
.audit-card dt { color: var(--text-muted); font-size: 11px; line-height: 1.45; }
.audit-card dd { margin: 0; text-align: right; font: 600 11px "IBM Plex Mono", monospace; line-height: 1.45; }
.audit-card a { display: inline-flex; min-height: 44px; align-items: center; color: var(--interactive); font-size: 12px; font-weight: 800; text-underline-offset: 3px; }
.source-note { margin: 12px 0 0; padding-top: 12px; border-top: 1px solid var(--border-subtle); }

.empty-state { min-height: 430px; display: grid; place-content: center; justify-items: start; border-top: 1px solid var(--border-subtle); }
.empty-state h2 { font-size: 28px; }
.empty-state p { max-width: 460px; color: var(--text-muted); font-size: 13px; line-height: 1.6; }

[hidden] { display: none !important; }

@media (max-width: 1120px) {
  .workspace { grid-template-columns: 340px minmax(0, 1fr); }
  .metric-grid { grid-template-columns: repeat(2, 1fr); }
  .detail-grid, .reserve-card { grid-template-columns: 1fr; }
  .decision-gate { grid-template-columns: 110px 1fr; }
  .next-action { grid-column: 2; padding: 12px 0 0; border-left: 0; border-top: 1px solid var(--border-strong); }
  .context-strip { grid-template-columns: repeat(2, 1fr); row-gap: 14px; }
  .context-strip > span:nth-child(3) { padding-left: 0; border-left: 0; }
}

@media (max-width: 900px) {
  .masthead { position: relative; padding: 12px 15px; }
  .mast-status .mode-badge { display: none; }
  .workspace { display: block; }
  .controls { padding: 24px 18px 20px; border-right: 0; border-bottom: 1px solid var(--border-strong); }
  .panel-intro { padding-bottom: 16px; }
  .panel-intro h2 { font-size: 27px; }
  .scenario-disclosure:not([open]) { border-bottom: 1px solid var(--border-subtle); }
  .results { padding: 28px 16px 60px; }
  .results-head { align-items: start; }
  .results h1 { font-size: clamp(32px, 8vw, 45px); }
  .decision-gate { grid-template-columns: 1fr; gap: 14px; }
  .next-action { grid-column: 1; }
  .metric-grid { grid-template-columns: 1fr 1fr; }
  .metric { min-height: 128px; }
  .chart-card { padding: 18px 12px; }
  .card-head { display: block; }
  .legend { margin-top: 13px; justify-content: flex-start; }
  .chart-wrap { overflow-x: auto; }
  #energyChart { min-width: 680px; }
  .form-action { position: static; background: transparent; }
}

@media (max-width: 520px) {
  .masthead { min-height: 64px; }
  .brand small, .mast-status { display: none; }
  .field-pair, .field-triple { grid-template-columns: 1fr; }
  .results-head { display: block; }
  .run-meta { margin-top: 15px; max-width: none; text-align: left; }
  .context-strip { grid-template-columns: 1fr 1fr; }
  .context-strip > span { padding: 0 9px; }
  .context-strip > span:nth-child(odd) { padding-left: 0; border-left: 0; }
  .metric-grid { grid-template-columns: 1fr; }
  .metric { min-height: 118px; }
  .metric strong { margin-top: 18px; }
  .reserve-card, .decision-card, .audit-card { padding: 18px; }
  .reserve-card { gap: 28px; }
}

@media (prefers-reduced-motion: reduce) {
  html { scroll-behavior: auto; }
  *, *::before, *::after { scroll-behavior: auto !important; transition-duration: .01ms !important; animation-duration: .01ms !important; animation-iteration-count: 1 !important; }
}
