:root {
  --ink: #172033;
  --muted: #5a6577;
  --paper: #f3f5f8;
  --surface: #ffffff;
  --line: #dce1e8;
  --accent: #1f5fbf;
  --accent-ink: #ffffff;
  --accent-soft: #e6eefa;
  --warn: #9a3b0f;
  --warn-soft: #fbeee6;
  --ok: #1d6b45;
  --ok-soft: #e4f3ea;
  --radius-s: 6px;
  --radius-m: 12px;
  --font: "Onest", "Segoe UI", system-ui, -apple-system, sans-serif;
}
@media (prefers-color-scheme: dark) {
  :root {
    --ink: #e7ebf2; --muted: #9aa5b6; --paper: #121722; --surface: #1a2130;
    --line: #2c3547; --accent: #6d9ff0; --accent-ink: #0d1422; --accent-soft: #1f2c46;
    --warn: #f0a878; --warn-soft: #3a2518; --ok: #7fd3a3; --ok-soft: #173427;
  }
}
* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body { font: 16px/1.55 var(--font); color: var(--ink); background: var(--paper); }
a { color: var(--accent); }
button, input, textarea, select { font: inherit; color: inherit; }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

/* ---------- Chat layout ---------- */
.app { display: grid; grid-template-columns: 300px 1fr; height: 100%; }
.side { background: var(--surface); border-right: 1px solid var(--line); display: flex; flex-direction: column; min-height: 0; }
.side-head { padding: 22px 22px 14px; }
.company { font-weight: 700; font-size: 1.05rem; line-height: 1.3; }
.side-sub { color: var(--muted); font-size: .88rem; margin-top: 2px; }
.kb { overflow-y: auto; padding: 4px 14px 22px; flex: 1; }
.kb h3 { font-size: .8rem; font-weight: 600; color: var(--muted); margin: 18px 8px 6px; }
.kb ul { list-style: none; margin: 0; padding: 0; }
.kb li { padding: 7px 8px; border-radius: var(--radius-s); font-size: .92rem; line-height: 1.35; }
.kb .empty { color: var(--muted); font-size: .9rem; padding: 8px; }
.side-foot { border-top: 1px solid var(--line); padding: 12px 22px; font-size: .85rem; }
.side-foot a { color: var(--muted); text-decoration: none; }
.side-foot a:hover { color: var(--accent); }

.main { display: flex; flex-direction: column; min-height: 0; }
.topbar { display: flex; align-items: center; gap: 12px; padding: 14px 24px; border-bottom: 1px solid var(--line); background: var(--surface); }
.topbar h1 { font-size: 1rem; margin: 0; flex: 1; font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.btn { border: 1px solid var(--line); background: var(--surface); border-radius: var(--radius-s); padding: 7px 14px; cursor: pointer; font-size: .92rem; }
.btn:hover { border-color: var(--accent); color: var(--accent); }
.btn-primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); font-weight: 600; }
.btn-primary:hover { color: var(--accent-ink); filter: brightness(1.08); }
.btn-primary:disabled { opacity: .5; cursor: not-allowed; }
.menu-btn { display: none; }

.thread { flex: 1; overflow-y: auto; padding: 28px 24px 12px; }
.thread-inner { max-width: 760px; margin: 0 auto; }

.welcome { padding: 8vh 0 24px; }
.welcome h2 { font-size: clamp(1.6rem, 3vw, 2.1rem); line-height: 1.2; margin: 0 0 10px; font-weight: 700; letter-spacing: -.01em; }
.welcome p { color: var(--muted); margin: 0 0 26px; max-width: 56ch; }
.suggest { display: grid; grid-template-columns: repeat(auto-fill, minmax(260px, 1fr)); gap: 10px; }
.suggest button { text-align: left; border: 1px solid var(--line); background: var(--surface); border-radius: var(--radius-m); padding: 14px 16px; cursor: pointer; line-height: 1.4; }
.suggest button:hover { border-color: var(--accent); }

.msg { margin: 0 0 22px; }
.msg.user { display: flex; justify-content: flex-end; }
.msg.user .bubble { background: var(--accent); color: var(--accent-ink); border-radius: var(--radius-m) var(--radius-m) 4px var(--radius-m); padding: 10px 16px; max-width: 80%; white-space: pre-wrap; overflow-wrap: anywhere; }
.msg.bot .body { overflow-wrap: anywhere; }
.msg.bot .body p { margin: 0 0 .7em; }
.msg.bot .body ul, .msg.bot .body ol { margin: 0 0 .7em; padding-left: 1.3em; }
.msg.bot .body li { margin: .2em 0; }
.cite { display: inline-block; min-width: 1.35em; padding: 0 .3em; margin: 0 1px; font-size: .72em; font-weight: 700; line-height: 1.5; text-align: center; vertical-align: .35em; border-radius: 4px; background: var(--accent-soft); color: var(--accent); cursor: pointer; border: 0; }
.msg.error .body { background: var(--warn-soft); color: var(--warn); padding: 10px 14px; border-radius: var(--radius-s); }

.sources { margin-top: 10px; display: flex; flex-direction: column; gap: 6px; }
.source { border-left: 3px solid var(--accent); background: var(--surface); padding: 8px 12px; border-radius: 0 var(--radius-s) var(--radius-s) 0; font-size: .88rem; }
.source summary { cursor: pointer; list-style: none; display: flex; gap: 8px; align-items: baseline; }
.source summary::-webkit-details-marker { display: none; }
.source .n { font-weight: 700; color: var(--accent); }
.source .cat { color: var(--muted); font-size: .82rem; margin-left: auto; white-space: nowrap; }
.source .snip { margin: 8px 0 2px; color: var(--muted); white-space: pre-wrap; }
.source.flash { animation: flash 1.2s ease; }
@keyframes flash { from { background: var(--accent-soft); } to { background: var(--surface); } }

.typing { display: inline-flex; gap: 5px; padding: 8px 0; }
.typing span { width: 7px; height: 7px; border-radius: 50%; background: var(--muted); animation: blink 1.2s infinite; }
.typing span:nth-child(2) { animation-delay: .2s; } .typing span:nth-child(3) { animation-delay: .4s; }
@keyframes blink { 0%, 80%, 100% { opacity: .25; } 40% { opacity: 1; } }

.composer { padding: 12px 24px 20px; }
.composer form { max-width: 760px; margin: 0 auto; display: flex; gap: 10px; align-items: flex-end; background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius-m); padding: 8px 8px 8px 14px; }
.composer form:focus-within { border-color: var(--accent); }
.composer textarea { flex: 1; border: 0; background: transparent; resize: none; max-height: 200px; padding: 6px 0; outline: none; line-height: 1.5; }
.composer .hint { max-width: 760px; margin: 8px auto 0; font-size: .8rem; color: var(--muted); }

@media (max-width: 820px) {
  .app { grid-template-columns: 1fr; }
  .side { position: fixed; inset: 0 auto 0 0; width: min(86vw, 320px); z-index: 20; transform: translateX(-100%); transition: transform .2s ease; }
  .side.open { transform: none; box-shadow: 0 0 0 100vmax rgba(0,0,0,.35); }
  .menu-btn { display: inline-block; }
  .thread, .composer { padding-left: 14px; padding-right: 14px; }
}
@media (prefers-reduced-motion: reduce) { * { animation: none !important; transition: none !important; } }

/* ---------- Admin ---------- */
.admin { max-width: 1100px; margin: 0 auto; padding: 28px 24px 60px; }
.admin-top { display: flex; align-items: center; gap: 16px; margin-bottom: 26px; }
.admin-top h1 { font-size: 1.35rem; margin: 0; flex: 1; }
.admin-top nav { display: flex; gap: 16px; font-size: .92rem; }
.panel { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius-m); padding: 22px; margin-bottom: 22px; }
.panel h2 { font-size: 1.05rem; margin: 0 0 14px; }
.stats { display: flex; gap: 34px; flex-wrap: wrap; margin-bottom: 22px; }
.stat b { display: block; font-size: 1.6rem; line-height: 1.1; }
.stat span { color: var(--muted); font-size: .88rem; }
.grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: 14px; }
.field { display: flex; flex-direction: column; gap: 5px; margin-bottom: 14px; }
.field label { font-size: .88rem; font-weight: 600; }
.field small { color: var(--muted); font-size: .82rem; }
.field input[type=text], .field input[type=password], .field textarea, .field select { border: 1px solid var(--line); border-radius: var(--radius-s); padding: 9px 11px; background: var(--paper); width: 100%; }
.field textarea { min-height: 160px; resize: vertical; }
.field textarea.tall { min-height: 60vh; font-size: .92rem; }
.table-wrap { overflow-x: auto; }
table { width: 100%; border-collapse: collapse; font-size: .92rem; }
th, td { text-align: left; padding: 10px 8px; border-bottom: 1px solid var(--line); vertical-align: top; }
th { font-size: .82rem; color: var(--muted); font-weight: 600; }
td.actions { white-space: nowrap; }
td.actions form { display: inline; }
.link-btn { background: none; border: 0; padding: 0 4px; color: var(--accent); cursor: pointer; font-size: .9rem; }
.link-btn.danger { color: var(--warn); }
.tag { display: inline-block; font-size: .78rem; padding: 1px 8px; border-radius: 99px; background: var(--paper); border: 1px solid var(--line); }
.tag.off { color: var(--warn); border-color: var(--warn); }
.tag.warn { color: var(--warn); }
.notice { padding: 11px 14px; border-radius: var(--radius-s); margin-bottom: 18px; }
.notice.ok { background: var(--ok-soft); color: var(--ok); }
.notice.err { background: var(--warn-soft); color: var(--warn); }
.muted { color: var(--muted); }
.login { max-width: 380px; margin: 12vh auto; }
@media (max-width: 700px) { .grid2 { grid-template-columns: 1fr; } }
