/* The series colours live in app.js (PALETTE) because Plotly needs literal
   values; the tokens here are the same palettes for the page chrome. Each
   mode's steps were validated against its own --bg surface for lightness
   band, chroma, CVD separation and contrast.

   Default is whatever the OS asks for; an explicit choice from the toggle
   sets data-theme on <html> and wins over it in both directions. */
:root {
  color-scheme: light;

  --bg: #ffffff;            /* page, and the surface the charts sit on */
  --raised: #f0f2f6;        /* sidebar, table header */
  --text: #1a1a19;
  --muted: #52514e;
  --border: #d6d9de;
  --border-soft: #e7e9ed;
  --accent: #2a78d6;
  --sel-bg: #eef3fb;
  --row-hover: #f6f8fa;
  --error: #c0392b;
  --warn: #a15c00;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    color-scheme: dark;

    --bg: #15161a;
    --raised: #1d1f24;
    --text: #e8e8e5;
    --muted: #9ea3ad;
    --border: #2f333c;
    --border-soft: #24272e;
    --accent: #3987e5;
    --sel-bg: #22252c;
    --row-hover: #1b1e24;
    --error: #e66767;
    --warn: #e3b341;
  }
}

:root[data-theme="dark"] {
  color-scheme: dark;

  --bg: #15161a;
  --raised: #1d1f24;
  --text: #e8e8e5;
  --muted: #9ea3ad;
  --border: #2f333c;
  --border-soft: #24272e;
  --accent: #3987e5;
  --sel-bg: #22252c;
  --row-hover: #1b1e24;
  --error: #e66767;
  --warn: #e3b341;
}

* { box-sizing: border-box; }

/* The rules below set `display` on #boot and #app, which would otherwise
   outrank the user-agent's [hidden] rule and leave both permanently shown. */
[hidden] { display: none !important; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: "Source Sans 3", "Source Sans Pro", -apple-system, BlinkMacSystemFont,
               "Segoe UI", Roboto, sans-serif;
  font-size: 15px;
  line-height: 1.5;
}

/* ---------- boot overlay ---------- */

#boot {
  position: fixed;
  inset: 0;
  display: grid;
  place-items: center;
  background: var(--bg);
  z-index: 10;
  padding: 16px;
}

.boot-card { max-width: 420px; text-align: center; }
.boot-note { color: var(--muted); font-size: 13px; }
#boot-msg { font-size: 16px; margin-bottom: 4px; }
#boot.error .spinner { display: none; }
#boot.error #boot-msg { color: var(--error); white-space: pre-wrap; text-align: left; }

.spinner {
  width: 34px; height: 34px;
  margin: 0 auto 18px;
  border: 3px solid var(--border);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 0.9s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* ---------- layout ---------- */

#app { display: flex; align-items: flex-start; min-height: 100vh; }

.sidebar {
  flex: 0 0 320px;
  background: var(--raised);
  border-right: 1px solid var(--border-soft);
  padding: 16px 20px 24px;
  position: sticky;
  top: 0;
  max-height: 100vh;
  overflow-y: auto;
}

.content {
  flex: 1 1 auto;
  min-width: 0;
  padding: 24px 32px 40px;
}

h1 { font-size: 30px; font-weight: 700; margin: 0 0 18px; }
h2 { font-size: 15px; font-weight: 600; margin: 16px 0 6px; }
h2:first-child { margin-top: 0; }
h3 { font-size: 19px; font-weight: 600; margin: 0 0 2px; }

/* ---------- lab branding ---------- */

/* The logo's own blue holds at least 4:1 against both themes' backgrounds,
   and its background is transparent, so one image serves light and dark. */
.brand {
  display: block;
  width: 160px;
  height: auto;
  margin: 0 auto 20px;
}
.boot-card .brand { margin-bottom: 28px; }
.details-body .brand { margin: 16px auto 4px; }

/* The icon is the full-size original from branding/, scaled down by the browser. */
.title { display: flex; align-items: center; gap: 12px; }
.title-icon { flex: none; width: auto; height: 36px; }

/* ---------- theme toggle ---------- */

.theme-toggle {
  display: flex;
  align-items: center;
  gap: 8px;
  width: 100%;
  margin-bottom: 14px;
  padding: 5px 10px;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--bg);
  color: var(--text);
  font: inherit;
  font-size: 13px;
  cursor: pointer;
}

.theme-toggle:hover { border-color: var(--accent); }
.theme-toggle:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; }

/* ---------- export ---------- */

.export-buttons { display: flex; flex-direction: column; gap: 6px; margin-bottom: 14px; }

.export-button {
  width: 100%;
  padding: 5px 10px;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--bg);
  color: var(--text);
  font: inherit;
  font-size: 13px;
  text-align: left;
  cursor: pointer;
}

.export-button:hover { border-color: var(--accent); }
.export-button:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; }

/* Glyph is set by app.js, which already knows which mode is showing */
.theme-icon {
  flex: 0 0 auto;
  font-size: 14px;
  line-height: 1;
  color: var(--muted);
}

/* ---------- controls ---------- */

/* One control: label and number box on a line, the slider beneath */
.control { margin-bottom: 10px; }
.control-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
}
.control-head label { font-size: 13px; color: var(--muted); }
.control-head input[type="number"] { flex: 0 0 84px; padding: 2px 6px; }

input[type="range"] { display: block; width: 100%; margin: 4px 0 0; accent-color: var(--accent); }

input[type="number"] {
  width: 100%;
  padding: 5px 7px;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--bg);
  color: var(--text);
  font: inherit;
  font-size: 14px;
}

input[type="number"]:focus-visible,
input[type="range"]:focus-visible,
input[type="radio"]:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 1px;
}

/* Each group on one line. A floated legend sits in the fieldset's content
   like any other box, so it can lead the line as a fixed-width label. */
.radios {
  border: 0;
  padding: 0;
  margin: 0 0 6px;
}
.radios legend {
  float: left;
  width: 58px;
  padding: 0;
  font-size: 13px;
  line-height: 24px;
  color: var(--muted);
}
.radios label { display: inline-block; margin-right: 12px; font-size: 14px; line-height: 24px; cursor: pointer; }
.radios label:last-child { margin-right: 0; }
.radios input { accent-color: var(--accent); margin: 0 5px 0 0; vertical-align: -1px; }

hr { border: 0; border-top: 1px solid var(--border); margin: 14px 0; }

.blurb { font-size: 13px; color: var(--muted); margin: 0 0 10px; }

details { font-size: 14px; }
summary { cursor: pointer; font-weight: 600; padding: 4px 0; }
.details-body ul { margin: 4px 0 10px; padding-left: 20px; }
.details-body p { margin: 6px 0 2px; }

/* ---------- plots and table ---------- */

.plot { width: 100%; height: 500px; }

.columns {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 28px;
  margin-top: 8px;
}

.hint { color: var(--muted); font-size: 13px; margin: 0 0 8px; }

.table-wrap {
  height: 400px;
  overflow: auto;
  border: 1px solid var(--border);
  border-radius: 6px;
}

table { border-collapse: collapse; width: 100%; font-size: 13px; }

thead th {
  position: sticky;
  top: 0;
  background: var(--raised);
  text-align: left;
  font-weight: 600;
  padding: 7px 9px;
  border-bottom: 1px solid var(--border);
  white-space: nowrap;
}

tbody td {
  padding: 6px 9px;
  border-bottom: 1px solid var(--border-soft);
  white-space: nowrap;
}

tbody tr { cursor: pointer; }
tbody tr:hover { background: var(--row-hover); }
/* The selected row is marked in its own polarization colour, set inline by
   app.js, so the table and the level-diagram arrows agree. */
tbody tr.selected { background: var(--sel-bg); }
/* In the individual-line view, the other lines the laser on the selected one
   also drives: the same polarization bar, on a fainter background. */
tbody tr.driven { background: color-mix(in srgb, var(--sel-bg) 45%, transparent); }

/* Headings are buttons, so sorting works from the keyboard too */
thead th button {
  all: unset;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  gap: 4px;
}
thead th button:hover { color: var(--accent); }
thead th button:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
.sort-mark { font-size: 10px; color: var(--muted); min-width: 8px; }
thead th[aria-sort] .sort-mark { color: var(--accent); }

.table-mode {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  font-size: 13px;
  margin: 0 0 8px;
  cursor: pointer;
}
.table-mode input { accent-color: var(--accent); margin: 0; }
.table-mode input:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; }

.col-pol { width: 1%; }
td.pol { font-weight: 600; }

/* A group reaching wider than the Doppler width has not really merged into
   one peak, so the number is marked rather than left to be read past. */
td.span { font-variant-numeric: tabular-nums; }
td.span.wide { color: var(--warn); font-weight: 600; }

footer {
  text-align: center;
  color: var(--muted);
  font-size: 12px;
  margin-top: 28px;
}
footer a { color: var(--muted); }
footer a:hover { color: var(--text); }

@media (max-width: 1100px) {
  .columns { grid-template-columns: 1fr; }
}

@media (max-width: 800px) {
  #app { display: block; }
  .sidebar { position: static; max-height: none; flex-basis: auto; border-right: 0; }
  .content { padding: 16px; }
}
