* {
    box-sizing: border-box;
}

body {
    margin: 0;
    font-family: 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
    background: var(--bg);
    color: var(--text-primary);
    overflow-x: hidden;
    transition: background 0.3s, color 0.3s;
}

:root {
    /* Dark Theme (Default) */
    --primary: #6366f1;
    --primary-hover: #4f46e5;
    --bg: #0f172a;
    --bg-sidebar: #020617;
    --bg-card: rgba(30, 41, 59, 0.7);
    --bg-surface: rgba(255, 255, 255, 0.02);
    --bg-surface-light: rgba(255, 255, 255, 0.05);
    --bg-surface-darker: rgba(0, 0, 0, 0.2);
    --border: rgba(255, 255, 255, 0.1);
    --accent: #10b981;
    --danger: #ef4444;
    --text-primary: #f8fafc;
    --text-muted: #94a3b8;
    --input-bg: rgba(15, 23, 42, 0.5);
    --shadow: 0 10px 25px rgba(0,0,0,0.5);
    --header-height: 70px;
    --sidebar-width: 280px;
}

body.light-mode {
    --bg: #f1f5f9;
    --bg-sidebar: #ffffff;
    --bg-card: #ffffff;
    --bg-surface: #ffffff;
    --bg-surface-light: #f8fafc;
    --bg-surface-darker: rgba(0, 0, 0, 0.03);
    --border: rgba(0, 0, 0, 0.08);
    --text-primary: #0f172a;
    --text-muted: #475569;
    --input-bg: #ffffff;
    --shadow: 0 10px 25px rgba(0,0,0,0.1);
}

/* Glassmorphism utility */
.glass {
    background: var(--bg-card);
    backdrop-filter: blur(12px);
    border: 1px solid var(--border);
    border-radius: 16px;
}

.layout {
    display: grid;
    grid-template-columns: 280px 1fr;
    height: 100vh;
}

/* Sidebar */
aside {
    background: var(--bg-sidebar);
    padding: 2rem 1.5rem;
    display: flex;
    flex-direction: column;
    gap: 2rem;
    border-right: 1px solid var(--border);
    transition: background 0.3s;
    height: 100vh;
    position: sticky;
    top: 0;
}

.logo {
    font-size: 1.5rem;
    font-weight: 800;
    background: linear-gradient(to right, #818cf8, #c084fc);
    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;
    letter-spacing: -0.025em;
}

nav {
    flex: 1;
    display: flex;
    flex-direction: column;
}

nav ul {
    list-style: none;
    padding: 0;
    margin: 0;
    display: flex;
    flex-direction: column;
    gap: 0.5rem;
    height: 100%;
}

.logout-link {
    margin-top: auto !important;
}

nav li a {
    display: flex;
    align-items: center;
    gap: 0.75rem;
    padding: 0.75rem 1rem;
    color: var(--text-muted);
    text-decoration: none;
    border-radius: 12px;
    transition: all 0.2s;
    font-weight: 500;
}

nav li a.active {
    background: rgba(99, 102, 241, 0.1);
    color: var(--primary);
    box-shadow: inset 0 0 0 1px rgba(99, 102, 241, 0.2);
}

nav li a:hover:not(.active) {
    background: var(--bg-surface-light);
    color: var(--text-primary);
}

/* Main Content */
main {
    padding: 2rem;
    overflow-y: auto;
    background: radial-gradient(circle at top right, rgba(99, 102, 241, 0.03), transparent 40%),
                radial-gradient(circle at bottom left, rgba(192, 132, 252, 0.03), transparent 40%);
}

header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 2rem;
}

.user-profile {
    display: flex;
    align-items: center;
    gap: 1rem;
    padding: 0.5rem 1rem;
    border-radius: 99px;
}

.avatar {
    width: 32px;
    height: 32px;
    background: linear-gradient(135deg, var(--primary), #c084fc);
    border-radius: 50%;
}

.header-left, .header-right {
    display: flex;
    align-items: center;
    gap: 1.25rem;
}

.theme-btn {
    width: 44px;
    height: 44px;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    border: 1px solid var(--border);
    background: var(--bg-surface-light);
}

.wp-card {
    border: 1px solid var(--border);
    border-radius: 12px;
    padding: 1.25rem;
    background: var(--bg-surface);
    display: flex;
    justify-content: space-between;
    align-items: center;
    transition: transform 0.2s, box-shadow 0.2s;
}

.wp-card:hover {
    transform: translateY(-2px);
    box-shadow: var(--shadow);
    border-color: var(--primary);
}

/* Dashboard Grid */
.dashboard-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));
    gap: 1.5rem;
}

.stat-card {
    padding: 1.5rem;
    display: flex;
    flex-direction: column;
    gap: 1rem;
}

.stat-value {
    font-size: 2.5rem;
    font-weight: 700;
}

.stat-label {
    color: var(--text-muted);
    font-size: 0.875rem;
}

/* Tables */
.data-table {
    width: 100%;
    border-collapse: collapse;
    margin-top: 1rem;
}

.data-table th {
    text-align: left;
    padding: 1rem;
    color: var(--text-muted);
    font-size: 0.75rem;
    text-transform: uppercase;
    letter-spacing: 0.05em;
    border-bottom: 1px solid var(--border);
}

.data-table td {
    padding: 1rem;
    border-bottom: 1px solid var(--border);
}

.badge {
    padding: 0.25rem 0.75rem;
    border-radius: 99px;
    font-size: 0.75rem;
    font-weight: 600;
}

.badge-work { background: rgba(245, 158, 11, 0.15); color: #f59e0b; }
.badge-done { background: rgba(16, 185, 129, 0.15); color: #10b981; }

/* Buttons */
.btn-primary {
    background: var(--primary);
    color: white;
    border: none;
    padding: 0.75rem 1.5rem;
    border-radius: 12px;
    font-weight: 600;
    cursor: pointer;
    transition: all 0.2s;
}

.btn-primary:hover {
    background: var(--primary-hover);
    transform: translateY(-1px);
    box-shadow: 0 4px 12px rgba(99, 102, 241, 0.3);
}

/* Forms */
.form-group {
    margin-bottom: 1.5rem;
    position: relative;
}

.form-label {
    display: block;
    margin-bottom: 0.5rem;
    color: var(--text-muted);
    font-size: 0.875rem;
}

input, select, textarea {
    width: 100%;
    background: var(--input-bg);
    border: 1px solid var(--border);
    border-radius: 12px;
    padding: 0.75rem 1rem;
    color: var(--text-primary);
    outline: none;
    transition: border-color 0.2s, background 0.2s, color 0.2s;
}

/* Accordions for Factors */
.accordion-item {
    border: 1px solid var(--border);
    border-radius: 8px;
    margin-bottom: 1rem;
    overflow: hidden;
    background: var(--bg-surface);
}
.accordion-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 1rem;
    cursor: pointer;
    background: var(--bg-surface-light);
    transition: background 0.2s;
}
.accordion-header:hover {
    background: rgba(255,255,255,0.08);
}
.accordion-header h3 {
    margin: 0;
    font-size: 1.1rem;
    display: flex;
    align-items: center;
    gap: 10px;
}
.accordion-content {
    padding: 1rem;
    display: none;
    border-top: 1px solid var(--border);
}
.accordion-content.active {
    display: block;
}
.factor-toggle {
    width: 18px;
    height: 18px;
    cursor: pointer;
}
.chevron {
    transition: transform 0.3s ease;
}
.chevron.open {
    transform: rotate(180deg);
}

.source-row {
    display: grid;
    grid-template-columns: 2fr 1fr 2fr 1fr auto;
    gap: 10px;
    align-items: center;
    margin-bottom: 10px;
}

input:focus {
    border-color: var(--primary);
}

/* Tabs Animation */
.tab-content {
    display: none;
    animation: fadeIn 0.3s ease-out;
}

.tab-content.active {
    display: block;
}

@keyframes fadeIn {
    from { opacity: 0; transform: translateY(10px); }
    to { opacity: 1; transform: translateY(0); }
}

/* Autocomplete */
.autocomplete-items {
    position: absolute;
    border: 1px solid var(--border);
    border-bottom: none;
    border-top: none;
    z-index: 1000;
    top: 100%;
    left: 0;
    right: 0;
    background: var(--bg-sidebar);
    border-radius: 0 0 12px 12px;
    max-height: 200px;
    overflow-y: auto;
    box-shadow: var(--shadow);
}

.autocomplete-items div {
    padding: 10px;
    cursor: pointer;
    border-bottom: 1px solid var(--border);
}

.autocomplete-items div:hover {
    background-color: var(--primary);
    color: white;
}

.autocomplete-active {
    background-color: var(--primary) !important;
    color: white;
}
/* Multi-select Dropdown */
.multi-select-container {
    position: relative;
    width: 100%;
}

.multi-select-trigger {
    background: var(--input-bg);
    border: 1px solid var(--border);
    border-radius: 12px;
    padding: 0.75rem 1rem;
    display: flex;
    justify-content: space-between;
    align-items: center;
    cursor: pointer;
    min-height: 48px;
    transition: all 0.2s;
}

.multi-select-trigger:hover {
    border-color: var(--primary);
    background: var(--bg-surface-light);
}

.multi-select-trigger.active {
    border-color: var(--primary);
    box-shadow: 0 0 0 2px rgba(99, 102, 241, 0.2);
}

.multi-select-values {
    display: flex;
    flex-wrap: wrap;
    gap: 4px;
    max-width: 85%;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.multi-select-placeholder {
    color: var(--text-muted);
}

.multi-select-dropdown {
    position: absolute;
    top: calc(100% + 4px);
    left: 0;
    right: 0;
    background: var(--bg-sidebar);
    border: 1px solid var(--border);
    border-radius: 12px;
    z-index: 2000;
    max-height: 300px;
    overflow-y: auto;
    box-shadow: var(--shadow);
    display: none;
    padding: 8px;
    animation: fadeInScale 0.2s ease-out;
}

.multi-select-dropdown.open {
    display: block;
}

.multi-select-search {
    margin-bottom: 8px;
}

.multi-select-search input {
    padding: 6px 10px;
    font-size: 0.85rem;
    background: var(--bg-surface);
}

.multi-select-option {
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 8px 12px;
    border-radius: 8px;
    cursor: pointer;
    transition: background 0.2s;
}

.multi-select-option:hover {
    background: rgba(255,255,255,0.05);
}

.multi-select-option input[type="checkbox"] {
    width: 16px;
    height: 16px;
    accent-color: var(--primary);
    margin: 0;
}

.multi-select-option-text {
    font-size: 0.875rem;
}

.multi-select-option-sub {
    font-size: 0.75rem;
    color: var(--text-muted);
    display: block;
}

.selected-badge {
    background: var(--primary);
    color: white;
    padding: 2px 8px;
    border-radius: 4px;
    font-size: 0.75rem;
    font-weight: 500;
}

@keyframes fadeInScale {
    from { opacity: 0; transform: scale(0.98) translateY(-10px); }
    to { opacity: 1; transform: scale(1) translateY(0); }
}

/* Tablet Optimization */
.touch-btn {
    min-height: 48px;
    padding: 12px 24px;
    display: flex;
    align-items: center;
    justify-content: center;
}

input, select, button, .multi-select-trigger, .accordion-header {
    touch-action: manipulation;
}

.stat-card {
    padding: 1.5rem !important;
}

/* Table Responsiveness */
.table-container {
    width: 100%;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    margin-bottom: 1rem;
    border-radius: 8px;
}

.factor-scroll-container {
    width: 100%;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    margin-bottom: 1rem;
    padding-bottom: 0.5rem;
}

/* Tablet & Mobile Optimization */
@media (max-width: 1024px) {
    .layout {
        grid-template-columns: 1fr;
        height: auto;
        min-height: 100vh;
    }

    aside {
        width: 100%;
        max-width: 100%;
        height: auto;
        border-right: none;
        border-bottom: 1px solid var(--border);
        padding: 1rem;
        gap: 1rem;
        position: sticky;
        top: 0;
        z-index: 1000;
        flex-direction: row;
        align-items: center;
        justify-content: space-between;
    }

    aside .logo {
        font-size: 1.25rem;
    }

    aside nav {
        margin-top: 0;
        flex: initial;
    }

    aside nav ul {
        flex-direction: row;
        gap: 0.25rem;
        overflow-x: auto;
        padding-bottom: 2px;
        scrollbar-width: none;
        height: auto;
    }
    
    .logout-link {
        margin-top: 0 !important;
    }

    aside nav ul::-webkit-scrollbar {
        display: none;
    }

    aside nav li a {
        padding: 0.5rem 0.75rem;
        font-size: 0.85rem;
        white-space: nowrap;
    }

    aside div[style*="margin-top: auto"] {
        margin-top: 0 !important;
        display: none; /* Hide logout in main nav on mobile, maybe move to header or footer */
    }

    main {
        padding: 1rem;
    }

    header {
        flex-direction: column;
        align-items: flex-start;
        gap: 1rem;
    }

    .dashboard-grid {
        grid-template-columns: repeat(auto-fit, minmax(280px, 1fr));
    }
}

@media (max-width: 768px) {
    .stat-value {
        font-size: 2rem;
    }

    .source-row {
        grid-template-columns: 1fr 1fr;
        gap: 0.75rem;
        padding: 1rem;
        background: var(--bg-surface-light);
        border-radius: 12px;
        border: 1px solid var(--border);
    }

    .source-row > div:first-child {
        grid-column: span 2;
    }

    .source-row button {
        grid-column: span 2;
        width: 100%;
    }

    /* Step 1 Grid */
    div[style*="grid-template-columns: 1fr 1fr"] {
        grid-template-columns: 1fr !important;
    }

    /* Step 2 workplace info grid */
    div[style*="grid-template-columns: minmax(0, 2fr) minmax(0, 2fr) minmax(120px, 1fr)"] {
        grid-template-columns: 1fr !important;
    }

    /* Illumination grid */
    div[style*="grid-template-columns: 1.5fr 1.5fr 1fr"] {
        grid-template-columns: 1fr !important;
    }
    
    div[style*="grid-template-columns: 1fr 1fr 1fr 1fr"] {
        grid-template-columns: 1fr 1fr !important;
    }

    /* Header responsiveness */
    .header-left, .header-right {
        width: 100%;
        justify-content: space-between;
    }
    
    .header-left {
        flex-direction: column;
        align-items: stretch;
    }

    .accordion-header h3 {
        font-size: 0.95rem;
    }

    .step-actions {
        flex-direction: column;
        gap: 1rem;
        align-items: stretch;
    }

    .step-actions button, .step-actions div {
        width: 100%;
    }

    .header-left h1 {
        font-size: 1.5rem;
    }

    /* Multi-select triggers on mobile */
    .multi-select-trigger {
        font-size: 0.9rem;
    }

    .btn-primary {
        min-height: 48px;
        padding: 0.75rem 1.25rem;
    }

    .wp-card {
        flex-direction: column;
        align-items: stretch;
        gap: 1rem;
    }

    .wp-card > div:last-child {
        display: flex;
        justify-content: flex-end;
        border-top: 1px solid var(--border);
        padding-top: 1rem;
    }
}

.actions-cell {
    display: flex;
    gap: 0.5rem;
    align-items: center;
}

.action-btn {
    padding: 6px 10px !important;
    font-size: 14px !important;
    min-height: auto !important;
    display: flex;
    align-items: center;
    justify-content: center;
}

@media (max-width: 768px) {
    .actions-cell {
        gap: 0.25rem;
    }
    .action-btn {
        padding: 5px !important;
        font-size: 12px !important;
    }
    
    .vib-source-item > input.vib-desc {
        grid-column: span 2;
    }
}

.mc-meas-row {
    display: grid;
    grid-template-columns: 50px 80px 80px 80px 80px 1fr 40px;
    gap: 0.5rem;
    margin-bottom: 0.25rem;
    padding: 0 5px;
    align-items: center;
}

@media (max-width: 480px) {
    body {
        font-size: 14px;
    }

    aside {
        padding: 0.5rem;
    }

    aside .logo {
        display: none;
    }

    aside nav ul li a span {
        display: none;
    }
    
    aside nav li a {
        padding: 0.6rem;
    }

    .stat-card {
        padding: 1.25rem !important;
    }
}
