.container {
    max-width: 800px;
    margin: 0 auto;
    background: white;
    border-radius: 15px;
    box-shadow: 0 20px 40px rgba(0,0,0,0.1);
    overflow: hidden;
}

/* The mini-player is a fixed overlay at the bottom. Treat the top of that
   player as the bottom of the scroll viewport so scrollIntoView never parks a
   focused row behind it. The top padding leaves headroom so a focused item at
   the top still has room for the lift-and-grow animation. */
html {
    scroll-padding-top: 70px;
    scroll-padding-bottom: 120px;
}

header {
    background: linear-gradient(135deg, #4facfe 0%, #00f2fe 100%);
    color: white;
    text-align: center;
    padding: 10px 20px;
}

.app-logo {
    max-height: 54px;
    max-width: 100%;
    object-fit: contain;
    filter: brightness(1.1);
}

/* The parenthetical portion of a title drops to its own smaller, non-bold line. */
.title-paren {
    display: block;
    font-size: 0.88em;
    font-weight: 400;
}

.library-main-header {
    position: relative;
}

.account-icon {
    position: absolute;
    top: 50%;
    right: 16px;
    transform: translateY(-50%);
    display: flex;
    align-items: center;
    justify-content: center;
    width: 40px;
    height: 40px;
    border-radius: 50%;
    color: white;
    background: rgba(255, 255, 255, 0.18);
    transition: background 0.2s ease;
}

.account-icon:hover {
    background: rgba(255, 255, 255, 0.35);
}

/* The account shortcut is for phones/desktops; on a TV the account is managed
   elsewhere, so it is hidden once remote navigation is in use. */
body.tv-nav .account-icon {
    display: none;
}

.admin-button {
    position: absolute;
    top: 50%;
    left: 16px;
    transform: translateY(-50%);
    color: white;
    text-decoration: none;
    font-weight: 600;
    font-size: 0.9em;
    background: rgba(255, 255, 255, 0.18);
    padding: 8px 14px;
    border-radius: 8px;
}

.admin-button:hover {
    background: rgba(255, 255, 255, 0.35);
}

body.tv-nav .admin-button {
    display: none;
}

.player-section {
    padding: 30px;
    background: #f8f9fa;
    border-bottom: 1px solid #e9ecef;
    position: relative;
}

.player-busy-overlay {
    display: none;
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    align-items: center;
    justify-content: center;
    background: rgba(248, 249, 250, 0.75);
    z-index: 5;
}

.player-busy-overlay.active {
    display: flex;
}

.player-busy-spinner {
    width: 48px;
    height: 48px;
    border: 5px solid rgba(102, 126, 234, 0.25);
    border-top-color: #667eea;
    border-radius: 50%;
    animation: player-busy-spin 0.8s linear infinite;
}

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

#player-container {
    display: flex;
    align-items: center;
    gap: 25px;
    margin-bottom: 10px;
    height: 240px;
}

#book-cover-container {
    margin-left: 30px;
    border-radius: 12px;
    box-shadow: 0 4px 16px rgba(0,0,0,0.5);
}

.book-cover-wrapper {
    border-radius: 12px;
    overflow: hidden;
}

#controls-container {
    flex: 1;
}

.current-book-thumbnail {
    width: 160px;
}

.current-track-info {
    flex: 1;
    text-align: center;
}

.current-series {
    font-size: 1.2em;
    font-weight: bold;
    color: #667eea;
    margin-bottom: 8px;
}

.series-part {
    font-weight: bold;
}

.by-text,
.author-part {
    font-weight: normal;
    color: #999;
    font-style: italic;
    font-size: 0.9em;
}

.by-text {
    margin: 0 1px;
}

.current-book-title {
    font-size: 1.6em;
    font-weight: bold;
    color: #2c3e50;
    margin-bottom: 8px;
}

.current-chapter-title {
    font-size: 1.1em;
    color: #7f8c8d;
    margin-bottom: 5px;
}

.custom-controls {
    display: flex;
    justify-content: center;
    align-items: center;
    gap: 15px;
    margin: 10px 0 5px 0;
    flex-wrap: wrap;
}

.control-btn {
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    border: none;
    border-radius: 50%;
    width: 50px;
    height: 50px;
    font-size: 1.6em;
    cursor: pointer;
    transition: all 0.3s ease;
    display: flex;
    align-items: center;
    justify-content: center;
    color: white;
    box-shadow: 0 4px 8px rgba(0,0,0,0.2);
    line-height: 0.8;
    font-family: monospace;
}

.control-btn:hover {
    transform: scale(1.1);
    box-shadow: 0 6px 12px rgba(0,0,0,0.3);
}

.control-btn:disabled {
    background: #6c757d;
    cursor: not-allowed;
    transform: none;
    box-shadow: none;
}

.control-btn:disabled:hover {
    background: #6c757d;
    transform: none;
    box-shadow: none;
}

/* Sleep Control */
.sleep-control {
    margin-top: 5px;
    text-align: center;
}

.sleep-btn {
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    border: none;
    border-radius: 50%;
    width: 50px;
    height: 50px;
    color: white;
    font-size: 1.2em;
    cursor: pointer;
    transition: all 0.3s ease;
    box-shadow: 0 2px 6px rgba(0,0,0,0.2);
}

.sleep-btn:hover {
    transform: scale(1.1);
    box-shadow: 0 6px 12px rgba(0,0,0,0.3);
}

.sleep-btn.active {
    background: linear-gradient(135deg, #2ecc71 0%, #27ae60 100%);
    animation: sleep-glow 3s ease-in-out infinite;
}

.sleep-btn.sleep-disabled {
    background: #6c757d;
    cursor: not-allowed;
    transform: none;
    box-shadow: none;
}

.sleep-btn.sleep-disabled:hover {
    background: #6c757d;
    transform: none;
    box-shadow: none;
}

.sleep-btn-inline {
    margin-left: 10px;
}

@keyframes sleep-glow {
    0% { 
        transform: scale(1);
        box-shadow: 0 0 5px rgba(46, 204, 113, 0.4);
    }
    50% { 
        transform: scale(1.03);
        box-shadow: 0 0 20px rgba(46, 204, 113, 0.8), 0 0 30px rgba(46, 204, 113, 0.4);
    }
    100% { 
        transform: scale(1);
        box-shadow: 0 0 5px rgba(46, 204, 113, 0.4);
    }
}

/* App Footer */
.app-footer {
    text-align: center;
    padding: 20px;
    margin-top: 40px;
    border-top: 1px solid #eee;
}

.version-link {
    color: #999;
    font-size: 0.8em;
    padding: 5px 10px;
}

/* PWA Mode */
.pwa-mode {
    padding-top: env(safe-area-inset-top);
    padding-bottom: env(safe-area-inset-bottom);
}

/* Tablet Mode - when width < 50% of height */
.viewport-tablet #player-container {
    flex-direction: column;
    align-items: center;
    height: auto;
    gap: 15px;
}

.viewport-tablet .current-book-thumbnail {
    width: 120px;
    height: auto;
    max-height: 180px;
    object-fit: contain;
}

/* Modal Styles */
/* Sleep Settings Styles */
.sleep-setting {
    margin-bottom: 20px;
}

.sleep-setting label {
    display: block;
    margin-bottom: 8px;
    font-weight: 500;
    color: #333;
}

.sleep-setting input {
    width: 100%;
    padding: 10px;
    border: 2px solid #e9ecef;
    border-radius: 8px;
    font-size: 1em;
}

.sleep-setting input:focus {
    outline: none;
    border-color: #4facfe;
    box-shadow: 0 0 0 3px rgba(79, 172, 254, 0.1);
}

.sleep-info {
    margin-top: 15px;
    padding: 10px;
    background: #f8f9fa;
    border-radius: 8px;
    text-align: center;
}

.sleep-info small {
    color: #666;
    font-style: italic;
}

.control-btn:active {
    transform: scale(0.95);
}

.time-display {
    display: flex;
    align-items: center;
    gap: 10px;
    margin-top: 20px;
    font-size: 0.9em;
    color: #666;
}

.progress-container {
    flex: 1;
    height: 6px;
    background: #e0e0e0;
    border-radius: 3px;
    overflow: hidden;
    cursor: pointer;
}

.progress-bar {
    width: 100%;
    height: 100%;
    position: relative;
}

.progress-fill {
    height: 100%;
    background: linear-gradient(90deg, #667eea, #764ba2);
    width: 0%;
    transition: width 0.1s ease;
    border-radius: 3px;
}

/* Book progress bar */
.book-progress-display {
    display: flex;
    align-items: center;
    gap: 10px;
    margin-top: 15px;
    font-size: 0.85em;
    color: #888;
}

.book-progress-container {
    flex: 1;
    height: 4px;
    background: #f0f0f0;
    border-radius: 2px;
    overflow: hidden;
}

.book-progress-bar {
    width: 100%;
    height: 100%;
    position: relative;
}

.book-progress-fill {
    height: 100%;
    background: linear-gradient(90deg, #95a5a6, #7f8c8d);
    width: 0%;
    transition: width 0.3s ease;
    border-radius: 2px;
}

#book-current-time,
#book-total-time {
    font-size: 0.8em;
    color: #999;
    min-width: 45px;
}

.playlist-section {
    padding: 30px;
}

/* Two-view shell */
.view {
    display: none;
}

.view.active {
    display: block;
}

#library-view .playlist-section {
    padding-bottom: 100px;
}

/* Library Header Component */
.library-header {
    display: flex;
    flex-direction: column;
    align-items: stretch;
    gap: 12px;
    margin-bottom: 20px;
    padding-bottom: 15px;
    border-bottom: 2px solid #e9ecef;
}

.search-container {
    display: flex;
    align-items: center;
    width: 100%;
    position: relative;
}

#library-search {
    padding: 8px 36px 8px 12px;
    border: 2px solid #e9ecef;
    border-radius: 8px;
    font-size: 0.9em;
    width: 100%;
    transition: border-color 0.3s ease;
}

.search-clear {
    position: absolute;
    right: 8px;
    top: 50%;
    transform: translateY(-50%);
    display: none;
    width: 22px;
    height: 22px;
    padding: 0;
    border: none;
    border-radius: 50%;
    background: #eef0fc;
    color: #667eea;
    font-size: 1.1em;
    font-weight: 600;
    line-height: 22px;
    text-align: center;
    cursor: pointer;
    transition: all 0.2s ease;
}

.search-clear:hover {
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    color: white;
}

.search-clear.visible {
    display: block;
}

#library-search:focus {
    outline: none;
    border-color: #4facfe;
    box-shadow: 0 0 0 3px rgba(79, 172, 254, 0.1);
}

.sort-radios {
    display: inline-flex;
    align-self: center;
    border: 2px solid #667eea;
    border-radius: 8px;
    overflow: hidden;
}

.sort-radio {
    padding: 8px 16px;
    background: white;
    color: #667eea;
    border: none;
    border-right: 1px solid #d6dbf5;
    font-size: 0.9em;
    font-weight: 500;
    cursor: pointer;
    transition: all 0.2s ease;
}

.sort-radio:last-child {
    border-right: none;
}

.sort-radio:hover {
    background: #eef0fc;
}

.sort-radio.active {
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    color: white;
}

/* The slim header (small logo + back) only appears once you drill into a group;
   the large logo header is reserved for the front page. */
#library-view .library-sub-header {
    display: none;
}

#library-view.group-mode .library-main-header,
#library-view.group-mode .library-header {
    display: none;
}

#library-view.group-mode .library-sub-header {
    display: flex;
}

.loading {
    text-align: center;
    padding: 40px;
    color: #7f8c8d;
    font-size: 1.1em;
}

.error {
    text-align: center;
    padding: 20px;
    color: #e74c3c;
    background: #fdf2f2;
    border-radius: 8px;
    margin: 20px 0;
}

/* Group cards (Series / Author group mode) */
.group-grid {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    gap: 16px;
    padding-top: 10px;
}

@media (min-width: 640px) {
    .group-grid {
        grid-template-columns: repeat(4, 1fr);
        gap: 24px;
    }
}

.group-card {
    cursor: pointer;
    text-align: center;
    transition: transform 0.2s ease;
}

.group-card:hover {
    transform: translateY(-3px);
}

.card-fan {
    position: relative;
    height: 150px;
    display: flex;
    align-items: flex-end;
    justify-content: center;
    margin-bottom: 12px;
}

.fan-card {
    position: absolute;
    bottom: 0;
    height: 128px;
    width: auto;
    border-radius: 6px;
    box-shadow: 0 4px 14px rgba(0,0,0,0.45);
    transform-origin: bottom center;
    transition: transform 0.25s ease;
}

.fan-card-empty {
    position: relative;
    height: 128px;
    width: 85px;
    border-radius: 6px;
    background: #2c2f48;
    box-shadow: 0 4px 14px rgba(0,0,0,0.45);
}

.group-card-title {
    font-weight: 600;
    color: #2c3e50;
    font-size: 1em;
    line-height: 1.2;
}

.group-card-count {
    color: #7f8c8d;
    font-size: 0.85em;
    margin-top: 3px;
}

/* Group drill-down header */
.group-detail-title {
    font-size: 1.3em;
    font-weight: 600;
    color: #2c3e50;
    margin-bottom: 20px;
    padding-bottom: 12px;
    border-bottom: 2px solid #e9ecef;
}

/* Book rows (flat All list and group drill-down) */
.book-row {
    display: flex;
    align-items: stretch;
    gap: 15px;
    background: white;
    border: 1px solid #c3c9d0;
    border-radius: 8px;
    padding: 12px 16px 8px;
    margin: 0 auto 8px;
    max-width: 80%;
    cursor: pointer;
    transition: all 0.2s ease;
    box-shadow: 0 2px 6px rgba(0, 0, 0, 0.12);
}

.book-row:hover {
    background: #f8f9fa;
    border-color: #667eea;
    transform: translateY(-1px);
    box-shadow: 0 4px 12px rgba(102, 126, 234, 0.15);
}

.book-row-cover-col {
    display: flex;
    flex-direction: column;
    align-items: center;
    flex-shrink: 0;
    width: 64px;
}

.book-row-cover {
    position: relative;
    line-height: 0;
}

.book-row-cover img {
    width: 64px;
    height: auto;
    display: block;
    border-radius: 6px;
    box-shadow: 0 2px 8px rgba(0,0,0,0.2);
}

.book-row-cover-spacer {
    flex: 1;
}

.book-read-badge {
    position: absolute;
    top: -6px;
    right: -6px;
    width: 22px;
    height: 22px;
    border-radius: 50%;
    background: #2ecc71;
    display: flex;
    align-items: center;
    justify-content: center;
    box-shadow: 0 1px 4px rgba(0,0,0,0.35);
}

.book-row-main {
    flex: 1;
    min-width: 0;
    display: flex;
    flex-direction: column;
}

.book-row-bottom {
    display: flex;
    justify-content: space-between;
    gap: 12px;
}

.book-row-meta {
    min-width: 0;
    display: flex;
    flex-direction: column;
    gap: 4px;
}

.book-row .resume-btn {
    width: 44px;
    height: 44px;
    font-size: 1.3em;
    flex-shrink: 0;
    align-self: flex-start;
}

.book-title-row {
    display: flex;
    align-items: baseline;
    flex-wrap: wrap;
}

.book-year {
    color: #7f8c8d;
    font-size: 0.9em;
    font-weight: 400;
    opacity: 0.8;
}

.book-title {
    font-weight: 600;
    font-size: 1.05em;
    line-height: 1.25;
    color: #2c3e50;
}

.book-author {
    font-size: 0.95em;
    font-style: italic;
    color: #666;
    opacity: 0.9;
}

.chapter-count {
    color: #7f8c8d;
    font-size: 0.9em;
}

.book-duration {
    color: #667eea;
    font-size: 0.9em;
    font-weight: 500;
    background: rgba(102, 126, 234, 0.1);
    padding: 2px 8px;
    border-radius: 12px;
}

/* Now Playing view */
.now-playing-header {
    position: relative;
    background: linear-gradient(135deg, #4facfe 0%, #00f2fe 100%);
    padding: 12px 16px;
    display: flex;
    align-items: center;
}

.np-logo {
    position: absolute;
    left: 50%;
    transform: translateX(-50%);
    height: 48px;
    object-fit: contain;
    filter: brightness(1.1);
}

.np-back-btn {
    background: rgba(255,255,255,0.2);
    color: white;
    border: none;
    padding: 8px 14px;
    border-radius: 8px;
    font-size: 0.95em;
    font-weight: 500;
    cursor: pointer;
    transition: background 0.2s ease;
}

.np-back-btn:hover {
    background: rgba(255,255,255,0.35);
}

.np-back-btn .back-arrow {
    font-size: 0.7em;
    vertical-align: middle;
}

.now-playing-chapters {
    padding: 20px 30px 40px;
}

.np-book-title {
    font-size: 1.3em;
    font-weight: 600;
    color: #2c3e50;
    margin-bottom: 15px;
    padding-bottom: 10px;
    border-bottom: 2px solid #e9ecef;
}

.chapter-list {
    display: flex;
    flex-direction: column;
    border: 1px solid #e9ecef;
    border-radius: 8px;
    overflow: hidden;
}

.chapter-item {
    padding: 8px 16px;
    border-bottom: 1px solid #f1f3f4;
    cursor: pointer;
    transition: all 0.2s ease;
    position: relative;
    user-select: none;
    touch-action: manipulation;
}

.chapter-item:last-child {
    border-bottom: none;
}

/* Hover styling only on devices with a real pointer, so a touch tap does not
   leave a "stuck" hover indent behind. */
@media (hover: hover) {
    .chapter-item:hover {
        background: #f8f9fa;
        padding-left: 25px;
    }
}

.chapter-item.active {
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    color: white;
    font-weight: 500;
    position: relative;
    overflow: hidden;
}

/* Progress bar for active chapter */
.chapter-item.active::before {
    content: '';
    position: absolute;
    top: 0;
    right: 0;
    bottom: 0;
    width: var(--chapter-progress, 100%);
    background: rgba(255, 255, 255, 0.1);
    border-left: 2px solid rgba(255, 255, 255, 0.3);
    transition: width 0.3s ease;
    z-index: 1;
}

/* Ensure text stays above progress bar */
.chapter-item.active .chapter-title,
.chapter-item.active .chapter-duration {
    position: relative;
    z-index: 2;
}

@media (hover: hover) {
    .chapter-item.active:hover {
        background: linear-gradient(135deg, #5a67d8 0%, #6b46a3 100%);
        padding-left: 20px;
    }
}

.chapter-item {
    display: flex;
    justify-content: space-between;
    align-items: center;
}

.chapter-title {
    font-size: 0.95em;
    flex: 1;
}

.chapter-duration {
    font-size: 0.85em;
    color: #7f8c8d;
    font-weight: 500;
    margin-left: 10px;
}

.chapter-item.active .chapter-duration {
    color: rgba(255,255,255,0.8);
}

/* Persistent mini-player */
.mini-player {
    position: fixed;
    left: 50%;
    transform: translateX(-50%);
    bottom: 0;
    width: 100%;
    max-width: 800px;
    background: #1f2233;
    color: white;
    box-shadow: 0 -4px 16px rgba(0,0,0,0.3);
    z-index: 900;
}

.mini-progress {
    height: 4px;
    background: rgba(255,255,255,0.15);
}

.mini-progress-fill {
    height: 100%;
    width: 0%;
    background: #2ecc71;
    transition: width 0.2s ease;
}

.mini-player-body {
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 8px 14px;
    cursor: pointer;
}

.mini-cover {
    height: 44px;
    width: auto;
    border-radius: 4px;
    box-shadow: 0 2px 6px rgba(0,0,0,0.4);
}

.mini-info {
    flex: 1;
    min-width: 0;
}

.mini-title {
    font-size: 0.95em;
    font-weight: 600;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.mini-subtitle {
    font-size: 0.8em;
    color: #aab;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.mini-play-pause {
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    border: none;
    color: white;
    width: 44px;
    height: 44px;
    border-radius: 50%;
    font-size: 1.3em;
    cursor: pointer;
    flex-shrink: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    line-height: 0.8;
    font-family: monospace;
}

.pwa-mode .mini-player-body {
    padding-bottom: calc(8px + env(safe-area-inset-bottom));
}

/* Responsive design */
@media (max-width: 600px) {
    .container {
        margin: 0;
        border-radius: 0;
    }
    
    .player-section,
    .playlist-section {
        padding: 20px;
    }
    
    header h1 {
        font-size: 1.5em;
    }
    
    .custom-controls {
        gap: 10px;
    }
    
    .control-btn {
        width: 45px;
        height: 45px;
        font-size: 1em;
    }

    .group-grid {
        gap: 10px;
    }

    .card-fan {
        height: 95px;
    }

    .fan-card,
    .fan-card-empty {
        height: 74px;
    }

    .fan-card-empty {
        width: 49px;
    }
}

/* TV remote (D-pad) focus styling. Only active once the remote is used, so a
   touch or mouse user never sees these outlines. */
/* TV remote selection on cards and book rows lifts and enlarges the item.
   Only active once the remote is used. */
body.tv-nav .group-card:focus,
body.tv-nav .book-row:focus {
    outline: none;
    transform: translateY(-12px) scale(1.22);
    position: relative;
    z-index: 2;
}

/* Buttons stay put but grow and glow rather than drawing a border. */
body.tv-nav .control-btn:focus,
body.tv-nav .sleep-btn:focus,
body.tv-nav .np-back-btn:focus,
body.tv-nav #mini-play-pause:focus {
    outline: none;
    transform: scale(1.25);
    box-shadow: 0 0 18px 4px rgba(120, 200, 255, 0.85);
    position: relative;
    z-index: 2;
}

/* Controls without an existing all-property transition need one so the grow
   and glow animate both into and out of focus. */
body.tv-nav .np-back-btn,
body.tv-nav #mini-play-pause {
    transition: transform 0.15s ease, box-shadow 0.15s ease;
}

/* The segmented sort control flips its colors - scaling a single segment would
   tear it out of the joined group. */
body.tv-nav .sort-radio:focus {
    outline: none;
    background: #ffd54a;
    color: #2c3e50;
    font-weight: 700;
}

/* The search field and the full-width mini-player bar highlight their
   background rather than scaling, which would distort them. */
body.tv-nav #library-search:focus,
body.tv-nav .mini-player-body:focus {
    outline: none;
    background: rgba(255, 213, 74, 0.25);
}

/* Chapters flip their colors instead of scaling - lifting a list row pushes it
   out of the chapter container. */
body.tv-nav .chapter-item:focus {
    outline: none;
    background: #ffd54a;
    color: #2c3e50;
    font-weight: 600;
}
