:root {
  --bg: #f4f6f9;
  --panel: #ffffff;
  --panel-2: #f8fafc;
  --text: #17202b;
  --muted: #5d6b7a;
  --line: #dfe5ec;
  --accent: #e8590c;
  --accent-ink: #ffffff;
  --accent-soft: #fff1e8;
  --blue: #1c7ed6;
  --danger: #c92a2a;
  --danger-soft: #fff0f0;
  --ok: #2f9e44;
  --shadow: 0 1px 2px rgba(16, 24, 40, 0.05), 0 2px 8px rgba(16, 24, 40, 0.05);
  --radius: 12px;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f141a;
    --panel: #171e27;
    --panel-2: #1c2530;
    --text: #e7edf4;
    --muted: #97a6b6;
    --line: #2a3542;
    --accent: #ff7a2e;
    --accent-ink: #1a0d04;
    --accent-soft: #2d1d12;
    --blue: #4dabf7;
    --danger: #ff6b6b;
    --danger-soft: #2e1a1c;
    --shadow: 0 1px 2px rgba(0, 0, 0, 0.3);
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  background: var(--bg);
  color: var(--text);
  font: 15px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
}
a { color: var(--blue); }
h2 { font-size: 1.05rem; margin: 0 0 12px; }
h3 { font-size: 0.98rem; margin: 0 0 10px; }
h4 { font-size: 0.8rem; text-transform: uppercase; letter-spacing: 0.05em; color: var(--muted); margin: 14px 0 8px; }
.sr-only { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); }
.muted, .hint { color: var(--muted); }
.hint { font-size: 0.82rem; margin: 6px 0 0; }
[hidden] { display: none !important; }

/* ---------------------------------------------------------------- top bar */
.topbar {
  display: flex; align-items: center; justify-content: space-between; gap: 16px;
  padding: 10px 20px; background: var(--panel); border-bottom: 1px solid var(--line);
  position: sticky; top: 0; z-index: 1000;
}
.brand { display: flex; align-items: center; gap: 10px; }
.logo { width: 34px; height: 34px; }
.logo-sun { fill: #ffb703; }
.logo-panel { fill: #1c3f66; }
.logo-grid { stroke: #8fb3d9; stroke-width: 1; fill: none; }
.brand-name { font-weight: 750; font-size: 1.15rem; letter-spacing: -0.01em; }
.brand-tag { color: var(--muted); font-size: 0.8rem; }
.topnav { display: flex; align-items: center; gap: 12px; }
.linkbtn { background: none; border: 0; color: var(--blue); font: inherit; cursor: pointer; padding: 6px; }
.linkbtn:hover { text-decoration: underline; }
.lang select { padding: 6px 8px; }

/* ---------------------------------------------------------------- layout */
.layout {
  display: grid; grid-template-columns: minmax(340px, 420px) minmax(0, 1fr);
  gap: 18px; padding: 18px 20px; max-width: 1680px; margin: 0 auto;
}
.sidebar { display: flex; flex-direction: column; gap: 14px; min-width: 0; }
.main { scroll-margin-top: 64px; min-width: 0; display: flex; flex-direction: column; gap: 14px; }
.card {
  background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius);
  padding: 16px; box-shadow: var(--shadow); min-width: 0;
}
.card-head { display: flex; align-items: center; justify-content: space-between; gap: 10px; flex-wrap: wrap; margin-bottom: 10px; }
.card-head h2, .card-head h3 { margin: 0; }
.grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: 10px 12px; }

/* ---------------------------------------------------------------- form */
.field { display: flex; flex-direction: column; gap: 4px; font-size: 0.84rem; color: var(--muted); min-width: 0; }
.field > span:first-child { line-height: 1.25; }
input, select, button { font: inherit; color: inherit; }
input[type="number"], input[type="text"], input[type="search"], select {
  width: 100%; padding: 7px 9px; border: 1px solid var(--line); border-radius: 8px;
  background: var(--panel-2); color: var(--text); min-width: 0;
}
input:focus-visible, select:focus-visible, button:focus-visible {
  outline: 2px solid var(--blue); outline-offset: 1px;
}
.inp { position: relative; display: block; }
.inp input { padding-right: 34px; }
.inp i { position: absolute; right: 9px; top: 50%; transform: translateY(-50%); font-style: normal; color: var(--muted); font-size: 0.8rem; pointer-events: none; }
.field.invalid input { border-color: var(--danger); background: var(--danger-soft); }
.years { display: flex; align-items: center; gap: 6px; }
.years b { color: var(--muted); font-weight: 400; }
.check { display: flex; align-items: center; gap: 8px; font-size: 0.86rem; grid-column: 1 / -1; cursor: pointer; }
.check input { width: 17px; height: 17px; accent-color: var(--accent); }
.disabled { opacity: 0.45; }
.derived b { color: var(--text); font-size: 0.95rem; padding: 7px 0; }

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  border-radius: 9px; border: 1px solid transparent; padding: 8px 14px; cursor: pointer;
  font-weight: 600; transition: background 0.15s, border-color 0.15s;
}
.btn:disabled { opacity: 0.6; cursor: progress; }
.btn-primary { background: var(--accent); color: var(--accent-ink); }
.btn-primary:hover:not(:disabled) { filter: brightness(1.06); }
.btn-ghost { background: var(--panel-2); border-color: var(--line); color: var(--text); }
.btn-ghost:hover { border-color: var(--muted); }
.btn-sm { padding: 5px 10px; font-size: 0.84rem; font-weight: 550; }
.btn-block { width: 100%; padding: 11px; font-size: 1rem; margin-top: 10px; }
.icon-btn {
  background: none; border: 1px solid transparent; border-radius: 7px; width: 30px; height: 30px;
  cursor: pointer; color: var(--muted); font-size: 0.95rem;
}
.icon-btn:hover:not(:disabled) { background: var(--panel-2); border-color: var(--line); color: var(--text); }
.icon-btn svg { width: 15px; height: 15px; fill: none; stroke: currentColor; stroke-width: 1.6; stroke-linecap: round; vertical-align: middle; }
.icon-btn:disabled { opacity: 0.3; cursor: default; }

/* ---------------------------------------------------------------- site */
.geo-search { display: flex; gap: 8px; margin-bottom: 8px; }
.geo-results { list-style: none; margin: 0 0 8px; padding: 4px; border: 1px solid var(--line); border-radius: 8px; max-height: 200px; overflow: auto; }
.geo-results li { font-size: 0.84rem; }
.geo-results li.muted { padding: 6px 8px; }
.geo-results button { width: 100%; text-align: left; background: none; border: 0; padding: 6px 8px; border-radius: 6px; cursor: pointer; }
.geo-results button:hover { background: var(--panel-2); }
.map { height: 230px; border-radius: 10px; border: 1px solid var(--line); z-index: 0; }
.map + .hint { margin-bottom: 10px; }

/* ---------------------------------------------------------------- arrays */
.array { border: 1px solid var(--line); border-radius: 10px; margin-bottom: 10px; background: var(--panel); }
.array-head { display: flex; align-items: center; gap: 4px; padding: 4px 6px 4px 4px; }
.array-toggle {
  flex: 1; display: flex; align-items: center; gap: 8px; min-width: 0; background: none; border: 0;
  padding: 8px; cursor: pointer; text-align: left; border-radius: 8px;
}
.array-toggle:hover { background: var(--panel-2); }
.chev { width: 8px; height: 8px; border-right: 2px solid var(--muted); border-bottom: 2px solid var(--muted); transform: rotate(45deg); transition: transform 0.15s; flex: none; margin: 0 4px 3px 2px; }
.array.closed .chev { transform: rotate(-45deg); margin-bottom: 0; }
.array-title { font-weight: 650; }
.array-badge { color: var(--muted); font-size: 0.8rem; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.array-tools { display: flex; gap: 2px; }
.array-body { padding: 4px 14px 14px; }
.array.closed .array-body { display: none; }
.array-body > .field { margin-bottom: 10px; }
.array-note { margin-top: 10px; }

.calc-card { position: sticky; bottom: 12px; z-index: 5; }
.progress { margin-top: 12px; }
.bar { height: 6px; background: var(--panel-2); border-radius: 4px; overflow: hidden; border: 1px solid var(--line); }
.bar-fill { height: 100%; width: 0; background: var(--accent); transition: width 0.3s; }
.progress-msg { font-size: 0.82rem; color: var(--muted); margin-top: 6px; }
.alert { margin: 10px 0 0; padding: 9px 11px; border-radius: 8px; background: var(--danger-soft); color: var(--danger); font-size: 0.86rem; border: 1px solid color-mix(in srgb, var(--danger) 30%, transparent); }

/* ---------------------------------------------------------------- tabs */
.tabs { display: flex; gap: 4px; border-bottom: 1px solid var(--line); }
.tab {
  background: none; border: 0; padding: 10px 16px; cursor: pointer; font-weight: 600; color: var(--muted);
  border-bottom: 2px solid transparent; margin-bottom: -1px;
}
.tab[aria-selected="true"] { color: var(--text); border-bottom-color: var(--accent); }
.panel { display: flex; flex-direction: column; gap: 14px; }
#results { display: flex; flex-direction: column; gap: 14px; }

.empty { text-align: center; padding: 70px 20px; color: var(--muted); }
.empty svg { width: 90px; height: 90px; opacity: 0.85; }
.empty h3 { color: var(--text); font-size: 1.15rem; margin-top: 10px; }
.empty p { max-width: 460px; margin: 0 auto; }

/* ---------------------------------------------------------------- results */
.res-head { display: flex; justify-content: space-between; align-items: flex-start; gap: 12px; flex-wrap: wrap; }
.res-head h2 { font-size: 1.3rem; margin: 0 0 2px; overflow-wrap: anywhere; }
.res-head p { margin: 0; font-size: 0.86rem; }
.actions { display: flex; gap: 6px; flex-wrap: wrap; }
.kpis { display: grid; grid-template-columns: repeat(auto-fit, minmax(170px, 1fr)); gap: 12px; }
.kpi { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); padding: 14px 16px; box-shadow: var(--shadow); }
.kpi:first-child { background: var(--accent-soft); border-color: color-mix(in srgb, var(--accent) 35%, var(--line)); }
.kpi-label { font-size: 0.8rem; color: var(--muted); }
.kpi-value { font-size: 1.6rem; font-weight: 720; letter-spacing: -0.02em; margin: 2px 0; font-variant-numeric: tabular-nums; }
.kpi-value small { font-size: 0.85rem; font-weight: 550; color: var(--muted); }
.kpi-sub { font-size: 0.78rem; color: var(--muted); }
.kpi.warn .kpi-value { color: var(--danger); }

.scenarios { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 4px; max-height: 260px; overflow: auto; }
.scn { display: flex; align-items: center; gap: 8px; padding: 4px 6px; border-radius: 8px; }
.scn.active { background: var(--panel-2); outline: 1px solid var(--line); }
.scn-check { display: flex; align-items: center; gap: 6px; cursor: pointer; }
.scn-check input { width: 16px; height: 16px; }
.dot { width: 10px; height: 10px; border-radius: 50%; display: inline-block; }
.scn-main { flex: 1; min-width: 0; text-align: left; background: none; border: 0; padding: 4px; cursor: pointer; display: flex; flex-direction: column; }
.scn-name { font-weight: 600; font-size: 0.9rem; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.scn-meta { font-size: 0.78rem; color: var(--muted); font-variant-numeric: tabular-nums; }

.charts { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 14px; }
.chart-card { margin: 0; }
.chart-card figcaption { font-weight: 650; font-size: 0.95rem; margin-bottom: 8px; display: flex; align-items: center; gap: 10px; flex-wrap: wrap; }
.chart-box { position: relative; height: 280px; }
.chart-box.short { height: 220px; }
.inline-select { width: auto !important; padding: 3px 8px !important; font-size: 0.84rem; font-weight: 500; }

.table-wrap { overflow-x: auto; }
.tbl { width: 100%; border-collapse: collapse; font-size: 0.86rem; font-variant-numeric: tabular-nums; }
.tbl th, .tbl td { padding: 7px 10px; text-align: right; border-bottom: 1px solid var(--line); white-space: nowrap; }
.tbl th:first-child, .tbl td:first-child { text-align: left; }
.tbl th { font-size: 0.76rem; text-transform: uppercase; letter-spacing: 0.03em; color: var(--muted); font-weight: 650; }
.tbl tfoot td { font-weight: 700; border-bottom: 0; }
details.card summary { cursor: pointer; font-weight: 650; }
details.card[open] summary { margin-bottom: 10px; }

/* ---------------------------------------------------------------- 3D */
.view-controls { display: flex; flex-wrap: wrap; gap: 12px; align-items: flex-end; }
.view-controls .field { min-width: 120px; }
.view-controls .field.grow { flex: 1; min-width: 240px; }
.view-controls #v-day { width: 80px; }
.time-row { display: flex; align-items: center; gap: 10px; }
.time-row input[type="range"] { flex: 1; accent-color: var(--accent); }
#v-time { color: var(--text); font-variant-numeric: tabular-nums; }
.view-wrap { padding: 0; overflow: hidden; }
.view3d { height: min(62vh, 560px); min-height: 320px; position: relative; }
.view3d canvas { display: block; width: 100% !important; height: 100% !important; touch-action: none; }
.legend { display: flex; flex-wrap: wrap; gap: 6px 16px; padding: 10px 14px; font-size: 0.8rem; color: var(--muted); border-top: 1px solid var(--line); }
.legend > span { display: inline-flex; align-items: center; gap: 6px; }
.sw { width: 12px; height: 12px; border-radius: 3px; display: inline-block; }
.sw-front { background: rgb(77, 158, 217); }
.sw-lit { background: rgb(77, 204, 89); }
.sw-shaded { background: rgb(230, 64, 51); }
.sw-dark { background: rgb(115, 115, 128); }
.v-info { font-size: 0.88rem; font-variant-numeric: tabular-nums; }
.v-state { margin-top: 4px; font-weight: 600; }

/* ---------------------------------------------------------------- misc */
.footer { max-width: 1680px; margin: 0 auto; padding: 4px 20px 28px; font-size: 0.78rem; color: var(--muted); }
.dialog { border: 1px solid var(--line); border-radius: 14px; background: var(--panel); color: var(--text); max-width: 720px; width: calc(100% - 32px); padding: 22px 24px; }
.dialog::backdrop { background: rgba(10, 15, 20, 0.45); }
.dialog-body { font-size: 0.92rem; }
.dialog-body h2 { font-size: 1.2rem; }
.dialog-body li { margin-bottom: 8px; }
.dialog-body code { background: var(--panel-2); padding: 1px 5px; border-radius: 5px; font-size: 0.86em; }
.dialog form { text-align: right; margin-top: 10px; }
.toast {
  position: fixed; left: 50%; bottom: 24px; transform: translateX(-50%); z-index: 2000;
  background: var(--text); color: var(--bg); padding: 10px 16px; border-radius: 10px; font-size: 0.9rem;
  box-shadow: 0 6px 20px rgba(0, 0, 0, 0.25); max-width: calc(100% - 32px);
}
.leaflet-container { font: inherit; }

/* ---------------------------------------------------------------- responsive */
@media (max-width: 1180px) {
  .charts { grid-template-columns: minmax(0, 1fr); }
}
@media (max-width: 900px) {
  .layout { grid-template-columns: minmax(0, 1fr); padding: 12px 16px; }
  .calc-card { position: static; }
  .topbar { padding: 8px 16px; }
  .brand-tag { display: none; }
}
@media (max-width: 420px) {
  .grid2 { grid-template-columns: minmax(0, 1fr); }
  .kpi-value { font-size: 1.35rem; }
  .card { padding: 13px; }
}

/* ---------------------------------------------------------------- print */
@media print {
  :root { --bg: #fff; --panel: #fff; --panel-2: #fff; --text: #000; --muted: #444; --line: #ccc; color-scheme: light; }
  .topbar, .sidebar, .tabs, .actions, .footer .linkbtn, .scn-load, .scn-del, .scn-check, #panel-3d, .toast { display: none !important; }
  .layout { display: block; padding: 0; }
  .card, .kpi { box-shadow: none; break-inside: avoid; }
  .charts { grid-template-columns: 1fr 1fr; }
  .chart-box { height: 230px; }
  details.card > *:not(summary) { display: block; }
}
