/*
 * Ghizzo BI — camada exclusiva para telas móveis.
 * Todas as alterações de componentes existentes ficam restritas a <= 800px,
 * preservando integralmente o layout desktop.
 */

@media (max-width: 800px) {
  html {
    -webkit-text-size-adjust: 100%;
    scroll-padding-top: 72px;
  }

  body.app-authenticated {
    min-width: 0;
    overflow-x: hidden;
  }

  /* ---------- Navegação móvel / drawer ---------- */
  .mobile-menu-toggle[hidden] {
    display: grid !important;
  }

  .mobile-menu-toggle {
    flex: 0 0 40px;
    width: 40px;
    height: 40px;
    border: 1px solid rgba(255,255,255,.10);
    border-radius: 12px;
    background: #111a2b;
    place-content: center;
    gap: 4px;
    padding: 0;
    color: #dbe5f1;
    cursor: pointer;
  }

  .mobile-menu-toggle span {
    display: block;
    width: 18px;
    height: 2px;
    border-radius: 99px;
    background: currentColor;
  }

  .topbar,
  .sidebar-collapsed .topbar {
    height: 62px;
    margin-left: 0 !important;
    padding: 0 max(12px, env(safe-area-inset-right)) 0 max(12px, env(safe-area-inset-left));
    z-index: 40;
  }

  .topbar-left {
    min-width: 0;
    gap: 10px;
  }

  .topbar .brand {
    min-width: 0;
    gap: 8px;
  }

  .topbar .brand-mark {
    width: 34px;
    height: 34px;
    border-radius: 10px;
    font-size: 13px;
  }

  .topbar .brand strong {
    max-width: 128px;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    font-size: 15px;
  }

  .topbar .brand small,
  .topbar .user-pill {
    display: none !important;
  }

  .top-actions {
    flex: 0 0 auto;
    gap: 4px;
  }

  .top-actions form {
    margin: 0;
  }

  .top-actions .link-button {
    min-height: 40px;
    padding: 0 10px;
    border-radius: 10px;
    font-size: 12px;
  }

  .app-sidebar,
  .sidebar-collapsed .app-sidebar {
    width: min(300px, 88vw) !important;
    padding: 0 12px 22px !important;
    transform: translateX(-105%) !important;
    box-shadow: 14px 0 42px rgba(0,0,0,.38) !important;
    z-index: 60 !important;
    overflow-y: auto !important;
    overscroll-behavior: contain;
  }

  .app-sidebar .sidebar-head,
  .sidebar-collapsed .app-sidebar .sidebar-head {
    height: 62px;
    justify-content: flex-start !important;
  }

  .app-sidebar .sidebar-nav,
  .sidebar-collapsed .app-sidebar .sidebar-nav {
    display: grid !important;
    gap: 6px;
  }

  .app-sidebar .sidebar-link,
  .sidebar-collapsed .app-sidebar .sidebar-link {
    justify-content: flex-start !important;
    gap: 12px !important;
    min-height: 50px;
    padding: 9px 11px !important;
    overflow: visible;
    font-size: 14px;
  }

  .app-sidebar .sidebar-link > span:last-child,
  .sidebar-collapsed .app-sidebar .sidebar-link > span:last-child {
    width: auto !important;
    opacity: 1 !important;
    overflow: visible !important;
  }

  .sidebar-mobile-open {
    overflow: hidden !important;
  }

  .sidebar-mobile-open .app-sidebar,
  .sidebar-mobile-open.sidebar-collapsed .app-sidebar {
    transform: translateX(0) !important;
  }

  .sidebar-mobile-open .sidebar-backdrop {
    display: block !important;
    position: fixed;
    inset: 0 !important;
    z-index: 55 !important;
    border: 0;
    background: rgba(2,6,12,.68);
    backdrop-filter: blur(2px);
  }

  .app-main,
  .sidebar-collapsed .app-main {
    width: 100%;
    min-width: 0;
    margin-left: 0 !important;
    padding-top: 14px;
  }

  /* ---------- Estrutura geral ---------- */
  .page {
    width: 100%;
    max-width: none;
    padding: 14px max(12px, env(safe-area-inset-right)) calc(38px + env(safe-area-inset-bottom)) max(12px, env(safe-area-inset-left));
  }

  .flash-stack {
    margin-bottom: 12px;
  }

  .flash {
    padding: 11px 12px;
    border-radius: 11px;
    font-size: 12px;
    line-height: 1.4;
  }

  .hero-row,
  .split-head,
  .module-header {
    margin-bottom: 18px;
  }

  .hero-row h1,
  .split-head h1,
  .module-header h1,
  .panel-card h1 {
    font-size: 26px;
    line-height: 1.08;
  }

  .panel-card {
    padding: 16px;
    border-radius: 15px;
    box-shadow: none;
  }

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

  .module-card {
    min-height: 96px;
    grid-template-columns: 44px minmax(0,1fr) 18px;
    gap: 11px;
    padding: 14px;
    border-radius: 15px;
  }

  .module-icon {
    width: 42px;
    height: 42px;
    border-radius: 12px;
    font-size: 17px;
  }

  .module-card h3 {
    font-size: 14px;
  }

  .module-card p {
    font-size: 11px;
    line-height: 1.4;
  }

  .status-card {
    width: 100%;
    padding: 12px 13px;
  }

  /* ---------- Controles e toque ---------- */
  input,
  select,
  textarea,
  button {
    max-width: 100%;
  }

  input,
  select,
  textarea {
    font-size: 16px; /* evita zoom automático no iOS */
  }

  button,
  .secondary,
  .primary,
  .small-action,
  .settings-tab,
  .fin-tab,
  .demo-tab {
    touch-action: manipulation;
  }

  .secondary,
  .primary {
    min-height: 44px;
  }

  .admin-layout,
  .roles-layout,
  .perm-grid,
  .page-permission-grid,
  .role-tab-options {
    grid-template-columns: 1fr !important;
  }

  .reset-row {
    grid-template-columns: 1fr !important;
  }

  .reset-row .secondary,
  .permission-actions .primary {
    width: 100%;
  }

  /* ---------- Tabs: rolagem horizontal em vez de esmagar ---------- */
  .fin-tabs,
  .demo-tabs,
  .settings-tabs,
  .dre-view-tabs {
    width: 100% !important;
    max-width: 100%;
    display: flex !important;
    flex-wrap: nowrap !important;
    overflow-x: auto;
    overflow-y: hidden;
    scrollbar-width: none;
    -webkit-overflow-scrolling: touch;
    scroll-snap-type: x proximity;
  }

  .fin-tabs::-webkit-scrollbar,
  .demo-tabs::-webkit-scrollbar,
  .settings-tabs::-webkit-scrollbar,
  .dre-view-tabs::-webkit-scrollbar {
    display: none;
  }

  .fin-tab,
  .demo-tab,
  .settings-tab,
  .dre-view-tabs .demo-tab {
    flex: 0 0 auto !important;
    min-width: max-content !important;
    scroll-snap-align: start;
  }

  .fin-tab {
    min-width: 168px !important;
    padding: 8px 10px;
  }

  .fin-tab-copy small {
    display: none;
  }

  /* Financeiro: navegação principal fixa no rodapé.
     Só é aplicada em Pagar / Receber / Bancos; o desktop permanece intacto. */
  .fin-tabs.mobile-fin-primary-tabs {
    position: fixed;
    left: 50%;
    right: auto;
    bottom: 0;
    z-index: 52;
    width: min(100%, 560px) !important;
    max-width: 100%;
    height: calc(68px + env(safe-area-inset-bottom));
    margin: 0 !important;
    padding: 7px max(10px, env(safe-area-inset-right)) max(7px, env(safe-area-inset-bottom)) max(10px, env(safe-area-inset-left));
    transform: translateX(-50%);
    display: flex !important;
    align-items: stretch;
    justify-content: center;
    gap: 6px;
    overflow: visible !important;
    border: 0;
    border-top: 1px solid rgba(255,255,255,.10);
    border-radius: 18px 18px 0 0;
    background: rgba(9,15,25,.96);
    box-shadow: 0 -12px 32px rgba(0,0,0,.30);
    backdrop-filter: blur(18px);
  }

  .mobile-fin-primary-tabs .fin-tab {
    position: relative;
    flex: 1 1 0 !important;
    min-width: 0 !important;
    max-width: 96px;
    min-height: 50px;
    padding: 4px !important;
    display: grid;
    place-items: center;
    border: 1px solid transparent;
    border-radius: 13px;
    background: transparent;
    transform: none !important;
  }

  .mobile-fin-primary-tabs .fin-tab-copy {
    display: none !important;
  }

  .mobile-fin-primary-tabs .fin-tab.reports {
    display: none !important;
  }

  .mobile-fin-primary-tabs .fin-tab-icon {
    width: 40px;
    height: 40px;
    flex-basis: 40px;
    border-radius: 12px;
    background: rgba(255,255,255,.035);
    border-color: rgba(255,255,255,.07);
    color: #8394aa;
    transition: transform .18s ease, background .18s ease, border-color .18s ease, color .18s ease, box-shadow .18s ease;
  }

  .mobile-fin-primary-tabs .fin-tab-icon svg {
    width: 20px;
    height: 20px;
  }

  .mobile-fin-primary-tabs .fin-tab.active {
    border-color: rgba(245,158,11,.18);
    background: rgba(245,158,11,.08);
    box-shadow: inset 0 0 0 1px rgba(245,158,11,.04);
  }

  .mobile-fin-primary-tabs .fin-tab.active::before {
    content: "";
    position: absolute;
    top: -7px;
    left: 50%;
    width: 32px;
    height: 3px;
    border-radius: 0 0 999px 999px;
    background: #fbbf24;
    transform: translateX(-50%);
    box-shadow: 0 2px 10px rgba(251,191,36,.35);
  }

  .mobile-fin-primary-tabs .fin-tab.active .fin-tab-icon {
    color: #f8cf79;
    border-color: rgba(245,158,11,.34);
    background: rgba(245,158,11,.13);
    box-shadow: 0 5px 16px rgba(0,0,0,.22);
    transform: translateY(-1px);
  }

  body:has(.mobile-fin-primary-tabs) .app-main {
    padding-bottom: calc(86px + env(safe-area-inset-bottom));
  }

  /* ---------- Financeiro ---------- */
  .fin-header,
  .billing-header,
  .demo-header,
  .monitoring-head {
    gap: 9px;
    margin-bottom: 12px;
  }

  .fin-header h1,
  .billing-header h1,
  .demo-header h1 {
    font-size: 24px;
  }

  .fin-header p,
  .billing-header p,
  .demo-header p {
    font-size: 11px;
    line-height: 1.45;
  }

  .fin-live,
  .billing-source,
  .demo-header-status {
    width: 100%;
    min-height: 36px;
    justify-content: flex-start;
    border-radius: 11px;
  }

  .fin-filter-card,
  .billing-filter-card,
  .report-selector,
  .bank-summary {
    padding: 12px;
    border-radius: 14px;
  }

  .fin-quick-filters {
    display: grid;
    grid-template-columns: repeat(2,minmax(0,1fr));
    gap: 7px;
  }

  .fin-quick-filters button {
    min-height: 40px;
    padding: 7px 8px;
    white-space: normal;
  }

  .fin-filter-grid,
  .report-selector,
  .report-receipts-fields {
    grid-template-columns: 1fr !important;
  }

  .fin-filter-submit,
  .billing-refresh,
  .report-button,
  .bank-refresh,
  .bank-save,
  .dre-button {
    min-height: 44px;
  }

  .fin-filter-submit,
  .report-actions,
  .report-actions .report-button,
  .bank-actions,
  .bank-actions .bank-refresh,
  .bank-actions .bank-save {
    width: 100%;
  }

  .bank-actions {
    display: grid;
    grid-template-columns: 1fr 1fr;
  }

  .fin-kpis,
  .billing-kpis,
  .monitor-stats,
  .dre-kpis {
    grid-template-columns: repeat(2,minmax(0,1fr)) !important;
    gap: 8px;
  }

  .fin-kpi,
  .billing-kpi,
  .dre-kpis article,
  .monitor-stats article {
    min-width: 0;
    padding: 12px;
    border-radius: 13px;
  }

  .fin-kpi strong,
  .billing-kpi > strong,
  .monitor-stats strong {
    font-size: 18px;
    overflow-wrap: anywhere;
  }

  .fin-kpi span,
  .billing-kpi > span,
  .dre-kpis span,
  .monitor-stats span {
    font-size: 8px;
    line-height: 1.35;
  }

  .fin-charts-grid,
  .dre-result-grid {
    grid-template-columns: 1fr !important;
  }

  .fin-panel,
  .billing-panel,
  .dre-panel,
  .report-payables,
  .report-receipts,
  .report-table-wrap {
    border-radius: 14px;
  }

  .fin-panel-head,
  .dre-panel-head,
  .report-payables-head,
  .report-receipts-head {
    padding: 12px;
  }

  .fin-daily-chart {
    height: 220px;
    padding: 14px 10px 10px;
    scroll-snap-type: x proximity;
  }

  .fin-day-bar {
    flex-basis: 46px;
    scroll-snap-align: start;
  }

  .fin-chart-tooltip {
    max-width: calc(100vw - 24px);
    white-space: normal;
  }

  /* tabelas continuam completas, mas com rolagem previsível no touch */
  .table-wrap,
  .fin-table-wrap,
  .billing-table-wrap,
  .report-table-wrap,
  .report-payables-list,
  .report-receipts-list,
  .dre-table-wrap,
  .result-table-wrap,
  .result-detail-wrap,
  .sql-result-wrap {
    max-width: 100%;
    overflow-x: auto !important;
    -webkit-overflow-scrolling: touch;
    overscroll-behavior-x: contain;
  }

  .fin-table-wrap,
  .result-detail-wrap {
    max-height: 62vh;
  }

  .fin-table th,
  .fin-table td,
  .report-table th,
  .report-table td,
  .report-payables-table th,
  .report-payables-table td,
  .dre-table th,
  .dre-table td,
  .result-table th,
  .result-table td {
    padding-top: 10px;
    padding-bottom: 10px;
  }

  .fin-table th:first-child,
  .fin-table td:first-child,
  .billing-table th:first-child,
  .billing-table td:first-child,
  .report-table th:first-child,
  .report-table td:first-child,
  .report-payables-table th:first-child,
  .report-payables-table td:first-child,
  .dre-table th:first-child,
  .dre-table td:first-child,
  .result-table th:first-child,
  .result-table td:first-child {
    position: sticky;
    left: 0;
    z-index: 2;
  }

  .fin-table td:first-child,
  .billing-table td:first-child,
  .report-table td:first-child,
  .report-payables-table td:first-child,
  .dre-table td:first-child,
  .result-table td:first-child {
    background: #0f1724;
  }

  .fin-table th:first-child,
  .billing-table th:first-child,
  .report-table th:first-child,
  .report-payables-table th:first-child,
  .dre-table th:first-child,
  .result-table th:first-child {
    z-index: 4;
    background: #172235;
  }

  /* ---------- DRE ---------- */
  .dre-result-controls,
  .dre-detail-filters,
  .dre-filters,
  .dre-bulk {
    width: 100%;
    grid-template-columns: 1fr !important;
  }

  .dre-result-controls label,
  .dre-result-controls select,
  .dre-detail-filters input,
  .dre-detail-filters select,
  .dre-filters input,
  .dre-filters select,
  .dre-category-select {
    width: 100%;
    min-width: 0;
  }

  .dre-result-controls .dre-button,
  .dre-panel-head .dre-button,
  .dre-savebar .dre-button {
    width: 100%;
  }

  .dre-statement {
    padding: 6px 12px 12px;
  }

  .dre-statement-row {
    min-height: 45px;
    gap: 8px;
  }

  .dre-statement-row > span {
    min-width: 0;
    line-height: 1.35;
  }

  .dre-statement-row > div {
    flex: 0 0 auto;
    align-items: flex-end;
    flex-direction: column;
    gap: 1px;
  }

  .dre-category-bars {
    padding: 12px;
  }

  .dre-category-legend {
    overflow-x: auto;
    flex-wrap: nowrap;
    -webkit-overflow-scrolling: touch;
  }

  .dre-category-legend span {
    flex: 0 0 auto;
  }

  .dre-savebar {
    position: sticky;
    bottom: env(safe-area-inset-bottom);
    z-index: 6;
    padding: 11px 12px;
  }

  /* ---------- Contas bancárias / relatórios ---------- */
  .bank-grid {
    grid-template-columns: 1fr !important;
    gap: 9px;
  }

  .bank-card {
    padding: 14px;
  }

  .bank-balance {
    font-size: 19px;
  }

  .report-actions {
    display: grid;
    grid-template-columns: 1fr 1fr;
  }

  .report-payables-head,
  .report-receipts-head {
    align-items: stretch;
    flex-direction: column;
  }

  .report-payables-total,
  .report-receipts-total {
    width: 100%;
    text-align: left;
  }

  .report-pdf-form .report-button {
    width: 100%;
  }

  /* ---------- Faturamento ---------- */
  .billing-filter-card,
  .billing-panel-head {
    align-items: stretch;
    flex-direction: column;
  }

  .billing-filter-card label {
    min-width: 0;
    width: 100%;
  }

  .billing-filter-card select,
  .billing-refresh {
    width: 100%;
  }

  .billing-panel {
    padding: 12px;
  }

  .billing-chart {
    min-height: 220px;
    grid-template-columns: repeat(12, minmax(54px,1fr));
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    scroll-snap-type: x proximity;
  }

  .billing-bar-item {
    height: 200px;
    scroll-snap-align: start;
  }

  /* ---------- Configurações / monitoria ---------- */
  .settings-tabs {
    margin-top: 0;
  }

  .settings-tab {
    min-height: 40px;
    padding: 9px 12px;
    font-size: 11px;
  }

  .permissions-panel,
  .role-create-panel,
  .roles-list-panel,
  .monitoring-panel {
    padding: 14px;
  }

  .permissions-head,
  .permissions-shortcut,
  .permission-user-head,
  .monitor-section-head {
    align-items: stretch;
    flex-direction: column;
  }

  .monitoring-head-actions,
  .monitor-filters,
  .monitor-pagination,
  .monitor-pagination-actions {
    width: 100%;
  }

  .monitoring-head-actions,
  .monitor-pagination-actions {
    justify-content: space-between;
  }

  .monitor-filters {
    display: grid;
    grid-template-columns: 1fr;
  }

  .monitor-filters select {
    width: 100%;
    min-width: 0;
    min-height: 42px;
  }

  /* ---------- Console SQL: usável, mas sem alterar desktop ---------- */
  .sql-page.app-main {
    padding-top: 0;
  }

  .sql-page {
    min-height: calc(100dvh - 62px);
  }

  .sql-workbench {
    height: auto;
    min-height: calc(100dvh - 62px);
    grid-template-columns: 1fr !important;
    grid-template-rows: 170px minmax(680px,auto) !important;
  }

  .sql-explorer {
    border-right: 0;
    border-bottom: 1px solid #26344b;
  }

  .sql-explorer-head {
    padding: 10px 12px 8px;
  }

  .sql-search-wrap {
    padding: 7px 10px;
  }

  .sql-workspace {
    grid-template-rows: 46px 270px 360px !important;
  }

  .sql-editor-toolbar {
    overflow-x: auto;
  }

  .sql-editor {
    padding: 11px 12px;
    font-size: 12px;
  }

  .sql-results-toolbar {
    min-width: 0;
  }

  .sql-results-title span {
    max-width: 110px;
  }
}

@media (max-width: 430px) {
  .fin-kpis,
  .billing-kpis,
  .monitor-stats,
  .dre-kpis {
    grid-template-columns: 1fr !important;
  }

  .bank-actions,
  .report-actions,
  .fin-quick-filters {
    grid-template-columns: 1fr;
  }

  .topbar .brand strong {
    max-width: 95px;
  }

  .dre-statement-row {
    align-items: flex-start;
  }
}
