/* Mobile presentation contract. Desktop and printed receipts keep theme.css. */
@media screen and (max-width: 768px) {
  .layout, .main, .main-content, .main-content > *, .field,
  .workspace-header-actions, .mobile-header-copy { min-width: 0; max-width: 100%; }
  .main { padding: 10px; }
  .mobile-header { margin-bottom: 8px; padding: 6px 10px; border-radius: 12px; }
  .mobile-header-copy strong { overflow-wrap: anywhere; }
  .workspace-header { display: grid; grid-template-columns: minmax(0, 1fr); position: static; padding: 8px; margin-bottom: 12px; }
  .workspace-header-copy { display: none; }
  .workspace-header-actions { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); gap: 6px; }
  .workspace-search { grid-column: 1 / -1; width: 100%; min-width: 0; }
  .sidebar { height: 100dvh; padding-bottom: max(12px, env(safe-area-inset-bottom)); }
  .sidebar-nav { overscroll-behavior: contain; }
  .sidebar-topbar, .sidebar .user-card { flex-shrink: 0; }
  .sidebar-item { min-height: 44px; }
  .sidebar-brand h2 { overflow-wrap: anywhere; }
  .main-content :is(.card, .desktop-module) { padding: 12px; min-width: 0; }
  .main-content :is(h1, h2, h3, p, strong, label) { overflow-wrap: anywhere; }
  .topbar h1 { font-size: clamp(1.4rem, 5vw, 1.8rem); }
  .main-content :is(.form-grid, .panel-grid, .operation-grid, .cadastro-layout,
    .client-crm-layout, .command-main-grid, .command-ranking-grid,
    .dashboard-main-grid, .dashboard-modern-grid, .dashboard-query-grid,
    .report-history-grid, .report-analytics-grid, .report-filter-grid,
    .agenda-modal-grid, .budget-layout, .nfe-layout) { grid-template-columns: minmax(0, 1fr) !important; }
  .form-grid > * { min-width: 0; max-width: 100%; grid-column: auto; }
  .main-content :is(.command-metric-grid, .dashboard-primary-kpis, .summary-grid,
    .entry-summary-grid, .command-yard-kpis, .command-yard-tickets) {
    grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 8px;
  }
  .metric-value, .command-yard-kpis strong { font-size: clamp(1.1rem, 4.5vw, 1.6rem); line-height: 1.2; }
  .button-row, .inline-actions, .cl-actions { flex-wrap: wrap; gap: 8px; }
  .btn, .icon-button, .cl-btn, .cl-chip, summary.btn, .password-toggle {
    min-height: 44px; max-width: 100%; white-space: normal; touch-action: manipulation;
  }
  .field :is(input, select, textarea), input.cl-search, .workspace-search input {
    min-width: 0; max-width: 100%; font-size: 16px !important;
  }
  .field :is(input, select) { min-height: 44px; }
  input[type='checkbox'], input[type='radio'] { min-height: 24px; width: 24px; }
  .main-content :is(img, video, canvas) { max-width: 100%; }
  .autocomplete-menu { max-width: 100%; max-height: min(280px, 40dvh); overflow: auto; }
  .table-wrap:has(> .mobile-records), .cl-list-panel:has(> .mobile-records) {
    min-width: 0; max-width: 100%; overflow: visible !important;
  }
  #app table.mobile-records { display: block; width: 100%; min-width: 0 !important; table-layout: auto; }
  #app table.mobile-records thead { display: none; }
  #app table.mobile-records tbody { display: grid; gap: 10px; width: 100%; }
  #app table.mobile-records tbody tr {
    display: grid; grid-template-columns: minmax(0, 1fr); width: 100%; min-width: 0;
    padding: 10px; margin: 0; border: 1px solid var(--line); border-radius: 12px;
  }
  #app table.mobile-records tbody td {
    display: grid; grid-template-columns: minmax(80px, .8fr) minmax(0, 1.2fr);
    gap: 8px; width: 100%; min-width: 0; max-width: 100%; padding: 8px 0;
    text-align: left; white-space: normal !important; overflow-wrap: anywhere;
    font-size: .9rem; border-bottom: 1px solid var(--line);
  }
  #app table.mobile-records tbody td::before {
    content: attr(data-label); display: block; font-size: .75rem; color: var(--muted);
    text-align: left; text-transform: none; overflow-wrap: anywhere;
  }
  #app table.mobile-records td[data-cell-type='actions'],
  #app table.mobile-records .mobile-record-message td { display: block; }
  #app table.mobile-records .mobile-record-message td::before { display: none; }
  #app table.mobile-records td:last-child { border-bottom: 0; }
  #app table.mobile-records td > * { min-width: 0; max-width: 100%; }
  #app table.mobile-records tfoot { display: block; width: 100%; }
  #app table.mobile-records tfoot tr { display: flex; flex-wrap: wrap; justify-content: space-between; }
  #app table.mobile-records tfoot td { width: auto; min-width: 0; }
  .mobile-row-toggle { display: none !important; }
  .modal-backdrop, .client-camera-modal { padding: 8px; }
  .modal-card, .modal-card-wide, .modal-card-xl, .client-camera-card {
    width: 100% !important; max-width: 100% !important; min-width: 0;
    max-height: calc(100dvh - 16px) !important; overflow-y: auto; overscroll-behavior: contain;
    padding: 12px; padding-bottom: max(12px, env(safe-area-inset-bottom));
  }
  .modal-titlebar { position: sticky; top: -12px; z-index: 3; background: var(--card); padding: 10px 0; gap: 8px; }
  .modal-titlebar > div { min-width: 0; }
  .modal-titlebar .icon-button { flex-shrink: 0; }
  .client-camera-viewport { max-height: 50dvh; }
  .client-camera-head small { white-space: normal; }
  #compraCurrentItemsCard, .compra-items-list { min-height: 0; }
  .cl-header, .cl-search-row { flex-wrap: wrap; gap: 8px; }
  .cl-header-right, .cl-chips { flex-wrap: wrap; max-width: 100%; }
  .cl-search { width: 100%; min-width: 0; }
  #premiumLogin .login-content { display: flex; flex-direction: column; padding: 16px; gap: 18px; }
  #premiumLogin .auth-login-panel { order: -1; width: 100%; min-width: 0; }
  #premiumLogin .login-card { width: 100%; min-width: 0; padding: 20px 16px; }
  #premiumLogin .brand-area { width: 100%; min-width: 0; }
  #premiumLogin .hero-title { font-size: 1.5rem; }
  #premiumLogin .feature-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  #premiumLogin .input-login { min-width: 0; width: 100%; font-size: 16px; }
  #premiumLogin .password-shell { min-width: 0; }
}
@media screen and (max-width: 360px) {
  .main-content :is(.command-metric-grid, .dashboard-primary-kpis, .summary-grid,
    .entry-summary-grid, .command-yard-kpis, .command-yard-tickets) { grid-template-columns: minmax(0, 1fr); }
}
@media (prefers-reduced-motion: reduce) {
  .sidebar, .sidebar-backdrop { transition: none; }
}
/* Operational mobile corrections: keep the existing controls and handlers. */
@media screen and (max-width: 1100px) {
  #compraForm .compra-item-entry {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }
  #compraForm .compra-item-entry > .field:first-child { grid-column: 1 / -1; }
  #compraForm .compra-item-entry > #valorVenda { display: none; }
  #compraForm .compra-item-entry > * { min-width: 0; max-width: 100%; }
  #compraForm .compra-add-item { grid-column: 1 / -1; width: 100%; justify-self: stretch; }
}
@media screen and (max-width: 768px) {
  #compraForm .compra-pdv-layout,
  #compraForm .compra-pdv-main { min-width: 0; grid-template-columns: minmax(0, 1fr); }
  #compraForm .compra-step-card { padding: 12px; }
  #compraForm .compra-item-entry { gap: 10px; }
  #compraForm .compra-item-entry > #valorVenda { display: none; }
  #compraForm .compra-item-entry > .compra-total-preview { grid-column: 1 / -1; }
  #compraForm .compra-item-entry label { white-space: normal; }
  #compraForm .compra-total-preview { grid-column: auto; padding: 8px; }
  #compraForm .compra-total-preview strong { white-space: normal; overflow-wrap: anywhere; }
  #compraForm .compra-item-inline-edit { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  #compraForm .compra-items-footer { flex-wrap: wrap; gap: 8px; }
  #compraForm .compra-footer-actions {
    position: static; grid-template-columns: minmax(0, 1fr); grid-template-areas: none;
    margin-top: 12px; padding: 10px; gap: 10px;
  }
  #compraForm .compra-footer-actions > * { grid-area: auto; min-width: 0; width: 100%; }
  #compraForm .compra-footer-tools { display: flex; flex-wrap: wrap; }
  #compraForm .compra-discount-inline { min-width: 0; max-width: 100%; }
  #compraForm .compra-footer-total { flex: 1 1 120px; }
  #compraForm .compra-footer-buttons {
    display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); margin: 0; gap: 8px;
  }
  #compraForm .compra-footer-buttons .btn {
    grid-area: auto; min-height: 44px; font-size: 14px !important; white-space: normal;
  }
  #compraForm .compra-footer-buttons .btn::after { content: none; }
  #compraForm #saveCompraFooterButton { grid-column: 1 / -1; min-height: 48px; }
  #compraForm .compra-more-menu > div { width: min(300px, calc(100vw - 48px)); min-width: 0; }
  #compraHistoryPanel {
    display: block; height: auto; min-height: 0; max-height: none; overflow: visible;
  }
  #compraHistoryPanel .compra-history-header,
  #compraHistoryPanel .compra-history-body,
  #compraHistoryPanel .compra-history-list {
    position: static; height: auto; min-height: 0; max-height: none; overflow: visible;
  }
  #compraHistoryPanel .compra-history-header { padding: 10px; }
  #compraHistoryPanel .compra-history-titlebar { gap: 8px; margin-bottom: 10px; }
  #compraHistoryPanel .compra-history-titlebar h1 { font-size: 18px; }
  #compraHistoryPanel .compra-history-titlebar p { font-size: 13px; margin-top: 4px; }
  #compraHistoryPanel .compra-history-filters { grid-template-columns: minmax(0, 1fr); gap: 8px; }
  #compraHistoryPanel .compra-history-filters > * { width: 100%; min-width: 0; min-height: 44px; font-size: 16px; }
  #compraHistoryPanel .compra-history-summary {
    grid-template-columns: repeat(3, minmax(0, 1fr)); padding: 10px; gap: 8px;
  }
  #compraHistoryPanel .compra-history-summary strong { white-space: normal; overflow-wrap: anywhere; font-size: 15px; }
  #compraHistoryPanel .compra-history-list { padding: 10px; }
  #compraHistoryPanel .compra-action-menu > div {
    position: static; width: 100%; min-width: 0; max-width: 100%; margin-top: 8px;
  }
  #compraHistoryPanel .compra-history-client { overflow: visible; text-overflow: clip; }
  .cl-page .cl-body { min-width: 0; }
  .cl-page .cl-list-panel { max-height: none; }
  #app .cl-table .cl-actions {
    display: grid; grid-template-columns: repeat(2, minmax(0, 1fr));
    width: 100%; gap: 8px; margin-top: 8px;
  }
  #app .cl-table .cl-actions .cl-btn { justify-content: center; min-height: 44px; }
  #app .cl-table .cl-actions .cl-btn-edit { grid-column: 1 / -1; }
  .whatsapp-premium-module .whatsapp-dashboard-lists { grid-template-columns: minmax(0, 1fr); align-items: start; }
  .whatsapp-dashboard-lists .table-wrap { height: auto; max-height: none; }
  #app .whatsapp-dashboard-lists table.mobile-records tbody td {
    display: block; text-align: left; line-height: 1.45; overflow: visible;
  }
  #app .whatsapp-dashboard-lists table.mobile-records tbody td::before { margin-bottom: 4px; }
  #app .whatsapp-dashboard-lists table.mobile-records tbody td:first-child { font-weight: 700; }
  #app .whatsapp-dashboard-lists .status-pill { white-space: normal; max-width: 100%; }
}
@media screen and (max-width: 768px) {
  /* History previously forced a 980px table with !important on phones. */
  #app #compraHistoryPanel table.mobile-records { display: block !important; min-width: 0 !important; }
  #app #compraHistoryPanel table.mobile-records thead { display: none !important; }
  #app #compraHistoryPanel table.mobile-records tbody,
  #app #compraHistoryPanel table.mobile-records tr { display: grid !important; grid-template-columns: minmax(0, 1fr); }
  #app #compraHistoryPanel table.mobile-records td {
    display: grid !important; position: static !important; grid-column: auto; grid-row: auto;
    width: 100% !important; max-width: 100% !important; text-align: left !important;
  }
  #app #compraHistoryPanel table.mobile-records td::before {
    display: block !important; content: attr(data-label) !important;
  }
  #app #compraHistoryPanel table.mobile-records td[data-cell-type='actions'] { display: block !important; }
}
@media screen and (min-width: 769px) and (max-width: 1100px) and (max-height: 500px) {
  #compraForm .compra-footer-actions { position: static; }
  #compraHistoryPanel, #compraHistoryPanel .compra-history-body,
  #compraHistoryPanel .compra-history-list { height: auto; min-height: 0; max-height: none; overflow: visible; }
  #compraHistoryPanel .compra-history-header { position: static; }
  #compraHistoryPanel .table-wrap { overflow-x: auto !important; max-width: 100%; }
}
