:root {
  --bg: #f4f5f7;
  --card: #ffffff;
  --text: #1c1f24;
  --muted: #5d6470;
  --accent: #3b5bdb;
  --accent-text: #ffffff;
  --border: #e1e4ea;
  --error-bg: #fdecec;
  --error-text: #a61b1b;
  --gap: 16px;
  color-scheme: light dark;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #14161a;
    --card: #1e2126;
    --text: #e8eaee;
    --muted: #9aa1ad;
    --accent: #7b93f5;
    --accent-text: #0e1116;
    --border: #2c3038;
    --error-bg: #3a1d1d;
    --error-text: #ffb4b4;
  }
}

@media (max-height: 700px) {
  :root { --gap: 10px; }
}

* { box-sizing: border-box; }

html, body { height: 100%; }

body {
  margin: 0;
  display: flex;
  justify-content: center;
  align-items: center;
  padding: 12px;
  background: var(--bg);
  color: var(--text);
  font: 15px/1.4 system-ui, -apple-system, "Segoe UI", sans-serif;
}

/* The whole app fits one screen; the card only scrolls as a last resort. */
.card {
  width: 100%;
  max-width: 640px;
  height: 100%;
  max-height: 760px;
  overflow-y: auto;
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: 16px;
  padding: 16px 20px;
  display: flex;
  flex-direction: column;
  gap: var(--gap);
}

.view {
  flex: 1;
  min-height: 0;
  display: flex;
  flex-direction: column;
  gap: var(--gap);
}

.top {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  flex-wrap: wrap;
}

h1 { margin: 0; font-size: 1.35rem; }
.hint, .summary { margin: 0; color: var(--muted); font-size: 0.9rem; }
.prompt { margin: 0; font-size: 1.1rem; font-weight: 600; text-align: center; }

select, button, input { font: inherit; color: var(--text); }

button {
  padding: 8px 14px;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: var(--bg);
  cursor: pointer;
}
button:disabled { opacity: 0.45; cursor: default; }

.primary, .transport {
  background: var(--accent);
  border-color: var(--accent);
  color: var(--accent-text);
  font-weight: 600;
}

/* Tabs and segmented choices */
.tabs, .segments {
  display: flex;
  padding: 3px;
  gap: 3px;
  border-radius: 10px;
  background: var(--bg);
  border: 1px solid var(--border);
}
.tab, .segment {
  border: 0;
  background: transparent;
  padding: 6px 12px;
  border-radius: 7px;
  color: var(--muted);
}
.tab.active, .segment.active {
  background: var(--card);
  color: var(--text);
  font-weight: 600;
  box-shadow: 0 1px 2px rgb(0 0 0 / 0.12);
}
.segments .segment { flex: 1 1 0; min-width: 0; padding: 6px 4px; hyphens: manual; overflow-wrap: break-word; }

/* Slider view */
.controls-row {
  display: flex;
  gap: 8px;
  align-items: end;
  flex-wrap: wrap;
}
.source { flex: 1 1 160px; display: grid; gap: 2px; }
.view > .source { flex: none; }
.source label, .field > span:first-child { font-weight: 600; font-size: 0.9rem; }
select {
  padding: 8px 10px;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: var(--bg);
}
.transport { min-width: 128px; }

.slider { display: grid; gap: 2px; }
.slider-head { display: flex; justify-content: space-between; gap: 12px; }
.slider-head label { font-weight: 600; }
.slider-head output { color: var(--muted); font-variant-numeric: tabular-nums; }
input[type="range"] {
  width: 100%;
  height: 28px;
  margin: 0;
  accent-color: var(--accent);
}

/* Setup form */
.form-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(120px, 1fr));
  gap: 8px 12px;
}
.field { display: grid; gap: 2px; }
.input-unit { display: flex; align-items: center; gap: 6px; }
.input-unit input {
  width: 100%;
  min-width: 0;
  padding: 7px 8px;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: var(--bg);
}
.input-unit span { color: var(--muted); }
.input-unit input[readonly] { color: var(--muted); border-style: dashed; }
.view > .primary { align-self: stretch; padding: 12px; margin-top: auto; }

/* Running tests */
.progress-head { display: grid; gap: 4px; font-variant-numeric: tabular-nums; }
progress { width: 100%; height: 6px; accent-color: var(--accent); }

.answers {
  display: grid;
  grid-template-columns: repeat(5, 1fr);
  gap: 6px;
  flex: 1;
  max-height: 160px;
}
.answer { padding: 8px 4px; font-weight: 600; }

.hear {
  flex: 1;
  min-height: 120px;
  max-height: 260px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 4px;
  font-size: 1.3rem;
  font-weight: 700;
  background: var(--accent);
  border-color: var(--accent);
  color: var(--accent-text);
  user-select: none;
  touch-action: manipulation;
}
.hear small { font-size: 0.8rem; font-weight: 400; opacity: 0.85; }

.steps { margin: 0; padding-left: 20px; display: grid; gap: 6px; }

.actions { display: flex; flex-wrap: wrap; gap: 8px; }

/* Results */
.chart { flex: 1; min-height: 0; display: flex; flex-direction: column; gap: var(--gap); }
.chart-canvas {
  flex: 1;
  min-height: 240px;
  border: 1px solid var(--border);
  border-radius: 8px;
  overflow: hidden;
}
.chart-canvas canvas { display: block; width: 100%; height: 100%; }

/* Raw data table; scrolls inside the card so the page itself never does. */
.result-table-wrap {
  flex: 1;
  min-height: 0;
  overflow: auto;
  border: 1px solid var(--border);
  border-radius: 8px;
}
.result-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.9rem;
  font-variant-numeric: tabular-nums;
}
.result-table caption {
  padding: 8px 10px;
  text-align: left;
  font-weight: 600;
}
.result-table th, .result-table td {
  padding: 5px 10px;
  text-align: left;
  border-top: 1px solid var(--border);
  white-space: nowrap;
}
.result-table th {
  position: sticky;
  top: 0;
  background: var(--bg);
  font-weight: 600;
}
.result-table td.num { text-align: right; }
.result-table tbody tr:nth-child(even) { background: var(--bg); }

/* Configuration view; the option groups scroll inside the card. */
.config-body {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  display: grid;
  align-content: start;
  gap: 12px;
  padding-right: 2px;
}
.group {
  margin: 0;
  min-width: 0;
  padding: 6px 12px 12px;
  border: 1px solid var(--border);
  border-radius: 8px;
  display: grid;
  gap: 8px;
}
.group legend { padding: 0 4px; font-weight: 600; }
.group .sub { margin: 4px 0 -2px; font-size: 0.85rem; font-weight: 600; color: var(--muted); }
.group .form-grid { align-items: end; }
.form-grid.wide-first { grid-template-columns: 2fr 1fr; }
.checks { display: flex; flex-wrap: wrap; gap: 4px 16px; }
.check { display: inline-flex; align-items: center; gap: 6px; font-size: 0.9rem; cursor: pointer; }
.check input { margin: 0; accent-color: var(--accent); }
.check:has(input:disabled) { opacity: 0.55; cursor: default; }

a.button {
  display: inline-block;
  padding: 8px 14px;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: var(--bg);
  color: var(--text);
  text-decoration: none;
}

/* QR-code view */
.qr {
  flex: 1 1 0;
  min-height: 0;
  display: flex;
  justify-content: center;
}
.qr svg {
  display: block;
  height: 100%;
  max-height: 340px;
  min-height: 140px;
  width: auto;
  max-width: 100%;
  aspect-ratio: 1;
  border-radius: 8px;
}
.qr-url {
  font-size: 0.85rem;
  color: var(--muted);
  text-align: center;
  word-break: break-all;
}
.qr-summary {
  margin: 0;
  padding-left: 20px;
  font-size: 0.9rem;
  display: grid;
  gap: 2px;
  flex: none;
  max-height: 7.5em;
  overflow-y: auto;
}

.error {
  margin: 0;
  padding: 8px 12px;
  border-radius: 8px;
  background: var(--error-bg);
  color: var(--error-text);
}

@media (max-width: 420px) {
  body { padding: 6px; }
  .card { padding: 12px 14px; border-radius: 12px; }
  /* Four tabs share the full width below the title. */
  .tabs { width: 100%; }
  .tab { flex: 1 1 auto; padding: 6px 4px; font-size: 0.85rem; }
  .form-grid.wide-first { grid-template-columns: 1fr; }
  .segment, .field > span:first-child { font-size: 0.85rem; }
}
