/* ============================================
   3D POLITICS — DECISION-MAKING MYSTERY
   Immersive layout: full-bleed scene, parchment overlay
   Brand tokens per architecture/brand/foundations.md
   ============================================ */

/* === CSS CUSTOM PROPERTIES (Design Tokens) === */
:root {
    /* 3D Politics Brand Colors (canonical — do not redefine) */
    --bg: #F5F1E8;
    --surface: #FFFCF5;
    --foreground: #1A1A1A;
    --muted: #6B6B6B;
    --accent: #C0392B;
    --accent-contrast: #FFFCF5;

    /* Pillar Colors */
    --decision-making: #C0392B;
    --decentralization: #2E86AB;
    --distribution: #5D9B4B;
    --synthesis: #7B4F8F;

    /* Parchment & ink */
    --parchment-hi: #F7EFDB;
    --parchment-lo: #EADDC0;
    --ink: #2A1F1A;
    --ink-soft: rgba(61, 46, 31, 0.45);

    /* Shadows */
    --shadow-soft: 0 2px 8px rgba(26, 26, 26, 0.08);
    --shadow-medium: 0 4px 16px rgba(26, 26, 26, 0.12);
    --shadow-deep: 0 12px 40px rgba(26, 26, 26, 0.35);

    /* Typography */
    --font-display: 'Helvetica Neue', 'Helvetica Now', 'Neue Haas Grotesk', -apple-system, sans-serif;
    --font-mono: 'IBM Plex Mono', 'JetBrains Mono', monospace;
    --font-serif: 'IBM Plex Serif', Georgia, serif;

    /* Spacing */
    --space-xs: 4px;
    --space-sm: 8px;
    --space-md: 16px;
    --space-lg: 24px;
    --space-xl: 40px;

    /* Transitions */
    --transition-fast: 0.15s ease;
    --transition-medium: 0.3s ease;
    --transition-slow: 0.5s ease;
}

/* === BASE === */
*, *::before, *::after {
    box-sizing: border-box;
}

html, body {
    margin: 0;
    padding: 0;
    height: 100%;
    overflow: hidden;
}

body {
    font-family: var(--font-serif);
    font-size: 16px;
    line-height: 1.7;
    color: var(--foreground);
    background: var(--bg);
    -webkit-font-smoothing: antialiased;
    -moz-osx-font-smoothing: grayscale;
}

/* === LAYOUT === */
.outer-container {
    position: relative;
    width: 100vw;
    height: 100vh;
    overflow: hidden;
}

/* === DIORAMA (full-bleed scene) === */
.diorama {
    position: absolute;
    inset: 0;
    width: 100%;
    height: 100%;
    overflow: hidden;
    background: var(--bg);
    cursor: crosshair; /* Suggests light interaction */
}

/* === LAYERS === */
.layer {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    pointer-events: none;
    will-change: transform;
    transition: opacity var(--transition-medium);
}

.layer svg {
    width: 100%;
    height: 100%;
    display: block;
}

.layer img {
    width: 100%;
    height: 100%;
    display: block;
    object-fit: cover; /* Full-bleed scenes crop, never distort */
}

/* Light overlay */
.light-layer {
    pointer-events: none;
    mix-blend-mode: soft-light;
    transition: background var(--transition-fast);
}

/* Readability scrim behind the parchment panel */
.scrim {
    position: absolute;
    inset: 0;
    pointer-events: none;
    background: linear-gradient(to top,
        rgba(26, 20, 14, 0.38) 0%,
        rgba(26, 20, 14, 0.10) 32%,
        transparent 58%);
}

/* === ROOM MOOD === */
/* Generated watercolor carries its own colors — keep grading subtle and warm.
   Pillar mood comes from the light overlay, not from recoloring the art. */
.diorama .layer {
    filter: sepia(0.08);
}

/* === KEEPER PORTRAIT (progressive: shown when assets exist) === */
.keeper-portrait {
    position: absolute;
    left: var(--space-lg);
    bottom: var(--space-lg);
    height: 46vh;
    z-index: 60;
    pointer-events: none;
    filter: drop-shadow(0 8px 24px rgba(26, 26, 26, 0.4));
}

.keeper-portrait img {
    height: 100%;
    display: block;
    transition: opacity var(--transition-medium);
}

/* === PARCHMENT (shared plaque style) === */
.parchment,
.narrative-container,
.fragment-progress,
.formula-mini {
    background:
        url("assets/ui/parchment.png") center / cover no-repeat,
        url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='240' height='240'%3E%3Cfilter id='n'%3E%3CfeTurbulence type='fractalNoise' baseFrequency='0.8' numOctaves='2'/%3E%3CfeColorMatrix values='0 0 0 0 0.23 0 0 0 0 0.18 0 0 0 0 0.12 0 0 0 0.05 0'/%3E%3C/filter%3E%3Crect width='240' height='240' filter='url(%23n)'/%3E%3C/svg%3E"),
        linear-gradient(160deg, var(--parchment-hi) 0%, #F1E6CC 55%, var(--parchment-lo) 100%);
    border: 1px solid #D8C9A8;
    box-shadow:
        var(--shadow-deep),
        inset 0 0 0 4px rgba(255, 252, 245, 0.35),
        inset 0 0 60px rgba(61, 46, 31, 0.10);
}

/* === NARRATIVE PARCHMENT PANEL === */
.narrative-container {
    position: absolute;
    left: 50%;
    bottom: var(--space-lg);
    transform: translateX(-50%);
    width: min(780px, 92vw);
    max-height: 38vh;
    overflow: visible; /* #story scrolls itself; keeps the keeper chip unclipped */
    padding: 20px 36px;
    border-radius: 8px;
    z-index: 70;
    display: flex;
    flex-direction: column;
}

.narrative-container::-webkit-scrollbar {
    width: 6px;
}
.narrative-container::-webkit-scrollbar-track {
    background: transparent;
}
.narrative-container::-webkit-scrollbar-thumb {
    background: #C9B48A;
    border-radius: 3px;
}
.narrative-container::-webkit-scrollbar-thumb:hover {
    background: #A8946A;
}

/* === STORY TEXT === */
#story {
    flex: 0 1 auto;
    min-height: 0;
    overflow-y: auto;
}

#choiceStrip {
    flex: none;
    margin-top: var(--space-sm);
}

#story p {
    font-family: var(--font-serif);
    font-size: 16px;
    line-height: 1.75;
    color: var(--ink);
    margin: 0 0 var(--space-md) 0;
    opacity: 0;
    animation: fadeInUp 0.5s ease forwards;
}

#story p:nth-child(1) { animation-delay: 0.1s; }
#story p:nth-child(2) { animation-delay: 0.2s; }
#story p:nth-child(3) { animation-delay: 0.3s; }
#story p:nth-child(4) { animation-delay: 0.4s; }
#story p:nth-child(5) { animation-delay: 0.5s; }

/* Manuscript drop cap on the first paragraph of each scene */
#story p:first-of-type::first-letter {
    font-family: var(--font-serif);
    font-size: 2.7em;
    font-weight: 600;
    color: var(--accent);
    float: left;
    line-height: 0.85;
    padding: 4px 10px 0 0;
}

/* === CHOICE BUTTONS (styled to match the scene) === */
p.choice {
    margin: var(--space-sm) 0;
    opacity: 0;
    animation: fadeInUp 0.4s ease forwards;
}

p.choice a {
    font-family: var(--font-display);
    font-size: 15px;
    font-weight: 500;
    color: #8A2A1D;
    text-decoration: none;
    display: flex;
    align-items: center;
    min-height: 44px; /* Touch target */
    padding: 10px 18px;
    background: linear-gradient(180deg, #F3E9D2 0%, #E9DCC0 100%);
    border: 1px solid var(--ink-soft);
    border-radius: 6px;
    box-shadow:
        0 2px 6px rgba(26, 26, 26, 0.18),
        inset 0 1px 0 rgba(255, 252, 245, 0.65);
    transition: all var(--transition-fast);
}

p.choice a:hover {
    color: #A02820;
    border-color: var(--accent);
    transform: translateY(-2px);
    box-shadow:
        0 6px 14px rgba(26, 26, 26, 0.22),
        inset 0 1px 0 rgba(255, 252, 245, 0.65);
}

p.choice a::before {
    content: "→";
    margin-right: 10px;
    opacity: 0.4;
    transition: opacity var(--transition-fast);
}

p.choice a:hover::before,
p.choice a:focus-visible::before {
    opacity: 1;
}

/* Visible keyboard focus */
p.choice a:focus-visible {
    outline: 2px solid var(--accent);
    outline-offset: 2px;
}

/* Strong/emphasis in narrative */
#story strong {
    font-weight: 600;
    color: var(--accent);
}

#story em {
    font-style: italic;
    color: #5A4A38;
}

/* === HUD PLAQUES === */
.fragment-progress {
    position: fixed;
    top: var(--space-lg);
    left: var(--space-lg);
    font-family: var(--font-mono);
    font-size: 13px;
    font-weight: 500;
    color: var(--ink);
    padding: var(--space-sm) var(--space-md);
    border-radius: 20px;
    z-index: 90;
    transition: all var(--transition-medium);
}

.fragment-progress.complete {
    color: var(--accent);
    box-shadow:
        0 0 20px rgba(192, 57, 43, 0.25),
        inset 0 0 0 4px rgba(255, 252, 245, 0.35);
}

.progress-count {
    color: var(--accent);
    font-weight: 600;
}

.formula-mini {
    position: fixed;
    top: var(--space-lg);
    right: var(--space-lg);
    padding: 12px 16px;
    border-radius: 8px;
    font-family: var(--font-mono);
    font-size: 12px;
    line-height: 2;
    z-index: 90;
}

.formula-mini[hidden] {
    display: none;
}

.formula-row {
    display: flex;
    align-items: center;
    gap: var(--space-sm);
}

.formula-label {
    font-weight: 600;
    color: var(--muted);
    min-width: 50px;
}

.formula-slot {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 32px;
    height: 32px;
    border-radius: 4px;
    background: rgba(61, 46, 31, 0.15);
    color: var(--muted);
    font-weight: 600;
    transition: all var(--transition-medium);
}

.formula-slot.filled {
    background: var(--accent);
    color: var(--accent-contrast);
    box-shadow: 0 0 12px rgba(192, 57, 43, 0.3);
    animation: slotGlow 2s ease infinite;
}

@keyframes slotGlow {
    0%, 100% { box-shadow: 0 0 8px rgba(192, 57, 43, 0.2); }
    50% { box-shadow: 0 0 16px rgba(192, 57, 43, 0.4); }
}

/* === PUZZLE CARDS === */
.puzzle-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(140px, 1fr));
    gap: 12px;
    margin: var(--space-md) 0;
}

.puzzle-card {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 8px;
    padding: 10px;
    min-height: 44px;
    background: linear-gradient(180deg, #F3E9D2 0%, #E9DCC0 100%);
    border: 1px solid var(--ink-soft);
    border-radius: 8px;
    box-shadow:
        0 2px 6px rgba(26, 26, 26, 0.18),
        inset 0 1px 0 rgba(255, 252, 245, 0.65);
    cursor: pointer;
    font-family: var(--font-display);
    font-size: 13px;
    font-weight: 500;
    color: var(--ink);
    text-align: center;
    transition: all var(--transition-fast);
}

.puzzle-card:hover {
    transform: translateY(-3px);
    border-color: var(--accent);
    box-shadow:
        0 8px 16px rgba(26, 26, 26, 0.22),
        inset 0 1px 0 rgba(255, 252, 245, 0.65);
}

.puzzle-card:focus-visible {
    outline: 2px solid var(--accent);
    outline-offset: 2px;
}

.puzzle-card img {
    width: 100%;
    aspect-ratio: 1;
    object-fit: contain;
    border-radius: 4px;
    pointer-events: none;
    mix-blend-mode: multiply; /* cream sheet bg melts into the card paper */
}

.puzzle-card .img-pair {
    display: flex;
    gap: 6px;
    width: 100%;
}

.puzzle-card .img-pair img {
    width: 50%;
}

.puzzle-card span {
    line-height: 1.35;
}

.puzzle-scenario {
    display: flex;
    gap: 12px;
    margin: var(--space-md) 0;
}

.puzzle-scenario img {
    flex: 1;
    width: 50%;
    object-fit: contain;
    background: linear-gradient(180deg, #F3E9D2 0%, #E9DCC0 100%);
    border: 1px solid var(--ink-soft);
    border-radius: 8px;
    mix-blend-mode: multiply;
}

/* === FRAGMENT GEMS === */
.slot-gem {
    width: 100%;
    height: 100%;
    object-fit: contain;
    display: block;
}

.flying-fragment img {
    width: 56px;
    height: 56px;
    object-fit: contain;
    display: block;
}

/* === FRAGMENT OVERLAY (Flying animations) === */
.fragment-overlay {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    pointer-events: none;
    z-index: 1000;
}

.flying-fragment {
    position: absolute;
    font-family: var(--font-mono);
    font-size: 28px;
    font-weight: 600;
    color: var(--accent);
    text-shadow: 0 0 20px rgba(192, 57, 43, 0.5);
    pointer-events: none;
    will-change: transform, opacity;
}

.flying-fragment[data-type="lot"] { color: var(--distribution); }
.flying-fragment[data-type="function"] { color: var(--synthesis); }
.flying-fragment[data-type="decision"] { color: var(--decision-making); }
.flying-fragment[data-type="psephos"] { color: var(--decision-making); }
.flying-fragment[data-type="agora"] { color: var(--distribution); }
.flying-fragment[data-type="dokimasia"] { color: var(--distribution); }
.flying-fragment[data-type="kleros"] { color: var(--decentralization); }
.flying-fragment[data-type="justification"] { color: var(--synthesis); }
.flying-fragment[data-type="interpretation"] { color: var(--synthesis); }

.fragment-glow {
    z-index: 50;
}

/* === ROOM TRANSITIONS === */
.diorama.transitioning .layer {
    opacity: 0 !important;
    transition: opacity 0.3s ease;
}

.diorama .layer {
    opacity: 1;
    transition: opacity 0.5s ease 0.2s; /* Delay fade-in for smoothness */
}

/* === OPENING SCENE === */
.opening-scene {
    font-style: italic;
    color: #5A4A38;
}

/* === ENDING === */
.ending-scene {
    text-align: center;
    font-size: 18px;
    line-height: 2;
}

.end {
    text-align: center;
    font-family: var(--font-display);
    font-size: 28px;
    font-weight: 700;
    color: var(--accent);
    margin-top: var(--space-xl);
    letter-spacing: 0.05em;
}

/* === ANIMATIONS === */
@keyframes fadeInUp {
    from {
        opacity: 0;
        transform: translateY(12px);
    }
    to {
        opacity: 1;
        transform: translateY(0);
    }
}

@keyframes pulse {
    0%, 100% { opacity: 1; }
    50% { opacity: 0.7; }
}

@keyframes float {
    0%, 100% { transform: translateY(0); }
    50% { transform: translateY(-8px); }
}

/* === RESPONSIVE === */
@media (max-width: 1100px) {
    .keeper-portrait {
        display: none; /* Parchment takes priority on narrower screens */
    }
}

@media (max-width: 900px) {
    .narrative-container {
        width: 94vw;
        max-height: 56vh;
        padding: 20px 24px;
        bottom: var(--space-md);
    }

    .fragment-progress {
        top: var(--space-sm);
        left: var(--space-sm);
        font-size: 12px;
    }

    .formula-mini {
        top: var(--space-sm);
        right: var(--space-sm);
        font-size: 11px;
        padding: 8px 12px;
    }

    .formula-slot {
        width: 28px;
        height: 28px;
    }

    #story p {
        font-size: 15px;
    }
}

@media (max-width: 600px) {
    .narrative-container {
        max-height: 62vh;
    }

    #story p {
        font-size: 14px;
        line-height: 1.6;
    }
}

/* === REDUCED MOTION === */
@media (prefers-reduced-motion: reduce) {
    .layer {
        transition: none !important;
        transform: none !important;
    }

    #story p,
    p.choice {
        animation: none;
        opacity: 1;
    }

    .flying-fragment {
        display: none;
    }

    .formula-slot.filled {
        animation: none;
    }
}

/* === HIGH CONTRAST === */
@media (prefers-contrast: high) {
    :root {
        --bg: #FFFFFF;
        --surface: #FFFFFF;
        --foreground: #000000;
        --muted: #333333;
        --ink: #000000;
        --parchment-hi: #FFFFFF;
        --parchment-lo: #F5F1E8;
    }
}

/* === BOOK MODE (opening / manuscript) === */
body[data-room="opening"] .narrative-container,
body[data-room="manuscript"] .narrative-container {
    background:
        url("assets/ui/parchment.png") center / cover no-repeat,
        linear-gradient(90deg,
            transparent 49.4%,
            rgba(61, 46, 31, 0.10) 50%,
            transparent 50.6%),
        url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='240' height='240'%3E%3Cfilter id='n'%3E%3CfeTurbulence type='fractalNoise' baseFrequency='0.8' numOctaves='2'/%3E%3CfeColorMatrix values='0 0 0 0 0.23 0 0 0 0 0.18 0 0 0 0 0.12 0 0 0 0.05 0'/%3E%3C/filter%3E%3Crect width='240' height='240' filter='url(%23n)'/%3E%3C/svg%3E"),
        linear-gradient(160deg, #F3E8CE 0%, #EDDFC0 55%, #E4D3B0 100%);
    border-radius: 12px 12px 6px 6px;
    border: 1px solid #C9B48A;
    box-shadow:
        0 16px 48px rgba(26, 26, 26, 0.45),
        inset 0 0 0 6px rgba(255, 252, 245, 0.25),
        inset 0 0 80px rgba(61, 46, 31, 0.14);
}

/* === FEATURE OVERLAY === */
.feature-overlay {
    position: fixed;
    inset: 0;
    background: rgba(26, 20, 14, 0.45);
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 200;
}

.feature-overlay[hidden] {
    display: none;
}

.feature-panel {
    position: relative;
    width: min(720px, 92vw);
    max-height: 84vh;
    overflow-y: auto;
    padding: 32px 40px 24px;
    border-radius: 10px;
}

/* frame mode: large parchment panel (inherits shared parchment style) */
.feature-panel[data-mode="frame"] {
    background:
        url("assets/ui/parchment.png") center / cover no-repeat,
        url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='240' height='240'%3E%3Cfilter id='n'%3E%3CfeTurbulence type='fractalNoise' baseFrequency='0.8' numOctaves='2'/%3E%3CfeColorMatrix values='0 0 0 0 0.23 0 0 0 0 0.18 0 0 0 0 0.12 0 0 0 0.05 0'/%3E%3C/filter%3E%3Crect width='240' height='240' filter='url(%23n)'/%3E%3C/svg%3E"),
        linear-gradient(160deg, var(--parchment-hi) 0%, #F1E6CC 55%, var(--parchment-lo) 100%);
    border: 1px solid #D8C9A8;
    box-shadow:
        var(--shadow-deep),
        inset 0 0 0 4px rgba(255, 252, 245, 0.35),
        inset 0 0 60px rgba(61, 46, 31, 0.10);
}

/* inscription mode: carved stone pedestal (generated slab image when present) */
.feature-panel[data-mode="inscription"] {
    background:
        url("assets/ui/pedestal.png") center / cover no-repeat,
        url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='240' height='240'%3E%3Cfilter id='n'%3E%3CfeTurbulence type='fractalNoise' baseFrequency='0.9' numOctaves='3'/%3E%3CfeColorMatrix values='0 0 0 0 0.1 0 0 0 0 0.09 0 0 0 0 0.08 0 0 0 0.12 0'/%3E%3C/filter%3E%3Crect width='240' height='240' filter='url(%23n)'/%3E%3C/svg%3E"),
        linear-gradient(165deg, #4A423C 0%, #3A3330 55%, #2E2825 100%);
    border: 1px solid rgba(61, 46, 31, 0.35);
    border-radius: 6px;
    padding: 104px 92px 64px;
    box-shadow:
        var(--shadow-deep),
        inset 0 0 60px rgba(61, 46, 31, 0.12);
}

.feature-panel[data-mode="inscription"] .feature-content {
    font-family: var(--font-serif);
    color: #43331F;
    text-shadow: 0 1px 0 rgba(255, 252, 245, 0.45), 0 -1px 1px rgba(61, 46, 31, 0.20);
    letter-spacing: 0.03em;
    text-align: center;
    font-size: 17px;
    line-height: 1.9;
    padding: var(--space-md) var(--space-sm);
}

.feature-panel[data-mode="inscription"] .feature-content p {
    color: #43331F;
    animation: none;
    opacity: 1;
}

.feature-close {
    position: absolute;
    top: 10px;
    right: 12px;
    width: 32px;
    height: 32px;
    border-radius: 50%;
    border: 1px solid var(--ink-soft);
    background: rgba(255, 252, 245, 0.5);
    color: var(--ink);
    font-size: 14px;
    cursor: pointer;
    line-height: 1;
    transition: all var(--transition-fast);
}

.feature-close:hover {
    border-color: var(--accent);
    color: var(--accent);
}

.feature-panel[data-mode="inscription"] .feature-close {
    background: rgba(255, 252, 245, 0.35);
    color: #43331F;
    border-color: rgba(67, 51, 31, 0.4);
}

.feature-content p {
    font-family: var(--font-serif);
    color: var(--ink);
    margin: 0 0 var(--space-md) 0;
}

.feature-choices {
    margin-top: var(--space-md);
}

/* Choices inside the overlay reuse the parchment button look */
.feature-choices p.choice {
    margin: var(--space-sm) 0;
}

.feature-choices p.choice a {
    font-family: var(--font-display);
    font-size: 15px;
    font-weight: 500;
    color: #8A2A1D;
    text-decoration: none;
    display: flex;
    align-items: center;
    justify-content: center;
    min-height: 44px;
    padding: 10px 18px;
    background: linear-gradient(180deg, #F3E9D2 0%, #E9DCC0 100%);
    border: 1px solid var(--ink-soft);
    border-radius: 6px;
    box-shadow: 0 2px 6px rgba(26, 26, 26, 0.18);
    transition: all var(--transition-fast);
}

.feature-choices p.choice a:hover {
    color: #A02820;
    border-color: var(--accent);
    transform: translateY(-2px);
}

/* === FRAME SLOT GRID === */
.frame-row {
    display: flex;
    align-items: center;
    gap: 12px;
    margin: 14px 0;
}

.frame-row-label {
    font-family: var(--font-mono);
    font-size: 12px;
    font-weight: 600;
    color: var(--muted);
    min-width: 52px;
}

.frame-slot {
    width: 64px;
    height: 64px;
    border-radius: 8px;
    border: 2px dashed rgba(61, 46, 31, 0.35);
    background: rgba(61, 46, 31, 0.06);
    display: inline-flex;
    align-items: center;
    justify-content: center;
    transition: all var(--transition-medium);
}

.frame-slot .frame-slot-hint {
    font-family: var(--font-mono);
    font-size: 18px;
    color: rgba(61, 46, 31, 0.30);
}

.frame-slot.filled {
    border: 2px solid rgba(61, 46, 31, 0.25);
    background: rgba(255, 252, 245, 0.5);
    box-shadow: 0 0 16px rgba(192, 57, 43, 0.18);
}

.frame-slot img {
    width: 90%;
    height: 90%;
    object-fit: contain;
}

.frame-slot {
    font-family: var(--font-mono);
    font-size: 20px;
    font-weight: 600;
    color: var(--accent);
}

/* === HUD BUTTONS === */
.hud-buttons {
    position: fixed;
    top: var(--space-lg);
    right: var(--space-lg);
    display: flex;
    gap: 8px;
    z-index: 95;
}

.hud-buttons button {
    width: 40px;
    height: 40px;
    border-radius: 50%;
    border: 1px solid #D8C9A8;
    background:
        url("assets/ui/parchment.png") center / cover no-repeat,
        linear-gradient(160deg, var(--parchment-hi) 0%, var(--parchment-lo) 100%);
    color: var(--ink);
    font-size: 18px;
    cursor: pointer;
    box-shadow: var(--shadow-medium);
    transition: all var(--transition-fast);
}

.hud-buttons button:hover {
    border-color: var(--accent);
    color: var(--accent);
    transform: translateY(-2px);
}

.hud-buttons button:focus-visible {
    outline: 2px solid var(--accent);
    outline-offset: 2px;
}

/* formula-mini shifts down to make room for the HUD buttons */
.formula-mini {
    top: 76px;
}

/* === TITLE SCREEN === */
.title-screen {
    position: fixed;
    inset: 0;
    background: rgba(26, 20, 14, 0.55);
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 300;
}

.title-screen[hidden] {
    display: none;
}

.title-card {
    text-align: center;
    padding: 48px 64px;
    border-radius: 10px;
    background:
        url("assets/ui/parchment.png") center / cover no-repeat,
        url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='240' height='240'%3E%3Cfilter id='n'%3E%3CfeTurbulence type='fractalNoise' baseFrequency='0.8' numOctaves='2'/%3E%3CfeColorMatrix values='0 0 0 0 0.23 0 0 0 0 0.18 0 0 0 0 0.12 0 0 0 0.05 0'/%3E%3C/filter%3E%3Crect width='240' height='240' filter='url(%23n)'/%3E%3C/svg%3E"),
        linear-gradient(160deg, var(--parchment-hi) 0%, #F1E6CC 55%, var(--parchment-lo) 100%);
    border: 1px solid #D8C9A8;
    box-shadow:
        var(--shadow-deep),
        inset 0 0 0 4px rgba(255, 252, 245, 0.35);
}

.title-card h1 {
    font-family: var(--font-serif);
    font-size: 34px;
    font-weight: 600;
    color: var(--ink);
    margin: 0 0 var(--space-sm) 0;
    letter-spacing: 0.02em;
}

.title-card .title-sub {
    font-family: var(--font-mono);
    font-size: 13px;
    color: var(--muted);
    margin: 0 0 var(--space-xl) 0;
    letter-spacing: 0.08em;
    text-transform: uppercase;
}

.title-card #btnBegin {
    font-family: var(--font-display);
    font-size: 17px;
    font-weight: 600;
    color: var(--accent-contrast);
    background: var(--accent);
    border: none;
    border-radius: 6px;
    padding: 14px 56px;
    cursor: pointer;
    box-shadow: 0 4px 12px rgba(192, 57, 43, 0.35);
    transition: all var(--transition-fast);
}

.title-card #btnBegin:hover {
    background: #A02820;
    transform: translateY(-2px);
}

.title-card #btnBegin:focus-visible {
    outline: 2px solid var(--ink);
    outline-offset: 3px;
}

/* === END PANEL (formula card + share) === */
.end-panel {
    margin-top: var(--space-xl);
    padding-top: var(--space-md);
    border-top: 1px solid rgba(61, 46, 31, 0.25);
    text-align: center;
}

.end-panel h2 {
    font-family: var(--font-serif);
    font-size: 20px;
    font-weight: 600;
    color: var(--ink);
    margin: 0 0 var(--space-md) 0;
}

.end-card-preview {
    width: 100%;
    border: 1px solid var(--ink-soft);
    border-radius: 8px;
    box-shadow: var(--shadow-medium);
    margin-bottom: var(--space-md);
    background: var(--parchment-hi);
    min-height: 120px;
}

.end-share-row {
    display: flex;
    flex-wrap: wrap;
    gap: 10px;
    justify-content: center;
    margin-bottom: var(--space-sm);
}

.end-btn {
    font-family: var(--font-display);
    font-size: 15px;
    font-weight: 500;
    color: #8A2A1D;
    text-decoration: none;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    min-height: 44px;
    padding: 10px 20px;
    background: linear-gradient(180deg, #F3E9D2 0%, #E9DCC0 100%);
    border: 1px solid var(--ink-soft);
    border-radius: 6px;
    box-shadow: 0 2px 6px rgba(26, 26, 26, 0.18);
    cursor: pointer;
    transition: all var(--transition-fast);
}

.end-btn:hover {
    color: #A02820;
    border-color: var(--accent);
    transform: translateY(-2px);
}

.end-btn:focus-visible {
    outline: 2px solid var(--accent);
    outline-offset: 2px;
}

.end-btn--small {
    font-size: 13px;
    min-height: 36px;
    padding: 6px 14px;
}

/* ============================================
   MOBILE SCENE-FIRST LAYOUT
   Scene dominates; parchment hugs content above a
   thumb-zone choice strip; collapsible sheet.
   ============================================ */

.sheet-handle {
    display: none;
}

.keeper-chip {
    display: none;
}

.keeper-chip img {
    transition: opacity var(--transition-medium);
}

@media (max-width: 1100px) {
    .keeper-chip {
        display: block;
        position: absolute;
        top: -26px;
        left: 14px;
        width: 60px;
        height: 60px;
        border-radius: 12px;
        overflow: hidden;
        border: 2px solid #D8C9A8;
        background: var(--parchment-hi);
        box-shadow: var(--shadow-medium);
        z-index: 75;
    }

    .keeper-chip img {
        width: 100%;
        height: 100%;
        object-fit: cover;
        object-position: top;
    }
}

@media (max-width: 900px) {
    /* Parchment: content-sized, pinned above the choice strip */
    .narrative-container {
        left: 2vw;
        right: 2vw;
        width: auto;
        bottom: calc(var(--space-sm) + env(safe-area-inset-bottom, 0px));
        transform: none;
        max-height: 46vh;
        padding: 10px 18px 10px;
        display: flex;
        flex-direction: column;
    }

    #story {
        flex: 0 1 auto;
        min-height: 0;
        overflow-y: auto;
    }

    /* Thumb zone: choices pinned at the sheet's bottom edge */
    #choiceStrip {
        flex: none;
        margin-top: var(--space-sm);
    }

    #choiceStrip p.choice {
        margin: 6px 0;
    }

    #choiceStrip p.choice a {
        min-height: 48px;
        padding: 8px 16px;
    }

    /* Puzzle cards compress to three columns */
    .puzzle-grid {
        grid-template-columns: repeat(3, 1fr);
        gap: 8px;
    }

    .puzzle-card span {
        font-size: 11px;
    }

    /* Collapse handle */
    .sheet-handle {
        display: block;
        height: 16px;
        margin: -4px 0 6px;
        cursor: pointer;
        position: relative;
        flex: none;
    }

    .sheet-handle::before {
        content: "";
        position: absolute;
        left: 50%;
        top: 6px;
        width: 44px;
        height: 4px;
        border-radius: 2px;
        background: rgba(61, 46, 31, 0.35);
        transform: translateX(-50%);
    }

    .narrative-container.collapsed {
        max-height: 34px;
        overflow: hidden;
        padding-bottom: 4px;
    }

    .narrative-container.collapsed #story,
    .narrative-container.collapsed #choiceStrip {
        display: none;
    }

    .narrative-container.collapsed .sheet-handle {
        height: 26px;
        margin: 0;
    }

    .narrative-container.collapsed .sheet-handle::before {
        display: none;
    }

    .narrative-container.collapsed .sheet-handle::after {
        content: "Story ▲";
        position: absolute;
        inset: 0;
        text-align: center;
        font-family: var(--font-mono);
        font-size: 12px;
        color: var(--accent);
    }

    /* Lighter scrim: scene stays visible above the sheet */
    .scrim {
        background: linear-gradient(to top,
            rgba(26, 20, 14, 0.32) 0%,
            rgba(26, 20, 14, 0.06) 45%,
            transparent 60%);
    }

    /* HUD diet */
    .fragment-progress {
        font-size: 11px;
        padding: 5px 10px;
    }

    .formula-mini {
        top: 60px;
        right: var(--space-sm);
        font-size: 10px;
        padding: 6px 8px;
        line-height: 1.6;
    }

    .formula-label {
        min-width: 38px;
    }

    .formula-slot {
        width: 20px;
        height: 20px;
    }

    .hud-buttons {
        top: var(--space-sm);
        right: var(--space-sm);
    }

    .hud-buttons button {
        width: 34px;
        height: 34px;
        font-size: 15px;
    }
}

/* === MOBILE REFINEMENTS: text-first sizing + denser choices + bust chip === */
@media (max-width: 1100px) {
    .keeper-chip {
        top: -76px;
        left: 10px;
        width: 72px;
        height: 72px;
    }
}

@media (max-width: 900px) {
    /* The text area owns its height budget — buttons never squeeze it */
    .narrative-container {
        max-height: none;
        overflow: visible; /* #story scrolls itself; keeps the keeper chip unclipped */
    }

    #story {
        max-height: 38vh;
        overflow-y: auto;
    }

    #story p {
        font-size: 15px;
    }

    /* Denser thumb-zone buttons (44px a11y floor kept) */
    #choiceStrip p.choice {
        margin: 5px 0;
    }

    #choiceStrip p.choice a {
        min-height: 44px;
        padding: 6px 14px;
        font-size: 14px;
    }
}

/* ============================================
   UNIFIED SHEET — all sizes
   Text scrolls in its own area, choices pinned at
   the sheet bottom, collapse handle everywhere,
   keeper chip as speaker marker.
   ============================================ */

.sheet-handle {
    display: block;
    height: 14px;
    margin: -8px 0 6px;
    cursor: pointer;
    position: relative;
    flex: none;
}

.sheet-handle::before {
    content: "";
    position: absolute;
    left: 50%;
    top: 5px;
    width: 36px;
    height: 4px;
    border-radius: 2px;
    background: rgba(61, 46, 31, 0.28);
    transform: translateX(-50%);
}

.sheet-handle:hover::before,
.sheet-handle:focus-visible::before {
    background: var(--accent);
}

.narrative-container.collapsed {
    max-height: 34px;
    overflow: hidden;
    padding-bottom: 4px;
}

.narrative-container.collapsed #story,
.narrative-container.collapsed #choiceStrip {
    display: none;
}

.narrative-container.collapsed .sheet-handle {
    height: 26px;
    margin: 0;
}

.narrative-container.collapsed .sheet-handle::before {
    display: none;
}

.narrative-container.collapsed .sheet-handle::after {
    content: "Story ▲";
    position: absolute;
    inset: 0;
    text-align: center;
    font-family: var(--font-mono);
    font-size: 12px;
    color: var(--accent);
}

.keeper-chip {
    display: block;
    position: absolute;
    top: -76px;
    left: 10px;
    width: 72px;
    height: 72px;
    border-radius: 12px;
    overflow: hidden;
    border: 2px solid #D8C9A8;
    background: var(--parchment-hi);
    box-shadow: var(--shadow-medium);
    z-index: 75;
}

.keeper-chip img {
    width: 100%;
    height: 100%;
    object-fit: cover;
}

p.choice a {
    padding: 8px 16px;
}

/* ============================================
   HOVER-EXPAND (desktop with real hover)
   Sheet rests collapsed; hovering expands it.
   New text briefly "peeks" it open so it is noticed.
   ============================================ */
@media (hover: hover) and (min-width: 901px) {
    .narrative-container {
        transition: max-height 0.35s ease;
    }

    .narrative-container:not(:hover):not(.peek) {
        max-height: 34px;
        overflow: hidden;
        padding-bottom: 4px;
    }

    .narrative-container:not(:hover):not(.peek) #story,
    .narrative-container:not(:hover):not(.peek) #choiceStrip {
        display: none;
    }

    .narrative-container:not(:hover):not(.peek) .sheet-handle {
        height: 26px;
        margin: 0;
    }

    .narrative-container:not(:hover):not(.peek) .sheet-handle::before {
        display: none;
    }

    .narrative-container:not(:hover):not(.peek) .sheet-handle::after {
        content: "Story ▲";
        position: absolute;
        inset: 0;
        text-align: center;
        font-family: var(--font-mono);
        font-size: 12px;
        color: var(--accent);
    }
}
