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

:root {
    --bg-primary: #0d0d0d;
    --bg-secondary: #1a1a1a;
    --bg-tertiary: #252525;
    --bg-hover: #2f2f2f;
    --text-primary: #e8e8e8;
    --text-secondary: #a0a0a0;
    --accent: #d4a64a;
    --accent-hover: #e8b84a;
    --accent-dim: #8b6914;
    --border: #333333;
    --shadow: rgba(0, 0, 0, 0.4);
}

body {
    font-family: 'Noto Sans Sinhala', sans-serif;
    background-color: var(--bg-primary);
    color: var(--text-primary);
    line-height: 1.6;
    height: 100vh;
    display: flex;
    flex-direction: column;
    overflow: hidden;
}

/* Allow scrolling on mobile for keyboard accessibility */
@media (max-width: 900px) {
    body {
        height: auto;
        min-height: 100vh;
        overflow-y: auto;
    }
}

/* Header */
.header {
    background-color: var(--bg-secondary);
    border-bottom: 1px solid var(--border);
    padding: 0.75rem 2rem;
    flex-shrink: 0;
}

.header-content {
    text-align: center;
    margin-bottom: 1rem;
}

.site-title {
    font-size: 1.8rem;
    font-weight: 700;
    color: var(--accent);
    margin-bottom: 0.25rem;
}

.site-subtitle {
    display: none;
    font-size: 0.9rem;
    color: var(--text-secondary);
}

/* Navigation */
.nav {
    display: flex;
    justify-content: center;
    gap: 0.5rem;
    flex-wrap: wrap;
}

.nav-btn {
    background: var(--bg-tertiary);
    border: 1px solid var(--border);
    color: var(--text-secondary);
    padding: 0.6rem 1.2rem;
    border-radius: 6px;
    cursor: pointer;
    font-family: inherit;
    font-size: 0.85rem;
    transition: all 0.2s ease;
}

.nav-btn:hover {
    background: var(--bg-hover);
    color: var(--text-primary);
}

.nav-btn.active {
    background: var(--accent);
    border-color: var(--accent);
    color: var(--bg-primary);
    font-weight: 600;
}

/* Main Content */
.main-content {
    display: flex;
    flex: 1;
    overflow: hidden;
    max-width: 1280px;
    margin: 0 auto;
    width: 100%;
    min-height: 0;
}

/* Sidebar */
.sidebar {
    width: 380px;
    min-width: 380px;
    background: var(--bg-secondary);
    border-right: 1px solid var(--border);
    display: flex;
    flex-direction: column;
    min-height: 0;
    overflow: hidden;
}

.search-box {
    padding: 0.75rem 1rem;
    border-bottom: 1px solid var(--border);
    display: flex;
    align-items: center;
    gap: 0.4rem;
    flex-wrap: nowrap;
}

.search-box input {
    flex: 1;
    min-width: 0;  /* Allows flex item to shrink below content size */
    padding: 0.7rem 1rem;
    background: var(--bg-tertiary);
    border: 1px solid var(--border);
    border-radius: 6px;
    color: var(--text-primary);
    font-family: inherit;
    font-size: 0.9rem;
}

.search-box input::placeholder {
    color: var(--text-secondary);
}

.search-box input:focus {
    outline: none;
    border-color: var(--accent);
}

.search-btn,
.clear-btn {
    width: 40px;
    height: 40px;
    border: 1px solid var(--border);
    background: var(--bg-secondary);
    color: var(--text-primary);
    border-radius: 6px;
    cursor: pointer;
    font-size: 1.5rem;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: all 0.2s ease;
    flex-shrink: 0;
}

.search-btn:hover,
.clear-btn:hover {
    background: var(--accent);
    color: var(--bg-primary);
    border-color: var(--accent);
}

.clear-btn {
    font-size: 1.2rem;  /* Smaller, less prominent */
    line-height: 1;
}

/* Transliteration Preview - Fixed width inline box */
.transliteration-preview {
    padding: 0 0.4rem;
    font-size: 0.7em;
    color: var(--text-secondary);
    width: 90px;  /* Compact fixed width - buttons stay in place */
    min-width: 90px;
    max-width: 90px;
    overflow-x: auto;
    overflow-y: hidden;
    white-space: nowrap;
    flex-shrink: 0;
    border-left: 1px solid var(--border);
    border-right: 1px solid var(--border);
    text-align: left;
}

.transliteration-preview small {
    font-style: italic;
    display: inline-block;
}

#sinhala-preview {
    color: var(--accent);
    font-weight: 500;
}

/* Tree Container */
.tree-container {
    flex: 1;
    overflow-y: auto;
    padding: 1rem;
}

.tree-container::-webkit-scrollbar {
    width: 8px;
}

.tree-container::-webkit-scrollbar-track {
    background: var(--bg-secondary);
}

.tree-container::-webkit-scrollbar-thumb {
    background: var(--border);
    border-radius: 4px;
}

.tree-container::-webkit-scrollbar-thumb:hover {
    background: var(--text-secondary);
}

/* Tree Items - Compact */
.tree-item {
    margin-bottom: 0.1rem;
    line-height: 1.3;
}

.tree-label {
    display: flex;
    align-items: flex-start;  /* Top-align icons with text */
    padding: 0.15rem 0.25rem;
    cursor: pointer;
    border-radius: 4px;
    transition: background 0.15s ease;
    gap: 0.5rem;
}

.tree-label:hover {
    background: var(--bg-hover);
}

.tree-toggle {
    width: 20px;
    height: 20px;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 0.9rem;
    opacity: 0.6;  /* Mute folder icons - they're just navigation */
}

/* Play icon for video items - make prominent since they're the actual content */
.tree-icon.play-icon {
    width: 20px;
    height: 20px;
    display: flex;
    align-items: flex-start;  /* Top-align icon with first line of text */
    color: var(--accent-hover);  /* Brighter yellow for prominence */
    font-size: 0.75rem;  /* Slightly larger */
    padding-left: 4px;  /* Adjusted spacing */
    padding-top: 3px;  /* Fine-tune vertical position to align with text baseline */
}

.tree-text {
    flex: 1;
    font-size: 0.9rem;
}

.tree-children {
    display: none;
    padding-left: 1.5rem;
}

.tree-item.expanded > .tree-children {
    display: block;
}

/* Leaf items (clickable videos) */
.tree-leaf .tree-label {
    color: var(--text-secondary);
}

.tree-leaf .tree-label:hover {
    color: var(--accent);
    background: var(--bg-hover);
}

.tree-leaf.active .tree-label {
    background: var(--accent);
    color: var(--bg-primary);
    font-weight: 500;
}

/* Playing indicator for active item */
.tree-leaf.active .tree-text {
    padding-left: 2rem;  /* Space for ►► */
    text-indent: -2rem;  /* Pull first line back */
}

.tree-leaf.active .tree-text::before {
    content: '▶▶ ';
}

/* Hide play icon when item is active (showing ►► instead) */
.tree-leaf.active .tree-icon.play-icon {
    display: none;
}

/* Search Results - Compact */
.search-results {
    padding: 0.5rem;
}

.search-header {
    font-size: 0.9rem;
    color: var(--text-secondary);
    margin-bottom: 0.5rem;
    padding-bottom: 0.25rem;
    border-bottom: 1px solid var(--border);
}

.search-header strong {
    color: var(--accent);
    font-size: 1.1rem;
}

.search-result-item {
    display: flex;
    align-items: flex-start;
    padding: 0.4rem 0.5rem;
    margin-bottom: 0.15rem;
    cursor: pointer;
    border-radius: 4px;
    transition: background 0.15s ease;
    gap: 0.5rem;
}

.search-result-item:hover {
    background: var(--bg-hover);
}

.search-result-item.folder-result {
    opacity: 0.6;
    cursor: default;
}

.search-result-item.folder-result:hover {
    background: transparent;
}

.result-icon {
    color: var(--accent);
    font-size: 0.8rem;
    width: 20px;
    text-align: center;
    flex-shrink: 0;
}

.result-content {
    flex: 1;
    display: flex;
    flex-direction: column;
    gap: 0.25rem;
}

.result-path {
    font-size: 0.75em;
    color: var(--text-secondary);
    opacity: 0.8;
}

.result-title {
    font-size: 1em;
    font-weight: 500;
    color: var(--text-primary);
}

.no-results {
    text-align: center;
    padding: 3rem 1rem;
    color: var(--text-secondary);
}

.no-results p:first-child {
    font-size: 1.1rem;
    margin-bottom: 0.5rem;
}

.no-results-sub {
    font-size: 0.85rem;
    opacity: 0.7;
}

/* Content Area */
.content-area {
    flex: 1;
    display: flex;
    flex-direction: column;
    padding: 2rem;
    overflow: hidden;
    height: 100%;
}

/* Video Container */
.video-container {
    width: 100%;
    max-width: 900px;
    margin: 0 auto;
    aspect-ratio: 16/9;
    background: var(--bg-secondary);
    border-radius: 8px;
    overflow: hidden;
    box-shadow: 0 4px 20px var(--shadow);
}

/* When showing image with instruction text, allow container to expand */
.video-container:has(.placeholder-below-image) {
    aspect-ratio: auto;
    overflow: visible;
    display: flex;
    flex-direction: column;
}

/* Keep image in 16:9 frame when showing with instruction text */
.video-container:has(.placeholder-below-image) img {
    aspect-ratio: 16/9;
    width: 100%;
    height: auto;
    object-fit: cover;
    flex-shrink: 0;
}

.video-container iframe {
    width: 100%;
    height: 100%;
    border: none;
}

.video-container img {
    width: 100%;
    height: 100%;
    object-fit: cover;
}

.placeholder {
    width: 100%;
    height: 100%;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    color: var(--text-secondary);
}

.placeholder-icon {
    font-size: 4rem;
    margin-bottom: 1rem;
    opacity: 0.3;
}

.placeholder p {
    font-size: 1rem;
}

.placeholder-sub {
    font-size: 0.85rem;
    opacity: 0.6;
    margin-top: 0.5rem;
}

/* Placeholder below image - different styling */
.placeholder-below-image {
    height: auto;
    padding: 2rem 1rem;
    justify-content: flex-start;
}

/* Hide instruction message on mobile to save space */
@media (max-width: 900px) {
    .placeholder-below-image {
        display: none;
    }
}

/* Video Info */
.video-info {
    max-width: 900px;
    margin: 1.5rem auto 0;
    width: 100%;
}

.video-title {
    font-size: 1.1rem;
    font-weight: 600;
    color: var(--text-primary);
    margin-bottom: 0.75rem;
}

.video-links {
    display: flex;
    gap: 1rem;
}

.video-link {
    color: var(--accent);
    text-decoration: none;
    font-size: 0.85rem;
    padding: 0.4rem 0.8rem;
    background: var(--bg-tertiary);
    border-radius: 4px;
    transition: all 0.2s ease;
}

.video-link:hover {
    background: var(--accent-dim);
}

/* Media Toggle Buttons */
.media-toggle {
    display: flex;
    gap: 0.5rem;
    margin-bottom: 1rem;
    justify-content: center;
}

.toggle-btn {
    padding: 0.4rem 0.8rem;
    background: var(--bg-tertiary);
    border: 1px solid var(--border);
    color: var(--text-secondary);
    border-radius: 4px;
    cursor: pointer;
    font-family: inherit;
    font-size: 0.8rem;
    transition: all 0.2s ease;
}

.toggle-btn:hover {
    background: var(--bg-hover);
    color: var(--text-primary);
}

.toggle-btn.active {
    background: var(--accent);
    border-color: var(--accent);
    color: var(--bg-primary);
}

/* Inline Links (within title) */
.inline-link {
    color: var(--accent);
    text-decoration: none;
    font-size: 0.85em;
    transition: all 0.2s ease;
}

.inline-link:hover {
    text-decoration: underline;
    color: var(--accent-hover);
}

.inline-link.pdf-link {
    color: #d2691e;
}

.inline-link.pdf-link:hover {
    color: #ff8c00;
}

/* Audio Player */
.audio-player {
    width: 100%;
    height: 100%;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 1.5rem;
    background: var(--bg-secondary);
    padding-top: 1rem;
}

/* Large play/pause button for easy access - minimal style, emoji only */
.play-pause-btn {
    background: transparent;
    border: none;
    font-size: 6rem;  /* Large emoji */
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    transition: all 0.2s ease;
    margin-bottom: 0.5rem;
    padding: 1rem;
}

.play-pause-btn:hover {
    transform: scale(1.1);
    filter: brightness(1.2);
}

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

.play-pause-icon {
    line-height: 1;
    display: block;
}

/* Optical centering: play emoji needs slight offset for visual balance */
.play-pause-btn .play-pause-icon {
    margin-left: 6px;  /* Shift play emoji right for optical center */
}

/* Remove offset when showing pause (it's already centered) */
.play-pause-btn[data-state="playing"] .play-pause-icon {
    margin-left: 0;
}

/* Audio Loading Spinner - YouTube Style */
.audio-loading {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 6.5rem;  /* Match headphone icon size exactly */
    height: 6.5rem;  /* Match headphone icon size exactly */
    margin-bottom: 0.5rem;  /* Match headphone icon margin */
    line-height: 1;  /* Match headphone line-height */
}

.spinner {
    width: 6.5rem;  /* Match headphone icon size in rem units */
    height: 6.5rem;
    position: relative;
}

.spinner::before,
.spinner::after {
    content: '';
    position: absolute;
    border-radius: 50%;
}

.spinner::before {
    width: 100%;
    height: 100%;
    border: 4px solid transparent;
    border-top-color: var(--accent);
    border-right-color: var(--accent);
    animation: yt-spin 0.8s linear infinite;
}

.spinner::after {
    width: 70%;
    height: 70%;
    top: 15%;
    left: 15%;
    border: 4px solid transparent;
    border-bottom-color: rgba(212, 166, 74, 0.5);
    border-left-color: rgba(212, 166, 74, 0.5);
    animation: yt-spin 1.2s linear infinite reverse;
}

@keyframes yt-spin {
    0% { transform: rotate(0deg); }
    100% { transform: rotate(360deg); }
}

.audio-player audio {
    width: 90%;
    max-width: 700px;
    height: 54px;
}

/* Audio Error State */
.audio-player.error {
    gap: 1rem;
}

.error-icon {
    font-size: 3rem;
}

.error-message {
    font-size: 1.1rem;
    color: var(--text-primary);
}

.error-sub {
    font-size: 0.9rem;
    color: var(--text-secondary);
    opacity: 0.7;
}

.retry-btn {
    margin-top: 1rem;
    padding: 0.75rem 1.5rem;
    background: var(--accent);
    color: #000;
    border: none;
    border-radius: 6px;
    font-size: 1rem;
    font-weight: 600;
    cursor: pointer;
    transition: all 0.2s ease;
}

.retry-btn:hover {
    background: var(--accent-hover);
    transform: translateY(-1px);
}

.retry-btn:active {
    transform: translateY(0);
}

/* Audio Controls - Subtle Skip Buttons */
.audio-controls {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 0.75rem;
    margin-top: 2rem;
    margin-bottom: 1.5rem;
    padding-bottom: 0.5rem;
}

.skip-btn {
    padding: 0.5rem 1rem;
    background: transparent;
    border: 1px solid var(--border);
    border-radius: 6px;
    color: var(--text-secondary);
    cursor: pointer;
    transition: all 0.15s ease;
    font-family: inherit;
    font-size: 0.85rem;
    font-weight: 500;
}

.skip-btn:hover {
    background: var(--bg-hover);
    border-color: var(--accent);
    color: var(--accent);
}

/* Inline Tipitaka link (deprecated - kept for compatibility) */
.tipitaka-link {
    color: var(--accent);
    text-decoration: none;
    font-size: 0.85em;
}

.tipitaka-link:hover {
    text-decoration: underline;
}

/* Footer - Compact */
.footer {
    background: var(--bg-secondary);
    border-top: 1px solid var(--border);
    padding: 0.5rem 2rem;
}

.social-links {
    display: flex;
    justify-content: center;
    align-items: center;
    gap: 0.75rem;
}

.social-icon {
    display: inline-block;
    transition: opacity 0.2s ease, transform 0.2s ease;
}

.social-icon img {
    height: 20px;  /* 50% of original 40px */
    width: auto;
    display: block;
}

.social-icon:hover {
    opacity: 0.8;
    transform: translateY(-2px);
}

/* Loading State */
.loading {
    text-align: center;
    padding: 2rem;
    color: var(--text-secondary);
}

/* Sidebar Toggle Button */
.sidebar-toggle {
    display: none;
    width: 100%;
    padding: 0.8rem 1rem;
    background: var(--accent);
    border: none;
    color: var(--bg-primary);
    font-family: inherit;
    font-size: 0.9rem;
    font-weight: 600;
    cursor: pointer;
    align-items: center;
    justify-content: center;
    gap: 0.5rem;
}

.sidebar-toggle:hover {
    background: var(--accent-hover);
}

.toggle-icon {
    font-size: 1.1rem;
}

.sidebar-content {
    display: flex;
    flex-direction: column;
    flex: 1;
    min-height: 0;
}

/* Responsive */
@media (max-width: 900px) {
    /* FRAME 1: Sticky header at top - always visible */
    .header {
        position: sticky;
        top: 0;
        z-index: 100;
        background: var(--bg-secondary);
    }

    .site-title {
        font-size: 1.4rem;
    }

    .site-subtitle {
        display: none;  /* Hide subtitle on mobile to save space */
    }

    /* FRAME 2: Scrollable middle section - video, search, tree scroll together */
    .main-content {
        flex-direction: column;  /* Vertical layout */
        overflow: visible;  /* Allow content to flow naturally */
        min-height: 0;
        flex: none;
        padding-bottom: 70px;  /* Space for fixed footer */
    }

    /* Video appears first in scroll order */
    .content-area {
        order: 1;
        width: 100%;
        padding: 1rem;
        overflow: visible;
        height: auto;
        flex: none;
    }

    /* Keep 16:9 aspect ratio on mobile to match YouTube */
    .video-container {
        aspect-ratio: 16/9;
        width: 100%;
        max-width: 100%;
    }

    /* Optimize audio player layout for mobile - maximize player, minimize gaps */
    .audio-player {
        gap: 0.25rem;  /* Minimal gap to save vertical space */
        padding: 0.25rem 0.5rem;
    }

    .play-pause-btn {
        font-size: 3.6rem;  /* 20% larger - more space from optimized gaps */
        margin-bottom: 0;  /* Remove bottom margin */
        padding: 0.25rem;
    }

    .audio-loading {
        width: 2.8rem;
        height: 2.8rem;
        margin-bottom: 0.25rem;
    }

    .spinner {
        width: 2.8rem;
        height: 2.8rem;
    }

    .audio-player audio {
        width: 90%;
        max-width: 100%;
        min-width: 0;
        height: 40px;  /* Smaller audio controls */
    }

    .audio-controls {
        margin-top: 0.25rem;  /* Reduce top margin to bring buttons closer */
        margin-bottom: 0.25rem;
        gap: 0.5rem;
    }

    .skip-btn {
        width: 32px;
        height: 32px;
        font-size: 0.7rem;
        padding: 0.3rem;
    }

    /* Audio error state - compact for mobile */
    .error-icon {
        font-size: 2rem;  /* Smaller on mobile (was 3rem desktop) */
    }

    .error-message {
        font-size: 0.9rem;  /* Smaller on mobile (was 1.1rem desktop) */
    }

    .error-sub {
        font-size: 0.75rem;  /* Smaller on mobile (was 0.9rem desktop) */
    }

    .retry-btn {
        padding: 0.5rem 1rem;  /* Smaller padding (was 0.75rem 1.5rem) */
        font-size: 0.85rem;  /* Smaller text (was 1rem) */
        margin-top: 0.5rem;  /* Less spacing (was 1rem) */
    }

    /* Search and tree appear below video */
    .sidebar {
        order: 2;
        width: 100%;
        min-width: 100%;
        position: relative;  /* Remove fixed positioning */
        border-right: none;
        border-bottom: 1px solid var(--border);
        overflow: visible;  /* Let content flow naturally */
    }

    /* Tree scrolls with body, not independently */
    .tree-container {
        overflow-y: visible;
        flex: none;
        height: auto;
    }

    .sidebar-content {
        overflow: visible;
        flex: none;
    }

    /* Hide hamburger toggle - not needed */
    .sidebar-toggle {
        display: none;
    }

    /* FRAME 3: Fixed footer at bottom - always visible */
    .footer {
        position: fixed;
        bottom: 0;
        left: 0;
        right: 0;
        z-index: 100;
        padding: 0.5rem 1rem;
        background: var(--bg-secondary);
        box-shadow: 0 -2px 10px var(--shadow);
    }

    .social-icon img {
        height: 20px;  /* Match desktop - 50% of original */
    }

    /* Smaller toggle buttons on mobile (20% smaller than desktop) */
    .toggle-btn {
        padding: 0.3rem 0.6rem;
        font-size: 0.65rem;
    }

    /* Smaller fonts for mobile */
    .tree-text {
        font-size: 0.85rem;
    }

    .video-title {
        font-size: 0.95rem;
    }

    .video-info {
        margin-top: 0.75rem;
    }
}

@media (max-width: 600px) {
    .header {
        padding: 0.75rem;
    }

    .site-title {
        font-size: 1.2rem;
    }

    .nav-btn {
        padding: 0.4rem 0.6rem;
        font-size: 0.7rem;
    }

    .search-box {
        padding: 0.75rem;
    }

    .search-box input {
        padding: 0.5rem 0.75rem;
        font-size: 0.8rem;
    }

    .tree-container {
        padding: 0.75rem;
    }

    .tree-label {
        padding: 0.4rem 0.5rem;
    }

    .tree-text {
        font-size: 0.75rem;
    }

    .video-title {
        font-size: 0.85rem;
    }
}

.inline-link.youtube-link {
    color: #FF0000;
}