:root {
    --bg: #f5f7fa;
    --surface: #ffffff;
    --text: #1f2937;
    --muted: #6b7280;
    --border: #e2e6ec;
    --line: #eef1f5;
    --accent: #2f6fde;
    --accent-weak: #e8f0fd;
    --danger: #d64545;
    --danger-weak: #fdeaea;
    --warn: #b7791f;
    --warn-weak: #fff6e0;
    --ok: #2f9e5b;
    --clinic: #6c4fd6;
    --field: #138a8a;
    --complex: #c26a00;
    --radius: 8px;
    font-family: "Segoe UI", system-ui, -apple-system, Roboto, Arial, sans-serif;
    font-size: 14px;
    color: var(--text);
}
* { box-sizing: border-box; }
body { margin: 0; background: var(--bg); }
button, input, select, textarea { font: inherit; color: inherit; }

.topbar {
    position: sticky; top: 0; z-index: 20;
    display: flex; flex-wrap: wrap; gap: 8px; align-items: center;
    padding: 10px 16px; background: var(--surface); border-bottom: 1px solid var(--border);
}
.topbar h1 { font-size: 16px; margin: 0 12px 0 0; font-weight: 600; }
.topbar .spacer { flex: 1; }
.month-nav { display: flex; align-items: center; gap: 4px; }
.month-nav .label { min-width: 130px; text-align: center; font-weight: 600; text-transform: capitalize; }
.who { color: var(--muted); font-size: 13px; }

.btn {
    border: 1px solid var(--border); background: var(--surface); border-radius: var(--radius);
    padding: 6px 12px; cursor: pointer; white-space: nowrap;
}
.btn:hover { border-color: #c5ccd6; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn.danger { color: var(--danger); border-color: #f0c4c4; }
.btn.icon { padding: 4px 9px; }
.btn:disabled { opacity: .5; cursor: default; }

.filters { display: flex; flex-wrap: wrap; gap: 8px; padding: 10px 16px; }
.filters select, .filters input {
    border: 1px solid var(--border); border-radius: var(--radius); padding: 5px 8px; background: var(--surface);
}

.filters .kind-filter { display: inline-flex; align-items: center; gap: 12px; padding: 5px 10px; border: 1px solid var(--border); border-radius: var(--radius); background: var(--surface); }
.filters .kind-filter label { display: inline-flex; align-items: center; gap: 4px; cursor: pointer; white-space: nowrap; }
.filters .kind-filter input { border: 0; padding: 0; margin: 0; }

.table-wrap { overflow: auto; margin: 0 16px 24px; background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); max-height: calc(100vh - 120px); }
table.cal { border-collapse: separate; border-spacing: 0; width: 100%; min-width: 1500px; }
table.cal th {
    position: sticky; top: 0; z-index: 5; background: #f0f3f8; font-weight: 600; font-size: 12px;
    text-align: left; padding: 6px 8px; border-bottom: 1px solid var(--border); border-right: 1px solid var(--line);
    white-space: pre-line; vertical-align: bottom;
}
table.cal td { padding: 5px 8px; border-bottom: 1px solid var(--line); border-right: 1px solid var(--line); vertical-align: top; }
table.cal td.num { text-align: right; font-variant-numeric: tabular-nums; }
table.cal td.c { text-align: center; }
table.cal tr.visit { cursor: pointer; }
table.cal tr.visit:hover td { background: var(--accent-weak); }
table.cal tr.visit.bd-start td { border-top: 1px solid #d5dbe4; }
table.cal td.note { white-space: pre-line; max-width: 380px; color: #374151; font-size: 13px; }

/* Строка с датой — яркая полоса, чтобы дни не сливались с записями (просьба Заика А.) */
tr.day td {
    background: #2f6fde; color: #fff; border-top: 3px solid #1d4fa8; padding: 8px 10px; font-size: 14px;
}
:root { --off: #fff8d6; --off-line: #f3e6a4; }
tr.day.weekend td { color: #4a2c00; background: #ffc233; border-top-color: #d99a00; }
tr.visit.off td { background: var(--off); }
tr.visit.off:hover td { background: #fff1b8; }
tr.day.short .date::after { content: " ·"; }
tr.day.empty td { background: #d6e4fb; color: #1d3f7a; border-top: 1px solid #b5cdf5; padding: 3px 10px; font-size: 13px; }
tr.day.empty.weekend td { background: #ffe9b0; color: #6b4a00; border-top-color: #f3d27a; }
tr.day .date { font-weight: 700; margin-right: 10px; font-size: 15px; }
tr.day.empty .date { font-size: 13px; }
tr.day .stat { margin-right: 14px; color: rgba(255,255,255,.8); }
tr.day .stat b { color: #fff; }
tr.day.weekend .stat { color: rgba(74,44,0,.75); }
tr.day.weekend .stat b { color: #4a2c00; }
tr.day.over td { background: #d64545; color: #fff; border-top-color: #a82a2a; }
tr.day.over .stat { color: rgba(255,255,255,.85); }
tr.day.over .stat b { color: #fff; }
tr.day .over-badge { color: #fff; background: rgba(0,0,0,.25); border-radius: 10px; padding: 1px 8px; font-weight: 600; margin-right: 14px; }
tr.day .muted { color: inherit; opacity: .8; }
tr.day .add { float: right; }

.tag { display: inline-block; padding: 1px 7px; border-radius: 10px; font-size: 12px; font-weight: 600; }
.tag.Клиника { background: #efeafd; color: var(--clinic); }
.tag.Выезд { background: #e3f5f5; color: var(--field); }
.tag.Комплекс { background: #fdf0dc; color: var(--complex); }
.yes { color: var(--ok); font-weight: 600; }
.no { color: #b3b9c2; }
.muted { color: var(--muted); }

/* Модалки */
.overlay { position: fixed; inset: 0; background: rgba(15, 23, 42, .35); display: none; align-items: flex-start; justify-content: center; z-index: 50; overflow: auto; padding: 32px 12px; }
.overlay.show { display: flex; }
.modal { background: var(--surface); border-radius: 12px; width: 100%; max-width: 860px; box-shadow: 0 20px 50px rgba(0, 0, 0, .2); }
.modal header { display: flex; align-items: center; padding: 14px 18px; border-bottom: 1px solid var(--border); font-weight: 600; font-size: 15px; }
.modal header .x { margin-left: auto; border: 0; background: none; font-size: 20px; cursor: pointer; color: var(--muted); }
.modal .body { padding: 16px 18px; }
.modal footer { display: flex; gap: 8px; padding: 12px 18px; border-top: 1px solid var(--border); }
.modal footer .spacer { flex: 1; }

.grid { display: grid; grid-template-columns: repeat(4, 1fr); gap: 10px 12px; }
.grid .span2 { grid-column: span 2; }
.grid .span4 { grid-column: span 4; }
.field label { display: block; font-size: 12px; color: var(--muted); margin-bottom: 3px; }
.field input, .field select, .field textarea {
    width: 100%; border: 1px solid var(--border); border-radius: 6px; padding: 6px 8px; background: #fff;
}
.field textarea { min-height: 70px; resize: vertical; }
.field input:focus, .field select:focus, .field textarea:focus { outline: 2px solid var(--accent-weak); border-color: var(--accent); }
.section-title { grid-column: span 4; font-size: 12px; font-weight: 600; text-transform: uppercase; letter-spacing: .03em; color: var(--muted); margin-top: 6px; }

.ac { position: relative; }
.ac-list { position: absolute; left: 0; right: 0; top: 100%; z-index: 60; background: #fff; border: 1px solid var(--border); border-radius: 6px; box-shadow: 0 8px 20px rgba(0,0,0,.1); max-height: 260px; overflow: auto; display: none; }
.ac-list.show { display: block; }
.ac-item { padding: 6px 10px; cursor: pointer; }
.ac-item:hover, .ac-item.active { background: var(--accent-weak); }
.ac-item small { color: var(--muted); margin-left: 6px; }

.days-pick { display: grid; grid-template-columns: repeat(7, 1fr); gap: 4px; }
.days-pick .dow { text-align: center; font-size: 12px; color: var(--muted); }
.days-pick label { text-align: center; border: 1px solid var(--border); border-radius: 6px; padding: 6px 0; cursor: pointer; user-select: none; }
.days-pick label.src { background: var(--accent-weak); border-color: var(--accent); cursor: default; }
.days-pick input { display: none; }
.days-pick label:has(input:checked) { background: var(--accent); color: #fff; border-color: var(--accent); }

/* Тосты */
#toasts { position: fixed; right: 16px; bottom: 16px; display: flex; flex-direction: column; gap: 8px; z-index: 100; max-width: 420px; }
.toast { padding: 10px 14px; border-radius: 8px; background: #1f2937; color: #fff; box-shadow: 0 6px 16px rgba(0,0,0,.2); white-space: pre-line; }
.toast.error { background: var(--danger); }
.toast.warn { background: var(--warn-weak); color: #6b4a0e; border: 1px solid #f1d58f; }
.toast.ok { background: var(--ok); }

.blocker { max-width: 520px; margin: 80px auto; background: var(--surface); padding: 28px; border-radius: 12px; border: 1px solid var(--border); text-align: center; }
.blocker h2 { margin-top: 0; font-size: 18px; }

/* Админка */
.admin-wrap { max-width: 980px; margin: 16px auto; padding: 0 16px; }
.card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); }
table.list { width: 100%; border-collapse: collapse; }
table.list th, table.list td { padding: 8px 12px; border-bottom: 1px solid var(--line); text-align: left; }
table.list th { font-size: 12px; color: var(--muted); font-weight: 600; background: #f8fafc; }
table.list tr.has-role td:first-child { font-weight: 600; }
table.list select { border: 1px solid var(--border); border-radius: 6px; padding: 4px 6px; }
.tabs { display: flex; gap: 4px; margin-bottom: 12px; }
.tabs .btn.active { background: var(--accent-weak); border-color: var(--accent); color: var(--accent); }

@media (max-width: 720px) {
    .grid { grid-template-columns: 1fr 1fr; }
    .grid .span4, .section-title { grid-column: span 2; }
    .topbar h1 { width: 100%; }
    .table-wrap { margin: 0 8px 16px; }
}

/* Сделки Б24 */
[hidden] { display: none !important; }
.nofact-count { color: var(--danger); }
.grid .span3 { grid-column: span 3; }
.deal-box { display: flex; flex-wrap: wrap; align-items: end; gap: 8px; }
.deal-box .chk { display: flex; align-items: center; gap: 6px; font-size: 13px; color: var(--text); margin: 0 0 6px; }
.deal-box .chk input { width: auto; }
.sync-err { color: var(--danger); font-size: 12px; align-self: center; }
.deal-mark { font-size: 11px; font-weight: 600; padding: 0 5px; border-radius: 6px; margin-left: 4px; vertical-align: 1px; }
.deal-mark.ok { color: var(--ok); background: #e6f6ec; }
.deal-mark.err { color: var(--danger); background: var(--danger-weak); }
.dm-actions { display: flex; flex-wrap: wrap; gap: 8px; margin: 12px 0; }
.dm-result { max-height: 50vh; overflow: auto; font-size: 13px; }
.dm-result table { border-collapse: collapse; width: 100%; }
.dm-result td, .dm-result th { text-align: left; padding: 3px 6px; border-bottom: 1px solid var(--line); vertical-align: top; }
@media (max-width: 720px) { .grid .span3 { grid-column: span 2; } }

/* Сегодня */
tr.day.today td:first-child { box-shadow: inset 8px 0 0 #ffd43b; }
tr.day.today .date { text-decoration: underline; text-underline-offset: 3px; }
tr.day.flash td { animation: flash 1.6s ease-out; }
@keyframes flash { from { background: #fff3bf; } }
table.cal td.deal { white-space: nowrap; }
table.cal td.deal a { color: var(--accent); text-decoration: none; font-variant-numeric: tabular-nums; }
table.cal td.deal a:hover { text-decoration: underline; }

/* Персонал в записи и отчёт по ЗП */
.staff-row { display: flex; gap: 8px; align-items: center; margin-bottom: 6px; flex-wrap: wrap; }
.staff-row select { flex: 1 1 180px; min-width: 0; }
.staff-row input[type=number] { width: 84px; }
.staff-row .sr-fee { width: 100px; }
.staff-row .sr-sum { min-width: 70px; font-size: 13px; }
.staff-row .btn { padding: 4px 9px; }
table.list input { border: 1px solid var(--border); border-radius: 6px; padding: 4px 6px; max-width: 100%; }
table.list td.num, table.list th.num { text-align: right; }
.pl-details { margin-top: 14px; }
.pl-details summary { cursor: pointer; font-weight: 600; margin-bottom: 4px; }
#stTable th, #stTable td { padding: 6px 5px; }
#stTable .st-fio { min-width: 190px; }
#stTable .st-note { min-width: 130px; }
#stTable .st-pos { min-width: 100px; }
#stTable .st-month, #stTable .st-fee { width: 88px !important; }
#stTable .btn { padding: 5px 8px; }
#staffModal .body { overflow-x: auto; }

/* Расходы выездов за месяц */
.cx-set { display: flex; gap: 18px; flex-wrap: wrap; align-items: flex-end; margin-bottom: 12px; padding: 10px 12px; background: var(--bg-soft, #f4f7fb); border-radius: 8px; }
.cx-set label { display: flex; flex-direction: column; font-size: 12px; color: var(--muted); gap: 3px; }
.cx-set input { width: 110px; border: 1px solid var(--border); border-radius: 6px; padding: 5px 7px; font-size: 14px; }
.cx-set small { font-size: 11px; }
#cxBody table.list th, #cxBody table.list td { padding: 5px 5px; font-size: 13px; }
#cxBody input.cx-in { width: 88px; text-align: right; }
#cxBody tr.cx-clinic td { color: var(--muted); }
#cxBody td.cx-neg { color: var(--danger, #d64545); font-weight: 600; }
#cxBody tr.cx-total td { border-top: 2px solid var(--border); font-weight: 700; }
#cxBody h4 { margin: 18px 0 6px; }
#costsModal .body { overflow-x: auto; }

td.who { white-space: nowrap; color: var(--muted); font-size: 12px; }
