/**
 * TradeBrain - Shared Responsive Styles
 * Mobile-first approach with standardized breakpoints
 *
 * Breakpoint System:
 * - xs: 0-479px (iPhone SE, Galaxy S21)
 * - sm: 480-767px (iPhone 14, Pixel 7)
 * - md: 768-1023px (iPad Mini, tablets)
 * - lg: 1024-1279px (iPad Pro, laptops)
 * - xl: 1280px+ (Desktop monitors)
 */

/* ============================================
   CSS CUSTOM PROPERTIES (shared variables)
   ============================================ */
:root {
    /* Spacing scale */
    --spacing-xs: 0.25rem;
    --spacing-sm: 0.5rem;
    --spacing-md: 1rem;
    --spacing-lg: 1.5rem;
    --spacing-xl: 2rem;

    /* Container max widths */
    --container-sm: 540px;
    --container-md: 720px;
    --container-lg: 960px;
    --container-xl: 1140px;
    --container-xxl: 1400px;

    /* Touch target minimum (accessibility) */
    --touch-target-min: 44px;

    /* Safe area insets for notched devices */
    --safe-area-top: env(safe-area-inset-top, 0);
    --safe-area-right: env(safe-area-inset-right, 0);
    --safe-area-bottom: env(safe-area-inset-bottom, 0);
    --safe-area-left: env(safe-area-inset-left, 0);
}

/* ============================================
   BASE RESPONSIVE UTILITIES
   ============================================ */

/* Prevent iOS zoom on input focus - must be 16px+ */
input, select, textarea {
    font-size: 16px !important;
}

@media (min-width: 768px) {
    input, select, textarea {
        font-size: inherit !important;
    }
}

/* Fluid typography using clamp */
.text-fluid-sm {
    font-size: clamp(0.75rem, 2vw, 0.875rem);
}

.text-fluid-base {
    font-size: clamp(0.875rem, 2.5vw, 1rem);
}

.text-fluid-lg {
    font-size: clamp(1rem, 3vw, 1.25rem);
}

.text-fluid-xl {
    font-size: clamp(1.25rem, 4vw, 1.5rem);
}

.text-fluid-2xl {
    font-size: clamp(1.5rem, 5vw, 2rem);
}

/* ============================================
   VISIBILITY UTILITIES
   ============================================ */

/* Hide on specific breakpoints */
.hide-xs {
    display: none !important;
}

@media (min-width: 480px) {
    .hide-xs {
        display: initial !important;
    }
    .hide-sm {
        display: none !important;
    }
}

@media (min-width: 768px) {
    .hide-sm {
        display: initial !important;
    }
    .hide-md {
        display: none !important;
    }
}

@media (min-width: 1024px) {
    .hide-md {
        display: initial !important;
    }
    .hide-lg {
        display: none !important;
    }
}

@media (min-width: 1280px) {
    .hide-lg {
        display: initial !important;
    }
}

/* Show only on specific breakpoints */
.show-xs-only {
    display: initial !important;
}

.show-sm-only,
.show-md-only,
.show-lg-only {
    display: none !important;
}

@media (min-width: 480px) {
    .show-xs-only {
        display: none !important;
    }
    .show-sm-only {
        display: initial !important;
    }
}

@media (min-width: 768px) {
    .show-sm-only {
        display: none !important;
    }
    .show-md-only {
        display: initial !important;
    }
}

@media (min-width: 1024px) {
    .show-md-only {
        display: none !important;
    }
    .show-lg-only {
        display: initial !important;
    }
}

@media (min-width: 1280px) {
    .show-lg-only {
        display: none !important;
    }
}

/* ============================================
   LAYOUT UTILITIES
   ============================================ */

/* Responsive container */
.container-responsive {
    width: 100%;
    padding-left: var(--spacing-md);
    padding-right: var(--spacing-md);
    margin-left: auto;
    margin-right: auto;
}

@media (min-width: 480px) {
    .container-responsive {
        padding-left: var(--spacing-lg);
        padding-right: var(--spacing-lg);
    }
}

@media (min-width: 768px) {
    .container-responsive {
        max-width: var(--container-md);
        padding-left: var(--spacing-xl);
        padding-right: var(--spacing-xl);
    }
}

@media (min-width: 1024px) {
    .container-responsive {
        max-width: var(--container-lg);
    }
}

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

/* Responsive grid */
.grid-responsive {
    display: grid;
    gap: var(--spacing-md);
    grid-template-columns: 1fr;
}

@media (min-width: 480px) {
    .grid-responsive.grid-2-sm {
        grid-template-columns: repeat(2, 1fr);
    }
}

@media (min-width: 768px) {
    .grid-responsive.grid-2-md {
        grid-template-columns: repeat(2, 1fr);
    }
    .grid-responsive.grid-3-md {
        grid-template-columns: repeat(3, 1fr);
    }
}

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

/* Responsive flex */
.flex-responsive {
    display: flex;
    flex-direction: column;
    gap: var(--spacing-md);
}

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

/* Stack to row responsive */
.stack-to-row {
    display: flex;
    flex-direction: column;
    gap: var(--spacing-md);
}

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

/* ============================================
   SPACING UTILITIES
   ============================================ */

/* Responsive padding */
.p-responsive {
    padding: var(--spacing-md);
}

@media (min-width: 480px) {
    .p-responsive {
        padding: var(--spacing-lg);
    }
}

@media (min-width: 768px) {
    .p-responsive {
        padding: var(--spacing-xl);
    }
}

/* Responsive margin */
.m-responsive {
    margin: var(--spacing-md);
}

@media (min-width: 480px) {
    .m-responsive {
        margin: var(--spacing-lg);
    }
}

@media (min-width: 768px) {
    .m-responsive {
        margin: var(--spacing-xl);
    }
}

/* ============================================
   COMPONENT RESPONSIVE OVERRIDES
   ============================================ */

/* Responsive modal */
.modal-responsive .modal-content {
    width: calc(100% - 1rem);
    max-width: 500px;
    margin: 0.5rem;
    max-height: calc(100vh - 1rem);
    overflow-y: auto;
}

@media (min-width: 480px) {
    .modal-responsive .modal-content {
        width: calc(100% - 2rem);
        margin: 1rem;
        max-height: calc(100vh - 2rem);
    }
}

@media (min-width: 768px) {
    .modal-responsive .modal-content {
        width: 90%;
        margin: auto;
        max-height: 90vh;
    }
}

/* Responsive table wrapper */
.table-responsive {
    width: 100%;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
}

/* Table scroll hint for mobile */
.table-responsive::after {
    content: '';
    position: absolute;
    right: 0;
    top: 0;
    bottom: 0;
    width: 30px;
    background: linear-gradient(to right, transparent, rgba(0, 0, 0, 0.1));
    pointer-events: none;
    opacity: 0;
    transition: opacity 0.3s;
}

.table-responsive.has-scroll::after {
    opacity: 1;
}

/* Card layout for tables on mobile */
.table-as-cards {
    display: block;
}

@media (max-width: 767px) {
    .table-as-cards thead {
        display: none;
    }

    .table-as-cards tbody {
        display: flex;
        flex-direction: column;
        gap: var(--spacing-md);
    }

    .table-as-cards tr {
        display: flex;
        flex-direction: column;
        padding: var(--spacing-md);
        background: var(--card-bg, rgba(255, 255, 255, 0.08));
        border-radius: 12px;
        border: 1px solid var(--border-color, rgba(255, 255, 255, 0.1));
    }

    .table-as-cards td {
        display: flex;
        justify-content: space-between;
        padding: var(--spacing-sm) 0;
        border-bottom: 1px solid var(--border-color, rgba(255, 255, 255, 0.1));
    }

    .table-as-cards td:last-child {
        border-bottom: none;
    }

    .table-as-cards td::before {
        content: attr(data-label);
        font-weight: 600;
        color: var(--text-secondary, #a5b4fc);
    }
}

@media (min-width: 768px) {
    .table-as-cards {
        display: table;
    }
}

/* Responsive form rows */
.form-row-responsive {
    display: grid;
    grid-template-columns: 1fr;
    gap: var(--spacing-md);
}

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

/* ============================================
   NAVIGATION RESPONSIVE
   ============================================ */

/* Mobile menu button */
.mobile-menu-btn {
    display: flex;
    align-items: center;
    justify-content: center;
    width: var(--touch-target-min);
    height: var(--touch-target-min);
    background: transparent;
    border: 1px solid var(--border-color, rgba(255, 255, 255, 0.1));
    border-radius: 8px;
    cursor: pointer;
    color: var(--text-secondary, #a5b4fc);
    transition: all 0.3s ease;
}

.mobile-menu-btn:hover {
    background: rgba(255, 255, 255, 0.1);
    color: var(--text-primary, #f1f5f9);
}

/* Hamburger icon */
.hamburger {
    width: 20px;
    height: 14px;
    display: flex;
    flex-direction: column;
    justify-content: space-between;
}

.hamburger span {
    display: block;
    width: 100%;
    height: 2px;
    background: currentColor;
    border-radius: 1px;
    transition: all 0.3s ease;
}

.hamburger.active span:nth-child(1) {
    transform: rotate(45deg) translate(4px, 4px);
}

.hamburger.active span:nth-child(2) {
    opacity: 0;
}

.hamburger.active span:nth-child(3) {
    transform: rotate(-45deg) translate(4px, -4px);
}

/* Mobile nav overlay */
.mobile-nav-overlay {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: rgba(0, 0, 0, 0.7);
    backdrop-filter: blur(4px);
    z-index: 998;
    opacity: 0;
    visibility: hidden;
    transition: all 0.3s ease;
}

.mobile-nav-overlay.active {
    opacity: 1;
    visibility: visible;
}

/* Mobile nav menu */
.mobile-nav-menu {
    position: fixed;
    top: 0;
    right: -280px;
    width: 280px;
    height: 100vh;
    background: var(--card-solid, #2d2a5e);
    z-index: 999;
    padding: var(--spacing-xl);
    transition: right 0.3s ease;
    overflow-y: auto;
}

.mobile-nav-menu.active {
    right: 0;
}

.mobile-nav-menu .nav-links {
    display: flex;
    flex-direction: column;
    gap: var(--spacing-sm);
}

.mobile-nav-menu .nav-links a {
    padding: var(--spacing-md);
    border-radius: 8px;
}

/* Show/hide based on screen size */
.desktop-only-nav {
    display: none;
}

.mobile-only-nav {
    display: flex;
}

@media (min-width: 768px) {
    .desktop-only-nav {
        display: flex;
    }
    .mobile-only-nav {
        display: none;
    }
}

/* ============================================
   BUTTON & TOUCH TARGET ACCESSIBILITY
   ============================================ */

/* Ensure minimum touch targets */
.btn-touch,
button,
[role="button"],
.clickable {
    min-height: var(--touch-target-min);
}

/* Larger tap area for small buttons */
.btn-sm {
    position: relative;
}

.btn-sm::before {
    content: '';
    position: absolute;
    top: -6px;
    right: -6px;
    bottom: -6px;
    left: -6px;
}

/* ============================================
   LOADING STATES RESPONSIVE
   ============================================ */

.loader-responsive {
    width: clamp(60px, 15vw, 100px);
    height: clamp(60px, 15vw, 100px);
}

/* ============================================
   FOOTER RESPONSIVE
   ============================================ */

.footer-responsive {
    flex-direction: column;
    text-align: center;
    gap: var(--spacing-md);
}

@media (min-width: 768px) {
    .footer-responsive {
        flex-direction: row;
        text-align: left;
        justify-content: space-between;
    }
}

.footer-links-responsive {
    display: flex;
    flex-wrap: wrap;
    justify-content: center;
    gap: var(--spacing-md);
}

@media (min-width: 768px) {
    .footer-links-responsive {
        justify-content: flex-end;
        gap: var(--spacing-lg);
    }
}

/* ============================================
   SAFE AREA PADDING (for notched devices)
   ============================================ */

.safe-padding-top {
    padding-top: max(var(--spacing-md), var(--safe-area-top));
}

.safe-padding-bottom {
    padding-bottom: max(var(--spacing-md), var(--safe-area-bottom));
}

.safe-padding-x {
    padding-left: max(var(--spacing-md), var(--safe-area-left));
    padding-right: max(var(--spacing-md), var(--safe-area-right));
}

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

@media print {
    .header,
    .footer,
    .btn,
    .modal,
    .mobile-menu-btn,
    .mobile-nav-menu,
    .mobile-nav-overlay,
    .no-print {
        display: none !important;
    }

    .card {
        break-inside: avoid;
        box-shadow: none;
        border: 1px solid #ddd;
    }

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

    a {
        color: black !important;
        text-decoration: underline;
    }

    .print-only {
        display: block !important;
    }
}

.print-only {
    display: none;
}

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

/* 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;
    }
}

/* Focus visible styles */
:focus-visible {
    outline: 2px solid var(--primary, #818cf8);
    outline-offset: 2px;
}

/* Skip to content link */
.skip-link {
    position: absolute;
    top: -100%;
    left: 0;
    background: var(--primary, #818cf8);
    color: white;
    padding: var(--spacing-md) var(--spacing-lg);
    z-index: 10000;
    transition: top 0.3s;
}

.skip-link:focus {
    top: 0;
}

/* ============================================
   Brain 3 — Country Group Card Grid (Responsive)
   ============================================ */
.brain3-country-grid {
    display: grid;
    grid-template-columns: repeat(4, 1fr);
    gap: 0.2rem 0.5rem;
    margin-bottom: 0.75rem;
    font-size: 0.72rem;
    color: #334155;
}

@media (max-width: 1023px) {
    .brain3-country-grid {
        grid-template-columns: repeat(2, 1fr);
    }
}

@media (max-width: 479px) {
    .brain3-country-grid {
        grid-template-columns: 1fr;
    }
}
