/* ==========================================================================
   BBLN Pro - Universal Mobile & Tablet Responsiveness System
   Engineered for iOS / Android, Safari, Chrome & Touch Devices
   Aba de Operações (Trading Room MT5) First-Class Mobile Experience
   ========================================================================== */

/* --------------------------------------------------------------------------
   DESKTOP DEFAULT RULES (>= 769px) - Protect & Preserve Desktop Terminal
   -------------------------------------------------------------------------- */
@media (min-width: 769px) {
  .mobile-header-bar,
  .mobile-bottom-nav,
  .sidebar-backdrop,
  .sidebar-close-btn,
  .tr-mobile-tabs,
  .tr-mobile-quick-actions {
    display: none !important;
  }

  .trading-room-view[data-mob-tab] .chart-card-container,
  .trading-room-view[data-mob-tab] .market-watch-card,
  .trading-room-view[data-mob-tab] .mt5-toolbox-panel {
    display: flex !important;
  }

  .tr-main-grid {
    display: grid !important;
    grid-template-columns: 1fr var(--market-watch-width) !important;
  }
}

/* --------------------------------------------------------------------------
   1. MOBILE APP SHELL & NAVIGATION (< 768px)
   -------------------------------------------------------------------------- */
@media (max-width: 768px) {
  html, body {
    overflow-x: hidden;
    position: fixed;
    width: 100%;
    height: 100%;
    -webkit-overflow-scrolling: touch;
  }

  body.drawer-locked {
    overflow: hidden !important;
  }

  #app-container {
    flex-direction: column;
    width: 100vw;
    height: 100vh;
    height: 100dvh;
    overflow: hidden;
  }

  /* Mobile Top Header Bar */
  .mobile-header-bar {
    display: flex !important;
    align-items: center;
    justify-content: space-between;
    height: 52px;
    padding: 0 12px;
    background: linear-gradient(180deg, #131828 0%, #0E1322 100%);
    border-bottom: 1px solid var(--border-subtle);
    z-index: 1000;
    flex-shrink: 0;
    box-shadow: 0 2px 10px rgba(0, 0, 0, 0.4);
  }

  .mobile-menu-btn {
    width: 38px;
    height: 38px;
    border-radius: var(--radius-sm);
    background: var(--bg-card);
    border: 1px solid var(--border-subtle);
    color: #FFFFFF;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    flex-shrink: 0;
    transition: all var(--transition-fast);
  }

  .mobile-menu-btn:active {
    background: var(--bg-hover);
    transform: scale(0.95);
  }

  .mobile-brand-box {
    display: flex;
    align-items: center;
    gap: 6px;
    font-size: 17px;
    font-weight: 800;
  }

  .mobile-header-actions {
    display: flex;
    align-items: center;
    gap: 8px;
  }

  .mobile-header-equity {
    display: flex;
    flex-direction: column;
    align-items: flex-end;
  }

  .mobile-equity-label {
    font-size: 8.5px;
    font-weight: 700;
    letter-spacing: 0.6px;
    color: var(--text-tertiary);
  }

  .mobile-equity-val {
    font-size: 13px;
    font-weight: 800;
    color: var(--color-up);
  }

  .mobile-order-quick-btn {
    width: 34px;
    height: 34px;
    border-radius: var(--radius-sm);
    background: linear-gradient(135deg, #3B82F6 0%, #1D4ED8 100%);
    border: 1px solid rgba(147, 197, 253, 0.4);
    color: #FFFFFF;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    box-shadow: 0 2px 8px rgba(37, 99, 235, 0.4);
  }

  /* Mobile Sliding Drawer Sidebar */
  .app-sidebar {
    position: fixed !important;
    left: 0;
    top: 0;
    bottom: 0;
    width: 280px;
    max-width: 82vw;
    height: 100%;
    z-index: 10000 !important;
    transform: translateX(-100%);
    transition: transform 0.28s cubic-bezier(0.16, 1, 0.3, 1);
    box-shadow: 0 0 50px rgba(0, 0, 0, 0.85);
    background-color: #0E1322 !important;
    border-right: 1px solid rgba(99, 102, 241, 0.25);
  }

  .app-sidebar.mobile-open {
    transform: translateX(0);
  }

  .sidebar-backdrop {
    position: fixed;
    inset: 0;
    background: rgba(0, 0, 0, 0.72);
    backdrop-filter: blur(4px);
    -webkit-backdrop-filter: blur(4px);
    opacity: 0;
    pointer-events: none;
    transition: opacity 0.25s ease;
    z-index: 9999;
  }

  .sidebar-backdrop.active {
    opacity: 1;
    pointer-events: auto;
  }

  .sidebar-close-btn {
    display: flex !important;
    align-items: center;
    justify-content: center;
    margin-left: auto;
    width: 30px;
    height: 30px;
    border-radius: 50%;
    background: rgba(255, 255, 255, 0.08);
    border: none;
    color: #FFFFFF;
    font-size: 18px;
    cursor: pointer;
  }

  /* Main Content Workspace on Mobile */
  .app-main {
    flex: 1;
    width: 100%;
    height: calc(100dvh - 52px - 58px) !important;
    min-height: 0;
    overflow: hidden;
  }

  .view-container {
    height: 100% !important;
    overflow-y: auto !important;
    -webkit-overflow-scrolling: touch;
  }

  /* Mobile Bottom Navigation Bar */
  .mobile-bottom-nav {
    display: flex !important;
    align-items: center;
    justify-content: space-around;
    height: 58px;
    padding-bottom: env(safe-area-inset-bottom, 0px);
    background: linear-gradient(180deg, #131828 0%, #0B0E17 100%);
    border-top: 1px solid var(--border-subtle);
    z-index: 900;
    flex-shrink: 0;
    box-shadow: 0 -4px 16px rgba(0, 0, 0, 0.45);
  }

  .mob-bottom-btn {
    flex: 1;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 3px;
    height: 100%;
    background: transparent;
    border: none;
    color: var(--text-tertiary);
    font-size: 10px;
    font-weight: 600;
    cursor: pointer;
    transition: all 0.15s ease;
    padding: 4px 0;
  }

  .mob-bottom-btn svg {
    width: 19px;
    height: 19px;
    fill: currentColor;
    transition: transform 0.15s ease;
  }

  .mob-bottom-btn:active svg {
    transform: scale(0.9);
  }

  .mob-bottom-btn.active {
    color: #FFFFFF;
  }

  .mob-bottom-btn.active svg {
    fill: #6366F1;
    filter: drop-shadow(0 0 6px rgba(99, 102, 241, 0.6));
  }

  .mob-bottom-btn.active span {
    color: #FFFFFF;
    font-weight: 700;
  }

  /* Hide Desktop Topbar on Dashboard / other views on mobile */
  .topbar {
    display: none !important;
  }
}

/* --------------------------------------------------------------------------
   2. ABA DE OPERAÇÕES (SALA DE OPERAÇÕES MT5) NO MOBILE
   -------------------------------------------------------------------------- */
@media (max-width: 768px) {
  #view-trading-room.active {
    height: 100% !important;
    overflow: hidden !important;
    display: flex !important;
    flex-direction: column !important;
  }

  .trading-room-view {
    padding: 6px 8px !important;
    gap: 6px !important;
    height: 100% !important;
    overflow-y: hidden !important;
    display: flex !important;
    flex-direction: column !important;
  }

  /* --- Ticker Banner on Mobile --- */
  .tr-ticker-banner {
    flex-direction: column !important;
    align-items: stretch !important;
    gap: 6px !important;
    padding: 8px 10px !important;
  }

  .ticker-left-group {
    width: 100% !important;
    justify-content: space-between !important;
    gap: 8px !important;
  }

  .coin-stack-icon {
    width: 26px !important;
    height: 26px !important;
  }

  .symbol-selector-dropdown {
    font-size: 15px !important;
    gap: 6px !important;
  }

  .last-price-block {
    margin-left: auto;
    gap: 6px !important;
  }

  .last-price-label {
    display: none;
  }

  .last-price-val {
    font-size: 17px !important;
  }

  .change-percent-badge {
    font-size: 11px !important;
    padding: 2px 6px !important;
  }

  .ticker-stats-right {
    width: 100% !important;
    overflow-x: auto !important;
    -webkit-overflow-scrolling: touch !important;
    gap: 12px !important;
    padding: 2px 0 3px 0 !important;
    scrollbar-width: none;
  }

  .ticker-stats-right::-webkit-scrollbar {
    display: none;
  }

  .ticker-stat-box {
    flex-shrink: 0;
    min-width: 60px;
  }

  .ticker-stat-box .box-label {
    font-size: 8.5px !important;
  }

  .ticker-stat-box .box-val {
    font-size: 11.5px !important;
  }

  .btn-topbar-nova-ordem,
  .btn-topbar-ai-robot {
    display: none !important;
  }

  /* --- Mobile Segmented Tabs (Gráfico | Posições | Mercado) --- */
  .tr-mobile-tabs {
    display: flex !important;
    align-items: center;
    background: var(--bg-card);
    border: 1px solid var(--border-subtle);
    border-radius: var(--radius-sm);
    padding: 3px;
    gap: 4px;
    flex-shrink: 0;
  }

  .tr-mobile-tab-btn {
    flex: 1;
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 6px;
    height: 32px;
    background: transparent;
    border: none;
    border-radius: 4px;
    color: var(--text-secondary);
    font-size: 11.5px;
    font-weight: 700;
    cursor: pointer;
    transition: all 0.15s ease;
  }

  .tr-mobile-tab-btn svg {
    flex-shrink: 0;
  }

  .tr-mobile-tab-btn.active {
    background: #6366F1;
    color: #FFFFFF;
    box-shadow: 0 2px 8px rgba(99, 102, 241, 0.4);
  }

  .tr-mob-badge {
    background: rgba(0, 0, 0, 0.4);
    color: #FFFFFF;
    font-size: 9.5px;
    font-weight: 800;
    padding: 1px 5px;
    border-radius: var(--radius-full);
  }

  .tr-mobile-tab-btn.active .tr-mob-badge {
    background: #FFFFFF;
    color: #6366F1;
  }

  /* --- Responsive Grid Split Handling --- */
  .tr-main-grid {
    display: flex !important;
    flex-direction: column !important;
    flex: 1 !important;
    min-height: 0 !important;
    gap: 6px !important;
    overflow: hidden !important;
  }

  /* TAB 1: GRÁFICO (Chart View) */
  .trading-room-view[data-mob-tab="chart"] .chart-card-container {
    display: flex !important;
    flex: 1 !important;
    height: 100% !important;
    min-height: 0 !important;
  }
  .trading-room-view[data-mob-tab="chart"] .market-watch-card {
    display: none !important;
  }
  .trading-room-view[data-mob-tab="chart"] .mt5-toolbox-panel {
    display: none !important;
  }

  /* TAB 2: POSIÇÕES (Toolbox View) */
  .trading-room-view[data-mob-tab="positions"] .chart-card-container {
    display: none !important;
  }
  .trading-room-view[data-mob-tab="positions"] .market-watch-card {
    display: none !important;
  }
  .trading-room-view[data-mob-tab="positions"] .mt5-toolbox-panel {
    display: flex !important;
    flex: 1 !important;
    height: 100% !important;
    min-height: 0 !important;
  }

  /* TAB 3: MERCADO (Market Watch View) */
  .trading-room-view[data-mob-tab="markets"] .chart-card-container {
    display: none !important;
  }
  .trading-room-view[data-mob-tab="markets"] .market-watch-card {
    display: flex !important;
    flex: 1 !important;
    height: 100% !important;
    min-height: 0 !important;
  }
  .trading-room-view[data-mob-tab="markets"] .mt5-toolbox-panel {
    display: none !important;
  }

  /* --- Market Watch on Mobile --- */
  .market-watch-card {
    border-radius: var(--radius-sm) !important;
  }

  .mw-header {
    padding: 8px 10px !important;
  }

  .mw-categories-pills {
    overflow-x: auto !important;
    -webkit-overflow-scrolling: touch !important;
    gap: 6px !important;
    padding-bottom: 2px !important;
    scrollbar-width: none;
  }

  .mw-categories-pills::-webkit-scrollbar {
    display: none;
  }

  .mw-cat-pill {
    flex-shrink: 0 !important;
    white-space: nowrap !important;
    padding: 4px 10px !important;
    font-size: 11px !important;
  }

  .mw-table th {
    padding: 6px 10px !important;
    font-size: 9.5px !important;
  }

  .mw-table td {
    padding: 8px 10px !important;
    font-size: 11.5px !important;
  }

  /* --- MT5 Toolbox (Posições e Histórico) on Mobile --- */
  .mt5-toolbox-panel {
    height: 100% !important;
    border-radius: var(--radius-sm) !important;
  }

  .mt5-toolbox-header {
    padding: 0 10px !important;
    height: 36px !important;
  }

  .mt5-toolbox-tabs {
    gap: 12px !important;
  }

  .mt5-tab-btn {
    font-size: 11px !important;
    padding: 7px 0 !important;
  }

  .btn-table-action {
    font-size: 10.5px !important;
    padding: 3px 8px !important;
  }

  .term-empty-state {
    padding: 30px 16px !important;
    text-align: center !important;
    font-size: 12px !important;
    color: var(--text-tertiary) !important;
  }

  /* --- Transform MT5 Table into Mobile Cards --- */
  .mt5-table thead {
    display: none !important;
  }

  .mt5-table,
  .mt5-table tbody {
    display: block !important;
    width: 100% !important;
  }

  .mt5-table tbody tr {
    display: flex !important;
    flex-direction: column !important;
    gap: 8px !important;
    margin: 8px 10px !important;
    padding: 12px 14px !important;
    background: linear-gradient(180deg, #182035 0%, #121829 100%) !important;
    border: 1px solid rgba(99, 102, 241, 0.22) !important;
    border-radius: var(--radius-md) !important;
    box-shadow: 0 4px 14px rgba(0, 0, 0, 0.4) !important;
  }

  .mt5-table tbody tr:hover {
    background: linear-gradient(180deg, #1B243D 0%, #151C30 100%) !important;
  }

  .mt5-table tbody td {
    display: flex !important;
    justify-content: space-between !important;
    align-items: center !important;
    padding: 2px 0 !important;
    border-bottom: 1px dashed rgba(255, 255, 255, 0.05) !important;
    font-size: 12px !important;
  }

  .mt5-table tbody td[colspan] {
    display: block !important;
    text-align: center !important;
    justify-content: center !important;
  }

  .mt5-table tbody td[colspan]::before {
    display: none !important;
  }

  .mt5-table tbody td::before {
    content: attr(data-label);
    font-size: 10.5px;
    font-weight: 700;
    color: var(--text-tertiary);
    text-transform: uppercase;
  }

  /* Top Card Highlights */
  .mt5-table tbody td[data-label="Símbolo"] {
    order: -3;
    font-size: 14px !important;
  }

  .mt5-table tbody td[data-label="Lucro"],
  .mt5-table tbody td[data-label="Lucro Realizado"] {
    order: -2;
    padding: 6px 0 !important;
    font-size: 15px !important;
    border-bottom: 1px solid rgba(255, 255, 255, 0.1) !important;
  }

  .mt5-table tbody td[data-label="Tipo"] {
    order: -1;
  }

  .mt5-table tbody td[data-label="Ação"] {
    order: 10;
    border-bottom: none !important;
    padding-top: 8px !important;
    width: 100%;
  }

  .mt5-table tbody td[data-label="Ação"]::before {
    display: none;
  }

  .btn-mt5-close-order {
    width: 100% !important;
    height: 38px !important;
    border-radius: var(--radius-sm) !important;
    background: rgba(239, 68, 68, 0.15) !important;
    border: 1px solid rgba(239, 68, 68, 0.45) !important;
    color: #EF4444 !important;
    font-weight: 700 !important;
    font-size: 12px !important;
    display: flex !important;
    align-items: center !important;
    justify-content: center !important;
    gap: 6px;
  }

  .btn-mt5-close-order::after {
    content: "Fechar Posição a Mercado";
    font-size: 11.5px;
    font-weight: 700;
  }

  .btn-mt5-close-order:active {
    background: #EF4444 !important;
    color: #FFFFFF !important;
  }

  /* --- MT5 Summary Footer on Mobile --- */
  .mt5-summary-footer {
    display: grid !important;
    grid-template-columns: repeat(2, 1fr) !important;
    gap: 6px 12px !important;
    padding: 8px 10px !important;
    height: auto !important;
    background: var(--bg-card-secondary) !important;
    border-top: 1px solid var(--border-subtle) !important;
  }

  .summary-metric {
    display: flex;
    justify-content: space-between;
    align-items: center;
    font-size: 11px !important;
  }

  .summary-metric .label {
    font-size: 10px !important;
    color: var(--text-tertiary) !important;
  }

  .summary-metric .val {
    font-size: 11px !important;
    font-weight: 700 !important;
  }

  /* --- Mobile Bottom Floating Quick Trading Actions --- */
  .tr-mobile-quick-actions {
    display: flex !important;
    align-items: center;
    gap: 8px;
    padding: 6px 4px 2px 4px;
    flex-shrink: 0;
  }

  .btn-mob-trade {
    flex: 1;
    height: 44px;
    border-radius: var(--radius-sm);
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    border: none;
    color: #FFFFFF;
    cursor: pointer;
    transition: transform 0.1s ease;
  }

  .btn-mob-trade:active {
    transform: scale(0.97);
  }

  .btn-mob-trade.sell {
    background: linear-gradient(135deg, #EF4444 0%, #B91C1C 100%);
    box-shadow: 0 2px 10px rgba(239, 68, 68, 0.4);
  }

  .btn-mob-trade.buy {
    background: linear-gradient(135deg, #10B981 0%, #047857 100%);
    box-shadow: 0 2px 10px rgba(16, 185, 129, 0.4);
  }

  .btn-mob-top {
    font-size: 11px;
    font-weight: 800;
    letter-spacing: 0.5px;
  }

  .btn-mob-price {
    font-size: 12px;
    font-weight: 700;
  }

  .btn-mob-boleta-trigger {
    width: 68px;
    height: 44px;
    border-radius: var(--radius-sm);
    background: var(--bg-card);
    border: 1px solid var(--border-medium);
    color: #FFFFFF;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 2px;
    font-size: 9.5px;
    font-weight: 700;
    cursor: pointer;
    flex-shrink: 0;
  }

  .btn-mob-boleta-trigger:active {
    background: var(--bg-hover);
  }
}

/* --------------------------------------------------------------------------
   3. BOLETA DE NEGOCIAÇÃO (MODAL MT5 NOVA ORDEM) NO MOBILE
   Transforma-se em Bottom Sheet tátil e ergonômica
   -------------------------------------------------------------------------- */
@media (max-width: 768px) {
  .modal-overlay {
    align-items: flex-end !important;
    padding: 0 !important;
  }

  .mt5-modal-dialog,
  .modal-crypto-dialog,
  .modal-ai-dialog,
  .ai-modal-dialog {
    width: 100% !important;
    max-width: 100% !important;
    border-radius: 20px 20px 0 0 !important;
    max-height: 90vh !important;
    max-height: 90dvh !important;
    overflow-y: auto !important;
    -webkit-overflow-scrolling: touch !important;
    padding: 14px 16px 28px 16px !important;
    margin: 0 !important;
    transform: translateY(100%) !important;
    transition: transform 0.28s cubic-bezier(0.16, 1, 0.3, 1) !important;
  }

  .modal-overlay.active .mt5-modal-dialog,
  .modal-overlay.open .mt5-modal-dialog,
  .modal-overlay.active .modal-crypto-dialog,
  .modal-overlay.open .modal-crypto-dialog,
  .modal-overlay.active .modal-ai-dialog,
  .modal-overlay.active .ai-modal-dialog {
    transform: translateY(0) !important;
  }

  /* Drag handle indicator */
  .mt5-modal-dialog::before,
  .modal-crypto-dialog::before,
  .ai-modal-dialog::before {
    content: '';
    display: block;
    width: 38px;
    height: 4px;
    background: rgba(255, 255, 255, 0.25);
    border-radius: 2px;
    margin: 0 auto 10px auto;
  }

  .modal-header {
    padding-bottom: 8px !important;
  }

  .modal-title {
    font-size: 14px !important;
  }

  .mt5-prices-dual-box {
    grid-template-columns: 1fr 1fr !important;
    gap: 8px !important;
  }

  .bid-ask-box-item {
    padding: 8px 10px !important;
  }

  .bid-ask-box-item .price {
    font-size: 16px !important;
  }

  /* Prevent iOS zoom on input focus by keeping font-size >= 16px */
  .boleta-lot-input,
  .boleta-num-input,
  .boleta-select,
  input[type="text"],
  input[type="number"] {
    font-size: 16px !important;
  }

  .boleta-lot-stepper-box {
    height: 42px !important;
  }

  .lot-step-btn {
    height: 42px !important;
    min-width: 44px !important;
  }

  .quick-lot-chips {
    overflow-x: auto !important;
    -webkit-overflow-scrolling: touch !important;
    padding-bottom: 4px !important;
    scrollbar-width: none;
  }

  .quick-lot-chips::-webkit-scrollbar {
    display: none;
  }

  .lot-chip {
    flex-shrink: 0 !important;
    padding: 6px 12px !important;
  }

  .boleta-tpsl-grid {
    grid-template-columns: 1fr !important;
    gap: 10px !important;
  }

  .boleta-telemetry-summary {
    display: grid !important;
    grid-template-columns: 1fr 1fr !important;
    gap: 8px !important;
    padding: 10px !important;
  }

  .mt5-order-action-buttons {
    flex-direction: row !important;
    gap: 8px !important;
  }

  .btn-mt5-market-sell,
  .btn-mt5-market-buy {
    height: 48px !important;
    padding: 0 10px !important;
  }

  .btn-action-price {
    font-size: 13px !important;
  }
}

/* --------------------------------------------------------------------------
   4. DEMAIS VIEWS NO MOBILE (DASHBOARD, WALLET, CONTRATOS, EXTRATO, AI)
   -------------------------------------------------------------------------- */
@media (max-width: 768px) {
  /* Wrappers */
  .view-content-wrapper,
  .wallet-view-content,
  .contracts-view-content,
  .transactions-view-content,
  .ai-view-content,
  .support-view-content,
  .dash-view-wrapper {
    padding: 12px 10px !important;
    gap: 14px !important;
  }

  /* Dashboard Cards */
  .dash-cards-grid {
    grid-template-columns: 1fr !important;
    gap: 12px !important;
  }

  .telemetry-card {
    padding: 14px !important;
  }

  .card-huge-number {
    font-size: 24px !important;
  }

  .card-mini-grid {
    grid-template-columns: 1fr 1fr !important;
    gap: 8px !important;
  }

  .dash-shortcuts-bar {
    overflow-x: auto !important;
    -webkit-overflow-scrolling: touch !important;
    padding-bottom: 4px !important;
    scrollbar-width: none;
  }

  .dash-shortcuts-bar::-webkit-scrollbar {
    display: none;
  }

  .shortcut-pill-btn {
    flex-shrink: 0 !important;
    white-space: nowrap !important;
  }

  .dash-middle-grid {
    grid-template-columns: 1fr !important;
    gap: 14px !important;
  }

  /* Table Horizontal Scrolling on Mobile */
  .data-table-container,
  .panel-body,
  .contracts-table-wrapper,
  .transactions-table-wrapper {
    width: 100% !important;
    overflow-x: auto !important;
    -webkit-overflow-scrolling: touch !important;
  }

  .data-table {
    min-width: 580px !important;
  }

  /* Wallet Cards */
  .wallet-cards-grid,
  .custody-summary-grid {
    grid-template-columns: 1fr !important;
    gap: 12px !important;
  }

  .wallet-actions-row {
    flex-direction: column !important;
    gap: 8px !important;
  }

  .wallet-actions-row button {
    width: 100% !important;
  }

  /* AI Quant View */
  .ai-kpi-grid,
  .agents-grid {
    grid-template-columns: 1fr !important;
    gap: 12px !important;
  }
}
