:root {
  --bg: #f6f6f3; --panel: #ffffff; --text: #1c1d22; --muted: #6b6e7a;
  --line: #e2e2dc; --accent: #262c5c; --accent-text: #ffffff; --user: #e8eaf6; --error: #b3261e;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #15161b; --panel: #1e2027; --text: #e9e9ee; --muted: #9a9dab;
    --line: #2e313b; --accent: #8f9bff; --accent-text: #12131a; --user: #2a2e4a; --error: #ffb4ab;
  }
}
* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { height: 100%; margin: 0; }
body {
  background: var(--bg); color: var(--text);
  font: 16px/1.6 -apple-system, BlinkMacSystemFont, "Apple SD Gothic Neo", "Malgun Gothic", sans-serif;
  -webkit-text-size-adjust: 100%;
}
button, input, textarea { font: inherit; color: inherit; }
button { cursor: pointer; border: 1px solid var(--line); background: var(--panel); border-radius: 10px; padding: 8px 14px; }
button.primary, .composer button, .login button { background: var(--accent); color: var(--accent-text); border-color: transparent; font-weight: 600; }
button.link { border: 0; background: none; color: var(--muted); }
button.icon { border: 0; background: none; font-size: 20px; padding: 4px 10px; }
button:disabled { opacity: .5; cursor: default; }
.muted { color: var(--muted); }
.error { color: var(--error); min-height: 1.6em; margin: 0; }

.login { min-height: 100%; display: grid; place-items: center; padding: 16px; }
.login form { width: min(340px, 100%); display: grid; gap: 10px; }
.login img { border-radius: 14px; }
.login h1 { margin: 0; font-size: 22px; }
.login input { padding: 10px 12px; border: 1px solid var(--line); border-radius: 10px; background: var(--panel); }

.app { height: 100dvh; display: flex; }
.sidebar {
  width: 260px; flex: none; background: var(--panel); border-right: 1px solid var(--line);
  display: flex; flex-direction: column; gap: 10px;
  padding: calc(12px + env(safe-area-inset-top)) 12px calc(12px + env(safe-area-inset-bottom));
}
.sidebar ul { list-style: none; margin: 0; padding: 0; flex: 1; overflow-y: auto; }
.sidebar li { display: flex; align-items: center; border-radius: 8px; }
.sidebar li.active { background: var(--user); }
.sidebar li button { border: 0; background: none; text-align: left; padding: 8px; }
.sidebar li .open { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.sidebar li .del { color: var(--muted); }

.main { flex: 1; min-width: 0; display: flex; flex-direction: column; }
header {
  display: flex; align-items: center; gap: 4px; border-bottom: 1px solid var(--line);
  padding: calc(8px + env(safe-area-inset-top)) 12px 8px;
}
header h1 { font-size: 16px; margin: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
#menu { display: none; }
.view { flex: 1; min-height: 0; display: flex; flex-direction: column; }
.soon { align-items: center; justify-content: center; color: var(--muted); padding: 24px; text-align: center; }

.messages { flex: 1; overflow-y: auto; padding: 16px; display: flex; flex-direction: column; gap: 12px; }
.msg { max-width: min(720px, 92%); padding: 10px 14px; border-radius: 14px; overflow-wrap: anywhere; }
.msg.user { align-self: flex-end; background: var(--user); white-space: pre-wrap; }
.msg.assistant { align-self: flex-start; background: var(--panel); border: 1px solid var(--line); }
.msg.error { align-self: center; color: var(--error); }
.msg p, .msg ul, .msg ol, .msg pre, .msg h3 { margin: 0 0 8px; }
.msg :last-child { margin-bottom: 0; }
.msg h3 { font-size: 1em; }
.msg ul, .msg ol { padding-left: 1.3em; }
.msg pre { background: var(--bg); padding: 10px; border-radius: 8px; overflow-x: auto; font-size: 14px; }
.msg code { font-family: ui-monospace, Menlo, monospace; font-size: .92em; }
.msg a { color: var(--accent); }
.empty { margin: auto; color: var(--muted); text-align: center; }

.composer { display: flex; gap: 8px; padding: 10px 12px; border-top: 1px solid var(--line); align-items: flex-end; }
.composer textarea {
  flex: 1; resize: none; max-height: 160px; padding: 10px 12px;
  border: 1px solid var(--line); border-radius: 12px; background: var(--panel);
}
.tabs { display: flex; border-top: 1px solid var(--line); background: var(--panel); padding-bottom: env(safe-area-inset-bottom); }
.tabs button { flex: 1; border: 0; border-radius: 0; background: none; padding: 10px 0; color: var(--muted); font-size: 14px; }
.tabs button.active { color: var(--accent); font-weight: 700; }
.scrim { position: fixed; inset: 0; background: rgba(0, 0, 0, .4); z-index: 9; }

@media (max-width: 760px) {
  #menu { display: block; }
  .sidebar { position: fixed; inset: 0 auto 0 0; z-index: 10; transform: translateX(-100%); transition: transform .2s; width: min(300px, 84vw); }
  .sidebar.open { transform: none; }
}
