:root {
  --bg: #f6f4ef;
  --panel: #ffffff;
  --ink: #1d2433;
  --muted: #5f6878;
  --line: #e3e0d8;
  --accent: #d9570b;
  --accent-ink: #ffffff;
  --chip: #efece4;
  --radius: 10px;
  --neg: #5f87bd; --lc-usable: #e0b84c; --lc-tree: #4e8a4a; --lc-built: #8a8a93; --lc-wet: #4f93c7;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #12151c; --panel: #1b202b; --ink: #e8eaf0; --muted: #98a1b3; --line: #2b3242;
    --accent: #f0803c; --accent-ink: #12151c; --chip: #262d3b; --neg: #7ba3d8; color-scheme: dark;
  }
}
* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body { background: var(--bg); color: var(--ink); font: 14px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif; }
h1 { font-size: 20px; margin: 0; letter-spacing: -0.01em; }
h2 { font-size: 11px; text-transform: uppercase; letter-spacing: 0.08em; color: var(--muted); margin: 0 0 8px; font-weight: 650; }
.sub { margin: 2px 0 0; color: var(--muted); font-size: 13px; }

#app {
  display: grid; height: 100vh; height: 100dvh;
  grid-template-columns: 330px minmax(0, 1fr) 360px;
  grid-template-rows: auto minmax(0, 1fr);
  grid-template-areas: "top top top" "controls stage side";
}
.top { grid-area: top; display: flex; justify-content: space-between; align-items: center; gap: 16px; padding: 12px 16px; background: var(--panel); border-bottom: 1px solid var(--line); }
.dl { white-space: nowrap; background: var(--accent); color: var(--accent-ink); text-decoration: none; padding: 8px 14px; border-radius: 8px; font-weight: 600; font-size: 13px; }
.dl:focus-visible, button:focus-visible, input:focus-visible + span, summary:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

#controls, #side { background: var(--panel); overflow-y: auto; padding: 14px 16px 24px; }
#controls { grid-area: controls; border-right: 1px solid var(--line); }
#side { grid-area: side; border-left: 1px solid var(--line); }
#controls > section, #side > section, #controls > details { margin-bottom: 18px; }
#stage { grid-area: stage; position: relative; min-height: 0; }
#map { position: absolute; inset: 0; }

.chips { display: flex; flex-wrap: wrap; gap: 6px; }
.chips button { border: 1px solid var(--line); background: var(--chip); color: var(--ink); border-radius: 999px; padding: 5px 11px; font: inherit; font-size: 13px; cursor: pointer; }
.chips button[aria-checked="true"] { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); font-weight: 600; }

.seg { display: grid; grid-auto-flow: column; grid-auto-columns: 1fr; border: 1px solid var(--line); border-radius: 8px; overflow: hidden; }
.seg label { position: relative; }
.seg input { position: absolute; opacity: 0; inset: 0; }
.seg span { display: block; text-align: center; padding: 7px 4px; font-size: 12.5px; background: var(--chip); cursor: pointer; border-right: 1px solid var(--line); }
.seg label:last-child span { border-right: 0; }
.seg input:checked + span { background: var(--accent); color: var(--accent-ink); font-weight: 650; }
.note { color: var(--muted); font-size: 12.5px; margin: 8px 0 0; }

.sliders { display: grid; gap: 10px; margin-top: 10px; }
.sl label { display: flex; justify-content: space-between; font-size: 12.5px; margin-bottom: 2px; }
.sl output { font-variant-numeric: tabular-nums; font-weight: 600; }
.sl input[type=range] { width: 100%; accent-color: var(--accent); }
.checks { display: flex; flex-wrap: wrap; gap: 6px 12px; margin-bottom: 8px; }
.check { display: flex; gap: 6px; align-items: center; font-size: 13px; margin-top: 8px; }
.checks .check { margin-top: 0; }
.check input { accent-color: var(--accent); }
details summary { cursor: pointer; font-weight: 650; font-size: 13px; padding: 4px 0; }
button#reset { border: 1px solid var(--line); background: var(--chip); color: var(--ink); padding: 7px 12px; border-radius: 8px; font: inherit; cursor: pointer; }

#legend { position: absolute; left: 12px; bottom: 28px; background: color-mix(in srgb, var(--panel) 92%, transparent); border: 1px solid var(--line); border-radius: var(--radius); padding: 8px 12px; min-width: 220px; max-width: 280px; font-size: 12px; backdrop-filter: blur(4px); }
#legend .bar { height: 10px; border-radius: 5px; margin: 6px 0 3px; }
#legend .ticks { display: flex; justify-content: space-between; font-variant-numeric: tabular-nums; color: var(--muted); }
#legend b { font-size: 12.5px; }
#loading { position: absolute; top: 12px; left: 50%; transform: translateX(-50%); background: var(--panel); border: 1px solid var(--line); padding: 6px 12px; border-radius: 999px; font-size: 12.5px; }
#loading[hidden] { display: none; }

#summary { background: var(--chip); border-radius: var(--radius); padding: 10px 12px; font-size: 13px; }
#summary b { font-size: 15px; }
ol#rank { list-style: none; margin: 0; padding: 0; }
ol#rank li {  display: grid; grid-template-columns: 22px 1fr auto; gap: 6px; align-items: center; padding: 6px 4px; border-bottom: 1px solid var(--line); cursor: pointer; border-radius: 6px; }
ol#rank li:hover, ol#rank li.sel { background: var(--chip); }
ol#rank .n { color: var(--muted); font-size: 12px; text-align: right; font-variant-numeric: tabular-nums; }
ol#rank .v { font-weight: 650; font-variant-numeric: tabular-nums; }
ol#rank .g { display: block; color: var(--muted); font-size: 11.5px; }

#detail h3 { margin: 0 0 2px; font-size: 16px; }
#detail table { width: 100%; border-collapse: collapse; margin-top: 8px; }
#detail td { padding: 3px 0; border-bottom: 1px solid var(--line); font-size: 13px; }
#detail td:last-child { text-align: right; font-variant-numeric: tabular-nums; font-weight: 600; }
#detail svg { width: 100%; height: 90px; margin-top: 10px; }
.tag { white-space: nowrap; display: inline-block; font-size: 11px; padding: 1px 7px; border-radius: 999px; background: var(--chip); color: var(--muted); margin-left: 6px; }
.warn { color: var(--accent); font-size: 12.5px; margin-top: 6px; }
.foot p { color: var(--muted); font-size: 12px; margin: 0 0 8px; }

.maplibregl-popup-content { background: var(--panel); color: var(--ink); font: 13px/1.4 system-ui, sans-serif; border-radius: 8px; padding: 8px 10px; }
.maplibregl-popup-tip { display: none; }

@media (max-width: 1100px) {
  #app { grid-template-columns: 300px minmax(0, 1fr); grid-template-areas: "top top" "controls stage" "controls side"; grid-template-rows: auto 1fr 40vh; }
  #side { border-left: 0; border-top: 1px solid var(--line); }
}
@media (max-width: 720px) {
  body { overflow: auto; }
  #app { display: flex; flex-direction: column; height: auto; }
  .top { order: 0; } #stage { order: 1; height: 62vh; flex: none; } #side { order: 2; } #controls { order: 3; }
  #controls, #side { border: 0; overflow: visible; }
  #legend { left: 8px; bottom: 24px; }
}

.sr { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }
.tabs { display: grid; grid-template-columns: 1fr 1fr; border: 1px solid var(--line); border-radius: 8px; overflow: hidden; margin-bottom: 8px; }
.tabs button { border: 0; background: var(--chip); color: var(--ink); padding: 7px 4px; font: inherit; font-size: 12.5px; cursor: pointer; }
.tabs button + button { border-left: 1px solid var(--line); }
.tabs button[aria-selected="true"] { background: var(--accent); color: var(--accent-ink); font-weight: 650; }
#rankSearch { width: 100%; margin-bottom: 6px; padding: 7px 10px; font: inherit; font-size: 13px; border: 1px solid var(--line); border-radius: 8px; background: var(--bg); color: var(--ink); }
#rankSearch:focus-visible, ol#rank li:focus-visible, input[type=range]:focus-visible, input[type=checkbox]:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
.badge { display: inline-block; font-size: 10.5px; line-height: 1.5; padding: 0 6px; margin-left: 6px; border-radius: 999px; background: var(--accent); color: var(--accent-ink); font-weight: 600; vertical-align: 1px; }
.tag.model { background: transparent; border: 1px dashed var(--muted); }
.empty { color: var(--muted); font-size: 12.5px; padding: 8px 4px; }
#detail h4.x { margin: 10px 0 0; font-size: 11px; text-transform: uppercase; letter-spacing: .08em; color: var(--muted); }

/* ---- v2: header, search, language ---- */
.top { flex-wrap: wrap; }
.brand { flex: 1 1 260px; min-width: 0; }
.topr { display: flex; align-items: center; gap: 10px; }
.search { position: relative; flex: 1 1 280px; max-width: 420px; }
#q { width: 100%; padding: 8px 12px; font: inherit; font-size: 13.5px; border: 1px solid var(--line); border-radius: 8px; background: var(--bg); color: var(--ink); }
#q:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; }
#qlist { position: absolute; z-index: 20; left: 0; right: 0; top: calc(100% + 4px); margin: 0; padding: 4px; list-style: none; background: var(--panel); border: 1px solid var(--line); border-radius: 8px; box-shadow: 0 8px 24px rgba(0,0,0,.18); max-height: 320px; overflow: auto; }
#qlist[hidden] { display: none; }
#qlist li { padding: 6px 8px; border-radius: 6px; cursor: pointer; font-size: 13px; }
#qlist li[aria-selected="true"], #qlist li:hover { background: var(--accent); color: var(--accent-ink); }
#qlist li[aria-selected="true"] .g, #qlist li:hover .g { color: inherit; }
#qlist .g { color: var(--muted); font-size: 11.5px; }
#qlist .empty { cursor: default; color: var(--muted); }
.lang { display: inline-grid; grid-auto-flow: column; border: 1px solid var(--line); border-radius: 8px; overflow: hidden; }
.lang button { border: 0; background: var(--chip); color: var(--ink); padding: 7px 10px; font: inherit; font-size: 12.5px; font-weight: 600; cursor: pointer; }
.lang button + button { border-left: 1px solid var(--line); }
.lang button[aria-pressed="true"] { background: var(--accent); color: var(--accent-ink); }

/* ---- v2: controls ---- */
.seg.wrap { display: flex; flex-wrap: wrap; }
.seg.wrap label { flex: 1 1 auto; }
.seg.wrap label[hidden] { display: none; }
.seg.wrap span { border-bottom: 1px solid var(--line); padding: 7px 10px; }
.seg input:focus-visible + span { outline: 2px solid var(--accent); outline-offset: -2px; }
.lbl { margin: 8px 0 4px; font-size: 12.5px; color: var(--muted); }

/* ---- v2: map overlays ---- */
#resBadge { position: absolute; right: 12px; bottom: 28px; display: flex; gap: 8px; align-items: center; background: color-mix(in srgb, var(--panel) 92%, transparent); border: 1px solid var(--line); border-radius: 999px; padding: 4px 12px; font-size: 12px; backdrop-filter: blur(4px); }
#busy { color: var(--accent); font-weight: 600; }
#busy[hidden] { display: none; }

/* ---- v2: tabs (4) ---- */
.tabs { display: flex; flex-wrap: wrap; }
.tabs button { flex: 1 1 auto; }
.tabs button[hidden] { display: none; }
.tabs button:nth-child(n+3) { border-top: 0; }
@media (min-width: 380px) { .tabs { flex-wrap: nowrap; } .tabs button { padding: 7px 6px; font-size: 12px; } }
#rankBox[hidden], #pf[hidden] { display: none; }

/* ---- v2: portfolio ---- */
.pfrank { margin: 8px 0; }
.kv { display: grid; grid-template-columns: 1fr auto; gap: 3px 12px; margin: 8px 0; font-size: 13px; }
.kv dt { color: var(--muted); }
.kv dd { margin: 0; text-align: right; font-weight: 600; font-variant-numeric: tabular-nums; }
.kv.small { font-size: 12px; }
#pf h4, #detail h4 { margin: 12px 0 4px; font-size: 11px; text-transform: uppercase; letter-spacing: .08em; color: var(--muted); }
ul.pfsys { list-style: none; margin: 0; padding: 0; font-size: 12.5px; }
ul.pfsys li { display: grid; grid-template-columns: 90px 1fr auto; gap: 8px; align-items: center; padding: 3px 0; }
ul.pfsys .bar { height: 8px; background: var(--chip); border-radius: 4px; overflow: hidden; }
ul.pfsys .bar i { display: block; height: 100%; background: var(--accent); }
ul.pfsys .v, ol.pflist .v { font-variant-numeric: tabular-nums; font-weight: 600; }
ol.pflist { list-style: none; margin: 0; padding: 0; }
ol.pflist li { display: grid; grid-template-columns: 22px 1fr auto; gap: 6px; align-items: center; padding: 6px 4px; border-bottom: 1px solid var(--line); cursor: pointer; border-radius: 6px; }
ol.pflist li:hover { background: var(--chip); }
ol.pflist .n { color: var(--muted); font-size: 12px; text-align: right; }
ol.pflist .g { display: block; color: var(--muted); font-size: 11.5px; }
ol.pflist li:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

/* ---- v2: region detail ---- */
.dhead { display: flex; justify-content: space-between; gap: 8px; align-items: flex-start; }
.dhead h2 { margin-bottom: 2px; }
.dact { display: flex; gap: 6px; flex-shrink: 0; }
.dact button { border: 1px solid var(--line); background: var(--chip); color: var(--ink); padding: 5px 10px; border-radius: 8px; font: inherit; font-size: 12.5px; cursor: pointer; }
.dact button#clearSel { padding: 5px 9px; font-size: 15px; line-height: 1; }
#detail code { font-size: 12px; background: var(--chip); padding: 0 5px; border-radius: 4px; }
#detail details { border-top: 1px solid var(--line); margin-top: 10px; padding-top: 6px; }
#detail details > summary { font-size: 12px; text-transform: uppercase; letter-spacing: .08em; color: var(--muted); font-weight: 650; }
#detail h5 { margin: 10px 0 0; font-size: 12px; color: var(--muted); font-weight: 600; }
#detail td:first-child { padding-right: 8px; }
#detail td .rp, .rp { color: var(--muted); font-weight: 400; font-size: 11.5px; }
.tag.meas { background: color-mix(in srgb, #3d8b5a 18%, transparent); color: var(--ink); }
.tag.inh { background: color-mix(in srgb, #6b7fd6 20%, transparent); color: var(--ink); }
.tag.model { background: transparent; border: 1px dashed var(--muted); }
.tags { display: flex; flex-wrap: wrap; gap: 4px; }
.lcbar { display: flex; height: 12px; border-radius: 6px; overflow: hidden; margin-top: 10px; background: var(--chip); }
.lcbar i { display: block; height: 100%; }
.lclegend { display: flex; flex-wrap: wrap; gap: 2px 10px; font-size: 11.5px; color: var(--muted); margin-top: 4px; }
.lclegend b { display: inline-block; width: 9px; height: 9px; border-radius: 2px; margin-right: 4px; }
table.rt { width: 100%; border-collapse: collapse; margin-top: 6px; }
table.rt th { font-size: 11px; color: var(--muted); font-weight: 600; text-align: right; padding: 2px 0; }
table.rt th:first-child, table.rt td:first-child { text-align: left; }
#detail table.rt td:last-child, #detail table.rt td { text-align: right; }
#detail table.rt td:first-child { text-align: left; font-weight: 400; }
#detail .ticks { display: flex; justify-content: space-between; font-size: 11px; color: var(--muted); }
#detail svg { color: var(--ink); }
.assump .kv { max-height: 320px; overflow: auto; }
.spin { display: inline-block; width: 10px; height: 10px; border: 2px solid var(--line); border-top-color: var(--accent); border-radius: 50%; animation: sp .8s linear infinite; vertical-align: -1px; }
@keyframes sp { to { transform: rotate(360deg); } }
@media (prefers-reduced-motion: reduce) { .spin { animation: none; } }

@media (max-width: 720px) {
  .top { padding: 10px 12px; }
  .search { order: 3; flex-basis: 100%; max-width: none; }
  .topr { margin-left: auto; }
  #resBadge { right: 8px; bottom: 60px; }
  #qlist { max-height: 240px; }
}

/* solar module picker */
#modSel { width: 100%; padding: 7px 8px; font: inherit; font-size: 13px; background: var(--chip); color: var(--ink); border: 1px solid var(--line); border-radius: 8px; }
#modSel:focus-visible, .mf input:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
.modcard { margin-top: 8px; padding: 8px 10px; border: 1px solid var(--line); border-radius: 8px; background: var(--chip); font-size: 12.5px; }
.modcard .mhead { display: flex; flex-wrap: wrap; gap: 6px; align-items: center; margin-bottom: 4px; }
.modcard .kv { margin: 0; }
.modcard a { color: var(--accent); }
.mquote { font-style: italic; }
.modform { margin-top: 10px; display: grid; gap: 8px; }
.mf { display: grid; gap: 2px; }
.mf label { font-size: 12.5px; }
.mf input { width: 100%; padding: 6px 8px; font: inherit; font-size: 13px; background: var(--panel); color: var(--ink); border: 1px solid var(--line); border-radius: 6px; }
.mf input[aria-invalid="true"] { border-color: var(--neg); border-width: 2px; }
.ferr { color: var(--neg); font-size: 12px; min-height: 0; margin: 0; }
.ferr:empty { display: none; }
#mf-reset { border: 1px solid var(--line); background: var(--chip); color: var(--ink); padding: 6px 12px; border-radius: 8px; font: inherit; cursor: pointer; }
/* module effect + comparison */
.cmpwrap { overflow-x: auto; }
table.cmp { width: 100%; border-collapse: collapse; font-size: 12px; font-variant-numeric: tabular-nums; }
table.cmp th, table.cmp td { padding: 4px 5px; text-align: right; border-bottom: 1px solid var(--line); white-space: nowrap; }
table.cmp th:first-child, table.cmp td:first-child { text-align: left; white-space: normal; min-width: 90px; }
table.cmp th button { all: unset; cursor: pointer; font-weight: 650; }
table.cmp th button:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
table.cmp th[aria-sort] button::after { content: " ↓"; }
table.cmp th[aria-sort="ascending"] button::after { content: " ↑"; }
table.cmp tr.best td { background: color-mix(in srgb, var(--accent) 16%, transparent); }
table.cmp tr.sel td:first-child { box-shadow: inset 3px 0 0 var(--accent); font-weight: 650; }
table.cmp .tag { margin-left: 4px; }
.modform[hidden], .modcard[hidden] { display: none; }
.mquote summary { font-weight: 500; font-size: 12px; color: var(--muted); }

/* ---- plants layer ---- */
.sw { display: inline-block; width: 11px; height: 11px; border-radius: 50%; background: var(--c, #888); border: 1.5px solid #fff; box-shadow: 0 0 0 1px rgba(0,0,0,.35); vertical-align: -1px; flex: none; }
.sw.ap { background: transparent; border: 2px dashed var(--c); box-shadow: none; }
.sw.solid { background: #888; }
.plchecks .check { margin-top: 0; }
#plLegend { position: absolute; right: 12px; top: 12px; background: color-mix(in srgb, var(--panel) 92%, transparent); border: 1px solid var(--line); border-radius: var(--radius); padding: 6px 10px; font-size: 12px; max-width: 250px; backdrop-filter: blur(4px); }
#plLegend[hidden] { display: none; }
#plLegend summary { font-size: 12px; padding: 0; }
#plLegend ul { list-style: none; margin: 6px 0 0; padding: 0; display: grid; gap: 3px; }
#plLegend li { display: flex; gap: 6px; align-items: center; }
#plLegend .lgsep { border-top: 1px solid var(--line); padding-top: 4px; margin-top: 2px; }
#plLegend .note { margin: 6px 0 0; font-size: 11.5px; }
#plantCard { border: 1px solid var(--accent); border-radius: var(--radius); padding: 10px 12px; margin-bottom: 18px; }
#plantCard[hidden] { display: none; }
#plantCard h3 { margin: 0 0 2px; font-size: 16px; }
#plantCard table { width: 100%; border-collapse: collapse; margin-top: 8px; }
#plantCard td { padding: 3px 0; border-bottom: 1px solid var(--line); font-size: 13px; vertical-align: top; }
#plantCard td:last-child { text-align: right; font-weight: 600; padding-left: 10px; overflow-wrap: anywhere; }
#plantCard a { color: var(--accent); }
.plbadge { margin: 8px 0 0; display: inline-block; padding: 2px 10px; border-radius: 999px; font-size: 12.5px; }
.plbadge.ex { background: color-mix(in srgb, #3d8b5a 20%, transparent); }
.plbadge.ap { background: transparent; border: 1px dashed var(--muted); }
#plantList { margin-bottom: 18px; }
#plantList[hidden] { display: none; }
#plantList summary { font-size: 12px; text-transform: uppercase; letter-spacing: .08em; color: var(--muted); font-weight: 650; }
table.pltable th:first-child, table.pltable td:first-child { min-width: 110px; }
table.pltable td:nth-child(3), table.pltable td:nth-child(4) { white-space: normal; text-align: left; }
table.pltable tr.sel td:first-child { box-shadow: inset 3px 0 0 var(--accent); }
button.plrow { all: unset; cursor: pointer; color: var(--accent); text-decoration: underline; text-underline-offset: 2px; font-weight: 600; }
button.plrow:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
ul.pllist { list-style: none; margin: 8px 0 0; padding: 0; font-size: 12.5px; display: grid; gap: 4px; }
.plcmp { margin: 8px 0 0; font-size: 12.5px; }
.plpop .maplibregl-popup-content { padding: 6px 9px; }

.navlinks { display: flex; gap: 10px; align-items: center; }
.navlinks a { color: var(--ink); font-size: 13px; text-decoration: none; padding: 6px 10px; border: 1px solid var(--line); border-radius: 8px; background: var(--chip); white-space: nowrap; }
.navlinks a:hover { border-color: var(--accent); }
@media (max-width: 720px) { .navlinks { display: none; } }
