:root {
  --bg: #f3f5fa; --surface: #ffffff; --surface-2: #f7f8fc; --text: #161b2e; --muted: #667089; --line: #e3e7f0;
  --brand: #3b5bdb; --brand-ink: #ffffff; --brand-soft: #e7ecff;
  --good: #14873f; --good-soft: #dcf5e5; --bad: #cf2e2e; --bad-soft: #fde6e6; --warn: #b26a00; --warn-soft: #fff0d1;
  --sidebar: #131a30; --sidebar-text: #aeb7d1; --sidebar-active: #ffffff;
  --shadow: 0 1px 2px rgba(20, 28, 55, .05), 0 6px 20px rgba(20, 28, 55, .06);
  --radius: 14px; --ease: .18s ease;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0d1120; --surface: #171d33; --surface-2: #1d243d; --text: #e9ecf6; --muted: #98a2bd; --line: #2a3250;
    --brand: #7b93ff; --brand-ink: #0b1030; --brand-soft: #262f57;
    --good: #4cd083; --good-soft: #14352a; --bad: #ff7676; --bad-soft: #3e2025; --warn: #ffb84d; --warn-soft: #3b2f14;
    --sidebar: #0a0e1b; --shadow: 0 1px 2px rgba(0, 0, 0, .3), 0 6px 20px rgba(0, 0, 0, .25);
  }
}
* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0; background: var(--bg); color: var(--text);
  font: 15px/1.5 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
}
h1, h2, h3 { margin: 0; line-height: 1.25; }
a { color: var(--brand); text-decoration: none; }
button, input, select, textarea { font: inherit; color: inherit; }
.muted { color: var(--muted); }
.hidden { display: none !important; }
.ic { flex-shrink: 0; }
.boot, .loading { padding: 40px; text-align: center; color: var(--muted); }

/* ---------- каркас ---------- */
.shell { display: grid; grid-template-columns: 248px minmax(0, 1fr); min-height: 100dvh; }
.sidebar {
  position: sticky; top: 0; height: 100dvh; background: var(--sidebar); color: var(--sidebar-text);
  display: flex; flex-direction: column; padding: 20px 14px;
}
.brand { display: flex; align-items: center; gap: 10px; font-weight: 700; font-size: 17px; color: #fff; padding: 4px 8px 20px; }
.logo { width: 34px; height: 34px; border-radius: 10px; background: var(--brand); display: grid; place-items: center; color: #fff; }
.sidebar nav { display: flex; flex-direction: column; gap: 4px; }
.sidebar nav a {
  display: flex; align-items: center; gap: 12px; padding: 11px 12px; border-radius: 10px;
  color: var(--sidebar-text); font-weight: 500; transition: background var(--ease), color var(--ease);
}
.sidebar nav a:hover { background: rgba(255, 255, 255, .06); color: #fff; }
.sidebar nav a.on { background: var(--brand); color: #fff; }
.side-foot { margin-top: auto; display: flex; align-items: center; gap: 6px; padding-top: 14px; border-top: 1px solid rgba(255, 255, 255, .08); }
.me { display: flex; align-items: center; gap: 10px; flex: 1; min-width: 0; padding: 6px; border-radius: 10px; color: #fff; }
.me:hover { background: rgba(255, 255, 255, .06); }
.me span:last-child { display: flex; flex-direction: column; min-width: 0; line-height: 1.25; }
.me b { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-size: 14px; }
.me small { color: var(--sidebar-text); font-size: 12px; }
.avatar {
  width: 36px; height: 36px; border-radius: 50%; background: var(--brand-soft); color: var(--brand);
  display: grid; place-items: center; font-weight: 700; font-size: 14px; flex-shrink: 0;
}
.content { min-width: 0; }
.topbar, .tabbar { display: none; }
.main { max-width: 1120px; margin: 0 auto; padding: 28px 28px 48px; }
.page-head { display: flex; align-items: flex-start; justify-content: space-between; gap: 12px; margin-bottom: 20px; flex-wrap: wrap; }
.page-head h1 { font-size: 24px; }
.page-head p { margin: 4px 0 0; color: var(--muted); }

@media (max-width: 860px) {
  .shell { grid-template-columns: 1fr; }
  .sidebar { display: none; }
  .topbar {
    display: flex; align-items: center; justify-content: space-between; position: sticky; top: 0; z-index: 20;
    padding: 10px 16px; padding-top: calc(10px + env(safe-area-inset-top)); background: var(--surface); border-bottom: 1px solid var(--line);
  }
  .topbar .brand { color: var(--text); padding: 0; }
  .tabbar {
    display: flex; position: fixed; left: 0; right: 0; bottom: 0; z-index: 20; background: var(--surface);
    border-top: 1px solid var(--line); padding-bottom: env(safe-area-inset-bottom);
  }
  .tabbar a {
    flex: 1; display: flex; flex-direction: column; align-items: center; gap: 2px; padding: 8px 2px 7px;
    color: var(--muted); font-size: 11px; font-weight: 600; min-width: 0;
  }
  .tabbar a span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; max-width: 100%; }
  .tabbar a.on { color: var(--brand); }
  .main { padding: 18px 14px 96px; }
  .page-head h1 { font-size: 21px; }
}

/* ---------- карточки, формы ---------- */
.card { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); box-shadow: var(--shadow); padding: 20px; margin-bottom: 16px; }
.card > h2 { font-size: 16px; margin-bottom: 14px; display: flex; align-items: center; justify-content: space-between; gap: 8px; }
.card > h2 small { color: var(--muted); font-weight: 500; font-size: 13px; }
.field { display: block; margin-bottom: 14px; }
.field:last-child { margin-bottom: 0; }
.field > span, .lbl { display: block; font-size: 13px; font-weight: 600; color: var(--muted); margin-bottom: 6px; }
.req { color: var(--bad); }
input[type=text], input[type=password], input[type=tel], input[type=date], input[type=time], input[type=search], select, textarea {
  display: block; width: 100%; padding: 11px 13px; background: var(--surface-2); border: 1px solid var(--line);
  border-radius: 10px; transition: border-color var(--ease), box-shadow var(--ease); min-height: 44px;
}
textarea { min-height: 88px; resize: vertical; }
input:focus, select:focus, textarea:focus { outline: none; border-color: var(--brand); box-shadow: 0 0 0 3px var(--brand-soft); }
.err { border-color: var(--bad) !important; }
.grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: 0 14px; }
.grid3 { display: grid; grid-template-columns: repeat(3, 1fr); gap: 0 14px; }
@media (max-width: 560px) { .grid2, .grid3 { grid-template-columns: 1fr; } .grid2.keep { grid-template-columns: 1fr 1fr; } }
.form-err { background: var(--bad-soft); color: var(--bad); padding: 10px 12px; border-radius: 10px; margin-bottom: 14px; font-size: 14px; }
.hint { color: var(--muted); font-size: 13px; margin-top: 6px; }

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 8px; padding: 11px 18px; min-height: 44px;
  border-radius: 10px; border: 1px solid var(--line); background: var(--surface); color: var(--text);
  font-weight: 600; cursor: pointer; transition: transform var(--ease), background var(--ease), opacity var(--ease);
}
.btn:hover { background: var(--surface-2); }
.btn:active { transform: translateY(1px); }
.btn:disabled { opacity: .55; cursor: default; }
.btn.primary { background: var(--brand); border-color: var(--brand); color: var(--brand-ink); }
.btn.primary:hover { filter: brightness(1.08); background: var(--brand); }
.btn.danger { color: var(--bad); }
.btn.danger.solid { background: var(--bad); border-color: var(--bad); color: #fff; }
.btn.sm { padding: 6px 12px; min-height: 34px; font-size: 14px; }
.btn.block { width: 100%; }
.icon-btn {
  display: inline-grid; place-items: center; width: 38px; height: 38px; border: 0; border-radius: 10px;
  background: transparent; color: var(--muted); cursor: pointer;
}
.icon-btn:hover { background: var(--surface-2); color: var(--text); }
.icon-btn.light { color: var(--sidebar-text); }
.icon-btn.light:hover { background: rgba(255, 255, 255, .08); color: #fff; }

.chips { display: flex; flex-wrap: wrap; gap: 8px; }
.chip {
  padding: 9px 15px; border: 1px solid var(--line); background: var(--surface-2); border-radius: 99px; cursor: pointer;
  font-size: 15px; transition: all var(--ease); min-height: 40px;
}
.chip:hover { border-color: var(--brand); }
.chip.on { background: var(--brand); color: var(--brand-ink); border-color: var(--brand); font-weight: 600; }
.chips.err { outline: 2px solid var(--bad); outline-offset: 4px; border-radius: 12px; border-color: transparent !important; }
.choice { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.choice.err { outline: 2px solid var(--bad); outline-offset: 4px; border-radius: 14px; }
.choice .chip { border-radius: 12px; padding: 16px; font-weight: 700; font-size: 16px; text-align: center; }
.choice .chip.yes.on { background: var(--good); border-color: var(--good); color: #fff; }
.choice .chip.nob.on { background: var(--bad); border-color: var(--bad); color: #fff; }
.sub { margin-top: 12px; }
.sticky-actions {
  position: sticky; bottom: 0; z-index: 5; margin: 0 -28px -48px; padding: 14px 28px; background: var(--bg);
  border-top: 1px solid var(--line); display: flex; gap: 10px;
}
.sticky-actions .btn.primary { flex: 1; }
@media (max-width: 860px) { .sticky-actions { bottom: 58px; margin: 0 -14px; padding: 12px 14px; } }

.badge { display: inline-block; padding: 3px 10px; border-radius: 99px; font-size: 12px; font-weight: 700; background: var(--surface-2); color: var(--muted); white-space: nowrap; }
.badge.good { background: var(--good-soft); color: var(--good); }
.badge.bad { background: var(--bad-soft); color: var(--bad); }
.badge.warn { background: var(--warn-soft); color: var(--warn); }
.badge.brand { background: var(--brand-soft); color: var(--brand); }

/* ---------- вход ---------- */
.auth { min-height: 100dvh; display: grid; place-items: center; padding: 20px; background: linear-gradient(160deg, #e8edff, var(--bg) 55%); }
@media (prefers-color-scheme: dark) { .auth { background: linear-gradient(160deg, #1a2150, var(--bg) 55%); } }
.auth-card { width: 100%; max-width: 400px; background: var(--surface); border: 1px solid var(--line); border-radius: 20px; box-shadow: var(--shadow); padding: 32px 28px; }
.auth-card .logo { width: 48px; height: 48px; border-radius: 14px; margin-bottom: 16px; }
.auth-card h1 { font-size: 24px; }
.auth-card .muted { margin: 4px 0 22px; }

/* ---------- таблица визитов ---------- */
.filters { display: flex; flex-direction: column; gap: 12px; }
.filter-row { display: flex; flex-wrap: wrap; gap: 10px; align-items: flex-end; }
.filter-row > * { flex: 1 1 150px; min-width: 0; }
.filter-row .field { margin: 0; }
.seg { display: flex; flex-wrap: wrap; gap: 6px; }
.seg button { padding: 7px 14px; border-radius: 99px; border: 1px solid var(--line); background: var(--surface-2); cursor: pointer; font-weight: 600; font-size: 14px; }
.seg button.on { background: var(--brand-soft); color: var(--brand); border-color: var(--brand); }
.tbl-wrap { overflow-x: auto; }
.tbl { width: 100%; border-collapse: collapse; }
.tbl th { text-align: left; font-size: 12px; text-transform: uppercase; letter-spacing: .04em; color: var(--muted); padding: 10px 12px; border-bottom: 1px solid var(--line); white-space: nowrap; }
.tbl td { padding: 13px 12px; border-bottom: 1px solid var(--line); vertical-align: top; }
.tbl tbody tr { cursor: pointer; transition: background var(--ease); }
.tbl tbody tr:hover { background: var(--surface-2); }
.tbl tbody tr:last-child td { border-bottom: 0; }
.tbl .sub-line { color: var(--muted); font-size: 13px; }
.tbl.static tbody tr { cursor: default; }
@media (max-width: 720px) {
  .tbl:not(.keep) thead { display: none; }
  .tbl:not(.keep), .tbl:not(.keep) tbody, .tbl:not(.keep) tr, .tbl:not(.keep) td { display: block; }
  .tbl:not(.keep) tr { padding: 12px 4px; border-bottom: 1px solid var(--line); }
  .tbl:not(.keep) td { border: 0; padding: 3px 8px; }
  .tbl:not(.keep) td[data-label]::before { content: attr(data-label) ": "; color: var(--muted); font-size: 13px; }
  .tbl:not(.keep) td.first { font-weight: 600; }
  .tbl:not(.keep) td.first::before { content: ""; }
}
.pager { display: flex; align-items: center; justify-content: space-between; gap: 10px; padding-top: 14px; color: var(--muted); font-size: 14px; flex-wrap: wrap; }
.empty { text-align: center; color: var(--muted); padding: 36px 12px; }
.empty b { display: block; color: var(--text); font-size: 16px; margin-bottom: 4px; }

/* ---------- статистика ---------- */
.kpis { display: grid; grid-template-columns: repeat(4, 1fr); gap: 14px; margin-bottom: 16px; }
@media (max-width: 760px) { .kpis { grid-template-columns: 1fr 1fr; } }
.kpi { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); box-shadow: var(--shadow); padding: 16px 18px; }
.kpi span { color: var(--muted); font-size: 13px; font-weight: 600; }
.kpi b { display: block; font-size: 30px; line-height: 1.2; margin-top: 4px; }
.kpi small { color: var(--muted); font-size: 13px; }
.cols2 { display: grid; grid-template-columns: 1fr 1fr; gap: 16px; }
@media (max-width: 860px) { .cols2 { grid-template-columns: 1fr; } }
.cols2 .card { margin-bottom: 0; }
.chart { width: 100%; height: auto; display: block; }
.chart .bar-v { fill: var(--brand-soft); }
.chart .bar-b { fill: var(--brand); }
.chart text { fill: var(--muted); font-size: 11px; }
.chart .grid { stroke: var(--line); stroke-width: 1; }
.legend { display: flex; gap: 16px; font-size: 13px; color: var(--muted); margin-top: 8px; }
.legend i { display: inline-block; width: 10px; height: 10px; border-radius: 3px; margin-right: 6px; }
.bars { display: flex; flex-direction: column; gap: 12px; }
.bar-row .top { display: flex; justify-content: space-between; gap: 10px; font-size: 14px; margin-bottom: 5px; }
.bar-row .top span:last-child { color: var(--muted); white-space: nowrap; }
.bar-track { height: 8px; background: var(--surface-2); border-radius: 99px; overflow: hidden; }
.bar-fill { height: 100%; background: var(--brand); border-radius: 99px; }
.bar-fill.bad { background: var(--bad); }
.bar-fill.good { background: var(--good); }

/* ---------- справочники и пользователи ---------- */
.dict-item { display: flex; align-items: center; gap: 8px; padding: 9px 0; border-bottom: 1px solid var(--line); }
.dict-item:last-of-type { border-bottom: 0; }
.dict-item .name { flex: 1; min-width: 0; overflow-wrap: anywhere; }
.dict-item.off .name { color: var(--muted); text-decoration: line-through; }
.add-row { display: flex; gap: 8px; margin-top: 12px; }
.add-row input { flex: 1; }
.switch { position: relative; width: 40px; height: 24px; flex-shrink: 0; }
.switch input { position: absolute; inset: 0; opacity: 0; margin: 0; cursor: pointer; z-index: 1; min-height: 0; }
.switch i { position: absolute; inset: 0; border-radius: 99px; background: var(--line); transition: background var(--ease); }
.switch i::after { content: ""; position: absolute; left: 3px; top: 3px; width: 18px; height: 18px; border-radius: 50%; background: #fff; transition: transform var(--ease); box-shadow: 0 1px 2px rgba(0, 0, 0, .25); }
.switch input:checked + i { background: var(--good); }
.switch input:checked + i::after { transform: translateX(16px); }
.row-actions { display: flex; gap: 4px; justify-content: flex-end; }
.pw-row { display: flex; gap: 8px; }
.pw-row input { flex: 1; }

/* ---------- окна и уведомления ---------- */
dialog.modal {
  border: 0; padding: 0; border-radius: 18px; width: min(520px, calc(100vw - 24px)); max-height: calc(100dvh - 24px);
  background: var(--surface); color: var(--text); box-shadow: 0 24px 60px rgba(0, 0, 0, .3);
}
dialog.modal.wide { width: min(640px, calc(100vw - 24px)); }
dialog.modal::backdrop { background: rgba(10, 14, 30, .55); backdrop-filter: blur(2px); }
.modal-h { display: flex; align-items: center; justify-content: space-between; padding: 16px 18px 8px 22px; }
.modal-h h3 { font-size: 18px; }
.modal-b { padding: 8px 22px 22px; overflow-y: auto; max-height: calc(100dvh - 110px); }
.actions { display: flex; gap: 10px; justify-content: flex-end; margin-top: 18px; flex-wrap: wrap; }
.dl { display: grid; grid-template-columns: 140px 1fr; gap: 10px 14px; margin: 0; }
.dl dt { color: var(--muted); font-size: 14px; }
.dl dd { margin: 0; overflow-wrap: anywhere; }
@media (max-width: 480px) { .dl { grid-template-columns: 1fr; gap: 0; } .dl dt { margin-top: 10px; } }
#toasts { position: fixed; top: 16px; left: 50%; transform: translateX(-50%); z-index: 100; display: flex; flex-direction: column; gap: 8px; align-items: center; pointer-events: none; width: max-content; max-width: calc(100vw - 24px); }
.toast { background: var(--text); color: var(--bg); padding: 11px 18px; border-radius: 12px; font-weight: 600; font-size: 14px; box-shadow: var(--shadow); animation: pop .2s ease; }
.toast.error { background: var(--bad); color: #fff; }
.toast.out { opacity: 0; transition: opacity .3s; }
@keyframes pop { from { transform: translateY(-8px); opacity: 0; } to { transform: none; opacity: 1; } }

/* ---------- РНП ---------- */
.month-nav { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }
.month-nav b { min-width: 130px; text-align: center; }
.rnp-wrap { overflow: auto; max-height: calc(100dvh - 360px); min-height: 280px; border-radius: var(--radius); }
.rnp { border-collapse: separate; border-spacing: 0; font-size: 13px; min-width: max-content; }
.rnp th, .rnp td { padding: 7px 10px; border-bottom: 1px solid var(--line); white-space: nowrap; text-align: right; background: var(--surface); }
.rnp thead th { position: sticky; top: 0; z-index: 3; color: var(--muted); font-size: 12px; }
.rnp .sticky-l { position: sticky; left: 0; z-index: 2; text-align: left; width: 220px; min-width: 220px; max-width: 220px; overflow: hidden; text-overflow: ellipsis; font-weight: 600; }
.rnp thead .sticky-l { z-index: 4; }
.rnp .sticky-l small { color: var(--muted); font-weight: 500; }
.rnp .tot { position: sticky; left: 220px; z-index: 2; background: var(--surface-2); font-weight: 700; border-right: 1px solid var(--line); }
.rnp thead .tot { z-index: 4; }
.rnp tr.sec td { position: sticky; left: 0; background: var(--brand-soft); color: var(--brand); font-weight: 700; text-align: left; z-index: 1; }
.rnp td.ed { cursor: text; }
.rnp td.ed:hover { background: var(--brand-soft); }
.rnp td input { width: 84px; padding: 4px 6px; min-height: 0; text-align: right; border-radius: 6px; }
.rnp tr.calc td, .rnp tr.calc th { font-style: italic; }
.rnp tr.calc td { color: var(--muted); }
.rnp tr.auto td { color: var(--good); font-weight: 600; }
.note-row { display: flex; gap: 12px; align-items: flex-start; padding: 8px 0; border-bottom: 1px solid var(--line); }
.note-row:last-child { border-bottom: 0; }
.note-row b { min-width: 86px; }
.note-row span { flex: 1; overflow-wrap: anywhere; }
@media (max-width: 860px) {
  .rnp-wrap { max-height: calc(100dvh - 300px); }
  .rnp .sticky-l { width: 150px; min-width: 150px; max-width: 150px; }
  .rnp .tot { left: 150px; }
}

/* ---------- Сводка ---------- */
.kpis-8 { grid-template-columns: repeat(4, 1fr); }
@media (max-width: 760px) { .kpis-8 { grid-template-columns: 1fr 1fr; } .kpi b { font-size: 24px; } }
.kpi small { display: block; min-height: 20px; }
.delta { font-weight: 700; font-size: 12px; margin-right: 4px; color: var(--muted); }
.delta.up { color: var(--good); }
.delta.down { color: var(--bad); }
.chart .bar-g { fill: var(--good); }
@media (max-width: 400px) { .kpi b { font-size: 20px; white-space: nowrap; } .kpi { padding: 14px; } }

/* ---------- Протоколы и задачи ---------- */
.task { display: flex; justify-content: space-between; gap: 14px; padding: 13px 0; border-bottom: 1px solid var(--line); cursor: pointer; }
.task:last-child { border-bottom: 0; }
.task:hover .task-main b { color: var(--brand); }
.task.done .task-main b { color: var(--muted); text-decoration: line-through; }
.task-main { min-width: 0; flex: 1; }
.task-main b { overflow-wrap: anywhere; }
.task-side { display: flex; flex-direction: column; align-items: flex-end; gap: 4px; flex-shrink: 0; }
.task-bar { display: flex; align-items: center; gap: 10px; margin-top: 6px; max-width: 320px; }
.task-bar .bar-track { flex: 1; height: 6px; }
.task-bar span { font-size: 12px; color: var(--muted); min-width: 34px; }
.late { color: var(--bad); font-weight: 700; }
.prose { white-space: pre-wrap; overflow-wrap: anywhere; }
.task-update { background: var(--surface-2); border-radius: 12px; padding: 16px; margin-top: 16px; }
input[type=range] { padding: 0; min-height: 28px; width: 100%; accent-color: var(--brand); background: transparent; border: 0; }
.meet { cursor: pointer; transition: border-color var(--ease), transform var(--ease); }
.meet:hover { border-color: var(--brand); }
.meet-head { display: flex; justify-content: space-between; gap: 10px; align-items: baseline; flex-wrap: wrap; }
.meet-sum { margin: 10px 0; color: var(--text); overflow-wrap: anywhere; }
.meet-foot { display: flex; justify-content: space-between; gap: 10px; flex-wrap: wrap; font-size: 13px; margin-top: 8px; }
.meet-tasks { font-weight: 600; }
/* меню на телефоне прокручивается, если пунктов много */
@media (max-width: 860px) {
  .tabbar { overflow-x: auto; scrollbar-width: none; }
  .tabbar::-webkit-scrollbar { display: none; }
  .tabbar a { flex: 1 0 74px; }
}

/* ---------- Уведомления ---------- */
.count { display: inline-block; min-width: 18px; height: 18px; padding: 0 5px; margin-left: auto; border-radius: 99px; background: var(--bad); color: #fff; font: 700 11px/18px inherit; font-style: normal; text-align: center; }
.sidebar nav a .count[hidden], .count[hidden] { display: none; }
.tabbar a { position: relative; }
.tabbar a .count { position: absolute; top: 4px; left: calc(50% + 6px); margin: 0; }
.bell { position: relative; display: grid; place-items: center; width: 38px; height: 38px; color: var(--muted); }
.bell .count { position: absolute; top: 0; right: -2px; margin: 0; }
.notice { display: flex; justify-content: space-between; gap: 12px; padding: 14px 0; border-bottom: 1px solid var(--line); cursor: pointer; }
.notice:last-child { border-bottom: 0; }
.notice:hover .notice-main b { color: var(--brand); }
.notice.unread .notice-main b { color: var(--text); }
.notice:not(.unread) { opacity: .65; }
.notice-main { min-width: 0; }
.notice .prose { color: var(--muted); font-size: 14px; margin-top: 2px; }
.dot { width: 10px; height: 10px; border-radius: 50%; background: var(--brand); flex-shrink: 0; margin-top: 6px; }
