/* ===== Темы ===== */
:root, :root[data-theme="dark"] {
  --bg: #070b0a;
  --bg-2: #0b1311;
  --bg-3: #111c18;
  --bg-hover: #13201c;
  --border: #1d2b26;
  --border-2: #2a3d36;
  --text: #e9f4f0;
  --muted: #7f9a90;
  --accent: #10b384;
  --accent-2: #0a7a5c;
  --accent-text: #052b20;
  --accent-soft: rgba(16, 179, 132, .13);
  --danger: #ff6464;
  --danger-soft: rgba(255, 100, 100, .12);
  --warn: #f5b73f;
  --warn-soft: rgba(245, 183, 63, .13);
  --shadow: 0 10px 32px rgba(0, 0, 0, .5);
  --chip-green-bg: rgba(16, 179, 132, .16); --chip-green-fg: #3ddfae;
  --chip-amber-bg: rgba(245, 183, 63, .15); --chip-amber-fg: #ffd076;
  --chip-red-bg: rgba(255, 100, 100, .14); --chip-red-fg: #ff9a9a;
  color-scheme: dark;
}
:root[data-theme="light"] {
  --bg: #f4f7f6;
  --bg-2: #ffffff;
  --bg-3: #eaf0ee;
  --bg-hover: #e3ece9;
  --border: #d9e2df;
  --border-2: #c2d0cb;
  --text: #14201c;
  --muted: #5e746c;
  --accent: #0d8f6a;
  --accent-2: #0a6e52;
  --accent-text: #ffffff;
  --accent-soft: rgba(13, 143, 106, .11);
  --danger: #d64545;
  --danger-soft: rgba(214, 69, 69, .1);
  --warn: #c98a12;
  --warn-soft: rgba(222, 168, 62, .18);
  --shadow: 0 10px 32px rgba(20, 40, 34, .18);
  --chip-green-bg: rgba(13, 143, 106, .14); --chip-green-fg: #0a6e52;
  --chip-amber-bg: rgba(201, 138, 18, .15); --chip-amber-fg: #8a5f0b;
  --chip-red-bg: rgba(214, 69, 69, .12); --chip-red-fg: #b03434;
  color-scheme: light;
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { height: 100%; }
body {
  margin: 0;
  font: 13.5px/1.45 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  background: var(--bg);
  color: var(--text);
  display: flex;
  flex-direction: column;
  overflow: hidden;
}
body.login-page { overflow: auto; }

::-webkit-scrollbar { width: 10px; height: 10px; }
::-webkit-scrollbar-thumb { background: var(--border-2); border-radius: 6px; border: 2px solid var(--bg); }
::-webkit-scrollbar-corner { background: transparent; }

/* ===== Шапка ===== */
.topbar {
  display: flex; align-items: center; gap: 10px;
  padding: 0 14px; height: 54px; flex: 0 0 auto;
  background: var(--bg-2); border-bottom: 1px solid var(--border);
}
.brand { display: flex; align-items: center; gap: 8px; user-select: none; }
.brand-oom { font-size: 24px; font-weight: 800; letter-spacing: -1px; line-height: 1; }
.brand-group { font-size: 8.5px; letter-spacing: .18em; color: var(--muted); line-height: 1.25; font-weight: 600; }
.top-divider { width: 1px; height: 24px; background: var(--border-2); margin: 0 4px; }
.app-title { font-weight: 650; font-size: 14.5px; white-space: nowrap; }
.spacer { flex: 1; }

.ws-box { display: inline-flex; align-items: center; gap: 2px; margin-left: 8px; }
.ws-select {
  background: var(--bg-3); border: 1px solid var(--border-2); color: var(--text);
  border-radius: 8px; padding: 5px 8px; font-size: 13px; max-width: 220px;
}

/* фильтры в шапке таблицы */
.grid thead th { position: sticky; }
.th-filter {
  position: absolute; right: 2px; bottom: 2px; cursor: pointer;
  font-size: 10px; color: var(--muted); padding: 2px 4px; border-radius: 5px;
  line-height: 1; user-select: none;
}
.th-filter:hover { background: var(--bg-3); color: var(--text); }
.th-filter.active { color: var(--accent); font-weight: 700; }
.filter-pop {
  position: fixed; z-index: 80; background: var(--bg-2);
  border: 1px solid var(--border-2); border-radius: 12px; box-shadow: var(--shadow);
  padding: 10px 12px; width: 260px; max-height: 380px; display: flex; flex-direction: column; gap: 8px;
}
.filter-pop .vals { overflow: auto; display: flex; flex-direction: column; gap: 2px; max-height: 220px; }
.filter-pop .vals label { display: flex; gap: 7px; align-items: center; font-size: 12.5px; cursor: pointer;
  padding: 2px 4px; border-radius: 6px; }
.filter-pop .vals label:hover { background: var(--bg-3); }
.filter-pop .rangebox { display: flex; gap: 8px; }
.filter-pop .rangebox input { width: 100%; }
.filter-pop .foot { display: flex; gap: 6px; justify-content: flex-end; }

/* расхлоп кабинетов в правах */
details.ws-group { border: 1px solid var(--border); border-radius: 10px; padding: 6px 10px; }
details.ws-group summary { cursor: pointer; user-select: none; padding: 2px 0; }
details.ws-group summary::marker { color: var(--accent); }

/* редактор формул */
.formula-input { width: 100%; min-height: 64px; font-family: ui-monospace, Menlo, monospace; font-size: 13px; resize: vertical; }
.formula-cols { display: flex; flex-wrap: wrap; gap: 5px; max-height: 160px; overflow: auto; }
.formula-cols button { font-size: 11.5px; padding: 3px 8px; }
.formula-err { color: var(--danger); font-size: 12px; min-height: 16px; }
.formula-ok { color: var(--accent); font-size: 12px; min-height: 16px; }

.presence { display: flex; align-items: center; margin-right: 6px; }
.avatar {
  width: 26px; height: 26px; border-radius: 50%; color: #fff;
  display: inline-flex; align-items: center; justify-content: center;
  font-size: 10.5px; font-weight: 700; margin-left: -6px;
  border: 2px solid var(--bg-2); cursor: default;
}
.avatar:first-child { margin-left: 0; }

/* ===== Кнопки и поля ===== */
.btn {
  display: inline-flex; align-items: center; gap: 6px;
  background: var(--bg-3); color: var(--text);
  border: 1px solid var(--border-2); border-radius: 8px;
  padding: 6px 12px; font: inherit; font-size: 13px; cursor: pointer;
  white-space: nowrap; transition: background .12s, border-color .12s;
}
.btn:hover { background: var(--bg-hover); border-color: var(--accent-2); }
.btn:disabled { opacity: .5; cursor: default; pointer-events: none; }
.btn-primary { background: var(--accent); border-color: var(--accent); color: var(--accent-text); font-weight: 650; }
.btn-primary:hover { background: var(--accent-2); border-color: var(--accent-2); color: #fff; }
.btn-ghost { background: transparent; border-color: transparent; color: var(--muted); }
.btn-ghost:hover { color: var(--text); background: var(--bg-3); border-color: transparent; }
.btn-icon { padding: 6px 9px; }
.btn-danger { color: var(--danger); }

input, select, textarea {
  font: inherit; color: var(--text);
  background: var(--bg-3); border: 1px solid var(--border-2);
  border-radius: 8px; padding: 7px 10px; outline: none;
}
input:focus, select:focus, textarea:focus { border-color: var(--accent); }
input::placeholder { color: var(--muted); }

.muted { color: var(--muted); }
.mono { font-variant-numeric: tabular-nums; }

/* ===== Вкладки ===== */
.tabsbar {
  display: flex; align-items: center; gap: 4px; padding: 0 10px;
  background: var(--bg-2); border-bottom: 1px solid var(--border); flex: 0 0 auto;
  overflow-x: auto;
}
.tab {
  padding: 9px 16px 7px; cursor: pointer; white-space: nowrap;
  color: var(--muted); border-bottom: 2px solid transparent;
  font-size: 13.5px; user-select: none;
}
.tab:hover { color: var(--text); }
.tab.active { color: var(--accent); border-bottom-color: var(--accent); font-weight: 650; }

/* ===== Тулбар ===== */
.toolbar {
  display: flex; align-items: center; gap: 8px;
  padding: 8px 12px; flex: 0 0 auto;
  background: var(--bg); border-bottom: 1px solid var(--border);
  flex-wrap: wrap;
}
.search { width: 240px; padding: 6px 10px; }
.lock-actions { display: inline-flex; gap: 6px; align-items: center; }

.admin-only, .editor-only { display: none !important; }
body.role-admin .admin-only:not([hidden]) { display: inline-flex !important; }
body.role-admin .editor-only:not([hidden]),
body.role-editor .editor-only:not([hidden]) { display: inline-flex !important; }

/* ===== Вьюхи-страницы ===== */
#viewTable { display: flex; flex-direction: column; flex: 1 1 auto; min-height: 0; }
.page { flex: 1 1 auto; overflow: auto; padding: 18px 22px; }
.page h2 { margin: 0 0 4px; font-size: 17px; }
.page .page-sub { color: var(--muted); font-size: 12.5px; margin-bottom: 16px; }
.page-cols { display: flex; gap: 20px; align-items: flex-start; }

/* словари */
.dict-list { width: 260px; flex: 0 0 auto; display: flex; flex-direction: column; gap: 4px; }
.dict-item {
  text-align: left; background: var(--bg-2); border: 1px solid var(--border);
  color: var(--text); border-radius: 10px; padding: 9px 12px; cursor: pointer; font: inherit;
}
.dict-item:hover { border-color: var(--accent-2); }
.dict-item.active { border-color: var(--accent); background: var(--accent-soft); }
.dict-item .cnt { color: var(--muted); font-size: 11.5px; }
.dict-editor {
  flex: 1; max-width: 560px; background: var(--bg-2); border: 1px solid var(--border);
  border-radius: 12px; padding: 16px 18px;
}
.dict-editor textarea { width: 100%; min-height: 260px; resize: vertical; font-size: 13px; line-height: 1.5; }

/* столбцы: сетка строк и заголовок */
.colrows { display: flex; flex-direction: column; gap: 5px; max-width: 1150px; }
.colrow, .colhead {
  display: grid;
  grid-template-columns: 46px 26px minmax(180px, 1fr) 150px minmax(120px, 170px) 86px minmax(110px, 170px) 70px;
  gap: 8px; align-items: center; padding: 6px 10px;
}
.colrow > *, .colhead > * { min-width: 0; overflow: hidden; }
.colrow .updown, .colrow .num { overflow: visible; }
/* узкие экраны: элементы строки переносятся, заголовок сетки скрывается */
@media (max-width: 1080px) {
  .colhead { display: none; }
  .colrow { display: flex; flex-wrap: wrap; }
  .colrow .num { flex: 0 0 40px; }
  .colrow .updown { flex: 0 0 22px; }
  .colrow input.title { flex: 1 1 200px; min-width: 160px; }
  .colrow select { flex: 0 1 160px; min-width: 130px; }
  .colrow > div:not(.updown):not(.num) { flex: 0 1 auto; }
  .colrow input.width { flex: 0 0 80px; width: 80px; }
  .colrow .rule-info { flex: 1 1 100%; order: 9; }
}
.colrow {
  background: var(--bg-2); border: 1px solid var(--border); border-radius: 10px;
}
.colhead {
  color: var(--muted); font-size: 11px; font-weight: 650; padding-bottom: 0;
  text-transform: uppercase; letter-spacing: .04em;
}
.colrow.protected { opacity: .85; }
.colrow .num {
  text-align: center; font-weight: 700; color: var(--accent);
  font-variant-numeric: tabular-nums; font-size: 13px;
}
.colrow input.width { width: 100%; }
.colrow .updown { display: flex; flex-direction: column; gap: 1px; }
.colrow .updown .rowact { padding: 0 4px; font-size: 10px; line-height: 1.3; }
.colrow .fmt-cell { display: flex; flex-direction: column; gap: 3px; min-width: 0; }
.colrow .fmt-cell select { width: 100%; }
.rule-info {
  font-size: 11px; color: var(--muted); overflow: hidden; text-overflow: ellipsis;
  white-space: nowrap;
}
.rule-info b { color: var(--accent); font-weight: 650; }
.coladd { display: flex; gap: 8px; align-items: flex-end; flex-wrap: wrap; margin-bottom: 14px; }

/* редактор соответствий (mapping) */
.map-grid { display: flex; flex-direction: column; gap: 4px; max-height: 320px; overflow: auto; }
.map-row { display: grid; grid-template-columns: 1fr 110px; gap: 8px; align-items: center; }
.map-row.map-row-txt { grid-template-columns: minmax(120px, 38%) 1fr; }
.map-row .val { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-size: 13px; }
.map-row input { width: 100%; text-align: right; }
.map-row input.txt { text-align: left; }
.kind-badge {
  font-size: 10px; padding: 1px 7px; border-radius: 6px; font-weight: 700;
  background: var(--accent-soft); color: var(--accent); letter-spacing: .03em;
}
/* условия правила-галочки */
.cond-row {
  display: grid; grid-template-columns: minmax(0, 1fr) 30px;
  gap: 8px; align-items: center;
  border: 1px solid var(--border); border-radius: 10px; padding: 8px 10px; margin-bottom: 8px;
}
.cond-row > select { width: 100%; min-width: 0; }
.cond-row .cond-del { justify-self: end; }
.cond-values {
  grid-column: 1 / -1; display: flex; flex-wrap: wrap; gap: 6px;
  max-height: 150px; overflow: auto; min-width: 0;
}
.cond-values label { display: inline-flex; gap: 5px; align-items: center; font-size: 12.5px;
  background: var(--bg-3); border-radius: 7px; padding: 3px 8px; cursor: pointer; }
.cond-values input[type="text"], .cond-values input:not([type]) { flex: 1 1 100%; }

/* номер столбца в шапке таблицы */
th .col-num {
  display: block; font-size: 9.5px; color: var(--accent); font-weight: 700;
  letter-spacing: .04em; margin-bottom: 2px;
}
.grid td.system { color: var(--muted); }

/* сводные вкладки */
.tab .pivot-mark { color: var(--accent); font-weight: 800; margin-right: 5px; }
.pivot-info {
  font-size: 12px; color: var(--muted); background: var(--accent-soft);
  border: 1px solid var(--accent-2); border-radius: 8px; padding: 4px 10px;
}
.verify-grid { border: 1px solid var(--border); border-radius: 10px; overflow: auto; max-height: 300px; }
.verify-grid table { width: 100%; border-collapse: collapse; font-size: 12px; }
.verify-grid th, .verify-grid td { padding: 4px 8px; border-bottom: 1px solid var(--border); text-align: left; white-space: nowrap; }
.verify-grid th { position: sticky; top: 0; background: var(--bg-3); color: var(--muted); }
.verify-grid .ok { color: var(--accent); text-align: center; }

/* ===== Таблица ===== */
.table-wrap { flex: 1 1 auto; overflow: auto; position: relative; }
table.grid { border-collapse: separate; border-spacing: 0; table-layout: fixed; }
.grid th, .grid td {
  border-right: 1px solid var(--border);
  border-bottom: 1px solid var(--border);
  padding: 5px 8px; font-size: 12.8px;
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
  height: 34px; background: var(--row-bg, var(--bg));
}
.grid thead th {
  position: sticky; top: 0; z-index: 5;
  background: var(--bg-2); color: var(--muted);
  font-size: 11px; font-weight: 650; line-height: 1.2;
  white-space: pre-line; vertical-align: middle; text-align: left;
  border-bottom: 1px solid var(--border-2);
  height: 46px; user-select: none;
}
.grid tbody tr { --row-bg: var(--bg); }
.grid tbody tr:nth-child(even) { --row-bg: color-mix(in srgb, var(--bg) 84%, var(--bg-3)); }
.grid tbody tr:hover { --row-bg: var(--bg-hover); }

/* Закреплённые колонки */
.grid .c-sel { position: sticky; left: 0; z-index: 3; text-align: center; }
.grid .c-num { position: sticky; z-index: 3; font-variant-numeric: tabular-nums; color: var(--muted); }
.grid .c-act { position: sticky; right: 0; z-index: 3; border-left: 1px solid var(--border); text-align: right; padding: 2px 6px; }
.grid thead .c-sel, .grid thead .c-num, .grid thead .c-act { z-index: 7; background: var(--bg-2); }

.grid td.t-money, .grid td.t-number { text-align: right; font-variant-numeric: tabular-nums; }
.grid td.t-checkbox { text-align: center; }
.grid td.computed { color: var(--muted); font-style: normal; }
.grid td .pos { color: var(--danger); }
.grid td .neg { color: var(--accent); }

.rownum { display: inline-flex; align-items: center; gap: 5px; }
.locked-ico { font-size: 11px; }

tr.locked { --row-bg: color-mix(in srgb, var(--bg) 88%, var(--warn)); }
tr.locked td:not(.c-act):not(.c-sel) { opacity: .68; }

/* Состояния ячеек */
.grid td { position: relative; }
td.dirty { background: var(--warn-soft); box-shadow: inset 0 0 0 1px color-mix(in srgb, var(--warn) 45%, transparent); }
td.draft-other { font-style: italic; }
td.invalid { box-shadow: inset 0 0 0 1.5px var(--danger); background: var(--danger-soft); }
.cell-flag {
  position: absolute; top: 0; right: 0; z-index: 2;
  font-size: 8.5px; font-style: normal; line-height: 1; padding: 2px 4px;
  border-radius: 0 0 0 6px; color: #fff; font-weight: 700; letter-spacing: .02em;
  max-width: 90%; overflow: hidden; text-overflow: ellipsis; pointer-events: none;
}
@keyframes savedflash { 0% { background-color: var(--accent-soft); } 100% { background-color: transparent; } }
tr.flash td { animation: savedflash 1.4s ease-out; }

.cell-editor {
  position: absolute; inset: 0; width: 100%; height: 100%;
  border: 1.6px solid var(--accent); border-radius: 0;
  background: var(--bg-3); color: var(--text);
  padding: 4px 7px; font-size: 12.8px;
}
select.cell-editor { padding: 2px 4px; }

.grid input[type="checkbox"] {
  width: 15px; height: 15px; accent-color: var(--accent); cursor: pointer;
}

/* Чипы статусов */
.chip { display: inline-block; padding: 2px 9px; border-radius: 20px; font-size: 11.5px; font-weight: 600; }
.chip-green { background: var(--chip-green-bg); color: var(--chip-green-fg); }
.chip-amber { background: var(--chip-amber-bg); color: var(--chip-amber-fg); }
.chip-red { background: var(--chip-red-bg); color: var(--chip-red-fg); }
.chip-gray { background: var(--bg-3); color: var(--muted); }

/* Кнопки в строке */
.rowact {
  background: transparent; border: none; cursor: pointer; border-radius: 6px;
  padding: 3px 5px; font-size: 13px; color: var(--muted); line-height: 1;
}
.rowact:hover { background: var(--bg-3); color: var(--text); }
.rowact.save { color: var(--accent); }
.rowact.del:hover { color: var(--danger); }

.empty {
  position: absolute; inset: 46px 0 0 0; display: flex; align-items: center; justify-content: center;
  color: var(--muted); font-size: 14px; pointer-events: none;
}

/* ===== Модалки ===== */
.backdrop {
  position: fixed; inset: 0; background: rgba(2, 8, 6, .6); z-index: 50;
  display: flex; align-items: flex-start; justify-content: center;
  padding: 7vh 16px 16px; backdrop-filter: blur(2px);
}
.modal {
  background: var(--bg-2); border: 1px solid var(--border-2); border-radius: 14px;
  box-shadow: var(--shadow); width: 560px; max-width: 100%;
  max-height: 84vh; display: flex; flex-direction: column;
}
.modal.wide { width: 980px; }
.modal-head {
  display: flex; align-items: center; gap: 10px;
  padding: 14px 18px; border-bottom: 1px solid var(--border);
  font-weight: 700; font-size: 15px;
}
.modal-head .x { margin-left: auto; }
.modal-body { padding: 14px 18px; overflow: auto; }
.modal-foot { padding: 12px 18px; border-top: 1px solid var(--border); display: flex; gap: 8px; justify-content: flex-end; }

.field { display: flex; flex-direction: column; gap: 4px; margin-bottom: 12px; }
.field label { font-size: 12px; color: var(--muted); }
.form-row { display: flex; gap: 10px; }
.form-row .field { flex: 1; }

table.list { width: 100%; border-collapse: collapse; }
table.list th, table.list td { text-align: left; padding: 7px 8px; border-bottom: 1px solid var(--border); font-size: 12.8px; vertical-align: top; }
table.list th { color: var(--muted); font-size: 11.5px; font-weight: 650; position: sticky; top: -14px; background: var(--bg-2); }
.log-details { color: var(--muted); font-size: 12px; white-space: normal; }
.log-details b { color: var(--text); font-weight: 600; }
.badge { font-size: 11px; padding: 2px 8px; border-radius: 6px; font-weight: 650; }
.badge.admin { background: var(--chip-green-bg); color: var(--chip-green-fg); }
.badge.editor { background: rgba(64, 140, 255, .15); color: #6aa7ff; }
:root[data-theme="light"] .badge.editor { color: #2563c4; }
.badge.viewer { background: var(--bg-3); color: var(--muted); }

/* ===== Меню пользователя ===== */
.usermenu { position: relative; }
.usermenu-drop {
  position: absolute; right: 0; top: calc(100% + 6px); z-index: 60;
  background: var(--bg-2); border: 1px solid var(--border-2); border-radius: 12px;
  box-shadow: var(--shadow); min-width: 220px; padding: 6px; display: none;
}
.usermenu.open .usermenu-drop { display: block; }
.usermenu-drop .item {
  display: flex; width: 100%; text-align: left; background: none; border: none;
  color: var(--text); font: inherit; padding: 8px 10px; border-radius: 8px; cursor: pointer; gap: 8px; align-items: center;
}
.usermenu-drop .item:hover { background: var(--bg-3); }
.usermenu-head { padding: 8px 10px; border-bottom: 1px solid var(--border); margin-bottom: 6px; }

/* ===== Тосты ===== */
#toasts { position: fixed; top: 64px; right: 16px; z-index: 100; display: flex; flex-direction: column; gap: 8px; }
.toast {
  background: var(--bg-2); border: 1px solid var(--border-2); border-left: 3px solid var(--accent);
  border-radius: 10px; padding: 10px 14px; box-shadow: var(--shadow);
  max-width: 380px; font-size: 13px; animation: toastin .18s ease-out;
}
.toast.error { border-left-color: var(--danger); }
.toast.warn { border-left-color: var(--warn); }
@keyframes toastin { from { transform: translateX(20px); opacity: 0; } to { transform: none; opacity: 1; } }

.conn-status {
  position: fixed; bottom: 14px; left: 14px; z-index: 90;
  background: var(--warn-soft); border: 1px solid var(--warn); color: var(--warn);
  padding: 6px 12px; border-radius: 8px; font-size: 12.5px;
}

/* ===== Страница входа ===== */
.login-page { display: flex; align-items: center; justify-content: center; }
.bg-waves { position: fixed; inset: 0; z-index: 0; overflow: hidden; pointer-events: none; }
.bg-waves svg { position: absolute; width: 130%; left: -15%; bottom: -8%; opacity: .85; }
:root[data-theme="light"] .bg-waves { opacity: .25; }
.login-card {
  position: relative; z-index: 1; width: 380px; max-width: calc(100vw - 32px);
  background: color-mix(in srgb, var(--bg-2) 92%, transparent);
  border: 1px solid var(--border-2); border-radius: 16px;
  padding: 30px 28px 26px; box-shadow: var(--shadow); backdrop-filter: blur(8px);
}
.login-card .brand { margin-bottom: 6px; }
.login-card h1 { font-size: 17px; margin: 10px 0 18px; font-weight: 650; }
.login-card .btn-primary { width: 100%; justify-content: center; padding: 9px; font-size: 14px; margin-top: 4px; }
.login-error { color: var(--danger); font-size: 12.5px; min-height: 18px; margin: 8px 0 2px; }
