:root {
  color-scheme: dark;
  --bg: #000;
  --bg-elev: #0d0d0f;
  --card: #1c1c1e;
  --card-2: #2c2c2e;
  --card-3: #3a3a3c;
  --sep: rgba(84, 84, 88, 0.6);
  --text: #fff;
  --text-2: rgba(235, 235, 245, 0.6);
  --text-3: rgba(235, 235, 245, 0.3);
  --accent: #ff9f0a;
  --accent-soft: rgba(255, 159, 10, 0.16);
  --green: #30d158;
  --red: #ff453a;
  --blue: #0a84ff;
  --radius: 12px;
  --font: -apple-system, BlinkMacSystemFont, 'SF Pro Text', 'Inter', system-ui, 'Segoe UI', Roboto, sans-serif;
  --font-display: -apple-system, BlinkMacSystemFont, 'SF Pro Display', 'Inter', system-ui, 'Segoe UI', Roboto, sans-serif;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --tabbar-h: 56px;
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html { background: var(--bg); }
body {
  margin: 0;
  min-height: 100dvh;
  background: var(--bg);
  color: var(--text);
  font-family: var(--font);
  font-size: 17px;
  line-height: 1.35;
  -webkit-font-smoothing: antialiased;
  -webkit-text-size-adjust: 100%;
  overscroll-behavior-y: none;
}
button, input, select, textarea { font: inherit; color: inherit; }
button { background: none; border: 0; padding: 0; cursor: pointer; }
a { color: var(--accent); text-decoration: none; }
svg { display: block; }
.sr-only { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); }

/* ---------- layout ---------- */
#view {
  max-width: 620px;
  margin: 0 auto;
  padding: calc(var(--safe-top) + 8px) 16px calc(var(--tabbar-h) + var(--safe-bottom) + 28px);
}
.topbar { display: flex; align-items: center; justify-content: space-between; min-height: 44px; }
.topbar .btn-text { color: var(--accent); font-size: 17px; padding: 8px 0; }
.icon-btn { width: 44px; height: 44px; display: grid; place-items: center; color: var(--accent); margin-right: -10px; }
.icon-btn svg { width: 24px; height: 24px; }
.large-title { font-family: var(--font-display); font-size: 34px; font-weight: 700; letter-spacing: 0.01em; margin: 2px 0 14px; }
.section-title { font-size: 13px; text-transform: uppercase; letter-spacing: 0.02em; color: var(--text-2); margin: 26px 16px 7px; }
.section-foot { font-size: 13px; color: var(--text-2); margin: 7px 16px 0; line-height: 1.4; }
.muted { color: var(--text-2); }

/* ---------- tab bar ---------- */
.tabbar {
  position: fixed; inset: auto 0 0 0; z-index: 20;
  display: flex; justify-content: center;
  padding: 6px 0 calc(var(--safe-bottom) + 4px);
  background: rgba(18, 18, 20, 0.82);
  -webkit-backdrop-filter: saturate(180%) blur(20px);
  backdrop-filter: saturate(180%) blur(20px);
  border-top: 0.5px solid var(--sep);
}
.tabbar a {
  flex: 1; max-width: 140px;
  display: flex; flex-direction: column; align-items: center; gap: 3px;
  color: rgba(235, 235, 245, 0.45); font-size: 10.5px; font-weight: 500; padding: 2px 0;
}
.tabbar a svg { width: 26px; height: 26px; }
.tabbar a.active { color: var(--accent); }

/* ---------- grouped lists ---------- */
.group { background: var(--card); border-radius: var(--radius); overflow: hidden; }
.row {
  position: relative; display: flex; align-items: center; gap: 12px;
  min-height: 46px; padding: 10px 16px; width: 100%; text-align: left;
}
.row + .row::before { content: ''; position: absolute; top: 0; left: 16px; right: 0; border-top: 0.5px solid var(--sep); }
.row .grow { flex: 1; min-width: 0; }
.row .value { color: var(--text-2); display: flex; align-items: center; gap: 6px; text-align: right; }
.row .chev { width: 8px; height: 13px; color: var(--text-3); flex: none; }
.row.tappable:active { background: var(--card-2); }
.row .check { color: var(--accent); width: 18px; height: 18px; flex: none; }
.row.danger { color: var(--red); justify-content: center; }
.row.action { color: var(--accent); justify-content: center; font-weight: 500; }
.row input[type='text'], .row input[type='password'] {
  flex: 1; background: none; border: 0; outline: none; font-size: 17px; text-align: right; color: var(--text-2);
}
.row input[type='text']:focus { color: var(--text); }

/* ---------- switch ---------- */
.switch { position: relative; width: 51px; height: 31px; flex: none; }
.switch input { position: absolute; inset: 0; opacity: 0; margin: 0; cursor: pointer; z-index: 1; }
.switch .track { position: absolute; inset: 0; border-radius: 31px; background: rgba(120, 120, 128, 0.32); transition: background 0.25s; }
.switch .thumb {
  position: absolute; top: 2px; left: 2px; width: 27px; height: 27px; border-radius: 50%; background: #fff;
  box-shadow: 0 3px 8px rgba(0, 0, 0, 0.15), 0 3px 1px rgba(0, 0, 0, 0.06);
  transition: transform 0.25s cubic-bezier(0.3, 0.7, 0.4, 1);
}
.switch input:checked ~ .track { background: var(--accent); }
.switch input:checked ~ .thumb { transform: translateX(20px); }

/* ---------- segmented ---------- */
.segmented { display: flex; background: rgba(118, 118, 128, 0.24); border-radius: 9px; padding: 2px; gap: 2px; width: 100%; }
.segmented button {
  flex: 1; padding: 6px 4px; border-radius: 7px; font-size: 13px; font-weight: 500; color: var(--text);
  transition: background 0.2s;
}
.segmented button.on { background: #636366; box-shadow: 0 3px 8px rgba(0, 0, 0, 0.12); }
.row.stack { flex-direction: column; align-items: stretch; gap: 10px; padding-top: 12px; padding-bottom: 12px; }
.row.stack .label-line { display: flex; justify-content: space-between; align-items: baseline; }
.row.stack .hint { font-size: 13px; color: var(--text-2); }

/* ---------- slider ---------- */
input[type='range'] {
  -webkit-appearance: none; appearance: none; width: 100%; height: 28px; background: transparent; margin: 0;
  --fill: 50%;
}
input[type='range']::-webkit-slider-runnable-track {
  height: 4px; border-radius: 4px;
  background: linear-gradient(90deg, var(--accent) var(--fill), rgba(120, 120, 128, 0.36) var(--fill));
}
input[type='range']::-webkit-slider-thumb {
  -webkit-appearance: none; width: 28px; height: 28px; border-radius: 50%; background: #fff; margin-top: -12px;
  box-shadow: 0 0.5px 4px rgba(0, 0, 0, 0.12), 0 6px 13px rgba(0, 0, 0, 0.12);
}
input[type='range']::-moz-range-track { height: 4px; border-radius: 4px; background: rgba(120, 120, 128, 0.36); }
input[type='range']::-moz-range-progress { height: 4px; border-radius: 4px; background: var(--accent); }
input[type='range']::-moz-range-thumb { width: 28px; height: 28px; border-radius: 50%; background: #fff; border: 0; }
input[type='range'].temp::-webkit-slider-runnable-track { background: linear-gradient(90deg, #ff9329, #ffd6aa 45%, #f1f4ff 100%); }
input[type='range'].temp::-moz-range-track { background: linear-gradient(90deg, #ff9329, #ffd6aa 45%, #f1f4ff 100%); }
input[type='range'].temp::-moz-range-progress { background: transparent; }
.slider-row { display: flex; align-items: center; gap: 12px; }
.slider-row svg { width: 20px; height: 20px; color: var(--text-2); flex: none; }

/* ---------- alarms ---------- */
.hero {
  position: relative; overflow: hidden; border-radius: 18px; padding: 18px 18px 16px; margin-bottom: 8px;
  background: radial-gradient(120% 140% at 100% 120%, rgba(255, 140, 40, 0.28), transparent 60%), var(--card);
}
.hero .eyebrow { font-size: 13px; font-weight: 600; color: var(--accent); text-transform: uppercase; letter-spacing: 0.04em; }
.hero .when { font-family: var(--font-display); font-size: 22px; font-weight: 600; margin-top: 6px; }
.hero .sub { color: var(--text-2); font-size: 15px; margin-top: 2px; }
.hero .strip { height: 6px; border-radius: 6px; margin-top: 14px; }
.hero .actions { display: flex; gap: 10px; margin-top: 14px; }
.pill {
  flex: 1; height: 44px; border-radius: 22px; font-weight: 600; font-size: 16px;
  background: var(--card-2); color: var(--text);
}
.pill.primary { background: var(--accent); color: #1a0f00; }
.pill.danger { background: rgba(255, 69, 58, 0.18); color: var(--red); }
.progress-ring { position: absolute; right: 16px; top: 16px; width: 44px; height: 44px; }

.alarm-list { border-top: 0.5px solid var(--sep); }
.alarm-row {
  position: relative; display: flex; align-items: center; gap: 12px;
  padding: 12px 0 13px; border-bottom: 0.5px solid var(--sep); cursor: pointer;
  transition: opacity 0.25s;
}
.alarm-row .time {
  font-family: var(--font-display); font-size: 58px; font-weight: 300; line-height: 1; letter-spacing: -0.01em;
  font-variant-numeric: tabular-nums;
}
.alarm-row .meta { font-size: 15px; margin-top: 4px; color: var(--text); }
.alarm-row .strip { height: 4px; border-radius: 4px; margin-top: 9px; width: min(220px, 100%); opacity: 0.9; }
.alarm-row.off .time, .alarm-row.off .meta { color: var(--text-2); }
.alarm-row.off .strip { opacity: 0.25; filter: grayscale(0.6); }
.alarm-row .del {
  width: 22px; height: 22px; border-radius: 50%; background: var(--red); flex: none; display: none; place-items: center;
}
.alarm-row .del::after { content: ''; width: 10px; height: 2px; background: #fff; border-radius: 2px; }
.editing .alarm-row .del { display: grid; }
.editing .alarm-row .switch { display: none; }
.empty { text-align: center; padding: 60px 20px; color: var(--text-2); }
.empty .sun { width: 64px; height: 64px; margin: 0 auto 14px; color: var(--accent); }
.empty .pill { max-width: 220px; margin-top: 18px; }

/* ---------- sheet ---------- */
.sheet-backdrop {
  position: fixed; inset: 0; z-index: 40; background: rgba(0, 0, 0, 0.5);
  opacity: 0; transition: opacity 0.3s;
}
.sheet-backdrop.open { opacity: 1; }
.sheet {
  position: fixed; z-index: 41; left: 0; right: 0; bottom: 0; top: calc(var(--safe-top) + 12px);
  max-width: 620px; margin: 0 auto;
  background: var(--bg-elev); border-radius: 14px 14px 0 0; overflow: hidden;
  transform: translateY(100%); transition: transform 0.38s cubic-bezier(0.2, 0.9, 0.3, 1);
  display: flex; flex-direction: column;
}
.sheet.open { transform: translateY(0); }
.sheet .pages { position: relative; flex: 1; overflow: hidden; }
.sheet .page {
  position: absolute; inset: 0; display: flex; flex-direction: column; background: var(--bg-elev);
  transition: transform 0.35s cubic-bezier(0.2, 0.9, 0.3, 1);
}
.sheet .page.enter { transform: translateX(100%); }
.sheet .page.behind { transform: translateX(-28%); }
.sheet .bar {
  display: grid; grid-template-columns: 1fr auto 1fr; align-items: center;
  min-height: 52px; padding: 0 16px; border-bottom: 0.5px solid transparent;
}
.sheet .bar .title { font-weight: 600; font-size: 17px; text-align: center; }
.sheet .bar .left { justify-self: start; color: var(--accent); display: flex; align-items: center; gap: 4px; padding: 10px 0; }
.sheet .bar .left svg { width: 12px; height: 20px; }
.sheet .bar .right { justify-self: end; color: var(--accent); font-weight: 600; padding: 10px 0; }
.sheet .bar .right:disabled { color: var(--text-3); }
.sheet .content { flex: 1; overflow-y: auto; -webkit-overflow-scrolling: touch; padding: 4px 16px calc(var(--safe-bottom) + 32px); }

/* ---------- wheel time picker ---------- */
.wheel-wrap { position: relative; display: flex; justify-content: center; gap: 0; height: 216px; margin: 6px 0 4px; }
.wheel-wrap::before {
  content: ''; position: absolute; left: 50%; transform: translateX(-50%); top: 90px; height: 36px; width: min(260px, 90%);
  background: rgba(118, 118, 128, 0.22); border-radius: 9px; pointer-events: none;
}
.wheel-wrap .colon { align-self: center; font-size: 26px; font-weight: 500; z-index: 1; padding: 0 2px; margin-top: -3px; }
.wheel {
  position: relative; width: 86px; height: 216px; overflow-y: scroll; scroll-snap-type: y mandatory;
  scrollbar-width: none; -webkit-mask-image: linear-gradient(transparent, #000 32%, #000 68%, transparent);
  mask-image: linear-gradient(transparent, #000 32%, #000 68%, transparent);
  overscroll-behavior: contain;
}
.wheel::-webkit-scrollbar { display: none; }
.wheel .pad { height: 90px; }
.wheel .item {
  height: 36px; line-height: 36px; text-align: center; scroll-snap-align: center;
  font-size: 23px; font-variant-numeric: tabular-nums; color: var(--text-2); transition: color 0.15s;
}
.wheel .item.sel { color: var(--text); }

/* ---------- bulbs ---------- */
.bulb-grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 12px; }
.bulb-card {
  background: var(--card); border-radius: 16px; padding: 14px; text-align: left; position: relative; overflow: hidden;
  display: flex; flex-direction: column; gap: 12px; min-height: 148px;
}
.bulb-card .glow {
  position: absolute; inset: -40% -40% auto auto; width: 180%; height: 180%; pointer-events: none;
  transition: opacity 1s linear, background 1s linear; opacity: 0;
}
.bulb {
  width: 46px; height: 46px; border-radius: 50%; background: #2a2a2e; position: relative;
  transition: background 1s linear, box-shadow 1s linear;
}
.bulb-card .name { font-weight: 600; font-size: 16px; position: relative; }
.bulb-card .state { font-size: 13px; color: var(--text-2); position: relative; margin-top: 2px; }
.bulb-card .badge {
  position: absolute; top: 12px; right: 12px; font-size: 11px; font-weight: 600; padding: 3px 8px; border-radius: 10px;
  background: rgba(0, 0, 0, 0.35); color: #ffd9a8;
}
.bulb-card .err { color: var(--red); }
.swatches { display: flex; gap: 10px; flex-wrap: wrap; padding: 4px 0; }
.swatch { width: 38px; height: 38px; border-radius: 50%; border: 2px solid transparent; box-shadow: inset 0 0 0 1px rgba(255, 255, 255, 0.15); }
.swatch:active { transform: scale(0.94); }

.banner {
  display: flex; gap: 12px; align-items: flex-start; padding: 13px 14px; border-radius: 14px;
  background: var(--accent-soft); color: #ffd9a8; font-size: 14px; line-height: 1.4; margin-bottom: 14px;
}
.banner svg { width: 20px; height: 20px; flex: none; margin-top: 1px; color: var(--accent); }
.banner b { color: #fff; font-weight: 600; }

/* ---------- bedside ---------- */
.bedside-intro { text-align: center; padding: 20px 8px 0; }
.bedside-intro .moon { width: 72px; height: 72px; margin: 8px auto 16px; color: var(--accent); }
.bedside-intro h2 { font-family: var(--font-display); font-size: 26px; margin: 0 0 8px; }
.bedside-intro p { color: var(--text-2); margin: 0 auto; max-width: 380px; font-size: 16px; }
.checklist { text-align: left; margin: 22px 0 18px; }
.checklist .row { font-size: 15px; }
.checklist .row svg { width: 22px; height: 22px; color: var(--accent); flex: none; }
.big-cta { width: 100%; height: 54px; border-radius: 14px; background: var(--accent); color: #1a0f00; font-weight: 700; font-size: 17px; }
.warn { color: #ffb340; font-size: 14px; margin-top: 12px; }

.nightstand {
  position: fixed; inset: 0; z-index: 60; background: #000; color: rgba(255, 120, 50, 0.42);
  display: flex; flex-direction: column; align-items: center; justify-content: center; text-align: center;
  padding: calc(var(--safe-top) + 20px) 24px calc(var(--safe-bottom) + 24px);
  user-select: none; -webkit-user-select: none; cursor: default;
}
.nightstand .sky { position: absolute; inset: 0; pointer-events: none; transition: background 2s linear; }
.nightstand .clock {
  position: relative; font-family: var(--font-display); font-weight: 200; font-size: min(30vw, 200px);
  line-height: 1; font-variant-numeric: tabular-nums; letter-spacing: -0.02em;
}
.nightstand .info { position: relative; font-size: 17px; margin-top: 14px; opacity: 0.85; }
.nightstand .controls { position: absolute; top: calc(var(--safe-top) + 14px); left: 16px; right: 16px; display: flex; justify-content: space-between; opacity: 0; transition: opacity 0.4s; pointer-events: none; }
.nightstand.show-controls .controls { opacity: 1; pointer-events: auto; }
.nightstand .controls button { color: rgba(255, 140, 70, 0.75); font-size: 15px; padding: 8px 10px; border-radius: 16px; background: rgba(255, 120, 50, 0.08); }
.nightstand .ring { position: relative; display: none; flex-direction: column; gap: 14px; width: min(340px, 100%); margin-top: 40px; }
.nightstand.ringing { color: #fff; }
.nightstand.ringing .ring { display: flex; }
.nightstand .ring .stop { height: 64px; border-radius: 32px; background: #fff; color: #000; font-size: 20px; font-weight: 700; }
.nightstand .ring .snooze { height: 54px; border-radius: 27px; background: rgba(255, 255, 255, 0.16); color: #fff; font-size: 18px; font-weight: 600; }
.nightstand .ring .up { height: 50px; border-radius: 25px; background: rgba(255, 255, 255, 0.12); color: #fff; font-size: 17px; font-weight: 600; }
.nightstand.sunrise .ring { display: flex; }
.nightstand.sunrise .ring .stop, .nightstand.sunrise .ring .snooze { display: none; }
.nightstand:not(.sunrise) .ring .up { display: none; }
.nightstand .tap-audio {
  position: absolute; bottom: calc(var(--safe-bottom) + 28px); left: 50%; transform: translateX(-50%);
  background: rgba(255, 120, 50, 0.14); color: #ffb37a; padding: 12px 18px; border-radius: 22px; font-size: 15px; display: none;
}
.nightstand.needs-audio .tap-audio { display: block; }

/* ---------- science ---------- */
.prose { color: rgba(235, 235, 245, 0.86); font-size: 16px; line-height: 1.55; }
.prose h2 { font-family: var(--font-display); font-size: 21px; color: #fff; margin: 30px 0 8px; }
.prose p { margin: 0 0 12px; }
.prose .lead { font-size: 18px; color: #fff; }
.prose ol, .prose ul { padding-left: 20px; margin: 0 0 12px; }
.prose li { margin-bottom: 6px; }
.prose .refs { font-size: 13px; color: var(--text-2); }
.prose .refs li { margin-bottom: 8px; }
.prose sup a { font-size: 11px; padding: 0 1px; }
.chart-card { background: var(--card); border-radius: 16px; padding: 14px 14px 10px; margin: 16px 0; }
.chart-card .cap { font-size: 13px; color: var(--text-2); margin-top: 6px; }
.facts { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 10px; margin: 16px 0; }
.fact { background: var(--card); border-radius: 14px; padding: 12px 14px; }
.fact .n { font-family: var(--font-display); font-size: 24px; font-weight: 600; color: var(--accent); }
.fact .t { font-size: 13px; color: var(--text-2); margin-top: 2px; line-height: 1.35; }

/* ---------- login ---------- */
.login { min-height: 100dvh; display: grid; place-items: center; padding: 24px 20px; }
.login .box { width: min(360px, 100%); text-align: center; }
.login img { width: 84px; height: 84px; border-radius: 20px; margin: 0 auto 18px; display: block; }
.login h1 { font-family: var(--font-display); font-size: 28px; margin: 0 0 6px; }
.login p { color: var(--text-2); margin: 0 0 26px; }
.login input {
  width: 100%; height: 50px; border-radius: 12px; border: 0; background: var(--card); padding: 0 16px; font-size: 17px; outline: none;
}
.login input:focus { box-shadow: 0 0 0 2px var(--accent); }
.login button { margin-top: 12px; }
.login .error { color: var(--red); font-size: 14px; min-height: 20px; margin-top: 10px; }

/* ---------- toast ---------- */
.toast {
  position: fixed; left: 50%; bottom: calc(var(--tabbar-h) + var(--safe-bottom) + 18px); z-index: 80;
  transform: translate(-50%, 20px); opacity: 0; transition: all 0.3s;
  background: rgba(44, 44, 46, 0.96); padding: 11px 18px; border-radius: 22px; font-size: 15px; white-space: nowrap;
  box-shadow: 0 8px 30px rgba(0, 0, 0, 0.4);
}
.toast.show { transform: translate(-50%, 0); opacity: 1; }

.offline-dot { display: inline-block; width: 8px; height: 8px; border-radius: 50%; background: var(--red); margin-right: 6px; }

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { transition-duration: 0.01ms !important; animation-duration: 0.01ms !important; }
}
@media (min-width: 700px) {
  .bulb-grid { grid-template-columns: repeat(4, minmax(0, 1fr)); }
}
