/**
 * Mirror AI - Responsive Design System v2.0
 *
 * A mobile-first, production-ready responsive framework.
 * This file should be loaded FIRST before any other CSS.
 *
 * Table of Contents:
 * 1. CSS Custom Properties (Design Tokens)
 * 2. Breakpoint System
 * 3. Safe Area Handling
 * 4. Viewport Fixes
 * 5. Container System
 * 6. Grid System
 * 7. Spacing Utilities
 * 8. Widget Base Styles
 * 9. Responsive Utilities
 * 10. Accessibility
 */

/* ==========================================================================
   1. CSS CUSTOM PROPERTIES (DESIGN TOKENS)
   ========================================================================== */

:root {
    /* -------------------------------------------------------------------------
       Breakpoints (for reference - use via media queries)
       Mobile-first: styles apply from 0px up, then override at breakpoints
       ------------------------------------------------------------------------- */
    --bp-sm: 480px;   /* Large phones */
    --bp-md: 768px;   /* Tablets */
    --bp-lg: 1024px;  /* Desktops */
    --bp-xl: 1440px;  /* Large desktops */

    /* -------------------------------------------------------------------------
       Spacing Scale (4px base unit)
       ------------------------------------------------------------------------- */
    --space-0: 0;
    --space-1: 0.25rem;   /* 4px */
    --space-2: 0.5rem;    /* 8px */
    --space-3: 0.75rem;   /* 12px */
    --space-4: 1rem;      /* 16px */
    --space-5: 1.25rem;   /* 20px */
    --space-6: 1.5rem;    /* 24px */
    --space-7: 2rem;      /* 32px */
    --space-8: 2.5rem;    /* 40px */
    --space-9: 3rem;      /* 48px */
    --space-10: 4rem;     /* 64px */
    --space-11: 5rem;     /* 80px */
    --space-12: 6rem;     /* 96px */

    /* -------------------------------------------------------------------------
       Container Widths
       ------------------------------------------------------------------------- */
    --container-sm: 540px;
    --container-md: 720px;
    --container-lg: 960px;
    --container-xl: 1140px;
    --container-xxl: 1320px;
    --container-fluid: 100%;

    /* -------------------------------------------------------------------------
       Safe Area Insets (updated by JS for notched devices)
       Default to 0, env() provides runtime values
       ------------------------------------------------------------------------- */
    --safe-area-top: env(safe-area-inset-top, 0px);
    --safe-area-right: env(safe-area-inset-right, 0px);
    --safe-area-bottom: env(safe-area-inset-bottom, 0px);
    --safe-area-left: env(safe-area-inset-left, 0px);

    /* -------------------------------------------------------------------------
       Layout Heights
       ------------------------------------------------------------------------- */
    --header-height: 60px;
    --header-height-mobile: 56px;
    --footer-height: auto;
    --input-area-height: 64px;
    --input-area-height-mobile: 60px;

    /* -------------------------------------------------------------------------
       Widget Dimensions (responsive)
       ------------------------------------------------------------------------- */
    --widget-width: min(350px, calc(100vw - var(--space-8)));
    --widget-height: min(500px, calc(100vh - 120px));
    --widget-width-mobile: calc(100vw - var(--space-4));
    --widget-height-mobile: calc(100vh - var(--header-height-mobile) - var(--safe-area-top) - var(--safe-area-bottom) - var(--space-4));

    /* -------------------------------------------------------------------------
       Sidebar Dimensions
       ------------------------------------------------------------------------- */
    --sidebar-width: 280px;
    --sidebar-width-collapsed: 60px;
    --sidebar-width-mobile: min(280px, calc(100vw - 60px));

    /* -------------------------------------------------------------------------
       Z-Index Scale
       ------------------------------------------------------------------------- */
    --z-dropdown: 100;
    --z-sticky: 200;
    --z-fixed: 300;
    --z-sidebar: 400;
    --z-modal-backdrop: 500;
    --z-modal: 600;
    --z-popover: 700;
    --z-tooltip: 800;
    --z-toast: 900;
    --z-max: 9999;

    /* -------------------------------------------------------------------------
       Typography Scale (fluid)
       ------------------------------------------------------------------------- */
    --font-size-xs: clamp(0.75rem, 0.7rem + 0.25vw, 0.8125rem);    /* 12-13px */
    --font-size-sm: clamp(0.8125rem, 0.775rem + 0.25vw, 0.875rem); /* 13-14px */
    --font-size-base: clamp(0.875rem, 0.85rem + 0.25vw, 1rem);     /* 14-16px */
    --font-size-md: clamp(1rem, 0.95rem + 0.25vw, 1.125rem);       /* 16-18px */
    --font-size-lg: clamp(1.125rem, 1rem + 0.5vw, 1.25rem);        /* 18-20px */
    --font-size-xl: clamp(1.25rem, 1.1rem + 0.75vw, 1.5rem);       /* 20-24px */
    --font-size-2xl: clamp(1.5rem, 1.25rem + 1vw, 2rem);           /* 24-32px */
    --font-size-3xl: clamp(1.875rem, 1.5rem + 1.5vw, 2.5rem);      /* 30-40px */
    --font-size-4xl: clamp(2.25rem, 1.75rem + 2vw, 3rem);          /* 36-48px */

    /* -------------------------------------------------------------------------
       Border Radius
       ------------------------------------------------------------------------- */
    --radius-sm: 4px;
    --radius-md: 8px;
    --radius-lg: 12px;
    --radius-xl: 16px;
    --radius-2xl: 24px;
    --radius-full: 9999px;

    /* -------------------------------------------------------------------------
       Transitions
       ------------------------------------------------------------------------- */
    --transition-fast: 150ms ease;
    --transition-base: 250ms ease;
    --transition-slow: 350ms ease;
    --transition-slower: 500ms ease;

    /* -------------------------------------------------------------------------
       Touch Target Minimum (WCAG 2.1 AA)
       ------------------------------------------------------------------------- */
    --touch-target-min: 44px;

    /* -------------------------------------------------------------------------
       Viewport Height Fix (updated by JS)
       Solves the mobile browser address bar issue
       ------------------------------------------------------------------------- */
    --vh: 1vh;
    --dvh: 1dvh; /* Dynamic viewport height (modern browsers) */

    /* -------------------------------------------------------------------------
       Content Width Constraints
       ------------------------------------------------------------------------- */
    --content-max-width: 65ch; /* Optimal reading width */
    --message-max-width: min(85%, 600px);
}

/* ==========================================================================
   2. BREAKPOINT SYSTEM (Mobile-First Media Queries)
   ========================================================================== */

/*
 * Usage: Start with mobile styles, then add overrides at each breakpoint
 *
 * @media (min-width: 480px)  { } -- sm: Large phones
 * @media (min-width: 768px)  { } -- md: Tablets
 * @media (min-width: 1024px) { } -- lg: Desktops
 * @media (min-width: 1440px) { } -- xl: Large desktops
 */

/* Small devices (landscape phones, 480px and up) */
@media (min-width: 480px) {
    :root {
        --header-height: 60px;
        --input-area-height: 64px;
    }
}

/* Medium devices (tablets, 768px and up) */
@media (min-width: 768px) {
    :root {
        --header-height: 64px;
    }
}

/* Large devices (desktops, 1024px and up) */
@media (min-width: 1024px) {
    :root {
        --header-height: 70px;
    }
}

/* ==========================================================================
   3. SAFE AREA HANDLING (Notched Devices)
   ========================================================================== */

/* Apply safe area padding to fixed/sticky elements */
@supports (padding: env(safe-area-inset-top)) {
    .safe-area-top {
        padding-top: env(safe-area-inset-top);
    }

    .safe-area-bottom {
        padding-bottom: env(safe-area-inset-bottom);
    }

    .safe-area-left {
        padding-left: env(safe-area-inset-left);
    }

    .safe-area-right {
        padding-right: env(safe-area-inset-right);
    }

    .safe-area-x {
        padding-left: env(safe-area-inset-left);
        padding-right: env(safe-area-inset-right);
    }

    .safe-area-y {
        padding-top: env(safe-area-inset-top);
        padding-bottom: env(safe-area-inset-bottom);
    }

    .safe-area-all {
        padding: env(safe-area-inset-top) env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left);
    }
}

/* ==========================================================================
   4. VIEWPORT FIXES
   ========================================================================== */

/*
 * Fix for mobile browsers where 100vh includes the address bar
 * JS updates --vh on resize/orientation change
 */
.full-height {
    height: 100vh;
    height: calc(var(--vh, 1vh) * 100);
    height: 100dvh; /* Modern browsers - dynamic viewport height */
}

.min-full-height {
    min-height: 100vh;
    min-height: calc(var(--vh, 1vh) * 100);
    min-height: 100dvh;
}

.max-full-height {
    max-height: 100vh;
    max-height: calc(var(--vh, 1vh) * 100);
    max-height: 100dvh;
}

/* Prevent horizontal overflow - CRITICAL FIX */
html, body {
    overflow-x: hidden;
    width: 100%;
    max-width: 100%;
}

/* Fix for elements that incorrectly use 100vw */
.full-width {
    width: 100%;
    max-width: 100%;
}

/* ==========================================================================
   5. CONTAINER SYSTEM
   ========================================================================== */

.r-container {
    width: 100%;
    margin-left: auto;
    margin-right: auto;
    padding-left: var(--space-4);
    padding-right: var(--space-4);
}

/* Responsive container max-widths */
@media (min-width: 480px) {
    .r-container {
        padding-left: var(--space-5);
        padding-right: var(--space-5);
    }
}

@media (min-width: 768px) {
    .r-container {
        padding-left: var(--space-6);
        padding-right: var(--space-6);
        max-width: var(--container-md);
    }

    .r-container-fluid {
        max-width: none;
    }
}

@media (min-width: 1024px) {
    .r-container {
        padding-left: var(--space-7);
        padding-right: var(--space-7);
        max-width: var(--container-lg);
    }
}

@media (min-width: 1440px) {
    .r-container {
        max-width: var(--container-xl);
    }
}

/* Container variants */
.r-container-sm { max-width: var(--container-sm); }
.r-container-md { max-width: var(--container-md); }
.r-container-lg { max-width: var(--container-lg); }
.r-container-xl { max-width: var(--container-xl); }
.r-container-xxl { max-width: var(--container-xxl); }
.r-container-fluid { max-width: none; }

/* ==========================================================================
   6. GRID SYSTEM
   ========================================================================== */

/* Auto-fit responsive grid */
.r-grid {
    display: grid;
    gap: var(--space-4);
    grid-template-columns: 1fr;
}

/* 2-column grid on tablet+ */
.r-grid-2 {
    grid-template-columns: repeat(auto-fit, minmax(min(100%, 280px), 1fr));
}

/* 3-column grid */
.r-grid-3 {
    grid-template-columns: repeat(auto-fit, minmax(min(100%, 250px), 1fr));
}

/* 4-column grid */
.r-grid-4 {
    grid-template-columns: repeat(auto-fit, minmax(min(100%, 220px), 1fr));
}

/* Fixed column grids with responsive behavior */
@media (min-width: 768px) {
    .r-grid-md-2 { grid-template-columns: repeat(2, 1fr); }
    .r-grid-md-3 { grid-template-columns: repeat(3, 1fr); }
}

@media (min-width: 1024px) {
    .r-grid-lg-2 { grid-template-columns: repeat(2, 1fr); }
    .r-grid-lg-3 { grid-template-columns: repeat(3, 1fr); }
    .r-grid-lg-4 { grid-template-columns: repeat(4, 1fr); }
}

/* Sidebar + Content layout */
.r-layout-sidebar {
    display: grid;
    grid-template-columns: 1fr;
    gap: 0;
}

@media (min-width: 768px) {
    .r-layout-sidebar {
        grid-template-columns: var(--sidebar-width) 1fr;
    }

    .r-layout-sidebar.sidebar-right {
        grid-template-columns: 1fr var(--sidebar-width);
    }
}

/* ==========================================================================
   7. SPACING UTILITIES
   ========================================================================== */

/* Margin utilities */
.r-m-0 { margin: var(--space-0); }
.r-m-1 { margin: var(--space-1); }
.r-m-2 { margin: var(--space-2); }
.r-m-3 { margin: var(--space-3); }
.r-m-4 { margin: var(--space-4); }
.r-m-5 { margin: var(--space-5); }
.r-m-6 { margin: var(--space-6); }
.r-m-7 { margin: var(--space-7); }
.r-m-8 { margin: var(--space-8); }
.r-m-auto { margin: auto; }

/* Padding utilities */
.r-p-0 { padding: var(--space-0); }
.r-p-1 { padding: var(--space-1); }
.r-p-2 { padding: var(--space-2); }
.r-p-3 { padding: var(--space-3); }
.r-p-4 { padding: var(--space-4); }
.r-p-5 { padding: var(--space-5); }
.r-p-6 { padding: var(--space-6); }
.r-p-7 { padding: var(--space-7); }
.r-p-8 { padding: var(--space-8); }

/* Directional spacing (responsive) */
.r-px-4 { padding-left: var(--space-4); padding-right: var(--space-4); }
.r-py-4 { padding-top: var(--space-4); padding-bottom: var(--space-4); }
.r-mx-auto { margin-left: auto; margin-right: auto; }

/* Gap utilities for flex/grid */
.r-gap-1 { gap: var(--space-1); }
.r-gap-2 { gap: var(--space-2); }
.r-gap-3 { gap: var(--space-3); }
.r-gap-4 { gap: var(--space-4); }
.r-gap-5 { gap: var(--space-5); }
.r-gap-6 { gap: var(--space-6); }

/* ==========================================================================
   8. WIDGET BASE STYLES
   ========================================================================== */

/* Responsive widget container */
.r-widget {
    position: fixed;
    width: var(--widget-width);
    height: var(--widget-height);
    max-width: calc(100vw - var(--space-4));
    max-height: calc(100vh - var(--space-4));
    max-height: calc(100dvh - var(--space-4));
    border-radius: var(--radius-xl);
    overflow: hidden;
    z-index: var(--z-modal);
}

/* Widget positioning */
.r-widget-bottom-right {
    bottom: max(var(--space-4), env(safe-area-inset-bottom, var(--space-4)));
    right: max(var(--space-4), env(safe-area-inset-right, var(--space-4)));
}

.r-widget-bottom-left {
    bottom: max(var(--space-4), env(safe-area-inset-bottom, var(--space-4)));
    left: max(var(--space-4), env(safe-area-inset-left, var(--space-4)));
}

.r-widget-center {
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
}

/* Full-screen widget on mobile */
@media (max-width: 479px) {
    .r-widget-fullscreen-mobile {
        width: 100%;
        height: 100%;
        max-width: 100%;
        max-height: 100%;
        top: 0;
        left: 0;
        right: 0;
        bottom: 0;
        border-radius: 0;
        padding-top: env(safe-area-inset-top, 0);
        padding-bottom: env(safe-area-inset-bottom, 0);
    }
}

/* Responsive sidebar widget */
.r-sidebar {
    position: fixed;
    top: 0;
    left: 0;
    width: var(--sidebar-width-mobile);
    height: 100%;
    height: 100dvh;
    z-index: var(--z-sidebar);
    transform: translateX(-100%);
    transition: transform var(--transition-base);
    padding-top: env(safe-area-inset-top, 0);
    padding-bottom: env(safe-area-inset-bottom, 0);
}

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

@media (min-width: 768px) {
    .r-sidebar {
        width: var(--sidebar-width);
        transform: translateX(0);
        position: relative;
        height: auto;
    }

    .r-sidebar.collapsible {
        transform: translateX(0);
    }
}

/* Responsive modal */
.r-modal {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    display: flex;
    align-items: flex-end; /* Mobile: slide up from bottom */
    justify-content: center;
    z-index: var(--z-modal);
    padding: var(--space-4);
    padding-bottom: max(var(--space-4), env(safe-area-inset-bottom));
}

.r-modal-content {
    width: 100%;
    max-width: min(90vw, 500px);
    max-height: calc(100vh - var(--space-8));
    max-height: calc(100dvh - var(--space-8));
    overflow-y: auto;
    border-radius: var(--radius-xl) var(--radius-xl) 0 0;
}

@media (min-width: 768px) {
    .r-modal {
        align-items: center; /* Desktop: center */
        padding: var(--space-6);
    }

    .r-modal-content {
        border-radius: var(--radius-xl);
    }
}

/* ==========================================================================
   9. RESPONSIVE UTILITIES
   ========================================================================== */

/* Display utilities */
.r-hidden { display: none !important; }
.r-block { display: block !important; }
.r-flex { display: flex !important; }
.r-grid-display { display: grid !important; }
.r-inline { display: inline !important; }
.r-inline-block { display: inline-block !important; }
.r-inline-flex { display: inline-flex !important; }

/* Mobile-only (hidden on tablet+) */
@media (min-width: 768px) {
    .r-hidden-md-up { display: none !important; }
}

/* Tablet+ only (hidden on mobile) */
@media (max-width: 767px) {
    .r-hidden-sm-down { display: none !important; }
}

/* Desktop+ only */
@media (max-width: 1023px) {
    .r-hidden-md-down { display: none !important; }
}

@media (min-width: 1024px) {
    .r-hidden-lg-up { display: none !important; }
}

/* Flex utilities */
.r-flex-row { flex-direction: row; }
.r-flex-col { flex-direction: column; }
.r-flex-wrap { flex-wrap: wrap; }
.r-flex-nowrap { flex-wrap: nowrap; }
.r-flex-1 { flex: 1; }
.r-flex-auto { flex: auto; }
.r-flex-none { flex: none; }

.r-items-start { align-items: flex-start; }
.r-items-center { align-items: center; }
.r-items-end { align-items: flex-end; }
.r-items-stretch { align-items: stretch; }

.r-justify-start { justify-content: flex-start; }
.r-justify-center { justify-content: center; }
.r-justify-end { justify-content: flex-end; }
.r-justify-between { justify-content: space-between; }
.r-justify-around { justify-content: space-around; }

/* Stack on mobile, row on tablet+ */
.r-stack-to-row {
    display: flex;
    flex-direction: column;
    gap: var(--space-4);
}

@media (min-width: 768px) {
    .r-stack-to-row {
        flex-direction: row;
    }
}

/* Text utilities */
.r-text-xs { font-size: var(--font-size-xs); }
.r-text-sm { font-size: var(--font-size-sm); }
.r-text-base { font-size: var(--font-size-base); }
.r-text-md { font-size: var(--font-size-md); }
.r-text-lg { font-size: var(--font-size-lg); }
.r-text-xl { font-size: var(--font-size-xl); }
.r-text-2xl { font-size: var(--font-size-2xl); }
.r-text-3xl { font-size: var(--font-size-3xl); }
.r-text-4xl { font-size: var(--font-size-4xl); }

.r-text-left { text-align: left; }
.r-text-center { text-align: center; }
.r-text-right { text-align: right; }

/* Truncate text */
.r-truncate {
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.r-line-clamp-2 {
    display: -webkit-box;
    -webkit-line-clamp: 2;
    -webkit-box-orient: vertical;
    overflow: hidden;
}

.r-line-clamp-3 {
    display: -webkit-box;
    -webkit-line-clamp: 3;
    -webkit-box-orient: vertical;
    overflow: hidden;
}

/* Width utilities */
.r-w-full { width: 100%; }
.r-w-auto { width: auto; }
.r-w-screen { width: 100vw; max-width: 100%; } /* Safe 100vw */
.r-max-w-full { max-width: 100%; }
.r-max-w-prose { max-width: var(--content-max-width); }

/* Height utilities */
.r-h-full { height: 100%; }
.r-h-auto { height: auto; }
.r-h-screen { height: 100vh; height: 100dvh; }
.r-min-h-screen { min-height: 100vh; min-height: 100dvh; }

/* Position utilities */
.r-relative { position: relative; }
.r-absolute { position: absolute; }
.r-fixed { position: fixed; }
.r-sticky { position: sticky; }

.r-inset-0 { top: 0; right: 0; bottom: 0; left: 0; }
.r-top-0 { top: 0; }
.r-right-0 { right: 0; }
.r-bottom-0 { bottom: 0; }
.r-left-0 { left: 0; }

/* Overflow utilities */
.r-overflow-hidden { overflow: hidden; }
.r-overflow-auto { overflow: auto; }
.r-overflow-scroll { overflow: scroll; }
.r-overflow-x-auto { overflow-x: auto; }
.r-overflow-y-auto { overflow-y: auto; }

/* Scrollable container with momentum */
.r-scroll-y {
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
    overscroll-behavior-y: contain;
}

.r-scroll-x {
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    overscroll-behavior-x: contain;
}

/* ==========================================================================
   10. ACCESSIBILITY
   ========================================================================== */

/* Minimum touch target size */
.r-touch-target {
    min-width: var(--touch-target-min);
    min-height: var(--touch-target-min);
}

/* Focus visible outline */
.r-focus-visible:focus-visible {
    outline: 2px solid var(--accent-cyan, #00f2fe);
    outline-offset: 2px;
}

/* Screen reader only */
.r-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;
}

/* Skip to main content link */
.r-skip-link {
    position: absolute;
    top: -100%;
    left: var(--space-4);
    z-index: var(--z-max);
    padding: var(--space-3) var(--space-4);
    background: var(--bg-primary, #0a0e27);
    color: var(--text-primary, #fff);
    border-radius: var(--radius-md);
    transition: top var(--transition-fast);
}

.r-skip-link:focus {
    top: var(--space-4);
}

/* Reduce motion for users who prefer it */
@media (prefers-reduced-motion: reduce) {
    *,
    *::before,
    *::after {
        animation-duration: 0.01ms !important;
        animation-iteration-count: 1 !important;
        transition-duration: 0.01ms !important;
        scroll-behavior: auto !important;
    }
}

/* ==========================================================================
   11. CHAT INTERFACE BASE (New Responsive System)
   ========================================================================== */

/* Main chat layout */
.r-chat-layout {
    display: flex;
    flex-direction: column;
    height: 100vh;
    height: 100dvh;
    overflow: hidden;
}

/* Chat header */
.r-chat-header {
    flex-shrink: 0;
    height: var(--header-height-mobile);
    padding: 0 var(--space-4);
    padding-top: env(safe-area-inset-top, 0);
    display: flex;
    align-items: center;
    z-index: var(--z-sticky);
}

@media (min-width: 768px) {
    .r-chat-header {
        height: var(--header-height);
        padding: 0 var(--space-6);
    }
}

/* Chat messages area */
.r-chat-messages {
    flex: 1;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
    overscroll-behavior-y: contain;
    padding: var(--space-4);
    padding-bottom: calc(var(--input-area-height-mobile) + var(--space-4));
}

@media (min-width: 768px) {
    .r-chat-messages {
        padding: var(--space-6);
        padding-bottom: calc(var(--input-area-height) + var(--space-6));
    }
}

/* Chat message bubble */
.r-chat-message {
    max-width: var(--message-max-width);
    padding: var(--space-3) var(--space-4);
    border-radius: var(--radius-lg);
    margin-bottom: var(--space-3);
    word-wrap: break-word;
    overflow-wrap: break-word;
}

.r-chat-message.user {
    margin-left: auto;
}

.r-chat-message.assistant {
    margin-right: auto;
}

/* Chat input area */
.r-chat-input-area {
    flex-shrink: 0;
    position: fixed;
    bottom: 0;
    left: 0;
    right: 0;
    padding: var(--space-3) var(--space-4);
    padding-bottom: max(var(--space-3), env(safe-area-inset-bottom));
    z-index: var(--z-sticky);
}

@media (min-width: 768px) {
    .r-chat-input-area {
        position: relative;
        padding: var(--space-4) var(--space-6);
    }
}

/* Chat input field */
.r-chat-input {
    width: 100%;
    min-height: var(--touch-target-min);
    max-height: 120px;
    padding: var(--space-3) var(--space-4);
    border-radius: var(--radius-xl);
    resize: none;
    font-size: var(--font-size-base);
}

/* ==========================================================================
   12. RESPONSIVE IMAGES & MEDIA
   ========================================================================== */

.r-img-responsive {
    max-width: 100%;
    height: auto;
    display: block;
}

.r-aspect-square { aspect-ratio: 1 / 1; }
.r-aspect-video { aspect-ratio: 16 / 9; }
.r-aspect-portrait { aspect-ratio: 3 / 4; }

/* Object fit utilities */
.r-object-cover { object-fit: cover; }
.r-object-contain { object-fit: contain; }
.r-object-fill { object-fit: fill; }

/* ==========================================================================
   13. BACKGROUND FIXES
   ========================================================================== */

/* Safe full-screen background (no horizontal scroll) */
.r-bg-fullscreen {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    pointer-events: none;
    z-index: -1;
}

/* Never use 100vw for width - use this instead */
.r-bg-cover {
    position: absolute;
    inset: 0;
    width: 100%;
    height: 100%;
}

/* ==========================================================================
   14. PRINT STYLES
   ========================================================================== */

@media print {
    .r-no-print { display: none !important; }

    .r-sidebar,
    .r-widget,
    .r-modal {
        display: none !important;
    }

    body {
        background: white !important;
        color: black !important;
    }
}

/* ==========================================================================
   15. LEGACY CLASS COMPATIBILITY - RESPONSIVE MESSAGE BUBBLES
   Provides responsive behavior for existing class names in the codebase
   ========================================================================== */

/* Message bubble responsive widths */
.message-bubble,
.chat-message .message-content {
    max-width: var(--message-max-width);
    word-wrap: break-word;
    overflow-wrap: break-word;
}

/* Mobile: Full width bubbles for easier reading */
@media (max-width: 479px) {
    .message-bubble,
    .chat-message .message-content {
        max-width: 95%;
    }
}

/* Tablet: Slightly constrained */
@media (min-width: 480px) and (max-width: 767px) {
    .message-bubble,
    .chat-message .message-content {
        max-width: 88%;
    }
}

/* Desktop: Standard max width */
@media (min-width: 768px) {
    .message-bubble,
    .chat-message .message-content {
        max-width: min(85%, 700px);
    }
}

/* Large desktop: Slightly wider */
@media (min-width: 1024px) {
    .message-bubble,
    .chat-message .message-content {
        max-width: min(80%, 800px);
    }
}

/* ==========================================================================
   16. DASHBOARD/CHAT LAYOUT FIXES
   Ensures the chat interface works properly at all breakpoints
   ========================================================================== */

/* Main chat container responsive behavior */
.chat-main,
#chat-view {
    display: flex;
    flex-direction: column;
    height: 100%;
    min-height: 0; /* Prevents flex overflow */
}

/* Chat messages container */
.chat-messages-container {
    flex: 1;
    overflow-y: auto;
    overflow-x: hidden;
    -webkit-overflow-scrolling: touch; /* Smooth scrolling on iOS */
    overscroll-behavior: contain; /* Prevents scroll chaining */
}

/* Chat input area sticky to bottom */
.chat-input-container {
    flex-shrink: 0;
    padding: var(--space-3) var(--space-4);
    padding-bottom: max(var(--space-3), var(--safe-area-bottom));
}

/* Tablet-specific adjustments (768px - 1023px) */
@media (min-width: 768px) and (max-width: 1023px) {
    .modern-chat-container {
        gap: var(--space-4);
    }

    .chat-sidebar {
        width: var(--sidebar-width-mobile);
    }

    .chat-messages-container {
        padding: var(--space-4);
    }

    .chat-input-container {
        padding: var(--space-4);
    }
}

/* Desktop adjustments */
@media (min-width: 1024px) {
    .chat-messages-container {
        padding: var(--space-6);
    }

    .chat-input-container {
        padding: var(--space-4) var(--space-6);
    }
}

/* ==========================================================================
   17. SIDEBAR RESPONSIVE SYSTEM
   Consistent sidebar behavior across all breakpoints
   ========================================================================== */

/* Sidebar base styles (desktop-first for sidebar) */
.chat-sidebar {
    width: var(--sidebar-width);
    flex-shrink: 0;
    display: flex;
    flex-direction: column;
    height: 100%;
    transition: transform var(--transition-base), width var(--transition-base);
}

/* Tablet: Narrower sidebar */
@media (min-width: 768px) and (max-width: 1023px) {
    .chat-sidebar {
        width: var(--sidebar-width-mobile);
    }

    /* Ensure proper spacing in sidebar items on tablet */
    .sidebar-header {
        padding: var(--space-4);
    }

    .nav-section-title {
        padding: 0 var(--space-4) var(--space-2);
        font-size: var(--font-size-sm);
    }

    .chat-item {
        padding: var(--space-3);
    }
}

/* Mobile: Sliding overlay sidebar */
@media (max-width: 767px) {
    .chat-sidebar {
        position: fixed;
        top: 0;
        left: 0;
        bottom: 0;
        width: var(--sidebar-width-mobile);
        z-index: var(--z-sidebar);
        transform: translateX(-100%);
        will-change: transform;
    }

    .chat-sidebar.is-open,
    .chat-sidebar.open {
        transform: translateX(0);
    }

    /* Backdrop when sidebar is open */
    .chat-sidebar-backdrop {
        position: fixed;
        inset: 0;
        background: rgba(0, 0, 0, 0.5);
        opacity: 0;
        visibility: hidden;
        z-index: calc(var(--z-sidebar) - 1);
        transition: opacity var(--transition-base), visibility var(--transition-base);
        backdrop-filter: blur(4px);
        -webkit-backdrop-filter: blur(4px);
    }

    .chat-sidebar-backdrop.is-visible,
    .chat-sidebar.is-open ~ .chat-sidebar-backdrop,
    .chat-sidebar.open ~ .chat-sidebar-backdrop {
        opacity: 1;
        visibility: visible;
    }

    /* Prevent body scroll when sidebar is open */
    body.sidebar-open {
        overflow: hidden;
        position: fixed;
        width: 100%;
    }
}

/* Mobile menu toggle button - TOP RIGHT */
.mobile-menu-toggle {
    display: none;
    position: fixed;
    top: max(var(--space-3), var(--safe-area-top));
    right: max(var(--space-3), var(--safe-area-right));
    left: auto;
    width: var(--touch-target-min);
    height: var(--touch-target-min);
    align-items: center;
    justify-content: center;
    background: rgba(21, 26, 53, 0.95);
    border: 1px solid rgba(0, 242, 254, 0.2);
    border-radius: var(--radius-lg);
    z-index: calc(var(--z-sidebar) + 1);
    cursor: pointer;
    color: #ffffff;
    font-size: 1.25rem;
    transition: all var(--transition-fast);
    -webkit-tap-highlight-color: transparent;
}

.mobile-menu-toggle:hover,
.mobile-menu-toggle:focus-visible {
    background: rgba(0, 242, 254, 0.1);
    border-color: rgba(0, 242, 254, 0.4);
    outline: none;
}

.mobile-menu-toggle:active {
    transform: scale(0.95);
}

@media (max-width: 767px) {
    .mobile-menu-toggle {
        display: flex;
    }
}

/* Sidebar close button (visible on mobile only) */
.sidebar-close-btn {
    display: none;
    position: absolute;
    top: var(--space-3);
    right: var(--space-3);
    width: var(--touch-target-min);
    height: var(--touch-target-min);
    align-items: center;
    justify-content: center;
    background: transparent;
    border: none;
    color: rgba(255, 255, 255, 0.7);
    font-size: 1.25rem;
    cursor: pointer;
    transition: color var(--transition-fast);
    -webkit-tap-highlight-color: transparent;
}

.sidebar-close-btn:hover,
.sidebar-close-btn:focus-visible {
    color: #ffffff;
    outline: none;
}

@media (max-width: 767px) {
    .sidebar-close-btn {
        display: flex;
    }
}

/* Sidebar content safe area padding on mobile */
@media (max-width: 767px) {
    .sidebar-header {
        padding-top: max(var(--space-4), calc(var(--safe-area-top) + var(--space-2)));
    }

    .sidebar-sessions,
    .sidebar-nav {
        padding-bottom: max(var(--space-4), var(--safe-area-bottom));
    }
}

/* ==========================================================================
   18. RESPONSIVE MODAL SYSTEM
   Mobile-first modal design with safe areas and proper sizing
   ========================================================================== */

/* Modal overlay - base styles */
.modal-overlay {
    position: fixed;
    inset: 0;
    background: rgba(0, 0, 0, 0.75);
    backdrop-filter: blur(8px);
    -webkit-backdrop-filter: blur(8px);
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: var(--z-modal);
    padding: var(--space-4);
    padding-top: max(var(--space-4), var(--safe-area-top));
    padding-bottom: max(var(--space-4), var(--safe-area-bottom));
    padding-left: max(var(--space-4), var(--safe-area-left));
    padding-right: max(var(--space-4), var(--safe-area-right));
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
}

/* Modal container - responsive sizing */
.modal-container {
    width: 100%;
    max-width: min(90vw, 600px);
    max-height: calc(100vh - var(--space-8) - var(--safe-area-top) - var(--safe-area-bottom));
    overflow: hidden;
    display: flex;
    flex-direction: column;
    border-radius: var(--radius-xl);
    animation: modalSlideUp 0.3s ease-out;
}

/* Tablet: slightly larger modals */
@media (min-width: 768px) {
    .modal-container {
        max-width: min(85vw, 700px);
    }
}

/* Desktop: standard max width */
@media (min-width: 1024px) {
    .modal-container {
        max-width: min(80vw, 800px);
    }
}

/* Large desktop: comfortable reading width */
@media (min-width: 1440px) {
    .modal-container {
        max-width: 900px;
    }
}

/* Modal header */
.modal-header {
    flex-shrink: 0;
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: var(--space-4);
    padding: var(--space-4) var(--space-5);
    border-bottom: 1px solid rgba(255, 255, 255, 0.1);
}

@media (min-width: 768px) {
    .modal-header {
        padding: var(--space-5) var(--space-6);
    }
}

/* Modal close button */
.modal-close {
    width: var(--touch-target-min);
    height: var(--touch-target-min);
    display: flex;
    align-items: center;
    justify-content: center;
    background: transparent;
    border: none;
    border-radius: var(--radius-lg);
    color: rgba(255, 255, 255, 0.7);
    font-size: 1.25rem;
    cursor: pointer;
    transition: all var(--transition-fast);
    -webkit-tap-highlight-color: transparent;
}

.modal-close:hover,
.modal-close:focus-visible {
    background: rgba(239, 68, 68, 0.15);
    color: #ef4444;
    outline: none;
}

/* Modal body */
.modal-body {
    flex: 1;
    overflow-y: auto;
    overflow-x: hidden;
    -webkit-overflow-scrolling: touch;
    overscroll-behavior: contain;
    padding: var(--space-4) var(--space-5);
}

@media (min-width: 768px) {
    .modal-body {
        padding: var(--space-5) var(--space-6);
    }
}

/* Modal footer */
.modal-footer {
    flex-shrink: 0;
    display: flex;
    align-items: center;
    justify-content: flex-end;
    gap: var(--space-3);
    padding: var(--space-4) var(--space-5);
    border-top: 1px solid rgba(255, 255, 255, 0.1);
}

@media (min-width: 768px) {
    .modal-footer {
        padding: var(--space-4) var(--space-6);
    }
}

/* Mobile: Stack buttons vertically */
@media (max-width: 479px) {
    .modal-footer {
        flex-direction: column;
    }

    .modal-footer .btn,
    .modal-footer button {
        width: 100%;
    }
}

/* Modal animation */
@keyframes modalSlideUp {
    from {
        opacity: 0;
        transform: translateY(20px) scale(0.98);
    }
    to {
        opacity: 1;
        transform: translateY(0) scale(1);
    }
}

/* Delete modal specific - smaller width */
.modal-container.delete-modal {
    max-width: min(90vw, 450px);
}

@media (min-width: 768px) {
    .modal-container.delete-modal {
        max-width: 450px;
    }
}

/* Full-screen modal for mobile (for complex dialogs) */
.modal-container.modal-fullscreen-mobile {
    max-width: 100%;
    max-height: 100%;
    border-radius: 0;
}

@media (max-width: 767px) {
    .modal-container.modal-fullscreen-mobile {
        width: 100%;
        height: 100%;
        max-width: 100vw;
        max-height: 100vh;
        border-radius: 0;
    }

    .modal-overlay:has(.modal-fullscreen-mobile) {
        padding: 0;
    }
}

/* Responsive form rows in modals */
.modal-body .form-row {
    display: grid;
    grid-template-columns: 1fr;
    gap: var(--space-4);
}

@media (min-width: 480px) {
    .modal-body .form-row {
        grid-template-columns: repeat(2, 1fr);
    }
}
