:root {
    color-scheme: dark;
    font-family: system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
    --panel-bg: rgba(22, 25, 34, 0.82);
    --panel-border: rgba(255, 255, 255, 0.08);
    --text: rgba(255, 255, 255, 0.9);
    --text-dim: rgba(255, 255, 255, 0.65);
    --accent: #7fe0ff;
}

* {
    box-sizing: border-box;
}

html,
body {
    margin: 0;
    height: 100%;
    overflow: hidden;
    background: radial-gradient(circle at top, #282840, #111119 60%);
    color: var(--text);
}

#canvas-container {
    position: fixed;
    inset: 0;
}

#canvas-container canvas {
    display: block;
}

/* ---------- back link ---------- */
.back-link {
    position: fixed;
    top: 16px;
    left: 16px;
    z-index: 20;
    padding: 8px 14px;
    border-radius: 999px;
    background: var(--panel-bg);
    border: 1px solid var(--panel-border);
    color: var(--text-dim);
    text-decoration: none;
    font-size: 0.85rem;
    backdrop-filter: blur(10px);
    transition: color 140ms ease, border-color 140ms ease;
}

.back-link:hover {
    color: var(--text);
    border-color: rgba(255, 255, 255, 0.25);
}

/* ---------- UI panel ---------- */
#ui-container {
    position: fixed;
    top: 16px;
    right: 16px;
    z-index: 20;
    width: 290px;
    max-height: calc(100vh - 32px);
    overflow-y: auto;
    padding: 18px;
    border-radius: 16px;
    background: var(--panel-bg);
    border: 1px solid var(--panel-border);
    box-shadow: 0 20px 60px rgba(0, 0, 0, 0.4);
    backdrop-filter: blur(12px);
    display: flex;
    flex-direction: column;
    gap: 14px;
}

body.ui-collapsed #ui-container {
    display: none;
}

#ui-toggle {
    position: fixed;
    top: 16px;
    right: 16px;
    z-index: 19;
    display: none;
}

body.ui-collapsed #ui-toggle {
    display: block;
}

#panel-handle,
#mobile-toolbar {
    display: none;
}

.ui-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 8px;
}

.ui-header h1 {
    margin: 0;
    font-size: 1.15rem;
    letter-spacing: 0.03em;
}

.header-controls {
    display: flex;
    gap: 6px;
}

.subtitle {
    font-size: 0.82rem;
    line-height: 1.5;
    color: var(--text-dim);
}

.subtitle a,
.modal-content a {
    color: var(--accent);
}

/* ---------- buttons & inputs ---------- */
.icon-btn {
    width: 34px;
    height: 34px;
    border-radius: 10px;
    border: 1px solid var(--panel-border);
    background: rgba(41, 45, 59, 0.85);
    color: var(--text);
    font-size: 1rem;
    cursor: pointer;
    backdrop-filter: blur(10px);
}

.icon-btn.small {
    width: 26px;
    height: 26px;
    font-size: 0.8rem;
    border-radius: 8px;
}

.icon-btn:hover {
    border-color: rgba(255, 255, 255, 0.25);
}

button {
    font-family: inherit;
}

.accent-btn {
    width: 100%;
    padding: 10px 12px;
    border-radius: 12px;
    border: 1px solid rgba(127, 224, 255, 0.35);
    background: rgba(127, 224, 255, 0.12);
    color: var(--accent);
    font-size: 0.92rem;
    font-weight: 600;
    cursor: pointer;
    transition: background 140ms ease, border-color 140ms ease;
}

.accent-btn:hover {
    background: rgba(127, 224, 255, 0.22);
    border-color: rgba(127, 224, 255, 0.6);
}

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

.control-group>label {
    font-size: 0.78rem;
    text-transform: uppercase;
    letter-spacing: 0.06em;
    color: var(--text-dim);
    display: flex;
    justify-content: space-between;
    align-items: baseline;
}

.value-badge {
    font-size: 0.78rem;
    color: var(--accent);
    text-transform: none;
    letter-spacing: 0;
    font-variant-numeric: tabular-nums;
}

.preset-grid {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 7px;
}

.texture-grid {
    grid-template-columns: repeat(3, 1fr);
}

.preset-btn,
.texture-btn,
#reset-view-btn {
    padding: 8px 10px;
    border-radius: 10px;
    border: 1px solid var(--panel-border);
    background: rgba(41, 45, 59, 0.85);
    color: var(--text);
    font-size: 0.85rem;
    cursor: pointer;
    transition: background 140ms ease, border-color 140ms ease;
}

.preset-btn:hover,
.texture-btn:hover,
#reset-view-btn:hover {
    background: rgba(61, 66, 82, 0.95);
    border-color: rgba(255, 255, 255, 0.25);
}

.preset-btn.active,
.texture-btn.active {
    border-color: rgba(127, 224, 255, 0.6);
    background: rgba(127, 224, 255, 0.14);
    color: var(--accent);
}

input[type="range"] {
    width: 100%;
    accent-color: var(--accent);
}

.checks {
    gap: 6px;
}

.check {
    display: flex;
    align-items: center;
    gap: 8px;
    font-size: 0.86rem;
    color: var(--text);
    cursor: pointer;
}

.check input {
    accent-color: var(--accent);
}

/* ---------- stats ---------- */
.stats {
    border-top: 1px solid var(--panel-border);
    padding-top: 10px;
    display: flex;
    flex-direction: column;
    gap: 4px;
}

.stat-row {
    display: flex;
    justify-content: space-between;
    font-size: 0.8rem;
    color: var(--text-dim);
}

.stat-value {
    color: var(--text);
    font-variant-numeric: tabular-nums;
}

/* ---------- draw overlay ---------- */
#draw-overlay {
    position: fixed;
    inset: 0;
    z-index: 40;
    display: none;
    background: rgba(10, 11, 16, 0.55);
    cursor: crosshair;
}

#draw-overlay.visible {
    display: block;
}

#draw-canvas {
    position: absolute;
    inset: 0;
    touch-action: none;
}

.draw-hint {
    position: absolute;
    top: 18px;
    left: 50%;
    transform: translateX(-50%);
    z-index: 41;
    display: flex;
    align-items: center;
    gap: 14px;
    max-width: min(92vw, 640px);
    padding: 10px 16px;
    border-radius: 12px;
    background: var(--panel-bg);
    border: 1px solid var(--panel-border);
    backdrop-filter: blur(10px);
    font-size: 0.88rem;
    color: var(--text);
    pointer-events: none;
}

.draw-hint button {
    pointer-events: auto;
    padding: 6px 14px;
    border-radius: 9px;
    border: 1px solid var(--panel-border);
    background: rgba(41, 45, 59, 0.85);
    color: var(--text);
    cursor: pointer;
    font-size: 0.85rem;
    flex-shrink: 0;
}

.draw-hint button:hover {
    border-color: rgba(255, 255, 255, 0.25);
}

/* ---------- modal ---------- */
.modal {
    position: fixed;
    inset: 0;
    z-index: 50;
    display: none;
    align-items: center;
    justify-content: center;
    background: rgba(0, 0, 0, 0.55);
    padding: 20px;
}

.modal.visible {
    display: flex;
}

.modal-content {
    position: relative;
    width: min(92vw, 620px);
    max-height: 84vh;
    overflow-y: auto;
    padding: 28px 32px;
    border-radius: 16px;
    background: rgba(28, 31, 42, 0.97);
    border: 1px solid var(--panel-border);
    box-shadow: 0 20px 60px rgba(0, 0, 0, 0.5);
    line-height: 1.6;
}

.modal-content h2 {
    margin-top: 0;
}

.modal-content h3 {
    margin-bottom: 0.4em;
    color: var(--accent);
}

.modal-content p,
.modal-content li {
    color: rgba(255, 255, 255, 0.8);
    font-size: 0.92rem;
}

.close-modal {
    position: absolute;
    top: 12px;
    right: 18px;
    font-size: 1.6rem;
    color: var(--text-dim);
    cursor: pointer;
}

.close-modal:hover {
    color: var(--text);
}

@media (max-width: 640px) {

    /* bottom sheet: lower third of the screen, canvas above */
    #ui-container {
        top: auto;
        bottom: 0;
        left: 0;
        right: 0;
        width: 100%;
        height: 33.5vh;
        max-height: none;
        border-radius: 16px 16px 0 0;
        border-bottom: none;
        padding-top: 0;
        gap: 12px;
        transition: transform 0.25s ease;
    }

    body.panel-collapsed #ui-container {
        transform: translateY(calc(100% - 40px));
    }

    /* pull handle: always visible, sticks to the top while scrolling */
    #panel-handle {
        display: flex;
        position: sticky;
        top: 0;
        z-index: 2;
        align-items: center;
        justify-content: center;
        height: 40px;
        flex-shrink: 0;
        margin: 0 -18px;
        background: rgb(28, 31, 42);
        border-radius: 16px 16px 0 0;
        cursor: pointer;
        touch-action: manipulation;
    }

    #panel-handle .grip {
        width: 46px;
        height: 5px;
        border-radius: 3px;
        background: rgba(255, 255, 255, 0.35);
    }

    body.panel-collapsed #panel-handle .grip {
        background: rgba(127, 224, 255, 0.6);
    }

    /* the pull handle supersedes the desktop minimize / reopen buttons */
    #minimize-btn,
    #ui-toggle {
        display: none;
    }

    /* quick-action toolbar, aligned to the right (Vibes stays on the left) */
    #mobile-toolbar {
        display: flex;
        position: fixed;
        top: 16px;
        right: 16px;
        gap: 8px;
        z-index: 20;
    }

    #mobile-toolbar .icon-btn {
        font-size: 1.05rem;
    }

    body.ui-collapsed #ui-container {
        display: flex;
    }
}
