/* Celular e telas pequenas (v0.17): menu em gaveta, tabelas viram cartões, formulários em uma coluna, janelas em tela cheia.
 * Carregado por último: só age abaixo de 900 px de largura (o computador continua exatamente como antes). */
.m-top, .m-scrim, .m-tab { display: none; }

@media (max-width: 900px) {
  :root { --alvo: 40px; }
  html, body { height: 100%; overflow: hidden; }
  .app { display: flex; flex-direction: column; height: 100dvh; }
  .main { flex: 1; min-height: 0; }
  .main::after { display: none; }                       /* marca d'água atrapalha em tela pequena */
  .view { padding: 14px 12px 20px; scroll-padding-bottom: 90px; -webkit-overflow-scrolling: touch; }

  /* barra do topo com botão do menu */
  .m-top { display: flex; align-items: center; gap: 10px; height: 52px; flex: none; padding: 0 10px; background: var(--ink); color: #fff; position: relative; z-index: 30; }
  .m-top button { width: 44px; height: 44px; border: 0; border-radius: 10px; background: rgba(255, 255, 255, .1); color: #fff; font-size: 22px; line-height: 1; cursor: pointer; }
  .m-top strong { font-size: 16px; letter-spacing: -.1px; }
  .m-top span { margin-left: auto; font-size: 12px; color: #9ea1b3; }

  /* menu lateral vira gaveta */
  .sidebar { position: fixed; z-index: 50; top: 0; bottom: 0; left: 0; width: min(300px, 86vw); transform: translateX(-102%); transition: transform .2s ease; box-shadow: 8px 0 30px rgba(0, 0, 0, .4); overflow-y: auto; }
  body.m-menu .sidebar { transform: none; }
  .m-scrim { position: fixed; inset: 0; z-index: 40; background: rgba(10, 10, 20, .5); }
  body.m-menu .m-scrim { display: block; }
  .nav-item, .nav-head { min-height: 44px; }

  /* títulos, barras de ferramentas e botões */
  .page-head { flex-wrap: wrap; gap: 10px; }
  .page-head h1 { font-size: 20px; }
  .page-head > span, .page-head > div > span { flex-wrap: wrap; }
  .sc .art.vazio { display: none; } .sc:has(> .art.vazio) { grid-template-columns: 1fr; }
  .page-head > div:last-child:not(:first-child) { width: 100%; display: flex; flex-wrap: wrap; gap: 8px; }
  .toolbar { gap: 8px; }
  .toolbar .grow, .toolbar select, .toolbar input:not([type=checkbox]):not([type=radio]) { flex: 1 1 100%; min-width: 0; width: 100%; }
  .btn, button.btn { min-height: var(--alvo); }
  .btn.small { min-height: 38px; }
  input, select, textarea { font-size: 16px; min-height: 42px; }   /* 16px evita o zoom automático do iPhone */
  input[type=checkbox], input[type=radio] { min-height: 0; width: 20px; height: 20px; }

  /* qualquer grade de formulário vira uma coluna (inclusive as com estilo escrito no código) */
  .grid { grid-template-columns: 1fr; }
  .grid > * { grid-column: 1 / -1 !important; min-width: 0; }
  .ed-tabs { max-width: 100%; overflow: hidden; }
  .ed-tabs > div:first-child, .ed-tabs [role=tablist] { overflow-x: auto; }
  [style*="grid-template-columns"] { grid-template-columns: 1fr !important; }
  .price-grid { grid-template-columns: 1fr 1fr; }
  .hh-g { grid-template-columns: 1fr; }
  .hh-b li > button:first-child { display: block; flex: 1; }
  .kpis, .kpis.small, .hm-ks, .perfis { grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 10px; }
  .hm-metrics { grid-template-columns: repeat(2, 1fr); }
  .hm-cols, .hm-dash, .pb-main, .pb-board, .pb-rail { grid-template-columns: 1fr !important; }
  .st-grid { grid-template-columns: 1fr; }
  .sc { grid-template-columns: 96px 1fr; }
  .funil { grid-template-columns: repeat(5, 78vw); scroll-snap-type: x mandatory; }
  .funil > * { scroll-snap-align: start; }
  .setores-tabs, .seg, .chip-row, .tabs { overflow-x: auto; flex-wrap: nowrap; -webkit-overflow-scrolling: touch; }
  .setores-tabs > *, .seg > * { flex: none; }
  .rel-graf li { grid-template-columns: minmax(80px, 120px) 1fr 84px; }
  .rec button { grid-template-columns: 70px 1fr auto; }

  /* janelas em tela cheia */
  #modal-root > *, .modal-back, .overlay { padding: 0 !important; }
  .modal, .modal.narrow { width: 100% !important; max-width: none; margin: 0 !important; min-height: 100dvh; border-radius: 0; }
  .modal-head { position: sticky; top: 0; z-index: 2; background: #fff; padding: 12px 14px; }
  .modal-body { padding: 14px; }
  .modal-foot { position: sticky; bottom: 0; flex-wrap: wrap; padding: 10px 14px; border-radius: 0; }
  .modal-foot .btn { flex: 1 1 auto; }

  /* barra de ações fixa no rodapé da tela */
  .actbar { position: sticky; bottom: -20px; margin: 16px -12px -20px; padding: 10px 12px; flex-wrap: wrap; gap: 8px; }
  .view .actbar { margin-top: 20px; }
  .actbar { flex-direction: column; align-items: stretch; flex-wrap: nowrap; }
  .actbar .grp { width: 100%; flex-wrap: nowrap; overflow-x: auto; -webkit-overflow-scrolling: touch; padding-bottom: 2px; }   /* botões em uma fileira que rola para o lado */
  .actbar .grp .btn, .actbar > .btn { flex: 1 0 auto; white-space: nowrap; }
  .actbar .grp .btn.primary { order: -1; }                       /* Salvar sempre aparece primeiro */
  .actbar .tm { width: 100%; margin: 0; font-size: 13px; }

  /* ajuda, avisos e assistente */
  #toasts { left: 10px; right: 10px; bottom: 10px; }
  .as-flut { bottom: 14px; }

  /* tabelas */
  .table-wrap, .table-wrap.pick { max-height: none; overflow-x: auto; }
  .card.table-wrap, .card > .table-wrap { overflow-x: auto; }
}

/* tabelas viram cartões (cada linha, um cartão; o nome da coluna aparece à esquerda do valor) */
@media (max-width: 700px) {
  table[data-stack], table[data-stack] tbody, table[data-stack] tfoot, table[data-stack] tr, table[data-stack] td { display: block; width: 100%; min-width: 0; }
  table[data-stack] thead { display: none; }
  .table-wrap:has(table[data-stack]) { overflow: visible; }
  .card:has(> .table-wrap > table[data-stack]) { background: transparent; box-shadow: none; border: 0; padding: 0; }
  table[data-stack] tr { margin: 0 0 10px; padding: 6px 12px; background: #fff; border: 1px solid var(--line); border-radius: 12px; box-shadow: 0 1px 2px rgba(15, 15, 30, .05); }
  table[data-stack] tr[onclick], table[data-stack] tr.click, table[data-stack] tr[data-id] { cursor: pointer; }
  table[data-stack] td, table[data-stack] td:first-child { display: flex; align-items: center; justify-content: space-between; gap: 12px; padding: 7px 0 !important; border: 0; border-bottom: 1px solid #f0f1f6; text-align: right; white-space: normal !important; min-height: 0; word-break: break-word; }
  table[data-stack] td:last-child { border-bottom: 0; }
  table[data-stack] td::before { content: attr(data-label); flex: 0 0 36%; max-width: 40%; text-align: left; font-size: 12px; font-weight: 500; color: var(--muted); }
  table[data-stack] td[data-label=""]::before { display: none; }
  table[data-stack] td[data-label=""] { justify-content: flex-end; flex-wrap: wrap; }
  table[data-stack] td:first-child { font-weight: 600; }
  table[data-stack] td input, table[data-stack] td select { width: 100%; min-width: 0; text-align: right; }
  table[data-stack] td[data-vazio] { display: none; }
  table[data-stack][data-plain] td, table[data-stack][data-plain] td:first-child { justify-content: flex-start; text-align: left; border-bottom: 0; padding: 2px 0 !important; }
  table[data-stack][data-plain] td.r { justify-content: flex-start; }
  table[data-stack] td:has(input:not([type=checkbox]):not([type=radio]), select, textarea) { flex-direction: column; align-items: stretch; text-align: left; gap: 4px; }
  table[data-stack] td:has(input:not([type=checkbox]):not([type=radio]), select, textarea)::before { flex: none; max-width: 100%; }
  table[data-stack] td input:not([type=checkbox]):not([type=radio]), table[data-stack] td select { text-align: left; }
  /* itens da OS/orçamento: cada item é um cartão com os campos um embaixo do outro */
  table.ltable tr { position: relative; padding-bottom: 10px; }
  table.ltable td { display: block !important; text-align: left; padding: 6px 0 !important; border-bottom: 0 !important; }
  table.ltable td::before { display: block; max-width: none; margin-bottom: 3px; }
  table.ltable td.c { text-align: left !important; display: block !important; font-weight: 700; color: var(--muted); padding-bottom: 0 !important; }
  table.ltable td.c::before { display: inline; content: 'Item '; margin: 0; font-size: 13px; }
  table.ltable td.ln-item .sub, table.ltable td .med, table.ltable .aj-row { display: flex; gap: 6px; align-items: center; flex-wrap: wrap; margin-top: 4px; }
  table.ltable .aj-row input { flex: 1 1 90px; width: auto; } table.ltable .aj-row select { flex: 0 0 70px; width: auto; }
  table.ltable td .med input { flex: 1 1 70px; width: auto; }
  table.ltable td:last-child { position: absolute; top: 4px; right: 4px; width: auto; padding: 0 !important; }
  table.ltable td:last-child::before { display: none; }
  table.ltable td.r { text-align: left; }
  table[data-stack] td.empty, table[data-stack] td[colspan] { display: block; text-align: center; }
  table[data-stack] td[colspan]::before { display: none; }
  table[data-stack] tfoot tr { background: #f6f7fb; }
  .comp-scroll, .comp-table { overflow-x: auto; }                   /* composição de custos: segue em rolagem, pois tem campos demais */
}

@media (max-width: 420px) {
  .kpis, .kpis.small { grid-template-columns: 1fr 1fr; }
  .kpi b, .kpis.small .kpi b { font-size: 19px; }
  .hm-metrics { grid-template-columns: 1fr 1fr; }
  .sc { grid-template-columns: 1fr; } .sc .art { height: 120px; }
}

@media (max-width: 900px) { .toolbar, .pb-tools { flex-wrap: wrap; } .view span[style*="margin-left:auto"] { margin-left: 0 !important; flex-wrap: wrap; } }
@media (max-width: 900px) { .seg button, .stab, .setores-tabs > *, .chip-row > * { white-space: nowrap; } }

/* ===== polimento visual do celular (v0.18): só abaixo de 900 px; o computador não muda ===== */
@media (max-width: 900px) {
  :root { --radius: 14px; --shadow: 0 1px 2px rgba(20, 20, 45, .05), 0 8px 20px -10px rgba(20, 20, 45, .16); }
  body { font-size: 15px; line-height: 1.45; -webkit-font-smoothing: antialiased; }

  /* topo: título da tela em destaque e a faixa de quatro cores da marca */
  .m-top { height: 56px; background: var(--ink); box-shadow: 0 3px 0 0 transparent; }
  .m-top::after { content: ""; position: absolute; left: 0; right: 0; bottom: -3px; height: 3px; background: linear-gradient(90deg, var(--orange) 0 25%, var(--purple) 25% 50%, var(--green) 50% 75%, var(--blue) 75%); }
  .m-top strong { display: none; }
  .m-top span#m-tit { margin: 0; font-size: 17px; font-weight: 700; letter-spacing: -.01em; color: #fff; }
  .m-top span#m-tit svg { display: none; }
  .m-top #m-btn { background: rgba(255, 255, 255, .08); }
  .view { padding: 18px 14px 24px; }

  /* barra de atalhos no rodapé */
  .m-tab { display: grid; grid-auto-flow: column; grid-auto-columns: 1fr; flex: none; background: #fff; border-top: 1px solid var(--line); padding: 6px 4px calc(6px + env(safe-area-inset-bottom)); position: relative; z-index: 30; box-shadow: 0 -6px 18px -12px rgba(20, 20, 45, .25); }
  .m-tab button { display: flex; flex-direction: column; align-items: center; gap: 3px; min-height: 52px; padding: 6px 2px; border: 0; background: none; color: var(--muted); font-size: 11px; font-weight: 600; letter-spacing: .01em; border-radius: 12px; cursor: pointer; }
  .m-tab button.on { color: var(--primary); }
  .m-tab button.on svg { stroke-width: 2.2; }
  .m-tab button.on::before { content: ""; position: absolute; width: 28px; height: 3px; border-radius: 3px; background: var(--primary); margin-top: -7px; }
  .m-tab button:active { background: #f1f2f7; }
  #toasts { bottom: 76px; }
  .as-flut { bottom: 76px !important; }

  /* menu em gaveta com ícones de linha */
  .nav-item .ico, .nav-head .ico { display: inline-flex; width: 22px; justify-content: center; font-size: 0; }
  .nav-item .ico svg, .nav-head .ico svg { display: block; }
  .nav-item { font-size: 15px; border-radius: 10px; }
  .nav-item.active { background: rgba(255, 255, 255, .09); }

  /* títulos e textos de apoio */
  .page-head h1 { font-size: 23px; font-weight: 700; letter-spacing: -.02em; line-height: 1.15; }
  .page-head p { font-size: 13.5px; line-height: 1.45; color: var(--muted); margin-top: 4px; }
  h2 { letter-spacing: -.01em; }

  /* cartões, indicadores e botões */
  .card { border-color: #eceef4; }
  .kpi { padding: 14px 14px 12px; }
  .kpi b, .kpis.small .kpi b, .hm-m b { font-size: 24px; font-weight: 700; letter-spacing: -.02em; font-variant-numeric: tabular-nums; line-height: 1.1; }
  .kpi span { font-size: 12.5px; color: var(--muted); }
  .btn { border-radius: 12px; font-weight: 600; padding: 9px 14px; }
  .btn.primary { box-shadow: 0 6px 14px -8px rgba(122, 20, 212, .7); }
  .pill, .badge { font-weight: 600; padding: 3px 10px; border-radius: 999px; }
  input, select, textarea { border-radius: 12px; border-color: #dcdfea; }
  input:focus-visible, select:focus-visible, textarea:focus-visible { outline: 2px solid var(--primary); outline-offset: 1px; box-shadow: 0 0 0 5px rgba(122, 20, 212, .14); }
  .tabs button, .seg button, .stab { border-radius: 999px; font-weight: 600; }

  /* início: números do dia primeiro, depois o que pede ação */
  .view:has(> .hm-top) { display: flex; flex-direction: column; }
  .view:has(> .hm-top) > * { order: 5; flex: none; }
  .view > .hm-top { order: 0; margin-bottom: 4px; }
  .view > .hm-metrics { order: 1; margin-bottom: 14px; }
  .view > .hm-hoje { order: 2; }
  .view > .hm-guia { order: 9; }
  .hm-top h1 { font-size: 24px; letter-spacing: -.02em; }
  .hm-emp { display: none; }
  .hm-metrics { border-radius: 16px; }
  .hm-m { padding: 14px; }
  .hm-hoje { padding: 14px; border-radius: 16px; }
  .hm-hoje h2 { font-size: 16px; margin-bottom: 10px; }
  .hh-b { border-left-width: 3px; padding-left: 12px; margin-bottom: 8px; }
  .hh-b h3 { font-size: 12.5px; letter-spacing: .02em; text-transform: none; font-weight: 700; color: var(--text); }
  .hh-b li { padding: 6px 0; font-size: 14.5px; border-bottom: 1px solid #f0f1f6; }
  .hh-b li:last-child { border-bottom: 0; }

  /* cartões que substituem as tabelas: mais ar, nome da coluna discreto, primeiro dado em destaque */
  table[data-stack] tr { padding: 4px 14px; border-radius: 16px; border-color: #eceef4; box-shadow: var(--shadow); }
  table[data-stack] td { font-size: 14.5px; font-variant-numeric: tabular-nums; }
  table[data-stack] td::before { font-size: 11.5px; letter-spacing: .01em; font-weight: 600; color: #8a8ea0; }
  table[data-stack] td:first-child { font-size: 16px; font-weight: 700; padding-top: 10px !important; }
  table[data-stack] td.r, table[data-stack] td .money { font-weight: 600; }

  /* produção por setor e kanban: cartões mais legíveis */
  .sc, .pb .cards > *, .funil > * { border-radius: 16px; }
  .sc header b { font-size: 16px; }
  .sc footer .btn.primary, .pb .btn.primary { min-height: 44px; }

  /* modais, barra de ações e avisos */
  .modal-head h2 { font-size: 18px; letter-spacing: -.01em; }
  .actbar { border-radius: 16px 16px 0 0; box-shadow: 0 -10px 24px -14px rgba(20, 20, 45, .3); }
}
@media (max-width: 900px) and (prefers-reduced-motion: no-preference) {
  .m-tab button, .btn, .nav-item { transition: background .15s ease, color .15s ease, transform .1s ease; }
  .btn:active, .m-tab button:active { transform: scale(.97); }
}

/* filtros das listas mais compactos no celular */
@media (max-width: 900px) {
  .tabs button { white-space: nowrap; flex: none; }
  .tabs { display: flex; gap: 8px; overflow-x: auto; padding-bottom: 4px; scrollbar-width: none; }
  .tabs::-webkit-scrollbar { display: none; }
  .toolbar select { flex: 1 1 calc(50% - 4px); width: auto; min-width: 0; }
  .toolbar input[type=search], .toolbar input[type=text].grow, .toolbar .grow { flex: 1 1 100%; }
  .toolbar label.inl { flex: 1 1 calc(50% - 4px); display: flex; align-items: center; gap: 6px; min-width: 0; font-size: 12.5px; color: var(--muted); }
  .toolbar label.inl input { flex: 1; width: auto; min-width: 0; }
  .toolbar input[type=date] { flex: 1 1 calc(50% - 4px); width: auto; min-width: 0; }
}

@media (max-width: 700px) { table[data-stack] td .tv { display: block; min-width: 0; text-align: right; } table[data-stack] td .tv > * { display: block; } table[data-stack] td .tv > strong, table[data-stack] td .tv > b { display: block; } table[data-stack] td .tv .pill { display: inline-block; } }
