/* =========================================
   1. RESET & BASE STYLES
   ========================================= */
* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
    transition: background-color 0.3s ease, color 0.3s ease, border-color 0.3s ease;
}

:root {
    /* Light Theme Variables */
    --bg-color: #f0f2f5;
    --container-bg: #ffffff;
    --text-color: #333333;
    --text-secondary: #666666;
    --primary-color: #1a73e8;
    --primary-hover: #1557b0;
    --success-color: #28a745;
    --warning-color: #f39c12;
    --error-color: #d32f2f;
    --border-color: #dddddd;
    --input-bg: #ffffff;
    --shadow-color: rgba(0, 0, 0, 0.1);
    --gallery-bg: rgba(0, 0, 0, 0.02);
}

.dark-mode {
    /* Dark Theme Variables */
    --bg-color: #121212;
    --container-bg: #1e1e1e;
    --text-color: #e0e0e0;
    --text-secondary: #aaaaaa;
    --primary-color: #64b5f6;
    --primary-hover: #42a5f5;
    --success-color: #81c784;
    --warning-color: #ffb74d;
    --error-color: #e57373;
    --border-color: #444444;
    --input-bg: #2d2d2d;
    --shadow-color: rgba(0, 0, 0, 0.5);
    --gallery-bg: rgba(255, 255, 255, 0.05);
}

body {
    font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
    background: var(--bg-color);
    color: var(--text-color);
    padding: 20px;
    line-height: 1.5;
    min-height: 100vh;
}

/* =========================================
   2. MAIN CONTAINER
   ========================================= */
.container {
    max-width: 600px;
    margin: 0 auto;
    background: var(--container-bg);
    padding: 30px;
    border-radius: 12px;
    box-shadow: 0 4px 20px var(--shadow-color);
    position: relative;
}

h2 {
    text-align: center;
    color: var(--primary-color);
    margin-bottom: 25px;
    font-weight: 700;
}

/* =========================================
   3. FORM ELEMENTS
   ========================================= */
select, 
input[type="text"], 
input[type="date"] {
    width: 100%;
    padding: 12px 15px;
    margin-bottom: 15px;
    border: 1px solid var(--border-color);
    border-radius: 8px;
    background: var(--input-bg);
    color: var(--text-color);
    font-size: 14px;
}

select:focus, 
input:focus {
    outline: none;
    border-color: var(--primary-color);
    box-shadow: 0 0 0 3px rgba(26, 115, 232, 0.2);
}

.checkbox-label {
    display: flex;
    align-items: center;
    gap: 10px;
    margin-bottom: 20px;
    cursor: pointer;
    user-select: none;
    font-size: 14px;
}

input[type="checkbox"] {
    width: 18px;
    height: 18px;
    cursor: pointer;
}

/* =========================================
   4. DROP ZONE (UPLOAD AREA)
   ========================================= */
.drop-zone {
    border: 2px dashed var(--primary-color);
    border-radius: 12px;
    padding: 40px 20px;
    text-align: center;
    cursor: pointer;
    background: rgba(26, 115, 232, 0.05);
    margin-bottom: 25px;
    transition: all 0.3s ease;
}

.drop-zone:hover, 
.drop-zone.dragover {
    background: rgba(26, 115, 232, 0.15);
    transform: translateY(-2px);
    box-shadow: 0 5px 15px var(--shadow-color);
}

.drop-zone-content i {
    font-size: 48px;
    color: var(--primary-color);
    margin-bottom: 15px;
    display: block;
}

.drop-zone-content p {
    font-weight: 600;
    margin-bottom: 5px;
}

.drop-zone-content small {
    color: var(--text-secondary);
}

/* =========================================
   5. PREVIEW AREA
   ========================================= */
.preview-container {
    display: flex;
    gap: 12px;
    flex-wrap: wrap;
    margin-bottom: 20px;
}

.preview-item {
    position: relative;
    width: 90px;
    height: 90px;
    border-radius: 8px;
    overflow: hidden;
    border: 1px solid var(--border-color);
    box-shadow: 0 2px 5px var(--shadow-color);
}

.preview {
    width: 100%;
    height: 100%;
    object-fit: cover;
}

.remove-preview {
    position: absolute;
    top: 4px;
    right: 4px;
    width: 22px;
    height: 22px;
    background: rgba(211, 47, 47, 0.9);
    color: white;
    border: none;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    font-size: 14px;
    padding: 0;
    transition: transform 0.2s;
}

.remove-preview:hover {
    transform: scale(1.1);
    background: #d32f2f;
}

/* =========================================
   6. PROGRESS BAR
   ========================================= */
.progress-container {
    margin-bottom: 25px;
    background: var(--bg-color);
    border-radius: 8px;
    padding: 15px;
    border: 1px solid var(--border-color);
}

.progress-header {
    display: flex;
    justify-content: space-between;
    margin-bottom: 8px;
    font-size: 13px;
    font-weight: 600;
    color: var(--text-secondary);
}

.progress-bar {
    height: 10px;
    background: var(--border-color);
    border-radius: 5px;
    overflow: hidden;
}

.progress-fill {
    height: 100%;
    background: linear-gradient(90deg, var(--primary-color), var(--success-color));
    width: 0%;
    transition: width 0.3s ease;
    border-radius: 5px;
}

/* =========================================
   7. BUTTONS
   ========================================= */
.button-group {
    display: flex;
    gap: 12px;
    flex-wrap: wrap;
}

button {
    flex: 1;
    min-width: 120px;
    padding: 12px;
    border: none;
    border-radius: 8px;
    cursor: pointer;
    font-weight: 600;
    font-size: 14px;
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
    color: white;
    transition: transform 0.2s, filter 0.2s;
    box-shadow: 0 2px 4px var(--shadow-color);
}

button:hover:not(:disabled) {
    filter: brightness(1.1);
    transform: translateY(-2px);
    box-shadow: 0 4px 8px var(--shadow-color);
}

button:active:not(:disabled) {
    transform: translateY(0);
}

button:disabled {
    opacity: 0.6;
    cursor: not-allowed;
    filter: grayscale(0.5);
}

/* Button Colors */
#uploadButton { background: var(--primary-color); }
#historyMenu { background: var(--success-color); }
#checkMenu { background: var(--warning-color); }
#cancelButton { background: var(--error-color); flex: 0 0 auto; }
#fetchHistory, #fetchCheck { background: var(--primary-color); width: auto; flex: 0 0 auto; }

/* =========================================
   8. SHORTCUTS & THEME TOGGLE
   ========================================= */
.shortcuts-help {
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    color: white;
    padding: 12px 15px;
    border-radius: 8px;
    margin-bottom: 25px;
    font-size: 13px;
    display: flex;
    gap: 10px;
    align-items: center;
    box-shadow: 0 4px 10px rgba(102, 126, 234, 0.3);
}

.theme-toggle-container {
    position: absolute;
    top: 20px;
    right: 20px;
}

#themeToggle {
    background: transparent;
    border: 1px solid var(--border-color);
    border-radius: 50%;
    width: 40px;
    height: 40px;
    padding: 0;
    font-size: 20px;
    color: var(--text-color);
    box-shadow: none;
    min-width: auto;
    flex: none;
}

#themeToggle:hover {
    background: var(--bg-color);
}

/* =========================================
   9. POPUP SYSTEM
   ========================================= */
.popup {
    display: none;
    position: fixed;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%) scale(0.95);
    background: var(--container-bg);
    padding: 25px;
    border-radius: 12px;
    box-shadow: 0 15px 50px rgba(0, 0, 0, 0.5);
    z-index: 1000;
    width: 95%;
    max-width: 800px; /* Rộng hơn để chứa Gallery */
    opacity: 0;
    transition: opacity 0.3s ease, transform 0.3s ease;
    max-height: 90vh;
    overflow-y: auto;
    border: 1px solid var(--border-color);
}

.popup.active {
    display: block;
    opacity: 1;
    transform: translate(-50%, -50%) scale(1);
}

.popup h3 {
    border-bottom: 1px solid var(--border-color);
    padding-bottom: 15px;
    margin-bottom: 20px;
    color: var(--text-color);
    font-size: 18px;
}

/* Popup Overlay Background (Optional) */
.popup::before {
    content: '';
    position: fixed;
    top: -100vh;
    left: -100vw;
    width: 300vw;
    height: 300vh;
    background: rgba(0, 0, 0, 0.5);
    z-index: -1;
    pointer-events: none; /* Để click xuyên qua nếu cần xử lý JS đóng */
}

/* =========================================
   10. TABLES
   ========================================= */
.table-container {
    overflow-x: auto;
    margin: 15px 0;
    border: 1px solid var(--border-color);
    border-radius: 8px;
    background: var(--input-bg);
}

table {
    width: 100%;
    border-collapse: collapse;
    font-size: 14px;
}

th, td {
    padding: 12px 15px;
    text-align: left;
    border-bottom: 1px solid var(--border-color);
    color: var(--text-color);
}

th {
    background: rgba(0, 0, 0, 0.03);
    font-weight: 600;
    color: var(--primary-color);
    white-space: nowrap;
}

tr:last-child td {
    border-bottom: none;
}

.action-preview {
    background: #6c757d;
    padding: 5px 10px;
    font-size: 12px;
    min-width: auto;
    width: auto;
    display: inline-flex;
}

/* =========================================
   11. GALLERY GRID (QUAN TRỌNG)
   ========================================= */
.gallery-container {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(110px, 1fr)); /* Tự động chia cột */
    gap: 12px;
    max-height: 400px;
    overflow-y: auto;
    padding: 15px;
    background: var(--gallery-bg);
    border: 1px solid var(--border-color);
    border-radius: 8px;
    margin-top: 15px;
}

.gallery-item {
    position: relative;
    aspect-ratio: 1 / 1; /* Hình vuông */
    background: var(--input-bg);
    border: 1px solid var(--border-color);
    border-radius: 8px;
    overflow: hidden;
    cursor: pointer;
    transition: transform 0.2s, border-color 0.2s;
}

.gallery-item:hover {
    transform: scale(1.05);
    border-color: var(--primary-color);
    z-index: 10;
    box-shadow: 0 4px 12px var(--shadow-color);
}

/* Ảnh thật */
.gallery-image {
    width: 100%;
    height: 100%;
    object-fit: cover;
    display: block;
    animation: fadeIn 0.3s ease;
}

/* Loading Spinner Placeholder */
.gallery-loading {
    height: 100%;
    width: 100%;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    color: var(--text-secondary);
    font-size: 11px;
    background: var(--input-bg);
    padding: 5px;
    text-align: center;
}

.gallery-loading i {
    font-size: 24px;
    margin-bottom: 8px;
    color: var(--primary-color);
    animation: spin 1s linear infinite;
}

/* Tên file đè lên ảnh */
.gallery-filename {
    position: absolute;
    bottom: 0;
    left: 0;
    right: 0;
    background: rgba(0, 0, 0, 0.7);
    color: white;
    font-size: 10px;
    padding: 4px;
    text-align: center;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

/* =========================================
   12. MODAL PREVIEW (ZOOM ẢNH)
   ========================================= */
.modal {
    display: none;
    position: fixed;
    z-index: 2000;
    left: 0;
    top: 0;
    width: 100%;
    height: 100%;
    background-color: rgba(0, 0, 0, 0.9);
    backdrop-filter: blur(5px);
}

.modal-content {
    position: relative;
    background-color: transparent;
    margin: 0 auto;
    padding: 0;
    width: 100%;
    height: 100%;
    display: flex;
    align-items: center;
    justify-content: center;
    flex-direction: column;
}

.modal-body {
    position: relative;
    max-width: 90%;
    max-height: 90%;
}

.modal img {
    max-width: 100%;
    max-height: 85vh;
    border-radius: 4px;
    box-shadow: 0 0 20px rgba(0,0,0,0.5);
    object-fit: contain;
}

.close {
    position: absolute;
    top: 20px;
    right: 30px;
    color: #f1f1f1;
    font-size: 40px;
    font-weight: bold;
    transition: 0.3s;
    cursor: pointer;
    z-index: 2001;
}

.close:hover,
.close:focus {
    color: var(--primary-color);
    text-decoration: none;
    cursor: pointer;
}

#modalTitle {
    color: white;
    margin-top: 10px;
    font-size: 16px;
    font-weight: normal;
}

/* =========================================
   13. TOAST NOTIFICATIONS
   ========================================= */
.toast-container {
    position: fixed;
    top: 20px;
    right: 20px;
    z-index: 3000;
    display: flex;
    flex-direction: column;
    gap: 10px;
}

.toast {
    background: white;
    padding: 15px 20px;
    border-radius: 8px;
    box-shadow: 0 5px 15px rgba(0, 0, 0, 0.2);
    display: flex;
    align-items: center;
    gap: 12px;
    min-width: 300px;
    transform: translateX(120%);
    transition: transform 0.3s cubic-bezier(0.68, -0.55, 0.27, 1.55);
    border-left: 5px solid #333;
    font-size: 14px;
    color: #333;
}

.toast.show {
    transform: translateX(0);
}

.toast-success { border-left-color: var(--success-color); }
.toast-error { border-left-color: var(--error-color); }
.toast-warning { border-left-color: var(--warning-color); }

.toast i { font-size: 20px; }
.toast-success i { color: var(--success-color); }
.toast-error i { color: var(--error-color); }
.toast-warning i { color: var(--warning-color); }

/* =========================================
   14. ANIMATIONS & MEDIA QUERIES
   ========================================= */
@keyframes spin { 100% { transform: rotate(360deg); } }
@keyframes fadeIn { from { opacity: 0; } to { opacity: 1; } }

/* Scrollbar Customization */
::-webkit-scrollbar { width: 8px; height: 8px; }
::-webkit-scrollbar-track { background: var(--bg-color); }
::-webkit-scrollbar-thumb { background: #bbb; border-radius: 4px; }
::-webkit-scrollbar-thumb:hover { background: #999; }

.dark-mode ::-webkit-scrollbar-thumb { background: #555; }
.dark-mode ::-webkit-scrollbar-thumb:hover { background: #777; }

/* Mobile Responsiveness */
@media (max-width: 768px) {
    .container {
        padding: 15px;
        margin: 10px;
        width: auto;
    }
    
    .button-group button {
        flex: 1 0 45%; /* 2 button per row */
    }
    
    .popup {
        width: 95%;
        padding: 15px;
    }
    
    .gallery-container {
        grid-template-columns: repeat(auto-fill, minmax(80px, 1fr));
    }

    .shortcuts-help {
        display: none; /* Hide on mobile */
    }
}
