:root {
  --bg: #f6f1ea;
  --surface: #fffdf9;
  --surface-2: #efe8de;
  --text: #2a2622;
  --muted: #7b7168;
  --line: #e3dacd;
  --accent: #2f5d50;
  --accent-text: #ffffff;
  --user-bubble: #2f5d50;
  --user-text: #f6f1ea;
  --danger: #b4432f;
  --warm: #e9b872;
  --radius: 18px;
  --font: -apple-system, BlinkMacSystemFont, "SF Pro Text", "Segoe UI", "PingFang SC", "Hiragino Sans GB", sans-serif;
  --serif: "New York", "Iowan Old Style", Georgia, "Songti SC", serif;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #1b1917;
    --surface: #24211e;
    --surface-2: #2e2a26;
    --text: #ece6de;
    --muted: #9d948a;
    --line: #38332e;
    --accent: #7fb7a4;
    --accent-text: #10201b;
    --user-bubble: #35574c;
    --user-text: #eef5f1;
    --danger: #e0806c;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body {
  background: var(--bg);
  color: var(--text);
  font: 17px/1.5 var(--font);
  -webkit-font-smoothing: antialiased;
  -webkit-text-size-adjust: 100%;
  display: flex;
  flex-direction: column;
  height: 100dvh;
  overflow: hidden;
}
button { font: inherit; color: inherit; cursor: pointer; -webkit-tap-highlight-color: transparent; }
svg { width: 24px; height: 24px; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; }

.topbar {
  display: flex; align-items: center; justify-content: space-between;
  padding: calc(env(safe-area-inset-top) + 6px) 12px 8px;
  border-bottom: 1px solid var(--line);
  background: var(--bg);
}
.title { display: flex; flex-direction: column; align-items: center; line-height: 1.2; }
.brand { font-family: var(--serif); font-size: 20px; font-weight: 600; letter-spacing: .3px; }
.subtitle { font-size: 13px; color: var(--muted); }
.icon-btn { background: none; border: 0; width: 44px; height: 44px; border-radius: 12px; display: grid; place-items: center; color: var(--muted); }
.icon-btn.active { color: var(--accent); }

.view { flex: 1; overflow-y: auto; -webkit-overflow-scrolling: touch; padding: 16px 16px 24px; }
.view-title { font-family: var(--serif); font-weight: 600; font-size: 24px; margin: 4px 0 16px; }

/* Chat */
.messages { display: flex; flex-direction: column; gap: 14px; max-width: 680px; margin: 0 auto; }
.empty { text-align: center; color: var(--muted); margin-top: 18vh; padding: 0 24px; }
.empty h2 { font-family: var(--serif); color: var(--text); font-weight: 600; font-size: 26px; margin: 0 0 8px; }
.msg { max-width: 86%; white-space: pre-wrap; word-wrap: break-word; }
.msg.user { align-self: flex-end; background: var(--user-bubble); color: var(--user-text); padding: 10px 14px; border-radius: var(--radius) var(--radius) 6px var(--radius); }
.msg.assistant { align-self: flex-start; font-family: var(--serif); font-size: 18px; line-height: 1.55; padding: 2px 2px; }
.msg.assistant.pending::after { content: ""; display: inline-block; width: 8px; height: 8px; margin-left: 4px; border-radius: 50%; background: var(--muted); animation: pulse 1s infinite ease-in-out; }
.msg time { display: block; font: 12px var(--font); color: var(--muted); margin-top: 4px; }
.msg.user time { color: color-mix(in srgb, var(--user-text) 70%, transparent); text-align: right; }
.msg.error { color: var(--danger); font-family: var(--font); font-size: 15px; }
@keyframes pulse { 0%, 100% { opacity: .25 } 50% { opacity: 1 } }

/* Composer */
.composer { border-top: 1px solid var(--line); background: var(--bg); padding: 8px 10px calc(env(safe-area-inset-bottom) + 8px); }
.composer[hidden] { display: none; }
.composer-row { display: flex; align-items: flex-end; gap: 8px; max-width: 680px; margin: 0 auto; }
#input {
  flex: 1; resize: none; border: 1px solid var(--line); background: var(--surface); color: var(--text);
  border-radius: 22px; padding: 10px 16px; font: 17px/1.4 var(--font); max-height: 40vh; outline: none;
}
#input:focus { border-color: var(--accent); }
.mic, .send { flex: none; width: 44px; height: 44px; border-radius: 50%; border: 0; display: grid; place-items: center; }
.mic { background: var(--surface-2); color: var(--text); }
.mic.recording { background: var(--danger); color: #fff; animation: ring 1.6s infinite; }
.mic.busy { opacity: .5; }
.send { background: var(--accent); color: var(--accent-text); }
.send:disabled { opacity: .35; }
@keyframes ring { 0% { box-shadow: 0 0 0 0 color-mix(in srgb, var(--danger) 50%, transparent) } 100% { box-shadow: 0 0 0 14px transparent } }
.recording { display: flex; align-items: center; gap: 8px; max-width: 680px; margin: 0 auto 8px; font-size: 15px; color: var(--muted); }
.recording[hidden] { display: none; }
.recording .dot { width: 10px; height: 10px; border-radius: 50%; background: var(--danger); animation: pulse 1s infinite; }
.recording #rec-time { color: var(--text); font-variant-numeric: tabular-nums; }
.rec-hint { margin-left: auto; font-size: 13px; }

/* History */
.days { list-style: none; padding: 0; margin: 0 auto; max-width: 680px; }
.days li button { width: 100%; text-align: left; background: var(--surface); border: 1px solid var(--line); border-radius: 14px; padding: 12px 14px; margin-bottom: 10px; }
.days .d { font-weight: 600; }
.days .n { color: var(--muted); font-size: 14px; margin-left: 6px; }
.days .h { display: block; color: var(--muted); font-size: 15px; margin-top: 2px; overflow: hidden; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; }
.back-today { display: block; margin: 8px auto 0; background: none; border: 1px solid var(--line); border-radius: 999px; padding: 6px 14px; font-size: 15px; color: var(--accent); }

/* Memory */
#memory { max-width: 680px; margin: 0 auto; }
.section { margin-bottom: 28px; }
.section > h2 { font-family: var(--serif); font-weight: 600; font-size: 22px; margin: 0 0 4px; }
.section > p.hint { color: var(--muted); font-size: 14px; margin: 0 0 12px; }
.card { background: var(--surface); border: 1px solid var(--line); border-radius: 14px; padding: 12px 14px; margin-bottom: 10px; }
.profile h3 { font-family: var(--serif); font-size: 17px; margin: 14px 0 4px; }
.profile h3:first-child { margin-top: 0; }
.profile ul { margin: 0; padding-left: 20px; }
.profile p { margin: 4px 0; }
.chip { display: inline-block; font-size: 12px; padding: 1px 8px; border-radius: 999px; background: var(--surface-2); color: var(--muted); margin-left: 6px; vertical-align: 2px; }
.chip.active { background: color-mix(in srgb, var(--accent) 18%, transparent); color: var(--accent); }
.fact { display: flex; gap: 8px; align-items: flex-start; padding: 8px 0; border-top: 1px solid var(--line); }
.fact:first-of-type { border-top: 0; }
.fact .s { flex: 1; }
.fact .meta { display: block; font-size: 12px; color: var(--muted); }
.fact.past .s { color: var(--muted); }
.fact-actions { display: flex; gap: 2px; }
.fact-actions button { background: none; border: 0; padding: 4px 6px; font-size: 13px; color: var(--muted); border-radius: 8px; }
.fact-actions button.on { color: var(--accent); font-weight: 600; }
details summary { cursor: pointer; list-style: none; }
details summary::-webkit-details-marker { display: none; }
details > summary .who { font-weight: 600; }
details > summary .rel { color: var(--muted); font-size: 15px; }
details[open] > summary { margin-bottom: 6px; }
.person-summary { color: var(--muted); font-size: 15px; margin: 0 0 6px; }
.add-fact { display: flex; gap: 8px; }
.add-fact input { flex: 1; border: 1px solid var(--line); border-radius: 12px; padding: 10px 12px; font: 16px var(--font); background: var(--surface); color: var(--text); }
.add-fact button, .btn { border: 0; background: var(--accent); color: var(--accent-text); border-radius: 12px; padding: 10px 14px; font-size: 15px; }
.btn.secondary { background: var(--surface-2); color: var(--text); }
.toolbar { display: flex; gap: 8px; flex-wrap: wrap; }
.loading { color: var(--muted); text-align: center; margin-top: 20vh; }

/* Sign-in gate */
body.gated .topbar, body.gated .composer, body.gated .view:not(.gate) { display: none !important; }
.gate { display: flex; align-items: center; justify-content: center; padding: 24px 20px calc(env(safe-area-inset-bottom) + 24px); }
.gate-card { max-width: 340px; width: 100%; text-align: center; display: flex; flex-direction: column; gap: 16px; }
.gate-brand { font-size: 44px; margin: 0; }
.gate-card p { margin: 0; line-height: 1.5; color: var(--muted, inherit); }
.gate-card .btn { width: 100%; padding: 14px 16px; font-size: 16px; }
.gate-error:empty { display: none; }
.card.row { display: flex; align-items: center; justify-content: space-between; gap: 12px; }
