:root {
  --bg: #f5f7fb;
  --surface: #ffffff;
  --surface-soft: #f8fafc;
  --border: #e6eaf2;
  --text: #172033;
  --muted: #7a8699;
  --primary: #2563eb;
  --primary-dark: #1d4ed8;
  --primary-soft: #eaf1ff;
  --success: #16a34a;
  --success-soft: #e8f8ee;
  --danger: #dc2626;
  --danger-soft: #fdecec;
  --warning: #d97706;
  --warning-soft: #fff5e6;
  --radius: 16px;
  --radius-sm: 10px;
  --shadow: 0 1px 2px rgba(18, 38, 63, .04), 0 8px 24px rgba(18, 38, 63, .05);
  --sidebar-w: 246px;
}

* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: "Inter", "Segoe UI", system-ui, -apple-system, sans-serif;
  font-size: 14px;
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
}
h1, h2, h3, p { margin: 0; }
h1 { font-size: 24px; font-weight: 650; letter-spacing: -.4px; }
h2 { font-size: 17px; font-weight: 650; }
button, input, select, textarea { font: inherit; color: inherit; }
button { cursor: pointer; }
.hidden { display: none !important; }
.muted { color: var(--muted); font-size: 13px; }
.w-full { width: 100%; }

.app { display: flex; min-height: 100vh; }
.sidebar {
  width: var(--sidebar-w);
  flex: 0 0 var(--sidebar-w);
  background: var(--surface);
  border-right: 1px solid var(--border);
  padding: 22px 16px 18px;
  display: flex;
  flex-direction: column;
  position: sticky;
  top: 0;
  height: 100vh;
}
.brand { display: flex; align-items: center; gap: 11px; padding: 0 8px 22px; }
.brand-mark {
  width: 42px; height: 42px; border-radius: 12px; flex: 0 0 auto;
  background: linear-gradient(135deg, #2563eb, #4f8cf7);
  color: #fff; font-weight: 750; font-size: 13px; letter-spacing: -.3px;
  display: grid; place-items: center; box-shadow: 0 6px 14px rgba(37, 99, 235, .25);
}
.brand strong { display: block; font-size: 15px; letter-spacing: -.2px; }
.brand span { display: block; color: var(--muted); font-size: 11.5px; margin-top: 1px; }
.nav { display: flex; flex-direction: column; gap: 4px; }
.nav-link {
  display: flex; align-items: center; gap: 11px;
  padding: 11px 13px; border-radius: var(--radius-sm);
  color: #536178; text-decoration: none; font-weight: 550;
  transition: background .15s, color .15s, transform .15s;
}
.nav-link:hover { background: var(--surface-soft); color: var(--text); transform: translateX(2px); }
.nav-link.active { background: var(--primary-soft); color: var(--primary-dark); font-weight: 650; }
.nav-icon { width: 20px; text-align: center; font-size: 16px; opacity: .85; }
.sidebar-footer { margin-top: auto; border-top: 1px solid var(--border); padding-top: 14px; }
.connection { display: flex; align-items: center; gap: 8px; color: var(--muted); font-size: 12px; padding: 0 6px 12px; }
.dot { width: 8px; height: 8px; border-radius: 50%; background: #b7c0cf; transition: background .2s; }
.dot.on { background: var(--success); box-shadow: 0 0 0 3px rgba(22,163,74,.12); }
.dot.off { background: var(--danger); }

.main { flex: 1; min-width: 0; }
.topbar {
  height: 86px; background: rgba(255,255,255,.88); backdrop-filter: blur(12px);
  border-bottom: 1px solid var(--border); display: flex; align-items: center;
  justify-content: space-between; gap: 20px; padding: 0 32px; position: sticky; top: 0; z-index: 20;
}
.eyebrow { color: var(--primary); font-size: 11px; font-weight: 700; text-transform: uppercase; letter-spacing: .9px; margin-bottom: 3px; }
.topbar-actions { display: flex; align-items: center; gap: 12px; }
.clock { color: var(--muted); font-variant-numeric: tabular-nums; font-size: 13px; min-width: 74px; text-align: right; }
.content { padding: 26px 32px 48px; max-width: 1680px; margin: 0 auto; }

.btn {
  border: 1px solid transparent; border-radius: var(--radius-sm);
  padding: 10px 16px; font-weight: 600; font-size: 13px;
  display: inline-flex; align-items: center; justify-content: center; gap: 7px;
  transition: transform .12s, box-shadow .15s, background .15s, border-color .15s;
  white-space: nowrap;
}
.btn:hover { transform: translateY(-1px); }
.btn:active { transform: translateY(0); }
.btn-primary { background: var(--primary); color: #fff; box-shadow: 0 5px 14px rgba(37,99,235,.22); }
.btn-primary:hover { background: var(--primary-dark); }
.btn-outline { background: var(--surface); color: #43506a; border-color: var(--border); }
.btn-outline:hover { border-color: #bcc9dd; background: var(--surface-soft); }
.btn-ghost { background: transparent; color: var(--muted); }
.btn-ghost:hover { background: var(--surface-soft); }
.btn-sm { padding: 7px 11px; font-size: 12px; }
.icon-btn { border: none; background: transparent; color: var(--muted); font-size: 17px; padding: 4px 8px; border-radius: 8px; }
.icon-btn:hover { background: var(--surface-soft); color: var(--text); }
.spin-icon { font-size: 17px; line-height: 1; }

.card {
  background: var(--surface); border: 1px solid var(--border);
  border-radius: var(--radius); box-shadow: var(--shadow);
}
.stats-grid { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 14px; margin-bottom: 20px; }
.stat-card { padding: 18px 20px; display: flex; align-items: flex-start; gap: 14px; }
.stat-icon { width: 42px; height: 42px; border-radius: 12px; display: grid; place-items: center; font-size: 19px; background: var(--primary-soft); color: var(--primary); flex: 0 0 auto; }
.stat-card.green .stat-icon { background: var(--success-soft); color: var(--success); }
.stat-card.red .stat-icon { background: var(--danger-soft); color: var(--danger); }
.stat-card.amber .stat-icon { background: var(--warning-soft); color: var(--warning); }
.stat-label { color: var(--muted); font-size: 12.5px; }
.stat-value { font-size: 24px; font-weight: 700; letter-spacing: -.6px; line-height: 1.25; font-variant-numeric: tabular-nums; }
.stat-value.small { font-size: 16px; letter-spacing: -.2px; padding-top: 4px; }
.stat-hint { color: var(--muted); font-size: 11.5px; }

.progress-card { padding: 16px 20px; margin-bottom: 18px; border-color: #cfe0ff; }
.progress-head { display: flex; justify-content: space-between; font-size: 13px; margin-bottom: 10px; }
.progress-head span { color: var(--muted); font-variant-numeric: tabular-nums; }
.progress-track { height: 7px; background: #edf2fb; border-radius: 99px; overflow: hidden; }
.progress-fill { height: 100%; width: 0; background: linear-gradient(90deg, #2563eb, #60a5fa); border-radius: 99px; transition: width .3s ease; }
.progress-log { margin-top: 10px; display: flex; flex-wrap: wrap; gap: 6px; }
.progress-chip { font-size: 11.5px; padding: 3px 8px; border-radius: 99px; background: var(--surface-soft); color: var(--muted); }
.progress-chip.ok { color: var(--success); background: var(--success-soft); }
.progress-chip.bad { color: var(--danger); background: var(--danger-soft); }

.table-card { overflow: hidden; }
.table-toolbar { display: flex; align-items: center; justify-content: space-between; gap: 12px; padding: 16px 18px; border-bottom: 1px solid var(--border); flex-wrap: wrap; }
.table-toolbar.wrap { gap: 10px; justify-content: flex-start; }
.toolbar-left, .toolbar-right { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; }
.search-box { display: flex; align-items: center; gap: 7px; border: 1px solid var(--border); background: var(--surface); border-radius: var(--radius-sm); padding: 0 12px; height: 38px; min-width: 250px; color: var(--muted); }
.search-box:focus-within { border-color: #a9c3f7; box-shadow: 0 0 0 3px rgba(37,99,235,.08); }
.search-box input { border: none; outline: none; background: transparent; width: 100%; font-size: 13px; }
.input, .select { height: 38px; border: 1px solid var(--border); background: var(--surface); border-radius: var(--radius-sm); padding: 0 11px; outline: none; font-size: 13px; transition: border-color .15s, box-shadow .15s; }
.input:focus, .select:focus { border-color: #a9c3f7; box-shadow: 0 0 0 3px rgba(37,99,235,.08); }
textarea.input { height: auto; padding: 10px 11px; resize: vertical; }
.select { min-width: 150px; }
.template-input { min-width: 210px; }
.date-input { min-width: 135px; }
.checkbox { display: inline-flex; align-items: center; gap: 7px; color: var(--muted); font-size: 12.5px; cursor: pointer; user-select: none; }
.checkbox input, .data-table input[type=checkbox] { width: 15px; height: 15px; accent-color: var(--primary); cursor: pointer; }

.table-wrap { overflow-x: auto; }
.data-table { width: 100%; border-collapse: collapse; min-width: 880px; }
.data-table th {
  text-align: left; font-size: 11.5px; font-weight: 650; letter-spacing: .3px;
  text-transform: uppercase; color: var(--muted); padding: 13px 16px;
  background: var(--surface-soft); border-bottom: 1px solid var(--border); white-space: nowrap;
}
.data-table td { padding: 14px 16px; border-bottom: 1px solid #f0f2f7; vertical-align: middle; }
.data-table tbody tr { transition: background .12s; }
.data-table tbody tr:hover { background: #fafbff; }
.data-table tbody tr.selected { background: #f0f6ff; }
.data-table tbody tr:last-child td { border-bottom: none; }
.col-check { width: 42px; }
.col-actions { text-align: right; }
.fio-cell { font-size: 15px; font-weight: 650; letter-spacing: -.15px; }
.fio-cell small { display: block; color: var(--muted); font-size: 11.5px; font-weight: 450; margin-top: 2px; }
.id-cell { color: #536178; font-variant-numeric: tabular-nums; font-size: 13px; }
.code-cell { display: flex; align-items: center; gap: 8px; }
.code-value { font-size: 19px; font-weight: 750; letter-spacing: 1.1px; color: var(--primary-dark); font-variant-numeric: tabular-nums; }
.code-value.empty { color: #b7c0cf; font-weight: 500; font-size: 15px; letter-spacing: 0; }
.code-value.expired { color: var(--danger); background: var(--danger-soft); padding: 2px 7px; border-radius: 7px; animation: expired-blink 1.1s ease-in-out infinite; }
.expired-text { color: var(--danger); font-weight: 650; }
@keyframes expired-blink {
  0%, 100% { opacity: 1; box-shadow: 0 0 0 0 rgba(220, 38, 38, 0); }
  50% { opacity: .35; box-shadow: 0 0 0 3px rgba(220, 38, 38, .16); }
}
@media (prefers-reduced-motion: reduce) {
  .code-value.expired { animation: none; }
}
.time-cell { color: var(--muted); font-variant-numeric: tabular-nums; }
.time-cell small { display: block; font-size: 11px; }
.badge { display: inline-flex; align-items: center; gap: 6px; padding: 4px 10px; border-radius: 99px; font-size: 11.5px; font-weight: 600; background: var(--surface-soft); color: var(--muted); white-space: nowrap; }
.badge::before { content: ""; width: 6px; height: 6px; border-radius: 50%; background: currentColor; }
.badge.ok { background: var(--success-soft); color: var(--success); }
.badge.needs_auth, .badge.error, .badge.mismatch, .badge.no_access { background: var(--danger-soft); color: var(--danger); }
.badge.disabled { background: #f0f2f7; color: #7a8699; }
.badge.checking { background: var(--warning-soft); color: var(--warning); }
.error-text { color: var(--danger); font-size: 11px; display: block; max-width: 230px; }
.row-actions { display: flex; justify-content: flex-end; gap: 6px; flex-wrap: wrap; }
.mini-btn { border: 1px solid var(--border); background: var(--surface); color: #43506a; border-radius: 8px; padding: 6px 9px; font-size: 11.5px; font-weight: 600; transition: .15s; }
.mini-btn:hover { border-color: #bcc9dd; color: var(--primary); background: var(--primary-soft); }
.mini-btn.copy { color: var(--primary); border-color: #cfe0ff; background: var(--primary-soft); }
.mini-btn.danger:hover { color: var(--danger); background: var(--danger-soft); border-color: #f4c7c7; }
.sortable { cursor: pointer; user-select: none; }
.sortable:hover { color: var(--primary); }
.sort-arrow { opacity: .5; font-size: 12px; }
.empty { text-align: center; padding: 44px 16px !important; color: var(--muted); }
.table-footer { display: flex; justify-content: space-between; align-items: center; padding: 13px 18px; border-top: 1px solid var(--border); color: var(--muted); font-size: 12px; }
.pager { display: flex; align-items: center; gap: 10px; }

.progress-log-modal { max-height: 220px; overflow-y: auto; }
.settings-grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 16px; align-items: start; }
.settings-card { padding: 22px; display: flex; flex-direction: column; gap: 12px; }
.settings-card h2 { margin-bottom: 2px; }
.settings-card label:not(.switch-row) { display: flex; flex-direction: column; gap: 6px; font-size: 13px; font-weight: 550; }
.hint { font-size: 11.5px; color: var(--muted); margin-top: -7px; }
.info-box { background: var(--surface-soft); border: 1px solid var(--border); border-radius: var(--radius-sm); padding: 10px 12px; font-size: 12.5px; color: var(--muted); }
.switch-row { display: flex; justify-content: space-between; align-items: center; gap: 16px; padding: 10px 12px; background: var(--surface-soft); border-radius: var(--radius-sm); cursor: pointer; }
.switch-row strong { display: block; font-size: 13px; }
.switch-row small { display: block; color: var(--muted); font-size: 11.5px; }
.switch-row input { width: 18px; height: 18px; accent-color: var(--primary); }
.backup-list { display: flex; flex-direction: column; gap: 6px; font-size: 12px; color: var(--muted); }
.admin-row { display: flex; align-items: center; justify-content: space-between; gap: 10px; padding: 8px 10px; background: var(--surface-soft); border-radius: var(--radius-sm); color: #2d3a52; font-weight: 600; }
#admin-add-form { display: flex; flex-direction: column; gap: 10px; margin-top: 14px; }

.modal { position: fixed; inset: 0; z-index: 100; display: grid; place-items: center; padding: 20px; }
.modal-backdrop { position: absolute; inset: 0; background: rgba(17, 28, 48, .45); backdrop-filter: blur(3px); }
.modal-dialog { position: relative; background: var(--surface); border-radius: 18px; box-shadow: 0 24px 70px rgba(14,26,48,.28); width: min(560px, 100%); padding: 24px; max-height: 90vh; overflow-y: auto; animation: pop .16s ease-out; }
.modal-wide { width: min(720px, 100%); }
@keyframes pop { from { transform: translateY(8px) scale(.98); opacity: 0; } }
.modal-header { display: flex; justify-content: space-between; align-items: center; margin-bottom: 18px; }
.modal-header h2 { font-size: 18px; }
#user-form { display: flex; flex-direction: column; gap: 14px; }
#user-form label, #password-form label { display: flex; flex-direction: column; gap: 6px; font-size: 13px; font-weight: 550; }
.form-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.form-subtitle { font-size: 13px; font-weight: 650; color: var(--text); padding-top: 4px; border-top: 1px solid var(--border); margin-top: 2px; }
.check-row { flex-direction: row !important; align-items: center; gap: 8px !important; font-weight: 550; }
.check-row input { width: 16px; height: 16px; accent-color: var(--primary); }
.modal-footer { display: flex; justify-content: flex-end; gap: 10px; margin-top: 8px; }
.auth-progress { display: flex; flex-direction: column; align-items: flex-start; gap: 12px; }
.spinner { width: 28px; height: 28px; border: 3px solid #dbe7ff; border-top-color: var(--primary); border-radius: 50%; animation: spin .8s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

.toasts { position: fixed; right: 22px; bottom: 22px; z-index: 200; display: flex; flex-direction: column; gap: 10px; align-items: flex-end; }
.toast { background: #172033; color: #fff; border-radius: 12px; padding: 12px 16px; box-shadow: 0 12px 32px rgba(12,22,40,.25); font-size: 13px; font-weight: 550; display: flex; align-items: center; gap: 9px; animation: slide .2s ease-out; max-width: 360px; }
.toast.success { background: #166534; }
.toast.error { background: #b91c1c; }
@keyframes slide { from { transform: translateX(14px); opacity: 0; } }

.auth-page { display: grid; place-items: center; min-height: 100vh; background: radial-gradient(circle at 20% 15%, #e7efff, transparent 45%), var(--bg); }
.auth-card { width: min(420px, calc(100% - 32px)); padding: 30px; display: flex; flex-direction: column; gap: 14px; }
.auth-card h1 { font-size: 22px; }
.auth-card form { display: flex; flex-direction: column; gap: 14px; }
.auth-card form label { display: flex; flex-direction: column; gap: 6px; font-size: 13px; font-weight: 550; }
.auth-brand { padding: 0 0 10px; }
.error { color: var(--danger); font-size: 12.5px; }

body.compact .data-table td { padding: 8px 12px; }
body.compact .data-table th { padding: 9px 12px; }

@media (max-width: 1200px) {
  .stats-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .settings-grid { grid-template-columns: 1fr; }
}
@media (max-width: 900px) {
  :root { --sidebar-w: 68px; }
  .sidebar { padding: 18px 10px; }
  .brand > div:last-child, .nav-link span:not(.nav-icon), .sidebar-footer { display: none; }
  .brand { padding: 0 4px 18px; justify-content: center; }
  .nav-link { justify-content: center; padding: 12px 0; }
  .topbar { padding: 0 18px; }
  .content { padding: 20px 18px 40px; }
  .stats-grid { grid-template-columns: 1fr 1fr; }
}
@media (max-width: 640px) {
  .topbar { height: auto; padding: 14px 16px; align-items: flex-start; flex-direction: column; }
  .topbar-actions { flex-wrap: wrap; }
  .stats-grid { grid-template-columns: 1fr; }
  .form-grid { grid-template-columns: 1fr; }
}
