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

:root {
    --c-bg: #f5f5f5;
    --c-surface: rgba(255, 255, 255, 0.92);
    --c-text: #1b1b1f;
    --c-text-dim: #5f5f67;
    --c-border: #d4d4d8;
    --c-control: #e8e8ec;
    --c-control-hover: #dcdce0;
    --c-accent: #3478f6;
    --radius: 8px;
}

/* System dark preference (when user hasn't forced light) */
@media (prefers-color-scheme: dark) {
    :root:not([data-theme="light"]) {
        --c-bg: #111113;
        --c-surface: rgba(32, 32, 36, 0.92);
        --c-text: #e4e4e8;
        --c-text-dim: #8e8e93;
        --c-border: #38383c;
        --c-control: #2c2c30;
        --c-control-hover: #3a3a3e;
        --c-accent: #4a9eff;
    }
}

/* User forced dark */
:root[data-theme="dark"] {
    --c-bg: #111113;
    --c-surface: rgba(32, 32, 36, 0.92);
    --c-text: #e4e4e8;
    --c-text-dim: #8e8e93;
    --c-border: #38383c;
    --c-control: #2c2c30;
    --c-control-hover: #3a3a3e;
    --c-accent: #4a9eff;
}

html, body {
    height: 100%;
    overflow: hidden;
    background: var(--c-bg);
    color: var(--c-text);
    font-family: -apple-system, BlinkMacSystemFont, "SF Pro Text", system-ui, sans-serif;
    -webkit-font-smoothing: antialiased;
    -webkit-text-size-adjust: 100%;
}

/* Layout */
#app {
    display: flex;
    flex-direction: column;
    height: 100vh;
    height: 100dvh;
}

/* Toolbar */
#toolbar {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 8px;
    padding: 8px 12px;
    background: var(--c-surface);
    backdrop-filter: blur(20px);
    -webkit-backdrop-filter: blur(20px);
    border-bottom: 1px solid var(--c-border);
    flex-shrink: 0;
    z-index: 100;
    transition: transform 0.3s ease, opacity 0.3s ease;
}

.toolbar-group {
    display: flex;
    align-items: center;
    gap: 6px;
}

/* Select dropdown */
.select-wrap {
    position: relative;
    display: inline-flex;
}

#deviceSelect {
    appearance: none;
    -webkit-appearance: none;
    font-family: inherit;
    font-size: 14px;
    color: var(--c-text);
    background: var(--c-control);
    border: 1px solid var(--c-border);
    border-radius: var(--radius);
    padding: 6px 30px 6px 10px;
    max-width: 220px;
    min-width: 80px;
    cursor: pointer;
    transition: background 0.15s;
}

#deviceSelect:hover {
    background: var(--c-control-hover);
}

.select-wrap::after {
    content: '';
    position: absolute;
    right: 10px;
    top: 50%;
    transform: translateY(-50%);
    width: 0;
    height: 0;
    border-left: 4px solid transparent;
    border-right: 4px solid transparent;
    border-top: 5px solid var(--c-text-dim);
    pointer-events: none;
}

/* Buttons */
button {
    font-family: inherit;
    font-size: 14px;
    color: var(--c-text);
    background: var(--c-control);
    border: 1px solid var(--c-border);
    border-radius: var(--radius);
    padding: 6px 10px;
    min-height: 34px;
    min-width: 34px;
    cursor: pointer;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 4px;
    white-space: nowrap;
    transition: background 0.15s;
    -webkit-user-select: none;
    user-select: none;
}

button:hover {
    background: var(--c-control-hover);
}

button:active {
    transform: scale(0.96);
}

button svg {
    width: 16px;
    height: 16px;
    flex-shrink: 0;
}

/* Mirror checkbox label */
#mirrorLabel {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    font-size: 14px;
    cursor: pointer;
    -webkit-user-select: none;
    user-select: none;
    padding: 6px 10px;
    background: var(--c-control);
    border: 1px solid var(--c-border);
    border-radius: var(--radius);
    min-height: 34px;
    transition: background 0.15s;
}

#mirrorLabel:hover {
    background: var(--c-control-hover);
}

#mirrorCheck {
    margin: 0;
    accent-color: var(--c-accent);
}

/* Video area */
#videoContainer {
    flex: 1;
    position: relative;
    background: #000;
    display: flex;
    align-items: center;
    justify-content: center;
    overflow: hidden;
    min-height: 0;
}

#video {
    display: block;
    width: 100%;
    height: 100%;
    object-fit: contain;
    background: transparent;
}

#video.mirrored {
    transform: scaleX(-1);
}

/* Overlay */
#overlay {
    position: absolute;
    inset: 0;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 20px;
    background: rgba(0, 0, 0, 0.75);
    color: #fff;
    z-index: 50;
    padding: 24px;
    text-align: center;
}

#overlay.hidden {
    display: none;
}

#statusText {
    font-size: 17px;
    font-weight: 500;
    opacity: 0.9;
    max-width: 300px;
}

#startBtn {
    background: var(--c-accent);
    color: #fff;
    border: none;
    padding: 12px 36px;
    font-size: 16px;
    font-weight: 600;
    border-radius: 12px;
    min-height: 44px;
}

#startBtn:hover {
    opacity: 0.9;
    background: var(--c-accent);
}

#startBtn:active {
    opacity: 0.8;
}

#startBtn:disabled {
    opacity: 0.4;
    cursor: not-allowed;
    transform: none;
}

/* Fullscreen: auto-hide toolbar */
.is-fullscreen #toolbar {
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
}

.is-fullscreen #toolbar.toolbar-hidden {
    transform: translateY(-100%);
    opacity: 0;
    pointer-events: none;
}

.is-fullscreen #videoContainer {
    height: 100%;
}

/* Safe areas for notched devices */
@supports (padding-top: env(safe-area-inset-top)) {
    #toolbar {
        padding-top: calc(8px + env(safe-area-inset-top));
        padding-left: calc(12px + env(safe-area-inset-left));
        padding-right: calc(12px + env(safe-area-inset-right));
    }
}

/* Small screens */
@media (max-width: 480px) {
    .toolbar-group {
        gap: 4px;
    }

    #deviceSelect {
        max-width: 130px;
        font-size: 13px;
        padding: 5px 26px 5px 8px;
    }

    button {
        padding: 5px 8px;
        min-height: 32px;
        min-width: 32px;
        font-size: 13px;
    }

    #mirrorLabel {
        padding: 5px 8px;
        min-height: 32px;
        font-size: 13px;
    }

    #mirrorLabel span {
        display: none;
    }
}
