/* ===== App-wide responsive layer ===== */
:root {
    --app-sidebar-width: 250px;
    --app-header-height: 60px;
    --app-mobile-gutter: 12px;
}

html {
    overflow-x: hidden;
}

img,
svg,
canvas,
iframe {
    max-width: 100%;
}

body > .d-flex,
body > .d-flex > [class*="flex-grow"],
body > .d-flex > [class*="flex-grow"] > main,
main > .container,
main > .container-fluid,
main > .container-xl {
    min-width: 0;
}

.navbar .container-fluid {
    min-width: 0;
    gap: 8px;
}

.navbar-brand,
.navbar .nav-link {
    min-width: 0;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.navbar .nav-link {
    display: flex;
    align-items: center;
}

.navbar .user-avatar {
    flex: 0 0 auto;
}

.table-responsive {
    width: 100%;
    max-width: 100%;
    overflow-x: auto !important;
    overflow-y: visible;
    -webkit-overflow-scrolling: touch;
}

.table-responsive > table,
.table-responsive .table {
    width: 100%;
    min-width: 680px;
}

.table th,
.table td {
    overflow-wrap: anywhere;
}

.form-control,
.form-select,
textarea,
button,
.btn {
    max-width: 100%;
}

.pagination {
    flex-wrap: wrap;
    gap: 4px;
}

.pagination .page-link {
    min-width: 36px;
    text-align: center;
}

.modal-dialog {
    max-width: calc(100% - 1rem);
}

/* ===== App-wide compact workspace ===== */
body > .d-flex > [class*="flex-grow"] > main,
.dashboard-main {
    padding: 10px 12px !important;
    font-size: 0.84rem;
}

.sidebar {
    width: var(--app-sidebar-width) !important;
}

.sidebar-header {
    padding: 16px 12px !important;
}

.sidebar-brand {
    padding: 20px 0 !important;
}

.sidebar-brand-mark {
    font-size: 2.5rem !important;
    margin-bottom: 8px !important;
}

.sidebar-header h2 {
    font-size: 1.2rem !important;
    line-height: 1.2 !important;
}

.sidebar-header p {
    font-size: 0.75rem !important;
}

.sidebar-nav {
    padding: 12px 0 !important;
}

.sidebar .nav-item {
    margin: 0 !important;
    border-left: 4px solid transparent !important;
}

.sidebar .nav-item a {
    display: flex !important;
    align-items: center !important;
    padding: 8px 12px !important;
    font-size: 0.9rem !important;
    line-height: 1.35 !important;
}

.sidebar .nav-item i {
    width: 22px !important;
    min-width: 22px !important;
    margin-right: 12px !important;
    font-size: 1.1rem !important;
    text-align: center !important;
    display: inline-flex !important;
    align-items: center !important;
    justify-content: center !important;
}

.navbar .nav-item {
    border-left: 0 !important;
}

.navbar .nav-item:hover {
    background: transparent !important;
    border-left: 0 !important;
}

body > .d-flex h1,
body > .d-flex .page-title,
body > .d-flex .users-page-title {
    font-size: 1.25rem !important;
    line-height: 1.25 !important;
    margin-bottom: 12px !important;
}

body > .d-flex h2 {
    font-size: 1.08rem !important;
    line-height: 1.25 !important;
}

body > .d-flex h3 {
    font-size: 1rem !important;
}

body > .d-flex .card,
body > .d-flex .filter-card,
body > .d-flex .chart-container,
body > .d-flex .form-card,
body > .d-flex .table-responsive,
body > .d-flex .table-list,
body > .d-flex .summary-card,
body > .d-flex .metric-card,
body > .d-flex .stat-card,
body > .d-flex .profile-card,
body > .d-flex .progress-card {
    border-radius: 8px !important;
    margin-bottom: 10px !important;
}

body > .d-flex .card-header {
    padding: 10px 12px !important;
}

body > .d-flex .card-header h5,
body > .d-flex .chart-title,
body > .d-flex .section-title,
body > .d-flex .filter-title,
body > .d-flex .table-list h5,
body > .d-flex .table-responsive h5 {
    font-size: 0.92rem !important;
    line-height: 1.25 !important;
    margin-bottom: 10px !important;
}

body > .d-flex .card-body,
body > .d-flex .modal-body,
body > .d-flex [id="summaryTabContent"],
body > .d-flex [id="filtersTabContent"] {
    padding: 12px !important;
}

body > .d-flex .form-label,
body > .d-flex .filter-label {
    font-size: 0.8rem !important;
    margin-bottom: 5px !important;
}

body > .d-flex .form-group {
    margin-bottom: 10px !important;
}

body > .d-flex .form-control,
body > .d-flex .form-select {
    min-height: 38px !important;
    padding: 7px 10px !important;
    font-size: 0.84rem !important;
}

body > .d-flex textarea.form-control {
    min-height: 76px !important;
}

body > .d-flex .btn,
body > .d-flex .btn-submit,
body > .d-flex .btn-filter,
body > .d-flex .btn-print,
body > .d-flex .btn-reset,
body > .d-flex .btn-danger-custom,
body > .d-flex .btn-toggle {
    padding: 7px 12px !important;
    font-size: 0.82rem !important;
    line-height: 1.25 !important;
}

body > .d-flex .table-responsive,
body > .d-flex .table-list {
    padding: 12px !important;
}

body > .d-flex table thead th {
    padding: 9px 10px !important;
    font-size: 0.74rem !important;
    line-height: 1.25 !important;
}

body > .d-flex table tbody td {
    padding: 8px 10px !important;
    font-size: 0.82rem !important;
    line-height: 1.35 !important;
}

body > .d-flex .badge,
body > .d-flex .badge-user,
body > .d-flex .badge-amount,
body > .d-flex .badge-category,
body > .d-flex .badge-role,
body > .d-flex .badge-active,
body > .d-flex .badge-inactive,
body > .d-flex .badge-action,
body > .d-flex .trend-badge {
    padding: 4px 8px !important;
    font-size: 0.74rem !important;
    line-height: 1.25 !important;
}

.user-avatar {
    width: 36px !important;
    height: 36px !important;
    font-size: 0.9rem !important;
}

html:has(.login-container),
html:has(.register-container),
body:has(.login-container),
body:has(.register-container) {
    height: auto !important;
    min-height: 100dvh;
    overflow-y: auto !important;
    overflow-x: hidden !important;
}

@media (max-width: 991.98px) {
    body > .d-flex {
        width: 100vw !important;
        height: 100dvh !important;
    }

    body > .d-flex > [class*="flex-grow"] {
        margin-left: 0 !important;
        width: 100vw !important;
        max-width: 100vw !important;
    }

    .sidebar {
        position: fixed !important;
        inset: 0 auto 0 0 !important;
        width: min(82vw, var(--app-sidebar-width)) !important;
        height: 100dvh !important;
        min-height: 100dvh !important;
        transform: translateX(-100%) !important;
        z-index: 1050 !important;
    }

    .sidebar.active {
        transform: translateX(0) !important;
    }

    .navbar {
        left: 0 !important;
        min-height: var(--app-header-height);
    }

    .navbar .container-fluid {
        flex-wrap: nowrap !important;
    }

    .toggle-sidebar {
        display: inline-flex !important;
        align-items: center;
        justify-content: center;
        width: 40px;
        height: 40px;
        flex: 0 0 40px;
    }

    body > .d-flex > [class*="flex-grow"] > main,
    .dashboard-main {
        margin-top: var(--app-header-height) !important;
    }
}

@media (max-width: 767.98px) {
    body {
        font-size: 0.88rem;
    }

    body > .d-flex > [class*="flex-grow"] > main,
    .dashboard-main {
        padding: var(--app-mobile-gutter) !important;
    }

    main > .container,
    main > .container-fluid,
    main > .container-xl,
    .trend-shell,
    .page-shell {
        width: 100% !important;
        max-width: 100% !important;
        padding-left: 0 !important;
        padding-right: 0 !important;
    }

    .row {
        --bs-gutter-x: 12px;
        --bs-gutter-y: 12px;
    }

    h1,
    .page-title,
    .users-page-title {
        font-size: clamp(1.25rem, 6vw, 1.55rem) !important;
        line-height: 1.2 !important;
        flex-wrap: wrap;
    }

    h2 {
        font-size: clamp(1.12rem, 5vw, 1.35rem) !important;
        line-height: 1.25 !important;
    }

    h3,
    .chart-title,
    .section-title,
    .filter-title {
        font-size: clamp(1rem, 4.5vw, 1.2rem) !important;
        line-height: 1.25 !important;
    }

    .card,
    .filter-card,
    .chart-container,
    .form-card,
    .table-responsive,
    .table-list,
    .summary-card,
    .metric-card,
    .stat-card,
    .profile-card,
    .progress-card {
        border-radius: 10px !important;
        padding: 12px !important;
        margin-bottom: 12px !important;
    }

    .card-header,
    .modal-header {
        padding: 12px 14px !important;
    }

    .card-body,
    .modal-body,
    [id="summaryTabContent"],
    [id="filtersTabContent"] {
        padding: 14px !important;
    }

    main .container-fluid > div[style*="justify-content:space-between"],
    main .container-fluid > div[style*="justify-content: space-between"] {
        flex-direction: column !important;
        align-items: stretch !important;
    }

    main .container-fluid > div[style*="justify-content:space-between"] > div,
    main .container-fluid > div[style*="justify-content: space-between"] > div {
        width: 100% !important;
    }

    main .container-fluid > div[style*="justify-content:space-between"] > div[style*="display: flex"],
    main .container-fluid > div[style*="justify-content: space-between"] > div[style*="display: flex"] {
        display: grid !important;
        grid-template-columns: 1fr;
        gap: 8px !important;
    }

    #filtersTab,
    #summaryTab {
        flex: 1 1 0 !important;
        min-width: 130px;
        padding: 10px 12px !important;
        text-align: center;
    }

    #filtersTabContent,
    #summaryTabContent {
        overflow-x: hidden;
    }

    .filter-actions {
        flex-direction: column !important;
        align-items: stretch !important;
        justify-content: flex-start !important;
    }

    .filter-actions > * {
        width: 100% !important;
        min-width: 0 !important;
        order: initial !important;
    }

    .filter-actions .pagination {
        justify-content: center;
    }

    .btn-filter,
    .btn-print,
    .btn-reset,
    .btn-submit,
    .btn-pdf,
    .btn-danger-custom,
    .form-actions .btn,
    .form-actions .btn-submit {
        width: 100% !important;
        justify-content: center !important;
        text-align: center !important;
    }

    .btn-group {
        width: 100%;
        gap: 6px;
    }

    .btn-group .btn {
        flex: 1 1 auto;
    }

    .form-control,
    .form-select,
    textarea.form-control {
        width: 100%;
        min-height: 42px;
        font-size: 16px !important;
    }

    .dashboard-category-chart,
    .chart-category-container {
        flex-direction: column !important;
        height: auto !important;
        align-items: stretch !important;
    }

    .dashboard-category-chart #categoryLegend,
    .category-legend {
        flex: 0 0 auto !important;
        width: 100% !important;
        max-height: 150px !important;
        padding-right: 0 !important;
        margin-bottom: 10px;
    }

    .dashboard-category-canvas,
    .category-chart-wrapper {
        min-height: 240px !important;
        width: 100% !important;
    }

    .chart-stage,
    .chart-wrap {
        height: 240px !important;
        min-height: 220px;
    }

    .report-donut-stage {
        height: auto !important;
        min-height: 0 !important;
        overflow: visible !important;
    }

    .report-donut-canvas {
        height: 240px !important;
        min-height: 240px !important;
    }

    .report-donut-legend {
        max-height: 170px !important;
    }

    .table-section-header {
        align-items: flex-start !important;
        flex-direction: column;
    }

    .table-responsive > table,
    .table-responsive .table {
        min-width: 640px;
    }

    .modal-dialog {
        margin: 0.5rem !important;
        max-width: calc(100% - 1rem) !important;
    }

    .modal-body {
        max-height: calc(100dvh - 120px);
        overflow-y: auto;
    }

    .pdf-preview-frame,
    .pdf-preview-loading {
        min-height: 320px !important;
        height: 60dvh !important;
    }

    .account-layout {
        grid-template-columns: 1fr !important;
    }

    .profile-card {
        position: static !important;
    }

    .report-meta {
        padding: 12px !important;
        word-break: break-word;
    }

    .insight-item {
        gap: 8px;
    }

    body:has(.login-container),
    body:has(.register-container) {
        align-items: flex-start !important;
        padding: 14px !important;
    }

    .login-container,
    .register-container {
        max-width: 100% !important;
        padding: 0 !important;
    }

    .login-header,
    .register-header {
        padding: 22px 16px !important;
    }

    .login-header h1,
    .register-header h1 {
        font-size: 1.45rem !important;
        line-height: 1.2;
    }

    .login-body,
    .register-body,
    .login-footer,
    .register-footer {
        padding: 18px !important;
    }

    .form-row {
        grid-template-columns: 1fr !important;
        gap: 0 !important;
    }
}

@media (max-width: 575.98px) {
    :root {
        --app-mobile-gutter: 10px;
    }

    .navbar-brand {
        max-width: 42vw;
        font-size: 0.95rem !important;
    }

    .navbar .nav-link {
        max-width: 34vw;
        font-size: 0.86rem;
        padding-left: 4px;
        padding-right: 4px;
    }

    .navbar .user-avatar {
        width: 30px !important;
        height: 30px !important;
        font-size: 0.75rem !important;
        margin-right: 4px !important;
    }

    .table-responsive > table,
    .table-responsive .table {
        min-width: 600px;
    }

    .pagination .page-link {
        padding: 6px 9px;
        min-width: 34px;
    }

    .badge,
    .badge-user,
    .badge-amount,
    .badge-category,
    .badge-role,
    .badge-active,
    .badge-inactive,
    .badge-action,
    .trend-badge {
        white-space: normal;
    }

    body.p-4 {
        padding: 1rem !important;
    }

    .container.my-5 {
        margin-top: 1rem !important;
        margin-bottom: 1rem !important;
    }

    #log {
        height: 220px !important;
    }
}
