:root {
  --bg: #f6f5f1; --surface: #ffffff; --text: #1d1d1b; --muted: #6b6a65; --border: #dedcd5;
  --accent: #1f6f5c; --accent-text: #ffffff; --user: #e3efe9; --danger: #a32d2d; --code: #efeee8;
  color-scheme: light dark;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #171716; --surface: #222220; --text: #ecebe6; --muted: #a09f98; --border: #3a3a37;
    --accent: #3c9a82; --accent-text: #0d1a16; --user: #233a33; --danger: #f09595; --code: #2c2c2a;
  }
}
* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { height: 100%; margin: 0; }
body {
  background: var(--bg); color: var(--text);
  font: 16px/1.5 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  -webkit-text-size-adjust: 100%;
}
button, input, textarea { font: inherit; color: inherit; }
button { cursor: pointer; border: 1px solid var(--border); background: var(--surface); border-radius: 10px; padding: 8px 14px; }
button.primary { background: var(--accent); color: var(--accent-text); border-color: var(--accent); font-weight: 600; }
button.icon { border: none; background: none; font-size: 22px; padding: 6px 10px; min-width: 44px; min-height: 44px; }
button.link { border: none; background: none; padding: 4px; color: var(--muted); text-decoration: underline; }
button:disabled { opacity: .5; cursor: default; }
.error { color: var(--danger); margin: 0; }

.login { min-height: 100%; display: grid; place-items: center; padding: 24px; }
.login-card { width: 100%; max-width: 340px; display: grid; gap: 14px; background: var(--surface); border: 1px solid var(--border); border-radius: 16px; padding: 28px 24px; text-align: center; }
.login-card img { margin: 0 auto; border-radius: 16px; }
.login-card h1 { font-size: 22px; font-weight: 600; margin: 0 0 6px; }
.login-card label { display: grid; gap: 4px; text-align: left; font-size: 14px; color: var(--muted); }
.login-card input { padding: 10px 12px; border: 1px solid var(--border); border-radius: 10px; background: var(--bg); font-size: 16px; }

.app { height: 100dvh; display: flex; }
.drawer { width: 290px; flex: none; display: flex; flex-direction: column; background: var(--surface); border-right: 1px solid var(--border); padding-top: env(safe-area-inset-top); }
.drawer-head { padding: 12px; }
.drawer-head button { width: 100%; }
.thread-list { flex: 1; overflow-y: auto; padding: 0 8px; }
.thread { display: flex; align-items: center; border-radius: 10px; }
.thread.active { background: var(--user); }
.thread .open { flex: 1; min-width: 0; text-align: left; border: none; background: none; padding: 10px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.thread .del { border: none; background: none; color: var(--muted); padding: 8px 10px; }
.drawer-foot { display: flex; justify-content: space-between; align-items: center; gap: 8px; padding: 12px; padding-bottom: calc(12px + env(safe-area-inset-bottom)); border-top: 1px solid var(--border); font-size: 14px; color: var(--muted); }
.scrim { position: absolute; inset: 0; background: rgba(0,0,0,.4); z-index: 9; }

.chat { flex: 1; min-width: 0; display: flex; flex-direction: column; }
.topbar { display: flex; align-items: center; gap: 4px; padding: env(safe-area-inset-top) 6px 0; border-bottom: 1px solid var(--border); background: var(--surface); min-height: 52px; }
.topbar h2 { flex: 1; min-width: 0; margin: 0; font-size: 16px; font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; text-align: center; }
.banner { margin: 0; padding: 8px 14px; font-size: 14px; background: var(--code); color: var(--muted); text-align: center; }
.messages { flex: 1; overflow-y: auto; padding: 16px 12px; display: flex; flex-direction: column; gap: 12px; }
.empty { margin: auto; color: var(--muted); text-align: center; }
.msg { max-width: min(720px, 92%); padding: 10px 14px; border-radius: 16px; overflow-wrap: anywhere; }
.msg.user { align-self: flex-end; background: var(--user); border-bottom-right-radius: 4px; white-space: pre-wrap; }
.msg.assistant { align-self: flex-start; background: var(--surface); border: 1px solid var(--border); border-bottom-left-radius: 4px; }
.msg.error { border-color: var(--danger); color: var(--danger); }
.msg p { margin: 0 0 .6em; } .msg p:last-child { margin-bottom: 0; }
.msg ul, .msg ol { margin: 0 0 .6em; padding-left: 1.3em; }
.msg h3 { font-size: 1em; font-weight: 600; margin: .6em 0 .3em; }
.msg code { background: var(--code); border-radius: 4px; padding: 1px 4px; font: 14px/1.4 ui-monospace, Consolas, monospace; }
.msg pre { background: var(--code); border-radius: 8px; padding: 10px; overflow-x: auto; margin: 0 0 .6em; }
.msg pre code { background: none; padding: 0; }
.msg a { color: var(--accent); }
.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; } }

.composer { display: flex; align-items: flex-end; gap: 8px; padding: 10px 12px calc(10px + env(safe-area-inset-bottom)); border-top: 1px solid var(--border); background: var(--surface); }
.composer textarea { flex: 1; resize: none; max-height: 160px; padding: 10px 12px; border: 1px solid var(--border); border-radius: 14px; background: var(--bg); font-size: 16px; line-height: 1.4; }
.composer button { min-width: 46px; min-height: 44px; border-radius: 14px; }

/* The settings are a page of their own (address /settings): the only scrolling area while they are shown. */
.settings { height: 100dvh; overflow-y: auto; overscroll-behavior: contain; -webkit-overflow-scrolling: touch; background: var(--bg); color: var(--text); }
.settings-inner { max-width: 640px; margin: 0 auto; padding: env(safe-area-inset-top) 16px calc(32px + env(safe-area-inset-bottom)); }
.settings header { display: flex; align-items: center; gap: 4px; min-height: 52px; margin-left: -10px; }
.settings header .icon { font-size: 30px; line-height: 1; }
.settings h2 { font-size: 18px; font-weight: 600; margin: 0; }
.settings h3 { font-size: 14px; font-weight: 600; color: var(--muted); margin: 8px 0; }
.settings .note { margin: 0 0 10px; padding: 8px 12px; border-radius: 10px; background: var(--user); }
.settings .note.error { background: none; border: 1px solid var(--danger); }
.integration { border: 1px solid var(--border); border-radius: 12px; padding: 12px; margin-bottom: 10px; }
.integration .row { display: flex; align-items: center; justify-content: space-between; gap: 10px; }
.integration .name { font-weight: 600; }
.integration .state { font-size: 14px; color: var(--muted); }
.integration .state.on { color: var(--accent); }
.integration p, .integration ul { font-size: 14px; color: var(--muted); margin: 8px 0 0; overflow-wrap: anywhere; }
.integration ul { padding-left: 1.2em; }
.drawer-head { display: grid; gap: 8px; }
.inbox-button { display: flex; align-items: center; justify-content: space-between; text-align: left; }
.badge { background: var(--accent); color: var(--accent-text); border-radius: 999px; padding: 0 8px; font-size: 13px; font-weight: 600; line-height: 22px; }
#menu { position: relative; }
.dot { position: absolute; top: 9px; right: 7px; width: 10px; height: 10px; border-radius: 50%; background: var(--danger); }
.integration.unread { border-color: var(--accent); box-shadow: inset 3px 0 0 var(--accent); }
.integration .inbox-body { color: var(--text); font-size: 16px; white-space: pre-line; }
.tabs { display: flex; flex-wrap: wrap; gap: 6px; margin: 6px 0 12px; }
.tabs button { flex: none; padding: 6px 12px; border-radius: 999px; font-size: 14px; }
.tabs button.active { background: var(--accent); color: var(--accent-text); border-color: var(--accent); font-weight: 600; }
.settings .hint { font-size: 14px; color: var(--muted); margin: 0 0 10px; }
.integration.form label.check { display: flex; align-items: center; gap: 10px; color: var(--text); font-size: 16px; }
.integration.form label.check input { width: 20px; height: 20px; padding: 0; flex: none; }
.integration textarea { width: 100%; min-height: 120px; resize: vertical; padding: 9px 11px; border: 1px solid var(--border); border-radius: 10px; background: var(--bg); color: var(--text); font: 14px/1.45 ui-monospace, Consolas, monospace; }
.integration textarea[readonly] { opacity: .75; }
.log-entry { border-bottom: 1px solid var(--border); padding: 8px 0; font-size: 14px; }
.log-entry:last-child { border-bottom: none; }
.log-entry .when { color: var(--muted); }
.log-entry.skipped { color: var(--muted); }
.integration.form { display: grid; gap: 10px; }
.integration.form label { display: grid; gap: 4px; font-size: 14px; color: var(--muted); }
.integration.form input, .integration select { padding: 9px 11px; border: 1px solid var(--border); border-radius: 10px; background: var(--bg); color: var(--text); font: inherit; font-size: 16px; min-width: 0; }
.integration.form button { justify-self: start; }
.integration .actions { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 10px; }
.integration code { background: var(--code); border-radius: 4px; padding: 1px 4px; font: 13px/1.4 ui-monospace, Consolas, monospace; }

@media (max-width: 800px) {
  .app { position: relative; }
  .drawer { position: absolute; z-index: 10; top: 0; bottom: 0; left: 0; transform: translateX(-100%); transition: transform .2s; }
  .drawer.open { transform: none; }
}
@media (min-width: 801px) {
  #menu { visibility: hidden; }
  .scrim { display: none !important; }
}
