/* ==================== CSS CUSTOM PROPERTIES (injected from config at runtime) ==================== */
:root {
    --dashboard-primary: #d12627;
    --dashboard-secondary: #e1b922;
}

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

body {
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
    background: #f5f6fa;
    color: #333;
    min-height: 100vh;
    -webkit-text-size-adjust: 100%;
}

/* ==================== LOADING ==================== */
#dashboard-root {
    position: relative;
    min-height: 100vh;
}

.dashboard-loading {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    min-height: 60vh;
    gap: 12px;
    color: #888;
    font-size: 14px;
}

.dashboard-loading-overlay {
    position: absolute;
    inset: 0;
    background: #f5f6fa;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 12px;
    color: #888;
    font-size: 14px;
    z-index: 100;
    transition: opacity 0.4s ease;
}

.dashboard-loading-overlay--hidden {
    opacity: 0;
    pointer-events: none;
}

.spinner {
    width: 32px;
    height: 32px;
    border: 3px solid #e0e0e0;
    border-top-color: var(--dashboard-primary);
    border-radius: 50%;
    animation: spin 0.8s linear infinite;
}

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

.dashboard-error {
    text-align: center;
    padding: 60px 20px;
    color: var(--dashboard-primary);
    font-size: 16px;
}

/* ==================== HEADER ==================== */
.dashboard-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 16px 24px;
    background: #fff;
    border-bottom: 1px solid #e0e0e0;
    flex-wrap: wrap;
    gap: 12px;
}

.dashboard-header__left {
    display: flex;
    align-items: center;
    gap: 12px;
}

.dashboard-header__logo {
    height: 70px;
    width: auto;
}

.dashboard-header__title {
    font-size: 20px;
    font-weight: 600;
    color: #333;
}

.dashboard-header__right {
    display: flex;
    align-items: center;
}

/* ==================== FILTER BAR ==================== */
.filter-bar {
    display: flex;
    align-items: center;
    gap: 12px;
    flex-wrap: wrap;
    position: relative;
}

.filter-bar--loading {
    pointer-events: none;
    opacity: 0.5;
}

.filter-bar--loading::after {
    content: '';
    position: absolute;
    top: 50%;
    right: -32px;
    width: 18px;
    height: 18px;
    border: 2px solid var(--dashboard-border, #ddd);
    border-top-color: var(--dashboard-primary, #4a90d9);
    border-radius: 50%;
    animation: spin .6s linear infinite;
}

.filter-bar__group {
    display: flex;
    align-items: center;
    gap: 4px;
}

.filter-bar__group label {
    font-size: 12px;
    color: #666;
    white-space: nowrap;
}

.filter-bar__input {
    padding: 6px 10px;
    border: 1px solid #ccc;
    border-radius: 4px;
    font-size: 13px;
    outline: none;
    min-height: 32px;
    transition: border-color 0.15s;
}

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

.filter-bar__presets {
    display: flex;
    gap: 4px;
}

.filter-bar__preset {
    padding: 5px 10px;
    border: 1px solid #ccc;
    border-radius: 4px;
    background: #fff;
    font-size: 12px;
    cursor: pointer;
    transition: all 0.15s;
}

.filter-bar__preset:hover {
    border-color: var(--dashboard-primary);
    color: var(--dashboard-primary);
}

.filter-bar__preset--clear {
    color: #888;
}

.filter-bar__select {
    padding: 6px 10px;
    border: 1px solid #ccc;
    border-radius: 4px;
    font-size: 13px;
    outline: none;
    max-width: 160px;
    background: #fff;
    min-height: 32px;
    transition: border-color 0.15s;
}

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

.filter-bar__date-range {
    display: flex;
    align-items: center;
    gap: 6px;
    padding: 2px 8px;
    background: #f8f8f8;
    border-radius: 4px;
    border: 1px solid #eee;
}

.filter-bar__range-label {
    font-size: 12px;
    font-weight: 600;
    color: #555;
    white-space: nowrap;
}

.filter-bar__reset {
    padding: 5px 12px;
    border: 1px solid #ccc;
    border-radius: 4px;
    background: #fff;
    font-size: 12px;
    cursor: pointer;
    color: #888;
    white-space: nowrap;
    transition: all 0.15s;
    margin-left: 4px;
}

.filter-bar__reset:hover {
    border-color: var(--dashboard-primary);
    color: var(--dashboard-primary);
}

.filter-bar__separator {
    width: 1px;
    height: 24px;
    background: #ddd;
    margin: 0 4px;
    flex-shrink: 0;
}

/* ==================== ROWS LAYOUT ==================== */
.dashboard-rows {
    display: flex;
    flex-direction: column;
    gap: 16px;
    padding: 20px 24px;
}

.dashboard-row {
    display: flex;
    flex-wrap: wrap;
    gap: 16px;
    min-height: 80px;
    border-radius: 8px;
    transition: outline 0.15s;
}

.dashboard-row .widget {
    flex: 1 1 0%;
    min-width: 0;
}

.dashboard-footer {
    padding: 0 24px 20px;
}

/* ==================== WIDGET ==================== */
.widget {
    background: #fff;
    border-radius: 8px;
    box-shadow: 0 1px 3px rgba(0,0,0,0.08);
    overflow: hidden;
    display: flex;
    flex-direction: column;
    transition: opacity 0.2s, box-shadow 0.15s;
}

.widget--dragging {
    opacity: 0.4;
    transform: scale(0.97);
    box-shadow: 0 12px 30px rgba(0,0,0,0.18);
    z-index: 10;
}

/* Widget sizes — control flex-basis within a row */
.widget--small  { flex: 0 1 25%; min-width: 200px; }
.widget--medium { flex: 1 1 33%; min-width: 260px; }
.widget--large  { flex: 2 1 50%; min-width: 320px; }
.widget--full   { flex: 1 1 100%; }

/* ==================== DROP PLACEHOLDER ==================== */
.drop-placeholder {
    flex: 1;
    min-width: 120px;
    min-height: 200px;
    border: 2px dashed var(--dashboard-primary);
    border-radius: 8px;
    background: rgba(209, 38, 39, 0.04);
    display: flex;
    align-items: center;
    justify-content: center;
}

.drop-placeholder__icon {
    width: 48px;
    height: 48px;
    border-radius: 50%;
    background: rgba(209, 38, 39, 0.08);
    color: var(--dashboard-primary);
    font-size: 28px;
    font-weight: 300;
    display: flex;
    align-items: center;
    justify-content: center;
    line-height: 1;
}

.dashboard-row--placeholder {
    min-height: 80px;
}

.widget__header {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 12px 16px;
    border-bottom: 1px solid #f0f0f0;
    min-height: 44px;
    cursor: grab;
}

.widget__header:active {
    cursor: grabbing;
}

.widget__drag-handle {
    color: #bbb;
    font-size: 14px;
    flex-shrink: 0;
    user-select: none;
}

.widget__title {
    font-size: 14px;
    font-weight: 600;
    color: #333;
    flex: 1;
}

.widget__controls {
    display: flex;
    gap: 4px;
    flex-shrink: 0;
    flex-wrap: wrap;
}

.widget__hint {
    font-size: 11px;
    color: #999;
    font-style: italic;
    white-space: nowrap;
    flex-shrink: 0;
}

.widget__tab {
    padding: 5px 10px;
    border: 1px solid #ddd;
    border-radius: 4px;
    background: #fff;
    font-size: 11px;
    cursor: pointer;
    transition: all 0.15s;
    min-height: 28px;
    min-width: 28px;
    display: inline-flex;
    align-items: center;
    justify-content: center;
}

.widget__tab:not(.widget__tab--active):hover {
    border-color: var(--dashboard-primary);
    color: var(--dashboard-primary);
}

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

/* ==================== WIDGET SUMMARY BANNER ==================== */
.widget__summary {
    display: flex;
    gap: 16px;
    padding: 8px 16px;
    background: #f8fafc;
    border-bottom: 1px solid #f0f0f0;
    flex-wrap: wrap;
}

.widget__summary-item {
    display: flex;
    flex-direction: column;
    gap: 2px;
}

.widget__summary-label {
    font-size: 10px;
    color: #888;
    text-transform: uppercase;
    letter-spacing: 0.5px;
}

.widget__summary-value {
    font-size: 16px;
    font-weight: 700;
    color: #333;
}

.widget__content {
    padding: 16px;
    height: 320px;
    position: relative;
    overflow: hidden;
}

.widget__content canvas {
    width: 100% !important;
    height: 100% !important;
}

.widget__loading {
    display: flex;
    align-items: center;
    justify-content: center;
    height: 100%;
    min-height: 200px;
}

.widget__error {
    display: flex;
    align-items: center;
    justify-content: center;
    height: 100%;
    color: var(--dashboard-primary);
    font-size: 13px;
}

.widget__empty {
    display: flex;
    align-items: center;
    justify-content: center;
    height: 100%;
    color: #999;
    font-size: 13px;
}

/* ==================== TABLE ==================== */
.widget-table-wrapper {
    overflow-x: auto;
    max-height: 400px;
    overflow-y: auto;
}

.widget-table {
    width: 100%;
    border-collapse: collapse;
    font-size: 12px;
}

.widget-table thead {
    position: sticky;
    top: 0;
    z-index: 1;
}

.widget-table th {
    background: #f8f8f8;
    padding: 8px 10px;
    text-align: left;
    font-weight: 600;
    border-bottom: 2px solid #e0e0e0;
    white-space: nowrap;
    user-select: none;
}

.widget-table td {
    padding: 7px 10px;
    border-bottom: 1px solid #f0f0f0;
}

.widget-table tbody tr:hover {
    background: #fafafa;
}

/* ==================== ADD WIDGET BUTTON ==================== */
.dashboard-add-widget-btn {
    width: 100%;
    min-height: 60px;
    border: 2px dashed #ccc;
    border-radius: 8px;
    background: transparent;
    color: #999;
    font-size: 15px;
    cursor: pointer;
    transition: all 0.2s;
}

.dashboard-add-widget-btn:hover {
    border-color: var(--dashboard-primary);
    color: var(--dashboard-primary);
}

/* ==================== DRILLDOWN MODAL ==================== */
.drilldown-overlay {
    position: fixed;
    inset: 0;
    background: rgba(0,0,0,0.5);
    z-index: 1000;
    display: none;
    align-items: center;
    justify-content: center;
}

.drilldown-overlay--visible {
    display: flex;
}

.drilldown-modal {
    background: #fff;
    border-radius: 10px;
    width: 90%;
    max-width: 900px;
    max-height: 80vh;
    display: flex;
    flex-direction: column;
    box-shadow: 0 8px 30px rgba(0,0,0,0.2);
}

.drilldown-modal__header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 16px 20px;
    border-bottom: 1px solid #e0e0e0;
}

.drilldown-modal__title {
    font-size: 16px;
    font-weight: 600;
}

.drilldown-modal__close {
    background: none;
    border: none;
    font-size: 24px;
    cursor: pointer;
    color: #888;
    padding: 4px 8px;
    min-width: 36px;
    min-height: 36px;
    display: flex;
    align-items: center;
    justify-content: center;
    border-radius: 4px;
    transition: background 0.15s, color 0.15s;
}

.drilldown-modal__close:hover { color: #333; background: #f5f5f5; }

.drilldown-modal__body {
    padding: 16px 20px;
    overflow-y: auto;
    flex: 1;
}

/* ==================== BUILDER MODAL ==================== */
.builder-overlay {
    position: fixed;
    inset: 0;
    background: rgba(0,0,0,0.5);
    z-index: 1000;
    display: none;
    align-items: center;
    justify-content: center;
}

.builder-overlay--visible {
    display: flex;
}

.builder-modal {
    background: #fff;
    border-radius: 10px;
    width: 90%;
    max-width: 480px;
    box-shadow: 0 8px 30px rgba(0,0,0,0.2);
}

.builder-modal__header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 16px 20px;
    border-bottom: 1px solid #e0e0e0;
}

.builder-modal__header h3 {
    font-size: 16px;
    font-weight: 600;
}

.builder-modal__close {
    background: none;
    border: none;
    font-size: 24px;
    cursor: pointer;
    color: #888;
    padding: 4px 8px;
    min-width: 36px;
    min-height: 36px;
    display: flex;
    align-items: center;
    justify-content: center;
    border-radius: 4px;
    transition: background 0.15s, color 0.15s;
}

.builder-modal__close:hover { color: #333; background: #f5f5f5; }

.builder-modal__body {
    padding: 20px;
    display: flex;
    flex-direction: column;
    gap: 14px;
}

.builder-field label {
    display: block;
    font-size: 12px;
    font-weight: 600;
    color: #555;
    margin-bottom: 4px;
}

.builder-field input,
.builder-field select {
    width: 100%;
    padding: 8px 10px;
    border: 1px solid #ccc;
    border-radius: 4px;
    font-size: 13px;
    outline: none;
}

.builder-field input:focus,
.builder-field select:focus {
    border-color: var(--dashboard-primary);
}

.builder-warning {
    display: block;
    margin-top: 5px;
    padding: 5px 8px;
    background: #fff8e1;
    border-left: 3px solid #f5a623;
    border-radius: 3px;
    font-size: 11px;
    color: #7a5800;
}

.builder-modal__footer {
    display: flex;
    justify-content: flex-end;
    gap: 8px;
    padding: 12px 20px;
    border-top: 1px solid #e0e0e0;
}

.btn-cancel {
    padding: 8px 16px;
    border: 1px solid #ccc;
    border-radius: 6px;
    background: #fff;
    cursor: pointer;
    font-size: 13px;
    min-height: 36px;
    transition: border-color 0.15s;
}

.btn-save {
    padding: 8px 16px;
    border: none;
    border-radius: 6px;
    background: var(--dashboard-primary);
    color: #fff;
    cursor: pointer;
    font-size: 13px;
    font-weight: 600;
    min-height: 36px;
    transition: opacity 0.15s;
}

.btn-save:hover { opacity: 0.9; }
.btn-cancel:hover { border-color: #999; }

/* ==================== WIDGET CHOOSER MODAL ==================== */
.chooser-overlay {
    position: fixed;
    inset: 0;
    background: rgba(0,0,0,0.5);
    z-index: 1000;
    display: none;
    align-items: center;
    justify-content: center;
}

.chooser-overlay--visible {
    display: flex;
}

.chooser-modal {
    background: #fff;
    border-radius: 10px;
    width: 90%;
    max-width: 520px;
    box-shadow: 0 8px 30px rgba(0,0,0,0.2);
}

.chooser-modal__header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 16px 20px;
    border-bottom: 1px solid #e0e0e0;
}

.chooser-modal__header h3 {
    font-size: 16px;
    font-weight: 600;
}

.chooser-modal__close {
    background: none;
    border: none;
    font-size: 24px;
    cursor: pointer;
    color: #888;
    padding: 4px 8px;
    min-width: 36px;
    min-height: 36px;
    display: flex;
    align-items: center;
    justify-content: center;
    border-radius: 4px;
    transition: background 0.15s, color 0.15s;
}

.chooser-modal__close:hover {
    color: #333;
    background: #f5f5f5;
}

.chooser-modal__body {
    display: flex;
    gap: 20px;
    padding: 30px 20px;
    justify-content: center;
}

.chooser-option {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 10px;
    padding: 24px 20px;
    border: 2px solid #e0e0e0;
    border-radius: 10px;
    cursor: pointer;
    transition: all 0.2s;
    flex: 1;
    max-width: 220px;
    text-align: center;
}

.chooser-option:hover {
    border-color: var(--dashboard-primary);
    background: rgba(209, 38, 39, 0.03);
}

.chooser-option:hover .chooser-option__icon {
    color: var(--dashboard-primary);
}

.chooser-option__icon {
    color: #999;
    transition: color 0.2s;
}

.chooser-option__label {
    font-size: 14px;
    font-weight: 600;
    color: #333;
}

.chooser-option__desc {
    font-size: 11px;
    color: #888;
    line-height: 1.4;
}

/* ==================== RESPONSIVE — TABLET (1024px) ==================== */
@media (max-width: 1024px) {
    .dashboard-header {
        padding: 14px 16px;
        gap: 10px;
    }

    .dashboard-header__logo { height: 48px; }
    .dashboard-header__title { font-size: 18px; }

    .dashboard-rows { padding: 16px; gap: 12px; }
    .dashboard-row { gap: 12px; }

    /* Widgets stack to max 2 per row on tablet */
    .dashboard-row { flex-wrap: wrap; }
    .dashboard-row .widget { flex: 1 1 calc(50% - 6px); min-width: 280px; }
    .widget--full { flex: 1 1 100%; }

    .widget__content { height: 280px; }

    /* Filter bar wraps more aggressively */
    .filter-bar { gap: 8px; }
    .filter-bar__date-range { gap: 4px; padding: 2px 6px; }
    .filter-bar__select { max-width: 140px; }

    .widget__hint { display: none; }
}

/* ==================== RESPONSIVE — MOBILE (768px) ==================== */
@media (max-width: 768px) {
    .dashboard-header {
        flex-direction: column;
        align-items: flex-start;
        padding: 12px 14px;
        gap: 10px;
    }

    .dashboard-header__left { gap: 10px; }
    .dashboard-header__logo { height: 40px; }
    .dashboard-header__title { font-size: 16px; }

    .dashboard-header__right {
        width: 100%;
        overflow-x: auto;
        -webkit-overflow-scrolling: touch;
    }

    /* Filter bar — full width, scrollable */
    .filter-bar {
        width: 100%;
        gap: 8px;
    }

    .filter-bar__group label { font-size: 11px; }

    .filter-bar__input,
    .filter-bar__select {
        padding: 7px 8px;
        font-size: 14px;
        min-height: 36px;
    }

    .filter-bar__select { max-width: none; flex: 1; }

    .filter-bar__preset {
        padding: 7px 12px;
        font-size: 12px;
        min-height: 36px;
    }

    .filter-bar__reset {
        padding: 7px 14px;
        font-size: 12px;
        min-height: 36px;
        margin-left: 0;
    }

    .filter-bar__date-range {
        flex-wrap: wrap;
        gap: 4px;
        padding: 4px 8px;
        width: 100%;
    }

    .filter-bar__range-label {
        width: 100%;
        font-size: 11px;
    }

    .filter-bar__separator { display: none; }

    /* Rows — single column */
    .dashboard-rows { padding: 10px 12px; gap: 10px; }
    .dashboard-row {
        flex-direction: column;
        gap: 10px;
    }
    .dashboard-row .widget { min-width: 0; flex: 1 1 100%; }

    /* Widget content */
    .widget__content { height: 260px; }
    .widget__header { padding: 10px 12px; gap: 6px; }
    .widget__title { font-size: 13px; }
    .widget__drag-handle { font-size: 12px; }
    .widget__tab { padding: 5px 8px; font-size: 11px; min-height: 30px; }
    .widget__hint { display: none; }

    /* Summary banner */
    .widget__summary { padding: 6px 12px; gap: 12px; }
    .widget__summary-label { font-size: 9px; }
    .widget__summary-value { font-size: 14px; }

    /* Table */
    .widget-table { font-size: 11px; }
    .widget-table th { padding: 6px 8px; }
    .widget-table td { padding: 5px 8px; }

    /* Footer */
    .dashboard-footer { padding: 0 12px 12px; }
    .dashboard-add-widget-btn { min-height: 48px; font-size: 14px; }

    /* Modals */
    .drilldown-modal { width: 96%; max-width: none; max-height: 90vh; }
    .drilldown-modal__header { padding: 12px 16px; }
    .drilldown-modal__title { font-size: 15px; }
    .drilldown-modal__body { padding: 12px 16px; }

    .builder-modal { width: 96%; max-width: none; }
    .builder-modal__header { padding: 12px 16px; }
    .builder-modal__body { padding: 16px; gap: 12px; }
    .builder-modal__footer { padding: 10px 16px; }

    .chooser-modal { width: 96%; max-width: none; }
    .chooser-modal__header { padding: 12px 16px; }
    .chooser-modal__body {
        flex-direction: column;
        align-items: center;
        padding: 20px 16px;
        gap: 12px;
    }
    .chooser-option { max-width: 100%; width: 100%; padding: 18px 16px; }
}

/* ==================== RESPONSIVE — SMALL MOBILE (480px) ==================== */
@media (max-width: 480px) {
    .dashboard-header { padding: 10px 12px; }
    .dashboard-header__logo { height: 32px; }
    .dashboard-header__title { font-size: 15px; }

    .dashboard-rows { padding: 8px; gap: 8px; }

    .widget__content { height: 220px; }
    .widget__header { padding: 8px 10px; }
    .widget__title { font-size: 12px; }
    .widget__controls { gap: 2px; }
    .widget__tab { padding: 4px 6px; font-size: 10px; }

    .widget__summary { padding: 5px 10px; gap: 8px; }
    .widget__summary-value { font-size: 13px; }

    /* Filter bar stacks vertically */
    .filter-bar {
        flex-direction: column;
        align-items: stretch;
    }

    .filter-bar__group {
        flex-direction: column;
        align-items: flex-start;
        gap: 2px;
    }

    .filter-bar__input,
    .filter-bar__select {
        width: 100%;
        max-width: none;
    }

    .filter-bar__presets {
        flex-wrap: wrap;
        gap: 6px;
    }

    .filter-bar__preset { flex: 1 1 auto; text-align: center; }
    .filter-bar__reset { width: 100%; text-align: center; }

    .filter-bar__date-range {
        flex-direction: column;
        align-items: stretch;
    }

    /* Table compact */
    .widget-table { font-size: 10px; }
    .widget-table th,
    .widget-table td { padding: 4px 6px; }

    /* Footer */
    .dashboard-footer { padding: 0 8px 8px; }
    .dashboard-add-widget-btn { min-height: 44px; font-size: 13px; }

    /* Modals full-screen */
    .drilldown-modal,
    .builder-modal,
    .chooser-modal {
        width: 100%;
        max-height: 100vh;
        border-radius: 0;
    }

    .chooser-option { padding: 14px 12px; }
    .chooser-option__icon svg { width: 36px; height: 36px; }
    .chooser-option__label { font-size: 13px; }
}
