/* =========================================================
   ReMind SaaS - Mobile/Tablet Phase 1
   Escopo: somente telas <= 900px
   Objetivo: impedir rolagem horizontal global e organizar telas comuns
   ========================================================= */

@media (max-width: 900px) {
  html,
  body {
    width: 100% !important;
    max-width: 100% !important;
    overflow-x: hidden !important;
  }

  body {
    min-width: 0 !important;
    -webkit-text-size-adjust: 100%;
  }

  *,
  *::before,
  *::after {
    box-sizing: border-box;
  }

  img,
  svg,
  canvas,
  video,
  iframe {
    max-width: 100%;
  }

  input,
  select,
  textarea,
  button {
    max-width: 100%;
  }

  /* Container principal */
  .clinic-saas-container {
    width: 100% !important;
    max-width: 100% !important;
    padding: 14px 12px 88px !important;
    margin: 0 !important;
    overflow-x: hidden !important;
  }

  /* Cabeçalho das páginas */
  .page-head {
    display: flex !important;
    flex-direction: column !important;
    align-items: stretch !important;
    gap: 12px !important;
    margin-bottom: 14px !important;
  }

  .page-head h1,
  .page-head h2 {
    font-size: 22px !important;
    line-height: 1.15 !important;
    margin: 0 !important;
  }

  .page-sub {
    font-size: 12px !important;
    line-height: 1.35 !important;
  }

  .page-head > div:last-child,
  .page-head > form,
  .page-head > a,
  .page-head .saas-btn {
    max-width: 100% !important;
  }

  .page-head > div:last-child {
    display: flex !important;
    flex-wrap: wrap !important;
    gap: 8px !important;
  }

  .page-head .saas-btn,
  .page-head a.saas-btn,
  .page-head button.saas-btn {
    min-height: 42px !important;
    justify-content: center !important;
    white-space: normal !important;
  }

  /* Cards/KPIs */
  .kpi-prime-grid,
  .dash-grid,
  .dash-charts {
    display: grid !important;
    grid-template-columns: 1fr !important;
    gap: 12px !important;
    margin-bottom: 16px !important;
  }

  .kpi-prime-card,
  .dash-kpi,
  .dash-chart,
  .wa-card,
  .wa-container,
  .financial-card {
    width: 100% !important;
    max-width: 100% !important;
    min-width: 0 !important;
    overflow: hidden !important;
    border-radius: 16px !important;
  }

  .kpi-prime-card {
    padding: 16px !important;
  }

  .kpi-prime-value,
  .dash-kpi-val {
    font-size: 24px !important;
    line-height: 1.1 !important;
    word-break: break-word;
  }

  /* Formulários */
  .field,
  .cs-field,
  .cs-field-3,
  .cs-field-4 {
    width: 100% !important;
    min-width: 0 !important;
    grid-column: 1 / -1 !important;
  }

  .cs-grid,
  .form-grid,
  .grid,
  form [style*="display:grid"],
  form [style*="display: grid"] {
    grid-template-columns: 1fr !important;
  }

  .field input,
  .field select,
  .field textarea,
  .cs-field input,
  .cs-field select,
  .cs-field textarea,
  input[type="text"],
  input[type="email"],
  input[type="number"],
  input[type="date"],
  input[type="datetime-local"],
  input[type="password"],
  select,
  textarea {
    width: 100% !important;
    min-width: 0 !important;
  }

  form [style*="min-width"] {
    min-width: 0 !important;
  }

  /* Tabelas: rolagem fica dentro do card, não no site inteiro */
  .table-responsive,
  .wa-container:has(.app-table),
  .wa-card:has(.app-table) {
    width: 100% !important;
    max-width: 100% !important;
    overflow-x: auto !important;
    overflow-y: hidden !important;
    -webkit-overflow-scrolling: touch;
  }

  .app-table {
    width: 100% !important;
    min-width: 680px;
  }

  .app-table th,
  .app-table td {
    white-space: nowrap;
  }

  /* Pacientes */
  .patients-searchbar__grid {
    grid-template-columns: 1fr !important;
  }

  .patients-searchbar__select,
  .patients-searchbar__button {
    width: 100% !important;
    min-width: 0 !important;
  }

  /* Login compacto tipo app */
  .auth-screen,
  .auth-shell {
    min-height: 100svh !important;
    width: 100% !important;
    overflow-x: hidden !important;
  }

  .auth-side {
    display: none !important;
  }

  .auth-main {
    min-height: 100svh !important;
    padding: 14px !important;
    align-items: center !important;
  }

  .auth-card {
    width: 100% !important;
    max-width: 420px !important;
    padding: 22px 18px !important;
    border-radius: 20px !important;
  }

  .auth-title {
    font-size: 22px !important;
  }

  .auth-sub {
    margin-bottom: 16px !important;
  }

  .auth-form {
    gap: 12px !important;
  }

  .auth-input {
    min-height: 44px !important;
  }

  .auth-footer-note {
    margin-top: 14px !important;
  }

  /* Modais */
  .gc-modal-bd,
  #rm-confirm-overlay,
  #rm-alert-overlay,
  #session-warning-modal {
    max-width: 100vw !important;
    overflow-x: hidden !important;
  }

  .gc-modal,
  #rm-confirm-box,
  #rm-alert-box {
    width: calc(100vw - 24px) !important;
    max-width: calc(100vw - 24px) !important;
    max-height: calc(100svh - 24px) !important;
    overflow-y: auto !important;
  }

  .gc-modal-head,
  .gc-modal-body {
    padding-left: 16px !important;
    padding-right: 16px !important;
  }

  /* Agenda fase 1:
     não cria ainda a agenda mobile final.
     Apenas impede que a grade de 7 dias empurre o site inteiro para horizontal.
  */
  .gc-wrap {
    width: 100% !important;
    max-width: 100% !important;
    overflow-x: auto !important;
    overflow-y: hidden !important;
    -webkit-overflow-scrolling: touch;
  }

  .gc-head,
  .gc-body {
    min-width: 960px !important;
  }

  .gc-body-wrap {
    overflow: visible !important;
  }

  .agenda-touch-drag-hint {
    font-size: 12px !important;
    line-height: 1.35 !important;
  }
}

@media (max-width: 520px) {
  .clinic-saas-container {
    padding-left: 10px !important;
    padding-right: 10px !important;
  }

  .page-head .saas-btn,
  .page-head a.saas-btn,
  .page-head button.saas-btn {
    width: 100% !important;
  }

  .auth-main {
    padding: 10px !important;
  }

  .auth-card {
    padding: 20px 16px !important;
  }

  .app-table {
    min-width: 620px;
  }
}

/* ReMind Mobile Phase 2 - Tables To Cards */
@media (max-width: 760px) {
  /* Tabelas principais viram cards */
  .table-responsive {
    overflow: visible !important;
  }

  .app-table {
    min-width: 0 !important;
    width: 100% !important;
    border-collapse: separate !important;
    border-spacing: 0 12px !important;
  }

  .app-table thead {
    display: none !important;
  }

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

  .app-table tr {
    background: #ffffff !important;
    border: 1px solid var(--border-color, #d8e1ee) !important;
    border-radius: 16px !important;
    box-shadow: 0 10px 24px rgba(15, 23, 42, .06) !important;
    padding: 12px !important;
    margin-bottom: 12px !important;
    overflow: hidden !important;
  }

  .app-table td {
    border: 0 !important;
    padding: 8px 2px !important;
    white-space: normal !important;
    text-align: left !important;
  }

  .app-table td::before {
    display: block;
    margin-bottom: 4px;
    font-size: 10px;
    line-height: 1;
    font-weight: 900;
    letter-spacing: .06em;
    text-transform: uppercase;
    color: var(--text-muted, #64748b);
  }

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

  .app-table td[colspan] {
    text-align: center !important;
    padding: 28px 14px !important;
  }

  .app-table td > div[style*="display:flex"],
  .app-table td > div[style*="display: flex"] {
    max-width: 100% !important;
    flex-wrap: wrap !important;
  }

  .table-actions,
  .app-table td:last-child > div {
    display: flex !important;
    flex-wrap: wrap !important;
    justify-content: flex-start !important;
    gap: 8px !important;
  }

  .table-actions .saas-btn,
  .app-table td:last-child .saas-btn {
    min-width: 42px !important;
    min-height: 38px !important;
    justify-content: center !important;
  }

  /* Pacientes: 5 colunas */
  .app-table tr td:nth-child(1)::before { content: "Paciente"; }
  .app-table tr td:nth-child(2)::before { content: "Contato"; }
  .app-table tr td:nth-child(3)::before { content: "Valor"; }
  .app-table tr td:nth-child(4)::before { content: "Status"; }
  .app-table tr td:nth-child(5)::before { content: "Ações"; }

  /* Financeiro Receitas: 7 colunas */
  .financial-page .app-table tr td:nth-child(1)::before,
  body .app-table tr td:nth-child(1)::before { content: "Paciente / item"; }

  /* Ajustes genéricos para tabelas financeiras:
     Como as abas têm quantidades diferentes de colunas, os rótulos abaixo
     são seguros e não quebram a leitura. */
  .app-table tr td:nth-child(6)::before { content: "Valor"; }
  .app-table tr td:nth-child(7)::before { content: "Ações"; }

  /* Financeiro: valores sempre destacados */
  .app-table td[style*="text-align:right"] {
    text-align: left !important;
  }

  .app-table td[style*="font-weight:700"],
  .app-table td strong {
    word-break: break-word !important;
  }

  /* Filtros e formulários financeiros */
  form div[style*="display:flex"][style*="flex-wrap"] {
    display: grid !important;
    grid-template-columns: 1fr !important;
    gap: 10px !important;
  }

  form div[style*="display:flex"][style*="flex-wrap"] > div,
  form div[style*="display:flex"][style*="flex-wrap"] > button,
  form div[style*="display:flex"][style*="flex-wrap"] > a {
    width: 100% !important;
    min-width: 0 !important;
  }

  form div[style*="display:flex"][style*="flex-wrap"] .saas-btn {
    width: 100% !important;
  }

  /* Abas financeiras */
  .saas-tabs,
  .financial-tabs {
    display: flex !important;
    overflow-x: auto !important;
    gap: 8px !important;
    padding-bottom: 6px !important;
    -webkit-overflow-scrolling: touch;
  }

  .saas-tab,
  .financial-tabs a {
    flex: 0 0 auto !important;
    white-space: nowrap !important;
  }
}
/* End ReMind Mobile Phase 2 - Tables To Cards */

/* ReMind Mobile Phase 3 - Agenda 2 Days */
@media (max-width: 760px) {
  .agenda-touch-drag-hint {
    margin: 8px 0 10px !important;
    padding: 10px 12px !important;
    font-size: 11px !important;
  }

  .gc-wrap {
    width: 100% !important;
    max-width: 100% !important;
    overflow-x: hidden !important;
    overflow-y: visible !important;
    border-radius: 16px !important;
  }

  .gc-head,
  .gc-body {
    width: 100% !important;
    min-width: 0 !important;
    grid-template-columns: 48px repeat(var(--agenda-visible-days, 2), minmax(0, 1fr)) !important;
  }

  .gc-head {
    position: sticky;
    top: 0;
    z-index: 5;
  }

  .gc-head-time {
    width: 48px !important;
    min-width: 48px !important;
  }

  .gc-head-day {
    min-width: 0 !important;
    padding: 8px 4px !important;
  }

  .gc-dow {
    font-size: 10px !important;
  }

  .gc-date {
    font-size: 15px !important;
  }

  .gc-body-wrap {
    width: 100% !important;
    max-width: 100% !important;
    overflow-x: hidden !important;
    overflow-y: auto !important;
    max-height: calc(100svh - 230px) !important;
    min-height: 520px !important;
    -webkit-overflow-scrolling: touch;
  }

  .gc-time-col {
    width: 48px !important;
    min-width: 48px !important;
  }

  .gc-time-slot {
    font-size: 10px !important;
    padding-right: 5px !important;
  }

  .gc-day {
    min-width: 0 !important;
  }

  .ev {
    border-radius: 8px !important;
    padding: 5px 5px !important;
    overflow: hidden !important;
    box-shadow: 0 4px 10px rgba(15, 23, 42, .10) !important;
  }

  .ev-name {
    font-size: 10px !important;
    line-height: 1.15 !important;
  }

  .ev-info {
    font-size: 9px !important;
    line-height: 1.1 !important;
  }

  .ev-birthday-badge,
  .ev-cancel-request .ev-info {
    display: none !important;
  }

  #weekLabel {
    min-width: 104px !important;
    padding: 0 8px !important;
    font-size: 10px !important;
  }

  #prevWeek,
  #nextWeek {
    min-width: 38px !important;
    width: 38px !important;
    height: 38px !important;
  }
}

@media (min-width: 761px) and (max-width: 1024px) {
  .gc-wrap {
    overflow-x: auto !important;
    -webkit-overflow-scrolling: touch;
  }

  .gc-head,
  .gc-body {
    min-width: 900px !important;
  }
}
/* End ReMind Mobile Phase 3 - Agenda 2 Days */

/* ReMind Mobile Phase 3.1 - Agenda Nav + Modal Back */
@media (max-width: 760px) {
  /* Navegação explícita da agenda */
  .page-head > div:last-child {
    width: 100% !important;
  }

  #prevWeek,
  #nextWeek,
  #agendaTodayMobile {
    width: auto !important;
    min-width: 86px !important;
    height: 40px !important;
    padding: 0 10px !important;
    display: inline-flex !important;
    align-items: center !important;
    justify-content: center !important;
    gap: 6px !important;
    font-size: 11px !important;
    font-weight: 900 !important;
    border-radius: 14px !important;
    white-space: nowrap !important;
  }

  #weekLabel {
    flex: 1 1 100% !important;
    order: -1 !important;
    min-height: 34px !important;
    margin-bottom: 6px !important;
    font-size: 12px !important;
  }

  #prevWeek span,
  #nextWeek span,
  #agendaTodayMobile span {
    display: inline !important;
  }

  /* Modal mobile: sempre permitir voltar/cancelar */
  #modalBackdrop.open,
  #actionBackdrop.open,
  #retroactiveModal.open {
    align-items: flex-start !important;
    padding: 12px !important;
    overflow-y: auto !important;
    -webkit-overflow-scrolling: touch;
  }

  #modalBackdrop .gc-modal,
  #actionBackdrop .gc-modal,
  #retroactiveModal .gc-modal {
    margin: 0 auto 24px !important;
    max-height: calc(100svh - 24px) !important;
    display: flex !important;
    flex-direction: column !important;
  }

  #modalBackdrop .gc-modal-head,
  #actionBackdrop .gc-modal-head,
  #retroactiveModal .gc-modal-head {
    flex: 0 0 auto !important;
    position: sticky !important;
    top: 0 !important;
    z-index: 2 !important;
  }

  #modalBackdrop .gc-modal-body,
  #actionBackdrop .gc-modal-body,
  #retroactiveModal .gc-modal-body {
    flex: 1 1 auto !important;
    overflow-y: auto !important;
    padding-bottom: 18px !important;
  }

  #modalBackdrop form > div:last-child,
  #retroactiveModal form > div:last-child {
    position: sticky !important;
    bottom: 0 !important;
    background: #ffffff !important;
    padding-top: 12px !important;
    margin-top: 12px !important;
    border-top: 1px solid var(--border-color, #d8e1ee) !important;
    display: grid !important;
    grid-template-columns: 1fr !important;
    gap: 8px !important;
  }

  #cancelModal,
  #modalBackdrop button[type="button"],
  #retroactiveModal button[type="button"] {
    display: inline-flex !important;
    width: 100% !important;
    min-height: 44px !important;
    justify-content: center !important;
  }

  #modalBackdrop button[type="submit"],
  #retroactiveModal button[type="submit"] {
    width: 100% !important;
    min-height: 44px !important;
    justify-content: center !important;
  }
}
/* End ReMind Mobile Phase 3.1 - Agenda Nav + Modal Back */

/* ReMind Mobile Phase 3.2 - Day/Week Nav + Compact Agenda */
@media (max-width: 640px) {
  :root {
    --hour-px: 42px;
  }

  #weekLabel {
    flex: 0 0 auto !important;
    order: 0 !important;
    min-width: 86px !important;
    width: 86px !important;
    margin-bottom: 0 !important;
    text-align: left !important;
    line-height: 1.1 !important;
  }

  #prevWeek,
  #nextWeek,
  #agendaTodayMobile {
    min-width: 0 !important;
    height: 36px !important;
    padding: 0 8px !important;
    font-size: 10px !important;
  }

  #prevWeek span,
  #nextWeek span,
  #agendaTodayMobile span {
    display: inline !important;
  }

  .agenda-week-nav-mobile {
    width: 100%;
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 8px;
    margin: 8px 0 10px;
  }

  .agenda-week-nav-mobile button {
    width: 100% !important;
    min-height: 34px !important;
    justify-content: center !important;
    font-size: 10px !important;
    font-weight: 900 !important;
    border-radius: 12px !important;
  }

  .gc-body-wrap {
    max-height: calc(100svh - 250px) !important;
    min-height: 440px !important;
  }

  .gc-head,
  .gc-body {
    grid-template-columns: 44px repeat(2, minmax(0, 1fr)) !important;
  }

  .gc-head-time,
  .gc-time-col {
    width: 44px !important;
    min-width: 44px !important;
  }

  .gc-time-slot {
    font-size: 9px !important;
  }

  .ev {
    padding: 4px 5px !important;
  }

  .ev-name {
    font-size: 9.5px !important;
  }

  .ev-info {
    font-size: 8px !important;
  }
}

@media (min-width: 641px) and (max-width: 1024px) {
  :root {
    --hour-px: 42px;
  }

  .gc-head,
  .gc-body {
    min-width: 840px !important;
    grid-template-columns: 54px repeat(7, minmax(105px, 1fr)) !important;
  }

  .gc-body-wrap {
    max-height: calc(100svh - 220px) !important;
    overflow: auto !important;
  }

  .gc-time-col,
  .gc-head-time {
    width: 54px !important;
    min-width: 54px !important;
  }

  .ev-name {
    font-size: 9.5px !important;
  }

  .ev-info {
    font-size: 8px !important;
  }
}
/* End ReMind Mobile Phase 3.2 - Day/Week Nav + Compact Agenda */
