:root {
  --bg: #f6f3ee;
  --panel: #fffdf9;
  --side: #efe9e0;
  --text: #2d2a26;
  --muted: #847b70;
  --line: #e3dbd0;
  --mine: #dcebe3;
  --theirs: #ffffff;
  --therapist: #ece6f5;
  --therapist-line: #c9bce0;
  --accent: #4f7a68;
  --accent-text: #ffffff;
  --error: #b5483b;
  --radius: 14px;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #1d1c1a;
    --panel: #252320;
    --side: #1a1917;
    --text: #ebe6de;
    --muted: #a0978b;
    --line: #3a3631;
    --mine: #2f4a3e;
    --theirs: #2e2b27;
    --therapist: #353046;
    --therapist-line: #514770;
    --accent: #7fb39c;
    --accent-text: #10201a;
    --error: #e07a6c;
  }
}
* { box-sizing: border-box; }
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;
}
[hidden] { display: none !important; }
button { font: inherit; cursor: pointer; border: 0; border-radius: 10px; padding: .5em .9em; background: transparent; color: inherit; }
button.primary { background: var(--accent); color: var(--accent-text); }
button.soft { background: var(--line); }
button.icon { font-size: 1.2em; padding: .3em .6em; }
button.link { padding: 0; color: var(--muted); text-decoration: underline; }
input, select, textarea {
  font: inherit; color: inherit; background: var(--panel);
  border: 1px solid var(--line); border-radius: 10px; padding: .55em .7em; width: 100%;
}
textarea { resize: none; }
label { display: block; margin: .8em 0; }
.muted { color: var(--muted); font-size: .9em; }
.error { color: var(--error); }

/* login */
#login { min-height: 100%; display: grid; place-items: center; padding: 16px; }
.card { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); padding: 24px; width: min(380px, 100%); }
.card h1 { margin: 0 0 .2em; font-weight: 600; }
.card button { width: 100%; margin-top: .5em; padding: .7em; }

/* layout */
#app { display: flex; height: 100dvh; }
#sidebar {
  width: 280px; flex-shrink: 0; background: var(--side); border-right: 1px solid var(--line);
  overflow-y: auto; padding: 12px;
}
.side-head { display: flex; justify-content: space-between; align-items: center; padding: 4px 6px 12px; }
.side-section { margin-bottom: 18px; }
.side-title { display: flex; justify-content: space-between; align-items: center; font-size: .8em; text-transform: uppercase; letter-spacing: .05em; color: var(--muted); padding: 0 6px; }
.side-title button { font-size: 1.3em; padding: 0 .4em; }
.notify { width: 100%; text-align: left; background: var(--panel); border: 1px solid var(--line); font-size: .9em; }
.notify.on { border-color: var(--accent); }
.hint { font-size: .78em; color: var(--muted); padding: 0 6px; }
#sidebar ul { list-style: none; margin: 4px 0; padding: 0; }
#sidebar li {
  padding: .5em .7em; border-radius: 10px; cursor: pointer; overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
  display: flex; align-items: center; gap: .4em;
}
#sidebar li span.t { overflow: hidden; text-overflow: ellipsis; }
#sidebar li:hover { background: var(--line); }
#sidebar li.active { background: var(--panel); box-shadow: 0 1px 0 var(--line); }
#sidebar li.unread::after { content: ""; width: 8px; height: 8px; border-radius: 50%; background: var(--accent); margin-left: auto; flex-shrink: 0; }
#backdrop { display: none; }

#main { flex: 1; display: flex; flex-direction: column; min-width: 0; }
#chat-head {
  display: flex; align-items: center; gap: 8px; padding: 10px 12px;
  border-bottom: 1px solid var(--line); background: var(--panel);
}
#chat-head .titles { flex: 1; min-width: 0; }
#chat-head h2 { margin: 0; font-size: 1.05em; font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
#chat-sub { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
#menu { display: none; }
.toggle { display: flex; align-items: center; gap: .3em; margin: 0; font-size: .85em; color: var(--muted); white-space: nowrap; }
.toggle input { width: auto; }

#messages { flex: 1; overflow-y: auto; padding: 16px 12px; display: flex; flex-direction: column; gap: 10px; }
.msg { max-width: min(680px, 88%); display: flex; flex-direction: column; }
.msg .who { font-size: .78em; color: var(--muted); margin: 0 6px 2px; }
.msg .bubble { padding: .6em .9em; border-radius: var(--radius); background: var(--theirs); border: 1px solid var(--line); white-space: pre-wrap; overflow-wrap: anywhere; }
.msg.mine { align-self: flex-end; }
.msg.mine .who { text-align: right; }
.msg.mine .bubble { background: var(--mine); border-color: transparent; }
.msg.therapist { align-self: flex-start; }
.msg.therapist .bubble { background: var(--therapist); border-color: var(--therapist-line); }
.msg .tag { font-size: .75em; color: var(--muted); margin: 2px 6px 0; }
.msg .actions { display: flex; gap: 10px; margin: 2px 6px 0; font-size: .8em; }
.msg.mine .actions, .msg.mine .tag { justify-content: flex-end; text-align: right; }
.msg .actions button { padding: 0; color: var(--muted); }
.msg .actions button:hover { color: var(--accent); }
.msg.relay { align-self: center; max-width: 90%; text-align: center; }
.msg.relay .bubble { background: transparent; border: 1px dashed var(--line); font-size: .88em; color: var(--muted); }
.msg.pending .bubble::after { content: "▍"; animation: blink 1s steps(2) infinite; }
@keyframes blink { 50% { opacity: 0; } }
.empty { margin: auto; text-align: center; color: var(--muted); max-width: 420px; }
.history-loader { text-align: center; color: var(--muted); font-size: .85em; padding: 4px 0; }

#status { min-height: 1.6em; padding: 0 16px; font-size: .85em; }
#composer { display: flex; gap: 8px; padding: 8px 12px calc(10px + env(safe-area-inset-bottom)); background: var(--panel); border-top: 1px solid var(--line); }
#composer textarea { max-height: 40vh; }
#composer button { font-size: 1.1em; padding: 0 1em; }

dialog { border: 1px solid var(--line); border-radius: var(--radius); background: var(--panel); color: var(--text); width: min(560px, calc(100% - 32px)); padding: 20px; }
dialog::backdrop { background: rgba(0,0,0,.35); }
dialog h3 { margin-top: 0; }
.modes { border: 0; padding: 0; margin: 0; }
.mode { display: flex; gap: .6em; align-items: flex-start; border: 1px solid var(--line); border-radius: 10px; padding: .6em .8em; margin: .5em 0; cursor: pointer; }
.mode input { width: auto; margin-top: .35em; }
.mode:has(input:checked) { border-color: var(--accent); }
.actions { display: flex; justify-content: flex-end; gap: 8px; }

#toast { position: fixed; left: 50%; bottom: 90px; transform: translateX(-50%); background: var(--text); color: var(--bg); padding: .6em 1em; border-radius: 10px; max-width: calc(100% - 32px); z-index: 10; }

@media (max-width: 760px) {
  #menu { display: block; }
  #sidebar { position: fixed; inset: 0 auto 0 0; z-index: 5; transform: translateX(-100%); transition: transform .2s; width: 85%; max-width: 320px; }
  body.side-open #sidebar { transform: none; }
  body.side-open #backdrop { display: block; position: fixed; inset: 0; background: rgba(0,0,0,.3); z-index: 4; }
  .msg { max-width: 92%; }
}
