/**
 * Responsive Design System
 * Mobile-specific overrides for touch targets, layout, and accessibility.
 *
 * Breakpoints (matches Tailwind):
 * - sm: 640px
 * - md: 768px
 * - lg: 1024px
 *
 * IMPORTANT: Do NOT add global (un-media-queried) size/padding/font rules here.
 * The design system (design-tokens.css + components.css) controls base sizing.
 * This file only overrides for specific viewport/device contexts.
 */

/* ============================================================================
   ROOT VARIABLES (responsive spacing)
   ============================================================================ */

:root {
    --touch-target-min: 44px;
    --mobile-gutter: 16px;
    --tablet-gutter: 20px;
    --desktop-gutter: 24px;
}

/* ============================================================================
   MOBILE (< 640px)
   ============================================================================ */

@media (max-width: 639px) {
    html, body {
        max-width: 100vw;
        overflow-x: hidden;
    }

    main {
        padding: 12px !important;
        max-width: 100%;
    }

    body {
        font-size: 16px;
        line-height: 1.5;
    }

    /* Touch-friendly controls — iOS zoom prevention + fat-finger targets */
    input,
    select,
    textarea {
        font-size: 16px;
        min-height: var(--touch-target-min);
        padding: 10px 12px;
    }

    button {
        font-size: 16px;
    }

    button:not([role="switch"]):not(.btn-icon):not(.va-mode-btn):not(.va-expand-btn):not(.va-expand-chat):not(.va-expand-voice):not(.va-feedback-btn):not(.va-header-btn):not(.chatwidget-feedback-btn),
    a[role="button"] {
        min-height: var(--touch-target-min);
        padding: 8px 12px;
    }

    /* Navigation links — touch targets */
    nav a,
    nav button {
        min-height: var(--touch-target-min);
        padding: 10px 12px;
    }

    /* Focus ring */
    input:focus,
    button:focus,
    a:focus,
    select:focus,
    textarea:focus {
        outline: 2px solid var(--color-primary, #8b5cf6);
        outline-offset: 2px;
    }

    /* Grid + spacing */
    .grid {
        gap: 12px !important;
    }

    .gap-3 { gap: 8px !important; }
    .gap-4 { gap: 12px !important; }
    .gap-6 { gap: 12px !important; }

    /* Card padding */
    [class*="p-6"],
    [class*="p-8"] {
        padding: 12px !important;
    }

    /* Modals — bottom sheet on mobile */
    [role="dialog"] {
        position: fixed;
        bottom: 0;
        left: 0;
        right: 0;
        width: 100%;
        border-radius: 16px 16px 0 0;
        max-height: 90vh;
        overflow-y: auto;
        -webkit-overflow-scrolling: touch;
    }

    /* Horizontal scroll for tables */
    .overflow-x-auto {
        -webkit-overflow-scrolling: touch;
        max-width: 100%;
        width: 100%;
    }

    table {
        font-size: 14px;
        max-width: 100%;
        table-layout: fixed;
    }

    table th, table td {
        padding: 10px 6px !important;
        word-wrap: break-word;
        overflow-wrap: break-word;
    }

    /* Shadows — simpler on mobile */
    .shadow-lg,
    .shadow-xl {
        box-shadow: 0 1px 3px rgba(0, 0, 0, 0.1);
    }

    /* Prevent content from being hidden behind widget orb */
    #main-content {
        padding-bottom: 80px !important;
    }

    /* Sub-navigation: horizontal scroll with fade hint */
    .subnav-mobile-wrap {
        overflow-x: auto;
        -webkit-overflow-scrolling: touch;
        scrollbar-width: none;
        -ms-overflow-style: none;
        padding-bottom: 2px;
    }
    .subnav-mobile-wrap::-webkit-scrollbar { display: none; }

    /* Tables — card-based stacking on mobile */
    .analytics-table-wrap {
        overflow-x: auto;
        -webkit-overflow-scrolling: touch;
        margin: 0 -12px;
        padding: 0 12px;
    }

    /* Prevent horizontal overflow on all containers */
    .card, .card-body, .editor-section {
        overflow-x: hidden;
        max-width: 100%;
    }

    /* Cookie consent — full width on mobile */
    #cookie-consent, [x-data="cookieConsent()"] > div {
        margin: 0 !important;
        max-width: 100% !important;
    }

    /* Notifications dropdown — full width minus gutters */
    [role="menu"].absolute {
        max-width: calc(100vw - 2rem);
    }

    /* Fix chart container overflow */
    canvas {
        max-width: 100% !important;
    }

    /* Breadcrumb — smaller on mobile */
    .breadcrumb-list {
        font-size: 12px;
    }

    /* Toast container — bottom center on mobile */
    #toast-container {
        right: 12px !important;
        left: 12px !important;
        bottom: calc(12px + env(safe-area-inset-bottom, 0px)) !important;
    }

    /* Editor toast — constrain width */
    .editor-toast {
        max-width: calc(100vw - 24px);
        left: 12px !important;
        right: 12px !important;
        transform: none !important;
    }

    /* Header — tighter padding */
    header .flex.items-center {
        gap: 4px;
    }

    /* Prevent wide elements from causing horizontal scroll */
    pre, code {
        overflow-x: auto;
        word-break: break-all;
    }
}

/* ============================================================================
   SMALL TABLET (640px – 767px)
   ============================================================================ */

@media (min-width: 640px) and (max-width: 767px) {
    main {
        padding: 16px !important;
    }

    .grid {
        gap: 16px !important;
    }

    .grid-cols-1,
    .sm\:grid-cols-2 {
        grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
    }

    .lg\:grid-cols-3,
    .lg\:grid-cols-4 {
        grid-template-columns: repeat(2, 1fr);
    }
}

/* ============================================================================
   TABLET (768px – 1023px)
   ============================================================================ */

@media (min-width: 768px) and (max-width: 1023px) {
    main {
        padding: 20px !important;
    }

    .grid {
        gap: 18px !important;
    }

    .md\:grid-cols-2 {
        grid-template-columns: repeat(2, 1fr);
    }

    .lg\:grid-cols-3 {
        grid-template-columns: repeat(2, 1fr);
    }

    .lg\:grid-cols-4 {
        grid-template-columns: repeat(2, 1fr);
    }

    .lg\:col-span-2 {
        grid-column: span 2;
    }
}

/* ============================================================================
   SUB-DESKTOP TOUCH TARGETS (< 1024px)
   ============================================================================ */

@media (max-width: 1023px) {
    /* Tables — compact on tablets */
    table {
        font-size: 14px;
    }

    table th {
        padding: 12px 8px !important;
        font-size: 12px;
    }

    table td {
        padding: 12px 8px !important;
        font-size: 13px;
    }

    /* Sidebar */
    aside {
        width: 100%;
        max-width: 280px;
    }
}

/* ============================================================================
   DESKTOP (1024px+)
   ============================================================================ */

@media (min-width: 1024px) {
    main {
        padding: 24px 32px !important;
    }

    .grid {
        gap: 24px !important;
    }

    .lg\:grid-cols-3 {
        grid-template-columns: repeat(3, 1fr);
    }

    .lg\:grid-cols-4 {
        grid-template-columns: repeat(4, 1fr);
    }

    .lg\:col-span-2 {
        grid-column: span 2;
    }
}

/* ============================================================================
   RESPONSIVE MODALS (< 768px)
   ============================================================================ */

@media (max-width: 767px) {
    [role="dialog"],
    .fixed.inset-0 {
        position: fixed;
        bottom: 0;
        left: 0;
        right: 0;
        width: 100vw;
        max-height: 90vh;
        border-radius: 20px 20px 0 0;
        overflow-y: auto;
        -webkit-overflow-scrolling: touch;
    }

    [role="dialog"] {
        top: auto !important;
    }

    .hidden.md\:block,
    .hidden.md\:flex {
        display: none !important;
    }
}

@media (min-width: 768px) {
    .md\:hidden {
        display: none !important;
    }
}

/* ============================================================================
   CARD PADDING (640px – 1023px)
   ============================================================================ */

@media (min-width: 640px) and (max-width: 1023px) {
    [class*="p-8"] {
        padding: 16px !important;
    }
}

/* ============================================================================
   RESPONSIVE IMAGES
   ============================================================================ */

img,
video {
    max-width: 100%;
    height: auto;
}

/* Avatar sizing — only for explicitly marked avatars */
.avatar.rounded-full,
[data-avatar].rounded-full {
    width: clamp(32px, 8vw, 64px);
    height: clamp(32px, 8vw, 64px);
}

/* ============================================================================
   ACCESSIBILITY
   ============================================================================ */

:focus-visible {
    outline: 2px solid var(--color-primary);
    outline-offset: 2px;
    border-radius: 4px;
}

button:focus-visible,
a:focus-visible,
input:focus-visible {
    outline-offset: 3px;
}

@media (prefers-contrast: more) {
    button,
    a {
        border: 1px solid currentColor;
    }

    input,
    select,
    textarea {
        border: 2px solid currentColor;
    }
}

@media (prefers-reduced-motion: reduce) {
    * {
        animation-duration: 0.01ms !important;
        animation-iteration-count: 1 !important;
        transition-duration: 0.01ms !important;
        scroll-behavior: auto !important;
    }
}

/* ============================================================================
   PRINT
   ============================================================================ */

@media print {
    main {
        padding: 0;
    }

    button,
    [role="button"],
    nav,
    aside,
    header {
        display: none !important;
    }
}

/* ============================================================================
   DEVICE-SPECIFIC & SAFARI FIXES
   ============================================================================ */

@supports (padding: max(0px)) {
    body {
        padding-left: env(safe-area-inset-left, 0px);
        padding-right: env(safe-area-inset-right, 0px);
        padding-bottom: env(safe-area-inset-bottom, 0px);
        box-sizing: border-box;
        max-width: 100vw;
    }
}

/* Safari: fix sticky positioning in scrollable containers */
@supports (-webkit-touch-callout: none) {
    .sticky {
        position: -webkit-sticky;
        position: sticky;
    }

    /* Safari: prevent rubber-band scroll showing background */
    body {
        overscroll-behavior-y: none;
    }

    /* Safari: fix 100vh being too tall (address bar not accounted) */
    .min-h-screen {
        min-height: -webkit-fill-available;
    }

    /* Safari: fix backdrop-filter jank */
    [style*="backdrop-filter"],
    [style*="-webkit-backdrop-filter"] {
        -webkit-transform: translateZ(0);
        transform: translateZ(0);
    }

    /* Safari: prevent text size adjust on orientation change */
    html {
        -webkit-text-size-adjust: 100%;
    }
}

/* iOS: prevent double-tap zoom on interactive elements */
@media (hover: none) and (pointer: coarse) {
    button, a, input, select, textarea, [role="button"] {
        touch-action: manipulation;
    }

    /* Larger tap targets on touch devices */
    .btn-icon, .editor-action-btn {
        min-width: 36px;
        min-height: 36px;
    }
}

@media (orientation: landscape) and (max-height: 500px) {
    main {
        padding-top: 8px !important;
        padding-bottom: 8px !important;
    }

    h1, h2 {
        margin-bottom: 8px;
    }
}

/* ============================================================================
   UTILITY CLASSES
   ============================================================================ */

.touch-target {
    min-height: 48px;
    min-width: 48px;
}

.responsive-container {
    width: 100%;
    margin-left: auto;
    margin-right: auto;
    padding-left: var(--mobile-gutter);
    padding-right: var(--mobile-gutter);
}

@media (min-width: 640px) {
    .responsive-container {
        padding-left: var(--tablet-gutter);
        padding-right: var(--tablet-gutter);
    }
}

@media (min-width: 1024px) {
    .responsive-container {
        max-width: 1280px;
        padding-left: var(--desktop-gutter);
        padding-right: var(--desktop-gutter);
    }
}

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

@media (min-width: 768px) {
    .responsive-flex {
        flex-direction: row;
        gap: 16px;
    }
}
