/* ---------- Токены ---------- */
:root {
  --bg: #f3f4f7;
  --surface: #ffffff;
  --surface-2: #f2f3f6;
  --surface-3: #e8eaef;
  --border: #e0e2e8;
  --text: #1c1e23;
  --text-2: #4f5562;
  --muted: #858b97;
  --accent: #5865f2;
  --accent-hover: #4752c4;
  --accent-soft: rgba(88, 101, 242, 0.12);
  --on-accent: #ffffff;
  --ok: #1f8f4e;
  --ok-soft: rgba(35, 165, 90, 0.14);
  --warn: #a86a08;
  --warn-soft: rgba(240, 178, 50, 0.18);
  --danger: #d1383d;
  --danger-soft: rgba(218, 55, 60, 0.12);
  --chart-1: #2a78d6;
  --chart-2: #eb6834;
  --chart-surface: #fcfcfb;
  --grid: #e6e7ea;
  --shadow: 0 8px 30px rgba(20, 22, 30, 0.12);
  --discord-bg: #ffffff;
  --discord-embed: #f2f3f5;
  --discord-text: #313338;
  --discord-quote: #c4c9ce;
  --discord-mention: rgba(88, 101, 242, 0.15);
  --discord-mention-text: #4752c4;
  --radius: 10px;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #111214;
    --surface: #1a1b1e;
    --surface-2: #222327;
    --surface-3: #2b2d31;
    --border: #2e3035;
    --text: #ebecef;
    --text-2: #b5bac1;
    --muted: #80848e;
    --accent-soft: rgba(88, 101, 242, 0.2);
    --ok: #3fbf74;
    --warn: #f0b232;
    --danger: #f2575b;
    --chart-1: #3987e5;
    --chart-2: #d95926;
    --chart-surface: #1a1a19;
    --grid: #2c2d30;
    --shadow: 0 8px 30px rgba(0, 0, 0, 0.45);
    --discord-bg: #313338;
    --discord-embed: #2b2d31;
    --discord-text: #dbdee1;
    --discord-quote: #4e5058;
    --discord-mention: rgba(88, 101, 242, 0.3);
    --discord-mention-text: #c9cdfb;
    color-scheme: dark;
  }
}

:root[data-theme="dark"] {
  --bg: #111214;
  --surface: #1a1b1e;
  --surface-2: #222327;
  --surface-3: #2b2d31;
  --border: #2e3035;
  --text: #ebecef;
  --text-2: #b5bac1;
  --muted: #80848e;
  --accent-soft: rgba(88, 101, 242, 0.2);
  --ok: #3fbf74;
  --warn: #f0b232;
  --danger: #f2575b;
  --chart-1: #3987e5;
  --chart-2: #d95926;
  --chart-surface: #1a1a19;
  --grid: #2c2d30;
  --shadow: 0 8px 30px rgba(0, 0, 0, 0.45);
  --discord-bg: #313338;
  --discord-embed: #2b2d31;
  --discord-text: #dbdee1;
  --discord-quote: #4e5058;
  --discord-mention: rgba(88, 101, 242, 0.3);
  --discord-mention-text: #c9cdfb;
  color-scheme: dark;
}

/* ---------- База ---------- */
* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  background: var(--bg);
  color: var(--text);
  font: 14px/1.45 Inter, system-ui, -apple-system, "Segoe UI", sans-serif;
  -webkit-font-smoothing: antialiased;
}
a { color: var(--accent); text-decoration: none; }
button, input, select, textarea { font: inherit; color: inherit; }
h1, h2, h3 { margin: 0; line-height: 1.2; }
h1 { font-size: 22px; font-weight: 700; }
h2 { font-size: 16px; font-weight: 600; }
h3 { font-size: 14px; font-weight: 600; }
.muted { color: var(--muted); }
.small { font-size: 12px; }
.mono { font-family: ui-monospace, "Cascadia Mono", Consolas, monospace; font-size: 12.5px; }
.nowrap { white-space: nowrap; }
.grow { flex: 1; min-width: 0; }
.row { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }
.stack { display: flex; flex-direction: column; gap: 12px; }
.hidden { display: none !important; }
svg.i { width: 16px; height: 16px; flex: none; stroke: currentColor; fill: none; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }

/* ---------- Каркас ---------- */
.layout { display: flex; min-height: 100vh; }
.sidebar {
  width: 248px; flex: none; background: var(--surface); border-right: 1px solid var(--border);
  display: flex; flex-direction: column; padding: 14px 12px; gap: 14px;
  position: sticky; top: 0; height: 100vh; overflow-y: auto;
}
.brand { display: flex; align-items: center; gap: 10px; font-weight: 700; font-size: 15px; padding: 2px 6px; }
.brand-logo { width: 28px; height: 28px; border-radius: 8px; background: var(--accent); color: #fff; display: grid; place-items: center; }
.org-select { width: 100%; }
.nav { display: flex; flex-direction: column; gap: 2px; }
.nav a {
  display: flex; align-items: center; gap: 10px; padding: 8px 10px; border-radius: 8px;
  color: var(--text-2); font-weight: 500;
}
.nav a:hover { background: var(--surface-2); color: var(--text); }
.nav a.active { background: var(--accent-soft); color: var(--text); }
.nav a .badge { margin-left: auto; }
.sb-bottom { margin-top: auto; display: flex; flex-direction: column; gap: 10px; }
.sync-pill {
  display: flex; align-items: center; gap: 8px; padding: 8px 10px; border-radius: 8px;
  background: var(--surface-2); font-size: 12.5px; color: var(--text-2); cursor: pointer; border: 0; text-align: left; width: 100%;
}
.dot { width: 8px; height: 8px; border-radius: 50%; background: var(--muted); flex: none; }
.dot.ok { background: var(--ok); }
.dot.pending, .dot.syncing { background: var(--warn); }
.dot.error { background: var(--danger); }
.user-box { display: flex; align-items: center; gap: 10px; padding: 6px; border-radius: 8px; }
.user-box .name { font-weight: 600; line-height: 1.2; }
.theme-switch { display: flex; background: var(--surface-2); border-radius: 8px; padding: 3px; }
.theme-switch button { flex: 1; border: 0; background: none; padding: 5px; border-radius: 6px; cursor: pointer; color: var(--muted); display: grid; place-items: center; }
.theme-switch button.on { background: var(--surface); color: var(--text); box-shadow: 0 1px 2px rgba(0,0,0,.15); }
.main { flex: 1; min-width: 0; padding: 24px 28px 60px; max-width: 1280px; }
.page-head { display: flex; align-items: center; gap: 12px; flex-wrap: wrap; margin-bottom: 18px; }
.page-head h1 { margin-right: auto; }
.mobile-bar { display: none; }

/* ---------- Элементы ---------- */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  height: 34px; padding: 0 12px; border-radius: 8px; border: 1px solid var(--border);
  background: var(--surface); color: var(--text); font-weight: 500; cursor: pointer; white-space: nowrap;
}
.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); }
.btn.danger { color: var(--danger); }
.btn.danger.solid { background: var(--danger); border-color: var(--danger); color: #fff; }
.btn.ghost { border-color: transparent; background: none; }
.btn.ghost:hover { background: var(--surface-2); }
.btn.sm { height: 28px; padding: 0 9px; font-size: 13px; }
.btn.icon { width: 34px; padding: 0; }
.btn.icon.sm { width: 28px; }

input[type=text], input[type=password], input[type=search], input[type=date], input[type=number], input[type=url], select, textarea {
  height: 34px; padding: 0 10px; border-radius: 8px; border: 1px solid var(--border);
  background: var(--surface); outline: none; min-width: 0;
}
textarea { height: auto; padding: 8px 10px; resize: vertical; min-height: 80px; width: 100%; }
input:focus, select:focus, textarea:focus { border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft); }
input[type=color] { width: 44px; height: 34px; padding: 2px; border: 1px solid var(--border); border-radius: 8px; background: var(--surface); }
input[type=checkbox] { width: 16px; height: 16px; accent-color: var(--accent); margin: 0; }
label.field { display: flex; flex-direction: column; gap: 5px; font-size: 12.5px; color: var(--text-2); font-weight: 500; }
label.field > input, label.field > select, label.field > textarea { width: 100%; font-size: 14px; color: var(--text); font-weight: 400; }
label.check { display: inline-flex; align-items: center; gap: 8px; cursor: pointer; }
.hint { font-size: 12px; color: var(--muted); font-weight: 400; }
.grid-2 { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.grid-3 { display: grid; grid-template-columns: repeat(3, 1fr); gap: 12px; }

.card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 16px; }
.card + .card { margin-top: 14px; }
.card-head { display: flex; align-items: center; gap: 10px; margin-bottom: 12px; }
.card-head h2 { margin-right: auto; }

.badge {
  display: inline-flex; align-items: center; gap: 4px; height: 20px; padding: 0 7px; border-radius: 10px;
  background: var(--surface-3); color: var(--text-2); font-size: 11.5px; font-weight: 600; white-space: nowrap;
}
.badge.ok { background: var(--ok-soft); color: var(--ok); }
.badge.warn { background: var(--warn-soft); color: var(--warn); }
.badge.danger { background: var(--danger-soft); color: var(--danger); }
.badge.accent { background: var(--accent-soft); color: var(--accent); }

.tabs { display: flex; gap: 4px; border-bottom: 1px solid var(--border); margin-bottom: 14px; overflow-x: auto; overflow-y: hidden; scrollbar-width: none; }
.tabs button {
  border: 0; background: none; padding: 9px 12px; cursor: pointer; color: var(--muted); font-weight: 600;
  border-bottom: 2px solid transparent; margin-bottom: -1px; display: inline-flex; gap: 6px; align-items: center; white-space: nowrap;
}
.tabs button.on { color: var(--text); border-color: var(--accent); }

.avatar {
  width: 32px; height: 32px; border-radius: 50%; flex: none; background: var(--surface-3);
  display: grid; place-items: center; font-weight: 600; font-size: 13px; color: var(--text-2); overflow: hidden;
}
.avatar img { width: 100%; height: 100%; object-fit: cover; }
.avatar.lg { width: 56px; height: 56px; font-size: 20px; }

.empty { text-align: center; padding: 40px 16px; color: var(--muted); }
.empty svg.i { width: 28px; height: 28px; margin-bottom: 8px; }

/* ---------- Таблица состава ---------- */
.toolbar { display: flex; gap: 8px; flex-wrap: wrap; margin-bottom: 12px; align-items: center; }
.toolbar input[type=search] { flex: 1; min-width: 200px; }
.table-wrap { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); overflow: hidden; }
table.list { width: 100%; border-collapse: collapse; }
table.list th { text-align: left; font-size: 12px; color: var(--muted); font-weight: 600; padding: 9px 10px; border-bottom: 1px solid var(--border); background: var(--surface-2); }
table.list td { padding: 8px 10px; border-bottom: 1px solid var(--border); vertical-align: middle; }
table.list tr:last-child td { border-bottom: 0; }
table.list tbody tr.member { cursor: pointer; }
table.list tbody tr.member:hover td { background: var(--surface-2); }
table.list tr.group td { background: var(--surface-2); font-weight: 600; font-size: 12.5px; color: var(--text-2); padding: 7px 10px; cursor: default; }
table.list tr.dragging td { opacity: .4; }
table.list tr.drop-before td { box-shadow: inset 0 2px 0 var(--accent); }
table.list tr.drop-after td { box-shadow: inset 0 -2px 0 var(--accent); }
.handle { cursor: grab; color: var(--muted); display: grid; place-items: center; }
.who { display: flex; align-items: center; gap: 10px; min-width: 0; }
.who .names { min-width: 0; }
.who .n1 { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.who .n2 { font-size: 12px; color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.copy { cursor: copy; color: var(--text-2); }
.copy:hover { color: var(--accent); }
.col-check, .col-handle { width: 34px; }
.bulk-bar {
  position: sticky; bottom: 16px; z-index: 5; margin-top: 12px; display: flex; align-items: center; gap: 8px; flex-wrap: wrap;
  background: var(--surface); border: 1px solid var(--border); border-radius: 12px; padding: 10px 12px; box-shadow: var(--shadow);
}

/* ---------- Модальные окна ---------- */
.overlay {
  position: fixed; inset: 0; background: rgba(8, 9, 12, 0.55); display: flex; align-items: flex-start; justify-content: center;
  padding: 6vh 16px 16px; z-index: 50; overflow-y: auto; animation: fade .12s ease-out;
}
.modal {
  background: var(--surface); border: 1px solid var(--border); border-radius: 14px; width: 100%; max-width: 480px;
  box-shadow: var(--shadow); display: flex; flex-direction: column; max-height: 88vh;
}
.modal.wide { max-width: 760px; }
.modal-head { display: flex; align-items: center; gap: 10px; padding: 16px 18px 12px; }
.modal-head h2 { margin-right: auto; }
.modal-body { padding: 4px 18px 16px; overflow-y: auto; }
.modal-foot { display: flex; gap: 8px; justify-content: flex-end; padding: 12px 18px 16px; border-top: 1px solid var(--border); flex-wrap: wrap; }
.modal-foot .left { margin-right: auto; display: flex; gap: 8px; flex-wrap: wrap; }
@keyframes fade { from { opacity: 0; } }

.chips { display: flex; gap: 6px; flex-wrap: wrap; }
.chip { border: 1px solid var(--border); background: var(--surface-2); border-radius: 14px; padding: 3px 10px; font-size: 12.5px; cursor: pointer; }
.chip:hover { border-color: var(--accent); }

.toasts { position: fixed; right: 16px; bottom: 16px; display: flex; flex-direction: column; gap: 8px; z-index: 100; max-width: calc(100vw - 32px); }
.toast {
  background: var(--surface); border: 1px solid var(--border); border-left: 3px solid var(--accent); border-radius: 10px;
  padding: 10px 14px; box-shadow: var(--shadow); max-width: 380px; animation: fade .15s ease-out;
}
.toast.error { border-left-color: var(--danger); }
.toast.ok { border-left-color: var(--ok); }

/* ---------- Карточка сотрудника ---------- */
.profile-head { display: flex; gap: 14px; align-items: center; margin-bottom: 14px; }
.items { display: flex; flex-direction: column; gap: 8px; }
.item { display: flex; gap: 10px; align-items: flex-start; padding: 10px 12px; background: var(--surface-2); border-radius: 8px; }
.item .body { flex: 1; min-width: 0; white-space: pre-wrap; word-break: break-word; }
.item .meta { font-size: 12px; color: var(--muted); margin-top: 2px; }

/* ---------- Аудит ---------- */
.audit-item { display: flex; gap: 12px; padding: 12px 14px; border-bottom: 1px solid var(--border); align-items: flex-start; }
.audit-item:last-child { border-bottom: 0; }
.audit-item.undone { opacity: .55; }
.audit-icon { width: 30px; height: 30px; border-radius: 8px; display: grid; place-items: center; flex: none; background: var(--surface-3); color: var(--text-2); }
.audit-icon.add, .audit-icon.restore { background: var(--ok-soft); color: var(--ok); }
.audit-icon.fire, .audit-icon.delete, .audit-icon.blacklist { background: var(--danger-soft); color: var(--danger); }
.audit-icon.leave, .audit-icon.warn { background: var(--warn-soft); color: var(--warn); }
.audit-icon.undo, .audit-icon.edit, .audit-icon.import { background: var(--accent-soft); color: var(--accent); }
.changes { margin-top: 4px; font-size: 12.5px; color: var(--text-2); }
.changes b { font-weight: 600; color: var(--text); }

/* ---------- Статистика ---------- */
.tiles { display: grid; grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); gap: 12px; margin-bottom: 14px; }
.tile { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 14px 16px; }
.tile .v { font-size: 26px; font-weight: 700; margin-top: 2px; font-variant-numeric: tabular-nums; }
.tile .l { font-size: 12.5px; color: var(--muted); font-weight: 500; }
.stats-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 14px; }
.stats-grid .card + .card { margin-top: 0; }
.chart { position: relative; }
.chart svg { display: block; width: 100%; height: auto; overflow: visible; }
.chart .grid-line { stroke: var(--grid); stroke-width: 1; }
.chart .axis-label { fill: var(--muted); font-size: 11px; }
.chart .bar { transition: opacity .1s; }
.chart .hit { fill: transparent; cursor: default; }
.legend { display: flex; gap: 14px; font-size: 12.5px; color: var(--text-2); margin-bottom: 8px; }
.legend span { display: inline-flex; align-items: center; gap: 6px; }
.legend i { width: 10px; height: 10px; border-radius: 3px; display: inline-block; }
.tooltip {
  position: absolute; pointer-events: none; background: var(--surface); border: 1px solid var(--border); border-radius: 8px;
  padding: 7px 10px; box-shadow: var(--shadow); font-size: 12.5px; white-space: nowrap; z-index: 5; transform: translate(-50%, -100%);
}
.hbar { display: grid; grid-template-columns: minmax(90px, 160px) 1fr 32px; gap: 10px; align-items: center; padding: 4px 0; }
.hbar .track { height: 10px; background: var(--surface-2); border-radius: 0 4px 4px 0; }
.hbar .fill { height: 100%; background: var(--chart-1); border-radius: 0 4px 4px 0; min-width: 2px; }
.hbar .num { text-align: right; font-variant-numeric: tabular-nums; color: var(--text-2); }
.list-rows .r { display: flex; align-items: center; gap: 10px; padding: 7px 0; border-bottom: 1px solid var(--border); }
.list-rows .r:last-child { border-bottom: 0; }

/* ---------- Предпросмотр Discord ---------- */
.discord { background: var(--discord-bg); color: var(--discord-text); border-radius: 10px; padding: 14px 16px; font-size: 15px; line-height: 1.375; }
.discord-msg { display: flex; gap: 14px; }
.discord-msg + .discord-msg { margin-top: 16px; }
.discord-avatar { width: 40px; height: 40px; border-radius: 50%; background: var(--accent); flex: none; display: grid; place-items: center; color: #fff; overflow: hidden; }
.discord-avatar img { width: 100%; height: 100%; object-fit: cover; }
.discord-name { font-weight: 600; display: flex; align-items: center; gap: 6px; }
.discord-bot { background: var(--accent); color: #fff; font-size: 10px; font-weight: 700; padding: 1px 5px; border-radius: 4px; }
.discord-embed { background: var(--discord-embed); border-left: 4px solid #1e1f22; border-radius: 4px; padding: 10px 16px 14px 12px; margin-top: 6px; max-width: 520px; word-break: break-word; }
.discord-embed h1 { font-size: 24px; font-weight: 700; margin: 6px 0 4px; }
.discord-embed h2 { font-size: 20px; font-weight: 700; margin: 6px 0 4px; }
.discord-embed .q { border-left: 4px solid var(--discord-quote); padding-left: 12px; }
.discord-embed .sub { font-size: 13px; color: var(--muted); }
.discord-embed .gap { height: 0.6em; }
.mention { background: var(--discord-mention); color: var(--discord-mention-text); border-radius: 3px; padding: 0 2px; font-weight: 500; }

/* ---------- Вход ---------- */
.auth { min-height: 100vh; display: grid; place-items: center; padding: 16px; }
.auth .card { width: 100%; max-width: 400px; padding: 26px; }
.auth h1 { margin-bottom: 4px; }

/* ---------- Должности ---------- */
.role-row { display: grid; grid-template-columns: 28px 64px 64px 1fr 1fr auto; gap: 8px; align-items: center; padding: 8px 0; border-bottom: 1px solid var(--border); }
.role-row:last-child { border-bottom: 0; }
.role-row input { width: 100%; }
.role-row .emoji { text-align: center; }

/* ---------- Адаптив ---------- */
@media (max-width: 900px) {
  .stats-grid, .grid-3 { grid-template-columns: 1fr; }
  .hide-md { display: none; }
}
@media (max-width: 760px) {
  .layout { display: block; }
  .sidebar { position: fixed; inset: 0 auto 0 0; z-index: 40; transform: translateX(-100%); transition: transform .18s; box-shadow: var(--shadow); }
  .sidebar.open { transform: none; }
  .mobile-bar { display: flex; align-items: center; gap: 10px; padding: 10px 16px; background: var(--surface); border-bottom: 1px solid var(--border); position: sticky; top: 0; z-index: 30; }
  .main { padding: 16px 16px 60px; }
  .grid-2 { grid-template-columns: 1fr; }
  .hide-sm { display: none; }
  .role-row { grid-template-columns: 24px 52px 52px 1fr auto; }
  .role-row .role-label { display: none; }
  .overlay { padding: 16px 8px; }
}

/* Поле ввода внутри растягивающегося блока занимает всю ширину */
.grow > input, .grow > select { flex: 1; width: 100%; }
