:root {
  --bg: #0f0f0f;
  --bg-elev: #171717;
  --bg-side: #101010;
  --bg-input: #212121;
  --bg-hover: #1f1f1f;
  --line: #2a2a2a;
  --fg: #ececec;
  --fg-dim: #a0a0a0;
  --link: #60a5fa;
  --accent: #3b82f6;
  --accent-hover: #2f6fe0;
  --danger: #ef4444;
  /* блоки кода, врезки, плашки — отдельными токенами, чтобы тёмная тема не протекала в светлую */
  --code-bg: #0b0b0b;
  --code-fg: #e6e6e6;
  --code-inline: #ffffff14;
  --soft: #ffffff0d;
  --chip: #ffffff12;
  --chip-hover: #ffffff20;
  --shadow: #0007;
  --scrim: #0009;
  --warn: #f0b429;
  --radius: 14px;
  --side-w: 264px;
}
:root[data-theme="light"] {
  --bg: #ffffff; --bg-elev: #f7f7f8; --bg-side: #f9f9f9; --bg-input: #ffffff;
  --bg-hover: #ececec; --line: #e3e3e3; --fg: #0d0d0d; --fg-dim: #6b6b6b;
  --link: #2563eb;
  --code-bg: #f4f5f7; --code-fg: #14161a; --code-inline: #00000010;
  --soft: #0000000d; --chip: #00000009; --chip-hover: #00000016;
  --shadow: #0003; --scrim: #0006;
  --warn: #a16207;
}
:root[data-theme="dark"] {
  --bg: #0f0f0f; --bg-elev: #171717; --bg-side: #101010; --bg-input: #212121;
  --bg-hover: #1f1f1f; --line: #2a2a2a; --fg: #ececec; --fg-dim: #a0a0a0;
  --link: #60a5fa;
  --code-bg: #0b0b0b; --code-fg: #e6e6e6; --code-inline: #ffffff14;
  --soft: #ffffff0d; --chip: #ffffff12; --chip-hover: #ffffff20;
  --shadow: #0007; --scrim: #0009;
  --warn: #f0b429;
}
@media (prefers-color-scheme: light) {
  :root:not([data-theme="dark"]):not([data-theme="light"]) {
    --bg: #ffffff; --bg-elev: #f7f7f8; --bg-side: #f9f9f9; --bg-input: #ffffff;
    --bg-hover: #ececec; --line: #e3e3e3; --fg: #0d0d0d; --fg-dim: #6b6b6b;
    --link: #2563eb;
    --code-bg: #f4f5f7; --code-fg: #14161a; --code-inline: #00000010;
    --soft: #0000000d; --chip: #00000009; --chip-hover: #00000016;
    --shadow: #0003; --scrim: #0006;
    --warn: #a16207;
  }
}
* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0; background: var(--bg); color: var(--fg);
  font: 15px/1.55 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
}
a { color: var(--link); text-decoration: none; }
a:hover { text-decoration: underline; }
button { font: inherit; cursor: pointer; }
input, select, textarea {
  font: inherit; color: var(--fg); background: var(--bg-input);
  border: 1px solid var(--line); border-radius: 10px; padding: 10px 12px; width: 100%;
}
input:focus, select:focus, textarea:focus { outline: 2px solid #2563eb55; border-color: #3b82f6; }
button.primary { background: var(--accent); border: 1px solid var(--accent); color: #fff; border-radius: 10px; padding: 10px 16px; }
button.primary:hover { background: var(--accent-hover); }
button.ghost { background: transparent; border: 1px solid var(--line); color: var(--fg); border-radius: 10px; padding: 9px 14px; }
button.ghost:hover { background: var(--bg-hover); }
button.small { padding: 5px 10px; font-size: 13px; }
.hint { color: var(--fg-dim); font-size: 13px; }
.ok { color: #4ade80; }
.error, .err { color: #f87171; }
.spacer { flex: 1; }
.grow { flex: 1; }

/* ---------------------------------------------------------------- layout */
.app { display: flex; height: 100dvh; overflow: hidden; }
.sidebar {
  width: var(--side-w); flex: 0 0 var(--side-w); background: var(--bg-side);
  border-right: 1px solid var(--line); display: flex; flex-direction: column; min-height: 0;
}
.side-top { padding: 12px; display: flex; gap: 6px; align-items: center; }
.new-chat {
  flex: 1; text-align: left; background: transparent; color: var(--fg);
  border: 1px solid var(--line); border-radius: 10px; padding: 10px 12px;
  display: flex; align-items: center; gap: 8px;
}
.new-chat:hover { background: var(--bg-hover); }
.new-chat span { font-size: 17px; line-height: 1; }
.side-scroll { flex: 1; overflow-y: auto; padding: 0 8px 8px; min-height: 0; }
.group-label { padding: 10px 10px 4px; font-size: 11.5px; text-transform: uppercase;
  letter-spacing: .4px; color: var(--fg-dim); }
.folder { margin-bottom: 4px; }
.folder-head {
  display: flex; align-items: center; gap: 6px; padding: 7px 8px; border-radius: 8px;
  cursor: pointer; color: var(--fg-dim); font-size: 12.5px; user-select: none;
}
.folder-head:hover { background: var(--bg-hover); color: var(--fg); }
.folder-head .fname { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.folder-head .caret { width: 12px; font-size: 10px; }
.folder-head .fmenu { background: none; border: 0; color: inherit; padding: 0 4px; opacity: 0; }
.folder-head:hover .fmenu { opacity: .8; }
.folder.archive .folder-head { color: var(--warn); }
.conv-item.in-folder { padding-left: 22px; }
.popup {
  position: fixed; z-index: 60; min-width: 200px; background: var(--bg-elev);
  border: 1px solid var(--line); border-radius: 12px; padding: 6px;
  box-shadow: 0 12px 40px var(--shadow); display: flex; flex-direction: column;
}
.popup button {
  text-align: left; background: none; border: 0; color: var(--fg);
  padding: 9px 10px; border-radius: 8px; font-size: 13.5px;
}
.popup button:hover { background: var(--bg-hover); }
.banner {
  max-width: 760px; margin: 10px auto 0; padding: 8px 14px; display: flex; flex-wrap: wrap;
  gap: 8px; align-items: center; font-size: 13px; color: var(--fg-dim);
  border: 1px solid var(--line); border-radius: 12px;
}
.banner.arch { color: var(--warn); border-color: var(--warn); }
.banner.files { border-style: dashed; }
.file-link { font-size: 12.5px; background: var(--chip); padding: 4px 8px; border-radius: 8px; color: var(--fg); border: 1px solid var(--line); }
.file-link:hover { text-decoration: none; background: var(--chip-hover); }
.chips { display: flex; flex-wrap: wrap; gap: 6px; padding: 0 2px 8px; }
.chip {
  display: inline-flex; align-items: center; gap: 6px; font-size: 12.5px;
  background: var(--chip); border: 1px solid var(--line); border-radius: 20px; padding: 4px 10px;
}
.chip-x { background: none; border: 0; color: var(--fg-dim); padding: 0; font-size: 12px; }
.chip-x:hover { color: var(--danger); }
.hint-row { text-align: center; font-size: 11.5px; color: var(--fg-dim); min-height: 14px; }
.conv-list { flex: 1; overflow-y: auto; padding: 0 8px 8px; min-height: 0; }
.conv-item {
  display: flex; align-items: center; gap: 6px; padding: 9px 10px; border-radius: 9px;
  color: var(--fg); cursor: pointer; font-size: 14px;
}
.conv-item:hover { background: var(--bg-hover); }
.conv-item.active { background: #2563eb22; }
.conv-item .t { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.conv-item .del { opacity: 0; background: none; border: 0; color: var(--fg-dim); padding: 2px 4px; border-radius: 6px; }
.conv-item:hover .del { opacity: 1; }
.conv-item .del:hover { color: var(--danger); }
.side-bottom { border-top: 1px solid var(--line); padding: 10px; display: flex; align-items: center; gap: 8px; }
.side-user { display: flex; align-items: center; gap: 9px; flex: 1; min-width: 0; color: var(--fg); }
.side-user:hover { text-decoration: none; }
.avatar {
  width: 30px; height: 30px; flex: 0 0 30px; border-radius: 50%; background: #3b82f6;
  color: #fff; display: grid; place-items: center; font-size: 14px; font-weight: 600;
}
.who { display: flex; flex-direction: column; min-width: 0; }
.who b { font-weight: 500; font-size: 13.5px; overflow: hidden; text-overflow: ellipsis; }
.who small { color: var(--fg-dim); font-size: 11.5px; }
.icon-btn {
  background: transparent; border: 0; color: var(--fg-dim); font-size: 17px; line-height: 1;
  padding: 8px; border-radius: 9px;
}
.icon-btn:hover { background: var(--bg-hover); color: var(--fg); text-decoration: none; }
.main { flex: 1; display: flex; flex-direction: column; min-width: 0; min-height: 0; }
.topbar {
  display: flex; align-items: center; gap: 8px; padding: 10px 14px;
  border-bottom: 1px solid var(--line); background: var(--bg);
}
.model-select {
  width: auto; max-width: 60vw; padding: 7px 10px; font-size: 14px;
  background: transparent; border-color: transparent; font-weight: 500;
}
.model-select:hover { background: var(--bg-hover); }
.model-select.effort { font-size: 13px; color: var(--fg-dim); max-width: 34vw; }
.model-select.effort:disabled { opacity: .45; }
.meta { margin-top: 8px; font-size: 11.5px; color: var(--fg-dim); opacity: .85; }
.note { margin: 0 auto 8px; max-width: 760px; padding: 0 20px; font-size: 12px; color: var(--warn); }
.only-mobile { display: none; }

/* ---------------------------------------------------------------- thread */
.thread { flex: 1; overflow-y: auto; padding: 26px 0 10px; min-height: 0; }
.empty { max-width: 760px; margin: 18vh auto 0; text-align: center; padding: 0 20px; }
.empty h2 { font-size: 28px; font-weight: 500; margin: 0 0 8px; }
.empty p { color: var(--fg-dim); margin: 0; }
.msg { max-width: 760px; margin: 0 auto; padding: 14px 20px; display: flex; gap: 14px; }
.msg + .msg { border-top: 1px solid var(--soft); }
.msg .role {
  width: 28px; height: 28px; flex: 0 0 28px; border-radius: 50%; display: grid; place-items: center;
  font-size: 12px; font-weight: 600; margin-top: 2px;
}
.msg.user .role { background: #3b82f6; color: #fff; }
.msg.assistant .role { background: #10a37f; color: #fff; }
.msg .body { flex: 1; min-width: 0; overflow-wrap: anywhere; }
.msg .body > :first-child { margin-top: 0; }
.msg .body > :last-child { margin-bottom: 0; }
.msg.user .body { white-space: pre-wrap; }
.msg .body p { margin: 0 0 12px; }
.msg .body h1, .msg .body h2, .msg .body h3 { margin: 20px 0 10px; line-height: 1.3; }
.msg .body h1 { font-size: 22px; }
.msg .body h2 { font-size: 19px; }
.msg .body h3 { font-size: 17px; }
.msg .body ul, .msg .body ol { margin: 0 0 12px; padding-left: 22px; }
.msg .body li { margin: 3px 0; }
.msg .body code {
  font: 13px/1.5 ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  background: var(--code-inline); color: var(--code-fg); padding: 2px 5px; border-radius: 5px;
}
.msg .body pre {
  background: var(--code-bg); color: var(--code-fg); border: 1px solid var(--line);
  border-radius: 12px; padding: 12px 14px; overflow-x: auto; margin: 0 0 12px; position: relative;
}
.msg .body pre code { background: none; color: var(--code-fg); padding: 0; font-size: 13px; }
.msg .body .copy {
  position: absolute; top: 6px; right: 6px; font-size: 11px; padding: 4px 8px;
  background: var(--chip); border: 1px solid var(--line); border-radius: 6px; color: var(--fg-dim); opacity: 0;
}
.msg .body pre:hover .copy { opacity: 1; }
.msg .body blockquote { margin: 0 0 12px; padding-left: 12px; border-left: 3px solid var(--line); color: var(--fg-dim); }
.msg .body table { border-collapse: collapse; margin: 0 0 12px; font-size: 14px; display: block; overflow-x: auto; }
.msg .body th, .msg .body td { border: 1px solid var(--line); padding: 6px 10px; }
.msg .body a { color: #60a5fa; }
.reason {
  border: 1px solid var(--line); border-radius: 10px; padding: 8px 12px; margin: 0 0 12px;
  color: var(--fg-dim); font-size: 13.5px; background: var(--soft);
}
.reason summary { cursor: pointer; }
.cursor::after { content: "▍"; animation: blink 1s steps(1) infinite; color: var(--fg-dim); }
@keyframes blink { 50% { opacity: 0; } }

/* ---------------------------------------------------------------- composer */
.composer {
  max-width: 760px; width: 100%; margin: 0 auto; padding: 6px 20px 4px;
  display: flex; flex-direction: column; position: relative;
}
.composer-row { display: flex; align-items: flex-end; gap: 8px; position: relative; }
.composer .clip { flex: 0 0 auto; margin-bottom: 6px; font-size: 16px; }
.composer textarea {
  resize: none; max-height: 40vh; padding: 13px 50px 13px 15px; border-radius: 26px;
  background: var(--bg-input); border: 1px solid var(--line); line-height: 1.45; overflow-y: auto;
  flex: 1;
}
.composer .send {
  position: absolute; right: 4px; bottom: 6px; width: 32px; height: 32px; border-radius: 50%;
  border: 0; background: var(--accent); color: #fff; font-size: 16px; display: grid; place-items: center;
}
.composer .send:disabled { background: #3b82f655; cursor: default; }
.status { text-align: center; color: var(--fg-dim); font-size: 12px; padding: 2px 0 10px; min-height: 20px; }

/* ---------------------------------------------------------------- auth */
.auth-wrap { min-height: 100dvh; display: grid; place-items: center; padding: 24px; }
.auth-card {
  width: 100%; max-width: 380px; background: var(--bg-elev); border: 1px solid var(--line);
  border-radius: 18px; padding: 28px 26px; display: flex; flex-direction: column; gap: 14px;
}
.auth-card h1 { font-size: 21px; font-weight: 500; margin: 0; }
.auth-card label { display: flex; flex-direction: column; gap: 6px; font-size: 13px; color: var(--fg-dim); }
.brand { font-size: 17px; font-weight: 600; letter-spacing: .2px; }
.brand i { color: var(--accent); font-style: normal; }

/* ---------------------------------------------------------------- pages */
.page { max-width: 820px; margin: 0 auto; min-height: 100dvh; display: flex; flex-direction: column; }
.page-body { padding: 18px 18px 40px; }
.card { background: var(--bg-elev); border: 1px solid var(--line); border-radius: var(--radius); padding: 18px; margin-bottom: 14px; }
.card h3 { margin: 0 0 12px; font-size: 16px; font-weight: 500; }
.row { display: flex; gap: 10px; flex-wrap: wrap; align-items: center; margin-bottom: 10px; }
.row input, .row select { flex: 1; min-width: 160px; }
.col { display: flex; flex-direction: column; gap: 10px; }
.trusted-row { display: flex; gap: 10px; align-items: center; padding: 8px 0; border-top: 1px solid var(--line); }
.trusted-row form { margin: 0; }
.key-row { border-top: 1px solid var(--line); padding: 12px 0; }
.key-row:first-of-type { border-top: 0; }
.key-head { margin-bottom: 8px; }
.key-head code { background: var(--code-inline); color: var(--code-fg); padding: 2px 6px; border-radius: 5px; font-size: 13px; }
.key-row form.row { margin-bottom: 6px; }
.key-row form.row input { min-width: 220px; }
.codes {
  background: var(--code-bg); color: var(--code-fg); border: 1px solid var(--line);
  border-radius: 10px; padding: 14px;
  font: 14px/1.9 ui-monospace, Menlo, Consolas, monospace; overflow-x: auto;
}
.audit { background: var(--code-bg); border: 1px solid var(--line); border-radius: 10px; padding: 12px;
  font: 12px/1.7 ui-monospace, Menlo, Consolas, monospace; color: var(--fg-dim); overflow-x: auto; }
.table-wrap { overflow-x: auto; }
table { border-collapse: collapse; width: 100%; font-size: 14px; }
table th, table td { border-bottom: 1px solid var(--line); padding: 8px 10px; text-align: left; }
table form { margin: 0; }

/* ---------------------------------------------------------------- mobile */
@media (max-width: 860px) {
  .sidebar {
    position: fixed; inset: 0 auto 0 0; z-index: 30; transform: translateX(-100%);
    transition: transform .22s ease; box-shadow: 0 0 40px var(--shadow);
  }
  .sidebar.open { transform: none; }
  .scrim { display: none; position: fixed; inset: 0; background: var(--scrim); z-index: 20; }
  .scrim.on { display: block; }
  .only-mobile { display: inline-block; }
  .msg { padding: 12px 14px; gap: 10px; }
  .composer { padding: 6px 12px 4px; }
  .composer .send { right: 22px; }
}
