/* ================================================================
   ObraAnalytics — MOBILE & TABLET RESPONSIVE CSS v2.0
   Mobile-First Approach com Bottom Navigation Bar
   Breakpoints:
     📱 Mobile   : max-width 640px
     📱 Mobile+  : max-width 768px
     💻 Tablet   : max-width 1024px
     🖥️ Desktop  : > 1024px (estilos padrão em style.css)
   ================================================================ */

/* ── Touch improvements globais ───────────────────────────── */
* {
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
}

/* ================================================================
   BOTTOM NAV BAR — Navegação inferior para mobile
   Substitui a sidebar em telas pequenas
   ================================================================ */
.bottom-nav {
  display: none; /* Oculta no desktop */
}

@media (max-width: 768px) {
  .bottom-nav {
    display: flex;
    position: fixed;
    bottom: 0;
    left: 0;
    right: 0;
    z-index: 400;
    background: var(--bg-surface);
    border-top: 1px solid var(--border);
    padding: 0;
    padding-bottom: env(safe-area-inset-bottom, 0px);
    box-shadow: 0 -4px 20px rgba(0,0,0,0.3);
  }

  .bottom-nav-item {
    flex: 1;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    padding: 8px 4px;
    gap: 3px;
    cursor: pointer;
    border: none;
    background: transparent;
    color: var(--text-muted);
    font-size: 10px;
    font-weight: 500;
    font-family: 'Inter', sans-serif;
    transition: color 0.2s, background 0.2s;
    min-height: 56px;
    text-decoration: none;
    position: relative;
  }

  .bottom-nav-item i {
    font-size: 18px;
    line-height: 1;
  }

  .bottom-nav-item span {
    font-size: 9px;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    max-width: 56px;
  }

  .bottom-nav-item.active {
    color: var(--accent-blue);
  }

  .bottom-nav-item.active::before {
    content: '';
    position: absolute;
    top: 0;
    left: 20%;
    right: 20%;
    height: 2px;
    background: var(--accent-blue);
    border-radius: 0 0 2px 2px;
  }

  .bottom-nav-item .nav-badge {
    position: absolute;
    top: 6px;
    right: calc(50% - 18px);
    background: var(--accent-red);
    color: white;
    font-size: 8px;
    font-weight: 700;
    min-width: 14px;
    height: 14px;
    border-radius: 7px;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 0 3px;
  }

  /* Botão "Mais" na bottom nav abre sidebar completa */
  .bottom-nav-more {
    position: relative;
  }
}

/* ================================================================
   TABLET — max 1024px
   ================================================================ */
@media (max-width: 1024px) {

  /* Sidebar vira overlay/drawer completo */
  .sidebar {
    transform: translateX(-100%);
    z-index: 500;
    box-shadow: 6px 0 40px rgba(0,0,0,0.7);
    width: 280px !important;
    transition: transform 0.3s cubic-bezier(0.4,0,0.2,1);
    /* Garante que brand e textos apareçam no drawer */
    overflow-y: auto;
  }

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

  /* Garante que a brand apareça no drawer */
  .sidebar .sidebar-brand {
    display: flex !important;
    align-items: center;
    gap: 12px;
    padding: 20px 20px 16px !important;
    border-bottom: 1px solid rgba(255,255,255,0.08);
    margin-bottom: 4px;
  }
  .sidebar .brand-text {
    display: flex !important;
    flex-direction: column;
  }
  .sidebar .brand-name {
    display: inline !important;
    font-size: 16px;
    font-weight: 800;
  }
  .sidebar .brand-tagline {
    display: inline !important;
    font-size: 10px;
  }

  /* Nav items com texto no drawer */
  .sidebar .nav-item {
    display: flex !important;
    align-items: center;
    justify-content: flex-start !important;
    padding: 13px 20px !important;
    gap: 13px !important;
    font-size: 14px;
    font-weight: 500;
  }
  .sidebar .nav-item span:not(.badge) {
    display: inline !important;
    flex: 1;
  }
  .sidebar .nav-item i {
    min-width: 20px;
    text-align: center;
    font-size: 16px;
  }
  .sidebar .badge {
    display: inline-flex !important;
  }

  /* Footer da sidebar visível no drawer */
  .sidebar .sidebar-footer {
    display: flex !important;
    flex-direction: column;
    gap: 8px;
    padding: 16px 20px !important;
  }
  .sidebar .status-indicator { display: flex !important; }
  .sidebar #status-text { display: inline !important; }

  /* Main ocupa tela toda */
  .main-content {
    margin-left: 0 !important;
  }
  .main-content.sidebar-collapsed {
    margin-left: 0 !important;
  }

  /* Overlay escuro quando sidebar aberta */
  .sidebar-overlay {
    display: none;
    position: fixed;
    inset: 0;
    background: rgba(0,0,0,0.6);
    z-index: 299;
    backdrop-filter: blur(3px);
    -webkit-backdrop-filter: blur(3px);
    transition: opacity 0.25s;
  }
  .sidebar-overlay.active {
    display: block;
    animation: overlayFadeIn 0.25s ease forwards;
  }
  @keyframes overlayFadeIn {
    from { opacity: 0; }
    to   { opacity: 1; }
  }

  /* Topbar: compacta */
  .topbar { padding: 0 16px; }
  .topbar-stat .stat-label { display: none; }
  .topbar-stat { padding: 5px 8px; font-size: 11px; }

  /* Botão Baixar Nuvem: só ícone + texto curto */
  #btnBaixarNuvem .btn-text { display: none; }

  /* Tabs: padding reduzido */
  .tab-content { padding: 16px; }

  /* KPI grids em 2 colunas */
  .kpi-grid,
  .stats-grid,
  .summary-grid {
    grid-template-columns: repeat(2, 1fr) !important;
  }

  /* Charts: 1 coluna */
  .charts-grid { grid-template-columns: 1fr !important; }

  /* Orçamento: painel lateral empilhado */
  #tab-orcamento > div {
    flex-direction: column !important;
  }
  #orcaPainelBusca {
    width: 100% !important;
    max-width: 100% !important;
    border-right: none !important;
    border-bottom: 1px solid var(--border);
    max-height: 48vh;
    overflow-y: auto;
  }
  #orcaPainelPlanilha {
    overflow-y: auto;
  }
}

/* ================================================================
   MOBILE+ — max 768px
   ================================================================ */
@media (max-width: 768px) {

  /* Espaço para bottom nav */
  .main-content {
    padding-bottom: calc(64px + env(safe-area-inset-bottom, 0px)) !important;
    margin-left: 0 !important; /* Sem sidebar fixa no mobile */
  }

  /* Font base menor */
  html { font-size: 13px; }

  /* ── TOPBAR MOBILE ── */
  .topbar {
    height: 56px;
    padding: 0 12px;
    gap: 8px;
  }
  /* Título da página: mostra mas compacto */
  .page-title {
    font-size: 14px;
    font-weight: 700;
    flex: 1;
    min-width: 0;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
  }
  .topbar-left { gap: 8px; flex: 1; min-width: 0; }
  .topbar-right { gap: 6px; flex-shrink: 0; }

  /* Esconde stats no topbar no mobile */
  #stat-cdo, #stat-total, #stat-bdi, #stat-items { display: none !important; }

  /* Badge da base: compacto mas visível */
  #baseIndicator {
    font-size: 10px;
    padding: 4px 8px;
    white-space: nowrap;
    max-width: 90px;
    overflow: hidden;
    text-overflow: ellipsis;
  }

  /* Botão Baixar Nuvem — só ícone no mobile */
  #btnBaixarNuvem {
    padding: 8px 10px !important;
    font-size: 11px !important;
    min-width: 36px;
  }
  #btnBaixarNuvem .btn-label { display: none; }

  /* ── BANNER TRIAL ── */
  #oa-trial-banner {
    font-size: 11px !important;
    padding: 8px 12px !important;
    display: flex !important;
    align-items: center;
    justify-content: center;
    gap: 4px;
    flex-wrap: nowrap;
    overflow: hidden;
  }

  /* Tab content */
  .tab-content { padding: 12px; }

  /* Cards */
  .card { padding: 14px; margin-bottom: 12px; }

  /* Section header compacto */
  .section-header { margin-bottom: 14px; }
  .section-header h2 { font-size: 15px; }
  .section-header p { font-size: 12px; }

  /* KPIs: 2 colunas */
  .kpi-grid { grid-template-columns: repeat(2, 1fr) !important; gap: 10px; }
  .kpi { padding: 14px; }

  /* Tabelas: scroll horizontal */
  .table-wrap, .sinapi-table-wrap {
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    border-radius: var(--radius);
  }
  table { min-width: 500px; }

  /* Área de upload */
  .upload-area {
    padding: 28px 16px;
    cursor: pointer;
  }
  .upload-area .upload-icon { font-size: 36px; }
  .upload-title { font-size: 15px; }
  .upload-formats {
    flex-wrap: wrap;
    gap: 6px;
    justify-content: center;
  }
  .format-badge { font-size: 11px; padding: 4px 8px; }

  /* Upload zone touch-friendly */
  .upload-zone {
    padding: 28px 16px !important;
    min-height: 150px !important;
  }

  /* CDO grid: 1 coluna */
  .cdo-body-grid {
    grid-template-columns: 1fr !important;
    padding: 12px;
    gap: 12px;
  }
  .cdo-kpi-row { grid-template-columns: repeat(2, 1fr); }

  /* Modais: bottom sheet no mobile */
  .modal-overlay {
    padding: 0;
    align-items: flex-end;
  }
  .modal-content,
  .modal-box,
  [id^="modal"] .modal-content {
    border-radius: 20px 20px 0 0 !important;
    max-height: 92vh;
    width: 100% !important;
    max-width: 100% !important;
    margin: 0 !important;
    overflow-y: auto;
  }
  /* Handle de arrastar para bottom sheets */
  .modal-content::before,
  .modal-box::before {
    content: '';
    display: block;
    width: 36px;
    height: 4px;
    background: var(--border-light);
    border-radius: 2px;
    margin: 10px auto 4px;
  }

  /* Botão processar */
  #btn-run { font-size: 12px; padding: 8px 14px; }

  /* Report/Relatório */
  .report-grid { grid-template-columns: 1fr !important; }

  /* Base SINAPI: esconde colunas menos importantes */
  .sinapi-table th:nth-child(4),
  .sinapi-table td:nth-child(4),
  .sinapi-table th:nth-child(6),
  .sinapi-table td:nth-child(6) { display: none; }

  /* Dashboard charts */
  .chart-container { height: 220px !important; }

  /* Orçamento busca */
  #orcaSearchInput { font-size: 16px !important; } /* evita zoom iOS */
  #orcaPainelBusca { max-height: 45vh; }

  /* Base Própria: tabela compacta */
  .bp-table th:nth-child(6),
  .bp-table td:nth-child(6),
  .bp-table th:nth-child(7),
  .bp-table td:nth-child(7) { display: none; }

  /* Footer sidebar */
  .sidebar-footer { padding: 12px 16px; }
  .sidebar-footer button,
  .sidebar-footer a {
    font-size: 11px !important;
    padding: 9px 10px !important;
    min-height: 42px;
  }

  /* Btn group: empilha no mobile */
  .btn-group {
    flex-direction: column;
    align-items: stretch;
  }
  .btn-group .btn {
    width: 100%;
    justify-content: center;
  }

  /* Cards de ação na tela inicial: 2 colunas */
  .action-cards-grid {
    grid-template-columns: repeat(2, 1fr) !important;
  }

  /* Formulários full-width */
  .form-row { flex-wrap: wrap; gap: 10px; }
  .form-row > * { flex: 1 1 calc(50% - 10px); min-width: 0; }
}

/* ================================================================
   MOBILE — max 640px
   ================================================================ */
@media (max-width: 640px) {

  /* Sidebar ocupa 88% da tela */
  .sidebar { width: 88vw !important; }

  /* KPIs: 2 colunas pequenas */
  .kpi-grid {
    grid-template-columns: repeat(2, 1fr) !important;
    gap: 8px;
  }
  .kpi { padding: 12px 10px; }
  .kpi-num { font-size: 22px; }
  .kpi-label { font-size: 10px; }

  /* Tab content mínimo */
  .tab-content { padding: 10px; }

  /* Topbar super compacto */
  .topbar { padding: 0 10px; height: 52px; }
  .page-title { font-size: 13px; max-width: 120px; }

  /* Menu toggle maior */
  .menu-toggle { min-width: 40px; min-height: 40px; }

  /* Botão processar: full width */
  #btn-run {
    width: 100%;
    justify-content: center;
  }

  /* Upload area compacta */
  .upload-zone {
    padding: 20px 12px !important;
    min-height: 130px !important;
  }
  .upload-zone p { font-size: 12px; }

  /* Esconde colunas de tabelas */
  .sinapi-table th:nth-child(n+5),
  .sinapi-table td:nth-child(n+5) { display: none; }

  /* Orçamento planilha: compacta */
  .orca-row td { padding: 8px 6px; font-size: 11px; }
  .orca-grupo-header { font-size: 12px; padding: 8px 10px; }

  /* Totais orçamento */
  .orca-totais-card { padding: 14px; }
  .orca-totais-card .total-geral { font-size: 20px; }

  /* Cards stats 2col */
  .bp-stats-grid {
    grid-template-columns: repeat(2, 1fr) !important;
    gap: 8px;
  }

  /* Paginação compacta */
  .pagination button { padding: 6px 10px; font-size: 11px; min-height: 36px; }

  /* Modal: altura máxima */
  .modal-content,
  .modal-box { max-height: 88vh; }

  /* Formulários: campos full width no mobile pequeino */
  .form-row > * { flex: 1 1 100%; }

  /* Topbar: esconde badge base */
  .base-size-badge { display: none; }

  /* Tutorial: box full width */
  #tut-box {
    width: calc(100vw - 16px) !important;
    max-width: calc(100vw - 16px) !important;
    left: 8px !important;
    right: 8px !important;
  }

  /* Cards de ação: 1 coluna em telas muuito pequenas */
  .action-cards-grid {
    grid-template-columns: 1fr !important;
  }

  /* Botão Baixar Nuvem: ícone only */
  #btnBaixarNuvem {
    padding: 8px 10px !important;
  }

  /* Section header muito compacto */
  .section-header h2 { font-size: 14px; }
  .section-header p { font-size: 11px; }
}

/* ================================================================
   MELHORIAS DE TOQUE — Touch UX
   ================================================================ */
@media (max-width: 1024px) {
  /* Área de toque mínima: 44px (Apple HIG) */
  button, a, .nav-item, [role="button"] {
    min-height: 44px;
  }

  /* Evita seleção acidental de texto */
  .nav-item, .btn, button:not(input) {
    user-select: none;
    -webkit-user-select: none;
  }

  /* Botão hamburguer destacado */
  .menu-toggle {
    background: rgba(59,130,246,0.12);
    border: 1px solid rgba(59,130,246,0.25);
    color: var(--accent-blue);
    border-radius: 8px;
    padding: 8px 12px;
    font-size: 16px;
    min-height: 40px;
    min-width: 44px;
    display: flex;
    align-items: center;
    justify-content: center;
  }
  .menu-toggle:hover,
  .menu-toggle:active {
    background: rgba(59,130,246,0.25);
  }
}

@media (max-width: 768px) {
  /* Inputs maiores para toque */
  input[type="text"],
  input[type="email"],
  input[type="password"],
  input[type="number"],
  input[type="search"],
  select,
  textarea {
    font-size: 16px !important; /* evita zoom automático no iOS */
    min-height: 44px;
  }
  textarea { min-height: 88px; }

  /* Nav items mais altos */
  .nav-item {
    padding: 13px 14px;
    font-size: 14px;
    min-height: 48px;
  }
  .nav-item i { font-size: 17px; }

  /* Scroll suave */
  .sidebar-nav,
  .table-wrap,
  .orca-resultados-wrap,
  .tab-content {
    -webkit-overflow-scrolling: touch;
    scroll-behavior: smooth;
  }

  /* Botões padrão */
  .btn {
    min-height: 42px;
    padding: 10px 16px;
    font-size: 13px;
  }
  .btn-sm { min-height: 36px; padding: 7px 12px; }
}

/* ================================================================
   SAFE AREA — iPhone com notch/Dynamic Island/Home Indicator
   ================================================================ */
@supports (padding: env(safe-area-inset-top)) {
  .topbar {
    padding-top: max(0px, env(safe-area-inset-top));
  }
  .sidebar {
    padding-top: max(0px, env(safe-area-inset-top));
    padding-bottom: max(16px, env(safe-area-inset-bottom));
  }
  .bottom-nav {
    padding-bottom: max(0px, env(safe-area-inset-bottom));
  }
}

/* ================================================================
   LANDSCAPE MOBILE — celular deitado
   ================================================================ */
@media (max-width: 900px) and (orientation: landscape) {
  .sidebar { width: 220px !important; }
  .tab-content { padding: 10px 12px; }
  .upload-zone { min-height: 100px !important; }
  .modal-overlay { align-items: center; }
  .modal-content,
  .modal-box {
    border-radius: 14px !important;
    max-height: 90vh;
    max-width: 88vw !important;
    width: 88vw !important;
    margin: auto !important;
  }
  .modal-content::before,
  .modal-box::before { display: none; }

  /* Bottom nav em landscape: esconde texto */
  .bottom-nav-item span { display: none; }
  .bottom-nav { height: 50px; }
  .bottom-nav-item { min-height: 50px; padding: 6px 4px; }
  .bottom-nav-item i { font-size: 20px; }

  /* Main: espaço para bottom nav menor */
  .main-content {
    padding-bottom: calc(50px + env(safe-area-inset-bottom, 0px)) !important;
  }
}

/* ================================================================
   TABLET SPECIFIC — 640px ~ 1024px
   ================================================================ */
@media (min-width: 641px) and (max-width: 1024px) {
  /* Tablet: sidebar como drawer */
  .sidebar { width: 280px !important; }

  /* Grids em 2-3 colunas */
  .kpi-grid { grid-template-columns: repeat(3, 1fr) !important; }

  /* Tab content com mais padding */
  .tab-content { padding: 20px; }

  /* Modais centradas */
  .modal-overlay { align-items: center; padding: 20px; }
  .modal-content,
  .modal-box {
    border-radius: var(--radius) !important;
    max-width: 600px !important;
    width: 90% !important;
    max-height: 88vh;
  }
  .modal-content::before,
  .modal-box::before { display: none; }
}

/* ================================================================
   ACESSIBILIDADE — High Contrast & Reduced Motion
   ================================================================ */
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.01ms !important;
  }
}

@media (prefers-contrast: high) {
  .sidebar { border-right: 2px solid var(--text-primary); }
  .nav-item.active { border-left: 4px solid var(--accent-blue); }
  .btn { border: 2px solid currentColor; }
}

/* ================================================================
   PWA / STANDALONE MODE
   ================================================================ */
@media (display-mode: standalone) {
  /* App instalado: remove border-radius do topbar */
  .topbar {
    padding-top: max(8px, env(safe-area-inset-top));
    border-radius: 0;
  }
}

/* ================================================================
   PRINT
   ================================================================ */
@media print {
  .sidebar, .topbar, #btnBaixarNuvem,
  .sidebar-footer, #btnRelatarBug,
  .bottom-nav { display: none !important; }
  .main-content {
    margin-left: 0 !important;
    padding-bottom: 0 !important;
  }
  .tab-content {
    display: block !important;
    padding: 0;
  }
}

/* ================================================================
   CARD MOBILE VISUAL — Melhorias específicas para cards no mobile
   ================================================================ */
@media (max-width: 768px) {
  /* Cards com sombra mais leve no mobile */
  .card {
    box-shadow: 0 2px 8px rgba(0,0,0,0.2);
  }

  /* Card header compacto */
  .card-header {
    padding: 12px 14px;
    font-size: 13px;
  }

  /* Upload area touch-friendly */
  .upload-area {
    border-radius: 14px;
    border: 2px dashed var(--border-light);
  }
  .upload-area:active {
    background: var(--bg-hover);
    border-color: var(--accent-blue);
  }

  /* Tabelas com células compactas */
  table th, table td {
    padding: 8px 10px;
    font-size: 12px;
  }

  /* Badges menores */
  .badge { font-size: 9px; padding: 2px 5px; }

  /* Filtros: scroll horizontal */
  .filter-bar {
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    white-space: nowrap;
    padding-bottom: 4px;
  }

  /* Botão de fechar modal maior */
  .modal-close,
  [class*="modal-close"] {
    min-width: 44px !important;
    min-height: 44px !important;
    font-size: 20px !important;
  }

  /* Toast notifications mobile */
  .toast-container {
    bottom: calc(64px + env(safe-area-inset-bottom, 8px)) !important;
    right: 10px !important;
    left: 10px !important;
    max-width: none !important;
  }
  .toast {
    width: 100%;
    font-size: 13px;
    padding: 12px 14px;
  }

  /* Tutorial tooltip: sempre no topo em mobile */
  #tut-tooltip, #tut-box {
    position: fixed !important;
  }
}

/* ================================================================
   SCROLL IMPROVEMENTS — Evita bounce/overscroll indesejado
   ================================================================ */
html {
  overflow-x: hidden;
}
body {
  overflow-x: hidden;
  position: relative;
}

/* iOS momentum scrolling */
.sidebar-nav,
.tab-content,
.table-wrap,
.modal-content,
.modal-box,
.orca-resultados-wrap {
  -webkit-overflow-scrolling: touch;
}

/* ================================================================
   LOADING SKELETON — Feedback visual enquanto carrega
   ================================================================ */
@media (max-width: 768px) {
  .skeleton {
    background: linear-gradient(90deg, var(--bg-elevated) 25%, var(--bg-hover) 50%, var(--bg-elevated) 75%);
    background-size: 200% 100%;
    animation: skeletonPulse 1.4s ease infinite;
    border-radius: 4px;
  }
  @keyframes skeletonPulse {
    0% { background-position: 200% 0; }
    100% { background-position: -200% 0; }
  }
}

/* ================================================================
   ORIENTAÇÃO — Aviso de rotação para telas muito pequenas
   ================================================================ */
@media (max-height: 400px) and (orientation: landscape) and (max-width: 768px) {
  /* Comprime mais o conteúdo */
  .topbar { height: 44px; }
  .bottom-nav { height: 44px; }
  .bottom-nav-item { min-height: 44px; }
  .main-content {
    padding-bottom: calc(44px + env(safe-area-inset-bottom, 0px)) !important;
  }
}
