:root {
  --bg: #f5f1ea;
  --surface: #fffdf9;
  --raised: #ffffff;
  --ink: #1f1d1a;
  --muted: #6d675e;
  --line: #e3ddd2;
  --accent: #2f5d50;
  --accent-ink: #ffffff;
  --bubble: #e4ece6;
  --danger: #a63b32;
  --radius: 14px;
  --serif: "Iowan Old Style", "Palatino Linotype", Palatino, Georgia, serif;
  color-scheme: light dark;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #131614;
    --surface: #1a1e1b;
    --raised: #222723;
    --ink: #e9e6df;
    --muted: #9a968c;
    --line: #2d332f;
    --accent: #8fc7b2;
    --accent-ink: #10231c;
    --bubble: #25332d;
    --danger: #e58a80;
  }
}
* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { height: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--ink);
  font: 16px/1.55 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  -webkit-text-size-adjust: 100%;
  overscroll-behavior: none;
}
h1, h2, h3 { font-family: var(--serif); font-weight: 600; margin: 0; text-wrap: balance; }
h3 { font-size: 1.15rem; margin-bottom: 4px; }
p { margin: 0 0 10px; }
code { font: 0.9em ui-monospace, SFMono-Regular, Menlo, monospace; background: var(--bubble); padding: 1px 5px; border-radius: 5px; overflow-wrap: anywhere; }
.muted { color: var(--muted); font-size: 0.92rem; }
em.muted { font-style: normal; font-size: 0.85rem; }
.error { color: var(--danger); min-height: 1.2em; font-size: 0.92rem; margin: 0; }
.note { font-size: 0.92rem; min-height: 1.2em; margin: 0; overflow-wrap: anywhere; }

input, textarea, select, button { font: inherit; color: inherit; }
input, textarea, select {
  width: 100%;
  background: var(--raised);
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 10px 12px;
  min-width: 0;
}
textarea { resize: vertical; }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
.field { display: block; margin-bottom: 12px; }
.field > span { display: block; font-size: 0.88rem; font-weight: 600; margin-bottom: 4px; }
.check { display: flex; gap: 8px; align-items: center; margin-bottom: 14px; }
.check input { width: auto; }
.row { display: flex; gap: 8px; align-items: center; }
.row.end { align-items: center; }
.grow { flex: 1; min-width: 0; }

.btn {
  display: inline-flex; align-items: center; justify-content: center;
  border: 1px solid var(--line); background: var(--raised);
  border-radius: 10px; padding: 9px 16px; cursor: pointer;
  text-decoration: none; white-space: nowrap; font-weight: 500;
}
.btn:hover { border-color: var(--accent); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
.btn.primary:hover { filter: brightness(1.08); }
.btn.danger { color: var(--danger); }
.btn.wide { width: 100%; }
.btn:disabled { opacity: 0.55; cursor: default; }
.icon-btn { background: none; border: 0; padding: 8px; border-radius: 10px; cursor: pointer; display: none; }

/* gate */
.gate { min-height: 100%; display: grid; place-items: center; padding: 24px 16px; }
.gate-card { width: 100%; max-width: 380px; background: var(--surface); border: 1px solid var(--line); border-radius: 20px; padding: 28px 24px; }
.gate-card h1 { font-size: 1.7rem; margin: 10px 0 6px; }
.gate-card .btn { width: 100%; margin-top: 8px; padding: 12px; }

/* shell */
.app { display: flex; height: 100dvh; }
.drawer {
  width: 280px; flex: none; background: var(--surface); border-right: 1px solid var(--line);
  display: flex; flex-direction: column; padding: 16px 12px calc(12px + env(safe-area-inset-bottom));
  padding-top: calc(16px + env(safe-area-inset-top));
}
.brand { display: flex; align-items: center; gap: 10px; font-family: var(--serif); font-size: 1.2rem; font-weight: 600; padding: 0 6px 14px; }
.conv-list { flex: 1; overflow-y: auto; margin: 12px 0; display: flex; flex-direction: column; gap: 2px; }
.conv {
  display: flex; align-items: center; gap: 4px; border-radius: 10px; padding-right: 4px;
}
.conv:hover, .conv.active { background: var(--bubble); }
.conv-open { flex: 1; min-width: 0; text-align: left; background: none; border: 0; padding: 9px 10px; cursor: pointer; border-radius: 10px; }
.conv-title { display: block; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.conv-sub { display: block; font-size: 0.78rem; color: var(--muted); }
.conv-del { background: none; border: 0; color: var(--muted); cursor: pointer; padding: 6px 8px; border-radius: 8px; opacity: 0; }
.conv:hover .conv-del, .conv.active .conv-del, .conv-del:focus-visible { opacity: 1; }
.conv-del:hover { color: var(--danger); }
.empty-list { color: var(--muted); font-size: 0.9rem; padding: 8px 10px; }
.drawer-foot { display: flex; gap: 4px; border-top: 1px solid var(--line); padding-top: 10px; }
.nav-btn { flex: 1; background: none; border: 0; padding: 9px 4px; border-radius: 10px; cursor: pointer; font-size: 0.92rem; }
.nav-btn:hover, .nav-btn.active { background: var(--bubble); }
.scrim { display: none; }

.main { flex: 1; min-width: 0; display: flex; flex-direction: column; }
.topbar {
  display: flex; align-items: center; gap: 10px; padding: 10px 16px;
  padding-top: calc(10px + env(safe-area-inset-top));
  border-bottom: 1px solid var(--line);
}
.topbar h2 { flex: 1; min-width: 0; font-size: 1.1rem; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.view { flex: 1; min-height: 0; display: flex; flex-direction: column; }
.page { overflow-y: auto; padding: 20px 16px calc(40px + env(safe-area-inset-bottom)); display: block; }
.page > * { max-width: 680px; margin-left: auto; margin-right: auto; }
.card { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 18px; margin-bottom: 16px; }
.card h3:not(:first-child) { margin-top: 18px; }

/* chat */
.messages { flex: 1; overflow-y: auto; padding: 20px 16px 8px; }
.messages > * { max-width: 720px; margin-left: auto; margin-right: auto; }
.msg { margin-bottom: 18px; overflow-wrap: anywhere; }
.msg.user { display: flex; justify-content: flex-end; }
.msg.user .body { background: var(--bubble); padding: 9px 14px; border-radius: 18px 18px 4px 18px; max-width: 85%; white-space: pre-wrap; }
.msg.assistant .body > :first-child { margin-top: 0; }
.msg.assistant .body > :last-child { margin-bottom: 0; }
.msg .body h1, .msg .body h2, .msg .body h3, .msg .body h4 { font-size: 1.05rem; margin: 14px 0 6px; }
.msg .body ul, .msg .body ol { margin: 0 0 10px; padding-left: 22px; }
.msg .body li { margin-bottom: 3px; }
.msg .body pre { background: var(--surface); border: 1px solid var(--line); border-radius: 10px; padding: 10px 12px; overflow-x: auto; }
.msg .body pre code { background: none; padding: 0; }
.msg .body a { color: var(--accent); }
.msg .foot { color: var(--muted); font-size: 0.78rem; margin-top: 6px; display: flex; gap: 10px; flex-wrap: wrap; align-items: center; }
.msg .foot button { background: none; border: 0; padding: 0; color: var(--accent); cursor: pointer; font-size: inherit; text-decoration: underline; }
.msg.failed .body { color: var(--danger); }
.typing::after { content: "▍"; animation: blink 1s steps(2) infinite; color: var(--muted); }
@keyframes blink { 50% { opacity: 0; } }
@media (prefers-reduced-motion: reduce) { .typing::after { animation: none; } }

.welcome { text-align: center; padding: 8vh 0 0; }
.welcome h1 { font-size: 1.8rem; margin-bottom: 8px; }

.composer {
  display: flex; gap: 8px; align-items: flex-end; width: 100%; max-width: 752px; margin: 0 auto;
  padding: 8px 16px calc(12px + env(safe-area-inset-bottom));
}
.composer textarea { resize: none; max-height: 180px; border-radius: 22px; padding: 11px 16px; }
.send { flex: none; width: 46px; height: 46px; border-radius: 50%; border: 0; background: var(--accent); color: var(--accent-ink); cursor: pointer; display: grid; place-items: center; }
.send:disabled { opacity: 0.5; cursor: default; }

/* memory */
.mem-list { margin-top: 14px; }
.mem {
  display: block; width: 100%; text-align: left; background: var(--surface); border: 1px solid var(--line);
  border-radius: 12px; padding: 12px 14px; margin-bottom: 8px; cursor: pointer;
}
.mem:hover { border-color: var(--accent); }
.mem-text { display: block; }
.mem-meta { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 8px; font-size: 0.78rem; color: var(--muted); align-items: center; }
.tag { background: var(--bubble); color: var(--ink); border-radius: 999px; padding: 1px 9px; }
.tag.domain { background: none; border: 1px solid var(--line); color: var(--muted); }

.token { display: flex; align-items: center; gap: 8px; padding: 6px 0; border-bottom: 1px solid var(--line); }
.token span { flex: 1; min-width: 0; }
#token-list { margin-bottom: 12px; }

dialog { border: 1px solid var(--line); border-radius: 18px; background: var(--surface); color: var(--ink); padding: 20px; width: min(520px, calc(100vw - 32px)); }
dialog::backdrop { background: rgba(0, 0, 0, 0.45); }
.dialog-form h3 { margin-bottom: 12px; }

@media (max-width: 800px) {
  .icon-btn { display: inline-flex; }
  .drawer {
    position: fixed; inset: 0 auto 0 0; z-index: 20; width: min(300px, 86vw);
    transform: translateX(-100%); transition: transform 0.2s ease; visibility: hidden;
  }
  .app.drawer-open .drawer { transform: none; visibility: visible; }
  .app.drawer-open .scrim { display: block; position: fixed; inset: 0; z-index: 10; background: rgba(0, 0, 0, 0.4); }
  .conv-del { opacity: 1; }
  .topbar { padding-left: 8px; }
}
@media (prefers-reduced-motion: reduce) { .drawer { transition: none; } }

/* reply progress */
.send .i-stop, .send.busy .i-send { display: none; }
.send.busy .i-stop { display: block; }
.msg .status { color: var(--muted); font-size: 0.88rem; display: flex; align-items: center; gap: 8px; margin-bottom: 4px; }
.msg .status::before { content: ""; width: 8px; height: 8px; border-radius: 50%; background: var(--accent); animation: pulse 1.2s ease-in-out infinite; }
@keyframes pulse { 50% { opacity: 0.25; } }
@media (prefers-reduced-motion: reduce) { .msg .status::before { animation: none; } }

/* model picker above the composer */
.modelbar { display: flex; align-items: center; gap: 6px; width: 100%; max-width: 752px; margin: 0 auto; padding: 4px 22px 0; font-size: 0.8rem; color: var(--muted); }
.modelbar select { width: auto; min-width: 0; max-width: 100%; flex: 0 1 auto; padding: 3px 6px; font-size: 0.85rem; color: var(--ink); background: var(--raised); border-color: var(--line); border-radius: 8px; text-overflow: ellipsis; }
/* the open dropdown list: without this some browsers draw it white with our text colour */
select option { background: var(--raised); color: var(--ink); }
