:root {
    --bg: #141618;
    --surface: #1c1f24;
    --surface-2: #262a31;
    --border: rgba(255, 255, 255, 0.1);
    --text: #eceff4;
    --text-dim: #9aa3b2;
    --accent: #c9a227;
    --accent-hover: #dbb42e;
    --accent-soft: rgba(201, 162, 39, 0.18);
    --danger: #c53030;
    --danger-glow: rgba(197, 48, 48, 0.25);
    --success: #3d8f5f;
    --font: "DM Sans", system-ui, sans-serif;
    --mono: "JetBrains Mono", monospace;
    --radius: 10px;
    --side-w: clamp(200px, 22vw, 280px);
}

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

html, body {
    height: 100%;
    height: 100dvh;
    overflow: hidden;
    font-family: var(--font);
    background: var(--bg);
    color: var(--text);
    -webkit-font-smoothing: antialiased;
}

html.is-display-slave,
html.is-display-slave body {
    background: var(--prompter-bg, #000);
    overscroll-behavior: none;
    touch-action: pan-y;
}

.app {
    display: flex;
    flex-direction: column;
    height: 100vh;
    height: 100dvh;
    height: 100svh;
    background: var(--bg);
}

html.is-display-slave .app {
    position: fixed;
    inset: 0;
    width: 100%;
    height: 100%;
    height: 100dvh;
    height: var(--app-vh, 100dvh);
    background: var(--prompter-bg, #000);
    padding:
        env(safe-area-inset-top, 0)
        env(safe-area-inset-right, 0)
        env(safe-area-inset-bottom, 0)
        env(safe-area-inset-left, 0);
}

.view { display: none; flex-direction: column; flex: 1; min-height: 0; }
.view.active { display: flex; }

/* ── Topbar ── */
.topbar {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 0.75rem;
    padding: clamp(0.45rem, 1.2vmin, 0.65rem) clamp(0.75rem, 2vw, 1.25rem);
    background: var(--surface);
    border-bottom: 1px solid var(--border);
    flex-shrink: 0;
}
.topbar-left { display: flex; align-items: center; gap: 0.75rem; min-width: 0; }
.topbar-controls { display: flex; align-items: center; gap: 0.45rem; flex-shrink: 0; }

.brand {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    color: var(--text);
    text-decoration: none;
    font-weight: 700;
    font-size: clamp(0.95rem, 1.8vw, 1.05rem);
    white-space: nowrap;
}
.brand:hover { color: var(--accent); }
.brand svg { color: var(--accent); flex-shrink: 0; }

.badge-live {
    font-size: 0.65rem;
    font-weight: 800;
    letter-spacing: 0.08em;
    padding: 0.2rem 0.45rem;
    border-radius: 4px;
    background: var(--danger);
    color: #fff;
}
.ntp-clock {
    display: flex;
    align-items: center;
    gap: 0.35rem;
    font-family: var(--mono);
    font-size: 0.85rem;
    color: var(--text-dim);
}
.ntp-dot {
    width: 7px;
    height: 7px;
    border-radius: 50%;
    background: #22c55e;
}
.status-chip {
    font-size: 0.72rem;
    font-weight: 600;
    color: var(--text-dim);
    padding: 0.25rem 0.55rem;
    border-radius: 999px;
    border: 1px solid var(--border);
    background: var(--surface-2);
    white-space: nowrap;
}
.dirty-chip {
    font-size: 0.68rem;
    font-weight: 700;
    letter-spacing: 0.03em;
    padding: 0.2rem 0.5rem;
    border-radius: 999px;
    background: var(--accent-soft);
    color: var(--accent);
    border: 1px solid var(--accent);
}

/* ── Buttons ── */
.btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 0.4rem;
    padding: 0.5rem 1rem;
    border: none;
    border-radius: 8px;
    font-family: var(--font);
    font-size: clamp(0.78rem, 1.4vw, 0.85rem);
    font-weight: 600;
    cursor: pointer;
    transition: background 0.12s, border-color 0.12s, transform 0.08s;
    touch-action: manipulation;
    white-space: nowrap;
}
.btn:active { transform: scale(0.97); }
.btn-sm { padding: 0.35rem 0.65rem; font-size: 0.75rem; min-width: 2rem; }
.btn-lg { padding: 0.75rem 1.5rem; font-size: 1rem; }
.btn-primary { background: var(--accent); color: #1a1a1a; }
.btn-primary:hover { background: var(--accent-hover); }
.btn-secondary { background: var(--surface-2); color: var(--text); border: 1px solid var(--border); }
.btn-secondary:hover { background: #323842; }
.btn-danger {
    background: var(--danger);
    color: #fff;
    font-weight: 700;
}
.btn-danger:hover { background: #a82828; }
.btn-danger-outline {
    background: transparent;
    color: var(--danger);
    border: 1px solid var(--danger);
}
.btn-danger-outline:hover { background: rgba(197, 48, 48, 0.1); }
.btn-ghost {
    background: var(--surface-2);
    color: var(--text-dim);
    border: 1px solid var(--border);
    padding: 0.45rem 0.65rem;
}
.btn-ghost:hover { color: var(--text); border-color: rgba(255, 255, 255, 0.22); background: #323842; }
.btn-toggle.active,
.btn-ghost[aria-pressed="true"] {
    background: var(--accent-soft);
    border-color: var(--accent);
    color: var(--accent);
}
.btn-icon {
    background: none;
    border: none;
    color: var(--text-dim);
    font-size: 1.5rem;
    cursor: pointer;
    line-height: 1;
    padding: 0.25rem;
}
.btn-icon:hover { color: var(--text); }

/* ── Editor layout ── */
.app-body {
    flex: 1;
    display: flex;
    flex-direction: row;
    min-height: 0;
    min-width: 0;
}

.side-panel {
    width: var(--side-w);
    flex-shrink: 0;
    display: flex;
    flex-direction: column;
    background: var(--surface);
    border-right: 1px solid var(--border);
    min-height: 0;
}
.panel-head {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 0.75rem 0.85rem;
    border-bottom: 1px solid var(--border);
}
.panel-title {
    font-size: 0.8rem;
    font-weight: 700;
    text-transform: uppercase;
    letter-spacing: 0.04em;
    color: var(--text-dim);
}
.panel-search { padding: 0.45rem 0.65rem; border-bottom: 1px solid var(--border); }
.panel-search .input { font-size: 0.82rem; padding: 0.4rem 0.6rem; }
.panel-foot {
    display: flex;
    flex-wrap: wrap;
    gap: 0.35rem;
    padding: 0.65rem;
    border-top: 1px solid var(--border);
}

.script-list {
    flex: 1;
    overflow-y: auto;
    padding: 0.4rem;
    min-height: 0;
}
.empty-state {
    padding: 1.25rem 0.75rem;
    text-align: center;
    color: var(--text-dim);
    font-size: 0.85rem;
}
.script-item {
    display: flex;
    align-items: center;
    gap: 0.4rem;
    padding: 0.55rem 0.6rem;
    border-radius: 8px;
    border: 1px solid transparent;
    cursor: pointer;
    margin-bottom: 0.25rem;
    background: transparent;
    transition: background 0.12s, border-color 0.12s;
}
.script-item:hover { background: var(--surface-2); }
.script-item.active {
    background: var(--accent-soft);
    border-color: var(--accent);
}
.script-item.dragging { opacity: 0.45; }
.script-item.over { border-color: var(--accent); }
.script-name {
    flex: 1;
    min-width: 0;
    font-size: 0.88rem;
    font-weight: 600;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}
.delete-script-btn {
    background: none;
    border: none;
    color: var(--text-dim);
    cursor: pointer;
    font-size: 0.85rem;
    padding: 0.15rem 0.3rem;
    border-radius: 4px;
}
.delete-script-btn:hover { color: var(--danger); background: rgba(197, 48, 48, 0.12); }

.main.editor-main {
    flex: 1;
    display: flex;
    flex-direction: column;
    gap: 0.65rem;
    padding: clamp(0.65rem, 1.5vmin, 1rem) clamp(0.75rem, 2vw, 1.25rem);
    min-width: 0;
    min-height: 0;
}
.editor-toolbar-row {
    display: flex;
    align-items: flex-end;
    gap: 0.65rem;
    flex-shrink: 0;
}
.palette-row { align-items: center; }
.palette-label {
    font-size: 0.72rem;
    color: var(--text-dim);
    font-weight: 500;
}
.grow { flex: 1; min-width: 0; }

.field { display: flex; flex-direction: column; gap: 0.3rem; }
.field span { font-size: 0.72rem; color: var(--text-dim); font-weight: 500; }
.input, .field input[type="text"] {
    background: var(--surface-2);
    border: 1px solid var(--border);
    border-radius: 8px;
    color: var(--text);
    font-family: var(--font);
    font-size: 0.9rem;
    padding: 0.5rem 0.75rem;
    outline: none;
    width: 100%;
}
.input:focus, .field input[type="text"]:focus {
    border-color: var(--accent);
    box-shadow: 0 0 0 2px var(--accent-soft);
}

.color-palette { display: flex; flex-wrap: wrap; gap: 0.35rem; }
.color-swatch {
    width: 22px;
    height: 22px;
    border-radius: 4px;
    border: 1px solid rgba(255, 255, 255, 0.25);
    cursor: pointer;
}
.color-swatch:hover { outline: 2px solid var(--accent); outline-offset: 1px; }

.text-editor {
    flex: 1;
    min-height: 12rem;
    overflow-y: auto;
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 1rem 1.15rem;
    font-size: 1.05rem;
    line-height: 1.55;
    outline: none;
}
.text-editor:empty::before {
    content: attr(data-placeholder);
    color: var(--text-dim);
    pointer-events: none;
}
.format-bar {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    gap: 0.55rem 0.85rem;
    padding: 0.4rem 0;
    flex-shrink: 0;
}
.toolbar-group { display: flex; align-items: center; gap: 0.25rem; }
.tb-btn {
    min-width: 2rem;
    height: 2rem;
    padding: 0 0.45rem;
    border-radius: 6px;
    border: 1px solid var(--border);
    background: var(--surface-2);
    color: var(--text);
    font-family: var(--font);
    font-size: 0.85rem;
    font-weight: 700;
    cursor: pointer;
}
.tb-btn:hover { border-color: var(--accent); color: var(--accent); }
.tb-btn i, .tb-btn u { font-style: italic; }
.palette-inline { display: flex; align-items: center; gap: 0.45rem; flex-wrap: wrap; }

.editor-cta {
    flex-shrink: 0;
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 1rem;
    padding-top: 0.25rem;
}
.mono-hint {
    font-family: var(--mono);
    font-size: 0.75rem;
    margin: 0;
}

.select {
    background: var(--surface-2);
    border: 1px solid var(--border);
    border-radius: 8px;
    color: var(--text);
    font-family: var(--font);
    font-size: 0.9rem;
    padding: 0.5rem 0.75rem;
    width: 100%;
    outline: none;
}
.select:focus { border-color: var(--accent); }

/* ── Prompter ── */
.view-prompter {
    position: relative;
    background: var(--prompter-bg, #000);
    min-height: 0;
    flex: 1;
}

html.is-display-slave .view-prompter,
.view-prompter.is-slave-responsive {
    position: absolute;
    inset: 0;
    width: 100% !important;
    height: 100% !important;
    transform: none !important;
    transform-origin: initial !important;
}
.progress-track {
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    height: 3px;
    z-index: 8;
    background: rgba(255, 255, 255, 0.08);
    pointer-events: none;
}
.progress-fill {
    height: 100%;
    width: 0%;
    background: var(--accent);
    transition: width 0.08s linear;
}
.prompter-display {
    flex: 1;
    overflow-y: auto;
    overflow-x: hidden;
    background: var(--prompter-bg, #000);
    color: #fff;
    min-height: 0;
}
.countdown-overlay {
    position: absolute;
    inset: 0;
    z-index: 20;
    display: flex;
    align-items: center;
    justify-content: center;
    background: rgba(0, 0, 0, 0.55);
    pointer-events: none;
}
.countdown-overlay[hidden] { display: none !important; }
.countdown-overlay span {
    font-family: var(--mono);
    font-size: clamp(5rem, 18vw, 10rem);
    font-weight: 700;
    color: var(--accent);
    text-shadow: 0 8px 40px rgba(0, 0, 0, 0.6);
}
.scrolling-content {
    outline: none;
    line-height: 1.35;
    font-weight: 600;
    word-wrap: break-word;
    padding-left: 10%;
    padding-right: 10%;
}
.marker-arrow {
    position: absolute;
    left: 0.35rem;
    top: 30%;
    width: 0;
    height: 0;
    border-top: 12px solid transparent;
    border-bottom: 12px solid transparent;
    border-left: 18px solid var(--accent);
    z-index: 5;
    pointer-events: none;
    transform: translateY(-50%);
}

.prompter-toolbar {
    position: absolute;
    left: 50%;
    bottom: 0.75rem;
    transform: translateX(-50%);
    z-index: 10;
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    justify-content: center;
    gap: 0.65rem 1rem;
    max-width: calc(100% - 1.5rem);
    padding: 0.65rem 0.85rem;
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: 12px;
    box-shadow: 0 12px 40px rgba(0, 0, 0, 0.55);
    transition: opacity 0.25s, transform 0.25s;
}
.prompter-toolbar.is-slave { display: none !important; }
.prompter-toolbar.is-hidden {
    opacity: 0;
    pointer-events: none;
    transform: translateX(-50%) translateY(12px);
}
.view-prompter.is-fullscreen .prompter-toolbar { bottom: 1rem; }
#voiceBtn[aria-pressed="true"] {
    background: rgba(197, 48, 48, 0.25);
    border-color: var(--danger);
    color: #f87171;
}
.tb-group {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    flex-wrap: wrap;
}
.tb-group.checks { gap: 0.75rem; }

.slider-field { display: flex; flex-direction: column; gap: 0.15rem; min-width: 110px; }
.slider-field.compact { min-width: 100px; }
.slider-field span { font-size: 0.68rem; color: var(--text-dim); }
.slider-field em {
    font-family: var(--mono);
    font-style: normal;
    color: var(--text);
    margin-left: 0.25rem;
}
.slider-field input[type="range"] { accent-color: var(--accent); width: 100%; }

.check-field {
    display: inline-flex;
    align-items: center;
    gap: 0.3rem;
    font-size: 0.75rem;
    color: var(--text-dim);
    cursor: pointer;
    user-select: none;
}
.check-field input { accent-color: var(--accent); }

.mobile-fs-btn {
    position: absolute;
    top: max(0.75rem, env(safe-area-inset-top, 0px));
    right: max(0.75rem, env(safe-area-inset-right, 0px));
    z-index: 12;
    min-width: 2.75rem;
    height: 2.75rem;
    padding: 0 0.75rem;
    gap: 0.4rem;
    border-radius: 8px;
    border: 1px solid var(--border);
    background: rgba(28, 31, 36, 0.9);
    color: var(--text);
    display: none;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    font-family: var(--font);
    font-size: 0.78rem;
    font-weight: 600;
}
.mobile-fs-btn:not([hidden]) { display: flex; }
.mobile-fs-label { white-space: nowrap; }
html.is-immersive .mobile-fs-btn { opacity: 0.35; }
html.is-immersive .mobile-fs-label { display: none; }
.mobile-fs-btn:not(:hover):not(:focus) .mobile-fs-label {
    display: none;
}
@media (hover: none) {
    .mobile-fs-btn .mobile-fs-label { display: none; }
}

/* ── Dialogs ── */
.edit-modal {
    border: none;
    border-radius: 12px;
    padding: 0;
    margin: auto;
    position: fixed;
    inset: 0;
    width: calc(100% - 1.5rem);
    max-width: 420px;
    height: fit-content;
    max-height: calc(100vh - 2rem);
    background: #181b20;
    color: var(--text);
    box-shadow: 0 24px 80px rgba(0, 0, 0, 0.7);
    overflow: hidden;
}
.edit-modal::backdrop { background: rgba(0, 0, 0, 0.75); }
.share-modal { max-width: 380px; }
.edit-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 0.75rem 1rem;
    border-bottom: 1px solid var(--border);
    background: var(--surface);
}
.edit-header h2 { font-size: 1rem; font-weight: 700; }
.edit-body { padding: 0.85rem 1rem; display: flex; flex-direction: column; gap: 0.75rem; }
.edit-footer {
    display: flex;
    justify-content: flex-end;
    gap: 0.5rem;
    padding: 0.75rem 1rem;
    border-top: 1px solid var(--border);
    background: var(--surface);
}
.help-list {
    list-style: none;
    display: flex;
    flex-direction: column;
    gap: 0.55rem;
    font-size: 0.9rem;
    color: var(--text-dim);
}
.help-list kbd {
    font-family: var(--mono);
    font-size: 0.78rem;
    color: var(--accent);
    background: var(--surface-2);
    border: 1px solid var(--border);
    border-radius: 4px;
    padding: 0.1rem 0.35rem;
}
.hint { font-size: 0.85rem; color: var(--text-dim); }
.share-body { align-items: stretch; text-align: center; }
.qr-wrap {
    display: flex;
    justify-content: center;
    min-height: 150px;
}
.qr-wrap img {
    border: 2px solid var(--border);
    border-radius: 8px;
    background: #fff;
}
.copy-feedback {
    color: var(--success);
    font-size: 0.85rem;
    font-weight: 600;
}

.toast-host {
    position: fixed;
    bottom: 1.25rem;
    right: 1.25rem;
    z-index: 100;
    display: flex;
    flex-direction: column;
    gap: 0.45rem;
    pointer-events: none;
}
.toast {
    pointer-events: none;
    padding: 0.65rem 0.95rem;
    border-radius: 8px;
    background: var(--surface);
    border: 1px solid var(--border);
    color: var(--text);
    font-size: 0.85rem;
    font-weight: 600;
    box-shadow: 0 10px 30px rgba(0, 0, 0, 0.45);
    animation: toast-in 0.2s ease;
}
.toast.ok { border-color: var(--success); }
.toast.err { border-color: var(--danger); color: #fca5a5; }
@keyframes toast-in {
    from { opacity: 0; transform: translateY(8px); }
    to { opacity: 1; transform: translateY(0); }
}

@media (max-width: 720px) {
    .app-body { flex-direction: column; }
    .side-panel {
        width: 100%;
        max-height: 38%;
        border-right: none;
        border-bottom: 1px solid var(--border);
    }
    .topbar-controls .btn-primary { display: none; }
}
