/* ============================================================
   Страница статистики территорий.
   Опирается на токены css/variables.css.
   ============================================================ */

:root {
    --stats-gain: #e01b0b;
    --stats-gain-soft: rgba(224, 27, 11, 0.14);
    --stats-gain-bar: rgba(224, 27, 11, 0.45);
    --stats-loss: #2f63b8;
    --stats-loss-soft: rgba(47, 99, 184, 0.14);
    --stats-loss-bar: rgba(47, 99, 184, 0.35);
    --stats-net: #1f2328;
    --stats-max-width: 1180px;
}

* {
    box-sizing: border-box;
}

html {
    scroll-behavior: smooth;
}

body.stats-page {
    margin: 0;
    min-height: 100vh;
    background: var(--gray-50);
    color: var(--text-main);
    font-family: var(--font-main);
    font-size: var(--font-size-md);
    line-height: 1.45;
}

.hidden {
    display: none !important;
}

a {
    color: var(--primary-dark);
}

code {
    font-family: var(--font-mono);
    font-size: 0.92em;
    background: var(--gray-100);
    border: 1px solid var(--gray-200);
    border-radius: var(--radius-sm);
    padding: 1px 5px;
}

/* ---------- Шапка ---------- */

.stats-header {
    position: sticky;
    top: 0;
    z-index: 20;
    background: rgba(255, 255, 255, 0.96);
    border-bottom: 1px solid var(--gray-200);
    box-shadow: 0 1px 8px rgba(0, 0, 0, 0.06);
    backdrop-filter: blur(6px);
}

.stats-header-inner {
    max-width: var(--stats-max-width);
    margin: 0 auto;
    padding: 12px 20px;
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 16px;
}

.stats-brand {
    font-family: var(--font-logo);
    font-size: var(--font-size-xl);
    letter-spacing: 1px;
    color: var(--brand);
    text-decoration: none;
    white-space: nowrap;
}

.stats-nav {
    display: flex;
    align-items: center;
    gap: 14px;
    font-size: var(--font-size-sm);
}

.stats-nav-link {
    display: inline-flex;
    align-items: center;
    gap: 4px;
    color: var(--text-secondary);
    text-decoration: none;
    padding: 6px 10px;
    border-radius: var(--radius-md);
    transition: background var(--transition-fast), color var(--transition-fast);
}

.stats-nav-icon {
    flex-shrink: 0;
}

.stats-nav-link:hover {
    background: var(--gray-100);
    color: var(--text-main);
}

.stats-nav-current {
    color: var(--text-main);
    font-weight: 700;
    padding: 6px 10px;
    border-radius: var(--radius-md);
    background: var(--gray-100);
}

/* ---------- Контейнер ---------- */

.stats-main {
    max-width: var(--stats-max-width);
    margin: 0 auto;
    padding: 26px 20px 70px;
}

.stats-title-row {
    display: flex;
    align-items: flex-start;
    justify-content: space-between;
    gap: 18px;
    margin-bottom: 20px;
}

.stats-title-row h1 {
    margin: 0 0 6px;
    font-size: var(--font-size-2xl);
    line-height: 1.15;
}

.stats-subtitle {
    margin: 0;
    max-width: 720px;
    color: var(--text-secondary);
}

.stats-updated {
    flex-shrink: 0;
    margin-top: 4px;
    font-size: var(--font-size-xs);
    color: var(--text-muted);
    text-align: right;
    white-space: nowrap;
}

/* ---------- Карточки сводки ---------- */

.stats-cards {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(240px, 1fr));
    gap: 14px;
    margin-bottom: 18px;
}

.stats-card {
    position: relative;
    display: flex;
    flex-direction: column;
    background: var(--surface);
    border: 1px solid var(--gray-200);
    border-radius: var(--radius-xl);
    padding: 16px 18px 15px;
    box-shadow: var(--shadow-sm);
    overflow: hidden;
}

.stats-card.is-gain {
    background: linear-gradient(180deg, rgba(224, 27, 11, 0.055), var(--surface) 70%);
    border-color: rgba(224, 27, 11, 0.18);
}

.stats-card.is-loss {
    background: linear-gradient(180deg, rgba(47, 99, 184, 0.06), var(--surface) 70%);
    border-color: rgba(47, 99, 184, 0.20);
}

.stats-card::before {
    content: "";
    position: absolute;
    inset: 0 auto 0 0;
    width: 4px;
    background: var(--gray-300);
}

.stats-card.is-gain::before { background: var(--stats-gain); }
.stats-card.is-loss::before { background: var(--stats-loss); }
.stats-card.is-total::before { background: var(--stats-net); }

.stats-card-label {
    display: block;
    margin-bottom: 6px;
    color: var(--text-muted);
    font-size: var(--font-size-xs);
    font-weight: 700;
    letter-spacing: 0.04em;
    text-transform: uppercase;
}

.stats-card-value {
    display: block;
    font-size: 1.7rem;
    font-weight: 700;
    line-height: 1.1;
    font-variant-numeric: tabular-nums;
    letter-spacing: -0.01em;
}

.stats-card-value.gain { color: var(--stats-gain); }
.stats-card-value.loss { color: var(--stats-loss); }
.stats-card-value.neutral { color: var(--stats-net); }

.stats-card-context {
    display: block;
    margin-top: 6px;
    color: var(--text-muted);
    font-size: 11px;
    line-height: 1.35;
}

.stats-card-lines {
    display: grid;
    gap: 3px;
    margin-top: auto;
    padding-top: 10px;
}

.stats-card-line {
    display: flex;
    align-items: baseline;
    justify-content: space-between;
    gap: 8px;
    font-size: var(--font-size-xs);
    line-height: 1.35;
}

.stats-card-line-label {
    color: var(--text-muted);
}

.stats-card-line-value {
    color: var(--text-main);
    font-variant-numeric: tabular-nums;
    text-align: right;
}

.stats-card-line-value.gain { color: var(--stats-gain); }
.stats-card-line-value.loss { color: var(--stats-loss); }
.stats-card-line-value.neutral { color: var(--text-secondary); }

/* ---------- Панели ---------- */

.stats-panel {
    background: var(--surface);
    border: 1px solid var(--gray-200);
    border-radius: var(--radius-xl);
    box-shadow: var(--shadow-sm);
    padding: 20px 22px 18px;
    margin-bottom: 18px;
}

.stats-panel h2 {
    margin: 0;
    font-size: var(--font-size-lg);
    white-space: nowrap;
}

.stats-panel-head {
    display: flex;
    align-items: flex-start;
    justify-content: space-between;
    gap: 16px;
    margin-bottom: 14px;
}

.stats-panel-sub {
    margin: 5px 0 0;
    font-size: var(--font-size-xs);
    color: var(--text-muted);
}

.stats-panel-title-row {
    display: flex;
    align-items: center;
    flex-wrap: wrap;
    gap: 8px;
}

.stats-granularity {
    display: inline-flex;
    border: 1px solid var(--gray-200);
    border-radius: 6px;
    overflow: hidden;
}

.stats-granularity button {
    background: var(--surface);
    border: 0;
    color: var(--text-muted);
    cursor: pointer;
    font: inherit;
    font-size: var(--font-size-xs);
    font-weight: 600;
    padding: 4px 8px;
}

.stats-granularity button + button {
    border-left: 1px solid var(--gray-200);
}

.stats-granularity button.is-active {
    background: var(--gray-100);
    color: var(--text-main);
}

.stats-icon-btn {
    align-items: center;
    background: var(--surface);
    border: 1px solid var(--gray-200);
    border-radius: 6px;
    color: var(--text-muted);
    cursor: pointer;
    display: inline-flex;
    height: 26px;
    justify-content: center;
    padding: 0;
    width: 26px;
}

.stats-icon-btn:hover,
.stats-icon-btn.is-active {
    border-color: var(--stats-gain);
    color: var(--stats-gain);
}

.stats-icon-btn.is-active {
    background: var(--stats-gain-soft);
}

.stats-icon-btn svg {
    display: block;
}

.stats-chart-note {
    margin: 7px 0 0;
    font-size: var(--font-size-xs);
    color: var(--text-muted);
}

.stats-chart-tooltip {
    position: absolute;
    z-index: 8;
    box-sizing: border-box;
    min-width: 210px;
    max-width: 280px;
    padding: 10px 12px;
    background: rgba(31, 35, 40, 0.97);
    border-radius: 8px;
    box-shadow: 0 6px 22px rgba(0, 0, 0, 0.28);
    color: #fff;
    pointer-events: none;
    transform: translate(-50%, calc(-100% - 10px));
}

.stats-chart-tooltip.is-below {
    transform: translate(-50%, 12px);
}

.stats-chart-tooltip.hidden {
    display: none;
}

.stats-chart-tooltip-title {
    color: rgba(255, 255, 255, 0.65);
    font-size: 11px;
    font-weight: 600;
    letter-spacing: 0.05em;
    text-transform: uppercase;
}

.stats-chart-tooltip-total {
    display: flex;
    align-items: baseline;
    flex-wrap: wrap;
    gap: 7px;
    margin: 5px 0 8px;
    padding-bottom: 8px;
    border-bottom: 1px solid rgba(255, 255, 255, 0.18);
    font-size: 10px;
    font-weight: 700;
    letter-spacing: 0.08em;
    line-height: 1.4;
}

.stats-chart-tooltip-total strong {
    font-size: 16px;
    letter-spacing: 0;
}

.stats-chart-tooltip-total.gain strong { color: #ffb3aa; }
.stats-chart-tooltip-total.loss strong { color: #9ec5f5; }

.stats-chart-tooltip-row {
    display: flex;
    justify-content: space-between;
    gap: 16px;
    color: rgba(255, 255, 255, 0.78);
    font-size: 12px;
    line-height: 1.75;
}

.stats-chart-tooltip-row b { font-weight: 600; }
.stats-chart-tooltip-row b.gain { color: #ffb3aa; }
.stats-chart-tooltip-row b.loss { color: #9ec5f5; }

.stats-chart-tooltip-meta {
    margin-top: 7px;
    padding-top: 7px;
    border-top: 1px solid rgba(255, 255, 255, 0.13);
    color: rgba(255, 255, 255, 0.58);
    font-size: 11px;
    line-height: 1.55;
}

.stats-chart-wrap {
    position: relative;
    height: 600px;
    max-height: 80vh;
    overflow-y: auto;
    overflow-x: hidden;
    scrollbar-width: thin;
}

.stats-chart-wrap canvas {
    width: 100% !important;
    height: 100% !important;
}

.stats-legend {
    display: flex;
    flex-wrap: wrap;
    gap: 12px;
    font-size: var(--font-size-xs);
    color: var(--text-secondary);
    padding-top: 4px;
}

.stats-legend-item {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    white-space: nowrap;
}

.stats-dot {
    width: 9px;
    height: 9px;
    border-radius: 2px;
    display: inline-block;
}

.stats-dot-gain { background: var(--stats-gain-bar); }
.stats-dot-loss { background: var(--stats-loss-bar); }
.stats-dot-net { background: linear-gradient(90deg, var(--stats-gain) 0 50%, var(--stats-loss) 50% 100%); height: 3px; border-radius: 2px; }

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

.stats-table-wrap {
    overflow-x: auto;
    border: 1px solid var(--gray-200);
    border-radius: var(--radius-lg);
}

.stats-table {
    width: 100%;
    min-width: 820px;
    border-collapse: collapse;
    font-size: var(--font-size-sm);
}

.stats-table th,
.stats-table td {
    padding: 9px 12px;
    border-bottom: 1px solid var(--gray-200);
    text-align: left;
    white-space: nowrap;
}

.stats-table th {
    background: var(--gray-50);
    color: var(--text-secondary);
    font-weight: 700;
    font-size: var(--font-size-xs);
    letter-spacing: 0.02em;
    text-transform: uppercase;
}

.stats-table th.num,
.stats-table td.num {
    text-align: right;
    font-variant-numeric: tabular-nums;
}

.stats-table tbody tr:last-child td {
    border-bottom: none;
}

.stats-table tbody tr:hover {
    background: var(--gray-50);
}

.stats-table tbody tr.is-partial {
    background: #fff8e1;
}

.stats-table tbody tr.is-partial:hover {
    background: #fff3cc;
}

.stats-table .month-cell {
    font-weight: 700;
}

.stats-table .gain { color: var(--stats-gain); }
.stats-table .loss { color: var(--stats-loss); }
.stats-table .net-pos { color: var(--stats-gain); font-weight: 700; }
.stats-table .net-neg { color: var(--stats-loss); font-weight: 700; }
.stats-table .muted { color: var(--text-muted); }

.stats-badge {
    display: inline-block;
    margin-left: 7px;
    padding: 1px 6px;
    border-radius: 999px;
    background: #ffe9a8;
    color: #7a5b00;
    font-size: 10px;
    font-weight: 700;
    letter-spacing: 0.02em;
    text-transform: uppercase;
    vertical-align: 1px;
}

.stats-matrix-wrap {
    overflow-x: auto;
    max-width: 100%;
    scrollbar-width: thin;
}

.stats-table--matrix {
    width: max-content;
    min-width: 100%;
    border-collapse: separate;
    border-spacing: 0;
    font-size: 12px;
}

.stats-table--matrix th,
.stats-table--matrix td {
    padding: 6px 8px;
    border-bottom: 1px solid var(--gray-200);
    white-space: nowrap;
}

.stats-table--matrix thead th {
    background: var(--gray-50);
}

.stats-table--matrix th.stats-matrix-name,
.stats-table--matrix td.stats-matrix-name {
    position: sticky;
    right: 0;
    z-index: 2;
    width: 260px;
    min-width: 260px;
    max-width: 300px;
    white-space: normal;
    text-align: left;
    background: var(--surface);
    box-shadow: -1px 0 0 var(--gray-200);
}

.stats-table--matrix thead th.stats-matrix-name {
    z-index: 3;
    background: var(--gray-50);
}

.stats-table--matrix tbody tr:hover td {
    background: var(--gray-50);
}

.stats-table--matrix tbody tr.is-selected td {
    background: #fff8e1;
}

.stats-table--matrix td.num,
.stats-table--matrix th.num {
    min-width: 54px;
}

.stats-table--matrix .stats-matrix-name,
.stats-table--matrix .stats-matrix-total {
    font-weight: 700;
}

.stats-table--matrix .stats-matrix-total {
    border-right: 1px solid var(--gray-300);
}

.stats-table--matrix .stats-matrix-year {
    text-align: center;
    border-left: 1px solid var(--gray-300);
}

.stats-table--matrix .is-year-start {
    border-left: 1px solid var(--gray-300);
}

.stats-table--matrix tbody tr.is-inactive {
    opacity: 0.55;
}

.stats-table--matrix tbody tr.is-inactive:hover {
    opacity: 0.78;
}

.stats-table--matrix td.net-pos { color: var(--stats-gain); }
.stats-table--matrix td.net-neg { color: var(--stats-loss); }
.stats-table--matrix td.is-zero { color: var(--text-muted); }

.stats-table--matrix tfoot td {
    border-top: 2px solid var(--gray-300);
    border-bottom: none;
    background: var(--gray-50);
    font-weight: 700;
}

.stats-png-wide {
    width: max-content;
    max-width: none;
}

.stats-png-wide .stats-matrix-wrap {
    overflow: visible;
}

.stats-table tbody tr.is-total td {
    font-weight: 700;
    background: var(--gray-50);
}

.stats-table tbody tr.is-unassigned {
    background: var(--gray-50);
}

.stats-table tbody tr.is-unassigned td:first-child {
    color: var(--text-secondary);
}

.stats-note {
    margin: 12px 0 0;
    font-size: var(--font-size-xs);
    color: var(--text-muted);
}

/* ---------- Фильтр периода и навигация ---------- */

.stats-toolbar {
    display: flex;
    align-items: center;
    gap: 14px;
    flex-wrap: wrap;
    margin-bottom: 18px;
    padding: 10px 14px;
    background: var(--surface);
    border: 1px solid var(--gray-200);
    border-radius: var(--radius-lg);
    box-shadow: var(--shadow-sm);
}

.stats-range,
.stats-inline-select {
    display: inline-flex;
    align-items: center;
    gap: 8px;
    color: var(--text-secondary);
    font-size: var(--font-size-xs);
    font-weight: 700;
    letter-spacing: 0.02em;
    text-transform: uppercase;
}

.stats-range select,
.stats-inline-select select {
    height: 34px;
    padding: 0 28px 0 10px;
    border: 1px solid var(--gray-300);
    border-radius: var(--radius-md);
    background: var(--surface);
    color: var(--text-main);
    font: inherit;
    text-transform: none;
    letter-spacing: 0;
    font-weight: 400;
}

.stats-toolbar-stats {
    display: inline-flex;
    align-items: center;
    flex-wrap: wrap;
    gap: 10px;
    color: var(--text-secondary);
    font-size: var(--font-size-xs);
    font-variant-numeric: tabular-nums;
}

.stats-toolbar-stats .gain { color: var(--stats-gain); font-weight: 600; }
.stats-toolbar-stats .loss { color: var(--stats-loss); font-weight: 600; }
.stats-toolbar-stats .net { font-weight: 700; }
.stats-toolbar-stats .sep { color: var(--gray-400); font-weight: 400; }

.stats-toolbar.is-gain {
    background: linear-gradient(180deg, rgba(224, 27, 11, 0.055), var(--surface) 70%);
    border-color: rgba(224, 27, 11, 0.18);
}

.stats-toolbar.is-loss {
    background: linear-gradient(180deg, rgba(47, 99, 184, 0.06), var(--surface) 70%);
    border-color: rgba(47, 99, 184, 0.20);
}

.stats-toolbar-stats .net.gain { color: var(--stats-gain); }
.stats-toolbar-stats .net.loss { color: var(--stats-loss); }

.stats-jump {
    display: flex;
    gap: 14px;
    flex-wrap: wrap;
    margin-left: auto;
}

.stats-jump a {
    color: var(--text-secondary);
    font-size: var(--font-size-xs);
    text-decoration: none;
}

.stats-jump a:hover {
    color: var(--stats-gain);
}

.stats-panel-actions {
    display: flex;
    align-items: center;
    gap: 14px;
    flex-wrap: wrap;
}

.stats-text-btn {
    background: none;
    border: 0;
    padding: 0;
    color: var(--stats-gain);
    cursor: pointer;
    font: inherit;
    font-size: var(--font-size-xs);
    font-weight: 600;
    text-decoration: underline;
    text-underline-offset: 2px;
}

.stats-text-btn:hover {
    color: #a8160a;
}

.stats-text-btn.hidden {
    display: none;
}

.stats-map-link {
    color: var(--stats-gain);
    font-size: var(--font-size-xs);
    text-decoration: none;
    white-space: nowrap;
}

.stats-map-link:hover {
    text-decoration: underline;
}

/* ---------- Направления и группировки ---------- */

.stats-table tbody tr.is-clickable {
    cursor: pointer;
}

.stats-table tbody tr.is-selected {
    background: #fff8e1;
    box-shadow: inset 3px 0 0 var(--stats-gain);
}

.stats-table tbody tr.is-selected:hover {
    background: #fff3cc;
}

.stats-table--compact {
    min-width: 620px;
}

.stats-direction-detail {
    margin-top: 20px;
    padding-top: 18px;
    border-top: 1px solid var(--gray-200);
}

.stats-direction-tools {
    display: flex;
    align-items: center;
    gap: 12px;
    flex-wrap: wrap;
}

.stats-direction-head {
    display: flex;
    align-items: flex-start;
    justify-content: space-between;
    gap: 16px;
    flex-wrap: wrap;
}

.stats-direction-head h3 {
    margin: 0;
    font-size: var(--font-size-lg);
}

.stats-direction-totals {
    display: flex;
    gap: 14px;
    flex-wrap: wrap;
    font-size: var(--font-size-sm);
    font-variant-numeric: tabular-nums;
}

.stats-direction-totals .gain { color: var(--stats-gain); }
.stats-direction-totals .loss { color: var(--stats-loss); }
.stats-direction-totals .net { font-weight: 700; }

.stats-chart-wrap--small {
    height: 300px;
    margin-top: 8px;
}

.stats-chart-wrap--total {
    height: 360px;
}

/* ---------- Пустое состояние ---------- */

.stats-empty {
    max-width: 620px;
    margin: 12vh auto 0;
    padding: 30px;
    background: var(--surface);
    border: 1px solid var(--gray-200);
    border-radius: var(--radius-xl);
    box-shadow: var(--shadow-sm);
    text-align: center;
}

.stats-empty h1 {
    margin: 0 0 10px;
    font-size: var(--font-size-xl);
}

.stats-empty p {
    margin: 0;
    color: var(--text-secondary);
}

/* ---------- Адаптив ---------- */

@media (max-width: 1020px) {
    .stats-cards {
        grid-template-columns: repeat(2, minmax(0, 1fr));
    }
}

@media (max-width: 760px) {
    .stats-header-inner {
        padding: 10px 14px;
    }

    .stats-brand {
        font-size: 1.2rem;
    }

    .stats-nav {
        gap: 6px;
    }

    .stats-nav-current {
        display: none;
    }

    .stats-main {
        padding: 18px 14px 50px;
    }

    .stats-title-row {
        flex-direction: column;
        gap: 8px;
        margin-bottom: 16px;
    }

    .stats-title-row h1 {
        font-size: 1.55rem;
    }

    .stats-updated {
        text-align: left;
        white-space: normal;
    }

    .stats-cards {
        grid-template-columns: 1fr;
    }

    .stats-panel {
        padding: 16px 14px 14px;
        border-radius: var(--radius-lg);
    }

    .stats-panel-head {
        flex-direction: column;
        gap: 8px;
    }

    .stats-chart-wrap {
        height: 440px;
        max-height: 80vh;
    }

    .stats-table {
        min-width: 720px;
        font-size: var(--font-size-xs);
    }

    .stats-table th,
    .stats-table td {
        padding: 8px 9px;
    }

    .stats-toolbar {
        align-items: flex-start;
        gap: 10px;
    }

    .stats-jump {
        width: 100%;
        margin-left: 0;
    }

    .stats-panel-actions {
        width: 100%;
        align-items: flex-start;
    }

    .stats-direction-head {
        flex-direction: column;
        gap: 6px;
    }

    .stats-chart-wrap--small {
        height: 260px;
    }

    .stats-chart-wrap--total {
        height: 300px;
    }

    .stats-table--matrix th.stats-matrix-name,
    .stats-table--matrix td.stats-matrix-name {
        width: 190px;
        min-width: 190px;
    }

    .stats-legend {
        gap: 9px;
    }
}
