/* ==========================================================================
   StreamTube - Modern YouTube-Inspired Theme (Dark / Light Mode + RWD)
   ========================================================================== */

:root,
[data-theme="dark"] {
    --bg-primary: #0f0f0f;
    --bg-secondary: #181818;
    --bg-elevated: #272727;
    --bg-hover: #383838;
    --bg-chip: #272727;
    --bg-chip-active: #f1f1f1;
    --text-primary: #f1f1f1;
    --text-secondary: #aaaaaa;
    --text-chip-active: #0f0f0f;
    --border-color: #303030;
    --brand-red: #ff0033;
    --link-color: #3ea6ff;
    --shadow-sm: 0 2px 8px rgba(0, 0, 0, 0.35);
}

[data-theme="light"] {
    --bg-primary: #ffffff;
    --bg-secondary: #f8f8f8;
    --bg-elevated: #f2f2f2;
    --bg-hover: #e5e5e5;
    --bg-chip: #f2f2f2;
    --bg-chip-active: #0f0f0f;
    --text-primary: #0f0f0f;
    --text-secondary: #606060;
    --text-chip-active: #ffffff;
    --border-color: #e5e5e5;
    --brand-red: #ff0033;
    --link-color: #065fd4;
    --shadow-sm: 0 2px 8px rgba(0, 0, 0, 0.08);
}

*,
*::before,
*::after {
    box-sizing: border-box;
}

html {
    font-size: 16px;
    scroll-behavior: smooth;
}

body {
    margin: 0;
    font-family: "Roboto", "Noto Sans TC", "PingFang HK", "Microsoft JhengHei", -apple-system, BlinkMacSystemFont, sans-serif;
    background-color: var(--bg-primary);
    color: var(--text-primary);
    line-height: 1.5;
    -webkit-font-smoothing: antialiased;
}

a {
    color: inherit;
    text-decoration: none;
}

img {
    max-width: 100%;
    display: block;
}

.sr-only {
    position: absolute;
    width: 1px;
    height: 1px;
    padding: 0;
    margin: -1px;
    overflow: hidden;
    clip: rect(0, 0, 0, 0);
    white-space: nowrap;
    border: 0;
}

/* ==========================================================================
   Top Header Navbar
   ========================================================================== */

.site-header {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    height: 56px;
    padding: 0 16px;
    background-color: var(--bg-primary);
    border-bottom: 1px solid var(--border-color);
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 16px;
    z-index: 1000;
}

.header-left,
.header-right {
    display: flex;
    align-items: center;
    gap: 10px;
    flex-shrink: 0;
}

.header-center {
    flex: 1;
    max-width: 620px;
    display: flex;
    justify-content: center;
}

.icon-btn {
    width: 40px;
    height: 40px;
    border: none;
    border-radius: 50%;
    background: transparent;
    color: var(--text-primary);
    display: inline-flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    transition: background-color 0.15s ease;
}

.icon-btn:hover {
    background-color: var(--bg-elevated);
}

.icon-btn svg {
    width: 24px;
    height: 24px;
    fill: currentColor;
}

.brand-logo {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    font-weight: 700;
    font-size: 1.15rem;
    letter-spacing: -0.02em;
}

.brand-icon {
    width: 30px;
    height: 22px;
    border-radius: 6px;
    background-color: var(--brand-red);
    color: #ffffff;
    display: inline-flex;
    align-items: center;
    justify-content: center;
}

.brand-icon svg {
    width: 16px;
    height: 16px;
    fill: currentColor;
}

.brand-region {
    font-size: 0.65rem;
    font-weight: 500;
    color: var(--text-secondary);
    align-self: flex-start;
    margin-top: 2px;
}

/* Search Bar */
.search-form {
    width: 100%;
    display: flex;
    align-items: center;
}

.search-form input[type="search"] {
    flex: 1;
    height: 40px;
    padding: 0 16px;
    border: 1px solid var(--border-color);
    border-right: none;
    border-radius: 20px 0 0 20px;
    background-color: var(--bg-secondary);
    color: var(--text-primary);
    font-size: 0.95rem;
    outline: none;
    transition: border-color 0.15s ease;
}

.search-form input[type="search"]:focus {
    border-color: var(--link-color);
}

.search-submit {
    height: 40px;
    width: 58px;
    border: 1px solid var(--border-color);
    border-radius: 0 20px 20px 0;
    background-color: var(--bg-elevated);
    color: var(--text-primary);
    display: inline-flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    transition: background-color 0.15s ease;
}

.search-submit:hover {
    background-color: var(--bg-hover);
}

.search-submit svg {
    width: 22px;
    height: 22px;
    fill: currentColor;
}

/* Theme toggle icon visibility */
[data-theme="dark"] .icon-sun { display: block; }
[data-theme="dark"] .icon-moon { display: none; }
[data-theme="light"] .icon-sun { display: none; }
[data-theme="light"] .icon-moon { display: block; }

/* ==========================================================================
   App Shell & Collapsible Sidebar
   ========================================================================== */

.app-shell {
    display: flex;
    padding-top: 56px;
    min-height: 100vh;
}

.sidebar {
    position: fixed;
    top: 56px;
    left: 0;
    bottom: 0;
    width: 240px;
    padding: 12px;
    background-color: var(--bg-primary);
    border-right: 1px solid var(--border-color);
    overflow-y: auto;
    transition: width 0.2s ease, transform 0.2s ease;
    z-index: 900;
}

.sidebar-backdrop {
    display: none;
    position: fixed;
    inset: 0;
    background: rgba(0, 0, 0, 0.55);
    z-index: 850;
}

.nav-section {
    display: flex;
    flex-direction: column;
    gap: 2px;
}

.nav-heading {
    margin: 6px 12px 6px;
    font-size: 0.8rem;
    font-weight: 600;
    color: var(--text-secondary);
    text-transform: uppercase;
    letter-spacing: 0.04em;
}

.nav-item {
    display: flex;
    align-items: center;
    gap: 18px;
    padding: 10px 12px;
    border-radius: 10px;
    font-size: 0.92rem;
    font-weight: 500;
    color: var(--text-primary);
    transition: background-color 0.15s ease;
}

.nav-item:hover {
    background-color: var(--bg-elevated);
}

.nav-item.is-active {
    background-color: var(--bg-elevated);
    font-weight: 700;
}

.nav-icon {
    width: 24px;
    height: 24px;
    flex-shrink: 0;
    display: inline-flex;
    align-items: center;
    justify-content: center;
}

.nav-icon svg {
    width: 22px;
    height: 22px;
    fill: currentColor;
}

.nav-divider {
    border: none;
    border-top: 1px solid var(--border-color);
    margin: 12px 0;
}

.nav-footer {
    padding: 8px 12px;
    font-size: 0.78rem;
    color: var(--text-secondary);
}

.nav-footer p {
    margin: 4px 0;
}

.nav-footer a:hover {
    text-decoration: underline;
}

.main-content {
    flex: 1;
    margin-left: 240px;
    padding: 20px 24px 48px;
    min-width: 0;
    transition: margin-left 0.2s ease;
}

/* Desktop collapsed sidebar state */
body.sidebar-collapsed .sidebar {
    width: 72px;
    padding: 12px 6px;
}

body.sidebar-collapsed .nav-item {
    flex-direction: column;
    gap: 4px;
    padding: 10px 4px;
    text-align: center;
}

body.sidebar-collapsed .nav-label {
    font-size: 0.68rem;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    max-width: 60px;
}

body.sidebar-collapsed .nav-heading,
body.sidebar-collapsed .nav-footer {
    display: none;
}

body.sidebar-collapsed .main-content {
    margin-left: 72px;
}

/* ==========================================================================
   Chips, Page Headers & Alerts
   ========================================================================== */

.category-chips {
    display: flex;
    align-items: center;
    gap: 10px;
    overflow-x: auto;
    padding-bottom: 12px;
    margin-bottom: 16px;
    scrollbar-width: none;
}

.category-chips::-webkit-scrollbar {
    display: none;
}

.chip {
    display: inline-flex;
    align-items: center;
    padding: 6px 14px;
    border-radius: 8px;
    background-color: var(--bg-chip);
    color: var(--text-primary);
    font-size: 0.88rem;
    font-weight: 500;
    white-space: nowrap;
    transition: background-color 0.15s ease;
}

.chip:hover {
    background-color: var(--bg-hover);
}

.chip.is-active {
    background-color: var(--bg-chip-active);
    color: var(--text-chip-active);
}

.page-header {
    margin-bottom: 18px;
}

.page-title {
    margin: 0;
    font-size: 1.35rem;
    font-weight: 700;
}

.section-title {
    margin: 0 0 16px;
    font-size: 1.1rem;
    font-weight: 700;
    display: flex;
    align-items: center;
    gap: 10px;
}

.alert-box {
    max-width: 680px;
    margin: 32px auto;
    padding: 24px;
    border-radius: 12px;
    background-color: var(--bg-secondary);
    border: 1px solid var(--border-color);
    text-align: center;
}

.alert-box h1,
.alert-box h2 {
    margin-top: 0;
    font-size: 1.25rem;
}

.muted-text {
    color: var(--text-secondary);
    font-size: 0.92rem;
}

/* ==========================================================================
   Homepage Video Grid (Desktop 4 / Tablet 2 / Mobile 1)
   ========================================================================== */

.video-grid {
    display: grid;
    grid-template-columns: repeat(4, minmax(0, 1fr));
    gap: 28px 16px;
}

.video-card {
    display: flex;
    flex-direction: column;
    gap: 12px;
}

.thumb-wrapper {
    position: relative;
    width: 100%;
    aspect-ratio: 16 / 9;
    border-radius: 12px;
    overflow: hidden;
    background-color: var(--bg-elevated);
}

.thumb-wrapper img {
    width: 100%;
    height: 100%;
    object-fit: cover;
    transition: transform 0.25s ease;
}

.thumb-link:hover .thumb-wrapper img,
.search-thumb-link:hover .thumb-wrapper img,
.related-thumb-link:hover .thumb-wrapper img {
    transform: scale(1.03);
}

.duration-badge {
    position: absolute;
    right: 6px;
    bottom: 6px;
    padding: 2px 6px;
    border-radius: 4px;
    background-color: rgba(0, 0, 0, 0.85);
    color: #ffffff;
    font-size: 0.76rem;
    font-weight: 600;
    line-height: 1.3;
    letter-spacing: 0.02em;
}

.video-card-body {
    display: flex;
    gap: 12px;
    align-items: flex-start;
}

.channel-avatar {
    width: 36px;
    height: 36px;
    border-radius: 50%;
    background: linear-gradient(135deg, #ff0033, #ff6b35);
    color: #ffffff;
    font-weight: 700;
    font-size: 0.95rem;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
    text-transform: uppercase;
}

.channel-avatar-sm {
    width: 24px;
    height: 24px;
    font-size: 0.75rem;
}

.channel-avatar-lg {
    width: 42px;
    height: 42px;
    font-size: 1.1rem;
}

.video-card-meta {
    min-width: 0;
    flex: 1;
}

.video-title {
    margin: 0 0 6px;
    font-size: 0.96rem;
    font-weight: 600;
    line-height: 1.4;
    display: -webkit-box;
    -webkit-line-clamp: 2;
    -webkit-box-orient: vertical;
    overflow: hidden;
}

.video-title a:hover {
    color: var(--link-color);
}

.channel-name {
    display: block;
    font-size: 0.84rem;
    color: var(--text-secondary);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

a.channel-name:hover {
    color: var(--text-primary);
}

.video-stats {
    display: flex;
    align-items: center;
    flex-wrap: wrap;
    gap: 5px;
    font-size: 0.82rem;
    color: var(--text-secondary);
    margin-top: 2px;
}

/* ==========================================================================
   Search Page
   ========================================================================== */

.search-header {
    display: flex;
    flex-direction: column;
    gap: 14px;
    margin-bottom: 20px;
    padding-bottom: 14px;
    border-bottom: 1px solid var(--border-color);
}

.sort-bar {
    display: flex;
    align-items: center;
    flex-wrap: wrap;
    gap: 8px;
}

.sort-label {
    font-size: 0.88rem;
    color: var(--text-secondary);
}

.search-results-list {
    display: flex;
    flex-direction: column;
    gap: 18px;
    max-width: 1080px;
}

.search-card {
    display: grid;
    grid-template-columns: 340px minmax(0, 1fr);
    gap: 16px;
    align-items: start;
}

.search-video-title {
    margin: 0 0 6px;
    font-size: 1.12rem;
    font-weight: 600;
    line-height: 1.38;
}

.search-video-title a:hover {
    color: var(--link-color);
}

.search-channel-row {
    display: flex;
    align-items: center;
    gap: 8px;
    margin: 10px 0;
}

.search-snippet {
    margin: 0;
    font-size: 0.86rem;
    color: var(--text-secondary);
    display: -webkit-box;
    -webkit-line-clamp: 2;
    -webkit-box-orient: vertical;
    overflow: hidden;
}

/* ==========================================================================
   Watch Page (Desktop 70% Main / 30% Related Sidebar)
   ========================================================================== */

.watch-layout {
    display: grid;
    grid-template-columns: minmax(0, 7fr) minmax(300px, 3fr);
    gap: 24px;
    max-width: 1680px;
    margin: 0 auto;
    align-items: start;
}

.player-aspect {
    position: relative;
    width: 100%;
    aspect-ratio: 16 / 9;
    border-radius: 12px;
    overflow: hidden;
    background-color: #000000;
    box-shadow: var(--shadow-sm);
}

.player-aspect iframe,
.player-aspect #yt-player {
    position: absolute;
    inset: 0;
    width: 100%;
    height: 100%;
    border: 0;
}

.video-details {
    margin-top: 14px;
}

.watch-title {
    margin: 0 0 12px;
    font-size: 1.35rem;
    font-weight: 700;
    line-height: 1.35;
}

.watch-meta-bar {
    display: flex;
    align-items: center;
    justify-content: space-between;
    flex-wrap: wrap;
    gap: 14px;
    margin-bottom: 14px;
}

.channel-info {
    display: flex;
    align-items: center;
    gap: 12px;
}

.watch-channel-name {
    font-weight: 700;
    font-size: 1rem;
    display: block;
}

.watch-channel-name:hover {
    color: var(--link-color);
}

.watch-channel-sub {
    margin: 0;
    font-size: 0.8rem;
    color: var(--text-secondary);
}

.watch-actions {
    display: flex;
    align-items: center;
    flex-wrap: wrap;
    gap: 8px;
}

.action-pill {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    height: 36px;
    padding: 0 14px;
    border-radius: 18px;
    border: none;
    background-color: var(--bg-elevated);
    color: var(--text-primary);
    font-size: 0.88rem;
    font-weight: 600;
    font-family: inherit;
}

.action-btn {
    cursor: pointer;
    transition: background-color 0.15s ease;
}

.action-btn:hover {
    background-color: var(--bg-hover);
}

.action-pill svg {
    width: 18px;
    height: 18px;
    fill: currentColor;
}

/* Description Box */
.description-box {
    background-color: var(--bg-elevated);
    border-radius: 12px;
    padding: 14px 16px;
    font-size: 0.92rem;
}

.description-stats {
    display: flex;
    align-items: center;
    flex-wrap: wrap;
    gap: 8px;
    margin-bottom: 10px;
    font-size: 0.9rem;
}

.description-content {
    word-break: break-word;
    line-height: 1.65;
}

.description-content.is-collapsed {
    max-height: 5.2em;
    overflow: hidden;
    position: relative;
}

.description-content a {
    color: var(--link-color);
}

.description-content a:hover {
    text-decoration: underline;
}

.description-toggle {
    margin-top: 8px;
    padding: 0;
    border: none;
    background: transparent;
    color: var(--text-primary);
    font-size: 0.88rem;
    font-weight: 700;
    cursor: pointer;
}

.description-toggle:hover {
    text-decoration: underline;
}

/* Comments Section */
.comments-section {
    margin-top: 28px;
    padding-top: 20px;
    border-top: 1px solid var(--border-color);
}

.comment-total {
    font-size: 0.9rem;
    font-weight: 500;
    color: var(--text-secondary);
}

.comment-list {
    list-style: none;
    padding: 0;
    margin: 0;
    display: flex;
    flex-direction: column;
    gap: 20px;
}

.comment-item {
    display: flex;
    gap: 14px;
    align-items: flex-start;
}

.comment-avatar {
    width: 40px;
    height: 40px;
    border-radius: 50%;
    object-fit: cover;
    flex-shrink: 0;
}

.comment-body {
    min-width: 0;
    flex: 1;
}

.comment-header {
    display: flex;
    align-items: baseline;
    gap: 8px;
    margin-bottom: 4px;
}

.comment-author {
    font-weight: 600;
    font-size: 0.88rem;
}

.comment-date {
    font-size: 0.78rem;
    color: var(--text-secondary);
}

.comment-text {
    margin: 0 0 6px;
    font-size: 0.92rem;
    word-break: break-word;
    line-height: 1.55;
}

.comment-footer {
    display: flex;
    align-items: center;
    gap: 14px;
    font-size: 0.8rem;
    color: var(--text-secondary);
}

/* Related Videos Sidebar */
.related-list {
    display: flex;
    flex-direction: column;
    gap: 12px;
}

.related-card {
    display: grid;
    grid-template-columns: 168px minmax(0, 1fr);
    gap: 10px;
    align-items: start;
}

.related-card .thumb-wrapper {
    border-radius: 8px;
}

.related-title {
    margin: 0 0 4px;
    font-size: 0.88rem;
    font-weight: 600;
    line-height: 1.35;
    display: -webkit-box;
    -webkit-line-clamp: 2;
    -webkit-box-orient: vertical;
    overflow: hidden;
}

.related-title a:hover {
    color: var(--link-color);
}

.related-meta .channel-name {
    margin: 0 0 2px;
    font-size: 0.78rem;
}

.related-meta .video-stats {
    font-size: 0.76rem;
}

/* ==========================================================================
   Pagination & Buttons
   ========================================================================== */

.pagination {
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin-top: 32px;
    padding-top: 20px;
    border-top: 1px solid var(--border-color);
}

.btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    padding: 9px 18px;
    border-radius: 20px;
    font-size: 0.9rem;
    font-weight: 600;
    transition: background-color 0.15s ease;
}

.btn-primary {
    background-color: var(--brand-red);
    color: #ffffff;
}

.btn-primary:hover {
    opacity: 0.9;
}

.btn-secondary {
    background-color: var(--bg-elevated);
    color: var(--text-primary);
}

.btn-secondary:hover {
    background-color: var(--bg-hover);
}

/* ==========================================================================
   Responsive Breakpoints (Tablet 2-col, Mobile 1-col, Mobile Drawer)
   ========================================================================== */

@media (max-width: 1199px) {
    .video-grid {
        grid-template-columns: repeat(2, minmax(0, 1fr));
    }

    .watch-layout {
        grid-template-columns: 1fr;
    }

    .related-list {
        display: grid;
        grid-template-columns: repeat(2, minmax(0, 1fr));
        gap: 16px;
    }
}

@media (max-width: 900px) {
    .sidebar {
        transform: translateX(-100%);
        width: 240px !important;
        padding: 12px !important;
    }

    body.sidebar-open .sidebar {
        transform: translateX(0);
    }

    body.sidebar-open .sidebar-backdrop {
        display: block;
    }

    body.sidebar-collapsed .nav-item {
        flex-direction: row;
        gap: 18px;
        padding: 10px 12px;
        text-align: left;
    }

    body.sidebar-collapsed .nav-label {
        font-size: 0.92rem;
        max-width: none;
    }

    body.sidebar-collapsed .nav-heading,
    body.sidebar-collapsed .nav-footer {
        display: block;
    }

    .main-content,
    body.sidebar-collapsed .main-content {
        margin-left: 0;
        padding: 16px;
    }

    .search-card {
        grid-template-columns: 1fr;
    }
}

@media (max-width: 639px) {
    .site-header {
        padding: 0 10px;
        gap: 8px;
    }

    .brand-text,
    .brand-region {
        display: none;
    }

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

    .related-list {
        grid-template-columns: 1fr;
    }

    .related-card {
        grid-template-columns: 144px minmax(0, 1fr);
    }

    .watch-title {
        font-size: 1.15rem;
    }
}
