/* ---------- Design tokens ---------- */
:root {
  --bg: #f6f7f9;
  --surface: #ffffff;
  --surface-2: #f0f2f5;
  --border: #dde1e7;
  --border-strong: #c5cbd4;
  --text: #1c2330;
  --text-muted: #5d6878;
  --accent: #1f6feb;
  --accent-soft: #e6effd;
  --accent-text: #134aa3;
  --result-bg: #f3f8f1;
  --result-text: #1d5b2c;
  --result-head: #e3efdf;
  --warn-bg: #fff6e0;
  --warn-border: #f1d58a;
  --warn-text: #6b4e00;
  --danger: #c9372c;
  --focus: 0 0 0 3px rgba(31, 111, 235, .35);
  --shadow: 0 1px 2px rgba(16, 24, 40, .06), 0 1px 3px rgba(16, 24, 40, .08);
  --radius: 10px;
  --sidebar-w: 300px;
  --topbar-h: 56px;
  --font: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  --mono: ui-monospace, "SF Mono", Menlo, Consolas, "Liberation Mono", monospace;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #0f1318;
    --surface: #161b22;
    --surface-2: #1c222b;
    --border: #2a313c;
    --border-strong: #3a4350;
    --text: #e3e8ef;
    --text-muted: #94a0b2;
    --accent: #4c8dff;
    --accent-soft: #19253a;
    --accent-text: #8db6ff;
    --result-bg: #13201a;
    --result-text: #8fd6a2;
    --result-head: #182a20;
    --warn-bg: #2a2310;
    --warn-border: #5a4a1a;
    --warn-text: #e8cf86;
    --danger: #ff6b5e;
    --shadow: none;
    color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #0f1318;
  --surface: #161b22;
  --surface-2: #1c222b;
  --border: #2a313c;
  --border-strong: #3a4350;
  --text: #e3e8ef;
  --text-muted: #94a0b2;
  --accent: #4c8dff;
  --accent-soft: #19253a;
  --accent-text: #8db6ff;
  --result-bg: #13201a;
  --result-text: #8fd6a2;
  --result-head: #182a20;
  --warn-bg: #2a2310;
  --warn-border: #5a4a1a;
  --warn-text: #e8cf86;
  --danger: #ff6b5e;
  --shadow: none;
  color-scheme: dark;
}

/* ---------- Base ---------- */
* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 15px/1.5 var(--font);
  -webkit-font-smoothing: antialiased;
}
button { font: inherit; color: inherit; }
svg { fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }
:focus-visible { outline: none; box-shadow: var(--focus); border-radius: 6px; }

/* ---------- Layout ---------- */
.app {
  display: grid;
  grid-template-columns: var(--sidebar-w) minmax(0, 1fr);
  grid-template-rows: var(--topbar-h) minmax(0, 1fr);
  grid-template-areas: "top top" "side main";
  height: 100vh;
  height: 100dvh;
}
.topbar {
  grid-area: top;
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 0 16px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  z-index: 30;
}
.sidebar {
  grid-area: side;
  display: flex;
  flex-direction: column;
  background: var(--surface);
  border-right: 1px solid var(--border);
  overflow: auto;
}
.main {
  grid-area: main;
  overflow: auto;
  padding: 20px 24px 40px;
}

/* ---------- Top bar ---------- */
.brand {
  display: flex; align-items: center; gap: 10px; min-width: 0; flex: 0 1 auto; overflow: hidden;
  text-decoration: none; color: inherit; border-radius: 8px;
}
.brand:hover .brand-name { color: var(--accent-text); }
.brand-mark { width: 30px; height: 30px; color: var(--accent); flex: none; }
.brand-text { display: flex; flex-direction: column; line-height: 1.15; min-width: 0; }
.brand-name { font-weight: 700; letter-spacing: .06em; }
.brand-sub { font-size: 12.5px; color: var(--text-muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.topbar-right { margin-left: auto; display: flex; align-items: center; gap: 8px; flex: none; }

.icon-btn {
  display: inline-grid; place-items: center;
  width: 36px; height: 36px;
  border: 1px solid transparent; border-radius: 8px;
  background: transparent; cursor: pointer; color: var(--text-muted);
}
.icon-btn:hover { background: var(--surface-2); color: var(--text); }
.icon-btn svg { width: 20px; height: 20px; }
.menu-toggle { display: none; }
#themeToggle .i-moon { display: none; }
:root[data-theme="dark"] #themeToggle .i-sun { display: none; }
:root[data-theme="dark"] #themeToggle .i-moon { display: block; }
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) #themeToggle .i-sun { display: none; }
  :root:not([data-theme="light"]) #themeToggle .i-moon { display: block; }
}

.lang-switch {
  display: inline-flex;
  padding: 3px;
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 9px;
}
.lang-switch button {
  border: 0; background: transparent; cursor: pointer;
  padding: 4px 10px; border-radius: 6px;
  font-size: 13px; font-weight: 600; color: var(--text-muted);
}
.lang-switch button:hover { color: var(--text); }
.lang-switch button[aria-pressed="true"] { background: var(--surface); color: var(--accent-text); box-shadow: var(--shadow); }

/* ---------- Tree menu ---------- */
.tree, .tree ul { list-style: none; margin: 0; padding: 0; }
.tree { padding: 12px 10px; flex: 1; }
.tree ul { padding-left: 14px; margin-left: 11px; border-left: 1px solid var(--border); }
.tree-row {
  display: flex; align-items: center; gap: 6px;
  width: 100%;
  padding: 6px 8px;
  border: 0; border-radius: 7px;
  background: transparent; cursor: pointer;
  text-align: left; color: var(--text);
  font-size: 14px; line-height: 1.3;
}
.tree-row:hover { background: var(--surface-2); }
.tree-row .chev { width: 16px; height: 16px; flex: none; color: var(--text-muted); transition: transform .15s; }
.tree-row[aria-expanded="true"] .chev { transform: rotate(90deg); }
.tree-row .num { flex: none; color: var(--text-muted); font-variant-numeric: tabular-nums; font-size: 12.5px; }
.tree-row .lbl { flex: 1; min-width: 0; }
.tree-group > .tree-row { font-weight: 600; }
.tree-leaf > .tree-row { padding-left: 28px; align-items: baseline; }
.tree-leaf > .tree-row.top-level { padding-left: 8px; }
.tree-leaf > .tree-row[aria-current="page"] {
  background: var(--accent-soft); color: var(--accent-text); font-weight: 600;
}
.tree li[hidden-children] > ul { display: none; }

.sidebar-foot {
  display: flex; flex-direction: column; gap: 2px;
  padding: 12px 18px 16px;
  border-top: 1px solid var(--border);
  font-size: 12px; color: var(--text-muted);
}
.sidebar-foot code { font-family: var(--mono); font-size: 11.5px; }

/* ---------- Main content ---------- */

.crumbs { font-size: 13px; color: var(--text-muted); margin-bottom: 4px; }
.screen-head { display: flex; flex-wrap: wrap; align-items: flex-end; gap: 12px 24px; margin-bottom: 14px; }
.screen-head { position: relative; }
.screen-head h1 { font-size: 22px; line-height: 1.25; margin: 0; font-weight: 650; }

/* "?" help button and popover */
.help-btn {
  display: inline-grid; place-items: center;
  width: 22px; height: 22px; margin-left: 10px; vertical-align: 3px;
  border: 1.5px solid var(--border-strong); border-radius: 50%;
  background: var(--surface); color: var(--text-muted);
  font: 700 13px/1 var(--font); cursor: help;
}
.help-btn:hover, .help-btn[aria-expanded="true"] { border-color: var(--accent); color: var(--accent-text); background: var(--accent-soft); }
.help-pop {
  position: absolute; z-index: 15; top: calc(100% + 8px); left: 0;
  width: min(560px, 100%);
  max-height: min(78vh, 760px); overflow-y: auto;
  padding: 14px 16px;
  background: var(--surface); color: var(--text);
  border: 1px solid var(--border); border-radius: var(--radius);
  box-shadow: 0 10px 30px rgba(16, 24, 40, .14), 0 2px 6px rgba(16, 24, 40, .08);
  font-size: 14px; line-height: 1.55;
}
.help-pop p { margin: 0 0 12px; }
.help-figure { margin: 0 0 12px; padding: 8px; border: 1px solid var(--border); border-radius: 8px; background: var(--bg); }
/* Diagram colours: blue = given, green = computed (same as the tables) */
.dg { display: block; width: 100%; height: auto; fill: none; stroke: none; font: 13px var(--font); }
.dg .ln { stroke: var(--text-muted); stroke-width: 1.3; fill: none; }
.dg .faint { stroke: var(--border-strong); stroke-width: 1; fill: none; }
.dg .in { stroke: var(--accent); stroke-width: 2; fill: none; }
.dg .out { stroke: var(--result-text); stroke-width: 2; fill: none; }
.dg .thick { stroke-width: 3; }
.dg .dash { stroke-dasharray: 5 4; }
.dg .ah { fill: var(--text-muted); }
.dg .ah-in { fill: var(--accent); }
.dg .ah-out { fill: var(--result-text); }
.dg .dot-in { fill: var(--accent); }
.dg .dot-out { fill: var(--result-text); }
.dg .dot-stage { fill: var(--surface); stroke: var(--accent); stroke-width: 1.5; }
.dg .sheet { fill: var(--surface); stroke: var(--text-muted); stroke-width: 1.5; }
.dg .region { fill: var(--result-bg); stroke: var(--result-text); stroke-width: 2; }
.dg text { stroke: none; }
.dg .tx { fill: var(--text-muted); }
.dg .tx-sm { fill: var(--text-muted); font-size: 12px; }
.dg .tx-strong { fill: var(--text); font-weight: 600; }
.dg .tx-in { fill: var(--accent-text); font-weight: 600; }
.dg .tx-out { fill: var(--result-text); font-weight: 600; }
.dg .big { font-size: 17px; }
.help-formula-title { font-size: 11.5px; text-transform: uppercase; letter-spacing: .06em; color: var(--text-muted); margin-bottom: 6px; }
.help-formula {
  padding: 8px 12px; border-radius: 8px; background: var(--surface-2);
  font: 13px/1.7 var(--mono); overflow-x: auto; white-space: pre;
}

/* Toolbar */
.toolbar { display: flex; flex-wrap: wrap; align-items: center; gap: 8px; margin-bottom: 12px; }
.btn {
  display: inline-flex; align-items: center; gap: 6px;
  height: 34px; padding: 0 12px;
  background: var(--surface); color: var(--text);
  border: 1px solid var(--border-strong); border-radius: 8px;
  font-size: 13.5px; cursor: pointer; white-space: nowrap;
}
.btn:hover { background: var(--surface-2); }
.btn svg { width: 16px; height: 16px; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn.primary:hover { filter: brightness(1.07); }
.toolbar .spacer { flex: 1; }
.field-inline { display: inline-flex; align-items: center; gap: 8px; font-size: 13px; color: var(--text-muted); }
.field-inline select {
  height: 34px; padding: 0 8px; border-radius: 8px;
  border: 1px solid var(--border-strong); background: var(--surface); color: var(--text); font: inherit;
}

/* ---------- Data table ---------- */
.table-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  overflow: auto;
  max-height: calc(100dvh - 300px);
  min-height: 220px;
}
table.grid {
  border-collapse: separate; border-spacing: 0;
  width: 100%;
  font-variant-numeric: tabular-nums;
}
.grid th, .grid td { border-bottom: 1px solid var(--border); border-right: 1px solid var(--border); }
.grid th:last-child, .grid td:last-child { border-right: 0; }
.grid thead th {
  position: sticky; z-index: 2;
  background: var(--surface-2);
  font-weight: 600; font-size: 13px;
  padding: 7px 10px; text-align: center; white-space: nowrap;
}
.grid thead tr:first-child th { top: 0; height: 32px; }
.grid thead tr:nth-child(2) th { top: 32px; }
.grid thead th.grp { font-size: 11.5px; text-transform: uppercase; letter-spacing: .06em; color: var(--text-muted); }
.grid thead th.res { background: var(--result-head); color: var(--result-text); }
.grid thead th .sym { font-family: var(--font); font-size: 14px; }
.grid thead th .unit { display: block; font-weight: 400; font-size: 11.5px; color: var(--text-muted); }
.grid thead th.res .unit { color: inherit; opacity: .75; }

.grid td { padding: 0; height: 38px; }
.grid td.n {
  width: 48px; text-align: center; color: var(--text-muted); font-size: 13px;
  background: var(--surface-2);
}
.grid td.in input {
  width: 100%; min-width: 110px; height: 37px;
  padding: 0 10px; border: 0; background: transparent; color: var(--text);
  font: 14px var(--mono); text-align: right;
}
.grid td.in.angle input { min-width: 150px; }
.grid td.in input:focus { outline: none; box-shadow: inset 0 0 0 2px var(--accent); background: var(--accent-soft); border-radius: 0; }
.grid td.in input::placeholder { color: var(--border-strong); }
.grid td.in input.invalid { box-shadow: inset 0 0 0 2px var(--danger); }
.grid td.out {
  padding: 0 10px; min-width: 120px;
  background: var(--result-bg); color: var(--result-text);
  font: 14px var(--mono); text-align: right; white-space: nowrap;
}
.grid td.out.empty { color: var(--text-muted); opacity: .6; }
.grid td.act { width: 40px; text-align: center; background: var(--surface); }
.grid tbody tr:hover td.n { color: var(--text); }
.row-del {
  width: 28px; height: 28px; border: 0; border-radius: 6px;
  background: transparent; color: var(--text-muted); cursor: pointer;
  opacity: 0; transition: opacity .1s;
}
.grid tbody tr:hover .row-del, .row-del:focus-visible { opacity: 1; }
.row-del:hover { background: var(--surface-2); color: var(--danger); }
.row-del svg { width: 16px; height: 16px; }
@media (hover: none) { .row-del { opacity: .7; } }
.grid tbody tr:last-child td { border-bottom: 0; }

.table-foot {
  display: flex; flex-wrap: wrap; gap: 6px 18px;
  margin-top: 10px; font-size: 12.5px; color: var(--text-muted);
}
.table-foot kbd {
  font: 11.5px var(--mono); padding: 1px 5px;
  border: 1px solid var(--border-strong); border-bottom-width: 2px; border-radius: 4px;
  background: var(--surface);
}
.legend-swatch { display: inline-block; width: 10px; height: 10px; border-radius: 2px; vertical-align: -1px; margin-right: 5px; }

/* ---------- Title page ---------- */
.title-page {
  min-height: calc(100dvh - var(--topbar-h) - 60px);
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  text-align: center; padding: 24px 8px;
}
.title-mark { width: 120px; height: 120px; margin-bottom: 20px; }
.title-name { margin: 0; font-size: clamp(44px, 9vw, 76px); font-weight: 750; letter-spacing: .14em; line-height: 1.1; }
.title-sub { margin: 14px 0 0; max-width: 34ch; font-size: clamp(16px, 2.4vw, 20px); color: var(--text-muted); line-height: 1.45; }

/* ---------- About page ---------- */
.about { max-width: 820px; }
.about h1 { font-size: 24px; margin: 0 0 6px; }
.about h2 { font-size: 17px; margin: 28px 0 10px; }
.about p { color: var(--text-muted); margin: 0 0 10px; }
.credits { width: 100%; border-collapse: collapse; background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); overflow: hidden; }
.credits th, .credits td { text-align: left; padding: 10px 14px; border-bottom: 1px solid var(--border); vertical-align: top; }
.credits th { background: var(--surface-2); font-size: 13px; }
.credits tr:last-child td { border-bottom: 0; }
.credits a { color: var(--accent-text); }
.const-list { font-family: var(--mono); font-size: 13px; background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 12px 16px; line-height: 1.8; }

/* ---------- Responsive ---------- */
@media (max-width: 960px) {
  .app { grid-template-columns: minmax(0, 1fr); grid-template-areas: "top" "main"; }
  .menu-toggle { display: inline-grid; }
  .sidebar {
    position: fixed; top: var(--topbar-h); bottom: 0; left: 0;
    width: min(320px, 86vw);
    transform: translateX(-100%); transition: transform .2s ease;
    z-index: 25; box-shadow: 0 8px 30px rgba(0,0,0,.18);
  }
  .app.nav-open .sidebar { transform: none; }
  .scrim { position: fixed; inset: var(--topbar-h) 0 0 0; background: rgba(10, 15, 25, .35); z-index: 20; }
  .main { padding: 16px; }
  .table-card { max-height: none; }
}
@media (max-width: 560px) {
  .topbar { gap: 8px; padding: 0 10px; }
  .brand-sub { display: none; }
  .lang-switch button { padding: 4px 8px; }
  .screen-head h1 { font-size: 19px; }
  .toolbar .btn .lbl { display: none; }
  .toolbar .btn { padding: 0 10px; }
}

/* ---------- Confirmation dialog and notification ---------- */
.modal-scrim {
  position: fixed; inset: 0; z-index: 50;
  display: grid; place-items: center; padding: 16px;
  background: rgba(10, 15, 25, .35);
}
.modal {
  width: min(420px, 100%); padding: 18px 20px;
  background: var(--surface); color: var(--text);
  border: 1px solid var(--border); border-radius: var(--radius);
  box-shadow: 0 10px 30px rgba(16, 24, 40, .2);
}
.modal p { margin: 0 0 16px; }
.modal-actions { display: flex; justify-content: flex-end; gap: 8px; }
.toast {
  position: fixed; z-index: 60; left: 50%; bottom: 20px;
  max-width: min(560px, calc(100vw - 32px));
  transform: translate(-50%, 20px); opacity: 0; pointer-events: none;
  padding: 10px 16px; border-radius: var(--radius);
  background: var(--text); color: var(--surface);
  font-size: 13.5px; overflow-wrap: anywhere;
  transition: opacity .2s, transform .2s;
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }

/* ---------- Print ---------- */
@media print {
  .topbar, .sidebar, .scrim, .toolbar, .table-foot, .act { display: none !important; }
  .app { display: block; height: auto; }
  .main { overflow: visible; padding: 0; }
  .table-card { max-height: none; overflow: visible; box-shadow: none; }
  .grid thead th { position: static; }
}
@media (max-width: 340px) {
  .brand-text { display: none; }
}
