:root {
  color-scheme: light dark;
  --bg: #ffffff;
  --bg-sunken: #f6f8fa;
  --bg-raised: #ffffff;
  --border: #d0d7de;
  --border-strong: #8c959f;
  --text: #1f2328;
  --text-muted: #59636e;
  --accent: #1f6feb;
  --accent-text: #ffffff;
  --danger: #cf222e;
  --success: #1a7f37;
  --warning: #9a6700;
  --radius: 8px;
  --gap: 16px;
}

/* Three states. "Auto" leaves data-theme unset and follows the OS; an explicit
   choice stamps the attribute and must win in *both* directions, which is why
   the media query excludes a pinned light theme and the dark tokens appear
   twice. color-scheme is set alongside so form controls and scrollbars follow. */
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #0d1117;
    --bg-sunken: #161b22;
    --bg-raised: #161b22;
    --border: #30363d;
    --border-strong: #6e7681;
    --text: #e6edf3;
    --text-muted: #9198a1;
    --accent: #4493f8;
    --accent-text: #0d1117;
    --danger: #f85149;
    --success: #3fb950;
    --warning: #d29922;
  }
}

:root[data-theme="dark"] {
  --bg: #0d1117;
  --bg-sunken: #161b22;
  --bg-raised: #161b22;
  --border: #30363d;
  --border-strong: #6e7681;
  --text: #e6edf3;
  --text-muted: #9198a1;
  --accent: #4493f8;
  --accent-text: #0d1117;
  --danger: #f85149;
  --success: #3fb950;
  --warning: #d29922;
  color-scheme: dark;
}

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

* { box-sizing: border-box; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 15px/1.5 -apple-system, BlinkMacSystemFont, "Segoe UI", Helvetica, Arial, sans-serif;
}

.hidden { display: none !important; }

.boot { display: grid; place-items: center; min-height: 100vh; }
.boot-text { color: var(--text-muted); }

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--gap);
  padding: 12px 24px;
  border-bottom: 1px solid var(--border);
  background: var(--bg-sunken);
  position: sticky;
  top: 0;
  z-index: 5;
}

.brand { font-size: 16px; margin: 0; }
.topbar-actions { display: flex; align-items: center; gap: 12px; }
.identity { color: var(--text-muted); font-size: 13px; }

.view { max-width: 1080px; margin: 0 auto; padding: 24px; }

.banner {
  max-width: 1080px;
  margin: 16px auto 0;
  padding: 10px 14px;
  border-radius: var(--radius);
  border: 1px solid var(--border);
  background: var(--bg-sunken);
  font-size: 14px;
}
.banner.is-error { border-color: var(--danger); color: var(--danger); }
.banner.is-success { border-color: var(--success); color: var(--success); }

h2 { font-size: 20px; margin: 0 0 4px; }
h3 { font-size: 15px; margin: 0 0 12px; }
.subtle { color: var(--text-muted); font-size: 13px; margin: 0 0 20px; }

.card {
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--bg-raised);
  padding: 16px;
  margin-bottom: var(--gap);
}

.row { display: flex; align-items: center; justify-content: space-between; gap: 12px; }
.row-wrap { flex-wrap: wrap; }
.stack { display: flex; flex-direction: column; gap: 4px; }
.actions { display: flex; gap: 8px; flex-wrap: wrap; }
.spacer { flex: 1; }

.grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(220px, 1fr));
  gap: 12px;
}

label { font-size: 13px; color: var(--text-muted); }

input[type="text"], input[type="number"], input[type="password"], select, textarea {
  width: 100%;
  padding: 6px 10px;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--bg);
  color: var(--text);
  font: inherit;
}
textarea { min-height: 74px; resize: vertical; }
input:focus-visible, select:focus-visible, textarea:focus-visible, button:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 1px;
}

.check { display: flex; align-items: center; gap: 8px; }
.check label { color: var(--text); }

.btn {
  padding: 6px 12px;
  border-radius: 6px;
  border: 1px solid var(--border-strong);
  background: var(--bg-sunken);
  color: var(--text);
  font: inherit;
  cursor: pointer;
}
.btn:hover { border-color: var(--accent); }
.btn:disabled { opacity: .55; cursor: not-allowed; }
.btn-primary { background: var(--accent); border-color: var(--accent); color: var(--accent-text); }
.btn-danger { color: var(--danger); border-color: var(--danger); background: transparent; }
.btn-quiet { background: transparent; border-color: transparent; color: var(--text-muted); }
.btn-quiet:hover { color: var(--text); }
.btn-small { padding: 3px 8px; font-size: 13px; }

.badge {
  display: inline-block;
  padding: 1px 8px;
  border-radius: 999px;
  font-size: 12px;
  border: 1px solid var(--border);
  color: var(--text-muted);
  white-space: nowrap;
}
.badge.is-ok { color: var(--success); border-color: var(--success); }
.badge.is-error { color: var(--danger); border-color: var(--danger); }
.badge.is-warn { color: var(--warning); border-color: var(--warning); }

table { width: 100%; border-collapse: collapse; font-size: 14px; }
th, td { text-align: left; padding: 8px 10px; border-bottom: 1px solid var(--border); vertical-align: top; }
th { color: var(--text-muted); font-weight: 600; font-size: 12px; text-transform: uppercase; letter-spacing: .03em; }
tbody tr:last-child td { border-bottom: none; }
.numeric { text-align: right; font-variant-numeric: tabular-nums; }

.link {
  background: none;
  border: none;
  padding: 0;
  color: var(--accent);
  font: inherit;
  cursor: pointer;
  text-align: left;
}
.link:hover { text-decoration: underline; }

.muted { color: var(--text-muted); }
.small { font-size: 13px; }
.truncate { max-width: 320px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.empty { color: var(--text-muted); padding: 20px; text-align: center; }
.scroll-x { overflow-x: auto; }
.mono { font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: 13px; }
.field-help { font-size: 12px; color: var(--text-muted); }
.divider { height: 1px; background: var(--border); margin: 16px 0; border: none; }

.group { margin-bottom: 18px; }
.group:last-child { margin-bottom: 0; }
.group-head {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 6px 10px;
  background: var(--bg-sunken);
  border: 1px solid var(--border);
  border-radius: 6px 6px 0 0;
  border-bottom: none;
}
.group-title { margin: 0; font-size: 14px; }
.group table { border: 1px solid var(--border); border-radius: 0 0 6px 6px; }

.order-buttons { display: flex; gap: 4px; }
.order-buttons .btn { padding: 2px 7px; line-height: 1.2; }

.theme-select {
  width: auto;
  padding: 4px 8px;
  font-size: 13px;
}

.export-area {
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: 12px;
  min-height: 150px;
}

/* -- model chooser --------------------------------------------------------
   Radio cards rather than a select: three options with two meters each do not
   fit in a dropdown, and the comparison is the point of showing them at all. */

/* -- settings form --------------------------------------------------------
   Grouped into sections, each with its own fixed-column grid. Fixed rather
   than auto-fit: auto-fit stretches a group of two to fill the row, so its
   fields no longer line up with the group of four above it, which is most of
   what made the old single-grid form look ragged. Column counts are chosen so
   no group ever leaves one field stranded on a row of its own. */

/* Container queries rather than media queries: .view caps at 1080px, so past
   that width the viewport keeps growing while the form does not. The columns
   should answer to the space the form actually has. */
.form-sections {
  display: grid;
  gap: 20px;
  container-type: inline-size;
  container-name: settings;
}

.section-title {
  margin: 0 0 8px;
  font-size: 12px;
  font-weight: 600;
  letter-spacing: 0.04em;
  text-transform: uppercase;
  color: var(--text-muted);
}

.grid-fixed {
  display: grid;
  grid-template-columns: 1fr;
  gap: 12px;
}

@container settings (min-width: 480px) {
  .grid-fixed { grid-template-columns: repeat(2, minmax(0, 1fr)); }
}

@container settings (min-width: 800px) {
  .grid-fixed { grid-template-columns: repeat(4, minmax(0, 1fr)); }
}

.advanced {
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 10px 12px;
  background: var(--bg-sunken);
}

.advanced > summary {
  cursor: pointer;
  font-size: 12px;
  font-weight: 600;
  letter-spacing: 0.04em;
  text-transform: uppercase;
  color: var(--text-muted);
}

.advanced[open] > summary { margin-bottom: 12px; }

.advanced .check { margin-top: 4px; }

/* auto-fit rather than a fixed count: a fourth model drops into the row on a
   wide screen and wraps on a narrow one, with no layout to revisit. */
.model-cards {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(210px, 1fr));
  gap: 8px;
  align-items: stretch;
}

.model-card {
  display: grid;
  grid-template-columns: auto 1fr;
  gap: 10px;
  /* stretch, not start: the body must fill the card's full height for the
     meters' auto margin to have any free space to push against. */
  align-items: stretch;
  padding: 10px 12px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--bg-raised);
  cursor: pointer;
}

.model-card:hover { border-color: var(--border-strong); }

/* :has lets the selected card light up without a JS class toggle on every
   change; browsers without it simply show the checked radio, which is legible
   on its own. */
.model-card:has(input:checked) {
  border-color: var(--accent);
  background: var(--bg-sunken);
}

.model-card.is-blocked { opacity: 0.6; cursor: not-allowed; }

.model-card input { margin-top: 3px; align-self: start; }

/* A row of cards looks ragged unless the meters line up, so the body is a
   full-height flex column and the meter block is pushed to the bottom with an
   auto margin. Flex rather than grid: an auto margin only absorbs free space
   in grid when a track is sized to take it, which start-packed content is not. */
.model-body { display: flex; flex-direction: column; gap: 3px; height: 100%; }
.model-name { font-weight: 600; }

.meters {
  display: flex;
  flex-wrap: wrap;
  gap: 4px 18px;
  margin-top: auto;
  padding-top: 6px;
}

.meter { display: flex; align-items: center; gap: 6px; }

.meter-label {
  font-size: 0.78rem;
  color: var(--text-muted);
  min-width: 4.6em;
}

.pips { display: inline-flex; gap: 3px; }

.pip {
  width: 11px;
  height: 6px;
  border-radius: 2px;
  background: var(--border);
}

.pip.is-on { background: var(--accent); }

.is-error-text { color: var(--danger); }
