:root {
    --sidebar-width: 260px;
    --sidebar-collapsed: 70px;
    --primary: #4361ee;
    --primary-light: #e8ecff;
    --success: #2ec4b6;
    --warning: #ff9f1c;
    --danger: #e71d36;
    --info: #17a2b8;
    --dark: #1a1a2e;
    --gray: #6c757d;
}

* { -webkit-tap-highlight-color: transparent; }

body {
    font-family: 'Segoe UI', system-ui, -apple-system, sans-serif;
    background-color: #f0f2f5;
    min-height: 100vh;
    overflow-x: hidden;
}

.wrapper { min-height: 100vh; }

/* ==================== SIDEBAR ==================== */
.sidebar {
    width: var(--sidebar-width);
    min-height: 100vh;
    position: fixed;
    top: 0;
    left: 0;
    z-index: 1050;
    transition: transform 0.3s ease;
    overflow-y: auto;
    overflow-x: hidden;
    background: linear-gradient(180deg, #1a1a2e 0%, #16213e 100%) !important;
    -webkit-overflow-scrolling: touch;
}

.sidebar-nav .nav-link {
    padding: 0.6rem 1rem;
    margin: 2px 8px;
    border-radius: 8px;
    transition: all 0.2s;
    font-size: 0.9rem;
    white-space: nowrap;
}

.sidebar-nav .nav-link:hover { background: rgba(255,255,255,0.1); }

.sidebar-nav .nav-link.active {
    background: var(--primary);
    color: #fff !important;
    box-shadow: 0 2px 8px rgba(67, 97, 238, 0.4);
}

.sidebar-header { border-color: rgba(255,255,255,0.1) !important; }

.sidebar-close-btn {
    text-decoration: none !important;
    opacity: 0.8;
    transition: opacity 0.2s;
    line-height: 1;
}
.sidebar-close-btn:hover { opacity: 1; }

.main-content {
    margin-left: var(--sidebar-width);
    transition: margin-left 0.3s ease;
    min-height: 100vh;
    display: flex;
    flex-direction: column;
    width: calc(100% - var(--sidebar-width));
}

.content-area { flex: 1; }

/* Sidebar overlay */
.sidebar-overlay {
    position: fixed;
    inset: 0;
    background: rgba(0,0,0,0.5);
    z-index: 1045;
    display: none;
    -webkit-backdrop-filter: blur(2px);
    backdrop-filter: blur(2px);
}
.sidebar-overlay.show { display: block; }

/* Custom scrollbar */
.sidebar::-webkit-scrollbar { width: 4px; }
.sidebar::-webkit-scrollbar-track { background: transparent; }
.sidebar::-webkit-scrollbar-thumb { background: rgba(255,255,255,0.2); border-radius: 4px; }

/* ==================== CARDS ==================== */
.stat-card {
    border: none;
    border-radius: 12px;
    transition: transform 0.2s, box-shadow 0.2s;
    overflow: hidden;
}
.stat-card:hover {
    transform: translateY(-2px);
    box-shadow: 0 8px 25px rgba(0,0,0,0.1);
}
.stat-card .card-body { padding: 1.25rem; }

.stat-icon {
    width: 48px;
    height: 48px;
    border-radius: 12px;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 1.3rem;
    flex-shrink: 0;
}

/* ==================== TABLES ==================== */
.table-card {
    border: none;
    border-radius: 12px;
    box-shadow: 0 2px 12px rgba(0,0,0,0.06);
    overflow: hidden;
}

.table thead th {
    background: #f8f9fa;
    border-bottom: 2px solid #dee2e6;
    font-weight: 600;
    font-size: 0.85rem;
    text-transform: uppercase;
    letter-spacing: 0.5px;
    color: #495057;
    white-space: nowrap;
}

.table-responsive { -webkit-overflow-scrolling: touch; }

/* ==================== BADGES ==================== */
.badge-status {
    padding: 0.35em 0.75em;
    border-radius: 6px;
    font-weight: 500;
    font-size: 0.78rem;
}

/* ==================== LOGIN ==================== */
.login-wrapper {
    min-height: 100vh;
    display: flex;
    align-items: center;
    justify-content: center;
    background: linear-gradient(135deg, #1a1a2e 0%, #16213e 50%, #0f3460 100%);
    padding: 1rem;
}

.login-card {
    width: 100%;
    max-width: 420px;
    border: none;
    border-radius: 16px;
    box-shadow: 0 20px 60px rgba(0,0,0,0.3);
}
.login-card .card-body { padding: 2.5rem; }

/* ==================== FORMS ==================== */
.form-control:focus, .form-select:focus {
    border-color: var(--primary);
    box-shadow: 0 0 0 0.2rem rgba(67, 97, 238, 0.15);
}

.select2-container--bootstrap-5 .select2-selection {
    border-radius: 0.375rem;
    min-height: 38px;
}

/* ==================== PAGE HEADER ==================== */
.page-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 1.5rem;
    flex-wrap: wrap;
    gap: 0.5rem;
}
.page-header h4 {
    margin: 0;
    font-weight: 700;
    color: #1a1a2e;
}

/* ==================== MODALS ==================== */
.modal-header {
    background: linear-gradient(135deg, var(--primary), #3a56d4);
    color: #fff;
    border-radius: 0;
}
.modal-header .btn-close { filter: brightness(0) invert(1); }

/* ==================== ACTION BUTTONS ==================== */
.btn-action {
    padding: 0.25rem 0.5rem;
    font-size: 0.8rem;
    border-radius: 6px;
}

/* ==================== DATATABLES ==================== */
div.dataTables_wrapper div.dataTables_filter input {
    border-radius: 8px;
    padding: 0.375rem 0.75rem;
}
div.dataTables_wrapper div.dataTables_length select {
    border-radius: 8px;
}
.dt-top-bar, .dt-bottom-bar { padding: 0 0.25rem; }

/* Ensure datatable wrapper doesn't overflow the page */
.dataTables_wrapper { max-width: 100%; overflow: hidden; }

/* DataTables responsive: expand/collapse control */
table.dataTable.dtr-inline.collapsed > tbody > tr > td.dtr-control,
table.dataTable.dtr-inline.collapsed > tbody > tr > th.dtr-control {
    position: relative;
    padding-left: 30px !important;
    cursor: pointer;
}
table.dataTable.dtr-inline.collapsed > tbody > tr > td.dtr-control::before,
table.dataTable.dtr-inline.collapsed > tbody > tr > th.dtr-control::before {
    content: '\25B6';
    position: absolute;
    top: 50%;
    left: 8px;
    transform: translateY(-50%);
    display: inline-block;
    color: var(--primary);
    font-size: 0.75rem;
    transition: transform 0.2s;
}
table.dataTable.dtr-inline.collapsed > tbody > tr.parent > td.dtr-control::before,
table.dataTable.dtr-inline.collapsed > tbody > tr.parent > th.dtr-control::before {
    content: '\25BC';
    color: var(--danger);
}

/* DataTables responsive: child row (hidden column details) */
table.dataTable > tbody > tr.child ul.dtr-details {
    list-style: none;
    padding: 0;
    margin: 0;
    width: 100%;
}
table.dataTable > tbody > tr.child ul.dtr-details > li {
    display: flex;
    justify-content: space-between;
    padding: 0.4rem 0.5rem;
    border-bottom: 1px solid #f0f0f0;
    font-size: 0.85rem;
}
table.dataTable > tbody > tr.child ul.dtr-details > li:last-child {
    border-bottom: none;
}
table.dataTable > tbody > tr.child ul.dtr-details > li .dtr-title {
    font-weight: 600;
    color: #495057;
    min-width: 90px;
    margin-right: 0.75rem;
    flex-shrink: 0;
}
table.dataTable > tbody > tr.child ul.dtr-details > li .dtr-data {
    text-align: right;
    flex: 1;
    min-width: 0;
    word-break: break-word;
}
table.dataTable > tbody > tr.child td {
    background: #f8f9fc;
    padding: 0.5rem !important;
}
table.dataTable > tbody > tr.child:hover td {
    background: #f0f2f8 !important;
}

/* ==================== FINANCE CARDS ==================== */
.finance-card {
    border-left: 4px solid;
    border-radius: 8px;
}
.finance-card.income { border-left-color: var(--success); }
.finance-card.expense { border-left-color: var(--danger); }
.finance-card.profit { border-left-color: var(--primary); }
.finance-card.debt { border-left-color: var(--warning); }

/* ==================== CALENDAR ==================== */
.calendar-cell {
    min-height: 100px;
    border: 1px solid #dee2e6;
    padding: 4px;
    vertical-align: top;
    cursor: pointer;
    transition: background 0.2s;
}
.calendar-cell:hover { background: var(--primary-light); }
.calendar-cell.today { background: #fff3cd; }

.calendar-event {
    font-size: 0.72rem;
    padding: 2px 6px;
    border-radius: 4px;
    margin-bottom: 2px;
    color: #fff;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    cursor: pointer;
}

/* ==================== ANIMATIONS ==================== */
@keyframes fadeIn {
    from { opacity: 0; transform: translateY(10px); }
    to { opacity: 1; transform: translateY(0); }
}
.content-area > * { animation: fadeIn 0.3s ease; }

/* ==================== PRINT ==================== */
@media print {
    .sidebar, .navbar, footer, .no-print, .sidebar-overlay { display: none !important; }
    .main-content { margin-left: 0 !important; width: 100% !important; }
}

/* ==============================================================
   RESPONSIVE: TABLET (max-width: 991.98px)
   ============================================================== */
@media (max-width: 991.98px) {
    .sidebar {
        transform: translateX(-100%);
    }
    .sidebar.show {
        transform: translateX(0);
    }
    .main-content {
        margin-left: 0;
        width: 100%;
    }

    /* Prevent body scroll when sidebar is open */
    body.sidebar-open { overflow: hidden; }

    .page-header h4 { font-size: 1.15rem; }

    /* Stat cards smaller */
    .stat-card .card-body { padding: 1rem; }
    .stat-icon { width: 40px; height: 40px; font-size: 1.1rem; }
    .stat-card .fs-4 { font-size: 1.25rem !important; }
    .stat-card .fs-5 { font-size: 1rem !important; }
}

/* ==============================================================
   RESPONSIVE: MOBILE (max-width: 767.98px)
   ============================================================== */
@media (max-width: 767.98px) {
    /* Sidebar full width on small phones */
    .sidebar { width: 280px; }

    /* Content */
    .content-area { padding: 0.75rem !important; }

    /* Page header stack */
    .page-header {
        flex-direction: column;
        align-items: flex-start;
        gap: 0.75rem;
        margin-bottom: 1rem;
    }
    .page-header h4 { font-size: 1.1rem; }
    .page-header .d-flex { width: 100%; flex-wrap: wrap; }

    /* Stat cards 2 columns */
    .stat-card .card-body { padding: 0.75rem; }
    .stat-card .fs-4 { font-size: 1.1rem !important; }
    .stat-card .fs-5 { font-size: 0.95rem !important; }
    .stat-icon { width: 36px; height: 36px; font-size: 1rem; }

    /* Tables */
    .table-card .card-body { padding: 0.5rem; }
    .table td, .table th { font-size: 0.8rem; padding: 0.4rem 0.5rem; }
    .btn-action { padding: 0.2rem 0.4rem; font-size: 0.75rem; }
    .btn-group .btn-action { padding: 0.3rem 0.5rem; }

    /* Ensure DataTables wrapper doesn't overflow */
    .dataTables_wrapper { overflow: hidden; }

    /* DataTables responsive child row adjustments */
    table.dataTable > tbody > tr.child ul.dtr-details > li {
        font-size: 0.8rem;
        padding: 0.35rem 0.4rem;
    }
    table.dataTable.dtr-inline.collapsed > tbody > tr > td.dtr-control {
        padding-left: 26px !important;
    }
    table.dataTable.dtr-inline.collapsed > tbody > tr > td.dtr-control::before {
        left: 6px;
        font-size: 0.65rem;
    }

    /* DataTables mobile overrides */
    div.dataTables_wrapper div.dataTables_filter,
    div.dataTables_wrapper div.dataTables_length {
        text-align: left !important;
        width: 100%;
    }
    div.dataTables_wrapper div.dataTables_filter input {
        width: 100% !important;
        margin-left: 0 !important;
    }
    div.dataTables_wrapper div.dataTables_info,
    div.dataTables_wrapper div.dataTables_paginate {
        text-align: center !important;
        width: 100%;
        margin-top: 0.5rem;
    }
    div.dataTables_wrapper div.dataTables_paginate .pagination {
        justify-content: center;
        flex-wrap: wrap;
    }
    .dataTables_wrapper .dataTables_paginate .paginate_button {
        padding: 0.3rem 0.6rem !important;
        font-size: 0.8rem;
    }

    /* Modals: full-screen on mobile */
    .modal-dialog {
        margin: 0 !important;
        max-width: 100% !important;
        min-height: 100vh;
    }
    .modal-content {
        border: none !important;
        border-radius: 0 !important;
        min-height: 100vh;
    }
    .modal-body {
        padding: 1rem !important;
        overflow-y: auto;
        -webkit-overflow-scrolling: touch;
    }
    .modal-header {
        border-radius: 0 !important;
        padding: 0.75rem 1rem;
        position: sticky;
        top: 0;
        z-index: 10;
    }
    .modal-header .modal-title { font-size: 1rem; }
    .modal-footer {
        position: sticky;
        bottom: 0;
        background: #fff;
        z-index: 10;
        padding: 0.75rem 1rem;
        border-top: 1px solid #dee2e6;
    }

    /* Modal form elements */
    .modal .row.g-3 > [class*="col-md"] { flex: 0 0 100%; max-width: 100%; }
    .modal .col-md-2, .modal .col-md-3, .modal .col-md-4,
    .modal .col-md-5, .modal .col-md-6, .modal .col-md-7,
    .modal .col-md-8, .modal .col-md-12 {
        flex: 0 0 100%;
        max-width: 100%;
    }
    .modal .form-label { font-size: 0.85rem; margin-bottom: 0.25rem; }
    .modal .form-control, .modal .form-select { font-size: 0.9rem; }

    /* Calendar cells smaller */
    .calendar-cell { min-height: 60px; padding: 2px; }
    .calendar-event { font-size: 0.6rem; padding: 1px 3px; }

    /* Cards layout */
    .card-header { padding: 0.75rem !important; }
    .card-body { padding: 0.75rem; }
    .card-header h6 { font-size: 0.9rem; }
    .card-header .badge.fs-6 { font-size: 0.8rem !important; }

    /* Concept & menu cards: full width */
    .col-xl-4.col-md-6, .col-xl-6 { flex: 0 0 100%; max-width: 100%; }

    /* Login */
    .login-card .card-body { padding: 1.5rem; }

    /* Select2 in modals */
    .select2-container { width: 100% !important; }
    .select2-dropdown { z-index: 1060; }

    /* SweetAlert mobile */
    .swal2-popup { font-size: 0.9rem !important; width: 90% !important; }

    /* Navbar */
    .navbar { padding: 0.5rem 0.75rem !important; }

    /* Finance card text */
    .finance-card .fs-4 { font-size: 1.1rem !important; }

    /* Input groups in discount fields */
    .input-group .form-control { min-width: 0; }
    .input-group .form-select { min-width: 60px; }

    /* Price breakdown table in appointment modal */
    .modal .table-sm td { font-size: 0.8rem; padding: 0.3rem 0.25rem; }

    /* Dropdown menus */
    .dropdown-menu { font-size: 0.85rem; }
}

/* ==============================================================
   RESPONSIVE: SMALL PHONES (max-width: 575.98px)
   ============================================================== */
@media (max-width: 575.98px) {
    .sidebar { width: 260px; }
    .content-area { padding: 0.5rem !important; }

    .page-header h4 { font-size: 1rem; }
    .page-header .btn { font-size: 0.8rem; padding: 0.3rem 0.6rem; }

    /* Calendar: show only day number + event count on very small screens */
    .calendar-cell { min-height: 45px; font-size: 0.7rem; }
    .calendar-event { font-size: 0.55rem; padding: 1px 2px; }

    /* Table button groups vertical on very small screens */
    .btn-group { flex-direction: column; gap: 2px; }
    .btn-group .btn-action { border-radius: 6px !important; }

    /* Modal price/deposit side by side */
    .modal .form-control-lg { font-size: 1rem !important; padding: 0.5rem 0.75rem; }

    /* Stat cards - hide icon on very small */
    .stat-icon { display: none; }
    .stat-card .card-body { padding: 0.6rem; }
}

/* ==============================================================
   RESPONSIVE: LARGE DESKTOP (min-width: 1400px)
   ============================================================== */
@media (min-width: 1400px) {
    .content-area { max-width: 1600px; }
}
