/* ============================================================
   kakuritsu.com — 確率
   Burnt-Orange Candlelight Palette
   Split-screen as probability axis (p | 1−p)
   ============================================================ */

:root {
    --ash: #0e0a08;
    --shadow: #1a1310;
    --seam: #2b1d15;
    --ember: #cc4a1b;
    --persimmon: #e26a2c;
    --lantern: #f4a261;
    --rice-paper: #f6ecd9;
    --smoke: #7a6855;

    --tempo: 0.65;
    --flip-duration: calc(1.4s / var(--tempo));

    --font-mono: 'JetBrains Mono', 'IBM Plex Mono', 'Space Mono', ui-monospace, monospace;
    --font-kanji: 'Klee One', 'JetBrains Mono', serif;
}

* {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
}

html, body {
    width: 100%;
    height: 100%;
    overflow: hidden;
}

body {
    font-family: var(--font-mono);
    color: var(--rice-paper);
    background-color: var(--ash);
    background-image: radial-gradient(ellipse at 50% 110%, #2b1d15 0%, #0e0a08 70%);
    -webkit-font-smoothing: antialiased;
    -moz-osx-font-smoothing: grayscale;
    line-height: 1.85;
    font-size: 16px;
    letter-spacing: 0;
    user-select: none;
    cursor: default;
}

.svg-defs {
    position: absolute;
    width: 0;
    height: 0;
    pointer-events: none;
}

/* Candle-atmospheric layer — bottom 30vh of viewport */
.candle-layer {
    position: fixed;
    left: 0;
    bottom: 0;
    width: 100%;
    height: 30vh;
    pointer-events: none;
    z-index: 1;
    background: linear-gradient(
        to top,
        rgba(204, 74, 27, 0.10) 0%,
        rgba(226, 106, 44, 0.06) 30%,
        rgba(244, 162, 97, 0.025) 60%,
        rgba(244, 162, 97, 0) 100%
    );
    filter: url(#candle-flicker);
    mix-blend-mode: screen;
    opacity: 0.85;
    transition: opacity 1.2s ease;
}

.candle-layer.calm {
    opacity: 0.6;
}

/* ----- Stage and split halves ----- */

.stage {
    position: fixed;
    inset: 0;
    width: 100%;
    height: 100%;
    z-index: 2;
}

.half {
    position: absolute;
    top: 0;
    width: 50vw;
    height: 100vh;
    perspective: 1600px;
    overflow: hidden;
    background: var(--shadow);
}

.half-left  { left: 0; }
.half-right { left: 50vw; }

.flipper {
    position: absolute;
    inset: 0;
    transform-style: preserve-3d;
    transition: transform var(--flip-duration) cubic-bezier(0.65, 0, 0.15, 1);
    transform: rotateY(0deg);
    will-change: transform;
}

.flipper.flipping {
    /* set inline; class hook only */
}

.panel {
    position: absolute;
    inset: 0;
    width: 100%;
    height: 100%;
    backface-visibility: hidden;
    -webkit-backface-visibility: hidden;
    background: var(--shadow);
    overflow: hidden;
    padding: clamp(1.4rem, 3vh, 2.6rem);
    display: flex;
    flex-direction: column;
}

.panel-front { transform: rotateY(0deg); }
.panel-back  { transform: rotateY(180deg); }

/* Doubled hairline frame as in 1970s math journals */
.panel::before,
.panel::after {
    content: "";
    position: absolute;
    pointer-events: none;
    border: 0.5px solid var(--seam);
}
.panel::before {
    inset: clamp(0.6rem, 1.2vh, 1rem);
    border-width: 0.5px;
}
.panel::after {
    inset: calc(clamp(0.6rem, 1.2vh, 1rem) - 4px);
    border-width: 1.5px;
    border-color: var(--seam);
}

/* ----- Scene shell ----- */

.scene-shell {
    position: relative;
    width: 100%;
    height: 100%;
    display: flex;
    flex-direction: column;
    padding: clamp(1.4rem, 3vh, 2.4rem) clamp(1.4rem, 3vw, 2.4rem);
    justify-content: space-between;
    gap: 0.8rem;
}

.scene-title {
    font-family: var(--font-mono);
    font-weight: 300;
    font-size: clamp(2.2rem, 5.5vw, 4.8rem);
    letter-spacing: -0.02em;
    line-height: 1.05;
    color: var(--rice-paper);
    z-index: 3;
}
.scene-title .ttl-sm {
    display: block;
    font-size: 0.32em;
    letter-spacing: 0.4em;
    color: var(--smoke);
    text-transform: uppercase;
    font-weight: 400;
    margin-bottom: 0.6em;
}
.scene-title .ttl-num {
    color: var(--ember);
    font-weight: 200;
}

.scene-canvas-wrap {
    flex: 1 1 auto;
    position: relative;
    width: 100%;
    min-height: 0;
    display: flex;
    align-items: center;
    justify-content: center;
}

.scene-canvas-wrap canvas,
.scene-canvas-wrap svg {
    max-width: 100%;
    max-height: 100%;
    display: block;
}

.scene-caption {
    font-family: var(--font-mono);
    font-size: 0.95rem;
    font-weight: 400;
    line-height: 1.85;
    color: var(--rice-paper);
    max-width: 28ch;
    white-space: pre-line;
}

.scene-footnote {
    font-family: var(--font-mono);
    font-size: 0.78rem;
    font-weight: 300;
    color: var(--smoke);
    font-feature-settings: "frac" 1, "tnum" 1, "zero" 1;
    letter-spacing: 0.02em;
}

/* Right-side panels: align caption to right margin so the seam is the binding spine */
.half-right .scene-shell { align-items: flex-end; text-align: right; }
.half-right .scene-caption { text-align: right; }
.half-left  .scene-shell { align-items: flex-start; }

/* ----- The seam ----- */

.seam {
    position: fixed;
    left: 50%;
    top: 0;
    transform: translateX(-50%);
    width: 1px;
    height: 100vh;
    background: var(--seam);
    z-index: 100;
    pointer-events: none;
    transition: width 4s cubic-bezier(0.65, 0, 0.15, 1), background 4s ease;
}

.seam-dots {
    position: absolute;
    inset: 0;
    pointer-events: none;
    overflow: visible;
}
.seam-dots .seam-dot {
    position: absolute;
    left: 50%;
    transform: translate(-50%, -50%);
    color: var(--smoke);
    font-family: var(--font-mono);
    font-size: 0.8rem;
    line-height: 1;
}

.seam-kanji {
    position: absolute;
    left: 50%;
    top: 8vh;
    transform: translate(-50%, 0);
    font-family: var(--font-kanji);
    font-weight: 600;
    color: var(--ember);
    font-size: 1.1rem;
    letter-spacing: 0.1em;
    writing-mode: vertical-rl;
    text-orientation: upright;
    pointer-events: none;
    text-shadow: 0 0 12px rgba(204, 74, 27, 0.3);
    opacity: 0.92;
}

.seam-decimal-wrap {
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);
    pointer-events: none;
    text-align: center;
    width: max-content;
}

.seam-decimal {
    display: block;
    font-family: var(--font-mono);
    font-weight: 100;
    font-size: clamp(3.2rem, 9vw, 7.5rem);
    color: var(--ember);
    font-variant-numeric: tabular-nums slashed-zero;
    font-feature-settings: "tnum" 1, "zero" 1;
    line-height: 1;
    letter-spacing: -0.01em;
    text-shadow: 0 0 24px rgba(204, 74, 27, 0.35), 0 0 8px rgba(204, 74, 27, 0.5);
    transition: color 600ms ease;
}

.seam-label {
    font-family: var(--font-mono);
    font-size: 0.78rem;
    color: var(--smoke);
    margin-top: 0.4rem;
    letter-spacing: 0.4em;
    text-transform: lowercase;
}

.seam-gauge {
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%, calc(50% + clamp(3.2rem, 9vw, 7.5rem) * 0.8 + 1.6rem));
    display: flex;
    flex-direction: column;
    gap: 1rem;
    pointer-events: auto;
    align-items: center;
}

/* Place gauge below decimal; reposition for clarity */
.seam-gauge {
    top: auto;
    bottom: 12vh;
    transform: translate(-50%, 0);
    flex-direction: column;
    gap: 0.55rem;
}

.gauge-dot {
    position: relative;
    width: 14px;
    height: 14px;
    background: transparent;
    border: 1px solid var(--seam);
    border-radius: 50%;
    cursor: pointer;
    pointer-events: auto;
    padding: 0;
    transition: background 400ms ease, border-color 400ms ease, transform 400ms ease;
    color: var(--smoke);
}
.gauge-dot:hover {
    border-color: var(--persimmon);
    transform: scale(1.2);
}
.gauge-dot.active {
    background: var(--ember);
    border-color: var(--ember);
    box-shadow: 0 0 10px rgba(204, 74, 27, 0.6);
}
.gauge-dot .dot-number {
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);
    font-size: 0.55rem;
    font-family: var(--font-mono);
    color: var(--rice-paper);
    opacity: 0;
    transition: opacity 200ms ease;
    pointer-events: none;
}
.gauge-dot.active .dot-number,
.gauge-dot:hover .dot-number {
    opacity: 0.85;
}

.seam-controls {
    position: absolute;
    left: 50%;
    bottom: 4vh;
    transform: translateX(-50%);
    display: flex;
    flex-direction: column;
    gap: 0.6rem;
    pointer-events: auto;
}
.seam-glyph {
    background: transparent;
    border: none;
    color: var(--smoke);
    font-family: var(--font-mono);
    font-size: 1.3rem;
    width: 26px;
    height: 26px;
    cursor: pointer;
    padding: 0;
    transition: color 300ms ease, transform 300ms ease;
    line-height: 1;
}
.seam-glyph:hover {
    color: var(--persimmon);
    transform: scale(1.15);
}

/* ----- Seam dissolve overlay (final scene) ----- */
.seam-dissolve {
    position: fixed;
    left: 50%;
    top: 0;
    transform: translateX(-50%);
    width: 0;
    height: 100vh;
    background: radial-gradient(ellipse at 50% 60%, rgba(204, 74, 27, 0.18) 0%, rgba(14, 10, 8, 0) 70%);
    z-index: 90;
    pointer-events: none;
    transition: width 4s cubic-bezier(0.65, 0, 0.15, 1), opacity 4s ease;
    opacity: 0;
}
.seam-dissolve.active {
    width: 100vw;
    opacity: 1;
}

/* When dissolved, the seam itself becomes invisible */
body.dissolved .seam { background: transparent; }
body.dissolved .panel::before,
body.dissolved .panel::after { border-color: transparent; transition: border-color 3s ease; }

/* ----- Scene-specific helpers ----- */

/* Coin (svg) — strokes are --ember, fills none */
.coin-svg {
    width: clamp(180px, 50%, 360px);
    aspect-ratio: 1 / 1;
    display: block;
    filter: drop-shadow(0 0 12px rgba(204, 74, 27, 0.18));
}
.coin-svg circle.coin-edge {
    fill: none;
    stroke: var(--ember);
    stroke-width: 1.5;
}
.coin-svg .coin-face {
    transform-origin: center;
}
.coin-svg text.coin-glyph {
    font-family: var(--font-kanji);
    font-size: 96px;
    fill: var(--ember);
    text-anchor: middle;
    dominant-baseline: central;
    font-weight: 600;
}
.coin-svg .coin-rim {
    fill: none;
    stroke: var(--seam);
    stroke-width: 0.5;
}

/* Die unfolded net */
.die-net-svg, .die-roll-svg {
    width: clamp(220px, 78%, 460px);
    aspect-ratio: 1 / 1;
    display: block;
}
.die-net-svg .face, .die-roll-svg .face {
    fill: none;
    stroke: var(--ember);
    stroke-width: 1.5;
}
.die-net-svg .pip, .die-roll-svg .pip {
    fill: var(--ember);
    stroke: none;
}
.die-roll-svg .face-shadow {
    fill: var(--seam);
    stroke: none;
    opacity: 0.5;
}

/* Urn */
.urn-svg {
    width: clamp(220px, 78%, 380px);
    height: auto;
    display: block;
}
.urn-svg path, .urn-svg rect, .urn-svg ellipse {
    fill: none;
    stroke: var(--ember);
    stroke-width: 1.5;
}
.urn-svg .pebble-w {
    fill: var(--rice-paper);
    stroke: var(--smoke);
    stroke-width: 0.5;
}
.urn-svg .pebble-b {
    fill: var(--seam);
    stroke: var(--ember);
    stroke-width: 0.5;
}

.urn-counts {
    margin-top: 0.5rem;
    font-size: 0.78rem;
    color: var(--smoke);
    font-feature-settings: "tnum" 1, "zero" 1;
    text-align: left;
    width: 100%;
    max-width: 24ch;
}
.half-right .urn-counts { text-align: right; align-self: flex-end; }

/* Bayesian conditional readout (right half scene 3) */
.bayes-formula {
    font-size: clamp(0.9rem, 1.4vw, 1.05rem);
    color: var(--rice-paper);
    line-height: 1.9;
    font-feature-settings: "tnum" 1, "frac" 1, "zero" 1;
    text-align: right;
    width: 100%;
}
.bayes-formula .row { display: block; }
.bayes-formula .lhs { color: var(--smoke); }
.bayes-formula .rhs { color: var(--ember); }
.bayes-formula .frac {
    display: inline-block;
    text-align: center;
    vertical-align: middle;
    line-height: 1.05;
    margin: 0 0.2em;
}
.bayes-formula .frac .num,
.bayes-formula .frac .den {
    display: block;
    padding: 0 0.3em;
}
.bayes-formula .frac .num {
    border-bottom: 0.5px solid var(--smoke);
}

/* Monty Hall doors */
.doors-wrap {
    display: flex;
    gap: 1.4rem;
    align-items: stretch;
    justify-content: center;
    width: 100%;
    height: 100%;
    padding: 0;
}
.door {
    flex: 1 1 0;
    background: transparent;
    border: 1.5px solid var(--ember);
    border-radius: 2px;
    cursor: pointer;
    position: relative;
    transition: border-color 600ms ease, background 600ms ease, transform 600ms ease, opacity 600ms ease;
    color: var(--ember);
    font-family: var(--font-mono);
    padding: 1rem 0.4rem;
    display: flex;
    flex-direction: column;
    justify-content: space-between;
    align-items: center;
    min-width: 0;
}
.door .door-num {
    font-size: 0.72rem;
    color: var(--smoke);
    letter-spacing: 0.4em;
}
.door .door-knob {
    width: 6px;
    height: 6px;
    border-radius: 50%;
    background: var(--ember);
    margin-top: 0.6em;
    box-shadow: 0 0 8px rgba(204,74,27,0.6);
}
.door .door-glyph {
    font-family: var(--font-kanji);
    font-size: clamp(1.4rem, 3vw, 2.2rem);
    color: var(--ember);
    margin: auto 0;
}
.door:hover { border-color: var(--persimmon); background: rgba(226, 106, 44, 0.04); }
.door.open {
    border-style: dashed;
    opacity: 0.4;
    color: var(--smoke);
}
.door.open .door-glyph { color: var(--smoke); }
.door.flicker .door-knob {
    animation: flicker 1.4s ease-in-out infinite;
}
@keyframes flicker {
    0%, 100% { opacity: 0.5; }
    50%      { opacity: 1; }
}

/* Calendar (Birthday) */
.cal-svg {
    width: 100%;
    height: 100%;
    max-height: 60vh;
    max-width: 100%;
}
.cal-svg .cal-cell {
    fill: var(--shadow);
    stroke: var(--seam);
    stroke-width: 0.5;
}
.cal-svg .cal-cell.collide {
    fill: var(--ember);
    stroke: var(--persimmon);
}
.cal-svg .cal-cell.collide-strong {
    fill: var(--persimmon);
    stroke: var(--lantern);
}

/* Silhouettes (right half scene 5) */
.silhouettes-wrap {
    width: 100%;
    height: 100%;
    position: relative;
    display: flex;
    align-items: flex-end;
    justify-content: center;
    overflow: hidden;
}

/* Walk canvases */
.walk-canvas {
    background: transparent;
    width: 100%;
    height: 100%;
    max-height: 60vh;
}

/* Galton (scene 7) */
.galton-canvas, .bell-canvas {
    background: transparent;
    width: 100%;
    height: 100%;
    max-height: 70vh;
    display: block;
}

/* Limit scene final decimal */
.limit-final {
    width: 100%;
    height: 100%;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 0.8rem;
}
.limit-final .word {
    color: var(--smoke);
    font-size: 0.85rem;
    letter-spacing: 0.5em;
    text-transform: uppercase;
}
.limit-final .word.kanji {
    font-family: var(--font-kanji);
    font-size: 1.4rem;
    letter-spacing: 0.4em;
    color: var(--ember);
    text-transform: none;
}

/* Number readouts on the panels */
.scene-readout {
    font-family: var(--font-mono);
    color: var(--ember);
    font-size: clamp(1.2rem, 2.4vw, 1.7rem);
    font-weight: 300;
    font-feature-settings: "tnum" 1, "zero" 1;
    letter-spacing: 0.04em;
    line-height: 1.4;
}
.scene-readout .lbl {
    color: var(--smoke);
    font-size: 0.6em;
    letter-spacing: 0.4em;
    margin-right: 0.6em;
    text-transform: uppercase;
}

/* Klee One inline kanji helper */
.kj { font-family: var(--font-kanji); color: var(--ember); }

/* Subtle soft pulse used in places — only on the live decimal crossing 0.5 */
.seam-decimal.equipoise {
    color: var(--lantern);
    text-shadow: 0 0 36px rgba(244, 162, 97, 0.65), 0 0 10px rgba(244, 162, 97, 0.6);
}

/* ----- Responsive: the design is desktop-first; on narrow screens the seam thickens slightly */
@media (max-width: 720px) {
    .seam { width: 1px; }
    .scene-title { font-size: clamp(1.6rem, 8vw, 3rem); }
    .scene-caption { font-size: 0.82rem; }
    .seam-decimal { font-size: clamp(2rem, 11vw, 4.5rem); }
    .seam-kanji { font-size: 0.8rem; top: 4vh; }
    .seam-controls { bottom: 2vh; }
    .seam-gauge { bottom: 8vh; }
}

/* In dissolution mode, hide all panel content except the seam decimal and bg */
body.dissolved .panel-inner > .scene-shell .scene-title,
body.dissolved .panel-inner > .scene-shell .scene-caption,
body.dissolved .panel-inner > .scene-shell .scene-footnote,
body.dissolved .panel-inner > .scene-shell .scene-canvas-wrap {
    transition: opacity 3s ease;
    opacity: 0;
}
body.dissolved .seam-gauge,
body.dissolved .seam-controls,
body.dissolved .seam-kanji {
    transition: opacity 2s ease;
    opacity: 0.15;
}
body.dissolved .seam-decimal {
    color: var(--lantern);
    text-shadow: 0 0 80px rgba(244, 162, 97, 0.5), 0 0 30px rgba(244, 162, 97, 0.7);
}
