:root {
    --primary-color: #4e73df;
    --secondary-color: #224abe;
    --success-color: #1cc88a;
    --info-color: #36b9cc;
    --warning-color: #f6c23e;
    --danger-color: #e74a3b;
    --light-color: #f8f9fc;
    --dark-color: #5a5c69;
    --sidebar-width: 260px;
    --sidebar-collapsed-width: 0px;
    --font-family-base: 'Sarabun', sans-serif;
    --bottom-nav-height: 64px;
    --navbar-height: 60px;
    --safe-area-top: env(safe-area-inset-top, 0px);
    --safe-area-bottom: env(safe-area-inset-bottom, 0px);
}

* {
    -webkit-tap-highlight-color: transparent;
}

body {
    font-family: var(--font-family-base);
    background-color: var(--light-color);
    color: var(--dark-color);
    overflow-x: hidden;
    -webkit-font-smoothing: antialiased;
    text-rendering: optimizeLegibility;
}

/* --- Sidebar --- */
#sidebar {
    min-width: var(--sidebar-width);
    max-width: var(--sidebar-width);
    height: 100vh;
    background: linear-gradient(180deg, var(--primary-color) 0%, var(--secondary-color) 100%);
    transition: transform 0.3s cubic-bezier(0.4, 0, 0.2, 1);
    position: fixed;
    top: 0;
    left: 0;
    bottom: 0;
    z-index: 1050;
    color: white;
    box-shadow: 4px 0 15px rgba(0, 0, 0, 0.1);
    overflow-y: auto;
    overflow-x: hidden;
    scrollbar-width: none;
    display: flex;
    flex-direction: column;
}

#sidebar.collapsed {
    transform: translateX(calc(-1 * var(--sidebar-width)));
}

#sidebar::-webkit-scrollbar {
    display: none;
}

.sidebar-header {
    padding: 25px 20px;
    display: flex;
    align-items: center;
    background: rgba(0, 0, 0, 0.05);
    margin-bottom: 10px;
}

#sidebar .nav-link {
    padding: 12px 18px;
    color: rgba(255, 255, 255, 0.75) !important;
    display: flex;
    align-items: center;
    border-radius: 12px;
    margin: 4px 15px;
    min-height: 46px;
    transition: all 0.2s ease;
    font-weight: 500;
    border-left: 3px solid transparent;
}

#sidebar .nav-link i {
    font-size: 1.2rem;
    margin-right: 12px;
    width: 24px;
    text-align: center;
    transition: transform 0.2s ease;
}

#sidebar .nav-link:hover {
    color: white !important;
    background: rgba(255, 255, 0, 0.1);
    transform: translateX(4px);
}

#sidebar .nav-link.active {
    color: white !important;
    background: rgba(255, 255, 255, 0.2);
    border-left: 3px solid white;
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
}

#sidebar .nav-link:hover i {
    transform: scale(1.1);
}

/* Submenu Styling */
#sidebar .submenu {
    background: rgba(0, 0, 0, 0.1);
    margin: 4px 15px;
    border-radius: 12px;
    padding: 5px 0;
}

#sidebar .submenu .nav-link {
    margin: 2px 10px 2px 25px;
    padding: 8px 15px;
    font-size: 0.9rem;
    opacity: 0.8;
    border-left: none;
}

#sidebar .submenu .nav-link:hover,
#sidebar .submenu .nav-link.active {
    opacity: 1;
    background: rgba(255, 255, 255, 0.1);
    transform: none;
}

#sidebar .submenu .nav-link i {
    font-size: 1rem;
    margin-right: 10px;
    width: 20px;
}

/* --- Content Area --- */
#content {
    width: 100%;
    margin-left: var(--sidebar-width);
    min-height: 100vh;
    transition: margin-left 0.3s ease;
    background-color: var(--light-color);
}

#content.expanded {
    margin-left: 0;
}

/* --- UI Elements --- */
.card {
    border: none;
    border-radius: 16px;
    box-shadow: 0 5px 15px rgba(0, 0, 0, 0.05);
}

/* บังคับไม่ให้มีการยุบตัวในสถานะคลิก (Active) */
.card:active,
.btn:active,
.btn-upload-photo:active,
.row>*:active {
    transform: none !important;
    transition: none !important;
}

/* Photo Preview (สำหรับการลงทะเบียนใบหน้า) */
.photo-preview-container {
    width: 150px;
    height: 180px;
    border: 2px dashed #dee2e6;
    border-radius: 10px;
    display: flex;
    align-items: center;
    justify-content: center;
    overflow: hidden;
    background-color: #f8f9fa;
    position: relative;
    max-width: 100%;
}

.photo-preview-container img {
    width: 100%;
    height: 100%;
    object-fit: cover;
}

/* --- Top Navbar --- */
.navbar {
    padding: 0.75rem 1.5rem;
    background: rgba(255, 255, 255, 0.95);
    backdrop-filter: blur(10px);
    border-bottom: 1px solid rgba(0, 0, 0, 0.05);
    height: var(--navbar-height);
    padding-top: max(0.75rem, var(--safe-area-top));
}

.mobile-brand {
    font-weight: 700;
    color: var(--primary-color);
    font-size: 1.1rem;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    max-width: 50vw;
}

/* --- Bottom Navigation (Mobile) --- */
.bottom-nav {
    display: none;
    position: fixed;
    bottom: 0;
    left: 0;
    width: 100%;
    height: calc(var(--bottom-nav-height) + var(--safe-area-bottom));
    padding-bottom: var(--safe-area-bottom);
    background: #ffffff !important;
    z-index: 1030;
    justify-content: space-around;
    align-items: center;
    box-shadow: 0 -2px 10px rgba(0, 0, 0, 0.1);
}

.bottom-nav-item {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    text-decoration: none;
    color: #6c757d;
    font-size: 0.68rem;
    font-weight: 500;
    padding: 8px 4px;
    flex: 1;
    height: 100%;
    transition: color 0.2s ease;
    gap: 2px;
    -webkit-tap-highlight-color: transparent;
}

.bottom-nav-item i {
    font-size: 1.4rem;
    transition: transform 0.2s ease;
}

.bottom-nav-item span {
    text-align: center;
    line-height: 1.1;
    max-width: 100%;
}

.bottom-nav-item.active {
    color: var(--primary-color);
}

.bottom-nav-item.active i {
    transform: scale(1.15);
}

.bottom-nav-item:active {
    color: var(--primary-color);
}

/* Select2 Fix for Modals */
.select2-container {
    z-index: 9999 !important;
}

/* --- Table Container (horizontal scroll on mobile) --- */
.table-container {
    width: 100%;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
}

/* --- PWA Install Banner --- */
.pwa-install-banner {
    position: fixed;
    bottom: calc(var(--bottom-nav-height) + var(--safe-area-bottom) + 16px);
    left: 16px;
    right: 16px;
    background: linear-gradient(135deg, var(--primary-color), var(--secondary-color));
    color: white;
    border-radius: 16px;
    padding: 14px 18px;
    box-shadow: 0 8px 24px rgba(78, 115, 223, 0.4);
    z-index: 1040;
    display: none;
    align-items: center;
    gap: 12px;
    animation: slideUp 0.4s ease;
}

.pwa-install-banner.show {
    display: flex;
}

.pwa-install-banner .pwa-icon {
    font-size: 1.8rem;
    flex-shrink: 0;
}

.pwa-install-banner .pwa-text {
    flex: 1;
    font-size: 0.88rem;
    line-height: 1.3;
}

.pwa-install-banner .pwa-text strong {
    display: block;
    font-size: 0.95rem;
    margin-bottom: 2px;
}

.pwa-install-banner .pwa-actions {
    display: flex;
    gap: 8px;
    flex-shrink: 0;
}

.pwa-install-banner .pwa-btn {
    border: none;
    border-radius: 8px;
    padding: 8px 14px;
    font-size: 0.82rem;
    font-weight: 600;
    cursor: pointer;
}

.pwa-install-banner .pwa-btn-install {
    background: white;
    color: var(--primary-color);
}

.pwa-install-banner .pwa-btn-dismiss {
    background: rgba(255, 255, 255, 0.2);
    color: white;
}

@keyframes slideUp {
    from { transform: translateY(120%); opacity: 0; }
    to   { transform: translateY(0);    opacity: 1; }
}

/* --- Pull to refresh hint --- */
.ptr-hint {
    text-align: center;
    padding: 10px;
    color: #6c757d;
    font-size: 0.82rem;
    display: none;
}

.ptr-hint.show {
    display: block;
}

/* --- Mobile Responsive --- */
@media (max-width: 991.98px) {
    #sidebar {
        transform: translateX(calc(-1 * var(--sidebar-width)));
    }

    #sidebar.active {
        transform: translateX(0);
        box-shadow: 10px 0 30px rgba(0, 0, 0, 0.2);
    }

    #content {
        margin-left: 0 !important;
        padding-bottom: calc(var(--bottom-nav-height) + var(--safe-area-bottom) + 20px) !important;
    }

    .bottom-nav {
        display: flex;
    }

    .row>* {
        margin-bottom: 1rem;
    }

    /* ปรับ navbar ให้เหมาะกับมือถือ */
    .navbar {
        padding: 0.5rem 0.75rem;
    }

    .container-fluid {
        padding: 12px !important;
    }
}

@media (max-width: 576px) {
    :root {
        --navbar-height: 54px;
    }

    .container-fluid {
        padding: 10px !important;
    }

    /* ปุ่มใหญ่ขึ้นสำหรับ touch */
    .btn {
        min-height: 44px;
        min-width: 44px;
        display: inline-flex;
        align-items: center;
        justify-content: center;
        font-size: 0.92rem;
    }

    .btn-sm {
        min-height: 38px;
        min-width: 38px;
    }

    .btn-lg {
        min-height: 52px;
        font-size: 1.05rem;
    }

    .form-control,
    .form-select {
        height: 48px;
        font-size: 16px !important; /* 16px ป้องกัน iOS auto-zoom */
    }

    /* Card ให้เต็มจอ */
    .card {
        border-radius: 12px;
        margin-bottom: 12px;
    }

    .card-body {
        padding: 14px;
    }

    /* ตาราง — ปรับให้สวยบนมือถือ */
    .table {
        font-size: 0.85rem;
    }

    .table th,
    .table td {
        padding: 0.5rem 0.4rem;
    }

    /* หัวข้อหน้า */
    h1 { font-size: 1.4rem; }
    h2 { font-size: 1.25rem; }
    h3 { font-size: 1.1rem; }
    h4 { font-size: 1rem; }

    /* Modal ให้เต็มจอบนมือถือ */
    .modal-dialog {
        margin: 0;
        max-width: 100%;
    }

    .modal-content {
        border-radius: 0;
        min-height: 100vh;
    }

    .modal-header {
        padding-top: calc(0.75rem + var(--safe-area-top));
    }

    .modal-body {
        padding: 14px;
    }

    /* Swal2 ปรับสำหรับมือถือ */
    .swal2-popup {
        font-size: 0.9rem !important;
    }

    .swal2-title {
        font-size: 1.1rem !important;
    }

    /* ซ่อน user display name บนมือถือเล็ก */
    .mobile-brand {
        font-size: 0.95rem;
        max-width: 40vw;
    }
}

/* --- รองรับ notch / dynamic island --- */
@supports (padding: env(safe-area-inset-top)) {
    .navbar {
        padding-top: max(0.75rem, env(safe-area-inset-top));
    }

    #content {
        padding-top: 0;
    }
}

/* --- ป้องกัน text selection บนปุ่ม (touch สะดวกขึ้น) --- */
.btn,
.nav-link,
.bottom-nav-item {
    -webkit-user-select: none;
    user-select: none;
}

/* --- Smooth scroll สำหรับ iOS --- */
body {
    -webkit-overflow-scrolling: touch;
}

/* --- ป้องกัน overscroll bounce บน Android --- */
html {
    overscroll-behavior-y: contain;
}

/* ================================================================
   Mobile Icon Grid Menu — แสดงเฉพาะ mobile/native app
   webapp บน desktop ยังใช้ sidebar เดิม (ไม่ถูกแตะต้อง)
   ================================================================ */

/* Backdrop */
.npk-menu-backdrop {
    position: fixed;
    inset: 0;
    background: rgba(0, 0, 0, 0.5);
    z-index: 1050;
    opacity: 0;
    visibility: hidden;
    pointer-events: none;
    transition: opacity 0.25s ease, visibility 0.25s;
}

.npk-menu-backdrop.show {
    opacity: 1;
    visibility: visible;
    pointer-events: auto;
}

/* Bottom sheet container */
.npk-menu-sheet {
    position: fixed;
    bottom: 0;
    left: 0;
    right: 0;
    max-height: calc(88vh - env(safe-area-inset-top, 0px));
    background: #ffffff;
    border-radius: 20px 20px 0 0;
    z-index: 1051;
    transform: translateY(105%);
    transition: transform 0.3s cubic-bezier(0.32, 0.72, 0, 1);
    box-shadow: 0 -8px 30px rgba(0, 0, 0, 0.2);
    overflow: hidden;
    display: flex;
    flex-direction: column;
}

.npk-menu-sheet.show {
    transform: translateY(0);
}

/* Handle bar */
.npk-menu-handle {
    width: 40px;
    height: 4px;
    background: #d1d5db;
    border-radius: 2px;
    margin: 10px auto 6px;
    flex-shrink: 0;
}

/* Header */
.npk-menu-header {
    display: flex;
    align-items: center;
    padding: 4px 16px 10px;
    flex-shrink: 0;
    border-bottom: 1px solid #f1f5f9;
}

.npk-menu-title {
    font-size: 1.05rem;
    font-weight: 700;
    color: #1e293b;
    font-family: 'Sarabun', 'Noto Sans Thai', sans-serif;
}

.npk-menu-user {
    flex: 1;
    text-align: center;
    font-size: 0.75rem;
    color: #64748b;
    overflow: hidden;
    white-space: nowrap;
    text-overflow: ellipsis;
    padding: 0 8px;
}

.npk-menu-user-name {
    font-weight: 600;
    color: #334155;
}

.npk-menu-user-role {
    margin-left: 6px;
    color: #94a3b8;
}

.npk-menu-close {
    width: 36px;
    height: 36px;
    border: none;
    background: #f1f5f9;
    border-radius: 50%;
    font-size: 1.3rem;
    color: #64748b;
    line-height: 1;
    cursor: pointer;
    flex-shrink: 0;
}

.npk-menu-close:active {
    background: #e2e8f0;
}

/* Search */
.npk-menu-search {
    padding: 10px 16px 6px;
    flex-shrink: 0;
}

.npk-menu-search input {
    width: 100%;
    padding: 10px 16px;
    border: 2px solid #e2e8f0;
    border-radius: 12px;
    font-size: 16px; /* ป้องกัน iOS zoom */
    font-family: 'Sarabun', 'Noto Sans Thai', sans-serif;
    outline: none;
    background: #f8fafc;
}

.npk-menu-search input:focus {
    border-color: #4e73df;
    background: #fff;
}

/* Grid area — scrollable */
.npk-menu-grid {
    flex: 1;
    overflow-y: auto;
    padding: 8px 12px calc(20px + var(--safe-area-bottom, 0px));
    -webkit-overflow-scrolling: touch;
}

.npk-menu-loading {
    text-align: center;
    padding: 40px 20px;
    color: #94a3b8;
    font-size: 0.9rem;
}

.npk-menu-spinner {
    width: 28px;
    height: 28px;
    border: 3px solid #e2e8f0;
    border-top-color: #4e73df;
    border-radius: 50%;
    margin: 0 auto 10px;
    animation: npk-spin 0.8s linear infinite;
}

@keyframes npk-spin {
    to { transform: rotate(360deg); }
}

.npk-menu-empty {
    text-align: center;
    padding: 40px 20px;
    color: #94a3b8;
    font-size: 0.9rem;
}

/* Group label */
.npk-menu-group-label {
    display: flex;
    align-items: center;
    gap: 8px;
    font-size: 1rem;
    font-weight: 700;
    color: #1e293b;
    padding: 10px 12px;
    margin: 14px 0 6px;
    background: #f1f5f9;
    border-left: 5px solid #4e73df;
    border-radius: 10px;
    cursor: pointer;
    font-family: 'Sarabun', 'Noto Sans Thai', sans-serif;
}

.npk-menu-group-name { flex: 1; }

.npk-menu-group-count {
    font-size: 0.75rem;
    font-weight: 600;
    color: #64748b;
    background: #fff;
    border-radius: 10px;
    padding: 1px 9px;
}

.npk-menu-group-chevron {
    font-size: 0.9rem;
    color: #64748b;
    transition: transform 0.2s;
}

.npk-menu-section.collapsed .npk-menu-icons { display: none; }
.npk-menu-section.collapsed .npk-menu-group-chevron { transform: rotate(-90deg); }

/* Filter chips */
.npk-menu-chips {
    display: flex;
    gap: 8px;
    overflow-x: auto;
    padding: 4px 16px 8px;
    flex-shrink: 0;
    -webkit-overflow-scrolling: touch;
    scrollbar-width: none;
}
.npk-menu-chips::-webkit-scrollbar { display: none; }

.npk-menu-chip {
    flex: 0 0 auto;
    min-height: 40px;
    padding: 6px 16px;
    border: 1.5px solid #e2e8f0;
    border-radius: 20px;
    background: #fff;
    color: #475569;
    font-size: 0.9rem;
    font-weight: 600;
    white-space: nowrap;
    font-family: 'Sarabun', 'Noto Sans Thai', sans-serif;
}
.npk-menu-chip.active {
    background: #4e73df;
    border-color: #4e73df;
    color: #fff;
}

/* Icon grid — 4 columns */
.npk-menu-icons {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    gap: 8px;
}

/* Single icon item */
.npk-menu-icon {
    display: flex;
    flex-direction: column;
    align-items: center;
    text-decoration: none;
    padding: 12px 4px;
    min-height: 104px;
    border-radius: 14px;
    position: relative;
    -webkit-tap-highlight-color: transparent;
}

.npk-menu-icon:active {
    background: #f1f5f9;
    transform: scale(0.95);
}

.npk-menu-icon-box {
    width: 64px;
    height: 64px;
    border-radius: 18px;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 1.9rem;
    margin-bottom: 6px;
}

.npk-menu-icon-label {
    font-size: 0.85rem;
    font-weight: 600;
    color: #334155;
    text-align: center;
    line-height: 1.25;
    font-family: 'Sarabun', 'Noto Sans Thai', sans-serif;
    display: -webkit-box;
    -webkit-line-clamp: 2;
    -webkit-box-orient: vertical;
    overflow: hidden;
    max-width: 100%;
    word-break: break-word;
}

/* Badge on icon */
.npk-menu-badge {
    position: absolute;
    top: 6px;
    right: 14px;
    background: #e74a3b;
    color: #fff;
    font-size: 0.6rem;
    font-weight: 700;
    min-width: 18px;
    height: 18px;
    border-radius: 9px;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 0 4px;
    border: 2px solid #fff;
}

/* จอกว้างขึ้น — 4 คอลัมน์ / แท็บเล็ต 6 คอลัมน์ */
@media (min-width: 400px) {
    .npk-menu-icons {
        grid-template-columns: repeat(4, 1fr);
    }
}
@media (min-width: 600px) and (max-width: 991.98px) {
    .npk-menu-icons {
        grid-template-columns: repeat(6, 1fr);
    }
}

/* --- Launcher panel บน dashboard (เฉพาะ mobile/native) --- */
.npk-launcher {
    display: none;
    background: #ffffff;
    border-radius: 16px;
    margin: 0 0 16px;
    padding: 14px 10px 10px;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.06);
}

.npk-launcher-title {
    font-size: 0.8rem;
    font-weight: 700;
    color: #64748b;
    padding: 0 8px 8px;
    font-family: 'Sarabun', 'Noto Sans Thai', sans-serif;
    display: flex;
    align-items: center;
    justify-content: space-between;
}

.npk-launcher-title .npk-launcher-more {
    font-size: 0.72rem;
    font-weight: 500;
    color: #4e73df;
    text-decoration: none;
    cursor: pointer;
}

.npk-launcher-icons {
    display: grid;
    grid-template-columns: repeat(4, 1fr);
    gap: 4px;
}

@media (min-width: 481px) and (max-width: 768px) {
    .npk-launcher-icons {
        grid-template-columns: repeat(6, 1fr);
    }
}
