/**
 * =========================================================================
 * 🏥 PACS Viewer Stylesheet — 4-Corner Medical HUD Overlays & Viewport
 * =========================================================================
 */

/* =========================================================================
 * 🌑 Full-Screen Container & Backdrop
 * ========================================================================= */
#pacsImageContainer {
    display: none;
    position: fixed;
    top: 0;
    left: 0;
    width: 100vw;
    height: 100vh;
    z-index: 999999;
    background: rgba(10, 15, 29, 0.96);
    flex-direction: column;
    justify-content: center;
    align-items: center;
    padding: 8px;
    box-sizing: border-box;
    backdrop-filter: blur(6px);
}

/* 📱 ปรับระยะเว้นด้านบนของกล่องดูภาพ PACS ใน Telegram (iPad / Desktop Mode) */
body.in-telegram-webapp:not(.is-mobile-ui) #pacsImageContainer,
html.in-telegram-webapp:not(.is-mobile-ui) #pacsImageContainer {
    padding-top: 70px;
    padding-bottom: 8px;
    padding-left: 8px;
    padding-right: 8px;
}

body.in-telegram-webapp.is-mobile-ui #pacsImageContainer,
html.in-telegram-webapp.is-mobile-ui #pacsImageContainer {
    padding-top: calc(var(--tg-content-safe-area-inset-top, var(--tg-safe-area-inset-top, env(safe-area-inset-top, 0px))) + 8px);
    padding-bottom: 8px;
    padding-left: 8px;
    padding-right: 8px;
}

/* =========================================================================
 * ⏳ Modern PACS Loading Stepper & Progress Dialog
 * ========================================================================= */
.pacs-loading-box {
    display: none;
    position: relative;
    width: 92%;
    max-width: 480px;
    padding: 22px 20px;
    color: #e2e8f0;
    border: 1px solid #334155;
    border-radius: 14px;
    background: #0f172a;
    box-shadow: 0 20px 45px rgba(0, 0, 0, 0.75);
    flex-direction: column;
    gap: 14px;
    box-sizing: border-box;
    animation: pacsFadeIn 0.25s ease;
}

.pacs-loading-close {
    position: absolute;
    top: 14px;
    right: 14px;
    width: 30px;
    height: 30px;
    border-radius: 8px;
    background: rgba(51, 65, 85, 0.5);
    border: 1px solid rgba(100, 116, 139, 0.3);
    color: #94a3b8;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    font-size: 0.85rem;
    transition: all 0.2s ease;
    z-index: 10;
}

.pacs-loading-close:hover {
    background: rgba(239, 68, 68, 0.2);
    border-color: rgba(239, 68, 68, 0.5);
    color: #fca5a5;
    transform: scale(1.05);
}

@keyframes pacsFadeIn {
    from {
        opacity: 0;
        transform: scale(0.96);
    }

    to {
        opacity: 1;
        transform: scale(1);
    }
}

.pacs-loading-header {
    text-align: center;
    padding-right: 24px;
    padding-left: 24px;
}

.pacs-loading-title-row {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
}

.pacs-loading-spinner {
    font-size: 1.2rem;
    color: #38bdf8;
}

.pacs-loading-title {
    font-size: 0.95rem;
    font-weight: 700;
    color: #f8fafc;
    letter-spacing: 0.2px;
}

.pacs-loading-subtitle {
    font-size: 0.76rem;
    color: #94a3b8;
    margin-top: 4px;
    min-height: 18px;
}

/* Stepper Workflow */
.pacs-stepper {
    display: flex;
    flex-direction: column;
    gap: 0;
    margin: 4px 0;
    padding: 8px 12px;
    background: rgba(30, 41, 59, 0.6);
    border: 1px solid rgba(51, 65, 85, 0.6);
    border-radius: 8px;
}

.pacs-step {
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 5px 0;
    transition: all 0.2s ease;
}

.pacs-step-circle {
    width: 24px;
    height: 24px;
    border-radius: 50%;
    background: #334155;
    color: #94a3b8;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 0.75rem;
    font-weight: 700;
    flex-shrink: 0;
    transition: all 0.25s ease;
}

.pacs-step-circle .step-num {
    display: inline-block;
}

.pacs-step-circle .step-spinner {
    display: none;
    font-size: 0.75rem;
}

.pacs-step-circle .step-check {
    display: none;
    font-size: 0.75rem;
}

.pacs-step-text {
    flex: 1;
    display: flex;
    flex-direction: column;
}

.pacs-step-text .step-label {
    font-size: 0.8rem;
    font-weight: 600;
    color: #94a3b8;
    transition: color 0.2s ease;
}

.pacs-step-text .step-sub {
    font-size: 0.7rem;
    color: #64748b;
    margin-top: 1px;
}

.pacs-step-line {
    width: 2px;
    height: 8px;
    background: #334155;
    margin-left: 11px;
    transition: background 0.25s ease;
}

/* Step States */
/* Pending */
.pacs-step.step-pending .pacs-step-circle {
    background: #1e293b;
    color: #64748b;
    border: 1px solid #334155;
}

/* Active (In-Progress) */
.pacs-step.step-active .pacs-step-circle {
    background: #0284c7;
    color: #ffffff;
    box-shadow: 0 0 8px rgba(56, 189, 248, 0.5);
    border: 1px solid #38bdf8;
}

.pacs-step.step-active .step-num {
    display: none;
}

.pacs-step.step-active .step-spinner {
    display: inline-block;
    color: #ffffff;
}

.pacs-step.step-active .step-check {
    display: none;
}

.pacs-step.step-active .step-label {
    color: #38bdf8;
    font-weight: 700;
}

.pacs-step.step-active .step-sub {
    color: #cbd5e1;
}

/* Done (Completed) */
.pacs-step.step-done .pacs-step-circle {
    background: #16a34a;
    color: #ffffff;
    border: 1px solid #22c55e;
}

.pacs-step.step-done .step-num {
    display: none;
}

.pacs-step.step-done .step-spinner {
    display: none;
}

.pacs-step.step-done .step-check {
    display: inline-block;
    color: #ffffff;
}

.pacs-step.step-done .step-label {
    color: #86efac;
}

.pacs-step.step-done .step-sub {
    color: #94a3b8;
}

.pacs-step-line.line-done {
    background: #16a34a;
}

/* Progress Track */
.pacs-progress-bar-track {
    width: 100%;
    height: 4px;
    background: #1e293b;
    border-radius: 4px;
    overflow: hidden;
    margin-top: 2px;
}

.pacs-progress-bar-fill {
    width: 0%;
    height: 100%;
    background: linear-gradient(90deg, #0284c7, #38bdf8, #22c55e);
    border-radius: 4px;
    transition: width 0.3s ease;
}

/* 🛑 Cancel Action Area */
.pacs-loading-actions {
    display: flex;
    justify-content: center;
    margin-top: 4px;
}

.pacs-btn-cancel {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
    width: 100%;
    max-width: 220px;
    padding: 9px 18px;
    background: rgba(239, 68, 68, 0.12);
    color: #fca5a5;
    border: 1px solid rgba(239, 68, 68, 0.35);
    border-radius: 8px;
    font-size: 0.85rem;
    font-weight: 600;
    cursor: pointer;
    transition: all 0.2s ease;
    user-select: none;
}

.pacs-btn-cancel:hover {
    background: rgba(239, 68, 68, 0.25);
    border-color: rgba(239, 68, 68, 0.6);
    color: #fee2e2;
    transform: translateY(-1px);
    box-shadow: 0 4px 14px rgba(239, 68, 68, 0.25);
}

.pacs-btn-cancel:active {
    transform: translateY(0);
}

#pacsStudyList {
    display: none;
    width: 100%;
    max-width: 520px;
    max-height: 80vh;
    overflow-y: auto;
    border: 1px solid #334155;
    border-radius: 10px;
    background: #ffffff;
    box-shadow: 0 10px 30px rgba(0, 0, 0, 0.6);
    overflow: hidden;
}

#pacsError {
    display: none;
    text-align: center;
    padding: 24px 32px;
    color: #991b1b;
    border: 1px solid #fca5a5;
    border-radius: 10px;
    background: #fef2f2;
    box-shadow: 0 10px 25px rgba(0, 0, 0, 0.4);
    max-width: 450px;
}

/* =========================================================================
 * 🎛️ PACS Main Viewer Area
 * ========================================================================= */
#pacsViewerArea {
    display: none;
    width: 100%;
    max-width: 1100px;
    height: 96vh;
    max-height: 96vh;
    border: 1px solid #334155;
    border-radius: 8px;
    background: #000000;
    overflow: hidden;
    flex-direction: column;
    box-shadow: 0 12px 35px rgba(0, 0, 0, 0.8);
}

body.in-telegram-webapp #pacsViewerArea,
html.in-telegram-webapp #pacsViewerArea {
    height: calc(100vh - 28px - var(--tg-safe-area-inset-top, env(safe-area-inset-top, 0px)));
    max-height: calc(100vh - 28px - var(--tg-safe-area-inset-top, env(safe-area-inset-top, 0px)));
}

/* =========================================================================
 * 🔝 Top Floating Bar
 * ========================================================================= */
.pacs-top-bar {
    background: #0f172a;
    padding: 8px 14px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    color: #ffffff;
    font-size: 0.85rem;
    border-bottom: 1px solid #1e293b;
    flex-shrink: 0;
    min-height: 42px;
    gap: 12px;
    z-index: 10;
}

.pacs-top-title {
    display: flex;
    align-items: center;
    gap: 8px;
    font-weight: 600;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.pacs-badge-desc {
    color: #94a3b8;
    font-size: 0.78rem;
    font-weight: normal;
}

.pacs-top-actions {
    display: flex;
    gap: 6px;
    align-items: center;
}

.pacs-btn-nav,
.pacs-btn-close {
    background: rgba(255, 255, 255, 0.12);
    color: #ffffff;
    border: 1px solid rgba(255, 255, 255, 0.15);
    padding: 4px 12px;
    font-size: 0.85rem;
    border-radius: 4px;
    cursor: pointer;
    transition: all 0.15s ease;
    display: flex;
    align-items: center;
    justify-content: center;
}

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

.pacs-btn-close {
    background: #dc2626;
    border-color: #ef4444;
}

.pacs-btn-close:hover {
    background: #b91c1c;
}

/* =========================================================================
 * 🖼️ PACS Viewport & 4-Corner HUD Overlays
 * ========================================================================= */
.pacs-viewport {
    position: relative;
    flex: 1;
    display: flex;
    align-items: center;
    justify-content: center;
    background: #000000;
    overflow: hidden;
    width: 100%;
}

.pacs-image {
    width: 100%;
    height: 100%;
    object-fit: contain;
    display: none;
    user-select: none;
}

.pacs-no-image {
    color: #64748b;
    display: none;
    font-size: 1rem;
}

/* Base style for all 4 corner HUDs */
.pacs-corner-overlay {
    position: absolute;
    z-index: 5;
    pointer-events: none;
    color: #e2e8f0;
    font-family: 'Consolas', 'Courier New', 'Sarabun', monospace;
    font-size: 0.78rem;
    line-height: 1.35;
    background: rgba(15, 23, 42, 0.45);
    padding: 6px 10px;
    border-radius: 6px;
    backdrop-filter: blur(3px);
    border: 1px solid rgba(255, 255, 255, 0.08);
    text-shadow: 0 1px 3px rgba(0, 0, 0, 0.9), 0 0 2px rgba(0, 0, 0, 0.9);
    max-width: 45%;
    box-sizing: border-box;
}

.pacs-val-highlight {
    font-weight: bold;
    font-size: 0.88rem;
}

/* 🌟 1. ซ้ายบนภาพ (Top-Left): PatientName HN AGE SEX */
.pacs-corner-tl {
    top: 8px;
    left: 8px;
    text-align: left;
}

.pacs-corner-tl .pacs-val-highlight {
    color: #38bdf8;
    /* Sky Blue */
}

/* 🌟 2. ขวาบนภาพ (Top-Right): X-RAY NAME, BODYPART, SERIES NUMBER, STUDYDATETIME, Accession */
.pacs-corner-tr {
    top: 8px;
    right: 8px;
    text-align: right;
}

.pacs-corner-tr .pacs-val-highlight {
    color: #fcd34d;
    /* Amber */
}

/* 🌟 3. ซ้ายล่างภาพ (Bottom-Left): Exposure and Image parameter */
.pacs-corner-bl {
    bottom: 8px;
    left: 8px;
    text-align: left;
    color: #4ade80;
    /* Mint Green */
}

/* 🌟 4. ขวาล่างภาพ (Bottom-Right): WindowWidth WindowsLevel */
.pacs-corner-br {
    bottom: 8px;
    right: 8px;
    text-align: right;
    color: #fde047;
    /* Yellow */
}

/* =========================================================================
 * 🎞️ Bottom Thumbnail Strip
 * ========================================================================= */
.pacs-thumbnail-strip {
    display: none;
    gap: 6px;
    padding: 8px 12px;
    background: #0f172a;
    overflow-x: auto;
    border-top: 1px solid #1e293b;
    flex-shrink: 0;
    align-items: center;
}

.pacs-thumb-item {
    width: 52px;
    height: 52px;
    object-fit: cover;
    cursor: pointer;
    border-radius: 4px;
    border: 2px solid transparent;
    flex-shrink: 0;
    transition: transform 0.15s ease, border-color 0.15s ease;
    background: #000;
}

.pacs-thumb-item:hover {
    transform: scale(1.06);
}

.pacs-thumb-item.active {
    border-color: #38bdf8;
    box-shadow: 0 0 8px rgba(56, 189, 248, 0.6);
}

/* =========================================================================
 * 📱 Mobile Responsiveness (≤ 550px)
 * ========================================================================= */
@media (max-width: 550px) {
    #pacsViewerArea {
        height: 100vh;
        max-height: 100vh;
        border-radius: 0;
        border: none;
    }

    .pacs-corner-overlay {
        font-size: 0.68rem;
        padding: 4px 6px;
        line-height: 1.25;
        max-width: 48%;
    }

    .pacs-val-highlight {
        font-size: 0.78rem;
    }

    .pacs-top-bar {
        padding: 6px 8px;
    }

    .pacs-btn-nav,
    .pacs-btn-close {
        padding: 3px 8px;
    }
}