:root {
    --toolbar-height: 64px;
    --media-width: min(760px, calc(50vw - 48px));
    --media-height: auto;
    --bg: #111317;
    --panel: rgba(25, 28, 35, 0.88);
    --text: #f5f7fb;
    --muted: #a9b0c2;
    --accent: #ffcc00;
    --danger: #e55757;
}

* { box-sizing: border-box; }

body {
    margin: 0;
    font-family: system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
    color: var(--text);
    background: var(--bg);
    overflow: hidden;
}

.toolbar {
    height: var(--toolbar-height);
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 0 16px;
    background: #171a21;
    border-bottom: 1px solid rgba(255,255,255,0.08);
    z-index: 50;
    position: relative;
}

.brand {
    font-weight: 700;
    margin-right: 8px;
    letter-spacing: 0.2px;
}

.button, button {
    border: 1px solid rgba(255,255,255,0.14);
    background: rgba(255,255,255,0.08);
    color: var(--text);
    padding: 9px 14px;
    border-radius: 10px;
    cursor: pointer;
    font: inherit;
    user-select: none;
}

.button:hover, button:hover { background: rgba(255,255,255,0.14); }
.button:disabled, button:disabled { opacity: 0.45; cursor: not-allowed; }
.record { background: rgba(229, 87, 87, 0.22); border-color: rgba(229,87,87,0.45); }
.record.recording { background: rgba(229, 87, 87, 0.72); }
.status { color: var(--muted); font-size: 14px; margin-left: auto; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

.microphone-controls {
    display: flex;
    align-items: center;
    gap: 8px;
    flex: 0 0 auto;
    color: var(--muted);
    font-size: 13px;
}

.microphone-label {
    color: var(--muted);
    font-size: 13px;
}

#microphoneSelect, #dialogMicrophoneSelect {
    width: 180px;
    border: 1px solid rgba(255,255,255,0.14);
    background: rgba(255,255,255,0.08);
    color: var(--text);
    padding: 8px 10px;
    border-radius: 10px;
    font: inherit;
    outline: none;
}

#microphoneSelect:focus, #dialogMicrophoneSelect:focus { border-color: rgba(255,204,0,0.55); }
#microphoneSelect:disabled, #dialogMicrophoneSelect:disabled { opacity: 0.48; cursor: not-allowed; }

.microphone-enable {
    display: flex;
    align-items: center;
    gap: 6px;
    white-space: nowrap;
    user-select: none;
}

.microphone-enable input { margin: 0; }

.youtube-loader {
    display: flex;
    align-items: center;
    gap: 8px;
    min-width: 280px;
    max-width: 420px;
    flex: 1 1 320px;
}

#youtubeUrlInput {
    width: 100%;
    min-width: 150px;
    border: 1px solid rgba(255,255,255,0.14);
    background: rgba(255,255,255,0.08);
    color: var(--text);
    padding: 9px 12px;
    border-radius: 10px;
    font: inherit;
    outline: none;
}

#youtubeUrlInput::placeholder { color: var(--muted); }
#youtubeUrlInput:focus { border-color: rgba(255,204,0,0.55); }

.stage {
    position: relative;
    height: calc(100vh - var(--toolbar-height));
    width: 100vw;
    overflow: hidden;
    background: radial-gradient(circle at center, #242933 0%, #111317 70%);
}

.empty-state {
    height: 100%;
    display: grid;
    place-content: center;
    text-align: center;
    color: var(--muted);
}
.empty-state h1 { color: var(--text); margin-bottom: 8px; }

.pdf-viewer {
    height: 100%;
    width: 100%;
    display: grid;
    grid-template-rows: minmax(0, 1fr) 54px;
    align-items: center;
    justify-items: center;
}

.stage.media-layout .pdf-viewer {
    width: 50vw;
}

.canvas-wrap {
    position: relative;
    max-width: 100vw;
    max-height: calc(100vh - var(--toolbar-height) - 54px);
    overflow: hidden;
    display: grid;
    place-items: center;
}

.stage.media-layout .canvas-wrap {
    max-width: 50vw;
}

#pdfCanvas {
    display: block;
    background: white;
    box-shadow: 0 12px 48px rgba(0,0,0,0.55);
    max-width: 100vw;
    max-height: calc(100vh - var(--toolbar-height) - 54px);
}

.stage.media-layout #pdfCanvas {
    max-width: 50vw;
}

.measure-highlight {
    position: absolute;
    pointer-events: none;
    width: 230px;
    height: 82px;
    transform: translate(-50%, -50%);
    border-radius: 50%;
    background: radial-gradient(circle at center,
        rgba(255, 230, 0, 0.62) 0%,
        rgba(255, 215, 0, 0.34) 38%,
        rgba(255, 180, 0, 0.12) 72%,
        rgba(255, 180, 0, 0.00) 100%);
    border: 1px solid rgba(255, 190, 0, 0.24);
    mix-blend-mode: multiply;
}

.page-controls {
    height: 54px;
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 12px;
    width: min(880px, 96vw);
    padding: 8px 12px;
    background: rgba(0,0,0,0.26);
    border-radius: 14px 14px 0 0;
}

.stage.media-layout .page-controls {
    width: min(880px, calc(50vw - 24px));
}

#pageSlider { width: min(620px, 64vw); }
#pageIndicator { color: var(--muted); font-size: 14px; min-width: 100px; text-align: right; }
.nav-button { width: 40px; height: 36px; padding: 0; font-size: 28px; line-height: 1; }

.media-panel {
    position: absolute;
    left: 75%;
    top: 50%;
    bottom: auto;
    transform: translate(-50%, -50%);
    z-index: 30;
    width: var(--media-width);
}

.media-shell {
    background: var(--panel);
    border: 1px solid rgba(255,255,255,0.12);
    border-radius: 18px;
    box-shadow: 0 12px 40px rgba(0,0,0,0.48);
    padding: 10px;
    backdrop-filter: blur(10px);
}

.media-view {
    width: 100%;
    aspect-ratio: 16 / 9;
    height: auto;
    background: black;
    border: 0;
    border-radius: 12px;
    display: block;
    object-fit: contain;
}

.mini-controls {
    display: flex;
    justify-content: center;
    gap: 8px;
    margin-top: 8px;
}
.mini-controls button { padding: 7px 12px; font-size: 14px; }

.countdown {
    position: absolute;
    inset: 0;
    display: grid;
    place-items: center;
    font-size: clamp(90px, 20vw, 240px);
    font-weight: 800;
    color: white;
    text-shadow: 0 8px 44px rgba(0,0,0,0.8);
    background: rgba(0,0,0,0.22);
    z-index: 100;
}

.hidden { display: none !important; }


@media (max-width: 900px) {
    .stage.media-layout .pdf-viewer { width: 100%; }
    .stage.media-layout .canvas-wrap { max-width: 100vw; }
    .stage.media-layout #pdfCanvas { max-width: 100vw; }
    .stage.media-layout .page-controls { width: min(880px, 96vw); }
    .media-panel {
        left: 50%;
        top: auto;
        bottom: 70px;
        width: min(360px, calc(100vw - 28px));
        transform: translateX(-50%);
    }
}

@media (max-width: 1200px) {
    .toolbar { gap: 8px; padding: 0 10px; }
    .brand { display: none; }
    .youtube-loader { min-width: 210px; max-width: 300px; flex-basis: 260px; }
    #microphoneSelect, #dialogMicrophoneSelect { width: 150px; }
    .microphone-label { display: none; }
    .microphone-enable { font-size: 12px; }
}

@media (max-width: 900px) {
    .toolbar { gap: 6px; padding: 0 8px; }
    .button, button { padding: 8px 10px; font-size: 13px; }
    .youtube-loader { min-width: 190px; flex-basis: 230px; }
    .status { display: none; }
    .microphone-controls { gap: 6px; }
    #microphoneSelect, #dialogMicrophoneSelect { width: 130px; }
    .microphone-enable span { max-width: 150px; overflow: hidden; text-overflow: ellipsis; }
    :root { --media-width: min(520px, calc(50vw - 28px)); }
}

.mobile-page-button {
    display: none;
}

@media (max-width: 900px) {
    .media-panel {
        left: 50%;
        top: auto;
        bottom: 64px;
        width: min(180px, 44vw);
        transform: translateX(-50%);
        z-index: 35;
    }

    .media-shell {
        border-radius: 12px;
        padding: 5px;
    }

    .media-view {
        border-radius: 8px;
    }

    .mini-controls {
        gap: 4px;
        margin-top: 4px;
    }

    .mini-controls button {
        padding: 4px 7px;
        font-size: 11px;
        border-radius: 8px;
    }

    .mobile-page-button {
        position: absolute;
        display: grid;
        place-items: center;
        top: calc(50% - 20px);
        width: 34px;
        height: 52px;
        padding: 0;
        border-radius: 999px;
        z-index: 40;
        font-size: 34px;
        line-height: 1;
        background: rgba(0,0,0,0.34);
        border-color: rgba(255,255,255,0.24);
        backdrop-filter: blur(6px);
    }

    .mobile-page-button-left {
        left: 6px;
    }

    .mobile-page-button-right {
        right: 6px;
    }

    .mobile-page-button:disabled {
        opacity: 0.18;
    }
}

@media (max-width: 420px) {
    .media-panel {
        bottom: 60px;
        width: min(150px, 42vw);
    }

    .mini-controls button {
        padding: 3px 5px;
        font-size: 10px;
    }

    .mobile-page-button {
        width: 30px;
        height: 46px;
        font-size: 30px;
    }
}

.microphone-dialog {
    width: min(460px, calc(100vw - 32px));
    border: 1px solid rgba(255,255,255,0.18);
    border-radius: 18px;
    background: #171a21;
    color: var(--text);
    padding: 0;
    box-shadow: 0 24px 90px rgba(0,0,0,0.65);
}

.microphone-dialog::backdrop {
    background: rgba(0,0,0,0.62);
}

.microphone-dialog-card {
    display: grid;
    gap: 12px;
    padding: 22px;
}

.microphone-dialog-card h2 {
    margin: 0;
    font-size: 20px;
}

.microphone-dialog-card p {
    margin: 0;
    color: var(--muted);
    line-height: 1.35;
}

.dialog-field {
    font-size: 13px;
    color: var(--muted);
}

#dialogMicrophoneSelect {
    width: 100%;
}

.dialog-actions {
    display: flex;
    justify-content: flex-end;
    gap: 10px;
    margin-top: 4px;
}


@media (max-width: 640px) {
    :root {
        --toolbar-height: 156px;
    }

    .toolbar {
        height: var(--toolbar-height);
        flex-wrap: wrap;
        align-content: center;
        align-items: center;
        gap: 8px;
        padding: 8px;
    }

    .toolbar > .button,
    .toolbar > button {
        flex: 0 0 auto;
    }

    .youtube-loader {
        order: 20;
        display: flex;
        flex: 1 0 100%;
        width: 100%;
        max-width: none;
        min-width: 0;
    }

    .microphone-controls {
        order: 30;
        display: flex;
        flex: 1 0 100%;
        width: 100%;
        max-width: none;
        justify-content: center;
        gap: 8px;
    }

    .microphone-label {
        display: inline;
    }

    #microphoneSelect {
        width: min(190px, 48vw);
    }

    .microphone-enable span {
        max-width: none;
        overflow: visible;
        text-overflow: clip;
    }

    #youtubeUrlInput {
        min-width: 0;
        flex: 1 1 auto;
    }

    #loadYoutubeButton {
        flex: 0 0 auto;
        white-space: nowrap;
    }
}

@media (max-width: 420px) {
    :root {
        --toolbar-height: 164px;
    }

    .button, button {
        padding: 7px 8px;
        font-size: 12px;
    }

    .youtube-loader {
        gap: 6px;
    }

    .microphone-controls {
        gap: 6px;
        font-size: 12px;
    }

    #microphoneSelect {
        width: min(170px, 46vw);
        padding: 7px 8px;
        font-size: 12px;
    }

    #youtubeUrlInput {
        padding: 8px 9px;
        font-size: 13px;
    }
}


/* iPad/tablet fixes */
.stage {
    height: calc(100dvh - var(--toolbar-height));
}

.canvas-wrap,
#pdfCanvas {
    max-height: calc(100dvh - var(--toolbar-height) - 54px);
}

.microphone-enable {
    min-height: 36px;
    padding: 4px 6px;
    border-radius: 10px;
}

.microphone-enable input {
    width: 18px;
    height: 18px;
}

@media (max-width: 900px) {
    .media-panel {
        bottom: calc(82px + env(safe-area-inset-bottom, 0px));
        width: min(160px, 40vw);
        max-height: calc(100dvh - var(--toolbar-height) - 120px);
    }

    .media-view {
        max-height: calc(100dvh - var(--toolbar-height) - 170px);
    }

    .mini-controls {
        flex-wrap: nowrap;
    }

    .mini-controls button {
        padding: 3px 6px;
        font-size: 10px;
    }
}

@media (max-width: 420px) {
    .media-panel {
        bottom: calc(74px + env(safe-area-inset-bottom, 0px));
        width: min(140px, 40vw);
    }
}


/* Draggable media preview. This affects only the on-screen preview, not the recording layout. */
.media-shell {
    position: relative;
}

.media-drag-handle {
    width: 100%;
    display: flex;
    align-items: center;
    justify-content: center;
    min-height: 26px;
    padding: 4px 8px;
    margin: 0 0 6px 0;
    border-radius: 10px;
    background: rgba(255,255,255,0.08);
    color: var(--muted);
    font-size: 12px;
    cursor: grab;
    touch-action: none;
}

.media-drag-handle:active,
.media-dragging .media-drag-handle {
    cursor: grabbing;
    background: rgba(255,255,255,0.16);
    color: var(--text);
}

.media-panel.media-dragged {
    position: absolute;
    z-index: 45;
}

.media-panel.media-dragging {
    user-select: none;
}

@media (max-width: 900px) {
    .media-drag-handle {
        min-height: 22px;
        padding: 2px 6px;
        margin-bottom: 3px;
        font-size: 10px;
    }
}


.recording-download-dialog {
    width: min(520px, calc(100vw - 32px));
    border: 1px solid rgba(255,255,255,0.18);
    border-radius: 18px;
    background: #171a21;
    color: var(--text);
    padding: 0;
    box-shadow: 0 24px 90px rgba(0,0,0,0.65);
}

.recording-download-dialog::backdrop {
    background: rgba(0,0,0,0.62);
}

.recording-download-card {
    display: grid;
    gap: 14px;
    padding: 22px;
}

.recording-download-card h2 {
    margin: 0;
    font-size: 20px;
}

.recording-download-card p {
    margin: 0;
    color: var(--muted);
    line-height: 1.4;
}

.recording-download-actions {
    display: flex;
    flex-wrap: wrap;
    justify-content: flex-end;
    gap: 10px;
    margin-top: 4px;
}

.recording-download-actions a {
    text-decoration: none;
}

@media (max-width: 520px) {
    .recording-download-actions {
        justify-content: stretch;
    }

    .recording-download-actions .button,
    .recording-download-actions button {
        flex: 1 1 100%;
        text-align: center;
    }
}
