/* ──────────────────────────────────────────────────────────────────────
   libpd-wasm — Pd-native demo, paper remix.
   Stylesheet extracted from webaudio-index-paper.html.
   ────────────────────────────────────────────────────────────────────── */

:root {
    --bg: #FCFCF7; /* near-white paper */
    --grid: rgba(160, 130, 60, 0.10);
    --panel: #FFFFFF;
    --ink: #0F1011;
    --ink2: #3A3833;
    --muted: #8A857A;
    --line: #1A1B1D;
    --line-soft: rgba(15, 16, 17, 0.18);
    --cable-sig: #2C7A56;
    --hilite: #E8C547;
    --hilite-soft: rgba(232, 197, 71, 0.42);
    --red: #B0382E;

    --font-ui: 'Inter', system-ui, -apple-system, sans-serif;
    --font-mono: 'JetBrains Mono', ui-monospace, 'SFMono-Regular', monospace;

    --grid-step: 18px;
    --sidebar-w: 17rem;
    --canvas-pad: 32px;

    color-scheme: only light;
}

* {
    box-sizing: border-box;
}

html, body {
    margin: 0;
    padding: 0;
}

body {
    font-family: var(--font-mono);
    font-size: 12.5px;
    line-height: 1.5;
    color: var(--ink);
    background: repeating-linear-gradient(0deg, transparent 0 calc(var(--grid-step) - 1px), var(--grid) calc(var(--grid-step) - 1px) var(--grid-step)),
    repeating-linear-gradient(90deg, transparent 0 calc(var(--grid-step) - 1px), var(--grid) calc(var(--grid-step) - 1px) var(--grid-step)),
    var(--bg);
    min-height: 100vh;
    display: flex;
    flex-direction: column;
}

a {
    color: var(--cable-sig);
    text-decoration-style: dotted;
    text-underline-offset: 2px;
}

a:hover {
    color: var(--ink);
}

/* ─── Top: comment-style header, no banded chrome ─── */
.pd-top {
    padding: var(--canvas-pad) var(--canvas-pad) 12px;
    max-width: 90rem;
    width: 100%;
    margin: 0 auto;
}

.pd-comment {
    font-family: var(--font-mono);
    font-size: 13px;
    color: var(--ink2);
    line-height: 1.65;
}

.pd-comment b {
    color: var(--ink);
    font-weight: 500;
}

.pd-comment code {
    font: inherit;
}

/* ─── Object boxes (transport buttons, [dac~], bundle, etc.) ─── */
.pd-obj, button.pd-obj {
    appearance: none;
    background: var(--panel);
    border: 1px solid var(--line);
    color: var(--ink);
    font-family: var(--font-mono);
    font-size: 12.5px;
    padding: 4px 12px;
    cursor: pointer;
    display: inline-flex;
    align-items: center;
    gap: 6px;
    position: relative;
    transition: background 80ms, transform 60ms;
}

.pd-obj:hover:not(:disabled) {
    background: #FFF8DC;
}

.pd-obj:active:not(:disabled) {
    background: var(--hilite);
    transform: translateY(0.5px);
}

.pd-obj:disabled {
    opacity: 0.42;
    cursor: not-allowed;
}

.pd-obj::before, .pd-obj::after {
    content: '';
    position: absolute;
    width: 7px;
    height: 2px;
    background: var(--line);
}

.pd-obj::before {
    top: -1px;
    left: 14px;
}

.pd-obj::after {
    bottom: -1px;
    right: 14px;
}

.pd-obj--accent {
    border-color: var(--line);
    border-left-width: 3px;
    border-left-color: var(--cable-sig);
    font-weight: 500;
}

/* While the transport is running, the primary button morphs into an
   explicit stop toggle (see main.js syncRunningChrome). Flip the
   cable-green accent edge to red so the role-switch is visible. */
.pd-obj.pd-obj--accent.pd-obj--running {
    border-left-color: var(--red);
}

.pd-obj.pd-obj--accent.pd-obj--running:hover:not(:disabled) {
    background: #FFE9E5;
}

.pd-obj-cap {
    font-family: var(--font-mono);
    font-size: 10.5px;
    color: var(--muted);
    margin-top: 3px;
    letter-spacing: 0.02em;
}

/* ─── Master volume — inline row inside transport ─── */
.pd-vol {
    display: inline-flex;
    align-items: center;
    gap: 10px;
    margin-left: auto;
}

.pd-vol-label {
    color: var(--muted);
    font-size: 11px;
    letter-spacing: 0.04em;
}

.pd-vol-val {
    color: var(--ink2);
    font-size: 11px;
    width: 2.6rem;
    text-align: right;
    font-variant-numeric: tabular-nums;
}

.pd-vol input[type=range] {
    width: 9rem;
    flex: 0 0 auto;
}

/* Stop button is wired up in JS (start/stop logic depends on it),
   but we don't want it visible — keep the element, hide the chrome. */
#stop {
    display: none;
}

/* ─── Transport row ─── */
.pd-transport {
    display: flex;
    align-items: flex-start;
    gap: 22px;
    flex-wrap: wrap;
}

/* ─── Pd-style range slider (hsl) ─── */
input[type="range"] {
    -webkit-appearance: none;
    appearance: none;
    width: 100%;
    height: 18px;
    margin: 0;
    padding: 0;
    background: transparent;
    border: 0;
    cursor: ew-resize;
}

input[type="range"]:focus {
    outline: 2px solid var(--hilite);
    outline-offset: -2px;
}

input[type="range"]::-webkit-slider-runnable-track {
    -webkit-appearance: none;
    appearance: none;
    box-sizing: border-box;
    height: 18px;
    background-color: var(--panel);
    border: 1px solid var(--line);
    box-shadow: none;
}

input[type="range"]::-webkit-slider-thumb {
    -webkit-appearance: none;
    appearance: none;
    width: 3px;
    height: 16px;
    background-color: var(--ink);
    border: 0;
    border-radius: 0;
    box-shadow: none;
    margin-top: 0;
    cursor: ew-resize;
}

input[type="range"]::-moz-range-track {
    box-sizing: border-box;
    height: 18px;
    background-color: var(--panel);
    border: 1px solid var(--line);
    box-shadow: none;
}

input[type="range"]::-moz-range-thumb {
    width: 3px;
    height: 16px;
    background: var(--ink);
    border-radius: 0;
    border: 0;
    cursor: ew-resize;
}

/* ─── App layout: sidebar | canvas ─── */
.pd-app {
    display: grid;
    grid-template-columns: var(--sidebar-w) 1fr;
    gap: 32px;
    padding: 4px var(--canvas-pad) 8px;
    max-width: 90rem;
    width: 100%;
    margin: 0 auto;
    flex: 1;
    min-height: 0;
}

/* ─── Sidebar (patch list) ─── */
.pd-sidebar {
    min-width: 0;
}

.pd-sidebar-hd {
    font-family: var(--font-mono);
    font-size: 11.5px;
    color: var(--muted);
    margin-bottom: 10px;
}

.pd-sidebar-hd b {
    color: var(--ink2);
    font-weight: 500;
}

.pd-sidebar-hd .pd-count {
    color: var(--muted);
    margin-left: 4px;
}

#pdPatchList {
    display: flex;
    flex-direction: column;
    gap: 6px;
}

.pd-patch {
    appearance: none;
    background: var(--panel);
    border: 1px solid var(--line);
    color: var(--ink);
    padding: 3px 10px 3px 8px;
    font-family: var(--font-mono);
    font-size: 11.5px;
    cursor: pointer;
    display: flex;
    align-items: center;
    gap: 8px;
    text-align: left;
    position: relative;
    transition: background 80ms;
    width: 100%;
}

.pd-patch:hover {
    background: #FFF8DC;
}

.pd-patch::before {
    content: '';
    position: absolute;
    top: -1px;
    left: 14px;
    width: 7px;
    height: 2px;
    background: var(--line);
}

.pd-patch.is-active {
    background: var(--hilite);
    outline: 1px solid var(--line);
    outline-offset: -1px;
    font-weight: 500;
}

/* Filled color SQUARE before each patch (not a circle dot) */
.pd-patch-tag {
    width: 11px;
    height: 11px;
    border: 1px solid var(--line);
    flex-shrink: 0;
}

.pd-patch-name {
    flex: 1;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.pd-patch-lib {
    font-size: 9px;
    padding: 1px 4px;
    border: 1px solid var(--line-soft);
    color: var(--muted);
    letter-spacing: 0.05em;
    text-transform: uppercase;
    flex-shrink: 0;
}

.pd-patch.is-active .pd-patch-lib {
    border-color: var(--ink2);
    color: var(--ink2);
}

.pd-patch--more {
    border-style: dashed;
    text-align: center;
    justify-content: center;
    color: var(--muted);
    cursor: default;
}

.pd-patch--more:hover {
    background: var(--panel);
}

/* Hide native select that main.js drives */
#patchPicker {
    position: absolute;
    left: -10000px;
    width: 1px;
    height: 1px;
}

/* ─── Canvas: bare, no panel chrome ─── */
.pd-canvas {
    min-width: 0;
    display: flex;
    flex-direction: column;
    gap: 22px;
}

/* Selected-patch eyebrow + title */
.pd-eyebrow {
    font-family: var(--font-mono);
    font-size: 12px;
    color: var(--muted);
}

.pd-eyebrow b {
    color: var(--ink2);
    font-weight: 500;
}

.pd-eyebrow .pd-cat {
    font-weight: 500;
}

.pd-title-h {
    font-family: var(--font-ui);
    font-size: 36px;
    font-weight: 500;
    line-height: 1.05;
    letter-spacing: -0.02em;
    color: var(--ink);
    margin: 8px 0 12px;
}

.pd-desc {
    font-family: var(--font-mono);
    font-size: 12.5px;
    color: var(--ink2);
    line-height: 1.7;
    max-width: 48rem;
    min-height: 1.4em;
}

.pd-desc:empty::before {
    content: '\\200B';
}

/* ─── Scope: bare canvas, full width ─── */
.pd-scope-canvas-wrap {
    background: #070A08;
    border: 1px solid var(--line);
    position: relative;
    min-height: 200px;
}

#pd-scope {
    display: block;
    width: 100%;
    height: 100%;
    min-height: 200px;
}

.pd-scope-overlay {
    position: absolute;
    inset: 0;
    pointer-events: none;
    padding: 6px 10px;
    font-family: var(--font-mono);
    font-size: 10px;
    color: rgba(214, 234, 224, 0.42);
    display: flex;
    justify-content: space-between;
}

.pd-scope-overlay .pd-scope-source {
    color: rgba(232, 197, 71, 0.7);
}

.pd-bundle-pills {
    display: inline-flex;
    gap: 0;
    border: 1px solid var(--line);
    background: var(--panel);
    font-family: var(--font-mono);
    font-size: 11.5px;
    position: relative;
}

.pd-bundle-pills::before {
    content: '';
    position: absolute;
    top: -1px;
    left: 14px;
    width: 7px;
    height: 2px;
    background: var(--line);
}

.pd-bundle-pill {
    padding: 3px 10px;
    color: var(--muted);
    border-right: 1px solid var(--line-soft);
}

.pd-bundle-pill:last-child {
    border-right: none;
}

.pd-bundle-pill.is-active {
    background: var(--hilite);
    color: var(--ink);
    font-weight: 500;
}

/* ─── Controls: bare, with `;;` comment header and 2-column grid ─── */
.pd-controls-head {
    font-family: var(--font-mono);
    font-size: 11.5px;
    color: var(--muted);
    margin-bottom: 10px;
}

.pd-controls-head b {
    color: var(--ink2);
    font-weight: 500;
}

.pd-controls-head .pd-meta {
    color: var(--muted);
    float: right;
}

/* Two columns when there's room, single column when narrow */
#controls {
    display: grid;
    grid-template-columns: 1fr 1fr;
    column-gap: 40px;
    row-gap: 0;
    transition: opacity 120ms;
}

#controls > em {
    grid-column: 1 / -1;
    font-family: var(--font-mono);
    font-style: normal;
    font-size: 12px;
    color: var(--muted);
    padding: 6px 0;
}

#controls.locked {
    opacity: 0.5;
}

.ctrl {
    display: grid;
    grid-template-columns: minmax(5rem, 8rem) minmax(6rem, 1fr) minmax(3rem, 5rem);
    gap: 12px;
    align-items: center;
    padding: 4px 0;
}

.ctrl label {
    font-family: var(--font-mono);
    font-size: 12px;
    color: var(--ink);
    text-align: right;
    cursor: default;
}

.ctrl .val {
    font-family: var(--font-mono);
    font-size: 11px;
    color: var(--muted);
    text-align: right;
    font-variant-numeric: tabular-nums;
}

/* Pd toggle (tgl) */
.ctrl input[type=checkbox] {
    appearance: none;
    -webkit-appearance: none;
    width: 18px;
    height: 18px;
    background: var(--panel);
    border: 1px solid var(--line);
    cursor: pointer;
    justify-self: start;
    position: relative;
    margin: 0;
}

.ctrl input[type=checkbox]:focus {
    outline: 2px solid var(--hilite);
    outline-offset: -2px;
}

.ctrl input[type=checkbox]:checked::before,
.ctrl input[type=checkbox]:checked::after {
    content: '';
    position: absolute;
    inset: 3px;
    background: var(--ink);
}

.ctrl input[type=checkbox]:checked::before {
    clip-path: polygon(0 0, 18% 0, 100% 100%, 82% 100%);
}

.ctrl input[type=checkbox]:checked::after {
    clip-path: polygon(82% 0, 100% 0, 18% 100%, 0 100%);
}

/* Pd bang button (bng) */
.ctrl > button {
    appearance: none;
    width: 20px;
    height: 20px;
    background: var(--panel);
    border: 1px solid var(--line);
    border-radius: 50%;
    padding: 2px;
    cursor: pointer;
    justify-self: start;
    position: relative;
    font-size: 0;
    color: transparent;
}

.ctrl > button::before {
    content: '';
    position: absolute;
    inset: 2px;
    border: 1px solid var(--line);
    border-radius: 50%;
}

.ctrl > button:hover {
    background: #FFF8DC;
}

.ctrl > button:active::before {
    background: var(--hilite);
    border-color: var(--hilite);
}

/* Pd number box (nbx) */
.ctrl input[type=number] {
    width: 100%;
    background: var(--panel);
    border: 1px solid var(--line);
    padding: 3px 6px;
    font-family: var(--font-mono);
    font-size: 12px;
    color: var(--ink);
}

.ctrl input[type=number]:focus {
    outline: 2px solid var(--hilite);
    outline-offset: -2px;
}

/* ─── Source: bare comment header + drawer ─── */
#patchSourcePanel {
    background: var(--panel);
    border: 1px solid var(--line);
}

#patchSourcePanel summary {
    cursor: pointer;
    padding: 5px 14px;
    list-style: none;
    font-family: var(--font-mono);
    font-size: 11.5px;
    color: var(--ink2);
    display: flex;
    align-items: center;
    gap: 8px;
    user-select: none;
    background: transparent;
    border-bottom: 1px solid var(--line-soft);
}

#patchSourcePanel summary > * {
    white-space: nowrap;
}

#patchSourcePanel summary::-webkit-details-marker {
    display: none;
}

#patchSourcePanel summary::before {
    content: '▾';
    color: var(--muted);
    transition: transform 120ms;
    display: inline-block;
    width: 10px;
}

#patchSourcePanel:not([open]) summary::before {
    transform: rotate(-90deg);
}

#patchSourcePanel:not([open]) summary {
    border-bottom-color: transparent;
}

#patchSourcePanel summary b {
    color: var(--ink);
    font-weight: 500;
}

#patchSourcePanel summary .pd-source-meta {
    margin-left: auto;
    color: var(--muted);
    flex-shrink: 0;
}

.pd-source-body {
    padding: 12px 14px;
}

#patch {
    width: 100%;
    min-height: 12rem;
    font-family: var(--font-mono);
    font-size: 11.5px;
    padding: 10px 12px;
    border: 1px solid var(--line);
    background: repeating-linear-gradient(0deg, transparent 0 calc(1.5em - 1px), var(--line-soft) calc(1.5em - 1px) 1.5em),
    var(--panel);
    line-height: 1.5;
    color: var(--ink);
    resize: vertical;
    tab-size: 2;
}

#patch:focus {
    outline: 2px solid var(--hilite);
    outline-offset: -2px;
}

#patch.dropping, #patchSourcePanel.dropping {
    border-color: var(--cable-sig);
    background-color: rgba(44, 122, 86, 0.06);
}

#patch.dropping {
    background: repeating-linear-gradient(0deg, transparent 0 calc(1.5em - 1px), var(--line-soft) calc(1.5em - 1px) 1.5em),
    rgba(44, 122, 86, 0.06);
}

.pd-source-body .pd-row {
    display: flex;
    gap: 8px;
    margin-top: 10px;
    justify-content: flex-end;
}

.pd-log-head {
    margin: 14px 0 6px;
    font-family: var(--font-mono);
    font-size: 11.5px;
    color: var(--ink2);
}

.pd-log-head b {
    color: var(--ink);
    font-weight: 500;
}

#log {
    display: block;
    margin: 0;
    padding: 8px 10px;
    background: var(--panel);
    border: 1px solid var(--line);
    font-family: var(--font-mono);
    font-size: 11px;
    color: var(--ink2);
    line-height: 1.55;
    max-height: 12rem;
    min-height: 4.5rem;
    overflow: auto;
    white-space: pre-wrap;
    word-break: break-word;
}

#log:empty::before {
    content: '(no output yet)';
    color: var(--muted);
    font-style: italic;
}

/* ─── Responsive ─── */
@media (max-width: 1100px) {
    #controls {
        grid-template-columns: 1fr;
    }
}

@media (max-width: 880px) {
    .pd-app {
        grid-template-columns: 1fr;
    }

    .pd-sidebar {
        padding-bottom: 8px;
        border-bottom: 1px dotted var(--line-soft);
        margin-bottom: 8px;
    }

    #pdPatchList {
        display: grid;
        grid-template-columns: repeat(auto-fill, minmax(11rem, 1fr));
        gap: 4px;
    }
}

@media (max-width: 720px) {
    :root {
        --canvas-pad: 18px;
    }

    .pd-transport {
        gap: 14px;
    }

    .pd-title-h {
        font-size: 28px;
    }

    .ctrl {
        grid-template-columns: 1fr minmax(8rem, 1fr) auto;
        gap: 8px;
    }

    .ctrl label {
        text-align: left;
    }
}

@media (max-width: 480px) {
    .ctrl {
        grid-template-columns: 1fr 1fr;
        row-gap: 4px;
    }

    .ctrl .val {
        text-align: right;
    }

    .ctrl input[type=range] {
        grid-column: 1 / -1;
    }
}

@media (prefers-reduced-motion: reduce) {
    *, *::before, *::after {
        animation: none !important;
        transition: none !important;
    }
}
