/* ============================================
   COMPREHENSIVE RESPONSIVE DESIGN SYSTEM
   Breakpoints:
   - Mobile Small: 480px↓
   - Mobile Large: 768px↓
   - Tablet: 1024px↓
   - Laptop: 1280px↓
   - Large Monitors: 1440px↑
   ============================================ */

/* ============================================
   BASE RESPONSIVE UTILITIES
   ============================================ */
* {
    box-sizing: border-box;
}

html {
    font-size: 16px;
    -webkit-text-size-adjust: 100%;
    -ms-text-size-adjust: 100%;
}

body {
    overflow-x: hidden;
    width: 100%;
    max-width: 100vw;
}

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

/* ============================================
   MOBILE MENU (HAMBURGER)
   ============================================ */
.mobile-menu-toggle {
    display: none;
    flex-direction: column;
    justify-content: space-around;
    width: 32px;
    height: 32px;
    background: transparent;
    border: none;
    cursor: pointer;
    padding: 0;
    z-index: 1001;
    gap: 4px;
}

.mobile-menu-toggle span {
    width: 100%;
    height: 3px;
    background: var(--text-primary);
    border-radius: 2px;
    transition: all 0.3s ease;
    transform-origin: center;
}

.mobile-menu-toggle.active span:nth-child(1) {
    transform: translateY(9px) rotate(45deg);
}

.mobile-menu-toggle.active span:nth-child(2) {
    opacity: 0;
    transform: translateX(-20px);
}

.mobile-menu-toggle.active span:nth-child(3) {
    transform: translateY(-9px) rotate(-45deg);
}

body.menu-open {
    overflow: hidden;
}

/* ============================================
   HEADER RESPONSIVE
   ============================================ */
@media (max-width: 1024px) {
    .header {
        padding: 0.75rem 1rem;
    }

    .header-content {
        flex-wrap: wrap;
        gap: 0.75rem;
    }

    .header-nav {
        flex-wrap: wrap;
        gap: 0.5rem;
    }
}

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

    .header {
        padding: 0.75rem;
        position: relative;
    }

    .header-content {
        flex-wrap: wrap;
        gap: 0.75rem;
    }

    .header-left {
        width: 100%;
        display: flex;
        justify-content: flex-start;
        align-items: center;
        flex-wrap: wrap;
        gap: 0.75rem;
    }

    .logo {
        font-size: 1rem;
        order: 1;
    }

    .dark-mode-toggle {
        padding: 6px 10px;
        font-size: 12px;
        gap: 4px;
        order: 2;
        flex-shrink: 0;
    }

    .dark-mode-toggle .toggle-text {
        display: none;
    }

    .mobile-menu-toggle {
        order: 3;
        margin-left: auto;
    }

    .header-nav {
        order: 4;
        width: 100%;
    }

    .header-nav {
        position: fixed;
        top: 60px;
        left: 0;
        right: 0;
        background: var(--bg-darker);
        border-bottom: 1px solid var(--border-color);
        flex-direction: column;
        padding: 1rem;
        transform: translateY(-100%);
        opacity: 0;
        visibility: hidden;
        transition: all 0.3s ease;
        z-index: 1000;
        box-shadow: 0 4px 12px rgba(0, 0, 0, 0.3);
        max-height: calc(100vh - 60px);
        overflow-y: auto;
    }

    .header-nav.mobile-active {
        transform: translateY(0);
        opacity: 1;
        visibility: visible;
    }

    .nav-link {
        width: 100%;
        padding: 0.75rem 1rem;
        text-align: left;
        border-radius: 6px;
        margin-bottom: 0.5rem;
    }

    .header-right {
        width: 100%;
        flex-wrap: wrap;
        gap: 0.75rem;
        justify-content: space-between;
    }

    .network-select {
        flex: 1;
        min-width: 120px;
        font-size: 0.8125rem;
        padding: 6px 10px;
    }

    .price-info .value {
        font-size: 0.875rem;
    }

    .connect-btn,
    .disconnect-btn,
    .demo-mode-btn,
    .back-to-landing-btn {
        padding: 6px 12px;
        font-size: 0.75rem;
        white-space: nowrap;
    }

    #walletContainer {
        flex-wrap: wrap;
        gap: 0.5rem;
        width: 100%;
    }
}

@media (max-width: 480px) {
    .header {
        padding: 0.5rem;
    }

    .logo span {
        font-size: 0.875rem;
    }

    .logo svg {
        width: 24px;
        height: 24px;
    }

    .dark-mode-toggle {
        padding: 4px 8px;
        font-size: 11px;
    }

    .dark-mode-toggle .toggle-icon {
        font-size: 14px;
    }

    .header-right {
        flex-direction: column;
        gap: 0.5rem;
    }

    .network-select,
    .connect-btn,
    .disconnect-btn,
    .demo-mode-btn,
    .back-to-landing-btn {
        width: 100%;
    }
}

/* ============================================
   MAIN CONTAINER RESPONSIVE
   ============================================ */
@media (max-width: 1280px) {
    .main-container {
        max-width: 100%;
        grid-template-columns: 1fr 22rem;
        gap: 1.25rem;
        padding: 1.25rem;
    }
}

@media (max-width: 1024px) {
    .main-container {
        grid-template-columns: 1fr !important;
        gap: 1rem;
        padding: 1rem;
    }

    .right-panel {
        order: -1;
    }

    .left-panel {
        order: 1;
    }
}

@media (max-width: 768px) {
    .main-container {
        padding: 0.75rem;
        gap: 0.75rem;
    }
}

@media (max-width: 480px) {
    .main-container {
        padding: 0.5rem;
        gap: 0.5rem;
    }
}

/* ============================================
   CHART CONTAINER RESPONSIVE
   ============================================ */
@media (max-width: 1024px) {
    .chart-container {
        width: 100%;
        min-height: auto;
    }

    .tradingview-widget-container {
        min-height: 35rem;
    }

    .tradingview-chart {
        min-height: 30rem;
    }
}

@media (max-width: 768px) {
    .chart-container {
        min-height: auto;
        width: 100%;
    }

    .chart-header {
        padding: 0.75rem;
        flex-direction: column;
        align-items: stretch;
        gap: 0.75rem;
    }

    .pair-selector {
        flex-direction: column;
        gap: 0.5rem;
        width: 100%;
    }

    .pair-info {
        flex-wrap: wrap;
        gap: 0.5rem;
    }

    .pair {
        font-size: 1.125rem;
    }

    .pair-price {
        font-size: 1rem;
    }

    .price-change {
        font-size: 0.75rem;
        padding: 2px 6px;
    }

    .oi-display {
        flex-wrap: wrap;
        gap: 0.75rem;
        width: 100%;
    }

    .oi-item {
        flex: 1;
        min-width: calc(50% - 0.375rem);
    }

    .chart-toolbar {
        padding: 0.5rem;
        flex-wrap: wrap;
        gap: 0.5rem;
        overflow-x: auto;
    }

    .timeframe-selector {
        flex-wrap: wrap;
        gap: 0.25rem;
        overflow-x: auto;
        -webkit-overflow-scrolling: touch;
    }

    .timeframe-btn {
        padding: 4px 8px;
        font-size: 0.6875rem;
        white-space: nowrap;
    }

    .tradingview-widget-container {
        min-height: 18.75rem;
        max-height: 18.75rem;
        overflow: hidden;
        position: relative;
    }

    .tradingview-chart {
        min-height: 18.75rem;
        max-height: 18.75rem;
        overflow-x: auto;
        overflow-y: hidden;
        -webkit-overflow-scrolling: touch;
    }

    /* Enable zoom and scroll for TradingView chart on mobile */
    .tradingview-widget-container iframe {
        transform-origin: top left;
        min-width: 100%;
    }
}

@media (max-width: 480px) {
    .chart-container {
        border-radius: 0.5rem;
    }

    .chart-header {
        padding: 0.5rem;
        gap: 0.5rem;
    }

    .pair {
        font-size: 1rem;
    }

    .pair-price {
        font-size: 0.875rem;
    }

    .symbol-select {
        width: 100%;
        min-width: 100%;
        font-size: 0.75rem;
        padding: 6px 28px 6px 8px;
    }

    .tradingview-widget-container {
        min-height: 15.625rem;
        max-height: 15.625rem;
    }

    .tradingview-chart {
        min-height: 15.625rem;
        max-height: 15.625rem;
    }

    .timeframe-btn {
        padding: 3px 6px;
        font-size: 0.625rem;
    }

    .chart-type-controls {
        gap: 0.25rem;
    }

    .chart-type-btn {
        padding: 4px 6px;
        font-size: 0.75rem;
    }
}

/* ============================================
   ORDER PANEL (RIGHT PANEL) RESPONSIVE
   ============================================ */
@media (max-width: 1024px) {
    .right-panel {
        width: 100%;
        max-width: 100%;
    }

    .trading-panel {
        width: 100%;
        max-width: 100%;
    }
}

@media (max-width: 768px) {
    .trading-panel {
        width: 100%;
        padding: 0;
        border-radius: 0.75rem;
    }

    .tabs {
        flex-wrap: wrap;
        border-bottom: 1px solid var(--border-color);
    }

    .tab {
        flex: 1;
        min-width: calc(33.333% - 4px);
        padding: 0.75rem 0.5rem;
        font-size: 0.75rem;
        text-align: center;
    }

    .tab-content {
        padding: 1rem;
    }

    .mark-price-box {
        padding: 0.75rem;
        margin-bottom: 0.75rem;
        font-size: 0.75rem;
        flex-direction: column;
        gap: 0.25rem;
        align-items: flex-start;
    }

    .futures-type-selector {
        margin-bottom: 1rem;
        padding-bottom: 0.75rem;
    }

    .futures-type-selector label {
        font-size: 0.75rem;
        margin-bottom: 0.5rem;
    }

    .futures-type-select,
    .collateral-select {
        width: 100%;
        padding: 0.75rem;
        font-size: 0.8125rem;
    }

    .direction-selector {
        grid-template-columns: 1fr 1fr;
        gap: 0.5rem;
        margin-bottom: 1rem;
    }

    .direction-btn {
        padding: 0.75rem 0.5rem;
        font-size: 0.75rem;
    }

    .direction-btn span:first-child {
        font-size: 0.75rem;
    }

    .direction-price {
        font-size: 0.625rem;
    }

    .leverage-selector {
        margin-bottom: 1rem;
    }

    .leverage-selector label {
        font-size: 0.75rem;
        margin-bottom: 0.5rem;
    }

    .leverage-buttons {
        grid-template-columns: repeat(4, 1fr);
        gap: 0.5rem;
        margin-bottom: 0.75rem;
    }

    .leverage-btn {
        padding: 0.5rem 0.25rem;
        font-size: 0.6875rem;
    }

    .leverage-input-wrapper {
        padding: 0.25rem;
    }

    .leverage-input-wrapper input {
        font-size: 0.875rem;
        padding: 0.5rem;
    }

    .input-group {
        margin-bottom: 1rem;
    }

    .input-group label {
        font-size: 0.75rem;
        margin-bottom: 0.5rem;
        flex-direction: column;
        align-items: flex-start;
        gap: 0.25rem;
    }

    .balance-text {
        font-size: 0.625rem;
    }

    .input-wrapper {
        flex-direction: column;
        gap: 0.5rem;
        width: 100%;
    }

    .input-wrapper input,
    .input-wrapper select {
        width: 100%;
        padding: 0.75rem;
        font-size: 0.8125rem;
    }

    .percentage-buttons {
        gap: 0.5rem;
        margin-top: 0.5rem;
    }

    .percent-btn {
        padding: 0.5rem;
        font-size: 0.6875rem;
    }

    .duration-selector {
        gap: 0.25rem;
        flex-wrap: wrap;
    }

    .duration-btn {
        padding: 0.5rem 0.75rem;
        font-size: 0.6875rem;
        flex: 1;
        min-width: calc(33.333% - 4px);
    }

    .order-type-selector {
        grid-template-columns: 1fr 1fr;
        gap: 0.5rem;
    }

    .order-type-btn {
        padding: 0.75rem 0.5rem;
        font-size: 0.75rem;
    }

    .trade-info {
        padding: 0.75rem;
        margin-bottom: 1rem;
    }

    .info-row {
        font-size: 0.75rem;
        margin-bottom: 0.75rem;
    }

    .info-label,
    .info-value {
        font-size: 0.75rem;
    }

    .trade-btn {
        width: 100%;
        padding: 0.875rem;
        font-size: 0.875rem;
        margin-bottom: 0.75rem;
    }

    .calc-inline-btn {
        width: 100%;
        padding: 0.75rem;
        font-size: 0.75rem;
    }
}

@media (max-width: 480px) {
    .tab {
        padding: 0.625rem 0.375rem;
        font-size: 0.6875rem;
    }

    .tab-content {
        padding: 0.75rem;
    }

    .direction-selector {
        grid-template-columns: 1fr;
        gap: 0.5rem;
    }

    .direction-btn {
        width: 100%;
        padding: 0.875rem;
    }

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

    .duration-btn {
        min-width: calc(50% - 4px);
    }

    .input-wrapper input,
    .input-wrapper select {
        padding: 0.625rem;
        font-size: 0.75rem;
    }
}

/* ============================================
   POSITIONS PANEL RESPONSIVE
   ============================================ */
@media (max-width: 1024px) {
    .positions-panel {
        width: 100%;
    }

    .positions-section-main {
        width: 100%;
        margin-top: 1rem;
    }
}

@media (max-width: 768px) {
    /* Convert positions table to cards on mobile */
    .positions-table-wrapper {
        overflow: visible;
    }

    .positions-table {
        display: block;
        min-width: auto;
    }

    .positions-table thead {
        display: none;
    }

    .positions-table tbody {
        display: block;
    }

    .positions-table tbody tr {
        display: block;
        background: var(--bg-panel);
        border: 1px solid var(--border-color);
        border-radius: 0.75rem;
        padding: 1rem;
        margin-bottom: 0.75rem;
        box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
    }

    .positions-table tbody tr.empty-positions {
        background: transparent;
        border: none;
        padding: 2rem 1rem;
        box-shadow: none;
    }

    .positions-table tbody tr.empty-positions td {
        display: block !important;
        border: none !important;
        padding: 0 !important;
    }

    .positions-table tbody tr:not(.empty-positions) td {
        display: flex;
        justify-content: space-between;
        align-items: center;
        padding: 0.625rem 0;
        border-bottom: 1px solid var(--border-color);
        font-size: 0.8125rem;
        flex-wrap: wrap;
        gap: 0.5rem;
    }

    .positions-table tbody tr:not(.empty-positions) td:last-of-type {
        border-bottom: none;
        padding-top: 0.75rem;
        margin-top: 0.5rem;
        border-top: 1px solid var(--border-color);
    }

    .positions-table tbody tr td::before {
        content: attr(data-label);
        font-weight: 600;
        color: var(--text-secondary);
        text-transform: uppercase;
        font-size: 0.6875rem;
        letter-spacing: 0.5px;
        margin-right: auto;
    }

    .positions-table tbody tr td .position-actions-cell {
        width: 100%;
        justify-content: flex-end;
        margin-top: 0.5rem;
    }

    .position-type-badge {
        padding: 4px 8px;
        font-size: 0.75rem;
    }

    .position-close-btn {
        padding: 0.625rem 1rem;
        font-size: 0.75rem;
        width: 100%;
    }
}

/* Keep table view for larger screens */
@media (min-width: 769px) {
    .positions-table {
        display: table;
    }

    .positions-table thead {
        display: table-header-group;
    }

    .positions-table tbody {
        display: table-row-group;
    }

    .positions-table tbody tr {
        display: table-row;
    }

    .positions-table tbody tr td {
        display: table-cell;
    }

    .positions-table tbody tr td::before {
        content: none;
    }
}

/* Alternative: Scrollable table view (for reference only)
    /*
    .positions-table-wrapper {
        overflow: visible;
    }

    .positions-table {
        display: block;
    }

    .positions-table thead {
        display: none;
    }

    .positions-table tbody {
        display: block;
    }

    .positions-table tbody tr {
        display: block;
        background: var(--bg-panel);
        border: 1px solid var(--border-color);
        border-radius: 0.75rem;
        padding: 1rem;
        margin-bottom: 0.75rem;
    }

    .positions-table tbody tr td {
        display: flex;
        justify-content: space-between;
        align-items: center;
        padding: 0.5rem 0;
        border-bottom: 1px solid var(--border-color);
        font-size: 0.8125rem;
    }

    .positions-table tbody tr td:last-child {
        border-bottom: none;
    }
    */

    .positions-table-header {
        flex-direction: column;
        align-items: flex-start;
        gap: 0.5rem;
        padding-bottom: 0.75rem;
    }

    .positions-table-header h3 {
        font-size: 1rem;
    }

    .positions-count {
        font-size: 0.75rem;
    }

    .positions-panel {
        padding: 0.75rem;
    }

    .panel-header {
        flex-direction: column;
        gap: 0.75rem;
        padding: 0.75rem;
    }

    .panel-tabs {
        width: 100%;
        flex-wrap: wrap;
        gap: 0.25rem;
    }

    .panel-tab {
        flex: 1;
        min-width: calc(33.333% - 4px);
        padding: 0.5rem 0.375rem;
        font-size: 0.6875rem;
        text-align: center;
    }

    .panel-actions {
        width: 100%;
        flex-wrap: wrap;
        gap: 0.5rem;
        justify-content: space-between;
    }

    .close-all-btn {
        flex: 1;
        padding: 0.5rem;
        font-size: 0.6875rem;
    }

    .expiry-toggle {
        flex: 1;
        justify-content: center;
    }

    .expiry-btn {
        flex: 1;
        padding: 0.5rem 0.375rem;
        font-size: 0.6875rem;
    }
}

@media (max-width: 480px) {
    .positions-table tbody tr {
        padding: 0.75rem;
    }

    .positions-table tbody tr td {
        font-size: 0.75rem;
        padding: 0.375rem 0;
    }

    .positions-table-header h3 {
        font-size: 0.875rem;
    }

    .panel-tab {
        min-width: calc(50% - 4px);
        font-size: 0.625rem;
        padding: 0.375rem 0.25rem;
    }
}

/* ============================================
   MARKET STATS PANEL RESPONSIVE
   ============================================ */
@media (max-width: 1024px) {
    .market-stats-panel {
        width: 100%;
    }
}

@media (max-width: 768px) {
    .stats-grid {
        grid-template-columns: repeat(2, 1fr);
        gap: 0.75rem;
        padding: 0.75rem;
    }

    .stat-item {
        padding: 0.75rem;
    }

    .stat-label {
        font-size: 0.6875rem;
    }

    .stat-value {
        font-size: 0.875rem;
    }
}

@media (max-width: 480px) {
    .stats-grid {
        grid-template-columns: 1fr;
        gap: 0.5rem;
        padding: 0.5rem;
    }

    .stat-item {
        padding: 0.625rem;
    }
}

/* ============================================
   RECENT TRADES PANEL RESPONSIVE
   ============================================ */
@media (max-width: 768px) {
    .recent-trades-panel {
        width: 100%;
    }

    .trades-list {
        padding: 0.75rem;
        font-size: 0.75rem;
    }

    .trade-item {
        padding: 0.625rem;
        font-size: 0.6875rem;
        margin-bottom: 0.5rem;
    }
}

/* ============================================
   MODALS RESPONSIVE
   ============================================ */
@media (max-width: 768px) {
    .modal-content {
        width: 95%;
        max-width: 95%;
        padding: 1rem;
        margin: 1rem;
    }

    .calc-modal {
        width: 95%;
    }

    .calc-grid {
        grid-template-columns: 1fr;
        gap: 1rem;
        padding: 1rem;
    }
}

@media (max-width: 480px) {
    .modal-content {
        width: 100%;
        max-width: 100%;
        padding: 0.75rem;
        margin: 0.5rem;
        border-radius: 0.5rem;
    }
}

/* ============================================
   FLOATING PNL RESPONSIVE
   ============================================ */
@media (max-width: 768px) {
    .floating-pnl {
        bottom: 0.75rem;
        left: 0.75rem;
        right: 0.75rem;
        border-radius: 0.75rem;
        padding: 0.75rem;
    }

    .floating-pnl-content {
        flex-direction: column;
        gap: 0.75rem;
    }

    .floating-pnl-info {
        font-size: 0.75rem;
    }

    .floating-close-btn {
        width: 100%;
        padding: 0.75rem;
        font-size: 0.75rem;
    }
}

/* ============================================
   TOAST NOTIFICATIONS RESPONSIVE
   ============================================ */
@media (max-width: 768px) {
    .toast-container {
        top: 0.5rem;
        left: 0.5rem;
        right: 0.5rem;
        bottom: auto;
    }

    .toast {
        min-width: auto;
        max-width: 100%;
        padding: 0.75rem;
    }

    .toast-title {
        font-size: 0.8125rem;
    }

    .toast-message {
        font-size: 0.75rem;
    }
}

/* ============================================
   UTILITY CLASSES
   ============================================ */
.hide-mobile {
    display: block;
}

.show-mobile {
    display: none;
}

@media (max-width: 768px) {
    .hide-mobile {
        display: none !important;
    }

    .show-mobile {
        display: block !important;
    }
}

.hide-tablet {
    display: block;
}

.show-tablet {
    display: none;
}

@media (max-width: 1024px) and (min-width: 769px) {
    .hide-tablet {
        display: none !important;
    }

    .show-tablet {
        display: block !important;
    }
}

/* ============================================
   TOUCH FRIENDLY ELEMENTS
   ============================================ */
@media (max-width: 768px) {
    button,
    .direction-btn,
    .leverage-btn,
    .trade-btn,
    .close-btn,
    .panel-tab,
    .tab,
    input[type="number"],
    input[type="text"],
    input[type="email"],
    select,
    .nav-link {
        min-height: 44px;
        touch-action: manipulation;
        -webkit-tap-highlight-color: transparent;
    }
}

/* ============================================
   SCROLLBAR STYLING
   ============================================ */
.positions-table-wrapper::-webkit-scrollbar,
.overflow-auto::-webkit-scrollbar {
    height: 8px;
    width: 8px;
}

.positions-table-wrapper::-webkit-scrollbar-track,
.overflow-auto::-webkit-scrollbar-track {
    background: var(--bg-darker);
    border-radius: 4px;
}

.positions-table-wrapper::-webkit-scrollbar-thumb,
.overflow-auto::-webkit-scrollbar-thumb {
    background: var(--border-color);
    border-radius: 4px;
}

.positions-table-wrapper::-webkit-scrollbar-thumb:hover,
.overflow-auto::-webkit-scrollbar-thumb:hover {
    background: var(--text-secondary);
}

/* ============================================
   PREVENT HORIZONTAL SCROLL
   ============================================ */
@media (max-width: 1440px) {
    body,
    html {
        overflow-x: hidden;
        max-width: 100vw;
        width: 100%;
    }

    .main-container,
    .header-content,
    .chart-container,
    .trading-panel,
    .positions-panel,
    .market-stats-panel,
    .recent-trades-panel,
    .left-panel,
    .right-panel {
        max-width: 100%;
        overflow-x: hidden;
        box-sizing: border-box;
    }
}

@media (max-width: 768px) {
    * {
        max-width: 100%;
        box-sizing: border-box;
    }

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

    /* Prevent any element from causing horizontal scroll */
    .input-wrapper,
    .input-group,
    .direction-selector,
    .leverage-buttons,
    .stats-grid,
    .panel-header,
    .panel-actions {
        max-width: 100%;
        overflow-x: hidden;
    }

    /* Ensure select elements don't overflow */
    select,
    .symbol-select,
    .network-select,
    .collateral-select,
    .futures-type-select {
        max-width: 100%;
        width: 100%;
        box-sizing: border-box;
    }
}

/* ============================================
   LARGE MONITORS (1440px+)
   ============================================ */
@media (min-width: 1440px) {
    .main-container {
        max-width: 87.5rem;
        margin: 0 auto;
    }

    .header-content {
        max-width: 87.5rem;
        margin: 0 auto;
    }
}

/* ============================================
   LANDSCAPE ORIENTATION
   ============================================ */
@media (max-width: 768px) and (orientation: landscape) {
    .tradingview-widget-container {
        min-height: 22rem;
        max-height: 22rem;
    }

    .tradingview-chart {
        min-height: 22rem;
        max-height: 22rem;
    }

    .direction-selector {
        grid-template-columns: 1fr 1fr;
    }
}
