:root {
  --bg: #f3f5f8;
  --panel: #ffffff;
  --fg: #16202c;
  --muted: #5d6b7c;
  --line: #d9dfe7;
  --accent: #0b5cad;
  --accent-fg: #ffffff;
  --input: #ffffff;
  --safe: #1a8f3a;
  --warn: #d9a400;
  --warn-strong: #d96b00;
  --bad: #c62828;
  --ratio: #8c33d9;
  --neutral: #9aa4b1;
  --chart-bg: #ffffff;
  --chart-fg: #16202c;
  --chart-muted: #5d6b7c;
  --chart-grid: #e8ecf1;
  --chart-axis: #b4bdc8;
  --c-water: #1f63d6;
  --c-req: #d32f2f;
  --c-ok: #1a8f3a;
  --c-bed: #8a5a2b;
  --c-cd: #6b7280;
  --c-depth: #0f8a8a;
  --radius: 10px;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #0f141b; --panel: #161d26; --fg: #e5ebf2; --muted: #9aa8b8; --line: #2a3441;
    --accent: #4b9cf0; --accent-fg: #06121f; --input: #0f151d;
    --chart-bg: #161d26; --chart-fg: #e5ebf2; --chart-muted: #9aa8b8; --chart-grid: #232d39; --chart-axis: #3a4656;
    --c-water: #5b9dff; --c-req: #ff6b6b; --c-ok: #3ccf6a; --c-bed: #c8925c; --c-cd: #8b95a3; --c-depth: #3cc7c7;
    --safe: #3ccf6a; --warn: #f2c94c; --bad: #ff6b6b; --ratio: #b27bf0; --neutral: #6b7686;
    color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #0f141b; --panel: #161d26; --fg: #e5ebf2; --muted: #9aa8b8; --line: #2a3441;
  --accent: #4b9cf0; --accent-fg: #06121f; --input: #0f151d;
  --chart-bg: #161d26; --chart-fg: #e5ebf2; --chart-muted: #9aa8b8; --chart-grid: #232d39; --chart-axis: #3a4656;
  --c-water: #5b9dff; --c-req: #ff6b6b; --c-ok: #3ccf6a; --c-bed: #c8925c; --c-cd: #8b95a3; --c-depth: #3cc7c7;
  --safe: #3ccf6a; --warn: #f2c94c; --bad: #ff6b6b; --ratio: #b27bf0; --neutral: #6b7686;
  color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  background: var(--bg); color: var(--fg);
  font: 14px/1.4 system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
}
button, input, select { font: inherit; color: inherit; }

.topbar {
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
  padding: 10px 16px; background: var(--panel); border-bottom: 1px solid var(--line);
}
.brand { display: flex; align-items: center; gap: 10px; min-width: 0; }
.brand svg { width: 32px; height: 32px; flex: none; fill: var(--accent); }
.brand svg .wave { fill: none; stroke: var(--accent); stroke-width: 2; }
.brand h1 { font-size: 17px; margin: 0; }
.brand p { margin: 0; color: var(--muted); font-size: 12.5px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.api-state { font-size: 12px; color: var(--muted); padding: 3px 9px; border: 1px solid var(--line); border-radius: 999px; white-space: nowrap; }
.api-state.ok { color: var(--safe); border-color: color-mix(in srgb, var(--safe) 40%, transparent); }
.api-state.err { color: var(--bad); border-color: color-mix(in srgb, var(--bad) 40%, transparent); }

.layout {
  display: grid; grid-template-columns: 440px minmax(0, 1fr); gap: 12px; padding: 12px;
  min-height: calc(100vh - 58px);
}
.panel { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); padding: 12px; min-width: 0; }
.inputs { display: flex; flex-direction: column; gap: 8px; }

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  padding: 7px 12px; border: 1px solid var(--line); border-radius: 8px; background: var(--input);
  cursor: pointer; min-height: 34px; text-align: center;
}
.btn:hover:not(:disabled) { border-color: var(--accent); }
.btn:disabled { opacity: .5; cursor: not-allowed; }
.btn.primary { background: var(--accent); color: var(--accent-fg); border-color: var(--accent); font-weight: 700; letter-spacing: .03em; }
.btn:focus-visible, .tab:focus-visible, .link:focus-visible, input:focus-visible, select:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; }
.file-btn { flex: 1; position: relative; overflow: hidden; }
.file-btn input { position: absolute; inset: 0; opacity: 0; cursor: pointer; }
.link { background: none; border: 0; color: var(--accent); cursor: pointer; padding: 4px; text-decoration: underline; font-size: 13px; white-space: nowrap; }
.file-row { display: flex; align-items: center; gap: 8px; }
.file-row.tight { justify-content: space-between; }
.file-label { color: var(--muted); font-style: italic; font-size: 13px; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.file-label.loaded { color: var(--fg); font-style: normal; }
.check { display: inline-flex; align-items: center; gap: 6px; font-size: 13px; white-space: nowrap; }

.grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.field { display: flex; flex-direction: column; gap: 3px; font-weight: 600; font-size: 13px; }
.field input { font-weight: 400; }
input[type=text], input[type=number], input[type=date], select {
  width: 100%; padding: 6px 8px; border: 1px solid var(--line); border-radius: 7px; background: var(--input); min-height: 32px;
}

.tabs { display: flex; gap: 2px; border-bottom: 1px solid var(--line); margin-top: 4px; overflow-x: auto; }
.tab { border: 1px solid transparent; border-bottom: 0; background: none; padding: 7px 11px; cursor: pointer; border-radius: 7px 7px 0 0; color: var(--muted); white-space: nowrap; }
.tab.active { border-color: var(--line); background: var(--panel); color: var(--fg); font-weight: 600; margin-bottom: -1px; }
.tabpane { display: none; flex-direction: column; gap: 6px; padding: 8px 2px 2px; min-height: 256px; }
.tabpane.active { display: flex; }
.row { display: grid; grid-template-columns: 150px minmax(0, 1fr); align-items: center; gap: 8px; }
.row.note em { color: var(--muted); font-size: 13px; }
.row input[type=range] { width: 100%; accent-color: var(--accent); }
.speed-read { padding-left: 158px; font-size: 13px; color: var(--muted); }
.speed-read strong { color: var(--fg); }

.actions { display: grid; grid-template-columns: repeat(4, 1fr); gap: 6px; margin-top: 4px; }
.status-log {
  flex: 1; min-height: 140px; max-height: 320px; overflow: auto; border: 1px solid var(--line); border-radius: 8px;
  padding: 8px; font: 12.5px/1.45 ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; background: var(--input); white-space: pre-wrap;
}
.status-log .warn { color: var(--warn-strong); }
.status-log .err { color: var(--bad); }
.status-log .ok { color: var(--safe); }

.results { display: flex; flex-direction: column; gap: 10px; }
.chart-box { position: relative; border: 1px solid var(--line); border-radius: 8px; overflow: hidden; background: var(--chart-bg); }
.chart-box canvas { display: block; width: 100%; height: 100%; }
.chart-box.main { flex: 1; min-height: 340px; }
.chart-box.main canvas { cursor: crosshair; }
.chart-box.profile { height: 220px; }

.keelcheck { display: block; min-width: 0; }
.status-line span:last-child { min-width: 0; overflow-wrap: anywhere; }
.check-head { display: flex; justify-content: space-between; gap: 8px; align-items: baseline; flex-wrap: wrap; }
.check-head .hint { color: var(--muted); font-size: 12px; }
#keel { width: 100%; accent-color: var(--accent); margin: 6px 0 8px; }
.readouts { display: grid; grid-template-columns: repeat(auto-fit, minmax(128px, 1fr)); gap: 6px; }
.readouts > div { border: 1px solid var(--line); border-radius: 8px; padding: 5px 8px; display: flex; flex-direction: column; gap: 1px; }
.readouts span { font-size: 11.5px; color: var(--muted); }
.readouts output { font-variant-numeric: tabular-nums; font-weight: 600; font-size: 15px; }
.readouts output.neg { color: var(--bad); }
.status-line { display: flex; align-items: center; gap: 10px; margin-top: 10px; font-weight: 700; }
.lamp { width: 18px; height: 18px; border-radius: 50%; background: var(--neutral); flex: none; box-shadow: inset 0 0 0 2px rgba(0,0,0,.12); }
.lamp.ok { background: var(--safe); } .lamp.no { background: var(--warn-strong); } .lamp.bad { background: var(--bad); }
.breakdown { margin-top: 8px; font-size: 13px; }
.breakdown summary { cursor: pointer; color: var(--muted); }
.breakdown table { border-collapse: collapse; margin-top: 6px; min-width: 320px; }
.breakdown td { padding: 3px 10px 3px 0; border-bottom: 1px solid var(--line); font-variant-numeric: tabular-nums; }
.breakdown td:last-child { text-align: right; font-weight: 600; }
.breakdown tr.total td { border-bottom: 0; font-weight: 700; }

/* planner overlay */
.overlay { position: fixed; inset: 0; background: rgba(8, 12, 18, .55); z-index: 50; display: flex; padding: 12px; }
.overlay[hidden] { display: none; }
.planner { background: var(--panel); border-radius: var(--radius); border: 1px solid var(--line); flex: 1; display: flex; flex-direction: column; gap: 8px; padding: 10px; min-width: 0; }
.planner-head { display: flex; justify-content: space-between; gap: 10px; align-items: flex-start; }
.planner-meta { font-weight: 700; font-size: 14px; }
.planner-status { font: 600 13px/1.4 ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; margin-top: 3px; }
.planner-status .state { padding: 1px 7px; border-radius: 5px; color: #fff; }
.planner-status .SAFE { background: var(--safe); } .planner-status .WARNING { background: var(--warn); color: #1d1600; }
.planner-status .RATIO { background: var(--ratio); } .planner-status .GROUNDING { background: var(--bad); }
.planner-grid { flex: 1; display: grid; grid-template-columns: 1fr 1fr; grid-template-rows: 1fr 1fr; gap: 8px; min-height: 0; }
.planner-grid .chart-box { min-height: 180px; }
.planner-controls { display: flex; flex-wrap: wrap; align-items: center; gap: 8px; border-top: 1px solid var(--line); padding-top: 8px; }
.planner-controls .inline { display: inline-flex; align-items: center; gap: 6px; }
.planner-controls select { width: auto; }
#plSlider { flex: 1; min-width: 160px; accent-color: var(--accent); }

.toasts { position: fixed; right: 16px; bottom: 16px; z-index: 80; display: flex; flex-direction: column; gap: 8px; max-width: min(420px, calc(100vw - 32px)); }
.toast { background: var(--panel); border: 1px solid var(--line); border-left: 4px solid var(--warn); border-radius: 8px; padding: 10px 34px 10px 12px; box-shadow: 0 8px 24px rgba(0,0,0,.18); position: relative; white-space: pre-wrap; font-size: 13px; }
.toast.error { border-left-color: var(--bad); } .toast.info { border-left-color: var(--accent); }
.toast strong { display: block; margin-bottom: 2px; }
.toast button { position: absolute; top: 6px; right: 6px; border: 0; background: none; cursor: pointer; color: var(--muted); font-size: 16px; }

.busy { position: relative; pointer-events: none; opacity: .7; }

@media (max-width: 1100px) {
  .layout { grid-template-columns: 1fr; }
  .chart-box.main { min-height: 320px; }
}
@media (max-width: 640px) {
  .layout { padding: 8px; gap: 8px; }
  .actions { grid-template-columns: 1fr 1fr; }
  .row { grid-template-columns: 120px minmax(0, 1fr); }
  .speed-read { padding-left: 128px; }
  .grid2 { grid-template-columns: 1fr; }
  .planner-grid { grid-template-columns: 1fr; grid-template-rows: repeat(4, minmax(200px, 1fr)); overflow: auto; }
  .overlay { padding: 0; }
  .brand p { display: none; }
}
