:root { color-scheme: light; }
* { box-sizing: border-box; }
body {
  margin: 0;
  font-family: Georgia, "Times New Roman", serif;
  line-height: 1.65;
  color: #222;
  background: #fafaf7;
}
header.site, footer.site, main { max-width: 42rem; margin: 0 auto; padding: 1rem 1.25rem; }
header.site { border-bottom: 1px solid #ddd; }
header.site .title { font-size: 1.1rem; color: #555; text-decoration: none; }
article h1 { font-size: 1.9rem; line-height: 1.25; }
article h2 { font-size: 1.3rem; margin-top: 2rem; }
footer.site { border-top: 1px solid #ddd; color: #777; font-size: 0.9rem; margin-top: 2rem; }
a.dot { color: #777; text-decoration: none; padding: 0 0.4rem; }
.notfound { text-align: center; padding-top: 4rem; }
form.login { max-width: 22rem; margin: 3rem auto; display: grid; gap: 0.8rem; }
form.login label { display: grid; gap: 0.25rem; }
form.login input { padding: 0.5rem; font: inherit; }
form.login button { padding: 0.55rem; font: inherit; cursor: pointer; }
.error { color: #a00; margin: 0; }
form.login input[type="checkbox"] { padding: 0; margin-right: 0.4rem; }
form.login label.check { display: block; }
.policy h1 { font-size: 1.6rem; }
.policy h2 { font-size: 1.15rem; margin-top: 1.5rem; }

/* ===== Chat app (body.app). Colours are CSS variables; theme = data-theme on <html>. ===== */
html[data-theme] {
  --bg: #f7f7f5; --panel: #efefec; --surface: #ffffff; --text: #1f2328; --muted: #6a6f76;
  --border: #dcdcd7; --accent: #2f6f5e; --accent-text: #ffffff; --hover: #e4e4df;
  --user-bg: #e8efe9; --code-bg: #f3f3f0; --danger: #b42318; --error-bg: #fdecea;
  color-scheme: light;
}
html[data-theme="dark"] {
  --bg: #17191c; --panel: #1e2125; --surface: #23272b; --text: #e6e6e3; --muted: #9aa0a6;
  --border: #33383e; --accent: #5fb39b; --accent-text: #0f1a17; --hover: #2b3035;
  --user-bg: #22322d; --code-bg: #0d1117; --danger: #f1867d; --error-bg: #3a2220;
  color-scheme: dark;
}
@media (prefers-color-scheme: dark) {
  html[data-theme="system"] {
    --bg: #17191c; --panel: #1e2125; --surface: #23272b; --text: #e6e6e3; --muted: #9aa0a6;
    --border: #33383e; --accent: #5fb39b; --accent-text: #0f1a17; --hover: #2b3035;
    --user-bg: #22322d; --code-bg: #0d1117; --danger: #f1867d; --error-bg: #3a2220;
    color-scheme: dark;
  }
}
[hidden] { display: none !important; }
body.app {
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  font-size: 15px; line-height: 1.55; color: var(--text); background: var(--bg);
}
body.app button, body.app input, body.app select, body.app textarea { font: inherit; color: inherit; }
body.app button {
  background: var(--surface); border: 1px solid var(--border); border-radius: 6px;
  padding: 0.3rem 0.7rem; cursor: pointer;
}
body.app button:hover { background: var(--hover); }
body.app button.primary { background: var(--accent); color: var(--accent-text); border-color: var(--accent); }
body.app a { color: var(--accent); }
.layout { display: flex; height: 100vh; height: 100dvh; }

/* --- sidebar --- */
.sidebar {
  width: 260px; flex: none; display: flex; flex-direction: column; gap: 0.5rem;
  padding: 0.75rem; background: var(--panel); border-right: 1px solid var(--border);
}
.sidebar .new-chat { width: 100%; padding: 0.5rem; text-align: left; }
.chat-list { flex: 1; overflow-y: auto; }
.chat-list ul, .results { list-style: none; margin: 0; padding: 0; }
.chat-item { position: relative; display: flex; align-items: center; gap: 0.2rem; border-radius: 6px; }
.chat-item:hover, .chat-item.current { background: var(--hover); }
body.app .chat-link {
  flex: 1; min-width: 0; padding: 0.35rem 0.5rem; color: var(--text);
  text-decoration: none; white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.chat-item.current .chat-link { font-weight: 600; }
.pin-mark { font-size: 0.8rem; }
.chat-empty { color: var(--muted); padding: 0.35rem 0.5rem; }
.all-chats-link { padding: 0.25rem 0.5rem; text-decoration: none; }

/* <details> menus: open/close without JS */
.chat-actions > summary, .user-menu > summary { list-style: none; cursor: pointer; }
.chat-actions > summary::-webkit-details-marker,
.user-menu > summary::-webkit-details-marker { display: none; }
.chat-actions > summary { padding: 0 0.45rem; color: var(--muted); border-radius: 4px; }
.chat-actions > summary:hover { background: var(--border); }
.menu {
  position: absolute; z-index: 10; display: grid; gap: 0.3rem; min-width: 12rem;
  padding: 0.5rem; background: var(--surface); border: 1px solid var(--border);
  border-radius: 8px; box-shadow: 0 6px 20px rgba(0, 0, 0, 0.15);
}
.chat-actions .menu { right: 0; top: 100%; }
.menu .rename { display: grid; gap: 0.3rem; }
.menu input { padding: 0.3rem; border: 1px solid var(--border); border-radius: 4px; background: var(--bg); }
.menu button, .menu a { text-align: left; }
body.app .menu a { padding: 0.3rem 0.7rem; text-decoration: none; color: var(--text); border-radius: 6px; }
.menu a:hover { background: var(--hover); }
.menu form { margin: 0; display: grid; }
body.app button.danger { color: var(--danger); }
.user-menu { position: relative; border-top: 1px solid var(--border); padding-top: 0.5rem; }
.user-menu > summary { padding: 0.35rem 0.5rem; border-radius: 6px; }
.user-menu > summary:hover { background: var(--hover); }
.user-menu .menu { bottom: 100%; left: 0; }

/* --- chat column --- */
.chat { flex: 1; min-width: 0; display: flex; flex-direction: column; }
.chat-head {
  display: flex; align-items: center; justify-content: space-between; gap: 1rem;
  padding: 0.6rem 1.25rem; border-bottom: 1px solid var(--border);
}
.chat-title { font-size: 1.05rem; margin: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
body.app button.link-button { border: none; background: none; color: var(--accent); }
.messages { flex: 1; overflow-y: auto; padding: 1rem 1.25rem; }
.messages > * { max-width: 48rem; margin-left: auto; margin-right: auto; }
.empty-hint { color: var(--muted); text-align: center; margin-top: 4rem; }
.msg { margin-bottom: 1.25rem; }
.msg[data-role="user"] { display: flex; flex-direction: column; align-items: flex-end; }
.msg[data-role="user"] .body {
  white-space: pre-wrap; background: var(--user-bg); padding: 0.6rem 0.9rem; border-radius: 10px;
}
.msg .body { overflow-wrap: anywhere; }
.msg .body > :first-child { margin-top: 0; }
.msg .body pre {
  position: relative; background: var(--code-bg); border: 1px solid var(--border);
  border-radius: 8px; padding: 0; overflow: hidden;
}
.msg .body pre code { display: block; overflow-x: auto; padding: 0.8rem 1rem; background: transparent; }
.msg .body code { font-family: ui-monospace, "Cascadia Mono", Consolas, monospace; font-size: 0.9em; }
.msg .body :not(pre) > code { background: var(--code-bg); padding: 0.1rem 0.3rem; border-radius: 4px; }
.msg .body table { border-collapse: collapse; }
.msg .body th, .msg .body td { border: 1px solid var(--border); padding: 0.3rem 0.6rem; }
.msg .body blockquote { margin: 0; padding-left: 1rem; border-left: 3px solid var(--border); color: var(--muted); }
body.app button.copy {
  position: absolute; top: 0.4rem; right: 0.4rem; font-size: 0.75rem; padding: 0.1rem 0.5rem; opacity: 0.85;
}
.meta { display: flex; flex-wrap: wrap; align-items: center; gap: 0.5rem; margin-top: 0.3rem; color: var(--muted); font-size: 0.82rem; }
body.app .meta button.save { font-size: 0.78rem; padding: 0.05rem 0.45rem; }
.error-banner {
  display: flex; align-items: center; gap: 0.75rem; flex-wrap: wrap; margin-top: 0.4rem;
  padding: 0.5rem 0.8rem; border-radius: 8px; background: var(--error-bg); color: var(--danger);
}
.chips { list-style: none; display: flex; flex-wrap: wrap; gap: 0.35rem; margin: 0 0 0.35rem; padding: 0; }
.chips:empty { display: none; }
.chip { background: var(--hover); border: 1px solid var(--border); border-radius: 999px; padding: 0.05rem 0.6rem; font-size: 0.82rem; }
body.app button.chip-remove { border: none; background: none; padding: 0 0.15rem; color: var(--muted); }

/* --- composer --- */
.composer { padding: 0.6rem 1.25rem 0.8rem; border-top: 1px solid var(--border); }
.composer > * { max-width: 48rem; margin-left: auto; margin-right: auto; }
.composer-row { display: flex; align-items: flex-end; gap: 0.5rem; }
.composer textarea {
  flex: 1; resize: none; min-height: 2.6rem; max-height: 15rem; padding: 0.55rem 0.75rem;
  border: 1px solid var(--border); border-radius: 10px; background: var(--surface);
}
.composer textarea:focus { outline: 2px solid var(--accent); outline-offset: -1px; }
.attach {
  display: inline-flex; align-items: center; justify-content: center; width: 2.4rem; height: 2.6rem;
  font-size: 1.3rem; border: 1px solid var(--border); border-radius: 10px; background: var(--surface); cursor: pointer;
}
.composer-row > button { height: 2.6rem; }
.composer-meta {
  display: flex; justify-content: space-between; gap: 1rem; flex-wrap: wrap;
  margin-top: 0.4rem; color: var(--muted); font-size: 0.85rem;
}
.composer-meta select {
  max-width: 16rem; padding: 0.1rem 0.3rem; background: var(--surface);
  border: 1px solid var(--border); border-radius: 6px;
}

/* --- all chats --- */
.all-chats { padding: 1rem 1.25rem; overflow-y: auto; }
.all-chats > * { max-width: 48rem; margin-left: auto; margin-right: auto; }
.all-chats h1 { font-size: 1.3rem; }
.search { display: flex; gap: 0.5rem; margin-bottom: 1rem; }
.search input { flex: 1; padding: 0.4rem 0.6rem; border: 1px solid var(--border); border-radius: 6px; background: var(--surface); }
.result { position: relative; display: flex; align-items: center; gap: 0.6rem; padding: 0.4rem 0.5rem; border-bottom: 1px solid var(--border); }
.result-link { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.result time { color: var(--muted); font-size: 0.85rem; }
.pager { display: flex; justify-content: space-between; margin-top: 1rem; }

/* --- narrow screens: sidebar above the chat --- */
@media (max-width: 760px) {
  .layout { flex-direction: column; height: auto; min-height: 100vh; }
  .sidebar { width: auto; border-right: none; border-bottom: 1px solid var(--border); }
  .chat-list { max-height: 30vh; }
  .chat { min-height: 70vh; }
  .messages, .composer, .chat-head, .all-chats { padding-left: 0.75rem; padding-right: 0.75rem; }
}

/* --- settings --- */
.settings-page { padding: 1rem 1.25rem; overflow-y: auto; }
.settings-page > *, .settings-page section > * { max-width: 40rem; }
.settings-page h1 { font-size: 1.3rem; }
.settings-page h2 { font-size: 1.1rem; margin-top: 2rem; border-top: 1px solid var(--border); padding-top: 1rem; }
.settings-page h3 { font-size: 1rem; margin-top: 1.25rem; }
.settings-page .stack { display: grid; gap: 0.7rem; }
.settings-page .stack label { display: grid; gap: 0.25rem; }
.settings-page .stack label.check { display: block; }
.settings-page input[type="text"], .settings-page input[type="password"], .settings-page select, .settings-page textarea {
  padding: 0.4rem 0.6rem; border: 1px solid var(--border); border-radius: 6px; background: var(--surface);
}
.settings-page .error { color: var(--danger); }
.settings-page .notice { background: var(--user-bg); padding: 0.5rem 0.8rem; border-radius: 8px; }
.settings-page .muted { color: var(--muted); font-size: 0.9rem; }
.settings-page .qr svg { max-width: 14rem; height: auto; background: #fff; }
.sessions { list-style: none; padding: 0; margin: 0 0 0.75rem; display: grid; gap: 0.6rem; }
.sessions li { border: 1px solid var(--border); border-radius: 8px; padding: 0.5rem 0.75rem; background: var(--surface); }
.sessions .badge { background: var(--accent); color: var(--accent-text); border-radius: 999px; padding: 0 0.5rem; font-size: 0.78rem; }
.sessions form { margin-top: 0.3rem; }
.spend-table { border-collapse: collapse; }
.spend-table th, .spend-table td { border: 1px solid var(--border); padding: 0.3rem 0.7rem; text-align: left; }

.admin-page > * { max-width: none; }
.admin-table { border-collapse: collapse; width: 100%; margin: 0.75rem 0; font-size: 0.9rem; }
.admin-table th, .admin-table td { border-bottom: 1px solid var(--border); padding: 0.4rem 0.5rem; text-align: left; vertical-align: middle; }
.admin-page form.inline { display: inline-flex; gap: 0.3rem; margin-right: 0.4rem; }
.admin-page .invite-new { margin: 0.75rem 0; }
.admin-page .invite-new input { max-width: 100%; }
