* { margin: 0; padding: 0; box-sizing: border-box; }
body { background: #000; overflow: hidden; font-family: 'Segoe UI', system-ui, sans-serif; color: #fff; }
#canvas { display: block; }
#ui {
    position: absolute; top: 16px; left: 16px;
    background: rgba(8,8,28,0.88); backdrop-filter: blur(12px);
    padding: 20px; border-radius: 12px;
    border: 1px solid rgba(255,255,255,0.08);
    min-width: 270px; user-select: none;
    transition: min-width 0.25s ease, padding 0.25s ease;
    overflow: visible;
    display: flex; flex-direction: column;
    max-height: calc(100vh - 32px);
}
#sidebarContent {
    overflow-y: auto; flex: 1; min-height: 0;
}
#sidebarContent::-webkit-scrollbar { width: 10px; }
#sidebarContent::-webkit-scrollbar-track { background: transparent; margin: 2px 0; }
#sidebarContent::-webkit-scrollbar-thumb {
    background: rgba(255,255,255,0.15); border-radius: 5px;
    border-left: 5px solid transparent; border-right: 2px solid transparent; background-clip: padding-box;
}
#ui h2 { margin-bottom: 4px; font-size: 20px; font-weight: 600; }
#ui .sub { font-size: 13px; color: #889; margin-bottom: 0; }
#sidebarHeader {
    display: flex; align-items: flex-start; justify-content: space-between;
    gap: 8px; margin-bottom: 16px;
}
#sidebarToggle {
    background: rgba(255,255,255,0.06);
    border: 1px solid rgba(255,255,255,0.1); border-radius: 6px;
    color: #9ab; width: 28px; height: 28px;
    font-size: 16px; line-height: 1; cursor: pointer;
    display: flex; align-items: center; justify-content: center;
    flex-shrink: 0; padding: 0;
    transition: background 0.2s, color 0.2s;
}
#sidebarToggle:hover { background: rgba(255,255,255,0.12); color: #fff; }
#ui.collapsed { min-width: 0; padding: 12px; }
#ui.collapsed #sidebarHeader > div { display: none; }
#ui.collapsed #sidebarContent { display: none; }
#ui.collapsed #sidebarHeader { margin-bottom: 0; }
.section { margin-bottom: 6px; }
.section summary {
    font-size: 13px; font-weight: 600; color: #9ac;
    text-transform: uppercase; letter-spacing: 0.06em;
    cursor: pointer; padding: 6px 0; list-style: none;
    display: flex; align-items: center; gap: 6px;
    border-bottom: 1px solid rgba(255,255,255,0.06);
}
.section summary::-webkit-details-marker { display: none; }
.section summary::before {
    content: '\25B6'; font-size: 9px; color: #78a;
    transition: transform 0.2s ease;
    display: inline-block;
}
.section[open] > summary::before { transform: rotate(90deg); }
.section-body { padding: 8px 0 2px; }
.section[open] > summary { border-bottom-color: rgba(255,255,255,0.06); margin-bottom: 2px; }
.cg { margin-bottom: 10px; }
.cg label { display: block; font-size: 14px; color: #aab; margin-bottom: 3px; }
.cg .v { float: right; color: #6af; font-variant-numeric: tabular-nums; }
.tip {
    display: inline-flex; align-items: center; justify-content: center;
    width: 16px; height: 16px; font-size: 11px; line-height: 1;
    border-radius: 50%; background: rgba(255,255,255,0.18); color: #bcd;
    cursor: help; position: relative; vertical-align: text-top;
    margin-left: 3px; flex-shrink: 0; transform: translateY(1px);
}
.tip:hover { background: rgba(255,255,255,0.25); color: #def; }
.tip:hover::after {
    content: attr(data-tip);
    position: absolute; left: calc(100% + 8px); top: 50%; transform: translateY(-50%);
    background: rgba(8,8,28,0.96); backdrop-filter: blur(8px);
    border: 1px solid rgba(255,255,255,0.1); border-radius: 6px;
    padding: 6px 10px; font-size: 13px; color: #cde;
    width: 220px; white-space: normal; line-height: 1.4;
    z-index: 200; pointer-events: none;
    box-shadow: 0 4px 12px rgba(0,0,0,0.4);
}
input[type="range"] { width: 100%; accent-color: #48f; cursor: pointer; }
select {
    width: 100%; padding: 7px 10px; font-size: 14px; font-family: inherit;
    background: rgba(255,255,255,0.06); color: #cde;
    border: 1px solid rgba(255,255,255,0.1); border-radius: 6px;
    cursor: pointer; appearance: none;
    background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='10' height='6'%3E%3Cpath d='M0 0l5 6 5-6z' fill='%23667'/%3E%3C/svg%3E");
    background-repeat: no-repeat; background-position: right 10px center;
}
select:hover { border-color: rgba(255,255,255,0.18); }
select:focus { outline: none; border-color: #48f; }
select option { background: #12122a; color: #cde; }
button#generate {
    width: 100%; padding: 10px; margin-top: 10px; margin-bottom: 12px;
    background: linear-gradient(135deg, #2255cc, #3366ee);
    color: white; border: none; border-radius: 8px;
    font-size: 15px; font-weight: 500; cursor: pointer;
    transition: background 0.3s ease, opacity 0.3s ease;
}
button#generate:hover { filter: brightness(1.15); }
button#generate.stale {
    background: linear-gradient(135deg, #b87a1a, #d4922a);
}
button#generate.stale:hover { filter: brightness(1.15); }
button#generate.generating {
    background: linear-gradient(135deg, #333, #444);
    cursor: wait; opacity: 0.8;
}
#reapplyBtn {
    width: 100%; padding: 8px; margin-top: 4px;
    background: rgba(255,255,255,0.06); color: #556;
    border: 1px solid rgba(255,255,255,0.08); border-radius: 8px;
    font-size: 14px; font-weight: 500; font-family: inherit; cursor: default;
    display: flex; align-items: center; justify-content: center; gap: 4px;
    transition: background 0.2s, color 0.2s, border-color 0.2s;
}
#reapplyBtn:disabled { opacity: 0.4; }
#reapplyBtn.spinning .reapply-icon { display: inline-block; animation: spin 0.8s linear infinite; }
#reapplyBtn.ready {
    background: linear-gradient(135deg, #1a7a4a, #22aa5a);
    color: #fff; border-color: transparent;
    cursor: pointer; opacity: 1;
}
#reapplyBtn.ready:hover { filter: brightness(1.15); }
#seedRow {
    display: flex; gap: 4px; margin-bottom: 12px;
}
#seedCode {
    flex: 1; min-width: 0;
    background: rgba(255,255,255,0.06); color: #6af;
    border: 1px solid rgba(255,255,255,0.1); border-radius: 6px;
    padding: 5px 8px; font-size: 13px; font-family: 'Consolas', 'Courier New', monospace;
    letter-spacing: 0.05em; outline: none;
}
#seedCode:focus { border-color: #48f; }
#seedCode.flash { animation: seedFlash 0.6s ease; }
@keyframes seedFlash {
    0%   { background: rgba(68,136,255,0.25); border-color: #48f; }
    100% { background: rgba(255,255,255,0.06); border-color: rgba(255,255,255,0.1); }
}
#seedRow button {
    background: rgba(255,255,255,0.06); color: #9ab;
    border: 1px solid rgba(255,255,255,0.1); border-radius: 6px;
    padding: 4px 8px; font-size: 13px; cursor: pointer;
    transition: background 0.2s, color 0.2s, border-color 0.2s; white-space: nowrap;
}
#seedRow button:hover { background: rgba(255,255,255,0.12); color: #fff; }
#loadBtn.ready {
    background: linear-gradient(135deg, #2255cc, #3366ee);
    color: #fff; border-color: transparent;
}
#loadBtn.ready:hover { filter: brightness(1.15); }
#seedError {
    font-size: 11px; color: #c44; margin: -8px 0 8px 2px;
    display: none;
}
#seedError.visible { display: block; }
.tg { margin-top: 14px; display: flex; gap: 10px; flex-wrap: wrap; }
.tg label { font-size: 13px; color: #99b; display: flex; align-items: center; gap: 4px; cursor: pointer; }
#stats {
    font-size: 11px;
    color: #556;
    line-height: 1.6;
    margin-top: 8px;
    padding-top: 8px;
    border-top: 1px solid rgba(255,255,255,0.06);
}
#repoLink {
    display: flex; align-items: center; gap: 6px;
    font-size: 12px; color: #556;
    text-decoration: none;
    margin-top: 10px; padding-top: 10px;
    border-top: 1px solid rgba(255,255,255,0.06);
    transition: color 0.2s;
}
#repoLink:hover { color: #9ab; }
#repoLink svg { flex-shrink: 0; }
.slider-hint {
    display: flex;
    justify-content: space-between;
    font-size: 11px;
    color: #667;
    margin-top: 2px;
    margin-bottom: 4px;
    pointer-events: none;
}
/* Detail slider warning levels */
.cg.detail-orange input[type=range] { accent-color: #e8a020; }
.cg.detail-red input[type=range] { accent-color: #d04030; }
.detail-warn { font-size: 11px; margin-top: -2px; margin-bottom: 4px; }
.detail-warn:empty { display: none; }
.detail-warn.orange { color: #e8a020; }
.detail-warn.red { color: #d04030; }
#topInfo {
    position: absolute; top: 16px; left: 50%; transform: translateX(-50%);
    font-size: 13px; color: #889;
    background: rgba(8,8,28,0.85); backdrop-filter: blur(8px);
    padding: 6px 14px; border-radius: 8px;
    border: 1px solid rgba(255,255,255,0.08);
    white-space: nowrap; pointer-events: none;
}
#info {
    position: absolute; bottom: 16px; left: 50%; transform: translateX(-50%);
    font-size: 13px; color: #667; transition: color 0.4s ease;
    pointer-events: none;
}
#info.nudge { animation: infoNudge 2.5s ease; }
@keyframes infoNudge {
    0%   { color: #667; }
    15%  { color: #acd; }
    85%  { color: #acd; }
    100% { color: #667; }
}
#hoverInfo {
    position: absolute; bottom: 40px; left: 50%; transform: translateX(-50%);
    font-size: 14px; color: #cde; background: rgba(8,8,28,0.85);
    backdrop-filter: blur(8px); padding: 6px 14px; border-radius: 8px;
    border: 1px solid rgba(255,255,255,0.08); white-space: nowrap;
    pointer-events: none; display: none; line-height: 1.5;
}

/* Help button */
#helpBtn {
    position: absolute; top: 16px; right: 16px;
    width: 36px; height: 36px; border-radius: 50%;
    background: rgba(40,60,120,0.7); backdrop-filter: blur(12px);
    border: 1px solid rgba(100,160,255,0.25);
    color: #8bf; font-size: 18px; font-weight: 700;
    cursor: pointer; z-index: 50;
    display: flex; align-items: center; justify-content: center;
    transition: background 0.2s, color 0.2s, border-color 0.2s;
    padding: 0; line-height: 1;
}
#helpBtn:hover { background: rgba(60,90,180,0.8); border-color: rgba(100,160,255,0.5); color: #fff; }

/* Tutorial overlay */
#tutorialOverlay {
    position: fixed; inset: 0;
    background: rgba(0,0,0,0.6);
    z-index: 200;
    display: flex; align-items: center; justify-content: center;
    transition: opacity 0.5s ease;
}
#tutorialOverlay.hidden {
    opacity: 0; pointer-events: none;
}

/* Tutorial card */
#tutorialCard {
    position: relative;
    max-width: 420px; width: 90%;
    background: rgba(8,8,28,0.94); backdrop-filter: blur(16px);
    border: 1px solid rgba(255,255,255,0.08);
    border-radius: 12px;
    padding: 32px 28px 24px;
    color: #cde;
    transition: opacity 0.5s ease, transform 0.5s ease;
}
#tutorialOverlay.hidden #tutorialCard {
    opacity: 0; transform: translateY(-16px);
}

/* Close button */
#tutorialClose {
    position: absolute; top: 10px; right: 14px;
    background: none; border: none;
    color: #667; font-size: 22px; cursor: pointer;
    line-height: 1; padding: 4px;
    transition: color 0.2s;
}
#tutorialClose:hover { color: #fff; }

/* Steps */
.tutorial-step {
    display: none;
}
.tutorial-step.active {
    display: block;
}
.tutorial-step h3 {
    font-size: 18px; font-weight: 600; color: #fff;
    margin-bottom: 10px;
}
.tutorial-step p {
    font-size: 14px; line-height: 1.6; color: #9ab;
    margin-bottom: 8px;
}
.tutorial-step p:last-of-type {
    margin-bottom: 0;
}
.tutorial-step p strong {
    color: #cde;
}

/* Dots */
.tutorial-dots {
    display: flex; justify-content: center; gap: 8px;
    margin: 20px 0 16px;
}
.dot {
    width: 8px; height: 8px; border-radius: 50%;
    background: rgba(255,255,255,0.2);
    transition: background 0.2s;
}
.dot.active {
    background: #48f;
}

/* Nav buttons */
.tutorial-nav {
    display: flex; justify-content: space-between;
}
.btn-ghost {
    background: none; border: 1px solid rgba(255,255,255,0.12);
    color: #9ab; padding: 8px 20px; border-radius: 8px;
    font-size: 14px; font-family: inherit; cursor: pointer;
    transition: border-color 0.2s, color 0.2s;
}
.btn-ghost:hover:not(:disabled) { border-color: rgba(255,255,255,0.25); color: #cde; }
.btn-ghost:disabled { opacity: 0.35; cursor: default; }
.btn-primary {
    background: linear-gradient(135deg, #2255cc, #3366ee);
    border: none; color: #fff; padding: 8px 20px; border-radius: 8px;
    font-size: 14px; font-weight: 500; font-family: inherit; cursor: pointer;
    transition: filter 0.2s;
}
.btn-primary:hover { filter: brightness(1.15); }

/* Build overlay — unified loading / generation screen */
.build-overlay {
    position: fixed; inset: 0;
    background: rgba(3,3,8,0.75);
    z-index: 100;
    display: flex; align-items: center; justify-content: center;
    transition: opacity 0.5s ease;
}
.build-overlay.hidden {
    opacity: 0; pointer-events: none;
}
.build-overlay.initial {
    background: #030308;
    z-index: 1000;
}
.build-overlay-inner {
    display: flex; flex-direction: column; align-items: center;
}
.build-overlay-spinner {
    width: 40px; height: 40px;
    border: 3px solid rgba(255,255,255,0.1);
    border-top-color: #48f;
    border-radius: 50%;
    animation: spin 0.8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }
.build-overlay-title {
    margin-top: 16px; color: #667;
    font-size: 14px;
}

/* Progress bar */
.progress-bar-container {
    width: 240px; height: 4px;
    background: rgba(255,255,255,0.08);
    border-radius: 2px;
    overflow: hidden;
    margin-top: 20px;
}
.progress-bar-fill {
    height: 100%; width: 100%;
    background: #48f;
    border-radius: 2px;
    transform: scaleX(0);
    transform-origin: left;
    transition: transform 0.4s ease;
    will-change: transform;
}
.progress-label {
    font-size: 13px; color: #9ab;
    margin-top: 8px;
    text-align: center;
}

/* Export button */
#exportBtn {
    width: 100%; padding: 9px; margin-bottom: 12px;
    background: none;
    color: #9ab; border: 1px solid rgba(255,255,255,0.12); border-radius: 8px;
    font-size: 14px; font-weight: 500; font-family: inherit; cursor: pointer;
    transition: border-color 0.2s, color 0.2s, background 0.2s;
}
#exportBtn:hover { border-color: rgba(255,255,255,0.25); color: #cde; background: rgba(255,255,255,0.04); }

/* Export overlay */
#exportOverlay {
    position: fixed; inset: 0;
    background: rgba(0,0,0,0.6);
    z-index: 200;
    display: flex; align-items: center; justify-content: center;
    transition: opacity 0.3s ease;
}
#exportOverlay.hidden {
    opacity: 0; pointer-events: none;
}
#exportCard {
    position: relative;
    max-width: 380px; width: 90%;
    background: rgba(8,8,28,0.94); backdrop-filter: blur(16px);
    border: 1px solid rgba(255,255,255,0.08);
    border-radius: 12px;
    padding: 28px 24px 20px;
    color: #cde;
}
#exportCard h3 {
    font-size: 18px; font-weight: 600; color: #fff;
    margin-bottom: 16px;
}
#exportClose {
    position: absolute; top: 10px; right: 14px;
    background: none; border: none;
    color: #667; font-size: 22px; cursor: pointer;
    line-height: 1; padding: 4px;
    transition: color 0.2s;
}
#exportClose:hover { color: #fff; }
.export-actions {
    display: flex; justify-content: flex-end; gap: 10px;
    margin-top: 18px;
}

/* Survey overlay */
#surveyOverlay {
    position: fixed; inset: 0;
    background: rgba(0,0,0,0.6);
    z-index: 200;
    display: flex; align-items: center; justify-content: center;
    transition: opacity 0.5s ease;
}
#surveyOverlay.hidden {
    opacity: 0; pointer-events: none;
}
#surveyCard {
    position: relative;
    max-width: 380px; width: 90%;
    background: rgba(8,8,28,0.94); backdrop-filter: blur(16px);
    border: 1px solid rgba(255,255,255,0.08);
    border-radius: 12px;
    padding: 28px 24px 20px;
    color: #cde;
}
#surveyCard h3 {
    font-size: 18px; font-weight: 600; color: #fff;
    margin-bottom: 10px;
}
#surveyCard p {
    font-size: 14px; line-height: 1.6; color: #9ab;
    margin-bottom: 0;
}
#surveyClose {
    position: absolute; top: 10px; right: 14px;
    background: none; border: none;
    color: #667; font-size: 22px; cursor: pointer;
    line-height: 1; padding: 4px;
    transition: color 0.2s;
}
#surveyClose:hover { color: #fff; }
.survey-actions {
    display: flex; justify-content: flex-end; gap: 10px;
    margin-top: 18px;
}
.survey-actions .btn-primary {
    text-decoration: none;
    display: inline-flex; align-items: center;
}

/* Sheet handle — hidden on desktop */
.sheet-handle { display: none; }

/* Edit toggle — hidden on desktop */
.edit-toggle { display: none; }

/* Refresh FAB — hidden on desktop */
.refresh-fab { display: none; }

/* ───── Mobile: tablets + phones ───── */
@media (max-width: 768px) {
    #topInfo { display: none; }

    /* Bottom sheet */
    #ui {
        position: fixed; bottom: 0; left: 0; right: 0;
        top: auto;
        display: flex; flex-direction: column;
        max-height: 70vh;
        overflow: visible;
        border-radius: 16px 16px 0 0;
        padding: 10px 20px 20px;
        min-width: 0;
        z-index: 30;
        transition: transform 0.35s cubic-bezier(0.32, 0.72, 0, 1);
        will-change: transform;
    }
    #ui.collapsed {
        transform: translateY(calc(100% - 60px));
        padding: 10px 20px 20px;
    }
    /* Override desktop display:none — content stays rendered (for full-height drag) but invisible */
    #ui.collapsed #sidebarContent { display: block; visibility: hidden; }
    #ui.collapsed #sidebarHeader > div { display: block; visibility: hidden; }
    #ui.collapsed #sidebarHeader { margin-bottom: 0; }
    /* Reveal content as soon as a drag begins */
    #ui.collapsed.dragging #sidebarContent,
    #ui.collapsed.dragging #sidebarHeader > div { visibility: visible; }

    /* Mobile sidebarContent becomes the scroll container */
    #sidebarContent {
        overflow-y: auto;
        -webkit-overflow-scrolling: touch;
        overscroll-behavior: contain;
        flex: 1;
        min-height: 0;
        padding-right: 0; padding-left: 0;
    }

    /* Disable content scroll during handle drag */
    #ui.dragging #sidebarContent {
        overflow-y: hidden;
        pointer-events: none;
    }

    /* Show drag handle */
    .sheet-handle {
        display: flex;
        justify-content: center;
        padding: 12px 0 8px;
        cursor: grab;
        touch-action: none;
        flex-shrink: 0;
    }
    .sheet-handle span {
        display: block;
        width: 36px; height: 4px;
        background: rgba(255,255,255,0.25);
        border-radius: 2px;
    }

    /* Hide desktop sidebar toggle */
    #sidebarToggle { display: none; }

    /* Edit toggle button */
    .edit-toggle {
        display: flex;
        align-items: center; justify-content: center;
        position: fixed; bottom: 70px; right: 16px;
        width: 48px; height: 48px;
        border-radius: 50%;
        background: rgba(40,60,120,0.8);
        backdrop-filter: blur(12px);
        border: 1px solid rgba(100,160,255,0.25);
        color: #8bf;
        cursor: pointer;
        z-index: 25;
        transition: background 0.2s, box-shadow 0.2s, color 0.2s;
    }
    .edit-toggle.active {
        background: rgba(34,170,90,0.85);
        border-color: rgba(34,170,90,0.5);
        color: #fff;
        box-shadow: 0 0 12px rgba(34,170,90,0.4);
    }

    /* Refresh FAB — mirrors edit toggle on the left */
    .refresh-fab {
        display: flex;
        align-items: center; justify-content: center;
        position: fixed; bottom: 70px; left: 16px;
        width: 48px; height: 48px;
        border-radius: 50%;
        background: rgba(40,60,120,0.8);
        backdrop-filter: blur(12px);
        border: 1px solid rgba(100,160,255,0.25);
        color: #8bf;
        cursor: pointer;
        z-index: 25;
        transition: background 0.3s, box-shadow 0.3s, color 0.3s, border-color 0.3s;
    }
    .refresh-fab:hover { background: rgba(60,90,180,0.9); color: #fff; }
    .refresh-fab.armed {
        background: rgba(34,170,90,0.85);
        border-color: rgba(34,170,90,0.5);
        color: #fff;
        box-shadow: 0 0 12px rgba(34,170,90,0.4);
    }

    /* Touch targets */
    #helpBtn { width: 44px; height: 44px; font-size: 20px; }
    .tg label input[type="checkbox"] { width: 20px; height: 20px; }
    button#generate { padding: 14px; font-size: 16px; }
    select { padding: 10px 12px; }
    #seedRow button { padding: 8px 12px; }

    /* Font sizes */
    .cg label { font-size: 15px; }
    .section summary { font-size: 14px; }
    #stats { font-size: 12px; }
    .slider-hint { font-size: 12px; }
    #seedCode { font-size: 14px; }

    /* Tooltips: reposition above */
    .tip:hover::after {
        left: 50%; top: auto;
        bottom: calc(100% + 8px);
        transform: translateX(-50%);
    }

    /* Info bars: shift up above collapsed sheet */
    #info { bottom: 68px; }
    #hoverInfo { bottom: 92px; }

    /* Tutorial card — tighter padding */
    #tutorialCard { padding: 24px 20px 18px; }
}

/* ───── Mobile landscape ───── */
@media (max-width: 768px) and (orientation: landscape) {
    #ui { max-height: 50vh; }
}

/* ───── Small phones ───── */
@media (max-width: 480px) {
    #ui { padding: 8px 14px 14px; }
    #tutorialCard { padding: 20px 16px 14px; }
    button#generate { padding: 12px; font-size: 15px; }
    .cg label { font-size: 14px; }
    .section summary { font-size: 13px; }
}
