:root {
  --bg: #f4f1ea;
  --panel: #fbf9f4;
  --ink: #23211d;
  --muted: #7a746a;
  --line: #e2ddd2;
  --me: #2f5d50;
  --me-ink: #ffffff;
  --them: #ffffff;
  --them-ink: #23211d;
  --accent: #2f5d50;
  --error: #a0392b;
  --radius: 18px;
  color-scheme: light dark;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #1c1b19;
    --panel: #232220;
    --ink: #ece8e0;
    --muted: #9a948a;
    --line: #34322e;
    --me: #3f7a69;
    --me-ink: #ffffff;
    --them: #2c2b28;
    --them-ink: #ece8e0;
    --accent: #6fb39f;
    --error: #e0806f;
  }
}

* { box-sizing: border-box; }

html, body { margin: 0; height: 100%; }

body {
  background: var(--bg);
  color: var(--ink);
  font: 16px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  -webkit-text-size-adjust: 100%;
}

button { font: inherit; cursor: pointer; }

/* --- login ------------------------------------------------------------- */

body.login { display: grid; place-items: center; padding: 16px; }

.login-box {
  width: 100%;
  max-width: 340px;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 28px 24px;
}

.login-box h1 { margin: 0 0 18px; font-size: 28px; font-weight: 600; letter-spacing: -0.01em; }

.login-box label { display: block; margin-bottom: 14px; color: var(--muted); font-size: 14px; }

.login-box input {
  display: block;
  width: 100%;
  margin-top: 4px;
  padding: 11px 12px;
  font: inherit;
  font-size: 16px;
  color: var(--ink);
  background: var(--bg);
  border: 1px solid var(--line);
  border-radius: 10px;
}

.login-box input:focus { outline: 2px solid var(--accent); outline-offset: -1px; }

.login-box button {
  width: 100%;
  margin-top: 6px;
  padding: 12px;
  border: 0;
  border-radius: 10px;
  background: var(--accent);
  color: #fff;
  font-weight: 600;
}

.login-error { color: var(--error); margin: 0 0 14px; font-size: 14px; }

/* --- chat -------------------------------------------------------------- */

body.chat {
  display: flex;
  flex-direction: column;
  height: 100dvh;
}

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 10px 16px;
  padding-top: max(10px, env(safe-area-inset-top));
  background: var(--panel);
  border-bottom: 1px solid var(--line);
}

.tabs { display: flex; align-items: center; gap: 4px; }

.tabs a {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 6px 10px;
  border-radius: 10px;
  color: var(--muted);
  text-decoration: none;
  font-weight: 600;
}

.tabs a.on { color: var(--ink); }
.tabs a:hover { background: color-mix(in srgb, var(--ink) 6%, transparent); }

.avatar {
  display: grid;
  place-items: center;
  width: 30px;
  height: 30px;
  border-radius: 50%;
  background: var(--accent);
  color: #fff;
  font-weight: 600;
}


button.link {
  background: none;
  border: 0;
  padding: 4px 0;
  color: var(--muted);
  font-size: 14px;
}

.log {
  flex: 1;
  overflow-y: auto;
  padding: 16px;
  display: flex;
  flex-direction: column;
  gap: 4px;
  overscroll-behavior: contain;
}

.log > :first-child { margin-top: auto; }

.day {
  align-self: center;
  margin: 14px 0 8px;
  color: var(--muted);
  font-size: 12px;
}

.msg {
  max-width: min(78%, 640px);
  padding: 8px 13px;
  border-radius: var(--radius);
  overflow-wrap: anywhere;
}

.msg.user {
  align-self: flex-end;
  background: var(--me);
  color: var(--me-ink);
  border-bottom-right-radius: 6px;
  white-space: pre-wrap;
}

.msg.assistant {
  align-self: flex-start;
  background: var(--them);
  color: var(--them-ink);
  border: 1px solid var(--line);
  border-bottom-left-radius: 6px;
}

.msg.assistant > :first-child { margin-top: 0; }
.msg.assistant > :last-child { margin-bottom: 0; }
.msg.assistant p { margin: 0 0 0.6em; }
.msg.assistant ul, .msg.assistant ol { margin: 0 0 0.6em; padding-left: 1.3em; }
.msg.assistant a { color: var(--accent); }
.msg.assistant code {
  font: 0.9em ui-monospace, SFMono-Regular, Menlo, monospace;
  background: color-mix(in srgb, var(--ink) 8%, transparent);
  padding: 0.1em 0.3em;
  border-radius: 4px;
}
.msg.assistant pre {
  margin: 0 0 0.6em;
  padding: 8px 10px;
  overflow-x: auto;
  background: color-mix(in srgb, var(--ink) 8%, transparent);
  border-radius: 8px;
}
.msg.assistant pre code { background: none; padding: 0; }

.msg.pending::after {
  content: "…";
  color: var(--muted);
  animation: blink 1s steps(2) infinite;
}

@keyframes blink { 50% { opacity: 0; } }

.msg-error {
  align-self: flex-start;
  margin: 2px 4px 6px;
  color: var(--error);
  font-size: 13px;
}

.time {
  font-size: 11px;
  color: var(--muted);
  margin: 0 6px 6px;
}
.time.user { align-self: flex-end; }
.time.assistant { align-self: flex-start; }

.composer {
  display: flex;
  align-items: flex-end;
  gap: 8px;
  padding: 10px 12px;
  padding-bottom: max(10px, env(safe-area-inset-bottom));
  background: var(--panel);
  border-top: 1px solid var(--line);
}

.composer textarea {
  flex: 1;
  resize: none;
  max-height: 40vh;
  padding: 9px 14px;
  font: inherit;
  font-size: 16px; /* keeps iOS from zooming on focus */
  color: var(--ink);
  background: var(--bg);
  border: 1px solid var(--line);
  border-radius: 20px;
}

.composer textarea:focus { outline: 2px solid var(--accent); outline-offset: -1px; }

.composer button {
  flex: none;
  display: grid;
  place-items: center;
  width: 40px;
  height: 40px;
  border: 0;
  border-radius: 50%;
  background: var(--accent);
  color: #fff;
}

.composer button:disabled { opacity: 0.4; cursor: default; }

/* --- activity bar and receipts ---------------------------------------- */

.activity {
  align-self: center;
  display: flex;
  align-items: center;
  gap: 8px;
  margin: 0 auto 8px;
  padding: 6px 14px;
  max-width: calc(100% - 32px);
  border-radius: 999px;
  background: var(--panel);
  border: 1px solid var(--line);
  color: var(--muted);
  font-size: 13px;
  box-shadow: 0 2px 10px color-mix(in srgb, var(--ink) 10%, transparent);
}

.activity[hidden] { display: none; }

.activity span:last-child {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.spinner {
  flex: none;
  width: 12px;
  height: 12px;
  border-radius: 50%;
  border: 2px solid var(--line);
  border-top-color: var(--accent);
  animation: spin 0.8s linear infinite;
}

.activity[data-state="done"] .spinner {
  animation: none;
  border-color: var(--accent);
  background: var(--accent);
}

.activity[data-state="error"] .spinner { animation: none; border-color: var(--error); }

@keyframes spin { to { transform: rotate(360deg); } }

.receipts {
  align-self: flex-start;
  margin: 3px 6px 0;
  font-size: 13px;
  color: var(--muted);
}

.receipts a { color: var(--accent); text-decoration: none; }
.receipts a:hover { text-decoration: underline; }

/* --- pages: Everything, topic, note ----------------------------------- */

body.page { min-height: 100dvh; }

.content {
  max-width: 720px;
  margin: 0 auto;
  padding: 16px 16px 48px;
}

.content h1 { font-size: 24px; margin: 4px 0 6px; letter-spacing: -0.01em; }
.content h2 {
  font-size: 13px;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--muted);
  margin: 26px 0 8px;
}

.crumb { margin: 4px 0 0; font-size: 13px; color: var(--muted); }
.crumb a, .content p a { color: var(--accent); }
.muted { color: var(--muted); font-size: 14px; margin: 0 0 8px; }
.empty { color: var(--muted); margin-top: 32px; }

.search { display: flex; gap: 8px; margin: 4px 0 12px; }

.search input {
  flex: 1;
  min-width: 0;
  padding: 10px 14px;
  font: inherit;
  font-size: 16px;
  color: var(--ink);
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 12px;
}

.search input:focus { outline: 2px solid var(--accent); outline-offset: -1px; }

.search button {
  padding: 0 16px;
  border: 0;
  border-radius: 12px;
  background: var(--accent);
  color: #fff;
  font-weight: 600;
}

.items {
  list-style: none;
  margin: 0;
  padding: 0;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 14px;
  overflow: hidden;
}

.items li + li { border-top: 1px solid var(--line); }

.items li > a, .items li.excerpt, .items li:not(:has(a)) {
  display: flex;
  flex-wrap: wrap;
  align-items: baseline;
  justify-content: space-between;
  gap: 2px 12px;
  padding: 11px 14px;
  color: var(--ink);
  text-decoration: none;
}

.items li > a:hover { background: color-mix(in srgb, var(--ink) 4%, transparent); }
.items li.excerpt { flex-direction: column; align-items: stretch; }
.items li.inactive { opacity: 0.55; }
.items .title { overflow-wrap: anywhere; }
.meta { color: var(--muted); font-size: 13px; }
.meta.due { color: var(--accent); font-weight: 600; }

.card {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 14px;
  padding: 12px 16px;
}

.md > :first-child { margin-top: 0; }
.md > :last-child { margin-bottom: 0; }
.md p { margin: 0 0 0.7em; }
.md ul, .md ol { margin: 0 0 0.7em; padding-left: 1.3em; }

.facts {
  display: grid;
  grid-template-columns: minmax(90px, max-content) 1fr;
  gap: 8px 16px;
  margin: 0;
  padding: 12px 16px;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 14px;
}

.facts dt { color: var(--muted); font-size: 14px; }
.facts dd { margin: 0; overflow-wrap: anywhere; }
.facts.past dd { color: var(--muted); }
