:root {
    color-scheme: dark;
    --bg:           #0a0b0d;
    --bg-elev:     #13151a;
    --fg:          #e6e6e6;
    --fg-dim:      #9aa0a8;
    --fg-muted:    #6a6a6a;
    --accent:      #6fd;
    --accent-dim:  #39756a;
    --panel:       #15171c;
    --panel-hi:   #1d2026;
    --panel-border: #262932;
    --danger:      #ff6b6b;
}

* { box-sizing: border-box; }

html, body {
    margin: 0;
    padding: 0;
    background: var(--bg);
    color: var(--fg);
    font-family: ui-sans-serif, system-ui, -apple-system, "Helvetica Neue", Arial, sans-serif;
    font-size: 13px;
    min-height: 100vh;
    overflow: hidden;         /* the canvas area handles its own overflow */
}

/* ---- Layout: sidebar + canvas ------------------------------------- */

#stage {
    display: grid;
    grid-template-columns: 240px 1fr;
    height: 100vh;
    min-height: 0;
}

#files-panel {
    background: var(--panel);
    border-right: 1px solid var(--panel-border);
    display: flex;
    flex-direction: column;
    min-height: 0;
}

#files-panel > header,
#files-panel > footer {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 0.75rem 0.75rem;
    border-bottom: 1px solid var(--panel-border);
    min-height: 46px;
}

#files-panel > footer {
    border-bottom: none;
    border-top: 1px solid var(--panel-border);
    gap: 0.4rem;
}

#files-panel h2 {
    margin: 0;
    font-size: 0.78rem;
    font-weight: 600;
    letter-spacing: 0.12em;
    text-transform: uppercase;
    color: var(--fg-dim);
}

#files-list {
    list-style: none;
    margin: 0;
    padding: 0.35rem 0;
    flex: 1 1 auto;
    overflow-y: auto;
    overflow-x: hidden;
}

#files-list:empty::before {
    content: "No files. Drag to import.";
    display: block;
    padding: 1.5rem 1rem;
    color: var(--fg-muted);
    font-size: 0.78rem;
    text-align: center;
    font-style: italic;
}

#files-list li {
    display: flex;
    align-items: center;
    gap: 0.55rem;
    padding: 0.3rem 0.5rem 0.3rem 0.7rem;
    margin: 0 0.3rem;
    border-radius: 4px;
    cursor: default;
    color: var(--fg);
    font-family: ui-monospace, Menlo, Consolas, monospace;
    font-size: 0.78rem;
    min-height: 26px;
}

#files-list li:hover {
    background: var(--panel-hi);
}

#files-list li .file-icon {
    flex: 0 0 auto;
    color: var(--fg-dim);
}

#files-list li .file-name {
    flex: 1 1 auto;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    cursor: pointer;
}

#files-list li .file-actions {
    flex: 0 0 auto;
    display: flex;
    gap: 0.15rem;
    opacity: 0;
    transition: opacity 0.1s ease;
}

#files-list li:hover .file-actions { opacity: 1; }

#files-list li .file-actions button {
    all: unset;
    color: var(--fg-muted);
    cursor: pointer;
    padding: 0.15rem 0.3rem;
    border-radius: 3px;
    display: inline-flex;
    align-items: center;
}
#files-list li .file-actions button:hover {
    color: var(--accent);
    background: rgba(102, 255, 221, 0.08);
}
#files-list li .file-actions .delete-btn:hover {
    color: var(--danger);
    background: rgba(255, 107, 107, 0.08);
}

/* ---- Screen area -------------------------------------------------- */

#screen-area {
    position: relative;
    display: flex;
    align-items: center;
    justify-content: center;
    background: var(--bg);
    min-width: 0;
    min-height: 0;
    overflow: hidden;
    padding: 32px;  /* breathing room around the canvas + its focus ring */
}

/* ---- Text editor (replaces the canvas when open) ---------------- */

#editor-area {
    position: absolute;
    inset: 0;
    display: flex;
    flex-direction: column;
    background: var(--bg);
    min-width: 0;
    min-height: 0;
}
#editor-area[hidden] { display: none; }

#editor-area > header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 0.55rem 0.75rem;
    border-bottom: 1px solid var(--panel-border);
    background: var(--panel);
}
#editor-title {
    font-family: ui-monospace, Menlo, Consolas, monospace;
    font-size: 0.82rem;
    color: var(--fg);
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}
.editor-actions { display: inline-flex; gap: 0.2rem; }

#editor-host {
    flex: 1 1 auto;
    min-height: 0;
    overflow: hidden;
}

/* CodeMirror fills its host; override a couple of defaults so dark
 * theme colours reach the gutter + scroll container. */
#editor-host .cm-editor { height: 100%; background: var(--bg); }
#editor-host .cm-scroller { font-family: ui-monospace, Menlo, Consolas, monospace; }

/* The focus ring lives on a wrapper instead of the canvas itself.
 * Using outline-offset on the canvas compounds border-radius with the
 * offset distance, so a 2 px canvas corner with 14 px offset renders
 * the ring at a 16 px radius. A wrapper with its own padding + radius
 * keeps the ring's corners independent (here: a tight 3 px). */
#screen-wrap {
    padding: 14px;
    border-radius: 3px;
    outline: 2px solid transparent;
    transition: outline-color 0.1s ease;
}
#screen-wrap:focus-within {
    outline-color: var(--accent);
}

#screen {
    image-rendering: pixelated;
    image-rendering: crisp-edges;
    background: #000;
    border-radius: 2px;
    outline: none;
    box-shadow: 0 4px 30px rgba(0, 0, 0, 0.6);
    display: block;
}

/* ---- Floating config button ------------------------------------ */

.icon-button {
    all: unset;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 30px;
    height: 30px;
    border-radius: 4px;
    color: var(--fg-dim);
    cursor: pointer;
    transition: color 0.08s ease, background 0.08s ease;
}
.icon-button:hover {
    color: var(--accent);
    background: var(--panel-hi);
}
.icon-button input[type="file"] {
    display: none;
}

.icon-button.floating {
    position: absolute;
    top: 0.8rem;
    right: 0.8rem;
    background: var(--panel);
    border: 1px solid var(--panel-border);
    backdrop-filter: blur(6px);
    z-index: 6;
}
/* Stack multiple floating buttons in the top-right. The cog is the
 * rightmost anchor; ? sits just to its left. Extend this pattern for
 * any future chrome that should live above the canvas. */
#open-help.floating { right: calc(0.8rem + 30px + 0.4rem); }
.icon-button.floating:hover {
    background: var(--panel-hi);
    border-color: var(--accent-dim);
}
/* The floating cog overlays the editor's top-right corner, hiding
 * the editor's own close button. Swap it out while the editor is up. */
#screen-area:has(#editor-area:not([hidden])) .icon-button.floating {
    display: none;
}

.ico {
    width: 18px;
    height: 18px;
    display: block;
}
.ico-large {
    width: 48px;
    height: 48px;
    display: block;
}

/* ---- Status bar (bottom) ----------------------------------------- */

#status {
    position: fixed;
    left: 240px;
    right: 0;
    bottom: 0;
    padding: 0.35rem 0.9rem;
    background: var(--panel);
    border-top: 1px solid var(--panel-border);
    color: var(--fg-muted);
    font-family: ui-monospace, Menlo, Consolas, monospace;
    font-size: 0.72rem;
    letter-spacing: 0.02em;
    min-height: 22px;
    z-index: 4;
    pointer-events: none;
}
#status.error { color: var(--danger); }

/* ---- Drop overlay ------------------------------------------------- */

#drop-overlay {
    position: fixed;
    inset: 0;
    background: rgba(8, 10, 14, 0.88);
    border: 2px dashed var(--accent);
    color: var(--accent);
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 1rem;
    font-size: 1.1rem;
    letter-spacing: 0.08em;
    pointer-events: none;
    z-index: 20;
}
#drop-overlay[hidden] { display: none; }

/* ---- Audio hint --------------------------------------------------- */

#audio-hint {
    position: fixed;
    left: 50%;
    bottom: 2.5rem;
    transform: translateX(-50%);
    display: inline-flex;
    align-items: center;
    gap: 0.5rem;
    background: var(--panel);
    border: 1px solid var(--panel-border);
    color: var(--accent);
    font-size: 0.78rem;
    padding: 0.4rem 0.8rem;
    border-radius: 20px;
    pointer-events: none;
    z-index: 8;
    box-shadow: 0 6px 16px rgba(0, 0, 0, 0.4);
}
#audio-hint[hidden] { display: none; }

#hint { display: none; }

/* ---- Config dialog ----------------------------------------------- */

#config-dialog {
    position: relative;  /* positioning context for the close button */
    background: var(--bg-elev);
    color: var(--fg);
    border: 1px solid var(--panel-border);
    border-radius: 8px;
    padding: 0;
    width: min(420px, 90vw);
    font: inherit;
    box-shadow: 0 16px 60px rgba(0, 0, 0, 0.6);
}
#config-dialog::backdrop { background: rgba(8, 10, 14, 0.7); }

/* Close button pinned 10 px from the dialog's top-right corner. */
#config-dialog #config-close {
    position: absolute;
    top: 10px;
    right: 10px;
    width: 26px;
    height: 26px;
    color: var(--fg-dim);
}
#config-dialog #config-close:hover {
    color: var(--fg);
    background: var(--panel-hi);
}

#config-form {
    /* Top padding clears the absolutely-positioned close button
     * (10 px offset + 26 px button + a little breathing room). */
    padding: 3rem 1rem 1rem 1rem;
}

.config-row {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 0.75rem;
    margin-top: 0.75rem;
    font-size: 0.85rem;
}
.config-row:first-of-type { margin-top: 0; }

.config-label {
    color: var(--fg);
    min-width: 7rem;
    font-size: 0.82rem;
}

.config-row select,
.config-row input[type="number"] {
    background: var(--bg);
    color: var(--fg);
    border: 1px solid var(--panel-border);
    border-radius: 4px;
    padding: 0.35rem 0.55rem;
    font: inherit;
    cursor: pointer;
}
.config-row select:hover,
.config-row input[type="number"]:hover {
    border-color: var(--accent-dim);
}

.slowdown-group {
    display: inline-flex;
    align-items: center;
    gap: 0.5rem;
}
.slowdown-group input[type="range"] {
    width: 7rem;
    accent-color: var(--accent);
}
.slowdown-group input[type="number"] {
    width: 5rem;
    text-align: right;
}

.config-hint {
    margin: 0.25rem 0 0.8rem 0;
    color: var(--fg-muted);
    font-size: 0.72rem;
    padding-left: 0.1rem;
    line-height: 1.4;
}

/* ---- Help / about dialog ---------------------------------------- */

#help-dialog {
    position: relative;
    background: var(--bg-elev);
    color: var(--fg);
    border: 1px solid var(--panel-border);
    border-radius: 8px;
    padding: 0;
    width: min(560px, 92vw);
    font: inherit;
    box-shadow: 0 16px 60px rgba(0, 0, 0, 0.6);
}
#help-dialog::backdrop { background: rgba(8, 10, 14, 0.7); }

#help-dialog #help-close {
    position: absolute;
    top: 10px;
    right: 10px;
    width: 26px;
    height: 26px;
    color: var(--fg-dim);
}
#help-dialog #help-close:hover {
    color: var(--fg);
    background: var(--panel-hi);
}

.help-body {
    padding: 1.25rem 1.5rem 1.5rem 1.5rem;
    font-size: 0.85rem;
    line-height: 1.5;
    max-height: 80vh;
    overflow-y: auto;
}
.help-body h2 {
    margin: 0 0 0.25rem 0;
    font-size: 1.1rem;
    color: var(--fg);
}
.help-body h3 {
    margin: 1.1rem 0 0.35rem 0;
    font-size: 0.82rem;
    text-transform: uppercase;
    letter-spacing: 0.08em;
    color: var(--fg-dim);
    font-weight: 600;
}
.help-body p {
    margin: 0.4rem 0;
    color: var(--fg);
}
.help-body ul {
    margin: 0.2rem 0;
    padding-left: 1.2rem;
}
.help-body li {
    margin: 0.2rem 0;
    color: var(--fg);
}
.help-body code {
    background: var(--panel);
    border: 1px solid var(--panel-border);
    border-radius: 3px;
    padding: 0.05rem 0.35rem;
    font-family: ui-monospace, Menlo, Consolas, monospace;
    font-size: 0.78rem;
    color: var(--accent);
}
.help-body a {
    color: var(--accent);
    text-decoration: none;
}
.help-body a:hover { text-decoration: underline; }
.help-footnote {
    margin-top: 1rem !important;
    padding-top: 0.75rem;
    border-top: 1px solid var(--panel-border);
    color: var(--fg-dim) !important;
    font-size: 0.78rem;
}

/* ---- Narrow viewport: stack panel above canvas ------------------- */

@media (max-width: 640px) {
    #stage {
        grid-template-columns: 1fr;
        grid-template-rows: auto 1fr;
    }
    #files-panel {
        border-right: none;
        border-bottom: 1px solid var(--panel-border);
        max-height: 30vh;
    }
    #status { left: 0; }
}
