:root {
    --bg: #0d1117;
    --bg-card: #161b22;
    --bg-card-2: #1c232c;
    --border: #30363d;
    --text: #e6edf3;
    --text-dim: #8b949e;
    --accent: #58a6ff;
    --green: #3fb950;
    --orange: #d29922;
    --red: #f85149;
    --purple: #bc8cff;
}

* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; }
body {
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
    background: var(--bg);
    color: var(--text);
    font-size: 14px;
    min-height: 100vh;
}

.topbar {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 18px 28px;
    border-bottom: 1px solid var(--border);
    background: var(--bg-card);
    position: sticky;
    top: 0;
    z-index: 10;
}
.brand-title {
    font-size: 18px;
    font-weight: 600;
}
.brand-sub {
    font-size: 12px;
    color: var(--text-dim);
    margin-top: 2px;
}
.switchers {
    display: flex;
    align-items: center;
    gap: 18px;
    flex-wrap: wrap;
}
.switcher-group {
    display: flex;
    align-items: center;
    gap: 8px;
}
.switcher-label {
    font-size: 11px;
    color: var(--text-dim);
    text-transform: uppercase;
    letter-spacing: 0.05em;
}
.dataset-switcher,
.model-switcher {
    display: flex;
    gap: 4px;
    background: var(--bg-card-2);
    padding: 4px;
    border-radius: 8px;
    border: 1px solid var(--border);
}
.ds-btn,
.model-btn {
    background: transparent;
    border: 0;
    color: var(--text-dim);
    padding: 7px 14px;
    border-radius: 6px;
    cursor: pointer;
    font-size: 13px;
    font-weight: 500;
    transition: background 0.15s, color 0.15s;
    white-space: nowrap;
}
.ds-btn:hover,
.model-btn:hover { color: var(--text); }
.ds-btn.active {
    background: var(--accent);
    color: #fff;
}
.model-btn.active {
    background: var(--purple);
    color: #fff;
}
.model-hint {
    margin: 18px 28px 0;
    padding: 12px 18px;
    border-left: 3px solid var(--purple);
    font-size: 12px;
    color: var(--text-dim);
    line-height: 1.5;
}
.model-hint strong { color: var(--text); }

.pdf-btn {
    display: inline-flex;
    align-items: center;
    gap: 7px;
    background: transparent;
    border: 1px solid var(--border);
    color: var(--text);
    padding: 7px 14px;
    border-radius: 8px;
    cursor: pointer;
    font-size: 13px;
    font-weight: 500;
    transition: background 0.15s, color 0.15s, border-color 0.15s;
}
.pdf-btn:hover { background: var(--accent); border-color: var(--accent); color: #fff; }
.pdf-btn:disabled { opacity: 0.5; cursor: progress; }

.method-card {
    border-left: 3px solid var(--accent);
}
.method-card p {
    margin: 0 0 10px;
    color: var(--text-dim);
    font-size: 13px;
    line-height: 1.55;
}
.method-card p strong { color: var(--text); }
.formula {
    background: var(--bg-card-2);
    border: 1px solid var(--border);
    border-radius: 6px;
    padding: 12px 16px;
    margin: 10px 0;
    font-family: ui-monospace, "SF Mono", Menlo, Consolas, monospace;
    font-size: 12px;
    color: var(--text);
    line-height: 1.7;
}
.formula div { white-space: pre-wrap; }

details.method {
    margin: 0 0 14px;
    background: var(--bg-card-2);
    border: 1px solid var(--border);
    border-radius: 6px;
    padding: 0;
    font-size: 12px;
    color: var(--text-dim);
}
details.method > summary {
    padding: 8px 14px;
    cursor: pointer;
    user-select: none;
    color: var(--text);
    font-weight: 500;
    list-style: none;
    display: flex;
    align-items: center;
    gap: 8px;
}
details.method > summary::-webkit-details-marker { display: none; }
details.method > summary::before {
    content: "▸";
    color: var(--accent);
    font-size: 10px;
    transition: transform 0.15s;
    display: inline-block;
}
details.method[open] > summary::before { transform: rotate(90deg); }
details.method > ul {
    margin: 0;
    padding: 4px 16px 12px 32px;
    line-height: 1.55;
}
details.method li {
    margin-bottom: 4px;
}
details.method code {
    font-family: ui-monospace, "SF Mono", Menlo, Consolas, monospace;
    font-size: 11px;
    background: rgba(88, 166, 255, 0.10);
    padding: 1px 5px;
    border-radius: 3px;
    color: var(--accent);
}

.print-only { display: none; }

@media print {
    @page {
        size: A4;
        margin: 12mm 10mm;
    }

    /* Force background colors/images to print (Chrome/Edge) */
    * {
        -webkit-print-color-adjust: exact !important;
        print-color-adjust: exact !important;
        color-adjust: exact !important;
    }

    html, body {
        background: #fff !important;
        color: #1f2328 !important;
        font-size: 11px;
    }

    .topbar,
    .model-hint,
    .switchers,
    .pdf-btn,
    .foot,
    .legend { display: none !important; }

    main {
        padding: 0 !important;
        max-width: 100% !important;
        margin: 0 !important;
    }

    .print-only { display: block !important; }
    .pdf-cover {
        page-break-after: always;
        break-after: page;
        padding: 30px 20px 20px !important;
    }

    .card {
        background: #fff !important;
        border: 1px solid #d0d7de !important;
        color: #1f2328 !important;
        page-break-inside: avoid;
        break-inside: avoid;
        margin: 0 0 10px !important;
        padding: 12px 14px !important;
        box-shadow: none !important;
    }
    .card h2 { color: #111 !important; font-size: 13px !important; margin: 0 0 6px !important; }
    .hint { color: #57606a !important; font-size: 10px !important; margin: 0 0 8px !important; }
    .hint span[style*="color"] { color: inherit !important; }

    /* KPI grid — fit on one row */
    .kpi-grid {
        grid-template-columns: repeat(6, 1fr) !important;
        gap: 6px !important;
        margin: 0 0 10px !important;
        page-break-inside: avoid;
        break-inside: avoid;
    }
    .kpi-card {
        background: #fff !important;
        border: 1px solid #d0d7de !important;
        color: #1f2328 !important;
        padding: 8px 10px !important;
    }
    .kpi-card.kpi-positive  { border-left: 2px solid #1f883d !important; }
    .kpi-card.kpi-forecast  { border-left: 2px solid #0969da !important; }
    .kpi-card.kpi-warn      { border-left: 2px solid #9a6700 !important; }
    .kpi-label { color: #57606a !important; font-size: 9px !important; }
    .kpi-value { color: #111 !important; font-size: 14px !important; }
    .kpi-sub   { color: #57606a !important; font-size: 9px !important; }

    /* Methodology blocks */
    .method-card { border-left: 3px solid #0969da !important; }
    .method-card p { color: #444 !important; font-size: 11px !important; line-height: 1.5; }
    .method-card p strong { color: #111 !important; }
    .formula {
        background: #f6f8fa !important;
        border: 1px solid #d0d7de !important;
        color: #111 !important;
        font-size: 10px !important;
        padding: 8px 10px !important;
    }

    details.method {
        background: #f6f8fa !important;
        border: 1px solid #d0d7de !important;
        color: #444 !important;
        font-size: 10px !important;
        margin: 0 0 8px !important;
    }
    details.method[open] > summary,
    details.method > summary { color: #111 !important; padding: 6px 10px !important; }
    details.method > summary::before { color: #0969da !important; }
    details.method > ul { padding: 2px 12px 8px 26px !important; }
    details.method code {
        background: #eaf3ff !important;
        color: #0969da !important;
        font-size: 10px !important;
    }

    /* Charts — fixed sizes for predictable print layout */
    .chart-wrap { height: 220px !important; }
    .card.half .chart-wrap { height: 190px !important; }
    .row {
        grid-template-columns: 1fr 1fr !important;
        gap: 10px !important;
        page-break-inside: avoid;
        break-inside: avoid;
    }

    /* Heatmap — fit page width by scaling cells */
    .heatmap-wrap {
        background: #fff !important;
        border: 1px solid #d0d7de !important;
        max-height: none !important;
        overflow: visible !important;
    }
    .hm-grid { font-size: 7px !important; }
    .hm-cell  { width: 20px !important; height: 14px !important; color: rgba(0,0,0,0.85) !important; }
    .hm-row-header {
        background: #fff !important;
        color: #1f2328 !important;
        border-right: 1px solid #d0d7de !important;
        font-size: 9px !important;
        height: 14px !important;
        padding: 0 6px !important;
    }
    .hm-row-header.partial { color: #9a6700 !important; }
    .hm-row-header .partial-badge {
        border-color: #9a6700 !important;
        color: #9a6700 !important;
    }
    .hm-col-header {
        background: #fff !important;
        color: #57606a !important;
        border-bottom: 1px solid #d0d7de !important;
        font-size: 8px !important;
        height: 14px !important;
    }
    .hm-corner {
        background: #fff !important;
        border-right: 1px solid #d0d7de !important;
        border-bottom: 1px solid #d0d7de !important;
        height: 14px !important;
    }

    /* Table */
    .table-wrap { max-height: none !important; overflow: visible !important; }
    table { font-size: 10px !important; }
    thead th {
        background: #f6f8fa !important;
        color: #57606a !important;
        border-bottom: 1px solid #d0d7de !important;
        padding: 6px 8px !important;
    }
    tbody td {
        border-bottom: 1px solid #eaeef2 !important;
        color: #1f2328 !important;
        padding: 4px 8px !important;
    }
    tbody td:first-child { color: #57606a !important; }
    tbody tr.partial td { color: #9a6700 !important; }

    .badge-partial-inline {
        border-color: #9a6700 !important;
        color: #9a6700 !important;
    }

    /* Page breaks */
    section.card:has(canvas#chart-timeline) { page-break-before: avoid; }
    section.card:has(canvas#chart-decay),
    section.card:has(canvas#chart-cohorts) { page-break-before: auto; }
    section.card:has(#heatmap) { page-break-before: always; }
    section.card:has(#cohort-table) { page-break-before: always; }
}

main {
    padding: 24px 28px 60px;
    max-width: 1500px;
    margin: 0 auto;
}

.kpi-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(220px, 1fr));
    gap: 14px;
    margin-bottom: 24px;
}
.kpi-card {
    background: var(--bg-card);
    border: 1px solid var(--border);
    border-radius: 10px;
    padding: 16px 18px;
}
.kpi-card.kpi-positive { border-left: 3px solid var(--green); }
.kpi-card.kpi-forecast { border-left: 3px solid var(--accent); }
.kpi-card.kpi-warn { border-left: 3px solid var(--orange); }
.kpi-label {
    font-size: 12px;
    color: var(--text-dim);
    text-transform: uppercase;
    letter-spacing: 0.05em;
}
.kpi-value {
    font-size: 22px;
    font-weight: 600;
    margin-top: 6px;
    line-height: 1.2;
}
.kpi-sub {
    font-size: 12px;
    color: var(--text-dim);
    margin-top: 4px;
}

.card {
    background: var(--bg-card);
    border: 1px solid var(--border);
    border-radius: 10px;
    padding: 20px 22px;
    margin-bottom: 18px;
}
.card h2 {
    margin: 0 0 6px;
    font-size: 15px;
    font-weight: 600;
}
.hint {
    margin: 0 0 14px;
    color: var(--text-dim);
    font-size: 12px;
    line-height: 1.45;
}
.row {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 18px;
    margin-bottom: 0;
}
.row .card { margin-bottom: 18px; }
@media (max-width: 1100px) {
    .row { grid-template-columns: 1fr; }
}

.chart-wrap {
    position: relative;
    width: 100%;
    height: 360px;
}
.card.half .chart-wrap { height: 320px; }

.table-wrap {
    overflow-x: auto;
    max-height: 480px;
    overflow-y: auto;
}
table {
    width: 100%;
    border-collapse: collapse;
    font-size: 13px;
}
thead th {
    position: sticky;
    top: 0;
    background: var(--bg-card-2);
    color: var(--text-dim);
    font-weight: 500;
    text-align: right;
    padding: 10px 12px;
    border-bottom: 1px solid var(--border);
    white-space: nowrap;
}
thead th:first-child { text-align: left; }
tbody td {
    padding: 8px 12px;
    border-bottom: 1px solid var(--border);
    text-align: right;
    font-variant-numeric: tabular-nums;
}
tbody td:first-child { text-align: left; color: var(--text-dim); }
tbody tr:hover { background: var(--bg-card-2); }

.heatmap-wrap {
    overflow-x: auto;
    max-height: 620px;
    overflow-y: auto;
    border: 1px solid var(--border);
    border-radius: 6px;
    background: var(--bg-card-2);
}
.hm-grid {
    display: grid;
    grid-auto-flow: row;
    font-size: 10px;
    line-height: 1;
    min-width: max-content;
}
.hm-cell {
    width: 32px;
    height: 22px;
    display: flex;
    align-items: center;
    justify-content: center;
    color: rgba(255, 255, 255, 0.85);
    border-right: 1px solid rgba(0, 0, 0, 0.25);
    border-bottom: 1px solid rgba(0, 0, 0, 0.25);
    font-variant-numeric: tabular-nums;
    cursor: default;
    user-select: none;
}
.hm-cell.empty { background: rgba(255, 255, 255, 0.02); color: transparent; }
.hm-row-header {
    position: sticky;
    left: 0;
    background: var(--bg-card);
    border-right: 1px solid var(--border);
    padding: 0 10px;
    font-size: 11px;
    display: flex;
    align-items: center;
    color: var(--text);
    white-space: nowrap;
    z-index: 2;
    height: 22px;
}
.hm-row-header.partial { color: var(--orange); }
.hm-row-header .partial-badge {
    margin-left: 6px;
    font-size: 9px;
    padding: 1px 5px;
    border: 1px solid var(--orange);
    border-radius: 3px;
    text-transform: uppercase;
    letter-spacing: 0.04em;
}
.hm-col-header {
    position: sticky;
    top: 0;
    background: var(--bg-card);
    border-bottom: 1px solid var(--border);
    color: var(--text-dim);
    font-size: 10px;
    height: 24px;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 0 2px;
    white-space: nowrap;
    z-index: 1;
}
.hm-corner {
    position: sticky;
    top: 0;
    left: 0;
    background: var(--bg-card);
    border-right: 1px solid var(--border);
    border-bottom: 1px solid var(--border);
    z-index: 3;
    height: 24px;
}
.legend {
    display: flex;
    align-items: center;
    gap: 10px;
    margin-top: 10px;
    color: var(--text-dim);
    font-size: 11px;
}
.legend-gradient {
    flex: 0 0 200px;
    height: 10px;
    background: linear-gradient(to right,
        rgba(88, 166, 255, 0.05),
        rgba(88, 166, 255, 0.6),
        rgba(63, 185, 80, 0.7),
        rgba(210, 153, 34, 0.85),
        rgba(248, 81, 73, 0.95));
    border-radius: 3px;
}
.badge-partial-inline {
    display: inline-block;
    padding: 1px 5px;
    border: 1px solid var(--orange);
    border-radius: 3px;
    color: var(--orange);
    font-size: 10px;
    text-transform: uppercase;
    letter-spacing: 0.04em;
}
tbody tr.partial td { color: var(--orange); }
tbody tr.partial td:first-child::after {
    content: " partial";
    font-size: 10px;
    border: 1px solid var(--orange);
    padding: 1px 4px;
    margin-left: 6px;
    border-radius: 3px;
    text-transform: uppercase;
    letter-spacing: 0.04em;
}

.foot {
    padding: 18px 28px 24px;
    border-top: 1px solid var(--border);
    color: var(--text-dim);
    font-size: 12px;
    text-align: center;
}
