:root {
    /* Utilitarian Light Theme */
    --bg-main: #f1f5f9;
    /* Slate-100 */
    --bg-panel: #ffffff;
    --primary-color: #0369a1;
    /* Sky-700 - Professional Blue */
    --accent-color: #3b82f6;
    /* Blue-500 */
    --success-color: #16a34a;
    --warning-color: #d97706;
    --danger-color: #dc2626;

    --text-main: #0f172a;
    /* Slate-900 */
    --text-muted: #64748b;
    /* Slate-500 */

    --border-color: #cbd5e1;
    /* Slate-300 */
    --shadow-sm: 0 1px 2px 0 rgb(0 0 0 / 0.05);
    --shadow-md: 0 4px 6px -1px rgb(0 0 0 / 0.1);
}

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

/* Utilitarian Card Style */
.glass-panel {
    background: var(--bg-panel);
    border: 1px solid var(--border-color);
    box-shadow: var(--shadow-sm);
    border-radius: 8px;
    /* Sharper corners */
    backdrop-filter: none;
    /* Remove glass effect */
}

/* Layout */
.app-container {
    display: flex;
    display: flex;
    height: 100vh;
    /* Fallback */
    height: 100dvh;
    /* Dynamic height for mobile browsers */
}

/* Sidebar */
.sidebar {
    width: 260px;
    padding: 2rem;
    background: #ffffff;
    border-right: 1px solid var(--border-color);
    display: flex;
    flex-direction: column;
}

.logo h2 {
    font-weight: 700;
    letter-spacing: -0.5px;
    margin-bottom: 2rem;
    color: var(--primary-color);
    background: none;
    -webkit-text-fill-color: initial;
}

.nav-links {
    list-style: none;
    padding: 0;
}

.nav-links li {
    padding: 0.75rem 1rem;
    margin-bottom: 0.25rem;
    border-radius: 6px;
    cursor: pointer;
    transition: all 0.2s ease;
    color: var(--text-muted);
    font-weight: 500;
    border: 1px solid transparent;
}

.nav-links li:hover {
    background: #f8fafc;
    color: var(--text-main);
}

.nav-links li.active {
    background: #e0f2fe;
    /* Light Blue BG */
    color: var(--primary-color);
    border-color: #bae6fd;
}

.nav-links li i {
    margin-right: 12px;
    width: 20px;
}

/* Main Content */
.main-content {
    flex: 1;
    padding: 2rem;
    overflow-y: auto;
    position: relative;
    padding-bottom: 5rem;
    /* Ensure bottom content is visible */
    -webkit-overflow-scrolling: touch;
    /* Smooth scroll on iOS */
}

.view {
    animation: fadeIn 0.4s ease-out;
}

.view.hidden {
    display: none;
}

/* Typography */
h1 {
    font-weight: 600;
    margin-bottom: 0.5rem;
}

/* Grid System */
.grid-layout {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));
    gap: 1.5rem;
    margin-top: 2rem;
}

.card {
    padding: 1.5rem;
    transition: transform 0.2s;
}

.card:hover {
    transform: translateY(-2px);
}

.card-header h3 {
    margin: 0;
    font-weight: 500;
    color: var(--primary-color);
}

/* Tables */
.data-table {
    width: 100%;
    border-collapse: collapse;
    margin-top: 1rem;
    background: white;
    border: 1px solid var(--border-color);
}

.data-table th {
    text-align: left;
    padding: 0.75rem 1rem;
    background: #f8fafc;
    border-bottom: 1px solid var(--border-color);
    color: var(--text-main);
    font-weight: 600;
    font-size: 0.85rem;
    text-transform: uppercase;
}

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

/* Buttons */
.btn-primary {
    background: var(--primary-color);
    border: 1px solid var(--primary-color);
    padding: 0.6rem 1.2rem;
    color: white;
    border-radius: 6px;
    cursor: pointer;
    font-weight: 500;
    box-shadow: var(--shadow-sm);
    transition: all 0.2s;
}

.btn-primary:hover {
    background: #0284c7;
    /* Darker Blue */
    transform: none;
    box-shadow: var(--shadow-md);
}

.btn-secondary {
    background: white;
    border: 1px solid var(--border-color);
    padding: 0.6rem 1.2rem;
    color: var(--text-muted);
    border-radius: 6px;
    cursor: pointer;
    font-weight: 500;
    margin-right: 0.5rem;
}

.btn-secondary:hover {
    background: #f1f5f9;
    color: var(--text-main);
}

/* Forms */
.form-group {
    margin-bottom: 1rem;
}

.form-group label {
    display: block;
    margin-bottom: 0.5rem;
    font-weight: 500;
    font-size: 0.9rem;
    color: var(--text-main);
}

.form-group input,
.form-group select {
    width: 100%;
    padding: 0.6rem;
    border: 1px solid var(--border-color);
    border-radius: 6px;
    font-family: inherit;
    font-size: 1rem;
    background: white;
    color: var(--text-main);
}

.form-group input:focus {
    outline: none;
    border-color: var(--primary-color);
    box-shadow: 0 0 0 2px rgba(3, 105, 161, 0.2);
}

/* Modals */
.modal {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: rgba(0, 0, 0, 0.5);
    /* Dim background */
    display: flex;
    justify-content: center;
    align-items: center;
    z-index: 1000;
    backdrop-filter: blur(2px);
}

.modal.hidden {
    display: none;
}

.modal-content {
    width: 100%;
    max-width: 500px;
    padding: 0;
    /* Header/Body handles padding */
    animation: fadeIn 0.2s ease-out;
    background: var(--bg-panel);
    /* Ensure background is set */
}

.modal-xl {
    max-width: 1100px;
    max-height: 90vh;
    display: flex;
    flex-direction: column;
}

.modal-xl .modal-body {
    overflow-y: auto;
    flex: 1;
}

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

.modal-header h3 {
    margin: 0;
    color: var(--text-main);
}

.modal-body {
    padding: 1.5rem;
}

.close-btn {
    background: none;
    border: none;
    font-size: 1.5rem;
    cursor: pointer;
    color: var(--text-muted);
}

.close-btn:hover {
    color: var(--danger-color);
}

/* Animations */
@keyframes fadeIn {
    from {
        opacity: 0;
        transform: translateY(10px);
    }

    to {
        opacity: 1;
        transform: translateY(0);
    }
}

/* Global Utilities */
.hidden {
    display: none !important;
}

/* Enforce Modal Visibility when not hidden */
.modal {
    /* ensure these aren't overwritten */
    display: flex;
    visibility: visible;
    opacity: 1;
}


/* --- Responsive Design (Tablets & Mobile) --- */
@media (max-width: 768px) {
    body {
        overflow: auto;
        /* Allow natural scrolling */
    }

    .app-container {
        flex-direction: column;
        height: auto;
        /* Let content dictate height */
    }

    .sidebar {
        width: 100%;
        height: auto;
        border-right: none;
        border-bottom: 1px solid var(--border-color);
        padding: 1rem;
    }

    .sidebar .logo h2 {
        margin-bottom: 1rem;
        font-size: 1.5rem;
    }

    .nav-links {
        display: flex;
        overflow-x: auto;
        gap: 0.5rem;
        padding-bottom: 0.5rem;
        /* Space for scrollbar if needed */
    }

    .nav-links li {
        margin-bottom: 0;
        white-space: nowrap;
        padding: 0.5rem 0.8rem;
        font-size: 0;
        /* Hide text */
        display: flex;
        justify-content: center;
        align-items: center;
    }

    .nav-links li i {
        margin-right: 0;
        font-size: 1.2rem;
        width: auto;
    }

    .main-content {
        padding: 1rem;
        overflow-y: visible;
        /* Let body handle scrolling */
    }

    /* Stack Grid Layouts */
    .grid-layout {
        grid-template-columns: 1fr !important;
        /* Force stack */
        gap: 1rem;
    }

    /* Modals */
    .modal {
        padding: 1rem;
        /* Buffer from edge */
        align-items: flex-start;
        /* Align top so we can scroll long modals */
        overflow-y: auto;
        /* Scroll the modal wrapper */
    }

    .modal-content {
        max-width: 100%;
        margin: 2rem auto;
        /* Vertical margin */
        max-height: none;
        /* Let it grow */
    }

    .modal-body {
        max-height: none !important;
        /* Override explicit max-heights */
        overflow-y: visible !important;
    }

    /* Forms in Modals: Stack them */
    #add-item-form>div[style*="grid-template-columns"] {
        grid-template-columns: 1fr !important;
        gap: 1rem !important;
    }
}

/* --- Responsive Tables --- */
/* Wrap tables in a container to allow scroll */
.table-responsive {
    width: 100%;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    border: 1px solid var(--border-color);
    border-radius: 6px;
}

.data-table {
    margin-top: 0;
    /* Container has border */
    border: none;
    min-width: 600px;
    /* Ensure table keeps shape and forces scroll */
}

/* Fix for PO Links on Mobile */
.data-table a {
    position: relative;
    z-index: 10;
    display: inline-block;
    padding: 0.2rem;
    /* Increase touch target */
}

/* Ensure card hover doesn't interfere on touch */
@media (hover: none) {
    .card:hover {
        transform: none;
    }
}

/* --- Modal Layout Helpers --- */
/* Replace inline flex/grid with these classes to enable mobile stacking */

/* Split Layout (Left: Form, Right: List) */
.modal-split-layout {
    display: flex;
    gap: 2rem;
    align-items: flex-start;
}

/* Grid for Form Fields (2 columns default) */
.modal-form-grid {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 1.5rem;
    /* Reduced from 2rem for better fit */
    margin-bottom: 1.5rem;
}

/* Fix alignment for inputs with uneven labels */
.modal-form-grid .form-group,
.modal-form-grid-3 .form-group {
    display: flex;
    flex-direction: column;
    height: 100%;
    margin-bottom: 0;
    /* Let grid gap handle spacing */
}

.modal-form-grid .form-group input,
.modal-form-grid-3 .form-group input,
.modal-form-grid .form-group select,
.modal-form-grid-3 .form-group select {
    margin-top: auto;
}

/* Grid for 3 columns */
.modal-form-grid-3 {
    display: grid;
    grid-template-columns: 1fr 1fr 1fr;
    gap: 1.5rem;
    margin-bottom: 1.5rem;
}

/* Responsive Header */
.responsive-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 2rem;
}

/* Modal specific mobile overrides */
@media (max-width: 768px) {
    .modal-split-layout {
        flex-direction: column;
        gap: 1.5rem;
    }

    .modal-split-layout>div {
        width: 100%;
        border-right: none !important;
        /* Remove separator line */
        padding-right: 0 !important;
        border-bottom: 1px solid var(--border-color);
        /* Add vertical separator */
        padding-bottom: 1.5rem;
        margin-bottom: 1.5rem;
    }

    /* Remove border from the last item if needed, but flex items are distinct divs */
    .modal-split-layout>div:last-child {
        border-bottom: none;
        padding-bottom: 0;
        margin-bottom: 0;
    }

    .modal-form-grid,
    .modal-form-grid-3 {
        grid-template-columns: 1fr;
        gap: 1rem;
    }

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

    .responsive-header>div {
        width: 100%;
    }

    /* Adjust button width in stacked header if desired */
    .responsive-header .btn-primary,
    .responsive-header .btn-secondary {
        width: 100%;
        margin-bottom: 0.5rem;
        display: flex;
        justify-content: center;
        align-items: center;
    }
}