:root {
    color-scheme: dark;
    --bg: #191d21;
    --panel: #252b31;
    --panel-dark: #1f2429;
    --stage: #f2f4f5;
    --topbar: #363f47;
    --text: #f1f1f1;
    --muted: #aab4be;
    --line: rgba(255, 255, 255, 0.14);
    --button: #f4f4f4;
    --button-text: #20262c;
    --accent: #49c6d8;
    --danger: #e16b5f;
}

* {
    box-sizing: border-box;
}

html,
body {
    width: 100%;
    height: 100%;
    margin: 0;
    overflow: hidden;
    background: var(--bg);
    font-family: Arial, Helvetica, sans-serif;
}

button,
input,
select,
textarea {
    font: inherit;
}

.app {
    position: relative;
    display: grid;
    grid-template-columns: minmax(0, 1fr) 300px;
    width: 100vw;
    height: 100vh;
    background:
        linear-gradient(135deg, rgba(73, 198, 216, 0.08), transparent 34%),
        linear-gradient(315deg, rgba(242, 184, 75, 0.06), transparent 28%),
        var(--bg);
    overflow: hidden;
}

.ambient-grid {
    position: absolute;
    inset: 0;
    z-index: 0;
    opacity: 0.16;
    background-image:
        linear-gradient(rgba(255, 255, 255, 0.08) 1px, transparent 1px),
        linear-gradient(90deg, rgba(255, 255, 255, 0.08) 1px, transparent 1px);
    background-size: 42px 42px;
    pointer-events: none;
}

.app.connected {
    grid-template-columns: minmax(0, 1fr) 0;
}

.app.connected.editing {
    grid-template-columns: minmax(0, 1fr) 0;
}

.app.connected:not(.editing) .side-panel {
    display: none;
}

.app.connected .workspace {
    display: grid;
    place-items: center;
    padding: 0;
    grid-column: 1 / -1;
}

.app.connected .stage {
    width: min(100vw, calc(100vh * 16 / 9));
    height: auto;
    max-width: 100vw;
    max-height: 100vh;
    aspect-ratio: 16 / 9;
    border: 0;
    border-radius: 0;
}

.app.connected .side-panel > .panel-section:not(.tools) {
    display: none;
}

.app.connected.editing .side-panel {
    position: absolute;
    display: grid;
    grid-template-rows: auto minmax(0, 1fr) auto;
    gap: 12px;
    top: 18px;
    right: 18px;
    bottom: 18px;
    width: 300px;
    margin: 0;
    z-index: 7;
}

.workspace {
    position: relative;
    z-index: 1;
    min-width: 0;
    min-height: 0;
    padding: 18px;
}

.status {
    margin-top: 6px;
    color: var(--muted);
    font-size: 13px;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.side-panel button,
.stream-item button {
    height: 42px;
    border: 0;
    border-radius: 2px;
    background: var(--button);
    color: var(--button-text);
    cursor: pointer;
    font-weight: 700;
    transition: background-color 120ms ease, color 120ms ease, transform 120ms ease;
}

.side-panel button:hover,
.stream-item button:hover,
.edit-button:hover {
    background: #ffffff;
    color: #222;
}

.side-panel button:active,
.stream-item button:active,
.edit-button:active {
    transform: translateY(1px);
}

.side-panel button:disabled,
.stream-item button:disabled {
    opacity: 0.55;
    cursor: default;
}

.stage {
    position: relative;
    width: 100%;
    height: 100%;
    min-width: 0;
    min-height: 0;
    overflow: hidden;
    border: 1px solid rgba(255, 255, 255, 0.12);
    border-radius: 8px;
    background:
        linear-gradient(0deg, rgba(0, 0, 0, 0.04), transparent),
        var(--stage);
    box-shadow: 0 18px 44px rgba(0, 0, 0, 0.24);
}

#remoteVideo {
    width: 100%;
    height: 100%;
    object-fit: cover;
    background: #fff;
}

.empty-video {
    position: absolute;
    inset: 0;
    display: grid;
    place-items: center;
    color: #6d7780;
    font-size: 26px;
    font-weight: 700;
    pointer-events: none;
}

.overlay-svg {
    position: absolute;
    inset: 0;
    width: 100%;
    height: 100%;
    pointer-events: auto;
    user-select: none;
}

.edit-button {
    position: absolute;
    right: 32px;
    bottom: 32px;
    display: none;
    width: 74px;
    height: 60px;
    border: 1px solid #d0d0d0;
    border-radius: 2px;
    background: #fff;
    color: #444;
    font-size: 22px;
    cursor: pointer;
    transition: background-color 120ms ease, color 120ms ease, transform 120ms ease;
}

.stage.stream-active .edit-button {
    display: block;
}

.app.connected.editing .edit-button {
    display: none;
}

.side-panel {
    position: relative;
    z-index: 1;
    display: grid;
    grid-template-rows: auto minmax(0, 1fr) auto;
    gap: 22px;
    margin: 18px 18px 18px 0;
    padding: 22px 16px;
    border: 1px solid var(--line);
    border-radius: 8px;
    background: rgba(37, 43, 49, 0.92);
    color: var(--text);
    box-shadow: 0 18px 44px rgba(0, 0, 0, 0.22);
}

.panel-section {
    display: grid;
    gap: 10px;
}

.streams {
    align-self: end;
}

.panel-section label,
.panel-section span,
.section-title {
    color: var(--text);
    font-size: 18px;
    font-weight: 700;
}

input,
select,
textarea {
    width: 100%;
    border: 0;
    border-radius: 2px;
    background: #f5f5f5;
    color: #333;
    padding: 9px 10px;
}

input[type="color"] {
    height: 34px;
    padding: 0;
}

textarea {
    resize: vertical;
}

.stream-list {
    display: grid;
    align-content: start;
    min-height: 0;
    overflow: auto;
}

.stream-list-wrapper {
    position: absolute;
    top: 18px;
    left: 18px;
    right: 18px;
    z-index: 4;
    display: none;
}

.stream-list-wrapper.visible {
    display: block;
}

.top-stream-list {
    gap: 12px;
    overflow: visible;
}

.stream-item {
    display: grid;
    grid-template-columns: minmax(190px, 1fr) auto 160px;
    align-items: center;
    gap: 32px;
    min-height: 92px;
    padding: 18px 32px;
    border: 1px solid rgba(255, 255, 255, 0.12);
    border-radius: 8px;
    background: rgba(54, 63, 71, 0.94);
    cursor: pointer;
    box-shadow: 0 14px 34px rgba(0, 0, 0, 0.2);
    transition: background-color 120ms ease, border-color 120ms ease, outline-color 120ms ease;
}

.stream-item:hover {
    border-color: rgba(73, 198, 216, 0.5);
    background: #43505a;
}

.stream-item.selected {
    outline: 2px solid var(--accent);
    outline-offset: -2px;
}

.stream-item.connected {
    background: #6f6f6f;
}

.stream-title {
    color: #fff;
    font-size: 25px;
    font-weight: 700;
}

.stream-users {
    color: var(--muted);
    font-size: 20px;
    font-weight: 700;
    white-space: nowrap;
}

.stream-placeholder {
    display: grid;
    align-items: center;
    height: 100%;
    padding: 0 32px;
    color: #e8e8e8;
    font-size: 24px;
    font-weight: 700;
}

.tools {
    display: none;
    align-self: start;
    grid-template-rows: auto auto auto auto auto;
    padding: 16px;
    border-top: 1px solid var(--line);
    border-radius: 6px;
    background: var(--panel);
}

.side-panel.editing .tools {
    display: grid;
}

.tool-row {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 18px;
}

.tool-row label {
    display: grid;
    gap: 8px;
}

.tool-grid {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 14px;
}

.tool-grid button {
    min-height: 48px;
}

.side-panel button.debug-toggle {
    appearance: none;
    justify-self: start;
    height: auto;
    min-height: 0;
    padding: 0;
    border: 0;
    background: transparent;
    color: #8d98a3;
    cursor: pointer;
    font-size: 12px;
    font-weight: 400;
    line-height: 1;
    text-decoration: underline;
    text-underline-offset: 3px;
    opacity: 0.62;
    transform: none;
}

.side-panel button.debug-toggle:hover,
.side-panel button.debug-toggle.active {
    background: transparent;
    color: #c4cbd2;
    opacity: 1;
}

.side-panel button.debug-toggle:active {
    transform: none;
}

.debug-panel {
    display: grid;
    gap: 14px;
    min-height: 0;
}

.debug-panel[hidden] {
    display: none;
}

.debug-actions {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 12px;
}

.debug-actions button {
    min-height: 38px;
    font-size: 13px;
}

.overlay-debug-panel {
    display: grid;
    gap: 8px;
    min-height: 0;
}

.calibration-panel {
    display: grid;
    gap: 8px;
    min-height: 0;
}

.calibration-status {
    min-height: 36px;
    color: var(--muted);
    font-size: 13px;
    line-height: 1.35;
}

#calibrationOutput {
    max-height: 220px;
    margin: 0;
    padding: 10px;
    overflow: auto;
    border: 1px solid var(--line);
    border-radius: 4px;
    background: rgba(10, 13, 16, 0.48);
    color: #f2d17b;
    font-family: Consolas, "Liberation Mono", monospace;
    font-size: 12px;
    line-height: 1.45;
    user-select: text;
    white-space: pre;
}

#overlayDebugOutput {
    max-height: 220px;
    margin: 0;
    padding: 10px;
    overflow: auto;
    border: 1px solid var(--line);
    border-radius: 4px;
    background: rgba(10, 13, 16, 0.48);
    color: #d6edf1;
    font-family: Consolas, "Liberation Mono", monospace;
    font-size: 12px;
    line-height: 1.45;
    user-select: text;
    white-space: pre;
}

.calibration-frame {
    fill: rgba(73, 198, 216, 0.08);
    stroke: #f2d17b;
    stroke-width: 3;
    stroke-dasharray: 14 9;
    pointer-events: none;
}

.app.calibrating .overlay-svg {
    cursor: crosshair;
}

#calibrationButton {
    background: var(--accent);
    color: #092127;
}

#calibrationButton:hover {
    background: #6ee0ee;
    color: #092127;
}

#disconnectButton {
    background: var(--danger);
    color: #fff;
}

#disconnectButton:hover {
    background: #f07d70;
    color: #fff;
}

#closeEditButton {
    width: 100%;
    background: #d6edf1;
    color: #092127;
}

#closeEditButton:hover {
    background: #ffffff;
    color: #092127;
}

.edit-panel-footer {
    align-self: end;
    display: none;
    gap: 10px;
    margin-top: auto;
    padding-top: 12px;
}

.side-panel.editing .edit-panel-footer {
    display: grid;
}

.overlay-object {
    cursor: move;
}

.overlay-object.selected {
    filter: drop-shadow(0 0 4px var(--accent));
}

.overlay-text-box {
    pointer-events: none;
    overflow: hidden;
}

.overlay-text {
    width: 100%;
    height: 100%;
    display: flex;
    align-items: center;
    justify-content: center;
    text-align: center;
    white-space: pre-wrap;
    overflow-wrap: anywhere;
    word-break: break-word;
    line-height: 1.1;
    pointer-events: none;
    font-family: Arial, Helvetica, sans-serif;
}

.text-editor {
    position: absolute;
    z-index: 6;
    border: 1px solid #ffffff;
    outline: 2px solid var(--accent);
    border-radius: 2px;
    background: rgba(255, 255, 255, 0.82);
    padding: 4px;
    text-align: center;
    font-family: Arial, Helvetica, sans-serif;
    line-height: 1.1;
    resize: none;
    overflow: hidden;
    overflow-wrap: anywhere;
    word-break: break-word;
}

.resize-handle {
    display: none;
    fill: #ffffff;
    stroke: var(--accent);
    stroke-width: 1;
    cursor: pointer;
}

.overlay-object.selected .resize-handle {
    display: block;
}

.preview {
    stroke-dasharray: 8 6;
    pointer-events: none;
}
