:root {
  color-scheme: light;
  --bg: #f3f5f8;
  --surface: #ffffff;
  --surface-2: #e8edf3;
  --border: #d3dae3;
  --text: #11161c;
  --text-muted: #56606c;
  --accent: #2a78d6;
  --accent-ink: #ffffff;
  --danger: #c93a39;
  --danger-ink: #ffffff;
  --warn-bg: #fff3d1;
  --warn-border: #e0a400;
  --warn-ink: #5c4000;
  --crit-bg: #fde6e5;
  --crit-border: #d64545;
  --crit-ink: #7a1414;
  --chart-bg: #ffffff;
  --chart-grid: #e3e8ee;
  --series-1: #2a78d6;
  --series-2: #eb6834;
  --font: system-ui, -apple-system, "Segoe UI", Roboto, "Hiragino Sans", "Noto Sans JP", sans-serif;
  --font-num: ui-monospace, "SF Mono", Menlo, Consolas, "Roboto Mono", monospace;
  --radius: 10px;
}

@media (prefers-color-scheme: dark) {
  :root {
    color-scheme: dark;
    --bg: #0f1216;
    --surface: #181c22;
    --surface-2: #232933;
    --border: #2f3743;
    --text: #eef1f5;
    --text-muted: #a3adb9;
    --accent: #3987e5;
    --accent-ink: #ffffff;
    --danger: #e05554;
    --danger-ink: #ffffff;
    --warn-bg: #3a2d07;
    --warn-border: #c98500;
    --warn-ink: #ffdc85;
    --crit-bg: #3d1515;
    --crit-border: #e66767;
    --crit-ink: #ffc2c2;
    --chart-bg: #181c22;
    --chart-grid: #2a313b;
    --series-1: #3987e5;
    --series-2: #d95926;
  }
}

* { box-sizing: border-box; }

html { -webkit-text-size-adjust: 100%; text-size-adjust: 100%; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: var(--font);
  font-size: 15px;
  line-height: 1.45;
  padding: 0 max(16px, env(safe-area-inset-right)) max(24px, env(safe-area-inset-bottom)) max(16px, env(safe-area-inset-left));
  overflow-x: hidden;
}

main, .appbar { max-width: 640px; margin: 0 auto; }

.num, .lp, .stats dd, .clock, .dose td, .record-status, input[type="number"] {
  font-variant-numeric: tabular-nums;
}

/* App bar and top-level views */
.appbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding-top: max(10px, env(safe-area-inset-top));
  padding-bottom: 10px;
}
.appbar h1 { margin: 0; font-size: 17px; letter-spacing: 0.02em; }
.viewtabs, .seg {
  display: inline-flex;
  background: var(--surface-2);
  border-radius: 8px;
  padding: 3px;
  gap: 2px;
}
.viewtabs button, .seg button {
  appearance: none;
  border: 0;
  background: transparent;
  color: var(--text-muted);
  font: inherit;
  font-size: 14px;
  min-height: 34px;
  padding: 0 14px;
  border-radius: 6px;
  cursor: pointer;
}
.viewtabs button[aria-selected="true"], .seg button[aria-pressed="true"] {
  background: var(--surface);
  color: var(--text);
  font-weight: 600;
  box-shadow: 0 1px 2px rgb(0 0 0 / 0.12);
}

/* Warnings */
.warnings { display: grid; gap: 6px; }
.warnings:empty { display: none; }
.warning {
  margin: 0;
  padding: 8px 12px;
  border-radius: 8px;
  border-left: 4px solid var(--warn-border);
  background: var(--warn-bg);
  color: var(--warn-ink);
  font-size: 14px;
  overflow-wrap: anywhere;
}
.warning.crit { border-left-color: var(--crit-border); background: var(--crit-bg); color: var(--crit-ink); font-weight: 600; }
.warning.info { border-left-color: var(--accent); background: var(--surface-2); color: var(--text); }

/* Readout */
.readout { text-align: center; padding: 8px 0 4px; }
.lp {
  display: block;
  font-family: var(--font-num);
  font-size: clamp(64px, 24vw, 128px);
  font-weight: 600;
  line-height: 1;
  letter-spacing: -0.03em;
}
.unit { color: var(--text-muted); font-size: 16px; margin-top: 4px; }

.stats {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 6px;
  margin: 12px 0 0;
}
.stats div {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 6px 4px;
  text-align: center;
  min-width: 0;
}
.stats dt { color: var(--text-muted); font-size: 12px; }
.stats dd { margin: 0; font-family: var(--font-num); font-size: 20px; font-weight: 600; }

.clock {
  text-align: center;
  font-family: var(--font-num);
  font-size: clamp(26px, 9vw, 40px);
  margin: 12px 0;
}

/* Buttons */
.controls { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.controls.single { grid-template-columns: 1fr; margin-top: 12px; }
.btn {
  appearance: none;
  font: inherit;
  font-size: 15px;
  min-height: 44px;
  padding: 0 12px;
  border-radius: var(--radius);
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  cursor: pointer;
}
.btn.primary {
  min-height: 52px;
  font-size: 17px;
  font-weight: 600;
  border-color: var(--accent);
  background: var(--accent);
  color: var(--accent-ink);
}
.btn.primary.active { background: var(--danger); border-color: var(--danger); color: var(--danger-ink); }
.btn:disabled { opacity: 0.45; cursor: default; }
.btn:focus-visible, .tabs button:focus-visible, .viewtabs button:focus-visible, .seg button:focus-visible,
select:focus-visible, input:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}
.record-status { color: var(--text-muted); font-size: 14px; text-align: center; margin: 8px 0 14px; }
.record-status.recording { color: var(--danger); font-weight: 600; }

/* Tabs and panels */
.tabs {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 4px;
  background: var(--surface-2);
  border-radius: var(--radius);
  padding: 4px;
}
.tabs button {
  appearance: none;
  border: 0;
  background: transparent;
  color: var(--text-muted);
  font: inherit;
  font-size: 14px;
  min-height: 38px;
  border-radius: 7px;
  cursor: pointer;
  min-width: 0;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.tabs button[aria-selected="true"] { background: var(--surface); color: var(--text); font-weight: 600; }

.panel {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 10px;
  margin-top: 8px;
}
.panel-head { display: flex; flex-wrap: wrap; align-items: center; justify-content: space-between; gap: 8px; }
.caption { margin: 0 0 6px; color: var(--text-muted); font-size: 13px; }
.panel-head .caption { margin: 0; }
.caption strong { color: var(--text); }
.chart { display: block; width: 100%; height: 220px; margin-top: 6px; }

.dose { width: 100%; border-collapse: collapse; font-size: 14px; }
.dose th, .dose td { padding: 8px 6px; border-bottom: 1px solid var(--border); }
.dose thead th { color: var(--text-muted); font-weight: 600; text-align: right; }
.dose tbody th { text-align: left; font-weight: 400; color: var(--text-muted); }
.dose td { text-align: right; font-family: var(--font-num); }

/* Cards: settings and diagnostics */
.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 12px;
  margin-top: 16px;
}
.card h2 { font-size: 15px; margin: 0 0 8px; }
.field {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  padding: 6px 0;
  border-top: 1px solid var(--border);
}
.field:first-of-type { border-top: 0; }
.field label { font-size: 14px; min-width: 0; }
select, input[type="number"] {
  font: inherit;
  font-size: 16px; /* >= 16px avoids iOS zoom on focus */
  min-height: 40px;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: var(--bg);
  color: var(--text);
  padding: 0 8px;
  max-width: 100%;
}
.stepper { display: flex; gap: 4px; align-items: center; }
.stepper .btn { min-height: 40px; min-width: 52px; padding: 0 6px; font-family: var(--font-num); }
.stepper input { width: 86px; text-align: right; font-family: var(--font-num); }
.note { margin: 8px 0 0; color: var(--text-muted); font-size: 13px; }

.diag { display: grid; grid-template-columns: auto minmax(0, 1fr); gap: 4px 12px; margin: 0; font-size: 13px; }
.diag dt { color: var(--text-muted); }
.diag dd { margin: 0; overflow-wrap: anywhere; font-variant-numeric: tabular-nums; }
.diag .bad { color: var(--crit-ink); font-weight: 600; }

input[type="file"] { font: inherit; font-size: 14px; max-width: 100%; }
.placeholder { color: var(--text-muted); text-align: center; padding: 40px 0; }

[hidden] { display: none !important; }
