/* ===== 3D build viewer (build.php) — LEGO Playground tokens copied from style.css ===== */
:root {
    --red:    #E3000B;
    --blue:   #006CB7;
    --yellow: #FFD700;
    --green:  #00A651;
    --surface: #FFFFFF;
    --cream:   #FFF8F0;
    --line:    #F0EBE4;
    --text:    #1A1A1A;
    --text2:   #5A5A5A;
    --shadow:   0 2px 12px rgba(0,0,0,0.06);
    --shadow-3: 0 8px 24px rgba(0,0,0,0.12);
    --radius:    16px;
    --radius-xs: 8px;
    --ease:     cubic-bezier(0.22, 1, 0.36, 1);
    --ease-in:  cubic-bezier(0.55, 0, 1, 0.45);
    --dur-fast: 150ms;
    --font-heading: 'Caveat', 'Segoe UI', system-ui, sans-serif;
    --font-body:    'Quicksand', system-ui, -apple-system, sans-serif;

    /* Layout */
    --gutter:  16px;
    --safe-t:  env(safe-area-inset-top, 0px);
    --safe-r:  env(safe-area-inset-right, 0px);
    --safe-b:  env(safe-area-inset-bottom, 0px);
    --safe-l:  env(safe-area-inset-left, 0px);
    --top-h:   52px;
    --legal-h: 28px;
    --panel-w: 344px;
    --row-h:   30px;
    --rows:    5;
    --btn:     44px;
}

*, *::before, *::after { box-sizing: border-box; }
[hidden] { display: none !important; }

html, body { margin: 0; height: 100%; overflow: hidden; overscroll-behavior: none; }
body {
    font: 500 15px/1.4 var(--font-body);
    color: var(--text);
    background: radial-gradient(ellipse 80% 70% at 50% 45%, var(--cream) 0%, #FFFFFF 75%) fixed, #FFFFFF;
    -webkit-tap-highlight-color: transparent;
}
body.mood { background: radial-gradient(ellipse 80% 70% at 50% 45%, color-mix(in srgb, var(--mood) 5%, var(--cream)) 0%, #FFFFFF 75%) fixed, #FFFFFF; }   /* build.js sets --mood + .mood (never in shot mode) */
#view { position: fixed; inset: 0; }
#view canvas { display: block; touch-action: none; }
:focus-visible { outline: 3px solid var(--blue); outline-offset: 2px; }
button { font: inherit; cursor: pointer; }

/* ===== Buttons and pills: lift on hover, press to 0.97 ===== */
.pill, .controls button, #buildIt, #ldrInfo {
    transition: transform var(--dur-fast) var(--ease), box-shadow var(--dur-fast) var(--ease), background var(--dur-fast);
}
@media (hover: hover) {
    .pill:hover, .controls button:hover, #buildIt:hover, #ldrInfo:hover { transform: translateY(-2px); box-shadow: var(--shadow-3); }
}
.pill:active, .controls button:active, #buildIt:active, #ldrInfo:active { transform: scale(0.97); transition-duration: 100ms; }

.pill {
    display: inline-flex; align-items: center; gap: 6px;
    min-height: 44px; padding: 0 14px;
    background: var(--surface); color: var(--text);
    border: 1px solid var(--line); border-radius: 999px; box-shadow: var(--shadow);
    font-size: 13px; font-weight: 600; text-decoration: none; white-space: nowrap;
}
.pill-short { display: none; }

/* ===== Top bar ===== */
#topbar {
    position: fixed; top: 0; left: 0; right: 0; z-index: 10;
    height: calc(var(--top-h) + var(--safe-t));
    padding: var(--safe-t) calc(var(--gutter) + var(--safe-r)) 0 calc(var(--gutter) + var(--safe-l));
    display: flex; align-items: center; gap: 12px;
    background: rgba(255,255,255,0.85); border-bottom: 1px solid var(--line);
    -webkit-backdrop-filter: blur(8px); backdrop-filter: blur(8px);
}
#back { min-height: 44px; color: var(--blue); }
.top-title { flex: 1; min-width: 0; font-weight: 700; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.top-tag { flex: none; padding: 3px 10px; border-radius: 999px; background: rgba(0,108,183,0.1); color: var(--blue); font-size: 12px; font-weight: 700; }

/* ===== Title block (bottom left) ===== */
#hero {
    position: fixed; z-index: 5;
    left: calc(var(--gutter) + var(--safe-l));
    bottom: calc(var(--legal-h) + var(--safe-b) + 12px);
    max-width: min(440px, calc(100vw - var(--panel-w) - 3 * var(--gutter)));
    pointer-events: none;
}
#hero a, #hero button { pointer-events: auto; }
/* Legibility over the model: a soft cream scrim behind the block + a cream halo hugging the letters (dark parts can sit behind the text) */
#hero::before {
    content: ''; position: absolute; z-index: -1; inset: -28px -48px -20px -32px; pointer-events: none;
    background: radial-gradient(ellipse 60% 62% at 36% 52%, rgba(255,248,240,0.82), rgba(255,248,240,0.5) 55%, rgba(255,248,240,0) 100%);
}
#eyebrow, #title, #stats {
    text-shadow: 0 0 1px #FFF8F0, 0 0 3px #FFF8F0, 0 0 8px rgba(255,248,240,0.95), 0 0 16px rgba(255,248,240,0.8);
}
#eyebrow { margin: 0; color: var(--red); font-size: 12px; font-weight: 700; letter-spacing: 0.04em; text-transform: uppercase; }
#title { margin: 2px 0 4px; font: 700 clamp(30px, 4vw, 48px)/1.05 var(--font-heading); }
#stats { margin: 0 0 12px; color: var(--text2); font-size: 14px; font-variant-numeric: tabular-nums; min-height: 1.4em; }
#downloads { position: relative; display: flex; flex-wrap: wrap; align-items: center; gap: 8px; }
#ldrInfo {
    width: 44px; height: 44px; padding: 0; border: 1px solid var(--line); border-radius: 50%;
    background: var(--surface); color: var(--blue); font-size: 16px; line-height: 1; box-shadow: var(--shadow);
}
#ldrTip {
    position: absolute; left: 0; bottom: calc(100% + 8px); z-index: 20;
    padding: 6px 10px; border-radius: var(--radius-xs);
    background: var(--text); color: #fff; font-size: 12px; white-space: nowrap;
    opacity: 0; transform: translateY(4px); pointer-events: none;
    transition: opacity var(--dur-fast) var(--ease), transform var(--dur-fast) var(--ease);
}
#ldrInfo:hover + #ldrTip, #ldrInfo:focus + #ldrTip { opacity: 1; transform: none; }

/* ===== Steps panel (bottom right, fixed height: list scrolls, controls pinned at the bottom) ===== */
#panel {
    position: fixed; z-index: 6;
    right: calc(var(--gutter) + var(--safe-r));
    bottom: calc(var(--legal-h) + var(--safe-b) + 12px);
    width: var(--panel-w);
    height: calc(var(--row-h) * var(--rows) + 138px);
    display: flex; flex-direction: column; gap: 8px;
    padding: 14px;
    background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); box-shadow: var(--shadow);
}
#stepHead { flex: none; display: flex; align-items: baseline; justify-content: space-between; gap: 8px; min-height: 28px; }
#stepLabel { font-weight: 700; font-size: 16px; font-variant-numeric: tabular-nums; }
#stepCount { color: var(--text2); font-size: 13px; font-weight: 600; font-variant-numeric: tabular-nums; }
body.done #stepLabel { color: var(--green); }
#partsToggle { display: none; }
#stepParts {
    flex: 1; min-height: 0; margin: 0; padding: 0; list-style: none;
    overflow: auto; overscroll-behavior: contain;
    font-size: 13px;
}
#stepParts li {
    height: var(--row-h); line-height: var(--row-h);
    white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
    animation: rowIn 200ms var(--ease) both;
}
#stepParts li:nth-child(2) { animation-delay: 20ms; }
#stepParts li:nth-child(3) { animation-delay: 40ms; }
#stepParts li:nth-child(4) { animation-delay: 60ms; }
#stepParts li:nth-child(5) { animation-delay: 80ms; }
#stepParts li:nth-child(n+6) { animation-delay: 100ms; }
#stepParts li span { color: var(--text2); }
#stepParts .swatch {
    display: inline-block; vertical-align: -2px; width: 14px; height: 14px; margin-right: 8px;
    border-radius: 3px; border: 1px solid rgba(0,0,0,0.25);
}
@keyframes rowIn { from { opacity: 0; transform: translateX(16px); } }

.controls { flex: none; display: flex; align-items: center; gap: 8px; }
.controls button {
    flex: none; display: grid; place-items: center;
    width: var(--btn); height: var(--btn); padding: 0; border: 0; border-radius: 12px;
    background: var(--blue); color: #fff;
}
.controls button svg { width: 22px; height: 22px; fill: none; stroke: currentColor; stroke-width: 2.5; stroke-linecap: round; stroke-linejoin: round; }
#slider { flex: 1; min-width: 0; height: var(--btn); margin: 0 0 0 4px; accent-color: var(--blue); cursor: pointer; }

/* Play: the one red button; progress ring fills to the next step (JS sets --progress 0..1) */
#play { position: relative; width: calc(var(--btn) + 6px); height: calc(var(--btn) + 6px); border-radius: 50%; background: var(--red); }
#play .ring { position: absolute; inset: 0; width: 100%; height: 100%; transform: rotate(-90deg); pointer-events: none; }
#play .ring circle { fill: none; stroke-width: 3; }
#play .ring .track { stroke: rgba(255,255,255,0.25); }
#play .ring .fill { stroke: #fff; stroke-dasharray: 1; stroke-dashoffset: calc(1 - var(--progress, 0)); stroke-linecap: round; }
#play .icon { position: relative; width: 16px; height: 18px; transform: translateX(1.5px); transition: transform var(--dur-fast) var(--ease); }   /* nudge ▶ to look centred */
#play .icon::before, #play .icon::after {
    content: ''; position: absolute; inset: 0; background: currentColor;
    transition: clip-path var(--dur-fast) var(--ease);
}
#play .icon::before { clip-path: polygon(0 0, 50% 25%, 50% 75%, 0 100%); }
#play .icon::after  { clip-path: polygon(50% 25%, 100% 50%, 100% 50%, 50% 75%); }
#play[aria-label="Pause"] .icon { transform: none; }
#play[aria-label="Pause"] .icon::before { clip-path: polygon(0 0, 35% 0, 35% 100%, 0 100%); }
#play[aria-label="Pause"] .icon::after  { clip-path: polygon(65% 0, 100% 0, 100% 100%, 65% 100%); }

/* Finish burst: JS appends ~12 span.bit here; CSS throws them (600 ms) */
#confetti { position: absolute; left: 50%; top: 0; width: 0; height: 0; pointer-events: none; }
.bit {
    position: absolute; left: -5px; top: -3px; width: 10px; height: 6px; border-radius: 2px;
    background: var(--red); opacity: 0;
    animation: bitOut 600ms var(--ease) forwards;
}
.bit:nth-child(4n+2) { background: var(--blue); }
.bit:nth-child(4n+3) { background: var(--yellow); }
.bit:nth-child(4n+4) { background: var(--green); }
.bit:nth-child(1)  { --dx: -120px; --dy: -70px;  --r: -200deg; }
.bit:nth-child(2)  { --dx: -90px;  --dy: -120px; --r: 160deg; }
.bit:nth-child(3)  { --dx: -55px;  --dy: -150px; --r: -120deg; }
.bit:nth-child(4)  { --dx: -20px;  --dy: -165px; --r: 240deg; }
.bit:nth-child(5)  { --dx: 20px;   --dy: -160px; --r: -260deg; }
.bit:nth-child(6)  { --dx: 55px;   --dy: -145px; --r: 180deg; }
.bit:nth-child(7)  { --dx: 90px;   --dy: -115px; --r: -150deg; }
.bit:nth-child(8)  { --dx: 120px;  --dy: -75px;  --r: 210deg; }
.bit:nth-child(9)  { --dx: -140px; --dy: -30px;  --r: 300deg; }
.bit:nth-child(10) { --dx: 140px;  --dy: -35px;  --r: -300deg; }
.bit:nth-child(11) { --dx: -70px;  --dy: -95px;  --r: 90deg; }
.bit:nth-child(12) { --dx: 70px;   --dy: -100px; --r: -90deg; }
@keyframes bitOut {
    0%   { opacity: 1; transform: translate(0, 0) rotate(0); }
    70%  { opacity: 1; }
    100% { opacity: 0; transform: translate(var(--dx, 0), var(--dy, -100px)) rotate(var(--r, 180deg)); }
}

/* ===== Build it (over the finished model until building starts) ===== */
#buildIt {
    position: fixed; left: 50%; top: 55%; z-index: 7;
    translate: -50% -50%;
    min-height: 56px; padding: 0 28px; border: 0; border-radius: 999px;
    background: var(--red); color: #fff; font-size: 20px; font-weight: 700;
    box-shadow: 0 8px 24px rgba(227,0,11,0.3);
    animation: buildIn 300ms var(--ease) 150ms both;
}
body.building #buildIt, #loading.show ~ #buildIt { display: none; }
@keyframes buildIn { from { opacity: 0; transform: scale(0.9); } }

/* ===== Loading: three bricks stack and unstack ===== */
#loading {
    position: fixed; inset: 0; z-index: 8;
    display: grid; place-items: center; pointer-events: none;
    opacity: 0; visibility: hidden;
    transition: opacity 200ms var(--ease-in), visibility 0s 200ms;
}
#loading.show { opacity: 1; visibility: visible; transition: none; }
.loading-card {
    display: flex; align-items: flex-end; gap: 12px; padding: 14px 20px;
    background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); box-shadow: var(--shadow);
    font-weight: 600; font-variant-numeric: tabular-nums;
}
.bricks { position: relative; width: 24px; height: 36px; }
.bricks i {
    position: absolute; left: 0; width: 24px; height: 10px; border-radius: 2px;
    background: var(--red);
    animation: stack 1.6s var(--ease) infinite both;
}
.bricks i::before, .bricks i::after { content: ''; position: absolute; top: -3px; width: 6px; height: 3px; border-radius: 1px 1px 0 0; background: inherit; }
.bricks i::before { left: 4px; }
.bricks i::after { right: 4px; }
.bricks i:nth-child(1) { bottom: 0; }
.bricks i:nth-child(2) { bottom: 12px; background: var(--blue); animation-delay: 0.15s; }
.bricks i:nth-child(3) { bottom: 24px; background: var(--yellow); animation-delay: 0.3s; }
@keyframes stack {
    0%       { opacity: 0; transform: translateY(-14px); }
    20%, 70% { opacity: 1; transform: none; }
    90%, 100% { opacity: 0; transform: translateY(-14px); }
}

/* ===== Legal line ===== */
#legal {
    position: fixed; left: 0; right: 0; bottom: 0; z-index: 4;
    min-height: calc(var(--legal-h) + var(--safe-b));
    padding: 0 calc(var(--gutter) + var(--safe-r)) var(--safe-b) calc(var(--gutter) + var(--safe-l));
    color: var(--text2); font-size: 11px; line-height: var(--legal-h); text-align: center;
}
#legal a { color: inherit; }

/* ===== Tablets: narrower panel so the download pills stay on one row ===== */
@media (max-width: 900px) and (min-width: 641px) {
    :root { --panel-w: 320px; }
    #downloads .pill { padding: 0 12px; font-size: 12px; }
}

/* ===== Short screens (landscape phones, small laptops) ===== */
@media (max-height: 560px) and (min-width: 641px) {
    :root { --rows: 3; --top-h: 44px; }
    #title { font-size: 28px; }
}

/* ===== Phones: title under the top bar, pills in the top bar, parts behind a drawer ===== */
@media (max-width: 640px) {
    :root { --top-h: 56px; --legal-h: 34px; --btn: 48px; }
    .top-title, .top-tag { display: none; }
    #back { min-height: 44px; padding: 0 12px; }
    #hero {
        top: calc(var(--top-h) + var(--safe-t) + 10px); bottom: auto; z-index: 11;
        right: calc(var(--gutter) + var(--safe-r)); max-width: none;
    }
    #eyebrow { font-size: 11px; }
    #title { font-size: 30px; }
    #stats { font-size: 13px; margin: 0; }
    #downloads {
        position: fixed; z-index: 11; gap: 6px;
        top: calc(var(--safe-t) + 6px); right: calc(var(--gutter) + var(--safe-r));
    }
    #downloads .pill { min-height: 44px; padding: 0 12px; }
    .pill-long { display: none; }
    .pill-short { display: inline; }
    #ldrTip { left: auto; right: 0; bottom: auto; top: calc(100% + 8px); transform: translateY(-4px); }
    #legal { padding-top: 4px; font-size: 10px; line-height: 1.3; }

    #panel {
        left: calc(var(--gutter) + var(--safe-l)); right: calc(var(--gutter) + var(--safe-r));
        bottom: calc(var(--legal-h) + var(--safe-b) + 6px);
        width: auto; height: auto; padding: 8px 12px 12px; gap: 4px;
    }
    #stepHead { min-height: 44px; align-items: center; padding-right: 24px; }
    #partsToggle {
        display: block; position: absolute; left: 0; right: 0; top: 0; height: 52px;
        padding: 0 16px 0 0; border: 0; border-radius: var(--radius) var(--radius) 0 0;
        background: transparent; color: var(--text2); text-align: right;
    }
    #partsToggle .chev { display: inline-block; font-size: 14px; transition: transform 200ms var(--ease); }
    #partsToggle[aria-expanded="true"] .chev { transform: rotate(180deg); }
    #stepParts {
        position: absolute; left: 0; right: 0; bottom: calc(100% + 8px);
        height: calc(var(--row-h) * var(--rows) + 20px); padding: 10px 14px;
        background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); box-shadow: var(--shadow-3);
        opacity: 0; visibility: hidden; transform: translateY(12px);
        transition: opacity 200ms var(--ease), transform 200ms var(--ease), visibility 0s 200ms;
    }
    #partsToggle[aria-expanded="true"] + #stepParts { opacity: 1; visibility: visible; transform: none; transition-delay: 0s; }
    .controls { justify-content: space-between; }
    #buildIt { top: 50%; }
}

/* ===== Shot mode (?shot=1): script/brick_render.py and script/brick_guide.py screenshots ===== */
body.shot #topbar, body.shot #hero, body.shot .controls, body.shot #buildIt,
body.shot #loading, body.shot #legal, body.shot #confetti, body.shot #stepCount, body.shot #partsToggle { display: none; }
body.shot #panel {
    top: 24px; left: 24px; right: auto; bottom: auto;
    width: auto; min-width: 260px; max-width: 360px; height: auto;
    padding: 10px 12px; border: 2px solid #13222e; border-radius: 12px; box-shadow: none;
}
body.shot #stepHead { min-height: 0; padding: 0; }
body.shot #stepLabel { font-size: 30px; line-height: 1.2; margin: 0 0 2px; color: var(--text); }
body.shot #stepParts {
    position: static; height: auto; max-height: none; padding: 0; overflow: visible;
    border: 0; box-shadow: none; opacity: 1; visibility: visible; transform: none;
    font-size: 15px;
}
body.shot #stepParts li { height: auto; line-height: 1.5; white-space: normal; }

/* ===== Thumb (?shot=1&thumb=1): script/brick_thumbs.py card renders, the finished model on plain cream, no step callout ===== */
body.thumb { background: var(--cream); }
body.thumb #panel { display: none; }

/* ===== Embed (?embed=1): build_index.php's stage iframe, model and floor only, the page's cream shows through ===== */
body.embed { background: transparent; }
body.embed #topbar, body.embed #hero, body.embed #panel, body.embed #buildIt, body.embed #legal, body.embed #loadingText { display: none; }
body.embed #view { pointer-events: none; }
body.embed .loading-card { padding: 0; background: none; border: 0; box-shadow: none; }

/* ===== Less motion: device setting, JS-set body.reduced, and screenshots ===== */
@media (prefers-reduced-motion: reduce) {
    *, *::before, *::after { animation: none !important; transition: none !important; }
}
body.reduced *, body.reduced *::before, body.reduced *::after,
body.shot *, body.shot *::before, body.shot *::after { animation: none !important; transition: none !important; }

/* ===== Far bricks (assets/build-backdrop.js): own canvas under #view (earlier in the DOM), faint and soft ===== */
#backdrop {
    position: fixed; inset: 0; width: 100%; height: 100%;
    display: block; pointer-events: none;
    opacity: 0.16; filter: blur(0.5px);   /* × each brick's 0.72–1 → 12–16 %; light blur so they read as bricks, not smudges */
}
body.shot #backdrop { display: none; }
