@import url('https://fonts.googleapis.com/css2?family=Noto+Sans+Thai:wght@300;400;500;600;700&display=swap');

/* ========================================
   RESET & BASE
   ======================================== */
* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

:root {
    /* Colors */
    --primary-50: #ecfdf5;
    --primary-100: #d1fae5;
    --primary-500: #10b981;
    --primary-600: #059669;
    --primary-700: #047857;

    --secondary-500: #3b82f6;
    --secondary-600: #2563eb;

    --gray-50: #f9fafb;
    --gray-100: #f3f4f6;
    --gray-200: #e5e7eb;
    --gray-300: #d1d5db;
    --gray-400: #9ca3af;
    --gray-500: #6b7280;
    --gray-600: #4b5563;
    --gray-700: #374151;
    --gray-800: #1f2937;
    --gray-900: #111827;

    --white: #ffffff;
    --black: #000000;

    --success: #10b981;
    --warning: #f59e0b;
    --error: #ef4444;
    --info: #3b82f6;

    /* Spacing (8px grid) */
    --space-1: 0.25rem;
    /* 4px */
    --space-2: 0.5rem;
    /* 8px */
    --space-3: 0.75rem;
    /* 12px */
    --space-4: 1rem;
    /* 16px */
    --space-5: 1.25rem;
    /* 20px */
    --space-6: 1.5rem;
    /* 24px */
    --space-8: 2rem;
    /* 32px */
    --space-10: 2.5rem;
    /* 40px */
    --space-12: 3rem;
    /* 48px */
    --space-16: 4rem;
    /* 64px */

    /* Typography */
    --font-sans: 'Noto Sans Thai', -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;

    --text-xs: 0.75rem;
    /* 12px */
    --text-sm: 0.875rem;
    /* 14px */
    --text-base: 1rem;
    /* 16px */
    --text-lg: 1.125rem;
    /* 18px */
    --text-xl: 1.25rem;
    /* 20px */
    --text-2xl: 1.5rem;
    /* 24px */
    --text-3xl: 1.875rem;
    /* 30px */
    --text-4xl: 2.25rem;
    /* 36px */

    /* Shadows */
    --shadow-sm: 0 1px 2px 0 rgba(0, 0, 0, 0.05);
    --shadow: 0 1px 3px 0 rgba(0, 0, 0, 0.1), 0 1px 2px 0 rgba(0, 0, 0, 0.06);
    --shadow-md: 0 4px 6px -1px rgba(0, 0, 0, 0.1), 0 2px 4px -1px rgba(0, 0, 0, 0.06);
    --shadow-lg: 0 10px 15px -3px rgba(0, 0, 0, 0.1), 0 4px 6px -2px rgba(0, 0, 0, 0.05);
    --shadow-xl: 0 20px 25px -5px rgba(0, 0, 0, 0.1), 0 10px 10px -5px rgba(0, 0, 0, 0.04);

    /* Border Radius */
    --radius-sm: 0.25rem;
    /* 4px */
    --radius: 0.5rem;
    /* 8px */
    --radius-md: 0.75rem;
    /* 12px */
    --radius-lg: 1rem;
    /* 16px */
    --radius-full: 9999px;

    /* Transitions */
    --transition-fast: 150ms cubic-bezier(0.4, 0, 0.2, 1);
    --transition-base: 200ms cubic-bezier(0.4, 0, 0.2, 1);
    --transition-slow: 300ms cubic-bezier(0.4, 0, 0.2, 1);

    /* Z-index */
    --z-dropdown: 1000;
    --z-sticky: 1020;
    --z-fixed: 1030;
    --z-modal-backdrop: 1040;
    --z-modal: 1050;
    --z-popover: 1060;
    --z-tooltip: 1070;
}

body {
    font-family: var(--font-sans);
    font-size: var(--text-base);
    line-height: 1.6;
    color: var(--gray-800);
    background-color: var(--gray-50);
    -webkit-font-smoothing: antialiased;
    -moz-osx-font-smoothing: grayscale;
}

/* ========================================
   LAYOUT
   ======================================== */
.container {
    width: 100%;
    max-width: 1280px;
    margin: 0 auto;
    padding: 0 var(--space-4);
}

@media (min-width: 640px) {
    .container {
        padding: 0 var(--space-6);
    }
}

/* ========================================
   SIDEBAR LAYOUT (Mobile-First)
   ======================================== */
.app-layout {
    display: flex;
    flex-direction: column;
    min-height: 100vh;
}

.sidebar {
    position: fixed;
    top: 0;
    left: -280px;
    width: 280px;
    height: 100vh;
    background: var(--white);
    border-right: 1px solid var(--gray-200);
    transition: left var(--transition-base);
    z-index: var(--z-fixed);
    overflow-y: auto;
    display: flex;
    flex-direction: column;
}

.sidebar.active {
    left: 0;
    box-shadow: var(--shadow-xl);
}

.sidebar-header {
    padding: var(--space-6) var(--space-4);
    border-bottom: 1px solid var(--gray-200);
}

.sidebar-logo {
    display: flex;
    align-items: center;
    gap: var(--space-3);
}

.sidebar-logo img {
    height: 32px;
}

.sidebar-logo-text {
    font-size: var(--text-xl);
    font-weight: 700;
    color: var(--gray-800);
}

.sidebar-nav {
    padding: var(--space-4) 0;
}

.sidebar-nav-item {
    display: flex;
    align-items: center;
    gap: var(--space-3);
    padding: var(--space-3) var(--space-4);
    color: var(--gray-600);
    text-decoration: none;
    font-weight: 500;
    transition: all var(--transition-fast);
    border-left: 3px solid transparent;
    width: 100%;
    /* Force full width */
    min-height: 48px;
    /* Prevent collapsing */
}

.sidebar-nav-item:hover {
    background: var(--gray-50);
    color: var(--primary-600);
}

.sidebar-nav-item.active {
    background: var(--primary-50);
    color: var(--primary-600);
    border-left-color: var(--primary-600);
}

.sidebar-nav-icon {
    width: 20px;
    height: 20px;
    flex-shrink: 0;
}

/* Main Content */
.main-content {
    flex: 1;
    padding: var(--space-4);
    margin-top: 60px;
    /* Mobile header height */
}

/* Mobile Header */
.mobile-header {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    height: 60px;
    background: var(--white);
    border-bottom: 1px solid var(--gray-200);
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 0 var(--space-4);
    z-index: var(--z-sticky);
}

.mobile-menu-btn {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 40px;
    height: 40px;
    border: none;
    background: transparent;
    cursor: pointer;
    border-radius: var(--radius);
    transition: background var(--transition-fast);
}

.mobile-menu-btn:hover {
    background: var(--gray-100);
}

.mobile-menu-btn svg {
    width: 24px;
    height: 24px;
    color: var(--gray-700);
}

.mobile-header-title {
    font-size: var(--text-lg);
    font-weight: 600;
    color: var(--gray-800);
}

/* Overlay */
.sidebar-overlay {
    display: none;
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: rgba(0, 0, 0, 0.5);
    z-index: calc(var(--z-fixed) - 1);
}

.sidebar-overlay.active {
    display: block;
}

/* Desktop Layout */
@media (min-width: 1024px) {
    .sidebar {
        left: 0;
        position: fixed;
    }

    .sidebar.active {
        box-shadow: none;
    }

    .main-content {
        margin-left: 280px;
        margin-top: 0;
        padding: var(--space-8);
    }

    .mobile-header {
        display: none;
    }

    .sidebar-overlay {
        display: none !important;
    }
}

/* ========================================
   STATS GRID
   ======================================== */
.stats-grid {
    display: grid;
    grid-template-columns: 1fr;
    gap: var(--space-4);
    margin-bottom: var(--space-6);
}

@media (min-width: 640px) {
    .stats-grid {
        grid-template-columns: repeat(2, 1fr);
    }
}

@media (min-width: 1024px) {
    .stats-grid {
        grid-template-columns: repeat(4, 1fr);
    }
}

.stat-card {
    background: var(--white);
    padding: var(--space-6);
    border-radius: var(--radius-lg);
    box-shadow: var(--shadow);
    border-left: 4px solid var(--primary-500);
}

.stat-card.warning {
    border-left-color: var(--warning);
}

.stat-card.success {
    border-left-color: var(--success);
}

.stat-card.info {
    border-left-color: var(--info);
}

.stat-label {
    font-size: var(--text-sm);
    color: var(--gray-600);
    margin-bottom: var(--space-2);
}

.stat-value {
    font-size: var(--text-3xl);
    font-weight: 700;
    color: var(--gray-800);
}

/* ========================================
   CARDS
   ======================================== */
.card {
    background: var(--white);
    border-radius: var(--radius-lg);
    box-shadow: var(--shadow);
    overflow: hidden;
    margin-bottom: var(--space-6);
}

.card-header {
    padding: var(--space-6);
    border-bottom: 1px solid var(--gray-200);
    display: flex;
    justify-content: space-between;
    align-items: center;
}

.card-title {
    font-size: var(--text-xl);
    font-weight: 600;
    color: var(--gray-800);
    margin: 0;
}

.card-body {
    padding: var(--space-6);
}

.card-footer {
    padding: var(--space-6);
    border-top: 1px solid var(--gray-200);
    background: var(--gray-50);
}

/* ========================================
   TABS
   ======================================== */
.tabs {
    display: flex;
    gap: var(--space-2);
    /* border-bottom: 2px solid var(--gray-200); */
}

.tab-btn {
    padding: var(--space-2) var(--space-4);
    background: transparent;
    border: none;
    border-bottom: 2px solid transparent;
    color: var(--gray-600);
    font-weight: 600;
    cursor: pointer;
    transition: all var(--transition-fast);
    margin-bottom: -1px;
    font-size: var(--text-base);
}

.tab-btn:hover {
    color: var(--gray-800);
    background: var(--gray-50);
    border-radius: var(--radius-sm);
}

.tab-btn.active {
    color: var(--primary-600);
    border-bottom-color: var(--primary-600);
}

.tab-content {
    display: none;
}

.tab-content.active {
    display: block;
    animation: fadeIn var(--transition-fast);
}

/* ========================================
   BUTTONS
   ======================================== */
.btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: var(--space-2);
    padding: var(--space-3) var(--space-6);
    font-size: var(--text-base);
    font-weight: 500;
    line-height: 1;
    text-decoration: none;
    border: 1px solid transparent;
    border-radius: var(--radius);
    cursor: pointer;
    transition: all var(--transition-fast);
    min-height: 44px;
    /* Touch-friendly */
    white-space: nowrap;
}

.btn:disabled {
    opacity: 0.5;
    cursor: not-allowed;
}

.btn-primary {
    background: var(--primary-500);
    color: var(--white);
    border-color: var(--primary-500);
}

.btn-primary:hover:not(:disabled) {
    background: var(--primary-600);
    border-color: var(--primary-600);
    transform: translateY(-1px);
    box-shadow: var(--shadow-md);
}

.btn-secondary {
    background: var(--white);
    color: var(--gray-700);
    border-color: var(--gray-300);
}

.btn-secondary:hover:not(:disabled) {
    background: var(--gray-50);
    border-color: var(--gray-400);
}

.btn-success {
    background: var(--success);
    color: var(--white);
}

.btn-danger {
    background: var(--error);
    color: var(--white);
}

.btn-outline {
    background: transparent;
    color: var(--primary-600);
    border: 1px solid var(--primary-600);
}

.btn-outline:hover {
    background: var(--primary-50);
}

.btn-sm {
    padding: var(--space-2) var(--space-4);
    font-size: var(--text-sm);
    min-height: 36px;
}

.btn-lg {
    padding: var(--space-4) var(--space-8);
    font-size: var(--text-lg);
    min-height: 52px;
}

.btn-icon {
    padding: var(--space-3);
    min-width: 44px;
}

/* ========================================
   FORMS
   ======================================== */
.form-group {
    margin-bottom: var(--space-6);
}

.form-label {
    display: block;
    margin-bottom: var(--space-2);
    font-size: var(--text-sm);
    font-weight: 500;
    color: var(--gray-700);
}

.form-label.required::after {
    content: ' *';
    color: var(--error);
}

.form-input,
.form-select,
.form-textarea {
    width: 100%;
    padding: var(--space-3) var(--space-4);
    font-size: var(--text-base);
    line-height: 1.5;
    color: var(--gray-800);
    background: var(--white);
    border: 1px solid var(--gray-300);
    border-radius: var(--radius);
    transition: all var(--transition-fast);
    min-height: 44px;
    /* Touch-friendly */
}

.form-input:focus,
.form-select:focus,
.form-textarea:focus {
    outline: none;
    border-color: var(--primary-500);
    box-shadow: 0 0 0 3px var(--primary-100);
}

.form-input:disabled,
.form-select:disabled,
.form-textarea:disabled {
    background: var(--gray-100);
    cursor: not-allowed;
}

.form-textarea {
    min-height: 120px;
    resize: vertical;
}

.form-error {
    margin-top: var(--space-2);
    font-size: var(--text-sm);
    color: var(--error);
}

/* ========================================
   TABLES (Mobile-First)
   ======================================== */
.table-container {
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    border-radius: var(--radius-lg);
    box-shadow: var(--shadow);
}

.table {
    width: 100%;
    border-collapse: collapse;
    background: var(--white);
}

.table thead {
    background: var(--gray-50);
    border-bottom: 2px solid var(--gray-200);
}

.table th {
    padding: var(--space-4);
    text-align: left;
    font-size: var(--text-sm);
    font-weight: 600;
    color: var(--gray-700);
    white-space: nowrap;
}

.table td {
    padding: var(--space-4);
    border-bottom: 1px solid var(--gray-200);
    font-size: var(--text-sm);
}

.table tbody tr:hover {
    background: var(--gray-50);
}

/* Mobile Card View (Base Styles) */
.table-mobile-card {
    display: none;
}

@media (max-width: 768px) {
    /* .table-mobile-card is already hidden by default */

    .table-mobile-card.active {
        display: block;
    }

    .mobile-card {
        background: var(--white);
        border-radius: var(--radius-lg);
        padding: var(--space-4);
        margin-bottom: var(--space-4);
        box-shadow: var(--shadow);
    }

    .mobile-card-row {
        display: flex;
        justify-content: space-between;
        padding: var(--space-2) 0;
        border-bottom: 1px solid var(--gray-100);
    }

    .mobile-card-row:last-child {
        border-bottom: none;
    }

    .mobile-card-label {
        font-weight: 600;
        color: var(--gray-600);
        font-size: var(--text-sm);
    }

    .mobile-card-value {
        color: var(--gray-800);
        font-size: var(--text-sm);
    }
}

/* ========================================
   MODALS
   ======================================== */
.modal-backdrop {
    display: none;
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: rgba(0, 0, 0, 0.5);
    z-index: var(--z-modal-backdrop);
    animation: fadeIn var(--transition-fast);
}

.modal-backdrop.active {
    display: flex !important;
    align-items: center !important;
    justify-content: center !important;
    padding: var(--space-4);
    position: fixed !important;
    top: 0 !important;
    left: 0 !important;
    right: 0 !important;
    bottom: 0 !important;
}

.modal {
    background: var(--white);
    border-radius: var(--radius-lg);
    box-shadow: var(--shadow-xl);
    max-width: 600px;
    width: 100%;
    max-height: 90vh;
    overflow-y: auto;
    animation: slideUp var(--transition-base);
}

.modal-lg {
    max-width: 900px;
}

.modal-header {
    padding: var(--space-6);
    border-bottom: 1px solid var(--gray-200);
    display: flex;
    align-items: center;
    justify-content: space-between;
}

.modal-title {
    font-size: var(--text-xl);
    font-weight: 600;
    color: var(--gray-800);
    margin: 0;
}

.modal-close {
    width: 32px;
    height: 32px;
    border: none;
    background: transparent;
    cursor: pointer;
    border-radius: var(--radius);
    transition: background var(--transition-fast);
    font-size: 1.5rem;
    display: flex;
    align-items: center;
    justify-content: center;
}

.modal-close:hover {
    background: var(--gray-100);
}

.modal-body {
    padding: var(--space-6);
}

.modal-footer {
    padding: var(--space-6);
    border-top: 1px solid var(--gray-200);
    display: flex;
    gap: var(--space-3);
    justify-content: flex-end;
}

/* ========================================
   UTILITIES
   ======================================== */
.text-center {
    text-align: center;
}

.text-right {
    text-align: right;
}

.font-bold {
    font-weight: 700;
}

.font-semibold {
    font-weight: 600;
}

.mt-4 {
    margin-top: var(--space-4);
}

.mb-4 {
    margin-bottom: var(--space-4);
}

.mr-4 {
    margin-right: var(--space-4);
}

.ml-4 {
    margin-left: var(--space-4);
}

.hidden {
    display: none;
}

@keyframes spin {
    from {
        transform: rotate(0deg);
    }

    to {
        transform: rotate(360deg);
    }
}

.animate-spin {
    animation: spin 1s linear infinite;
}

.loading-spinner {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    color: var(--primary-600);
}


/* ========================================
   ANIMATIONS
   ======================================== */
@keyframes fadeIn {
    from {
        opacity: 0;
    }

    to {
        opacity: 1;
    }
}

@keyframes slideUp {
    from {
        opacity: 0;
        transform: translateY(20px);
    }

    to {
        opacity: 1;
        transform: translateY(0);
    }
}

/* ========================================
   RESPONSIVE BREAKPOINTS
   ======================================== */
/* Mobile: < 640px (default) */
/* Tablet: 640px - 1023px */
/* Desktop: >= 1024px */

@media (min-width: 640px) {
    .card-body {
        padding: var(--space-8);
    }
}

@media (min-width: 1024px) {
    .main-content {
        padding: var(--space-12);
    }
}

.custom-dropdown {
    position: relative;
    width: 100%;
}

.custom-dropdown-trigger {
    display: flex;
    justify-content: space-between;
    align-items: center;
    width: 100%;
    padding: 0.75rem 1rem;
    background-color: white;
    border: 1px solid #d1d5db;
    border-radius: 0.5rem;
    cursor: pointer;
    transition: all 0.2s;
    font-size: 0.95rem;
    color: #374151;
}

.custom-dropdown-trigger:hover {
    border-color: var(--primary-color, #10b981);
}

.custom-dropdown-menu {
    display: none;
    position: absolute;
    z-index: 50;
    width: 100%;
    max-height: 300px;
    margin-top: 0.5rem;
    background-color: white;
    border: 1px solid #e5e7eb;
    border-radius: 0.5rem;
    box-shadow: 0 10px 15px -3px rgba(0, 0, 0, 0.1);
    overflow: hidden;
}

.custom-dropdown-menu.active {
    display: block;
    animation: fadeIn 0.15s ease-out;
}

.custom-dropdown-search-container {
    position: sticky;
    top: 0;
    padding: 0.75rem;
    background-color: white;
    border-bottom: 1px solid #f3f4f6;
    z-index: 10;
}

.custom-dropdown-search {
    width: 100%;
    padding: 0.5rem 0.75rem;
    border: 1px solid #e5e7eb;
    border-radius: 0.375rem;
    font-size: 0.9rem;
    outline: none;
    background-color: #f9fafb;
}

.custom-dropdown-search:focus {
    border-color: var(--primary-color, #10b981);
    background-color: white;
}

.custom-dropdown-items {
    max-height: 220px;
    overflow-y: auto;
    scrollbar-width: thin;
}

.custom-dropdown-item {
    padding: 0.75rem 1rem;
    cursor: pointer;
    transition: background-color 0.15s;
    border-bottom: 1px solid #f9fafb;
    display: flex;
    flex-direction: column;
}

.custom-dropdown-item:last-child {
    border-bottom: none;
}

.custom-dropdown-item:hover {
    background-color: #f3f4f6;
}

.custom-dropdown-item.selected {
    background-color: #ecfdf5;
    border-left: 3px solid var(--primary-color, #10b981);
}

.item-main {
    font-weight: 500;
    color: #111827;
}

.item-sub {
    font-size: 0.85rem;
    color: #6b7280;
    margin-top: 2px;
}

@keyframes fadeIn {
    from {
        opacity: 0;
        transform: translateY(-5px);
    }

    to {
        opacity: 1;
        transform: translateY(0);
    }
}