/* ========================================================
   Nay Chi Clinic — Professional Medical Image System
   ======================================================== */

:root {
    --primary: #1d4ed8;
    --primary-hover: #1e40af;
    --primary-light: #eff6ff;
    --primary-border: #bfdbfe;
    --danger: #dc2626;
    --danger-hover: #b91c1c;
    --success: #16a34a;
    --success-light: #f0fdf4;
    --error-light: #fef2f2;
    --gray-50: #f8fafc;
    --gray-100: #f1f5f9;
    --gray-200: #e2e8f0;
    --gray-300: #cbd5e1;
    --gray-400: #94a3b8;
    --gray-500: #64748b;
    --gray-600: #475569;
    --gray-700: #334155;
    --gray-800: #1e293b;
    --gray-900: #0f172a;
    --radius: 8px;
    --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 -1px rgba(0, 0, 0, 0.1);
    --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);
}

*, *::before, *::after {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
}

body {
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
    background: var(--gray-50);
    color: var(--gray-800);
    line-height: 1.5;
    min-height: 100vh;
    display: flex;
    flex-direction: column;
}

/* === Icons === */
.icon {
    display: inline-block;
    vertical-align: middle;
    flex-shrink: 0;
}

.nav-icon {
    margin-right: 0; /* Gap handles spacing now */
    vertical-align: middle;
    flex-shrink: 0;
    display: inline-block;
}

.user-icon {
    margin-right: 0.25rem;
    vertical-align: -2px;
}

/* === Login Page === */
.login-page {
    background: linear-gradient(135deg, #0f172a 0%, #1e3a8a 50%, #1d4ed8 100%);
    display: flex;
    align-items: center;
    justify-content: center;
    min-height: 100vh;
    padding: 1.5rem;
}

.login-container {
    width: 100%;
    max-width: 420px;
}

.login-card {
    background: #ffffff;
    border-radius: 12px;
    padding: 2.5rem 2rem;
    box-shadow: 0 25px 50px -12px rgba(0, 0, 0, 0.25);
    border: 1px solid rgba(255, 255, 255, 0.1);
}

.login-header {
    text-align: center;
    margin-bottom: 2rem;
}

.login-logo-mark {
    width: 60px;
    height: 60px;
    background: var(--primary-light);
    color: var(--primary);
    border-radius: 50%;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    margin-bottom: 1rem;
    border: 2px solid var(--primary-border);
}

.login-header h1 {
    font-size: 1.65rem;
    font-weight: 700;
    color: var(--gray-900);
    letter-spacing: -0.025em;
    margin-bottom: 0.25rem;
}

.login-header p {
    color: var(--gray-500);
    font-size: 0.875rem;
}

/* === App Shell Layout (Desktop & Mobile) === */
.app-body {
    overflow: hidden; /* App takes full screen, scroll inside content */
    background: var(--gray-50);
}

.app-layout {
    display: flex;
    height: 100vh;
    width: 100vw;
}

/* === Desktop Sidebar === */
.app-sidebar {
    width: 250px;
    background: #ffffff;
    border-right: 1px solid var(--gray-200);
    display: flex;
    flex-direction: column;
    z-index: 1000;
    flex-shrink: 0;
}

.sidebar-brand {
    padding: 1.5rem 1.25rem;
    border-bottom: 1px solid var(--gray-100);
}

.sidebar-brand .brand-link {
    display: flex;
    align-items: center;
    gap: 0.75rem;
    text-decoration: none;
    color: var(--primary);
}

.sidebar-brand .brand-text {
    font-size: 1.25rem;
    font-weight: 700;
    letter-spacing: -0.02em;
}

.sidebar-nav {
    flex: 1;
    padding: 1.5rem 0.75rem;
    display: flex;
    flex-direction: column;
    gap: 0.5rem;
}

.sidebar-footer {
    padding: 1rem 0.75rem;
    border-top: 1px solid var(--gray-100);
}

.nav-item {
    display: flex;
    align-items: center;
    gap: 0.75rem;
    padding: 0.75rem 1rem;
    color: var(--gray-600);
    text-decoration: none;
    font-size: 0.95rem;
    font-weight: 500;
    border-radius: var(--radius);
    transition: all 0.2s ease;
}

.nav-item:hover {
    background: var(--gray-50);
    color: var(--gray-900);
}

.nav-item.active {
    background: var(--primary-light);
    color: var(--primary);
    font-weight: 600;
}

.nav-item.text-danger:hover {
    background: var(--error-light);
    color: var(--danger);
}

/* === Main Content Area === */
.app-main {
    flex: 1;
    display: flex;
    flex-direction: column;
    height: 100vh;
    overflow: hidden;
    position: relative;
}

.app-header-mobile {
    display: none; /* hidden on desktop */
}

.app-content-scroll {
    flex: 1;
    overflow-y: auto;
    padding: 2rem;
}

.container {
    max-width: 1000px;
    margin: 0 auto;
    width: 100%;
}

.page-title-row {
    margin-bottom: 1.5rem;
    display: flex;
    align-items: center;
    justify-content: space-between;
}

.page-title-row h2 {
    font-size: 1.75rem;
    font-weight: 700;
    color: var(--gray-900);
    letter-spacing: -0.03em;
}

/* === Cards === */
.card {
    background: #ffffff;
    border-radius: var(--radius);
    border: 1px solid var(--gray-200);
    box-shadow: var(--shadow);
    padding: 1.5rem;
    margin-bottom: 1.5rem;
}

.card-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin-bottom: 1rem;
}

.card h2 {
    font-size: 1.25rem;
    font-weight: 700;
    color: var(--gray-900);
    margin-bottom: 0.5rem;
}

.card h3 {
    font-size: 1.05rem;
    font-weight: 600;
    color: var(--gray-800);
    display: flex;
    align-items: center;
    gap: 0.5rem;
}

.card hr {
    border: none;
    border-top: 1px solid var(--gray-200);
    margin: 1.25rem 0;
}

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

.form-group label {
    display: block;
    font-weight: 600;
    font-size: 0.875rem;
    color: var(--gray-700);
    margin-bottom: 0.35rem;
}

.form-group input[type="text"],
.form-group input[type="password"],
.form-group input[type="email"],
.form-group select,
.form-select {
    width: 100%;
    padding: 0.625rem 0.875rem;
    border: 1px solid var(--gray-300);
    border-radius: var(--radius);
    font-size: 0.9375rem;
    color: var(--gray-900);
    background-color: #ffffff;
    transition: border-color 0.15s ease, box-shadow 0.15s ease;
    outline: none;
}

.form-group input:focus,
.form-group select:focus,
.form-select:focus {
    border-color: var(--primary);
    box-shadow: 0 0 0 3px rgba(29, 78, 216, 0.15);
}

/* === Buttons === */
.btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 0.45rem;
    padding: 0.625rem 1.25rem;
    font-size: 0.9375rem;
    font-weight: 600;
    border: 1px solid transparent;
    border-radius: var(--radius);
    cursor: pointer;
    text-decoration: none;
    transition: all 0.15s ease;
    white-space: nowrap;
    line-height: 1.25;
}

.btn-sm {
    padding: 0.4rem 0.75rem;
    font-size: 0.8125rem;
    border-radius: 6px;
}

.btn-lg {
    padding: 0.75rem 1.75rem;
    font-size: 1rem;
}

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

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

.btn-outline {
    background: #ffffff;
    color: var(--gray-700);
    border-color: var(--gray-300);
}

.btn-outline:hover {
    background: var(--gray-50);
    border-color: var(--gray-400);
    color: var(--gray-900);
}

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

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

.btn-block {
    width: 100%;
}

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

/* === Alerts === */
.alert {
    padding: 0.875rem 1.15rem;
    border-radius: var(--radius);
    font-size: 0.9rem;
    margin-bottom: 1.25rem;
    font-weight: 500;
    display: flex;
    align-items: center;
    gap: 0.6rem;
}

.alert-error {
    background: var(--error-light);
    color: var(--danger);
    border: 1px solid #fecaca;
}

.alert-success {
    background: var(--success-light);
    color: var(--success);
    border: 1px solid #bbf7d0;
}

/* === Upload Area === */
.upload-section {
    border: 2px dashed var(--gray-300);
    background: var(--gray-50);
    transition: all 0.2s ease;
    border-radius: var(--radius);
}

.upload-section.drag-over {
    border-color: var(--primary);
    background: var(--primary-light);
}

.upload-area {
    text-align: center;
    padding: 3rem 1.5rem;
    cursor: pointer;
}

.upload-icon-svg {
    color: var(--primary);
    margin-bottom: 0.75rem;
    display: inline-flex;
    align-items: center;
    justify-content: center;
}

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

.upload-area .link {
    color: var(--primary);
    cursor: pointer;
    font-weight: 600;
    text-decoration: underline;
}

.upload-preview {
    margin-top: 1.25rem;
}

.upload-preview img {
    max-width: 240px;
    max-height: 180px;
    border-radius: var(--radius);
    border: 2px solid var(--gray-300);
    box-shadow: var(--shadow-sm);
}

/* === Gallery Section === */
.gallery-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    flex-wrap: wrap;
    gap: 1rem;
    margin-bottom: 1.5rem;
    padding-bottom: 1rem;
    border-bottom: 1px solid var(--gray-200);
}

.search-form {
    display: flex;
    gap: 0.5rem;
    align-items: center;
}

.search-form input[type="text"] {
    padding: 0.45rem 0.85rem;
    border: 1px solid var(--gray-300);
    border-radius: var(--radius);
    font-size: 0.875rem;
    width: 240px;
    outline: none;
}

.search-form input:focus {
    border-color: var(--primary);
    box-shadow: 0 0 0 3px rgba(29, 78, 216, 0.15);
}

.image-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(260px, 1fr));
    gap: 1.25rem;
}

.image-card {
    background: #ffffff;
    border: 1px solid var(--gray-200);
    border-radius: var(--radius);
    overflow: hidden;
    transition: transform 0.15s ease, box-shadow 0.15s ease;
    display: flex;
    flex-direction: column;
}

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

.image-thumb {
    width: 100%;
    aspect-ratio: 16 / 11;
    overflow: hidden;
    background: var(--gray-100);
}

.image-thumb a {
    display: block;
    width: 100%;
    height: 100%;
}

.image-thumb img {
    width: 100%;
    height: 100%;
    object-fit: cover;
    display: block;
    transition: transform 0.2s ease;
}

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

.image-info {
    padding: 0.875rem;
    flex: 1;
}

.image-name {
    font-weight: 600;
    font-size: 0.9rem;
    color: var(--gray-900);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    margin-bottom: 0.25rem;
}

.image-actions {
    display: flex;
    gap: 0.4rem;
    padding: 0 0.875rem 0.875rem;
    flex-wrap: wrap;
    border-top: 1px solid var(--gray-100);
    padding-top: 0.65rem;
}

.inline-form {
    display: inline;
}

/* === Empty State === */
.empty-state {
    text-align: center;
    padding: 3.5rem 1rem;
    color: var(--gray-500);
}

/* === Pagination === */
.pagination {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 1rem;
    margin-top: 1.75rem;
    padding-top: 1.5rem;
    border-top: 1px solid var(--gray-200);
}

.page-info {
    color: var(--gray-600);
    font-size: 0.875rem;
    font-weight: 500;
}

/* === View Page Layout === */
.view-layout {
    display: grid;
    grid-template-columns: 1fr 340px;
    gap: 1.5rem;
    align-items: start;
}

.view-image-container {
    background: #ffffff;
    border: 1px solid var(--gray-200);
    border-radius: var(--radius);
    box-shadow: var(--shadow);
    padding: 1rem;
    text-align: center;
}

.view-image {
    max-width: 100%;
    max-height: 80vh;
    border-radius: 4px;
    box-shadow: var(--shadow-sm);
}

.view-sidebar .card {
    position: sticky;
    top: 80px;
}

.qr-container {
    display: flex;
    justify-content: center;
    padding: 0.75rem 0;
}

.qr-container canvas,
.qr-container img {
    border-radius: 6px;
    box-shadow: var(--shadow-sm);
}

.link-box {
    display: flex;
    gap: 0.4rem;
    margin-top: 0.35rem;
    margin-bottom: 0.75rem;
}

.link-input {
    flex: 1;
    padding: 0.45rem 0.65rem;
    border: 1px solid var(--gray-300);
    border-radius: var(--radius);
    font-size: 0.78rem;
    color: var(--gray-700);
    background: var(--gray-50);
    outline: none;
    min-width: 0;
    font-family: monospace;
}

/* === Spinner === */
.spinner {
    display: inline-block;
    width: 16px;
    height: 16px;
    border: 2px solid rgba(255,255,255,0.3);
    border-radius: 50%;
    border-top-color: #ffffff;
    animation: spin 0.8s linear infinite;
    vertical-align: middle;
}

@keyframes spin {
    to { transform: rotate(360deg); }
}

/* === Utilities === */
.text-muted { color: var(--gray-500); }
.text-sm { font-size: 0.8125rem; }

/* ========================================================
   Mobile App Layout (Bottom Navigation)
   ======================================================== */
@media (max-width: 860px) {
    .app-body {
        overflow: visible;
    }
    
    .app-layout {
        flex-direction: column;
        height: auto;
        min-height: 100vh;
    }

    /* Transform Sidebar into Bottom Bar */
    .app-sidebar {
        position: fixed;
        bottom: 0;
        left: 0;
        right: 0;
        width: 100%;
        height: calc(65px + env(safe-area-inset-bottom));
        border-right: none;
        border-top: 1px solid var(--gray-200);
        flex-direction: row;
        padding: 0 0.5rem;
        padding-bottom: env(safe-area-inset-bottom);
        box-shadow: 0 -4px 20px rgba(0, 0, 0, 0.05);
        background: rgba(255, 255, 255, 0.95);
        backdrop-filter: blur(10px);
    }

    .sidebar-brand { display: none; }
    .sidebar-footer { display: none; } /* Hide logout on bottom nav */

    .sidebar-nav {
        flex: 1;
        flex-direction: row;
        justify-content: space-around;
        align-items: stretch;
        padding: 0;
        gap: 0;
    }

    .nav-item {
        flex: 1;
        flex-direction: column;
        justify-content: center;
        padding: 0.4rem 0;
        gap: 0.25rem;
        font-size: 0.65rem;
        border-radius: 0;
    }

    .nav-item.active {
        background: transparent;
        position: relative;
    }
    
    .nav-item.active::after {
        content: "";
        position: absolute;
        top: -1px;
        left: 50%;
        transform: translateX(-50%);
        width: 30px;
        height: 3px;
        background: var(--primary);
        border-radius: 0 0 4px 4px;
    }

    /* Mobile Header */
    .app-main {
        height: auto;
        overflow: visible;
    }

    .app-header-mobile {
        display: flex;
        align-items: center;
        justify-content: space-between;
        padding: 0 1.25rem;
        height: 60px;
        background: #ffffff;
        border-bottom: 1px solid var(--gray-200);
        position: sticky;
        top: 0;
        z-index: 900;
    }

    .mobile-brand {
        display: flex;
        align-items: center;
        gap: 0.5rem;
        color: var(--primary);
        font-weight: 700;
        font-size: 1.1rem;
    }

    .user-avatar {
        width: 32px;
        height: 32px;
        background: var(--primary-light);
        color: var(--primary);
        border-radius: 50%;
        display: flex;
        align-items: center;
        justify-content: center;
        font-weight: 700;
        font-size: 0.9rem;
    }

    .app-content-scroll {
        padding: 1.25rem 1rem 85px 1rem;
        overflow: visible;
    }

    .page-title-row {
        margin-bottom: 1rem;
    }

    .page-title-row h2 {
        font-size: 1.35rem;
    }

    .view-layout {
        grid-template-columns: 1fr;
    }

    .view-sidebar .card {
        position: static;
    }

    .gallery-header {
        flex-direction: column;
        align-items: stretch;
    }

    .search-form {
        width: 100%;
    }

    .search-form input[type="text"] {
        flex: 1;
        width: auto;
    }

    .image-grid {
        grid-template-columns: repeat(auto-fill, minmax(160px, 1fr));
        gap: 1rem;
    }
}

@media (max-width: 480px) {
    .image-grid {
        grid-template-columns: 1fr;
    }
    .card {
        padding: 1.25rem;
    }
}
