/* Modern CSS Reset and Base Styles */
* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

:root {
    --primary-color: #4f46e5;
    --primary-dark: #4338ca;
    --primary-light: #6366f1;
    --secondary-color: #10b981;
    --success-color: #10b981;
    --danger-color: #ef4444;
    --warning-color: #f59e0b;
    --info-color: #3b82f6;
    --dark-bg: #1f2937;
    --darker-bg: #111827;
    --light-bg: #f9fafb;
    --border-color: #e5e7eb;
    --text-primary: #111827;
    --text-secondary: #6b7280;
    --text-light: #9ca3af;
    --sidebar-width: 250px;
    --sidebar-collapsed: 70px;
    --topnav-height: 60px;
    --banner-height: 0px;
    --shadow-sm: 0 1px 2px 0 rgb(0 0 0 / 0.05);
    --shadow-md: 0 4px 6px -1px rgb(0 0 0 / 0.1);
    --shadow-lg: 0 10px 15px -3px rgb(0 0 0 / 0.1);
}

body {
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;
    font-size: 14px;
    line-height: 1.5;
    color: var(--text-primary);
    background-color: var(--light-bg);
}

/* Modal Styles */
.modal {
    display: flex;
    position: fixed;
    z-index: 1000;
    left: 0;
    top: 0;
    width: 100%;
    height: 100%;
    background-color: rgba(0, 0, 0, 0.12);
    align-items: center;
    justify-content: center;
}

.modal-content {
    background-color: white;
    border-radius: 12px;
    box-shadow: 0 8px 40px rgba(0, 0, 0, 0.22), 0 2px 8px rgba(0, 0, 0, 0.12);
    animation: slideIn 0.25s ease-out;
    min-width: 520px;
    max-width: 680px;
    width: 100%;
    max-height: 90vh;
    overflow-y: auto;
}

@keyframes slideIn {
    from {
        opacity: 0;
        transform: translateY(-20px);
    }
    to {
        opacity: 1;
        transform: translateY(0);
    }
}

.modal-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 20px;
    border-bottom: 1px solid var(--border-color);
}

.modal-header h3 {
    font-size: 18px;
    font-weight: 600;
    color: var(--text-primary);
    display: flex;
    align-items: center;
    gap: 10px;
}

.modal-footer {
    display: flex;
    justify-content: flex-end;
    gap: 10px;
    padding: 16px 20px;
    border-top: 1px solid var(--border-color);
    background-color: white;
    position: sticky;
    bottom: 0;
    z-index: 1;
}

.close-btn {
    background: none;
    border: none;
    font-size: 24px;
    color: var(--text-secondary);
    cursor: pointer;
    padding: 0;
    width: 32px;
    height: 32px;
    display: flex;
    align-items: center;
    justify-content: center;
    border-radius: 6px;
    transition: all 0.2s;
}

.close-btn:hover {
    background-color: var(--light-bg);
    color: var(--text-primary);
}

/* Checkbox alignment in form groups */
.form-group.checkbox-group {
    display: flex;
    align-items: center;
    gap: 8px;
    padding-top: 4px;
}
.form-group.checkbox-group input[type="checkbox"] {
    width: 16px;
    height: 16px;
    margin: 0;
    flex-shrink: 0;
    cursor: pointer;
}
.form-group.checkbox-group label {
    margin: 0;
    line-height: 1;
    cursor: pointer;
    font-weight: 500;
}

/* Portal-style login redirect box (shown when unauthenticated) */
.portal-login-redirect {
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
}

.portal-login-box {
    background: white;
    border-radius: 12px;
    box-shadow: 0 10px 15px -3px rgb(0 0 0 / 0.1);
    padding: 48px 40px;
    width: 420px;
    max-width: 90vw;
    text-align: center;
    animation: slideIn 0.3s ease-out;
}

.portal-login-logo {
    display: block;
    font-size: 24px;
    font-weight: 700;
    color: var(--primary-color);
    text-decoration: none;
    letter-spacing: -0.5px;
    margin-bottom: 24px;
}

.portal-login-box h2 {
    font-size: 22px;
    font-weight: 700;
    color: var(--text-primary);
    margin-bottom: 8px;
}

.portal-login-box p {
    font-size: 14px;
    color: var(--text-secondary);
    margin-bottom: 28px;
}

.portal-login-btn {
    display: block;
    width: 100%;
    padding: 13px;
    background: var(--primary-color);
    color: white;
    border-radius: 8px;
    font-size: 15px;
    font-weight: 600;
    text-decoration: none;
    transition: background 0.15s, box-shadow 0.15s;
    box-shadow: 0 4px 14px rgba(79,70,229,0.35);
    margin-bottom: 20px;
}

.portal-login-btn:hover {
    background: var(--primary-dark);
    box-shadow: 0 6px 20px rgba(79,70,229,0.4);
}

.portal-login-note {
    font-size: 13px;
    color: var(--text-secondary);
    margin-bottom: 0 !important;
}

.portal-login-note a {
    color: var(--primary-color);
    text-decoration: none;
}

.portal-login-note a:hover {
    text-decoration: underline;
}

/* Form Styles */
.form-group {
    margin-bottom: 20px;
}

.form-group label {
    display: block;
    margin-bottom: 6px;
    font-weight: 500;
    color: var(--text-primary);
}

.form-group input {
    width: 100%;
    padding: 10px 14px;
    border: 1px solid var(--border-color);
    border-radius: 6px;
    font-size: 14px;
    transition: all 0.2s;
}

.form-group input:focus {
    outline: none;
    border-color: var(--primary-color);
    box-shadow: 0 0 0 3px rgba(79, 70, 229, 0.1);
}

/* Button Styles */
.btn {
    padding: 10px 20px;
    border: none;
    border-radius: 6px;
    font-size: 14px;
    font-weight: 500;
    cursor: pointer;
    transition: all 0.2s;
    display: inline-flex;
    align-items: center;
    gap: 8px;
    text-decoration: none;
}

.btn-primary {
    background-color: var(--primary-color);
    color: white;
}

.btn-primary:hover {
    background-color: var(--primary-dark);
    transform: translateY(-1px);
    box-shadow: var(--shadow-md);
}

.btn-secondary {
    background-color: var(--secondary-color);
    color: white;
}

.btn-danger {
    background-color: var(--danger-color);
    color: white;
}

.btn-outline {
    background-color: transparent;
    border: 1px solid var(--border-color);
    color: var(--text-primary);
}

.btn-sm {
    padding: 6px 12px;
    font-size: 13px;
}

.btn-block {
    width: 100%;
    justify-content: center;
}

/* Top Navigation */
.topnav {
    position: fixed;
    top: var(--banner-height, 0px);
    left: 0;
    right: 0;
    height: var(--topnav-height);
    background-color: white;
    border-bottom: 1px solid var(--border-color);
    display: flex;
    align-items: center;
    padding: 0 20px;
    z-index: 100;
    box-shadow: var(--shadow-sm);
}

.menu-toggle {
    background: none;
    border: none;
    font-size: 20px;
    cursor: pointer;
    padding: 8px;
    color: var(--text-primary);
    margin-right: 15px;
    border-radius: 6px;
    transition: background-color 0.2s;
}

.menu-toggle:hover {
    background-color: var(--light-bg);
}

.nav-brand {
    display: flex;
    align-items: center;
    gap: 10px;
    font-size: 20px;
    font-weight: 700;
    color: var(--primary-color);
    text-decoration: none;
    padding: 8px 12px;
    border-radius: 6px;
    transition: background-color 0.2s;
}

.nav-brand:hover {
    background-color: var(--light-bg);
}

.nav-actions {
    margin-left: auto;
    display: flex;
    align-items: center;
    gap: 8px;
    min-width: 0;
}

.user-info {
    color: var(--text-secondary);
    font-size: 13px;
    padding: 4px 10px;
    border-radius: 6px;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    max-width: 180px;
    cursor: pointer;
}

.user-info:hover {
    background-color: var(--light-bg);
    color: var(--text-primary);
}

.btn-ghost {
    background-color: transparent;
    border: none;
    color: var(--text-secondary);
}

.btn-ghost:hover {
    background-color: var(--light-bg);
    color: var(--primary-color);
}

/* Sidebar Navigation */
.sidebar {
    position: fixed;
    left: 0;
    top: calc(var(--banner-height, 0px) + var(--topnav-height));
    bottom: 0;
    width: var(--sidebar-width);
    background-color: var(--dark-bg);
    transition: width 0.3s ease;
    z-index: 90;
    overflow: hidden;
    overflow-y: auto;
}

.sidebar.collapsed {
    width: var(--sidebar-collapsed);
}

.sidebar.collapsed .nav-item {
    justify-content: center;
    padding: 12px 0;
    gap: 0;
}

.sidebar.collapsed .nav-item span {
    display: none;
}

.sidebar.collapsed .nav-item i {
    font-size: 1.1em;
}

.sidebar-nav {
    padding: 20px 0;
}

.nav-separator {
    border: none;
    border-top: 1px solid rgba(255, 255, 255, 0.1);
    margin: 8px 20px;
}

.nav-item {
    display: flex;
    align-items: center;
    gap: 15px;
    padding: 12px 20px;
    color: #d1d5db;
    text-decoration: none;
    transition: all 0.2s;
    border-left: 3px solid transparent;
}

.nav-item:hover {
    background-color: rgba(255, 255, 255, 0.05);
    color: white;
}

.nav-item.active {
    background-color: rgba(79, 70, 229, 0.1);
    border-left-color: var(--primary-light);
    color: var(--primary-light);
}

.nav-item i {
    font-size: 18px;
    width: 20px;
    text-align: center;
}

/* Main Content */
.main-content {
    margin-left: var(--sidebar-width);
    margin-top: calc(var(--banner-height, 0px) + var(--topnav-height));
    padding: 30px;
    min-height: calc(100vh - var(--banner-height, 0px) - var(--topnav-height));
    transition: margin-left 0.3s ease;
}

.sidebar.collapsed ~ .main-content {
    margin-left: var(--sidebar-collapsed);
}

/* Cards */
.card {
    background-color: white;
    border-radius: 8px;
    box-shadow: var(--shadow-sm);
    padding: 20px;
    margin-bottom: 20px;
}

.card-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 20px;
    padding-bottom: 15px;
    border-bottom: 1px solid var(--border-color);
}

.card-title {
    font-size: 18px;
    font-weight: 600;
    color: var(--text-primary);
}

/* Stats Grid */
.stats-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
    gap: 20px;
    margin-bottom: 30px;
}

.stat-card {
    background: white;
    border-radius: 8px;
    padding: 20px;
    box-shadow: var(--shadow-sm);
    border-left: 4px solid var(--primary-color);
}

.stat-card.success {
    border-left-color: var(--secondary-color);
}

.stat-card.warning {
    border-left-color: var(--warning-color);
}

.stat-card.danger {
    border-left-color: var(--danger-color);
}

.stat-header {
    display: flex;
    justify-content: space-between;
    align-items: flex-start;
    margin-bottom: 10px;
}

.stat-label {
    font-size: 13px;
    color: var(--text-secondary);
    text-transform: uppercase;
    letter-spacing: 0.5px;
}

.stat-icon {
    font-size: 24px;
    opacity: 0.3;
}

.stat-value {
    font-size: 32px;
    font-weight: 700;
    color: var(--text-primary);
}

.stat-change {
    font-size: 12px;
    margin-top: 5px;
}

.stat-change.positive {
    color: var(--secondary-color);
}

.stat-change.negative {
    color: var(--danger-color);
}

/* Table Styles */
.table-container {
    overflow-x: auto;
}

table {
    width: 100%;
    border-collapse: collapse;
}

thead {
    background-color: var(--light-bg);
}

th {
    text-align: left;
    padding: 12px;
    font-weight: 600;
    color: var(--text-secondary);
    font-size: 13px;
    text-transform: uppercase;
    letter-spacing: 0.5px;
}

td {
    padding: 12px;
    border-bottom: 1px solid var(--border-color);
}

tr:hover {
    background-color: #e8ecf0;
}

/* Error Message */
.error-message {
    color: var(--danger-color);
    background-color: rgba(239, 68, 68, 0.1);
    padding: 10px;
    border-radius: 6px;
    margin-top: 10px;
    display: none;
}

.error-message.show {
    display: block;
}

/* Chart Container */
.chart-container {
    position: relative;
    height: 200px;
    margin: 20px 0;
}

/* Responsive Design */
@media (max-width: 768px) {
    .sidebar {
        width: var(--sidebar-width) !important;
        transform: translateX(-100%);
        transition: transform 0.3s ease;
        z-index: 200;
        box-shadow: none;
    }

    .sidebar.active {
        transform: translateX(0);
        box-shadow: 4px 0 20px rgba(0,0,0,0.4);
    }

    /* Dim overlay behind open sidebar */
    .sidebar-overlay {
        display: none;
        position: fixed;
        inset: 0;
        background: rgba(0,0,0,0.45);
        z-index: 190;
    }
    .sidebar-overlay.active { display: block; }

    .main-content {
        margin-left: 0;
    }

    /* Collapsed sidebar on mobile still hides off-screen until toggled active */
    .sidebar.collapsed {
        width: var(--sidebar-width) !important;
        transform: translateX(-100%);
    }
    .sidebar.collapsed.active {
        transform: translateX(0);
    }

    .stats-grid {
        grid-template-columns: 1fr;
    }

    .nav-actions .user-info {
        display: none;
    }

    .nav-btn-label {
        display: none;
    }
}

/* Loading Spinner */
.spinner {
    border: 3px solid var(--border-color);
    border-top: 3px solid var(--primary-color);
    border-radius: 50%;
    width: 40px;
    height: 40px;
    animation: spin 1s linear infinite;
    margin: 40px auto;
}

@keyframes spin {
    0% { transform: rotate(0deg); }
    100% { transform: rotate(360deg); }
}

/* Badge */
.badge {
    display: inline-block;
    padding: 4px 8px;
    border-radius: 12px;
    font-size: 12px;
    font-weight: 500;
}

.badge-success {
    background-color: rgba(16, 185, 129, 0.1);
    color: var(--secondary-color);
}

.badge-danger {
    background-color: rgba(239, 68, 68, 0.1);
    color: var(--danger-color);
}

.badge-warning {
    background-color: rgba(245, 158, 11, 0.1);
    color: var(--warning-color);
}

/* Action Buttons */
.actions {
    display: flex;
    gap: 8px;
}

.icon-btn {
    background: none;
    border: none;
    cursor: pointer;
    padding: 6px;
    border-radius: 4px;
    color: var(--text-secondary);
    transition: all 0.2s;
}

.icon-btn:hover {
    background-color: var(--light-bg);
    color: var(--primary-color);
}

/* Filter Bar */
.filter-bar {
    display: flex;
    gap: 10px;
    margin-bottom: 20px;
    flex-wrap: wrap;
}

.filter-bar input,
.filter-bar select {
    padding: 8px 12px;
    border: 1px solid var(--border-color);
    border-radius: 6px;
    font-size: 14px;
}

/* Page Header */
.page-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 30px;
}

.page-header h1 {
    font-size: 28px;
    font-weight: 700;
    color: var(--text-primary);
    margin: 0;
}

/* Form Controls */
.form-control {
    width: 100%;
    padding: 10px 14px;
    border: 1px solid var(--border-color);
    border-radius: 6px;
    font-size: 14px;
    transition: all 0.2s;
}

.form-control:focus {
    outline: none;
    border-color: var(--primary-color);
    box-shadow: 0 0 0 3px rgba(79, 70, 229, 0.1);
}

.form-control:disabled {
    background-color: var(--light-bg);
    cursor: not-allowed;
}

/* Sticky page header — keeps New/Edit buttons visible while content scrolls */
.page-header {
    position: sticky;
    top: calc(var(--banner-height, 0px) + var(--topnav-height));
    z-index: 10;
    background: var(--light-bg);
    padding: 12px 0 12px;
    margin-bottom: 20px;
    box-shadow: 0 2px 6px rgba(0,0,0,0.06);
}


/* Modal trap — ensures modals sit above sticky header */
.modal {
    z-index: 1000;
}

/* Reusable 2-column form grid — collapses to 1 column on mobile */
.form-grid-2 {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 20px;
    margin-bottom: 20px;
}
.form-grid-2-sm {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 16px;
    margin-bottom: 16px;
}
.form-grid-3 {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    gap: 14px;
    margin-bottom: 20px;
}
.form-grid-4 {
    display: grid;
    grid-template-columns: repeat(4, 1fr);
    gap: 12px;
    margin-top: 10px;
}

/* Mobile: modals fill the screen width and grids collapse to single column */
@media (max-width: 600px) {
    .modal-content {
        min-width: unset;
        width: calc(100% - 24px);
        max-width: calc(100% - 24px);
        border-radius: 12px;
        max-height: 92vh;
    }

    .form-grid-2,
    .form-grid-2-sm,
    .form-grid-3 {
        grid-template-columns: 1fr;
    }

    .form-grid-4 {
        grid-template-columns: 1fr 1fr;
    }

    .modal-footer {
        flex-wrap: wrap;
        gap: 8px;
    }

    .modal-footer .btn {
        flex: 1;
        min-width: 120px;
        text-align: center;
        justify-content: center;
    }

    /* Reset span cells so they don't overflow single-column grid */
    .form-grid-2 > *,
    .form-grid-2-sm > *,
    .form-grid-3 > * {
        grid-column: auto !important;
    }
}

/* ─── Pagination bar ──────────────────────────────────────────────── */
.pagination-bar {
    display: flex;
    align-items: center;
    gap: 6px;
    padding: 10px 16px;
    border-top: 1px solid var(--border-color);
}
.pagination-bar .pagination-info {
    flex: 1;
    text-align: center;
    font-size: 13px;
    color: var(--text-secondary);
}

/* ─── Markdown-rendered content ───────────────────────────────────── */
.md-rendered p { margin: 0 0 4px; }
.md-rendered p:last-child { margin-bottom: 0; }
.md-rendered ul, .md-rendered ol { margin: 2px 0 4px; padding-left: 18px; }
.md-rendered h1, .md-rendered h2, .md-rendered h3 { font-size: inherit; font-weight: 600; margin: 4px 0 2px; }
.md-rendered code { background: var(--bg-secondary, #f5f5f5); padding: 1px 4px; border-radius: 3px; font-size: 0.9em; }
.md-rendered pre { background: var(--bg-secondary, #f5f5f5); padding: 8px; border-radius: 4px; overflow-x: auto; }
.md-rendered blockquote { border-left: 3px solid var(--border-color); margin: 4px 0; padding-left: 10px; color: var(--text-secondary); }

/* Reports "Download PDF": print only the snapshot region, not the app chrome */
#printRegion { display: none; }

@media print {
    body.printing-report > :not(#printRegion) { display: none !important; }
    body.printing-report #printRegion { display: block !important; }
    #printRegion { padding: 0; color: #000; }
    #printRegion .print-report-header h1 { font-size: 22px; margin: 0 0 4px; }
    #printRegion .print-report-meta { color: #555; font-size: 12px; margin-bottom: 16px; }
    #printRegion button, #printRegion .btn { display: none !important; }
    #printRegion .report-chart-img { max-width: 100%; }
    #printRegion .card { box-shadow: none; border: 1px solid #ddd; page-break-inside: avoid; margin-bottom: 14px; }
    #printRegion table { width: 100%; border-collapse: collapse; font-size: 11px; }
    #printRegion th, #printRegion td { border: 1px solid #ddd; padding: 4px 6px; text-align: left; }
    #printRegion a { color: #000; text-decoration: none; }
}
