/* ---------- Tokens ---------- */
:root {
  color-scheme: light;
  --page: #f9f9f7;
  --surface: #fcfcfb;
  --fill: #eeede8;
  --ink: #0b0b0b;
  --ink-2: #52514e;
  --muted: #898781;
  --grid: #e1e0d9;
  --axis: #c3c2b7;
  --border: rgba(11, 11, 11, 0.10);
  --border-2: rgba(11, 11, 11, 0.16);
  --btn: #0b0b0b;
  --btn-ink: #ffffff;
  --danger: #c0302f;
  --critical: #d03b3b;
  --focus: #2a78d6;
  /* Series: one fixed hue per state (validated categorical slots 1-3) */
  --s-asleep: #2a78d6;
  --s-awake: #eb6834;
  --shadow: 0 8px 30px rgba(0, 0, 0, 0.06);
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    color-scheme: dark;
    --page: #0d0d0d;
    --surface: #1a1a19;
    --fill: #262624;
    --ink: #ffffff;
    --ink-2: #c3c2b7;
    --muted: #898781;
    --grid: #2c2c2a;
    --axis: #383835;
    --border: rgba(255, 255, 255, 0.10);
    --border-2: rgba(255, 255, 255, 0.16);
    --btn: #ecebe6;
    --btn-ink: #0b0b0b;
    --danger: #e66767;
    --critical: #d03b3b;
    --focus: #3987e5;
    --s-asleep: #3987e5;
    --s-awake: #d95926;
    --shadow: 0 8px 30px rgba(0, 0, 0, 0.4);
  }
}
:root[data-theme="dark"] {
  color-scheme: dark;
  --page: #0d0d0d;
  --surface: #1a1a19;
  --fill: #262624;
  --ink: #ffffff;
  --ink-2: #c3c2b7;
  --muted: #898781;
  --grid: #2c2c2a;
  --axis: #383835;
  --border: rgba(255, 255, 255, 0.10);
  --border-2: rgba(255, 255, 255, 0.16);
  --btn: #ecebe6;
  --btn-ink: #0b0b0b;
  --danger: #e66767;
  --critical: #d03b3b;
  --focus: #3987e5;
  --s-asleep: #3987e5;
  --s-awake: #d95926;
  --shadow: 0 8px 30px rgba(0, 0, 0, 0.4);
}

.s-asleep { --c: var(--s-asleep); }
.s-awake { --c: var(--s-awake); }

/* ---------- Base ---------- */
*, *::before, *::after { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  background: var(--page);
  color: var(--ink);
  font: 16px/1.45 system-ui, -apple-system, "Segoe UI", sans-serif;
  -webkit-font-smoothing: antialiased;
}
button { font: inherit; color: inherit; }
[hidden] { display: none !important; }
:focus-visible { outline: 2px solid var(--focus); outline-offset: 2px; }
h2 { font-size: 18px; margin: 0; }
code { font: 13px/1.4 ui-monospace, SFMono-Regular, Menlo, monospace; background: var(--fill); padding: 1px 4px; border-radius: 4px; }
.small { font-size: 14px; color: var(--ink-2); margin: 0; }
.muted { color: var(--muted); }
.label { font-size: 13px; font-weight: 500; color: var(--ink-2); margin: 0 0 6px; }

.app {
  max-width: 560px;
  margin: 0 auto;
  padding: 0 16px calc(84px + env(safe-area-inset-bottom));
}
.topbar { padding: calc(14px + env(safe-area-inset-top)) 0 12px; }
.eyebrow { margin: 0; font-size: 13px; font-weight: 600; color: var(--ink-2); }
.topbar h1 { margin: 0; font-size: 30px; line-height: 1.15; font-weight: 700; letter-spacing: -0.01em; }

.view { display: grid; gap: 12px; }
.card { background: var(--surface); border-radius: 16px; box-shadow: 0 0 0 1px var(--border); padding: 16px; }
.stack { display: grid; gap: 12px; }
.stack > p { margin: 0; }

/* ---------- Controls ---------- */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  min-height: 46px; padding: 0 18px;
  border: 0; border-radius: 12px;
  background: var(--fill); color: var(--ink);
  font-size: 16px; font-weight: 600;
  cursor: pointer; -webkit-tap-highlight-color: transparent;
}
.btn:active { opacity: 0.75; }
.btn.primary { background: var(--btn); color: var(--btn-ink); }
.btn.ghost { background: transparent; box-shadow: inset 0 0 0 1px var(--border-2); }
.btn.danger { color: var(--danger); }
.btn.block { width: 100%; }
.icon-btn { border: 0; background: none; font-size: 24px; line-height: 1; color: var(--ink-2); padding: 4px 8px; cursor: pointer; }

.seg {
  display: flex; gap: 2px; padding: 3px;
  background: var(--fill); border-radius: 12px;
}
.seg button {
  flex: 1 1 0; min-width: 0; min-height: 40px;
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  padding: 0 6px; border: 0; border-radius: 9px;
  background: transparent; color: var(--ink-2);
  font-size: 15px; font-weight: 500; white-space: nowrap;
  cursor: pointer; -webkit-tap-highlight-color: transparent;
}
.seg button[aria-checked="true"] {
  background: var(--surface); color: var(--ink); font-weight: 600;
  box-shadow: 0 1px 2px rgba(0, 0, 0, 0.08), 0 0 0 0.5px var(--border);
}
.seg.compact button { min-height: 34px; font-size: 14px; }

.key { width: 8px; height: 8px; border-radius: 50%; flex: none; background: var(--c); }

.field { display: grid; gap: 6px; min-width: 0; }
.field > span { font-size: 13px; font-weight: 500; color: var(--ink-2); }
.field small { font-size: 13px; color: var(--ink-2); }
.field-row { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
input[type="text"], input[type="number"], input[type="date"], input[type="time"], textarea {
  width: 100%; min-width: 0; min-height: 46px;
  padding: 10px 12px;
  font: inherit; font-size: 17px; color: var(--ink);
  background: var(--page);
  border: 1px solid var(--border-2); border-radius: 10px;
}
input[type="date"], input[type="time"] { -webkit-appearance: none; appearance: none; display: block; }
input::-webkit-date-and-time-value { text-align: left; }
textarea { min-height: 120px; resize: vertical; font: 16px/1.4 ui-monospace, SFMono-Regular, Menlo, monospace; }
.row-btns { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }

/* ---------- Count ---------- */
.install { display: flex; gap: 8px; align-items: flex-start; }
.install p { margin: 4px 0 0; font-size: 14px; color: var(--ink-2); }

.pad {
  position: relative; display: grid; place-items: center;
  width: min(78vw, 300px); aspect-ratio: 1; margin: 12px auto 0;
  border: 0; border-radius: 50%;
  background: var(--surface);
  box-shadow: 0 0 0 1px var(--border), var(--shadow);
  touch-action: none; user-select: none; -webkit-user-select: none; -webkit-touch-callout: none;
  -webkit-tap-highlight-color: transparent; cursor: pointer;
}
.pad.pulse { animation: pulse 0.16s ease-out; }
@keyframes pulse { from { transform: scale(0.965); } to { transform: scale(1); } }
.ring { position: absolute; inset: 0; width: 100%; height: 100%; transform: rotate(-90deg); pointer-events: none; }
.ring circle { fill: none; stroke-width: 7; }
.ring-track { stroke: var(--grid); }
.ring-prog { stroke: var(--c); stroke-linecap: round; stroke-dasharray: 578.05; stroke-dashoffset: 578.05; opacity: 0; }
.pad[data-phase="running"] .ring-prog, .pad[data-phase="done"] .ring-prog { opacity: 1; }
.pad-inner { display: flex; flex-direction: column; align-items: center; pointer-events: none; }
.pad-count { font-size: 84px; font-weight: 600; line-height: 1; letter-spacing: -0.02em; }
.pad[data-phase="idle"] .pad-count { font-size: 44px; font-weight: 600; }
.pad-sub { margin-top: 10px; font-size: 15px; color: var(--ink-2); font-variant-numeric: tabular-nums; }
.count-actions { display: flex; justify-content: center; min-height: 46px; }
.hint { margin: 0; text-align: center; font-size: 15px; color: var(--ink-2); }
.last { margin: 0; text-align: center; font-size: 14px; color: var(--ink-2); }
.last strong { color: var(--ink); }

/* ---------- Result sheet ---------- */
.result { display: grid; grid-template-columns: 56px 1fr 56px; align-items: center; }
.result-mid { text-align: center; }
.result-num { font-size: 64px; font-weight: 600; line-height: 1; letter-spacing: -0.02em; }
.result-unit { font-size: 14px; color: var(--ink-2); margin-top: 4px; }
.step {
  width: 48px; height: 48px; border-radius: 50%; border: 0;
  background: var(--fill); font-size: 26px; line-height: 1; cursor: pointer;
}
.result-when { margin: -4px 0 0; text-align: center; font-size: 14px; color: var(--ink-2); }
.notice {
  display: flex; gap: 8px; align-items: baseline;
  padding: 10px 12px; border-radius: 10px;
  background: var(--fill); font-size: 14px;
}
.flag { color: var(--critical); font-size: 11px; }

/* ---------- Sheets ---------- */
dialog.sheet {
  width: 100%; max-width: 560px; max-height: 92dvh;
  margin: auto auto 0; padding: 0; border: 0;
  border-radius: 20px 20px 0 0;
  background: var(--surface); color: var(--ink);
  box-shadow: 0 -10px 40px rgba(0, 0, 0, 0.2);
  overflow: auto;
}
dialog.sheet[open] { animation: slideup 0.22s ease-out; }
@keyframes slideup { from { transform: translateY(40px); opacity: 0; } to { transform: none; opacity: 1; } }
dialog.sheet::backdrop { background: rgba(0, 0, 0, 0.4); }
.sheet-body { display: grid; gap: 14px; padding: 20px 16px calc(16px + env(safe-area-inset-bottom)); }
.sheet-actions { display: flex; gap: 8px; }
.sheet-actions .btn { flex: 1; }
.sheet-actions .spacer { display: none; }
#sheet-entry .sheet-actions .btn { flex: 0 1 auto; }
#sheet-entry .sheet-actions .spacer { display: block; flex: 1; }
.form-error { margin: 0; color: var(--danger); font-size: 14px; }

/* ---------- Log ---------- */
.day { margin: 10px 0 -4px; font-size: 13px; font-weight: 600; color: var(--ink-2); }
.rows { list-style: none; margin: 0; padding: 0; background: var(--surface); border-radius: 14px; box-shadow: 0 0 0 1px var(--border); overflow: hidden; }
.rows li + li { border-top: 1px solid var(--grid); }
.row {
  display: grid; grid-template-columns: 46px 1fr auto; align-items: center; gap: 12px;
  width: 100%; min-height: 52px; padding: 8px 14px;
  border: 0; background: none; text-align: left; cursor: pointer;
  -webkit-tap-highlight-color: transparent;
}
.row:active { background: var(--fill); }
.row-time { font-size: 15px; color: var(--ink-2); font-variant-numeric: tabular-nums; }
.row-state { display: flex; align-items: center; gap: 8px; min-width: 0; }
.row-txt { display: grid; min-width: 0; }
.row-note { font-size: 13px; color: var(--ink-2); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.row-bpm { display: inline-flex; align-items: baseline; gap: 6px; font-size: 20px; font-weight: 600; font-variant-numeric: tabular-nums; }
.empty { margin: 24px 8px; text-align: center; color: var(--ink-2); }

/* ---------- Trends ---------- */
.tiles { display: grid; grid-template-columns: repeat(2, 1fr); gap: 8px; }
.tile { background: var(--surface); border-radius: 14px; box-shadow: 0 0 0 1px var(--border); padding: 12px; min-width: 0; }
.tile-lbl { display: flex; align-items: center; gap: 6px; font-size: 13px; color: var(--ink-2); white-space: nowrap; }
.tile-val { margin-top: 6px; font-size: 30px; font-weight: 600; line-height: 1.1; }
.tile-val small { margin-left: 4px; font-size: 13px; font-weight: 500; color: var(--ink-2); }
.tile-sub { font-size: 12px; color: var(--ink-2); }

.chart-card { padding: 12px 12px 14px; }
.legend { display: flex; flex-wrap: wrap; gap: 6px; margin-bottom: 4px; }
.lg-item {
  display: inline-flex; align-items: center; gap: 6px;
  min-height: 32px; padding: 0 10px; border: 0; border-radius: 999px;
  background: var(--fill); font-size: 13px; color: var(--ink); cursor: pointer;
  -webkit-tap-highlight-color: transparent;
}
.lg-item[aria-pressed="false"] { background: transparent; color: var(--muted); box-shadow: inset 0 0 0 1px var(--border); }
.lg-item[aria-pressed="false"] .key { background: transparent; box-shadow: inset 0 0 0 1.5px var(--c); }
.lg-static { background: transparent; cursor: default; padding: 0 4px; color: var(--ink-2); }
.dashkey { width: 16px; border-top: 1.5px dashed var(--ink-2); }
.chart-wrap { position: relative; }
#chart { display: block; overflow: visible; touch-action: pan-y; -webkit-user-select: none; user-select: none; }
.caption { margin: 8px 0 0; font-size: 13px; color: var(--ink-2); }

.tick { fill: var(--muted); font-size: 11px; font-variant-numeric: tabular-nums; }
.grid { stroke: var(--grid); stroke-width: 1; }
.axis { stroke: var(--axis); stroke-width: 1; }
.threshold { stroke: var(--ink-2); stroke-width: 1.25; stroke-dasharray: 4 4; }
.threshold-label { fill: var(--ink-2); }
.empty-label { fill: var(--ink-2); font-size: 14px; }
.line { fill: none; stroke: var(--c); stroke-width: 2; stroke-linejoin: round; stroke-linecap: round; }
.dot { fill: var(--c); stroke: var(--surface); stroke-width: 2; }
.hl { fill: none; stroke: var(--ink); stroke-width: 2; pointer-events: none; }

.tooltip {
  position: absolute; z-index: 2; pointer-events: none;
  min-width: 120px; max-width: 220px; padding: 8px 10px;
  background: var(--surface); border-radius: 10px;
  box-shadow: 0 0 0 1px var(--border), 0 6px 20px rgba(0, 0, 0, 0.14);
  font-size: 13px;
}
.tip-val { font-size: 20px; font-weight: 600; line-height: 1.2; }
.tip-unit { font-size: 12px; font-weight: 500; color: var(--ink-2); }
.tip-state { display: flex; align-items: center; gap: 6px; color: var(--ink-2); }
.linekey { width: 12px; height: 2px; border-radius: 1px; background: var(--c); }
.tip-when { color: var(--ink-2); }
.tip-note { margin-top: 4px; color: var(--ink); }

/* ---------- Import ---------- */
.import-result { display: grid; gap: 8px; padding: 12px; border-radius: 12px; background: var(--fill); font-size: 14px; }
.import-result p { margin: 0; }
.import-result ul { margin: 0; padding-left: 18px; color: var(--ink-2); }
.import-result code { background: var(--surface); }

/* ---------- Tab bar ---------- */
.tabbar {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 5;
  background: var(--surface);
  background: color-mix(in srgb, var(--surface) 90%, transparent);
  -webkit-backdrop-filter: saturate(1.4) blur(16px);
  backdrop-filter: saturate(1.4) blur(16px);
  border-top: 1px solid var(--border);
  padding: 6px 8px calc(6px + env(safe-area-inset-bottom));
}
.tabbar-inner { display: flex; max-width: 560px; margin: 0 auto; }
.tabbar button {
  flex: 1; display: flex; flex-direction: column; align-items: center; gap: 2px;
  padding: 4px 0; border: 0; background: none;
  font-size: 11px; font-weight: 500; color: var(--muted);
  cursor: pointer; -webkit-tap-highlight-color: transparent;
}
.tabbar button[aria-current="page"] { color: var(--ink); font-weight: 600; }
.tabbar svg { width: 26px; height: 26px; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; }

/* ---------- Toast ---------- */
.toast {
  position: fixed; left: 50%; bottom: calc(86px + env(safe-area-inset-bottom)); z-index: 20;
  max-width: calc(100% - 32px); padding: 10px 16px; border-radius: 999px;
  background: var(--btn); color: var(--btn-ink); font-size: 15px; font-weight: 500;
  opacity: 0; transform: translate(-50%, 16px); transition: opacity 0.2s, transform 0.2s;
  pointer-events: none;
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }

@media (prefers-reduced-motion: reduce) {
  .pad.pulse, dialog.sheet[open] { animation: none; }
  .toast { transition: none; }
}

/* ---------- Backup reminder ---------- */
.backup-banner { display: grid; gap: 10px; padding: 14px 16px; }
.banner-text { display: grid; gap: 2px; margin: 0; font-size: 14px; color: var(--ink-2); }
.banner-text strong { font-size: 16px; color: var(--ink); }
.banner-btns { display: flex; justify-content: flex-end; gap: 8px; }
.btn.small { min-height: 38px; padding: 0 16px; font-size: 15px; border-radius: 10px; }

/* ---------- Import preview ---------- */
.imp-summary { margin: 0; font-size: 16px; }
.imp-row { display: grid; grid-template-columns: 1fr auto auto; align-items: center; gap: 12px; min-height: 52px; padding: 8px 14px; }
.imp-more { display: block; text-align: center; color: var(--ink-2); min-height: 0; }
.bad-list { margin: 0; padding-left: 18px; color: var(--ink-2); font-size: 14px; }
#sheet-import h2, #sheet-entry h2 { text-align: center; }
