/* =====================================================================
   Sistema de Controle de Maquinas - estilos da aplicacao
   Base: Bootstrap 5. Aqui ficam apenas os ajustes proprios.
   Regra de ouro: NADA pode ultrapassar a largura da viewport.
   ===================================================================== */

:root {
  --brand: #1f3a5f;
  --brand-soft: #eef3f9;
  --brand-dark: #16293f;
  --positive: #166534;
  --negative: #b91c1c;
  --surface: #ffffff;
  --page-bg: #f4f6f9;
  --border: #e2e7ee;
  --muted: #6b7280;
  --radius: 12px;
  --shadow-sm: 0 1px 2px rgba(16, 24, 40, .05);
  --shadow-md: 0 4px 16px rgba(16, 24, 40, .08);
  --topbar-h: 56px;
}

/* --- Nada estoura a tela, em nenhuma largura --- */
*, *::before, *::after { box-sizing: border-box; }

html, body {
  max-width: 100%;
  overflow-x: hidden;   /* trava definitiva contra rolagem horizontal */
}

body {
  background: var(--page-bg);
  color: #1f2933;
  font-size: .95rem;
  -webkit-text-size-adjust: 100%;
}

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

/* Quebra palavras longas (e-mails, URLs) em vez de alargar o layout. */
.text-break-anywhere { overflow-wrap: anywhere; word-break: break-word; }
td, th, p, h1, h2, h3, h4, h5, h6, .card-title, .list-group-item { overflow-wrap: anywhere; }

/* =====================  Layout  ===================== */
.app-shell { min-height: 100vh; display: flex; flex-direction: column; }

.app-topbar {
  height: var(--topbar-h);
  background: var(--brand);
  color: #fff;
  position: sticky;
  top: 0;
  z-index: 1030;
}
.app-topbar .navbar-brand { color: #fff; font-weight: 600; font-size: 1rem; }
.app-topbar .btn-icon {
  color: #fff;
  border: none;
  background: transparent;
  min-height: 44px;      /* alvo de toque confortavel no celular */
  min-width: 44px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
}
.app-topbar .btn-icon:hover, .app-topbar .btn-icon:focus-visible { color: #fff; background: rgba(255,255,255,.12); }
.app-topbar #globalSearchInput { min-height: 36px; }   /* campo do topo, fora do alcance da regra de toque */

.app-body { flex: 1; display: flex; min-width: 0; }

.app-sidebar {
  width: 232px;
  flex: 0 0 232px;
  background: var(--surface);
  border-right: 1px solid var(--border);
  padding: 1rem .75rem;
  overflow-y: auto;
}
.app-sidebar .nav-link {
  color: #46505c;
  border-radius: 8px;
  padding: .6rem .75rem;
  font-weight: 500;
  display: flex;
  align-items: center;
  gap: .6rem;
  margin-bottom: .15rem;
}
.app-sidebar .nav-link:hover { background: var(--brand-soft); color: var(--brand); }
.app-sidebar .nav-link.active { background: var(--brand); color: #fff; }

.app-main {
  flex: 1;
  min-width: 0;          /* essencial: permite o flex encolher abaixo do conteudo */
  padding: 1.25rem;
  max-width: 100%;
}

@media (max-width: 991.98px) {
  .app-sidebar {
    position: fixed;
    top: var(--topbar-h);
    bottom: 0;
    left: 0;
    z-index: 1040;
    transform: translateX(-100%);
    transition: transform .2s ease;
    box-shadow: var(--shadow-md);
  }
  .app-sidebar.open { transform: translateX(0); }
  .app-main { padding: 1rem .85rem; }
}

@media (max-width: 575.98px) {
  .app-main { padding: .85rem .7rem; }
  body { font-size: .93rem; }
}

.sidebar-backdrop {
  position: fixed; inset: var(--topbar-h) 0 0 0;
  background: rgba(15, 23, 42, .45);
  z-index: 1035;
}

/* =====================  Cards e metricas  ===================== */
.card {
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow-sm);
}
.card-header {
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  font-weight: 600;
  border-radius: var(--radius) var(--radius) 0 0 !important;
}

.metric-card { height: 100%; }
.metric-card .metric-label {
  font-size: .74rem;
  text-transform: uppercase;
  letter-spacing: .04em;
  color: var(--muted);
  font-weight: 600;
}
.metric-card .metric-value {
  font-size: clamp(1.05rem, 3.4vw, 1.4rem);
  font-weight: 700;
  line-height: 1.25;
  overflow-wrap: normal;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.metric-card .metric-value.is-long { font-size: clamp(.95rem, 3vw, 1.2rem); }
.metric-card .metric-icon {
  width: 38px; height: 38px;
  border-radius: 10px;
  display: grid; place-items: center;
  background: var(--brand-soft);
  color: var(--brand);
  flex: 0 0 38px;
}

.value-positive { color: var(--positive); }
.value-negative { color: var(--negative); }

/* =====================  Tabelas responsivas  =====================
   Em telas < 768px cada linha vira um cartao empilhado, usando
   data-label para rotular. Isso elimina a rolagem horizontal sem
   esconder nenhuma informacao.
   ================================================================ */
.table-responsive-cards { width: 100%; max-width: 100%; }
.table-responsive-cards table { width: 100%; margin-bottom: 0; }

/* Em telas medias/grandes, uma tabela com muitas colunas rola DENTRO do
   proprio cartao. A pagina em si nunca ganha rolagem horizontal. */
@media (min-width: 768px) {
  .table-responsive-cards { overflow-x: auto; -webkit-overflow-scrolling: touch; }
  .table-responsive-cards table { min-width: 720px; }

  /* Tabelas com muitas colunas de dinheiro (o relatorio) precisam de mais
     folga: espremidas, a data quebra em cinco linhas e fica ilegivel. */
  .table-responsive-cards table.table-wide { min-width: 1180px; }
}

@media (max-width: 767.98px) {
  .table-responsive-cards thead { display: none; }
  .table-responsive-cards table,
  .table-responsive-cards tbody,
  .table-responsive-cards tr,
  .table-responsive-cards td { display: block; width: 100%; }

  .table-responsive-cards tr {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: 10px;
    margin-bottom: .6rem;
    padding: .35rem .15rem;
  }
  .table-responsive-cards td {
    border: none !important;
    display: flex;
    justify-content: space-between;
    align-items: baseline;
    gap: .75rem;
    padding: .35rem .75rem;
    text-align: right;
  }
  .table-responsive-cards td::before {
    content: attr(data-label);
    font-weight: 600;
    color: var(--muted);
    font-size: .78rem;
    text-transform: uppercase;
    letter-spacing: .02em;
    text-align: left;
    flex: 0 0 auto;
  }
  .table-responsive-cards td[data-label=""]::before { content: none; }
  .table-responsive-cards td.cell-actions { justify-content: flex-end; padding-top: .5rem; }
}

.table > :not(caption) > * > * { padding: .65rem .6rem; }
.table thead th {
  font-size: .78rem;
  text-transform: uppercase;
  letter-spacing: .03em;
  color: var(--muted);
  font-weight: 600;
  white-space: nowrap;
}

/* =====================  Formularios  ===================== */
.form-label { font-weight: 600; font-size: .85rem; margin-bottom: .3rem; }
.form-control, .form-select {
  border-radius: 9px;
  border-color: #cfd6e0;
  min-height: 44px;          /* alvo de toque confortavel no celular */
  font-size: 1rem;           /* >=16px evita o zoom automatico no iOS */
}
.form-control:focus, .form-select:focus {
  border-color: var(--brand);
  box-shadow: 0 0 0 .2rem rgba(31, 58, 95, .12);
}
textarea.form-control { min-height: 88px; }
.invalid-feedback { display: block; min-height: 0; }
.form-control.is-invalid, .form-select.is-invalid { border-color: var(--negative); }

.btn { border-radius: 9px; font-weight: 500; }
.btn-lg, .btn-touch { min-height: 48px; }
.btn-primary { background: var(--brand); border-color: var(--brand); }
.btn-primary:hover, .btn-primary:focus { background: var(--brand-dark); border-color: var(--brand-dark); }
.btn-outline-primary { color: var(--brand); border-color: var(--brand); }
.btn-outline-primary:hover { background: var(--brand); border-color: var(--brand); }

/* =====================  Coleta (mobile first)  ===================== */
.step-badge {
  width: 26px; height: 26px;
  border-radius: 50%;
  background: var(--brand);
  color: #fff;
  display: inline-grid; place-items: center;
  font-size: .8rem; font-weight: 700;
  flex: 0 0 26px;
}
.step-card { border-left: 3px solid var(--brand); }
.step-card.is-disabled { opacity: .55; pointer-events: none; }

.reading-box {
  background: var(--brand-soft);
  border: 1px solid #dbe4f0;
  border-radius: 10px;
  padding: .8rem .9rem;
}
.reading-box .reading-label {
  font-size: .72rem; text-transform: uppercase; letter-spacing: .04em;
  color: var(--muted); font-weight: 600;
}
.reading-box .reading-value { font-size: 1.15rem; font-weight: 700; overflow-wrap: anywhere; }

.result-box {
  background: #0f2f4f;
  color: #fff;
  border-radius: 12px;
  padding: 1rem;
}
.result-box .result-value { font-size: clamp(1.5rem, 7vw, 2rem); font-weight: 800; line-height: 1.1; }
.result-box .result-line { font-size: .85rem; opacity: .85; }

.photo-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(88px, 1fr)); gap: .5rem; }
.photo-thumb {
  position: relative; aspect-ratio: 1; border-radius: 9px; overflow: hidden;
  border: 1px solid var(--border); background: #f1f3f7;
}
.photo-thumb img { width: 100%; height: 100%; object-fit: cover; }
.photo-thumb .btn-remove {
  position: absolute; top: 3px; right: 3px;
  width: 26px; height: 26px; border-radius: 50%;
  border: none; background: rgba(17, 24, 39, .75); color: #fff;
  display: grid; place-items: center; font-size: .8rem; line-height: 1;
}

/* =====================  Busca global  ===================== */
.global-search { position: relative; max-width: 420px; width: 100%; }
.global-search-results {
  position: absolute; top: calc(100% + 4px); left: 0; right: 0;
  background: #fff; border: 1px solid var(--border); border-radius: 10px;
  box-shadow: var(--shadow-md); z-index: 1050; max-height: 60vh; overflow-y: auto;
}
.global-search-results .list-group-item { border: none; border-bottom: 1px solid #f1f3f7; }
.global-search-results .list-group-item:last-child { border-bottom: none; }

/* =====================  Estados  ===================== */
.state-block { padding: 2.2rem 1rem; text-align: center; color: var(--muted); }
.state-block .state-icon { font-size: 2rem; opacity: .45; margin-bottom: .5rem; }

.skeleton {
  background: linear-gradient(90deg, #eef0f4 25%, #f7f8fa 50%, #eef0f4 75%);
  background-size: 200% 100%;
  animation: skeleton 1.2s infinite;
  border-radius: 6px;
}
@keyframes skeleton { 0% { background-position: 200% 0; } 100% { background-position: -200% 0; } }
@media (prefers-reduced-motion: reduce) { .skeleton { animation: none; } }

.badge-status { font-weight: 600; font-size: .74rem; padding: .34em .6em; }

/* Toasts sempre dentro da tela */
/* O container do toast cobre um canto inteiro da tela. Sem isto, a area
   vazia dele engole cliques em botoes que ficam embaixo - no celular, o
   "Salvar" do rodape do modal. O container fica transparente ao toque; so
   o toast em si volta a receber cliques. */
.toast-container {
  z-index: 1090;
  max-width: calc(100vw - 1.5rem);
  pointer-events: none;
  padding-bottom: env(safe-area-inset-bottom, 0px);
}
.toast-container .toast { pointer-events: auto; }

/* No celular, o canto inferior direito e exatamente onde ficam os botoes de
   acao - o "Salvar" do rodape de um modal, por exemplo. Um toast ali cobre o
   botao e o toque vai parar no aviso em vez da acao. Por isso, em tela
   pequena, os avisos aparecem no topo. */
@media (max-width: 767.98px) {
  .toast-container {
    top: calc(var(--topbar-h) + .5rem) !important;
    bottom: auto !important;
    left: 0 !important;
    right: 0 !important;
    align-items: center;
    max-width: 100%;
  }
}

/* Modais: corpo rolavel em telas baixas */
.modal-body { max-height: calc(100vh - 210px); overflow-y: auto; }

/* Graficos nunca alargam a pagina */
.chart-wrap { position: relative; width: 100%; max-width: 100%; overflow: hidden; }

/* Paginacao compacta no celular */
@media (max-width: 575.98px) {
  .pagination { flex-wrap: wrap; gap: .2rem; }
  .page-link { min-width: 40px; text-align: center; }
}

/* ---------------------------------------------------------------
   Selecao por clique (filtros do relatorio)

   Os filtros do relatorio sao escolhidos tocando nos itens, nao em
   dropdowns: no celular, abrir uma lista suspensa para marcar sete
   maquinas e um castigo. Cada item e um alvo de toque de 44px.
   --------------------------------------------------------------- */
.pick-list {
  display: flex;
  flex-wrap: wrap;
  gap: .4rem;
  max-height: 210px;
  overflow-y: auto;
  padding: .5rem;
  border: 1px solid var(--bs-border-color, #dee2e6);
  border-radius: .5rem;
  background: #fff;
  -webkit-overflow-scrolling: touch;
}

.pick-item {
  display: inline-flex;
  align-items: center;
  gap: .35rem;
  min-height: 44px;
  padding: .35rem .75rem;
  border: 1px solid var(--bs-border-color, #dee2e6);
  border-radius: 999px;
  background: #fff;
  color: inherit;
  font-size: .875rem;
  line-height: 1.2;
  text-align: left;
  cursor: pointer;
  transition: background-color .12s ease, border-color .12s ease;
}

.pick-item:hover { border-color: #9aa5b1; }
.pick-item:focus-visible { outline: 3px solid rgba(31, 58, 95, .35); outline-offset: 2px; }

.pick-item[aria-pressed="true"] {
  background: #1f3a5f;
  border-color: #1f3a5f;
  color: #fff;
  font-weight: 600;
}

.pick-item[aria-pressed="true"]::before { content: "\2713"; font-weight: 700; }

.pick-empty { color: #6b7280; font-size: .875rem; padding: .5rem; }

/* Impressao */
@media print {
  .app-topbar, .app-sidebar, .no-print { display: none !important; }
  .app-main { padding: 0; }
  .card { box-shadow: none; border-color: #ccc; }
}
