/* ===================================================================
   Pedalboard Planner — layered on top of Bootstrap 5 dark theme
   =================================================================== */

:root {
    --pp-sidebar-bg: #181a1d;
    --pp-sidebar-border: #2a2d31;
    --pp-canvas-bg: #18191b;
    --pp-canvas-grid: rgba(255, 255, 255, 0.04);
    --pp-accent: #4caf50;
    --pp-accent-soft: rgba(76, 175, 80, .22);
    --pp-success: #7ee495;
    --pp-danger: #ef4444;
    --pp-board-frame: #2a2d31;
}

html, body {
    height: 100vh;
    height: 100dvh; /* dynamic viewport — correct under mobile browser URL bars */
    margin: 0;
    overflow: hidden;
    overscroll-behavior: none; /* kill pull-to-refresh / rubber-banding */
}

body {
    display: flex;
    background-color: var(--pp-canvas-bg);
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto,
                 "Helvetica Neue", Arial, sans-serif;
}

/* ---------- SIDEBAR ---------- */

#sidebar {
    width: 320px;
    min-width: 320px;
    background-color: var(--pp-sidebar-bg);
    border-right: 1px solid var(--pp-sidebar-border);
    z-index: 10;
    position: relative;
    overflow: hidden;
}

.sidebar-brand {
    border-bottom: 1px solid var(--pp-sidebar-border);
}

.brand-icon {
    color: var(--pp-accent);
    font-size: 1.4rem;
}

.sidebar-brand h1 {
    letter-spacing: .2px;
}

.sidebar-body {
    overflow: hidden;
    min-height: 0;
}

.sidebar-footer {
    border-top: 1px solid var(--pp-sidebar-border);
}

.step-badge {
    display: inline-flex;
    width: 18px;
    height: 18px;
    border-radius: 50%;
    background: var(--pp-accent-soft);
    color: var(--pp-accent);
    align-items: center;
    justify-content: center;
    font-weight: 700;
    font-size: .7rem;
    margin-right: 4px;
    vertical-align: 1px;
}

.autosave-indicator {
    opacity: .55;
    transition: opacity .2s ease, color .2s ease;
}

.autosave-indicator.flash {
    opacity: 1;
    color: var(--pp-success);
}

/* ---------- ON-CANVAS LIST (sidebar) ---------- */

#on-canvas-list {
    min-height: 0;
}

.board-list-item {
    background: rgba(255, 255, 255, 0.03);
    padding: 8px 10px;
    margin-bottom: 6px;
    border-radius: 6px;
    font-size: 0.85rem;
    cursor: pointer;
    border-left: 3px solid transparent;
    transition: background .15s ease, border-color .15s ease;
}

.board-list-item:hover {
    background: rgba(255, 255, 255, 0.06);
}

.board-list-item.selected {
    border-left-color: var(--pp-accent);
    background: rgba(76, 201, 240, 0.10);
}

.board-list-item-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: 6px;
}

.pedal-sub-list {
    list-style-type: none;
    padding-left: 8px;
    margin: 6px 0 0;
    border-left: 1px dashed #4a4d52;
    margin-left: 4px;
}

.pedal-sub-item {
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: 6px;
    font-size: 0.78rem;
    padding: 2px 0 2px 8px;
    color: #c0c4c9;
}

.remove-btn {
    background: transparent;
    color: var(--pp-danger);
    border: none;
    padding: 2px 6px;
    border-radius: 4px;
    font-size: 0.8rem;
    line-height: 1;
    cursor: pointer;
}

.remove-btn:hover {
    background: var(--pp-danger);
    color: #fff;
}

/* ---------- MAIN / CANVAS ---------- */

#main {
    flex: 1;
    display: flex;
    flex-direction: column;
    height: 100vh;
    height: 100dvh;
    position: relative;
    min-width: 0;
}

#toolbar {
    background-color: #1c1e21;
    border-bottom: 1px solid var(--pp-sidebar-border);
    z-index: 5;
}

#canvas-container {
    flex: 1;
    width: 100%;
    height: 100%;
    overflow: hidden;
    position: relative;
    background-color: var(--pp-canvas-bg);
    background-image:
        radial-gradient(var(--pp-canvas-grid) 1px, transparent 1px);
    background-size: 20px 20px;
    cursor: grab;
    touch-action: none; /* we own pan/pinch via pointer events */
}

.pedal, .placed-board {
    touch-action: none;
}

/* Stop stray text selection / the iOS long-press callout from hijacking
   touch navigation. Inputs stay selectable so search still works.
   `manipulation` disables double-tap-zoom without the tap-delay/JS hacks. */
body {
    -webkit-user-select: none;
    user-select: none;
    -webkit-touch-callout: none;
    touch-action: manipulation;
}

input, textarea {
    -webkit-user-select: text;
    user-select: text;
}

#canvas-container:active {
    cursor: grabbing;
}

#board-wrapper {
    position: absolute;
    transform-origin: 0 0;
    left: 0;
    top: 0;
    will-change: transform;
}

/* ---------- BOARDS ---------- */

.placed-board {
    margin: 0;
    padding: 0;
    position: absolute;
    top: 0;
    left: 0;
    box-sizing: border-box;
    border-radius: 8px;
    transition: box-shadow .15s ease-in-out;
}

.placed-board.custom-frame {
    border-radius: 0;
    background-color: #242729;
    box-shadow:
        inset 0 0 15px rgba(0, 0, 0, 0.75),
        0 6px 15px rgba(0, 0, 0, 0.5);
    border-style: solid;
    border-width: 12px;
    border-top-color: #e2e6e9;
    border-right-color: #abb0b3;
    border-bottom-color: #8c9194;
    border-left-color: #ccd0d3;
}

.placed-board.focused {
    box-shadow:
        0 0 0 2px var(--pp-accent),
        0 6px 15px rgba(0, 0, 0, 0.5);
}

/* ---------- PEDALS ---------- */

.pedal {
    position: absolute;
    cursor: grab;
    user-select: none;
    border-radius: 4px;
}

.pedal:active {
    cursor: grabbing;
}

.pedal img {
    width: 100%;
    height: 100%;
    border-radius: 4px;
    pointer-events: none;
    object-fit: contain;
}

.pedal.focused {
    outline: 2px solid var(--pp-accent);
    outline-offset: 2px;
    box-shadow: 0 0 16px var(--pp-accent-soft);
}

/* ---------- CUSTOM SEARCH LISTS ---------- */

.custom-list {
    background: #111316;
    border: 1px solid #2a2d31;
    border-radius: 6px;
    max-height: 220px;
    overflow-y: auto;
    display: none;
    flex-direction: column;
    margin-top: 4px;
}

.custom-list.active {
    display: flex;
}

.list-item {
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 6px 10px;
    color: #c5c9cd;
    font-size: 0.85rem;
    cursor: pointer;
    border-bottom: 1px solid #1c1e21;
    user-select: none;
}

.list-item:last-child {
    border-bottom: none;
}

.list-thumb {
    width: 38px;
    height: 38px;
    flex: 0 0 38px;
    object-fit: contain;
    border-radius: 4px;
    background: #0f1114;
    pointer-events: none;
}

.list-label {
    flex: 1 1 auto;
    min-width: 0;
}

/* hover effect — only when NOT in keyboard-navigation mode */
body:not(.kbd-nav) .list-item:hover {
    background: rgba(76, 201, 240, 0.15);
    color: #fff;
}

.list-item.highlighted {
    background: var(--pp-accent);
    color: #fff;
    font-weight: 600;
}

.list-sentinel {
    height: 1px;
    margin-top: -1px;
}

/* ---------- PREVIEW OVERLAY ---------- */

#preview-overlay {
    position: absolute;
    inset: 0;
    background: rgba(0, 0, 0, 0.85);
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    z-index: 1000;
    pointer-events: none;
    opacity: 1;
    transition: opacity .15s ease-in-out;
}

#preview-overlay.hidden {
    opacity: 0;
    pointer-events: none;
}

#preview-image {
    max-width: 60%;
    max-height: 60%;
    box-shadow: 0 20px 50px rgba(0, 0, 0, 1);
    border-radius: 10px;
}

#preview-image[src=""],
#preview-image.hidden {
    display: none;
}

#preview-name {
    margin-top: 18px;
    color: #fff;
    font-size: 1.75rem;
    text-shadow: 2px 2px 4px rgba(0, 0, 0, 0.5);
    text-align: center;
    max-width: 70%;
}

#board-preview-dimensions {
    color: #bbb;
    font-size: 1rem;
    text-align: center;
    margin-top: 6px;
    display: none;
}

#board-preview-dimensions.visible {
    display: block;
}

/* ---------- FOOTER STATUS BAR ---------- */

#footer-status-bar {
    background-color: #1a1c1f;
    border-top: 1px solid var(--pp-sidebar-border);
    z-index: 20;
}

#help-status-bar {
    color: #8a9095;
    font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
    letter-spacing: .3px;
}

#help-status-bar .key {
    background: #2a2d31;
    padding: 1px 6px;
    border-radius: 3px;
    color: #c5c9cd;
    font-size: .75rem;
    margin: 0 1px;
}

.bg-shade {
    width: 18px;
    height: 18px;
    border-radius: 50%;
    border: 1px solid #444;
    cursor: pointer;
    box-shadow: 0 1px 3px rgba(0, 0, 0, 0.3);
    transition: transform .1s ease;
}

.bg-shade:hover {
    transform: scale(1.15);
}

.bg-shade.selected {
    border: 2px solid var(--pp-accent);
}

/* ---------- BOOTSTRAP FORM TWEAKS for dark canvas ---------- */

.form-control {
    background-color: #0f1114;
    border-color: #2a2d31;
    color: #e6e8ea;
}

.form-control:focus {
    background-color: #0f1114;
    border-color: var(--pp-accent);
    color: #fff;
    box-shadow: 0 0 0 .15rem var(--pp-accent-soft);
}

.form-check-input:checked {
    background-color: var(--pp-accent);
    border-color: var(--pp-accent);
}

/* ---------- UTILITIES ---------- */

.hidden { display: none !important; }

[hidden] { display: none !important; }

/* ===================================================================
   MOBILE — bottom sheet (portrait) + side drawer (landscape)
   Desktop defaults: handle, backdrop and menu button are hidden.
   =================================================================== */

#sheet-handle,
#sidebar-backdrop {
    display: none;
}

/* Reachability on mobile (sheet + drawer): scroll the panel body. */
@media (max-width: 767px), (orientation: landscape) and (max-height: 500px) {
    .sidebar-body {
        overflow-y: auto;
        -webkit-overflow-scrolling: touch;
    }
    .sidebar-body .control-group.flex-grow-1 {
        flex: 0 0 auto;
    }
    #on-canvas-list {
        overflow: visible;
    }
    /* Pad the scroll area by the keyboard height so the last results can
       scroll clear of the on-screen keyboard. Driven by the actual keyboard
       (visualViewport), not focus state, so tapping a result never shifts it. */
    body.kb-open .sidebar-body {
        padding-bottom: var(--kb, 0);
    }
}

/* Touch action bar: never on desktop (keyboard shortcuts cover it).
   On mobile the media queries reveal it via #touch-actions:not([hidden]). */
#touch-actions {
    display: none;
    position: fixed;
    left: 50%;
    transform: translateX(-50%);
    z-index: 150;
    gap: 8px;
    padding: 8px 10px;
    border-radius: 12px;
    background: rgba(28, 30, 33, 0.96);
    border: 1px solid var(--pp-sidebar-border);
    box-shadow: 0 6px 20px rgba(0, 0, 0, 0.5);
}

/* ---- Phone portrait: #sidebar is a bottom sheet ---- */
@media (max-width: 767px) and (orientation: portrait) {
    #sidebar {
        position: fixed;
        left: 0;
        right: 0;
        bottom: 0;
        width: 100%;
        min-width: 0;
        height: 78dvh;
        border-right: none;
        border-top: 1px solid var(--pp-sidebar-border);
        border-radius: 16px 16px 0 0;
        z-index: 200;
        transform: translateY(calc(78dvh - var(--sheet-peek, 96px)));
        transition: transform .28s cubic-bezier(.32, .72, 0, 1);
        padding-bottom: env(safe-area-inset-bottom);
        box-shadow: 0 -8px 30px rgba(0, 0, 0, .45);
    }

    #sidebar.open {
        transform: translateY(0);
    }

    /* Grab handle is the always-visible peek */
    #sheet-handle {
        display: block;
        width: 100%;
        height: 28px;
        border: none;
        background: transparent;
        position: relative;
        flex: 0 0 auto;
        cursor: grab;
        touch-action: none;
    }

    #sheet-handle::before {
        content: "";
        position: absolute;
        top: 10px;
        left: 50%;
        transform: translateX(-50%);
        width: 40px;
        height: 5px;
        border-radius: 3px;
        background: #5a5e63;
    }

    /* No big brand header in the sheet — the handle replaces it.
       !important to beat Bootstrap's .d-flex utility on the same element. */
    .sidebar-brand {
        display: none !important;
    }

    /* Toolbar: icons only, drop the "Workspace" label and menu button */
    .toolbar-title,
    .btn-label {
        display: none;
    }

    #footer-status-bar {
        display: none !important;
    }

    /* No hover on touch — thumbnails are the preview */
    #preview-overlay {
        display: none !important;
    }

    /* Action bar sits just above the collapsed sheet peek */
    #touch-actions:not([hidden]) {
        display: flex;
        bottom: calc(var(--sheet-peek, 96px) + env(safe-area-inset-bottom) + 14px);
    }
}

/* ---- Phone landscape: #sidebar is a left slide-in drawer ---- */
@media (orientation: landscape) and (max-height: 500px) {
    #sidebar {
        position: fixed;
        top: 0;
        left: 0;
        height: 100dvh;
        width: min(86%, 340px);
        transform: translateX(-100%);
        transition: transform .25s ease;
        z-index: 200;
        padding-left: env(safe-area-inset-left);
        box-shadow: 6px 0 30px rgba(0, 0, 0, .5);
    }

    #sidebar.open {
        transform: translateX(0);
    }

    #sidebar-backdrop {
        display: block;
        position: fixed;
        inset: 0;
        background: rgba(0, 0, 0, .5);
        z-index: 199;
        opacity: 0;
        pointer-events: none;
        transition: opacity .25s ease;
    }

    #sidebar-backdrop.open {
        opacity: 1;
        pointer-events: auto;
    }

    .toolbar-title,
    .btn-label {
        display: none;
    }

    #footer-status-bar {
        display: none !important;
    }

    #toolbar {
        padding-top: env(safe-area-inset-top);
    }

    #preview-overlay {
        display: none !important;
    }

    #touch-actions:not([hidden]) {
        display: flex;
        bottom: calc(env(safe-area-inset-bottom) + 14px);
    }
}
