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

body {
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, sans-serif;
    background: linear-gradient(135deg, #2351ac 0%, #2bba77 100%);
    min-height: 100vh;
    color: #333;
}

.screen {
    min-height: 100vh;
    display: flex;
    align-items: center;
    justify-content: center;
}

.hidden {
    display: none !important;
}

/* ─── Login Screen ─────────────────────────────────────────── */
.login-container {
    background: white;
    padding: 40px;
    border-radius: 10px;
    box-shadow: 0 10px 40px rgba(0, 0, 0, 0.2);
    width: 100%;
    max-width: 400px;
}

.login-logo {
    max-width: 250px;
    width: 100%;
    height: auto;
    margin: 0 auto 24px;
    display: block;
}

/* Login Tabs */
.login-tabs {
    display: flex;
    border-bottom: 2px solid #e1e8ed;
    margin-bottom: 28px;
}

.login-tab-btn {
    flex: 1;
    padding: 10px 0;
    background: none;
    border: none;
    border-bottom: 3px solid transparent;
    margin-bottom: -2px;
    font-size: 14px;
    font-weight: 600;
    color: #888;
    cursor: pointer;
    transition: all 0.2s;
}

.login-tab-btn:hover {
    color: #555;
}

.login-tab-btn.active {
    color: #2351ac;
    border-bottom-color: #2351ac;
}

/* ─── Form Elements ─────────────────────────────────────────── */
.form-group {
    margin-bottom: 20px;
}

.form-group label {
    display: block;
    margin-bottom: 8px;
    font-weight: 500;
    color: #555;
}

.form-group input {
    width: 100%;
    padding: 12px;
    border: 2px solid #e1e8ed;
    border-radius: 6px;
    font-size: 14px;
    transition: border-color 0.3s;
}

.form-group input:focus {
    outline: none;
    border-color: #2351ac;
}

/* ─── Buttons ───────────────────────────────────────────────── */
.btn {
    padding: 12px 24px;
    border: none;
    border-radius: 6px;
    font-size: 14px;
    font-weight: 600;
    cursor: pointer;
    transition: all 0.3s;
}

.btn-primary {
    background: #6c757d;
    color: white;
    width: 100%;
}

.btn-primary:hover {
    background: #555;
}

.btn-primary:disabled {
    background: #adb5bd;
    cursor: not-allowed;
    opacity: 0.65;
}

.btn-secondary {
    background: #6c757d;
    color: white;
}

.btn-secondary:hover {
    background: #5a6268;
}

.btn-control {
    background: #5568d3;
    color: white;
    padding: 10px 20px;
}

.btn-control:hover {
    background: #5568d3;
}

.btn-search {
    background: #2351ac;
    color: white;
    padding: 10px 20px;
    width: auto;
    border-radius: 6px;
    flex-shrink: 0;
}

.btn-search:hover {
    background: #1a3d87;
}

.error-message {
    color: #dc3545;
    font-size: 14px;
    margin-top: 10px;
    text-align: center;
}

/* ─── Upload Toast ──────────────────────────────────────────── */
.upload-toast {
    position: fixed;
    bottom: 24px;
    left: 50%;
    transform: translateX(-50%);
    padding: 14px 28px;
    border-radius: 8px;
    font-size: 14px;
    font-weight: 500;
    z-index: 9999;
    box-shadow: 0 4px 20px rgba(0,0,0,0.18);
    transition: opacity 0.3s ease-out;
    text-align: center;
}

.upload-toast.success {
    background: #d4edda;
    color: #155724;
    border: 1px solid #c3e6cb;
}

.upload-toast.error {
    background: #f8d7da;
    color: #721c24;
    border: 1px solid #f5c6cb;
}

.upload-toast.info {
    background: #cce5ff;
    color: #004085;
    border: 1px solid #b8daff;
}

.upload-toast.fade-out {
    opacity: 0;
}

/* ─── Dashboard ─────────────────────────────────────────────── */
#dashboard-screen {
    display: block;
    align-items: flex-start;
    padding: 0;
}

.header {
    background: white;
    padding: 16px 24px;
    box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
    display: flex;
    justify-content: space-between;
    align-items: center;
}

.dashboard-logo {
    max-width: 200px;
    width: 100%;
    height: auto;
}

.header-right {
    display: flex;
    align-items: center;
    gap: 16px;
}

.user-info {
    display: flex;
    align-items: center;
    gap: 15px;
}

.user-info span {
    color: #555;
    font-weight: 500;
}

.user-info .btn {
    width: auto;
}

/* ─── Hamburger Menu ────────────────────────────────────────── */
.hamburger-menu {
    position: relative;
}

.hamburger-btn {
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    gap: 5px;
    width: 40px;
    height: 40px;
    background: none;
    border: 2px solid #ddd;
    border-radius: 6px;
    cursor: pointer;
    padding: 6px;
    transition: border-color 0.2s, background 0.2s;
}

.hamburger-btn:hover {
    border-color: #6c757d;
    background: #f8f9fa;
}

.hamburger-btn span {
    display: block;
    width: 20px;
    height: 2px;
    background: #555;
    border-radius: 2px;
    transition: background 0.2s;
}

.hamburger-btn.open span:nth-child(1) {
    transform: translateY(7px) rotate(45deg);
}
.hamburger-btn.open span:nth-child(2) {
    opacity: 0;
}
.hamburger-btn.open span:nth-child(3) {
    transform: translateY(-7px) rotate(-45deg);
}

.hamburger-dropdown {
    position: absolute;
    top: calc(100% + 8px);
    right: 0;
    background: white;
    border: 1px solid #e1e8ed;
    border-radius: 8px;
    box-shadow: 0 4px 20px rgba(0,0,0,0.12);
    min-width: 180px;
    z-index: 500;
    overflow: hidden;
}

.dropdown-item {
    display: block;
    width: 100%;
    padding: 12px 18px;
    background: none;
    border: none;
    text-align: left;
    font-size: 14px;
    font-weight: 500;
    color: #333;
    cursor: pointer;
    transition: background 0.15s;
}

.dropdown-item:hover {
    background: #f0f4ff;
    color: #2351ac;
}

/* ─── Drop Overlay ──────────────────────────────────────────── */
.drop-overlay {
    position: fixed;
    inset: 0;
    background: rgba(35, 81, 172, 0.15);
    border: 4px dashed #2351ac;
    z-index: 400;
    display: flex;
    align-items: center;
    justify-content: center;
    pointer-events: none;
}

.drop-overlay.active {
    display: flex !important;
    pointer-events: all;
}

.drop-overlay-content {
    background: white;
    border-radius: 12px;
    padding: 40px 60px;
    box-shadow: 0 8px 32px rgba(0,0,0,0.15);
}

.drop-overlay-content p {
    font-size: 20px;
    font-weight: 600;
    color: #2351ac;
}

/* ─── Main Content ──────────────────────────────────────────── */
.main-content {
    padding: 40px;
    max-width: 1200px;
    margin: 0 auto;
}

/* ─── Studies Section (shared by patient + admin) ───────────── */
.studies-section {
    background: white;
    padding: 30px;
    border-radius: 10px;
    box-shadow: 0 4px 20px rgba(0, 0, 0, 0.1);
}

.studies-section h2 {
    color: #333;
    margin-bottom: 20px;
}

.studies-list {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(300px, 1fr));
    gap: 20px;
}

.study-card {
    background: #f8f9fa;
    padding: 20px;
    border-radius: 8px;
    border: 2px solid #e1e8ed;
    cursor: pointer;
    transition: all 0.3s;
}

.study-card:hover {
    border-color: #6c757d;
    transform: translateY(-2px);
    box-shadow: 0 4px 12px rgba(102, 126, 234, 0.2);
}

.study-card h3 {
    color: #333;
    margin-bottom: 10px;
    font-size: 16px;
}

.study-card p {
    color: #666;
    font-size: 13px;
    margin-top: 4px;
}

.study-card .card-tag {
    display: inline-block;
    margin-top: 8px;
    padding: 3px 8px;
    border-radius: 4px;
    font-size: 11px;
    font-weight: 600;
    background: #e8f0fe;
    color: #2351ac;
}

.loading {
    text-align: center;
    color: #666;
    padding: 20px;
    grid-column: 1 / -1;
}

/* ─── Admin Search Bar ──────────────────────────────────────── */
.search-bar-container {
    display: flex;
    gap: 10px;
    margin-bottom: 20px;
    align-items: center;
}

.search-input {
    flex: 1;
    padding: 12px 16px;
    border: 2px solid #e1e8ed;
    border-radius: 6px;
    font-size: 14px;
    transition: border-color 0.3s;
}

.search-input:focus {
    outline: none;
    border-color: #2351ac;
}

.search-status {
    font-size: 13px;
    color: #888;
    margin-bottom: 12px;
    min-height: 18px;
}

/* ─── Available Studies (Admin) ─────────────────────────────── */
.available-studies-divider {
    border-top: 2px solid #e1e8ed;
    margin: 30px 0 24px;
}

.available-studies-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 16px;
    flex-wrap: wrap;
    gap: 12px;
}

.available-studies-header h2 {
    margin-bottom: 0;
}

.results-per-page {
    display: flex;
    align-items: center;
    gap: 10px;
}

.results-per-page label {
    font-size: 13px;
    font-weight: 500;
    color: #666;
    white-space: nowrap;
}

.results-per-page select {
    padding: 8px 12px;
    border: 2px solid #e1e8ed;
    border-radius: 6px;
    font-size: 14px;
    font-weight: 500;
    color: #333;
    background: white;
    cursor: pointer;
    transition: border-color 0.3s;
    min-width: 70px;
}

.results-per-page select:focus {
    outline: none;
    border-color: #2351ac;
}

/* ─── Viewer Modal ──────────────────────────────────────────── */
.modal {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(0, 0, 0, 0.8);
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 1000;
}

.modal-content {
    background: white;
    border-radius: 10px;
    width: 98%;
    max-width: 2000px;
    height: 95vh;
    display: flex;
    flex-direction: column;
    box-shadow: 0 10px 50px rgba(0, 0, 0, 0.3);
}

.modal-header {
    padding: 20px 30px;
    border-bottom: 2px solid #e1e8ed;
    display: flex;
    justify-content: space-between;
    align-items: center;
}

.modal-header h2 {
    color: #333;
    display: flex;
    align-items: center;
    gap: 10px;
}

.cache-indicator {
    font-size: 12px;
    padding: 4px 8px;
    background: #4caf50;
    color: white;
    border-radius: 4px;
    font-weight: normal;
}

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

.modal-actions .btn {
    width: auto;
}

.modal-body {
    padding: 20px;
    flex: 1;
    overflow: hidden;
    display: flex;
}

/* ─── Unified Viewer ────────────────────────────────────────── */
.unified-viewer {
    display: flex;
    width: 100%;
    height: 100%;
    min-height: 750px;
}

.viewer-panel {
    flex: 1;
    display: flex;
    flex-direction: column;
    background: #f8f9fa;
    border-radius: 8px;
    padding: 15px;
    overflow: hidden;
}

.viewer-tabs {
    display: flex;
    gap: 10px;
    margin-bottom: 15px;
}

.tab-btn {
    flex: 1;
    padding: 10px 20px;
    border: none;
    background: #e1e8ed;
    color: #555;
    font-weight: 600;
    cursor: pointer;
    border-radius: 6px;
    transition: all 0.3s;
}

.tab-btn:hover {
    background: #d1d8dd;
}

.tab-btn.active {
    background: #555;
    color: white;
}

.viewer-display-container {
    flex: 1;
    display: flex;
    flex-direction: column;
    min-height: 0;
}

.viewer-display {
    background: #000;
    border-radius: 8px;
    flex: 1;
    display: flex;
    align-items: center;
    justify-content: center;
    position: relative;
    overflow: hidden;
    min-height: 500px;
}

.viewer-display img,
.viewer-display video {
    max-width: 100%;
    max-height: 100%;
    object-fit: contain;
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    transition: opacity 0.15s ease-in-out;
    opacity: 1;
}

.viewer-display embed {
    width: 100%;
    height: 100%;
    border: none;
}

.viewer-display img.hidden,
.viewer-display video.hidden,
.viewer-display embed.hidden {
    opacity: 0;
    pointer-events: none;
    position: absolute;
}

.viewer-display .no-content,
.viewer-display .loading-indicator {
    position: relative;
    z-index: 1;
}

.viewer-counter-display {
    display: flex;
    justify-content: center;
    align-items: center;
    padding: 10px 0;
}

.viewer-thumbnail-strip {
    border-top: 2px solid #e1e8ed;
    padding-top: 15px;
    margin-top: 10px;
}

.thumbnail-container {
    display: flex;
    gap: 10px;
    overflow-x: scroll;
    padding: 5px 0;
    scroll-behavior: smooth;
}

.thumbnail-container::-webkit-scrollbar { height: 8px; }
.thumbnail-container::-webkit-scrollbar-track { background: #f1f1f1; border-radius: 4px; }
.thumbnail-container::-webkit-scrollbar-thumb { background: #888; border-radius: 4px; }
.thumbnail-container::-webkit-scrollbar-thumb:hover { background: #555; }

.thumbnail {
    width: 100px;
    height: 75px;
    border: 2px solid transparent;
    border-radius: 6px;
    cursor: pointer;
    overflow: hidden;
    flex-shrink: 0;
    transition: all 0.3s;
    background: #000;
    display: flex;
    align-items: center;
    justify-content: center;
    position: relative;
}

.thumbnail:hover { border-color: #667eea; transform: scale(1.05); }
.thumbnail.active { border-color: #667eea; box-shadow: 0 0 10px rgba(102, 126, 234, 0.5); }

.thumbnail img,
.thumbnail video { width: 100%; height: 100%; object-fit: cover; }
.thumbnail canvas { width: 100%; height: 100%; object-fit: cover; }



/* ─── Loading ───────────────────────────────────────────────── */
.loading-indicator {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 20px;
    color: #fff;
}

.spinner {
    width: 50px;
    height: 50px;
    border: 4px solid rgba(255, 255, 255, 0.3);
    border-top-color: #555;
    border-radius: 50%;
    animation: spin 1s linear infinite;
}

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

.loading-indicator p { color: #fff; font-size: 16px; margin: 0; }
.no-content { color: #999; font-size: 16px; }
.file-counter { color: #666; font-weight: 500; min-width: 80px; text-align: center; }

/* ─── Mobile PDF Viewer (PDF.js rendered pages) ───────────── */
.viewer-pdf-mobile {
    width: 100%;
    height: 100%;
    overflow-y: auto;
    overflow-x: hidden;
    -webkit-overflow-scrolling: touch;
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 4px;
    padding: 4px 0;
    background: #525659;
}

.viewer-pdf-mobile canvas {
    display: block;
    max-width: 100%;
    height: auto;
    box-shadow: 0 1px 4px rgba(0,0,0,0.3);
    background: white;
}

.viewer-pdf-mobile.hidden {
    display: none !important;
}

/* ─── Responsive: Tablet ────────────────────────────────────── */
@media (max-width: 1200px) {
    .thumbnail { width: 90px; height: 68px; }
}

/* ─── Responsive: Mobile ────────────────────────────────────── */
@media (max-width: 768px) {
    /* Login */
    .login-container {
        margin: 16px;
        padding: 24px 20px;
        max-width: none;
        width: calc(100% - 32px);
    }

    .login-logo { max-width: 200px; }

    .login-tabs { margin-bottom: 20px; }

    .login-tab-btn { font-size: 13px; padding: 8px 0; }

    .form-group { margin-bottom: 16px; }

    .form-group input {
        padding: 10px;
        font-size: 16px; /* prevents iOS zoom on focus */
    }

    /* Header */
    .header {
        flex-direction: row;
        flex-wrap: wrap;
        gap: 10px;
        padding: 12px 16px;
    }

    .dashboard-logo { max-width: 140px; }

    .header-right {
        flex-direction: row;
        gap: 10px;
    }

    .user-info {
        gap: 8px;
    }

    .user-info span {
        font-size: 12px;
        max-width: 100px;
        overflow: hidden;
        text-overflow: ellipsis;
        white-space: nowrap;
    }

    .user-info .btn {
        padding: 8px 12px;
        font-size: 12px;
    }

    /* Main content */
    .main-content { padding: 16px; }

    .studies-section {
        padding: 16px;
        border-radius: 8px;
    }

    .studies-section h2 {
        font-size: 18px;
        margin-bottom: 14px;
    }

    .studies-list {
        grid-template-columns: 1fr;
        gap: 12px;
    }

    .study-card {
        padding: 14px;
    }

    .study-card h3 { font-size: 14px; }
    .study-card p { font-size: 12px; }

    /* Admin search */
    .search-bar-container {
        flex-direction: column;
        align-items: stretch;
    }

    .search-input {
        font-size: 16px; /* prevents iOS zoom */
    }

    .btn-search { width: 100%; }

    .available-studies-header {
        flex-direction: column;
        align-items: flex-start;
    }

    /* Upload toast */
    .upload-toast {
        left: 16px;
        right: 16px;
        transform: none;
        width: auto;
        padding: 12px 16px;
        font-size: 13px;
        bottom: 16px;
    }

    /* Drop overlay */
    .drop-overlay-content {
        padding: 24px 32px;
    }

    .drop-overlay-content p { font-size: 16px; }

    /* ─── Modal / Viewer (mobile) ──────────────────────────── */
    .modal {
        align-items: stretch;
        justify-content: stretch;
    }

    .modal-content {
        width: 100%;
        height: 100%;
        max-width: none;
        border-radius: 0;
        box-shadow: none;
    }

    .modal-header {
        padding: 12px 16px;
        flex-direction: column;
        gap: 10px;
        align-items: flex-start;
    }

    .modal-header h2 {
        font-size: 14px;
        width: 100%;
        overflow: hidden;
        text-overflow: ellipsis;
        white-space: nowrap;
    }

    .modal-actions {
        width: 100%;
        gap: 8px;
    }

    .modal-actions .btn {
        flex: 1;
        padding: 8px 12px;
        font-size: 12px;
        text-align: center;
    }

    .modal-body {
        padding: 8px;
        overflow-y: auto;
    }

    .unified-viewer {
        min-height: auto;
        flex-direction: column;
    }

    .viewer-panel {
        min-height: auto;
        padding: 8px;
        overflow: visible;
    }

    .viewer-tabs {
        gap: 6px;
        margin-bottom: 8px;
    }

    .tab-btn {
        padding: 8px 6px;
        font-size: 12px;
        border-radius: 4px;
    }

    .viewer-display {
        min-height: 300px;
        border-radius: 6px;
    }

    .viewer-counter-display { padding: 6px 0; }

    .file-counter { font-size: 13px; }

    .viewer-thumbnail-strip {
        padding-top: 8px;
        margin-top: 6px;
    }

    .thumbnail {
        width: 64px;
        height: 48px;
        border-radius: 4px;
    }
}

/* ─── Responsive: Small phones ──────────────────────────────── */
@media (max-width: 400px) {
    .login-container {
        padding: 20px 16px;
        margin: 12px;
        width: calc(100% - 24px);
    }

    .header { padding: 10px 12px; }

    .dashboard-logo { max-width: 110px; }

    .main-content { padding: 12px; }

    .studies-section { padding: 12px; }

    .modal-header h2 { font-size: 13px; }

    .viewer-display { min-height: 220px; }

    .thumbnail { width: 52px; height: 40px; }
}

/* ─── Responsive: Landscape phones ──────────────────────────── */
@media (max-height: 500px) and (orientation: landscape) {
    .modal-header { padding: 8px 16px; gap: 6px; }

    .modal-body { padding: 4px 8px; }

    .viewer-display { min-height: 180px; }

    .viewer-tabs { margin-bottom: 4px; }

    .viewer-thumbnail-strip {
        padding-top: 4px;
        margin-top: 4px;
    }

    .thumbnail { width: 52px; height: 40px; }
}