:root {
    --bg: #0f172a;
    --panel: #111827;
    --panel-soft: #1f2937;
    --text: #e5e7eb;
    --muted: #9ca3af;
    --border: #374151;
    --accent: #38bdf8;
    --danger: #f87171;
    --ok: #34d399;
    --input: #0b1220;
}

* {
    box-sizing: border-box;
}

html,
body {
    margin: 0;
    min-height: 100%;
    font-family: Arial, Helvetica, sans-serif;
    background: #0b1120;
    color: var(--text);
}

a {
    color: inherit;
}

.login-body {
    min-height: 100vh;
    display: grid;
    place-items: center;
    background:
        radial-gradient(circle at top, rgba(56, 189, 248, 0.20), transparent 35%),
        #0b1120;
}

.login-card {
    width: min(560px, calc(100vw - 32px));
    padding: 26px;
    border: 1px solid var(--border);
    border-radius: 20px;
    background: rgba(17, 24, 39, 0.96);
    box-shadow: 0 20px 60px rgba(0, 0, 0, 0.35);
}

.login-logo-wrap {
    display: flex;
    justify-content: center;
    margin-bottom: 16px;
}

.login-logo {
    max-width: 430px;
    width: 100%;
    height: auto;
    border-radius: 10px;
}

.login-title {
    text-align: center;
    margin: 0 0 22px;
    font-size: 2rem;
    letter-spacing: 0.02em;
}

.login-form {
    display: grid;
    gap: 14px;
}

.login-form-row {
    display: grid;
    grid-template-columns: 110px minmax(0, 1fr);
    align-items: center;
    gap: 12px;
}

.login-form-row label {
    margin: 0;
    color: var(--text);
    font-weight: 700;
    font-size: 0.92rem;
}

.login-button-row {
    display: flex;
    justify-content: center;
    margin-top: 8px;
}

.login-button-row button {
    min-width: 150px;
}

h1,
h2 {
    margin: 0 0 12px;
}

h2 {
    font-size: 0.95rem;
}

.muted {
    color: var(--muted);
    font-size: 0.88rem;
}

.tiny-note {
    color: var(--muted);
    font-size: 0.78rem;
    margin-top: 18px;
    line-height: 1.45;
}

label {
    display: grid;
    gap: 7px;
    margin: 14px 0;
    color: var(--muted);
    font-size: 0.9rem;
}

input,
select {
    width: 100%;
    border: 1px solid var(--border);
    background: var(--input);
    color: var(--text);
    padding: 11px 12px;
    border-radius: 12px;
    outline: none;
}

input::placeholder {
    color: rgba(229, 231, 235, 0.58);
}

select {
    cursor: pointer;
}

input:focus,
select:focus {
    border-color: var(--accent);
    box-shadow: 0 0 0 3px rgba(56, 189, 248, 0.12);
}

button,
.button {
    border: 0;
    background: var(--accent);
    color: #031525;
    padding: 10px 14px;
    border-radius: 11px;
    font-weight: 700;
    cursor: pointer;
    text-decoration: none;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
    white-space: nowrap;
}

button:hover,
.button:hover {
    filter: brightness(1.08);
}

.button.ghost,
button.ghost {
    background: rgba(255, 255, 255, 0.08);
    color: var(--text);
    border: 1px solid var(--border);
}

.button.small {
    padding: 8px 10px;
    border-radius: 10px;
    font-size: 0.85rem;
}

.button.tiny,
button.tiny {
    padding: 5px 7px;
    border-radius: 8px;
    font-size: 0.72rem;
}

.compact-button {
    padding: 7px 10px;
    font-size: 0.78rem;
}

.alert {
    padding: 12px;
    border-radius: 12px;
    margin: 14px 0;
}

.alert-error {
    background: rgba(248, 113, 113, 0.12);
    border: 1px solid rgba(248, 113, 113, 0.35);
    color: #fecaca;
}

.topbar {
    min-height: 54px;
    padding: 6px 12px;
    display: flex;
    align-items: center;
    justify-content: space-between;
    background: rgba(17, 24, 39, 0.98);
    border-bottom: 1px solid var(--border);
    position: sticky;
    top: 0;
    z-index: 5;
}

.dashboard-topbar {
    min-height: 118px;
    justify-content: center;
    position: relative;
}

.dashboard-brand {
    display: grid;
    justify-items: center;
    gap: 8px;
}

.dashboard-logo {
    width: min(430px, calc(100vw - 170px));
    height: auto;
    border-radius: 8px;
}

.dashboard-brand h1 {
    margin: 0;
    font-size: 2rem;
    letter-spacing: 0.02em;
}

.dashboard-logout {
    position: absolute;
    right: 14px;
    top: 14px;
}

.topbar-brand {
    display: flex;
    align-items: center;
    gap: 10px;
    min-width: 0;
}

.topbar-logo {
    height: 38px;
    width: auto;
    max-width: 190px;
    object-fit: contain;
    border-radius: 6px;
}

.topbar strong {
    margin-right: 8px;
    font-size: 0.9rem;
}

.top-actions {
    display: flex;
    gap: 8px;
}

.page {
    padding: 18px;
    display: grid;
    gap: 16px;
}

.dashboard-page {
    padding-top: 16px;
}

.panel {
    background: var(--panel);
    border: 1px solid var(--border);
    border-radius: 18px;
    padding: 18px;
}

.section-title {
    margin-bottom: 16px;
}

.centered-title {
    text-align: center;
}

.panel-heading {
    display: flex;
    align-items: baseline;
    justify-content: space-between;
    gap: 12px;
}

.centered-panel-heading {
    display: grid;
    justify-items: center;
    gap: 4px;
    text-align: center;
}

.centered-panel-heading h2 {
    margin-bottom: 0;
    font-size: 1.25rem;
}

.search-grid {
    display: grid;
    grid-template-columns: repeat(6, minmax(130px, 1fr));
    gap: 12px;
    align-items: end;
}

.compact-search-grid {
    grid-template-columns: repeat(4, minmax(160px, 1fr));
}

.compact-search-grid button {
    height: 42px;
}

.search-actions {
    display: flex;
    align-items: end;
}

.table-wrap {
    overflow-x: auto;
}

.results-table {
    width: 100%;
    border-collapse: collapse;
    min-width: 900px;
}

.results-table th,
.results-table td {
    padding: 12px;
    border-bottom: 1px solid var(--border);
    vertical-align: top;
    text-align: left;
}

.results-table th {
    color: var(--muted);
    font-weight: 700;
    background: rgba(255, 255, 255, 0.03);
}

.empty {
    color: var(--muted);
    text-align: center;
    padding: 30px !important;
}

.empty.compact {
    padding: 10px !important;
    font-size: 0.82rem;
    text-align: left;
}

.empty.error,
.error {
    color: #fecaca;
}

.viewer-body {
    height: 100vh;
    overflow: hidden;
}

.viewer-shell {
    height: calc(100vh - 54px);
    min-height: 0;
    display: grid;
    grid-template-columns: 260px minmax(0, 1fr);
}

.viewer-shell-full {
    height: 100vh;
}

.viewer-sidebar {
    min-height: 0;
    border-right: 1px solid var(--border);
    background: var(--panel);
    padding: 8px;
    overflow: hidden;
    display: flex;
    flex-direction: column;
}

.viewer-sidebar-scroll {
    min-height: 0;
    flex: 1 1 auto;
    overflow-y: auto;
    overflow-x: hidden;
    padding-right: 3px;
}

.viewer-sidebar-footer {
    flex: 0 0 auto;
    border-top: 1px solid var(--border);
    margin-top: 8px;
    padding-top: 8px;
    display: grid;
    grid-template-columns: 1fr;
    gap: 8px;
}

.viewer-sidebar-footer .button {
    width: 100%;
}

.sidebar-section {
    display: block;
    margin-bottom: 12px;
}

.sidebar-heading {
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: 8px;
    margin-bottom: 6px;
    position: sticky;
    top: -8px;
    z-index: 2;
    background: var(--panel);
    padding: 4px 0;
}

.sidebar-heading h2 {
    margin: 0;
}

.study-meta {
    background: rgba(255, 255, 255, 0.05);
    border: 1px solid var(--border);
    border-radius: 10px;
    padding: 8px;
    margin-bottom: 8px;
    color: var(--muted);
    font-size: 0.75rem;
    line-height: 1.35;
}

.study-meta strong {
    color: var(--text);
}

.patient-studies-list,
.series-list {
    display: grid;
    align-content: start;
    gap: 7px;
}

.patient-study-item {
    width: 100%;
    display: grid;
    gap: 2px;
    text-align: left;
    background: rgba(255, 255, 255, 0.055);
    color: var(--text);
    border: 1px solid var(--border);
    padding: 8px;
    border-radius: 10px;
}

.patient-study-item:hover {
    border-color: rgba(56, 189, 248, 0.7);
}

.patient-study-item.loaded {
    border-color: rgba(52, 211, 153, 0.65);
    background: rgba(52, 211, 153, 0.10);
}

.patient-study-date {
    color: var(--accent);
    font-size: 0.7rem;
    font-weight: 800;
}

.patient-study-main {
    font-size: 0.78rem;
    font-weight: 800;
}

.patient-study-sub,
.patient-study-status {
    color: var(--muted);
    font-size: 0.68rem;
}

.patient-study-status {
    color: var(--ok);
}

.loaded-study-card {
    border: 1px solid var(--border);
    border-radius: 11px;
    overflow: hidden;
    background: rgba(255, 255, 255, 0.025);
}

.loaded-study-title {
    display: flex;
    justify-content: space-between;
    gap: 6px;
    padding: 8px;
    background: rgba(56, 189, 248, 0.12);
    border-bottom: 1px solid var(--border);
    font-size: 0.74rem;
    font-weight: 800;
}

.series-card {
    border-bottom: 1px solid var(--border);
}

.series-card:last-child {
    border-bottom: 0;
}

.series-title {
    padding: 7px 8px;
    background: var(--panel-soft);
    font-weight: 700;
    font-size: 0.72rem;
}

.image-list {
    display: grid;
    gap: 6px;
    padding: 6px;
}

.image-item {
    width: 100%;
    justify-content: space-between;
    background: rgba(255, 255, 255, 0.06);
    color: var(--text);
    border: 1px solid transparent;
    padding: 6px 8px;
    font-size: 0.72rem;
    border-radius: 8px;
}

.image-item.active {
    border-color: var(--accent);
    background: rgba(56, 189, 248, 0.16);
}

.thumbnail-list {
    gap: 7px;
}

.image-thumb-item {
    display: grid;
    grid-template-columns: 62px minmax(0, 1fr);
    align-items: center;
    gap: 8px;
    padding: 6px;
    text-align: left;
}

.image-thumb {
    width: 62px;
    height: 48px;
    object-fit: cover;
    background: #000;
    border-radius: 6px;
    border: 1px solid rgba(255, 255, 255, 0.16);
}

.thumb-text {
    min-width: 0;
    display: grid;
    gap: 3px;
}

.thumb-text strong {
    font-size: 0.74rem;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.thumb-text span {
    color: var(--muted);
    font-size: 0.68rem;
}

.stack-open-button {
    width: 100%;
    display: grid;
    grid-template-columns: 68px minmax(0, 1fr);
    align-items: center;
    gap: 8px;
    background: rgba(52, 211, 153, 0.14);
    color: #d1fae5;
    border: 1px solid rgba(52, 211, 153, 0.45);
    padding: 7px;
    font-size: 0.74rem;
    border-radius: 8px;
    text-align: left;
}

.stack-open-button.active {
    border-color: var(--accent);
    background: rgba(56, 189, 248, 0.18);
}

.stack-preview-thumb {
    width: 68px;
    height: 54px;
    object-fit: cover;
    background: #000;
    border-radius: 6px;
    border: 1px solid rgba(255, 255, 255, 0.16);
}

.stack-open-text {
    display: grid;
    gap: 3px;
    min-width: 0;
}

.stack-open-text strong {
    font-size: 0.78rem;
}

.stack-open-text span {
    color: var(--text);
    font-size: 0.68rem;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.stack-open-text em {
    color: var(--muted);
    font-size: 0.68rem;
    font-style: normal;
}

.viewer-main {
    min-height: 0;
    display: grid;
    grid-template-rows: 42px minmax(0, 1fr);
    min-width: 0;
    background: #000;
}

.viewer-toolbar {
    min-height: 42px;
    padding: 5px 7px;
    display: flex;
    align-items: center;
    gap: 6px;
    background: #111;
    border-bottom: 1px solid #333;
    overflow-x: auto;
}

.viewer-toolbar button {
    padding: 6px 8px;
    border-radius: 8px;
    font-size: 0.72rem;
}

.toggle-btn.active,
.viewer-toolbar button.active,
.layout-btn.active {
    background: var(--ok);
    color: #052016;
}

.fps-control {
    display: inline-flex;
    align-items: center;
    gap: 4px;
    margin: 0;
    color: var(--muted);
    font-size: 0.72rem;
}

.fps-control input {
    width: 54px;
    padding: 5px 6px;
    border-radius: 8px;
    font-size: 0.72rem;
}

.stack-control {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    min-width: 240px;
}

.stack-info {
    color: var(--muted);
    font-size: 0.72rem;
    white-space: nowrap;
}

#stackSlider {
    width: 150px;
    padding: 0;
}

.toolbar-divider {
    width: 1px;
    height: 24px;
    background: #333;
    margin: 0 3px;
    flex: 0 0 auto;
}

.viewer-counter {
    margin-left: auto;
    color: var(--muted);
    white-space: nowrap;
    font-size: 0.78rem;
}

.multi-viewer-stage {
    height: 100%;
    min-height: 0;
    display: grid;
    background: #000;
}

.multi-viewer-stage.layout-1 {
    grid-template-columns: 1fr;
    grid-template-rows: 1fr;
}

.multi-viewer-stage.layout-2 {
    grid-template-columns: repeat(2, minmax(0, 1fr));
    grid-template-rows: 1fr;
}

.multi-viewer-stage.layout-4 {
    grid-template-columns: repeat(2, minmax(0, 1fr));
    grid-template-rows: repeat(2, minmax(0, 1fr));
}

.viewport {
    position: relative;
    overflow: hidden;
    background: #000;
    border: 1px solid #222;
    cursor: grab;
    min-width: 0;
    min-height: 0;
}

.viewport.active {
    border-color: var(--accent);
    box-shadow: inset 0 0 0 2px rgba(56, 189, 248, 0.55);
}

.viewport.dragging {
    cursor: grabbing;
}

.viewport-label {
    position: absolute;
    top: 6px;
    left: 6px;
    z-index: 2;
    background: rgba(0, 0, 0, 0.68);
    color: #e5e7eb;
    border: 1px solid rgba(255, 255, 255, 0.14);
    border-radius: 999px;
    padding: 4px 8px;
    font-size: 0.68rem;
    pointer-events: none;
}

.viewport-empty {
    position: absolute;
    inset: 0;
    color: var(--muted);
    display: grid;
    place-items: center;
    font-size: 0.82rem;
}

.viewport-image {
    position: absolute;
    left: 50%;
    top: 50%;
    max-width: none;
    max-height: none;
    transform-origin: center center;
    user-select: none;
    -webkit-user-drag: none;
    display: none;
    will-change: transform;
}

@media (max-width: 1250px) {
    .stack-control {
        min-width: 190px;
    }

    #stackSlider {
        width: 110px;
    }

    .viewer-counter {
        display: none;
    }

    .compact-search-grid {
        grid-template-columns: repeat(3, minmax(160px, 1fr));
    }
}

@media (max-width: 1100px) {
    .search-grid,
    .compact-search-grid {
        grid-template-columns: repeat(2, minmax(130px, 1fr));
    }

    .viewer-shell {
        grid-template-columns: 240px minmax(0, 1fr);
    }

    .topbar-logo {
        max-width: 155px;
    }

    .image-thumb-item {
        grid-template-columns: 52px minmax(0, 1fr);
    }

    .image-thumb {
        width: 52px;
        height: 42px;
    }

    .stack-open-button {
        grid-template-columns: 56px minmax(0, 1fr);
    }

    .stack-preview-thumb {
        width: 56px;
        height: 46px;
    }
}

@media (max-width: 760px) {
    .topbar {
        height: auto;
        min-height: 54px;
        gap: 8px;
        align-items: flex-start;
        flex-direction: column;
        padding: 10px;
    }

    .dashboard-topbar {
        align-items: center;
        padding-top: 44px;
    }

    .dashboard-logo {
        width: min(360px, calc(100vw - 28px));
    }

    .dashboard-logout {
        top: 10px;
        right: 10px;
    }

    .search-grid,
    .compact-search-grid {
        grid-template-columns: 1fr;
    }

    .login-form-row {
        grid-template-columns: 1fr;
        gap: 6px;
    }

    .viewer-body {
        overflow: auto;
    }

    .viewer-shell,
    .viewer-shell-full {
        height: auto;
        grid-template-columns: 1fr;
    }

    .viewer-sidebar {
        height: 420px;
        border-right: 0;
        border-bottom: 1px solid var(--border);
    }

    .viewer-main {
        height: 72vh;
    }

    .multi-viewer-stage.layout-2,
    .multi-viewer-stage.layout-4 {
        grid-template-columns: 1fr;
        grid-template-rows: repeat(4, minmax(0, 1fr));
    }
}