:root {
  --bg: #f6f7fb;
  --surface: #ffffff;
  --surface-2: #eef1f7;
  --border: #dfe4ee;
  --text: #1b2433;
  --muted: #667085;
  --primary: #1f3a5f;
  --primary-ink: #ffffff;
  --accent: #f5c451;
  --user-bubble: #1f3a5f;
  --user-ink: #ffffff;
  --danger: #c0392b;
  --ok: #1e7b4f;
  --radius: 14px;
  --shadow: 0 1px 2px rgba(16, 24, 40, .06), 0 4px 16px rgba(16, 24, 40, .06);
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #0f141c;
    --surface: #161d28;
    --surface-2: #1d2634;
    --border: #2a3444;
    --text: #e6ebf3;
    --muted: #94a0b4;
    --primary: #3d6aa3;
    --primary-ink: #ffffff;
    --user-bubble: #2d5687;
    --user-ink: #ffffff;
    --danger: #ff6b5b;
    --ok: #5ccc96;
    --shadow: 0 1px 2px rgba(0, 0, 0, .4);
    color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #0f141c;
  --surface: #161d28;
  --surface-2: #1d2634;
  --border: #2a3444;
  --text: #e6ebf3;
  --muted: #94a0b4;
  --primary: #3d6aa3;
  --primary-ink: #ffffff;
  --user-bubble: #2d5687;
  --user-ink: #ffffff;
  --danger: #ff6b5b;
  --ok: #5ccc96;
  --shadow: 0 1px 2px rgba(0, 0, 0, .4);
  color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 16px/1.55 system-ui, -apple-system, "Segoe UI", Roboto, "Noto Sans", sans-serif;
  -webkit-font-smoothing: antialiased;
}
a { color: var(--primary); }
h1 { font-size: 1.6rem; margin: .2em 0 .5em; }
h2 { font-size: 1.15rem; margin: 0 0 .6em; }
code { background: var(--surface-2); padding: .1em .35em; border-radius: 6px; font-size: .92em; }
.muted { color: var(--muted); }
.small { font-size: .85rem; }
.only-mobile { display: none; }

button, input, select, textarea { font: inherit; color: inherit; }
button { cursor: pointer; }
input, select, textarea {
  width: 100%;
  padding: .55rem .7rem;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--surface);
}
input[type="checkbox"] { width: auto; }
input:focus, select:focus, textarea:focus { outline: 2px solid var(--primary); outline-offset: 0; border-color: transparent; }
label { display: block; font-size: .9rem; color: var(--muted); }
label > input, label > select, label > textarea { margin-top: .3rem; color: var(--text); }
label.check { display: inline-flex; align-items: center; gap: .4rem; color: var(--text); margin: 0; }

.btn-primary, .btn-ghost, .btn-danger, .btn-stop, .icon-btn {
  border: 0;
  border-radius: 10px;
  padding: .55rem 1rem;
  font-weight: 600;
  text-decoration: none;
  display: inline-block;
}
.btn-primary { background: var(--primary); color: var(--primary-ink); }
.btn-primary:hover { filter: brightness(1.08); }
.btn-ghost { background: var(--surface-2); color: var(--text); border: 1px solid var(--border); }
.btn-danger { background: transparent; color: var(--danger); border: 1px solid var(--danger); }
.btn-stop { background: var(--danger); color: #fff; }
.icon-btn { background: transparent; color: var(--text); padding: .3rem .55rem; font-size: 1.1rem; }
.full { width: 100%; }

.alert { padding: .65rem .9rem; border-radius: 10px; margin: .6rem 0; font-size: .95rem; }
.alert.error { background: color-mix(in srgb, var(--danger) 14%, transparent); color: var(--danger); }
.alert.ok { background: color-mix(in srgb, var(--ok) 14%, transparent); color: var(--ok); }
[hidden] { display: none !important; }

/* ---------- Вход / регистрация ---------- */
.auth-body { display: grid; place-items: center; min-height: 100vh; padding: 16px; }
.auth-card {
  width: 100%; max-width: 400px; background: var(--surface); border-radius: 20px;
  box-shadow: var(--shadow); padding: 28px 24px; border: 1px solid var(--border);
}
.auth-brand { display: flex; align-items: center; gap: .6rem; text-decoration: none; color: var(--text); font-weight: 700; font-size: 1.25rem; margin-bottom: 18px; }
.auth-brand.small { font-size: 1rem; margin: 0; }
.tabs { display: flex; gap: 4px; background: var(--surface-2); padding: 4px; border-radius: 12px; margin-bottom: 16px; }
.tab { flex: 1; text-align: center; padding: .5rem; border-radius: 9px; text-decoration: none; color: var(--muted); font-weight: 600; }
.tab.active { background: var(--surface); color: var(--text); box-shadow: var(--shadow); }
.stack { display: flex; flex-direction: column; gap: 14px; }

/* ---------- Приложение (чат) ---------- */
.app { display: flex; height: 100vh; height: 100dvh; overflow: hidden; }
.side {
  width: 280px; flex: 0 0 280px; background: var(--surface); border-right: 1px solid var(--border);
  display: flex; flex-direction: column; padding: 14px; gap: 10px; min-height: 0;
}
.side-top { display: flex; align-items: center; justify-content: space-between; }
.brand { display: flex; align-items: center; gap: .55rem; text-decoration: none; color: var(--text); font-weight: 700; font-size: 1.1rem; }
.search { font-size: .92rem; padding: .45rem .7rem; }
.conv-list { flex: 1; overflow-y: auto; min-height: 0; margin: 0 -6px; padding: 0 6px; }
.conv-group { font-size: .75rem; text-transform: uppercase; letter-spacing: .04em; color: var(--muted); margin: 12px 6px 4px; }
.conv-item {
  display: flex; align-items: center; gap: 4px; border-radius: 10px; padding: 2px 4px 2px 10px; margin-bottom: 2px;
}
.conv-item:hover, .conv-item.active { background: var(--surface-2); }
.conv-item.active { box-shadow: inset 3px 0 0 var(--primary); }
.conv-open {
  flex: 1; min-width: 0; text-align: left; background: none; border: 0; padding: .45rem 0; color: var(--text);
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis; font-size: .95rem;
}
.conv-acts { display: none; gap: 2px; }
.conv-item:hover .conv-acts, .conv-item.active .conv-acts, .conv-item:focus-within .conv-acts { display: flex; }
.mini { background: none; border: 0; padding: 4px 6px; border-radius: 8px; color: var(--muted); font-size: .9rem; }
.mini:hover { color: var(--text); background: var(--surface); }
.pin-mark { color: var(--accent); font-size: .8rem; margin-right: 2px; }
.side-foot { border-top: 1px solid var(--border); padding-top: 10px; font-size: .9rem; }
.quota { color: var(--muted); font-size: .85rem; margin-bottom: 6px; }
.side-links { display: flex; flex-wrap: wrap; gap: 4px 12px; }
.side-links a { color: var(--muted); text-decoration: none; }
.side-links a:hover { color: var(--text); text-decoration: underline; }

.main { flex: 1; min-width: 0; display: flex; flex-direction: column; position: relative; }
.topbar {
  display: flex; gap: 12px; align-items: flex-end; padding: 12px 18px; border-bottom: 1px solid var(--border);
  background: color-mix(in srgb, var(--bg) 88%, transparent); backdrop-filter: blur(6px); flex-wrap: wrap;
}
.sel-wrap { font-size: .78rem; color: var(--muted); min-width: 0; flex: 1 1 180px; max-width: 320px; }
.sel-wrap select { font-size: .95rem; padding: .4rem .6rem; }

.msgs { flex: 1; overflow-y: auto; padding: 22px 16px 10px; scroll-behavior: smooth; }
.msg-wrap { max-width: 780px; margin: 0 auto 18px; }
.msg { display: flex; flex-direction: column; }
.msg.user { align-items: flex-end; }
.bubble { max-width: 92%; padding: .75rem 1rem; border-radius: var(--radius); overflow-wrap: anywhere; }
.msg.user .bubble { background: var(--user-bubble); color: var(--user-ink); border-bottom-right-radius: 4px; white-space: pre-wrap; }
.msg.assistant .bubble { background: var(--surface); border: 1px solid var(--border); border-bottom-left-radius: 4px; box-shadow: var(--shadow); width: 100%; }
.msg-meta { font-size: .78rem; color: var(--muted); margin: 4px 6px 0; display: flex; gap: 10px; align-items: center; }
.msg-meta button { background: none; border: 0; color: var(--muted); padding: 2px 4px; border-radius: 6px; font-size: .8rem; }
.msg-meta button:hover { color: var(--text); background: var(--surface-2); }
.bubble p { margin: .4em 0; }
.bubble h3, .bubble h4, .bubble h5, .bubble h6 { margin: .8em 0 .3em; line-height: 1.3; }
.bubble ul, .bubble ol { padding-left: 1.3em; margin: .4em 0; }
.bubble pre { background: var(--surface-2); padding: .8rem 1rem; border-radius: 10px; overflow-x: auto; font-size: .9rem; margin: .6em 0; }
.bubble pre code { background: none; padding: 0; }
.bubble :not(pre) > code { background: var(--surface-2); }
.typing::after { content: "▍"; animation: blink 1s steps(2, start) infinite; color: var(--muted); }
@keyframes blink { to { visibility: hidden; } }

.welcome { max-width: 680px; margin: 8vh auto 0; text-align: center; }
.welcome h1 { font-size: 1.9rem; }
.chips { display: flex; flex-wrap: wrap; gap: 8px; justify-content: center; margin-top: 18px; }
.chip {
  background: var(--surface); border: 1px solid var(--border); border-radius: 999px; padding: .45rem .9rem;
  font-size: .92rem; color: var(--text);
}
.chip:hover { border-color: var(--primary); }

.composer {
  max-width: 780px; width: calc(100% - 32px); margin: 0 auto; display: flex; gap: 10px; align-items: flex-end;
  background: var(--surface); border: 1px solid var(--border); border-radius: 18px; padding: 10px; box-shadow: var(--shadow);
}
.composer textarea {
  border: 0; resize: none; max-height: 200px; background: transparent; padding: .45rem .5rem; line-height: 1.45;
}
.composer textarea:focus { outline: none; }
.composer .send { padding: .6rem 1.1rem; flex: 0 0 auto; }
.composer .btn-stop { flex: 0 0 auto; padding: .6rem 1.1rem; }
.hint { text-align: center; color: var(--muted); font-size: .78rem; margin: 8px 0 12px; }

.scrim { display: none; }

/* ---------- Страници (профил, админ) ---------- */
.page-body { min-height: 100vh; }
.page-head {
  display: flex; justify-content: space-between; align-items: center; gap: 12px; flex-wrap: wrap;
  padding: 14px 20px; background: var(--surface); border-bottom: 1px solid var(--border);
}
.page-head nav { font-size: .95rem; }
.page { max-width: 1100px; margin: 0 auto; padding: 20px 16px 60px; }
.page.narrow { max-width: 640px; }
.card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 18px; margin-bottom: 16px; box-shadow: var(--shadow); }
.admin-tabs { margin-bottom: 18px; flex-wrap: wrap; }
.admin-tabs .tab { flex: 0 0 auto; padding: .5rem .9rem; }
.admin-card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 16px; margin-bottom: 14px; display: flex; flex-direction: column; gap: 12px; box-shadow: var(--shadow); }
.admin-head { display: flex; flex-wrap: wrap; gap: 6px 14px; align-items: baseline; }
.admin-actions { display: flex; flex-wrap: wrap; gap: 10px; align-items: center; justify-content: flex-end; }
.admin-actions .grow-input { flex: 1 1 200px; }
.grid4 { display: grid; grid-template-columns: repeat(auto-fit, minmax(190px, 1fr)); gap: 12px; }
.toolbar { display: flex; gap: 8px; margin-bottom: 12px; }
.toolbar input { max-width: 320px; }
.tbl { width: 100%; border-collapse: collapse; }
.tbl th, .tbl td { text-align: left; padding: .5rem .6rem; border-bottom: 1px solid var(--border); }
.stat-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(160px, 1fr)); gap: 12px; margin-bottom: 16px; }
.stat { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 14px; box-shadow: var(--shadow); }
.stat-v { font-size: 1.6rem; font-weight: 700; }
.stat-k { color: var(--muted); font-size: .9rem; }
.steps { padding-left: 1.2em; }

@media (max-width: 860px) {
  .only-mobile { display: inline-block; }
  .app { position: relative; }
  .side {
    position: fixed; inset: 0 auto 0 0; z-index: 30; transform: translateX(-102%);
    transition: transform .2s ease; width: min(86vw, 300px); box-shadow: var(--shadow);
  }
  .side.open { transform: none; }
  .scrim { display: block; position: fixed; inset: 0; background: rgba(0, 0, 0, .35); z-index: 20; }
  .sel-wrap { min-width: 0; flex: 1 1 140px; }
  .bubble { max-width: 100%; }
  .msgs { padding: 14px 10px 8px; }
  .composer { width: calc(100% - 20px); }
}
