:root {
    color-scheme: dark;
    --bg: #0b0f14;
    --panel: #111821;
    --panel-2: #16212d;
    --panel-3: #1d2a38;
    --line: rgba(255, 255, 255, 0.08);
    --line-strong: rgba(255, 255, 255, 0.14);
    --text: #edf3fb;
    --muted: #9fb0c3;
    --accent: #53a6ff;
    --accent-strong: #2f8fff;
    --accent-soft: rgba(83, 166, 255, 0.18);
    --success: #30c48d;
    --warning: #ffb347;
    --danger: #ff6b6b;
    --shadow: 0 22px 60px rgba(0, 0, 0, 0.35);
    --radius: 18px;
    --radius-sm: 12px;
}

* {
    box-sizing: border-box;
}

html,
body {
    margin: 0;
    height: 100%;
    overflow: hidden;
    background:
        radial-gradient(circle at top left, rgba(83, 166, 255, 0.12), transparent 24%),
        radial-gradient(circle at top right, rgba(48, 196, 141, 0.09), transparent 18%),
        linear-gradient(180deg, #081018 0%, #0b0f14 100%);
    color: var(--text);
    font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

button,
input {
    font: inherit;
}

img {
    max-width: 100%;
}

.app-shell {
    display: flex;
    flex-direction: column;
    gap: 16px;
    min-height: 100dvh;
    height: 100dvh;
    padding: 16px;
    overflow: hidden;
}

.topbar {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 16px;
    padding: 14px 18px;
    background: rgba(17, 24, 33, 0.78);
    border: 1px solid var(--line);
    border-radius: var(--radius);
    backdrop-filter: blur(18px);
    box-shadow: var(--shadow);
    flex-shrink: 0;
}

.brand {
    display: flex;
    align-items: center;
    gap: 14px;
}

.brand-logo {
    width: 52px;
    height: 52px;
    object-fit: contain;
    border-radius: 14px;
    background: rgba(255, 255, 255, 0.04);
    padding: 8px;
}

.brand-copy {
    display: flex;
    flex-direction: column;
    gap: 4px;
}

.brand-copy strong {
    font-size: 1rem;
    font-weight: 700;
}

.brand-copy span {
    color: var(--muted);
    font-size: 0.9rem;
}

.topbar-actions {
    display: flex;
    align-items: center;
    gap: 10px;
}

.layout-grid {
    display: grid;
    grid-template-columns: minmax(0, 1fr) 390px;
    gap: 16px;
    flex: 1;
    min-height: 0;
    overflow: hidden;
}

.viewer-card,
.panel-card {
    background: rgba(17, 24, 33, 0.86);
    border: 1px solid var(--line);
    border-radius: var(--radius);
    backdrop-filter: blur(18px);
    box-shadow: var(--shadow);
}

.viewer-card {
    display: flex;
    flex-direction: column;
    min-height: 0;
    overflow: hidden;
}

.status-pill {
    display: inline-flex;
    align-items: center;
    gap: 8px;
    min-height: 32px;
    padding: 0 12px;
    border-radius: 999px;
    font-size: 0.85rem;
    font-weight: 700;
    letter-spacing: 0.02em;
    background: rgba(255, 255, 255, 0.07);
    border: 1px solid var(--line);
    width: fit-content;
}

.status-pill::before {
    content: '';
    width: 10px;
    height: 10px;
    border-radius: 50%;
    background: currentColor;
    box-shadow: 0 0 0 4px rgba(255, 255, 255, 0.06);
}

.status-pill.idle {
    color: var(--muted);
}

.status-pill.connecting {
    color: var(--warning);
}

.status-pill.connected {
    color: var(--success);
}

.status-pill.error,
.status-pill.disconnected {
    color: var(--danger);
}

.status-pill.disconnecting {
    color: var(--warning);
}

.viewer-frame {
    position: relative;
    flex: 1;
    min-height: 0;
    display: grid;
    place-items: center;
    overflow: hidden;
    background:
        linear-gradient(180deg, rgba(7, 11, 16, 0.92), rgba(12, 18, 25, 0.98)),
        radial-gradient(circle at center, rgba(83, 166, 255, 0.06), transparent 35%);
}

.viewer-frame.fit-screen {
    overflow: hidden;
    place-items: center;
}

.viewer-frame.actual-size {
    overflow: auto;
    place-items: start center;
}

.remote-image,
.remote-canvas {
    display: block;
    user-select: none;
    -webkit-user-drag: none;
    opacity: 0;
    transition: opacity 140ms ease;
    transform-origin: center center;
}

.remote-image {
    position: absolute;
    inset: 0;
    width: 100%;
    height: 100%;
    max-width: none;
    max-height: none;
    object-fit: contain;
    object-position: center;
}

.remote-canvas {
    position: relative;
    inset: auto;
    width: auto;
    height: auto;
    max-width: 100%;
    max-height: 100%;
    pointer-events: none;
}

.viewer-frame.actual-size .remote-image {
    position: relative;
    inset: auto;
    width: auto;
    height: auto;
    max-width: none;
    max-height: none;
    object-fit: unset;
    object-position: center;
}

.viewer-frame.actual-size .remote-canvas {
    position: relative;
    inset: auto;
    width: auto;
    height: auto;
    max-width: none;
    max-height: none;
}

.viewer-frame.has-frame .remote-image,
.viewer-frame.has-frame .remote-canvas {
    opacity: 1;
}

.viewer-overlay {
    position: absolute;
    inset: 0;
    display: grid;
    place-items: center;
    padding: 24px;
    background: linear-gradient(180deg, rgba(7, 11, 16, 0.35), rgba(7, 11, 16, 0.75));
}

.overlay-card {
    width: min(420px, 100%);
    padding: 24px;
    border-radius: var(--radius);
    background: rgba(17, 24, 33, 0.88);
    border: 1px solid var(--line-strong);
    text-align: center;
}

.overlay-card h2 {
    margin: 0 0 8px;
    font-size: 1.2rem;
}

.overlay-card p {
    margin: 0;
    color: var(--muted);
}

.viewer-hud {
    position: absolute;
    left: 16px;
    right: 16px;
    bottom: 16px;
    display: flex;
    flex-wrap: wrap;
    gap: 8px;
    pointer-events: none;
}

.viewer-hud span {
    display: inline-flex;
    align-items: center;
    min-height: 34px;
    padding: 0 12px;
    border-radius: 999px;
    background: rgba(6, 10, 15, 0.76);
    border: 1px solid rgba(255, 255, 255, 0.08);
    color: #d7e3f0;
    font-size: 0.85rem;
    font-weight: 600;
}

.control-panel {
    display: flex;
    flex-direction: column;
    gap: 16px;
    min-height: 0;
    overflow: hidden;
}

.panel-card {
    padding: 16px;
    min-height: 0;
    overflow: hidden;
    display: flex;
    flex-direction: column;
}

.connection-card {
    flex: 0 0 auto;
}

.stats-card,
.chat-card,
.activity-card {
    flex: 1 1 0;
}

.panel-card-header {
    display: flex;
    align-items: flex-start;
    justify-content: space-between;
    gap: 12px;
    margin-bottom: 14px;
    flex-shrink: 0;
}

.panel-card-header-copy {
    display: flex;
    flex-direction: column;
    gap: 6px;
    min-width: 0;
}

.panel-card-header h2 {
    margin: 0;
    font-size: 1.05rem;
}

.panel-card-header p {
    margin: 0;
    color: var(--muted);
    font-size: 0.92rem;
}

.panel-card-body {
    display: flex;
    flex-direction: column;
    min-height: 0;
}

.panel-card-body-fill {
    flex: 1 1 auto;
}

.panel-card.is-collapsed {
    flex: 0 0 auto;
}

.panel-card.is-collapsed .panel-card-body {
    display: none;
}

.panel-card.is-collapsed .panel-card-header {
    margin-bottom: 0;
}

.connection-card-body {
    gap: 16px;
}

.connection-session-summary {
    display: flex;
    flex-direction: column;
    gap: 10px;
    padding: 14px;
    border-radius: 14px;
    background: rgba(255, 255, 255, 0.03);
    border: 1px solid var(--line);
}

.connection-session-copy {
    display: flex;
    flex-direction: column;
    gap: 6px;
}

.connection-session-copy h3 {
    margin: 0;
    font-size: 1.05rem;
}

.connection-session-copy p {
    margin: 0;
    color: var(--muted);
    font-size: 0.92rem;
}

.form-grid {
    display: grid;
    gap: 12px;
}

.field {
    display: grid;
    gap: 8px;
}

.field span {
    font-size: 0.88rem;
    color: #ced8e4;
    font-weight: 600;
}

.field input,
.chat-input {
    width: 100%;
    min-height: 44px;
    padding: 0 14px;
    border-radius: var(--radius-sm);
    border: 1px solid rgba(255, 255, 255, 0.08);
    background: rgba(255, 255, 255, 0.03);
    color: var(--text);
    outline: none;
    transition: border-color 140ms ease, background 140ms ease, box-shadow 140ms ease;
}

.field input:focus,
.chat-input:focus {
    border-color: var(--accent-strong);
    box-shadow: 0 0 0 4px rgba(47, 143, 255, 0.16);
    background: rgba(255, 255, 255, 0.05);
}

.primary-button,
.secondary-button,
.ghost-button,
.panel-collapse-button {
    min-height: 44px;
    padding: 0 16px;
    border-radius: 12px;
    border: 1px solid transparent;
    color: white;
    cursor: pointer;
    font-weight: 700;
    transition: transform 120ms ease, opacity 120ms ease, background 120ms ease, border-color 120ms ease;
}

.primary-button:hover,
.secondary-button:hover,
.ghost-button:hover,
.panel-collapse-button:hover {
    transform: translateY(-1px);
}

.primary-button:disabled,
.secondary-button:disabled,
.ghost-button:disabled,
.panel-collapse-button:disabled {
    cursor: not-allowed;
    opacity: 0.45;
    transform: none;
}

.primary-button {
    background: linear-gradient(180deg, var(--accent), var(--accent-strong));
    box-shadow: 0 12px 28px rgba(47, 143, 255, 0.24);
}

.secondary-button {
    background: rgba(255, 255, 255, 0.05);
    border-color: rgba(255, 255, 255, 0.09);
}

.ghost-button,
.panel-collapse-button {
    background: transparent;
    border-color: rgba(255, 255, 255, 0.12);
}

.action-grid {
    display: grid;
    grid-template-columns: repeat(3, minmax(0, 1fr));
    gap: 10px;
}

.action-grid-button {
    width: 100%;
    min-width: 0;
    text-align: center;
}

.action-grid-button-wide-label {
    font-size: 0.88rem;
}

.panel-collapse-button {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 44px;
    min-width: 44px;
    padding: 0;
    flex-shrink: 0;
}

.panel-collapse-button-icon {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    font-size: 1rem;
    line-height: 1;
    transition: transform 140ms ease;
}

.panel-card.is-collapsed .panel-collapse-button-icon {
    transform: rotate(-90deg);
}

.stats-grid {
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 12px;
    margin: 0;
}

.stats-grid div {
    padding: 12px;
    border-radius: 14px;
    background: rgba(255, 255, 255, 0.03);
    border: 1px solid var(--line);
}

.stats-grid dt {
    font-size: 0.76rem;
    color: var(--muted);
    margin-bottom: 8px;
    text-transform: uppercase;
    letter-spacing: 0.04em;
}

.stats-grid dd {
    margin: 0;
    font-weight: 700;
    word-break: break-word;
}

.chat-card,
.activity-card {
    min-height: 0;
}

.chat-log,
.activity-log {
    flex: 1;
    min-height: 0;
    max-height: none;
    overflow: auto;
    padding: 12px;
    border-radius: 14px;
    border: 1px solid var(--line);
    background: rgba(255, 255, 255, 0.025);
}

.chat-log {
    display: flex;
    flex-direction: column;
    gap: 10px;
}

.chat-message {
    padding: 12px 14px;
    border-radius: 14px;
    background: rgba(255, 255, 255, 0.04);
    border: 1px solid rgba(255, 255, 255, 0.06);
}

.chat-message.self {
    background: rgba(83, 166, 255, 0.12);
    border-color: rgba(83, 166, 255, 0.22);
}

.chat-message-header {
    display: flex;
    justify-content: space-between;
    gap: 12px;
    margin-bottom: 6px;
    font-size: 0.82rem;
    color: var(--muted);
}

.chat-message-body {
    white-space: pre-wrap;
    word-break: break-word;
    line-height: 1.45;
}

.chat-input-row {
    display: grid;
    grid-template-columns: minmax(0, 1fr) auto;
    gap: 10px;
    margin-top: 12px;
    flex-shrink: 0;
}

.activity-log {
    display: flex;
    flex-direction: column;
    gap: 10px;
}

.activity-entry {
    padding: 10px 12px;
    border-radius: 12px;
    border: 1px solid rgba(255, 255, 255, 0.06);
    background: rgba(255, 255, 255, 0.03);
}

.activity-entry strong {
    display: block;
    margin-bottom: 4px;
    font-size: 0.88rem;
}

.activity-entry span {
    display: block;
    color: var(--muted);
    font-size: 0.8rem;
    margin-bottom: 4px;
}

.activity-entry p {
    margin: 0;
    color: #d8e2ee;
    line-height: 1.4;
    word-break: break-word;
}

.hidden-panel .layout-grid {
    grid-template-columns: minmax(0, 1fr);
}

.hidden-panel .control-panel {
    display: none;
}

@media (max-width: 1280px) {
    html,
    body {
        overflow: auto;
    }

    .app-shell {
        height: auto;
        min-height: 100dvh;
        overflow: visible;
    }

    .layout-grid {
        grid-template-columns: 1fr;
        overflow: visible;
    }

    .control-panel {
        display: flex;
        flex-direction: column;
        overflow: visible;
    }

    .viewer-frame {
        min-height: 420px;
    }
}

@media (max-width: 768px) {
    .app-shell {
        padding: 10px;
    }

    .topbar,
    .panel-card {
        padding: 14px;
    }

    .topbar {
        flex-direction: column;
        align-items: stretch;
    }

    .topbar-actions {
        width: 100%;
    }

    .topbar-actions button {
        width: 100%;
    }

    .action-grid {
        grid-template-columns: repeat(2, minmax(0, 1fr));
    }

    .stats-grid {
        grid-template-columns: 1fr;
    }

    .chat-input-row {
        grid-template-columns: 1fr;
    }
}

@media (max-width: 520px) {
    .action-grid {
        grid-template-columns: 1fr;
    }
}

.remote-cursor {
    position: absolute;
    left: -1000px;
    top: -1000px;
    width: 14px;
    height: 14px;
    border: 2px solid rgba(255, 255, 255, 0.96);
    border-radius: 50%;
    background: rgba(83, 166, 255, 0.18);
    box-shadow:
        0 0 0 1px rgba(0, 0, 0, 0.65),
        0 6px 18px rgba(0, 0, 0, 0.35);
    transform: translate(-50%, -50%);
    pointer-events: none;
    opacity: 0;
    z-index: 6;
    transition: opacity 80ms linear;
}

.remote-cursor::after {
    content: '';
    position: absolute;
    left: 50%;
    top: 50%;
    width: 4px;
    height: 4px;
    background: rgba(255, 255, 255, 0.96);
    border-radius: 50%;
    transform: translate(-50%, -50%);
}

.remote-cursor.visible {
    opacity: 1;
}