/**
 * Cure Space - Layout Styles
 * Grid & Speaker+Gallery Layouts
 * Inspired by Zoom, Google Meet, Teams
 *
 * @package Cure_Space
 */

/* Layout Container - fill entire video area */
.cure-space-room {
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    width: 100%;
    height: 100%;
    display: flex;
    flex-direction: column;
}

.cure-space-main-view {
    flex: 1;
    position: relative;
    overflow: hidden;
    display: flex;
    align-items: center;
    justify-content: center;
    width: 100%;
    min-height: 0; /* Allow flex shrinking */
}

/* Speaker Container */
.cure-space-speaker-container {
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    display: none;
    padding: 0;
}

.cure-space-speaker-container .cure-space-participant {
    width: 100%;
    height: 100%;
    border-radius: 0;
    aspect-ratio: auto;
}

.cure-space-speaker-container .cure-space-participant.cure-space-speaker {
    animation: speakerIn 0.4s cubic-bezier(0.4, 0, 0.2, 1);
}

@keyframes speakerIn {
    from {
        opacity: 0;
        transform: scale(0.98);
    }
    to {
        opacity: 1;
        transform: scale(1);
    }
}

/* Grid Container - fills the main view */
.cure-space-grid-container {
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    display: none;
    padding: 12px;
    gap: 12px;
    align-items: center;
    align-content: center;
    justify-content: center;
    justify-items: center;
    width: 100%;
    height: 100%;
    box-sizing: border-box;
}

/* Grid Layout - Smart Responsive Grid */

/* Single participant (1-on-1 call) - maximize video size like Zoom/Meet */
.cure-space-layout-grid .cure-space-grid-container[data-count="1"] {
    display: flex !important;
    align-items: center;
    justify-content: center;
    padding: 8px;
}

.cure-space-layout-grid .cure-space-grid-container[data-count="1"] .cure-space-participant:not(.cure-space-participant-hidden) {
    width: 100%;
    height: 100%;
    max-width: 100%;
    max-height: 100%;
    aspect-ratio: auto; /* Let it fill naturally */
    border-radius: var(--cure-space-radius);
}

/* Two participants - side by side like Meet/Teams */
.cure-space-layout-grid .cure-space-grid-container[data-count="2"] {
    display: grid !important;
    grid-template-columns: repeat(2, 1fr);
    align-items: stretch;
    align-content: stretch;
    padding: 8px;
    gap: 8px;
}

.cure-space-layout-grid .cure-space-grid-container[data-count="2"] .cure-space-participant:not(.cure-space-participant-hidden) {
    width: 100%;
    height: 100%;
    max-height: 100%;
    aspect-ratio: auto;
}

/* Three participants - all in a row */
.cure-space-layout-grid .cure-space-grid-container[data-count="3"] {
    display: grid !important;
    grid-template-columns: repeat(3, 1fr);
    grid-template-rows: 1fr;
    align-items: stretch;
    justify-items: stretch;
    padding: 8px;
    gap: 8px;
}

.cure-space-layout-grid .cure-space-grid-container[data-count="3"] .cure-space-participant {
    width: 100%;
    height: 100%;
    max-width: none;
    max-height: none;
}

/* Four participants - 2x2 grid */
.cure-space-layout-grid .cure-space-grid-container[data-count="4"] {
    display: grid !important;
    grid-template-columns: repeat(2, 1fr);
    grid-template-rows: repeat(2, 1fr);
    align-items: stretch;
    justify-items: stretch;
    padding: 8px;
    gap: 8px;
}

.cure-space-layout-grid .cure-space-grid-container[data-count="4"] .cure-space-participant {
    width: 100%;
    height: 100%;
    max-width: none;
    max-height: none;
}

/* Five or six participants - 3x2 grid */
.cure-space-layout-grid .cure-space-grid-container[data-count="5"],
.cure-space-layout-grid .cure-space-grid-container[data-count="6"] {
    display: grid !important;
    grid-template-columns: repeat(3, 1fr);
    grid-template-rows: repeat(2, 1fr);
    align-items: stretch;
    justify-items: stretch;
    padding: 8px;
    gap: 8px;
}

/* Seven to nine participants - 3x3 grid */
.cure-space-layout-grid .cure-space-grid-container[data-count="7"],
.cure-space-layout-grid .cure-space-grid-container[data-count="8"],
.cure-space-layout-grid .cure-space-grid-container[data-count="9"] {
    display: grid !important;
    grid-template-columns: repeat(3, 1fr);
    grid-template-rows: repeat(3, 1fr);
    align-items: stretch;
    justify-items: stretch;
    padding: 6px;
    gap: 6px;
}

/* For more than 9 participants - 4x3 grid */
.cure-space-layout-grid .cure-space-grid-container[data-count="10"],
.cure-space-layout-grid .cure-space-grid-container[data-count="11"],
.cure-space-layout-grid .cure-space-grid-container[data-count="12"] {
    display: grid !important;
    grid-template-columns: repeat(4, 1fr);
    grid-template-rows: repeat(3, 1fr);
    align-items: stretch;
    justify-items: stretch;
    padding: 6px;
    gap: 6px;
}

/* Gallery Strip */
.cure-space-gallery {
    display: none;
    align-items: center;
    gap: 12px;
    padding: 14px 20px;
    background: var(--cure-space-bg-secondary);
    border-top: 1px solid var(--cure-space-border);
}

.cure-space-gallery-items {
    flex: 1;
    display: flex;
    gap: 12px;
    overflow-x: auto;
    scroll-behavior: smooth;
    padding: 4px 0;
    scrollbar-width: none;
    -ms-overflow-style: none;
}

.cure-space-gallery-items::-webkit-scrollbar {
    display: none;
}

.cure-space-gallery-item {
    flex: 0 0 160px;
    height: 90px;
    border-radius: var(--cure-space-radius-sm);
    overflow: hidden;
    cursor: pointer;
    transition: var(--cure-space-transition);
    border: 2px solid transparent;
    background: var(--cure-space-bg-tertiary);
}

.cure-space-gallery-item:hover {
    transform: scale(1.04);
    border-color: rgba(255, 255, 255, 0.2);
}

.cure-space-gallery-item.active-speaker {
    border-color: var(--cure-space-success);
    box-shadow: 0 0 16px rgba(34, 197, 94, 0.25);
}

.cure-space-gallery-item .cure-space-participant-video {
    width: 100%;
    height: 100%;
}

.cure-space-gallery-item .cure-space-participant-info {
    padding: 10px 10px 8px;
}

.cure-space-gallery-item .cure-space-participant-name {
    font-size: 10px;
}

.cure-space-gallery-item .cure-space-participant-actions {
    display: none;
}

/* Gallery Navigation */
.cure-space-gallery-nav {
    flex: 0 0 auto;
    width: 36px;
    height: 36px;
    background: var(--cure-space-accent);
    border: 1px solid var(--cure-space-border);
    border-radius: var(--cure-space-radius-sm);
    color: var(--cure-space-text);
    font-size: 14px;
    cursor: pointer;
    transition: var(--cure-space-transition);
    display: flex;
    align-items: center;
    justify-content: center;
}

.cure-space-gallery-nav:hover:not(:disabled) {
    background: var(--cure-space-bg-tertiary);
    border-color: var(--cure-space-border-light);
}

.cure-space-gallery-nav:disabled {
    opacity: 0.3;
    cursor: not-allowed;
}

/* Layout States */

/* Empty Room - message element */
.cure-space-empty-message {
    display: none;
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    color: var(--cure-space-text-muted);
    font-size: 16px;
    font-weight: 500;
    text-align: center;
}

.cure-space-layout-empty .cure-space-empty-message {
    display: block;
}

/* Grid Layout Active - base styles */
/* Default: display grid for fallback when data-count isn't set yet */
.cure-space-layout-grid .cure-space-grid-container {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(280px, 1fr));
    gap: 16px;
    padding: 16px;
}

/* Grid items - fill their grid cells */
.cure-space-grid-container .cure-space-participant,
.cure-space-grid-container .cure-space-grid-item {
    min-width: 0;
    min-height: 0;
    width: 100%;
    height: 100%;
}

/* Ensure participant-video fills the participant container */
.cure-space-grid-container .cure-space-participant .cure-space-participant-video {
    width: 100%;
    height: 100%;
    min-height: 0;
}

/* Hide pin button in grid layout - only useful for speaker layout */
.cure-space-layout-grid .cure-space-participant-actions {
    display: none;
}

.cure-space-layout-grid .cure-space-speaker-container {
    display: none;
}

.cure-space-layout-grid .cure-space-gallery {
    display: none;
}

/* Local preview is now in wrapper, not inside layout container */
/* Always show by default (grid layout or empty) */
.cure-space-local-preview {
    display: block;
}

/* Hide in speaker layout (participant is in gallery instead) */
.cure-space-wrapper-layout-speaker .cure-space-local-preview {
    display: none;
}

/* Speaker Layout Active */
.cure-space-layout-speaker .cure-space-speaker-container {
    display: block;
}

.cure-space-layout-speaker .cure-space-grid-container {
    display: none;
}

.cure-space-layout-speaker .cure-space-gallery {
    display: flex;
}

/* Grid Items */
.cure-space-grid-item {
    width: 100%;
    height: 100%;
    animation: gridItemIn 0.3s cubic-bezier(0.4, 0, 0.2, 1);
}

/* Hide non-publishing participants */
.cure-space-participant-hidden {
    display: none !important;
}

@keyframes gridItemIn {
    from {
        opacity: 0;
        transform: scale(0.95);
    }
    to {
        opacity: 1;
        transform: scale(1);
    }
}

/* Pinned indicator */
.cure-space-participant.pinned::before {
    content: '';
    position: absolute;
    top: 14px;
    left: 14px;
    width: 28px;
    height: 28px;
    background: var(--cure-space-primary);
    border-radius: var(--cure-space-radius-xs);
    z-index: 5;
    background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='white' stroke-width='2.5'%3E%3Cpath d='M21 10c0 7-9 13-9 13s-9-6-9-13a9 9 0 0 1 18 0z'/%3E%3Ccircle cx='12' cy='10' r='3'/%3E%3C/svg%3E");
    background-size: 16px 16px;
    background-repeat: no-repeat;
    background-position: center;
}

/* Screen Sharing Indicator */
.cure-space-participant.screen-sharing::after {
    content: 'Screen';
    position: absolute;
    top: 14px;
    left: 14px;
    padding: 6px 12px;
    background: var(--cure-space-success);
    border-radius: var(--cure-space-radius-xs);
    font-size: 11px;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.05em;
    z-index: 5;
}

/* Muted indicator */
.cure-space-participant.audio-muted .cure-space-audio-indicator {
    background: var(--cure-space-error) !important;
    box-shadow: none !important;
    animation: none !important;
}

/* Video Off State */
.cure-space-participant.video-off .cure-space-participant-video {
    background: linear-gradient(145deg, var(--cure-space-accent) 0%, var(--cure-space-bg) 100%);
}

.cure-space-participant.video-off .cure-space-participant-video::before {
    content: '';
    width: 72px;
    height: 72px;
    background: var(--cure-space-bg-tertiary);
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
}

.cure-space-participant.video-off .cure-space-participant-video video {
    display: none;
}

/* Connection Quality Indicator */
.cure-space-connection-quality {
    position: absolute;
    top: 14px;
    right: 14px;
    display: flex;
    gap: 3px;
    z-index: 5;
}

.cure-space-connection-bar {
    width: 4px;
    background: rgba(255, 255, 255, 0.2);
    border-radius: 2px;
}

.cure-space-connection-bar:nth-child(1) { height: 8px; }
.cure-space-connection-bar:nth-child(2) { height: 12px; }
.cure-space-connection-bar:nth-child(3) { height: 16px; }

.cure-space-connection-bar.active {
    background: var(--cure-space-success);
}

.cure-space-connection-quality.poor .cure-space-connection-bar.active {
    background: var(--cure-space-error);
}

.cure-space-connection-quality.fair .cure-space-connection-bar.active {
    background: var(--cure-space-warning);
}

/* Responsive Layout Adjustments */
@media (max-width: 1200px) {
    .cure-space-layout-grid .cure-space-grid-container[data-count="3"] .cure-space-participant:nth-child(3) {
        max-width: 60%;
    }
    
    .cure-space-gallery-item {
        flex: 0 0 140px;
        height: 80px;
    }
}

@media (max-width: 1024px) {
    /* Stack vertically on tablet */
    .cure-space-layout-grid .cure-space-grid-container[data-count="2"] {
        grid-template-columns: 1fr;
        grid-template-rows: repeat(2, 1fr);
    }
    
    .cure-space-grid-container {
        padding: 8px;
        gap: 8px;
    }
    
    .cure-space-gallery-item {
        flex: 0 0 120px;
        height: 70px;
    }
}

@media (max-width: 768px) {
    /* Mobile - stack everything vertically */
    .cure-space-grid-container {
        padding: 4px;
        gap: 4px;
    }
    
    /* Single participant on mobile - full screen */
    .cure-space-layout-grid .cure-space-grid-container[data-count="1"] {
        padding: 0;
    }
    
    .cure-space-layout-grid .cure-space-grid-container[data-count="1"] .cure-space-participant {
        border-radius: 0;
    }
    
    /* Two participants on mobile - stack vertically */
    .cure-space-layout-grid .cure-space-grid-container[data-count="2"] {
        grid-template-columns: 1fr;
        grid-template-rows: repeat(2, 1fr);
    }
    
    /* 3 participants on mobile - 2x2 grid with third spanning bottom */
    .cure-space-layout-grid .cure-space-grid-container[data-count="3"] {
        grid-template-columns: repeat(2, 1fr);
        grid-template-rows: repeat(2, 1fr);
    }
    
    .cure-space-layout-grid .cure-space-grid-container[data-count="3"] .cure-space-participant:nth-child(3) {
        grid-column: 1 / 3;
    }
    
    /* 4 participants on mobile - 2x2 grid */
    .cure-space-layout-grid .cure-space-grid-container[data-count="4"] {
        grid-template-columns: repeat(2, 1fr);
        grid-template-rows: repeat(2, 1fr);
    }
    
    .cure-space-gallery {
        padding: 8px;
    }
    
    .cure-space-gallery-item {
        flex: 0 0 90px;
        height: 50px;
    }
    
    .cure-space-gallery-nav {
        width: 28px;
        height: 28px;
        font-size: 11px;
    }
    
    .cure-space-participant-info {
        padding: 12px 10px 8px;
    }
    
    .cure-space-participant-name {
        font-size: 11px;
    }
}

/* Landscape optimizations for mobile */
@media (max-width: 896px) and (orientation: landscape) {
    /* In landscape, use side-by-side for 2 participants */
    .cure-space-layout-grid .cure-space-grid-container[data-count="2"] {
        grid-template-columns: repeat(2, 1fr);
        grid-template-rows: 1fr;
    }
    
    /* Smaller local preview in landscape */
    .cure-space-local-preview {
        width: 100px;
        bottom: 8px;
        right: 8px;
    }
    
    .cure-space-gallery {
        flex-direction: column;
        width: 90px;
        height: 100%;
        position: absolute;
        right: 0;
        top: 0;
        bottom: 0;
        border-top: none;
        border-left: 1px solid var(--cure-space-border);
        padding: 8px 6px;
    }
    
    .cure-space-gallery-items {
        flex-direction: column;
        overflow-y: auto;
        overflow-x: hidden;
    }
    
    .cure-space-gallery-item {
        flex: 0 0 50px;
        width: 100%;
        height: 40px;
    }
}

/* Smooth transitions for layout changes */
.cure-space-room.transitioning .cure-space-participant {
    transition: all 0.4s cubic-bezier(0.4, 0, 0.2, 1);
}
