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

:root {
    --text: #e0e0e0;
    --text-muted: #9999b3;
    --bg: #1a1a2e;
    --accent: #a78bfa;
    --accent-hover: #c4b5fd;
    --border: rgba(255, 255, 255, 0.08);
    --shadow: rgba(0, 0, 0, 0.4);
    --content-bg: #16162a;
    --card-bg: #22223a;
    --danger: #ff4757;
    --radius: 8px;
    --tab-height: 56px;
    --player-height: 62px;
    --safe-bottom: env(safe-area-inset-bottom, 0px);
}

html {
    background: var(--bg);
}

body {
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
    background: var(--bg);
    color: var(--text);
    height: 100vh;
    height: 100dvh;
    overflow: hidden;
    -webkit-user-select: none;
    user-select: none;
}

body.fs-open { overflow: hidden; }

.screen { display: none; height: 100vh; height: 100dvh; }
.screen.active { display: flex; flex-direction: column; }

/* ── Login ── */
.login-container {
    margin: auto; padding: 2rem; width: 100%; max-width: 360px; text-align: center;
    padding-top: calc(2rem + env(safe-area-inset-top, 0px));
}
.login-container h1 { font-size: 2.5rem; margin-bottom: 0.25rem; color: var(--accent); }
.subtitle { color: var(--text-muted); margin-bottom: 2rem; }
#build-version {
    position: fixed; bottom: calc(8px + env(safe-area-inset-bottom, 0px)); left: 8px;
    font-size: 0.65rem; color: var(--text-muted); opacity: 0.5;
}
#login-form { display: flex; flex-direction: column; gap: 0.75rem; }
input[type="url"], input[type="text"], input[type="password"] {
    padding: 0.75rem 1rem; background: var(--card-bg); border: 1px solid var(--border);
    border-radius: var(--radius); color: var(--text); font-size: 16px; outline: none;
}
input:focus { border-color: var(--accent); }
button { cursor: pointer; font-size: 1rem; border: none; color: var(--text); background: none; }
#login-form button[type="submit"] {
    padding: 0.75rem; background: var(--accent); border-radius: var(--radius);
    font-weight: 600; color: #fff; margin-top: 0.5rem;
}
#login-form button[type="submit"]:hover { background: var(--accent-hover); }
.error { color: var(--danger); font-size: 0.875rem; min-height: 1.25rem; }

/* ── Header ── */
header {
    display: flex; align-items: center; justify-content: space-between;
    padding: 0.75rem 1rem; background: var(--bg);
    border-bottom: 1px solid var(--border); flex-shrink: 0; z-index: 10;
    padding-top: calc(0.75rem + env(safe-area-inset-top, 0px));
}
.header-left, .header-right { display: flex; align-items: center; gap: 0.5rem; }
header h2 { font-size: 1.125rem; font-weight: 600; }
.icon-btn {
    background: none; border: none; color: var(--text-muted); font-size: 1.25rem;
    padding: 0.25rem 0.5rem; cursor: pointer; border-radius: var(--radius);
    display: flex; align-items: center; justify-content: center;
}
.icon-btn:hover { color: var(--text); background: var(--card-bg); }
.hidden { display: none !important; }

/* ── Library selector ── */
#library-selector {
    padding: 0.5rem 1rem; background: var(--bg); border-bottom: 1px solid var(--border);
}
#library-select {
    width: 100%; padding: 0.5rem; background: var(--card-bg);
    border: 1px solid var(--border); border-radius: var(--radius);
    color: var(--text); font-size: 0.9rem;
}

/* ── Search ── */
#search-overlay {
    position: absolute; top: 0; left: 0; right: 0; bottom: 0;
    background: var(--bg); z-index: 50; display: flex; flex-direction: column;
    padding-top: env(safe-area-inset-top, 0px);
}
.search-bar {
    display: flex; align-items: center; gap: 0.5rem; padding: 0.75rem 1rem;
    border-bottom: 1px solid var(--border);
}
.search-bar input {
    flex: 1; padding: 0.5rem 0.75rem; background: var(--card-bg);
    border: 1px solid var(--border); border-radius: var(--radius);
    color: var(--text); outline: none;
}
.search-bar input:focus { border-color: var(--accent); }
#search-results { flex: 1; overflow-y: auto; padding: 1rem; }

/* ── Content ── */
main {
    flex: 1; overflow-y: auto; padding: 1rem;
    background: var(--content-bg);
}

/* ── Section headings ── */
.section-title {
    font-size: 1.1rem; font-weight: 700; margin-bottom: 0.75rem; margin-top: 1.25rem;
    color: var(--text);
}
.section-title:first-child { margin-top: 0; }

/* ── Horizontal scroll row ── */
.h-scroll {
    display: flex; gap: 0.75rem; overflow-x: auto; padding-bottom: 0.5rem;
    scroll-snap-type: x mandatory; -webkit-overflow-scrolling: touch;
}
.h-scroll::-webkit-scrollbar { display: none; }
.h-scroll .card {
    flex-shrink: 0; width: 130px; scroll-snap-align: start; cursor: pointer;
}
.h-scroll .card img {
    width: 130px; height: 130px; border-radius: var(--radius);
    object-fit: cover; background: var(--card-bg);
}
.h-scroll .card .card-title {
    font-size: 0.8rem; font-weight: 600; margin-top: 0.35rem;
    white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.h-scroll .card .card-sub {
    font-size: 0.7rem; color: var(--text-muted);
    white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.h-scroll .card .card-progress {
    height: 3px; background: rgba(255,255,255,0.1); border-radius: 2px; margin-top: 0.25rem;
}
.h-scroll .card .card-progress-fill {
    height: 100%; background: var(--accent); border-radius: 2px;
}

/* ── Grid ── */
.grid {
    display: grid; grid-template-columns: repeat(auto-fill, minmax(130px, 1fr)); gap: 1rem;
}
.grid-item { cursor: pointer; border-radius: var(--radius); overflow: hidden; background: var(--card-bg); position: relative; }
.h-scroll .card { position: relative; }
.grid-item img {
    width: 100%; aspect-ratio: 1; object-fit: cover; background: var(--card-bg);
}
.grid-item .item-progress { height: 3px; background: rgba(255,255,255,0.1); }
.grid-item .item-progress-fill { height: 100%; background: var(--accent); }
.grid-item .item-info { padding: 0.5rem 0.6rem; }
.grid-item .item-title {
    font-size: 0.8rem; font-weight: 600;
    white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.grid-item .item-subtitle {
    font-size: 0.7rem; color: var(--text-muted);
    white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}

/* ── Play overlay on hover (pointer devices only) ── */
@media (hover: hover) {
    .grid-item::after, .h-scroll .card::after {
        content: "▶"; position: absolute; top: 0; left: 0;
        width: 100%; aspect-ratio: 1; display: flex;
        align-items: center; justify-content: center;
        background: rgba(0, 0, 0, 0.45); color: #fff;
        font-size: 2rem; opacity: 0;
        transition: opacity 0.15s ease; pointer-events: none;
        border-radius: var(--radius) var(--radius) 0 0;
    }
    .h-scroll .card::after {
        border-radius: var(--radius);
    }
    .grid-item:hover::after, .h-scroll .card:hover::after {
        opacity: 1;
    }
}

/* ── List items ── */
.list-view { display: flex; flex-direction: column; gap: 0.25rem; }
.list-item {
    display: flex; align-items: center; gap: 0.75rem;
    padding: 0.75rem; border-radius: var(--radius); cursor: pointer;
}
.list-item:hover { background: var(--card-bg); }
.list-item img {
    width: 48px; height: 48px; border-radius: var(--radius); object-fit: cover;
    background: var(--card-bg); flex-shrink: 0;
}
.list-item .list-name { font-weight: 600; font-size: 0.9rem; }
.list-item .list-count { font-size: 0.8rem; color: var(--text-muted); }
.list-placeholder {
    width: 48px; height: 48px; border-radius: var(--radius); background: var(--card-bg);
    display: flex; align-items: center; justify-content: center;
    font-size: 1.2rem; color: var(--text-muted); flex-shrink: 0;
}
.series-mosaic {
    width: 48px; height: 48px; border-radius: var(--radius); overflow: hidden;
    display: grid; flex-shrink: 0; background: var(--card-bg);
}
.series-mosaic img { width: 100%; height: 100%; object-fit: cover; display: block; }
.mosaic-2 { grid-template-columns: 1fr 1fr; grid-template-rows: 1fr; }
.mosaic-4 { grid-template-columns: 1fr 1fr; grid-template-rows: 1fr 1fr; }
.mosaic-2 img, .mosaic-4 img { aspect-ratio: 1; }

/* ── Detail view ── */
.detail-view { display: flex; flex-direction: column; gap: 1rem; }
.detail-header { display: flex; gap: 1rem; align-items: flex-start; }
.detail-cover {
    width: 140px; height: 140px; border-radius: var(--radius);
    object-fit: cover; background: var(--card-bg); flex-shrink: 0;
}
.detail-meta h3 { font-size: 1.25rem; margin-bottom: 0.25rem; }
.detail-meta .author { color: var(--text-muted); margin-bottom: 0.25rem; font-size: 0.9rem; }
.detail-meta .narrator { color: var(--text-muted); font-size: 0.85rem; font-style: italic; }
.detail-meta .duration { font-size: 0.8rem; color: var(--text-muted); margin-top: 0.5rem; }
.detail-meta .description {
    font-size: 0.85rem; color: var(--text-muted); line-height: 1.4;
    max-height: 4.2em; overflow: hidden; margin-top: 0.5rem;
}
.play-btn-large {
    display: flex; align-items: center; justify-content: center; gap: 0.5rem;
    padding: 0.75rem 1.5rem; background: var(--accent); color: #fff;
    border-radius: var(--radius); font-weight: 600; font-size: 1rem;
    border: none; cursor: pointer; width: 100%; max-width: 300px;
}
.play-btn-large:hover { background: var(--accent-hover); }

/* ── Tracklist with progress bars ── */
.tracklist { list-style: none; }
.tracklist-item { position: relative; overflow: hidden; }
.tracklist-item + .tracklist-item { border-top: 1px solid var(--border); }
.tracklist-progress {
    position: absolute; top: 0; left: 0; bottom: 0; width: 0%;
    background: color-mix(in srgb, var(--accent) 12%, transparent);
    pointer-events: none; z-index: 0; transition: width 0.15s linear;
}
.tracklist-play {
    display: flex; align-items: center; gap: 12px; width: 100%;
    padding: 11px 8px; background: none; border: none; color: var(--text);
    cursor: pointer; font-size: 0.9rem; text-align: left;
    border-radius: 4px; position: relative; z-index: 1;
}
.tracklist-play:hover { background: color-mix(in srgb, var(--accent) 8%, transparent); }
.tracklist-item.is-active .tracklist-play { color: var(--accent); }
.tracklist-num { width: 24px; text-align: center; color: var(--text-muted); flex-shrink: 0; font-size: 0.8rem; }
.tracklist-title { flex: 1; }
.tracklist-duration { color: var(--text-muted); font-size: 0.8rem; }
.text-muted { color: var(--text-muted); font-size: 0.8rem; }
.ep-cover { width: 40px; height: 40px; border-radius: 4px; object-fit: cover; flex-shrink: 0; }

/* ── Tab bar ── */
#tab-bar {
    position: fixed; bottom: 0; left: 0; right: 0;
    height: calc(var(--tab-height) + var(--safe-bottom));
    padding-bottom: var(--safe-bottom);
    background: color-mix(in srgb, var(--bg) 90%, transparent);
    backdrop-filter: blur(16px) saturate(1.8);
    -webkit-backdrop-filter: blur(16px) saturate(1.8);
    border-top: 1px solid var(--border);
    display: flex; z-index: 20;
}
.tab {
    flex: 1; display: flex; flex-direction: column; align-items: center;
    justify-content: center; gap: 2px; background: none; border: none;
    color: var(--text-muted); font-size: 0.65rem; cursor: pointer; padding: 4px 0;
}
.tab svg { stroke: var(--text-muted); }
.tab.active { color: var(--accent); }
.tab.active svg { stroke: var(--accent); }
.tab:hover { color: var(--text); }

/* ── Mini Player (glassmorphic) ── */
#player-bar {
    position: fixed; left: 0; right: 0; z-index: 15;
    bottom: calc(var(--tab-height) + var(--safe-bottom));
    height: var(--player-height);
    background: color-mix(in srgb, var(--bg) 85%, transparent);
    backdrop-filter: blur(16px) saturate(1.8);
    -webkit-backdrop-filter: blur(16px) saturate(1.8);
    border-top: 1px solid var(--border);
    display: flex; align-items: center; padding: 0 12px; gap: 10px;
    cursor: pointer;
}
.pp-scrubber-bg {
    display: none; position: absolute; top: 0; left: 0; bottom: 0;
    width: 0%; background: color-mix(in srgb, var(--accent) 15%, transparent);
    pointer-events: none; z-index: 0; transition: width 0.15s linear;
}
.pp-cover {
    width: 42px; height: 42px; border-radius: 4px; object-fit: cover;
    background: var(--card-bg); flex-shrink: 0; z-index: 1;
}
.pp-info { min-width: 0; flex-shrink: 1; z-index: 1; }
.pp-track {
    white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
    font-size: 13px; font-weight: 500; line-height: 1.3;
}
.pp-narrator {
    font-size: 11px; color: var(--text-muted); white-space: nowrap;
    overflow: hidden; text-overflow: ellipsis; font-style: italic; line-height: 1.2;
}
.pp-controls { display: flex; align-items: center; gap: 2px; flex-shrink: 0; z-index: 1; }
.pp-btn {
    background: none; border: none; color: var(--text); font-size: 16px;
    cursor: pointer; padding: 5px; line-height: 1; flex-shrink: 0;
}
.pp-btn:hover { color: var(--accent); }
.pp-scrubber { flex: 2; display: flex; align-items: center; min-width: 0; z-index: 1; }
.pp-scrubber input[type=range] { width: 100%; cursor: pointer; accent-color: var(--accent); }
.pp-time { font-size: 11px; color: var(--text-muted); white-space: nowrap; min-width: 80px; text-align: center; z-index: 1; }

/* Sleep timer (mini) */
.pp-sleep-btn { position: relative; z-index: 1; }
.pp-sleep-btn.active { color: var(--accent); }
.pp-sleep-menu {
    display: none; position: absolute; bottom: calc(100% + 8px); right: 0;
    background: var(--bg); border: 1px solid var(--border); border-radius: 8px;
    padding: 4px; min-width: 130px; box-shadow: 0 4px 12px var(--shadow); z-index: 100;
}
.pp-sleep-menu.open { display: flex; flex-direction: column; }
.pp-sleep-menu button {
    display: block; width: 100%; padding: 7px 10px; background: none;
    border: none; color: var(--text); text-align: left; cursor: pointer;
    font-size: 0.8rem; border-radius: 4px;
}
.pp-sleep-menu button:hover { background: rgba(255,255,255,0.06); }
.pp-sleep-indicator { font-size: 10px; color: var(--accent); white-space: nowrap; display: none; z-index: 1; }
.pp-sleep-indicator.active { display: block; }

/* ── Fullscreen Player ── */
.fs-player {
    position: fixed; inset: 0; z-index: 10000; background: var(--bg);
    display: flex; flex-direction: column; animation: fs-fade-in 0.2s ease;
}
@keyframes fs-fade-in {
    from { opacity: 0; transform: translateY(20px); }
    to { opacity: 1; transform: translateY(0); }
}
.fs-close {
    position: absolute; top: calc(12px + env(safe-area-inset-top, 0px));
    right: 12px; background: none; border: none; color: var(--text-muted);
    font-size: 28px; cursor: pointer; z-index: 1; padding: 4px 10px;
}
.fs-close:hover { color: var(--text); }
.fs-scroll {
    flex: 1; overflow-y: auto; display: flex; align-items: center;
    justify-content: center; padding: 24px 40px;
    padding-top: calc(50px + env(safe-area-inset-top, 0px));
}
.fs-cover {
    max-width: 500px; max-height: 500px; width: auto; height: auto;
    object-fit: contain; border-radius: 14px;
    box-shadow: 0 8px 32px var(--shadow); flex-shrink: 0;
}
.fs-bottom {
    position: relative; flex-shrink: 0; display: flex; flex-direction: column;
    align-items: center; padding: 16px 24px 28px;
    padding-bottom: calc(28px + var(--safe-bottom));
    background: var(--bg); border-top: 1px solid var(--border);
}
.fs-title { font-size: 1.2rem; font-weight: 700; text-align: center; margin-bottom: 2px; }
.fs-narrator { color: var(--text-muted); font-size: 0.85rem; font-style: italic; margin-bottom: 2px; }
.fs-chapter { color: var(--accent); font-size: 0.8rem; margin-bottom: 4px; }
.fs-progress-summary { font-size: 0.7rem; color: var(--text-muted); margin-bottom: 6px; }
.fs-scrubber { width: 100%; max-width: 400px; margin-bottom: 2px; }
.fs-scrubber input[type=range] { width: 100%; cursor: pointer; accent-color: var(--accent); }
.fs-time-row {
    display: flex; justify-content: space-between; width: 100%;
    max-width: 400px; margin-top: -4px; margin-bottom: 8px;
}
.fs-time-elapsed, .fs-time-remaining { color: var(--text-muted); font-size: 0.75rem; }
.fs-controls { display: flex; align-items: center; gap: 12px; margin-bottom: 6px; }
.fs-btn {
    background: none; border: none; color: var(--text); cursor: pointer;
    padding: 8px; display: flex; align-items: center; justify-content: center;
}
.fs-btn:hover { color: var(--accent); }
.fs-btn-play {
    width: 56px; height: 56px; border-radius: 50%;
    background: var(--accent); color: #fff;
}
.fs-btn-play:hover { background: var(--accent-hover); color: #fff; }
.skip-btn svg { width: 36px; height: 36px; }

/* FS sleep */
.fs-sleep-wrap { position: relative; display: flex; align-items: center; gap: 4px; }
.fs-sleep-btn {
    background: none; border: none; color: var(--text-muted); cursor: pointer;
    padding: 8px; display: flex; align-items: center;
}
.fs-sleep-btn:hover { color: var(--accent); }
.fs-sleep-btn.active { color: var(--accent); }
.fs-sleep-indicator { font-size: 0.7rem; color: var(--accent); white-space: nowrap; }
.fs-sleep-menu {
    display: none; position: absolute; bottom: calc(100% + 4px);
    left: 50%; transform: translateX(-50%); background: var(--card-bg);
    border: 1px solid var(--border); border-radius: 8px; padding: 4px;
    min-width: 140px; box-shadow: 0 4px 12px var(--shadow); z-index: 10;
}
.fs-sleep-menu.open { display: flex; flex-direction: column; }
.fs-sleep-menu button {
    display: block; width: 100%; padding: 8px 12px; background: none;
    border: none; color: var(--text); text-align: left; cursor: pointer;
    font-size: 0.85rem; border-radius: 4px;
}
.fs-sleep-menu button:hover { background: color-mix(in srgb, var(--accent) 10%, transparent); }

/* FS chapters */
.fs-chapter-list {
    width: 100%; max-width: 400px; max-height: 250px; overflow-y: auto; margin-top: 0.5rem;
}

.text-btn {
    color: var(--accent); font-size: 0.85rem; background: none;
    border: none; cursor: pointer; padding: 0.5rem 1rem;
}
.danger-btn {
    padding: 0.75rem; background: var(--danger); border-radius: var(--radius);
    font-weight: 600; color: #fff; border: none; cursor: pointer; width: 100%;
}

/* ── Settings Modal ── */
.modal {
    position: fixed; inset: 0; background: rgba(0,0,0,0.6); z-index: 200;
    display: flex; align-items: center; justify-content: center;
}
.settings-content {
    background: var(--bg); border: 1px solid var(--border); border-radius: 12px;
    width: 90%; max-width: 400px; max-height: 80vh; overflow-y: auto;
}
.modal-header {
    display: flex; align-items: center; justify-content: space-between;
    padding: 1rem; border-bottom: 1px solid var(--border);
}
.modal-body { padding: 1rem; display: flex; flex-direction: column; gap: 1rem; }
.modal-body label {
    display: flex; justify-content: space-between; align-items: center; padding: 0.5rem 0;
}
.modal-body select {
    padding: 0.5rem; background: var(--card-bg); border: 1px solid var(--border);
    border-radius: var(--radius); color: var(--text); font-size: 0.9rem;
}
.server-info { padding-top: 1rem; border-top: 1px solid var(--border); font-size: 0.8rem; color: var(--text-muted); }
.server-info p { margin-bottom: 0.25rem; }

/* ── Loading ── */
.loading {
    display: flex; align-items: center; justify-content: center;
    padding: 3rem; color: var(--text-muted);
}
.loading::after {
    content: ""; width: 20px; height: 20px;
    border: 2px solid rgba(255,255,255,0.1); border-top-color: var(--accent);
    border-radius: 50%; animation: spin 0.6s linear infinite; margin-left: 0.5rem;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* ── MOBILE ── */
@media (max-width: 600px) {
    .pp-scrubber { display: none; }
    .pp-time { display: none; }
    .pp-info { flex: 1; }
    .pp-scrubber-bg { display: block; }
    .pp-sleep-btn { display: none; }
    .pp-sleep-indicator { display: none !important; }

    .grid { grid-template-columns: repeat(auto-fill, minmax(110px, 1fr)); gap: 0.75rem; }
    .h-scroll .card { width: 110px; }
    .h-scroll .card img { width: 110px; height: 110px; }

    .detail-header { flex-direction: column; align-items: center; text-align: center; }
    .detail-cover { width: 180px; height: 180px; }

    .fs-scroll {
        padding: 20px 16px 12px;
        padding-top: calc(50px + env(safe-area-inset-top, 0px));
        flex-direction: column; gap: 16px; align-items: center;
        justify-content: flex-start;
    }
    .fs-cover { max-width: 280px; max-height: 280px; }
    .fs-bottom {
        padding: 10px 16px 12px;
        padding-bottom: calc(12px + var(--safe-bottom));
    }

    .tab span { font-size: 0.6rem; }
}

@media (min-width: 768px) {
    .grid { grid-template-columns: repeat(auto-fill, minmax(160px, 1fr)); }
    .detail-cover { width: 200px; height: 200px; }
    .h-scroll .card { width: 150px; }
    .h-scroll .card img { width: 150px; height: 150px; }
}
