:root {
  --bg: #f6f7f9;
  --surface: #ffffff;
  --surface-2: #f1f3f6;
  --border: #e3e6eb;
  --text: #1b1f27;
  --muted: #6a7280;
  --accent: #2f6fed;
  --accent-soft: #e8effd;
  --accent-text: #ffffff;
  --danger: #d6453d;
  --ok: #1f9d55;
  --warn: #c98a0a;
  --note: #fff8e1;
  --note-border: #f1dc9a;
  --sidebar: #161a22;
  --sidebar-text: #c9ced8;
  --sidebar-active: #252b37;
  --shadow: 0 1px 2px rgba(16, 24, 40, .06), 0 8px 24px rgba(16, 24, 40, .08);
  --radius: 10px;
  --s-new: #2f6fed; --s-open: #7b5cf0; --s-pending: #c98a0a; --s-resolved: #1f9d55; --s-closed: #8a919e;
  --p-low: #8a919e; --p-normal: #2f6fed; --p-high: #e07a1f; --p-urgent: #d6453d;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f1218; --surface: #171b23; --surface-2: #1e232d; --border: #2a303c; --text: #e6e9ef; --muted: #8e97a6;
    --accent: #5b8cff; --accent-soft: #1d2a47; --note: #2b2615; --note-border: #5a4a1c;
    --sidebar: #0b0e13; --sidebar-active: #1c212b; --shadow: 0 1px 2px rgba(0,0,0,.4), 0 8px 24px rgba(0,0,0,.35);
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body { font: 14px/1.5 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif; background: var(--bg); color: var(--text); }
a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }
h1 { font-size: 20px; margin: 0; font-weight: 650; letter-spacing: -.01em; }
h2 { font-size: 15px; margin: 0 0 12px; font-weight: 650; }
h3 { font-size: 13px; margin: 0 0 8px; font-weight: 600; color: var(--muted); text-transform: uppercase; letter-spacing: .04em; }
code, .mono { font-family: ui-monospace, SFMono-Regular, Consolas, monospace; font-size: 12.5px; }
pre { background: var(--surface-2); border: 1px solid var(--border); border-radius: 8px; padding: 12px; overflow: auto; font-size: 12.5px; margin: 8px 0; }
.muted { color: var(--muted); }
.small { font-size: 12.5px; }
.hidden { display: none !important; }

/* Layout */
.app { display: grid; grid-template-columns: 236px 1fr; height: 100vh; }
.sidebar { background: var(--sidebar); color: var(--sidebar-text); display: flex; flex-direction: column; padding: 14px 10px; overflow-y: auto; }
.brand { display: flex; align-items: center; gap: 10px; padding: 4px 8px 14px; color: #fff; font-weight: 700; font-size: 15px; }
.brand-logo { width: 28px; height: 28px; border-radius: 8px; background: linear-gradient(135deg, #2f6fed, #7b5cf0); display: grid; place-items: center; font-size: 14px; }
.nav-section { margin: 14px 8px 4px; font-size: 11px; text-transform: uppercase; letter-spacing: .06em; color: #6b7382; }
.nav a { display: flex; align-items: center; justify-content: space-between; padding: 7px 10px; border-radius: 7px; color: var(--sidebar-text); margin: 1px 0; }
.nav a:hover { background: var(--sidebar-active); text-decoration: none; color: #fff; }
.nav a.active { background: var(--sidebar-active); color: #fff; font-weight: 600; }
.nav .count { font-size: 11.5px; background: #2a3140; color: #c9ced8; padding: 0 7px; border-radius: 10px; min-width: 22px; text-align: center; }
.nav .count.hot { background: #d6453d; color: #fff; }
.sidebar .btn-new { margin: 2px 4px 6px; justify-content: center; }
.sidebar-footer { margin-top: auto; padding: 10px 8px 0; border-top: 1px solid #232937; font-size: 13px; }
.sidebar-footer .who { color: #fff; font-weight: 600; }
.sidebar-footer a { color: #8e97a6; margin-right: 10px; }
.main { overflow-y: auto; min-width: 0; }
.page { padding: 22px 28px 40px; max-width: 1400px; }
.page-head { display: flex; align-items: center; gap: 12px; margin-bottom: 16px; flex-wrap: wrap; }
.page-head .spacer { flex: 1; }
.mobile-bar { display: none; }

/* Controls */
.btn { display: inline-flex; align-items: center; gap: 6px; border: 1px solid var(--border); background: var(--surface); color: var(--text); padding: 7px 13px; border-radius: 8px; font: inherit; font-weight: 550; cursor: pointer; white-space: nowrap; }
.btn:hover { background: var(--surface-2); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-text); }
.btn.primary:hover { filter: brightness(1.08); }
.btn.danger { color: var(--danger); }
.btn.small { padding: 4px 9px; font-size: 12.5px; }
.btn:disabled { opacity: .55; cursor: default; }
input, select, textarea { font: inherit; color: var(--text); background: var(--surface); border: 1px solid var(--border); border-radius: 8px; padding: 7px 10px; width: 100%; }
input:focus, select:focus, textarea:focus { outline: 2px solid var(--accent-soft); border-color: var(--accent); }
textarea { resize: vertical; min-height: 90px; }
input[type=checkbox] { width: auto; }
.field { display: block; margin-bottom: 12px; }
label.field > span { display: block; font-size: 12.5px; color: var(--muted); margin-bottom: 4px; font-weight: 550; }
.field-error { color: var(--danger); font-size: 12px; margin-top: 3px; }
.row { display: flex; gap: 12px; }
.row > * { flex: 1; min-width: 0; }
.search { position: relative; min-width: 260px; flex: 1; max-width: 420px; }
.search input { padding-left: 32px; }
.search::before { content: "⌕"; position: absolute; left: 11px; top: 6px; color: var(--muted); font-size: 16px; }
.filters { display: flex; gap: 8px; flex-wrap: wrap; }
.filters select { width: auto; }
kbd { font-family: inherit; font-size: 11px; border: 1px solid var(--border); border-bottom-width: 2px; border-radius: 4px; padding: 0 5px; color: var(--muted); background: var(--surface); }

/* Cards & tables */
.card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 18px; }
.card + .card { margin-top: 16px; }
.table { width: 100%; border-collapse: collapse; background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); overflow: hidden; }
.table th { text-align: left; font-size: 12px; font-weight: 600; color: var(--muted); padding: 9px 12px; border-bottom: 1px solid var(--border); background: var(--surface); white-space: nowrap; }
.table td { padding: 10px 12px; border-bottom: 1px solid var(--border); vertical-align: middle; }
.table tr:last-child td { border-bottom: 0; }
.table tbody tr.clickable { cursor: pointer; }
.table tbody tr.clickable:hover td { background: var(--surface-2); }
.table .subj { font-weight: 550; }
.table td.subj-cell { min-width: 260px; width: 50%; }
.table td.client-cell { max-width: 220px; }
@media (max-width: 1250px) { .table .hide-md { display: none; } }
.table .num { color: var(--muted); font-variant-numeric: tabular-nums; white-space: nowrap; }
.empty { text-align: center; padding: 48px 20px; color: var(--muted); }
.more { text-align: center; margin-top: 14px; }

.pill { display: inline-flex; align-items: center; gap: 6px; font-size: 12px; font-weight: 600; padding: 2px 9px; border-radius: 20px; white-space: nowrap; background: var(--surface-2); }
.pill::before { content: ""; width: 7px; height: 7px; border-radius: 50%; background: currentColor; }
.st-new { color: var(--s-new); } .st-open { color: var(--s-open); } .st-pending { color: var(--s-pending); } .st-resolved { color: var(--s-resolved); } .st-closed { color: var(--s-closed); }
.pr { font-size: 12px; font-weight: 600; white-space: nowrap; }
.pr-low { color: var(--p-low); } .pr-normal { color: var(--muted); } .pr-high { color: var(--p-high); } .pr-urgent { color: var(--p-urgent); }
.tag { display: inline-block; font-size: 11.5px; background: var(--accent-soft); color: var(--accent); padding: 1px 8px; border-radius: 10px; margin: 0 4px 2px 0; }
.overdue { color: var(--danger); font-weight: 600; }
.avatar { display: inline-grid; place-items: center; width: 24px; height: 24px; border-radius: 50%; background: var(--accent-soft); color: var(--accent); font-size: 11px; font-weight: 700; flex: none; }
.who-cell { display: flex; align-items: center; gap: 8px; white-space: nowrap; }

/* Ticket view */
.ticket { display: grid; grid-template-columns: minmax(0, 1fr) 320px; gap: 20px; align-items: start; }
.ticket-title { font-size: 20px; font-weight: 650; border: 1px solid transparent; padding: 3px 6px; margin-left: -7px; border-radius: 6px; width: 100%; background: transparent; }
.ticket-title:hover { border-color: var(--border); }
.thread { display: flex; flex-direction: column; gap: 12px; }
.msg { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 12px 16px; }
.msg.client { border-left: 3px solid var(--accent); }
.msg.agent { border-left: 3px solid var(--ok); }
.msg.internal { background: var(--note); border-color: var(--note-border); border-left: 3px solid var(--warn); }
.msg-head { display: flex; align-items: center; gap: 8px; margin-bottom: 6px; font-size: 13px; }
.msg-head .when { margin-left: auto; color: var(--muted); font-size: 12px; }
.msg-body { white-space: pre-wrap; word-wrap: break-word; }
.badge { font-size: 11px; font-weight: 600; padding: 1px 7px; border-radius: 10px; background: var(--surface-2); color: var(--muted); }
.reply { position: sticky; bottom: 0; background: var(--bg); padding: 12px 0 4px; }
.reply-box { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); box-shadow: var(--shadow); }
.reply-box.internal { background: var(--note); border-color: var(--note-border); }
.reply-tabs { display: flex; gap: 2px; padding: 6px 8px 0; }
.reply-tabs button { border: 0; background: transparent; padding: 6px 10px; border-radius: 6px; font: inherit; font-weight: 600; color: var(--muted); cursor: pointer; }
.reply-tabs button.active { color: var(--text); background: var(--surface-2); }
.reply-box textarea { border: 0; background: transparent; min-height: 100px; outline: none; }
.reply-actions { display: flex; align-items: center; gap: 8px; padding: 8px 10px; border-top: 1px solid var(--border); flex-wrap: wrap; }
.reply-actions select { width: auto; }
.props { position: sticky; top: 16px; }
.prop { margin-bottom: 12px; }
.prop > span { display: block; font-size: 12px; color: var(--muted); margin-bottom: 4px; font-weight: 550; }
.kv { display: grid; grid-template-columns: 110px 1fr; gap: 6px 10px; font-size: 13px; }
.kv dt { color: var(--muted); }
.kv dd { margin: 0; min-width: 0; overflow-wrap: anywhere; }
.copy { cursor: pointer; border-bottom: 1px dashed var(--muted); }
.copy:hover { color: var(--accent); }

/* Picker */
.picker { position: relative; }
.picker-list { position: absolute; z-index: 30; left: 0; right: 0; top: calc(100% + 4px); background: var(--surface); border: 1px solid var(--border); border-radius: 8px; box-shadow: var(--shadow); max-height: 260px; overflow-y: auto; }
.picker-list div { padding: 7px 10px; cursor: pointer; }
.picker-list div:hover, .picker-list div.active { background: var(--surface-2); }
.picker-list .sub { font-size: 12px; color: var(--muted); }
.picker-clear { position: absolute; right: 8px; top: 7px; border: 0; background: none; color: var(--muted); cursor: pointer; font-size: 15px; }

/* Modal & toast */
.modal-bg { position: fixed; inset: 0; background: rgba(10, 14, 22, .45); display: grid; place-items: start center; padding: 8vh 16px; z-index: 50; overflow-y: auto; }
.modal { background: var(--surface); border-radius: 14px; box-shadow: var(--shadow); width: 100%; max-width: 620px; padding: 22px; }
.modal-head { display: flex; align-items: center; margin-bottom: 16px; }
.modal-head h2 { margin: 0; flex: 1; font-size: 17px; }
.modal-foot { display: flex; justify-content: flex-end; gap: 8px; margin-top: 8px; }
.x { border: 0; background: none; font-size: 22px; line-height: 1; color: var(--muted); cursor: pointer; }
.toasts { position: fixed; right: 18px; bottom: 18px; display: flex; flex-direction: column; gap: 8px; z-index: 60; }
.toast { background: #1b1f27; color: #fff; padding: 10px 14px; border-radius: 8px; box-shadow: var(--shadow); max-width: 380px; animation: pop .15s ease-out; }
.toast.error { background: var(--danger); }
@keyframes pop { from { transform: translateY(6px); opacity: 0; } }

/* Tabs */
.tabs { display: flex; gap: 4px; border-bottom: 1px solid var(--border); margin-bottom: 18px; }
.tabs a { padding: 8px 14px; color: var(--muted); font-weight: 600; border-bottom: 2px solid transparent; margin-bottom: -1px; }
.tabs a:hover { color: var(--text); text-decoration: none; }
.tabs a.active { color: var(--text); border-color: var(--accent); }

.grid-2 { display: grid; grid-template-columns: repeat(auto-fit, minmax(300px, 1fr)); gap: 16px; }
.stat-row { display: grid; grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); gap: 12px; margin-bottom: 18px; }
.stat { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 12px 14px; cursor: pointer; }
.stat:hover { border-color: var(--accent); }
.stat b { display: block; font-size: 22px; font-variant-numeric: tabular-nums; }
.stat span { color: var(--muted); font-size: 12.5px; }
.secret { background: var(--note); border: 1px solid var(--note-border); padding: 10px 12px; border-radius: 8px; margin: 10px 0; word-break: break-all; }
.checks { display: grid; grid-template-columns: repeat(auto-fill, minmax(190px, 1fr)); gap: 4px 12px; }
.checks label { display: flex; gap: 6px; align-items: center; font-size: 13px; }
.status-dot { display: inline-block; width: 8px; height: 8px; border-radius: 50%; margin-right: 6px; }

/* Login */
.login-wrap { min-height: 100vh; display: grid; place-items: center; padding: 16px; }
.login { width: 100%; max-width: 360px; }
.login .brand { color: var(--text); justify-content: center; font-size: 18px; padding-bottom: 20px; }

@media (max-width: 900px) {
  .app { grid-template-columns: 1fr; }
  .sidebar { position: fixed; inset: 0 auto 0 0; width: 260px; z-index: 40; transform: translateX(-100%); transition: transform .2s; }
  .sidebar.open { transform: none; }
  .mobile-bar { display: flex; align-items: center; gap: 10px; padding: 10px 16px; background: var(--sidebar); color: #fff; position: sticky; top: 0; z-index: 20; }
  .mobile-bar button { background: none; border: 0; color: #fff; font-size: 20px; cursor: pointer; }
  .page { padding: 16px; }
  .ticket { grid-template-columns: 1fr; }
  .props { position: static; }
  .table .hide-sm { display: none; }
  .search { min-width: 0; max-width: none; flex-basis: 100%; order: 3; }
  .filters { flex-basis: 100%; order: 4; }
  .filters select { flex: 1; min-width: 0; }
  .table td.subj-cell { min-width: 0; }
  .table td, .table th { padding: 9px 8px; }
  .row { flex-direction: column; gap: 0; }
}

/* База знаний */
.kb-split { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.kb-editor { min-height: 460px; font-size: 13px; line-height: 1.55; }
.kb-preview { background: var(--surface); border: 1px solid var(--border); border-radius: 8px; padding: 6px 16px; min-height: 460px; max-height: 70vh; overflow-y: auto; }
.file-row { display: flex; align-items: center; gap: 6px; padding: 8px 0; border-bottom: 1px solid var(--border); }
.file-row a { overflow-wrap: anywhere; font-weight: 550; }
.drop-hint { margin-top: 8px; }
.drag .card { border-color: var(--accent); background: var(--accent-soft); }
.kb-pick { max-height: 55vh; overflow-y: auto; }
.kb-pick-row { display: flex; align-items: center; gap: 8px; padding: 10px 4px; border-bottom: 1px solid var(--border); }

.md { line-height: 1.65; overflow-wrap: anywhere; }
.md h2 { font-size: 18px; margin: 22px 0 8px; }
.md h3 { font-size: 15px; margin: 18px 0 6px; text-transform: none; letter-spacing: 0; color: var(--text); }
.md h4 { font-size: 14px; margin: 14px 0 6px; }
.md p { margin: 10px 0; }
.md ul, .md ol { padding-left: 22px; margin: 10px 0; }
.md li { margin: 4px 0; }
.md img { max-width: 100%; border-radius: 8px; border: 1px solid var(--border); margin: 6px 0; }
.md code { background: var(--surface-2); padding: 1px 5px; border-radius: 4px; }
.md pre code { background: none; padding: 0; }
.md blockquote { margin: 12px 0; padding: 10px 14px; background: var(--note); border-left: 3px solid var(--warn); border-radius: 6px; }
.md hr { border: 0; border-top: 1px solid var(--border); margin: 18px 0; }

@media (max-width: 900px) {
  .kb-split { grid-template-columns: 1fr; }
  .kb-editor, .kb-preview { min-height: 260px; }
}
