/* -------------------------------------------------------------------------- */
/* 1. VARIABLES & RESET                                                      */
/* -------------------------------------------------------------------------- */
@import url('https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700&display=swap');

:root {
    --background: #ffffff;
    --foreground: #020817;
    --muted: #f1f5f9;
    --muted-foreground: #64748b;
    --card: #ffffff;
    --card-foreground: #020817;
    --border: #e2e8f0;
    --input: #e2e8f0;
    --primary: #0f172a;
    --primary-foreground: #f8fafc;
    --destructive: #ef4444;
    --destructive-foreground: #f8fafc;
    --ring: #94a3b8;
    --radius: 0.375rem;
    
    /* Layout Variables - Compact */
    --sidebar-width: 250px;
    --sidebar-collapsed-width: 60px;
    --header-height: 56px; 
}

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

body {
    font-family: 'Inter', sans-serif;
    background-color: #f8fafc;
    color: var(--foreground);
    line-height: 1.4; /* Tighter line height */
    font-size: 0.875rem; /* 14px */
    -webkit-font-smoothing: antialiased;
}

/* -------------------------------------------------------------------------- */
/* 2. GLOBAL COMPONENTS                                                      */
/* -------------------------------------------------------------------------- */

/* Utilities */
.w-full { width: 100%; }
.mb-4 { margin-bottom: 0.75rem; } /* Reduced from 1rem */
.mt-4 { margin-top: 0.75rem; }
.text-center { text-align: center; }

/* Flex Center (Login Page) */
.flex-center {
    display: flex;
    justify-content: center;
    align-items: center;
    min-height: 100vh;
    padding: 1rem;
}

/* Card Component - Compact */
.card {
    background-color: var(--card);
    color: var(--card-foreground);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    box-shadow: 0 1px 2px 0 rgba(0, 0, 0, 0.05);
    overflow: hidden;
}

.flex-center .card { width: 100%; max-width: 380px; }

/* Tighter Card Spacing */
.card-header { padding: 1rem 1rem 0.5rem; } 
.card-title { font-size: 1.125rem; font-weight: 600; margin-bottom: 0.125rem; }
.card-description { font-size: 0.8125rem; color: var(--muted-foreground); }
.card-content { padding: 1rem; }
.card-footer { padding: 0.75rem 1rem; background-color: #f8fafc; border-top: 1px solid var(--border); }

/* Inputs */
.form-group { margin-bottom: 0.75rem; }
.label { display: block; font-size: 0.8125rem; font-weight: 500; margin-bottom: 0.375rem; }
.input {
    display: flex;
    height: 2.25rem; /* 36px */
    width: 100%;
    border-radius: var(--radius);
    border: 1px solid var(--input);
    background-color: transparent;
    padding: 0.25rem 0.75rem;
    font-size: 0.875rem;
    transition: all 0.2s;
}
.input:focus { outline: none; border-color: var(--ring); box-shadow: 0 0 0 2px rgba(148, 163, 184, 0.2); }

/* Buttons */
.btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    border-radius: var(--radius);
    font-size: 0.875rem;
    font-weight: 500;
    height: 2.25rem;
    padding: 0 1rem;
    cursor: pointer;
    transition: background-color 0.2s;
    border: none;
    text-decoration: none;
}
.btn-primary { background-color: var(--primary); color: var(--primary-foreground); width: 100%; }
.btn-primary:hover { background-color: rgba(15, 23, 42, 0.9); }
.btn-sm { height: 1.75rem; padding: 0 0.75rem; font-size: 0.75rem; }

/* Alerts */
.alert-destructive {
    background-color: #fef2f2;
    border: 1px solid #fecaca;
    color: #991b1b;
    padding: 0.625rem;
    border-radius: var(--radius);
    font-size: 0.8125rem;
    margin-bottom: 0.75rem;
    display: flex;
    align-items: center;
    gap: 0.5rem;
}
.alert-icon { width: 1rem; height: 1rem; }

/* -------------------------------------------------------------------------- */
/* 3. LAYOUT & SIDEBAR                                                       */
/* -------------------------------------------------------------------------- */

.app-wrapper { display: flex; min-height: 100vh; }

.sidebar {
    width: var(--sidebar-width);
    background: var(--card);
    border-right: 1px solid var(--border);
    display: flex;
    flex-direction: column;
    position: fixed;
    top: 0;
    left: 0;
    height: 100vh;
    transition: width 0.3s ease, transform 0.3s ease;
    z-index: 50;
}
.sidebar-header {
    height: var(--header-height);
    display: flex;
    align-items: center;
    padding: 0 1.25rem;
    font-weight: 700;
    font-size: 1.125rem;
    border-bottom: 1px solid var(--border);
    white-space: nowrap;
    overflow: hidden;
}
.sidebar-nav { flex: 1; padding: 0.75rem 0.5rem; overflow-y: auto; }
.nav-label { font-size: 0.7rem; font-weight: 600; color: var(--muted-foreground); padding: 0.5rem 0.75rem; text-transform: uppercase; letter-spacing: 0.05em; }
.nav-link {
    display: flex; align-items: center; gap: 0.625rem;
    padding: 0.5rem 0.75rem;
    color: var(--muted-foreground); text-decoration: none;
    border-radius: 0.375rem; transition: all 0.2s;
    white-space: nowrap; overflow: hidden;
    font-size: 0.8125rem;
    margin-bottom: 0.25rem;
}
.nav-link:hover { background-color: var(--muted); color: var(--foreground); }
.nav-link.active { background-color: var(--primary); color: var(--primary-foreground); }
.nav-icon { width: 1.125rem; height: 1.125rem; flex-shrink: 0; }
.sidebar-footer { padding: 0.75rem; border-top: 1px solid var(--border); font-size: 0.7rem; color: var(--muted-foreground); white-space: nowrap; overflow: hidden; }

/* Collapsed State */
body.collapsed .sidebar { width: var(--sidebar-collapsed-width); }
body.collapsed .sidebar-header span:not(.logo-icon), 
body.collapsed .nav-link span, 
body.collapsed .nav-label, 
body.collapsed .sidebar-footer span { display: none; }
body.collapsed .sidebar-header { justify-content: center; padding: 0; }
body.collapsed .nav-link { justify-content: center; padding: 0.5rem 0; }
body.collapsed .main-content { margin-left: var(--sidebar-collapsed-width); }

.main-content {
    flex: 1;
    margin-left: var(--sidebar-width);
    transition: margin-left 0.3s ease;
    min-height: 100vh;
    display: flex;
    flex-direction: column;
}

.header {
    height: var(--header-height);
    background: var(--background);
    border-bottom: 1px solid var(--border);
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 0 1.5rem;
    position: sticky;
    top: 0;
    z-index: 40;
}
.toggle-btn { background: none; border: none; cursor: pointer; color: var(--muted-foreground); padding: 0.25rem; }
.user-menu { display: flex; align-items: center; gap: 0.75rem; }
.page-content { padding: 1.25rem; }

/* -------------------------------------------------------------------------- */
/* 4. DASHBOARD SPECIFIC & TABLES                                            */
/* -------------------------------------------------------------------------- */

/* Dashboard Header */
.dashboard-header { margin-bottom: 1.25rem; }
.dashboard-title { font-size: 1.5rem; font-weight: 700; letter-spacing: -0.025em; margin-bottom: 0.25rem; }
.dashboard-subtitle { color: var(--muted-foreground); font-size: 0.875rem; }

/* Stats Card */
.grid-cards {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(220px, 1fr));
    gap: 1rem; /* Compact gap */
    margin-bottom: 1.25rem;
}
.stat-label { font-size: 0.75rem; color: var(--muted-foreground); font-weight: 500; }
.stat-value { font-size: 1.5rem; font-weight: 700; color: var(--foreground); margin-top: 0.25rem; letter-spacing: -0.025em; }
.stat-desc { font-size: 0.75rem; color: #16a34a; margin-top: 0.25rem; display: flex; align-items: center; gap: 0.25rem; }
.stat-desc.negative { color: #dc2626; }

/* Tables */
.table-container { width: 100%; overflow-x: auto; border-radius: var(--radius); border: 1px solid var(--border); }
.dash-table { width: 100%; border-collapse: collapse; font-size: 0.8125rem; background-color: var(--card); }
.dash-table th { text-align: left; padding: 0.5rem 0.75rem; color: var(--muted-foreground); font-weight: 500; border-bottom: 1px solid var(--border); background-color: #f8fafc; white-space: nowrap; }
.dash-table td { padding: 0.5rem 0.75rem; border-bottom: 1px solid var(--border); color: var(--foreground); vertical-align: middle; }
.dash-table tr:last-child td { border-bottom: none; }
.dash-table tr:hover { background-color: #f8fafc; }

/* Badges */
.badge { display: inline-flex; align-items: center; padding: 0.125rem 0.5rem; border-radius: 9999px; font-size: 0.7rem; font-weight: 500; line-height: 1.2; }
.badge-gray { background: #f1f5f9; color: #475569; }
.badge-green { background: #dcfce7; color: #166534; }
.badge-red { background: #fee2e2; color: #991b1b; }
.badge-paid { background: #dcfce7; color: #166534; }
.badge-pending { background: #ffedd5; color: #9a3412; }

/* Action Buttons */
.action-btn { background: none; border: none; cursor: pointer; padding: 0.25rem; color: var(--muted-foreground); transition: color 0.2s; }
.action-btn:hover { color: var(--foreground); }
.action-btn.delete:hover { color: var(--destructive); }

/* Search & Pagination */
.toolbar { display: flex; justify-content: space-between; align-items: center; gap: 1rem; margin-bottom: 0.75rem; }
.search-container { position: relative; max-width: 300px; width: 100%; }
.search-icon { position: absolute; left: 0.75rem; top: 50%; transform: translateY(-50%); color: var(--muted-foreground); width: 0.875rem; height: 0.875rem; }
.search-input { padding-left: 2.25rem; }
.pagination { display: flex; align-items: center; justify-content: space-between; padding-top: 0.75rem; border-top: 1px solid var(--border); margin-top: 0; }
.pagination-info { font-size: 0.75rem; color: var(--muted-foreground); }
.pagination-btns { display: flex; gap: 0.25rem; }
.page-btn { display: inline-flex; align-items: center; justify-content: center; width: 1.75rem; height: 1.75rem; border-radius: var(--radius); border: 1px solid var(--border); background: var(--background); color: var(--foreground); font-size: 0.8125rem; text-decoration: none; transition: all 0.2s; }
.page-btn:hover:not(.disabled) { background-color: var(--muted); }
.page-btn.active { background-color: var(--primary); color: var(--primary-foreground); border-color: var(--primary); }
.page-btn.disabled { opacity: 0.5; cursor: not-allowed; }

/* Mobile */
@media (max-width: 768px) {
    .sidebar { transform: translateX(-100%); width: 250px !important; }
    body.mobile-open .sidebar { transform: translateX(0); }
    .main-content { margin-left: 0 !important; }
    body.collapsed .sidebar { width: 250px; }
}

/* -------------------------------------------------------------------------- */
/* 5. MOBILE RESPONSIVE OVERRIDES                                            */
/* -------------------------------------------------------------------------- */

@media (max-width: 768px) {
    /* --- Layout & Sidebar --- */
    .app-wrapper {
        position: relative;
        overflow-x: hidden;
    }

    .sidebar {
        transform: translateX(-100%);
        width: 250px !important;
        box-shadow: 2px 0 10px rgba(0,0,0,0.1);
    }

    /* When mobile menu is open (requires JS toggling .mobile-open on body) */
    body.mobile-open .sidebar {
        transform: translateX(0);
    }
    
    /* Overlay effect for open sidebar */
    body.mobile-open::before {
        content: '';
        position: fixed;
        top: 0; left: 0; right: 0; bottom: 0;
        background: rgba(0,0,0,0.5);
        z-index: 45;
        backdrop-filter: blur(2px);
    }

    .main-content {
        margin-left: 0 !important;
        width: 100%;
    }

    .header {
        padding: 0 1rem;
    }

    /* --- Dashboard Elements --- */
    
    /* Stack Grid Cards (Stats) */
    .grid-cards {
        grid-template-columns: 1fr;
    }

    /* --- Toolbar (Search & Filters) --- */
    .toolbar {
        flex-direction: column;
        align-items: stretch;
        gap: 0.75rem;
    }

    .search-container {
        max-width: 100%;
    }

    .toolbar select, 
    .toolbar .btn {
        width: 100% !important;
        justify-content: center;
    }

    /* --- Tables --- */
    /* Ensure tables can scroll horizontally and font is readable */
    .table-container {
        margin: 0 -1rem; /* Negative margin to let table touch edges */
        width: calc(100% + 2rem);
        border-left: none; 
        border-right: none;
        border-radius: 0;
    }
    
    .dash-table th, 
    .dash-table td {
        padding: 0.75rem 0.5rem;
        white-space: nowrap; /* Prevent wrapping inside cells */
    }

    /* --- Pagination --- */
    .pagination {
        flex-direction: column;
        gap: 1rem;
        align-items: center;
    }
    
    .pagination-info {
        text-align: center;
        width: 100%;
    }

    /* --- AGGRESSIVE INLINE STYLE OVERRIDES --- */
    /* This targets the inline flex/grid styles used in your PHP files 
       to force them to stack vertically on mobile */

    /* 1. Force Page Headers (Title + Buttons) to stack */
    div[style*="justify-content: space-between"] {
        flex-direction: column;
        align-items: flex-start !important;
        gap: 1rem;
    }
    
    div[style*="justify-content: space-between"] > div,
    div[style*="justify-content: space-between"] > a,
    div[style*="justify-content: space-between"] > button {
        width: 100%; /* Make action buttons full width */
        margin-bottom: 0.5rem;
    }

    /* 2. Force Form Grids (2 cols / 3 cols) to stack into 1 column */
    form div[style*="grid-template-columns"],
    .card-content div[style*="grid-template-columns"] {
        display: flex !important;
        flex-direction: column !important;
        gap: 0.75rem !important;
    }

    /* 3. Adjust Modals */
    /* Target elements that look like modals based on fixed positioning */
    div[style*="position: fixed"] .card {
        width: 95% !important;
        max-height: 85vh;
        margin: 1rem auto;
    }

    /* 4. Login/Center Pages */
    .flex-center {
        padding: 1rem;
        align-items: flex-start; /* Push to top so keyboard doesn't hide input */
        padding-top: 10vh;
    }
    
    .flex-center .card {
        width: 100%;
    }
}