/* === Fonts === */
@font-face {
    font-family: 'EB Garamond';
    font-style: normal;
    font-weight: 400;
    font-display: swap;
    src: url('/fonts/eb-garamond-v27-latin-regular.woff2') format('woff2');
}
@font-face {
    font-family: 'EB Garamond';
    font-style: italic;
    font-weight: 400;
    font-display: swap;
    src: url('/fonts/eb-garamond-v27-latin-italic.woff2') format('woff2');
}
@font-face {
    font-family: 'Inter';
    font-style: normal;
    font-weight: 400;
    font-display: swap;
    src: url('/fonts/inter-v13-latin-regular.woff2') format('woff2');
}

/* === Reset & Base === */
*, *::before, *::after {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

:root {
    --bg: #0a0908;
    --text: #d4cfc4;
    --text-dim: rgba(212, 207, 196, 0.56);
    --accent: #8a7a5a;
    --focus: #b69f74;
    --border: rgba(255, 255, 255, 0.06);
    --serif: 'EB Garamond', Georgia, 'Times New Roman', serif;
    --sans: 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', system-ui, sans-serif;
}

html, body {
    height: 100%;
    overflow: hidden;
    background: var(--bg);
    color: var(--text);
    font-family: var(--sans);
    -webkit-font-smoothing: antialiased;
    -moz-osx-font-smoothing: grayscale;
}

button:focus-visible,
input:focus-visible,
[tabindex]:focus-visible,
.tracklist li:focus-visible,
.lyrics .line-group.timed:focus-visible {
    outline: 2px solid var(--focus);
    outline-offset: 2px;
}

/* subtle noise texture */
body::after {
    content: '';
    position: fixed;
    inset: 0;
    opacity: 0.035;
    background-image: url("data:image/svg+xml,%3Csvg viewBox='0 0 256 256' xmlns='http://www.w3.org/2000/svg'%3E%3Cfilter id='n'%3E%3CfeTurbulence type='fractalNoise' baseFrequency='0.9' numOctaves='4' stitchTiles='stitch'/%3E%3C/filter%3E%3Crect width='100%25' height='100%25' filter='url(%23n)'/%3E%3C/svg%3E");
    background-size: 220px 220px;
    background-position: 0 0;
    animation: grain-shift 8s steps(8, end) infinite;
    pointer-events: none;
    z-index: 9999;
}

@keyframes grain-shift {
    0% { background-position: 0 0; }
    20% { background-position: -18px 12px; }
    40% { background-position: 14px -16px; }
    60% { background-position: -10px 9px; }
    80% { background-position: 11px -13px; }
    100% { background-position: 0 0; }
}

/* === Screens === */
.screen {
    position: fixed;
    inset: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    opacity: 0;
    visibility: hidden;
    transition: opacity 0.6s ease, visibility 0.6s ease;
}

.screen.active {
    opacity: 1;
    visibility: visible;
}

/* === Album Selector === */
.selector-inner {
    width: 100%;
    max-width: 720px;
    padding: 0 24px;
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 24px;
}

.selector-prompt {
    text-align: center;
    font-family: var(--sans);
    font-size: 0.78rem;
    letter-spacing: 0.14em;
    text-transform: uppercase;
    color: rgba(212, 207, 196, 0.72);
    user-select: none;
}

.album-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(180px, 1fr));
    gap: 24px;
    width: 100%;
}

.album-card {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 8px;
    cursor: pointer;
    padding: 12px;
    border-radius: 8px;
    transition: background 0.2s ease, transform 0.15s ease;
    outline: none;
}

.album-card:hover,
.album-card:focus-visible {
    background: rgba(255, 255, 255, 0.04);
    transform: scale(1.03);
}

.album-card-cover {
    width: 100%;
    aspect-ratio: 1;
    object-fit: cover;
    border-radius: 4px;
    background: rgba(255, 255, 255, 0.02);
}

.album-card-title {
    font-family: var(--serif);
    font-size: 1rem;
    color: var(--text);
    text-align: center;
    line-height: 1.3;
}

.album-card-artist {
    font-family: var(--sans);
    font-size: 0.75rem;
    color: rgba(212, 207, 196, 0.55);
    text-align: center;
}

/* === Gate === */
.gate-inner {
    width: 100%;
    max-width: 360px;
    padding: 0 24px;
    display: flex;
    flex-direction: column;
    gap: 10px;
}

.gate-prompt {
    text-align: center;
    font-family: var(--sans);
    font-size: 0.78rem;
    letter-spacing: 0.14em;
    text-transform: uppercase;
    color: rgba(212, 207, 196, 0.72);
    user-select: none;
}

#passphrase {
    width: 100%;
    background: rgba(255, 255, 255, 0.03);
    border: 1px solid rgba(212, 207, 196, 0.32);
    border-radius: 8px;
    color: var(--text);
    font-family: var(--serif);
    font-size: 1.15rem;
    padding: 13px 14px;
    outline: none;
    text-align: center;
    letter-spacing: 0.03em;
    -webkit-text-security: disc;
    transition: border-color 0.25s ease, box-shadow 0.25s ease, background 0.25s ease;
}

#passphrase:focus {
    border-color: var(--accent);
    box-shadow: 0 0 0 2px rgba(138, 122, 90, 0.2);
    background: rgba(255, 255, 255, 0.05);
}

#passphrase::placeholder {
    color: rgba(212, 207, 196, 0.58);
}

/* Shake animation — wrong passphrase */
@keyframes shake {
    0%, 100% { transform: translateX(0); }
    10%, 30%, 50%, 70%, 90% { transform: translateX(-4px); }
    20%, 40%, 60%, 80% { transform: translateX(4px); }
}

#passphrase.shake {
    animation: shake 0.5s ease;
}

/* Pulse animation — server error */
@keyframes pulse-border {
    0%, 100% { border-bottom-color: var(--border); }
    50% { border-bottom-color: rgba(138, 90, 60, 0.5); }
}

#passphrase.pulse {
    animation: pulse-border 2s ease infinite;
}

/* === Player === */
.player-inner {
    width: 100%;
    max-width: 480px;
    height: 100%;
    display: flex;
    flex-direction: column;
    align-items: center;
    padding: 48px 24px max(16px, env(safe-area-inset-bottom));
    overflow: hidden;
}

/* Cover */
.cover {
    width: 140px;
    height: 140px;
    object-fit: cover;
    border-radius: 2px;
    opacity: 0.85;
    flex-shrink: 0;
    background: rgba(255, 255, 255, 0.04);
}

.cover.hidden {
    display: none;
}

.cover.fallback {
    object-fit: contain;
    padding: 8px;
}

/* Oscilloscope */
#oscilloscope {
    width: 100%;
    height: 84px;
    flex-shrink: 0;
    margin: 16px 0;
}

/* Track title */
.track-title {
    font-family: var(--serif);
    font-size: 1.5rem;
    text-align: center;
    padding: 0 0 12px;
    flex-shrink: 0;
    min-height: 2.2rem;
}

/* Lyrics */
.lyrics-container {
    flex: 1;
    width: 100%;
    overflow-y: auto;
    overflow-x: hidden;
    position: relative;
    overscroll-behavior: contain;
    -webkit-overflow-scrolling: touch;
    scroll-padding-top: 96px;
    scroll-padding-bottom: 160px;
    mask-image: linear-gradient(to bottom, transparent 0%, black 8%, black 92%, transparent 100%);
    -webkit-mask-image: linear-gradient(to bottom, transparent 0%, black 8%, black 92%, transparent 100%);
    -ms-overflow-style: none;
    scrollbar-width: none;
}

.lyrics-container.hidden-by-shortcut {
    display: none;
}

#player.lyrics-hidden .tracklist {
    max-height: 260px;
}

.lyrics {
    font-family: var(--serif);
    font-size: 1.15rem;
    line-height: 1.8;
    text-align: center;
    padding: 40px 0;
    color: var(--text-dim);
}

.lyrics .line {
    padding: 1px 0;
    color: var(--text-dim);
    opacity: 0.55;
    transition: color 0.1s linear, opacity 0.1s linear;
}

.lyrics .line.active {
    color: var(--text);
    opacity: 1;
    text-shadow: 0 0 10px rgba(212, 207, 196, 0.18);
}

.lyrics .line-group {
    padding: 1px 0;
}

.lyrics .line-group.timed {
    cursor: pointer;
}

.lyrics .section-label {
    font-family: var(--sans);
    font-size: 0.68rem;
    letter-spacing: 0.14em;
    text-transform: uppercase;
    color: rgba(212, 207, 196, 0.46);
    margin: 0.2rem 0 0.35rem;
}

.lyrics .line-group.section-break {
    margin-top: 0.95rem;
}

.lyrics .line-group.active-group .line {
    opacity: 0.82;
}

.lyrics .line-group.active-group .line.active {
    opacity: 1;
}

.lyrics .no-lyrics {
    color: var(--text-dim);
    font-style: italic;
    padding-top: 60px;
}

.lyrics .markdown-content {
    text-align: left;
    padding: 20px 12px;
    color: var(--text);
    opacity: 0.8;
}

.lyrics .markdown-content p {
    margin-bottom: 1em;
}

.lyrics .markdown-content em {
    font-style: italic;
}

.lyrics .markdown-content strong {
    font-weight: 600;
    color: var(--text);
}

/* Controls */
.controls {
    width: 100%;
    flex-shrink: 0;
    padding: 12px 0 8px;
    z-index: 3;
}

.control-buttons {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 32px;
    margin-bottom: 12px;
}

.control-btn {
    background: none;
    border: none;
    color: var(--text-dim);
    font-size: 1rem;
    cursor: pointer;
    min-width: 44px;
    min-height: 44px;
    padding: 8px;
    transition: color 0.2s ease;
    line-height: 1;
    touch-action: manipulation;
}

.control-btn:hover {
    color: var(--text);
}

.control-btn-main {
    width: 48px;
    height: 48px;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    font-size: 1.45rem;
    color: var(--text);
    font-family: var(--sans);
    font-weight: 500;
    border: 1px solid var(--border);
    border-radius: 50%;
    padding: 0;
}

.control-btn svg,
.control-btn-main svg {
    display: block;
}

/* Progress bar */
.progress-container {
    width: 100%;
}

.progress {
    -webkit-appearance: none;
    appearance: none;
    width: 100%;
    height: 20px;
    background: transparent;
    outline: none;
    cursor: pointer;
}

.progress::-webkit-slider-runnable-track {
    height: 2px;
    background: var(--border);
    border-radius: 1px;
}

.progress::-webkit-slider-thumb {
    -webkit-appearance: none;
    width: 10px;
    height: 10px;
    background: var(--text);
    border-radius: 50%;
    cursor: pointer;
    margin-top: -4px;
}

.progress::-moz-range-track {
    height: 2px;
    background: var(--border);
    border-radius: 1px;
}

.progress::-moz-range-thumb {
    width: 10px;
    height: 10px;
    background: var(--text);
    border-radius: 50%;
    border: none;
    cursor: pointer;
}

.progress-times {
    display: flex;
    justify-content: space-between;
    font-size: 0.75rem;
    color: var(--text-dim);
    margin-top: 4px;
    font-variant-numeric: tabular-nums;
}

.volume-container {
    margin-top: 10px;
    display: flex;
    align-items: center;
    gap: 10px;
}

.volume-btn {
    min-width: 42px;
    padding: 4px 8px;
    font-size: 0.7rem;
    letter-spacing: 0.08em;
}

.volume {
    -webkit-appearance: none;
    appearance: none;
    width: 100%;
    height: 20px;
    background: transparent;
    outline: none;
}

.volume::-webkit-slider-runnable-track {
    height: 2px;
    background: var(--border);
    border-radius: 1px;
}

.volume::-webkit-slider-thumb {
    -webkit-appearance: none;
    width: 9px;
    height: 9px;
    background: var(--text);
    border-radius: 50%;
    cursor: pointer;
    margin-top: -3.5px;
}

.volume::-moz-range-track {
    height: 2px;
    background: var(--border);
    border-radius: 1px;
}

.volume::-moz-range-thumb {
    width: 9px;
    height: 9px;
    background: var(--text);
    border-radius: 50%;
    border: none;
    cursor: pointer;
}

/* Track list */
.tracklist-toggle {
    width: 100%;
    background: none;
    border: none;
    font-family: var(--sans);
    font-size: 0.8rem;
    color: var(--text-dim);
    cursor: pointer;
    min-height: 44px;
    padding: 12px 0 4px;
    text-align: center;
    user-select: none;
    flex-shrink: 0;
    touch-action: manipulation;
}

.tracklist-toggle:hover {
    color: var(--text);
}

.tracklist-arrow {
    display: inline-block;
    transition: transform 0.3s ease;
    font-size: 0.7rem;
}

.tracklist-toggle.open .tracklist-arrow {
    transform: rotate(180deg);
}

.tracklist {
    width: 100%;
    overflow-y: auto;
    flex-shrink: 0;
    max-height: 0;
    transition: max-height 0.4s ease;
}

.tracklist.expanded {
    max-height: 300px;
}

.tracklist ol {
    list-style: none;
    padding: 8px 0;
}

.tracklist li {
    font-family: var(--serif);
    font-size: 0.95rem;
    padding: 10px 12px;
    cursor: pointer;
    color: var(--text-dim);
    transition: color 0.2s ease;
    display: flex;
    align-items: baseline;
    gap: 12px;
}

.tracklist li:hover {
    color: var(--text);
}

.tracklist li:focus-visible {
    color: var(--text);
}

.tracklist li.active {
    color: var(--text);
}

.tracklist li .track-num {
    font-family: var(--sans);
    font-size: 0.75rem;
    min-width: 24px;
    text-align: right;
    opacity: 0.5;
}

.tracklist li.active .track-num {
    color: var(--accent);
    opacity: 1;
}

.tracklist li .track-title-text {
    flex: 1;
    min-width: 0;
}

.tracklist li .track-dl {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 28px;
    height: 28px;
    flex-shrink: 0;
    margin-left: auto;
    color: var(--text-dim);
    opacity: 0.4;
    border-radius: 6px;
    transition: opacity 0.2s ease, color 0.2s ease, background 0.15s ease;
    text-decoration: none;
}

.tracklist li:hover .track-dl,
.tracklist li:focus-visible .track-dl,
.tracklist li.active .track-dl {
    opacity: 0.8;
}

.tracklist li .track-dl:hover {
    opacity: 1;
    color: var(--text);
    background: rgba(255, 255, 255, 0.08);
}

.tracklist li .track-dl:active {
    background: rgba(255, 255, 255, 0.12);
}

/* Scrollbar */
.lyrics-container::-webkit-scrollbar {
    width: 0;
    height: 0;
    display: none;
}

.tracklist::-webkit-scrollbar {
    width: 4px;
}

.tracklist::-webkit-scrollbar-track {
    background: transparent;
}

.tracklist::-webkit-scrollbar-thumb {
    background: var(--border);
    border-radius: 2px;
}

/* === Responsive === */
@media (max-width: 768px) {
    .controls {
        position: sticky;
        bottom: 0;
        margin-top: 10px;
        padding: 12px 0 calc(10px + env(safe-area-inset-bottom));
        background: linear-gradient(to top, rgba(10, 9, 8, 0.95) 65%, rgba(10, 9, 8, 0));
        backdrop-filter: blur(6px);
    }

    .lyrics-container {
        width: calc(100% + 6px);
        margin-right: -6px;
        padding-right: 6px;
        scroll-padding-bottom: 180px;
    }

    .tracklist li {
        padding: 12px 12px;
    }

    .tracklist li .track-dl {
        opacity: 1;
    }

    .tracklist {
        -webkit-overflow-scrolling: touch;
        overscroll-behavior: contain;
    }

    .tracklist.expanded {
        /* Keep the list visible above sticky controls on small phones. */
        max-height: clamp(170px, 42vh, 320px);
    }

    .tracklist ol {
        padding-bottom: calc(128px + env(safe-area-inset-bottom));
    }
}

@media (max-width: 480px) {
    .player-inner {
        padding: 32px 16px max(12px, env(safe-area-inset-bottom));
    }

    .gate-inner {
        padding: 0 18px;
    }

    .cover {
        width: 100px;
        height: 100px;
    }

    #oscilloscope {
        height: 52px;
        margin: 12px 0;
    }

    .track-title {
        font-size: 1.25rem;
    }

    .lyrics {
        font-size: 1.05rem;
    }
}

@media (max-height: 600px) {
    .cover {
        width: 80px;
        height: 80px;
    }

    #oscilloscope {
        height: 40px;
        margin: 8px 0;
    }

    .track-title {
        padding-bottom: 6px;
    }

    .player-inner {
        padding-top: 16px;
    }
}

@media (min-width: 1024px) and (min-height: 680px) {
    .player-inner {
        max-width: 1320px;
        width: min(1320px, calc(100vw - 64px));
        margin: 0 auto;
        padding: 36px 32px 24px;
        display: grid;
        grid-template-columns: minmax(360px, 440px) minmax(520px, 680px);
        grid-template-rows: auto auto auto auto minmax(0, 1fr);
        grid-template-areas:
            "cover lyrics"
            "scope lyrics"
            "title lyrics"
            "controls lyrics"
            "tracklist lyrics";
        column-gap: 56px;
        row-gap: 6px;
        align-items: start;
        justify-content: center;
    }

    .cover {
        grid-area: cover;
        width: 164px;
        height: 164px;
        justify-self: center;
        margin-bottom: 6px;
    }

    #oscilloscope {
        grid-area: scope;
        height: 118px;
        margin: 8px 0 6px;
    }

    .track-title {
        grid-area: title;
        padding-bottom: 8px;
        font-size: 1.65rem;
    }

    .controls {
        grid-area: controls;
        padding: 8px 0;
    }

    .tracklist-toggle {
        display: none;
    }

    .tracklist {
        grid-area: tracklist;
        align-self: stretch;
        min-height: 0;
        max-height: none;
        overflow-y: auto;
        transition: none;
        -webkit-mask-image: linear-gradient(to bottom, #000 calc(100% - 32px), transparent);
        mask-image: linear-gradient(to bottom, #000 calc(100% - 32px), transparent);
        scrollbar-width: thin;
        scrollbar-color: var(--border) transparent;
    }

    .tracklist::-webkit-scrollbar {
        width: 4px;
    }

    .tracklist::-webkit-scrollbar-track {
        background: transparent;
    }

    .tracklist::-webkit-scrollbar-thumb {
        background: var(--border);
        border-radius: 2px;
    }

    .tracklist.collapsed,
    .tracklist.expanded {
        max-height: none;
    }

    .lyrics-container {
        grid-area: lyrics;
        height: calc(100vh - 112px);
        min-height: 420px;
        width: 100%;
    }

    .lyrics {
        text-align: left;
        padding: 70px 24px;
        font-size: 1.22rem;
    }

    #player.lyrics-hidden .player-inner {
        max-width: 760px;
        grid-template-columns: minmax(380px, 1fr);
        grid-template-areas:
            "cover"
            "scope"
            "title"
            "controls"
            "tracklist";
    }

    #player.lyrics-hidden .tracklist {
        max-height: none;
        height: 100%;
        overflow-y: auto;
    }
}

/* === Utilities === */
.hidden {
    display: none !important;
}
