:root {
  --bg: #f5f6fa;
  --sidebar: #eceef5;
  --surface: #ffffff;
  --text: #191b24;
  --muted: #5e6275;
  --faint: #8a8ea0;
  --border: #e0e3ec;
  --border-strong: #c9cdda;
  --accent: #5b4ee6;
  --accent-2: #2f80ed;
  --accent-text: #ffffff;
  --accent-soft: #ebe9fd;
  --user-bubble: #e7e8f3;
  --code-bg: #eef0f6;
  --warn-bg: #fbf1dc;
  --warn-text: #7a5612;
  --error-bg: #fbe5e5;
  --error-text: #9b2226;
  --ok-bg: #e3f5ea;
  --ok-text: #1d6b3d;
  --info-bg: #e9eefb;
  --info-text: #34446b;
  --shadow: 0 1px 2px rgb(20 22 40 / 0.04), 0 6px 20px rgb(20 22 40 / 0.07);
  --radius: 14px;
  --hover: rgb(110 116 150 / 0.14);
  --brand-1: #5b4ee6;
  --brand-2: #2f80ed;
  --font: ui-sans-serif, system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  --mono: ui-monospace, SFMono-Regular, Menlo, Consolas, "Liberation Mono", monospace;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #121318;
    --sidebar: #0d0e12;
    --surface: #1c1e26;
    --text: #e9eaf1;
    --muted: #a3a7b9;
    --faint: #757a8e;
    --border: #2b2e3a;
    --border-strong: #3d4152;
    --accent: #8f86ff;
    --accent-2: #5ea3ff;
    --accent-text: #0f1015;
    --accent-soft: #272449;
    --user-bubble: #262937;
    --code-bg: #0c0d11;
    --warn-bg: #3a2f17;
    --warn-text: #f0cf8a;
    --error-bg: #3d1b1e;
    --error-text: #f4a9ad;
    --ok-bg: #16301f;
    --ok-text: #9fe0b5;
    --info-bg: #1e2540;
    --info-text: #b7c4ec;
    --shadow: 0 1px 2px rgb(0 0 0 / 0.3), 0 6px 20px rgb(0 0 0 / 0.35);
    color-scheme: dark;
  }
}

*, *::before, *::after { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 16px/1.6 var(--font);
  -webkit-font-smoothing: antialiased;
  -webkit-text-size-adjust: 100%;
}
button, input, select, textarea { font: inherit; color: inherit; }
button { cursor: pointer; }
[hidden] { display: none !important; }
code, pre { font-family: var(--mono); }
.sr-only {
  position: absolute; width: 1px; height: 1px; padding: 0; margin: -1px;
  overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; border: 0;
}
.muted { color: var(--muted); }
svg { width: 20px; height: 20px; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; flex: none; }
.ico { display: inline-flex; }
.ico svg { width: 16px; height: 16px; }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

/* ---------------------------------------------------------------- gate */

.gate { display: grid; place-items: center; min-height: 100%; padding: 24px 16px; }
.gate-card {
  width: 100%; max-width: 400px; padding: 32px 28px;
  background: var(--surface); border: 1px solid var(--border);
  border-radius: 20px; box-shadow: var(--shadow); text-align: center;
}
.gate-logo { display: block; margin: 0 auto 12px; }
.gate-card h1 { font-size: 1.5rem; margin: 0 0 6px; letter-spacing: -0.01em; }
.gate-card p { margin: 0 0 18px; }
.gate-form { display: grid; gap: 10px; text-align: left; }
.gate-form input:not([type="checkbox"]), .card input:not([type="checkbox"]) {
  width: 100%; padding: 11px 14px; border-radius: 12px;
  border: 1px solid var(--border-strong); background: var(--bg); font-size: 16px;
}
.gate-form input:focus, .card input:focus { outline: none; border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft); }
.check { display: flex; align-items: center; gap: 8px; font-size: 0.9rem; color: var(--muted); cursor: pointer; padding: 2px 2px 4px; }
.check input { width: 18px; height: 18px; accent-color: var(--accent); margin: 0; }
.gate-error { color: var(--error-text); background: var(--error-bg); padding: 8px 12px; border-radius: 10px; margin: 4px 0 0 !important; font-size: 0.92rem; text-align: center; }
.gate-list { text-align: left; margin: 0 0 18px; padding-left: 20px; }
.gate-list li { margin-bottom: 6px; }
.gate code, .card code { background: var(--code-bg); padding: 1px 6px; border-radius: 6px; font-size: 0.88em; }
.btn-primary {
  padding: 11px 16px; border-radius: 12px; border: 0;
  background: linear-gradient(135deg, var(--brand-1), var(--brand-2)); color: #fff; font-weight: 600;
}
.btn-primary:hover { filter: brightness(1.07); }
.btn-secondary, .btn-danger {
  padding: 9px 14px; border-radius: 10px; border: 1px solid var(--border-strong);
  background: var(--surface); font-weight: 600; font-size: 0.9rem; white-space: nowrap;
}
.btn-secondary:hover { border-color: var(--accent); color: var(--accent); }
.btn-danger { color: var(--error-text); }
.btn-danger:hover { background: var(--error-bg); border-color: var(--error-text); }

/* ------------------------------------------------- admin & account pages */

.page { min-height: 100%; }
.page-wrap { max-width: 760px; margin: 0 auto; padding: 24px 16px 48px; }
.page-head { margin-bottom: 18px; }
.page-head h1 { font-size: 1.6rem; margin: 6px 0 0; letter-spacing: -0.015em; }
.back { display: inline-flex; align-items: center; gap: 4px; color: var(--muted); text-decoration: none; font-size: 0.92rem; }
.back:hover { color: var(--accent); }
.back svg { width: 18px; height: 18px; }
.card { background: var(--surface); border: 1px solid var(--border); border-radius: 16px; padding: 20px; margin-bottom: 16px; box-shadow: var(--shadow); }
.card h2 { font-size: 1.1rem; margin: 0 0 4px; }
.card > p { margin: 0 0 14px; }
.small { font-size: 0.85rem; }
.form-grid { display: grid; grid-template-columns: 1fr 1fr auto; gap: 10px; align-items: end; }
.form-grid label { display: grid; gap: 4px; font-size: 0.85rem; color: var(--muted); font-weight: 600; }
.card .form-grid:has(input[name="old_password"]) { grid-template-columns: 1fr; max-width: 360px; }
.flash { padding: 10px 14px; border-radius: 12px; font-size: 0.92rem; margin: 0 0 16px; }
.flash.ok { background: var(--ok-bg); color: var(--ok-text); }
.flash.err { background: var(--error-bg); color: var(--error-text); }
.user-list { list-style: none; margin: 0; padding: 0; }
.user-row { display: flex; align-items: center; justify-content: space-between; gap: 12px; flex-wrap: wrap; padding: 14px 0; border-top: 1px solid var(--border); }
.user-row:first-child { border-top: 0; padding-top: 4px; }
.user-info { display: grid; gap: 2px; min-width: 0; justify-items: start; }
.user-actions { display: flex; gap: 8px; flex-wrap: wrap; }
.inline-form { display: flex; gap: 6px; }
.card .inline-form input { width: 160px; padding: 8px 12px; font-size: 0.9rem; }
.badge { display: inline-block; margin-left: 6px; padding: 1px 8px; border-radius: 999px; background: var(--accent-soft); color: var(--accent); font-size: 0.75rem; font-weight: 700; vertical-align: 2px; }
@media (max-width: 640px) {
  .form-grid { grid-template-columns: 1fr; }
  .user-actions, .inline-form { width: 100%; }
  .card .inline-form input { flex: 1; width: auto; }
}

/* ---------------------------------------------------------------- shell */

.app { overflow: hidden; }
.shell { display: flex; height: 100dvh; height: 100vh; }
@supports (height: 100dvh) { .shell { height: 100dvh; } }

.sidebar {
  width: 272px; flex: none; display: flex; flex-direction: column;
  background: var(--sidebar); border-right: 1px solid var(--border);
}
.brand { display: flex; align-items: center; gap: 10px; padding: 16px 16px 4px; font-weight: 800; font-size: 1.08rem; letter-spacing: 0.01em; }
.brand img { border-radius: 8px; }
.sidebar-top { padding: 10px 12px 8px; }
.btn-new {
  width: 100%; display: flex; align-items: center; gap: 8px;
  padding: 10px 12px; border-radius: 12px; border: 1px solid var(--border-strong);
  background: var(--surface); font-weight: 600; font-size: 0.95rem;
}
.btn-new:hover { border-color: var(--accent); }
.btn-new svg { width: 18px; height: 18px; color: var(--accent); }
.conv-list { flex: 1; overflow-y: auto; padding: 4px 8px 12px; }
.conv-group { font-size: 0.75rem; font-weight: 600; color: var(--faint); text-transform: uppercase; letter-spacing: 0.04em; padding: 14px 8px 4px; }
.conv-empty { color: var(--faint); font-size: 0.9rem; padding: 12px 8px; margin: 0; }
.conv-item { display: flex; align-items: center; border-radius: 10px; }
.conv-item:hover, .conv-item.active { background: var(--hover); }
.conv-open {
  flex: 1; min-width: 0; text-align: left; padding: 8px 10px; border: 0; background: none;
  font-size: 0.92rem; white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.conv-del { border: 0; background: none; padding: 6px 8px; color: var(--faint); opacity: 0; display: inline-flex; }
.conv-item:hover .conv-del, .conv-item.active .conv-del, .conv-del:focus-visible { opacity: 1; }
.conv-del:hover { color: var(--error-text); }
.sidebar-bottom { padding: 10px 12px 14px; border-top: 1px solid var(--border); display: grid; gap: 2px; }
.who { display: flex; align-items: center; gap: 10px; padding: 4px 10px 8px; min-width: 0; }
.avatar {
  width: 30px; height: 30px; border-radius: 50%; flex: none; display: grid; place-items: center;
  background: linear-gradient(135deg, var(--brand-1), var(--brand-2)); color: #fff; font-weight: 700; font-size: 0.85rem;
}
.who-name { font-weight: 600; font-size: 0.92rem; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.btn-ghost { display: block; width: 100%; padding: 8px 10px; border: 0; background: none; color: var(--muted); font-size: 0.88rem; border-radius: 10px; text-align: left; text-decoration: none; }
.btn-ghost:hover { background: var(--hover); color: var(--text); }
.scrim { display: none; }

.main { flex: 1; min-width: 0; display: flex; flex-direction: column; }
.topbar { display: flex; align-items: center; gap: 8px; padding: 10px 16px; border-bottom: 1px solid transparent; }
.spacer { flex: 1; }
.icon-btn {
  display: inline-grid; place-items: center; width: 38px; height: 38px; flex: none;
  border: 0; border-radius: 10px; background: none; color: var(--muted);
}
.icon-btn:hover { background: var(--hover); color: var(--text); }
.menu-btn { display: none; }
.model-select {
  min-width: 0; max-width: 100%; padding: 7px 30px 7px 12px; border-radius: 10px;
  border: 1px solid var(--border); background: var(--surface); font-weight: 600; font-size: 0.92rem;
  appearance: none; -webkit-appearance: none;
  background-image: linear-gradient(45deg, transparent 50%, var(--muted) 50%), linear-gradient(135deg, var(--muted) 50%, transparent 50%);
  background-position: calc(100% - 15px) 50%, calc(100% - 10px) 50%;
  background-size: 5px 5px; background-repeat: no-repeat;
  text-overflow: ellipsis;
}

/* ---------------------------------------------------------------- thread */

.messages { flex: 1; overflow-y: auto; overscroll-behavior: contain; scroll-behavior: auto; }
.thread { max-width: 780px; margin: 0 auto; padding: 16px 16px 32px; display: flex; flex-direction: column; gap: 22px; }

.empty { max-width: 640px; margin: 0 auto; padding: 12vh 16px 24px; text-align: center; }
.empty-logo { display: block; margin: 0 auto 14px; }
.empty h1 { font-size: clamp(1.4rem, 4vw, 1.9rem); font-weight: 650; letter-spacing: -0.015em; margin: 0 0 6px; }
.empty-sub { color: var(--muted); margin: 0 0 24px; }
.empty-logo { border-radius: 14px; box-shadow: var(--shadow); }
.suggestions { display: grid; grid-template-columns: repeat(auto-fit, minmax(230px, 1fr)); gap: 10px; }
.suggestion {
  text-align: left; padding: 12px 14px; border-radius: 12px; border: 1px solid var(--border);
  background: var(--surface); color: var(--muted); font-size: 0.92rem; line-height: 1.4;
}
.suggestion:hover { border-color: var(--border-strong); color: var(--text); }

.msg.user { align-self: flex-end; max-width: min(85%, 640px); display: flex; flex-direction: column; align-items: flex-end; gap: 6px; }
.bubble { background: var(--user-bubble); padding: 10px 16px; border-radius: 18px 18px 4px 18px; white-space: pre-wrap; overflow-wrap: anywhere; }
.msg-files { display: flex; flex-wrap: wrap; gap: 6px; justify-content: flex-end; }
.msg-thumb { max-width: 220px; max-height: 220px; border-radius: 12px; border: 1px solid var(--border); object-fit: cover; }
.file-chip {
  display: inline-flex; align-items: center; gap: 6px; max-width: 240px;
  padding: 6px 10px; border-radius: 10px; border: 1px solid var(--border); background: var(--surface); font-size: 0.85rem;
}
.file-chip.gone { opacity: 0.6; text-decoration: line-through; }
.file-name { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; min-width: 0; }

.msg.assistant { min-width: 0; }
.thinking { margin-bottom: 10px; border-left: 2px solid var(--border-strong); padding-left: 12px; }
.thinking summary { cursor: pointer; color: var(--muted); font-size: 0.88rem; list-style: none; user-select: none; }
.thinking summary::-webkit-details-marker { display: none; }
.thinking summary::before { content: "▸ "; }
.thinking[open] summary::before { content: "▾ "; }
.thinking-body { white-space: pre-wrap; color: var(--muted); font-size: 0.88rem; margin-top: 6px; max-height: 320px; overflow-y: auto; }

.activity { display: grid; gap: 8px; margin-bottom: 12px; }
.act-search { display: flex; align-items: center; gap: 6px; color: var(--muted); font-size: 0.88rem; }
.act-sources { display: flex; gap: 6px; overflow-x: auto; padding-bottom: 2px; scrollbar-width: thin; }
.source {
  flex: none; display: grid; width: 180px; padding: 7px 10px; border-radius: 10px;
  border: 1px solid var(--border); background: var(--surface); text-decoration: none; color: inherit; font-size: 0.8rem; line-height: 1.3;
}
.source:hover { border-color: var(--border-strong); }
.source-host { color: var(--faint); font-size: 0.74rem; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.source-title { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

.typing { display: inline-flex; gap: 5px; padding: 8px 0; }
.typing i { width: 7px; height: 7px; border-radius: 50%; background: var(--accent); opacity: 0.35; animation: blink 1.2s infinite; }
.typing i:nth-child(2) { animation-delay: 0.2s; }
.typing i:nth-child(3) { animation-delay: 0.4s; }
@keyframes blink { 0%, 80%, 100% { opacity: 0.25; } 40% { opacity: 1; } }
@media (prefers-reduced-motion: reduce) { .typing i { animation: none; opacity: 0.7; } }

.notes { display: grid; gap: 8px; }
.notes:empty { display: none; }
.notes:not(:empty) { margin-top: 10px; }
.note { padding: 8px 12px; border-radius: 10px; font-size: 0.9rem; display: flex; align-items: center; gap: 10px; flex-wrap: wrap; }
.note.info { background: var(--info-bg); color: var(--info-text); }
.note.warn { background: var(--warn-bg); color: var(--warn-text); }
.note.error { background: var(--error-bg); color: var(--error-text); }
.note-btn { border: 1px solid currentColor; background: none; border-radius: 8px; padding: 3px 10px; font-size: 0.85rem; font-weight: 600; }

.actions { display: flex; align-items: center; gap: 4px; margin-top: 8px; flex-wrap: wrap; }
.act-btn {
  display: inline-flex; align-items: center; gap: 5px; border: 0; background: none;
  color: var(--faint); padding: 4px 8px; border-radius: 8px; font-size: 0.82rem;
}
.act-btn:hover { background: var(--hover); color: var(--text); }
.act-btn.done, .code-copy.done { color: var(--accent); }
.meta { color: var(--faint); font-size: 0.78rem; margin-left: 6px; }

/* markdown */
.md { overflow-wrap: anywhere; }
.md > :first-child { margin-top: 0; }
.md > :last-child { margin-bottom: 0; }
.md p, .md ul, .md ol, .md blockquote, .md table, .md .code-wrap { margin: 0 0 0.85em; }
.md h1, .md h2, .md h3, .md h4 { line-height: 1.3; margin: 1.3em 0 0.5em; letter-spacing: -0.01em; }
.md h1 { font-size: 1.4rem; } .md h2 { font-size: 1.22rem; } .md h3 { font-size: 1.08rem; } .md h4 { font-size: 1rem; }
.md ul, .md ol { padding-left: 1.4em; }
.md li { margin: 0.2em 0; }
.md a { color: var(--accent); }
.md blockquote { border-left: 3px solid var(--border-strong); padding-left: 12px; color: var(--muted); }
.md hr { border: 0; border-top: 1px solid var(--border); margin: 1.4em 0; }
.md img { max-width: 100%; }
.md :not(pre) > code { background: var(--code-bg); padding: 0.12em 0.4em; border-radius: 6px; font-size: 0.88em; }
.md pre { margin: 0; padding: 12px 14px; overflow-x: auto; font-size: 0.86rem; line-height: 1.5; background: var(--code-bg); }
.md > pre { border-radius: 12px; margin-bottom: 0.85em; }
.code-wrap { border: 1px solid var(--border); border-radius: 12px; overflow: hidden; }
.code-head { display: flex; align-items: center; justify-content: space-between; padding: 4px 6px 4px 12px; font-size: 0.78rem; color: var(--faint); border-bottom: 1px solid var(--border); background: var(--surface); }
.code-copy { display: inline-flex; align-items: center; gap: 5px; border: 0; background: none; color: var(--faint); padding: 3px 8px; border-radius: 6px; font-size: 0.78rem; }
.code-copy:hover { color: var(--text); background: var(--hover); }
.md table { border-collapse: collapse; display: block; overflow-x: auto; max-width: 100%; font-size: 0.92rem; }
.md th, .md td { border: 1px solid var(--border); padding: 6px 10px; text-align: left; vertical-align: top; }
.md th { background: var(--code-bg); }

/* ------------------------------------------------------------- composer */

.composer-wrap { padding: 0 16px calc(10px + env(safe-area-inset-bottom)); }
.composer {
  max-width: 780px; margin: 0 auto; background: var(--surface); border: 1px solid var(--border-strong);
  border-radius: 20px; box-shadow: var(--shadow); padding: 10px 10px 8px 14px;
}
.composer:focus-within { border-color: var(--accent); }
.composer textarea {
  display: block; width: 100%; resize: none; border: 0; outline: 0; background: none;
  padding: 4px 2px; max-height: 40vh; min-height: 28px; line-height: 1.5; font-size: 16px;
}
.composer textarea::placeholder { color: var(--faint); }
.composer-bar { display: flex; align-items: center; gap: 6px; margin-top: 6px; flex-wrap: wrap; }
.chip-select, .chip-toggle {
  display: inline-flex; align-items: center; gap: 6px; height: 32px; padding: 0 10px;
  border-radius: 999px; border: 1px solid var(--border); background: none; color: var(--muted); font-size: 0.84rem;
}
.chip-select { appearance: none; -webkit-appearance: none; padding-right: 10px; }
.chip-toggle { cursor: pointer; user-select: none; }
.chip-toggle input { position: absolute; opacity: 0; pointer-events: none; }
.chip-toggle svg { width: 15px; height: 15px; }
.chip-toggle.on { background: var(--accent-soft); border-color: var(--accent); color: var(--accent); }
.chip-toggle:focus-within { outline: 2px solid var(--accent); outline-offset: 2px; }
.send-btn {
  width: 36px; height: 36px; border-radius: 12px; border: 0; display: inline-grid; place-items: center;
  background: linear-gradient(135deg, var(--brand-1), var(--brand-2)); color: #fff;
}
.send-btn:hover { filter: brightness(1.06); }
.send-btn svg { stroke-width: 2.2; }
.send-btn.stop { background: var(--text); color: var(--bg); }
.send-btn.stop svg { fill: currentColor; stroke: none; }
.fineprint { text-align: center; color: var(--faint); font-size: 0.74rem; margin: 6px auto 0; max-width: 780px; line-height: 1.4; }

.attachments { display: flex; gap: 8px; overflow-x: auto; padding-bottom: 8px; }
.attach {
  position: relative; flex: none; display: flex; align-items: center; gap: 8px; max-width: 220px;
  padding: 6px 30px 6px 6px; border-radius: 12px; border: 1px solid var(--border); background: var(--bg); font-size: 0.84rem;
}
.attach img { width: 40px; height: 40px; border-radius: 8px; object-fit: cover; }
.attach > .ico { width: 40px; height: 40px; display: grid; place-items: center; border-radius: 8px; background: var(--accent-soft); color: var(--accent); }
.attach.loading { opacity: 0.6; }
.attach-x { position: absolute; top: 4px; right: 4px; border: 0; background: none; color: var(--faint); padding: 3px; border-radius: 6px; display: inline-flex; }
.attach-x:hover { color: var(--text); }

.toast {
  position: fixed; left: 50%; bottom: calc(110px + env(safe-area-inset-bottom)); transform: translateX(-50%);
  background: var(--text); color: var(--bg); padding: 10px 16px; border-radius: 12px; font-size: 0.9rem;
  box-shadow: var(--shadow); z-index: 50; max-width: calc(100% - 32px); text-align: center;
}
.drop-hint {
  position: fixed; inset: 12px; z-index: 60; display: grid; place-items: center; pointer-events: none;
  border: 2px dashed var(--accent); border-radius: 20px; background: color-mix(in srgb, var(--accent) 8%, transparent);
  color: var(--accent); font-weight: 600; font-size: 1.1rem;
}

/* --------------------------------------------------------------- mobile */

@media (max-width: 899px) {
  .menu-btn { display: inline-grid; }
  .sidebar {
    position: fixed; inset: 0 auto 0 0; z-index: 40; width: min(300px, 85vw);
    transform: translateX(-100%); transition: transform 0.2s ease; box-shadow: var(--shadow);
  }
  .sidebar-open .sidebar { transform: none; }
  .scrim { display: block; position: fixed; inset: 0; z-index: 30; background: rgb(0 0 0 / 0.35); }
  .conv-del { opacity: 1; }
  .topbar { padding: 8px 10px; }
  .composer-wrap { padding: 0 10px calc(8px + env(safe-area-inset-bottom)); }
  .thread { padding: 12px 14px 24px; }
  .empty { padding-top: 6vh; }
  .msg.user { max-width: 92%; }
}
@media (prefers-reduced-motion: reduce) { .sidebar { transition: none; } }
