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

:root {
    --bg:        #0f0e0c;
    --surface:   #1a1916;
    --surface2:  #242320;
    --border:    rgba(255,255,255,0.08);
    --text:      #f0ede6;
    --muted:     #7a7770;
    --accent:    #c9a96e;
    --accent2:   #e8c88a;
    --danger:    #e07070;
    --radius:    14px;
    --ff-serif:  'Playfair Display', Georgia, serif;
    --ff-sans:   'DM Sans', system-ui, sans-serif;
    --transition: 0.2s ease;
}

body {
    font-family: var(--ff-sans);
    background: var(--bg);
    color: var(--text);
    min-height: 100vh;
    padding: 40px 20px 80px;
    position: relative;
    overflow-x: hidden;
}

/* Noise texture overlay */
.noise {
    position: fixed;
    inset: 0;
    pointer-events: none;
    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='noise'%3E%3CfeTurbulence type='fractalNoise' baseFrequency='0.9' numOctaves='4' stitchTiles='stitch'/%3E%3C/filter%3E%3Crect width='100%25' height='100%25' filter='url(%23noise)'/%3E%3C/svg%3E");
    background-size: 200px 200px;
    z-index: 1000;
}

/* ── Layout ─────────────────────────────────────────────────────── */
.container {
    max-width: 860px;
    margin: 0 auto;
}

/* ── Header ─────────────────────────────────────────────────────── */
header {
    margin-bottom: 48px;
    padding-bottom: 32px;
    border-bottom: 1px solid var(--border);
}

.header-label {
    font-size: 11px;
    font-weight: 500;
    letter-spacing: 0.2em;
    color: var(--accent);
    margin-bottom: 12px;
}

header h1 {
    font-family: var(--ff-serif);
    font-size: clamp(2rem, 5vw, 3.2rem);
    font-weight: 700;
    line-height: 1.1;
    color: var(--text);
}

/* ── Book Grid ──────────────────────────────────────────────────── */
.audiobook-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(240px, 1fr));
    gap: 16px;
    margin-bottom: 40px;
}

/* Loading state */
.loading-state {
    grid-column: 1/-1;
    display: flex;
    align-items: center;
    gap: 12px;
    color: var(--muted);
    font-size: 0.9rem;
    padding: 20px 0;
}

.spinner {
    width: 18px;
    height: 18px;
    border: 2px solid var(--border);
    border-top-color: var(--accent);
    border-radius: 50%;
    animation: spin 0.8s linear infinite;
}

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

/* Book cards */
.book-card {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 24px;
    cursor: pointer;
    transition: border-color var(--transition), transform var(--transition), background var(--transition);
    display: flex;
    flex-direction: column;
    gap: 6px;
    position: relative;
    overflow: hidden;
}

.book-card::before {
    content: '';
    position: absolute;
    inset: 0;
    background: linear-gradient(135deg, rgba(201,169,110,0.06) 0%, transparent 60%);
    opacity: 0;
    transition: opacity var(--transition);
}

.book-card:hover::before { opacity: 1; }
.book-card:hover {
    border-color: rgba(201,169,110,0.3);
    transform: translateY(-2px);
    background: var(--surface2);
}

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

.book-card.active {
    border-color: var(--accent);
    background: var(--surface2);
}

.book-card-spine {
    position: absolute;
    left: 0; top: 0; bottom: 0;
    width: 3px;
    background: var(--accent);
    opacity: 0;
    transition: opacity var(--transition);
}

.book-card:hover .book-card-spine,
.book-card.active .book-card-spine { opacity: 1; }

.book-title {
    font-family: var(--ff-serif);
    font-size: 1.05rem;
    font-weight: 700;
    color: var(--text);
    line-height: 1.3;
}

.book-author {
    font-size: 0.82rem;
    color: var(--muted);
    font-weight: 300;
}

.book-meta {
    margin-top: 12px;
    font-size: 0.78rem;
    color: var(--accent);
    letter-spacing: 0.05em;
    font-weight: 500;
}

/* ── Player ─────────────────────────────────────────────────────── */
.player {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    overflow: hidden;
    animation: slideUp 0.3s ease;
}

@keyframes slideUp {
    from { opacity: 0; transform: translateY(16px); }
    to   { opacity: 1; transform: translateY(0); }
}

.player[hidden] { display: none; }

.player-header {
    display: flex;
    justify-content: space-between;
    align-items: flex-start;
    padding: 28px 28px 0;
    gap: 16px;
}

.player-label {
    font-size: 10px;
    letter-spacing: 0.2em;
    color: var(--accent);
    font-weight: 500;
    margin-bottom: 8px;
}

.player-meta h2 {
    font-family: var(--ff-serif);
    font-size: 1.35rem;
    font-weight: 700;
    line-height: 1.2;
    color: var(--text);
    margin-bottom: 4px;
}

.player-meta p {
    font-size: 0.82rem;
    color: var(--muted);
    font-weight: 300;
}

.close-player {
    background: none;
    border: 1px solid var(--border);
    color: var(--muted);
    width: 32px;
    height: 32px;
    border-radius: 50%;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 0.75rem;
    flex-shrink: 0;
    transition: color var(--transition), border-color var(--transition);
}

.close-player:hover {
    color: var(--text);
    border-color: rgba(255,255,255,0.25);
}

/* ── Audio Controls ─────────────────────────────────────────────── */
.audio-wrapper {
    padding: 24px 28px 20px;
    border-bottom: 1px solid var(--border);
}

/* Progress bar */
.progress-container {
    cursor: pointer;
    padding: 8px 0;
    user-select: none;
}

.progress-bar {
    height: 3px;
    background: var(--surface2);
    border-radius: 2px;
    position: relative;
    overflow: visible;
}

.progress-fill {
    height: 100%;
    background: var(--accent);
    border-radius: 2px;
    width: 0%;
    transition: width 0.1s linear;
    position: relative;
}

.progress-thumb {
    position: absolute;
    top: 50%;
    right: -6px;
    transform: translateY(-50%) scale(0);
    width: 12px;
    height: 12px;
    background: var(--accent2);
    border-radius: 50%;
    transition: transform 0.15s ease;
}

.progress-container:hover .progress-thumb,
.progress-container:focus .progress-thumb {
    transform: translateY(-50%) scale(1);
}

.time-display {
    display: flex;
    justify-content: space-between;
    margin-top: 8px;
    font-size: 0.75rem;
    color: var(--muted);
    font-variant-numeric: tabular-nums;
}

/* Error message */
.error-message {
    background: rgba(224, 112, 112, 0.1);
    border: 1px solid rgba(224, 112, 112, 0.3);
    color: var(--danger);
    border-radius: 8px;
    padding: 10px 14px;
    font-size: 0.83rem;
    margin-top: 12px;
}
.error-message[hidden] { display: none; }

/* Main controls */
.main-controls {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 12px;
    margin-top: 20px;
}

.ctrl-btn {
    background: none;
    border: none;
    color: var(--muted);
    cursor: pointer;
    padding: 8px;
    border-radius: 50%;
    transition: color var(--transition), background var(--transition), transform var(--transition);
    display: flex;
    align-items: center;
    justify-content: center;
    position: relative;
}

.ctrl-btn svg { width: 20px; height: 20px; display: block; }

.ctrl-btn:hover { color: var(--text); background: var(--surface2); }
.ctrl-btn:active { transform: scale(0.92); }
.ctrl-btn:disabled { opacity: 0.3; cursor: not-allowed; }
.ctrl-btn:disabled:hover { background: none; transform: none; }

/* Skip buttons */
.skip-btn {
    font-size: 0;
}
.skip-btn svg { width: 18px; height: 18px; }
.skip-btn span {
    position: absolute;
    font-size: 9px;
    font-weight: 700;
    color: inherit;
    bottom: 6px;
}

/* Play/Pause */
.play-btn {
    width: 52px;
    height: 52px;
    background: var(--accent) !important;
    color: var(--bg) !important;
    border-radius: 50%;
    flex-shrink: 0;
}

.play-btn svg { width: 20px; height: 20px; }
.play-btn:hover { background: var(--accent2) !important; }

.btn-spinner {
    width: 20px;
    height: 20px;
    border: 2px solid rgba(15,14,12,0.3);
    border-top-color: var(--bg);
    border-radius: 50%;
    animation: spin 0.8s linear infinite;
}

/* Secondary controls */
.secondary-controls {
    display: flex;
    justify-content: flex-end;
    margin-top: 16px;
    gap: 20px;
}

.ctrl-label {
    font-size: 10px;
    letter-spacing: 0.15em;
    color: var(--muted);
    font-weight: 500;
    display: block;
    margin-bottom: 6px;
}

.speed-buttons {
    display: flex;
    gap: 4px;
}

.speed-btn {
    background: none;
    border: 1px solid var(--border);
    color: var(--muted);
    padding: 4px 9px;
    border-radius: 6px;
    font-size: 0.78rem;
    cursor: pointer;
    font-family: var(--ff-sans);
    transition: all var(--transition);
}

.speed-btn:hover { color: var(--text); border-color: rgba(255,255,255,0.2); }
.speed-btn.active {
    background: var(--accent);
    border-color: var(--accent);
    color: var(--bg);
    font-weight: 500;
}

/* ── Chapters Panel ─────────────────────────────────────────────── */
.chapters-panel {
    padding: 20px 28px 28px;
}

.chapters-label {
    font-size: 10px;
    letter-spacing: 0.2em;
    color: var(--muted);
    font-weight: 500;
    margin-bottom: 12px;
}

.chapters-list {
    display: flex;
    flex-direction: column;
    gap: 2px;
    max-height: 260px;
    overflow-y: auto;
    scrollbar-width: thin;
    scrollbar-color: var(--surface2) transparent;
}

.chapter-item {
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 10px 12px;
    border-radius: 8px;
    cursor: pointer;
    transition: background var(--transition);
    font-size: 0.88rem;
    color: var(--muted);
    border: 1px solid transparent;
}

.chapter-item:hover {
    background: var(--surface2);
    color: var(--text);
}

.chapter-item.active {
    background: rgba(201,169,110,0.1);
    border-color: rgba(201,169,110,0.2);
    color: var(--accent);
}

.chapter-num {
    font-size: 0.72rem;
    font-variant-numeric: tabular-nums;
    color: var(--muted);
    width: 24px;
    flex-shrink: 0;
    font-weight: 500;
}

.chapter-item.active .chapter-num {
    color: var(--accent);
}

.chapter-playing-icon {
    margin-left: auto;
    display: flex;
    align-items: center;
    gap: 2px;
    flex-shrink: 0;
}

.chapter-playing-icon span {
    display: block;
    width: 3px;
    background: var(--accent);
    border-radius: 2px;
    animation: equalizer 0.8s ease-in-out infinite alternate;
}

.chapter-playing-icon span:nth-child(1) { height: 8px; animation-delay: 0s; }
.chapter-playing-icon span:nth-child(2) { height: 12px; animation-delay: 0.15s; }
.chapter-playing-icon span:nth-child(3) { height: 6px; animation-delay: 0.3s; }

@keyframes equalizer {
    from { transform: scaleY(0.4); }
    to   { transform: scaleY(1); }
}

/* ── Responsive ─────────────────────────────────────────────────── */
@media (max-width: 600px) {
    body { padding: 24px 16px 60px; }

    .player-header,
    .audio-wrapper,
    .chapters-panel { padding-left: 20px; padding-right: 20px; }

    .audiobook-grid { grid-template-columns: 1fr; }

    .secondary-controls { justify-content: center; }
}
