/* ================================================================
   MEGARISE CAPITAL — GLOBAL RESPONSIVE PATCHES
   Loaded on every page via head.php. Uses attribute selectors to
   collapse inline `style="grid-template-columns:..."` layouts on
   small screens, plus general safety rules and nav/sidebar polish.
================================================================ */

/* --------------------------------------------------------------
   0. GLOBAL SAFETY
-------------------------------------------------------------- */
html, body { overflow-x: hidden; max-width: 100vw; }
* { box-sizing: border-box; }
img, video, iframe { max-width: 100%; height: auto; }
table { max-width: 100%; }
.table-container, .data-table-wrap { overflow-x: auto; -webkit-overflow-scrolling: touch; }
pre, code { white-space: pre-wrap; word-break: break-word; }
input, select, textarea { max-width: 100%; }

/* Prevent inline-styled cards from breaking small screens */
.card { max-width: 100%; box-sizing: border-box; }

/* --------------------------------------------------------------
   1. TABLET (≤ 1024px)
-------------------------------------------------------------- */
@media (max-width: 1024px) {
    /* Public hero/content 2-column inline grids collapse */
    .hero .container[style*="grid-template-columns"],
    section[style*="grid-template-columns:1fr 1fr"] > .container,
    .container[style*="grid-template-columns:1fr 1fr"] {
        grid-template-columns: 1fr !important;
        gap: 32px !important;
    }

    /* 4-col stat grids → 2 col on tablet */
    div[style*="grid-template-columns:repeat(4,1fr)"],
    div[style*="grid-template-columns: repeat(4, 1fr)"],
    div[style*="grid-template-columns:1fr 1fr 1fr 1fr"] {
        grid-template-columns: 1fr 1fr !important;
    }

    /* Admin chat 320px + content → stack sidebar on top */
    div[style*="grid-template-columns:320px 1fr"] {
        grid-template-columns: 1fr !important;
    }

    /* Public nav hides primary links, shows hamburger (already in CSS) */
    .nav-links { display: none !important; }
}

/* --------------------------------------------------------------
   2. SMALL TABLET / LARGE PHONE (≤ 768px)
-------------------------------------------------------------- */
@media (max-width: 768px) {
    /* ----- Universal inline-grid collapse ----- */
    [style*="grid-template-columns:1fr 1fr"],
    [style*="grid-template-columns: 1fr 1fr"],
    [style*="grid-template-columns:1fr 1fr 1fr"],
    [style*="grid-template-columns: 1fr 1fr 1fr"],
    [style*="grid-template-columns:1fr 1fr 1fr 1fr"],
    [style*="grid-template-columns: 1fr 1fr 1fr 1fr"],
    [style*="grid-template-columns:repeat(3, 1fr)"],
    [style*="grid-template-columns:repeat(3,1fr)"],
    [style*="grid-template-columns:repeat(4, 1fr)"],
    [style*="grid-template-columns:repeat(4,1fr)"],
    [style*="grid-template-columns:2fr 1fr"],
    [style*="grid-template-columns:1fr 2fr"],
    [style*="grid-template-columns:5fr 7fr"],
    [style*="grid-template-columns:7fr 5fr"],
    [style*="grid-template-columns:1fr auto"],
    [style*="grid-template-columns:auto 1fr"],
    [style*="grid-template-columns:auto 1fr 1fr auto auto"],
    [style*="grid-template-columns:1fr 1fr 1fr auto"] {
        grid-template-columns: 1fr !important;
        gap: 16px !important;
    }

    /* Common class-based grids */
    .grid.grid-2,
    .grid.grid-3,
    .grid.grid-4 { grid-template-columns: 1fr !important; }
    .stat-cards { grid-template-columns: 1fr 1fr !important; gap: 12px !important; }

    /* ----- Public site hero tweaks ----- */
    .hero { padding: 48px 0 32px !important; }
    .hero-title { font-size: 2rem !important; line-height: 1.15 !important; }
    .hero-subtitle { font-size: 1rem !important; }
    .hero-actions { flex-direction: column !important; gap: 10px !important; align-items: stretch !important; }
    .hero-actions .btn { width: 100% !important; }
    .section { padding: 48px 0 !important; }
    .section-title { font-size: 1.75rem !important; }

    /* Hero stat row (inline flex with gap:48px) */
    div[style*="gap:48px"][style*="margin-top:48px"],
    .hero div[style*="display:flex"][style*="gap:48px"] {
        flex-wrap: wrap !important;
        gap: 20px 32px !important;
    }

    /* ----- Public nav ----- */
    .top-nav { padding: 12px 16px !important; gap: 8px !important; }
    .nav-brand { font-size: 0.95rem !important; }
    .nav-actions .btn:not(.nav-lang-btn):not(.mobile-menu-toggle) { padding: 6px 12px !important; font-size: 0.8rem !important; }

    /* ----- Dashboard / admin sidebar behavior ----- */
    /* Sidebar becomes a drawer; .open added by app.js */
    .sidebar {
        transform: translateX(-100%);
        transition: transform 0.25s ease;
        width: 280px !important;
        z-index: 1200;
    }
    .sidebar.open { transform: translateX(0); }
    .sidebar-overlay {
        display: none;
        position: fixed; inset: 0;
        background: rgba(0,0,0,0.45);
        z-index: 1100;
        backdrop-filter: blur(2px);
    }
    .sidebar.open + .sidebar-overlay,
    .sidebar-overlay.active { display: block; }
    /* RTL-friendly drawer */
    html[dir="rtl"] .sidebar { transform: translateX(100%); }
    html[dir="rtl"] .sidebar.open { transform: translateX(0); }

    .dash-content,
    .admin-content {
        margin-left: 0 !important;
        padding-top: 72px !important;
    }
    .dash-content-inner,
    .admin-content-inner { padding: 16px !important; gap: 20px !important; }

    .admin-header,
    .dash-header {
        left: 0 !important;
        padding: 10px 16px !important;
    }
    /* Show hamburger */
    .dash-header-mobile-toggle { display: inline-flex !important; }
    /* Shrink admin search */
    .admin-header input[type="text"] { font-size: 0.8rem !important; padding: 8px 12px 8px 36px !important; }
    .admin-header div[style*="max-width:320px"] { max-width: 180px !important; }
    .admin-header p[style*="0.8125rem"],
    .admin-header p[style*="0.6875rem"] { display: none; }

    /* ----- Dashboard specific ----- */
    .portfolio-card { padding: 24px !important; }
    .portfolio-card-value { font-size: 1.75rem !important; word-break: break-all; }
    .portfolio-card-actions { flex-wrap: wrap; gap: 8px; }
    .portfolio-card-actions .btn { flex: 1; min-width: 120px; }
    .stat-card { padding: 16px !important; }
    .stat-card-value.sm { font-size: 1.1rem !important; }
    .trade-buttons { grid-template-columns: 1fr 1fr !important; gap: 8px; }
    .media-panel, .trade-panel { padding: 20px !important; }
    .market-ticker-label { padding: 8px 12px; font-size: 0.65rem; }
    .market-ticker-track { animation-duration: 25s; gap: 20px; }

    /* ----- Forms & misc ----- */
    .form-group { margin-bottom: 16px; }
    .btn-lg { padding: 12px 20px !important; font-size: 0.95rem !important; }

    /* Chat widget (floating) out of the way on mobile */
    .chat-widget { right: 12px !important; bottom: 12px !important; }
    .chat-popup { width: calc(100vw - 24px) !important; max-width: 340px; }

    /* Plan cards padding */
    .plan-card { padding: 24px !important; }
    .plan-price { font-size: 2rem !important; }

    /* Testimonial grid single column */
    .testimonials-grid { grid-template-columns: 1fr !important; gap: 18px !important; }
    .testimonial-card { padding: 24px !important; }

    /* Tesla plans full width */
    .tesla-plans-grid { grid-template-columns: 1fr !important; gap: 18px !important; max-width: 420px !important; margin: 0 auto !important; }
    .tesla-plan-card.is-featured { transform: none !important; }

    /* Data tables — let them scroll horizontally */
    .data-table { font-size: 0.8rem; min-width: 520px; }

    /* CTA sections */
    .cta-section { padding: 40px 24px !important; }

    /* Hero image stat badge scale down */
    .hero-image-stat {
        padding: 10px 14px !important;
        max-width: 200px !important;
    }
    .hero-image-stat div[style*="2rem"],
    .hero-image-stat div[style*="1.5rem"] { font-size: 1.1rem !important; }
}

/* --------------------------------------------------------------
   3. SMALL PHONE (≤ 480px)
-------------------------------------------------------------- */
@media (max-width: 480px) {
    .hero-title { font-size: 1.65rem !important; }
    .hero-subtitle { font-size: 0.9rem !important; }
    .section-title { font-size: 1.4rem !important; }
    .stat-cards { grid-template-columns: 1fr !important; }
    .trade-buttons { grid-template-columns: 1fr !important; }
    .dash-content-inner, .admin-content-inner { padding: 12px !important; }
    .portfolio-card-value { font-size: 1.5rem !important; }
    .tesla-plan-rate-num { font-size: 2.5rem !important; }
    .tesla-plan-card { padding: 28px 22px !important; }
    .plan-price { font-size: 1.75rem !important; }
    .card { padding: 20px !important; }

    /* Admin header compress */
    .admin-header div[style*="max-width:320px"],
    .admin-header > div:first-child > div:nth-child(2) { display: none !important; }
    .admin-header > div:last-child { gap: 10px !important; }
    .admin-header img { width: 32px !important; height: 32px !important; }

    /* Buttons full-width by default inside cards */
    .card > form > .btn,
    .form-group + .btn { width: 100%; }

    /* Chat bubbles take more width */
    .chat-popup { width: calc(100vw - 16px) !important; }

    /* Reduce heavy horizontal paddings */
    .container { padding-left: 16px !important; padding-right: 16px !important; }
    .hero .container { padding-left: 16px !important; padding-right: 16px !important; }
}

/* --------------------------------------------------------------
   4. LARGE DESKTOP (≥ 1600px) — prevent over-stretched layouts
-------------------------------------------------------------- */
@media (min-width: 1600px) {
    .container { max-width: 1400px; margin-left: auto; margin-right: auto; }
}

/* --------------------------------------------------------------
   5. TOUCH-FRIENDLY TAP TARGETS
-------------------------------------------------------------- */
@media (hover: none) and (pointer: coarse) {
    .btn, .sidebar-link, .nav-links a, .pagination-link {
        min-height: 40px;
    }
    .faq-question { min-height: 48px; }
}

/* --------------------------------------------------------------
   6. REDUCE MOTION
-------------------------------------------------------------- */
@media (prefers-reduced-motion: reduce) {
    .market-ticker-track { animation: none !important; }
    * { transition-duration: 0.01ms !important; animation-duration: 0.01ms !important; }
}

