:root {
  --bg: #000;
  --surface: #1c1c1e;
  --surface-2: #2c2c2e;
  --text: #fff;
  --muted: #98989f;
  --accent: #ff9f0a;      /* warm amber */
  --accent-soft: rgba(255,159,10,.16);
  --blue: #0a84ff;
  --green: #30d158;
  --red: #ff453a;
  --radius: 18px;
  --sat: env(safe-area-inset-top);
  --sab: env(safe-area-inset-bottom);
}

/* Dark is the default. Light only when explicitly chosen, or Auto + a light device. */
:root[data-theme="light"] {
  --bg: #f2f2f7;
  --surface: #fff;
  --surface-2: #e9e9ef;
  --text: #000;
  --muted: #8a8a8e;
}
@media (prefers-color-scheme: light) {
  :root[data-theme="auto"] {
    --bg: #f2f2f7;
    --surface: #fff;
    --surface-2: #e9e9ef;
    --text: #000;
    --muted: #8a8a8e;
  }
}

* { box-sizing: border-box; margin: 0; padding: 0; -webkit-tap-highlight-color: transparent; }

body {
  font-family: -apple-system, BlinkMacSystemFont, "SF Pro Text", "Segoe UI", system-ui, sans-serif;
  background: var(--bg);
  color: var(--text);
  -webkit-font-smoothing: antialiased;
  padding: calc(var(--sat) + 8px) 16px calc(var(--sab) + 24px);
  max-width: 560px;
  margin: 0 auto;
  min-height: 100vh;
}

.muted { color: var(--muted); }

header {
  display: flex; align-items: center; justify-content: space-between;
  padding: 8px 4px 20px; cursor: pointer;
}
header h1 { font-size: 34px; font-weight: 800; letter-spacing: -.5px; }
header .muted { font-size: 15px; margin-top: 2px; }
.gear { color: var(--muted); font-size: 22px; }

/* status */
.status { display: grid; grid-template-columns: repeat(3, 1fr); gap: 10px; margin-bottom: 16px; }
.stat {
  background: var(--surface); border-radius: var(--radius);
  padding: 14px 12px; display: flex; flex-direction: column; gap: 6px;
}
.stat-label { font-size: 12px; color: var(--muted); text-transform: uppercase; letter-spacing: .4px; font-weight: 600; }
.stat-value { font-size: 17px; font-weight: 700; }

/* actions */
.actions { display: grid; grid-template-columns: repeat(3, 1fr); gap: 12px; margin-bottom: 12px; }
.action {
  border: none; border-radius: var(--radius); background: var(--surface); color: var(--text);
  padding: 22px 8px; font-size: 16px; font-weight: 700;
  display: flex; flex-direction: column; align-items: center; gap: 8px; cursor: pointer;
  transition: transform .08s ease, background .15s ease;
}
.action:active { transform: scale(.96); }
.action-icon { font-size: 30px; line-height: 1; }
.action.feed { background: var(--accent-soft); }
.action.sleep.active { background: var(--blue); color: #fff; }
.action-wide {
  width: 100%; border: none; border-radius: 14px; background: var(--surface); color: var(--muted);
  padding: 14px; font-size: 15px; font-weight: 600; cursor: pointer; margin-bottom: 24px;
}
.action-wide:active { background: var(--surface-2); }

/* tallies */
.tallies { display: grid; grid-template-columns: repeat(3,1fr); gap: 10px; margin-bottom: 20px; }
.tally { background: var(--surface); border-radius: 14px; padding: 12px; text-align: center; }
.tally span { display: block; font-size: 22px; font-weight: 800; }
.tally small { color: var(--muted); font-size: 11px; }

.recent h2 { font-size: 20px; font-weight: 700; margin: 4px 4px 10px; }

/* event list */
#event-list { list-style: none; display: flex; flex-direction: column; gap: 8px; }
.event {
  background: var(--surface); border-radius: 14px; padding: 12px 14px;
  display: flex; align-items: center; gap: 12px; cursor: pointer;
}
.event:active { background: var(--surface-2); }
.ev-icon { font-size: 22px; }
.ev-body { display: flex; flex-direction: column; flex: 1; min-width: 0; }
.ev-title { font-weight: 600; font-size: 15px; }
.ev-sub { color: var(--muted); font-size: 13px; }
.ev-time { color: var(--muted); font-size: 14px; font-variant-numeric: tabular-nums; }
.empty { color: var(--muted); text-align: center; padding: 24px; font-size: 15px; }

/* age hero */
.age-hero { display: grid; grid-template-columns: repeat(4, 1fr); gap: 10px; background: var(--surface); border-radius: var(--radius); padding: 18px 12px; margin-bottom: 16px; }
.age-block { text-align: center; }
.age-num { display: block; font-size: 30px; font-weight: 800; font-variant-numeric: tabular-nums; letter-spacing: -1px; }
.age-block small { color: var(--muted); font-size: 11px; text-transform: uppercase; letter-spacing: .5px; }
.age-hero.unset { display: block; text-align: center; padding: 22px; color: var(--muted); cursor: pointer; font-weight: 600; }

.sync-status { text-align: center; font-size: 12px; font-weight: 600; padding: 0 0 10px; min-height: 14px; }
.sync-status.ok { color: var(--muted); }
.sync-status.err { color: var(--red); cursor: pointer; }

/* history toggle */
.list-head { display: flex; margin: 4px 4px 12px; }
.seg { display: inline-flex; background: var(--surface); border-radius: 11px; padding: 3px; }
.seg-btn { border: none; background: transparent; color: var(--muted); padding: 7px 18px; border-radius: 9px; font-weight: 700; font-size: 14px; cursor: pointer; font-family: inherit; }
.seg-btn.active { background: var(--surface-2); color: var(--text); }
.day-header { list-style: none; color: var(--muted); font-size: 12px; font-weight: 700; text-transform: uppercase; letter-spacing: .6px; padding: 16px 4px 2px; }

/* wide screens: use the room */
@media (min-width: 700px) {
  body { max-width: 880px; }
  .age-hero { padding: 30px; gap: 24px; }
  .age-num { font-size: 52px; }
  .age-block small { font-size: 13px; }
  #event-list { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; align-content: start; }
  .day-header { grid-column: 1 / -1; }
}

/* sheets */
#backdrop {
  position: fixed; inset: 0; background: rgba(0,0,0,.5); opacity: 0; pointer-events: none;
  transition: opacity .2s ease; z-index: 10;
}
#backdrop.show { opacity: 1; pointer-events: auto; }
.sheet {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 20;
  background: var(--surface); border-radius: 22px 22px 0 0;
  padding: 10px 20px calc(var(--sab) + 24px); transform: translateY(110%);
  transition: transform .28s cubic-bezier(.32,.72,0,1);
  max-width: 560px; margin: 0 auto;
}
.sheet.show { transform: translateY(0); }
.sheet-grip { width: 38px; height: 5px; background: var(--muted); opacity: .4; border-radius: 3px; margin: 4px auto 14px; }
.sheet h3 { font-size: 22px; font-weight: 700; margin-bottom: 16px; }
.hint { color: var(--muted); font-size: 13px; margin: 10px 0 4px; }

.timers { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.timer-btn {
  background: var(--surface-2); border: 2px solid transparent; border-radius: 16px;
  padding: 20px; color: var(--text); cursor: pointer; display: flex; flex-direction: column; gap: 6px; align-items: center;
}
.timer-btn small { color: var(--muted); font-weight: 600; }
.timer-btn .timer { font-size: 30px; font-weight: 800; font-variant-numeric: tabular-nums; }
.timer-btn.running { border-color: var(--accent); background: var(--accent-soft); }
.timer-btn .timer-start { font-size: 11px; color: var(--muted); font-weight: 600; min-height: 13px; }

.field-row { display: flex; gap: 10px; margin: 14px 0; }
.field-row label { flex: 1; display: flex; flex-direction: column; gap: 6px; font-size: 13px; color: var(--muted); font-weight: 600; }
label.block { display: flex; flex-direction: column; gap: 8px; font-size: 14px; color: var(--muted); font-weight: 600; margin-bottom: 16px; }
input, select {
  background: var(--surface-2); border: none; border-radius: 12px; padding: 14px;
  color: var(--text); font-size: 17px; width: 100%; font-family: inherit;
}
#feed-notes { margin-top: 4px; }
textarea { background: var(--surface-2); border: none; border-radius: 12px; padding: 12px; color: var(--text); font-family: ui-monospace, Menlo, monospace; font-size: 12px; width: 100%; height: 200px; resize: vertical; }

.big-choices { display: grid; grid-template-columns: repeat(3,1fr); gap: 12px; }
.big-choices button {
  background: var(--surface-2); border: none; border-radius: 16px; padding: 22px 8px;
  color: var(--text); font-size: 15px; font-weight: 700; cursor: pointer;
  display: flex; flex-direction: column; align-items: center; gap: 8px;
}
.big-choices button span { font-size: 15px; }
.big-choices button:first-child { font-size: 26px; }
.big-choices button:active { background: var(--accent-soft); }
.big-choices.four { grid-template-columns: repeat(2, 1fr); }

.sheet-actions { display: flex; gap: 10px; margin-top: 22px; }
.sheet-actions button { flex: 1; border: none; border-radius: 14px; padding: 16px; font-size: 16px; font-weight: 700; cursor: pointer; font-family: inherit; }
.primary { background: var(--accent); color: #000; }
.ghost { background: var(--surface-2); color: var(--text); }
.danger { background: transparent; color: var(--red); flex: 0 0 auto !important; padding: 16px 18px !important; }

/* login gate */
#login { position: fixed; inset: 0; z-index: 100; background: var(--bg); display: none; align-items: center; justify-content: center; padding: 24px; }
.login-card { max-width: 320px; width: 100%; text-align: center; display: flex; flex-direction: column; gap: 14px; }
.login-card h2 { font-size: 34px; font-weight: 800; }
.login-card input { text-align: center; }
#login-err { display: none; color: var(--red) !important; }
.ghost.small { border: none; border-radius: 10px; padding: 9px 14px; font-size: 13px; font-weight: 700; cursor: pointer; font-family: inherit; margin: 6px 0 2px; }
#edit-fields:not(:empty) { margin: 4px 0; }
