/* =========================================
   1. RESET & BASE VARIABLES
   ========================================= */
:root {
    --primary: #4f46e5;
    --primary-dark: #4338ca;
    --secondary: #6366f1;
    --success: #10b981;
    --warning: #f59e0b;
    --danger: #ef4444;
    --info: #3b82f6;

    --bg-body: #f3f4f6;
    --bg-white: #ffffff;
    --bg-sidebar: #ffffff;

    --text-dark: #1f2937;
    --text-medium: #4b5563;
    --text-light: #9ca3af;

    --border-color: #e5e7eb;
    --radius: 8px;
    --shadow: 0 1px 3px 0 rgba(0, 0, 0, 0.1), 0 1px 2px 0 rgba(0, 0, 0, 0.06);
    --shadow-lg: 0 10px 15px -3px rgba(0, 0, 0, 0.1);

    --sidebar-width: 260px;
    --header-height: 64px;
}

* { box-sizing: border-box; }

body {
    font-family: 'Inter', -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
    background-color: var(--bg-body);
    color: var(--text-dark);
    margin: 0;
    line-height: 1.5;
    -webkit-font-smoothing: antialiased;
}

a { text-decoration: none; color: inherit; }

/* =========================================
   2. LAYOUT (SIDEBAR & NAVBAR)
   ========================================= */
.app-wrapper {
    display: flex;
    min-height: 100vh;
}

/* --- Sidebar --- */
.sidebar {
    width: var(--sidebar-width);
    background: var(--bg-sidebar);
    border-right: 1px solid var(--border-color);
    position: fixed;
    height: 100vh;
    display: flex;
    flex-direction: column;
    z-index: 50;
    transition: transform 0.3s ease, width 0.3s ease;
}

.sidebar__brand {
    height: var(--header-height);
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 0 24px;
    font-weight: 800;
    font-size: 1.25rem;
    color: var(--primary);
    border-bottom: 1px solid var(--border-color);
    justify-content: space-between; /* Added for mobile close button alignment */
}

.sidebar__nav {
    padding: 24px 16px;
    flex: 1;
    overflow-y: auto;
}

.sidebar__nav ul { list-style: none; padding: 0; margin: 0; }
.sidebar__nav li { margin-bottom: 4px; }

.sidebar__nav .nav-divider {
    font-size: 0.7rem;
    text-transform: uppercase;
    color: var(--text-light);
    font-weight: 700;
    padding: 24px 12px 8px 12px;
    letter-spacing: 0.05em;
}

.sidebar__nav a {
    display: flex;
    align-items: center;
    justify-content: flex-start;
    gap: 12px;
    padding: 12px 16px;
    color: var(--text-medium);
    border-radius: var(--radius);
    transition: all 0.2s ease;
    font-weight: 500;
    font-size: 0.95rem;
}

.sidebar__nav a i {
    width: 24px;
    text-align: center;
    color: var(--text-light);
    transition: color 0.2s;
    font-size: 1rem;
}

.sidebar__nav a:hover { background-color: #f3f4f6; color: var(--text-dark); }
.sidebar__nav a:hover i { color: var(--primary); }

.sidebar__nav a.active {
    background-color: #eef2ff;
    color: var(--primary);
    font-weight: 600;
}
.sidebar__nav a.active i { color: var(--primary); }

.nav-badge {
    margin-left: auto;
    background: var(--danger);
    color: white;
    font-size: 0.7rem;
    font-weight: 700;
    padding: 2px 8px;
    border-radius: 12px;
    min-width: 20px;
    text-align: center;
    line-height: 1.2;
}

.sidebar__footer {
    padding: 16px 24px;
    border-top: 1px solid var(--border-color);
    color: var(--text-light);
    font-size: 0.75rem;
    text-align: center;
}

/* --- Main Content --- */
.main-content {
    margin-left: var(--sidebar-width);
    flex: 1;
    width: calc(100% - var(--sidebar-width));
    display: flex;
    flex-direction: column;
    transition: margin-left 0.3s ease, width 0.3s ease;
}

/* --- Navbar --- */
.navbar {
    height: var(--header-height);
    background: var(--bg-white);
    border-bottom: 1px solid var(--border-color);
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 0 32px;
    position: sticky;
    top: 0;
    z-index: 40;
}

.navbar__left {
    display: flex;
    align-items: center;
}

/* Mobile Toggle Button (Hidden on Desktop) */
#sidebar-toggle {
    display: none;
    background: none;
    border: none;
    font-size: 1.25rem;
    color: var(--text-dark);
    cursor: pointer;
    margin-right: 16px;
}

#sidebar-close {
    display: none;
    cursor: pointer;
    color: var(--text-light);
}

.navbar__title {
    font-size: 1.1rem;
    font-weight: 600;
    color: var(--text-dark);
    margin: 0;
}

.navbar__right {
    display: flex;
    align-items: center;
    gap: 24px;
}

.nav-icon-btn {
    position: relative;
    width: 40px; height: 40px;
    display: flex; align-items: center; justify-content: center;
    color: var(--text-medium);
    border-radius: 50%;
    transition: all 0.2s;
    font-size: 1.2rem;
}

.nav-icon-btn:hover { background-color: #f3f4f6; color: var(--primary); }

.nav-badge-icon {
    position: absolute;
    top: 2px; right: 2px;
    background: var(--danger); color: white;
    font-size: 0.7rem; font-weight: 700;
    width: 18px; height: 18px; border-radius: 50%;
    display: flex; align-items: center; justify-content: center;
    border: 2px solid white; box-sizing: content-box;
}

.navbar__profile {
    display: flex; align-items: center; gap: 12px;
    border-left: 1px solid var(--border-color);
    padding-left: 16px;
}

.navbar__profile .user-info {
    text-align: right; display: flex; flex-direction: column; align-items: flex-end;
}

.navbar__profile .user-name { font-size: 0.9rem; font-weight: 600; color: var(--text-dark); }

.navbar__profile .avatar-circle {
    width: 40px; height: 40px;
    background: var(--primary); color: white;
    border-radius: 50%;
    display: flex; align-items: center; justify-content: center;
    font-weight: 700; font-size: 1.1rem;
}

/* =========================================
   3. DASHBOARD GRIDS & CARDS
   ========================================= */
.page-content { padding: 32px; max-width: 1400px; margin: 0 auto; width: 100%; }
.app-container { max-width: 1400px; margin: 0 auto; }

.page-header { display: flex; justify-content: space-between; align-items: flex-start; margin-bottom: 32px; }
.page-header__title { margin: 0 0 12px 0; font-size: 1.5rem; color: var(--text-dark); }
.page-header__title span { display: block; font-size: 0.875rem; font-weight: 400; color: var(--text-light); margin-bottom: 4px; }

.status-bar { display: flex; align-items: center; gap: 16px; font-size: 0.9rem; }
.status-warning { color: var(--warning); font-weight: 500; display: flex; align-items: center; gap: 6px; }

.content-grid { display: grid; grid-template-columns: 1fr 380px; gap: 24px; }

.card {
    background: var(--bg-white); border-radius: var(--radius);
    border: 1px solid var(--border-color); box-shadow: var(--shadow);
    overflow: hidden; margin-bottom: 24px;
}
.card--padded { padding: 24px; }
.card__header { padding: 16px 20px; border-bottom: 1px solid var(--border-color); background: #f9fafb; }
.card__header h3 { margin: 0; font-size: 1rem; font-weight: 600; color: var(--text-dark); display: flex; align-items: center; gap: 8px; }
.card__body { padding: 20px; }

/* KPI Grid */
.kpi-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(220px, 1fr)); gap: 20px; margin-bottom: 24px; }
.kpi-card { background: #fff; border-radius: var(--radius); padding: 24px; display: flex; align-items: center; gap: 20px; box-shadow: var(--shadow); border: 1px solid var(--border-color); transition: transform 0.2s; }
.kpi-card:hover { transform: translateY(-2px); box-shadow: var(--shadow-lg); }
.kpi-icon { width: 50px; height: 50px; border-radius: 12px; display: flex; align-items: center; justify-content: center; font-size: 1.5rem; flex-shrink: 0; }
.kpi-info h3 { margin: 0; font-size: 0.85rem; color: var(--text-light); text-transform: uppercase; letter-spacing: 0.5px; font-weight: 600; }
.kpi-info .number { font-size: 1.75rem; font-weight: 800; color: var(--text-dark); line-height: 1.2; margin-top: 4px; }

.kpi-card.blue .kpi-icon { background: #eff6ff; color: #3b82f6; }
.kpi-card.orange .kpi-icon { background: #fff7ed; color: #f97316; }
.kpi-card.green .kpi-icon { background: #f0fdf4; color: #22c55e; }
.kpi-card.red .kpi-icon { background: #fef2f2; color: #ef4444; }

/* =========================================
   4. TABLES & COMPONENTS
   ========================================= */
.table { width: 100%; border-collapse: collapse; font-size: 0.9rem; }
.table th { text-align: left; padding: 12px 16px; background: #f9fafb; color: var(--text-medium); font-weight: 600; border-bottom: 1px solid var(--border-color); }
.table td { padding: 12px 16px; border-bottom: 1px solid var(--border-color); vertical-align: middle; }
.table tr:last-child td { border-bottom: none; }

.action-buttons-wrapper { display: flex; justify-content: flex-end; align-items: center; gap: 8px; }
.btn-icon-only { width: 32px; height: 32px; display: flex; align-items: center; justify-content: center; border-radius: 6px; color: var(--text-light); border: 1px solid var(--border-color); background: white; transition: all 0.2s; }
.btn-icon-only:hover { color: var(--primary); border-color: var(--primary); background: #eef2ff; }

.progress-mini { display: flex; gap: 4px; height: 6px; width: 80px; border-radius: 3px; overflow: hidden; }
.progress-mini div { flex: 1; border-radius: 2px; }

.user-avatar-sm { width: 24px; height: 24px; background: #e2e8f0; border-radius: 50%; display: flex; align-items: center; justify-content: center; font-size: 10px; color: var(--text-medium); }

/* Buttons & Badges */
.badge { display: inline-flex; align-items: center; padding: 4px 10px; border-radius: 9999px; font-size: 0.75rem; font-weight: 600; text-transform: uppercase; }
.badge--lg { padding: 6px 12px; font-size: 0.85rem; }
.badge--sm { padding: 2px 8px; font-size: 0.7rem; }
.badge--neutral { background: #f3f4f6; color: var(--text-medium); border: 1px solid #d1d5db; }

.status-gray { background: #e5e7eb; color: #374151; }
.status-blue { background: #dbeafe; color: #1e40af; }
.status-orange { background: #ffedd5; color: #9a3412; }
.status-teal { background: #ccfbf1; color: #115e59; }
.status-green { background: #d1fae5; color: #065f46; }

.btn { display: inline-flex; align-items: center; justify-content: center; gap: 8px; padding: 10px 16px; border-radius: 6px; font-weight: 500; font-size: 0.9rem; border: none; cursor: pointer; transition: all 0.2s; text-decoration: none; line-height: 1.2; }
.btn--block { width: 100%; }
.btn--sm { padding: 6px 12px; font-size: 0.8rem; }
.btn--lg { padding: 14px; font-size: 1rem; }
.btn--primary { background: var(--primary); color: white; }
.btn--primary:hover { background: var(--primary-dark); }
.btn--secondary { background: white; border: 1px solid var(--border-color); color: var(--text-dark); }
.btn--secondary:hover { background: #f9fafb; border-color: #d1d5db; }
.btn--success { background: var(--success); color: white; }
.btn--success:hover { background: #059669; }
.btn--danger { background: var(--danger); color: white; }
.btn--danger:hover { background: #dc2626; }
.btn--warning { background: var(--warning); color: white; }
.btn--warning:hover { background: #d97706; }
.btn-group { display: flex; gap: 10px; width: 100%; }

/* Form Controls */
.form-control { width: 100%; padding: 10px 12px; border: 1px solid var(--border-color); border-radius: 6px; font-family: inherit; font-size: 0.9rem; margin-bottom: 16px; background: #fff; }
.form-control:focus { outline: none; border-color: var(--primary); box-shadow: 0 0 0 3px rgba(79, 70, 229, 0.1); }
.file-drop-area { border: 2px dashed var(--border-color); border-radius: 6px; padding: 24px; text-align: center; position: relative; margin-bottom: 12px; transition: 0.2s; background: #f9fafb; }
.file-drop-area:hover { border-color: var(--primary); background: #eef2ff; }
.file-drop-area input { position: absolute; left: 0; top: 0; width: 100%; height: 100%; opacity: 0; cursor: pointer; }

/* Checkboxes & Tabs */
.checkbox-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(110px, 1fr)); gap: 10px; }
.checkbox-card { position: relative; }
.checkbox-card input[type="checkbox"] { position: absolute; opacity: 0; cursor: pointer; height: 0; width: 0; }
.checkbox-card label { display: block; padding: 8px 12px; background: #fff; border: 1px solid var(--border-color); border-radius: 6px; text-align: center; font-size: 0.85rem; font-weight: 500; color: var(--text-medium); cursor: pointer; transition: all 0.2s; }
.checkbox-card input[type="checkbox"]:checked + label { background: #eff6ff; border-color: var(--primary); color: var(--primary); font-weight: 600; box-shadow: 0 1px 2px rgba(79, 70, 229, 0.1); }

.panel-tabs { display: flex; gap: 4px; border-bottom: 2px solid var(--border-color); margin-bottom: 20px; overflow-x: auto; }
.panel-tab { padding: 10px 24px; background: #f3f4f6; border: 1px solid var(--border-color); border-bottom: none; border-radius: 8px 8px 0 0; cursor: pointer; font-weight: 600; color: var(--text-light); transition: all 0.2s; min-width: 60px; text-align: center; }
.panel-tab.active { background: var(--bg-white); color: var(--primary); border-color: var(--border-color); border-bottom: 2px solid var(--bg-white); margin-bottom: -2px; }
.panel-content { display: none; animation: fadeIn 0.3s ease; }
.panel-content.active { display: block; }

.upload-zone { border: 2px dashed var(--border-color); border-radius: var(--radius); padding: 30px; text-align: center; background: #f9fafb; position: relative; transition: all 0.2s; cursor: pointer; }
.upload-zone:hover { border-color: var(--primary); background: #eef2ff; }
.upload-zone input[type="file"] { position: absolute; top: 0; left: 0; width: 100%; height: 100%; opacity: 0; cursor: pointer; }
.upload-icon { font-size: 2rem; color: var(--text-light); margin-bottom: 8px; }

/* Specific Order Components */
.pro-desc__general { background: #f8fafc; border: 1px solid #e2e8f0; border-radius: var(--radius); padding: 16px; margin-bottom: 24px; }
.pro-desc__label { font-weight: 600; color: var(--text-medium); margin-bottom: 8px; display: flex; gap: 8px; align-items: center; }
.pro-specs { border: 1px solid var(--border-color); border-radius: var(--radius); overflow: hidden; }
.pro-specs__header { background: #f1f5f9; padding: 12px 16px; font-weight: 700; color: var(--text-medium); font-size: 0.85rem; text-transform: uppercase; letter-spacing: 0.05em; border-bottom: 1px solid var(--border-color); }
.pro-specs__body { padding: 20px; background: var(--bg-white); }
.spec-card { background: var(--bg-white); border: 1px solid var(--border-color); border-left: 4px solid var(--primary); border-radius: 4px; padding: 16px; margin-bottom: 16px; }
.spec-card__title { color: var(--primary); margin: 0 0 12px 0; font-size: 0.9rem; font-weight: 700; }
.spec-row { display: flex; margin-bottom: 6px; font-size: 0.875rem; }
.spec-row__label { width: 140px; color: var(--text-light); font-weight: 500; flex-shrink: 0; }
.spec-row__value { color: var(--text-dark); font-weight: 500; }

.state-box { text-align: center; padding: 24px; border-radius: var(--radius); margin-bottom: 20px; }
.state-box__icon { font-size: 2rem; margin-bottom: 12px; }
.state-box h3, .state-box h4 { margin: 0 0 8px 0; }
.state-box p { margin: 0 0 16px 0; font-size: 0.9rem; opacity: 0.8; }
.state-box--success { background: #ecfdf5; color: #065f46; border: 1px solid #a7f3d0; }
.state-box--warning { background: #fffbeb; color: #92400e; border: 1px solid #fde68a; }

/* Notifications List */
.notif-list { list-style: none; padding: 0; margin: 0; }
.notif-item { border-bottom: 1px solid var(--border-color); transition: background-color 0.2s; border-left: 4px solid transparent; }
.notif-item:hover { background-color: #f9fafb; }
.notif-item--unread { background-color: #f0f9ff; border-left-color: var(--primary); }
.notif-item--unread:hover { background-color: #e0f2fe; }
.notif-link { display: flex; align-items: center; padding: 20px; text-decoration: none; color: inherit; gap: 16px; }
.notif-icon { width: 40px; height: 40px; border-radius: 50%; background-color: #fff; border: 1px solid var(--border-color); display: flex; align-items: center; justify-content: center; color: var(--text-light); flex-shrink: 0; }
.notif-item--unread .notif-icon { background-color: var(--primary); color: white; border-color: var(--primary); }
.notif-content { flex: 1; }
.notif-message { font-size: 0.95rem; color: var(--text-dark); margin-bottom: 4px; font-weight: 500; }
.notif-item--unread .notif-message { font-weight: 700; color: #1e40af; }
.notif-date { font-size: 0.8rem; color: var(--text-light); display: flex; align-items: center; gap: 6px; }

/* Modals & Utils */
.alert-banner { background: linear-gradient(90deg, #f59e0b 0%, #d97706 100%); color: white; padding: 12px 32px; display: flex; justify-content: space-between; align-items: center; box-shadow: 0 4px 6px -1px rgba(245, 158, 11, 0.2); position: relative; z-index: 30; }
.modal { display: none; position: fixed; top: 0; left: 0; width: 100%; height: 100%; background: rgba(0,0,0,0.5); z-index: 1000; align-items: center; justify-content: center; }
.modal.active { display: flex; animation: fadeIn 0.2s; }
.modal__content { background: white; width: 100%; max-width: 450px; padding: 32px; border-radius: var(--radius); box-shadow: var(--shadow-lg); }
.modal__header { display: flex; justify-content: space-between; align-items: center; margin-bottom: 24px; }
.modal__close { font-size: 1.5rem; cursor: pointer; color: var(--text-light); }
.text-muted { color: var(--text-light) !important; }
.text-success { color: var(--success) !important; }
.text-danger { color: var(--danger) !important; }
.text-primary { color: var(--primary) !important; }
.auth-layout { min-height: 100vh; display: flex; justify-content: center; align-items: center; background: #f3f4f6; background-image: radial-gradient(var(--border-color) 1px, transparent 1px); background-size: 20px 20px; }

@keyframes fadeIn { from { opacity: 0; } to { opacity: 1; } }

/* ==========================================================================
   5. RESPONSIVE ENGINE (Handles Mobile & Tablet)
   ========================================================================== */

/* --- Tablet & Small Laptops (Max 1024px) --- */
@media (max-width: 1024px) {
    :root { --sidebar-width: 0px; }
    
    #sidebar-toggle { display: inline-flex; }
    #sidebar-close { display: block; }

    .sidebar {
        transform: translateX(-100%);
        z-index: 999;
        box-shadow: 10px 0 30px rgba(0,0,0,0.1);
        width: 260px; /* Keep fixed width when off-screen */
    }

    .sidebar.active {
        transform: translateX(0);
    }

    .main-content { margin-left: 0; width: 100%; }
    .content-grid { grid-template-columns: 1fr; }
}

/* --- Mobile Devices (Max 768px) --- */
@media (max-width: 768px) {
    /* 1. Absolute Screen Lock */
    html, body {
        overflow-x: hidden;
        width: 100%;
        max-width: 100vw;
        margin: 0;
        padding: 0;
    }

    .app-wrapper {
        width: 100%;
        max-width: 100vw;
        overflow-x: hidden;
    }

    /* 2. Force main content and page content to stay strictly inside */
    .main-content { 
        display: block !important; 
        width: 100% !important; 
        max-width: 100vw !important;
        margin-left: 0 !important;
    }

    .page-content { 
        padding: 12px; /* Slightly reduced padding to give cards more room */
        width: 100%;
        max-width: 100vw;
        box-sizing: border-box; /* Crucial: stops padding from adding to width */
        overflow-x: hidden;
    }

    .content-grid {
        display: flex;
        flex-direction: column;
        width: 100%;
        max-width: 100%;
        box-sizing: border-box;
    }

.card {
        width: 100%;
        max-width: 100%;
        margin-left: 0;
        margin-right: 0;
        box-sizing: border-box;
        min-width: 0 !important; /* Forces the card to stay inside the phone screen */
    }

    /* 3. Chat Form / Submit Button Fixes */
    /* Assuming your chat input area is a flex row */
    .chat-input-wrapper, 
    form[action*="chat"],
    .message-input-area { 
        display: flex !important;
        width: 100% !important;
        box-sizing: border-box !important;
        gap: 8px;
        align-items: center;
    }

    /* Force the text input to shrink and not push buttons off screen */
    .chat-input-wrapper input[type="text"],
    .chat-input-wrapper textarea,
    input#chat-message {
        flex: 1 !important;
        min-width: 0 !important; /* THE MAGIC FIX FOR FLEX INPUTS */
        width: 100%;
    }

    /* Protect the buttons from getting squished */
    #chatSubmitBtn, 
    .chat-submit,
    .btn-icon-only {
        flex-shrink: 0 !important;
    }

    /* 4. Navbar Protections (Fixing Image 1) */
    .navbar { padding: 0 12px; gap: 10px; }
    .navbar__left { flex: 1; min-width: 0; }
    
    /* Forces the title to stay on one line and add "..." if it's too long */
    .navbar__title { 
        font-size: 0.95rem; 
        white-space: nowrap !important; 
        overflow: hidden !important; 
        text-overflow: ellipsis !important; 
        display: block;
    }
    
    .navbar__right { gap: 12px; flex-shrink: 0; }
    .navbar__profile { padding-left: 12px; }
    .navbar__profile .user-info { display: none; }
    
    /* Miscellaneous Mobile Fixes */
    .page-header { flex-direction: column; align-items: flex-start; gap: 16px; }
    .kpi-grid { grid-template-columns: 1fr !important; }
    .card__body { overflow-x: auto; -webkit-overflow-scrolling: touch; width: 100%; box-sizing: border-box; }
/* Turns the table into a self-contained scrolling block */
    .table { 
        display: block !important; 
        width: 100% !important; 
        overflow-x: auto !important; 
        -webkit-overflow-scrolling: touch; 
        border-bottom: 1px solid var(--border-color);
    }
    
    /* Prevents the text inside the table from squishing into unreadable, tall columns */
    .table th, .table td {
        white-space: nowrap; 
    }    .spec-row { flex-direction: column; margin-bottom: 12px; }
    .spec-row__label { width: 100% !important; font-size: 0.75rem; margin-bottom: 2px; }
}

/* --- Small Phones (Max 480px) --- */
@media (max-width: 480px) {
    .checkbox-grid { grid-template-columns: repeat(auto-fill, minmax(130px, 1fr)); }
    .panel-tab { padding: 10px 12px; font-size: 0.8rem; }
    .alert-banner { flex-direction: column; text-align: center; gap: 12px; }
}