:root {
    --bg-color: #111111;
    --text-color: #ffffff;
    --accent-color: #3b82f6;
    --glass-bg: rgba(30, 30, 30, 0.6);
    --glass-border: rgba(255, 255, 255, 0.1);
    --font-main: 'Inter', sans-serif;
    --font-display: 'Outfit', sans-serif;
}

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

body {
    background-color: var(--bg-color);
    color: var(--text-color);
    font-family: var(--font-main);
    overflow: hidden;
    /* Hide scrollbars, we handle scrolling manually or via overflow-y */
    height: 100vh;
    width: 100vw;
}

#app {
    position: relative;
    width: 100%;
    height: 100%;
}

/* Teleprompter Area */
#prompter-container {
    width: 100%;
    height: 100%;
    overflow-y: hidden;
    /* We scroll the text element or use transform */
    position: relative;
    display: flex;
    justify-content: center;
}

#prompter-text {
    width: 100%;
    padding-top: 45vh;
    /* Start text in middle */
    padding-bottom: 45vh;
    padding-left: 10%;
    /* Controlled by margin setting */
    padding-right: 10%;
    /* Controlled by margin setting */
    font-size: 48px;
    line-height: 1.5;
    outline: none;
    white-space: pre-wrap;
    text-align: left;
    caret-color: var(--accent-color);
    /* Smooth scrolling is handled by JS, but we need this for manual scroll */
    overflow-y: auto;
    height: 100%;
    scrollbar-width: none;
    /* Firefox */
}

#prompter-text::-webkit-scrollbar {
    display: none;
    /* Chrome/Safari */
}

#prompter-text.mirror {
    transform: scaleX(-1);
}

/* Scroll Marker (Optional visual aid) */
#scroll-marker {
    position: absolute;
    left: 0;
    top: 40%;
    width: 20px;
    height: 2px;
    background: var(--accent-color);
    opacity: 0.5;
}

/* Floating Controls - Glassmorphism */
#controls-overlay {
    position: absolute;
    bottom: 20px;
    right: 20px;
    width: 320px;
    background: var(--glass-bg);
    backdrop-filter: blur(12px);
    -webkit-backdrop-filter: blur(12px);
    border: 1px solid var(--glass-border);
    border-radius: 16px;
    padding: 20px;
    box-shadow: 0 8px 32px rgba(0, 0, 0, 0.3);
    transition: opacity 0.3s ease, transform 0.3s ease;
    z-index: 100;
    display: flex;
    flex-direction: column;
    gap: 16px;
}

#controls-overlay.hidden {
    opacity: 0;
    transform: translateY(20px);
    pointer-events: none;
}

.controls-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 8px;
}

.controls-header h3 {
    font-family: var(--font-display);
    font-weight: 700;
    font-size: 1.2rem;
}

.control-group {
    display: flex;
    flex-direction: column;
    gap: 8px;
}

.control-group.row {
    flex-direction: row;
    justify-content: space-between;
}

.control-group label {
    font-size: 0.85rem;
    color: rgba(255, 255, 255, 0.7);
    display: flex;
    justify-content: space-between;
}

/* Inputs */
input[type="range"] {
    -webkit-appearance: none;
    width: 100%;
    height: 6px;
    background: rgba(255, 255, 255, 0.2);
    border-radius: 3px;
    outline: none;
}

input[type="range"]::-webkit-slider-thumb {
    -webkit-appearance: none;
    width: 16px;
    height: 16px;
    background: var(--accent-color);
    border-radius: 50%;
    cursor: pointer;
    transition: transform 0.1s;
}

input[type="range"]::-webkit-slider-thumb:hover {
    transform: scale(1.2);
}

input[type="color"] {
    -webkit-appearance: none;
    border: none;
    width: 40px;
    height: 40px;
    border-radius: 8px;
    background: none;
    cursor: pointer;
}

input[type="color"]::-webkit-color-swatch-wrapper {
    padding: 0;
}

input[type="color"]::-webkit-color-swatch {
    border: 1px solid var(--glass-border);
    border-radius: 4px;
}

/* Mobile Optimization */
@media (max-width: 768px) {
    #prompter-text {
        font-size: 32px;
        /* Smaller default font on mobile */
        padding-left: 5%;
        padding-right: 5%;
    }

    #controls-overlay {
        width: 100%;
        /* Full width on mobile */
        height: 100%;
        /* Full height */
        top: 0;
        left: 0;
        right: 0;
        bottom: 0;
        border-radius: 0;
        /* Remove border radius */
        overflow-y: auto;
        /* Enable scrolling */
        padding-bottom: 100px;
        /* Space for floating buttons */
        background: rgba(17, 17, 17, 0.95);
        /* Darker background for readability */
    }

    .floating-btn {
        width: 56px;
        height: 56px;
        font-size: 1.5rem;
    }

    /* Larger touch targets for inputs */
    input[type="range"]::-webkit-slider-thumb {
        width: 24px;
        height: 24px;
    }

    .control-group label {
        font-size: 1rem;
        /* Readable labels */
    }

    /* Visual indicator for Edit Mode */
    #prompter-text[contenteditable="true"] {
        border: 2px dashed var(--accent-color);
        border-radius: 8px;
        padding: 20px;
        /* Add some padding so border isn't tight */
        margin-top: 40vh;
        /* Adjust for padding */
        margin-bottom: 40vh;
    }
}

/* Landscape Mobile Optimization */
@media (max-height: 600px) {
    #controls-overlay {
        width: 100%;
        height: 100%;
        top: 0;
        left: 0;
        right: 0;
        bottom: 0;
        border-radius: 0;
        overflow-y: auto;
        padding-bottom: 100px;
        background: rgba(17, 17, 17, 0.95);
    }
}

/* Buttons */
.icon-btn {
    background: none;
    border: none;
    color: white;
    font-size: 1.2rem;
    cursor: pointer;
    opacity: 0.7;
    transition: opacity 0.2s;
}

.icon-btn:hover {
    opacity: 1;
}

.floating-actions {
    position: absolute;
    bottom: 20px;
    right: 20px;
    display: flex;
    gap: 10px;
    z-index: 90;
}

.floating-btn {
    position: relative;
    /* Reset absolute positioning */
    bottom: auto;
    right: auto;
    background: var(--glass-bg);
    backdrop-filter: blur(8px);
    width: 48px;
    height: 48px;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.2);
    border: 1px solid var(--glass-border);
}

.floating-btn.active {
    background: var(--accent-color);
    color: white;
    border-color: var(--accent-color);
}

.primary-btn {
    background: var(--accent-color);
    color: white;
    border: none;
    padding: 10px;
    border-radius: 8px;
    font-weight: 600;
    cursor: pointer;
    transition: background 0.2s;
}

.primary-btn:hover {
    background: #2563eb;
}

/* Toggle Switch */
.toggle-switch {
    position: relative;
    width: 40px;
    height: 20px;
}

.toggle-switch input {
    opacity: 0;
    width: 0;
    height: 0;
}

.toggle-switch label {
    position: absolute;
    cursor: pointer;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background-color: rgba(255, 255, 255, 0.2);
    transition: .4s;
    border-radius: 20px;
}

.toggle-switch label:before {
    position: absolute;
    content: "";
    height: 16px;
    width: 16px;
    left: 2px;
    bottom: 2px;
    background-color: white;
    transition: .4s;
    border-radius: 50%;
}

.toggle-switch input:checked+label {
    background-color: var(--accent-color);
}

.toggle-switch input:checked+label:before {
    transform: translateX(20px);
}

/* Alignment Buttons */
.alignment-buttons {
    display: flex;
    gap: 8px;
}

.align-btn {
    flex: 1;
    background: rgba(255, 255, 255, 0.1);
    border: 1px solid rgba(255, 255, 255, 0.2);
    color: white;
    padding: 8px 12px;
    border-radius: 6px;
    font-size: 0.85rem;
    cursor: pointer;
    transition: all 0.2s;
}

.align-btn:hover {
    background: rgba(255, 255, 255, 0.2);
}

.align-btn.active {
    background: var(--accent-color);
    border-color: var(--accent-color);
}

/* Modal */
.modal {
    display: flex;
    /* Hidden by default via class */
    position: fixed;
    z-index: 200;
    left: 0;
    top: 0;
    width: 100%;
    height: 100%;
    background-color: rgba(0, 0, 0, 0.8);
    align-items: center;
    justify-content: center;
    backdrop-filter: blur(5px);
}

.modal.hidden {
    display: none;
}

.modal-content {
    background-color: #1e1e1e;
    padding: 30px;
    border: 1px solid #333;
    border-radius: 16px;
    width: 90%;
    max-width: 400px;
    text-align: center;
    position: relative;
    color: white;
}

.close {
    position: absolute;
    top: 10px;
    right: 20px;
    color: #aaa;
    font-size: 28px;
    font-weight: bold;
    cursor: pointer;
}

.close:hover {
    color: white;
}

#qrcode {
    margin: 20px auto;
    background: white;
    padding: 10px;
    border-radius: 8px;
    width: fit-content;
}

.link-text {
    margin-top: 10px;
    font-size: 0.9rem;
    color: #aaa;
}

.link-text a {
    color: var(--accent-color);
    text-decoration: none;
}

.status {
    margin-top: 15px;
    font-size: 0.8rem;
    color: #666;
}

/* Play Status Overlay */
.status-overlay {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    pointer-events: none;
    z-index: 50;
    opacity: 0;
    transition: opacity 0.3s;
}

.status-overlay.visible {
    opacity: 1;
    animation: fadeOut 1s forwards;
}

#play-icon {
    font-size: 100px;
    color: rgba(255, 255, 255, 0.3);
}

@keyframes fadeOut {
    0% {
        opacity: 1;
        transform: translate(-50%, -50%) scale(1);
    }

    100% {
        opacity: 0;
        transform: translate(-50%, -50%) scale(1.5);
    }
}

/* Top Bar */
#top-bar {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 80px;
    background: linear-gradient(to bottom, rgba(0, 0, 0, 0.9), rgba(0, 0, 0, 0));
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 30px;
    z-index: 200;
    transition: transform 0.3s ease, opacity 0.3s ease;
    padding-top: 10px;
}

#top-bar.hidden {
    transform: translateY(-100%);
    opacity: 0;
    pointer-events: none;
}

#top-bar .icon-btn {
    font-size: 2rem;
    background: rgba(255, 255, 255, 0.1);
    width: 50px;
    height: 50px;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: background 0.2s, transform 0.1s;
}

#top-bar .icon-btn:hover {
    background: rgba(255, 255, 255, 0.3);
    transform: scale(1.1);
}

.top-speed-control {
    display: flex;
    align-items: center;
    gap: 15px;
    background: rgba(255, 255, 255, 0.1);
    padding: 10px 20px;
    border-radius: 30px;
    backdrop-filter: blur(5px);
}

.top-speed-control input[type="range"] {
    width: 200px;
}

/* Context Menu */
#context-menu {
    position: absolute;
    background: var(--glass-bg);
    backdrop-filter: blur(12px);
    -webkit-backdrop-filter: blur(12px);
    border: 1px solid var(--glass-border);
    border-radius: 8px;
    padding: 8px;
    display: flex;
    gap: 8px;
    align-items: center;
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.3);
    z-index: 300;
    transition: opacity 0.2s, transform 0.2s;
}

#context-menu.hidden {
    opacity: 0;
    pointer-events: none;
    transform: translateY(10px);
}

#context-menu button {
    background: none;
    border: none;
    color: white;
    font-size: 1.1rem;
    cursor: pointer;
    padding: 4px 8px;
    border-radius: 4px;
    transition: background 0.2s;
}

#context-menu button:hover {
    background: rgba(255, 255, 255, 0.2);
}

#context-menu .separator {
    width: 1px;
    height: 20px;
    background: rgba(255, 255, 255, 0.2);
}

#context-menu input[type="color"] {
    width: 24px;
    height: 24px;
    border-radius: 4px;
    border: none;
    padding: 0;
}

#context-menu input[type="color"]::-webkit-color-swatch {
    border: 1px solid rgba(255, 255, 255, 0.2);
    border-radius: 4px;
}

/* ---------- Follow my voice ---------- */
.hidden-group { display: none !important; }

#lang-select {
    width: 100%;
    padding: 9px 10px;
    border-radius: 8px;
    border: 1px solid var(--glass-border);
    background: rgba(255, 255, 255, 0.08);
    color: #fff;
    font: inherit;
    font-size: 0.9rem;
}
#lang-select option { color: #000; }
#lang-detected { opacity: 0.7; }
.hint { font-size: 0.78rem; color: rgba(255, 255, 255, 0.55); line-height: 1.4; }

/* Words already read, and the word being read */
::highlight(tp-read) { color: color-mix(in srgb, var(--text-color) 35%, transparent); }
::highlight(tp-current) { background-color: rgba(250, 204, 21, 0.35); }

body.voice-mode #scroll-marker { display: block; top: 40%; height: 4px; width: 14px; opacity: 0.9; border-radius: 0 3px 3px 0; }
body.voice-mode #top-speed-control { display: none; }
#top-mode-btn { font-size: 1.4rem !important; }
body.voice-mode #top-mode-btn { background: var(--accent-color) !important; }

#voice-status {
    position: fixed;
    top: 16px;
    left: 16px;
    z-index: 150;
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 6px 12px;
    border-radius: 999px;
    background: rgba(0, 0, 0, 0.6);
    color: rgba(255, 255, 255, 0.6);
    font-size: 0.85rem;
    pointer-events: none;
}
#voice-status.hidden { display: none; }
#voice-status .dot { width: 9px; height: 9px; border-radius: 50%; background: #666; }
#voice-status.on { color: #fff; }
#voice-status.on .dot { background: #4ade80; box-shadow: 0 0 8px #4ade80; }

#toast {
    position: fixed;
    left: 50%;
    top: 20px;
    transform: translateX(-50%);
    z-index: 300;
    max-width: calc(100% - 32px);
    padding: 10px 16px;
    border-radius: 12px;
    background: #fff;
    color: #000;
    font-size: 0.95rem;
    font-weight: 500;
    pointer-events: none;
}
#toast.hidden { display: none; }

/* Pasted structure */
#prompter-text ul, #prompter-text ol { padding-left: 1.2em; }

/* =========================================================
   Settings panel (refreshed)
   ========================================================= */
#controls-overlay {
    width: 340px;
    max-height: calc(100vh - 40px);
    overflow-y: auto;
    padding: 18px 18px 16px;
    gap: 0;
    background: rgba(24, 24, 27, 0.86);
    border-color: rgba(255, 255, 255, 0.08);
    border-radius: 18px;
    box-shadow: 0 20px 60px rgba(0, 0, 0, 0.5);
    scrollbar-width: thin;
    scrollbar-color: rgba(255, 255, 255, 0.15) transparent;
    font-size: 0.9rem;
}

.controls-header {
    margin: 0 0 14px;
}
.controls-header h3 {
    font-size: 1.15rem;
    letter-spacing: -0.01em;
}
.close-btn {
    width: 34px;
    height: 34px;
    border-radius: 50%;
    border: 0;
    display: grid;
    place-items: center;
    background: rgba(255, 255, 255, 0.08);
    color: rgba(255, 255, 255, 0.8);
    cursor: pointer;
    transition: background 0.15s, color 0.15s;
}
.close-btn:hover { background: rgba(255, 255, 255, 0.16); color: #fff; }

.settings-section {
    display: flex;
    flex-direction: column;
    gap: 12px;
    padding: 16px 0;
    border-top: 1px solid rgba(255, 255, 255, 0.07);
}
.settings-section:first-of-type { border-top: 0; padding-top: 0; }
.settings-section h4 {
    font-family: var(--font-main);
    font-size: 0.78rem;
    font-weight: 600;
    color: rgba(255, 255, 255, 0.45);
    margin: 0 0 -2px;
}

/* Play */
.play-btn {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 10px;
    width: 100%;
    height: 48px;
    border: 0;
    border-radius: 12px;
    background: #4ade80;
    color: #04210f;
    font: 600 1rem var(--font-main);
    cursor: pointer;
    transition: background 0.15s, transform 0.08s;
}
.play-btn:hover { background: #5ee68f; }
.play-btn:active { transform: scale(0.98); }
.play-btn.playing { background: #f5f5f5; color: #111; }
.play-glyph { font-size: 0.9rem; letter-spacing: -2px; }

.btn-grid {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 8px;
}
.secondary-btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
    height: 40px;
    padding: 0 12px;
    border-radius: 10px;
    border: 1px solid rgba(255, 255, 255, 0.1);
    background: rgba(255, 255, 255, 0.06);
    color: #fff;
    font: 500 0.88rem var(--font-main);
    cursor: pointer;
    transition: background 0.15s, border-color 0.15s;
}
.secondary-btn:hover { background: rgba(255, 255, 255, 0.12); }
.secondary-btn.active { background: var(--accent-color); border-color: var(--accent-color); }
.secondary-btn.wide { width: 100%; }
.secondary-btn svg { flex: none; opacity: 0.9; }

.text-btn {
    align-self: center;
    border: 0;
    background: none;
    color: rgba(255, 255, 255, 0.45);
    font: 500 0.82rem var(--font-main);
    cursor: pointer;
    padding: 4px 8px;
}
.text-btn:hover { color: rgba(255, 255, 255, 0.85); }
.text-btn.left { align-self: flex-start; padding: 0; color: var(--accent-color); }

/* Segmented controls (scroll mode, alignment) */
.alignment-buttons.seg {
    gap: 2px;
    padding: 3px;
    border-radius: 11px;
    background: rgba(255, 255, 255, 0.06);
}
.seg .align-btn {
    display: grid;
    place-items: center;
    height: 34px;
    padding: 0 8px;
    border: 0;
    border-radius: 8px;
    background: transparent;
    color: rgba(255, 255, 255, 0.7);
    font: 500 0.85rem var(--font-main);
    transition: background 0.15s, color 0.15s;
}
.seg .align-btn:hover { background: rgba(255, 255, 255, 0.08); color: #fff; }
.seg .align-btn.active { background: var(--accent-color); color: #fff; }

/* Sliders */
.control-group { gap: 10px; }
.control-group label {
    font-size: 0.85rem;
    color: rgba(255, 255, 255, 0.8);
    align-items: baseline;
}
.control-group label .val {
    font-variant-numeric: tabular-nums;
    color: rgba(255, 255, 255, 0.5);
}
#controls-overlay input[type="range"] {
    height: 4px;
    background: rgba(255, 255, 255, 0.15);
    border-radius: 2px;
    margin: 6px 0;
}
#controls-overlay input[type="range"]::-webkit-slider-thumb {
    width: 18px;
    height: 18px;
    background: #fff;
    box-shadow: 0 1px 4px rgba(0, 0, 0, 0.4);
}
#controls-overlay input[type="range"]::-moz-range-thumb {
    width: 18px;
    height: 18px;
    border: 0;
    border-radius: 50%;
    background: #fff;
}

/* Select */
.select-wrap { position: relative; }
.select-wrap::after {
    content: '';
    position: absolute;
    right: 14px;
    top: 50%;
    width: 7px;
    height: 7px;
    border-right: 1.6px solid rgba(255, 255, 255, 0.6);
    border-bottom: 1.6px solid rgba(255, 255, 255, 0.6);
    transform: translateY(-70%) rotate(45deg);
    pointer-events: none;
}
#controls-overlay select {
    -webkit-appearance: none;
    appearance: none;
    width: 100%;
    height: 40px;
    padding: 0 34px 0 12px;
    border-radius: 10px;
    border: 1px solid rgba(255, 255, 255, 0.1);
    background: rgba(255, 255, 255, 0.06);
    color: #fff;
    font: 500 0.88rem var(--font-main);
    cursor: pointer;
}
#controls-overlay select:disabled { opacity: 0.55; cursor: default; }
#controls-overlay select option { color: #000; }

.mic-level {
    height: 4px;
    border-radius: 2px;
    background: rgba(255, 255, 255, 0.08);
    overflow: hidden;
    margin-top: -4px;
}
.mic-level i {
    display: block;
    height: 100%;
    width: 0;
    background: #4ade80;
    transition: width 0.08s linear;
}

/* Rows: label left, control right */
.setting-row {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 12px;
    min-height: 36px;
    color: rgba(255, 255, 255, 0.85);
    font-size: 0.88rem;
    cursor: pointer;
}
.setting-row small {
    display: block;
    color: rgba(255, 255, 255, 0.4);
    font-size: 0.75rem;
}
.setting-row input[type="color"] {
    width: 44px;
    height: 28px;
    border-radius: 8px;
}
.setting-row input[type="color"]::-webkit-color-swatch { border-radius: 7px; border: 1px solid rgba(255, 255, 255, 0.2); }
.setting-row input[type="color"]::-moz-color-swatch { border-radius: 7px; border: 1px solid rgba(255, 255, 255, 0.2); }

.toggle-switch { width: 44px; height: 26px; flex: none; }
.toggle-switch label { border-radius: 13px; background-color: rgba(255, 255, 255, 0.18); }
.toggle-switch label:before { width: 20px; height: 20px; left: 3px; bottom: 3px; transition: 0.2s; }
.toggle-switch input:checked + label:before { transform: translateX(18px); }

.settings-footer { gap: 6px; padding-bottom: 0; }

#controls-overlay :focus-visible,
.floating-btn:focus-visible {
    outline: 2px solid var(--accent-color);
    outline-offset: 2px;
}

/* Bigger settings button */
.floating-actions { bottom: 24px; right: 24px; }
.floating-btn {
    width: 64px;
    height: 64px;
    color: rgba(255, 255, 255, 0.85);
    background: rgba(30, 30, 30, 0.7);
    border: 1px solid rgba(255, 255, 255, 0.12);
    box-shadow: 0 6px 20px rgba(0, 0, 0, 0.35);
    cursor: pointer;
    transition: background 0.15s, color 0.15s, transform 0.2s;
}
.floating-btn:hover { color: #fff; background: rgba(50, 50, 50, 0.85); transform: rotate(30deg); }

@media (max-width: 768px), (max-height: 600px) {
    #controls-overlay {
        width: 100%;
        max-height: none;
        padding: max(18px, env(safe-area-inset-top)) 20px 120px;
        background: rgba(17, 17, 18, 0.97);
        font-size: 1rem;
        border-radius: 0;
        border: 0;
    }
    #controls-overlay > * { width: 100%; max-width: 520px; margin-left: auto; margin-right: auto; }
    .control-group label { font-size: 0.95rem; }
    .floating-btn { width: 64px; height: 64px; }
}

@media (prefers-reduced-motion: reduce) {
    .floating-btn:hover { transform: none; }
}

/* Soft follow: no word marks; lines above the reading line fade gradually */
body.voice-mode.voice-soft #prompter-text:not([contenteditable="true"]) {
    -webkit-mask-image: linear-gradient(to bottom, rgba(0, 0, 0, 0.18) 0%, rgba(0, 0, 0, 0.3) 18%, rgba(0, 0, 0, 0.75) 32%, #000 40%, #000 100%);
            mask-image: linear-gradient(to bottom, rgba(0, 0, 0, 0.18) 0%, rgba(0, 0, 0, 0.3) 18%, rgba(0, 0, 0, 0.75) 32%, #000 40%, #000 100%);
}
body.voice-mode.voice-soft #scroll-marker { opacity: 0.45; }

/* Settings above the hover bar; header stays visible while the panel scrolls */
#controls-overlay {
    z-index: 250;
    max-height: calc(100dvh - 40px);
    padding-top: 0;
}
.controls-header {
    position: sticky;
    top: 0;
    z-index: 2;
    margin: 0 -18px 4px;
    padding: 16px 18px 10px;
    background: rgba(24, 24, 27, 0.97);
}
@media (max-width: 768px), (max-height: 600px) {
    #controls-overlay { height: 100dvh; max-height: 100dvh; padding-top: 0; }
    .controls-header {
        margin: 0 auto 4px;
        padding: max(16px, env(safe-area-inset-top)) 0 10px;
        background: rgba(17, 17, 18, 0.99);
    }
}
.controls-header { box-shadow: 0 -6px 0 rgba(24, 24, 27, 0.97); }
@media (max-width: 768px), (max-height: 600px) {
    .controls-header { box-shadow: 0 -6px 0 rgba(17, 17, 18, 0.99); }
}

/* Settings fade and slide in from the right edge on desktop */
@media (hover: hover) and (pointer: fine) and (min-width: 769px) and (min-height: 601px) {
    #controls-overlay {
        transition: opacity 0.28s ease, transform 0.28s ease;
    }
    #controls-overlay.hidden {
        transform: translateX(24px);
    }
}
@media (prefers-reduced-motion: reduce) {
    #controls-overlay, #controls-overlay.hidden { transition: opacity 0.2s ease; transform: none; }
}

/* Right-edge tab */
#edge-tab {
    position: fixed;
    right: 0;
    top: 50%;
    transform: translate(10px, -50%);
    z-index: 95;
    width: 26px;
    height: 72px;
    padding: 0 0 0 4px;
    display: grid;
    place-items: center start;
    border: 1px solid rgba(255, 255, 255, 0.12);
    border-right: 0;
    border-radius: 12px 0 0 12px;
    background: rgba(40, 40, 44, 0.55);
    color: rgba(255, 255, 255, 0.7);
    opacity: 0.35;
    cursor: pointer;
    transition: opacity 0.25s ease, transform 0.25s ease, background 0.2s;
}
#edge-tab.near,
#edge-tab:focus-visible {
    opacity: 1;
    transform: translate(0, -50%);
    background: rgba(50, 50, 56, 0.85);
}
#edge-tab:hover { background: var(--accent-color); color: #fff; }
/* Out of the way while settings are open, while presenting, and on touch screens */
#controls-overlay:not(.hidden) ~ #edge-tab { opacity: 0; pointer-events: none; transform: translate(100%, -50%); }
body.is-playing #edge-tab:not(.near) { opacity: 0.12; }
@media (hover: none), (pointer: coarse) { #edge-tab { display: none; } }

/* Top tab */
#top-tab {
    position: fixed;
    left: 50%;
    top: 0;
    transform: translate(-50%, -10px);
    z-index: 95;
    width: 72px;
    height: 26px;
    padding: 4px 0 0;
    display: grid;
    place-items: end center;
    border: 1px solid rgba(255, 255, 255, 0.12);
    border-top: 0;
    border-radius: 0 0 12px 12px;
    background: rgba(40, 40, 44, 0.55);
    color: rgba(255, 255, 255, 0.7);
    opacity: 0.35;
    cursor: pointer;
    transition: opacity 0.25s ease, transform 0.25s ease, background 0.2s;
}
#top-tab svg { margin-bottom: 4px; }
#top-tab.near,
#top-tab:focus-visible {
    opacity: 1;
    transform: translate(-50%, 0);
    background: rgba(50, 50, 56, 0.85);
}
#top-tab:hover { background: var(--accent-color); color: #fff; }
#top-bar:not(.hidden) ~ #top-tab,
#controls-overlay:not(.hidden) ~ #top-tab { opacity: 0; pointer-events: none; transform: translate(-50%, -100%); }
body.is-playing #top-tab:not(.near) { opacity: 0.12; }
@media (hover: none), (pointer: coarse) { #top-tab { display: none; } }
/* A little more visible at rest */
#edge-tab { opacity: 0.6; transform: translate(4px, -50%); }
#top-tab { opacity: 0.6; transform: translate(-50%, -4px); }
#edge-tab.near, #edge-tab:focus-visible { opacity: 1; transform: translate(0, -50%); }
#top-tab.near, #top-tab:focus-visible { opacity: 1; transform: translate(-50%, 0); }
#controls-overlay:not(.hidden) ~ #edge-tab { opacity: 0; transform: translate(100%, -50%); }
#top-bar:not(.hidden) ~ #top-tab, #controls-overlay:not(.hidden) ~ #top-tab { opacity: 0; transform: translate(-50%, -100%); }
body.is-playing #edge-tab:not(.near), body.is-playing #top-tab:not(.near) { opacity: 0.15; }

/* ---------- Edit mode: the whole view says so ---------- */
body.editing #app::after {
    content: '';
    position: fixed;
    inset: 0;
    border: 3px solid var(--accent-color);
    box-shadow: inset 0 0 0 1px rgba(59, 130, 246, 0.25), inset 0 0 80px rgba(59, 130, 246, 0.18);
    pointer-events: none;
    z-index: 120;
    animation: editFrameIn 0.25s ease;
}
@keyframes editFrameIn { from { opacity: 0; } to { opacity: 1; } }

#edit-banner {
    position: fixed;
    top: 14px;
    left: 50%;
    transform: translateX(-50%);
    z-index: 220;
    display: flex;
    align-items: center;
    gap: 10px;
    max-width: calc(100% - 32px);
    padding: 6px 6px 6px 14px;
    border-radius: 999px;
    background: var(--accent-color);
    color: #fff;
    font: 500 0.9rem var(--font-main);
    box-shadow: 0 8px 24px rgba(0, 0, 0, 0.35);
    transition: opacity 0.2s ease, transform 0.2s ease;
}
#edit-banner.hidden { opacity: 0; transform: translate(-50%, -12px); pointer-events: none; }
#edit-banner strong { font-weight: 700; margin-right: 4px; }
#edit-banner .edit-hint { opacity: 0.85; }
#edit-done-btn {
    flex: none;
    height: 30px;
    padding: 0 14px;
    border: 0;
    border-radius: 999px;
    background: #fff;
    color: #0b2a5b;
    font: 600 0.85rem var(--font-main);
    cursor: pointer;
}
#edit-done-btn:hover { background: #e8efff; }
body.editing #top-tab, body.editing #edge-tab { opacity: 0 !important; pointer-events: none; }
@media (max-width: 600px) { #edit-banner .edit-hint { display: none; } }
/* The frame above replaces the old dashed edit border on small screens (it also shifted the text) */
@media (max-width: 768px) {
    #prompter-text[contenteditable="true"] {
        border: 0;
        border-radius: 0;
        margin-top: 0;
        margin-bottom: 0;
        padding-top: 45vh;
        padding-bottom: 45vh;
    }
}

/* ---------- Clearer edge tabs with a label; the gear is for touch screens only ---------- */
#edge-tab {
    width: 32px;
    height: auto;
    padding: 14px 0 14px 2px;
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 8px;
    background: rgba(45, 45, 52, 0.8);
    border-color: rgba(255, 255, 255, 0.16);
    color: rgba(255, 255, 255, 0.85);
    opacity: 0.85;
    transform: translate(2px, -50%);
}
#edge-tab span {
    writing-mode: vertical-rl;
    transform: rotate(180deg);
    font: 600 0.72rem var(--font-main);
    letter-spacing: 0.04em;
}
#top-tab {
    width: auto;
    height: 30px;
    padding: 0 14px 2px;
    display: flex;
    align-items: center;
    gap: 6px;
    background: rgba(45, 45, 52, 0.8);
    border-color: rgba(255, 255, 255, 0.16);
    color: rgba(255, 255, 255, 0.85);
    font: 600 0.72rem var(--font-main);
    letter-spacing: 0.04em;
    opacity: 0.85;
    transform: translate(-50%, -2px);
}
#top-tab svg { margin: 0; }
#edge-tab.near, #edge-tab:focus-visible { opacity: 1; transform: translate(0, -50%); background: rgba(55, 55, 64, 0.95); }
#top-tab.near, #top-tab:focus-visible { opacity: 1; transform: translate(-50%, 0); background: rgba(55, 55, 64, 0.95); }
#edge-tab:hover, #top-tab:hover { background: var(--accent-color); color: #fff; }
#controls-overlay:not(.hidden) ~ #edge-tab { opacity: 0; transform: translate(100%, -50%); }
#top-bar:not(.hidden) ~ #top-tab, #controls-overlay:not(.hidden) ~ #top-tab { opacity: 0; transform: translate(-50%, -100%); }
body.is-playing #edge-tab:not(.near), body.is-playing #top-tab:not(.near) { opacity: 0.15; }

/* Gear only where there is no mouse to reach the edges */
@media (hover: hover) and (pointer: fine) {
    .floating-actions { display: none; }
}
#edge-tab { width: 36px; padding: 16px 0 16px 3px; gap: 10px; }
#edge-tab span, #top-tab { font-size: 0.82rem; }
#top-tab { height: 34px; padding: 0 16px 2px; }

.privacy-note { padding: 10px 12px; border-radius: 10px; background: rgba(255, 255, 255, 0.05); }
.privacy-note:empty { display: none; }
.privacy-note[data-state="local"] { background: rgba(74, 222, 128, 0.12); color: rgba(220, 255, 230, 0.9); }
.privacy-note strong { color: #fff; }
.privacy-note .text-btn { display: block; margin-top: 6px; }

.sr-only { position: absolute; width: 1px; height: 1px; padding: 0; margin: -1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; border: 0; }
.private-line { display: flex; gap: 8px; align-items: flex-start; padding: 10px 12px; border-radius: 10px; background: rgba(74, 222, 128, 0.1); color: rgba(220, 255, 230, 0.9); font-size: 0.82rem; line-height: 1.4; }
.private-line svg { flex: none; margin-top: 2px; color: #4ade80; }
.settings-links { display: flex; justify-content: center; gap: 16px; padding-top: 4px; }
.settings-links a { color: rgba(255, 255, 255, 0.6); font-size: 0.82rem; text-decoration: none; }
.settings-links a:hover { color: #fff; text-decoration: underline; }

/* Credit */
#credit {
    position: fixed;
    left: 16px;
    bottom: 14px;
    z-index: 90;
    padding: 4px 8px;
    border-radius: 8px;
    color: rgba(255, 255, 255, 0.45);
    font: 500 0.78rem var(--font-main);
    text-decoration: none;
    transition: opacity 0.25s ease, color 0.15s, background 0.15s;
}
#credit:hover { color: #fff; background: rgba(255, 255, 255, 0.08); }
body.is-playing #credit, body.editing #credit { opacity: 0; pointer-events: none; }
.settings-credit { text-align: center; color: rgba(255, 255, 255, 0.45); font-size: 0.8rem; }
.settings-credit a { color: rgba(255, 255, 255, 0.75); text-decoration: none; }
.settings-credit a:hover { color: #fff; text-decoration: underline; }
#credit { background: rgba(17, 17, 17, 0.7); backdrop-filter: blur(6px); -webkit-backdrop-filter: blur(6px); }

/* ---------- History panel (left) ---------- */
#history-panel {
    position: fixed;
    left: 20px;
    top: 20px;
    bottom: 20px;
    width: 340px;
    z-index: 250;
    display: flex;
    flex-direction: column;
    gap: 12px;
    padding: 0 18px 16px;
    overflow-y: auto;
    background: rgba(24, 24, 27, 0.92);
    backdrop-filter: blur(12px);
    -webkit-backdrop-filter: blur(12px);
    border: 1px solid rgba(255, 255, 255, 0.08);
    border-radius: 18px;
    box-shadow: 0 20px 60px rgba(0, 0, 0, 0.5);
    color: #fff;
    font-size: 0.9rem;
    transition: opacity 0.28s ease, transform 0.28s ease;
    scrollbar-width: thin;
    scrollbar-color: rgba(255, 255, 255, 0.15) transparent;
}
#history-panel.hidden { opacity: 0; transform: translateX(-24px); pointer-events: none; }
#history-panel .controls-header { margin: 0 -18px 0; padding: 16px 18px 6px; }
.history-note { color: rgba(255, 255, 255, 0.45); font-size: 0.8rem; margin-top: -4px; }
#history-list { list-style: none; display: flex; flex-direction: column; gap: 6px; }
.history-empty { color: rgba(255, 255, 255, 0.55); font-size: 0.88rem; line-height: 1.5; padding: 8px 2px; }
.h-item {
    display: flex;
    align-items: stretch;
    border-radius: 12px;
    background: rgba(255, 255, 255, 0.04);
    border: 1px solid transparent;
    transition: background 0.15s, border-color 0.15s;
}
.h-item:hover { background: rgba(255, 255, 255, 0.08); }
.h-item.current { border-color: rgba(59, 130, 246, 0.6); background: rgba(59, 130, 246, 0.1); }
.h-main {
    flex: 1;
    min-width: 0;
    display: flex;
    flex-direction: column;
    gap: 3px;
    padding: 10px 12px;
    border: 0;
    background: none;
    color: #fff;
    text-align: left;
    cursor: pointer;
    font: inherit;
}
.h-title { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.h-meta { color: rgba(255, 255, 255, 0.5); font-size: 0.78rem; }
.h-meta b { color: #8db6ff; font-weight: 600; }
.h-actions { display: flex; align-items: center; padding-right: 6px; opacity: 0; transition: opacity 0.15s; }
.h-item:hover .h-actions, .h-item:focus-within .h-actions { opacity: 1; }
.h-actions button {
    min-width: 30px;
    height: 30px;
    display: grid;
    place-items: center;
    border: 0;
    border-radius: 8px;
    background: none;
    color: rgba(255, 255, 255, 0.65);
    cursor: pointer;
    font: 600 0.75rem var(--font-main);
}
.h-actions button:hover { background: rgba(255, 255, 255, 0.1); color: #fff; }
.h-actions button.confirm { background: #ef4444; color: #fff; padding: 0 8px; }
.h-rename {
    flex: 1;
    margin: 6px;
    height: 36px;
    padding: 0 10px;
    border-radius: 8px;
    border: 1px solid var(--accent-color);
    background: rgba(0, 0, 0, 0.3);
    color: #fff;
    font: 600 0.9rem var(--font-main);
}
@media (hover: none) { .h-actions { opacity: 1; } }
@media (max-width: 768px), (max-height: 600px) {
    #history-panel { left: 0; top: 0; bottom: 0; width: 100%; border-radius: 0; border: 0; padding: 0 20px 40px; background: rgba(17, 17, 18, 0.98); }
    #history-panel > * { width: 100%; max-width: 520px; margin-left: auto; margin-right: auto; }
}

/* History tab on the left edge */
#history-tab {
    position: fixed;
    left: 0;
    top: 60%;
    z-index: 95;
    width: 36px;
    padding: 16px 3px 16px 0;
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 10px;
    border: 1px solid rgba(255, 255, 255, 0.16);
    border-left: 0;
    border-radius: 0 12px 12px 0;
    background: rgba(45, 45, 52, 0.8);
    color: rgba(255, 255, 255, 0.85);
    opacity: 0.85;
    cursor: pointer;
    transform: translate(-2px, -50%);
    transition: opacity 0.25s ease, transform 0.25s ease, background 0.2s;
}
#history-tab span { writing-mode: vertical-rl; transform: rotate(180deg); font: 600 0.82rem var(--font-main); letter-spacing: 0.04em; }
#history-tab.near, #history-tab:focus-visible { opacity: 1; transform: translate(0, -50%); background: rgba(55, 55, 64, 0.95); }
#history-tab:hover { background: var(--accent-color); color: #fff; }
#history-panel:not(.hidden) ~ #history-tab { opacity: 0; pointer-events: none; transform: translate(-100%, -50%); }
body.is-playing #history-tab:not(.near) { opacity: 0.15; }
body.editing #history-tab { opacity: 0 !important; pointer-events: none; }
@media (hover: none), (pointer: coarse) { #history-tab { display: none; } }

/* Pinch: let the page pan vertically but leave pinch to us */
#prompter-text { touch-action: pan-y; }

/* Modal close is now a real button */
.close { background: none; border: 0; padding: 6px; border-radius: 8px; line-height: 0; top: 12px; right: 12px; }
.close:hover { background: rgba(255, 255, 255, 0.1); }

/* Icons */
.ic { display: block; flex: none; }
#top-bar .icon-btn .ic { margin: auto; }
.play-glyph .ic { fill: currentColor; }
#play-icon .ic { width: 96px; height: 96px; }
#context-menu button { display: grid; place-items: center; }

/* ---------- Edit mode: clearer blue ---------- */
body.editing #app::after {
    border: 4px solid var(--accent-color);
    box-shadow:
        inset 0 0 0 1px rgba(147, 197, 253, 0.5),
        inset 0 0 40px rgba(59, 130, 246, 0.45),
        inset 0 0 140px rgba(59, 130, 246, 0.28);
    background: rgba(59, 130, 246, 0.05);
}

/* ---------- Playback bar: consistent on every screen size ---------- */
#top-bar {
    height: auto;
    gap: 10px;
    padding: max(14px, env(safe-area-inset-top)) max(14px, env(safe-area-inset-right)) 22px max(14px, env(safe-area-inset-left));
    background: linear-gradient(to bottom, rgba(0, 0, 0, 0.92) 55%, rgba(0, 0, 0, 0));
}
#top-bar .icon-btn {
    flex: none;
    width: 44px;
    height: 44px;
    padding: 0;
    border: 1px solid rgba(255, 255, 255, 0.12);
    background: rgba(255, 255, 255, 0.08);
    color: #fff;
    opacity: 1;
    font-size: 1rem;
}
#top-bar .icon-btn:hover { background: rgba(255, 255, 255, 0.18); transform: none; }
#top-bar #top-play-btn { background: #4ade80; border-color: #4ade80; color: #04210f; }
#top-bar #top-play-btn .ic { fill: currentColor; }
#top-bar #top-play-btn:hover { background: #5ee68f; }
#top-mode-btn { font-size: 1rem !important; }
.top-speed-control {
    flex: 0 1 340px;
    min-width: 0;
    height: 44px;
    gap: 12px;
    padding: 0 16px;
    border-radius: 22px;
    border: 1px solid rgba(255, 255, 255, 0.12);
    background: rgba(255, 255, 255, 0.08);
    font-size: 0.85rem;
    color: rgba(255, 255, 255, 0.75);
}
.top-speed-control input[type="range"] { flex: 1; width: auto; min-width: 0; height: 4px; }
.top-speed-control input[type="range"]::-webkit-slider-thumb { width: 18px; height: 18px; background: #fff; }
#top-speed-val { min-width: 2.2em; text-align: right; font-variant-numeric: tabular-nums; color: #fff; }
@media (max-width: 600px) {
    #top-bar { justify-content: flex-start; }
    .top-speed-control { flex: 1 1 auto; padding: 0 12px; gap: 10px; }
    .top-speed-control > span:first-child { display: none; }
}

/* Edge tabs on touch screens too: tap to open settings, history or the playback bar */
@media (hover: none), (pointer: coarse) {
    #edge-tab, #top-tab, #history-tab { display: flex !important; }
}

/* Credit: smaller on phones */
@media (max-width: 600px) {
    #credit { font-size: 0.7rem; left: 10px; bottom: max(10px, env(safe-area-inset-bottom)); }
}

/* =========================================================
   Dithered sliders
   ========================================================= */
.dslider {
    position: relative;
    isolation: isolate;
    height: 42px;
    border-radius: 12px;
    overflow: hidden;
    background: rgba(255, 255, 255, 0.05);
    border: 1px solid rgba(255, 255, 255, 0.09);
    transition: background 0.15s, border-color 0.15s, transform 0.12s;
    touch-action: none;
}
.dslider:hover { background: rgba(255, 255, 255, 0.08); border-color: rgba(255, 255, 255, 0.16); }
.dslider.dragging { border-color: rgba(255, 255, 255, 0.3); }
.dslider:focus-within { outline: 2px solid var(--accent-color); outline-offset: 2px; }
.dslider canvas { position: absolute; inset: 0; width: 100%; height: 100%; image-rendering: pixelated; }
.dslider-label,
.dslider-value {
    position: absolute;
    top: 50%;
    transform: translateY(-50%);
    z-index: 1;
    color: #fff;
    pointer-events: none;
    font: 600 0.82rem var(--font-main);
    letter-spacing: 0.01em;
    white-space: nowrap;
}
.dslider-label { left: 13px; }
.dslider-value { right: 13px; font-variant-numeric: tabular-nums; }
#controls-overlay .dslider input[type="range"],
.top-speed-control .dslider input[type="range"],
.dslider input[type="range"] {
    position: absolute;
    inset: 0;
    z-index: 2;
    width: 100%;
    height: 100%;
    margin: 0;
    padding: 0;
    opacity: 0;
    cursor: ew-resize;
    -webkit-appearance: none;
    appearance: none;
    background: transparent;
}
/* The old label rows are now inside the slider */
.control-group:has(.dslider) > label { display: none; }

/* Playback bar slider */
.top-speed-control { padding: 0; gap: 0; border: 0; background: none; backdrop-filter: none; height: 44px; }
.top-speed-control > span { display: none; }
.dslider.compact { flex: 1; height: 44px; border-radius: 22px; }
.dslider.compact .dslider-label { left: 18px; }
.dslider.compact .dslider-value { right: 18px; }

/* =========================================================
   Segmented controls with a sliding highlight
   ========================================================= */
.seg.sliding { position: relative; }
.seg.sliding .seg-ind {
    position: absolute;
    top: 3px;
    bottom: 3px;
    left: 3px;
    border-radius: 8px;
    background: var(--accent-color);
    box-shadow: 0 2px 10px rgba(59, 130, 246, 0.35);
    transition: transform 0.28s cubic-bezier(0.3, 0.7, 0.2, 1), width 0.28s cubic-bezier(0.3, 0.7, 0.2, 1), opacity 0.2s;
    z-index: 0;
}
.seg.sliding .align-btn { position: relative; z-index: 1; }
.seg.sliding .align-btn.active { background: transparent; color: #fff; }
.seg.sliding .align-btn:hover:not(.active) { background: transparent; color: #fff; }

/* =========================================================
   Small refinements
   ========================================================= */
.play-btn:active, .secondary-btn:active, #top-bar .icon-btn:active, .close-btn:active { transform: scale(0.96); }
.secondary-btn, .close-btn, #top-bar .icon-btn { transition: background 0.15s, border-color 0.15s, transform 0.08s; }
.close-btn:hover .ic { transform: rotate(90deg); }
.close-btn .ic { transition: transform 0.2s ease; }
#toast {
    background: rgba(24, 24, 27, 0.92);
    color: #fff;
    border: 1px solid rgba(255, 255, 255, 0.12);
    box-shadow: 0 10px 30px rgba(0, 0, 0, 0.4);
    backdrop-filter: blur(10px);
    -webkit-backdrop-filter: blur(10px);
    animation: toastIn 0.25s ease;
}
@keyframes toastIn { from { opacity: 0; transform: translate(-50%, -8px); } to { opacity: 1; transform: translate(-50%, 0); } }
.settings-section h4 { letter-spacing: 0.01em; }
#controls-overlay, #history-panel { background: rgba(20, 20, 23, 0.9); }
@media (prefers-reduced-motion: reduce) {
    .seg.sliding .seg-ind, #toast { transition: none; animation: none; }
}

/* The slider handles touch/mouse itself; the range input stays for keyboard and screen readers */
.dslider { cursor: ew-resize; -webkit-user-select: none; user-select: none; -webkit-tap-highlight-color: transparent; }
#controls-overlay .dslider input[type="range"],
.top-speed-control .dslider input[type="range"],
.dslider input[type="range"] { pointer-events: none; }
