:root {
    --blue: #005bff;
    --blue-dark: #0048cc;
    --blue-soft: #e6efff;
    --accent: #f91155;
    --accent-dark: #d90d48;
    --bg: #f2f3f5;
    --surface: #ffffff;
    --text: #001a34;
    --text-2: #4d5f74;
    --muted: #707f8d;
    --border: #e1e4e8;
    --good: #10a05a;
    --bad: #e3243b;
    --radius: 12px;
    --radius-sm: 8px;
    --series-1: #005bff;
    --series-2: #eb6834;
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }

html, body {
    margin: 0;
    background: var(--bg);
    color: var(--text);
    font: 14px/1.45 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
}

h1 { font-size: 26px; margin: 8px 0 20px; font-weight: 700; }
h2 { font-size: 20px; margin: 0; }
h3 { font-size: 15px; margin: 0 0 12px; }
a { color: var(--blue); }
.muted { color: var(--muted); }
.small { font-size: 12px; }

/* Шапка */
.topbar {
    display: flex; align-items: center; gap: 24px;
    padding: 0 24px; height: 60px;
    background: var(--surface);
    border-bottom: 1px solid var(--border);
    position: sticky; top: 0; z-index: 20;
}
.brand { display: flex; align-items: center; gap: 10px; text-decoration: none; color: var(--text); font-size: 17px; }
.brand b { color: var(--blue); }
.brand-mark {
    width: 32px; height: 32px; border-radius: 9px;
    background: linear-gradient(135deg, #1f2937, #4b5563); color: #fff; font-weight: 800; font-size: 13px; letter-spacing: -.5px;
    display: inline-flex; align-items: center; justify-content: center;
}
.brand-lg { font-size: 20px; margin-bottom: 8px; }
.topnav { display: flex; gap: 4px; flex: 1; }
.topnav a {
    color: var(--text-2); text-decoration: none; padding: 8px 12px; border-radius: var(--radius-sm); font-weight: 500;
}
.topnav a:hover { background: var(--bg); color: var(--text); }
.user { display: flex; align-items: center; gap: 12px; color: var(--text-2); }
.user form { margin: 0; }

/* Раскладка */
.page { max-width: 1440px; margin: 0 auto; padding: 24px; }
.page.narrow { max-width: 640px; }
.page-head { display: flex; align-items: center; justify-content: space-between; gap: 16px; }
.card {
    background: var(--surface); border-radius: var(--radius);
    padding: 20px; margin-bottom: 20px;
    box-shadow: 0 1px 2px rgba(0, 26, 52, .06);
}
.stack { display: flex; flex-direction: column; gap: 14px; }
.row-gap { display: flex; gap: 10px; flex-wrap: wrap; }
.inline { display: inline; margin: 0; }

/* Формы */
label { display: flex; flex-direction: column; gap: 6px; font-weight: 500; color: var(--text-2); }
input[type=text], input[type=password], input[type=date], input[type=search], input[type=number], input[type=time], select, textarea {
    font: inherit; color: var(--text);
    border: 1px solid var(--border); border-radius: var(--radius-sm);
    padding: 9px 12px; background: #fff; width: 100%;
}
textarea { resize: vertical; min-height: 44px; }
input:focus, select:focus, textarea:focus { outline: 2px solid var(--blue-soft); border-color: var(--blue); }
fieldset { border: 0; padding: 0; margin: 0; min-width: 0; }
legend { font-weight: 600; padding: 0; margin-bottom: 8px; color: var(--text); }
label.check, label.radio { flex-direction: row; align-items: center; gap: 8px; font-weight: 400; color: var(--text); cursor: pointer; }
label.radio { margin-bottom: 6px; flex-wrap: wrap; }

.filter-grid {
    display: grid; gap: 20px 28px;
    grid-template-columns: repeat(auto-fit, minmax(260px, 1fr));
}
.field-wide { grid-column: 1 / -1; }
.two-cols { display: grid; grid-template-columns: 1fr 1fr; gap: 16px; }
.dates { display: flex; gap: 10px; margin-top: 10px; }
.dates label { flex: 1; flex-direction: row; align-items: center; gap: 6px; font-weight: 400; }
#periodHint { margin-top: 6px; }
.form-actions {
    display: flex; align-items: center; justify-content: space-between; gap: 16px; flex-wrap: wrap;
    margin-top: 20px; padding-top: 16px; border-top: 1px solid var(--border);
}

.chips { display: flex; flex-wrap: wrap; gap: 8px; }
.chip-check { flex-direction: row; cursor: pointer; }
.chip-check input { position: absolute; opacity: 0; pointer-events: none; }
.chip-check span {
    display: inline-block; padding: 7px 14px; border-radius: 20px;
    background: var(--bg); color: var(--text); font-weight: 500; border: 1px solid transparent;
    user-select: none;
}
.chip-check input:checked + span { background: var(--blue-soft); color: var(--blue); border-color: var(--blue); }
.chip-check input:focus-visible + span { outline: 2px solid var(--blue); outline-offset: 2px; }
.chip { display: inline-block; padding: 2px 10px; border-radius: 12px; background: var(--blue-soft); color: var(--blue); margin: 2px 4px 2px 0; font-size: 12px; }

/* Кнопки */
.btn {
    display: inline-flex; align-items: center; justify-content: center; gap: 6px;
    border: 0; border-radius: var(--radius-sm); padding: 10px 18px;
    font: inherit; font-weight: 600; cursor: pointer; text-decoration: none; white-space: nowrap;
}
.btn-primary { background: var(--blue); color: #fff; }
.btn-primary:hover { background: var(--blue-dark); }
.btn-primary:disabled { opacity: .55; cursor: progress; }
.btn-accent { background: var(--accent); color: #fff; }
.btn-accent:hover { background: var(--accent-dark); }
.btn-ghost { background: var(--bg); color: var(--text); }
.btn-ghost:hover { background: #e5e7eb; }
.btn-danger { background: #fdecee; color: var(--bad); }
.btn-danger:hover { background: #fbd5da; }
.btn-sm { padding: 6px 12px; font-size: 13px; }
.btn-lg { padding: 12px 28px; font-size: 15px; }
.btn-block { width: 100%; }

/* Сообщения */
.alert { padding: 12px 16px; border-radius: var(--radius-sm); margin-bottom: 16px; }
.alert-error { background: #fdecee; color: #a3112a; }
.alert-ok { background: #e7f6ee; color: #0b6b3c; }
.alert-warn { background: #fff6e0; color: #7a5300; }
.form-actions + .alert { margin: 16px 0 0; }
.badge { padding: 2px 10px; border-radius: 12px; font-size: 12px; font-weight: 600; }
.badge-ok { background: #e7f6ee; color: #0b6b3c; }
.badge-off { background: #f0f1f3; color: var(--muted); }

/* Вход */
.login-page { display: flex; min-height: 100vh; align-items: center; justify-content: center; padding: 16px; }
.login-card { background: #fff; border-radius: 16px; padding: 32px; width: 100%; max-width: 380px; box-shadow: 0 8px 30px rgba(0, 26, 52, .08); }
.login-card h1 { font-size: 22px; margin: 12px 0 18px; }

/* Прогресс */
.progress { display: flex; align-items: center; gap: 16px; }
.progress-title { font-weight: 600; }
.spinner {
    width: 32px; height: 32px; border-radius: 50%;
    border: 3px solid var(--blue-soft); border-top-color: var(--blue);
    animation: spin 0.9s linear infinite; flex: none;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* Результаты */
.results-head { display: flex; align-items: flex-end; justify-content: space-between; gap: 16px; margin: 8px 0 16px; flex-wrap: wrap; }
.kpis { display: grid; grid-template-columns: repeat(auto-fit, minmax(190px, 1fr)); gap: 16px; margin-bottom: 20px; }
.kpi { background: var(--surface); border-radius: var(--radius); padding: 16px 18px; box-shadow: 0 1px 2px rgba(0, 26, 52, .06); }
.kpi-title { color: var(--text-2); font-size: 13px; }
.kpi-value { font-size: 24px; font-weight: 700; margin: 4px 0 2px; font-variant-numeric: tabular-nums; }
.kpi-prev { font-size: 12px; color: var(--muted); }
.delta { font-weight: 600; font-size: 12px; white-space: nowrap; }
.delta.good { color: var(--good); }
.delta.bad { color: var(--bad); }
.delta.flat { color: var(--muted); }

.charts { display: grid; grid-template-columns: 3fr 2fr; gap: 20px; }
.charts > * { min-width: 0; }
.chart-card { margin-bottom: 20px; }
.chart-box { position: relative; height: 300px; }

/* Таблица */
.table-card { padding: 0; overflow: hidden; }
.table-toolbar { display: flex; gap: 16px; align-items: center; justify-content: space-between; padding: 16px 20px; flex-wrap: wrap; }
.table-toolbar input[type=search] { max-width: 360px; }
.table-toolbar .chip-check span { padding: 5px 12px; font-size: 13px; }
.table-scroll { overflow: auto; max-height: 72vh; border-top: 1px solid var(--border); }
.table-note { padding: 10px 20px; }

.data-table { border-collapse: separate; border-spacing: 0; width: max-content; min-width: 100%; font-variant-numeric: tabular-nums; }
.data-table th, .data-table td { padding: 8px 12px; border-bottom: 1px solid var(--border); white-space: nowrap; background: #fff; }
.data-table thead th { position: sticky; z-index: 3; background: #f7f8fa; font-weight: 600; color: var(--text-2); font-size: 12px; text-align: right; vertical-align: bottom; }
.data-table thead tr:first-child th { top: 0; text-align: center; color: var(--blue); background: var(--blue-soft); border-left: 2px solid #fff; }
.data-table thead tr:nth-child(2) th { top: 33px; cursor: pointer; white-space: normal; min-width: 96px; max-width: 140px; }
.data-table thead tr:nth-child(2) th:hover { color: var(--blue); }
.data-table th.draggable { cursor: grab; }
.data-table th.dragging { opacity: .4; }
.data-table th.drop-before { box-shadow: inset 3px 0 0 var(--blue); }
.data-table th.drop-after { box-shadow: inset -3px 0 0 var(--blue); }
.data-table th.sorted-asc::after { content: " ▲"; color: var(--blue); }
.data-table th.sorted-desc::after { content: " ▼"; color: var(--blue); }
.data-table td.num { text-align: right; }
.data-table td .delta { display: block; font-size: 11px; }
.data-table .text { text-align: left !important; }
.data-table .name { min-width: 260px; max-width: 360px; white-space: normal; }
.data-table .sticky-1, .data-table .sticky-2 { position: sticky; z-index: 2; }
.data-table thead .sticky-1, .data-table thead .sticky-2 { z-index: 4; }
.data-table .sticky-1 { left: 0; min-width: 120px; }
.data-table .sticky-2 { left: 120px; box-shadow: 2px 0 0 var(--border); }
.data-table tbody tr:hover td { background: #f5f8ff; }
.data-table tfoot td { position: sticky; bottom: 0; background: #f7f8fa; font-weight: 700; border-top: 2px solid var(--border); z-index: 2; }
.data-table tfoot td.sticky-1, .data-table tfoot td.sticky-2 { z-index: 3; }
.empty-row td { text-align: center !important; color: var(--muted); padding: 24px; }

.simple-table { width: 100%; border-collapse: collapse; }
.simple-table th, .simple-table td { text-align: left; padding: 12px 16px; border-bottom: 1px solid var(--border); vertical-align: middle; }
.simple-table th { color: var(--text-2); font-size: 12px; background: #f7f8fa; }
.simple-table .actions { text-align: right; white-space: nowrap; }

/* Себестоимость */
.cost-toolbar { display: flex; justify-content: space-between; align-items: flex-end; gap: 16px; flex-wrap: wrap; }
.cost-store label { min-width: 260px; }
.cost-excel { display: flex; gap: 12px; align-items: center; flex-wrap: wrap; }
.upload-form { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.upload-form input[type=file] { max-width: 260px; }
.simple-table th.num, .simple-table td.num { text-align: right; }
.cost-input { max-width: 140px; text-align: right; }
.cost-table thead th { position: sticky; top: 0; z-index: 1; }
.cost-actions { padding: 0 20px 20px; }

/* Фильтр схем и выбор колонок над таблицей */
.toolbar-right { display: flex; gap: 12px; align-items: center; flex-wrap: wrap; }
.scheme-filter { display: flex; gap: 6px; align-items: center; }
.scheme-filter .label { color: var(--text-2); font-size: 13px; margin-right: 2px; }
.columns-menu { position: relative; }
.columns-panel {
    position: absolute; right: 0; top: calc(100% + 6px); z-index: 30;
    background: #fff; border: 1px solid var(--border); border-radius: var(--radius);
    box-shadow: 0 8px 30px rgba(0, 26, 52, .12); padding: 14px 16px; width: 340px; max-height: 70vh; overflow: auto;
}
.columns-panel h4 { margin: 12px 0 6px; font-size: 12px; color: var(--muted); text-transform: uppercase; letter-spacing: .03em; }
.columns-panel h4:first-of-type { margin-top: 4px; }
.columns-panel label.check { padding: 3px 0; font-size: 13px; }
.columns-panel .panel-actions { display: flex; justify-content: space-between; margin-top: 12px; padding-top: 10px; border-top: 1px solid var(--border); }
.cost-missing { display: block; font-size: 11px; color: #b26b00; font-weight: 600; }

@media (max-width: 900px) {
    .charts { grid-template-columns: 1fr; }
    .two-cols { grid-template-columns: 1fr; }
    .topbar { padding: 0 16px; gap: 12px; height: auto; flex-wrap: wrap; padding-top: 8px; padding-bottom: 8px; }
    .topnav { order: 3; flex-basis: 100%; overflow-x: auto; }
    .page { padding: 16px; }
    .kpis { grid-template-columns: 1fr 1fr; gap: 10px; }
    .kpi-value { font-size: 20px; }
    .data-table .sticky-2 { position: static; box-shadow: none; }
    .simple-table { display: block; overflow-x: auto; }
}

/* Рассылки */
.steps { margin: 0; padding-left: 20px; line-height: 1.6; }
#chats { margin: 28px 0 12px; }
#weekDaysBlock, #monthDayBlock { margin: 4px 0 12px; }
.simple-table .actions form + form, .simple-table .actions a + form { margin-left: 4px; }
.mailings-table .actions { width: 1%; }
.action-grid { display: grid; grid-template-columns: repeat(2, auto); gap: 6px; justify-content: end; }
.action-grid form, .action-grid a { margin: 0 !important; }
.action-grid .btn { width: 100%; }
.mailings-table td { font-size: 14px; }
