/* ============================================================
   MOBILE RESPONSIVE — Isolated Mobile & Tablet CSS (< 1024px)
   Carregado DEPOIS de styles.css.
   ============================================================ */

/* --- 1. Base & Viewport Setup (< 1024px) --- */
@media (max-width: 1023px) {
  html,
  body {
    width: 100% !important;
    max-width: 100vw !important;
    min-height: 100dvh;
    margin: 0;
    padding: 0;
    overflow-x: hidden !important;
    position: relative;
    background: #f8fafc !important;
  }

  /* Ocultar decorações pesadas de fundo no mobile */
  .bg-decor {
    display: none !important;
  }

  /* Container Principal 100% Tela travado horizontalmente */
  .app {
    width: 100% !important;
    max-width: 100vw !important;
    min-height: 100dvh !important;
    height: 100dvh !important;
    margin: 0 !important;
    padding: 0 !important;
    overflow-x: hidden !important;
    overflow-y: hidden !important;
    position: static !important;
    z-index: auto !important;
    display: flex !important;
    flex-direction: column !important;
    box-sizing: border-box !important;
  }

  .frame {
    width: 100% !important;
    max-width: 100vw !important;
    min-height: 100dvh !important;
    height: 100dvh !important;
    border-radius: 0 !important;
    border: none !important;
    box-shadow: none !important;
    backdrop-filter: none !important;
    -webkit-backdrop-filter: none !important;
    position: static !important;
    z-index: auto !important;
    display: flex !important;
    flex-direction: column !important;
    overflow-x: hidden !important;
    overflow-y: hidden !important;
    background: #ffffff !important;
    box-sizing: border-box !important;
  }

  /* Área Principal e Views */
  .main {
    flex: 1 !important;
    width: 100% !important;
    max-width: 100vw !important;
    height: calc(100dvh - 56px) !important;
    min-height: 0 !important;
    display: flex !important;
    flex-direction: column !important;
    overflow-x: hidden !important;
    overflow-y: hidden !important;
    background: #ffffff !important;
    box-sizing: border-box !important;
  }

  .view {
    display: none !important;
    flex: 1 !important;
    width: 100% !important;
    max-width: 100% !important;
    height: 100% !important;
    min-height: 0 !important;
    overflow-y: auto !important;
    overflow-x: hidden !important;
    -webkit-overflow-scrolling: touch !important;
    padding: 16px 12px 40px 12px !important;
    box-sizing: border-box !important;
    position: relative !important;
  }

  .view.active:not([hidden]) {
    display: flex !important;
    flex-direction: column !important;
  }

  .view[hidden],
  .view:not(.active),
  [hidden] {
    display: none !important;
  }

  /* --- 2. Header Mobile com Menu Aparente --- */
  .content-header {
    height: 56px !important;
    min-height: 56px !important;
    max-height: 56px !important;
    padding: 0 14px !important;
    background: #ffffff !important;
    border-bottom: 1px solid var(--border, #e5e7eb) !important;
    display: flex !important;
    align-items: center !important;
    justify-content: space-between !important;
    z-index: 50 !important;
  }

  .header-left {
    display: flex !important;
    align-items: center !important;
    gap: 10px !important;
    min-width: 0 !important;
  }

  .mobile-menu-btn {
    display: flex !important;
    align-items: center !important;
    justify-content: center !important;
    width: 42px !important;
    height: 42px !important;
    min-width: 42px !important;
    min-height: 42px !important;
    border-radius: 10px !important;
    background: var(--bg-2, #f3f4f6) !important;
    border: 1px solid var(--border, #e5e7eb) !important;
    color: var(--text-1, #111827) !important;
    cursor: pointer !important;
    touch-action: manipulation !important;
    padding: 0 !important;
    flex-shrink: 0 !important;
  }

  .mobile-menu-btn .icon {
    width: 22px !important;
    height: 22px !important;
  }

  .content-title {
    font-size: 1.1rem !important;
    font-weight: 700 !important;
    white-space: nowrap !important;
    overflow: hidden !important;
    text-overflow: ellipsis !important;
  }

  .header-actions {
    display: flex !important;
    align-items: center !important;
    gap: 8px !important;
    flex-shrink: 0 !important;
  }

  .icon-btn {
    width: 38px !important;
    height: 38px !important;
    min-width: 38px !important;
    min-height: 38px !important;
    border-radius: 10px !important;
  }

  /* --- 3. Sidebar Drawer & Backdrop Mobile --- */
  .sidebar {
    position: fixed !important;
    top: 0 !important;
    left: 0 !important;
    bottom: 0 !important;
    width: 285px !important;
    max-width: 82vw !important;
    height: 100dvh !important;
    background: #ffffff !important;
    z-index: 10000 !important;
    transform: translateX(-100%) !important;
    transition: transform 0.28s cubic-bezier(0.4, 0, 0.2, 1) !important;
    box-shadow: 0 0 35px rgba(0, 0, 0, 0.25) !important;
    display: flex !important;
    flex-direction: column !important;
    overflow-y: auto !important;
    -webkit-overflow-scrolling: touch !important;
    padding: 16px 12px !important;
    border-right: 1px solid var(--border, #e5e7eb) !important;
    box-sizing: border-box !important;
    pointer-events: none !important;
  }

  .frame.sidebar-open .sidebar,
  #dashboard-screen.sidebar-open .sidebar,
  body.sidebar-active .sidebar {
    transform: translateX(0) !important;
    pointer-events: auto !important;
  }

  .mobile-backdrop {
    display: none;
    position: fixed !important;
    inset: 0 !important;
    background: rgba(15, 23, 42, 0.5) !important;
    backdrop-filter: blur(4px) !important;
    -webkit-backdrop-filter: blur(4px) !important;
    z-index: 9990 !important;
    touch-action: manipulation !important;
    pointer-events: none !important;
    opacity: 0;
    transition: opacity 0.25s ease;
  }

  .frame.sidebar-open ~ .mobile-backdrop,
  #dashboard-screen.sidebar-open ~ .mobile-backdrop,
  body.sidebar-active .mobile-backdrop {
    display: block !important;
    opacity: 1 !important;
    pointer-events: auto !important;
  }

  .sidebar .nav-item {
    min-height: 44px !important;
    padding: 10px 14px !important;
    font-size: 14px !important;
    border-radius: 10px !important;
    margin-bottom: 3px !important;
    cursor: pointer !important;
    touch-action: manipulation !important;
  }

  .sidebar .nav-item .icon {
    width: 20px !important;
    height: 20px !important;
  }

  .mobile-close-btn {
    display: flex !important;
    align-items: center !important;
    justify-content: center !important;
    width: 36px !important;
    height: 36px !important;
    min-width: 36px !important;
    border-radius: 8px !important;
    background: var(--bg-2, #f3f4f6) !important;
    cursor: pointer !important;
    touch-action: manipulation !important;
  }

  /* --- 4. Dashboard & Chat Container Mobile --- */
  #view-dashboard.active:not([hidden]) {
    height: 100% !important;
    max-height: 100% !important;
    display: flex !important;
    flex-direction: column !important;
    padding: 0 !important;
    overflow: hidden !important;
  }

  #view-dashboard:not(.active),
  #view-dashboard[hidden] {
    display: none !important;
  }

  .dash-wrap {
    flex: 1 !important;
    height: 100% !important;
    max-height: 100% !important;
    min-height: 0 !important;
    width: 100% !important;
    max-width: 100% !important;
    padding: 10px 10px 14px 10px !important;
    display: flex !important;
    flex-direction: column !important;
    justify-content: flex-start !important;
    align-items: stretch !important;
    box-sizing: border-box !important;
    overflow: hidden !important;
  }

  .dash-wrap:not(.chat-active) {
    justify-content: center !important;
    align-items: center !important;
    overflow-y: auto !important;
  }

  .dash-wrap.chat-active .orb,
  .dash-wrap.chat-active .dash-heading,
  .dash-wrap.chat-active .dash-sub {
    display: none !important;
  }

  .dash-wrap .orb {
    width: 52px !important;
    height: 52px !important;
    margin: 0 auto 10px auto !important;
    flex-shrink: 0 !important;
  }

  .dash-heading {
    font-size: 1.25rem !important;
    line-height: 1.25 !important;
    margin: 0 0 6px 0 !important;
    font-weight: 800 !important;
    text-align: center !important;
    flex-shrink: 0 !important;
  }

  .dash-sub {
    font-size: 13px !important;
    line-height: 1.45 !important;
    color: var(--text-2, #6b7280) !important;
    margin: 0 0 16px 0 !important;
    max-width: 95% !important;
    text-align: center !important;
    flex-shrink: 0 !important;
  }

  /* Chat Messages Container estritamente contido com scroll vertical */
  .chat-messages-container {
    flex: 1 !important;
    height: 100% !important;
    min-height: 0 !important;
    width: 100% !important;
    max-width: 100% !important;
    margin: 0 0 8px 0 !important;
    display: flex !important;
    flex-direction: column !important;
    gap: 12px !important;
    padding: 4px 2px 8px 2px !important;
    overflow-x: hidden !important;
    overflow-y: auto !important;
    -webkit-overflow-scrolling: touch !important;
    box-sizing: border-box !important;
  }

  .dash-wrap:not(.chat-active) .chat-messages-container {
    flex: 0 1 auto !important;
    max-height: 280px !important;
  }

  .message {
    display: flex !important;
    width: 100% !important;
    max-width: 100% !important;
    min-width: 0 !important;
    gap: 8px !important;
    box-sizing: border-box !important;
  }

  .message-avatar {
    width: 28px !important;
    height: 28px !important;
    min-width: 28px !important;
    min-height: 28px !important;
    flex-shrink: 0 !important;
  }

  .message-content {
    min-width: 0 !important;
    max-width: 100% !important;
    flex: 1 1 auto !important;
    padding: 12px 14px !important;
    font-size: 13.5px !important;
    line-height: 1.55 !important;
    border-radius: 14px !important;
    box-sizing: border-box !important;
    word-break: break-word !important;
    overflow-wrap: break-word !important;
    overflow-x: hidden !important;
  }

  .message.user .message-content {
    max-width: 85% !important;
    flex: 0 1 auto !important;
  }

  .message.assistant .message-content {
    max-width: calc(100% - 36px) !important;
    width: 100% !important;
    flex: 1 1 auto !important;
  }

  /* Scroll lateral isolado para tabelas Markdown no Chat & Histórico */
  .table-responsive {
    display: block !important;
    width: 100% !important;
    max-width: 100% !important;
    overflow-x: auto !important;
    -webkit-overflow-scrolling: touch !important;
    overscroll-behavior-x: contain !important;
    margin: 10px 0 !important;
    border: 1px solid var(--border, #e5e7eb) !important;
    border-radius: 10px !important;
    background: #ffffff !important;
    box-sizing: border-box !important;
    position: relative !important;
  }

  .table-responsive table,
  .message-content table,
  .history-msg-body table,
  .chat-table {
    display: table !important;
    width: 100% !important;
    min-width: 480px !important;
    max-width: none !important;
    margin: 0 !important;
    border: none !important;
    border-collapse: collapse !important;
  }

  .table-responsive table th,
  .table-responsive table td,
  .message-content table th,
  .message-content table td,
  .history-msg-body table th,
  .history-msg-body table td,
  .chat-table th,
  .chat-table td {
    padding: 8px 12px !important;
    font-size: 12.5px !important;
    white-space: nowrap !important;
    text-align: left !important;
  }

  /* --- 4.1 Gráficos & Donut Charts Mobile (-20% font-size e scroll responsivo) --- */
  .chart-responsive {
    display: block !important;
    width: 100% !important;
    max-width: 100% !important;
    overflow-x: auto !important;
    -webkit-overflow-scrolling: touch !important;
    overscroll-behavior-x: contain !important;
    margin: 8px 0 !important;
    box-sizing: border-box !important;
  }

  .chart-card,
  .message-content div[style*="border-radius: 12px"] {
    max-width: 100% !important;
    width: 100% !important;
    padding: 10px 12px !important;
    box-sizing: border-box !important;
  }

  /* Redução de ~20% no font-size e espaçamentos dos gráficos no Mobile */
  .chart-card div[style*="font-size: 15px"],
  .chart-card div[style*="font-size: 13.5px"],
  .message-content div[style*="border-radius: 12px"] > div:first-child {
    font-size: 12px !important;
    margin-bottom: 8px !important;
  }

  .chart-card div[style*="font-size: 13px"],
  .chart-card div[style*="font-size: 12px"],
  .chart-card div[style*="font-size: 11.5px"],
  .chart-card div[style*="font-size: 11px"],
  .message-content div[style*="border-radius: 12px"] div[style*="justify-content: space-between"] {
    font-size: 10.5px !important;
    gap: 4px !important;
  }

  .chart-card div[style*="justify-content: space-between"] span,
  .message-content div[style*="border-radius: 12px"] div[style*="justify-content: space-between"] span {
    font-size: 10.5px !important;
  }

  .chart-card svg,
  .message-content div[style*="border-radius: 12px"] svg {
    max-width: 125px !important;
    max-height: 125px !important;
    margin: 0 auto !important;
  }

  .chart-card div[style*="width: 160px"],
  .chart-card div[style*="width: 140px"],
  .message-content div[style*="border-radius: 12px"] div[style*="width: 160px"],
  .message-content div[style*="border-radius: 12px"] div[style*="width: 140px"] {
    width: 125px !important;
    height: 125px !important;
    margin: 0 auto !important;
  }

  /* Composer de Pergunta Mobile */
  .composer {
    width: 100% !important;
    max-width: 100% !important;
    min-width: 0 !important;
    margin: 0 !important;
    padding: 12px 14px !important;
    border-radius: 16px !important;
    background: #ffffff !important;
    border: 1px solid var(--border, #e5e7eb) !important;
    box-shadow: 0 6px 20px rgba(0, 0, 0, 0.06) !important;
    box-sizing: border-box !important;
  }

  .composer-row {
    display: flex !important;
    align-items: flex-start !important;
    gap: 10px !important;
    margin-bottom: 10px !important;
  }

  .composer-sparkle {
    width: 20px !important;
    height: 20px !important;
    margin-top: 2px !important;
    flex-shrink: 0 !important;
    color: var(--accent, #6366f1) !important;
  }

  .composer-input {
    font-size: 16px !important; /* Previne auto-zoom no iOS */
    line-height: 1.4 !important;
    min-height: 44px !important;
    max-height: 120px !important;
    width: 100% !important;
    padding: 4px 0 !important;
    color: var(--text-1, #111827) !important;
  }

  .composer-footer {
    display: flex !important;
    justify-content: space-between !important;
    align-items: center !important;
    padding-top: 8px !important;
    border-top: 1px solid var(--border-soft, #f3f4f6) !important;
  }

  .tool-btn {
    width: 38px !important;
    height: 38px !important;
    min-width: 38px !important;
    min-height: 38px !important;
    border-radius: 10px !important;
    display: flex !important;
    align-items: center !important;
    justify-content: center !important;
  }

  .send-btn {
    width: 38px !important;
    height: 38px !important;
    min-width: 38px !important;
    min-height: 38px !important;
    border-radius: 10px !important;
    display: flex !important;
    align-items: center !important;
    justify-content: center !important;
    background: #111827 !important;
    color: #ffffff !important;
  }

  /* --- 5. Inputs & Prevenção de Zoom iOS --- */
  input,
  textarea,
  select {
    font-size: 16px !important;
  }

  /* --- 6. Harmonização de Cards e Seções de Views (< 1024px) --- */
  .view-header {
    margin-bottom: 16px !important;
  }

  .section-title {
    font-size: 1.2rem !important;
  }

  .dropzone {
    padding: 24px 16px !important;
    border-radius: 14px !important;
    margin-bottom: 18px !important;
  }

  .kb-header {
    flex-direction: column !important;
    align-items: flex-start !important;
    gap: 12px !important;
  }

  .kb-actions {
    width: 100% !important;
    justify-content: flex-start !important;
  }

  /* Tabelas Administrativas em formato Card no Mobile */
  .table-scroll {
    overflow-x: visible !important;
    border: none !important;
    border-radius: 0 !important;
    width: 100% !important;
    max-width: 100% !important;
    background: transparent !important;
  }

  .table-scroll table,
  .kb-table,
  .users-table,
  .history-table,
  #csv-imports-table {
    width: 100% !important;
    min-width: 0 !important;
    max-width: 100% !important;
    display: block !important;
  }

  .kb-table thead,
  .users-table thead,
  .history-table thead,
  #csv-imports-table thead {
    display: none !important;
  }

  .kb-table tbody,
  .users-table tbody,
  .history-table tbody,
  #csv-imports-table tbody {
    display: flex !important;
    flex-direction: column !important;
    gap: 12px !important;
    width: 100% !important;
    min-width: 0 !important;
    max-width: 100% !important;
  }

  .kb-table tbody tr,
  .users-table tbody tr,
  .history-table tbody tr,
  #csv-imports-table tbody tr {
    display: flex !important;
    flex-direction: column !important;
    background: var(--bg-2, #f8fafc) !important;
    border: 1px solid var(--border, #e5e7eb) !important;
    border-radius: 12px !important;
    padding: 14px !important;
    gap: 8px !important;
    box-sizing: border-box !important;
    width: 100% !important;
    min-width: 0 !important;
    max-width: 100% !important;
  }

  .kb-table tbody td,
  .users-table tbody td,
  .history-table tbody td,
  #csv-imports-table tbody td {
    display: flex !important;
    justify-content: space-between !important;
    align-items: center !important;
    padding: 6px 0 !important;
    border-bottom: 1px solid var(--border-soft, #f1f5f9) !important;
    font-size: 13.5px !important;
    width: 100% !important;
    min-width: 0 !important;
    box-sizing: border-box !important;
    color: var(--text-1, #111827) !important;
    word-break: break-word !important;
  }

  .kb-table tbody td:last-child,
  .users-table tbody td:last-child,
  .history-table tbody td:last-child,
  #csv-imports-table tbody td:last-child {
    border-bottom: none !important;
    padding-top: 8px !important;
    justify-content: flex-end !important;
    gap: 8px !important;
  }

  .kb-table tbody td::before,
  .users-table tbody td::before,
  .history-table tbody td::before,
  #csv-imports-table tbody td::before {
    content: attr(data-label) !important;
    font-weight: 600 !important;
    font-size: 12px !important;
    color: var(--text-3, #9ca3af) !important;
    text-transform: uppercase !important;
    margin-right: 12px !important;
    flex-shrink: 0 !important;
  }

  /* Configurações (Settings) Mobile */
  .settings-section {
    padding: 0 !important;
    max-width: 100% !important;
  }

  .settings-card-body {
    grid-template-columns: 1fr !important;
  }

  .settings-card-header {
    flex-direction: column !important;
    align-items: flex-start !important;
    gap: 8px !important;
  }

  .settings-actions {
    flex-direction: column !important;
    gap: 10px !important;
  }

  .settings-actions .btn {
    width: 100% !important;
    min-height: 44px !important;
  }

  /* Modais no Mobile */
  .modal {
    width: 92% !important;
    max-width: 440px !important;
    margin: 16px !important;
    padding: 20px 16px !important;
    border-radius: 16px !important;
  }

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

  .modal-actions .btn {
    width: 100% !important;
    min-height: 44px !important;
  }

  /* --- 7. Analytics Mobile Responsive (< 1024px) --- */
  .analytics-view {
    padding: 0 !important;
    gap: 16px !important;
  }

  .analytics-header {
    flex-direction: column !important;
    align-items: flex-start !important;
    gap: 8px !important;
  }

  .analytics-filter-card {
    flex-direction: column !important;
    align-items: stretch !important;
    padding: 14px !important;
    gap: 12px !important;
  }

  .filter-group-presets {
    width: 100% !important;
    overflow-x: auto !important;
    flex-wrap: nowrap !important;
    padding-bottom: 4px !important;
  }

  .filter-group-presets .btn-preset {
    white-space: nowrap !important;
    flex-shrink: 0 !important;
  }

  .filter-group-dates {
    width: 100% !important;
    flex-direction: column !important;
    align-items: stretch !important;
    gap: 8px !important;
  }

  .date-field {
    width: 100% !important;
    justify-content: space-between !important;
  }

  .date-field input {
    flex: 1 !important;
  }

  .btn-filter-action,
  .btn-refresh-action {
    width: 100% !important;
    justify-content: center !important;
    min-height: 40px !important;
  }

  .analytics-kpis-grid {
    grid-template-columns: repeat(2, 1fr) !important;
    gap: 10px !important;
  }

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

  .editorial-chart-card.full-width {
    grid-column: span 1 !important;
  }

  .editorial-card-header {
    flex-direction: column !important;
    align-items: flex-start !important;
    gap: 10px !important;
  }

  .chart-toggle-group {
    width: 100% !important;
  }

  .chart-toggle-btn {
    flex: 1 !important;
    justify-content: center !important;
  }

  .editorial-donut-wrap {
    flex-direction: column !important;
    align-items: center !important;
  }
}
