/* =========================================================================
   Tracker — Apple Health-style design system (light)
   ========================================================================= */

:root {
  /* Surfaces */
  --bg: #f2f2f7;
  --surface: #ffffff;
  --surface-2: #fbfbfd;
  --fill: rgba(120, 120, 128, 0.12);
  --fill-2: rgba(120, 120, 128, 0.20);
  --fill-3: rgba(120, 120, 128, 0.08);

  /* Text */
  --label: #1c1c1e;
  --label-2: rgba(60, 60, 67, 0.60);
  --label-3: rgba(60, 60, 67, 0.30);
  --label-4: rgba(60, 60, 67, 0.18);
  --on-accent: #ffffff;

  /* Lines */
  --separator: rgba(60, 60, 67, 0.12);
  --separator-strong: rgba(60, 60, 67, 0.20);

  /* Brand + semantic */
  --accent: #007aff;
  --accent-press: #0060df;
  --accent-soft: rgba(0, 122, 255, 0.12);
  --good: #34c759;
  --bad: #ff3b30;
  --warn: #ff9500;
  --purple: #5e5ce6;

  /* Domain */
  --sleep-a: #30d5c8;
  --sleep-b: #0fb5c9;
  --stage-awake: #ff9f0a;
  --stage-rem: #40c8e0;
  --stage-core: #3a82f7;
  --stage-deep: #3634a3;
  --move-a: #ff375f;
  --move-b: #ff9f0a;

  /* Radii */
  --r-sm: 10px;
  --r-md: 14px;
  --r-lg: 20px;
  --r-xl: 28px;
  --r-full: 999px;

  /* Shadows */
  --shadow-sm: 0 1px 2px rgba(0, 0, 0, 0.04);
  --shadow-card: 0 1px 3px rgba(0, 0, 0, 0.04), 0 8px 24px rgba(0, 0, 0, 0.05);
  --shadow-pop: 0 12px 40px rgba(0, 0, 0, 0.16);
  --shadow-ring: 0 6px 20px rgba(0, 0, 0, 0.08);

  /* Motion */
  --ease-quart: cubic-bezier(0.25, 1, 0.5, 1);
  --ease-expo: cubic-bezier(0.16, 1, 0.3, 1);
  --d-fast: 150ms;
  --d: 220ms;
  --d-slow: 340ms;

  /* Layout */
  --col: 760px;
  --pad: 20px;
  --tabbar-h: 64px;

  /* z-scale */
  --z-nav: 100;
  --z-sticky: 200;
  --z-backdrop: 300;
  --z-sheet: 310;
  --z-toast: 400;
  --z-timer: 420;

  --font: -apple-system, BlinkMacSystemFont, "SF Pro Text", "SF Pro Display",
    "Inter", system-ui, "Segoe UI", Roboto, sans-serif;
  --mono: ui-monospace, "SF Mono", "SFMono-Regular", "Menlo", monospace;
}

/* ------------------------------------------------------------------ reset */
*, *::before, *::after { box-sizing: border-box; }
* { margin: 0; }
html { -webkit-text-size-adjust: 100%; text-size-adjust: 100%; }
body {
  font-family: var(--font);
  background: var(--bg);
  color: var(--label);
  font-size: 17px;
  line-height: 1.4;
  -webkit-font-smoothing: antialiased;
  text-rendering: optimizeLegibility;
  min-height: 100dvh;
  overflow-x: hidden;
}
img, svg, video { display: block; max-width: 100%; }
button, input, select, textarea { font: inherit; color: inherit; }
button { cursor: pointer; background: none; border: none; }
a { color: var(--accent); text-decoration: none; }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; border-radius: 6px; }
::selection { background: var(--accent-soft); }

/* --------------------------------------------------------------- type */
.t-large { font-size: 34px; font-weight: 700; letter-spacing: -0.022em; line-height: 1.1; }
.t-title { font-size: 28px; font-weight: 700; letter-spacing: -0.02em; line-height: 1.15; }
.t-title2 { font-size: 22px; font-weight: 700; letter-spacing: -0.015em; }
.t-title3 { font-size: 20px; font-weight: 600; letter-spacing: -0.01em; }
.t-headline { font-size: 17px; font-weight: 600; letter-spacing: -0.01em; }
.t-body { font-size: 17px; font-weight: 400; }
.t-subhead { font-size: 15px; font-weight: 400; }
.t-foot { font-size: 13px; font-weight: 400; color: var(--label-2); }
.t-caption { font-size: 12px; font-weight: 500; letter-spacing: 0.02em; color: var(--label-2); }
.num { font-variant-numeric: tabular-nums; font-feature-settings: "tnum"; }
.muted { color: var(--label-2); }
.tint { color: var(--accent); }
.good { color: var(--good); } .bad { color: var(--bad); }
.center { text-align: center; }
.eyebrow { font-size: 13px; font-weight: 600; color: var(--label-2); text-transform: uppercase; letter-spacing: 0.04em; }

/* --------------------------------------------------------------- layout */
.app {
  max-width: var(--col);
  margin: 0 auto;
  padding: max(16px, env(safe-area-inset-top)) var(--pad)
           calc(var(--tabbar-h) + 32px + env(safe-area-inset-bottom));
  min-height: 100dvh;
}
.page-head { padding: 12px 4px 20px; display: flex; align-items: flex-end; justify-content: space-between; gap: 12px; }
.page-head .sub { color: var(--label-2); font-size: 15px; margin-top: 2px; }
.stack { display: flex; flex-direction: column; gap: 16px; }
.stack-lg { display: flex; flex-direction: column; gap: 24px; }
.row-between { display: flex; align-items: center; justify-content: space-between; gap: 12px; }
.grid-2 { display: grid; grid-template-columns: 1fr 1fr; gap: 14px; }
.hstack { display: flex; align-items: center; gap: 12px; }
.wrap { flex-wrap: wrap; }
.grow { flex: 1; min-width: 0; }
.hide { display: none !important; }
.section-title { display: flex; align-items: center; justify-content: space-between; padding: 4px 6px 8px; }
.section-title h2 { font-size: 20px; font-weight: 700; letter-spacing: -0.01em; }

/* --------------------------------------------------------------- card */
.card {
  background: var(--surface);
  border-radius: var(--r-lg);
  box-shadow: var(--shadow-card);
  padding: 20px;
}
.card.tight { padding: 6px; }
.card.pad0 { padding: 0; overflow: hidden; }
.card-head { display: flex; align-items: center; justify-content: space-between; gap: 10px; margin-bottom: 14px; }
.card-head h3 { font-size: 17px; font-weight: 600; }
.card-head .lead { display: flex; align-items: center; gap: 9px; }

/* --------------------------------------------------------------- rows (grouped list) */
.list { display: flex; flex-direction: column; }
.list-row {
  display: flex; align-items: center; gap: 14px;
  padding: 14px 20px; min-height: 52px;
  position: relative; text-align: left; width: 100%;
  transition: background var(--d-fast) var(--ease-quart);
}
.list-row + .list-row::before {
  content: ""; position: absolute; top: 0; left: 20px; right: 0; height: 1px;
  background: var(--separator);
}
button.list-row:active, a.list-row:active { background: var(--fill-3); }
.list-row .r-title { font-weight: 500; }
.list-row .r-sub { color: var(--label-2); font-size: 13px; }
.list-row .r-value { margin-left: auto; color: var(--label-2); font-variant-numeric: tabular-nums; }
.chevron { width: 8px; height: 14px; color: var(--label-3); flex: none; }
.leadicon {
  width: 30px; height: 30px; border-radius: 8px; flex: none;
  display: grid; place-items: center; color: #fff;
}

/* --------------------------------------------------------------- buttons */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  height: 44px; padding: 0 18px; border-radius: var(--r-full);
  font-size: 16px; font-weight: 600; white-space: nowrap;
  transition: transform var(--d-fast) var(--ease-quart),
              background var(--d-fast), opacity var(--d-fast), box-shadow var(--d-fast);
  user-select: none;
}
.btn:active { transform: scale(0.965); }
.btn:disabled { opacity: 0.45; pointer-events: none; }
.btn-primary { background: var(--accent); color: var(--on-accent); }
.btn-primary:hover { background: var(--accent-press); }
.btn-secondary { background: var(--fill); color: var(--accent); }
.btn-secondary:hover { background: var(--fill-2); }
.btn-ghost { color: var(--accent); height: 40px; padding: 0 10px; }
.btn-ghost:hover { background: var(--fill-3); }
.btn-danger { background: rgba(255, 59, 48, 0.12); color: var(--bad); }
.btn-danger:hover { background: rgba(255, 59, 48, 0.2); }
.btn-block { display: flex; width: 100%; }
.btn-lg { height: 52px; font-size: 17px; }
.btn.loading { color: transparent !important; position: relative; pointer-events: none; }
.btn.loading::after {
  content: ""; position: absolute; width: 18px; height: 18px; border-radius: 50%;
  border: 2px solid currentColor; border-top-color: transparent;
  color: var(--on-accent); animation: spin 0.7s linear infinite;
}
.btn-secondary.loading::after, .btn-ghost.loading::after { color: var(--accent); }
.icon-btn {
  width: 40px; height: 40px; border-radius: var(--r-full); display: grid; place-items: center;
  color: var(--accent); transition: background var(--d-fast);
}
.icon-btn:hover { background: var(--fill-3); }
.icon-btn:active { transform: scale(0.92); }
.plus-btn {
  width: 32px; height: 32px; border-radius: 50%; background: var(--accent-soft);
  color: var(--accent); display: grid; place-items: center; flex: none;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* --------------------------------------------------------------- fields */
.field-label { font-size: 13px; font-weight: 500; color: var(--label-2); margin: 0 0 6px 4px; display: block; }
.field {
  width: 100%; height: 48px; padding: 0 15px; border-radius: var(--r-md);
  background: var(--fill); border: 1px solid transparent; color: var(--label);
  font-size: 17px; transition: background var(--d-fast), border-color var(--d-fast), box-shadow var(--d-fast);
}
textarea.field { height: auto; padding: 12px 15px; resize: vertical; min-height: 84px; line-height: 1.45; }
.field::placeholder { color: var(--label-3); }
.field:focus { outline: none; background: var(--surface); border-color: var(--accent); box-shadow: 0 0 0 4px var(--accent-soft); }
.field:disabled { opacity: 0.5; }
select.field { -webkit-appearance: none; appearance: none;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='8' viewBox='0 0 12 8'%3E%3Cpath fill='%23999' d='M6 8 0 2 1.4.6 6 5.2 10.6.6 12 2z'/%3E%3C/svg%3E");
  background-repeat: no-repeat; background-position: right 15px center; padding-right: 38px; }
.big-input {
  width: 100%; text-align: center; border: none; background: none; color: var(--label);
  font-size: 64px; font-weight: 700; letter-spacing: -0.03em; font-variant-numeric: tabular-nums;
}
.big-input:focus { outline: none; }
.field-row { display: flex; gap: 10px; }

/* --------------------------------------------------------------- segmented */
.segmented {
  display: inline-flex; background: var(--fill); border-radius: var(--r-md); padding: 2px; gap: 2px;
  position: relative; width: 100%;
}
.segmented button {
  flex: 1; height: 34px; border-radius: 11px; font-size: 14px; font-weight: 600; color: var(--label);
  position: relative; z-index: 1; transition: color var(--d-fast); white-space: nowrap; padding: 0 8px;
}
.segmented button[aria-selected="true"] { color: var(--label); }
.segmented button:not([aria-selected="true"]) { color: var(--label-2); }
.segmented .thumb {
  position: absolute; top: 2px; bottom: 2px; border-radius: 11px; background: var(--surface);
  box-shadow: var(--shadow-sm); z-index: 0;
  transition: transform var(--d) var(--ease-quart), width var(--d) var(--ease-quart);
}

/* pills / chips */
.pills { display: flex; gap: 8px; overflow-x: auto; padding: 2px; scrollbar-width: none; -webkit-overflow-scrolling: touch; }
.pills::-webkit-scrollbar { display: none; }
.pill {
  flex: none; height: 34px; padding: 0 15px; border-radius: var(--r-full); background: var(--fill);
  color: var(--label); font-size: 14px; font-weight: 600; transition: background var(--d-fast), color var(--d-fast), transform var(--d-fast);
}
.pill:active { transform: scale(0.95); }
.pill[aria-selected="true"] { background: var(--accent); color: #fff; }
.chip {
  display: inline-flex; align-items: center; gap: 6px; height: 26px; padding: 0 10px;
  border-radius: var(--r-full); background: var(--fill); font-size: 12px; font-weight: 600; color: var(--label-2);
}
.chip .dot { width: 8px; height: 8px; border-radius: 50%; }

/* --------------------------------------------------------------- stat tiles */
.stat { display: flex; flex-direction: column; gap: 2px; }
.stat .k { font-size: 13px; font-weight: 500; color: var(--label-2); }
.stat .v { font-size: 26px; font-weight: 700; letter-spacing: -0.02em; font-variant-numeric: tabular-nums; }
.stat .v small { font-size: 15px; font-weight: 600; color: var(--label-2); }
.delta { display: inline-flex; align-items: center; gap: 3px; font-size: 14px; font-weight: 600; }
.delta svg { width: 11px; height: 11px; }

/* --------------------------------------------------------------- rings */
.ring-wrap { position: relative; display: grid; place-items: center; }
.ring-wrap .ring-center { position: absolute; inset: 0; display: grid; place-content: center; text-align: center; }
.ring svg { transform: rotate(-90deg); overflow: visible; }
.ring .track { fill: none; }
.ring .val { fill: none; stroke-linecap: round; transition: stroke-dashoffset var(--d-slow) var(--ease-expo); }

/* --------------------------------------------------------------- charts */
.chart { width: 100%; overflow: visible; }
.chart .axis { stroke: var(--separator); stroke-width: 1; }
.chart .gridline { stroke: var(--separator); stroke-width: 1; stroke-dasharray: 1 5; stroke-linecap: round; }
.chart .line { fill: none; stroke: var(--accent); stroke-width: 2.5; stroke-linecap: round; stroke-linejoin: round; }
.chart .area { fill: url(#areaGrad); opacity: 0.9; }
.chart .dot { fill: var(--surface); stroke: var(--accent); stroke-width: 2.5; }
.chart .dot-hit { fill: transparent; }
.chart .lbl { fill: var(--label-2); font-size: 11px; font-variant-numeric: tabular-nums; }
.chart .bar { transition: y var(--d-slow) var(--ease-expo), height var(--d-slow) var(--ease-expo); }
.chart-tip {
  position: absolute; pointer-events: none; background: var(--label); color: #fff;
  padding: 6px 10px; border-radius: 8px; font-size: 12px; font-weight: 600; white-space: nowrap;
  transform: translate(-50%, -130%); opacity: 0; transition: opacity var(--d-fast); z-index: 2;
  font-variant-numeric: tabular-nums; box-shadow: var(--shadow-pop);
}
.chart-tip.show { opacity: 1; }

/* --------------------------------------------------------------- sleep stages bar */
.stagebar { display: flex; height: 14px; border-radius: var(--r-full); overflow: hidden; background: var(--fill); }
.stagebar span { height: 100%; transition: width var(--d-slow) var(--ease-expo); }
.stage-legend { display: flex; flex-wrap: wrap; gap: 12px 18px; }
.stage-legend .li { display: flex; align-items: center; gap: 7px; }
.stage-legend .sw { width: 10px; height: 10px; border-radius: 3px; }
.stage-legend .lv { font-weight: 600; font-variant-numeric: tabular-nums; }

/* --------------------------------------------------------------- tab bar / nav */
.tabbar {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: var(--z-nav);
  height: calc(var(--tabbar-h) + env(safe-area-inset-bottom));
  padding-bottom: env(safe-area-inset-bottom);
  display: flex; justify-content: center; gap: 4px;
  background: rgba(255, 255, 255, 0.82);
  -webkit-backdrop-filter: saturate(180%) blur(20px);
  backdrop-filter: saturate(180%) blur(20px);
  border-top: 1px solid var(--separator);
}
.tab {
  flex: 1; max-width: 120px; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 3px;
  color: var(--label-3); font-size: 10px; font-weight: 600; letter-spacing: 0.01em; padding-top: 8px;
  transition: color var(--d-fast);
}
.tab svg { width: 26px; height: 26px; }
.tab[aria-current="page"] { color: var(--accent); }
.tab:active { opacity: 0.6; }

@media (min-width: 760px) {
  :root { --tabbar-h: 0px; }
  .tabbar {
    position: sticky; top: 12px; bottom: auto; height: 56px; max-width: 520px; margin: 12px auto 0;
    border-radius: var(--r-full); border: 1px solid var(--separator); box-shadow: var(--shadow-card);
    padding-bottom: 0; gap: 2px;
  }
  .tab { flex-direction: row; gap: 8px; font-size: 14px; border-radius: var(--r-full); padding: 0 14px; }
  .tab svg { width: 20px; height: 20px; }
  .tab[aria-current="page"] { background: var(--accent-soft); }
  .app { padding-top: 0; padding-bottom: 60px; }
}

/* --------------------------------------------------------------- sheet / dialog */
dialog.sheet {
  border: none; padding: 0; background: transparent; color: var(--label);
  max-width: 560px; width: 100%; margin: auto auto 0; max-height: 92dvh;
}
@media (min-width: 640px) { dialog.sheet { margin: auto; } }
dialog.sheet::backdrop { background: rgba(0, 0, 0, 0.4); -webkit-backdrop-filter: blur(2px); backdrop-filter: blur(2px); }
.sheet-inner {
  background: var(--bg); border-radius: 22px 22px 0 0; padding: 8px 20px calc(24px + env(safe-area-inset-bottom));
  max-height: 92dvh; overflow-y: auto; -webkit-overflow-scrolling: touch;
}
@media (min-width: 640px) { .sheet-inner { border-radius: 22px; } }
.sheet-grip { width: 36px; height: 5px; border-radius: 3px; background: var(--label-4); margin: 8px auto 12px; }
.sheet-head { display: flex; align-items: center; justify-content: space-between; padding: 4px 0 16px; }
.sheet-head h2 { font-size: 20px; font-weight: 700; }
dialog.sheet[open] { animation: sheet-in var(--d-slow) var(--ease-expo); }
dialog.sheet[open]::backdrop { animation: fade-in var(--d) ease; }
@keyframes sheet-in { from { transform: translateY(24px); opacity: 0; } to { transform: translateY(0); opacity: 1; } }
@keyframes fade-in { from { opacity: 0; } to { opacity: 1; } }

/* --------------------------------------------------------------- toast */
.toast-host { position: fixed; left: 0; right: 0; top: calc(12px + env(safe-area-inset-top)); z-index: var(--z-toast);
  display: flex; flex-direction: column; align-items: center; gap: 8px; pointer-events: none; padding: 0 16px; }
.toast {
  display: flex; align-items: center; gap: 10px; max-width: 420px;
  background: rgba(28, 28, 30, 0.94); color: #fff; padding: 12px 18px; border-radius: var(--r-full);
  font-size: 15px; font-weight: 500; box-shadow: var(--shadow-pop);
  -webkit-backdrop-filter: blur(10px); backdrop-filter: blur(10px);
  animation: toast-in var(--d) var(--ease-expo);
}
.toast.out { animation: toast-out var(--d) var(--ease-quart) forwards; }
.toast svg { width: 18px; height: 18px; flex: none; }
.toast.ok svg { color: var(--good); } .toast.err svg { color: var(--bad); }
@keyframes toast-in { from { transform: translateY(-16px); opacity: 0; } to { transform: translateY(0); opacity: 1; } }
@keyframes toast-out { to { transform: translateY(-16px); opacity: 0; } }

/* --------------------------------------------------------------- skeleton / empty */
.skel { background: linear-gradient(100deg, var(--fill) 30%, var(--fill-3) 50%, var(--fill) 70%);
  background-size: 200% 100%; animation: shimmer 1.3s linear infinite; border-radius: 8px; }
@keyframes shimmer { to { background-position: -200% 0; } }
.empty { text-align: center; padding: 36px 20px; color: var(--label-2); }
.empty .ico { width: 52px; height: 52px; border-radius: 16px; background: var(--fill); display: grid; place-items: center; margin: 0 auto 14px; color: var(--label-3); }
.empty h4 { font-size: 17px; font-weight: 600; color: var(--label); margin-bottom: 4px; }
.empty p { font-size: 14px; max-width: 34ch; margin: 0 auto; }

/* --------------------------------------------------------------- misc */
.divider { height: 1px; background: var(--separator); margin: 4px 0; }
.badge { display: inline-flex; align-items: center; padding: 2px 8px; border-radius: var(--r-full); font-size: 12px; font-weight: 700; }
.badge.ok { background: rgba(52, 199, 89, 0.14); color: #248a3d; }
.badge.warn { background: rgba(255, 149, 0, 0.16); color: #b25b00; }
.sr-only { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }

/* entrance stagger (enhances already-visible content) */
.reveal { animation: reveal var(--d-slow) var(--ease-expo) both; }
@keyframes reveal { from { opacity: 0; transform: translateY(12px); } to { opacity: 1; transform: none; } }

/* count-up helper keeps layout stable */
.countup { font-variant-numeric: tabular-nums; }

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: 0.001ms !important; animation-iteration-count: 1 !important;
    transition-duration: 0.001ms !important; scroll-behavior: auto !important;
  }
  .reveal { opacity: 1 !important; transform: none !important; }
}
