:root {
  color-scheme: dark;
  --bg: #1b1420;
  --panel: #251c2b;
  --raised: #322738;
  --line: #43364a;
  --text: #f3edf2;
  --muted: #b3a6b6;
  --accent: #d9825b;
  --accent-strong: #b95f3a;
  --mine: #33283c;
  --theirs: #2a2030;
  --danger: #e0676d;
  --sans: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  --mono: ui-monospace, Menlo, "DejaVu Sans Mono", Consolas, monospace;
}
* { 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 var(--sans); }
h1, h2, h3 { margin: 0; font-weight: 600; }
h1 { font-size: 26px; }
h2 { font-size: 18px; }
h3 { font-size: 14px; margin-top: 12px; color: var(--muted); }
p { margin: 0; }
.muted { color: var(--muted); font-size: 14px; }
.grow { flex: 1; }
.row { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }

input, textarea, select, button { font: inherit; color: var(--text); }
input, textarea, select { width: 100%; background: var(--raised); border: 1px solid var(--line); border-radius: 8px; padding: 8px 10px; }
textarea { resize: vertical; display: block; }
input[type=checkbox], input[type=range] { width: auto; }
input[type=range] { width: 100%; padding: 0; accent-color: var(--accent); }
button { background: var(--raised); border: 1px solid var(--line); border-radius: 8px; padding: 8px 12px; min-height: 40px; cursor: pointer; }
button:hover:not(:disabled) { border-color: var(--muted); }
button:disabled { opacity: .5; cursor: not-allowed; }
button.primary { background: var(--accent-strong); border-color: transparent; color: #fff; font-weight: 600; }
button.ghost { background: none; border-color: transparent; color: var(--muted); }
button.danger { color: var(--danger); }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; }
label { display: block; font-size: 14px; color: var(--muted); }
label.check { display: flex; gap: 8px; align-items: flex-start; color: var(--text); }
output { float: right; color: var(--text); }

#auth { min-height: 100%; display: flex; align-items: center; justify-content: center; padding: 16px; }
.auth-card { width: 100%; max-width: 380px; background: var(--panel); border: 1px solid var(--line); border-radius: 14px; padding: 24px; text-align: center; }
.auth-card img { border-radius: 18px; }
.auth-card form { display: flex; flex-direction: column; gap: 12px; text-align: left; margin-top: 14px; }
.tabs { display: flex; gap: 4px; margin-top: 14px; border-bottom: 1px solid var(--line); }
.tabs button { flex: 1; background: none; border: 0; border-bottom: 2px solid transparent; border-radius: 0; color: var(--muted); }
.tabs button[aria-selected=true] { color: var(--text); border-bottom-color: var(--accent); }

#app { height: 100dvh; display: flex; overflow: hidden; }
#sidebar { width: 270px; flex: none; background: var(--panel); border-right: 1px solid var(--line); display: flex; flex-direction: column; gap: 10px; padding: 12px; padding-top: max(12px, env(safe-area-inset-top)); }
.brand { display: flex; align-items: center; gap: 8px; }
.brand strong { flex: 1; font-size: 18px; }
.brand img { border-radius: 8px; }
#chatList { flex: 1; overflow-y: auto; display: flex; flex-direction: column; gap: 2px; }
#chatList button { display: block; width: 100%; text-align: left; background: none; border-color: transparent; }
#chatList button[aria-current=true] { background: var(--raised); }
#chatList small { display: block; color: var(--muted); }
.chat-name { display: block; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.foot { display: flex; flex-direction: column; gap: 6px; padding-bottom: env(safe-area-inset-bottom); }

#main { flex: 1; min-width: 0; display: flex; flex-direction: column; }
#topbar { display: flex; align-items: center; gap: 8px; padding: 8px 12px; padding-top: max(8px, env(safe-area-inset-top)); border-bottom: 1px solid var(--line); }
#title { flex: 1; min-width: 60px; background: none; border-color: transparent; font-weight: 600; }
#memory { white-space: nowrap; }
#messages { flex: 1; overflow-y: auto; padding: 14px; display: flex; flex-direction: column; gap: 10px; }
.welcome { margin: auto; max-width: 420px; text-align: center; color: var(--muted); }

.message { max-width: min(760px, 92%); background: var(--theirs); border-radius: 12px; padding: 8px 12px; }
.message.user { align-self: flex-end; background: var(--mine); }
.message.forgotten { opacity: .55; }
.message.forgotten::before { content: "Outside his memory now"; display: block; font-size: 12px; color: var(--muted); }
.speaker { font-size: 13px; font-weight: 600; color: var(--accent); }
.text p { white-space: pre-wrap; overflow-wrap: anywhere; }
.text p + p { margin-top: 6px; }
.text .action { font-style: italic; color: var(--muted); }
.text em { color: var(--muted); }
.tools { display: flex; gap: 2px; margin: 2px -6px -4px; }
.tools button { background: none; border: 0; min-height: 30px; padding: 2px 6px; font-size: 13px; color: var(--muted); }
.tools button:hover:not(:disabled) { color: var(--text); }
.message textarea { margin-top: 4px; }

.irc #messages { gap: 2px; font: 14px/1.5 var(--mono); }
.irc .message, .irc .message.user { align-self: stretch; max-width: none; background: none; border-radius: 0; padding: 2px 4px; }
.irc .speaker { display: none; }
.irc .text .action { color: #c9a0dc; }
.irc .nick { color: var(--accent); }
.irc .message.user .nick { color: #7fc8a9; }
.irc .tools { margin: 0; opacity: .6; }

#composer { display: flex; flex-direction: column; gap: 6px; padding: 8px 12px; padding-bottom: max(8px, env(safe-area-inset-bottom)); border-top: 1px solid var(--line); }

#drawer { width: 340px; flex: none; overflow-y: auto; background: var(--panel); border-left: 1px solid var(--line); padding: 12px; padding-top: max(12px, env(safe-area-inset-top)); display: flex; flex-direction: column; gap: 8px; }
#drawer > * { flex: none; }

dialog { background: var(--panel); color: var(--text); border: 1px solid var(--line); border-radius: 14px; width: min(560px, calc(100vw - 24px)); max-height: 85dvh; padding: 16px; }
dialog::backdrop { background: rgba(0, 0, 0, .6); }
#account form { display: flex; flex-direction: column; gap: 10px; margin: 12px 0; }
#account > button { width: 100%; }
.library-group { margin-top: 12px; display: flex; flex-direction: column; gap: 6px; }
.library-group details { background: var(--raised); border-radius: 8px; padding: 8px 10px; }
.library-group details[open] { display: flex; flex-direction: column; gap: 6px; }
.library-group summary { cursor: pointer; }
.library-group input, .library-group textarea { background: var(--panel); margin-top: 6px; }

#toast { position: fixed; left: 50%; bottom: 96px; transform: translateX(-50%); width: max-content; max-width: min(92vw, 520px); background: var(--raised); border: 1px solid var(--accent); border-radius: 10px; padding: 8px 14px; z-index: 30; }
#scrim { position: fixed; inset: 0; background: rgba(0, 0, 0, .5); z-index: 10; }

.narrow-only { display: none; }
@media (max-width: 900px) {
  .narrow-only { display: inline-block; }
  #sidebar, #drawer { position: fixed; top: 0; bottom: 0; z-index: 20; }
  #sidebar { left: 0; width: min(300px, 86vw); transform: translateX(-102%); transition: transform .15s; }
  #sidebar.open { transform: none; }
  #drawer { right: 0; width: min(380px, 92vw); }
  #memory { display: none; }
}
