:root {
  --bg: #f6f4ef;
  --surface: #ffffff;
  --surface-2: #efece5;
  --line: #e2ded5;
  --text: #22262a;
  --muted: #6b6f73;
  --accent: #4f7a6a;
  --accent-ink: #ffffff;
  --accent-soft: #dfeae4;
  --pull: #b8735a;
  --pull-soft: #f3e2da;
  --urge: #4f7a6a;
  --urge-soft: #dfeae4;
  --journal: #6a6fa8;
  --journal-soft: #e4e5f3;
  --danger: #b4483c;
  --shadow: 0 1px 2px rgba(0,0,0,.05), 0 4px 16px rgba(0,0,0,.05);
  --radius: 16px;
  --tabbar-h: 64px;
  --safe-b: env(safe-area-inset-bottom, 0px);
  --safe-t: env(safe-area-inset-top, 0px);
  color-scheme: light dark;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #16181a;
    --surface: #202326;
    --surface-2: #2a2e31;
    --line: #33383c;
    --text: #e9e7e2;
    --muted: #9a9ea2;
    --accent: #7fb39f;
    --accent-ink: #10201a;
    --accent-soft: #24362f;
    --pull: #d99a80;
    --pull-soft: #3a2a23;
    --urge: #7fb39f;
    --urge-soft: #24362f;
    --journal: #9fa4dc;
    --journal-soft: #2a2c44;
    --danger: #e0786b;
    --shadow: none;
  }
}

* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; }
body {
  background: var(--bg);
  color: var(--text);
  font: 16px/1.45 -apple-system, BlinkMacSystemFont, "SF Pro Text", "Segoe UI", Roboto, sans-serif;
  -webkit-font-smoothing: antialiased;
  -webkit-tap-highlight-color: transparent;
  min-height: 100vh;
  min-height: 100dvh;
}
button, input, textarea, select { font: inherit; color: inherit; }
button { cursor: pointer; }
h1, h2, h3 { margin: 0; line-height: 1.2; }
h1 { font-size: 28px; font-weight: 700; letter-spacing: -.01em; }
h2 { font-size: 20px; font-weight: 650; }
h3 { font-size: 15px; font-weight: 600; }
p { margin: 0; }
.muted { color: var(--muted); }
.small { font-size: 14px; }
.tiny { font-size: 12.5px; }
[hidden] { display: none !important; }

/* ---------- layout ---------- */
.screen {
  max-width: 640px;
  margin: 0 auto;
  padding: calc(var(--safe-t) + 20px) 16px calc(var(--tabbar-h) + var(--safe-b) + 28px);
}
.screen.no-tabs { padding-bottom: calc(var(--safe-b) + 110px); }
.stack > * + * { margin-top: 12px; }
.stack-lg > * + * { margin-top: 22px; }
.row { display: flex; align-items: center; gap: 10px; }
.row.between { justify-content: space-between; }
.grow { flex: 1; min-width: 0; }

.card {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 16px;
  box-shadow: var(--shadow);
}

/* ---------- buttons ---------- */
.btn {
  appearance: none;
  border: 1px solid var(--line);
  background: var(--surface);
  border-radius: 12px;
  padding: 12px 16px;
  font-weight: 600;
  min-height: 46px;
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
}
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
.btn.danger { color: var(--danger); }
.btn.danger.solid { background: var(--danger); border-color: var(--danger); color: #fff; }
.btn.block { width: 100%; }
.btn.ghost { background: transparent; border-color: transparent; color: var(--accent); padding: 8px 10px; min-height: 40px; }
.btn:disabled { opacity: .5; cursor: default; }
.btn:focus-visible, .chip:focus-visible, .tab:focus-visible, .big-action:focus-visible, .seg button:focus-visible, .entry:focus-visible {
  outline: 3px solid color-mix(in srgb, var(--accent) 45%, transparent);
  outline-offset: 2px;
}

.big-actions { display: grid; gap: 12px; }
.big-action {
  appearance: none; text-align: left; width: 100%;
  border: 1px solid var(--line); border-radius: var(--radius);
  background: var(--surface); box-shadow: var(--shadow);
  padding: 16px; display: flex; align-items: center; gap: 14px;
}
.big-action .dot {
  width: 44px; height: 44px; border-radius: 12px; flex: none;
  display: grid; place-items: center; font-size: 20px;
}
.big-action.pull .dot { background: var(--pull-soft); color: var(--pull); }
.big-action.urge .dot { background: var(--urge-soft); color: var(--urge); }
.big-action.journal .dot { background: var(--journal-soft); color: var(--journal); }
.big-action strong { display: block; font-size: 17px; }
.big-action span.muted { font-size: 14px; }

/* ---------- forms ---------- */
label.field { display: block; }
label.field > span { display: block; font-weight: 600; margin-bottom: 6px; }
input[type=text], input[type=password], input[type=datetime-local], input[type=search], textarea, select {
  width: 100%;
  border: 1px solid var(--line);
  background: var(--surface);
  border-radius: 12px;
  padding: 12px 14px;
  font-size: 16px; /* prevents iOS zoom */
}
input:focus, textarea:focus, select:focus { outline: 2px solid var(--accent); outline-offset: -1px; }
textarea { min-height: 120px; resize: vertical; line-height: 1.5; }
textarea.journal { min-height: 46vh; }

.section-title { display: flex; align-items: baseline; justify-content: space-between; gap: 8px; margin-bottom: 10px; }
.section-title .hint { font-size: 13px; color: var(--muted); font-weight: 400; }

.chips { display: flex; flex-wrap: wrap; gap: 8px; }
.chip {
  appearance: none;
  border: 1px solid var(--line);
  background: var(--surface);
  border-radius: 999px;
  padding: 8px 14px;
  font-size: 15px;
  min-height: 38px;
}
.chip[aria-pressed=true] { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
.chip.add { border-style: dashed; color: var(--muted); }
.chip.static { cursor: default; padding: 4px 10px; min-height: 0; font-size: 13px; background: var(--surface-2); border-color: transparent; }

.seg { display: flex; background: var(--surface-2); border-radius: 12px; padding: 3px; gap: 3px; }
.seg button {
  flex: 1; appearance: none; border: 0; background: transparent;
  border-radius: 9px; padding: 9px 6px; font-size: 14px; font-weight: 600; color: var(--muted);
  min-height: 40px;
}
.seg button[aria-pressed=true] { background: var(--surface); color: var(--accent); box-shadow: var(--shadow), inset 0 0 0 1.5px var(--accent); }
.seg.scale button { font-size: 15px; }

.type-pill {
  display: inline-flex; align-items: center; gap: 6px;
  font-size: 12.5px; font-weight: 700; letter-spacing: .02em;
  padding: 3px 9px; border-radius: 999px;
}
.type-pill.pull { background: var(--pull-soft); color: var(--pull); }
.type-pill.urge { background: var(--urge-soft); color: var(--urge); }
.type-pill.journal { background: var(--journal-soft); color: var(--journal); }

/* sticky action bar on editor screens */
.savebar {
  position: fixed; left: 0; right: 0; bottom: 0;
  padding: 12px 16px calc(var(--safe-b) + 12px);
  background: color-mix(in srgb, var(--bg) 88%, transparent);
  -webkit-backdrop-filter: blur(12px); backdrop-filter: blur(12px);
  border-top: 1px solid var(--line);
  z-index: 20;
}
.savebar .inner { max-width: 640px; margin: 0 auto; display: flex; gap: 10px; }
.savebar .btn { flex: 1; }

.topbar { display: flex; align-items: center; justify-content: space-between; gap: 8px; margin-bottom: 18px; }
.topbar .btn.ghost { margin-left: -10px; }

/* ---------- tab bar ---------- */
.tabbar {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 20;
  height: calc(var(--tabbar-h) + var(--safe-b));
  padding-bottom: var(--safe-b);
  background: color-mix(in srgb, var(--surface) 92%, transparent);
  -webkit-backdrop-filter: blur(14px); backdrop-filter: blur(14px);
  border-top: 1px solid var(--line);
  display: flex; justify-content: center;
}
.tabbar .inner { display: flex; width: 100%; max-width: 640px; }
.tab {
  flex: 1; appearance: none; border: 0; background: transparent;
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 3px;
  font-size: 11.5px; font-weight: 600; color: var(--muted);
}
.tab svg { width: 24px; height: 24px; }
.tab[aria-current=page] { color: var(--accent); }

/* ---------- history ---------- */
.day-head { font-size: 13px; font-weight: 700; color: var(--muted); text-transform: uppercase; letter-spacing: .05em; margin: 22px 4px 8px; }
.entry {
  appearance: none; width: 100%; text-align: left;
  background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius);
  padding: 14px 16px; display: block; box-shadow: var(--shadow);
}
.entry + .entry { margin-top: 8px; }
.entry .meta { display: flex; align-items: center; justify-content: space-between; gap: 8px; margin-bottom: 6px; }
.entry .summary { font-size: 14.5px; color: var(--muted); }
.entry .note { margin-top: 6px; font-size: 15px; display: -webkit-box; -webkit-line-clamp: 3; -webkit-box-orient: vertical; overflow: hidden; white-space: pre-wrap; }

.detail dl { margin: 0; }
.detail dt { font-size: 13px; color: var(--muted); font-weight: 600; margin-top: 14px; }
.detail dd { margin: 4px 0 0; }
.detail .note-full { white-space: pre-wrap; line-height: 1.55; }

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

/* ---------- insights ---------- */
.stats { display: grid; grid-template-columns: repeat(3, 1fr); gap: 10px; }
.stat { background: var(--surface); border: 1px solid var(--line); border-radius: 14px; padding: 12px; box-shadow: var(--shadow); }
.stat .num { font-size: 26px; font-weight: 700; line-height: 1.1; font-variant-numeric: tabular-nums; }
.stat .lbl { font-size: 12.5px; color: var(--muted); margin-top: 2px; }

.bars { display: grid; gap: 8px; }
.bar-row { display: grid; grid-template-columns: minmax(90px, 38%) 1fr auto; align-items: center; gap: 10px; font-size: 14.5px; }
.bar-row .name { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.bar-track { display: block; height: 10px; border-radius: 999px; background: var(--surface-2); overflow: hidden; }
.bar-fill { display: block; height: 100%; width: 0; border-radius: 999px; background: var(--accent); }
.bar-fill.pull { background: var(--pull); }
.bar-row .n { font-variant-numeric: tabular-nums; color: var(--muted); min-width: 2ch; text-align: right; }

.daychart { display: flex; align-items: flex-end; gap: 4px; height: 120px; padding-top: 6px; }
.daycol { flex: 1; display: flex; flex-direction: column; justify-content: flex-end; align-items: stretch; height: 100%; gap: 2px; min-width: 0; }
.daycol .seg-pull { display: block; background: var(--pull); border-radius: 3px; height: 0; }
.daycol .seg-urge { display: block; background: var(--urge); border-radius: 3px; height: 0; opacity: .85; }
.daylabels { display: flex; gap: 4px; margin-top: 6px; }
.daylabels span { flex: 1 1 0; display: flex; justify-content: center; font-size: 10.5px; color: var(--muted); min-width: 0; white-space: nowrap; overflow: visible; }
.legend { display: flex; gap: 14px; font-size: 13px; color: var(--muted); margin-top: 10px; }
.legend i { display: inline-block; width: 10px; height: 10px; border-radius: 3px; margin-right: 6px; vertical-align: -1px; }
.legend i.pull { background: var(--pull); }
.legend i.urge { background: var(--urge); }

/* ---------- settings ---------- */
.list { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); overflow: hidden; box-shadow: var(--shadow); }
.list > * { display: flex; align-items: center; justify-content: space-between; gap: 10px; width: 100%; padding: 14px 16px; border: 0; background: transparent; text-align: left; min-height: 52px; }
.list > * + * { border-top: 1px solid var(--line); }
.list .chev { color: var(--muted); }
.list select { width: auto; padding: 6px 10px; }
.list-label { font-size: 13px; color: var(--muted); font-weight: 700; text-transform: uppercase; letter-spacing: .05em; margin: 0 4px 8px; }

/* ---------- lock screen ---------- */
.lock {
  min-height: 100vh; min-height: 100dvh;
  display: flex; flex-direction: column; justify-content: center;
  max-width: 420px; margin: 0 auto;
  padding: calc(var(--safe-t) + 24px) 16px calc(var(--safe-b) + 24px);
}
.lock .logo { width: 64px; height: 64px; border-radius: 16px; margin-bottom: 18px; }
.lock .err { color: var(--danger); font-size: 14px; min-height: 20px; }
.callout { background: var(--accent-soft); border-radius: 14px; padding: 14px 16px; font-size: 14.5px; }
.callout.warn { background: var(--pull-soft); }
.callout ul { margin: 6px 0 0; padding-left: 20px; }
.callout li + li { margin-top: 4px; }

.spinner { width: 18px; height: 18px; border: 2px solid currentColor; border-right-color: transparent; border-radius: 50%; animation: spin .8s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

/* ---------- modal ---------- */
.modal-back {
  position: fixed; inset: 0; z-index: 50;
  background: rgba(0,0,0,.35);
  display: flex; align-items: flex-end; justify-content: center;
  padding: 16px 16px calc(var(--safe-b) + 16px);
}
@media (min-width: 600px) { .modal-back { align-items: center; } }
.modal {
  background: var(--surface); color: var(--text);
  border-radius: 20px; width: 100%; max-width: 440px;
  padding: 20px; box-shadow: 0 10px 40px rgba(0,0,0,.25);
}
.modal .actions { display: flex; gap: 10px; margin-top: 18px; }
.modal .actions .btn { flex: 1; }

/* ---------- toast & privacy cover ---------- */
#toast {
  position: fixed; left: 50%; transform: translateX(-50%);
  bottom: calc(var(--tabbar-h) + var(--safe-b) + 16px);
  background: var(--text); color: var(--bg);
  padding: 10px 16px; border-radius: 12px; font-size: 14.5px; font-weight: 600;
  z-index: 60; max-width: calc(100% - 32px); text-align: center;
}
#cover { position: fixed; inset: 0; background: var(--bg); z-index: 100; }

@media (prefers-reduced-motion: reduce) { .spinner { animation-duration: 2s; } }

.modal-body { margin-top: 8px; }
.modal-body > * + * { margin-top: 8px; }
.m-input { margin-top: 14px; }
.list a { color: inherit; text-decoration: none; }
.btn-danger-text { color: var(--danger); font-weight: 600; }
.callout .btn.ghost { display: flex; padding-left: 0; margin-top: 4px; }
.list + p { margin-top: 8px; padding: 0 4px; }
.detail dt:first-child { margin-top: 0; }
