:root {
  --bg: #f6f5f2;
  --surface: #ffffff;
  --surface-2: #f0efeb;
  --text: #1f2328;
  --muted: #6b6f76;
  --line: #e2e0da;
  --accent: #5b5bd6;
  --accent-ink: #ffffff;
  --danger: #c2413b;

  --k-work: #8792a2;
  --k-commute: #c3c8d0;
  --k-prep: #c3c8d0;
  --k-gym: #e8833a;
  --k-high: #7a4fd6;
  --k-medium: #3b7be0;
  --k-low: #1f9e8f;
  --k-meal: #6fb35a;
  --k-routine: #d9649b;
  --k-break: #ebe8e1;
  --k-ink: #ffffff;
  --awake: #fbfaf8;
  --asleep: #ecebe6;
  --today: #fff6dd;
  --radius: 12px;
}
:root[data-theme="dark"] {
  color-scheme: dark;
  --bg: #15161a; --surface: #1e2026; --surface-2: #262830; --text: #e8e9ec; --muted: #9a9ea8;
  --line: #31343d; --accent: #8b8bf0; --accent-ink: #12121a; --danger: #ef7d76;
  --k-work: #5d6676; --k-commute: #3d424c; --k-prep: #3d424c; --k-break: #2a2c33;
  --awake: #1b1d22; --asleep: #121317; --today: #2b2618;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    color-scheme: dark;
    --bg: #15161a; --surface: #1e2026; --surface-2: #262830; --text: #e8e9ec; --muted: #9a9ea8;
    --line: #31343d; --accent: #8b8bf0; --accent-ink: #12121a; --danger: #ef7d76;
    --k-work: #5d6676; --k-commute: #3d424c; --k-prep: #3d424c; --k-break: #2a2c33;
    --awake: #1b1d22; --asleep: #121317; --today: #2b2618;
  }
}

* { box-sizing: border-box; }
body {
  margin: 0; background: var(--bg); color: var(--text);
  font: 15px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}
h1 { font-size: 18px; margin: 0; }
h2 { font-size: 15px; margin: 0 0 12px; }
.muted { color: var(--muted); }
.small { font-size: 13px; }
[hidden] { display: none !important; }

/* header */
body::before {
  content: ""; position: fixed; inset: 0 0 auto; height: env(safe-area-inset-top, 0px); z-index: 30; background: var(--bg);
}
.top {
  position: sticky; top: env(safe-area-inset-top, 0px); z-index: 20; background: var(--bg);
  display: flex; align-items: center; justify-content: space-between; gap: 10px 16px; flex-wrap: wrap;
  padding-block: 12px;
  padding-left: max(24px, env(safe-area-inset-left, 0px)); padding-right: max(24px, env(safe-area-inset-right, 0px));
  border-bottom: 1px solid var(--line);
}
.sync { color: var(--muted); font-size: 11px; }
.sync[data-state="synced"] { color: var(--k-low); }
.sync[data-state="error"] { color: var(--danger); }
.brand { display: flex; align-items: center; gap: 10px; }
.logo { font-size: 26px; color: var(--accent); }
.tabs { display: flex; gap: 4px; background: var(--surface-2); padding: 4px; border-radius: 10px; }
.tabs button {
  border: 0; background: transparent; color: var(--muted); font: inherit; font-weight: 600;
  padding: 6px 16px; border-radius: 7px; cursor: pointer;
}
.tabs button[aria-selected="true"] { background: var(--surface); color: var(--text); box-shadow: 0 1px 2px rgba(0,0,0,.08); }

main {
  max-width: 1280px; margin: 0 auto; padding-block: 20px 60px;
  padding-left: max(24px, env(safe-area-inset-left, 0px)); padding-right: max(24px, env(safe-area-inset-right, 0px));
}

/* cards & forms */
.cards { display: grid; grid-template-columns: repeat(auto-fill, minmax(320px, 1fr)); gap: 16px; }
.card { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 18px; min-width: 0; }
.card.wide { grid-column: 1 / -1; }
.card-head { display: flex; justify-content: space-between; align-items: center; gap: 12px; flex-wrap: wrap; margin-bottom: 12px; }
.card-head h2 { margin: 0; }

/* Collapsible cards: the title is a full-width tap target; collapsed cards show only that row. */
.card-head h2:has(.card-toggle) { flex: 1 1 auto; }
.card-toggle {
  all: unset; box-sizing: border-box; display: flex; align-items: center; justify-content: space-between; gap: 12px;
  width: 100%; min-height: 44px; margin: -12px 0; padding: 12px 0; cursor: pointer;
  font: inherit; color: inherit; -webkit-tap-highlight-color: transparent; touch-action: manipulation;
}
.card-toggle:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; border-radius: 6px; }
.card-arrow {
  flex: none; width: 8px; height: 8px; margin-right: 4px;
  border-right: 2px solid var(--muted); border-bottom: 2px solid var(--muted);
  transform: translateY(-2px) rotate(45deg); transition: transform .15s ease;
}
.card.collapsed .card-arrow { transform: rotate(-45deg); }
.card.collapsed { padding-block: 12px; align-self: start; }
.card.collapsed > h2, .card.collapsed > .card-head { margin-bottom: 0; }
.card.collapsed > :not(h2):not(.card-head), .card.collapsed > .card-head > :not(h2) { display: none !important; }
@media (prefers-reduced-motion: reduce) { .card-arrow { transition: none; } }
.fields { display: grid; grid-template-columns: 1fr 1fr; gap: 10px 12px; }
label { display: flex; flex-direction: column; gap: 4px; font-size: 13px; color: var(--muted); }
label .unit { font-size: 11px; opacity: .8; }
input, select {
  font: inherit; color: var(--text); background: var(--surface-2); border: 1px solid var(--line);
  border-radius: 8px; padding: 7px 9px; min-width: 0;
}
input:focus, select:focus { outline: 2px solid var(--accent); outline-offset: -1px; }
input[type="range"] { padding: 0; accent-color: var(--accent); background: none; border: 0; }
label.check { flex-direction: row; align-items: center; gap: 8px; color: var(--text); font-size: 14px; margin: 10px 0 0; cursor: pointer; }
label.check input { width: 16px; height: 16px; accent-color: var(--accent); }
.range-label { color: var(--text); font-size: 14px; }
.sub-label { font-size: 13px; color: var(--muted); margin: 12px 0 6px; }
.inline-label { flex-direction: row; align-items: center; gap: 6px; }

.btn, .btn-ghost {
  font: inherit; font-weight: 600; border-radius: 9px; padding: 8px 16px; cursor: pointer; border: 1px solid transparent;
  display: inline-flex; align-items: center; gap: 6px; white-space: nowrap;
}
.btn { background: var(--accent); color: var(--accent-ink); }
.btn:hover { filter: brightness(1.07); }
.btn.big { font-size: 17px; padding: 12px 26px; }
.btn-ghost { background: var(--surface); color: var(--text); border-color: var(--line); }
.btn-ghost:hover { background: var(--surface-2); }
.btn-ghost.sm, .btn.sm { padding: 5px 11px; font-size: 13px; }
.btn-ghost.danger { color: var(--danger); }
.icon-btn { border: 0; background: none; color: var(--muted); cursor: pointer; font-size: 14px; padding: 4px 6px; border-radius: 6px; }
.icon-btn:hover { background: var(--surface-2); color: var(--text); }
.row-btns { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; }
.file-btn { flex-direction: row; color: var(--text); font-size: 15px; }
.next-row { display: flex; justify-content: flex-end; align-items: center; gap: 8px 16px; flex-wrap: wrap; margin-top: 20px; }
.next-row p { margin: 0; max-width: 46ch; text-align: right; }

.intro { background: var(--surface); border: 1px solid var(--line); border-left: 4px solid var(--accent); border-radius: var(--radius); padding: 14px 18px; margin-bottom: 16px; }

.daytogs { display: flex; gap: 4px; flex-wrap: wrap; }
.daytog {
  font: inherit; font-size: 12px; font-weight: 600; border: 1px solid var(--line); background: var(--surface-2);
  color: var(--muted); border-radius: 6px; padding: 4px 7px; cursor: pointer; min-width: 38px;
}
.daytog[aria-pressed="true"] { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); }

.list-rows { display: flex; flex-direction: column; gap: 8px; margin-bottom: 10px; }
.row-inline { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.row-inline input[type="number"] { width: 70px; }
.row-inline input.name { flex: 1 1 140px; }
.routine-row { padding: 10px; background: var(--surface-2); border-radius: 10px; }
.routine-row input { background: var(--surface); }

/* work schedule */
.work-row { display: flex; gap: 12px; padding: 8px 0; border-top: 1px solid var(--line); align-items: center; }
.work-row:first-child { border-top: 0; }
.work-day { width: 64px; font-weight: 600; flex-shrink: 0; }
.shifts { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; }
.shift { display: inline-flex; align-items: center; gap: 4px; background: var(--surface-2); padding: 4px 4px 4px 6px; border-radius: 9px; }
.shift input { background: var(--surface); padding: 4px 6px; }
.off { font-size: 13px; padding: 0 4px; }

/* tasks */
.task-form { display: grid; grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); gap: 10px 12px; align-items: end; padding-bottom: 16px; border-bottom: 1px solid var(--line); }
.task-form .grow { grid-column: 1 / -1; }
.form-checks { display: flex; gap: 16px; flex-wrap: wrap; grid-column: 1 / -1; }
.form-checks label.check { margin: 0; }
.form-actions { display: flex; gap: 8px; grid-column: 1 / -1; justify-content: flex-end; }
.task-item { display: flex; gap: 12px; align-items: center; padding: 10px 0; border-bottom: 1px solid var(--line); }
.task-dot { width: 10px; height: 10px; border-radius: 50%; flex-shrink: 0; }
.task-main { flex: 1; min-width: 0; }
.task-name { font-weight: 600; }
.task-meta { font-size: 13px; color: var(--muted); }
.empty-note { color: var(--muted); padding: 16px 0 4px; }

/* plan */
.plan-bar { display: flex; justify-content: space-between; align-items: center; gap: 12px; flex-wrap: wrap; margin-bottom: 12px; }
.chips { display: flex; gap: 8px; flex-wrap: wrap; }
.chip { background: var(--surface); border: 1px solid var(--line); border-radius: 999px; padding: 4px 12px; font-size: 13px; }
.chip b { font-variant-numeric: tabular-nums; }
.warn { background: var(--surface); border: 1px solid var(--line); border-left: 4px solid var(--danger); border-radius: var(--radius); padding: 12px 16px; margin-bottom: 12px; }
.warn ul { margin: 6px 0 0; padding-left: 20px; }
.hint { margin: 0 0 10px; }

.grid-scroll { overflow-x: auto; background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); }
.grid { display: grid; grid-template-columns: 52px repeat(7, minmax(120px, 1fr)); min-width: 900px; }
.col-head { position: sticky; top: 0; height: 44px; display: flex; flex-direction: column; justify-content: center; align-items: center; border-bottom: 1px solid var(--line); border-left: 1px solid var(--line); font-size: 13px; }
.col-head b { font-size: 14px; }
.col-head.today { background: var(--today); }
.axis-head { border-bottom: 1px solid var(--line); }
.axis { position: relative; }
.axis span { position: absolute; right: 6px; font-size: 11px; color: var(--muted); transform: translateY(-50%); }
.col { position: relative; border-left: 1px solid var(--line); background: var(--asleep); }
.col.today { box-shadow: inset 0 0 0 2px var(--today); }
.awake { position: absolute; left: 0; right: 0; background: var(--awake); }
.hline { position: absolute; left: 0; right: 0; border-top: 1px dashed var(--line); opacity: .7; }
.sleep-tag { position: absolute; left: 4px; font-size: 10px; color: var(--muted); }

.blk {
  position: absolute; left: 3px; right: 3px; border-radius: 6px; border: 0; padding: 2px 6px;
  font: inherit; font-size: 12px; line-height: 1.25; text-align: left; overflow: hidden; color: var(--k-ink);
  cursor: default; display: block;
}
.blk .t { display: block; font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.blk .tm { display: block; opacity: .85; font-size: 11px; white-space: nowrap; }
.blk.clickable { cursor: pointer; }
.blk.clickable:hover { filter: brightness(1.08); box-shadow: 0 2px 6px rgba(0,0,0,.15); }
.k-work { background: var(--k-work); }
.k-commute, .k-prep, .k-travel { background: var(--k-commute); color: var(--text); }
.k-gym { background: var(--k-gym); }
.k-task.p-high { background: var(--k-high); }
.k-task.p-medium { background: var(--k-medium); }
.k-task.p-low { background: var(--k-low); }
.k-meal { background: var(--k-meal); }
.k-routine { background: var(--k-routine); }
.k-break {
  background: repeating-linear-gradient(135deg, var(--k-break) 0 6px, transparent 6px 12px);
  color: var(--muted); font-style: italic; left: 10px; right: 10px;
}
.blk.done { opacity: .55; }
.blk.done .t::before { content: "✓ "; }
.blk.skipped { opacity: .45; }
.blk.skipped .t { text-decoration: line-through; }

.legend { display: flex; flex-wrap: wrap; gap: 14px; margin-top: 12px; font-size: 12px; color: var(--muted); }
.legend span { display: inline-flex; align-items: center; gap: 6px; }
.legend i { width: 12px; height: 12px; border-radius: 3px; display: inline-block; }

/* list view */
.agenda { display: grid; grid-template-columns: repeat(auto-fill, minmax(260px, 1fr)); gap: 12px; }
.agenda .day { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 14px; }
.agenda .day.today { border-color: var(--accent); }
.agenda h3 { margin: 0 0 8px; font-size: 15px; }
.agenda .it { display: flex; gap: 10px; padding: 4px 0; font-size: 14px; align-items: baseline; }
.agenda .it .tm { color: var(--muted); font-size: 12px; width: 120px; flex-shrink: 0; font-variant-numeric: tabular-nums; }
.agenda .it.brk { color: var(--muted); font-style: italic; font-size: 13px; }
.agenda .it.clickable { cursor: pointer; }
.agenda .it.done .lbl { text-decoration: line-through; opacity: .6; }
.agenda .it.skipped .lbl { text-decoration: line-through; color: var(--danger); }
.agenda .sw { width: 8px; height: 8px; border-radius: 50%; flex-shrink: 0; transform: translateY(-1px); }

.empty { text-align: center; padding: 60px 0; color: var(--muted); }

/* insights */
.ins-row { display: flex; align-items: center; gap: 10px; padding: 4px 0; font-size: 14px; }
.ins-row .lbl { width: 190px; flex-shrink: 0; }
.bar { flex: 1; height: 8px; background: var(--surface-2); border-radius: 4px; overflow: hidden; max-width: 260px; }
.bar i { display: block; height: 100%; background: var(--accent); }

/* popover & toast */
.pop {
  position: fixed; z-index: 50; background: var(--surface); border: 1px solid var(--line); border-radius: 12px;
  box-shadow: 0 10px 30px rgba(0,0,0,.18); padding: 14px; width: 300px; max-height: calc(100vh - 16px); overflow-y: auto;
}
.pop h4 { margin: 0 0 2px; font-size: 15px; }
.pop .row-btns { margin-top: 12px; }
.toast {
  position: fixed; bottom: 24px; left: 50%; transform: translateX(-50%); z-index: 60;
  background: var(--text); color: var(--bg); padding: 10px 18px; border-radius: 10px; font-size: 14px;
}

/* "why here?" inside the block popover */
.sr { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }
.why { margin-top: 12px; padding-top: 10px; border-top: 1px solid var(--line); }
.why-h { font-size: 11px; font-weight: 700; letter-spacing: .06em; text-transform: uppercase; color: var(--muted); margin-bottom: 6px; }
.why ul { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 5px; }
.why li { display: flex; gap: 8px; font-size: 13px; line-height: 1.35; }
.why-ic { flex-shrink: 0; width: 12px; font-size: 10px; padding-top: 2px; }
.why li.up .why-ic { color: var(--k-low); }
.why li.down .why-ic { color: var(--danger); }
.why-foot { font-size: 12px; color: var(--muted); margin-top: 8px; }

/* progress */
.nudge { display: flex; gap: 12px; align-items: center; justify-content: space-between; flex-wrap: wrap; }
.stats { display: grid; grid-template-columns: repeat(auto-fit, minmax(180px, 1fr)); gap: 12px; margin-bottom: 16px; }
.stat { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 14px 16px; display: flex; flex-direction: column; gap: 6px; }
.stat-l { font-size: 11px; font-weight: 700; letter-spacing: .06em; text-transform: uppercase; color: var(--muted); }
.stat-n { font-size: 30px; font-weight: 700; line-height: 1.1; font-variant-numeric: tabular-nums; }
.stat-n span { font-size: 15px; font-weight: 600; color: var(--muted); }
.stat-s { font-size: 12px; color: var(--muted); }
.meter { height: 6px; background: var(--surface-2); border-radius: 3px; overflow: hidden; }
.meter i { display: block; height: 100%; background: var(--accent); border-radius: 3px; }

.type-row {
  display: grid; grid-template-columns: minmax(90px, 1fr) minmax(120px, 1.2fr) minmax(90px, 1fr) auto;
  gap: 8px 16px; align-items: center; padding: 10px 0; border-top: 1px solid var(--line); font-size: 14px;
}
.type-row:first-child { border-top: 0; }
.type-name { font-weight: 600; }
.type-streak b, .type-rate b { font-variant-numeric: tabular-nums; }
.wkbars { display: flex; gap: 2px; align-items: flex-end; height: 32px; }
.wk { width: 14px; height: 100%; display: flex; align-items: flex-end; background: var(--surface-2); border-radius: 4px 4px 0 0; }
.wk i { display: block; width: 100%; background: var(--accent); border-radius: 4px 4px 0 0; }
.wk.none { background: transparent; border-bottom: 2px solid var(--line); border-radius: 0; }
.wk:hover i { filter: brightness(1.15); }

/* in-page dialog */
.modal {
  position: fixed; inset: 0; z-index: 70; background: rgba(0,0,0,.4);
  display: flex; align-items: center; justify-content: center;
  padding: max(16px, env(safe-area-inset-top, 0px)) max(16px, env(safe-area-inset-right, 0px)) max(16px, env(safe-area-inset-bottom, 0px)) max(16px, env(safe-area-inset-left, 0px));
}
.modal-card { background: var(--surface); color: var(--text); border-radius: 14px; padding: 20px; width: min(420px, 100%); box-shadow: 0 20px 50px rgba(0,0,0,.3); }
.modal-card p { margin: 0 0 16px; }
.modal-card textarea { width: 100%; font: 12px/1.4 ui-monospace, Consolas, monospace; background: var(--surface-2); color: var(--text); border: 1px solid var(--line); border-radius: 8px; padding: 8px; margin-bottom: 12px; }
.row-btns.end { justify-content: flex-end; }
#notif-code-row { max-width: 260px; margin-bottom: 12px; }
#notif-code-row[hidden], #notif-card[hidden] { display: none; }
#notif-hint:empty { display: none; }

/* phone day picker */
.daypick { display: grid; grid-template-columns: repeat(7, 1fr); gap: 4px; margin-bottom: 12px; }
.daypick button {
  font: inherit; border: 1px solid var(--line); background: var(--surface); color: var(--text); border-radius: 10px;
  padding: 6px 0 4px; display: flex; flex-direction: column; align-items: center; cursor: pointer; line-height: 1.2;
}
.daypick span { font-size: 11px; color: var(--muted); }
.daypick b { font-size: 16px; font-variant-numeric: tabular-nums; }
.daypick i { font-style: normal; font-size: 10px; color: var(--accent); letter-spacing: 1px; height: 12px; }
.daypick button.is-today { border-color: var(--accent); }
.daypick button[aria-selected="true"] { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
.daypick button[aria-selected="true"] span, .daypick button[aria-selected="true"] i { color: var(--accent-ink); }

@media (max-width: 700px) {
  .top { padding-left: max(16px, env(safe-area-inset-left, 0px)); padding-right: max(16px, env(safe-area-inset-right, 0px)); padding-block: 10px; }
  .tabs { width: 100%; }
  .tabs button { flex: 1; padding: 8px 0; }
  main { padding-left: max(16px, env(safe-area-inset-left, 0px)); padding-right: max(16px, env(safe-area-inset-right, 0px)); padding-block: 16px 60px; }
  .fields { grid-template-columns: 1fr; }
  .cards { grid-template-columns: 1fr; }
  .card { padding: 16px; }
  .cards:has(> [data-card]) { gap: 10px; }
  .work-row { flex-direction: column; align-items: flex-start; gap: 6px; }
  .task-form { grid-template-columns: 1fr 1fr; }
  input, select { font-size: 16px; } /* stops iOS zooming into fields */
  .agenda { grid-template-columns: 1fr; }
  .agenda .it { padding: 7px 0; font-size: 15px; }
  .agenda .it .tm { width: 112px; }
  .agenda .it.clickable .lbl { text-decoration-line: none; }
  .next-row .btn { width: 100%; justify-content: center; }
  .plan-bar { flex-direction: column; align-items: stretch; }
  .pop { width: min(300px, calc(100vw - 32px)); }
  .tabs button { padding: 8px 0; font-size: 14px; }
  .type-row { grid-template-columns: 1fr auto; }
  .type-streak, .type-rate { font-size: 13px; }
  .stats { grid-template-columns: 1fr 1fr; }
  .stats .stat:last-child { grid-column: 1 / -1; }
}

@media print {
  .top .tabs, .plan-bar .row-btns, .hint, .next-row { display: none !important; }
  body { background: #fff; }
  .grid-scroll { overflow: visible; border: 0; }
}
