/**
 * MiraiSignal Responsive Design System
 * Mobile-first breakpoints, card grids, touch targets, swipe gestures.
 * Import AFTER main.css and theme-system.css.
 */

/* ===== Breakpoint Tokens (reference) =====
 * xs:  0–479px     (small phone)
 * sm:  480–767px   (large phone)
 * md:  768–1023px  (tablet)
 * lg:  1024–1439px (laptop)
 * xl:  1440–1919px (desktop)
 * 2xl: 1920+       (wide)
 * ===== */

/* ===== Base: Mobile First (xs) ===== */

.container {
    width: 100%;
    padding-left: var(--space-3, 12px);
    padding-right: var(--space-3, 12px);
    margin: 0 auto;
}

/* Card grid — single column by default */
.card-grid {
    display: grid;
    grid-template-columns: 1fr;
    gap: var(--space-3, 12px);
}

/* ===== Touch-Friendly Targets ===== */

/* Ensure minimum 44px tap target on interactive elements */
.btn,
button,
a.btn,
select,
input[type="checkbox"],
input[type="radio"],
.form-control,
.tab-btn,
.badge[role="button"] {
    min-height: 44px;
    min-width: 44px;
}

/* Increase touch area for small buttons without changing visual size */
.btn-sm {
    min-height: 44px;
    min-width: 44px;
    padding: 10px 14px;
}

/* Larger tap zone on table rows on touch devices */
@media (pointer: coarse) {
    .table td,
    .table th {
        padding: 14px 12px;
    }

    /* Increase spacing between interactive items */
    .btn + .btn,
    .badge + .badge {
        margin-left: 8px;
    }
}

/* ===== Small Phone (xs: < 480px) ===== */

@media (max-width: 479px) {
    html {
        font-size: 13px;
    }

    .container {
        padding-left: var(--space-2, 8px);
        padding-right: var(--space-2, 8px);
    }

    /* Stack everything vertically */
    .flex-between {
        flex-direction: column;
        align-items: flex-start;
        gap: var(--space-2, 8px);
    }

    /* Full-width buttons on small screens */
    .btn {
        width: 100%;
        justify-content: center;
    }

    .btn-sm {
        width: auto;
    }

    /* Cards: reduce padding */
    .card {
        padding: var(--space-2, 8px);
    }

    .card-header {
        flex-direction: column;
        align-items: flex-start;
        gap: var(--space-1, 4px);
    }

    /* Price display — smaller on tiny screens */
    .price-lg { font-size: 22px; }
    .price-md { font-size: 16px; }

    /* Table: horizontal scroll */
    .table-responsive {
        overflow-x: auto;
        -webkit-overflow-scrolling: touch;
        margin: 0 calc(-1 * var(--space-2, 8px));
        padding: 0 var(--space-2, 8px);
    }

    .table {
        min-width: 600px;
    }

    /* Hide non-essential columns */
    .hide-xs {
        display: none !important;
    }

    /* Notification: full-width */
    .notification,
    .toast-container {
        left: var(--space-2, 8px) !important;
        right: var(--space-2, 8px) !important;
        max-width: none !important;
    }
}

/* ===== Large Phone (sm: 480-767px) ===== */

@media (min-width: 480px) {
    .container {
        padding-left: var(--space-4, 16px);
        padding-right: var(--space-4, 16px);
    }

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

    .hide-sm-up {
        display: none !important;
    }
}

/* ===== Tablet (md: 768-1023px) ===== */

@media (min-width: 768px) {
    html {
        font-size: 14px;
    }

    .container {
        padding-left: var(--space-6, 24px);
        padding-right: var(--space-6, 24px);
    }

    .card-grid {
        grid-template-columns: repeat(2, 1fr);
        gap: var(--space-4, 16px);
    }

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

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

    .hide-md-up {
        display: none !important;
    }

    .show-md-up {
        display: block !important;
    }
}

/* ===== Laptop (lg: 1024-1439px) ===== */

@media (min-width: 1024px) {
    .container {
        padding-left: var(--space-8, 32px);
        padding-right: var(--space-8, 32px);
    }

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

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

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

    .hide-lg-up {
        display: none !important;
    }
}

/* ===== Desktop (xl: 1440+) ===== */

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

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

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

/* ===== Wide (2xl: 1920+) ===== */

@media (min-width: 1920px) {
    html {
        font-size: 15px;
    }

    .container {
        max-width: 1800px;
        padding-left: var(--space-10, 40px);
        padding-right: var(--space-10, 40px);
    }
}

/* ===== Ultra-Wide (2560+) ===== */

@media (min-width: 2560px) {
    html {
        font-size: 16px;
    }

    .container {
        max-width: 2200px;
    }
}

/* ===== Mobile Bottom Nav ===== */

.mobile-bottom-nav {
    display: none;
}

@media (max-width: 767px) {
    .mobile-bottom-nav {
        display: flex !important;
        position: fixed;
        bottom: 0;
        left: 0;
        right: 0;
        height: 56px;
        background: var(--bg-secondary, #1a1a2e);
        border-top: 1px solid var(--border-color, #333);
        z-index: 1000;
        padding-bottom: env(safe-area-inset-bottom, 0px);
    }
    .mobile-bottom-nav a {
        flex: 1;
        display: flex;
        align-items: center;
        justify-content: center;
        color: var(--text-secondary, #888);
        text-decoration: none;
        font-size: 11px;
        font-weight: 600;
        letter-spacing: 0.5px;
    }
    .mobile-bottom-nav a.active {
        color: var(--accent-gold, #c8a870);
    }
    .mobile-bottom-nav a svg {
        width: 18px;
        height: 18px;
        margin-right: 4px;
    }
}

/* ===== Chart Page Mobile ===== */

@media (max-width: 767px) {
    .page-container {
        display: flex !important;
        flex-direction: column !important;
        height: auto !important;
        grid-template-columns: none !important;
        overflow: visible !important;
    }
    .chart-section, .strategy-section, .info-section {
        width: 100% !important;
        border-right: none !important;
        display: flex !important;
        padding-right: 0 !important;
        padding-left: 0 !important;
    }
    .strategy-section, .info-section {
        border-top: 1px solid var(--border-color, #333);
        max-height: none;
        overflow-y: visible;
    }
    .chart-section {
        min-height: 50vh;
    }
}

/* ===== Dashboard Layout Mobile ===== */

@media (max-width: 767px) {
    .dashboard-header { display: none; }
    .dashboard-main { padding: 8px !important; }
    body { padding-bottom: 56px; }

    /* Strategies page: horizontal scroll for controls */
    .table-controls-bar.two-rows .controls-row {
        overflow-x: auto;
        -webkit-overflow-scrolling: touch;
        white-space: nowrap;
        padding-bottom: 4px;
    }

    .strategies-table-container {
        overflow-x: auto;
        -webkit-overflow-scrolling: touch;
    }

    /* Macro page */
    .macro-container { padding: 8px !important; }
    .macro-grid { grid-template-columns: 1fr !important; }
    .macro-card[style*="grid-column: span 2"] {
        grid-column: span 1 !important;
    }

    /* Heatmap page */
    .heatmap-container { padding: 8px !important; }
}

/* ===== Swipe Gesture Support ===== */

.swipeable {
    touch-action: pan-y;
    -webkit-overflow-scrolling: touch;
    overflow-x: auto;
    scroll-snap-type: x mandatory;
    scrollbar-width: none;
    -ms-overflow-style: none;
}

.swipeable::-webkit-scrollbar {
    display: none;
}

.swipeable > * {
    scroll-snap-align: start;
    flex-shrink: 0;
}

/* Horizontal scroll indicators for swipeable areas */
.swipeable-wrapper {
    position: relative;
}

.swipeable-wrapper::after {
    content: '';
    position: absolute;
    right: 0;
    top: 0;
    bottom: 0;
    width: 24px;
    background: linear-gradient(90deg, transparent, var(--bg-primary, #0a0a0a));
    pointer-events: none;
    opacity: 0.8;
    transition: opacity 0.2s ease;
}

/* Hide gradient when fully scrolled */
.swipeable-wrapper.scrolled-end::after {
    opacity: 0;
}

/* ===== Landscape Phone Adjustments ===== */

@media (max-height: 500px) and (orientation: landscape) {
    .page-container {
        padding: var(--space-2, 8px) var(--space-4, 16px);
    }

    /* Reduce vertical spacing in landscape */
    .card {
        padding: var(--space-2, 8px);
    }

    .card-header {
        margin-bottom: var(--space-1, 4px);
        padding-bottom: var(--space-1, 4px);
    }
}

/* ===== High-DPI / Retina Optimizations ===== */

@media (-webkit-min-device-pixel-ratio: 2), (min-resolution: 192dpi) {
    /* Sharper borders on retina */
    .card,
    .btn,
    .badge,
    .form-control {
        border-width: 1px;
    }

    /* Ensure crisp pixel rendering */
    img, canvas {
        image-rendering: -webkit-optimize-contrast;
    }
}

/* ===== Responsive Utility Classes ===== */

.hide-mobile { display: none; }
.show-mobile { display: block; }

@media (min-width: 768px) {
    .hide-mobile { display: block; }
    .show-mobile { display: none; }
    .hide-desktop { display: none; }
}

/* Text alignment responsive */
.text-center-mobile {
    text-align: center;
}

@media (min-width: 768px) {
    .text-center-mobile {
        text-align: left;
    }
}

/* Stack columns on mobile */
.stack-mobile {
    display: flex;
    flex-direction: column;
    gap: var(--space-2, 8px);
}

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