/*
 * Стили веб-панели. Без фреймворков: переменные-токены + простые компоненты.
 * Цвета графиков и «хрома» — из проверенной палитры (синий = прибыль/данные, красный = убыток).
 */

:root {
    color-scheme: light;
    --page: #f9f9f7;
    --surface: #fcfcfb;
    --surface-2: #f1f0ec;
    --border: rgba(11, 11, 11, 0.10);
    --grid: #e1e0d9;
    --axis: #c3c2b7;
    --text: #0b0b0b;
    --text-2: #52514e;
    --muted: #7a7873;
    --accent: #2a78d6;
    --accent-hover: #256abf;
    --link: #1c5cab;
    --pos: #006300;
    --neg: #c62f2f;
    --series-1: #2a78d6;
    --series-neg: #e34948;
    --warn-bg: #fff4d6;
    --warn-text: #6b4a00;
    --error-bg: #fde8e8;
    --error-text: #8a1c1c;
    --success-bg: #e5f4e5;
    --success-text: #0f4d0f;
    --info-bg: #e6f0fb;
    --info-text: #184f95;
    --radius: 10px;
    --shadow: 0 1px 2px rgba(0, 0, 0, 0.04);
    --font: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}

@media (prefers-color-scheme: dark) {
    :root {
        color-scheme: dark;
        --page: #0d0d0d;
        --surface: #1a1a19;
        --surface-2: #242422;
        --border: rgba(255, 255, 255, 0.10);
        --grid: #2c2c2a;
        --axis: #383835;
        --text: #ffffff;
        --text-2: #c3c2b7;
        --muted: #9a988f;
        --accent: #3987e5;
        --accent-hover: #5598e7;
        --link: #6da7ec;
        --pos: #3cc43c;
        --neg: #ff7b7b;
        --series-1: #3987e5;
        --series-neg: #e66767;
        --warn-bg: #3a2e0a;
        --warn-text: #fad27a;
        --error-bg: #3d1515;
        --error-text: #ffb3b3;
        --success-bg: #12301a;
        --success-text: #9be29b;
        --info-bg: #13243b;
        --info-text: #9ec5f4;
        --shadow: none;
    }
}

* { box-sizing: border-box; }

html { -webkit-text-size-adjust: 100%; }

body {
    margin: 0;
    background: var(--page);
    color: var(--text);
    font: 15px/1.5 var(--font);
}

a { color: var(--link); text-decoration: none; }
a:hover { text-decoration: underline; }

code, pre.code {
    font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
    font-size: 0.9em;
    background: var(--surface-2);
    border-radius: 6px;
}
code { padding: 1px 5px; }
pre.code { padding: 10px 12px; overflow-x: auto; white-space: pre-wrap; }

.h1 { font-size: 26px; line-height: 1.2; margin: 0; font-weight: 650; }
.h2 { font-size: 20px; margin: 0 0 8px; font-weight: 600; }
.h3 { font-size: 16px; margin: 0; font-weight: 600; }
.muted { color: var(--muted); }
.small { font-size: 13px; }
.nowrap { white-space: nowrap; }
.pre { white-space: pre-wrap; }
.break { word-break: break-all; }
.pos { color: var(--pos); }
.neg { color: var(--neg); }
.sr-only {
    position: absolute; width: 1px; height: 1px; padding: 0; margin: -1px;
    overflow: hidden; clip: rect(0, 0, 0, 0); border: 0;
}

/* --- Каркас ------------------------------------------------------------------------------ */

.topbar {
    background: var(--surface);
    border-bottom: 1px solid var(--border);
    position: sticky;
    top: 0;
    z-index: 10;
}
.topbar-inner {
    max-width: 1320px;
    margin: 0 auto;
    padding: 0 16px;
    display: flex;
    align-items: center;
    gap: 20px;
    min-height: 56px;
}
.brand { font-weight: 700; color: var(--text); display: flex; align-items: center; gap: 8px; white-space: nowrap; }
.brand:hover { text-decoration: none; }
.brand-mark {
    display: inline-grid; place-items: center; width: 28px; height: 28px;
    background: var(--accent); color: #fff; border-radius: 8px; font-size: 16px;
}
.nav { display: flex; gap: 4px; overflow-x: auto; flex: 1; scrollbar-width: none; }
.nav::-webkit-scrollbar { display: none; }
.nav a {
    color: var(--text-2); padding: 6px 12px; border-radius: 8px; white-space: nowrap;
}
.nav a:hover { background: var(--surface-2); text-decoration: none; }
.nav a.active { background: var(--surface-2); color: var(--text); font-weight: 600; }
.logout { display: flex; align-items: center; gap: 8px; margin: 0; white-space: nowrap; }

.container { max-width: 1320px; margin: 0 auto; padding: 20px 16px 40px; }
.footer { max-width: 1320px; margin: 0 auto; padding: 0 16px 24px; }

.page-head {
    display: flex; justify-content: space-between; align-items: flex-end;
    gap: 12px; flex-wrap: wrap; margin-bottom: 16px;
}
.actions { display: flex; gap: 8px; flex-wrap: wrap; margin: 0; }

/* --- Карточки, сетки ----------------------------------------------------------------------- */

.card {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 16px;
    box-shadow: var(--shadow);
    margin-bottom: 16px;
    min-width: 0;
}
.card.flush { padding: 0; overflow: hidden; }
.card-head {
    display: flex; justify-content: space-between; align-items: baseline;
    gap: 12px; margin-bottom: 12px; flex-wrap: wrap;
}
.card-head.padded { padding: 16px 16px 0; }
.grid-2 { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 16px; }
.grid-2 > .card { margin-bottom: 0; }
.grid-2 { margin-bottom: 16px; }
.grid-profile { display: grid; grid-template-columns: minmax(0, 1fr) minmax(0, 2fr); gap: 16px; margin-bottom: 16px; }
.grid-profile > .card, .grid-profile > .kpis { margin-bottom: 0; }

@media (max-width: 900px) {
    .grid-2, .grid-profile { grid-template-columns: minmax(0, 1fr); }
}

/* --- Показатели (stat tiles) --------------------------------------------------------------- */

.kpis {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(170px, 1fr));
    gap: 12px;
    margin-bottom: 16px;
}
.kpi {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 12px 14px;
    min-width: 0;
}
.kpi-hero { grid-column: span 2; }
.kpi-label { font-size: 13px; color: var(--text-2); }
.kpi-value { font-size: 21px; font-weight: 600; line-height: 1.3; margin-top: 2px; white-space: nowrap; }
.kpi-value.hero { font-size: 40px; line-height: 1.15; }
.kpi-value.small-value { font-size: 18px; }
.kpi-meta { font-size: 13px; margin-top: 4px; display: flex; gap: 6px; flex-wrap: wrap; min-height: 18px; }
.kpis-compact .kpi-value.hero { font-size: 34px; }
.delta { font-weight: 600; }
.delta.up { color: var(--pos); }
.delta.down { color: var(--neg); }
.delta.flat { color: var(--muted); }

@media (max-width: 520px) {
    .kpi-hero { grid-column: 1 / -1; }
    .kpis { grid-template-columns: repeat(2, minmax(0, 1fr)); }
    .kpi-value.hero { font-size: 32px; }
}

.totals {
    display: flex; flex-wrap: wrap; gap: 8px 24px; align-items: baseline;
    padding: 10px 2px 14px;
}

/* --- Фильтры -------------------------------------------------------------------------------- */

.filters { display: flex; flex-wrap: wrap; gap: 8px 16px; align-items: center; margin-bottom: 4px; }
.presets, .tabs { display: flex; flex-wrap: wrap; gap: 6px; }
.range { display: flex; gap: 6px; align-items: center; margin: 0; flex-wrap: wrap; }
.range input[type=date] { width: 150px; }
.period-label { margin: 4px 0 16px; }
.pill {
    display: inline-block; padding: 4px 10px; border-radius: 999px;
    border: 1px solid var(--border); background: var(--surface); color: var(--text-2);
    font-size: 13px; white-space: nowrap;
}
.pill:hover { text-decoration: none; background: var(--surface-2); }
.pill.active { background: var(--text); color: var(--surface); border-color: var(--text); }
.toolbar {
    display: flex; flex-wrap: wrap; gap: 10px 16px; align-items: center;
    justify-content: space-between; margin-bottom: 12px;
}
.toolbar.filters-form { justify-content: flex-start; }
.search { display: flex; gap: 6px; margin: 0; flex: 1 1 280px; max-width: 460px; }
.search input { flex: 1; }
.check { display: inline-flex; align-items: center; gap: 6px; font-size: 14px; white-space: nowrap; }

/* --- Формы --------------------------------------------------------------------------------- */

input[type=text], input[type=search], input[type=password], input[type=tel], input[type=date],
input:not([type]), select, textarea {
    font: inherit;
    color: var(--text);
    background: var(--surface);
    border: 1px solid var(--axis);
    border-radius: 8px;
    padding: 7px 10px;
    min-width: 0;
    max-width: 100%;
}
input:focus, select:focus, textarea:focus, .btn:focus-visible, a:focus-visible {
    outline: 2px solid var(--accent);
    outline-offset: 1px;
}
textarea { width: 100%; resize: vertical; }
label { display: block; font-size: 14px; color: var(--text-2); }
label:not(.check) > input, label > select, label > textarea { display: block; width: 100%; margin-top: 4px; color: var(--text); }
.form-inline label:not(.check) > input, .form-inline label > select { width: auto; min-width: 140px; }
.hint { display: block; margin-top: 4px; font-size: 12px; color: var(--muted); }
.form-stack { display: flex; flex-direction: column; gap: 12px; }
.form-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(220px, 1fr)); gap: 14px 16px; align-items: start; }
.form-grid .span-2 { grid-column: 1 / -1; }
.form-inline { display: flex; flex-wrap: wrap; gap: 10px 14px; align-items: flex-end; }
.form-actions { display: flex; gap: 8px; margin-top: 4px; }
fieldset.card { margin: 0; }
fieldset legend { font-weight: 600; padding: 0 4px; }
.side { display: flex; flex-direction: column; gap: 12px; }
.money-input { display: flex; gap: 8px; }
.money-input input { flex: 1; font-size: 20px; font-weight: 600; }
label .money-input { margin-top: 4px; }
.money-input.compact input { font-size: inherit; font-weight: 400; }
.money-input.compact select { font-size: inherit; }
.money-input select { font-size: 16px; }
.sides { margin-bottom: 16px; }

.btn {
    display: inline-flex; align-items: center; justify-content: center; gap: 6px;
    font: inherit; font-size: 14px; font-weight: 500;
    padding: 7px 14px; border-radius: 8px; cursor: pointer;
    border: 1px solid var(--axis); background: var(--surface); color: var(--text);
    white-space: nowrap;
}
.btn:hover { background: var(--surface-2); text-decoration: none; }
.btn-primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn-primary:hover { background: var(--accent-hover); }
.btn-danger { color: var(--neg); border-color: var(--neg); }
.btn-small { padding: 5px 10px; font-size: 13px; }
.btn[disabled] { opacity: 0.5; cursor: not-allowed; }
.btn-link { background: none; border: 0; padding: 0; color: var(--link); cursor: pointer; font: inherit; }
.btn-link.danger { color: var(--neg); }
.button-row { display: flex; gap: 8px; flex-wrap: wrap; }
.button-row form { margin: 0; }
.danger-zone { margin-top: 16px; }

/* --- Таблицы -------------------------------------------------------------------------------- */

.table-wrap { overflow-x: auto; -webkit-overflow-scrolling: touch; }
.table { width: 100%; border-collapse: collapse; font-size: 14px; }
.table th {
    text-align: left; font-weight: 500; color: var(--text-2); font-size: 13px;
    padding: 8px 10px; border-bottom: 1px solid var(--grid); white-space: nowrap;
}
.table td { padding: 8px 10px; border-bottom: 1px solid var(--grid); vertical-align: top; }
.table tbody tr:last-child td { border-bottom: 0; }
.table tbody tr:hover td { background: var(--surface-2); }
.table .num { text-align: right; font-variant-numeric: tabular-nums; white-space: nowrap; }
.table.compact td, .table.compact th { padding: 4px 8px; }
.card.flush .table th:first-child, .card.flush .table td:first-child { padding-left: 16px; }
.card.flush .table th:last-child, .card.flush .table td:last-child { padding-right: 16px; }
.table .comment-row td { border-top: 0; padding-top: 0; }
.table tr:has(+ .comment-row) td { border-bottom: 0; }
.row-actions { white-space: nowrap; text-align: right; }
.row-actions form { display: inline; margin: 0 0 0 8px; }
a.sort { color: inherit; }
a.sort.active { color: var(--text); font-weight: 600; }
.clamp { max-width: 280px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

.meter-cell { min-width: 120px; white-space: nowrap; }
.meter {
    display: inline-block; width: 90px; height: 8px; border-radius: 4px;
    background: var(--surface-2); vertical-align: middle; margin-right: 6px; overflow: hidden;
}
.meter > span { display: block; height: 100%; background: var(--series-1); border-radius: 4px; }

.badge {
    display: inline-block; padding: 1px 8px; border-radius: 999px; font-size: 12px;
    background: var(--surface-2); color: var(--text-2); white-space: nowrap;
}
.badge-warn { background: var(--warn-bg); color: var(--warn-text); }

.abc-summary { display: flex; gap: 12px; flex-wrap: wrap; padding: 8px 16px 12px; }
.abc { font-size: 14px; }
.abc-badge {
    display: inline-grid; place-items: center; width: 24px; height: 24px; border-radius: 6px;
    font-weight: 700; font-size: 13px;
}
.abc-a { --abc: #1c5cab; }
.abc-b { --abc: #6da7ec; }
.abc-c { --abc: var(--axis); }
.abc-badge { background: var(--abc); color: #fff; }
.abc-badge.abc-c { color: var(--text); }
.abc strong { display: inline-grid; place-items: center; width: 22px; height: 22px; border-radius: 6px; background: var(--abc); color: #fff; margin-right: 4px; }
.abc-c strong { color: var(--text); }

/* --- Сообщения ------------------------------------------------------------------------------ */

.flash { padding: 10px 14px; border-radius: 8px; margin-bottom: 12px; font-size: 14px; background: var(--info-bg); color: var(--info-text); }
.flash-success { background: var(--success-bg); color: var(--success-text); }
.flash-error { background: var(--error-bg); color: var(--error-text); }
.flash-warning { background: var(--warn-bg); color: var(--warn-text); }
.flash a { color: inherit; text-decoration: underline; }

.empty { text-align: center; padding: 60px 16px; }
.empty-inline { padding: 16px; margin: 0; }
.onboarding { border-color: var(--accent); }

/* --- Карточка клиента ------------------------------------------------------------------------ */

.props { display: grid; grid-template-columns: max-content minmax(0, 1fr); gap: 8px 16px; margin: 0; }
.props dt { color: var(--muted); font-size: 13px; padding-top: 1px; }
.props dd { margin: 0; overflow-wrap: anywhere; }

/* --- Графики -------------------------------------------------------------------------------- */

.chart-card { display: flex; flex-direction: column; }
.chart-box { position: relative; height: 240px; }
.table-view { margin-top: 10px; }
.table-view summary { cursor: pointer; color: var(--text-2); }
.table-view .table-wrap { max-height: 260px; overflow-y: auto; }

/* --- Форма сделки: предпросмотр -------------------------------------------------------------- */

.preview dl { display: grid; grid-template-columns: max-content minmax(0, 1fr); gap: 6px 14px; margin: 8px 0 0; }
.preview dt { color: var(--muted); font-size: 13px; }
.preview dd { margin: 0; font-variant-numeric: tabular-nums; }
.preview .big { font-size: 24px; font-weight: 600; }
.preview ul { margin: 8px 0 0; padding-left: 18px; color: var(--warn-text); font-size: 13px; }
.preview.stale { opacity: 0.6; }

/* --- Вход ------------------------------------------------------------------------------------ */

body.guest { display: grid; place-items: center; min-height: 100vh; padding: 16px; }
.guest-box { width: 100%; max-width: 380px; }
.guest-brand { justify-content: center; margin-bottom: 16px; font-size: 18px; }

@media (max-width: 640px) {
    .topbar-inner { flex-wrap: wrap; gap: 8px 12px; padding-top: 8px; padding-bottom: 8px; }
    .nav { order: 3; flex-basis: 100%; }
    .logout { margin-left: auto; }
    .h1 { font-size: 22px; }
    .range input[type=date] { width: 140px; }
    .money-input input { font-size: 18px; }
}

@media print {
    .topbar, .filters, .actions, .row-actions, .toolbar, .pagination { display: none !important; }
    body { background: #fff; }
}

.pagination { display: flex; gap: 6px; align-items: center; flex-wrap: wrap; margin: 12px 0; }
