/* ==========================================================================
   loophole.dev — A Japanese-Minimal Codex of Recursion
   Palette: analogous (yellow-green to forest, two paper neutrals)
   Display: Bebas Neue · Body: Shippori Mincho · Code: JetBrains Mono
   ========================================================================== */

:root {
    --washi: #F2EFE6;   /* paper ground */
    --rice:  #E7E2D2;   /* inset cells, code background */
    --moss:  #7A8A4F;   /* primary leaf-vein, active stroke */
    --matcha:#A6B26A;   /* secondary fill, hover state */
    --cedar: #3B4A2A;   /* accent, leaf-shadow */
    --sumi:  #1B1A18;   /* body type, hairline grid */
    --stone: #2A2926;   /* secondary body type */

    --display: 'Bebas Neue', 'Inter', sans-serif;
    --body: 'Shippori Mincho', 'Lora', Georgia, serif;
    --mono: 'JetBrains Mono', 'Space Mono', monospace;

    --hex-w: clamp(220px, 22vw, 320px);
    --hex-h: calc(var(--hex-w) * 0.866);
    --gutter: -1px;

    --ease-codex: cubic-bezier(0.65, 0, 0.35, 1);
}

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

html, body {
    background: var(--washi);
    color: var(--sumi);
    font-family: var(--body);
    font-size: 15px;
    line-height: 1.78;
    letter-spacing: -0.005em;
    overflow-x: hidden;
    scroll-behavior: smooth;
}

body {
    cursor: none;
    min-height: 100vh;
    position: relative;
}

a { color: inherit; text-decoration: none; }
ul, ol { list-style: none; }
sub { font-size: 0.62em; vertical-align: sub; letter-spacing: 0; }

/* ==========================================================================
   Hex-grid watermark
   ========================================================================== */
.hex-watermark {
    position: fixed;
    inset: 0;
    pointer-events: none;
    z-index: 0;
    opacity: 0.04;
    background-image:
        radial-gradient(circle at 0% 0%, var(--sumi) 1px, transparent 1px),
        radial-gradient(circle at 50% 50%, var(--sumi) 1px, transparent 1px);
    background-size: 80px 138.56px, 80px 138.56px;
    background-position: 0 0, 40px 69.28px;
}

/* ==========================================================================
   Folio (top strip)
   ========================================================================== */
.folio {
    position: fixed;
    top: 0; left: 0; right: 0;
    height: 64px;
    z-index: 50;
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 0 clamp(20px, 4vw, 56px);
    background: var(--washi);
    border-bottom: 1px solid rgba(27, 26, 24, 0.12);
    pointer-events: none;
}

.folio-mark,
.folio-chapter {
    font-family: var(--display);
    font-size: 14px;
    letter-spacing: 0.42em;
    color: var(--cedar);
    text-transform: uppercase;
    transition: opacity 600ms var(--ease-codex);
}

.folio-mark { color: var(--sumi); }

.folio-chapter {
    flex: 1;
    text-align: center;
    color: var(--moss);
    font-size: 13px;
    letter-spacing: 0.36em;
}

.folio-icon {
    color: var(--cedar);
    display: flex;
    align-items: center;
    justify-content: center;
    width: 24px;
    height: 24px;
}

/* ==========================================================================
   Cursor leaf + ink trail
   ========================================================================== */
.cursor-leaf {
    position: fixed;
    top: 0; left: 0;
    width: 16px;
    height: 22px;
    pointer-events: none;
    z-index: 100;
    transform: translate(0, 0) rotate(0deg);
    will-change: transform;
}

.cursor-leaf svg { display: block; overflow: visible; }

.ink-trail {
    position: fixed;
    inset: 0;
    width: 100vw;
    height: 100vh;
    pointer-events: none;
    z-index: 99;
}

#ink-trail-path {
    stroke-dasharray: 1 0;
    transition: opacity 800ms var(--ease-codex);
}

/* ==========================================================================
   Leaf-rail navigation (right edge)
   ========================================================================== */
.leaf-rail {
    position: fixed;
    right: 24px;
    top: 50%;
    transform: translateY(-50%);
    z-index: 40;
    display: flex;
    align-items: center;
    pointer-events: none;
}

.leaf-rail svg {
    position: absolute;
    right: -4px;
    top: 50%;
    transform: translateY(-50%);
    opacity: 0.55;
    pointer-events: none;
}

.leaf-rail-notches {
    position: relative;
    display: flex;
    flex-direction: column;
    gap: 36px;
    padding: 24px 12px;
    pointer-events: auto;
}

.rail-notch {
    display: flex;
    align-items: center;
    gap: 12px;
    cursor: none;
    color: var(--cedar);
    font-family: var(--display);
    font-size: 10.5px;
    letter-spacing: 0.32em;
    text-transform: uppercase;
    opacity: 0.55;
    transition: opacity 480ms var(--ease-codex), color 480ms var(--ease-codex);
}

.rail-notch-dot {
    display: inline-block;
    width: 6px;
    height: 6px;
    border-radius: 50%;
    background: var(--cedar);
    opacity: 0.55;
    transition: background 480ms var(--ease-codex), transform 480ms var(--ease-codex);
}

.rail-notch-label {
    opacity: 0;
    transform: translateX(-6px);
    transition: opacity 480ms var(--ease-codex), transform 480ms var(--ease-codex);
    white-space: nowrap;
}

.rail-notch:hover .rail-notch-label,
.rail-notch.active .rail-notch-label {
    opacity: 1;
    transform: translateX(0);
}

.rail-notch.active {
    opacity: 1;
    color: var(--moss);
}

.rail-notch.active .rail-notch-dot {
    background: var(--moss);
    transform: scale(1.4);
}

/* ==========================================================================
   Codex container
   ========================================================================== */
.codex {
    position: relative;
    z-index: 1;
    padding-top: 64px;
    padding-bottom: 0;
}

/* ==========================================================================
   Chapters + chapter meta
   ========================================================================== */
.chapter {
    position: relative;
    min-height: 100vh;
    padding: clamp(80px, 10vh, 140px) clamp(28px, 6vw, 96px) clamp(80px, 10vh, 140px);
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: flex-start;
}

.chapter-meta {
    display: flex;
    align-items: baseline;
    gap: 28px;
    margin-bottom: clamp(48px, 8vh, 96px);
    align-self: flex-start;
    padding-left: clamp(20px, 5vw, 80px);
    border-left: 1px solid rgba(27, 26, 24, 0.18);
}

.chapter-numeral {
    font-family: var(--display);
    font-size: clamp(2.4rem, 6.4vw, 5.6rem);
    letter-spacing: 0.06em;
    line-height: 0.9;
    color: var(--cedar);
}

.chapter-title {
    font-family: var(--display);
    font-size: clamp(1.1rem, 1.6vw, 1.5rem);
    letter-spacing: 0.42em;
    color: var(--moss);
    text-transform: uppercase;
    padding-bottom: 6px;
}

/* ==========================================================================
   Hex grid clusters
   ========================================================================== */
.hex-cluster {
    width: 100%;
    max-width: 1320px;
    display: grid;
    grid-template-columns: repeat(auto-fit, var(--hex-w));
    gap: var(--gutter);
    justify-content: center;
    padding: 0 clamp(0px, 4vw, 40px);
}

.cluster-hero,
.cluster-bamboo {
    grid-template-columns: repeat(4, var(--hex-w));
}

.cluster-fern,
.cluster-loophole {
    grid-template-columns: repeat(3, var(--hex-w));
}

.cluster-ginkgo {
    grid-template-columns: repeat(2, var(--hex-w));
}

@media (max-width: 920px) {
    .cluster-hero, .cluster-bamboo, .cluster-fern,
    .cluster-loophole, .cluster-ginkgo {
        grid-template-columns: repeat(2, var(--hex-w));
    }
}
@media (max-width: 560px) {
    .cluster-hero, .cluster-bamboo, .cluster-fern,
    .cluster-loophole, .cluster-ginkgo {
        grid-template-columns: 1fr;
    }
    :root { --hex-w: 88vw; }
}

/* Offset every other row for honeycomb tessellation */
.hex-cluster .hex {
    width: var(--hex-w);
    height: var(--hex-h);
    position: relative;
    margin-bottom: calc(var(--hex-h) * -0.25);
}

.hex-cluster .hex:nth-child(2n) {
    transform: translateY(calc(var(--hex-h) * 0.5));
}

/* ==========================================================================
   Hex shape via clip-path
   ========================================================================== */
.hex {
    background: var(--washi);
    border: 1px solid rgba(27, 26, 24, 0.16);
    clip-path: polygon(
        25% 0%, 75% 0%,
        100% 50%,
        75% 100%, 25% 100%,
        0% 50%
    );
    opacity: 0;
    transform: translateY(12px);
    transition: opacity 720ms var(--ease-codex), transform 720ms var(--ease-codex);
    overflow: hidden;
    will-change: opacity, transform;
}

.hex.is-revealed {
    opacity: 1;
    transform: translateY(0);
}

.hex-inner {
    position: absolute;
    inset: 0;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    padding: clamp(22px, 3.2vw, 44px);
    text-align: center;
    gap: 12px;
}

.cell-index {
    position: absolute;
    bottom: 14%;
    right: 18%;
    font-family: var(--display);
    font-size: 10px;
    letter-spacing: 0.24em;
    color: var(--cedar);
    opacity: 0.5;
}

/* ==========================================================================
   Cell taxonomy
   ========================================================================== */

/* Lesson cell */
.lesson-cell { background: var(--washi); }
.lesson-cell .lesson-body {
    font-family: var(--body);
    font-size: 14px;
    line-height: 1.78;
    color: var(--stone);
    max-width: 80%;
}
.lesson-cell .lesson-snip {
    font-family: var(--mono);
    font-size: 11.5px;
    color: var(--moss);
    background: var(--rice);
    padding: 2px 8px;
    margin-top: 4px;
    letter-spacing: 0;
}

/* Code cell */
.code-cell {
    background: var(--rice);
    border-color: rgba(59, 74, 42, 0.25);
}
.code-cell .hex-inner { padding: clamp(18px, 2.8vw, 38px); }
.code-block {
    font-family: var(--mono);
    font-size: 11.5px;
    line-height: 1.62;
    color: var(--sumi);
    text-align: left;
    width: 75%;
    overflow: hidden;
    font-feature-settings: "calt" 0, "liga" 0;
}
.code-block code { font-family: inherit; }
.code-block .kw { color: var(--cedar); font-weight: 500; }
.code-block .fn { color: var(--moss); }
.code-block .str { color: var(--matcha); }
.code-block .comment { color: rgba(27,26,24,0.45); font-style: italic; }

/* Diagram cell */
.diagram-cell { background: var(--washi); }
.diagram-cell .leaf-svg {
    width: 78%;
    height: auto;
    max-width: 220px;
}

/* Aphorism cell */
.aphorism-cell {
    background: var(--washi);
    border-color: rgba(122, 138, 79, 0.45);
}
.aphorism {
    font-family: var(--display);
    font-size: clamp(1.2rem, 1.9vw, 1.7rem);
    letter-spacing: 0.18em;
    color: var(--cedar);
    text-transform: uppercase;
    line-height: 1.05;
    max-width: 80%;
}

/* Negative (ma) cell */
.ma-cell {
    background: var(--washi);
    border-color: rgba(27, 26, 24, 0.08);
}

/* Loop cell — a single ~3px gap in the border at tail-recursion point */
.loop-cell {
    background: var(--washi);
    border-color: transparent;
    position: relative;
}
.loop-cell::before {
    content: "";
    position: absolute;
    inset: 0;
    clip-path: polygon(
        25% 0%, 75% 0%,
        100% 50%,
        75% 100%, 25% 100%,
        0% 50%
    );
    border: 1px solid rgba(122, 138, 79, 0.6);
    pointer-events: none;
    /* mask one segment to create the loophole gap */
    -webkit-mask-image: conic-gradient(from 28deg at 50% 50%, #000 0deg, #000 350deg, transparent 350deg, transparent 360deg);
            mask-image: conic-gradient(from 28deg at 50% 50%, #000 0deg, #000 350deg, transparent 350deg, transparent 360deg);
}
.loop-cell::after {
    content: "";
    position: absolute;
    width: 3px; height: 3px;
    right: 8%; top: 24%;
    border-radius: 50%;
    background: var(--matcha);
    opacity: 0.85;
}
.loop-cell:hover .loophole-curl {
    stroke: var(--cedar);
}

/* ==========================================================================
   Cluster-specific layouts: bamboo / fern / loophole tweak
   ========================================================================== */
.cluster-hero { gap: 0; }
.cluster-bamboo { gap: 0; }

/* ==========================================================================
   Hero diagram special draw
   ========================================================================== */
.hero-diagram .leaf-margin,
.hero-diagram .leaf-midrib,
.hero-diagram .leaf-vein {
    stroke-dasharray: 1;
    stroke-dashoffset: 1;
}

/* ==========================================================================
   Ginkgo chapter (sticky master leaf + hex column)
   ========================================================================== */
.ginkgo-layout {
    width: 100%;
    max-width: 1320px;
    display: grid;
    grid-template-columns: minmax(260px, 1fr) minmax(0, 2fr);
    gap: clamp(28px, 4vw, 80px);
    align-items: flex-start;
}

.ginkgo-sticky {
    position: sticky;
    top: 96px;
    align-self: flex-start;
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 18px;
    padding: 12px 0;
}

.ginkgo-master {
    width: 100%;
    max-width: 320px;
    height: auto;
}

.ginkgo-half {
    transition: fill 800ms var(--ease-codex), stroke 800ms var(--ease-codex);
}

.ginkgo-half.lit {
    fill: rgba(166, 178, 106, 0.35);
    stroke: var(--moss);
}

.ginkgo-fan-vein {
    stroke-dasharray: 1;
    stroke-dashoffset: 1;
}

.ginkgo-caption {
    display: flex;
    align-items: baseline;
    gap: 10px;
    font-family: var(--display);
    color: var(--cedar);
    letter-spacing: 0.36em;
    font-size: 11px;
}
.caption-mark { font-size: 18px; color: var(--moss); }

@media (max-width: 760px) {
    .ginkgo-layout { grid-template-columns: 1fr; }
    .ginkgo-sticky { position: relative; top: 0; }
}

/* ==========================================================================
   Fern chapter
   ========================================================================== */
.fern-svg {
    width: 88%;
    height: auto;
    max-width: 240px;
}

.fern-pinna-rib,
.fern-rib {
    stroke-dasharray: 1;
    stroke-dashoffset: 1;
}

.fern-cell .fern-pinna {
    transition: opacity 800ms var(--ease-codex);
    opacity: 1;
}

.fern-cell .fern-pinna.highlight path {
    stroke: var(--cedar);
    stroke-width: 1.4;
}

/* ==========================================================================
   Pine interlude — horizontal scroll, two threads side-by-side
   ========================================================================== */
.pine-interlude {
    width: 100%;
    overflow: hidden;
    padding: clamp(40px, 6vh, 80px) 0;
}

.pine-interlude-track {
    display: grid;
    grid-template-columns: 1fr 60px 1fr;
    align-items: stretch;
    gap: 0;
    width: 100%;
    max-width: 1180px;
    margin: 0 auto;
}

.pine-thread {
    display: flex;
    flex-direction: column;
    gap: 0;
    align-items: center;
}

.pine-thread .hex {
    width: var(--hex-w);
    height: var(--hex-h);
    margin-bottom: calc(var(--hex-h) * -0.18);
}

.pine-fascicle {
    position: relative;
    display: flex;
    align-items: stretch;
    justify-content: center;
}

.pine-svg {
    height: 100%;
    width: 60px;
    min-height: 600px;
}

.pine-needle {
    stroke-dasharray: 1;
    stroke-dashoffset: 1;
}

.pine-fasc-knot,
.pine-lock {
    stroke-dasharray: 1;
    stroke-dashoffset: 1;
}

.thread-tag {
    display: inline-block;
    margin-left: 6px;
    font-size: 9px;
    color: var(--moss);
    letter-spacing: 0.24em;
}

@media (max-width: 760px) {
    .pine-interlude-track {
        grid-template-columns: 1fr;
    }
    .pine-fascicle { display: none; }
}

/* ==========================================================================
   Lotus ring
   ========================================================================== */
.lotus-ring {
    position: relative;
    width: min(90vw, 720px);
    aspect-ratio: 1 / 1;
    margin: clamp(40px, 6vh, 80px) auto;
}

.lotus-petal {
    position: absolute;
    width: clamp(140px, 18vw, 220px);
    height: calc(clamp(140px, 18vw, 220px) * 0.866);
    top: 50%; left: 50%;
    transform-origin: center;
    cursor: none;
}

.lotus-petal[data-petal="1"] { transform: translate(-50%, -50%) rotate(0deg) translateY(-200px) rotate(0deg); }
.lotus-petal[data-petal="2"] { transform: translate(-50%, -50%) rotate(45deg) translateY(-200px) rotate(-45deg); }
.lotus-petal[data-petal="3"] { transform: translate(-50%, -50%) rotate(90deg) translateY(-200px) rotate(-90deg); }
.lotus-petal[data-petal="4"] { transform: translate(-50%, -50%) rotate(135deg) translateY(-200px) rotate(-135deg); }
.lotus-petal[data-petal="5"] { transform: translate(-50%, -50%) rotate(180deg) translateY(-200px) rotate(-180deg); }
.lotus-petal[data-petal="6"] { transform: translate(-50%, -50%) rotate(225deg) translateY(-200px) rotate(-225deg); }
.lotus-petal[data-petal="7"] { transform: translate(-50%, -50%) rotate(270deg) translateY(-200px) rotate(-270deg); }
.lotus-petal[data-petal="8"] { transform: translate(-50%, -50%) rotate(315deg) translateY(-200px) rotate(-315deg); }

@media (max-width: 720px) {
    .lotus-petal[data-petal="1"] { transform: translate(-50%, -50%) rotate(0deg) translateY(-130px) rotate(0deg); }
    .lotus-petal[data-petal="2"] { transform: translate(-50%, -50%) rotate(45deg) translateY(-130px) rotate(-45deg); }
    .lotus-petal[data-petal="3"] { transform: translate(-50%, -50%) rotate(90deg) translateY(-130px) rotate(-90deg); }
    .lotus-petal[data-petal="4"] { transform: translate(-50%, -50%) rotate(135deg) translateY(-130px) rotate(-135deg); }
    .lotus-petal[data-petal="5"] { transform: translate(-50%, -50%) rotate(180deg) translateY(-130px) rotate(-180deg); }
    .lotus-petal[data-petal="6"] { transform: translate(-50%, -50%) rotate(225deg) translateY(-130px) rotate(-225deg); }
    .lotus-petal[data-petal="7"] { transform: translate(-50%, -50%) rotate(270deg) translateY(-130px) rotate(-270deg); }
    .lotus-petal[data-petal="8"] { transform: translate(-50%, -50%) rotate(315deg) translateY(-130px) rotate(-315deg); }
}

.petal-inner {
    display: flex;
    align-items: center;
    justify-content: center;
    flex-direction: column;
    gap: 4px;
}

.petal-svg {
    width: 60%;
    height: auto;
}

.petal-path {
    stroke-dasharray: 1;
    stroke-dashoffset: 1;
}

.petal-mark {
    font-family: var(--display);
    font-size: 12px;
    letter-spacing: 0.24em;
    color: var(--cedar);
}

.lotus-petal:hover .petal-path {
    stroke: var(--cedar);
}

.lotus-center {
    position: absolute;
    width: clamp(170px, 22vw, 260px);
    height: calc(clamp(170px, 22vw, 260px) * 0.866);
    top: 50%; left: 50%;
    transform: translate(-50%, -50%);
    background: var(--rice);
    border: 1px solid rgba(59,74,42,0.3);
    clip-path: polygon(
        25% 0%, 75% 0%,
        100% 50%,
        75% 100%, 25% 100%,
        0% 50%
    );
    opacity: 1;
}
.lotus-center .hex-inner { padding: clamp(20px, 3vw, 36px); }
.lotus-code { width: 90%; }

.lotus-aside {
    text-align: center;
    margin: clamp(20px, 4vh, 56px) auto 0;
    max-width: 520px;
    padding: 0 24px;
}

.lotus-aside-text {
    font-family: var(--body);
    font-size: 16px;
    color: var(--stone);
    font-weight: 500;
    line-height: 1.7;
}

/* ==========================================================================
   Loophole chapter — the closing curl
   ========================================================================== */
.loophole-svg {
    width: 88%;
    height: auto;
    max-width: 240px;
}

.loophole-curl {
    stroke-dasharray: 1;
    stroke-dashoffset: 1;
}

/* Codex footer (enso + folio-end) */
.codex-footer {
    margin-top: clamp(60px, 10vh, 120px);
    width: 100%;
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 28px;
    padding-bottom: clamp(60px, 10vh, 120px);
}

.enso-cell {
    width: clamp(120px, 16vw, 180px);
    height: clamp(120px, 16vw, 180px);
}

.enso-svg {
    width: 100%;
    height: 100%;
}

#enso-path {
    stroke-dasharray: 1;
    stroke-dashoffset: 1;
}

.folio-end {
    font-family: var(--display);
    font-size: 11px;
    letter-spacing: 0.42em;
    color: var(--cedar);
    text-transform: uppercase;
}

/* ==========================================================================
   Fade-in stagger handled in JS by setting --reveal-delay
   ========================================================================== */
.hex {
    transition-delay: var(--reveal-delay, 0ms);
}

/* ==========================================================================
   Hide cursor across interactive elements (we draw our own)
   ========================================================================== */
a, button, input, textarea, select {
    cursor: none;
}

/* ==========================================================================
   Print-codex flourishes (small details)
   ========================================================================== */
.chapter::after {
    content: "";
    display: block;
    width: 1px;
    height: 32px;
    margin: clamp(40px, 6vh, 80px) auto 0;
    background: rgba(27, 26, 24, 0.18);
}

.chapter:last-of-type::after { display: none; }
