/* Mobile Styles for Bari Meeting */

/* Tablet Portrait & Large Mobile (576px - 767px) */
@media (max-width: 767px) {

    
    /* Prevent mobile overflow and scrolling */
    html, body {
        overflow: hidden !important;
        height: 100vh !important;
        height: calc(var(--vh, 1vh) * 100) !important;
        width: 100vw !important;
        max-width: 100vw !important;
        margin: 0 !important;
        padding: 0 !important;
        position: relative !important;
        touch-action: manipulation;
    }

    .meeting-wrapper {
        width: 100vw !important;
        height: calc(var(--vh, 1vh) * 100) !important;
        max-width: 100vw !important;
    }

    /* Hide chat header on mobile since we have video title display */
    .chat-header {
        display: none !important;
    }

    /* Hide chat by default on mobile */
    .chat-toggle-btn {
        display: inline-flex !important;
        align-items: center;
        justify-content: center;
        gap: 4px;
        top: 14px;
        left: 14px;
        right: auto;
        min-width: 40px;
        height: 40px;
        padding: 0 12px;
        font-size: 12px;
        font-weight: 600;
        border-radius: 20px;
        box-shadow: 0 8px 18px -12px rgba(0, 0, 0, 0.4);
    }
    .chat-container {
        position: fixed;
        left: 0;
        top: 0;
        bottom: 0;
        width: min(320px, 82vw);
        max-width: 82vw;
        transform: translateX(-105%);
        transition: transform 0.28s ease;
        box-shadow: 8px 0 24px -12px rgba(0,0,0,0.45);
        z-index: 1200;
        pointer-events: none;
    }
    .chat-container.open {
        transform: translateX(0);
        pointer-events: auto;
    }
    .chat-container.open .chat-close-btn { display: flex; }
    .chat-toggle-btn.hidden { display: none !important; }

    /* Hide chat input when chat is closed */
    .chat-container:not(.open) .chat-input {
        display: none !important;
    }

    /* Ensure meeting interface fits viewport - only when active */
    #meeting-interface.active {
        height: 100vh !important;
        height: calc(var(--vh, 1vh) * 100) !important;
        max-height: calc(var(--vh, 1vh) * 100) !important;
        width: 100vw !important;
        max-width: 100vw !important;
        overflow: hidden !important;
        border-radius: 0 !important;
        margin: 0 !important;
        padding: 0 !important;
        position: fixed !important;
        top: 0 !important;
        left: 0 !important;
        right: 0 !important;
        bottom: 0 !important;
        z-index: 1 !important;
        box-sizing: border-box !important;
        display: flex !important;
        flex-direction: column !important;
    }

    /* Ensure hidden state is respected */
    #meeting-interface:not(.active) {
        display: none !important;
    }

    .chat-input {
        padding: 5px 6px;
        gap: 5px;
        align-items: center;
    }
    .chat-input input {
        flex: 1;
        min-height: 32px;
        border-radius: 8px;
        font-size: 12px;
        padding: 6px 10px;
    }
    /* Tablet / large mobile chat send button (compact size) */
    .chat-input button {
        padding: 0 !important;
        font-size: 0 !important;
        width: 30px !important;
        height: 30px !important;
        min-width: 30px !important;
        max-width: 30px !important;
        border-radius: 8px !important;
        display: inline-flex !important;
        align-items: center !important;
        justify-content: center !important;
        flex-shrink: 0;
    }
    .chat-input button::after {
        content: '➤';
        font-size: 12px;
        color: #fff;
        line-height: 1;
    }

    .meeting-body {
        position: relative;
        width: 100%;
        height: 100%;
        flex: 1;
        display: flex;
        overflow: hidden;
    }

    /* Adjust video layout for portrait tablets */
    .video-grid {
        padding: 0;
        gap: 0;
        bottom: 0;
        position: fixed;
        top: 0;
        left: 0;
        right: 0;
        width: 100vw;
        height: 100vh;
        overflow-y: auto;
        overflow-x: hidden;
        -webkit-overflow-scrolling: touch;
        align-content: start;
    }
    .video-tile {
        width: 100vw;
        max-width: 100vw;
        height: auto;
        flex: 1 1 100%;
        aspect-ratio: 16/9; /* Landscape to match actual video feeds */
        border-radius: 0;
        margin: 0;
    }

    /* Single remote video optimization for tablets - no bezels */
    .video-grid.single-remote {
        width: 100vw !important;
        height: 100vh !important;
        align-items: center;
        padding: 0 !important;
        bottom: 0 !important;
        left: 0 !important;
        right: 0 !important;
        top: 0 !important;
    }
    .video-grid.single-remote .video-tile {
        width: 100vw;
        height: 100%;
        max-width: 100%;
        max-height: 100%;
        aspect-ratio: unset !important;
        border-radius: 0;
        border: none;
        box-shadow: none;
    }
    .video-grid.single-remote .video-tile video {
        width: 100%;
        height: 100%;
        object-fit: cover;
        border-radius: inherit;
    }

    .local-video-container {
        width: 90px;
        height: auto;
        border-radius: 8px;
        aspect-ratio: 9 / 16; /* Smartphone portrait ratio */
        position: fixed;
        bottom: 85px;
        right: 20px;
        z-index: 1100;
        box-shadow: 0 4px 12px rgba(0,0,0,0.3);
    }

    /* Show local video nameplate on mobile */
    .local-video-nameplate {
        display: block !important;
        opacity: 1 !important;
    }

    /* Video title positioning for mobile - HIDDEN */
    .video-title-container {
        display: none !important;
    }

    .video-title-text {
        font-size: 13px;
        padding: 8px 12px;
        border-radius: 8px;
    }

    .meeting-controls {
        bottom: 30px;
        gap: 10px;
        position: fixed;
        left: 50%;
        transform: translateX(-50%);
        z-index: 1000;
        width: auto;
        max-width: calc(100vw - 40px);
        display: flex;
        justify-content: center;
        flex-wrap: nowrap;
    }
    .control-btn {
        width: 52px;
        height: 52px;
        font-size: 22px;
    }
    .control-btn.danger {
        width: 64px;
        height: 64px;
    }

    /* Mute indicator for mobile */
    .mute-indicator {
        width: 28px;
        height: 28px;
        top: 6px;
        right: 6px;
    }
    .mute-indicator img {
        width: 16px;
        height: 16px;
    }

    /* Form adjustments for tablets */
    .join-card {
        max-width: 500px;
        margin: 20px;
        padding: 0;
        box-shadow: 0 12px 40px rgba(0,0,0,0.15);
    }
    .join-card .card-header {
        padding: 25px 20px 20px 20px;
    }
    .join-card .card-header > div {
        width: 100px !important;
        height: 100px !important;
    }
    .join-card .card-body {
        padding: 25px;
    }
    .form-control {
        padding: 14px 16px;
        font-size: 16px;
        border-radius: 12px;
        border: 2px solid #e8eaed;
        transition: all 0.3s ease;
    }
    .form-control:focus {
        border-color: var(--bari-medium-blue);
        box-shadow: 0 0 0 3px rgba(30, 136, 229, 0.1);
        transform: translateY(-1px);
    }
    .btn-primary {
        padding: 16px;
        font-size: 16px;
        font-weight: 600;
        border-radius: 12px;
        transition: all 0.3s ease;
    }
    .btn-primary:hover {
        transform: translateY(-2px);
        box-shadow: 0 8px 25px rgba(30, 136, 229, 0.4);
    }

    /* Mobile chat input button - circular shape */
    .chat-input {
        padding: 8px 10px;
        gap: 8px;
    }
    .chat-input input {
        flex: 1;
        min-height: 42px;
        border-radius: 14px;
        font-size: 14px;
    }
    /* Remove oversized earlier definition; keep unified 34px from tablet section */
}

/* Mobile Mode - Smartphone optimized (480px - 575px) */
@media (max-width: 575px) {

    /* Force smartphone-like portrait orientation and constraints */
    html, body {
        max-width: 100vw !important;
        overflow: hidden !important;
        position: relative !important;
        width: 100vw !important;
        height: 100vh !important;
        height: calc(var(--vh, 1vh) * 100) !important;
        margin: 0 !important;
        padding: 0 !important;
    }

    /* Smartphone-optimized interface - only when active */
    #meeting-interface.active {
        border-radius: 0 !important;
        margin: 0 !important;
        box-shadow: none !important;
        width: 100vw !important;
        max-width: 100vw !important;
        position: fixed !important;
        top: 0 !important;
        left: 0 !important;
        right: 0 !important;
        bottom: 0 !important;
        height: 100vh !important;
        height: calc(var(--vh, 1vh) * 100) !important;
        display: flex !important;
        flex-direction: column !important;
        z-index: 1 !important;
        box-sizing: border-box !important;
    }

    /* Ensure hidden state on smartphones */
    #meeting-interface:not(.active) {
        display: none !important;
    }

    .chat-container {
        width: 280px;
        max-width: 80vw;
        border-radius: 0 20px 20px 0; /* Rounded right edge for mobile */
        height: 100%;
    }

    .video-grid {
        padding: 0;
        gap: 0;
        bottom: 0;
        position: fixed;
        top: 0;
        left: 0;
        right: 0;
        width: 100vw;
        height: 100vh;
        overflow-y: auto;
        overflow-x: hidden;
        -webkit-overflow-scrolling: touch;
        box-sizing: border-box;
    }

    /* Video tiles for mobile - full width, no bezels */
    .video-tile {
        width: 100%;
        max-width: 100%;
        height: auto;
        aspect-ratio: 16 / 9; /* Standard video ratio */
        border-radius: 0;
        flex: 1 1 100%;
        margin: 0;
    }

    /* Single video optimized for smartphone screen - full viewport, no bezels */
    .video-grid.single-remote {
        display: flex;
        justify-content: center;
        align-items: center;
        bottom: 0 !important;
        height: 100vh !important;
        width: 100vw !important;
        left: 0 !important;
        right: 0 !important;
        top: 0 !important;
        padding: 0 !important;
    }
    .video-grid.single-remote .video-tile {
        width: 100vw;
        height: 100%;
        max-width: 100%;
        max-height: 100%;
        aspect-ratio: unset !important;
        border-radius: 0;
        border: none;
        box-shadow: none;
        display: flex;
        align-items: center;
        justify-content: center;
    }
    .video-grid.single-remote .video-tile video {
        width: 100%;
        height: 100%;
        object-fit: cover;
        border-radius: inherit;
    }

    .local-video-container {
        width: 80px;
        height: auto;
        border-radius: 6px;
        aspect-ratio: 9 / 16; /* Smartphone portrait ratio */
        box-shadow: 0 3px 10px rgba(0,0,0,0.3);
        position: fixed;
        bottom: 75px;
        right: 16px;
        z-index: 1100;
    }

    /* Video title positioning for smartphone - HIDDEN */
    .video-title-container {
        display: none !important;
    }

    .video-title-text {
        font-size: 13px;
        padding: 8px 12px;
        border-radius: 8px;
    }

    .meeting-controls {
        bottom: 30px;
        gap: 8px;
        justify-content: center;
        padding: 0 10px;
        position: fixed;
        left: 50%;
        transform: translateX(-50%);
        z-index: 1000;
        width: auto;
        max-width: calc(100vw - 30px);
        display: flex;
        flex-wrap: nowrap;
        box-sizing: border-box;
    }
    .control-btn {
        width: 48px;
        height: 48px;
        font-size: 20px;
        border-radius: 50%; /* Circular buttons for mobile */
    }
    .control-btn.danger {
        width: 56px;
        height: 56px;
        border-radius: 50%;
    }
    .control-btn img { width: 24px; height: 24px; }
    .control-btn.danger img { width: 26px; height: 26px; }

    /* Mobile-optimized form */
    .join-card {
        max-width: calc(100vw - 30px);
        margin: 15px;
        border-radius: 24px;
        box-shadow: 0 10px 40px rgba(0,0,0,0.12);
        backdrop-filter: blur(20px);
    }
    .join-card .card-header {
        padding: 24px 20px 18px 20px;
        border-radius: 24px 24px 0 0;
    }
    .join-card .card-header > div {
        width: 80px !important;
        height: 80px !important;
        border-radius: 20px !important;
    }
    .join-card .card-body {
        padding: 24px;
        border-radius: 0 0 24px 24px;
    }
    .form-label {
        font-weight: 600;
        color: var(--bari-dark-blue);
        margin-bottom: 8px;
    }

    .form-control {
        padding: 16px 18px;
        font-size: 16px; /* Prevents zoom on iOS */
        border-radius: 16px;
        border: 2px solid #e8eaed;
        background: rgba(255, 255, 255, 0.9);
        transition: all 0.3s ease;
    }
    .form-control:focus {
        border-color: var(--bari-medium-blue);
        box-shadow: 0 0 0 4px rgba(30, 136, 229, 0.08);
        background: rgba(255, 255, 255, 1);
    }
    .btn-primary {
        padding: 18px;
        font-size: 16px;
        border-radius: 16px;
        font-weight: 600;
        background: linear-gradient(135deg, var(--bari-medium-blue), var(--bari-dark-blue));
        transition: all 0.3s ease;
        position: relative;
        overflow: hidden;
    }
    .btn-primary::before {
        content: '';
        position: absolute;
        top: 0;
        left: -100%;
        width: 100%;
        height: 100%;
        background: linear-gradient(90deg, transparent, rgba(255,255,255,0.2), transparent);
        transition: left 0.5s;
    }
    .btn-primary:hover::before {
        left: 100%;
    }
    .btn-primary:active {
        transform: scale(0.98);
    }

    /* Mobile chat input adjustments */
    .chat-input {
        padding: 4px 5px;
        gap: 4px;
        align-items: center;
    }
    .chat-input input {
        flex: 1;
        min-height: 30px;
        border-radius: 8px;
        font-size: 12px;
        padding: 5px 8px;
    }
    /* Smartphone optimized sizing */
    .chat-input button {
        width: 28px !important;
        height: 28px !important;
        min-width: 28px !important;
        max-width: 28px !important;
        border-radius: 8px !important;
        flex-shrink: 0;
    }
}

/* Ultra Small Mobile - Compact smartphone (320px - 479px) */
@media (max-width: 479px) {
    
    /* Ultra-compact smartphone layout */
    .chat-container {
        width: 260px;
        max-width: 75vw;
        border-radius: 0 15px 15px 0;
    }

    .video-grid {
        padding: 0;
        bottom: 0;
        height: 100vh;
        position: fixed;
        width: 100vw;
        left: 0;
        right: 0;
        top: 0;
        gap: 0;
    }

    /* Compact single video for smallest screens - no bezels */
    .video-grid.single-remote {
        display: flex;
        justify-content: center;
        align-items: center;
        padding: 0 !important;
        bottom: 0 !important;
        height: 100vh !important;
        width: 100vw !important;
        left: 0 !important;
        right: 0 !important;
        top: 0 !important;
    }
    .video-grid.single-remote .video-tile {
        width: 100vw;
        height: 100%;
        max-width: 100%;
        max-height: 100%;
        aspect-ratio: unset !important;
        border-radius: 0;
        border: none;
        box-shadow: none;
    }
    .video-grid.single-remote .video-tile video {
        width: 100%;
        height: 100%;
        object-fit: cover;
        border-radius: inherit;
    }

    .local-video-container {
        width: 70px;
        height: auto;
        border-radius: 6px;
        aspect-ratio: 9 / 16; /* Smartphone portrait ratio */
        box-shadow: 0 2px 8px rgba(0,0,0,0.25);
        position: fixed;
        bottom: 70px;
        right: 12px;
        z-index: 1100;
    }

    /* Video title positioning for ultra-compact mobile - HIDDEN */
    .video-title-container {
        display: none !important;
    }

    .video-title-text {
        font-size: 13px;
        padding: 8px 12px;
        border-radius: 8px;
    }

    .meeting-controls {
        bottom: 30px;
        bottom: calc(30px + env(safe-area-inset-bottom, 0px));
        gap: 5px;
        justify-content: center;
        padding: 0 8px;
        position: fixed;
        left: 50%;
        transform: translateX(-50%);
        z-index: 1000;
    }
    .control-btn {
        width: 44px;
        height: 44px;
        font-size: 18px;
        border-radius: 50%; /* Circular for mobile */
    }
    .control-btn.danger {
        width: 50px;
        height: 50px;
        border-radius: 50%;
    }

    /* Ultra-compact form */
    .join-card {
        max-width: calc(100vw - 20px);
        margin: 10px;
        border-radius: 20px;
        box-shadow: 0 8px 32px rgba(0,0,0,0.1);
        backdrop-filter: blur(20px);
    }
    .join-card .card-header {
        padding: 20px 16px 16px 16px;
        border-radius: 20px 20px 0 0;
    }
    .join-card .card-header > div {
        width: 70px !important;
        height: 70px !important;
        border-radius: 18px !important;
    }
    .join-card .card-body {
        padding: 20px;
        border-radius: 0 0 20px 20px;
    }

    .form-control {
        padding: 14px 16px;
        border-radius: 14px;
        background: rgba(255, 255, 255, 0.95);
    }
    .btn-primary {
        padding: 16px;
        border-radius: 14px;
        background: linear-gradient(135deg, var(--bari-medium-blue), var(--bari-dark-blue));
    }

    /* Ultra-compact chat input */
    .chat-input {
        padding: 3px 4px;
        gap: 3px;
        align-items: center;
    }
    .chat-input input {
        flex: 1;
        min-height: 28px;
        border-radius: 6px;
        font-size: 11px;
        padding: 4px 6px;
    }
    .chat-input button {
        width: 26px !important;
        height: 26px !important;
        min-width: 26px !important;
        max-width: 26px !important;
        border-radius: 6px !important;
        flex-shrink: 0;
    }
}

/* Mobile Landscape orientation adjustments */
@media (max-height: 600px) and (orientation: landscape) and (max-width: 767px) {
    /* Maintain smartphone-like proportions even in landscape */

    .meeting-controls {
        bottom: 8px;
        gap: 6px;
        justify-content: center;
    }
    .control-btn {
        width: 42px;
        height: 42px;
        border-radius: 50%;
    }
    .control-btn.danger {
        width: 48px;
        height: 48px;
        border-radius: 50%;
    }

    .join-card {
        max-width: 450px;
        margin: 15px auto;
        border-radius: 20px;
    }
}

/* Mobile Chat Notification Circle */
.chat-toggle-btn {
    position: relative;
}

.chat-toggle-btn::after {
    content: '';
    position: absolute;
    top: -5px;
    right: -5px;
    width: 12px;
    height: 12px;
    background: #dc3545;
    border-radius: 50%;
    opacity: 0;
    transform: scale(0);
    transition: all 0.3s ease;
    z-index: 1001;
}

.chat-toggle-btn.has-notification::after {
    opacity: 1;
    transform: scale(1);
}

/* Touch-friendly adjustments for mobile */
@media (pointer: coarse) and (max-width: 767px) {
    .control-btn {
        min-height: 44px;
        min-width: 44px;
    }
    .chat-input input {
        min-height: 44px;
    }
    .chat-input button {
        min-height: 44px;
        min-width: 44px;
    }
}

/* Fix for Android browser bottom elements */
@supports (padding-bottom: env(safe-area-inset-bottom)) {
    .meeting-controls {
        padding-bottom: env(safe-area-inset-bottom);
    }
}

/* Android browser toolbar fixes */
body.firefox-android {
    padding-top: var(--safe-area-top, 0px);
}

body.chrome-android {
    padding-bottom: var(--safe-area-bottom, 0px);
}

body.samsung-device {
    /* Samsung One UI specific adjustments */
    padding-bottom: calc(var(--safe-area-bottom, 0px) + env(safe-area-inset-bottom, 0px));
}

/* Fix meeting container for Android browsers */
body.android-toolbar-visible #meeting-container {
    height: calc(var(--available-height, 100vh) - var(--safe-area-top, 0px) - var(--safe-area-bottom, 0px));
    position: relative;
}

body.android-toolbar-visible #flow-meeting {
    height: 100%;
    display: flex;
    flex-direction: column;
}

body.android-toolbar-visible #meeting-interface {
    flex: 1;
    display: flex;
    flex-direction: column;
    overflow: hidden;
}

body.android-toolbar-visible .meeting-body {
    flex: 1;
    display: flex;
    overflow: hidden;
}

body.android-toolbar-visible #tile-area {
    flex: 1;
    overflow-y: auto;
    overflow-x: hidden;
    -webkit-overflow-scrolling: touch;
}

body.android-toolbar-visible .controls {
    position: relative;
    bottom: auto;
    padding-bottom: calc(var(--safe-area-bottom, 10px) + 10px);
}

/* Samsung Galaxy A54 specific (412x915 viewport) */
@media screen and (max-width: 412px) and (max-height: 915px) {
    body {
        position: fixed;
        width: 100%;
        height: 100%;
        overflow: hidden;
    }

    #meeting-container {
        position: fixed;
        top: 0;
        left: 0;
        right: 0;
        bottom: 0;
        height: calc(var(--available-height, 100vh));
        overflow: hidden;
    }

    #flow-meeting {
        height: 100%;
        position: relative;
    }

    .video-tile {
        max-height: 160px !important;
        width: 100% !important;
    }

    .controls {
        position: fixed;
        bottom: calc(var(--safe-area-bottom, 0px) + 10px);
        left: 50%;
        transform: translateX(-50%);
        z-index: 500;
    }

    /* Prevent content from being cut off */
    .meeting-wrapper {
        height: calc(var(--available-height, 100vh));
        position: fixed;
        width: 100%;
        top: 0;
        overflow: hidden;
    }
}

/* Firefox Android specific fixes */
body.firefox-android #meeting-interface {
    margin-top: var(--safe-area-top, 0px);
    height: calc(100% - var(--safe-area-top, 0px));
}

/* Chrome Android specific fixes */
body.chrome-android #meeting-interface {
    margin-bottom: var(--safe-area-bottom, 0px);
    height: calc(100% - var(--safe-area-bottom, 0px));
}

/* Ensure video tiles stay within viewport */
body.android-toolbar-visible .video-tile {
    max-height: calc((var(--available-height, 100vh) - 180px) / 2);
}

/* Mobile-specific styles for chat input */
@media (max-width: 768px) {
    .chat-input {
        padding: 4px 5px;
        gap: 4px;
        align-items: center;
        box-sizing: border-box;
    }

    .chat-input input {
        flex: 1;
        padding: 6px 8px;
        font-size: 12px;
        min-height: 30px;
        border-radius: 8px;
    }

    .chat-input button {
        padding: 0;
        min-width: 30px;
        width: 30px;
        height: 30px;
        max-width: 30px;
        font-size: 0;
        border-radius: 8px;
        flex-shrink: 0;
        display: inline-flex;
        align-items: center;
        justify-content: center;
    }

    .chat-input button::after {
        content: '➤';
        font-size: 12px;
        color: #fff;
        line-height: 1;
    }
}

/* Mobile Chat Toggle Button - Hidden by default */
.chat-toggle-btn {
    display: none;
    position: fixed;
    top: 15px;
    left: 15px;
    right: auto;
    z-index: 1100;
    background: rgba(13, 71, 161, 0.9);
    color: white;
    border: none;
    border-radius: 24px;
    padding: 12px 18px;
    font-size: 14px;
    font-weight: 600;
    cursor: pointer;
    backdrop-filter: blur(10px);
    box-shadow: 0 4px 12px rgba(0,0,0,0.3);
    align-items: center;
    gap: 6px;
}

.chat-toggle-btn::after {
    content: '';
    position: absolute;
    top: -5px;
    right: -5px;
    width: 18px;
    height: 18px;
    background: #dc3545;
    border-radius: 50%;
    opacity: 0;
    transform: scale(0);
    transition: all 0.3s ease;
    z-index: 1200;
}

.chat-toggle-btn.has-notification::after {
    opacity: 1;
    transform: scale(1);
}

.chat-close-btn {
    display: none;
    position: absolute;
    top: 8px;
    right: 8px;
    width: 24px;
    height: 24px;
    background: #dc3545;
    color: white;
    border: none;
    border-radius: 50%;
    font-size: 12px;
    font-weight: bold;
    cursor: pointer;
    z-index: 1001;
    align-items: center;
    justify-content: center;
    box-shadow: 0 2px 6px rgba(220, 53, 69, 0.4);
    transition: transform 0.2s ease, box-shadow 0.2s ease;
}

.chat-close-btn:hover {
    transform: scale(1.1);
    box-shadow: 0 3px 8px rgba(220, 53, 69, 0.6);
}

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