/* ==========================================================================
   Lifeform - the simulator shell
   ========================================================================== */

.lab {
    display: grid;
    grid-template-columns: minmax(0, 1fr) 340px;
    height: calc(100dvh - var(--nav-h));
    position: relative;
    z-index: 1;
    background: var(--canvas-bg);
}

/* ------------------------------------------------------------------ stage */
.lab-stage {
    position: relative;
    overflow: hidden;
    background: var(--canvas-bg);
    cursor: crosshair;
}
.lab-stage[data-mode="pan"] { cursor: grab; }
.lab-stage[data-mode="pan"]:active { cursor: grabbing; }
.lab-stage[data-mode="stamp"] { cursor: copy; }

/* The Lab is a fixed application surface with nothing to scroll, so it takes the
   whole gesture: one finger draws, two fingers pinch and pan. */
.lab-canvas { position: absolute; inset: 0; width: 100%; height: 100%; touch-action: none; }

/* ------------------------------------------------------------------ hud */
.hud {
    position: absolute;
    top: 14px;
    left: 14px;
    z-index: 5;
    display: flex;
    flex-direction: column;
    gap: 8px;
    pointer-events: none;
}

.hud-panel {
    pointer-events: auto;
    background: color-mix(in srgb, var(--panel) 84%, transparent);
    backdrop-filter: blur(12px) saturate(160%);
    -webkit-backdrop-filter: blur(12px) saturate(160%);
    border: 1px solid var(--line);
    border-radius: 11px;
    padding: .7rem .85rem;
    box-shadow: var(--shadow-2);
}

.hud-stats {
    display: grid;
    grid-template-columns: repeat(2, minmax(78px, auto));
    gap: .55rem 1.4rem;
}
.hud-stat-label {
    font-family: var(--font-mono);
    font-size: .62rem;
    letter-spacing: .13em;
    text-transform: uppercase;
    color: var(--ink-4);
}
.hud-stat-value {
    font-family: var(--font-mono);
    font-size: 1.02rem;
    font-weight: 500;
    color: var(--ink);
    font-variant-numeric: tabular-nums;
    line-height: 1.25;
}
.hud-stat-value small { font-size: .68em; color: var(--ink-3); margin-left: .2em; }

.hud-spark { margin-top: .55rem; }
.hud-spark canvas { width: 100%; height: 34px; display: block; }
.hud-spark-label {
    display: flex;
    justify-content: space-between;
    font-family: var(--font-mono);
    font-size: .6rem;
    letter-spacing: .1em;
    text-transform: uppercase;
    color: var(--ink-4);
    margin-bottom: .25rem;
}

.hud-flag {
    display: inline-flex;
    align-items: center;
    gap: .45rem;
    padding: .3rem .6rem;
    border-radius: 8px;
    background: color-mix(in srgb, var(--panel) 84%, transparent);
    backdrop-filter: blur(12px);
    border: 1px solid var(--line);
    font-family: var(--font-mono);
    font-size: .7rem;
    color: var(--amber);
    pointer-events: auto;
}
.hud-flag[hidden] { display: none; }
.hud-flag i {
    width: 6px; height: 6px;
    border-radius: 1px;
    background: currentColor;
}

/* ------------------------------------------------------------------ toolbar */
.toolbar {
    position: absolute;
    left: 50%;
    bottom: 18px;
    z-index: 6;
    translate: -50% 0;
    display: flex;
    align-items: center;
    gap: .3rem;
    padding: .4rem;
    background: color-mix(in srgb, var(--panel) 88%, transparent);
    backdrop-filter: blur(14px) saturate(160%);
    -webkit-backdrop-filter: blur(14px) saturate(160%);
    border: 1px solid var(--line);
    border-radius: 14px;
    box-shadow: var(--shadow-3);
    /* max-content stops the shrink-to-fit width being clipped by left:50% */
    width: max-content;
    max-width: calc(100% - 28px);
    flex-wrap: wrap;
    justify-content: center;
}

.tool {
    display: inline-grid;
    place-items: center;
    width: 38px;
    height: 38px;
    border: 0;
    border-radius: 10px;
    background: transparent;
    color: var(--ink-2);
    cursor: pointer;
    position: relative;
    transition: background .14s var(--ease), color .14s var(--ease);
}
.tool:hover { background: var(--raised); color: var(--ink); }
.tool.is-active { background: var(--accent); color: var(--accent-ink); }
.tool svg { width: 18px; height: 18px; }
.tool--wide {
    width: auto;
    padding-inline: .7rem .85rem;
    gap: .42rem;
    grid-auto-flow: column;
    font-size: .84rem;
    font-weight: 550;
    font-family: inherit;
    white-space: nowrap;
}

.tool[data-tip]::after {
    content: attr(data-tip);
    position: absolute;
    bottom: calc(100% + 9px);
    left: 50%;
    translate: -50% 0;
    padding: .3rem .55rem;
    border-radius: 7px;
    background: var(--overlay);
    border: 1px solid var(--line);
    color: var(--ink);
    font-family: var(--font-body);
    font-size: .74rem;
    white-space: nowrap;
    opacity: 0;
    pointer-events: none;
    transition: opacity .14s var(--ease);
    box-shadow: var(--shadow-2);
    z-index: 10;
}
.tool[data-tip]:hover::after { opacity: 1; }

.tool-sep { width: 1px; height: 24px; background: var(--line); margin-inline: .25rem; }

.tool-speed {
    display: flex;
    align-items: center;
    gap: .6rem;
    padding-inline: .7rem .5rem;
    min-width: 168px;
}
.tool-speed input[type="range"] { flex: 1; }
.tool-speed output {
    font-family: var(--font-mono);
    font-size: .72rem;
    color: var(--ink-3);
    min-width: 52px;
    text-align: right;
    font-variant-numeric: tabular-nums;
}

/* ------------------------------------------------------------------ panel */
.lab-panel {
    display: flex;
    flex-direction: column;
    min-height: 0;
    border-left: 1px solid var(--line);
    background: var(--surface);
}

.lab-tabs {
    display: grid;
    grid-auto-flow: column;
    grid-auto-columns: 1fr;
    border-bottom: 1px solid var(--line);
    background: var(--panel);
    flex: none;
}
.lab-tab {
    padding: .8rem .3rem;
    border: 0;
    border-bottom: 2px solid transparent;
    background: transparent;
    color: var(--ink-3);
    font-family: var(--font-mono);
    font-size: .68rem;
    letter-spacing: .12em;
    text-transform: uppercase;
    cursor: pointer;
    transition: color .16s var(--ease), border-color .16s var(--ease), background .16s var(--ease);
}
.lab-tab:hover { color: var(--ink-2); background: var(--raised); }
.lab-tab[aria-selected="true"] { color: var(--accent); border-bottom-color: var(--accent); background: var(--surface); }

.lab-pane {
    flex: 1;
    min-height: 0;
    overflow-y: auto;
    padding: 1.05rem;
    scrollbar-width: thin;
}
.lab-pane[hidden] { display: none; }
.lab-pane::-webkit-scrollbar { width: 9px; }
.lab-pane::-webkit-scrollbar-thumb { background: var(--line-strong); border-radius: 9px; border: 3px solid var(--surface); }

.panel-group + .panel-group { margin-top: 1.5rem; padding-top: 1.5rem; border-top: 1px solid var(--line-soft); }
.panel-title {
    font-family: var(--font-mono);
    font-size: .66rem;
    letter-spacing: .14em;
    text-transform: uppercase;
    color: var(--ink-4);
    margin-bottom: .75rem;
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: .5rem;
}

/* pattern browser */
.pattern-search { margin-bottom: .8rem; }

.pat-cats {
    display: flex;
    flex-wrap: wrap;
    gap: .3rem;
    margin-bottom: .85rem;
}
.pat-cat {
    padding: .22rem .55rem;
    border: 1px solid var(--line);
    border-radius: 999px;
    background: transparent;
    color: var(--ink-3);
    font-family: var(--font-mono);
    font-size: .66rem;
    letter-spacing: .05em;
    text-transform: uppercase;
    cursor: pointer;
    transition: all .14s var(--ease);
}
.pat-cat:hover { color: var(--ink); border-color: var(--line-strong); }
.pat-cat.is-active { background: var(--accent); border-color: transparent; color: var(--accent-ink); }

.pat-list { display: flex; flex-direction: column; gap: .3rem; }

.pat-item {
    display: grid;
    grid-template-columns: 46px 1fr;
    gap: .7rem;
    align-items: center;
    padding: .45rem;
    border: 1px solid transparent;
    border-radius: 10px;
    background: transparent;
    text-align: left;
    cursor: pointer;
    color: inherit;
    font-family: inherit;
    transition: background .14s var(--ease), border-color .14s var(--ease);
}
.pat-item:hover { background: var(--panel); border-color: var(--line); }
.pat-item.is-selected { background: var(--panel); border-color: var(--accent); }
.pat-thumb {
    width: 46px; height: 34px;
    border-radius: 5px;
    background: var(--canvas-bg);
    border: 1px solid var(--line-soft);
    overflow: hidden;
}
.pat-thumb canvas { width: 100%; height: 100%; }
.pat-name { font-size: .875rem; color: var(--ink); line-height: 1.25; }
.pat-meta {
    font-family: var(--font-mono);
    font-size: .655rem;
    color: var(--ink-4);
    letter-spacing: .04em;
    margin-top: .12rem;
}

/* rule editor */
.rule-bits { display: grid; grid-template-columns: repeat(9, 1fr); gap: .22rem; }
.rule-bit {
    aspect-ratio: 1;
    border: 1px solid var(--line);
    border-radius: 6px;
    background: var(--panel);
    color: var(--ink-3);
    font-family: var(--font-mono);
    font-size: .74rem;
    cursor: pointer;
    transition: all .14s var(--ease);
}
.rule-bit:hover { border-color: var(--line-strong); color: var(--ink); }
.rule-bit.is-on { background: var(--accent); border-color: transparent; color: var(--accent-ink); font-weight: 600; }
.rule-bits-label {
    font-family: var(--font-mono);
    font-size: .66rem;
    letter-spacing: .1em;
    text-transform: uppercase;
    color: var(--ink-4);
    margin: .8rem 0 .4rem;
}

.rule-string {
    display: flex;
    gap: .4rem;
    align-items: center;
    margin-top: .9rem;
}
.rule-string input {
    font-family: var(--font-mono);
    letter-spacing: .04em;
    text-transform: uppercase;
}

.preset-list { display: flex; flex-direction: column; gap: .28rem; }
.preset {
    display: flex;
    align-items: baseline;
    justify-content: space-between;
    gap: .6rem;
    width: 100%;
    padding: .5rem .6rem;
    border: 1px solid transparent;
    border-radius: 9px;
    background: transparent;
    color: var(--ink-2);
    font-family: inherit;
    font-size: .855rem;
    text-align: left;
    cursor: pointer;
    transition: all .14s var(--ease);
}
.preset:hover { background: var(--panel); border-color: var(--line); color: var(--ink); }
.preset.is-active { background: var(--panel); border-color: var(--accent); color: var(--ink); }
.preset code { font-size: .68rem; background: none; border: 0; padding: 0; color: var(--ink-4); flex: none; }
.preset span { display: block; font-size: .74rem; color: var(--ink-4); margin-top: .1rem; line-height: 1.4; }

/* settings rows */
.setting {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 1rem;
    padding: .5rem 0;
    font-size: .875rem;
    color: var(--ink-2);
}
.setting + .setting { border-top: 1px solid var(--line-soft); }
.setting-label b { display: block; color: var(--ink); font-weight: 550; font-size: .875rem; }
.setting-label span { display: block; font-size: .74rem; color: var(--ink-4); line-height: 1.4; }

.switch {
    position: relative;
    flex: none;
    width: 38px;
    height: 21px;
    border-radius: 999px;
    border: 1px solid var(--line-strong);
    background: var(--panel);
    cursor: pointer;
    transition: background .18s var(--ease), border-color .18s var(--ease);
}
.switch::after {
    content: "";
    position: absolute;
    top: 2px; left: 2px;
    width: 15px; height: 15px;
    border-radius: 50%;
    background: var(--ink-3);
    transition: translate .18s var(--ease), background .18s var(--ease);
}
.switch[aria-checked="true"] { background: var(--accent); border-color: transparent; }
.switch[aria-checked="true"]::after { translate: 17px 0; background: var(--accent-ink); }

.seg {
    display: inline-flex;
    padding: 2px;
    border: 1px solid var(--line);
    border-radius: 9px;
    background: var(--panel);
    flex: none;
}
.seg button {
    padding: .26rem .6rem;
    border: 0;
    border-radius: 7px;
    background: transparent;
    color: var(--ink-3);
    font-family: var(--font-mono);
    font-size: .7rem;
    cursor: pointer;
    transition: all .14s var(--ease);
}
.seg button:hover { color: var(--ink); }
.seg button.is-active { background: var(--accent); color: var(--accent-ink); }

/* code / rle box */
.code-box {
    width: 100%;
    min-height: 120px;
    font-family: var(--font-mono);
    font-size: .74rem;
    line-height: 1.5;
    letter-spacing: .01em;
    word-break: break-all;
    background: var(--canvas-bg);
}

/* ------------------------------------------------------------------ modal */
.modal-backdrop {
    position: fixed;
    inset: 0;
    z-index: 300;
    display: grid;
    place-items: center;
    padding: 1.5rem;
    background: color-mix(in srgb, var(--void) 78%, transparent);
    backdrop-filter: blur(6px);
    animation: fade .18s var(--ease);
}
.modal-backdrop[hidden] { display: none; }
@keyframes fade { from { opacity: 0; } }

.modal {
    width: 100%;
    max-width: 560px;
    max-height: 85vh;
    overflow-y: auto;
    background: var(--panel);
    border: 1px solid var(--line);
    border-radius: var(--radius);
    box-shadow: var(--shadow-3);
    padding: 1.5rem;
    animation: rise .22s var(--ease);
}
@keyframes rise { from { opacity: 0; translate: 0 10px; } }
.modal h2 { font-size: 1.3rem; margin-bottom: .3rem; }
.modal-head { display: flex; align-items: flex-start; justify-content: space-between; gap: 1rem; margin-bottom: 1.2rem; }
.modal-foot { display: flex; justify-content: flex-end; gap: .6rem; margin-top: 1.4rem; }

.keys { display: grid; grid-template-columns: repeat(auto-fit, minmax(210px, 1fr)); gap: .45rem 1.6rem; }
.keys div { display: flex; align-items: center; justify-content: space-between; gap: 1rem; padding: .28rem 0; border-bottom: 1px solid var(--line-soft); font-size: .875rem; color: var(--ink-2); }
.keys div span { display: flex; gap: .25rem; }

/* ------------------------------------------------------------------ toast */
.toast-stack {
    position: fixed;
    bottom: 20px;
    right: 20px;
    z-index: 400;
    display: flex;
    flex-direction: column;
    gap: .5rem;
    pointer-events: none;
}
.toast {
    display: flex;
    align-items: center;
    gap: .6rem;
    padding: .65rem .9rem;
    border: 1px solid var(--line);
    border-left: 2px solid var(--accent);
    border-radius: 10px;
    background: var(--overlay);
    color: var(--ink);
    font-size: .875rem;
    box-shadow: var(--shadow-3);
    animation: toast-in .25s var(--ease);
    pointer-events: auto;
    max-width: 340px;
}
.toast--error { border-left-color: var(--rose); }
@keyframes toast-in { from { opacity: 0; translate: 12px 0; } }

/* ------------------------------------------------------------------ labs (other automata) */
.sim-shell {
    display: grid;
    grid-template-columns: minmax(0, 1fr) 320px;
    gap: 1.4rem;
    align-items: start;
}
/* A grid item's automatic minimum size is its content's min-content width, and
   a canvas contributes its intrinsic (attribute) width. Without this the stage
   column refuses to shrink and the page scrolls sideways on a phone. */
.sim-shell > * { min-width: 0; }
.sim-stage {
    position: relative;
    border: 1px solid var(--line);
    border-radius: var(--radius);
    background: var(--canvas-bg);
    overflow: hidden;
}
/* These sit inside a long scrolling article, so the browser keeps vertical
   panning AND pinch-zoom - otherwise a swipe starting on the canvas traps the
   reader on the page and a pinch scribbles on the grid instead of zooming.
   Taps and horizontal drags are left to the drawing handlers. */
.sim-stage canvas { width: 100%; display: block; touch-action: pan-y pinch-zoom; }
.sim-bar {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    gap: .4rem;
    padding: .5rem;
    border-top: 1px solid var(--line);
    background: var(--panel);
}
.sim-panel {
    border: 1px solid var(--line);
    border-radius: var(--radius);
    background: var(--panel);
    padding: 1.1rem;
    position: sticky;
    top: calc(var(--nav-h) + 20px);
    max-height: calc(100dvh - var(--nav-h) - 40px);
    overflow-y: auto;
}
/* Dead-end state for the simulators that can reach extinction. */
.sim-empty {
    position: absolute;
    left: 50%;
    top: 46%;
    translate: -50% -50%;
    z-index: 7;
    width: min(430px, calc(100% - 32px));
    text-align: center;
    padding: 1.3rem 1.4rem;
    background: color-mix(in srgb, var(--panel) 94%, transparent);
    backdrop-filter: blur(12px) saturate(160%);
    -webkit-backdrop-filter: blur(12px) saturate(160%);
    border: 1px solid var(--line);
    border-radius: var(--radius);
    box-shadow: var(--shadow-3);
    animation: rise .22s var(--ease);
}
.sim-empty h3 { font-size: 1.05rem; margin-bottom: .4rem; }
.sim-empty p { font-size: .88rem; color: var(--ink-2); margin: 0 0 1rem; line-height: 1.5; }
.sim-empty .btn-row { justify-content: center; gap: .5rem; }

.sim-readout {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(84px, 1fr));
    gap: .7rem;
    font-family: var(--font-mono);
}
.sim-readout div span {
    display: block;
    font-size: .62rem;
    letter-spacing: .12em;
    text-transform: uppercase;
    color: var(--ink-4);
}
.sim-readout div b {
    font-size: 1.02rem;
    font-weight: 500;
    color: var(--ink);
    font-variant-numeric: tabular-nums;
}

.rule-chip-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(58px, 1fr)); gap: .3rem; }
.rule-chip {
    padding: .35rem 0;
    border: 1px solid var(--line);
    border-radius: 7px;
    background: var(--panel);
    color: var(--ink-3);
    font-family: var(--font-mono);
    font-size: .72rem;
    cursor: pointer;
    transition: all .14s var(--ease);
}
.rule-chip:hover { border-color: var(--line-strong); color: var(--ink); }
.rule-chip.is-active { background: var(--accent); border-color: transparent; color: var(--accent-ink); font-weight: 600; }

.state-key { display: flex; flex-wrap: wrap; gap: .8rem; margin-top: .8rem; }
.state-key div { display: flex; align-items: center; gap: .4rem; font-size: .78rem; color: var(--ink-3); }
.state-key i { width: 11px; height: 11px; border-radius: 3px; display: block; }

/* ------------------------------------------------------------------ responsive */
@media (max-width: 1080px) {
    .sim-shell { grid-template-columns: minmax(0, 1fr); }
    .sim-panel { position: static; max-height: none; }
}

@media (max-width: 900px) {
    .lab {
        grid-template-columns: 1fr;
        grid-template-rows: minmax(0, 1fr) auto;
        height: calc(100dvh - var(--nav-h));
    }
    .lab-panel {
        border-left: 0;
        border-top: 1px solid var(--line);
        height: 46dvh;
    }
    .toolbar { bottom: 12px; }
    .hud-stats { grid-template-columns: repeat(2, minmax(66px, auto)); gap: .4rem .9rem; }
    .hud-stat-value { font-size: .9rem; }
}

@media (max-width: 700px) {
    /* The labels are what make the transport controls findable on a desktop, but
       they overflow a phone. Icons plus the accessible name carry it there. */
    .tool--wide span { display: none; }
    .tool--wide { width: 38px; padding-inline: 0; }

    /* One row that scrolls sideways, not three that wrap. Wrapping cost 118px of
       a 424px stage - a third of the playable area - to show controls that fit
       comfortably in a single swipeable strip. */
    .toolbar {
        flex-wrap: nowrap;
        justify-content: flex-start;
        overflow-x: auto;
        overflow-y: hidden;
        scrollbar-width: none;
        left: 12px;
        right: 12px;
        translate: none;
        width: auto;
        max-width: none;
        bottom: 12px;
    }
    .toolbar::-webkit-scrollbar { display: none; }
    .toolbar > * { flex: none; }

    /* Redundant or meaningless on a phone: pinch replaces the zoom buttons, and
       a keyboard-shortcut list is no use without a keyboard. */
    #btn-zoom-in, #btn-zoom-out, #btn-full, #btn-keys { display: none; }

    /* The strip scrolls, so what sits in the first screenful matters. Ordered by
       how often it is reached for rather than by the desktop grouping - Random
       and Clear were being pushed out of sight behind the speed slider. */
    .toolbar .tool-sep { display: none; }
    #btn-play { order: 1; }
    #btn-step { order: 2; }
    #btn-random { order: 3; }
    #btn-clear { order: 4; }
    .tool-speed { order: 5; }
    #btn-fit { order: 6; }
    #btn-grid { order: 7; }
    #btn-reset { order: 8; }

    .tool[data-tip]::after { display: none; }
    .hud { top: 10px; left: 10px; right: 10px; }
    .hud-panel { padding: .5rem .7rem; }
}

/* Sized for fingers rather than cursors. 44px is the smallest target most
   accessibility guidance accepts; the desktop sizes are deliberately smaller. */
@media (pointer: coarse) {
    .tool, .tool--wide { width: 44px; height: 44px; }
    .tool svg { width: 20px; height: 20px; }
    .lab-tab { padding-block: 1.05rem; }
    .pat-item { padding: .6rem .5rem; }
    .pat-cat, .pill { padding-block: .5rem; }
    .seg button { padding-block: .55rem; }
    .switch { width: 46px; height: 26px; }
    .switch::after { width: 20px; height: 20px; }
    .switch[aria-checked="true"]::after { translate: 20px 0; }
    input[type="range"] { height: 34px; }
    .tool-speed { min-width: 140px; }
    .tool-speed output { min-width: 52px; }
}

@media (max-width: 620px) {
    /* The HUD is an overlay, so every row it grows costs visible grid. Keep the
       three figures worth glancing at and drop the rest. */
    .hud-spark,
    .hud-stats > div:nth-child(3) { display: none; }
    .hud-stats { grid-template-columns: repeat(3, auto); gap: .3rem 1.1rem; }
    .hud-stat-value { font-size: .95rem; }
    .lab-panel { height: 42dvh; }
}
