* {
    box-sizing: border-box;
}

html,
body {
    -webkit-text-size-adjust: 100%; /* iOS 가로 회전 때 글자 자동 확대 방지 */
    text-size-adjust: 100%;
    -webkit-tap-highlight-color: transparent; /* 모바일 탭할 때 회색 박스 */
    margin: 0;
    padding: 0;
    height: 100%;
    background: #1b1c1f;
    overflow: hidden;
    font-family:
        system-ui,
        -apple-system,
        'Segoe UI',
        'XH KR',
        sans-serif;
}

body {
    display: flex;
    align-items: center;
    justify-content: center;
}

/* 모바일 세로 비율 뷰포트. 양옆(또는 위아래) 빈 공간은 body 배경색이 그대로 보임 */
#viewport {
    position: relative;
    height: 100vh; /* dvh 를 모르는 브라우저용 */
    height: 100dvh;
    aspect-ratio: 9 / 19.5;
    max-width: 100vw;
    max-height: 100vh;
    max-height: 100dvh;
    background: #000;
    overflow: hidden;
    box-shadow: 0 0 40px rgba(0, 0, 0, 0.6);
}

#scene {
    position: absolute;
    inset: 0;
    width: 100%;
    height: 100%;
    display: block;
    touch-action: none;
}

/* 화면 아래쪽 조작 안내 인디케이터 */
#hint {
    position: absolute;
    left: 0;
    right: 0;
    bottom: calc(14px + env(safe-area-inset-bottom, 0px));
    padding: 0 14px;
    text-align: center;
    color: rgba(255, 255, 255, 0.55);
    font-size: 11px;
    line-height: 1.35;
    letter-spacing: 0.02em;
    pointer-events: none;
}

/* 실제 유저용 액션 버튼 (개발자 GUI와 별개) — 화면 상단 중앙 */
#spread-btn {
    position: absolute;
    top: calc(16px + env(safe-area-inset-top, 0px));
    left: 50%;
    transform: translateX(-50%);
    z-index: 4;
    min-width: 110px;
    /* Start / Shuffle / Exit 로 글자가 바뀔 때 폭이 출렁이지 않게 */
    padding: 10px 20px;
    border: none;
    outline: none;
    border-radius: 3px;
    background: rgba(255, 255, 255, 0.12);
    -webkit-backdrop-filter: blur(6px);
    backdrop-filter: blur(6px);
    color: #fff;
    font-size: 14px;
    font-weight: 600;
    letter-spacing: 0.02em;
    cursor: pointer;
}

#spread-btn:active {
    background: rgba(255, 255, 255, 0.24);
}

#spread-btn:focus,
#spread-btn:focus-visible {
    outline: none;
}

/* 섞기 시퀀스가 도는 동안 잠김 — 눌러도 아무 일 없는 것처럼 보이지 않게 상태를 표시 */
#spread-btn:disabled {
    opacity: 0.45;
    cursor: default;
}

/* 로딩 오버레이: 모델/텍스처가 전부 준비될 때까지 씬을 완전히 가림.
   main.js가 로드 완료 후 .hidden 을 붙여 페이드아웃시키고 DOM에서 제거함 */
#loading {
    position: absolute;
    inset: 0;
    z-index: 10;
    background: #000;
    display: flex;
    align-items: center;
    justify-content: center;
    opacity: 1;
    transition: opacity 0.35s ease;
}

#loading.hidden {
    opacity: 0;
    pointer-events: none;
}

.loading-spinner {
    width: 26px;
    height: 26px;
    border: 2px solid rgba(255, 255, 255, 0.18);
    border-top-color: rgba(255, 255, 255, 0.75);
    border-radius: 50%;
    animation: loading-spin 0.9s linear infinite;
}

@keyframes loading-spin {
    to {
        transform: rotate(360deg);
    }
}

/* 화면 아래 묶음: 재생 버튼 / 안내·리딩 박스 / Change the word — 박스 높이가 바뀌어도 위아래로 알아서 쌓임 */
#dock {
    position: absolute;
    bottom: calc(9% + env(safe-area-inset-bottom, 0px));
    left: 50%;
    z-index: 3;
    display: flex;
    flex-direction: column;
    gap: 12px;
    width: min(86%, 380px);
    transform: translateX(-50%);
    pointer-events: none;
}
/* 리딩 화면: 아래 버튼까지 들어가도록 조금 내림 */
#dock.is-reading {
    bottom: calc(4% + env(safe-area-inset-bottom, 0px));
}

/* 안내·리딩 박스: 박스는 바로 나타나고 글자는 main.js가 한 자씩 채움.
   카드가 뒤로 지나가도록 배경을 검정으로 채움 */
#prompt {
    display: none;
    min-height: calc(1.4em + 1.8em + 2px);
    /* 글자가 비어 있어도 박스 높이 유지 */
    margin: 0;
    padding: 0.9em 1em;
    font:
        500 14px/1.4 'Helvetica Neue',
        Helvetica,
        Arial,
        'XH KR',
        sans-serif;
    color: #8689c3;
    text-align: center;
    white-space: nowrap;
    background: #000;
    border: 1px solid rgba(134, 137, 195, 0.6);
}
/* 글자는 박스 세로 가운데에서 시작 — 타이핑으로 줄이 늘면 위아래로 고르게 퍼지며 가운데를 유지
     (리딩 문안은 박스 높이를 전체 문안 기준으로 미리 잡아 두므로, 첫 줄이 박스 가운데에서 시작함) */
#prompt.is-on {
    display: flex;
    flex-direction: column;
    justify-content: center;
}
/* 카드 트랙 음원이 도는 동안: 박스 배경이 재생 바처럼 왼쪽부터 채워짐 (--progress 0~1, main.js가 매 프레임 갱신) */
#prompt.is-charging {
    background: linear-gradient(to right, rgba(134, 137, 195, 0.3) calc(var(--progress, 0) * 100%), #000 calc(var(--progress, 0) * 100%));
}
/* ♫ 로 트랙을 듣는 동안: 윗변이 재생 바처럼 왼쪽부터 채워짐 (--track 0~1, main.js가 갱신) */
#prompt.is-track {
    background:
        linear-gradient(#8689c3, #8689c3) top left / calc(var(--track, 0) * 100%) 3px no-repeat,
        #000;
}
/* 재생 바가 보이는 동안 박스를 누르거나 끌어서 트랙 위치를 옮김 (main.js) */
#prompt.is-charging,
#prompt.is-track {
    pointer-events: auto;
    touch-action: none;
    user-select: none;
}
/* 리딩 문안: 여러 줄로 감싸고 조금 작게 */
#prompt.is-reading {
    font-size: 14px;
    line-height: 1.55;
    white-space: normal;
    word-break: keep-all;
}

/* 카드 트랙 음원 조작 묶음 (재생/일시정지 + 건너뛰기) — EN/KR 사이 가운데에 한 덩어리로 */
#track-bar {
    display: flex;
    align-items: center;
    gap: 5px;
}
/* 다시 듣기는 리딩 화면에서만 (main.js가 data-mode 를 charge / reading 으로 바꿈) */
#track-bar:not([data-mode='reading']) [data-act='replay'] {
    display: none;
}

/* 리딩 재생 버튼 ▶ ❚❚ / 카드 음원 건너뛰기 ⏭ */
#reading-controls,
#track-controls {
    display: none;
    justify-content: center;
    gap: 5px;
    color: #8689c3;
    pointer-events: auto;
}
#reading-controls.is-on,
#track-controls.is-on {
    display: flex;
}
/* EN/KR · Track Preview 와 같은 동그란 테두리 버튼 */
#reading-controls button,
#track-controls button {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 34px;
    height: 34px;
    padding: 0;
    color: #8689c3;
    background: #000;
    border: 1px solid rgba(134, 137, 195, 0.6);
    border-radius: 99999px;
    cursor: pointer;
}
#reading-controls button svg,
#track-controls button svg {
    display: block;
    width: 16px;
    height: 16px;
    fill: currentColor;
}
/* 재생/일시정지 겸용 버튼: 재생 중이면 일시정지 아이콘 */
#reading-controls button .icon-pause,
#track-controls button .icon-pause,
#reading-controls button.is-playing .icon-play,
#track-controls button.is-playing .icon-play {
    display: none;
}
#reading-controls button.is-playing .icon-pause,
#track-controls button.is-playing .icon-pause {
    display: inline;
}

/* 1단계로 고른 카드: 다시 고르기 / 이 카드로 */
#pick-choice {
    display: none;
    gap: 8px;
    pointer-events: auto;
}
#pick-choice.is-on {
    display: flex;
    animation: dock-fade-in 0.5s ease both;
}
#pick-choice button {
    flex: 1;
    padding: 0.95em 0.6em;
    font:
        600 14px/1.2 'Helvetica Neue',
        Helvetica,
        Arial,
        'XH KR',
        sans-serif;
    color: #8689c3;
    background: #000;
    border: 1px solid rgba(134, 137, 195, 0.6);
    border-radius: 0;
    cursor: pointer;
    word-break: keep-all;
}
#pick-choice button[data-act='yes'] {
    color: #000;
    background: #8689c3;
    border-color: #8689c3;
}

/* 키워드 다시 고르기 */
#change-word {
    display: none;
    padding: 0.95em 1em;
    font:
        600 14px/1 'Helvetica Neue',
        Helvetica,
        Arial,
        'XH KR',
        sans-serif;
    color: #000;
    background: #8689c3;
    border: 0;
    border-radius: 0;
    cursor: pointer;
    pointer-events: auto;
}
/* 뒤집은 카드 화면에선 처음부터 흐리게 보이되 누를 수 없음 — 리딩이 끝나면(is-on) 또렷해지며 눌림 */
#dock.is-reading #change-word {
    display: block;
    opacity: 0.3;
    pointer-events: none;
    cursor: default;
    transition: opacity 0.6s ease;
}
#change-word.is-on,
#dock.is-reading #change-word.is-on {
    display: block;
    opacity: 1;
    pointer-events: auto;
    cursor: pointer;
}

@keyframes dock-fade-in {
    from {
        opacity: 0;
        transform: translateY(6px);
    }
    to {
        opacity: 1;
        transform: none;
    }
}

/* 뒤집은 카드 도구 (이미지 저장. 정면으로 보기는 카드 탭) — 위쪽 문장(What's your + 키워드) 바로 아래 */
#card-tools {
    position: absolute;
    top: calc(104px + env(safe-area-inset-top, 0px));
    left: 50%;
    z-index: 3;
    display: flex;
    gap: 14px;
    transform: translateX(-50%);
    pointer-events: none;
}
#card-tools button {
    display: none;
    width: 34px;
    height: 34px;
    padding: 5px;
    color: #8689c3;
    background: none;
    border: 0;
    cursor: pointer;
    pointer-events: auto;
}
#card-tools button.is-on {
    display: block;
}
#card-tools button svg {
    display: block;
    width: 100%;
    height: 100%;
    fill: currentColor;
}

/* 박스 위 한 줄: [EN KR] 왼쪽 · ♪ 가운데 · [재생/다시 듣기] 오른쪽 (가운데가 늘 정가운데에 오도록 3칸 격자) */
#lang-toggle {
    display: none;
    grid-template-columns: 1fr auto 1fr;
    align-items: center;
    pointer-events: auto;
    width: 100%;
    font:
        600 12px/1 'Helvetica Neue',
        Helvetica,
        Arial,
        'XH KR',
        sans-serif;
}
/* (안내 박스가 보일 때 — :has() 를 못 읽는 브라우저가 있어 main.js가 #dock 에 has-prompt 를 붙임) */
#dock.has-prompt #lang-toggle {
    display: grid;
}
#lang-toggle .lang-group {
    display: flex;
    gap: 6px;
    justify-self: start;
}
/* EN/KR · ♪ — 정원 버튼 */
#lang-toggle button[data-lang],
#lang-toggle #track-open {
    width: 34px;
    height: 34px;
    padding: 0;
    font: inherit;
    color: #8689c3;
    background: #000;
    border: 1px solid rgba(134, 137, 195, 0.6);
    border-radius: 99999px;
    cursor: pointer;
}
#lang-toggle button[data-lang].is-active,
#lang-toggle #track-open.is-active {
    color: #000;
    background: #8689c3;
}
#lang-toggle .track-wrap {
    position: relative;
    grid-column: 2;
}
/* 트랙 볼륨 — ♫ 위로 세로 바. 트랙 재생 중에만 (main.js가 is-on), --vol 0~1 */
#lang-toggle .track-volume {
    position: absolute;
    bottom: calc(100% + 12px);
    left: 50%;
    width: 3px;
    height: 84px;
    background: rgba(134, 137, 195, 0.25);
    cursor: pointer;
    touch-action: none;
    /* 나타날 때·사라질 때 모두 서서히 (사라지는 동안엔 눌리지 않게) */
    opacity: 0;
    visibility: hidden;
    pointer-events: none;
    transform: translate(-50%, 6px);
    transition:
        opacity 0.3s ease,
        transform 0.3s ease,
        visibility 0s linear 0.3s;
}
#lang-toggle .track-volume.is-on {
    opacity: 1;
    visibility: visible;
    pointer-events: auto;
    transform: translate(-50%, 0);
    transition:
        opacity 0.3s ease,
        transform 0.3s ease,
        visibility 0s;
}
/* 손가락으로 잡기 쉽게 좌우로 누를 수 있는 범위를 넓힘 */
#lang-toggle .track-volume::before {
    position: absolute;
    inset: -6px -14px;
    content: '';
}
#lang-toggle .track-volume .tv-fill {
    position: absolute;
    bottom: 0;
    width: 100%;
    height: calc(var(--vol, 0.9) * 100%);
    background: #8689c3;
}
#lang-toggle .track-volume .tv-knob {
    position: absolute;
    bottom: calc(var(--vol, 0.9) * 100% - 5px);
    left: 50%;
    width: 10px;
    height: 10px;
    background: #8689c3;
    border-radius: 99999px;
    transform: translateX(-50%);
}
#lang-toggle #track-open {
    visibility: hidden;
}
/* 리딩 화면에서만 (자리는 늘 차지해서 줄이 흔들리지 않게) */
#lang-toggle #track-open.is-on {
    visibility: visible;
}
/* 트랙 재생 중 */
#lang-toggle #track-open.is-playing {
    color: #000;
    background: #8689c3;
}
#lang-toggle #track-open svg {
    display: block;
    flex: none;
    width: 16px;
    height: 16px;
    fill: currentColor;
}
/* ♫ + 'Track Preview' — 동그라미 대신 글자까지 들어가는 알약 모양 */
#lang-toggle #track-open {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 6px;
    width: auto;
    padding: 0 14px 0 11px;
    white-space: nowrap;
}
#lang-toggle #track-open .track-label {
    font-weight: 600;
    line-height: 1;
}
#lang-toggle #track-bar {
    grid-column: 3;
    justify-self: end;
}

/* 카드 인사이트 모드 전환용 페이드 오버레이. main.js가 opacity를 직접 구동함 */
#fade-overlay {
    position: absolute;
    inset: 0;
    background: #000;
    opacity: 0;
    pointer-events: none;
    z-index: 5;
}

/* 카드 뒤집기 타격 순간의 흰 플래시. main.js(updateImpact)가 opacity를 직접 구동함 */
#flash-overlay {
    position: absolute;
    inset: 0;
    background: #fff;
    opacity: 0;
    pointer-events: none;
    z-index: 5;
    mix-blend-mode: screen;
}

/* lil-gui는 기본적으로 화면 우상단 고정 -> 뷰포트 바깥 빈 공간에 자연스럽게 배치됨 */
.lil-gui.root {
    --width: 300px;
}

/* 모바일(폭 640px 이하): 화면을 넓게 쓰도록 아래 묶음을 화면 아래에 바짝(10px) 붙이고,
   박스 글자는 12px, EN/KR 은 10px 로 작게. 위쪽 문장(What's your)도 인트로 쪽에서 위에 10px 로 붙으므로
   카드 도구(저장·공유)도 그만큼 올림 */
@media (max-width: 640px) {
    #dock,
    #dock.is-reading {
        bottom: calc(30px + env(safe-area-inset-bottom, 0px));
        gap: 8px; /* EN/KR 줄 · 질문 박스 · 선택지 · Change the word 사이 간격 */
    }

    /* 나레이션(질문) 박스 · Choose again / Yes, this one · Change the word 글자 크기를 같게 */
    #prompt,
    #prompt.is-reading,
    #pick-choice button,
    #change-word {
        font-size: 10px;
    }
    #track-popup .tp-buttons button {
        width: 28px;
        height: 28px;
    }
    #track-popup .tp-buttons .tp-play {
        width: 38px;
        height: 38px;
    }
    #track-popup .tp-buttons button svg {
        width: 13px;
        height: 13px;
    }
    /* 트랙 팝업도 같은 크기 (재생 시간 포함) */
    #track-popup .tp-panel,
    #track-popup .tp-time {
        font-size: 10px;
    }

    #lang-toggle {
        font-size: 10px;
    }
    #lang-toggle button[data-lang],
    #lang-toggle #track-open,
    #reading-controls button,
    #track-controls button {
        width: 28px;
        height: 28px;
    }
    #reading-controls button svg,
    #track-controls button svg {
        width: 13px;
        height: 13px;
    }
    #lang-toggle #track-open {
        width: auto;
        padding: 0 11px 0 9px;
        gap: 5px;
    }
    #lang-toggle #track-open svg {
        width: 13px;
        height: 13px;
    }

    #card-tools {
        top: calc(71px + env(safe-area-inset-top, 0px)); /* 위 키워드 상자와의 간격을 예전의 절반쯤으로 */
        gap: 10px;
    }
    #card-tools button {
        width: 26px;
        height: 26px;
        padding: 4px;
    }
}

/* 모바일 환경(화면이 좁아서 뷰포트가 꽉 차는 상태)에서는 개발용 파라미터 UI를 완전히 숨김.
   단 ?dev / G 키로 일부러 연 경우(body.dev-gui)에는 좁은 화면에서도 보이게 둠 */
@media (max-width: 700px) {
    body:not(.dev-gui) .lil-gui.root {
        display: none !important;
    }
}

/* ---- ?intro 모드: 인트로 페이지(../index.html) 안, 문장 아래 영역에 iframe으로 뜨는 상태 ----
   모바일 프레임 대신 iframe을 꽉 채우고, 바깥 여백 색(#1b1c1f) 없이 전부 검정 */
html.intro {
    background: #000;
}

body.intro {
    background: #000;
}
body.intro #viewport {
    z-index: 1;
    width: 100%;
    height: 100%;
    max-width: none;
    aspect-ratio: auto;
    box-shadow: none;
}
/* 인트로에선 섞기·멈춤이 자동 흐름이라 버튼/조작 안내는 쓰지 않음 */
body.intro #loading,
body.intro #spread-btn,
body.intro #hint {
    display: none;
}

/* ♫ 트랙 팝업 — 카드 화면 위에 반투명 검정을 깔고 가운데 패널 */
#track-popup {
    position: absolute;
    inset: 0;
    z-index: 20;
    display: none;
    align-items: center;
    justify-content: center;
    padding: 16px;
    background: rgba(0, 0, 0, 0.6);
    -webkit-backdrop-filter: blur(3px);
    backdrop-filter: blur(3px);
}
#track-popup.is-on {
    display: flex;
    animation: dock-fade-in 0.35s ease both;
}
#track-popup .tp-panel {
    position: relative;
    display: flex;
    flex-direction: column;
    gap: 14px;
    width: min(100%, 340px);
    padding: 34px 22px 22px;
    font:
        600 14px/1.3 'Helvetica Neue',
        Helvetica,
        Arial,
        'XH KR',
        sans-serif;
    color: #8689c3;
    background: #000;
    border: 1px solid rgba(134, 137, 195, 0.6);
}
#track-popup .tp-close {
    position: absolute;
    top: 8px;
    right: 8px;
    width: 30px;
    height: 30px;
    padding: 6px;
    color: inherit;
    background: none;
    border: 0;
    cursor: pointer;
}
#track-popup svg {
    display: block;
    width: 100%;
    height: 100%;
    fill: currentColor;
}
/* 앨범 로고: 검정 PNG 를 모양(마스크)으로만 쓰고 라벤더로 칠함 */
#track-popup .tp-logo {
    height: 120px;
    background: #8689c3;
    mask: var(--logo) center / contain no-repeat;
    -webkit-mask: var(--logo) center / contain no-repeat;
}
#track-popup .tp-logo.is-text {
    display: none;
}
/* 곡 제목: 로고가 있으면 숨김 (로고 파일이 없는 트랙일 때만 대신 보임) */
#track-popup .tp-title {
    display: none;
    margin: 0;
    text-align: center;
}
#track-popup .tp-logo.is-text + .tp-title {
    display: block;
}
/* 재생바·볼륨: 브라우저 기본 모양(테두리·포커스 외곽선) 없이 가는 선 + 동그란 손잡이 */
#track-popup input[type='range'] {
    width: 100%;
    height: 16px;
    margin: 0;
    background: none;
    border: 0;
    outline: none;
    cursor: pointer;
    appearance: none;
    -webkit-appearance: none;
}
#track-popup input[type='range']:focus,
#track-popup input[type='range']:focus-visible {
    outline: none;
}
/* 당긴 만큼(--p, JS가 넣음)은 라벤더로 채움 */
#track-popup input[type='range']::-webkit-slider-runnable-track {
    height: 3px;
    background: linear-gradient(to right, #8689c3 var(--p, 0%), rgba(134, 137, 195, 0.35) var(--p, 0%));
    border: 0;
    border-radius: 2px;
}
#track-popup input[type='range']::-moz-range-track {
    height: 3px;
    background: rgba(134, 137, 195, 0.35);
    border: 0;
    border-radius: 2px;
}
#track-popup input[type='range']::-moz-range-progress {
    height: 3px;
    background: #8689c3;
    border-radius: 2px;
}
#track-popup input[type='range']::-webkit-slider-thumb {
    width: 12px;
    height: 12px;
    margin-top: -4.5px;
    background: #8689c3;
    border: 0;
    border-radius: 50%;
    box-shadow: none;
    -webkit-appearance: none;
}
#track-popup input[type='range']::-moz-range-thumb {
    width: 12px;
    height: 12px;
    background: #8689c3;
    border: 0;
    border-radius: 50%;
}
#track-popup .tp-time {
    display: flex;
    justify-content: space-between;
    margin-top: -8px;
    font-size: 12px;
    font-variant-numeric: tabular-nums;
    opacity: 0.8;
}
#track-popup .tp-buttons {
    display: flex;
    justify-content: center;
    align-items: center;
    gap: 10px;
}
/* 볼륨 · 되돌리기: 나레이션 조작 버튼과 같은 동그란 테두리 버튼 + 같은 아이콘 크기 */
#track-popup .tp-buttons button {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 34px;
    height: 34px;
    padding: 0;
    color: #8689c3;
    background: #000;
    border: 1px solid rgba(134, 137, 195, 0.6);
    border-radius: 99999px;
    cursor: pointer;
}
#track-popup .tp-buttons button svg {
    width: 16px;
    height: 16px;
}
/* 가운데 재생: 꽉 찬 원 (아이콘 크기는 옆 버튼들과 같게) */
#track-popup .tp-buttons .tp-play {
    width: 44px;
    height: 44px;
    color: #000;
    background: #8689c3;
    border-color: #8689c3;
}
#track-popup .tp-buttons .icon-pause,
#track-popup .tp-buttons .tp-play.is-playing .icon-play {
    display: none;
}
#track-popup .tp-buttons .tp-play.is-playing .icon-pause {
    display: inline;
}
/* 볼륨 슬라이드: 평소엔 닫혀 있다가 볼륨 버튼을 누르면 아래로 펼쳐짐 (높이 0fr → 1fr) */
#track-popup .tp-vol {
    display: grid;
    grid-template-rows: 0fr;
    margin-top: -14px;
    /* 닫혀 있을 땐 패널 간격(gap)만큼 당겨 빈자리가 안 남게 */
    opacity: 0;
    transition:
        grid-template-rows 0.3s ease,
        margin-top 0.3s ease,
        opacity 0.25s ease;
}
#track-popup .tp-vol.is-open {
    grid-template-rows: 1fr;
    margin-top: 0;
    opacity: 1;
}
#track-popup .tp-vol-inner {
    overflow: hidden;
    min-height: 0;
}
#track-popup .tp-vol-inner input {
    margin: 6px 0;
}

/* 모바일: 버튼을 빠르게 두 번 눌러도 화면이 확대되지 않게, 길게 눌러도 글자 선택·메뉴가 안 뜨게 */
button,
input[type='range'] {
    touch-action: manipulation;
    -webkit-user-select: none;
    user-select: none;
    -webkit-touch-callout: none;
}
