:root {
  --bg: #f1f4f9; --surface: #ffffff; --border: #dde3ec; --text: #1e293b; --muted: #64748b;
  --accent: #2563eb; --accent-dark: #1e3a8a; --accent-soft: #dbe6fe; --stripe: #f8fafc; --heading: #eef2f7;
  --danger: #dc2626; --ok: #059669; --ok-dark: #064e3b;
  --field: #fff7d6; --field-edge: #eadfae; --field-error: #fdd9d9;
  --radius: 10px;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Arial, sans-serif;
  font-size: 14px; color: var(--text); background: var(--bg);
}
* { box-sizing: border-box; }
body { margin: 0; background: var(--bg); color: var(--text); }
h1, h2, h3 { margin: 0; }
.muted { color: var(--muted); font-size: 12px; }
.hidden { display: none !important; }

/* ---------- кнопки: объёмные, при нажатии «вдавливаются» ---------- */
.btn {
  font: inherit; font-size: 13px; cursor: pointer; white-space: nowrap;
  padding: 7px 14px; border-radius: 7px; color: var(--text);
  border: 1px solid #8f9bb0; background: linear-gradient(#ffffff, #e8edf4);
  box-shadow: inset 0 1px 0 #fff, 0 2px 0 #b4bfcf, 0 3px 6px rgba(15, 23, 42, .12);
  transition: transform .05s, box-shadow .05s, filter .1s;
}
.btn:hover { filter: brightness(1.03); }
.btn:active:not(:disabled) { transform: translateY(2px); box-shadow: inset 0 2px 4px rgba(15, 23, 42, .25), 0 0 0 #b4bfcf; }
.btn:disabled { cursor: default; opacity: .5; box-shadow: none; }
.btn:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
.btn.accent { color: #fff; font-weight: 600; border-color: var(--accent-dark);
  background: linear-gradient(#4f86f7, #2563eb 55%, #1d4ed8);
  box-shadow: inset 0 1px 0 #93b4fa, 0 2px 0 var(--accent-dark), 0 3px 8px rgba(37, 99, 235, .35); }
.btn.print { color: #fff; font-weight: 600; border-color: var(--ok-dark);
  background: linear-gradient(#10b981, #059669 55%, #047857);
  box-shadow: inset 0 1px 0 #6ee7b7, 0 2px 0 var(--ok-dark), 0 3px 8px rgba(5, 150, 105, .35); }
.btn.danger { color: var(--danger); }
.btn.small { padding: 5px 10px; font-size: 12px; }
a.btn { text-decoration: none; display: inline-block; }

/* ---------- каркас ---------- */
.topbar {
  display: flex; align-items: center; gap: 10px; padding: 12px 20px;
  background: var(--surface); border-bottom: 1px solid var(--border); position: sticky; top: 0; z-index: 10;
}
.topbar h1 { font-size: 17px; }
.topbar .spacer { flex: 1; }
.card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); }
.layout { display: grid; grid-template-columns: minmax(360px, 520px) 1fr; gap: 14px; padding: 14px; align-items: start; }
@media (max-width: 1000px) { .layout { grid-template-columns: 1fr; } }

/* ---------- список ---------- */
.sidebar { padding: 14px; display: flex; flex-direction: column; min-height: 0; }
.sidebar .head { display: flex; align-items: baseline; gap: 10px; margin-bottom: 10px; }
.search { width: 100%; padding: 8px 10px; border: 1px solid var(--border); border-radius: 7px; font: inherit; margin-bottom: 10px; }
.search:focus, .inp:focus { outline: none; border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft); }
.scroll { overflow: auto; max-height: calc(100vh - 230px); border-radius: 6px; }
table.grid { width: 100%; border-collapse: collapse; font-size: 13px; }
table.grid th {
  position: sticky; top: 0; background: var(--heading); color: var(--muted); font-size: 11px; font-weight: 700;
  text-align: left; padding: 7px 8px; border-bottom: 1px solid var(--border); white-space: nowrap; cursor: default;
}
table.grid th.sortable { cursor: pointer; user-select: none; }
table.grid td { padding: 6px 8px; border-bottom: 1px solid #edf1f6; vertical-align: top; }
table.grid tbody tr:nth-child(even) td { background: var(--stripe); }
table.grid tbody tr { cursor: default; }
table.grid tbody tr.sel td { background: var(--accent-soft); }
table.grid tbody tr:hover td { background: #eef3fd; }
table.grid .num { text-align: right; font-variant-numeric: tabular-nums; white-space: nowrap; }
table.grid .ek { color: var(--ok); font-weight: 600; }
table.grid .nowrap { white-space: nowrap; }
table.grid .empty td { color: var(--muted); text-align: center; padding: 18px; }

/* ---------- карточка декларации ---------- */
.summary { padding: 16px 20px 10px; }
.summary .row { display: flex; align-items: center; gap: 12px; flex-wrap: wrap; }
.summary h2 { font-size: 20px; }
.badge { color: #fff; font-size: 11px; font-weight: 700; padding: 3px 8px; border-radius: 5px; background: var(--accent); }
.badge.ek { background: var(--ok); }
.summary .actions { margin-left: auto; display: flex; gap: 8px; flex-wrap: wrap; }
.summary .sub { margin: 4px 0 10px; }
.metrics { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); gap: 10px 18px; border-top: 1px solid var(--border); padding-top: 10px; }
@media (max-width: 1300px) { .metrics { grid-template-columns: repeat(2, minmax(0, 1fr)); } }
.metric .cap { font-size: 10px; letter-spacing: .04em; text-transform: uppercase; color: var(--muted); }
.metric .val { font-weight: 700; font-size: 14px; margin-top: 2px; overflow-wrap: anywhere; }
.metric .sub2 { font-size: 12px; color: var(--muted); overflow-wrap: anywhere; }
.stale { color: #b45309; }

/* ---------- вкладки ---------- */
.tabs { display: flex; gap: 2px; margin: 14px 0 0; }
.tab { font: inherit; font-weight: 600; font-size: 13px; padding: 8px 16px; border: 1px solid var(--border); border-bottom: none;
  border-radius: 8px 8px 0 0; background: transparent; color: var(--muted); cursor: pointer; }
.tab.active { background: var(--surface); color: var(--accent); }
.panel { padding: 14px; border-top-left-radius: 0; }
.panel h3 { font-size: 15px; margin-bottom: 10px; }
.panel h3 .muted { font-weight: 400; margin-left: 6px; }
.panel .sec + .sec { margin-top: 18px; }
.group td { background: var(--heading) !important; font-weight: 700; }

/* ---------- пользователь, вход, диалоги ---------- */
.user-bar { position: relative; display: flex; align-items: center; gap: 8px; }
.user-btn { display: flex; align-items: center; gap: 8px; font: inherit; font-size: 13px; background: transparent;
  border: 1px solid var(--border); border-radius: 20px; padding: 3px 10px 3px 3px; cursor: pointer; color: var(--text); }
.user-btn:hover { background: var(--stripe); }
.avatar { width: 26px; height: 26px; border-radius: 50%; background: var(--accent); color: #fff; font-weight: 700;
  display: grid; place-items: center; font-size: 12px; }
.user-name { max-width: 180px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.role-badge { font-size: 10px; font-weight: 700; padding: 2px 7px; border-radius: 10px; background: var(--heading); color: var(--muted); }
.role-badge.admin { background: var(--accent-soft); color: var(--accent); }
.user-menu { position: absolute; right: 0; top: calc(100% + 6px); min-width: 190px; background: var(--surface);
  border: 1px solid var(--border); border-radius: 8px; box-shadow: 0 10px 30px rgba(15, 23, 42, .18); padding: 4px; z-index: 30; }
.user-menu a, .user-menu button { display: block; width: 100%; text-align: left; font: inherit; font-size: 13px; padding: 8px 12px;
  border: 0; background: transparent; color: var(--text); text-decoration: none; border-radius: 6px; cursor: pointer; }
.user-menu a:hover, .user-menu button:hover { background: var(--stripe); }
.readonly-note { font-size: 12px; color: var(--muted); border: 1px dashed var(--border); border-radius: 6px; padding: 4px 10px; }
.inp { font: inherit; width: 100%; padding: 8px 10px; border: 1px solid var(--border); border-radius: 7px;
  background: var(--surface); color: var(--text); }
.dlg { border: 1px solid var(--border); border-radius: 12px; padding: 22px 24px; width: min(420px, 92vw);
  background: var(--surface); color: var(--text); box-shadow: 0 20px 60px rgba(0, 0, 0, .3); }
.dlg::backdrop { background: rgba(15, 23, 42, .45); }
.dlg h2 { font-size: 18px; margin-bottom: 14px; }
.dlg label, .login-card label { display: grid; gap: 5px; font-size: 13px; font-weight: 600; margin-bottom: 12px; }
.dlg-actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 8px; }
.form-error { color: var(--danger); font-size: 13px; min-height: 18px; margin-bottom: 4px; }
.login-view { min-height: 100vh; display: flex; flex-direction: column; }
.login-page { flex: 1; display: grid; place-items: center; padding: 24px 14px; }
.login-card { width: min(760px, 100%); overflow: hidden; }
.form-card-head, .users-section-head { padding: 15px 20px; border-bottom: 1px solid var(--border); }
.form-card-head h2 { font-size: 20px; }
.form-card-head p { margin: 4px 0 0; }
.form-card-body { padding: 18px 20px 20px; }
.login-content { display: grid; grid-template-columns: minmax(0, 1fr) minmax(190px, 230px); gap: 24px; align-items: start; }
.login-fields { min-width: 0; }
.login-users { border: 0; margin: 0 0 12px; padding: 0; min-width: 0; }
.login-users legend { font-size: 13px; font-weight: 600; margin-bottom: 5px; }
.login-users > .muted, .login-users > .form-error { display: block; margin-top: 7px; }
.login-user-details { display: flex; align-items: center; gap: 10px; padding: 10px 12px; margin-top: 8px;
  border: 1px solid var(--border); border-radius: 7px; background: var(--stripe); }
.login-user-info { display: grid; gap: 2px; flex: 1; min-width: 0; overflow-wrap: anywhere; }
.login-user-info .muted { margin: 0; }
.login-user-details .role-badge { flex: none; }
.login-card .btn { width: 100%; margin-top: 6px; padding: 10px; }
.login-video { display: grid; gap: 8px; align-content: start; }
.login-video-title { color: var(--muted); font-size: 13px; }
.login-video video { display: block; width: 100%; aspect-ratio: 720 / 1276; object-fit: contain;
  background: #000; border-radius: 8px; }
.login-video-hint { color: var(--muted); font-size: 12px; line-height: 1.4; }
@media (max-width: 620px) {
  .login-content { grid-template-columns: minmax(0, 1fr); gap: 16px; }
  .login-video { justify-items: center; }
  .login-video video, .login-video-hint { width: min(100%, 220px); }
}
@media (max-width: 760px) {
  .topbar { flex-wrap: wrap; }
  #userBar { flex: 1 1 100%; }
  .user-bar { flex-wrap: wrap; justify-content: space-between; }
  .user-name { max-width: 120px; }
}
.users-page { max-width: 1200px; margin: 0 auto; padding: 14px; display: grid; gap: 14px; }
.users-page .card { overflow: hidden; }
.users-section-head { display: flex; align-items: baseline; gap: 10px; }
.users-section-head h2 { font-size: 17px; }
.users-section-body { padding: 16px 20px 18px; }
.add-user { display: grid; grid-template-columns: minmax(120px, 1fr) minmax(180px, 1.4fr) minmax(120px, 1fr) minmax(150px, 1fr) auto; gap: 12px; align-items: end; }
.add-user label { display: grid; gap: 5px; min-width: 0; font-size: 13px; font-weight: 600; }
.add-user .btn { min-height: 35px; }
.users-table-wrap { overflow: auto; }
.users-page table.grid { min-width: 900px; }
.users-page table.grid th, .users-page table.grid td { padding: 9px 10px; }
.users-page table.grid td { vertical-align: middle; }
@media (max-width: 900px) {
  .add-user { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .add-user .btn { grid-column: 2; }
}
@media (max-width: 560px) {
  .users-page { padding: 10px; gap: 10px; }
  .users-section-head { padding: 13px 14px; }
  .users-section-body { padding: 14px; }
  .add-user { grid-template-columns: 1fr; }
  .add-user .btn { grid-column: 1; }
}
table.grid td .inp { padding: 4px 6px; font-size: 13px; }
table.grid td.actions { white-space: nowrap; text-align: right; }
.role-legend { display: flex; gap: 10px 18px; flex-wrap: wrap; font-size: 12px; color: var(--muted);
  border-top: 1px solid var(--border); margin-top: 16px; padding-top: 12px; }

/* ---------- уведомления ---------- */
.toast { position: fixed; right: 20px; bottom: 20px; max-width: 460px; padding: 12px 16px; border-radius: 8px;
  background: #0f172a; color: #fff; box-shadow: 0 10px 30px rgba(0,0,0,.25); white-space: pre-line; z-index: 100; font-size: 13px; }
.toast.error { background: #7f1d1d; }
.placeholder { padding: 40px 20px; text-align: center; color: var(--muted); }

/* ---------- редактор на бланке ---------- */
.editor-bar { flex-wrap: wrap; }
.pager { display: flex; align-items: center; gap: 6px; }
.pager span { min-width: 110px; text-align: center; font-weight: 600; }
.statusbar { position: sticky; bottom: 0; background: var(--surface); border-top: 1px solid var(--border);
  padding: 8px 20px; display: flex; gap: 16px; align-items: center; z-index: 10; }
.statusbar .msg { flex: 1; min-width: 0; }
.statusbar .err { color: var(--danger); font-weight: 600; }
.sheet-area { padding: 18px; display: flex; justify-content: center; }
.sheet-wrap { position: relative; background: #fff; box-shadow: 0 2px 14px rgba(15, 23, 42, .18); border-radius: 2px; }
.sheet { position: absolute; left: 0; top: 0; transform-origin: 0 0; }
.sheet svg { position: absolute; left: 0; top: 0; overflow: visible; }
.lbl { position: absolute; color: #005200; font-family: Arial, sans-serif; line-height: 1.1; overflow: hidden; padding: 0.5px 1px; }
.lbl.nowrap { white-space: nowrap; }
.fld { position: absolute; font-family: Arial, sans-serif; font-size: 7px; line-height: 1.15; color: #000;
  background: var(--field); border: 0.4px solid var(--field-edge); padding: 0 1.5px; margin: 0; border-radius: 0; }
.fld:focus { outline: none; border-color: var(--accent); background: #fff; box-shadow: 0 0 0 0.8px var(--accent); }
.fld.err { background: var(--field-error); border-color: var(--danger); }
.fld[readonly] { background: #fbfbf6; border-color: #ece8d8; cursor: default; }
.fld[readonly]:focus { box-shadow: none; border-color: #cfc8a8; }
.fld.c { text-align: center; } .fld.r { text-align: right; } .fld.b { font-weight: 700; }
textarea.fld { resize: none; overflow: hidden; }
/* стрелка списка значений съедает ширину узких граф — список открывается стрелкой вниз или двойным щелчком */
.fld::-webkit-calendar-picker-indicator { display: none !important; }
.fld::-webkit-list-button { display: none !important; }
.ro { position: absolute; font-family: Arial, sans-serif; font-size: 7px; color: #475569; line-height: 1.15;
  white-space: pre-line; overflow: hidden; padding: 1px 1.5px; }
.ro.c { text-align: center; } .ro.r { text-align: right; } .ro.b { font-weight: 700; }
.del-item { position: absolute; font: 700 5.5px Arial, sans-serif; color: var(--danger); background: #fff;
  border: 0.4px solid #f3b4b4; border-radius: 2px; padding: 0.5px 3px; cursor: pointer; }
.del-item:hover { background: #fee2e2; }
label.check { display: flex; align-items: center; gap: 6px; font-size: 13px; }

@media (prefers-color-scheme: dark) {
  :root { --bg: #0f172a; --surface: #111827; --border: #283244; --text: #e5e7eb; --muted: #94a3b8;
    --stripe: #141d2e; --heading: #1b2436; --accent-soft: #1e3a8a55; }
  .btn:not(.accent):not(.print):not(.danger) { color: #1e293b; }
  table.grid tbody tr:hover td { background: #1d2a44; }
  .search { background: #0b1220; color: var(--text); }
}

/* ---------- вкладки приложения ---------- */
.app-tabs { display: flex; gap: 4px; padding: 0 20px; background: var(--surface); border-bottom: 1px solid var(--border);
  overflow-x: auto; }
.app-tab { padding: 10px 16px; font-weight: 600; font-size: 13px; color: var(--muted); text-decoration: none; white-space: nowrap;
  border-bottom: 3px solid transparent; margin-bottom: -1px; }
.app-tab:hover { color: var(--text); }
.app-tab.active { color: var(--accent); border-bottom-color: var(--accent); }
.app-tab:focus-visible { outline: 2px solid var(--accent); outline-offset: -2px; }

/* ---------- журнал входов ---------- */
.tab-page { display: grid; gap: 14px; }
.log-filter { display: grid; grid-template-columns: minmax(200px, 2fr) minmax(130px, 1fr) minmax(140px, 1fr) minmax(140px, 1fr) auto auto;
  gap: 12px; align-items: end; }
.log-filter label { display: grid; gap: 5px; min-width: 0; font-size: 13px; font-weight: 600; }
.log-filter .btn { min-height: 35px; }
.log-table td.client { max-width: 320px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; color: var(--muted); font-size: 12px; }
.log-table td.ip { font-variant-numeric: tabular-nums; }
.status-badge { font-size: 11px; font-weight: 700; padding: 2px 8px; border-radius: 10px; white-space: nowrap; }
.status-badge.ok { background: #d1fae5; color: var(--ok-dark); }
.status-badge.fail { background: #fee2e2; color: #991b1b; }
@media (max-width: 900px) { .log-filter { grid-template-columns: repeat(2, minmax(0, 1fr)); } }
@media (max-width: 560px) { .log-filter { grid-template-columns: 1fr; } }
