/* CSS Variables & Theme */
:root {
    --primary: #2563eb;
    --primary-hover: #1d4ed8;
    --primary-light: #eff6ff;
    --primary-text: #ffffff;
    --danger: #ef4444;
    --danger-hover: #dc2626;
    --success: #10b981;
    --warning: #f59e0b;
    --bg-main: #f8fafc;
    --bg-card: #ffffff;
    --bg-card-hover: #f1f5f9;
    --text-main: #0f172a;
    --text-muted: #64748b;
    --border-color: #e2e8f0;
    --shadow-sm: 0 1px 2px 0 rgba(0, 0, 0, 0.05);
    --shadow-md: 0 4px 6px -1px rgba(0, 0, 0, 0.1), 0 2px 4px -2px rgba(0, 0, 0, 0.1);
    --shadow-lg: 0 10px 15px -3px rgba(0, 0, 0, 0.1), 0 4px 6px -4px rgba(0, 0, 0, 0.1);
    --radius-sm: 6px;
    --radius-md: 10px;
    --radius-lg: 16px;
    --transition: all 0.2s cubic-bezier(0.4, 0, 0.2, 1);
    --font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
}

@media (prefers-color-scheme: dark) {
    :root {
        --bg-main: #090d16;
        --bg-card: #131b2e;
        --bg-card-hover: #1c2744;
        --text-main: #f8fafc;
        --text-muted: #94a3b8;
        --border-color: #1e293b;
        --shadow-sm: 0 1px 2px 0 rgba(0, 0, 0, 0.3);
        --shadow-md: 0 4px 6px -1px rgba(0, 0, 0, 0.4);
        --shadow-lg: 0 10px 15px -3px rgba(0, 0, 0, 0.5);
    }
}

/* Base Reset & Box Sizing */
*, *::before, *::after {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
}

html, body {
    width: 100%;
    min-height: 100%;
    font-family: var(--font-family);
    background-color: var(--bg-main);
    color: var(--text-main);
    line-height: 1.5;
    -webkit-font-smoothing: antialiased;
    -webkit-tap-highlight-color: transparent;
}

/* Offline Banner */
.offline-banner {
    background-color: #f59e0b;
    color: #78350f;
    text-align: center;
    padding: 8px 16px;
    font-size: 0.875rem;
    font-weight: 600;
    position: sticky;
    top: 0;
    z-index: 999;
    box-shadow: var(--shadow-sm);
    animation: slideDown 0.3s ease-out;
}

.offline-banner.hidden {
    display: none;
}

@keyframes slideDown {
    from { transform: translateY(-100%); }
    to { transform: translateY(0); }
}

/* Toast Container */
#toast-container {
    position: fixed;
    top: max(20px, env(safe-area-inset-top));
    right: 20px;
    z-index: 2000;
    display: flex;
    flex-direction: column;
    gap: 8px;
    pointer-events: none;
}

.toast {
    background: #1e293b;
    color: #ffffff;
    padding: 12px 20px;
    border-radius: var(--radius-md);
    box-shadow: var(--shadow-lg);
    font-size: 0.9rem;
    pointer-events: auto;
    animation: fadeInToast 0.3s ease forwards;
    display: flex;
    align-items: center;
    gap: 10px;
    max-width: 90vw;
}

.toast.toast-error {
    background: var(--danger);
}

.toast.toast-success {
    background: var(--success);
}

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

/* Utility Helpers */
.hidden {
    display: none !important;
}

/* Buttons */
.btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 6px;
    padding: 8px 16px;
    min-height: 42px;
    font-size: 0.95rem;
    font-weight: 600;
    border: none;
    border-radius: var(--radius-md);
    cursor: pointer;
    transition: var(--transition);
    text-decoration: none;
    user-select: none;
    white-space: nowrap;
}

.btn:disabled {
    opacity: 0.5;
    cursor: not-allowed;
}

.btn-primary {
    background-color: var(--primary);
    color: var(--primary-text);
}

.btn-primary:not(:disabled):hover {
    background-color: var(--primary-hover);
    transform: translateY(-1px);
}

.btn-secondary {
    background-color: #0284c7;
    color: #ffffff;
}

.btn-secondary:not(:disabled):hover {
    background-color: #0369a1;
    transform: translateY(-1px);
}

.btn-outline {
    background-color: transparent;
    color: var(--text-main);
    border: 1px solid var(--border-color);
}

.btn-outline:hover {
    background-color: var(--bg-card-hover);
}

.btn-danger {
    background-color: transparent;
    color: var(--danger);
    border: 1px solid var(--danger);
}

.btn-danger:hover {
    background-color: var(--danger);
    color: #ffffff;
}

.btn-install {
    background: linear-gradient(135deg, #3b82f6, #8b5cf6);
    color: #ffffff;
    box-shadow: var(--shadow-sm);
}

.btn-install:hover {
    filter: brightness(1.1);
    transform: translateY(-1px);
}

.btn-block {
    width: 100%;
}

.icon-btn {
    background: transparent;
    border: none;
    color: inherit;
    cursor: pointer;
    font-size: 1.25rem;
    padding: 8px;
    border-radius: var(--radius-sm);
    display: inline-flex;
    align-items: center;
    justify-content: center;
    transition: var(--transition);
}

.icon-btn:hover {
    background-color: rgba(255, 255, 255, 0.1);
}

/* Authentication Card */
.auth-card {
    width: min(90vw, 380px);
    margin: 80px auto;
    padding: 32px 24px;
    background: var(--bg-card);
    border: 1px solid var(--border-color);
    border-radius: var(--radius-lg);
    box-shadow: var(--shadow-md);
    text-align: center;
    animation: fadeIn 0.4s ease;
}

.auth-header {
    margin-bottom: 24px;
}

.app-icon {
    border-radius: 14px;
    box-shadow: var(--shadow-sm);
    margin-bottom: 12px;
}

.auth-subtitle {
    color: var(--text-muted);
    font-size: 0.875rem;
    margin-top: 4px;
}

.input-group {
    text-align: left;
    margin-bottom: 16px;
}

.input-group label {
    display: block;
    font-size: 0.85rem;
    font-weight: 600;
    color: var(--text-muted);
    margin-bottom: 6px;
}

.input-group input {
    width: 100%;
    padding: 10px 14px;
    font-size: 1rem;
    border: 1px solid var(--border-color);
    border-radius: var(--radius-md);
    background-color: var(--bg-main);
    color: var(--text-main);
    outline: none;
    transition: var(--transition);
}

.input-group input:focus {
    border-color: var(--primary);
    box-shadow: 0 0 0 3px rgba(37, 99, 235, 0.15);
}

.error-msg {
    color: var(--danger);
    font-size: 0.875rem;
    margin-top: 12px;
    min-height: 20px;
}

/* App Layout & Header */
.app-layout {
    max-width: 1280px;
    margin: 0 auto;
    padding: 16px 20px;
    padding-top: max(16px, env(safe-area-inset-top));
    padding-bottom: max(32px, env(safe-area-inset-bottom));
}

.app-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 12px 0 20px 0;
    border-bottom: 1px solid var(--border-color);
    gap: 16px;
}

.brand {
    display: flex;
    align-items: center;
    gap: 12px;
}

.brand-icon {
    border-radius: 10px;
}

.brand-title {
    font-size: 1.35rem;
    font-weight: 700;
    line-height: 1.2;
}

.photo-stats {
    font-size: 0.8rem;
    color: var(--text-muted);
}

.header-actions {
    display: flex;
    align-items: center;
    gap: 10px;
}

/* Toolbar & Controls */
.toolbar-section {
    margin: 20px 0;
}

.toolbar-card {
    background-color: var(--bg-card);
    border: 1px solid var(--border-color);
    border-radius: var(--radius-lg);
    padding: 16px;
    box-shadow: var(--shadow-sm);
    display: flex;
    flex-direction: column;
    gap: 14px;
}

.upload-bar {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    gap: 10px;
}

.upload-bar input[type="file"] {
    display: none;
}

.file-picker-label {
    display: inline-flex;
    align-items: center;
    gap: 8px;
    padding: 9px 16px;
    background-color: var(--bg-main);
    color: var(--text-main);
    border: 1px dashed var(--border-color);
    border-radius: var(--radius-md);
    cursor: pointer;
    font-size: 0.9rem;
    transition: var(--transition);
    min-height: 42px;
    max-width: 240px;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.file-picker-label:hover {
    border-color: var(--primary);
    background-color: var(--bg-card-hover);
}

.path-input {
    flex: 1;
    min-width: 180px;
    padding: 10px 14px;
    font-size: 0.9rem;
    border: 1px solid var(--border-color);
    border-radius: var(--radius-md);
    background-color: var(--bg-main);
    color: var(--text-main);
    outline: none;
    transition: var(--transition);
    min-height: 42px;
}

.path-input:focus {
    border-color: var(--primary);
    box-shadow: 0 0 0 3px rgba(37, 99, 235, 0.15);
}

.filter-bar {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    justify-content: space-between;
    gap: 12px;
}

.search-box {
    position: relative;
    flex: 1;
    min-width: 200px;
}

.search-icon {
    position: absolute;
    left: 12px;
    top: 50%;
    transform: translateY(-50%);
    color: var(--text-muted);
    font-size: 0.9rem;
    pointer-events: none;
}

.search-box input {
    width: 100%;
    padding: 10px 14px 10px 36px;
    font-size: 0.9rem;
    border: 1px solid var(--border-color);
    border-radius: var(--radius-md);
    background-color: var(--bg-main);
    color: var(--text-main);
    outline: none;
    transition: var(--transition);
    min-height: 42px;
}

.search-box input:focus {
    border-color: var(--primary);
    box-shadow: 0 0 0 3px rgba(37, 99, 235, 0.15);
}

/* Gallery & Photo Grid (RWD) */
.gallery-section {
    min-height: 300px;
}

.photo-grid {
    display: grid;
    /* RWD fluid grid */
    grid-template-columns: repeat(auto-fill, minmax(220px, 1fr));
    gap: 20px;
}

.empty-state {
    text-align: center;
    padding: 60px 20px;
    color: var(--text-muted);
    font-size: 1.1rem;
}

.photo-card {
    background-color: var(--bg-card);
    border: 1px solid var(--border-color);
    border-radius: var(--radius-md);
    overflow: hidden;
    box-shadow: var(--shadow-sm);
    display: flex;
    flex-direction: column;
    transition: var(--transition);
    position: relative;
}

.photo-card:hover {
    transform: translateY(-3px);
    box-shadow: var(--shadow-md);
}

.photo-thumb-wrapper {
    position: relative;
    width: 100%;
    aspect-ratio: 4 / 3;
    background-color: #e2e8f0;
    cursor: pointer;
    overflow: hidden;
}

.photo-card img {
    width: 100%;
    height: 100%;
    object-fit: cover;
    display: block;
    transition: transform 0.3s ease;
}

.photo-card:hover img {
    transform: scale(1.03);
}

.photo-card-body {
    padding: 12px;
    display: flex;
    flex-direction: column;
    justify-content: space-between;
    flex: 1;
}

.photo-name {
    font-size: 0.875rem;
    font-weight: 500;
    color: var(--text-main);
    word-break: break-all;
    overflow: hidden;
    text-overflow: ellipsis;
    display: -webkit-box;
    -webkit-line-clamp: 2;
    -webkit-box-orient: vertical;
    margin-bottom: 10px;
    line-height: 1.35;
}

.photo-card .actions {
    display: flex;
    gap: 8px;
    margin-top: auto;
}

.photo-card .actions .btn {
    flex: 1;
    min-height: 36px;
    padding: 6px 10px;
    font-size: 0.85rem;
}

/* Fullscreen Carousel / Lightbox */
.carousel-overlay {
    position: fixed;
    top: 0;
    left: 0;
    width: 100vw;
    height: 100vh;
    background: rgba(0, 0, 0, 0.94);
    backdrop-filter: blur(10px);
    -webkit-backdrop-filter: blur(10px);
    z-index: 3000;
    display: flex;
    flex-direction: column;
    justify-content: space-between;
    padding: max(16px, env(safe-area-inset-top)) max(16px, env(safe-area-inset-right)) max(16px, env(safe-area-inset-bottom)) max(16px, env(safe-area-inset-left));
    animation: fadeIn 0.25s ease;
    touch-action: pan-y;
}

.carousel-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    color: #ffffff;
    z-index: 10;
}

.carousel-counter {
    font-size: 0.95rem;
    font-weight: 600;
    background: rgba(255, 255, 255, 0.15);
    padding: 4px 12px;
    border-radius: 20px;
}

.carousel-header-actions {
    display: flex;
    gap: 12px;
}

.carousel-header-actions .icon-btn {
    color: #ffffff;
    font-size: 1.4rem;
    width: 44px;
    height: 44px;
    border-radius: 50%;
    background: rgba(255, 255, 255, 0.15);
}

.carousel-header-actions .icon-btn:hover {
    background: rgba(255, 255, 255, 0.3);
}

.carousel-body {
    position: relative;
    flex: 1;
    display: flex;
    align-items: center;
    justify-content: center;
    overflow: hidden;
    margin: 10px 0;
}

.carousel-image-wrapper {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    max-width: 90vw;
    max-height: 80vh;
}

#carousel-img {
    max-width: 88vw;
    max-height: 75vh;
    object-fit: contain;
    border-radius: var(--radius-sm);
    box-shadow: 0 10px 30px rgba(0, 0, 0, 0.6);
    user-select: none;
    transition: transform 0.2s ease;
}

.carousel-caption {
    color: #f1f5f9;
    font-size: 0.9rem;
    margin-top: 10px;
    text-align: center;
    background: rgba(0, 0, 0, 0.5);
    padding: 4px 14px;
    border-radius: 12px;
    max-width: 80vw;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.nav-btn {
    background: rgba(255, 255, 255, 0.12);
    color: #ffffff;
    border: none;
    font-size: 2rem;
    cursor: pointer;
    width: 52px;
    height: 52px;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: var(--transition);
    user-select: none;
    z-index: 10;
}

.nav-btn:hover {
    background: rgba(255, 255, 255, 0.3);
}

.nav-btn.left {
    position: absolute;
    left: 20px;
}

.nav-btn.right {
    position: absolute;
    right: 20px;
}

.carousel-footer {
    text-align: center;
    color: rgba(255, 255, 255, 0.6);
    font-size: 0.8rem;
}

/* Modal Dialog */
.modal-overlay {
    position: fixed;
    top: 0; left: 0; width: 100vw; height: 100vh;
    background: rgba(0, 0, 0, 0.5);
    backdrop-filter: blur(4px);
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 4000;
    padding: 20px;
    animation: fadeIn 0.2s ease;
}

.modal-card {
    background: var(--bg-card);
    border: 1px solid var(--border-color);
    border-radius: var(--radius-lg);
    box-shadow: var(--shadow-lg);
    width: min(92vw, 440px);
    padding: 24px;
    animation: modalPop 0.25s cubic-bezier(0.16, 1, 0.3, 1);
}

.modal-title {
    font-size: 1.2rem;
    font-weight: 700;
    margin-bottom: 8px;
}

.modal-desc {
    color: var(--text-muted);
    font-size: 0.9rem;
    margin-bottom: 16px;
    word-break: break-all;
}

.modal-input-group {
    margin-bottom: 20px;
}

.modal-input {
    width: 100%;
    padding: 10px 14px;
    font-size: 0.95rem;
    border: 1px solid var(--border-color);
    border-radius: var(--radius-md);
    background: var(--bg-main);
    color: var(--text-main);
    outline: none;
}

.modal-input:focus {
    border-color: var(--primary);
    box-shadow: 0 0 0 3px rgba(37, 99, 235, 0.15);
}

.modal-actions {
    display: flex;
    justify-content: flex-end;
    gap: 10px;
}

/* Animations */
@keyframes fadeIn {
    from { opacity: 0; }
    to { opacity: 1; }
}

@keyframes modalPop {
    from { opacity: 0; transform: scale(0.95); }
    to { opacity: 1; transform: scale(1); }
}

/* ========================================================= */
/* RWD Responsive Media Queries                              */
/* ========================================================= */

/* Mobile Phones (<= 640px) */
@media (max-width: 640px) {
    .app-layout {
        padding: 12px 14px;
        padding-top: max(12px, env(safe-area-inset-top));
    }

    .brand-title {
        font-size: 1.15rem;
    }

    .header-actions .btn .btn-text {
        display: none; /* Icon-only on small screen */
    }

    .header-actions .btn {
        padding: 8px 10px;
        min-height: 38px;
    }

    .toolbar-card {
        padding: 12px;
        gap: 12px;
    }

    .upload-bar {
        flex-direction: column;
        align-items: stretch;
    }

    .file-picker-label {
        max-width: 100%;
        justify-content: center;
    }

    .path-input {
        width: 100%;
    }

    .upload-bar .btn {
        width: 100%;
    }

    .filter-bar {
        flex-direction: column;
        align-items: stretch;
    }

    .search-box {
        width: 100%;
    }

    .btn-carousel {
        width: 100%;
    }

    /* 2 columns on mobile */
    .photo-grid {
        grid-template-columns: repeat(auto-fill, minmax(140px, 1fr));
        gap: 12px;
    }

    .photo-card-body {
        padding: 8px;
    }

    .photo-name {
        font-size: 0.8rem;
        margin-bottom: 6px;
    }

    .photo-card .actions {
        flex-direction: column;
        gap: 4px;
    }

    .photo-card .actions .btn {
        padding: 5px;
        font-size: 0.78rem;
        min-height: 32px;
    }

    /* Fullscreen Carousel adjustments on mobile */
    .nav-btn {
        width: 42px;
        height: 42px;
        font-size: 1.4rem;
    }

    .nav-btn.left {
        left: 8px;
    }

    .nav-btn.right {
        right: 8px;
    }

    #carousel-img {
        max-width: 94vw;
        max-height: 70vh;
    }
}

/* Tablet (641px to 1024px) */
@media (min-width: 641px) and (max-width: 1024px) {
    .photo-grid {
        grid-template-columns: repeat(auto-fill, minmax(180px, 1fr));
        gap: 16px;
    }

    .file-picker-label {
        max-width: 200px;
    }
}

/* Standalone PWA Mode (Home Screen) enhancements */
@media (display-mode: standalone) {
    body {
        user-select: none;
    }
    
    .app-header {
        padding-top: 8px;
    }
}
