/* ============================================================
   PartnersTV | style.css
   Google DeepMind Engineering Team — Optimized Build
   ============================================================ */

:root {
    --bg-color: #F9F9F9;
    --surface-color: #FFFFFF;
    --text-primary: #0F0F0F;
    --text-secondary: #606060;
    --accent-blue: #065fd4;
    --safe-top: env(safe-area-inset-top, 20px);
    --safe-bottom: env(safe-area-inset-bottom, 20px);
    --ease-snap: cubic-bezier(0.25, 1, 0.5, 1);
    --header-logo-h: 52px;
    --header-cats-h: 48px;
    --transition-header: 0.3s cubic-bezier(0.4, 0, 0.2, 1);
}

/* ============================================================
   RESET & BASE
   ============================================================ */
* {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
    -webkit-tap-highlight-color: transparent;
    outline: none;
}

body {
    background-color: var(--bg-color);
    color: var(--text-primary);
    font-family: 'Inter', sans-serif;
    overflow: hidden;
    height: 100vh;
    width: 100vw;
    position: relative;
}

/* ============================================================
   FEED / CONTENT AREA
   ============================================================ */
#tube-content-area {
    position: absolute;
    inset: 0;
    z-index: 1;
}

.tube-view {
    position: absolute;
    inset: 0;
    overflow-y: auto;
    padding-bottom: 80px;
    -webkit-overflow-scrolling: touch;
}

.tube-view::-webkit-scrollbar {
    display: none;
}

/* ============================================================
   HEADER — Scroll-aware (ข้อ 3 & 3.1)
   
   Physics model:
   - Logo row uses max-height animation (layout-stable, no reflow jump)
   - Dead-zone threshold: 8px prevents flicker on micro-scroll
   - Compact trigger: scrollTop > 60px AND delta > 8px downward
   - Restore trigger: delta < -8px upward (any scroll position)
   ============================================================ */
.tube-header {
    background: rgba(249, 249, 249, 0.97);
    backdrop-filter: blur(10px);
    -webkit-backdrop-filter: blur(10px);
    position: sticky;
    top: 0;
    z-index: 20;
    border-bottom: 1px solid rgba(0, 0, 0, 0.05);
    will-change: transform;
}

/* Logo row — collapses on scroll down */
.tube-header-logo {
    padding: calc(var(--safe-top) + 8px) 20px 8px;
    overflow: hidden;
    max-height: 80px;
    opacity: 1;
    transition:
        max-height var(--transition-header),
        padding var(--transition-header),
        opacity 0.25s ease;
}

/* Category chips row — always visible */
.tube-header-cats {
    padding: 0 20px 8px;
    transition: padding-top var(--transition-header);
}

/* Compact state: logo hidden, cats shift up to fill space */
.tube-header.header-compact .tube-header-logo {
    max-height: 0;
    padding-top: 0;
    padding-bottom: 0;
    opacity: 0;
}

.tube-header.header-compact .tube-header-cats {
    padding-top: calc(var(--safe-top) + 6px);
}

/* ============================================================
   CATEGORY BAR & CHIPS
   ============================================================ */
.tube-category-bar {
    display: flex;
    gap: 8px;
    overflow-x: auto;
    scrollbar-width: none;
    padding-bottom: 5px;
}

.tube-category-bar::-webkit-scrollbar {
    display: none;
}

.tube-chip {
    background: rgba(0, 0, 0, 0.05);
    color: var(--text-primary);
    padding: 8px 16px;
    border-radius: 20px;
    font-size: 13px;
    font-weight: 600;
    cursor: pointer;
    white-space: nowrap;
    flex-shrink: 0;
    transition: background 0.2s, color 0.2s;
}

.tube-chip.active {
    background: var(--text-primary);
    color: white;
}

/* ============================================================
   HOME FEED
   ============================================================ */
#tube-home-feed {
    padding: 20px;
    display: flex;
    flex-direction: column;
    gap: 24px;
}

/* Horizontal video card */
.tube-video-card {
    cursor: pointer;
    background: white;
    border-radius: 12px;
    overflow: hidden;
    box-shadow: 0 2px 12px rgba(0, 0, 0, 0.03);
    transition: transform 0.2s;
}

.tube-video-card:active {
    transform: scale(0.98);
}

.tube-thumbnail {
    width: 100%;
    aspect-ratio: 16 / 9;
    background: #eee;
    position: relative;
}

.tube-thumb-img {
    width: 100%;
    height: 100%;
    object-fit: cover;
}

.tube-meta {
    display: flex;
    gap: 12px;
    padding: 12px;
}

.tube-avatar {
    width: 36px;
    height: 36px;
    border-radius: 50%;
    background: #ccc;
    object-fit: cover;
    flex-shrink: 0;
}

/* Shorts grid */
.shorts-grid {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 12px;
    margin: 10px 0;
}

.short-card {
    position: relative;
    aspect-ratio: 9 / 16;
    background: #000;
    border-radius: 12px;
    overflow: hidden;
    cursor: pointer;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
}

.short-card:active {
    transform: scale(0.98);
    transition: transform 0.2s;
}

.short-thumb {
    width: 100%;
    height: 100%;
    object-fit: cover;
    opacity: 0.9;
}

.short-overlay {
    position: absolute;
    bottom: 0;
    left: 0;
    right: 0;
    padding: 12px;
    background: linear-gradient(to top, rgba(0, 0, 0, 0.8), transparent);
    color: white;
}

.short-title {
    font-size: 13px;
    font-weight: 600;
    display: -webkit-box;
    -webkit-line-clamp: 2;
    -webkit-box-orient: vertical;
    overflow: hidden;
    text-shadow: 0 1px 2px rgba(0, 0, 0, 0.5);
    margin-bottom: 4px;
}

.short-views {
    font-size: 10px;
    opacity: 0.8;
}

/* Infinite scroll trigger */
#infinite-scroll-trigger {
    height: 60px;
    display: flex;
    align-items: center;
    justify-content: center;
    opacity: 0;
}

#infinite-scroll-trigger.loading {
    opacity: 1;
}

/* ============================================================
   HORIZONTAL PLAYER OVERLAY
   ============================================================ */
#tube-player-overlay {
    position: fixed;
    inset: 0;
    z-index: 100;
    display: flex;
    flex-direction: column;
    pointer-events: none;
}

#tube-player-bg {
    position: absolute;
    inset: 0;
    background: rgba(0, 0, 0, 0.8);
    z-index: -1;
    opacity: 0;
    pointer-events: auto;
}

#tube-video-container {
    width: 100%;
    aspect-ratio: 16 / 9;
    background: #000;
    position: relative;
    z-index: 2;
    transform-origin: top left;
    box-shadow: 0 4px 30px rgba(0, 0, 0, 0.3);
    pointer-events: auto;
    overflow: hidden;
    flex-shrink: 0;
    will-change: transform, width, height;
}

#tube-video-container video {
    width: 100%;
    height: 100%;
    object-fit: contain;
    display: block;
}

/* ============================================================
   PLAYER CONTROLS LAYER
   ============================================================ */
.tube-controls-layer {
    position: absolute;
    inset: 0;
    background: rgba(0, 0, 0, 0.4);
    display: flex;
    flex-direction: column;
    justify-content: space-between;
    opacity: 0;
    transition: opacity 0.2s;
    pointer-events: none;
    z-index: 5;
}

.tube-controls-layer.show {
    opacity: 1;
    pointer-events: auto;
}

.tube-ctl-top {
    padding: calc(var(--safe-top) + 10px) 16px;
    display: flex;
    justify-content: space-between;
    align-items: flex-start;
}

.ctl-right-stack {
    display: flex;
    flex-direction: column;
    gap: 16px;
    align-items: center;
}

.tube-ctl-icon {
    color: white;
    font-size: 20px;
    cursor: pointer;
    text-shadow: 0 2px 4px rgba(0, 0, 0, 0.5);
    border-radius: 50%;
    background: rgba(0, 0, 0, 0.2);
    backdrop-filter: blur(4px);
    -webkit-backdrop-filter: blur(4px);
    width: 40px;
    height: 40px;
    display: grid;
    place-items: center;
}

.tube-ctl-center {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    display: flex;
    align-items: center;
    gap: 40px;
}

.tube-play-big {
    font-size: 50px;
    color: white;
    filter: drop-shadow(0 2px 8px rgba(0, 0, 0, 0.5));
}

.tube-ctl-bottom {
    padding: 10px 16px;
    background: linear-gradient(to top, rgba(0, 0, 0, 0.8), transparent);
}

/* ============================================================
   INFO / FULL CONTROLS PANEL
   ============================================================ */
#tube-full-controls {
    flex: 1;
    background: var(--surface-color);
    opacity: 0;
    overflow-y: auto;
    pointer-events: auto;
}

.meta-row {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 0 16px 16px;
}

.author-grp {
    display: flex;
    gap: 10px;
    align-items: center;
}

.btn-comments {
    background: transparent;
    border: 1px solid #ddd;
    padding: 6px 12px;
    border-radius: 18px;
    font-size: 12px;
    font-weight: 600;
    color: var(--text-primary);
    cursor: pointer;
    display: flex;
    gap: 6px;
    align-items: center;
}

.btn-comments:active {
    background: #f0f0f0;
}

/* ============================================================
   SEEK BAR & SCRUB OVERLAY
   ============================================================ */
.seek-container {
    padding: 15px 0;
    cursor: pointer;
    touch-action: none;
}

.seek-track {
    width: 100%;
    height: 4px;
    background: rgba(255, 255, 255, 0.3);
    border-radius: 2px;
    position: relative;
}

.seek-fill {
    height: 100%;
    background: var(--accent-blue);
    width: 0%;
    border-radius: 2px;
    position: relative;
}

.seek-thumb {
    position: absolute;
    right: -8px;
    top: -6px;
    width: 16px;
    height: 16px;
    background: white;
    border-radius: 50%;
    transform: scale(0);
    transition: transform 0.1s;
    box-shadow: 0 0 5px rgba(0, 0, 0, 0.5);
}

.seek-container:active .seek-thumb,
.tube-controls-layer.show .seek-thumb {
    transform: scale(1);
}

#scrub-overlay {
    position: absolute;
    inset: 0;
    background: rgba(0, 0, 0, 0.4);
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    color: white;
    font-family: 'JetBrains Mono', monospace;
    opacity: 0;
    pointer-events: none;
    transition: opacity 0.2s;
    z-index: 10;
}

#scrub-overlay.active {
    opacity: 1;
}

.scrub-time {
    font-size: 28px;
    font-weight: 700;
    text-shadow: 0 2px 10px rgba(0, 0, 0, 0.5);
}

/* ============================================================
   TOAST NOTIFICATION
   ============================================================ */
#toast-msg {
    position: fixed;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    background: rgba(0, 0, 0, 0.85);
    color: white;
    padding: 12px 24px;
    border-radius: 30px;
    font-size: 14px;
    font-weight: 600;
    opacity: 0;
    pointer-events: none;
    transition: opacity 0.3s;
    z-index: 200;
    backdrop-filter: blur(8px);
    -webkit-backdrop-filter: blur(8px);
}

#toast-msg.show {
    opacity: 1;
}

/* ============================================================
   RELATED / RECOMMENDATIONS
   ============================================================ */
.player-sticky-cats {
    position: sticky;
    top: 0;
    z-index: 10;
    background: rgba(255, 255, 255, 0.98);
    padding: 12px 16px;
    border-bottom: 1px solid rgba(0, 0, 0, 0.05);
    display: flex;
    gap: 8px;
    overflow-x: auto;
    scrollbar-width: none;
}

.related-item {
    display: flex;
    gap: 10px;
    padding: 12px 16px;
    cursor: pointer;
}

.r-thumb {
    width: 140px;
    aspect-ratio: 16 / 9;
    background: #333;
    border-radius: 8px;
    overflow: hidden;
    flex-shrink: 0;
}

/* ============================================================
   PLAYER STATE MODES
   ============================================================ */

/* Vertical video mode */
#tube-player-overlay.mode-vertical #tube-video-container {
    height: 100vh;
    aspect-ratio: unset;
}

#tube-player-overlay.mode-vertical #tube-video-container video {
    object-fit: cover;
}

#tube-player-overlay.mode-vertical #tube-full-controls {
    display: none;
}

/* Minimized (picture-in-picture) mode */
.tube-minimized #tube-full-controls {
    opacity: 0;
    pointer-events: none;
}

.tube-minimized #tube-player-bg {
    opacity: 0;
    pointer-events: none;
}

.tube-minimized .tube-controls-layer {
    display: none;
}

.tube-minimized #scrub-overlay {
    display: none;
}

/* Animation class */
.animating #tube-video-container {
    transition: transform 0.4s var(--ease-snap),
                width 0.3s ease,
                height 0.3s ease,
                opacity 0.3s ease;
}

.animating #tube-player-bg,
.animating #tube-full-controls {
    transition: all 0.3s ease;
}

/* Hidden / off-screen */
.tube-hidden {
    transform: translateY(100%);
    pointer-events: none;
}

/* ============================================================
   LANDSCAPE FULLSCREEN MODE (ข้อ 4.1)
   
   Physics: CSS rotate(90deg) on the video container
   - Rotates 90° clockwise so portrait phone → landscape view
   - width = 100vh, height = 100vw fills the rotated viewport
   - translateY(-100%) corrects position after rotation
   - transform-origin: top left keeps anchor stable
   ============================================================ */
#tube-player-overlay.mode-landscape-full {
    background: #000;
}

#tube-player-overlay.mode-landscape-full #tube-full-controls {
    display: none !important;
}

#tube-player-overlay.mode-landscape-full #tube-player-bg {
    opacity: 1 !important;
}

/* ============================================================
   SHORTS / VERTICAL PLAYER OVERLAY (ข้อ 4)
   
   Stack model:
   - Each .short-slide is absolutely positioned at translateY(N*100%)
   - Current slide = 0%, next = +100%, prev = -100%
   - Swipe gesture updates translateY in real-time (live drag)
   - On touchend: threshold = 20% of viewport height
   - Slides outside ±(maxPreload+1) range have src removed (memory)
   ============================================================ */
#shorts-player-overlay {
    position: fixed;
    inset: 0;
    z-index: 150;
    background: #000;
    display: flex;
    flex-direction: column;
    pointer-events: none;
    transform: translateY(100%);
    transition: transform 0.35s var(--ease-snap);
    will-change: transform;
    touch-action: none;
}

#shorts-player-overlay.active {
    transform: translateY(0);
    pointer-events: auto;
}

#shorts-reel {
    position: relative;
    width: 100%;
    height: 100%;
    overflow: hidden;
}

/* Individual short slide */
.short-slide {
    position: absolute;
    inset: 0;
    width: 100%;
    height: 100%;
    background: #000;
    will-change: transform;
}

.short-slide video {
    width: 100%;
    height: 100%;
    object-fit: cover;
    display: block;
}

/* Slide overlay UI layer */
.short-slide-ui {
    position: absolute;
    inset: 0;
    pointer-events: none;
    z-index: 5;
}

/* Top bar */
.shorts-top-bar {
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    padding: calc(var(--safe-top) + 12px) 16px 12px;
    display: flex;
    align-items: center;
    justify-content: space-between;
    background: linear-gradient(to bottom, rgba(0, 0, 0, 0.5), transparent);
    pointer-events: auto;
}

.shorts-top-bar span {
    color: white;
    font-size: 17px;
    font-weight: 700;
    letter-spacing: 0.5px;
}

.shorts-btn-close {
    width: 40px;
    height: 40px;
    background: rgba(255, 255, 255, 0.15);
    backdrop-filter: blur(6px);
    -webkit-backdrop-filter: blur(6px);
    border-radius: 50%;
    display: grid;
    place-items: center;
    color: white;
    font-size: 18px;
    cursor: pointer;
    pointer-events: auto;
}

/* Bottom info */
.shorts-bottom-info {
    position: absolute;
    bottom: 0;
    left: 0;
    right: 0;
    padding: 24px 16px calc(var(--safe-bottom) + 20px);
    background: linear-gradient(to top, rgba(0, 0, 0, 0.75), transparent);
    pointer-events: auto;
}

.shorts-video-title {
    color: white;
    font-size: 15px;
    font-weight: 700;
    margin-bottom: 6px;
    text-shadow: 0 1px 4px rgba(0, 0, 0, 0.5);
}

.shorts-video-author {
    color: rgba(255, 255, 255, 0.8);
    font-size: 13px;
}

/* Right action bar */
.shorts-right-actions {
    position: absolute;
    right: 12px;
    bottom: 100px;
    display: flex;
    flex-direction: column;
    gap: 20px;
    align-items: center;
    pointer-events: auto;
}

.short-action-btn {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 4px;
    color: white;
    cursor: pointer;
}

.short-action-btn i {
    font-size: 26px;
    filter: drop-shadow(0 1px 4px rgba(0, 0, 0, 0.5));
}

.short-action-btn span {
    font-size: 11px;
    font-weight: 600;
}

/* Center play/pause tap indicator */
.shorts-center-play {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%) scale(0);
    width: 70px;
    height: 70px;
    background: rgba(0, 0, 0, 0.5);
    border-radius: 50%;
    display: grid;
    place-items: center;
    color: white;
    font-size: 30px;
    transition: transform 0.2s var(--ease-snap), opacity 0.3s ease;
    opacity: 0;
    pointer-events: none;
    z-index: 6;
}

.shorts-center-play.show {
    transform: translate(-50%, -50%) scale(1);
    opacity: 1;
}

/* Pager dots (right edge) */
.shorts-pager {
    position: absolute;
    right: 6px;
    top: 50%;
    transform: translateY(-50%);
    display: flex;
    flex-direction: column;
    gap: 6px;
    pointer-events: none;
    z-index: 10;
}

.pager-dot {
    width: 4px;
    height: 4px;
    background: rgba(255, 255, 255, 0.4);
    border-radius: 50%;
    transition: background 0.2s, height 0.2s, border-radius 0.2s;
}

.pager-dot.active {
    background: white;
    height: 16px;
    border-radius: 2px;
}
