:root {
  --primary: #f39200; --primary-dark: #d97f00;
  --link: #1d6fd6; --danger: #dc2626;
  --bg: #f5f6f8; --card: #fff; --text: #1f2937; --muted: #6b7280; --line: #e5e7eb; --line-strong: #d1d5db;
  --side-bg: #1c2638; --side-text: #d6dce6; --side-muted: #8a96aa; --side-hover: #263349; --side-active: #2d3c56;
  --topbar-h: 56px;
  --topbar-bg: #fff; --input-bg: #fff; --hover: #f3f4f6; --row-hover: #fafbfc; --th-bg: #f9fafb;
  --focus: #fcd38a; --filter-icon: #3b82f6; --soft: #f1f5f9; --note: #fff8e8;
  --side-active-text: #fff; --on-primary: #fff;
}
* { box-sizing: border-box; }
html, body { margin: 0; }
body { font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif; font-size: 15px; background: var(--bg); color: var(--text); }
a { color: var(--link); text-decoration: none; }
a:hover { text-decoration: underline; }
.icon { width: 16px; height: 16px; flex: none; vertical-align: -3px; }

/* ---------- Шапка ---------- */
.topbar { position: sticky; top: 0; z-index: 20; height: var(--topbar-h); display: flex; align-items: center; gap: 16px;
          padding: 0 20px; background: var(--topbar-bg); border-bottom: 1px solid var(--line); }
.brand { display: flex; align-items: center; gap: 14px; color: var(--text); min-width: 0; }
.brand:hover { text-decoration: none; }
.logo { display: inline-flex; align-items: center; font-weight: 800; font-size: 1.25rem; letter-spacing: .12em; color: var(--side-bg); }
.logo img { max-height: 40px; max-width: 180px; display: block; }
.site-title { font-weight: 700; font-size: 1.05rem; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.topbar-user { margin-left: auto; display: flex; align-items: center; gap: 10px; white-space: nowrap; }
.topbar-user form { margin: 0; }
.topbar-user .who { color: var(--muted); margin-right: 4px; }
.topbar-user .who strong { color: var(--text); }
.badge.role { background: #fff4dc; color: #a15c00; border: 1px solid #fde3a7; }

/* ---------- Каркас и левое меню ---------- */
body.app .layout { display: grid; grid-template-columns: 236px 1fr; min-height: calc(100vh - var(--topbar-h));
                   background: linear-gradient(90deg, var(--side-bg) 236px, transparent 236px); }
.sidebar { position: sticky; top: var(--topbar-h); height: calc(100vh - var(--topbar-h)); overflow-y: auto;
           display: flex; flex-direction: column; background: var(--side-bg); color: var(--side-text); padding: 14px 10px; }
.menu { display: flex; flex-direction: column; gap: 2px; }
.menu a { display: flex; align-items: center; gap: 12px; padding: 9px 12px; border-radius: 6px; color: var(--side-text);
          border-left: 3px solid transparent; font-size: .95rem; }
.menu a .icon { width: 18px; height: 18px; color: var(--side-muted); }
.menu a:hover { background: var(--side-hover); color: var(--side-active-text); text-decoration: none; }
.menu a.active { background: var(--side-active); color: var(--side-active-text); border-left-color: var(--primary); }
.menu a.active .icon { color: var(--side-active-text); }
.menu-title { margin: 16px 12px 6px; font-size: .72rem; font-weight: 700; text-transform: uppercase; letter-spacing: .1em; color: var(--side-muted); }
.menu-title:first-child { margin-top: 4px; }
.usage { margin-top: auto; padding: 12px 12px 4px; font-size: .8rem; color: var(--side-muted); }
main { min-width: 0; padding: 22px 26px; }
body.plain main { max-width: 1040px; margin: 0 auto; }

/* ---------- Заголовки ---------- */
h1 { font-size: 1.35rem; margin: 4px 0 14px; }
h2 { font-size: 1.05rem; margin: 20px 0 10px; }
.page-title { display: flex; align-items: baseline; flex-wrap: wrap; gap: 8px; margin-bottom: 14px; }
.page-title h1 { margin: 0; }
.page-title .spacer { flex: 1; }
.page-head { display: flex; flex-wrap: wrap; align-items: flex-start; gap: 12px; justify-content: space-between; }
.sub { margin: -8px 0 12px; }

/* ---------- Кнопки ---------- */
.btn, button { display: inline-flex; align-items: center; justify-content: center; gap: 7px; height: 36px; padding: 0 14px;
               border-radius: 6px; border: 1px solid var(--primary); background: var(--primary); color: var(--on-primary);
               font: inherit; font-weight: 600; cursor: pointer; white-space: nowrap; text-decoration: none; }
.btn:hover, button:hover { background: var(--primary-dark); border-color: var(--primary-dark); text-decoration: none; }
.btn.outline, .btn.secondary { background: var(--input-bg); color: var(--text); border-color: var(--line-strong); font-weight: 500; }
.btn.outline:hover, .btn.secondary:hover { background: var(--hover); border-color: var(--muted); }
.btn.disabled { opacity: .5; pointer-events: none; }
.btn.danger-btn { background: var(--danger); border-color: var(--danger); }
.btn.sm { height: 32px; padding: 0 10px; font-size: .88rem; }
.btn.xs { height: 28px; padding: 0 9px; font-size: .82rem; }
button.link { height: auto; padding: 0; border: 0; background: none; color: var(--link); font-weight: 400; }
button.link:hover { background: none; text-decoration: underline; }
.icon-btn { display: inline-flex; align-items: center; justify-content: center; width: 28px; height: 28px; padding: 0;
            border: 0; border-radius: 6px; background: none; color: var(--muted); cursor: pointer; }
.icon-btn:hover { background: var(--hover); color: var(--text); }
.icon-btn.danger:hover { background: #fee2e2; color: var(--danger); }
.danger { color: var(--danger) !important; }

/* ---------- Панель над таблицей ---------- */
.toolbar { display: flex; flex-wrap: wrap; align-items: center; gap: 8px; margin-bottom: 14px; }
.toolbar-check { margin-left: 6px; color: var(--muted); }
.toolbar .shown { margin-left: auto; color: var(--muted); font-size: .9rem; }

/* ---------- Таблица ---------- */
.table-wrap { overflow-x: auto; background: var(--card); border: 1px solid var(--line); border-radius: 8px; }
table { width: 100%; border-collapse: collapse; background: var(--card); }
.table-wrap > table { border: 0; }
th, td { text-align: left; padding: 10px; border-bottom: 1px solid var(--line); vertical-align: middle; }
th { font-weight: 600; font-size: .88rem; white-space: nowrap; }
table:not(.grid) { border: 1px solid var(--line); border-radius: 8px; }
table:not(.grid) th { background: var(--th-bg); }
table.grid td { white-space: nowrap; }
table.grid tbody tr[data-href] { cursor: pointer; }
table.grid tbody tr:hover td { background: var(--row-hover); }
table.grid tbody tr:last-child td { border-bottom: 0; }
table.grid th.sortable { padding: 0; }
th.sortable a { display: flex; align-items: flex-end; gap: 4px; padding: 11px 10px 9px; color: var(--text); white-space: normal; max-width: 160px; line-height: 1.25; }
table.grid thead tr:first-child th { vertical-align: bottom; }
th.sortable a:hover { text-decoration: none; color: var(--link); }
th.sortable .arrow { font-size: .7em; color: var(--text); }
.filter-row th { padding: 0 6px 8px; font-weight: 400; border-bottom: 1px solid var(--line-strong); }
.filter-row select, .filter-row input { height: 30px; padding: 3px 8px; font-size: .85rem; min-width: 80px; }
.search-input { position: relative; display: block; }
.search-input .icon { position: absolute; left: 8px; top: 8px; width: 14px; height: 14px; color: var(--filter-icon); }
.search-input input { padding-left: 28px; }
.row-muted td { color: var(--muted); }
td.cell-expired { background: #fde8e8 !important; color: var(--danger); font-weight: 700; }
td.cell-expiring { background: #fff6dc !important; color: #a15c00; font-weight: 600; }
.files { display: flex; align-items: center; gap: 4px; }
.files form { margin: 0; }
.file-link { max-width: 130px; overflow: hidden; text-overflow: ellipsis; display: inline-block; vertical-align: middle; }
.more { font-size: .82rem; color: var(--muted); }
td.col-actions, th.col-actions { width: 1%; }
td.col-actions { white-space: nowrap; }
td.col-actions form { display: inline; margin: 0; }
td.empty { text-align: center; padding: 26px; }
label.icon-btn, label.btn { margin: 0; }

/* ---------- Формы и карточки ---------- */
input, textarea, select { width: 100%; height: 36px; padding: 6px 10px; border: 1px solid var(--line-strong); border-radius: 6px;
                          font: inherit; background: var(--input-bg); color: var(--text); }
textarea { height: auto; }
input:focus, textarea:focus, select:focus { outline: 2px solid var(--focus); border-color: var(--primary); }
input[type=checkbox], input[type=radio] { width: auto; height: auto; accent-color: var(--primary); }
input[type=file] { border: 0; padding: 0; height: auto; }
label { display: grid; gap: 4px; font-weight: 500; align-content: start; }
label.check { display: inline-flex; gap: 8px; align-items: center; font-weight: 400; }
fieldset { border: 1px solid var(--line); border-radius: 8px; padding: 12px; display: grid; gap: 12px; margin: 0; }
legend { font-weight: 600; padding: 0 4px; }
.card { display: grid; gap: 14px; background: var(--card); padding: 18px; border-radius: 8px; border: 1px solid var(--line); margin-bottom: 16px; }
.card.narrow { max-width: 420px; margin: 40px auto; }
.card h1, .card h2 { margin: 0; }
.row { display: flex; gap: 12px; align-items: center; flex-wrap: wrap; }
.row form { margin: 0; }
.grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.form-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(260px, 1fr)); gap: 14px; align-items: stretch; }
.form-grid .wide { grid-column: 1 / -1; }
.form-grid .date-row { grid-column: 1 / -1; display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 14px; align-items: stretch; }
/* Поля ввода прижаты ко дну ячейки, поэтому в одной строке они всегда на одном уровне,
   даже если подпись занимает одну или две строки */
.form-grid label,
.form-grid .date-row label { align-content: end; }
.note { background: var(--note); padding: 8px 10px; border-radius: 6px; margin: 0; }
.upload { display: flex; flex-wrap: wrap; gap: 12px; align-items: center; }
.upload input[type=file] { flex: 1 1 200px; }
.search { display: flex; gap: 8px; flex: 1 1 320px; margin: 0; }
table.perm { border: 0; }
table.perm td { border: 0; padding: 6px 0; }
table.perm td:first-child { padding-right: 12px; }
.details { display: grid; grid-template-columns: minmax(160px, 260px) 1fr; gap: 8px 16px; margin: 0; }
.details dt { color: var(--muted); }
.details dd { margin: 0; }
.actions { display: flex; gap: 12px; justify-content: flex-end; white-space: nowrap; }
.actions form { margin: 0; }
.muted, .empty { color: var(--muted); }
.req { color: var(--danger); }
small { font-size: .85em; }
.nowrap { white-space: nowrap; }
.pre { white-space: pre-line; }
.break { word-break: break-word; }

/* ---------- Метки ---------- */
.badge { display: inline-block; font-size: .76rem; padding: 2px 8px; border-radius: 999px; margin: 1px 2px 1px 0; white-space: nowrap; font-weight: 600; }
.badge.full, .badge.st-active { background: #dcfce7; color: #166534; }
.badge.read { background: #e0f2fe; color: #075985; }
.badge.none { background: var(--soft); color: var(--muted); }
.badge.expired, .badge.st-expired { background: #fee2e2; color: #991b1b; }
.badge.expiring { background: #fef3c7; color: #92400e; }
.badge.st-suspended { background: #fef3c7; color: #92400e; }
.badge.st-annulled { background: #e5e7eb; color: #374151; }

/* ---------- Прочее ---------- */
.tiles { list-style: none; padding: 0; margin: 0 0 8px; display: grid; grid-template-columns: repeat(auto-fill, minmax(260px, 1fr)); gap: 12px; }
.tiles a { display: grid; gap: 6px; justify-items: start; padding: 16px; background: var(--card); border: 1px solid var(--line);
           border-left: 3px solid var(--primary); border-radius: 8px; color: var(--text); height: 100%; }
.tiles a:hover { border-color: var(--primary); text-decoration: none; }
.tiles a strong { display: flex; align-items: center; gap: 8px; font-size: 1.1rem; }
.flash { padding: 10px 12px; border-radius: 6px; margin-bottom: 12px; }
.flash.success { background: #dcfce7; }
.flash.error { background: #fee2e2; }
.logo-preview { padding: 16px; border: 1px dashed var(--line-strong); border-radius: 8px; display: flex; justify-content: center; background: var(--input-bg); }
.logo-preview img { max-height: 80px; max-width: 100%; }
.card.errors { border-color: #fecaca; background: #fef2f2; }
.card.errors ul, ul.help { margin: 0; padding-left: 20px; display: grid; gap: 4px; }
.chip { display: inline-block; padding: 2px 8px; margin: 2px 4px 2px 0; border-radius: 6px; background: var(--soft); font-size: .85rem; }

/* ---------- Телефоны ---------- */
@media (max-width: 860px) {
  .topbar { height: auto; flex-wrap: wrap; padding: 8px 12px; position: static; }
  .topbar-user { margin-left: 0; flex-wrap: wrap; }
  .topbar-user .who { width: 100%; }
  .site-title { white-space: normal; }
  body.app .layout { display: block; background: none; }
  .sidebar { position: static; height: auto; padding: 8px; }
  .menu { flex-direction: row; flex-wrap: wrap; }
  .menu-title { display: none; }
  .usage { display: none; }
  main { padding: 14px; }
  .toolbar .shown { margin-left: 0; width: 100%; }
  .hide-sm { display: none; }
  .grid2 { grid-template-columns: 1fr; }
  .details { grid-template-columns: 1fr; gap: 2px; }
  .details dd { margin-bottom: 8px; }
  table.perm td { display: block; }
}

/* ================= Темы оформления ================= */
/* 2. «Светлая» — светлое меню, синий акцент */
[data-theme="light"] {
  --primary: #2563eb; --primary-dark: #1d4ed8; --link: #2563eb; --focus: #bfdbfe; --filter-icon: #2563eb;
  --bg: #f4f6fb; --side-bg: #ffffff; --side-text: #334155; --side-muted: #94a3b8; --side-hover: #eef2ff;
  --side-active: #e0e7ff; --side-active-text: #1e3a8a; --note: #eff6ff;
}
[data-theme="light"] .sidebar { border-right: 1px solid var(--line); }
[data-theme="light"] .logo { color: #1e3a8a; }

/* 3. «Тёмная» — тёмный фон, бирюзовый акцент */
[data-theme="dark"] {
  --primary: #14b8a6; --primary-dark: #0d9488; --on-primary: #04201d; --link: #5eead4; --danger: #f87171;
  --bg: #0f141b; --card: #161d27; --text: #e5e9f0; --muted: #94a3b8; --line: #263142; --line-strong: #334155;
  --topbar-bg: #121821; --input-bg: #0f141b; --hover: #1f2937; --row-hover: #1a2330; --th-bg: #1a2330;
  --focus: #134e4a; --filter-icon: #5eead4; --soft: #1f2937; --note: #12302d;
  --side-bg: #0b1016; --side-text: #cbd5e1; --side-muted: #64748b; --side-hover: #161d27; --side-active: #1b2633;
}
[data-theme="dark"] .logo { color: #e5e9f0; }
[data-theme="dark"] td.cell-expired { background: #3b1414 !important; color: #fca5a5; }
[data-theme="dark"] td.cell-expiring { background: #3a2a0c !important; color: #fcd34d; }
[data-theme="dark"] .flash.success { background: #12352a; }
[data-theme="dark"] .flash.error { background: #3b1414; }
[data-theme="dark"] .badge.role { background: #12302d; color: #5eead4; border-color: #134e4a; }

/* 4. «Индустриальная» — графит, красный акцент, строгие углы */
[data-theme="steel"] {
  --primary: #c62828; --primary-dark: #a31f1f; --link: #b71c1c; --focus: #fecaca; --filter-icon: #6b7280;
  --bg: #eceef1; --text: #22262b; --line: #d7dade; --line-strong: #b9bec5; --th-bg: #e4e6ea;
  --side-bg: #2b2f36; --side-text: #e1e3e6; --side-muted: #8b919a; --side-hover: #353a42; --side-active: #3d434c;
  --topbar-bg: #22262b; --note: #fdecea;
}
[data-theme="steel"] .topbar { border-bottom: 3px solid var(--primary); }
[data-theme="steel"] .topbar .logo, [data-theme="steel"] .site-title { color: #fff; }
[data-theme="steel"] .topbar-user .who, [data-theme="steel"] .topbar-user .who strong { color: #e1e3e6; }
[data-theme="steel"] .topbar .btn.outline { background: transparent; color: #e1e3e6; border-color: #555b64; }
[data-theme="steel"] .topbar .btn.outline:hover { background: #353a42; }
[data-theme="steel"] .btn, [data-theme="steel"] button, [data-theme="steel"] .card,
[data-theme="steel"] .table-wrap, [data-theme="steel"] input, [data-theme="steel"] select { border-radius: 2px; }
[data-theme="steel"] .menu a { border-radius: 0; }
[data-theme="steel"] table.grid thead tr:first-child th { background: var(--th-bg); text-transform: uppercase; font-size: .76rem; letter-spacing: .04em; }

/* Выбор темы в настройках */
.theme-list { display: grid; gap: 8px; }
.theme-option { display: flex; justify-content: flex-start; padding: 8px 10px; border: 1px solid var(--line); border-radius: 6px; cursor: pointer; }
.swatch { display: inline-block; width: 44px; height: 18px; border-radius: 4px; border: 1px solid var(--line-strong); }
.swatch-orange { background: linear-gradient(90deg, #1c2638 50%, #f39200 50%); }
.swatch-light { background: linear-gradient(90deg, #ffffff 50%, #2563eb 50%); }
.swatch-dark { background: linear-gradient(90deg, #0b1016 50%, #14b8a6 50%); }
.swatch-steel { background: linear-gradient(90deg, #2b2f36 50%, #c62828 50%); }
.icon-btn.danger:not(:hover) { color: var(--muted) !important; }

/* ---------- Папки ---------- */
.crumbs { display: flex; flex-wrap: wrap; align-items: center; gap: 6px; margin: -4px 0 12px; color: var(--muted); }
.crumbs .icon { color: var(--primary); }
.crumbs .sep { color: var(--line-strong); }
.inline-form { display: flex; gap: 6px; margin: 0; }
.inline-form input { width: 220px; }
.toolbar form { margin: 0; }
.name-cell { display: flex; align-items: center; gap: 10px; }
.name-cell .folder-icon { color: var(--primary); width: 20px; height: 20px; }
.name-cell .file-icon { color: var(--muted); width: 18px; height: 18px; }
.files-table tbody tr:not([data-href]) { cursor: default; }
.files-table td.col-actions form { display: inline; margin: 0; }
.dropzone.dragging { outline: 3px dashed var(--primary); outline-offset: -3px; }

.checks { display: flex; flex-wrap: wrap; gap: 6px 16px; min-height: 36px; align-items: center; }

.col-check { width: 36px; padding-right: 0 !important; }
.col-check input { width: 16px; height: 16px; cursor: pointer; }
tr.selected td { background: var(--note) !important; }
.btn.danger-outline:not(:disabled) { color: var(--danger); border-color: #f3b4b4; }
.btn:disabled { opacity: .5; cursor: default; }

/* ---------- Мастер «Создать сертификат» ---------- */
.stepper { list-style: none; display: flex; flex-wrap: wrap; gap: 6px 22px; padding: 0; margin: 0 0 18px; color: var(--muted); }
.stepper li { display: flex; align-items: center; gap: 8px; }
.stepper span { display: inline-flex; align-items: center; justify-content: center; width: 26px; height: 26px; border-radius: 50%;
                border: 2px solid var(--line-strong); font-weight: 700; font-size: .85rem; }
.stepper li.current { color: var(--text); font-weight: 600; }
.stepper li.current span { border-color: var(--primary); background: var(--primary); color: var(--on-primary); }
.stepper li.done span { border-color: var(--primary); color: var(--primary); }
.choice-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(200px, 1fr)); gap: 14px; max-width: 640px; }
.choice-list { display: grid; gap: 12px; max-width: 760px; margin-bottom: 14px; }
.choice { display: grid; gap: 8px; padding: 16px 18px; background: var(--card); border: 1px solid var(--line);
          border-left: 4px solid var(--primary); border-radius: 8px; color: var(--text); }
.choice:hover { border-color: var(--primary); text-decoration: none; box-shadow: 0 2px 10px rgba(0,0,0,.06); }
.choice.big { justify-items: center; padding: 26px; }
.choice.big strong { font-size: 2rem; }
.choice-head { display: flex; flex-wrap: wrap; gap: 6px 12px; align-items: baseline; }
.ranges { margin: 0; padding: 0; list-style: none; font-family: ui-monospace, "Cascadia Mono", Consolas, monospace; font-size: .9rem; display: grid; gap: 2px; }
.variant-summary { gap: 8px; }
.test-grid { width: 100%; border-collapse: collapse; }
.test-grid th, .test-grid td { border: 1px solid var(--line-strong); padding: 5px 8px; text-align: left; }
.test-grid th { background: var(--note); }
.test-grid td:not(:first-child), .test-grid th:not(:first-child) { text-align: center; width: 100px; }
.test-grid input[type=radio] { width: 16px; height: 16px; cursor: pointer; }
.check-row { display: inline-flex; align-items: center; gap: 8px; cursor: pointer; }
.check-row input[type=checkbox] { width: 16px; height: 16px; margin: 0; cursor: pointer; }
.card.narrow-left { max-width: 520px; }
.dates-preview { margin: 0; padding: 10px 12px; border-radius: 6px; background: var(--note); }
.form-actions { display: grid; gap: 10px; width: 100%; box-sizing: border-box; }
.form-actions .btn-full, .form-actions button.btn-full { width: 100%; justify-content: center; }
.cert-note { max-width: 760px; width: 100%; box-sizing: border-box; }
