:root {
    --color-bg: #f4f5f7;
    --color-surface: #ffffff;
    --color-surface-hover: #f0f1f4;
    --color-border: #d8dbe3;
    --color-text: #1a1d26;
    --color-text-secondary: #5c6370;
    --color-primary: #4f46e5;
    --color-primary-hover: #6366f1;
    --color-danger: #dc2626;
    --color-success: #16a34a;
    --color-warning: #d97706;
    --radius: 8px;
    --shadow: 0 1px 3px rgba(15, 23, 42, 0.08), 0 1px 2px rgba(15, 23, 42, 0.06);
}

* { margin: 0; padding: 0; box-sizing: border-box; }

body {
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
    background: var(--color-bg);
    color: var(--color-text);
    line-height: 1.6;
}

.app {
    max-width: 1400px;
    margin: 0 auto;
    padding: 24px;
}

.app-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 16px;
    margin-bottom: 32px;
}

.app-header-main {
    flex: 1;
    text-align: center;
}

.app-header h1 {
    font-size: 2rem;
    font-weight: 700;
    color: var(--color-primary);
}

.app-header .subtitle {
    color: var(--color-text-secondary);
    font-size: 0.95rem;
    margin-top: 4px;
}

.app-header-actions {
    display: flex;
    flex-wrap: wrap;
    gap: 8px;
    flex: 0 0 auto;
    justify-content: flex-end;
}

.btn-logout {
    white-space: nowrap;
}

/* Login view */
.login-view {
    display: flex;
    justify-content: center;
    align-items: flex-start;
    padding: 48px 16px;
}

.login-card {
    background: var(--color-surface);
    border: 1px solid var(--color-border);
    border-radius: var(--radius);
    box-shadow: var(--shadow);
    padding: 32px;
    width: 100%;
    max-width: 380px;
    display: flex;
    flex-direction: column;
    gap: 16px;
}

.login-title {
    font-size: 1.3rem;
    font-weight: 700;
}

.login-hint {
    color: var(--color-text-secondary);
    font-size: 0.85rem;
    margin-top: -8px;
}

.login-field {
    display: flex;
    flex-direction: column;
    gap: 6px;
}

.login-field label {
    font-size: 0.85rem;
    color: var(--color-text-secondary);
    font-weight: 500;
}

.login-error {
    background: rgba(220, 38, 38, 0.08);
    border: 1px solid var(--color-danger);
    border-radius: 6px;
    padding: 8px 12px;
    color: var(--color-text);
    font-size: 0.85rem;
}

.login-submit {
    margin-top: 4px;
}

/* Filters Panel */
.filters-panel {
    background: var(--color-surface);
    border: 1px solid var(--color-border);
    border-radius: var(--radius);
    padding: 20px;
    margin-bottom: 24px;
}

.filters-title {
    font-size: 1.1rem;
    font-weight: 600;
    margin-bottom: 16px;
}

.filters-row {
    display: flex;
    flex-wrap: wrap;
    gap: 16px;
    align-items: flex-start;
}

.filter-group {
    display: flex;
    flex-direction: column;
    gap: 6px;
    min-width: 160px;
}

.filter-group label {
    font-size: 0.85rem;
    color: var(--color-text-secondary);
    font-weight: 500;
}

.required { color: var(--color-danger); }

.filter-input {
    background: var(--color-surface);
    border: 1px solid var(--color-border);
    border-radius: 6px;
    padding: 8px 12px;
    color: var(--color-text);
    font-size: 0.9rem;
    outline: none;
    transition: border-color 0.2s;
}

.filter-input:focus { border-color: var(--color-primary); }

/* Native <select> dropdowns reuse .filter-input but need an explicit arrow and
   appearance reset so they match the theme across browsers. */
select.filter-input {
    appearance: none;
    -webkit-appearance: none;
    -moz-appearance: none;
    cursor: pointer;
    padding-right: 32px;
    background-color: var(--color-surface);
    background-image: url("data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' width='12' height='12' viewBox='0 0 12 12'><path fill='%235c6370' d='M2 4l4 4 4-4z'/></svg>");
    background-repeat: no-repeat;
    background-position: right 10px center;
}

select.filter-input option {
    background: var(--color-bg);
    color: var(--color-text);
}

/* Author multi-select rendered as toggle chips. */
.filter-group-authors { min-width: 220px; }

.filter-hint {
    font-size: 0.72rem;
    color: var(--color-text-secondary);
    font-weight: 400;
}

.chip-group {
    display: flex;
    flex-wrap: wrap;
    gap: 6px;
    max-height: 96px;
    overflow-y: auto;
    padding: 2px;
}

.chip {
    display: inline-flex;
    align-items: center;
    gap: 4px;
    padding: 5px 10px;
    border-radius: 14px;
    cursor: pointer;
    font-size: 0.82rem;
    background: var(--color-surface);
    border: 1px solid var(--color-border);
    transition: all 0.15s;
    color: var(--color-text);
    user-select: none;
}

.chip:hover { border-color: var(--color-primary); }

.chip.active {
    background: var(--color-primary);
    border-color: var(--color-primary);
    color: #fff;
}

.chip input { display: none; }

.chip-group-empty {
    font-size: 0.8rem;
    color: var(--color-text-secondary);
    padding: 8px 0;
}

.radio-group, .checkbox-group {
    display: flex;
    gap: 4px;
    flex-wrap: wrap;
}

.radio-label, .checkbox-label {
    display: flex;
    align-items: center;
    gap: 4px;
    padding: 6px 12px;
    border-radius: 6px;
    cursor: pointer;
    font-size: 0.85rem;
    background: var(--color-surface);
    border: 1px solid var(--color-border);
    transition: all 0.2s;
    color: var(--color-text);
}

.radio-label.active, .checkbox-label.active {
    background: var(--color-primary);
    border-color: var(--color-primary);
    color: #fff;
}

.radio-label input, .checkbox-label input { display: none; }

.filters-actions {
    margin-top: 16px;
    display: flex;
    gap: 8px;
}

.btn {
    padding: 8px 20px;
    border-radius: 6px;
    border: none;
    font-size: 0.9rem;
    font-weight: 600;
    cursor: pointer;
    transition: all 0.2s;
}

.btn:disabled { opacity: 0.6; cursor: not-allowed; }

.btn-primary { background: var(--color-primary); color: #fff; }
.btn-primary:hover:not(:disabled) { background: var(--color-primary-hover); }

.btn-secondary {
    background: var(--color-surface-hover);
    color: var(--color-text);
    border: 1px solid var(--color-border);
}
.btn-secondary:hover { background: var(--color-border); }

/* Error & Loading */
.error-banner {
    background: rgba(220, 38, 38, 0.08);
    border: 1px solid var(--color-danger);
    border-radius: var(--radius);
    padding: 12px 16px;
    margin-bottom: 20px;
    display: flex;
    align-items: center;
    gap: 8px;
}

.btn-close-error {
    margin-left: auto;
    background: none;
    border: none;
    color: var(--color-danger);
    cursor: pointer;
    font-size: 1.1rem;
}

.loading-overlay {
    text-align: center;
    padding: 60px 20px;
    color: var(--color-text-secondary);
}

.spinner {
    width: 40px;
    height: 40px;
    border: 3px solid var(--color-border);
    border-top-color: var(--color-primary);
    border-radius: 50%;
    animation: spin 0.8s linear infinite;
    margin: 0 auto 16px;
}

@keyframes spin { to { transform: rotate(360deg); } }

/* Dashboard Grid (config-driven layout engine).
   Widgets declare gridColumn / gridRow in config.json; the container provides a
   2-track base grid and the cells position themselves via inline styles. */
.dashboard-grid {
    display: grid;
    grid-template-columns: repeat(2, 1fr);
    grid-auto-rows: minmax(min-content, auto);
    gap: 20px;
    margin-bottom: 24px;
}

.grid-cell {
    min-width: 0;
}

/* Главный график — по центру страницы, крупнее остальных. */
.grid-cell-featured {
    display: flex;
    justify-content: center;
    margin-bottom: 4px;
}

.grid-cell-featured .widget {
    width: 100%;
    max-width: 920px;
}

.grid-cell-featured .widget-title {
    text-align: center;
    font-size: 1.15rem;
}

.grid-cell-featured .chart-container {
    min-height: 340px;
}

.grid-cell-featured .chart-container canvas {
    height: 340px !important;
}

@media (max-width: 900px) {
    .dashboard-grid { grid-template-columns: 1fr; }
    .grid-cell { grid-column: 1 / -1 !important; }
}

/* Generic widget shell (shared by chart/table/card/list widgets). */
.widget {
    position: relative;
    background: var(--color-surface);
    border: 1px solid var(--color-border);
    border-radius: var(--radius);
    padding: 16px;
    box-shadow: var(--shadow);
    height: 100%;
}

.widget-title {
    font-size: 1rem;
    font-weight: 600;
    margin-bottom: 12px;
}

.widget-loading {
    position: absolute;
    inset: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    background: rgba(255, 255, 255, 0.72);
    border-radius: var(--radius);
}

.widget-error {
    margin-top: 8px;
    background: rgba(220, 38, 38, 0.08);
    border: 1px solid var(--color-danger);
    border-radius: 6px;
    padding: 8px 12px;
    color: var(--color-text);
    font-size: 0.82rem;
}

.widget-unknown {
    background: var(--color-surface);
    border: 1px dashed var(--color-border);
    border-radius: var(--radius);
    padding: 16px;
    color: var(--color-text-secondary);
}

.widget-card .card-value {
    font-size: 2rem;
    font-weight: 700;
    color: var(--color-primary-hover);
}

/* Chart Card */
.chart-card {
    background: var(--color-surface);
    border: 1px solid var(--color-border);
    border-radius: var(--radius);
    padding: 16px;
    box-shadow: var(--shadow);
}

.chart-title {
    font-size: 1rem;
    font-weight: 600;
    margin-bottom: 12px;
}

.chart-container {
    position: relative;
    width: 100%;
    min-height: 280px;
}

.chart-container canvas {
    width: 100% !important;
    height: 280px !important;
}

.chart-empty {
    display: flex;
    align-items: center;
    justify-content: center;
    min-height: 200px;
    color: var(--color-text-secondary);
    font-size: 0.9rem;
}

/* Data Table */
.table-card {
    background: var(--color-surface);
    border: 1px solid var(--color-border);
    border-radius: var(--radius);
    padding: 16px;
    box-shadow: var(--shadow);
}

.table-title {
    font-size: 1rem;
    font-weight: 600;
    margin-bottom: 12px;
}

.table-wrapper { overflow-x: auto; }

.data-table {
    width: 100%;
    border-collapse: collapse;
    font-size: 0.85rem;
}

.data-table th, .data-table td {
    padding: 8px 12px;
    text-align: left;
    border-bottom: 1px solid var(--color-border);
    white-space: nowrap;
}

.data-table th {
    background: var(--color-surface-hover);
    color: var(--color-text-secondary);
    font-weight: 600;
    position: sticky;
    top: 0;
    cursor: pointer;
    user-select: none;
}

.data-table th:hover { color: var(--color-primary-hover); }

.data-table tbody tr:hover { background: var(--color-surface-hover); }

.table-empty {
    display: flex;
    align-items: center;
    justify-content: center;
    min-height: 120px;
    color: var(--color-text-secondary);
}

.table-footer {
    margin-top: 8px;
    text-align: right;
    font-size: 0.8rem;
    color: var(--color-text-secondary);
}

/* Pagination */
.pagination-info {
    text-align: center;
    padding: 12px;
    color: var(--color-text-secondary);
    font-size: 0.85rem;
    display: flex;
    justify-content: center;
    gap: 12px;
    align-items: center;
}

.has-more-badge {
    background: var(--color-warning);
    color: #000;
    padding: 2px 8px;
    border-radius: 4px;
    font-size: 0.75rem;
    font-weight: 600;
}

/* Печать: книжная A4, две колонки (как на экране). */
@page {
    size: A4 portrait;
    margin: 12mm;
}

@media print {
    body {
        background: #fff;
        color: #000;
        -webkit-print-color-adjust: exact;
        print-color-adjust: exact;
    }

    .no-print,
    .btn-close-error {
        display: none !important;
    }

    .app {
        max-width: none;
        padding: 0;
    }

    .app-header {
        margin-bottom: 12px;
        page-break-after: avoid;
    }

    .app-header h1 {
        font-size: 1.35rem;
        color: #1a1d26;
    }

    .error-banner {
        display: none !important;
    }

    .dashboard-grid {
        grid-template-columns: repeat(2, 1fr);
        gap: 10px;
    }

    .widget {
        box-shadow: none;
        border: 1px solid #ccc;
        page-break-inside: avoid;
        break-inside: avoid;
    }

    .grid-cell-featured {
        page-break-after: avoid;
    }

    .grid-cell-featured .widget {
        max-width: none;
    }

    .chart-container {
        position: relative;
        width: 100%;
        min-height: 0;
        overflow: hidden;
        height: 120px;
    }

    .grid-cell-featured .chart-container {
        height: 150px;
    }

    .chart-container canvas {
        width: 100% !important;
        height: 100% !important;
        max-height: 100% !important;
    }

    .widget-loading {
        display: none !important;
    }

    .spinner {
        animation: none;
        border-top-color: var(--color-border);
    }
}
