/* SEW — celular, tablet, notebook e TV. Carregado por último para valer sobre o CSS de cada página. */

html {
  box-sizing: border-box;
  -webkit-text-size-adjust: 100%;
  text-size-adjust: 100%;
  max-width: 100%;
}
*, *::before, *::after { box-sizing: inherit; }

html, body {
  max-width: 100%;
}
body {
  overflow-x: clip;
  overflow-y: auto;
  min-height: 100vh;
  min-height: 100dvh;
}

img, video {
  max-width: 100%;
}
.foto-box img {
  width: 140px;
  height: 140px;
  max-width: 140px;
}

.wrap, .panel, .box, .chat {
  max-width: 100%;
  min-width: 0;
}
.wrap {
  width: auto;
}

.topbar {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 8px 14px;
  padding-top: max(14px, env(safe-area-inset-top));
  padding-left: max(16px, env(safe-area-inset-left));
  padding-right: max(16px, env(safe-area-inset-right));
}
.topbar > div {
  min-width: 0;
  overflow-wrap: anywhere;
}

h1, h2, h3, p, td, th, .msg, .hint, .subtitle, .note {
  overflow-wrap: break-word;
}

.table-wrap,
.sew-table-scroll {
  width: 100%;
  max-width: 100%;
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
  overscroll-behavior-x: contain;
}
.sew-table-scroll:focus-visible {
  outline: 3px solid #f1c40f;
  outline-offset: 2px;
}

#chat {
  -webkit-overflow-scrolling: touch;
  overscroll-behavior: contain;
  touch-action: pan-y;
}

a, button, .btn, .card, .tab, .pend, .chip span, summary {
  touch-action: manipulation;
  -webkit-tap-highlight-color: rgba(11, 61, 145, .18);
}

input:focus-visible,
select:focus-visible,
textarea:focus-visible,
button:focus-visible,
a:focus-visible,
.btn:focus-visible,
.tab:focus-visible,
.card:focus-visible,
.pend:focus-visible,
.area-tab:focus-visible,
.chip input:focus-visible + span {
  outline: 3px solid #f1c40f;
  outline-offset: 2px;
}
.sew-composer-bar textarea:focus-visible {
  outline: 3px solid #f1c40f;
  outline-offset: 2px;
}

.sew-account-footer {
  position: fixed;
  right: max(16px, env(safe-area-inset-right));
  bottom: max(12px, env(safe-area-inset-bottom));
  z-index: 40;
  text-align: right;
  font: 12px/1.45 Arial, sans-serif;
  letter-spacing: .02em;
}
.sew-account-footer a {
  color: rgba(255, 255, 255, .55);
  text-decoration: none;
  display: block;
  margin-bottom: 4px;
  padding: 4px 2px;
}
.sew-account-footer a:first-child {
  font-weight: 700;
  color: rgba(255, 255, 255, .75);
}

/* Celular e tablet estreito */
@media (max-width: 900px) {
  body {
    font-size: 16px;
  }
  .wrap {
    grid-template-columns: minmax(0, 1fr);
    margin-top: 10px;
    margin-bottom: 10px;
  }
  .panel, .box, .chat {
    padding: 14px;
  }
  h1 { font-size: 26px; }
  h2 { font-size: 20px; }
  .topbar {
    flex-wrap: nowrap;
    gap: 8px;
    padding-top: max(10px, env(safe-area-inset-top));
    padding-bottom: 10px;
    font-size: 15px;
  }
  .topbar a {
    min-height: 0;
    white-space: nowrap;
  }
  .btn, button, .tab, .area-tab, .save, .linkbtn, .pager a {
    min-height: 44px;
    font-size: 15px;
  }
  .card, .pend {
    display: block;
    font-size: 16px;
  }
  input:not([type="checkbox"]):not([type="radio"]):not([type="hidden"]):not([type="file"]),
  select,
  textarea,
  .sew-composer-bar textarea {
    font-size: 16px;
  }
}

@media (max-width: 760px) {
  .msg {
    max-width: min(92%, 100%);
    font-size: 16px;
  }
  .sew-composer-plus,
  .sew-composer-send {
    min-width: 44px;
    min-height: 44px;
  }
  .btn, button, .tab, .area-tab, .save, .linkbtn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
  }

  /* Formulários comuns: espaço para rolar o campo para cima do teclado. */
  body:not(:has(.sew-composer)) {
    padding-bottom: var(--sew-kb, 0px);
  }

  /* Chat: a tela visível vira uma coluna. O campo fica no fim, acima do teclado. */
  html:has(.sew-composer),
  body:has(.sew-composer) {
    height: var(--sew-vvh, 100dvh);
    max-height: var(--sew-vvh, 100dvh);
    overflow: hidden;
  }
  body:has(.sew-composer) {
    display: flex;
    flex-direction: column;
    min-height: 0;
    padding-bottom: 0;
    transform: translateY(var(--sew-vvt, 0px));
  }
  body:has(.sew-composer) .topbar {
    flex: 0 0 auto;
  }
  body:has(.sew-composer) .wrap {
    flex: 1 1 auto;
    min-height: 0;
    width: 100%;
    margin: 0;
    padding: 8px;
    display: flex;
    flex-direction: column;
  }
  body:has(.sew-composer) .chat {
    flex: 1 1 auto;
    min-height: 0;
    display: flex;
    flex-direction: column;
    padding: 12px;
    overflow: hidden;
  }
  body:has(.sew-composer) .header,
  body:has(.sew-composer) .sew-actions,
  body:has(.sew-composer) #ticketInfo,
  body:has(.sew-composer) .notice,
  body:has(.sew-composer) .hist {
    flex: 0 0 auto;
  }
  body:has(.sew-composer) .hist {
    order: 1;
    max-height: 56px;
    overflow: auto;
    font-size: 13px;
  }
  body:has(.sew-composer) #chat {
    flex: 1 1 auto;
    height: auto;
    min-height: 120px;
    max-height: none;
    overflow: auto;
  }
  body:has(.sew-composer) .sew-composer {
    order: 2;
    position: relative;
    left: auto;
    right: auto;
    bottom: auto;
    flex: 0 0 auto;
    z-index: 5;
    margin: 8px 0 0;
    padding: 0;
    background: #fff;
    box-shadow: none;
  }
  .sew-composer-bar {
    flex-wrap: nowrap;
    align-items: flex-end;
  }
  .sew-composer-bar textarea {
    flex: 1 1 auto;
    min-height: 44px;
    max-height: 96px;
    font-size: 16px;
  }
  .sew-composer-send {
    flex: 0 0 auto;
  }
}

@media (max-width: 640px) {
  .wrap {
    padding-left: 12px;
    padding-right: 12px;
  }
  .filters,
  .filters.single,
  .grid,
  .row2,
  .info-grid,
  .health,
  .stats,
  .cards {
    grid-template-columns: 1fr;
  }
  .filter-row.main input[type="text"],
  .filter-row.main input[type="date"],
  .filter-row.main select,
  .tab,
  .sew-transfer select {
    flex: 1 1 100%;
    min-width: 0;
    width: 100%;
  }
  .filter-actions {
    margin-left: 0;
    width: 100%;
  }
  .filter-actions .btn {
    flex: 1 1 auto;
    text-align: center;
  }
  .sew-actions,
  .sew-transfer,
  .sew-transfer-row {
    width: 100%;
  }
  .sew-actions .btn,
  .sew-transfer .btn {
    flex: 0 1 auto;
    min-height: 40px;
    text-align: center;
    justify-content: center;
  }
  .cards {
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 10px;
  }
  .stats {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }
  .search {
    flex-wrap: wrap;
  }
  .search input, .search select {
    flex: 1 1 100%;
    min-width: 0;
    width: 100%;
  }
  .item-row {
    flex-wrap: wrap;
  }
  .item-row input {
    flex: 1 1 calc(100% - 52px);
    min-width: 0;
  }
  .header, .head, .header-box {
    align-items: flex-start;
  }
  h1 { font-size: 24px; }
  .sew-table-scroll table.sew-table-wide {
    min-width: 680px;
  }
  .ua, .ctx {
    max-width: none;
    white-space: normal;
  }
}

@media (max-width: 420px) {
  .panel, .box, .chat {
    padding: 12px;
    border-radius: 12px;
  }
  .cards {
    grid-template-columns: 1fr;
  }
}

/* Teclado aberto em paisagem: cabeçalho mais baixo, chat continua na coluna. */
@media (max-width: 900px) and (max-height: 520px) {
  body:has(.sew-composer) .topbar { padding-top: 6px; padding-bottom: 6px; }
  body:has(.sew-composer) .hist { display: none; }
  .wrap { margin-top: 8px; margin-bottom: 8px; }
}

/* Notebook baixo (ex.: 1366×768): chat e envio na mesma tela */
@media (min-width: 900px) and (max-height: 820px) {
  #chat {
    height: auto;
    min-height: 240px;
    max-height: calc(100vh - 250px);
    max-height: calc(100dvh - 250px);
  }
}

/* Toque: alvos maiores e campos sem zoom forçado no iOS */
@media (pointer: coarse) {
  .btn, button, .tab, .area-tab, .save, .linkbtn, .pager a, .pend {
    min-height: 44px;
  }
  .chip span {
    min-height: 40px;
    display: inline-flex;
    align-items: center;
    padding: 8px 12px;
  }
  input:not([type="checkbox"]):not([type="radio"]):not([type="hidden"]):not([type="file"]),
  select,
  textarea,
  .sew-composer-bar textarea {
    font-size: 16px;
  }
  .preview-remove, .att-del {
    min-width: 36px;
    min-height: 36px;
  }
  input[type="checkbox"],
  input[type="radio"] {
    width: 22px;
    height: 22px;
  }
  .card:hover, .btn:hover, .pend:hover, .tab:hover, .btn-upload:hover {
    transform: none;
  }
}

/* Login: começa no topo para o teclado não cobrir senha e botão. */
@media (max-width: 900px) {
  body:not(:has(.sew-composer)) {
    flex-direction: column;
    align-items: center;
    justify-content: flex-start;
    overflow-y: auto;
    padding-top: max(12px, env(safe-area-inset-top));
  }
}

@media (max-width: 720px) {
  .sew-account-footer {
    left: 0;
    right: 0;
    bottom: 0;
    display: flex;
    justify-content: space-around;
    align-items: stretch;
    text-align: center;
    background: rgba(0, 0, 0, .78);
    padding: 4px 8px calc(4px + env(safe-area-inset-bottom));
    gap: 6px;
  }
  .sew-account-footer a {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    flex: 1 1 0;
    min-height: 44px;
    margin: 0;
    color: #fff;
    font-weight: 700;
  }
  body:has(.sew-account-footer) {
    padding-bottom: calc(68px + env(safe-area-inset-bottom));
  }
}

/* TV e monitores grandes: texto e alvos legíveis à distância, sem esticar até a borda */
@media (min-width: 1600px) {
  body { font-size: 17px; }
  .topbar { font-size: 18px; padding: 18px 28px; }
  .wrap { max-width: min(1680px, 94vw); }
  .panel, .box, .chat { padding: 28px; }
  h1 { font-size: 34px; }
  .btn, button, .tab, .area-tab, .card, .topbar a, input, select, textarea {
    font-size: 17px;
  }
  .btn, button, .tab, .area-tab, .save, .linkbtn {
    min-height: 48px;
    padding: 12px 18px;
  }
  .preview-remove, .pw-toggle, .sew-composer-plus {
    min-height: 44px;
    padding: 0;
  }
  #chat { font-size: 18px; }
  .msg { font-size: 18px; padding: 14px 16px; }
  .cards, .stats, .grid {
    gap: 18px;
  }
}

@media (min-width: 2200px) {
  body { font-size: 20px; }
  .topbar { font-size: 22px; }
  .wrap { max-width: min(2000px, 90vw); }
  h1 { font-size: 42px; }
  .btn, button, .tab, .area-tab, input, select, textarea, .card, .msg, #chat {
    font-size: 20px;
  }
  .btn, button, .tab, .area-tab, .save, .linkbtn, .topbar a {
    min-height: 56px;
  }
  .preview-remove, .pw-toggle, .sew-composer-plus {
    min-height: 48px;
  }
  .sew-account-footer { font-size: 18px; }
}

@media (prefers-reduced-motion: reduce) {
  .card, .btn, .tab, .pend, .btn-upload {
    transition: none;
  }
}
