/* Settlement Analysis. Colours and type as apps.onurkoc.net.
   Colour scheme: the system setting (prefers-color-scheme), unless
   data-theme="light" or "dark" is set on <html> by the switch in the
   header (app/ui/theme.js). [data-theme="light"] also gives the hidden
   element #theme-light the light colours for the PNG export. */
:root, [data-theme="light"] {
  --bg: #f6f5f2;
  --surface: #ffffff;
  --text: #1c1b19;
  --muted: #5f5c55;
  --line: #dedbd3;
  --grid: #eeece6;
  --accent: #c2410c;
  --accent-soft: #fbe9df;
  --shadow: 0 1px 2px rgba(0,0,0,.04), 0 8px 24px rgba(0,0,0,.06);
  --error: #b42318;
  --ok: #2e7d4f;
  /* plots */
  --tunnel1: #2563eb;
  --tunnel2: #0d9488;
  --sh: #7c3aed;
  --building: #65a30d;
  --tunnel-fill: #a8a29e;
  --field: #8a867d;
  /* damage categories 0 ... 4-5, the same in light and dark mode:
     dark text on these colours >= 4.7:1 (WCAG AA), the colours against
     the light and the dark surface >= 3.6:1 (lines in the plots) */
  --cat0: #2c9959;
  --cat1: #74911f;
  --cat2: #a7810e;
  --cat3: #d96412;
  --cat4: #e15a56;
  --cat-text: #1c1b19;
  --band-alpha: 0.16;
  --font: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  --font-plot: system-ui, "Segoe UI", Roboto, Arial, sans-serif;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #141413;
    --surface: #1d1c1a;
    --text: #f1efe9;
    --muted: #a6a29a;
    --line: #34322e;
    --grid: #2a2926;
    --accent: #f07a3e;
    --accent-soft: #3a2418;
    --shadow: 0 1px 2px rgba(0,0,0,.3), 0 8px 24px rgba(0,0,0,.35);
    --error: #f97066;
    --ok: #4cc27f;
    --tunnel1: #60a5fa;
    --tunnel2: #2dd4bf;
    --sh: #a78bfa;
    --building: #84cc16;
    --tunnel-fill: #57534e;
    --field: #8f8b83;
    --band-alpha: 0.22;
    color-scheme: dark;
  }
}

/* dark colours chosen in the header, also with a light system */
:root[data-theme="dark"] {
  --bg: #141413;
  --surface: #1d1c1a;
  --text: #f1efe9;
  --muted: #a6a29a;
  --line: #34322e;
  --grid: #2a2926;
  --accent: #f07a3e;
  --accent-soft: #3a2418;
  --shadow: 0 1px 2px rgba(0,0,0,.3), 0 8px 24px rgba(0,0,0,.35);
  --error: #f97066;
  --ok: #4cc27f;
  --tunnel1: #60a5fa;
  --tunnel2: #2dd4bf;
  --sh: #a78bfa;
  --building: #84cc16;
  --tunnel-fill: #57534e;
  --field: #8f8b83;
  --band-alpha: 0.22;
  color-scheme: dark;
}

* { box-sizing: border-box; }
/* hidden wins over display: grid/flex of the layout rules */
[hidden] { display: none !important; }
html, body { margin: 0; }
body {
  background: var(--bg);
  color: var(--text);
  font-family: var(--font);
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
}
.wrap { max-width: 1440px; margin: 0 auto; padding: 0 16px; }
a { color: var(--accent); }
button, input { font: inherit; color: inherit; }

/* header and tabs */
.top { border-bottom: 1px solid var(--line); background: var(--surface); }
.top-row { display: flex; align-items: flex-end; justify-content: space-between; gap: 8px 16px; padding-top: 20px; flex-wrap: wrap; }
.eyebrow {
  font-size: 12px; letter-spacing: .12em; text-transform: uppercase;
  color: var(--accent); font-weight: 600; margin: 0 0 4px;
}
h1 { font-size: clamp(24px, 4vw, 32px); line-height: 1.1; margin: 0 0 12px; letter-spacing: -.02em; }
.top-actions { display: flex; align-items: center; gap: 14px; flex-wrap: wrap; justify-content: flex-end; margin-bottom: 12px; }
.back { font-size: 14px; font-weight: 600; text-decoration: none; white-space: nowrap; }
.seg.theme-switch { margin: 0; }
.seg.theme-switch span { padding: 3px 10px; font-size: 12px; }
.tabs { display: flex; gap: 4px; }
.tabs [role="tab"] {
  background: none; border: 0; border-bottom: 2px solid transparent;
  padding: 8px 14px; cursor: pointer; color: var(--muted); font-weight: 600;
}
.tabs [role="tab"][aria-selected="true"] { color: var(--text); border-bottom-color: var(--accent); }

main { padding-top: 16px; padding-bottom: 40px; }
.card {
  background: var(--surface); border: 1px solid var(--line); border-radius: 14px;
  box-shadow: var(--shadow);
}

/* layout: inputs left, plots and results right; one column on a phone */
.calc { display: grid; gap: 16px; grid-template-columns: minmax(0, 1fr); }
@media (min-width: 900px) {
  .calc { grid-template-columns: 330px minmax(0, 1fr); align-items: start; }
  .inputs { position: sticky; top: 12px; max-height: calc(100vh - 24px); overflow: auto; }
  .inputs > summary { display: none; }
}
.main { display: grid; gap: 16px; min-width: 0; }

/* inputs */
.inputs { padding: 4px 14px 14px; }
.inputs > summary {
  cursor: pointer; font-weight: 700; padding: 10px 0; list-style-position: inside;
}
fieldset { border: 0; border-top: 1px solid var(--line); margin: 10px 0 0; padding: 10px 0 0; min-width: 0; }
fieldset:first-of-type { border-top: 0; }
legend { font-weight: 700; font-size: 15px; padding: 0; }
.tunnel h3 { font-size: 13px; margin: 10px 0 2px; color: var(--accent); text-transform: uppercase; letter-spacing: .08em; }
.field { margin: 8px 0; }
.field label { display: block; font-size: 13px; color: var(--muted); }
.unit { color: var(--muted); }
.pair { display: grid; grid-template-columns: minmax(0, 1fr) 92px; gap: 10px; align-items: center; }
.pair input[type="range"] { width: 100%; accent-color: var(--accent); }
.pair input[type="text"] {
  width: 100%; padding: 4px 6px; border: 1px solid var(--line); border-radius: 6px;
  background: var(--bg); text-align: right;
}
.field.invalid input[type="text"] { border-color: var(--error); outline: 1px solid var(--error); }
.err { color: var(--error); font-size: 12px; min-height: 0; }
.seg { display: inline-flex; border: 1px solid var(--line); border-radius: 8px; overflow: hidden; margin: 6px 0 2px; }
.seg label { position: relative; }
.seg input { position: absolute; opacity: 0; inset: 0; cursor: pointer; }
.seg span { display: block; padding: 4px 12px; font-size: 13px; }
.seg input:checked + span { background: var(--accent-soft); color: var(--accent); font-weight: 600; }
.seg input:focus-visible + span { outline: 2px solid var(--accent); outline-offset: -2px; }
.check { display: flex; gap: 8px; align-items: center; font-size: 13px; margin: 10px 0 4px; }
.check input { accent-color: var(--accent); }

/* plots */
.plots { padding: 8px 8px 4px; min-width: 0; overflow: hidden; }
/* at most as wide as in the earlier app (950 px), narrower on a small screen */
.plot { width: 100%; min-width: 0; max-width: 950px; }
.plot-bar { display: flex; justify-content: flex-end; padding: 4px 8px 0; max-width: 950px; }
.plot-bar .check { margin: 0; }
.msg {
  border: 1px solid var(--error); color: var(--error); background: var(--surface);
  border-radius: 10px; padding: 8px 12px; font-weight: 600;
}
body.stale .plots, body.stale .results { opacity: .45; }

/* results */
.results { padding: 12px 16px 16px; }
.results-head { display: flex; justify-content: space-between; align-items: center; gap: 12px; flex-wrap: wrap; }
.results-head h2 { font-size: 18px; margin: 0; }
.actions { display: flex; gap: 8px; }
.form-actions { justify-content: flex-end; margin-top: 12px; }
.actions button {
  border: 1px solid var(--line); background: var(--bg); border-radius: 8px; padding: 6px 12px;
  cursor: pointer; font-size: 14px; font-weight: 600;
}
.actions button:hover:not(:disabled) { border-color: var(--accent); color: var(--accent); }
.actions button:disabled { opacity: .5; cursor: default; }
.results-grid { display: grid; gap: 4px 28px; grid-template-columns: repeat(auto-fit, minmax(min(100%, 340px), 1fr)); margin-top: 8px; }
.results-grid table { border-collapse: collapse; width: 100%; font-size: 14px; align-self: start; }
.results-grid caption {
  text-align: left; font-weight: 700; font-size: 13px; color: var(--accent);
  text-transform: uppercase; letter-spacing: .06em; padding: 10px 0 4px;
}
.results-grid th { text-align: left; font-weight: 400; color: var(--muted); padding: 3px 8px 3px 0; white-space: nowrap; }
.results-grid td { padding: 3px 0; border-top: 1px solid var(--grid); }
.results-grid th { border-top: 1px solid var(--grid); }
.results-grid td.value { text-align: right; font-variant-numeric: tabular-nums; font-weight: 600; white-space: nowrap; }
.results-grid td.unit { color: var(--muted); padding-left: 6px; width: 4.5em; font-size: 13px; }
.results-grid td.category { white-space: normal; }
.chip {
  display: inline-block; padding: 1px 8px; border-radius: 6px;
  color: var(--cat-text); font-weight: 700;
}
.cat0 { background: var(--cat0); } .cat1 { background: var(--cat1); } .cat2 { background: var(--cat2); }
.cat3 { background: var(--cat3); } .cat4 { background: var(--cat4); }
td.ok { color: var(--ok); }
td.fail { color: var(--error); }

.info { padding: 16px 20px 24px; max-width: 980px; }
.info h2 { margin: 0 0 6px; }
.info h3 { font-size: 16px; margin: 22px 0 6px; color: var(--accent); }
.info p, .info li { max-width: 78ch; }
.info .note { border-left: 3px solid var(--accent); padding: 4px 10px; background: var(--accent-soft); border-radius: 4px; }
.info .small { font-size: 13px; color: var(--muted); }
.info .symbols { font-size: 14px; color: var(--muted); }
.formulas { overflow-x: auto; overflow-y: hidden; padding: 2px 0; }
.formulas .katex-display { margin: 8px 0; text-align: left; }
.formulas .katex-display > .katex { text-align: left; }
.katex { font-size: 1.08em; }
@media (max-width: 599px) { .formulas .katex { font-size: 0.95em; } }
.table-wrap { overflow-x: auto; }
table.data { border-collapse: collapse; font-size: 14px; margin: 6px 0; }
table.data th, table.data td { text-align: left; padding: 4px 14px 4px 0; border-bottom: 1px solid var(--grid); }
table.data th { color: var(--muted); font-weight: 600; }
.sources li { margin: 4px 0; }
footer { border-top: 1px solid var(--line); padding: 20px 0 32px; color: var(--muted); font-size: 14px; }
footer a { color: inherit; }
