:root {
    --primary-color: #0d6efd;
    --secondary-color: #6c757d;
    --bg-color: #f8f9fa;
    --text-color: #212529;
    --card-bg: #ffffff;
    --border-color: #dee2e6;
    --success-color: #198754;
    --danger-color: #dc3545;
    --font-family: 'Inter', sans-serif;
}

* {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
}

body {
    font-family: var(--font-family);
    background-color: var(--bg-color);
    color: var(--text-color);
    line-height: 1.6;
}

/* Utilities */
.container {
    max-width: 1200px;
    margin: 0 auto;
    padding: 20px;
}

.d-none {
    display: none !important;
}

.d-flex {
    display: flex;
}

.flex-column {
    flex-direction: column;
}

.align-center {
    align-items: center;
}

.justify-center {
    justify-content: center;
}

.justify-between {
    justify-content: space-between;
}

.w-full {
    width: 100%;
}

.mt-2 {
    margin-top: 1rem;
}

.mb-2 {
    margin-bottom: 1rem;
}

.mt-4 {
    margin-top: 2rem;
}

/* Buttons */
.btn {
    display: inline-block;
    padding: 10px 20px;
    border: none;
    border-radius: 5px;
    cursor: pointer;
    font-size: 1rem;
    font-weight: 500;
    transition: filter 0.2s;
}

.btn:hover {
    filter: brightness(0.9);
}

.btn-primary {
    background-color: var(--primary-color);
    color: white;
}

.btn-secondary {
    background-color: var(--secondary-color);
    color: white;
}

/* Forms */
.form-group {
    margin-bottom: 15px;
}

.form-label {
    display: block;
    margin-bottom: 5px;
    font-weight: 500;
}

.form-control {
    width: 100%;
    padding: 10px;
    border: 1px solid var(--border-color);
    border-radius: 5px;
    font-size: 1rem;
}

/* Login Page specific */
.login-container {
    height: 100vh;
    display: flex;
    align-items: center;
    justify-content: center;
    background-color: #eef2f6;
}

.login-card {
    background: var(--card-bg);
    padding: 40px;
    border-radius: 10px;
    box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
    width: 100%;
    max-width: 400px;
    text-align: center;
}

.company-logo {
    max-width: 150px;
    margin-bottom: 20px;
}

/* Loading */
.loading-screen {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: var(--bg-color);
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    z-index: 9999;
}

.spinner {
    border: 4px solid #f3f3f3;
    border-top: 4px solid var(--primary-color);
    border-radius: 50%;
    width: 40px;
    height: 40px;
    animation: spin 1s linear infinite;
    margin-bottom: 15px;
}

@keyframes spin {
    0% {
        transform: rotate(0deg);
    }

    100% {
        transform: rotate(360deg);
    }

    /* CSS fixed */
}

/* Dashboard Layout */
.app-layout {
    display: flex;
    height: 100vh;
}

.sidebar {
    width: 250px;
    background: white;
    border-right: 1px solid var(--border-color);
    display: flex;
    flex-direction: column;
    padding: 20px;
}

.main-content {
    flex: 1;
    padding: 20px;
    overflow-y: auto;
}

.nav-link {
    display: block;
    padding: 10px;
    color: var(--text-color);
    text-decoration: none;
    border-radius: 5px;
    margin-bottom: 5px;
}

.nav-link:hover,
.nav-link.active {
    background-color: var(--primary-color);
    color: white;
}

.alert {
    padding: 15px;
    border-radius: 5px;
    margin-bottom: 20px;
}

/* --- Top Header & Notifications --- */
.top-header {
    background: #fff;
    padding: 15px 20px;
    border-bottom: 1px solid var(--border-color);
    display: flex;
    justify-content: flex-end;
    /* Align items to right */
    align-items: center;
    gap: 20px;
    margin-bottom: 20px;
    border-radius: 8px;
    box-shadow: 0 1px 3px rgba(0, 0, 0, 0.05);
}

.notification-wrapper {
    position: relative;
    cursor: pointer;
}

.notification-bell {
    font-size: 1.2rem;
    color: var(--secondary-color);
    position: relative;
    transition: color 0.2s;
}

.notification-bell:hover {
    color: var(--primary-color);
}

.notification-dot {
    position: absolute;
    top: -2px;
    right: -2px;
    width: 8px;
    height: 8px;
    background: var(--danger-color);
    border-radius: 50%;
    display: none;
    /* JS will toggle this */
}

.notification-dropdown {
    position: absolute;
    top: 30px;
    right: 0;
    width: 300px;
    background: white;
    border: 1px solid var(--border-color);
    border-radius: 8px;
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
    z-index: 1000;
    display: none;
    flex-direction: column;
}

.notif-header {
    padding: 10px 15px;
    border-bottom: 1px solid #f0f0f0;
    font-weight: 600;
    font-size: 0.9em;
    color: var(--text-color);
    background: #f8f9fa;
    border-radius: 8px 8px 0 0;
}

.notif-list {
    max-height: 300px;
    overflow-y: auto;
}

.notif-item {
    padding: 12px 15px;
    border-bottom: 1px solid #f0f0f0;
    font-size: 0.85em;
    color: #555;
    transition: background 0.2s;
}

.notif-item:hover {
    background: #f8f9fa;
}

.notif-item.unread {
    background: #e6f2ff;
    /* Light blue tint for unread */
    color: #000;
    font-weight: 500;
}

.notif-empty {
    padding: 20px;
    text-align: center;
    color: #999;
    font-size: 0.9em;
}

.user-profile-snap {
    display: flex;
    align-items: center;
    gap: 10px;
    font-size: 0.9em;
    color: var(--text-color);
}

.alert-danger {
    background-color: #f8d7da;
    color: #721c24;
    border: 1px solid #f5c6cb;
}

/* --- Chat Widget --- */
/* --- Chat Widget --- */
.chat-launcher {
    position: fixed;
    bottom: 30px;
    right: 30px;
    width: 60px;
    height: 60px;
    background: var(--primary-color);
    color: white;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 24px;
    cursor: pointer;
    box-shadow: 0 4px 15px rgba(0, 0, 0, 0.2);
    z-index: 10001;
    transition: transform 0.2s, background 0.2s;
}

.chat-launcher:hover {
    transform: scale(1.1);
    background: #0b5ed7;
}

.chat-window {
    position: fixed;
    bottom: 100px;
    right: 30px;
    width: 350px;
    height: 450px;
    background: white;
    border-radius: 12px;
    box-shadow: 0 5px 30px rgba(0, 0, 0, 0.15);
    z-index: 10001;
    display: flex;
    flex-direction: column;
    overflow: hidden;
    font-family: var(--font-family);
}

.chat-header {
    background: var(--primary-color);
    color: white;
    padding: 15px;
    cursor: pointer;
    display: flex;
    justify-content: space-between;
    align-items: center;
    font-weight: 600;
}

.chat-body {
    flex: 1;
    display: flex;
    flex-direction: column;
    background: #fff;
    overflow: hidden;
    /* Prevent body scroll, children will scroll */
}

.chat-user-list {
    flex: 1;
    overflow-y: auto;
    padding: 5px 0;
}

.chat-user-item {
    padding: 10px 15px;
    border-bottom: 1px solid #f0f0f0;
    cursor: pointer;
    display: flex;
    align-items: center;
    gap: 12px;
    transition: background 0.2s;
}

.chat-user-item:hover {
    background: #f8f9fa;
}

.avatar-sm {
    width: 36px;
    height: 36px;
    background: #e9ecef;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    font-weight: bold;
    color: #555;
    text-transform: uppercase;
}

.user-info .name {
    font-size: 0.95em;
    font-weight: 600;
    color: var(--text-color);
}

.user-info .role {
    font-size: 0.75em;
    color: #777;
    text-transform: uppercase;
}

/* Conversation View */
.chat-conversation {
    flex: 1;
    display: flex;
    flex-direction: column;
    height: 100%;
}

.chat-nav {
    padding: 10px 15px;
    background: #f8f9fa;
    display: flex;
    align-items: center;
    gap: 10px;
    border-bottom: 1px solid #dee2e6;
    font-size: 0.95em;
    font-weight: 600;
}

.chat-nav button {
    border: none;
    background: none;
    cursor: pointer;
    font-size: 1.2em;
    color: #555;
}

.chat-messages {
    flex: 1;
    padding: 15px;
    overflow-y: auto;
    display: flex;
    flex-direction: column;
    gap: 10px;
    background-color: #f5f7fb;
}

.message {
    max-width: 80%;
    padding: 10px 14px;
    border-radius: 15px;
    font-size: 0.9em;
    position: relative;
    word-wrap: break-word;
    box-shadow: 0 1px 2px rgba(0, 0, 0, 0.05);
}

.message.me {
    align-self: flex-end;
    background: var(--primary-color);
    color: white;
    border-bottom-right-radius: 2px;
}

.message.other {
    align-self: flex-start;
    background: white;
    color: #212529;
    border-bottom-left-radius: 2px;
    border: 1px solid #eee;
}

.message .time {
    font-size: 0.7em;
    opacity: 0.7;
    margin-top: 4px;
    text-align: right;
    display: block;
}

.chat-input-area {
    padding: 12px;
    background: white;
    border-top: 1px solid #dee2e6;
    display: flex;
    gap: 10px;
}

.chat-input-area input {
    flex: 1;
    padding: 10px 15px;
    border: 1px solid #dee2e6;
    border-radius: 20px;
    outline: none;
    background: #f8f9fa;
}

.chat-input-area button {
    background: none;
    border: none;
    color: var(--primary-color);
    font-size: 1.4em;
    cursor: pointer;
    padding: 0 5px;
}

/* Responsive / Mobile Styles */
.menu-toggle {
    display: none;
    font-size: 1.5rem;
    cursor: pointer;
    margin-right: 15px;
    color: var(--primary-color);
}

.top-header {
    margin-bottom: 20px;
}

.sidebar-overlay {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: rgba(0, 0, 0, 0.5);
    z-index: 1040;
    display: none;
}

.sidebar-overlay.show {
    display: block;
}

/* TABLET & MOBILE Responsive Styles (up to 1024px for iPad Landscape etc) */
@media (max-width: 1024px) {
    .app-layout {
        flex-direction: column;
    }

    /* Hide Sidebar completely on mobile/tablet */
    .sidebar {
        display: none !important;
    }

    .sidebar-overlay {
        display: none !important;
    }

    .menu-toggle {
        display: none !important;
        /* Hide old toggle */
    }

    /* Full width content */
    .main-content {
        padding: 15px;
        width: 100%;
        margin-left: 0;
        padding-bottom: 80px !important;
        /* Space for bottom nav */
    }

    /* Bottom Navigation Bar */
    .mobile-bottom-nav {
        position: fixed;
        bottom: 0;
        left: 0;
        width: 100%;
        height: 65px;
        background: #ffffff;
        border-top: 1px solid #dee2e6;
        display: flex;
        justify-content: space-around;
        align-items: center;
        z-index: 2000;
        box-shadow: 0 -2px 10px rgba(0, 0, 0, 0.05);
        padding-bottom: env(safe-area-inset-bottom);
    }

    .nav-item-mobile {
        display: flex;
        flex-direction: column;
        align-items: center;
        justify-content: center;
        color: #888;
        font-size: 0.75rem;
        background: none;
        border: none;
        padding: 5px;
        flex: 1;
    }

    .nav-item-mobile i {
        font-size: 1.4rem;
        margin-bottom: 2px;
    }

    .nav-item-mobile.active {
        color: var(--primary-color);
        font-weight: 600;
    }

    /* Ensure tables scroll horizontally */
    table {
        display: block;
        overflow-x: auto;
        white-space: nowrap;
    }

    .card {
        width: 100% !important;
    }

    .d-flex {
        flex-wrap: wrap;
    }

    body {
        overflow-x: hidden;
    }

    /* KPI Responsive Table */
    .kpi-table thead {
        display: none;
    }

    .kpi-table,
    .kpi-table tbody,
    .kpi-table tr,
    .kpi-table td {
        display: block;
        width: 100%;
    }

    .kpi-table tr {
        margin-bottom: 15px;
        background: #fff;
        border: 1px solid #ddd;
        border-radius: 8px;
        box-shadow: 0 1px 3px rgba(0, 0, 0, 0.05);
    }

    .kpi-table td {
        text-align: right;
        padding: 10px 15px;
        position: relative;
        border: none;
        border-bottom: 1px solid #eee;
        display: flex;
        justify-content: space-between;
        align-items: center;
    }

    .kpi-table td:last-child {
        border-bottom: 0;
    }

    .kpi-table td::before {
        content: attr(data-label);
        font-weight: bold;
        color: #555;
        text-transform: uppercase;
        font-size: 0.75rem;
        text-align: left;
    }
}

/* Chat View Styles */
.chat-layout {
    display: flex;
    height: calc(100vh - 140px);
    background: white;
    border-radius: 8px;
    overflow: hidden;
    border: 1px solid #ddd;
}

#chat-view-sidebar {
    width: 300px;
    border-right: 1px solid #eee;
    display: flex;
    flex-direction: column;
    background: #f8f9fa;
}

#chat-view-main {
    flex: 1;
    display: flex;
    flex-direction: column;
    background: white;
    position: relative;
}

.msg-bubble {
    max-width: 70%;
    padding: 8px 12px;
    border-radius: 8px;
    font-size: 0.9rem;
    position: relative;
    box-shadow: 0 1px 1px rgba(0, 0, 0, 0.1);
}

.msg-bubble.me {
    align-self: flex-end;
    background: #dcf8c6;
    border-top-right-radius: 0;
}

.msg-bubble.other {
    align-self: flex-start;
    background: #fff;
    border-top-left-radius: 0;
}

.msg-time {
    font-size: 0.65rem;
    text-align: right;
    margin-top: 2px;
    opacity: 0.7;
    display: none;
    z-index: 9999;
}

/* Hide Mobile Elements on Desktop */
.mobile-bottom-nav,
.mobile-menu-drawer {
    display: none !important;
}

/* TABLET & MOBILE Responsive Styles (up to 1024px for iPad Landscape etc) */
@media (max-width: 1024px) {
    .mobile-bottom-nav {
        display: flex !important;
    }

    .mobile-menu-drawer {
        display: grid !important;
    }

    .chat-layout {
        height: calc(100vh - 150px) !important;
        /* Adjust for bottom nav */
        flex-direction: column;
        border: none;
        border-radius: 0;
    }

    #chat-view-sidebar {
        width: 100% !important;
        height: 100%;
        border-right: none;
    }

    #chat-view-main {
        display: none;
        height: 100%;
        position: absolute;
        top: 0;
        left: 0;
        width: 100%;
        z-index: 10;
    }

    #chat-view-main.active {
        display: flex;
    }

    /* Mobile Menu Drawer */
    .mobile-menu-drawer {
        position: fixed;
        bottom: -100%;
        left: 0;
        width: 100%;
        background: white;
        border-top-left-radius: 20px;
        border-top-right-radius: 20px;
        box-shadow: 0 -4px 15px rgba(0, 0, 0, 0.15);
        transition: bottom 0.3s cubic-bezier(0.175, 0.885, 0.32, 1.275);
        z-index: 1050;
        /* Above bottom nav (1000/2000? Nav is usually high) */
        padding: 20px;
        padding-bottom: 80px;
        /* Space for bottom nav */
        display: grid;
        grid-template-columns: repeat(4, 1fr);
        /* 4 icons per row? or 3? */
        gap: 15px;
        max-height: 80vh;
        overflow-y: auto;
    }

    .mobile-menu-drawer.open {
        bottom: 0;
    }

    .drawer-item {
        background: none;
        border: none;
        display: flex;
        flex-direction: column;
        align-items: center;
        gap: 8px;
        padding: 10px;
        border-radius: 12px;
        color: #333;
    }

    .drawer-item:active {
        background: #f0f0f0;
    }

    .drawer-item i {
        font-size: 1.8rem;
        color: var(--primary-color);
    }

    .drawer-item:active i {
        transform: scale(0.9);
    }

    .drawer-item span {
        font-size: 0.75rem;
        font-weight: 500;
        line-height: 1.2;
    }

    /* Ishikawa / Analysis View Responsive */
    .ishikawa-grid {
        display: grid;
        grid-template-columns: 1fr 1fr;
        gap: 20px;
    }

    .chart-container {
        position: relative;
        height: 300px;
        width: 100%;
    }

    @media (max-width: 768px) {
        .ishikawa-grid {
            grid-template-columns: 1fr !important;
        }

        .chart-container {
            height: 250px;
        }

        .mermaid {
            overflow-x: auto;
        }
    }
}