/* ---------- Theme tokens ---------- */
:root {
  --bg: #f4f6f5; --panel: #fff; --panel-2: #f8faf9;
  --ink: #14201c; --ink-2: #4a5753; --ink-3: #7b8783;
  --line: #e1e6e3; --line-2: #eef1ef;
  --accent: #0f766e; --accent-ink: #fff; --accent-soft: #e2f3f0;
  --solar: #e8a317; --grid: #3b6fd8; --dis: #14a37f; --chg: #8b5cf6; --tariff: #d9480f; --demand: #14201c;
  --danger: #c92a2a; --danger-soft: #fdecec; --ok: #2b8a3e; --warn: #b7791f;
  --shadow: 0 1px 2px rgba(16,24,22,.05), 0 6px 18px rgba(16,24,22,.05);
  --mono: "JetBrains Mono", ui-monospace, Menlo, monospace;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #0d1311; --panel: #151d1a; --panel-2: #1a2420; --ink: #e7eeeb; --ink-2: #aebbb6; --ink-3: #7d8b86;
    --line: #26322e; --line-2: #1f2a26; --accent: #2dd4bf; --accent-ink: #062b27; --accent-soft: #123430;
    --solar: #f2b84b; --grid: #6b9cf5; --dis: #34d399; --chg: #a78bfa; --tariff: #ff8a5c; --demand: #e7eeeb;
    --danger: #ff6b6b; --danger-soft: #3a1a1a; --ok: #51cf66; --warn: #fcc419;
    --shadow: 0 1px 2px rgba(0,0,0,.3), 0 6px 20px rgba(0,0,0,.25); color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #0d1311; --panel: #151d1a; --panel-2: #1a2420; --ink: #e7eeeb; --ink-2: #aebbb6; --ink-3: #7d8b86;
  --line: #26322e; --line-2: #1f2a26; --accent: #2dd4bf; --accent-ink: #062b27; --accent-soft: #123430;
  --solar: #f2b84b; --grid: #6b9cf5; --dis: #34d399; --chg: #a78bfa; --tariff: #ff8a5c; --demand: #e7eeeb;
  --danger: #ff6b6b; --danger-soft: #3a1a1a; --ok: #51cf66; --warn: #fcc419;
  --shadow: 0 1px 2px rgba(0,0,0,.3), 0 6px 20px rgba(0,0,0,.25); color-scheme: dark;
}

/* ---------- Base ---------- */
* { box-sizing: border-box; }
html, body { margin: 0; }
body { background: var(--bg); color: var(--ink); font: 14px/1.55 Inter, system-ui, -apple-system, "Segoe UI", sans-serif; -webkit-font-smoothing: antialiased; }
button, input, select, textarea { font: inherit; color: inherit; }
h1, h2, h3 { margin: 0; letter-spacing: -.01em; }
h2 { font-size: 15px; font-weight: 600; }
h3 { font-size: 12px; font-weight: 600; text-transform: uppercase; letter-spacing: .06em; color: var(--ink-3); margin: 18px 0 8px; }
a { color: var(--accent); }
.muted { color: var(--ink-3); }
.small { font-size: 12px; }
.center { text-align: center; }
.spacer { flex: 1; }
.mono { font-family: var(--mono); }

/* ---------- Header ---------- */
.top { position: sticky; top: 0; z-index: 20; background: color-mix(in srgb, var(--bg) 85%, transparent); backdrop-filter: blur(10px); border-bottom: 1px solid var(--line); }
.top-in { max-width: 1320px; margin: 0 auto; padding: 10px 20px; display: flex; align-items: center; gap: 10px; }
.brand { display: flex; align-items: center; gap: 9px; font-weight: 700; font-size: 16px; }
.brand-mark { width: 30px; height: 30px; border-radius: 8px; background: #0f766e; display: grid; place-items: center; }
.brand-sub { color: var(--accent); }
.pill { display: inline-flex; align-items: center; gap: 7px; padding: 4px 11px; border-radius: 999px; border: 1px solid var(--line); background: var(--panel); font-size: 12.5px; color: var(--ink-2); white-space: nowrap; }
.dot { width: 8px; height: 8px; border-radius: 50%; background: var(--warn); animation: pulse 1.2s infinite; }
.dot.ok { background: var(--ok); animation: none; box-shadow: 0 0 0 3px color-mix(in srgb, var(--ok) 25%, transparent); }
.dot.bad { background: var(--danger); animation: none; }
@keyframes pulse { 50% { opacity: .35; } }
.btn-ghost { border: 1px solid var(--line); background: var(--panel); border-radius: 9px; height: 32px; padding: 0 11px; cursor: pointer; display: inline-flex; align-items: center; color: var(--ink-2); font-size: 13px; text-decoration: none; }
.btn-ghost:hover { color: var(--ink); border-color: var(--ink-3); }

/* ---------- Intro + pipeline ---------- */
.page { max-width: 1320px; margin: 0 auto; padding: 24px 20px 48px; }
.intro h1 { font-size: clamp(22px, 3vw, 30px); font-weight: 700; }
.intro p { max-width: 760px; color: var(--ink-2); margin: 8px 0 16px; }
.pipeline { list-style: none; margin: 0 0 22px; padding: 0; display: flex; flex-wrap: wrap; gap: 8px; }
.pipeline li { display: flex; align-items: center; gap: 8px; padding: 6px 12px 6px 6px; border-radius: 999px; background: var(--panel); border: 1px solid var(--line); font-size: 13px; color: var(--ink-2); transition: all .2s; }
.pipeline li b { width: 22px; height: 22px; border-radius: 50%; display: grid; place-items: center; background: var(--line-2); font-size: 11.5px; }
.pipeline li.done { border-color: color-mix(in srgb, var(--accent) 40%, var(--line)); color: var(--ink); }
.pipeline li.done b { background: var(--accent); color: var(--accent-ink); }
.pipeline li.active { border-color: var(--accent); color: var(--ink); box-shadow: 0 0 0 3px var(--accent-soft); }
.pipeline li.active b { background: var(--accent-soft); color: var(--accent); animation: pulse 1s infinite; }
.pipeline li.fail { border-color: var(--danger); color: var(--danger); }
.pipeline li.fail b { background: var(--danger); color: #fff; }

/* ---------- Layout + cards ---------- */
.layout { display: grid; grid-template-columns: minmax(320px, 400px) 1fr; gap: 20px; align-items: start; }
.card { background: var(--panel); border: 1px solid var(--line); border-radius: 14px; box-shadow: var(--shadow); padding: 18px; }
.card-head { display: flex; align-items: center; justify-content: space-between; gap: 12px; flex-wrap: wrap; margin-bottom: 12px; }
.inputs { position: sticky; top: 72px; }
.inputs h3:first-of-type { margin-top: 4px; }
.results { display: grid; gap: 16px; min-width: 0; }
select { border: 1px solid var(--line); background: var(--panel-2); border-radius: 9px; padding: 6px 10px; max-width: 100%; cursor: pointer; }
.field-head { display: flex; align-items: baseline; justify-content: space-between; }
.field-head h3 { margin-bottom: 8px; }

/* Notes */
.note { position: relative; margin-bottom: 8px; }
.note textarea { width: 100%; resize: none; overflow: hidden; border: 1px solid var(--line); background: var(--panel-2); border-radius: 10px; padding: 9px 30px 9px 34px; min-height: 44px; line-height: 1.45; }
.note textarea:focus, select:focus { outline: none; border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft); }
.note .idx { position: absolute; left: 10px; top: 10px; font: 500 11px var(--mono); color: var(--ink-3); }
.note .rm { position: absolute; right: 6px; top: 7px; border: 0; background: none; color: var(--ink-3); cursor: pointer; font-size: 16px; line-height: 1; padding: 4px; }
.note .rm:hover { color: var(--danger); }
.btn-link { border: 0; background: none; color: var(--accent); cursor: pointer; padding: 2px 0; font-weight: 500; }
.btn-link[disabled] { color: var(--ink-3); cursor: default; }

/* Battery stats */
.battery { display: grid; grid-template-columns: repeat(3, 1fr); gap: 6px; }
.battery div { background: var(--panel-2); border: 1px solid var(--line-2); border-radius: 9px; padding: 6px 9px; }
.battery span { display: block; font-size: 11px; color: var(--ink-3); }
.battery b { font-weight: 600; font-size: 13.5px; }

/* Profile mini chart */
.profile svg { display: block; width: 100%; height: 90px; }
.legend { display: flex; flex-wrap: wrap; align-items: center; gap: 4px 12px; color: var(--ink-2); }
.inputs .legend { margin-top: 6px; }
.inputs .sw.demand { opacity: .22; }
.sw { display: inline-block; width: 10px; height: 10px; border-radius: 3px; margin-right: -6px; vertical-align: -1px; }
.sw.line { height: 3px; border-radius: 2px; width: 12px; vertical-align: 3px; }
.sw.demand { background: var(--demand); } .sw.solar { background: var(--solar); } .sw.grid { background: var(--grid); }
.sw.dis { background: var(--dis); } .sw.chg { background: var(--chg); } .sw.tariff { background: var(--tariff); }
.sw.reserve { background: color-mix(in srgb, var(--accent) 18%, transparent); border: 1px dashed var(--accent); }

/* Run button */
.btn-primary { margin-top: 20px; width: 100%; height: 44px; border: 0; border-radius: 11px; background: var(--accent); color: var(--accent-ink); font-weight: 600; font-size: 15px; cursor: pointer; display: flex; align-items: center; justify-content: center; gap: 10px; transition: filter .15s; }
.btn-primary:hover { filter: brightness(1.08); }
.btn-primary[disabled] { opacity: .7; cursor: progress; }
.spinner { display: none; width: 16px; height: 16px; border-radius: 50%; border: 2px solid currentColor; border-right-color: transparent; animation: spin .7s linear infinite; }
.btn-primary.busy .spinner { display: block; }
@keyframes spin { to { transform: rotate(360deg); } }
#runHint { margin: 8px 0 0; }

/* ---------- Results ---------- */
.banner { border: 1px solid var(--danger); background: var(--danger-soft); color: var(--danger); border-radius: 12px; padding: 12px 14px; }
.source { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; color: var(--ink-2); font-size: 13px; }
.tag { display: inline-flex; align-items: center; gap: 6px; padding: 3px 10px; border-radius: 999px; font-size: 12px; font-weight: 600; }
.tag.live { background: var(--accent-soft); color: var(--accent); }
.tag.saved { background: var(--line-2); color: var(--ink-2); }
.tag.stale { background: color-mix(in srgb, var(--warn) 18%, transparent); color: var(--warn); }

.kpis { display: grid; grid-template-columns: repeat(4, 1fr); gap: 12px; }
.kpi { background: var(--panel); border: 1px solid var(--line); border-radius: 14px; box-shadow: var(--shadow); padding: 14px 16px; }
.kpi span { font-size: 12px; color: var(--ink-3); }
.kpi b { display: block; font-size: 22px; font-weight: 700; letter-spacing: -.02em; margin-top: 2px; }
.kpi small { color: var(--ink-3); font-size: 12px; }
.kpi.good b { color: var(--ok); }

.directives { display: grid; gap: 10px; }
.dir { border: 1px solid var(--line); border-radius: 12px; padding: 12px 14px; background: var(--panel-2); }
.dir.noop { opacity: .8; }
.dir-top { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }
.dir-note { margin: 6px 0 8px; color: var(--ink-2); font-style: italic; }
.badge { font: 600 11.5px var(--mono); padding: 2px 8px; border-radius: 6px; background: var(--accent-soft); color: var(--accent); }
.dir.noop .badge { background: var(--line-2); color: var(--ink-3); }
.params { font: 12px var(--mono); color: var(--ink-2); }
.strip { display: grid; grid-template-columns: repeat(24, 1fr); gap: 2px; margin-top: 6px; }
.strip i { height: 10px; border-radius: 2px; background: var(--line-2); }
.strip i.on { background: var(--accent); }
.strip-labels { display: flex; justify-content: space-between; font-size: 10.5px; color: var(--ink-3); margin-top: 3px; }
.dir-why { margin-top: 8px; font-size: 12.5px; color: var(--ink-3); }

.chart { position: relative; }
.chart svg { display: block; width: 100%; }
.chart .hover-col { fill: var(--ink); opacity: 0; }
.chart .hover-col:hover { opacity: .06; }
.tooltip { position: fixed; z-index: 50; pointer-events: none; background: var(--panel); border: 1px solid var(--line); box-shadow: var(--shadow); border-radius: 10px; padding: 8px 10px; font-size: 12px; min-width: 150px; }
.tooltip b { display: block; margin-bottom: 3px; }
.tooltip div { display: flex; justify-content: space-between; gap: 14px; }

details.card > summary { list-style: none; display: flex; align-items: center; gap: 10px; cursor: pointer; }
details.card > summary::-webkit-details-marker { display: none; }
details.card > summary::before { content: "▸"; color: var(--ink-3); transition: transform .15s; }
details.card[open] > summary::before { transform: rotate(90deg); }
details.card[open] > summary { margin-bottom: 12px; }
.table-wrap { overflow-x: auto; }
table { width: 100%; border-collapse: collapse; font-size: 13px; font-variant-numeric: tabular-nums; }
th, td { padding: 6px 10px; text-align: right; border-bottom: 1px solid var(--line-2); white-space: nowrap; }
th { color: var(--ink-3); font-weight: 500; font-size: 12px; }
th:first-child, td:first-child, th:nth-child(5), td:nth-child(5) { text-align: left; }
td .act { font-size: 12px; font-weight: 600; }
.act.charge { color: var(--chg); } .act.discharge { color: var(--dis); } .act.idle { color: var(--ink-3); }
.raw-tabs { display: flex; gap: 6px; margin-bottom: 8px; align-items: center; }
.tab { border: 1px solid var(--line); background: none; border-radius: 8px; padding: 4px 10px; cursor: pointer; font-size: 12.5px; color: var(--ink-2); }
.tab.on { background: var(--accent-soft); border-color: transparent; color: var(--accent); font-weight: 600; }
.raw { margin: 0; max-height: 420px; overflow: auto; background: var(--panel-2); border: 1px solid var(--line-2); border-radius: 10px; padding: 12px; font: 12px/1.5 var(--mono); white-space: pre; }

.toast { position: fixed; left: 50%; bottom: 24px; transform: translate(-50%, 20px); background: var(--ink); color: var(--bg); padding: 8px 14px; border-radius: 9px; font-size: 13px; opacity: 0; transition: all .2s; pointer-events: none; }
.toast.on { opacity: 1; transform: translate(-50%, 0); }

/* ---------- Responsive ---------- */
@media (max-width: 980px) {
  .layout { grid-template-columns: 1fr; }
  .inputs { position: static; }
  .kpis { grid-template-columns: repeat(2, 1fr); }
}
@media (max-width: 560px) {
  .page { padding: 18px 16px 40px; }
  .top-in { padding: 10px 16px; }
  .hide-sm { display: none; }
  .pill span:last-child { display: none; }
  .kpi b { font-size: 19px; }
  .card { padding: 14px; }
}
