:root {
  --primary: #264796;
  --primary-dark: #1b3570;
  --accent: #00abca;
  --brand-grey: #c5c6c6;
  --bg: #f3f5f8;
  --surface: #ffffff;
  --text: #1c2430;
  --muted: #6b7686;
  --border: #e1e6ed;
  --danger: #c62828;
  --success: #2e7d32;
  --radius: 10px;
  --shadow: 0 1px 3px rgba(16, 24, 40, .08), 0 1px 2px rgba(16, 24, 40, .04);
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }

body {
  margin: 0;
  font-family: "Segoe UI", system-ui, -apple-system, Roboto, Arial, sans-serif;
  font-size: 14px;
  color: var(--text);
  background: var(--bg);
}

h1, h2, h3 { margin: 0; font-weight: 600; }

/* ---------- Бренд ---------- */
.brand { display: flex; align-items: center; gap: 12px; }
.brand-logo { width: 46px; height: 46px; border-radius: 50%; background: #fff; flex-shrink: 0; }
.brand-title { font-weight: 700; font-size: 16px; letter-spacing: .2px; }
.brand-sub { font-size: 12px; opacity: .75; text-transform: uppercase; letter-spacing: 1px; }

/* ---------- Вход ---------- */
.login-view {
  min-height: 100vh;
  display: flex; align-items: center; justify-content: center;
  background: linear-gradient(135deg, #172c5e 0%, #264796 55%, #00abca 140%);
  padding: 20px;
}
.login-card {
  width: 100%; max-width: 380px;
  background: var(--surface);
  border-radius: 14px;
  padding: 32px;
  box-shadow: 0 20px 50px rgba(0, 0, 0, .25);
  display: flex; flex-direction: column; gap: 16px;
}
.login-card h1 { font-size: 17px; margin-top: 4px; text-align: center; color: var(--muted); font-weight: 500; }
.login-logo { width: 150px; height: 150px; align-self: center; }
.login-brand { text-align: center; color: var(--primary); }
.login-brand .brand-title { font-size: 24px; }

/* ---------- Формы ---------- */
label { display: flex; flex-direction: column; gap: 6px; font-weight: 500; font-size: 13px; color: #3a4552; }
input, select, textarea {
  font: inherit;
  padding: 9px 11px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: #fff;
  color: var(--text);
}
input:focus, select:focus, textarea:focus { outline: 2px solid rgba(38, 71, 150, .25); border-color: var(--primary); }
label.checkbox { flex-direction: row; align-items: center; gap: 8px; }
.form-grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 14px; }
.form-grid .full { grid-column: 1 / -1; }
/* Поля форм не шире своей колонки: длинные варианты списков и значения не раздвигают окно */
label, .form-grid > * { min-width: 0; }
label > input:not([type=checkbox]):not([type=radio]), label > select, label > textarea { width: 100%; max-width: 100%; min-width: 0; }
select { max-width: 100%; text-overflow: ellipsis; }
.modal-dialog { overflow-x: hidden; }
.form-error {
  background: #fdecea; color: var(--danger);
  border-radius: 8px; padding: 9px 12px; font-size: 13px;
}
.form-actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 18px; }
.hint { color: var(--muted); font-size: 12px; font-weight: 400; }

/* ---------- Кнопки ---------- */
.btn {
  font: inherit; font-weight: 500;
  border: 1px solid transparent;
  border-radius: 8px;
  padding: 8px 14px;
  cursor: pointer;
  background: #eef1f5; color: var(--text);
  transition: background .15s;
}
.btn:hover { background: #e2e7ee; }
.btn:disabled { opacity: .6; cursor: default; }
.btn-primary { background: var(--primary); color: #fff; }
.btn-primary:hover { background: var(--primary-dark); }
.btn-danger { background: var(--danger); color: #fff; }
.btn-danger:hover { background: #a31f1f; }
.btn-ghost { background: transparent; border-color: var(--border); }
.btn-sm { padding: 5px 10px; font-size: 13px; }
.btn-block { width: 100%; padding: 11px; }

/* ---------- Каркас ---------- */
.layout { display: flex; min-height: 100vh; }
.sidebar {
  width: 240px; flex-shrink: 0;
  background: #172c5e; color: #fff;
  padding: 20px 14px;
  display: flex; flex-direction: column; gap: 28px;
}
.nav { display: flex; flex-direction: column; gap: 4px; }
.nav a {
  color: rgba(255, 255, 255, .78);
  text-decoration: none;
  padding: 10px 12px; border-radius: 8px;
}
.nav a:hover { background: rgba(255, 255, 255, .08); color: #fff; }
.nav a.active { background: var(--primary); color: #fff; box-shadow: inset 3px 0 0 var(--accent); }
.sidebar-footer { margin-top: auto; font-size: 12px; color: rgba(255, 255, 255, .45); text-align: center; }
.main { flex: 1; min-width: 0; display: flex; flex-direction: column; }
.topbar {
  display: flex; align-items: center; justify-content: space-between;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  padding: 14px 28px;
}
.topbar h2 { font-size: 18px; }
.userbox { display: flex; align-items: center; gap: 14px; }
.userbox-text { text-align: right; }
.userbox-name { font-weight: 600; }
.userbox-role { color: var(--muted); font-size: 12px; }
.content { padding: 28px; display: flex; flex-direction: column; gap: 24px; }

/* ---------- Карточки ---------- */
.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 20px;
}
.card-title { font-size: 15px; margin-bottom: 14px; }
.card-header { display: flex; align-items: center; justify-content: space-between; gap: 12px; margin-bottom: 14px; flex-wrap: wrap; }
.card-header .card-title { margin: 0; }

.stats { display: grid; grid-template-columns: repeat(auto-fit, minmax(190px, 1fr)); gap: 16px; }
.stat { border-left: 4px solid var(--primary); }
.stat:nth-child(even) { border-left-color: var(--accent); }
.stat-label { color: var(--muted); font-size: 13px; }
.stat-value { font-size: 30px; font-weight: 700; margin-top: 6px; }

.grid-2 { display: grid; grid-template-columns: 1fr 1.4fr; gap: 24px; }

.welcome { display: flex; justify-content: space-between; align-items: center; gap: 16px; flex-wrap: wrap; }
.welcome h3 { font-size: 20px; }
.welcome p { margin: 4px 0 0; color: var(--muted); }

/* ---------- Быстрые действия ---------- */
.quick-actions { display: grid; grid-template-columns: repeat(auto-fit, minmax(220px, 1fr)); gap: 16px; }
.quick-action {
  display: flex; align-items: center; gap: 14px;
  text-align: left; cursor: pointer; font: inherit; color: inherit;
  transition: border-color .15s, transform .15s;
}
.quick-action:hover { border-color: var(--primary); transform: translateY(-1px); }
.quick-action-icon {
  width: 44px; height: 44px; border-radius: 10px; flex-shrink: 0;
  display: flex; align-items: center; justify-content: center;
  font-size: 22px; font-weight: 600;
  background: #e9eef8; color: var(--primary);
}
.quick-action-icon.accent { background: #dff4f8; color: #007f97; }
.quick-action-title { font-weight: 600; }
.quick-action-sub { color: var(--muted); font-size: 12px; margin-top: 2px; }

/* ---------- Разбивка по ролям ---------- */
.role-bars { display: flex; flex-direction: column; gap: 12px; }
.role-bar-head { display: flex; justify-content: space-between; margin-bottom: 4px; }
.role-bar-track { height: 8px; background: #edf0f4; border-radius: 4px; overflow: hidden; }
.role-bar-fill { height: 100%; background: var(--primary); border-radius: 4px; }

/* ---------- Таблицы ---------- */
.table-wrap { overflow-x: auto; }
table { width: 100%; border-collapse: collapse; }
th, td { text-align: left; padding: 10px 12px; border-bottom: 1px solid var(--border); vertical-align: middle; }
th { font-size: 12px; text-transform: uppercase; letter-spacing: .4px; color: var(--muted); font-weight: 600; background: #fafbfc; }
tr:last-child td { border-bottom: none; }
td.actions { white-space: nowrap; text-align: right; }
td.actions .btn + .btn { margin-left: 4px; }
.muted { color: var(--muted); }
.empty { text-align: center; color: var(--muted); padding: 24px; }

.badge {
  display: inline-block;
  padding: 3px 9px; border-radius: 999px;
  font-size: 12px; font-weight: 500;
  background: #e8f0fa; color: var(--primary);
}
.badge-admin { background: #fff3cd; color: #8a6100; }
.badge-system { background: #eef1f5; color: var(--muted); }
.badge-general_director { background: #e7e3fb; color: #4b2fb4; }
.badge-deputy_general_director { background: #dff4f8; color: #007f97; }
.badge-department_director { background: #e3f6ee; color: #1d7a4a; }
.badge-accounting { background: #fbe9e3; color: #a3401a; }
.status-on { color: var(--success); }
.status-off { color: var(--danger); }

.filters { display: flex; gap: 8px; flex-wrap: wrap; }
.filters input { min-width: 240px; }

/* ---------- Профиль ---------- */
.profile-list { display: grid; grid-template-columns: 180px 1fr; gap: 10px 16px; margin: 0; }
.profile-list dt { color: var(--muted); }
.profile-list dd { margin: 0; font-weight: 500; }

/* ---------- Модальное окно ---------- */
.modal {
  position: fixed; inset: 0;
  background: rgba(13, 27, 42, .5);
  display: flex; align-items: center; justify-content: center;
  padding: 20px; z-index: 50;
}
.modal-dialog {
  background: var(--surface);
  border-radius: 12px;
  width: 100%; max-width: 560px;
  max-height: 90vh; overflow-y: auto;
  padding: 22px 24px;
  box-shadow: 0 20px 50px rgba(0, 0, 0, .3);
}
.modal-header { display: flex; justify-content: space-between; align-items: center; margin-bottom: 18px; }
.modal-header h3 { font-size: 17px; }
.modal-close { background: none; border: none; font-size: 24px; cursor: pointer; color: var(--muted); line-height: 1; }
.modal-back { margin-right: 14px; }
.modal-header h3 { flex: 1; }

/* Форма-страница: вся рабочая область справа от меню */
.modal.modal-page { inset: 0 0 0 256px; padding: 0; display: block; overflow-y: auto; background: var(--bg); z-index: 45; }
.modal-page .modal-dialog { max-width: none; max-height: none; min-height: 100%; overflow: visible; border-radius: 0; box-shadow: none;
  background: transparent; padding: 22px 32px 40px; }
.modal-page .modal-header { margin-bottom: 20px; }
.modal-page .modal-header h3 { font-size: 22px; }
.modal-page .modal-close { display: none; }
.modal-page #modal-body > form { background: var(--surface); border-radius: var(--radius, 14px); box-shadow: var(--shadow); padding: 26px 28px; }
.modal-page .form-grid { grid-template-columns: repeat(auto-fill, minmax(min(100%, 300px), 1fr)); gap: 16px 20px; }
.modal-page textarea { min-height: 110px; }
@media (max-width: 900px) {
  .modal.modal-page { inset: 0; }
  .modal-page .modal-dialog { padding: 16px; }
  .modal-page #modal-body > form { padding: 18px 16px; }
}

/* ---------- Уведомления ---------- */
.toast {
  position: fixed; right: 24px; bottom: 24px;
  background: #1c2430; color: #fff;
  padding: 12px 18px; border-radius: 8px;
  box-shadow: 0 8px 24px rgba(0, 0, 0, .2);
  z-index: 60;
}
.toast.error { background: var(--danger); }

@media (max-width: 900px) {
  .layout { flex-direction: column; }
  .sidebar-footer { display: none; }
  .sidebar { width: auto; flex-direction: row; align-items: center; justify-content: space-between; padding: 12px 16px; }
  .nav { flex-direction: row; }
  .grid-2 { grid-template-columns: 1fr; }
  .form-grid { grid-template-columns: minmax(0, 1fr); }
  .content { padding: 16px; }
}

textarea { resize: vertical; min-height: 72px; }
a.btn, a.quick-action { text-decoration: none; }

/* ---------- NIPI-002: группы меню ---------- */
.nav-group-toggle {
  width: 100%;
  display: flex; align-items: center; justify-content: space-between;
  font: inherit; text-align: left; cursor: pointer;
  background: none; border: none; border-radius: 8px;
  color: rgba(255, 255, 255, .78);
  padding: 10px 12px;
}
.nav-group-toggle:hover { background: rgba(255, 255, 255, .08); color: #fff; }
.nav-group-toggle::after { content: '›'; font-size: 18px; line-height: 1; transition: transform .15s; }
.nav-group.open .nav-group-toggle::after { transform: rotate(90deg); }
.nav-sub { display: none; flex-direction: column; gap: 2px; margin: 2px 0 4px 12px; padding-left: 8px; border-left: 1px solid rgba(255, 255, 255, .15); }
.nav-group.open .nav-sub { display: flex; }
.nav-sub a { padding: 8px 12px; font-size: 13px; }
.nav-divider { height: 1px; background: rgba(255, 255, 255, .12); margin: 8px 4px; }

/* ---------- NIPI-002: права роли ---------- */
.perm-fieldset { border: 1px solid var(--border); border-radius: 10px; padding: 12px 16px 6px; margin: 16px 0 0; }
.perm-fieldset legend { font-weight: 600; padding: 0 6px; }
.perm-fieldset[disabled] { opacity: .75; }
.perm-group { margin-bottom: 10px; }
.perm-group-title { font-size: 12px; text-transform: uppercase; letter-spacing: .4px; color: var(--muted); margin: 6px 0 4px; }
label.switch { flex-direction: row; align-items: center; gap: 10px; font-weight: 400; padding: 5px 0; cursor: pointer; }
label.switch input { position: absolute; opacity: 0; width: 1px; height: 1px; }
.switch-track {
  position: relative; flex-shrink: 0;
  width: 36px; height: 20px; border-radius: 999px;
  background: #cfd5de; transition: background .15s;
}
.switch-track::after {
  content: ''; position: absolute; top: 2px; left: 2px;
  width: 16px; height: 16px; border-radius: 50%;
  background: #fff; box-shadow: 0 1px 2px rgba(0, 0, 0, .2); transition: transform .15s;
}
label.switch input:checked + .switch-track { background: var(--primary); }
label.switch input:checked + .switch-track::after { transform: translateX(16px); }
label.switch input:focus-visible + .switch-track { outline: 2px solid rgba(38, 71, 150, .35); outline-offset: 2px; }

/* ---------- NIPI-002: разделы, контрагенты, настройки ---------- */
.placeholder { text-align: center; padding: 48px 24px; }
.placeholder h3 { font-size: 20px; margin: 12px 0 8px; }
.placeholder p { margin: 6px auto; max-width: 560px; }
.placeholder-badge { display: inline-block; padding: 4px 12px; border-radius: 999px; background: #dff4f8; color: #007f97; font-size: 12px; font-weight: 600; }
a.link { color: var(--primary); text-decoration: none; }
a.link:hover { text-decoration: underline; }
.badge-cp-customer { background: #e9eef8; color: var(--primary); }
.badge-cp-supplier { background: #e3f6ee; color: #1d7a4a; }
.badge-cp-contractor { background: #fbe9e3; color: #a3401a; }
.badge-cp-partner { background: #e7e3fb; color: #4b2fb4; }
.badge-cp-other { background: #eef1f5; color: var(--muted); }
.badge-dt-incoming { background: #e9eef8; color: var(--primary); }
.badge-dt-outgoing { background: #dff4f8; color: #007f97; }
.badge-dt-internal { background: #eef1f5; color: #3a4552; }
.badge-dt-contract { background: #e7e3fb; color: #4b2fb4; }
.badge-dt-financial { background: #e3f6ee; color: #1d7a4a; }
.badge-dt-other { background: #eef1f5; color: var(--muted); }
code { font-family: Consolas, 'Courier New', monospace; font-size: 12px; background: #eef1f5; padding: 2px 6px; border-radius: 4px; }
#settings-form { display: flex; flex-direction: column; gap: 24px; }
.settings-footer { display: flex; align-items: center; justify-content: space-between; gap: 16px; flex-wrap: wrap; }
.settings-footer .form-error { flex: 1; }

@media (max-width: 900px) {
  .nav { flex-wrap: wrap; }
  .nav-divider { display: none; }
  .nav-sub { margin-left: 0; }
}
