:root {
  --bg: #f5f6f8;
  --surface: #ffffff;
  --surface-2: #f0f2f5;
  --ink: #18212b;
  --muted: #5b6876;
  --line: #e1e5ea;
  --accent: #2450a6;
  --accent-hover: #1c428c;
  --accent-soft: #e8eefa;
  --on-accent: #ffffff;
  --client: #b86e0c;
  --client-soft: #fdf3e4;
  --ours: #23794a;
  --ours-soft: #e7f5ec;
  --note: #6f7c8a;
  --note-soft: #f1f3f5;
  --ai: #6542b8;
  --ai-soft: #f1ecfb;
  --danger: #b8322a;
  --danger-soft: #fbeaea;
  --shadow: 0 1px 2px rgba(16, 24, 40, .05), 0 1px 3px rgba(16, 24, 40, .06);
  --radius: 10px;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #0f1419;
    --surface: #171e26;
    --surface-2: #1e2731;
    --ink: #e4e9ee;
    --muted: #93a0ae;
    --line: #2a3542;
    --accent: #6b9bf2;
    --accent-hover: #87aff5;
    --accent-soft: #1d2b45;
    --on-accent: #0c1420;
    --client: #e9a650;
    --client-soft: #2f2414;
    --ours: #5cc48a;
    --ours-soft: #15291e;
    --note: #9aa6b3;
    --note-soft: #1e2630;
    --ai: #a98cf0;
    --ai-soft: #251e3a;
    --danger: #f07a70;
    --danger-soft: #3a1c1a;
    --shadow: none;
    color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #0f1419; --surface: #171e26; --surface-2: #1e2731; --ink: #e4e9ee; --muted: #93a0ae; --line: #2a3542;
  --accent: #6b9bf2; --accent-hover: #87aff5; --accent-soft: #1d2b45; --on-accent: #0c1420;
  --client: #e9a650; --client-soft: #2f2414; --ours: #5cc48a; --ours-soft: #15291e; --note: #9aa6b3; --note-soft: #1e2630;
  --ai: #a98cf0; --ai-soft: #251e3a; --danger: #f07a70; --danger-soft: #3a1c1a; --shadow: none; color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  background: var(--bg);
  color: var(--ink);
  font: 15px/1.5 system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
}
a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }
button, input, textarea, select { font: inherit; color: inherit; }
h1, h2, h3 { margin: 0; line-height: 1.25; }
h1 { font-size: 22px; font-weight: 650; }
h2 { font-size: 17px; font-weight: 650; }
h3 { font-size: 14px; font-weight: 650; text-transform: uppercase; letter-spacing: .04em; color: var(--muted); }
.mono { font-variant-numeric: tabular-nums; }
.muted { color: var(--muted); }
.small { font-size: 13px; }
.hidden { display: none !important; }

/* ── шапка ── */
.top {
  position: sticky; top: 0; z-index: 10;
  background: var(--surface); border-bottom: 1px solid var(--line);
}
.top-in {
  max-width: 1240px; margin: 0 auto; padding: 10px 16px;
  display: flex; align-items: center; gap: 16px;
}
.brand { display: flex; align-items: center; gap: 9px; font-weight: 700; font-size: 16px; color: var(--ink); white-space: nowrap; }
.brand:hover { text-decoration: none; }
.brand-mark { width: 26px; height: 26px; border-radius: 7px; background: var(--accent); display: grid; place-items: center; }
.brand-mark svg { width: 15px; height: 15px; }
.search { flex: 1; position: relative; max-width: 560px; }
.search input {
  width: 100%; height: 40px; padding: 0 12px 0 38px;
  border: 1px solid var(--line); border-radius: 9px; background: var(--surface-2);
  outline: none; transition: border-color .15s, background .15s;
}
.search input:focus { border-color: var(--accent); background: var(--surface); }
.search svg { position: absolute; left: 12px; top: 11px; width: 18px; height: 18px; color: var(--muted); }
.user { margin-left: 4px; display: flex; align-items: center; gap: 6px; white-space: nowrap; }
.user-name { color: var(--muted); font-size: 14px; }

/* ── кнопки и поля ── */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  height: 36px; padding: 0 14px; border-radius: 8px; border: 1px solid var(--line);
  background: var(--surface); cursor: pointer; font-weight: 550; font-size: 14px; white-space: nowrap;
  transition: background .12s, border-color .12s;
}
.btn:hover { background: var(--surface-2); }
.btn:disabled { opacity: .55; cursor: default; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--on-accent); }
.btn.primary:hover { background: var(--accent-hover); border-color: var(--accent-hover); }
.btn.ai { background: var(--ai); border-color: var(--ai); color: #fff; }
.btn.ai:hover { filter: brightness(1.08); }
.btn.ghost { border-color: transparent; background: transparent; }
.btn.ghost:hover { background: var(--surface-2); }
.btn.danger { color: var(--danger); }
.btn.sm { height: 30px; padding: 0 10px; font-size: 13px; }
.btn.block { width: 100%; }
.icon-btn { width: 30px; height: 30px; padding: 0; border-radius: 7px; border: 0; background: transparent; color: var(--muted); cursor: pointer; display: inline-grid; place-items: center; }
.icon-btn:hover { background: var(--surface-2); color: var(--ink); }
.icon-btn svg { width: 16px; height: 16px; }

label.f { display: block; margin-bottom: 12px; }
label.f > span { display: block; font-size: 13px; color: var(--muted); margin-bottom: 4px; font-weight: 550; }
.inp, textarea.inp, select.inp {
  width: 100%; padding: 8px 11px; border: 1px solid var(--line); border-radius: 8px;
  background: var(--surface); outline: none; transition: border-color .15s, box-shadow .15s;
}
.inp:focus { border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft); }
textarea.inp { resize: vertical; min-height: 90px; line-height: 1.45; }
.row { display: flex; gap: 10px; align-items: center; flex-wrap: wrap; }
.row.end { justify-content: flex-end; }
.grow { flex: 1; }
.err { color: var(--danger); background: var(--danger-soft); padding: 8px 11px; border-radius: 8px; font-size: 14px; margin: 8px 0; }
.ok { color: var(--ours); font-size: 14px; }

/* ── раскладка ── */
.wrap { max-width: 1240px; margin: 0 auto; padding: 20px 16px 60px; }
.panel { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); box-shadow: var(--shadow); }
.panel-pad { padding: 16px; }
.head-row { display: flex; align-items: center; gap: 12px; flex-wrap: wrap; margin-bottom: 16px; }

/* ── фильтры и список ── */
.chips { display: flex; gap: 6px; flex-wrap: wrap; }
.chip {
  height: 32px; padding: 0 12px; border-radius: 16px; border: 1px solid var(--line);
  background: var(--surface); cursor: pointer; font-size: 14px; font-weight: 550; color: var(--muted);
}
.chip.on { background: var(--accent-soft); border-color: transparent; color: var(--accent); }
.chip b { font-weight: 650; margin-left: 4px; }

.list { overflow: hidden; }
.item {
  display: grid; grid-template-columns: 190px minmax(0, 1fr) auto 120px; gap: 14px; align-items: center;
  padding: 12px 16px; border-top: 1px solid var(--line); color: var(--ink);
}
.item:first-child { border-top: 0; }
.item:hover { background: var(--surface-2); text-decoration: none; }
.item .ph { font-weight: 650; font-variant-numeric: tabular-nums; }
.item .who { font-size: 13px; color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.item .subj { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.item .when { text-align: right; font-size: 13px; color: var(--muted); }
.empty { padding: 36px 16px; text-align: center; color: var(--muted); }
.empty .btn { margin-top: 12px; }

.badge { display: inline-flex; align-items: center; height: 24px; padding: 0 9px; border-radius: 12px; font-size: 12.5px; font-weight: 600; white-space: nowrap; }
.badge.need { background: var(--client-soft); color: var(--client); }
.badge.wait { background: var(--ours-soft); color: var(--ours); }
.badge.closed { background: var(--note-soft); color: var(--note); }
.badge.new { background: var(--accent-soft); color: var(--accent); }

/* ── карточка ── */
.card-head { padding: 16px; display: flex; gap: 16px; align-items: flex-start; flex-wrap: wrap; margin-bottom: 16px; }
.card-head .ph { font-size: 22px; font-weight: 700; font-variant-numeric: tabular-nums; }
.card-head .meta { flex: 1; min-width: 220px; }
.card-head .meta .line { color: var(--muted); margin-top: 2px; }
.card-head .meta .subj { margin-top: 6px; }
.seg { display: inline-flex; background: var(--surface-2); border-radius: 9px; padding: 3px; gap: 2px; }
.seg button { border: 0; background: transparent; height: 30px; padding: 0 12px; border-radius: 7px; cursor: pointer; font-size: 13.5px; font-weight: 550; color: var(--muted); }
.seg button.on { background: var(--surface); color: var(--ink); box-shadow: var(--shadow); }
:root[data-theme="dark"] .seg button.on { background: var(--line); }
@media (prefers-color-scheme: dark) { :root:not([data-theme="light"]) .seg button.on { background: var(--line); } }

.cols { display: grid; grid-template-columns: minmax(0, 1fr) 400px; gap: 16px; align-items: start; }
.side { position: sticky; top: 76px; display: flex; flex-direction: column; gap: 16px; }

.timeline { display: flex; flex-direction: column; gap: 10px; }
.entry { border: 1px solid var(--line); border-left: 4px solid var(--note); border-radius: var(--radius); background: var(--surface); box-shadow: var(--shadow); }
.entry.client { border-left-color: var(--client); }
.entry.ours { border-left-color: var(--ours); }
.entry.note { border-left-color: var(--note); }
.entry.ai { border-left-color: var(--ai); }
.entry-h { display: flex; align-items: center; gap: 8px; padding: 9px 12px 0 14px; font-size: 13px; color: var(--muted); }
.entry-h .kind { font-weight: 650; }
.entry.client .kind { color: var(--client); }
.entry.ours .kind { color: var(--ours); }
.entry.ai .kind { color: var(--ai); }
.entry-h .sp { flex: 1; }
.entry-b { padding: 6px 14px 13px; white-space: pre-wrap; word-wrap: break-word; }
.entry-b.md { white-space: normal; }
.md p { margin: 0 0 8px; white-space: pre-line; }
.md p:last-child { margin-bottom: 0; }
.md ul, .md ol { margin: 0 0 8px; padding-left: 22px; }
.md li { margin: 2px 0; }
.md .mh { font-weight: 700; margin: 10px 0 4px; }
.md .mh:first-child { margin-top: 0; }
.q { margin: 6px 14px 0; padding: 7px 10px; background: var(--surface-2); border-radius: 7px; font-size: 14px; }
.draft { margin: 0 14px 14px; border: 1px dashed var(--ai); border-radius: 8px; background: var(--ai-soft); }
.draft-h { display: flex; align-items: center; gap: 6px; padding: 7px 10px 0 12px; font-size: 13px; font-weight: 650; color: var(--ai); }
.draft-h .sp { flex: 1; }
.draft-b { padding: 6px 12px 11px; white-space: pre-wrap; word-wrap: break-word; }
.from-ai { font-size: 12px; color: var(--ai); }

.side .panel h2 { margin-bottom: 10px; }
.ai-box { border-color: var(--ai); }
.ai-box .hint { font-size: 13px; color: var(--muted); margin: -4px 0 10px; }
.ai-state { display: flex; align-items: center; gap: 10px; padding: 10px 12px; background: var(--ai-soft); border-radius: 8px; color: var(--ai); font-size: 14px; font-weight: 550; margin-top: 10px; }
.spin { width: 16px; height: 16px; border-radius: 50%; border: 2px solid currentColor; border-right-color: transparent; animation: spin .8s linear infinite; flex: none; }
@keyframes spin { to { transform: rotate(360deg); } }
.warn { font-size: 13px; color: var(--client); background: var(--client-soft); padding: 7px 10px; border-radius: 8px; margin-top: 10px; }

.kind-seg { display: grid; grid-template-columns: repeat(3, 1fr); width: 100%; margin-bottom: 10px; }
.kind-seg button { padding: 0 6px; }
.kind-seg button.on[data-k="client"] { color: var(--client); }
.kind-seg button.on[data-k="ours"] { color: var(--ours); }

/* ── вход ── */
.login { min-height: 100vh; display: grid; place-items: center; padding: 16px; }
.login .panel { width: 100%; max-width: 360px; padding: 28px 24px; }
.login .brand { margin-bottom: 18px; font-size: 18px; }

/* ── админка ── */
.tabs { display: flex; gap: 4px; border-bottom: 1px solid var(--line); margin-bottom: 16px; }
.tabs button { border: 0; background: none; padding: 10px 14px; cursor: pointer; font-weight: 600; color: var(--muted); border-bottom: 2px solid transparent; margin-bottom: -1px; }
.tabs button.on { color: var(--accent); border-bottom-color: var(--accent); }
table.t { width: 100%; border-collapse: collapse; font-size: 14px; }
table.t th { text-align: left; font-weight: 600; color: var(--muted); font-size: 13px; padding: 8px 10px; border-bottom: 1px solid var(--line); }
table.t td { padding: 9px 10px; border-bottom: 1px solid var(--line); vertical-align: top; }
.tscroll { overflow-x: auto; }
.dot { display: inline-block; width: 8px; height: 8px; border-radius: 50%; margin-right: 6px; vertical-align: 1px; }
.dot.on { background: var(--ours); }
.dot.off { background: var(--danger); }

.toast { position: fixed; left: 50%; bottom: 24px; transform: translateX(-50%); background: var(--ink); color: var(--bg); padding: 9px 16px; border-radius: 8px; font-size: 14px; z-index: 50; box-shadow: 0 6px 20px rgba(0,0,0,.2); }

@media (max-width: 960px) {
  .cols { grid-template-columns: 1fr; }
  .side { position: static; order: -1; }
}
@media (max-width: 720px) {
  .top-in { flex-wrap: wrap; gap: 10px; }
  .search { order: 3; flex-basis: 100%; max-width: none; }
  .user-name { display: none; }
  .item { grid-template-columns: minmax(0, 1fr) auto; gap: 4px 10px; }
  .item .subj { grid-column: 1 / -1; grid-row: 2; }
  .item .when { grid-column: 2; grid-row: 1; }
  .item > .badge-cell { grid-column: 1 / -1; grid-row: 3; }
  .card-head .ph { font-size: 19px; }
}

/* ── навигация ── */
.nav { display: flex; gap: 2px; margin-left: auto; }
.nav a { padding: 7px 11px; border-radius: 8px; color: var(--muted); font-size: 14px; font-weight: 550; white-space: nowrap; }
.nav a:hover { background: var(--surface-2); color: var(--ink); text-decoration: none; }
.nav a.on { background: var(--accent-soft); color: var(--accent); }

/* ── вопросы юриста, слабые места ── */
.qa { margin: 0 14px 14px; border: 1px solid var(--line); border-radius: 8px; padding: 10px 12px; background: var(--surface-2); }
.qa-h { font-size: 13px; font-weight: 700; color: var(--ai); margin-bottom: 6px; }
.qa-item { padding: 8px 0; border-top: 1px solid var(--line); }
.qa-item:first-of-type { border-top: 0; }
.qa-q { font-weight: 600; margin-bottom: 4px; }
.qa-a { color: var(--ours); white-space: pre-wrap; }
.qa textarea.inp { min-height: 54px; }
.tag { display: inline-block; font-size: 11.5px; font-weight: 600; padding: 1px 8px; border-radius: 10px; background: var(--note-soft); color: var(--note); vertical-align: 1px; }
.tag.kb { background: var(--accent-soft); color: var(--accent); }
.tag.pr-h { background: var(--danger-soft); color: var(--danger); }
.tag.pr-m { background: var(--client-soft); color: var(--client); }
.tag.pr-l { background: var(--note-soft); color: var(--note); }
.weak { margin: 0 14px 14px; padding: 9px 12px; border-radius: 8px; background: var(--client-soft); }
.weak-h { font-size: 13px; font-weight: 700; color: var(--client); }
.weak ul { margin: 4px 0 0; padding-left: 20px; }

/* ── база знаний и рекомендации ── */
.kb-list { display: flex; flex-direction: column; gap: 10px; }
.kb-q { font-weight: 650; }
.kb-a { white-space: pre-wrap; margin: 6px 0 8px; }
.rec.fixed, .rec.rejected { opacity: .8; }
.rec-l { font-size: 12.5px; font-weight: 650; color: var(--muted); text-transform: uppercase; letter-spacing: .03em; margin-top: 8px; }
.rec-t { white-space: pre-wrap; }
ul.obs { margin: 10px 0 0; padding-left: 20px; }
ul.obs li { margin: 6px 0; }

@media (max-width: 720px) {
  .nav { order: 4; flex-basis: 100%; margin-left: 0; overflow-x: auto; }
  .user { margin-left: auto; }
}
