:root {
    --bg: #0a0a0a;
    --panel: #1a1a1a;
    --panel-2: #2a2a2a;
    --border: #333;
    --accent: #00ff88;
    --text: #ffffff;
    --muted: #999;
    --app-bar-h: 56px;
}

* { box-sizing: border-box; }

body {
    font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
    margin: 0;
    padding: 0;
    background: var(--bg);
    color: var(--text);
}

/* ---------------------------------------------------------------- App bar */
.app-bar {
    position: sticky;
    top: 0;
    z-index: 30;
    height: var(--app-bar-h);
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 0 14px;
    background: rgba(15, 15, 15, 0.92);
    backdrop-filter: blur(6px);
    border-bottom: 1px solid var(--border);
}

.app-title {
    margin: 0;
    font-size: 18px;
    color: var(--accent);
    font-weight: 700;
}

.gen-indicator {
    margin-left: auto;
    font-size: 13px;
    color: var(--muted);
}

.icon-btn {
    background: transparent;
    border: none;
    color: var(--text);
    font-size: 20px;
    line-height: 1;
    padding: 8px;
    border-radius: 6px;
    cursor: pointer;
}
.icon-btn:hover { background: rgba(255, 255, 255, 0.08); }

/* Inline Evolve button (wide pointer-fine only; hidden by default → FAB used) */
.evolve-bar { display: none; }

/* ---------------------------------------------------------------- Layout */
.container {
    max-width: 1200px;
    margin: 0 auto;
    padding: 16px;
}

.about-link {
    display: inline-block;
    margin-bottom: 12px;
    color: var(--muted);
    font-size: 13px;
    text-decoration: none;
}
.about-link:hover { color: var(--accent); text-decoration: underline; }

.grid {
    display: grid;
    grid-template-columns: repeat(2, 1fr);
    gap: 12px;
}

/* ---------------------------------------------------------------- Cells */
.individual {
    aspect-ratio: 1;
    border: 2px solid var(--border);
    border-radius: 8px;
    cursor: pointer;
    transition: border-color 0.2s ease, box-shadow 0.2s ease, transform 0.15s ease;
    position: relative;
    overflow: hidden;
    background: var(--panel);
    -webkit-tap-highlight-color: transparent;
    user-select: none;
}

.individual:hover { border-color: #666; }

.individual.selected {
    border-color: var(--accent);
    box-shadow: 0 0 18px rgba(0, 255, 136, 0.35);
}

/* A liked cell shows a filled heart in the corner. */
.individual.selected::after {
    /* U+FE0E forces text presentation so mobile doesn't substitute the red
       heart emoji (which ignores CSS color). */
    content: "\2665\FE0E";
    position: absolute;
    top: 6px;
    left: 8px;
    color: var(--accent);
    font-size: 16px;
    text-shadow: 0 0 4px rgba(0, 0, 0, 0.8);
    pointer-events: none;
}

.individual canvas {
    width: 100%;
    height: 100%;
    display: block;
}

/* Audio cells: a play triangle to audition without liking. */
.play-btn {
    position: absolute;
    bottom: 6px;
    right: 6px;
    width: 34px;
    height: 34px;
    border: none;
    border-radius: 50%;
    background: rgba(0, 0, 0, 0.55);
    color: #fff;
    font-size: 14px;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: background 0.2s, transform 0.15s;
}
.play-btn:hover { background: rgba(0, 255, 136, 0.7); transform: scale(1.08); }
.play-btn.playing { background: rgba(0, 255, 136, 0.85); color: #000; }

/* Zoom affordance: hidden by default, revealed on hover (pointer-fine only). */
.zoom-btn {
    position: absolute;
    top: 6px;
    right: 6px;
    width: 30px;
    height: 30px;
    border: none;
    border-radius: 6px;
    background: rgba(0, 0, 0, 0.55);
    color: #fff;
    font-size: 15px;
    cursor: pointer;
    display: none;
    align-items: center;
    justify-content: center;
}
.zoom-btn:hover { background: rgba(255, 255, 255, 0.25); }

/* ---------------------------------------------------------------- FAB */
.fab {
    position: fixed;
    right: 18px;
    bottom: 18px;
    z-index: 40;
    width: 60px;
    height: 60px;
    border-radius: 50%;
    border: none;
    background: var(--accent);
    color: #000;
    cursor: pointer;
    box-shadow: 0 4px 16px rgba(0, 0, 0, 0.5);
    display: flex;
    align-items: center;
    justify-content: center;
}
.fab:hover { background: #00cc6a; }
.fab:active { transform: scale(0.95); }
.fab-icon { font-size: 26px; line-height: 1; }

/* Selected-count badge (on both FAB and inline button). */
.sel-badge {
    position: absolute;
    top: -4px;
    right: -4px;
    min-width: 20px;
    height: 20px;
    padding: 0 5px;
    border-radius: 10px;
    background: #ff3b6b;
    color: #fff;
    font-size: 12px;
    font-weight: 700;
    display: flex;
    align-items: center;
    justify-content: center;
}
.sel-badge.empty { display: none; }

/* ---------------------------------------------------------------- Drawer */
.scrim {
    position: fixed;
    inset: 0;
    z-index: 45;
    background: rgba(0, 0, 0, 0.5);
    opacity: 0;
    pointer-events: none;
    transition: opacity 0.25s ease;
}
.scrim.open { opacity: 1; pointer-events: auto; }

.drawer {
    position: fixed;
    top: 0;
    left: 0;
    z-index: 50;
    width: min(360px, 86vw);
    height: 100%;
    background: var(--panel);
    border-right: 1px solid var(--border);
    transform: translateX(-100%);
    transition: transform 0.25s ease;
    overflow-y: auto;
    padding: 0 16px 24px;
}
.drawer.open { transform: translateX(0); }

.drawer-header {
    position: sticky;
    top: 0;
    background: var(--panel);
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 14px 0;
    border-bottom: 1px solid var(--border);
    margin-bottom: 8px;
}
.drawer-header h2 { margin: 0; font-size: 18px; color: var(--accent); }

.drawer-section { padding: 14px 0; border-bottom: 1px solid var(--border); }
.drawer-section h3 { margin: 0 0 10px; font-size: 13px; text-transform: uppercase; letter-spacing: 0.05em; color: var(--muted); }

.info-line { margin-top: 10px; font-size: 13px; color: var(--muted); }

.drawer-footer { padding-top: 16px; text-align: center; }
.drawer-footer a { color: var(--accent); text-decoration: none; }
.drawer-footer a:hover { text-decoration: underline; }

select, .secondary-btn {
    font-size: 14px;
    border-radius: 6px;
}
select {
    width: 100%;
    padding: 8px 10px;
    margin-bottom: 8px;
    border: 1px solid var(--border);
    background: var(--panel-2);
    color: var(--text);
}
select:focus { outline: none; border-color: var(--accent); }

.secondary-btn {
    padding: 8px 14px;
    border: 1px solid var(--border);
    background: var(--panel-2);
    color: var(--text);
    cursor: pointer;
}
.secondary-btn:hover { background: #353535; border-color: #555; }

#eeg-status { display: inline-block; margin-left: 8px; font-size: 12px; color: var(--muted); }

/* History */
.history-list { display: flex; flex-wrap: wrap; gap: 6px; }
.history-item {
    padding: 5px 10px;
    background: var(--panel-2);
    border-radius: 4px;
    cursor: pointer;
    font-size: 13px;
    transition: background 0.2s;
}
.history-item:hover { background: #444; }
.history-item.current { background: var(--accent); color: #000; }

/* Genome / current individual */
.genome-content {
    font-family: 'Courier New', monospace;
    font-size: 12px;
    color: #aaa;
    max-height: 260px;
    overflow-y: auto;
    padding: 10px;
    background: var(--bg);
    border-radius: 4px;
    white-space: pre-wrap;
    word-break: break-word;
    line-height: 1.6;
}
.genome-content em { color: #666; font-style: italic; }
.genome-label { color: var(--accent); font-weight: bold; margin-bottom: 5px; display: block; }
.edit-hint { color: var(--accent); margin-top: 8px; font-style: italic; white-space: normal; }

/* Palette extension (now lives in the drawer; neutral dark styling). */
.extensions-container { font-size: 13px; }
.extension-section h3 { color: var(--muted); font-size: 13px; margin: 0 0 8px; }
.palette-selector {
    padding: 6px 8px;
    border: 1px solid var(--border);
    border-radius: 4px;
    background: var(--panel-2);
    color: var(--text);
    width: 100%;
}
.palette-preview { display: inline-flex; margin-top: 10px; border: 1px solid var(--border); border-radius: 4px; overflow: hidden; }
.color-swatch { width: 20px; height: 20px; cursor: pointer; }
.color-swatch:hover { transform: scale(1.1); }
.hotkey-hint { font-size: 12px; color: var(--muted); margin-top: 8px; }

/* Drum "Performance" panel: one row per global dial —
   [checkbox] [label] [slider] [readout]. */
.drum-dial { display: grid; grid-template-columns: auto auto 1fr 56px; align-items: center; gap: 8px; margin-top: 10px; }
.drum-dial input[type="checkbox"] { margin: 0; accent-color: var(--accent); }
.drum-dial-label { font-size: 13px; color: var(--text); cursor: pointer; white-space: nowrap; }
.drum-dial-slider { width: 100%; accent-color: var(--accent); }
.drum-dial-slider:disabled { opacity: 0.4; }
.drum-dial-readout { font-size: 12px; color: var(--muted); text-align: right; font-variant-numeric: tabular-nums; }

/* Code-editor extension (UserGeneratorDrawingIndividual). The sidebar holds just
   a button + status; editing happens in the full-screen .code-modal below. */
.code-modal {
    position: fixed;
    inset: 0;
    z-index: 70;
    background: rgba(0, 0, 0, 0.85);
    display: none;
    align-items: center;
    justify-content: center;
    padding: 20px;
}
.code-modal.open { display: flex; }
.code-modal-inner {
    position: relative;
    display: flex;
    flex-direction: column;
    width: 100%;
    max-width: 900px;
    max-height: 100%;
    background: var(--panel);
    border-radius: 12px;
    padding: 16px;
    box-sizing: border-box;
}
.code-modal-header { display: flex; align-items: center; gap: 12px; margin-bottom: 10px; }
.code-modal-header h3 { margin: 0; color: var(--muted); font-size: 14px; }
.code-modal-section {
    padding: 4px 8px;
    border: 1px solid var(--border);
    border-radius: 4px;
    background: var(--panel-2);
    color: var(--text);
}
.code-modal-close { position: absolute; top: 10px; right: 10px; }
.code-modal-textarea {
    flex: 1 1 auto;
    width: 100%;
    min-height: 60vh;
    padding: 12px;
    border: 1px solid var(--border);
    border-radius: 6px;
    background: var(--panel-2);
    color: var(--text);
    font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
    font-size: 13px;
    line-height: 1.5;
    tab-size: 4;
    white-space: pre;
    resize: none;
    box-sizing: border-box;
}
.code-modal-footer { display: flex; align-items: center; gap: 12px; margin-top: 12px; }
.code-modal-footer .code-editor-status { margin-top: 0; }
.code-editor-apply {
    margin-top: 8px;
    padding: 6px 12px;
    border: 1px solid var(--border);
    border-radius: 4px;
    background: var(--panel-2);
    color: var(--text);
    cursor: pointer;
}
.code-editor-apply:hover { background: var(--panel); }
.code-editor-reset {
    padding: 6px 12px;
    border: 1px solid var(--border);
    border-radius: 4px;
    background: transparent;
    color: var(--muted);
    cursor: pointer;
}
.code-editor-reset:hover { background: var(--panel-2); color: var(--text); }
.code-editor-status { font-size: 12px; color: var(--muted); margin-top: 8px; white-space: pre-wrap; }
.code-editor-status.error { color: #ff6b6b; }

/* ---------------------------------------------------------------- Lightbox */
.lightbox {
    position: fixed;
    inset: 0;
    z-index: 60;
    background: rgba(0, 0, 0, 0.85);
    display: none;
    align-items: center;
    justify-content: center;
    padding: 20px;
}
.lightbox.open { display: flex; }

.lightbox-inner {
    position: relative;
    max-width: 960px;
    width: 100%;
    max-height: 100%;
    overflow-y: auto;
    background: var(--panel);
    border-radius: 12px;
    padding: 16px;
}
.lightbox-close { position: absolute; top: 8px; right: 8px; z-index: 1; }
.lightbox-save {
    position: absolute;
    top: 8px;
    left: 8px;
    z-index: 1;
    background: var(--accent);
    color: #000;
    border: none;
    border-radius: 6px;
    padding: 8px 14px;
    font-weight: 700;
    font-size: 14px;
    cursor: pointer;
}
.lightbox-save:hover { background: #00cc6a; }
.lightbox-save:active { transform: translateY(1px); }
/* Second action button, offset to sit just right of Save (same top row). Only
   one of STL / MIDI is ever shown (3D vs. audio/melody), so they share the slot;
   for audio, WAV takes Save's slot at left:8px and MIDI sits here beside it. */
.lightbox-export-stl,
.lightbox-export-midi { left: 104px; }

/* About page reuses the lightbox chrome (overlay + close button). */
.about-inner { max-width: 640px; padding-top: 48px; }
.about-content { white-space: pre-wrap; line-height: 1.5; }

/* Placement banner: pick a grid tile to replace with a loaded individual. */
.place-banner {
    position: fixed;
    left: 50%;
    top: 16px;
    transform: translateX(-50%);
    display: none;
    align-items: center;
    gap: 12px;
    background: var(--panel);
    border: 1px solid var(--accent);
    border-radius: 10px;
    padding: 10px 14px;
    z-index: 900;
    max-width: calc(100% - 24px);
    box-shadow: 0 6px 20px rgba(0, 0, 0, 0.4);
}
.place-banner.open { display: flex; }
.place-banner #place-preview {
    width: 48px;
    height: 48px;
    border-radius: 6px;
    background: var(--bg);
    flex: none;
}
.place-banner .place-text { font-size: 14px; line-height: 1.3; }
/* While placing, make grid tiles read as click targets to replace. */
.grid.placing .individual { cursor: copy; }
.grid.placing .individual:hover { outline: 2px solid var(--accent); outline-offset: 2px; }

/* Transient confirmation toast (image saved, etc.) */
.toast {
    position: fixed;
    left: 50%;
    bottom: 32px;
    transform: translateX(-50%) translateY(12px);
    background: rgba(20, 20, 20, 0.95);
    color: var(--text);
    border: 1px solid rgba(255, 255, 255, 0.12);
    border-radius: 8px;
    padding: 10px 18px;
    font-size: 14px;
    z-index: 1000;
    opacity: 0;
    pointer-events: none;
    transition: opacity 0.2s ease, transform 0.2s ease;
}
.toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }
#lightbox-canvas {
    width: min(100%, 80vh);
    height: auto;
    aspect-ratio: 1;
    display: block;
    margin: 0 auto 14px;
    border-radius: 8px;
    background: var(--bg);
}

/* ---------------------------------------------------------------- Responsive */
@media (min-width: 600px) {
    .grid { grid-template-columns: repeat(3, 1fr); gap: 14px; }
}

@media (min-width: 900px) {
    .grid { grid-template-columns: repeat(4, 1fr); gap: 15px; }
    .app-title { font-size: 20px; }
}

/* Pointer-fine (mouse/trackpad): Evolve goes inline in the app bar, the FAB
   is hidden, and the zoom affordance is revealed on hover. */
@media (hover: hover) and (pointer: fine) {
    .fab { display: none; }
    .evolve-bar {
        position: absolute;
        left: 50%;
        transform: translateX(-50%);
        display: inline-flex;
        align-items: center;
        padding: 8px 18px;
        background: var(--accent);
        color: #000;
        border: none;
        border-radius: 6px;
        font-weight: 700;
        cursor: pointer;
    }
    .evolve-bar:hover { background: #00cc6a; }
    .individual:hover .zoom-btn { display: flex; }
    .individual:hover { transform: scale(1.02); }
}
