:root {
  --brand: #00753A;          /* verde Recheio (logótipo) */
  --brand-dark: #005A2D;
  --accent: #3FA66B;
  --accent-dark: #2E8A55;
  --sidebar-bg: #062B18;
  --sidebar-bg2: #0B3D23;
  --ink: #1e293b;
  --muted: #64748b;
  --line: #e2e8f0;
  --bg: #f1f5f9;
  --radius: 12px;
}

* { box-sizing: border-box; }
body { background: var(--bg); color: var(--ink); font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif; }

/* ----------------------------------------------------------------- Shell */
.app-shell { display: flex; min-height: 100vh; }

.sidebar {
  width: 260px; flex-shrink: 0; background: var(--sidebar-bg);
  color: #cbd5e1; display: flex; flex-direction: column;
  position: sticky; top: 0; height: 100vh; padding: 18px 14px;
}
.brand {
  display: flex; align-items: center; gap: 10px; color: #fff;
  font-size: 1.05rem; letter-spacing: .5px; padding: 6px 8px 18px;
  border-bottom: 1px solid rgba(255,255,255,.08); margin-bottom: 12px;
}
.brand i { color: var(--brand); font-size: 1.4rem; }
.brand strong { color: var(--accent); }

.side-nav { display: flex; flex-direction: column; gap: 2px; flex: 1; overflow-y: auto; }
.side-section {
  font-size: .68rem; text-transform: uppercase; letter-spacing: .12em;
  color: #64748b; padding: 14px 10px 6px; font-weight: 600;
}
.side-link {
  display: flex; align-items: center; gap: 11px; padding: 10px 12px;
  color: #cbd5e1; text-decoration: none; border-radius: 9px; font-size: .92rem;
  transition: background .15s, color .15s;
}
.side-link i { font-size: 1.05rem; width: 20px; text-align: center; }
.side-link:hover { background: var(--sidebar-bg2); color: #fff; }
.side-link.active { background: var(--brand); color: #fff; font-weight: 600; }

.sidebar-footer { border-top: 1px solid rgba(255,255,255,.08); padding-top: 12px; }
.side-user { display: flex; align-items: center; gap: 10px; color: #e2e8f0; text-decoration: none; padding: 6px 8px; }
.side-user i { font-size: 1.6rem; color: var(--brand); }
.side-user span { display: flex; flex-direction: column; line-height: 1.2; }
.side-user small { color: #94a3b8; font-size: .72rem; }

/* ------------------------------------------------------------------ Main */
.main { flex: 1; min-width: 0; display: flex; flex-direction: column; }
.topbar {
  display: flex; align-items: center; gap: 16px; background: #fff;
  padding: 18px 28px; border-bottom: 1px solid var(--line); position: sticky; top: 0; z-index: 20;
}
.topbar-title { flex: 1; }
.page-title { font-size: 1.3rem; font-weight: 700; margin: 0; }
.page-subtitle { margin: 2px 0 0; color: var(--muted); font-size: .85rem; }
.page-actions { display: flex; gap: 8px; flex-wrap: wrap; }

/* Sino de alertas no topo */
.topbar-bell { position: relative; display: grid; place-items: center; width: 40px; height: 40px; border-radius: 10px; color: var(--ink); text-decoration: none; font-size: 1.25rem; transition: background .15s; }
.topbar-bell:hover { background: var(--bg); color: var(--brand); }
.notif-badge { position: absolute; top: 2px; right: 2px; min-width: 18px; height: 18px; padding: 0 5px; border-radius: 9px; background: var(--accent); color: #fff; font-size: .7rem; font-weight: 700; line-height: 18px; text-align: center; }
.notif-unread { background: #fff8ec; }
.notif-dot { width: 10px; height: 10px; border-radius: 50%; flex-shrink: 0; }
.content { padding: 26px 28px; flex: 1; }
.messages { padding: 12px 28px 0; }

/* ----------------------------------------------------------------- Cards */
.card { border: 1px solid var(--line); border-radius: var(--radius); box-shadow: 0 1px 2px rgba(15,23,42,.04); }
.card-clean { background: #fff; border: 1px solid var(--line); border-radius: var(--radius); padding: 20px; }

.stat-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(210px, 1fr)); gap: 16px; }
.stat {
  background: #fff; border: 1px solid var(--line); border-radius: var(--radius);
  padding: 20px; display: flex; align-items: center; gap: 16px;
}
.stat .icon { width: 52px; height: 52px; border-radius: 12px; display: grid; place-items: center; font-size: 1.5rem; color: #fff; }
.stat .icon.indigo { background: var(--brand); }
.stat .icon.green { background: #059669; }
.stat .icon.amber { background: var(--accent); }
.stat .icon.sky { background: #0284c7; }
.stat .value { font-size: 1.7rem; font-weight: 700; line-height: 1; }
.stat .label { color: var(--muted); font-size: .85rem; margin-top: 4px; }

/* --------------------------------------------------------------- Tables */
.table-wrap { background: #fff; border: 1px solid var(--line); border-radius: var(--radius); overflow: hidden; }
.table { margin: 0; }
.table thead th { background: #f8fafc; color: var(--muted); font-size: .76rem; text-transform: uppercase; letter-spacing: .04em; border-bottom: 1px solid var(--line); white-space: nowrap; }
.table td { vertical-align: middle; }
.table-grid input.form-control-sm, .table-grid textarea { min-width: 120px; }
.table-grid .btn-light:hover .bi-arrow-up,
.table-grid .btn-light:hover .bi-arrow-down { color: var(--accent); }

.thumb { width: 46px; height: 46px; object-fit: cover; border-radius: 8px; border: 1px solid var(--line); background: #f8fafc; }
.thumb-lg { width: 100%; max-width: 320px; border-radius: var(--radius); border: 1px solid var(--line); }
.thumb-ph { width: 46px; height: 46px; border-radius: 8px; background: #f1f5f9; display: grid; place-items: center; color: #cbd5e1; }

/* --------------------------------------------------------------- Badges */
.badge-role { background: var(--brand); }
.status-RASCUNHO { background: #94a3b8; }
.status-EM_PRODUCAO { background: #0284c7; }
.status-EM_REVISAO { background: #d97706; }
.status-APROVADO { background: #059669; }
.status-CONCLUIDO { background: #334155; }
.status-COMERCIAL { background:#e8f0fe; color:#1a56db; }
.status-APROVACAO_INTERNA { background:#fef3c7; color:#92400e; }
.status-APROVACAO_FINAL { background:#dcfce7; color:#166534; }
.status-ABERTO { background: #d97706; }
.status-EM_CURSO { background: #0284c7; }
.status-RESOLVIDO { background: #059669; }

/* ---------------------------------------------------------- Image gallery */
.gallery { display: grid; grid-template-columns: repeat(auto-fill, minmax(160px, 1fr)); gap: 16px; }
.gallery-item { background: #fff; border: 1px solid var(--line); border-radius: var(--radius); overflow: hidden; }
.gallery-item .img { aspect-ratio: 1; background: #f8fafc; display: grid; place-items: center; overflow: hidden; }
.gallery-item .img img { width: 100%; height: 100%; object-fit: cover; }
/* Selos e logos: mostrar inteiros (não cortar), sobre xadrez para ver a transparência. */
.gallery-item .grafico-img { background: repeating-conic-gradient(#f1f5f9 0% 25%, #fff 0% 50%) 50% / 16px 16px; }
.gallery-item .grafico-img img { object-fit: contain; padding: 8px; }
.gallery-item .meta { padding: 8px 10px; font-size: .78rem; }
.gallery-item .meta .ean { font-weight: 600; word-break: break-all; }
.gallery-actions { display: flex; gap: 4px; padding: 0 10px 10px; }

/* ------------------------------------------------------------------ Auth */
.auth-body { background: linear-gradient(135deg, var(--sidebar-bg), var(--brand)); }
.auth-wrap { min-height: 100vh; display: flex; flex-direction: column; align-items: center; justify-content: center; padding: 24px; }
.auth-card { background: #fff; border-radius: 16px; padding: 36px; width: 100%; max-width: 410px; box-shadow: 0 20px 50px rgba(0,0,0,.3); }
.auth-logo { display: block; max-width: 220px; width: 100%; height: auto; margin: 0 auto 16px; }
.auth-brand { font-size: 1.15rem; letter-spacing: .5px; text-align: center; margin-bottom: 6px; }
.auth-brand i { color: var(--brand); }
.auth-brand strong { color: var(--brand); }
.auth-title { font-size: 1.05rem; font-weight: 600; text-align: center; color: var(--muted); margin-bottom: 22px; }
.auth-foot { color: #cbd5e1; font-size: .8rem; margin-top: 18px; }
.auth-card label { font-weight: 600; font-size: .85rem; margin-bottom: 4px; }
.auth-card .form-control { padding: 11px 13px; }

/* --------------------------------------------------------------- Buttons */
.btn-brand { background: var(--brand); border-color: var(--brand); color: #fff; }
.btn-brand:hover { background: var(--brand-dark); border-color: var(--brand-dark); color: #fff; }

/* PDF / provas */
.pdf-layout { display: grid; grid-template-columns: 1fr 320px; gap: 18px; align-items: start; }
.pdf-stage { background: #475569; border-radius: var(--radius); padding: 18px; max-height: 80vh; overflow: auto; }
.pdf-page { position: relative; margin: 0 auto 16px; box-shadow: 0 4px 14px rgba(0,0,0,.3); background: #fff; }
.pdf-overlay { position: absolute; inset: 0; cursor: crosshair; }
.pdf-pin { position: absolute; transform: translate(-50%, -100%); background: var(--brand); color: #fff; border: 2px solid #fff;
  width: 26px; height: 26px; border-radius: 50% 50% 50% 0; transform-origin: bottom; rotate: -45deg;
  display: grid; place-items: center; font-size: .7rem; font-weight: 700; cursor: pointer; box-shadow: 0 2px 6px rgba(0,0,0,.4); }
.pdf-pin span { rotate: 45deg; }
.pdf-pin.resolved { background: #059669; }
.comments-panel { background: #fff; border: 1px solid var(--line); border-radius: var(--radius); padding: 16px; position: sticky; top: 90px; max-height: 80vh; overflow: auto; }
.comment-item { border: 1px solid var(--line); border-radius: 9px; padding: 10px 12px; margin-bottom: 10px; font-size: .85rem; }
.comment-item.resolved { opacity: .6; }
.comment-item .meta { color: var(--muted); font-size: .72rem; display: flex; justify-content: space-between; }

@media (max-width: 992px) {
  .sidebar { position: fixed; left: 0; top: 0; z-index: 1000; transform: translateX(-100%); transition: transform .2s; }
  .sidebar.is-open { transform: translateX(0); }
  .pdf-layout { grid-template-columns: 1fr; }
}
.empty-state { text-align: center; padding: 50px 20px; color: var(--muted); }
.empty-state i { font-size: 2.6rem; color: #cbd5e1; display: block; margin-bottom: 10px; }

/* Separador de página na grelha de itens (clicável para mostrar/ocultar) */
.page-sep td { background: #eef2f7; font-weight: 600; font-size: .8rem; color: var(--muted); padding: 8px 12px; border-top: 2px solid var(--line); }
.page-sep { cursor: pointer; user-select: none; }
.page-sep:hover td { background: #e4eaf2; }
.page-chevron { display: inline-block; transition: transform .15s; color: var(--brand); }
tbody.page-group.collapsed .page-chevron { transform: rotate(-90deg); }
tbody.page-group.collapsed .item-row { display: none; }
/* Pesquisa nos registos: enquanto filtra, mostra as linhas que batem certo em
   todas as páginas (mesmo recolhidas) e esconde as páginas sem resultados. */
table.filtrando tbody.page-group.collapsed .item-row { display: table-row; }
table.filtrando tbody.page-group .item-row.filtro-oculto { display: none; }
table.filtrando tbody.page-group.sem-resultados { display: none; }
td.sel-cell { cursor: pointer; user-select: none; }
body.arrastando-selecao, body.arrastando-selecao * { user-select: none; cursor: ns-resize; }
/* A barra das ações em massa acompanha o scroll, logo abaixo da topbar (também
   sticky): --topbar-h é a altura real da topbar, medida em app_base.html. */
.massa-barra { position: sticky; top: var(--topbar-h, 0px); z-index: 15; background: #fff; border: 1px solid var(--line); border-radius: var(--radius); padding: 10px 12px; transition: box-shadow .15s; }
.massa-barra.presa { box-shadow: 0 6px 16px rgba(15, 23, 42, .12); }
/* Menus estreitos (os nomes dos selos são longos), para a barra caber em ~2 linhas. */
.massa-barra select.form-select { max-width: 170px; }
.massa-barra input[name="massa_a_partir_de"] { max-width: 130px; }
/* Em ecrã estreito a barra ocuparia meio ecrã: aí fica no sítio. */
@media (max-width: 767.98px) { .massa-barra { position: static; } }

/* Logo do cliente no rodapé da sidebar */
.side-logo { background: #fff; border-radius: 10px; padding: 8px 10px; margin-bottom: 12px; display: flex; justify-content: center; }
.side-logo img { max-width: 180px; width: 100%; height: auto; object-fit: contain; }

/* Esboço de expedição — folha com aparência de página do folheto */
.folha-expedir { background: #fff; border: 1px solid var(--line); border-radius: 6px; box-shadow: 0 8px 30px rgba(15,23,42,.16); padding: 3.5%; margin: 0 auto; max-width: 820px; display: flex; flex-direction: column; }
.folha-cabecalho { text-align: center; font-size: .8rem; font-weight: 600; color: var(--muted); letter-spacing: .04em; text-transform: uppercase; padding-bottom: 10px; margin-bottom: 12px; border-bottom: 1px solid var(--line); }

/* Grelha da página: o número de colunas vem da configuração do Comercial */
.expedir-grid { display: grid; grid-template-columns: repeat(var(--grelha-colunas, 3), 1fr); gap: 10px; flex: 1; }
.expedir-cell { position: relative; border: 1px solid var(--line); border-radius: 8px; background: #fff; min-height: 150px; padding: 22px 8px 8px; display: flex; flex-direction: column; align-items: center; justify-content: center; text-align: center; }
.expedir-cell.empty { background: #f8fafc; border-style: dashed; }
.expedir-grid.editavel .expedir-cell { cursor: grab; }
.espaco-etiqueta { color: #cbd5e1; font-size: .72rem; font-weight: 600; text-transform: uppercase; letter-spacing: .04em; }
.expedir-cell.sortable-ghost { opacity: .4; }
.expedir-pos { position: absolute; top: 5px; left: 8px; font-size: .7rem; font-weight: 700; color: var(--muted); }

/* Mosaico das fotos dentro de uma posição (grupo de dinâmica) */
.expedir-fotos { display: grid; grid-template-columns: repeat(2, 1fr); gap: 4px; width: 100%; place-items: center; }
.expedir-fotos.n1 { grid-template-columns: 1fr; }
.expedir-fotos img { width: 100%; max-height: 110px; object-fit: contain; }
.expedir-fotos.n1 img { max-height: 130px; }
.expedir-noimg { width: 100%; height: 90px; display: grid; place-items: center; color: #cbd5e1; font-size: 1.7rem; }
.expedir-label { margin-top: 8px; font-size: .76rem; line-height: 1.22; color: var(--ink); font-weight: 600; }

/* Posições sem sítio na grelha da página (sem coordenadas para o EasyCatalog) */
.faixa-transbordo { border: 1px dashed #fca5a5; border-radius: 8px; background: #fef2f2; padding: 14px; }
.faixa-transbordo .expedir-cell { min-height: 110px; }
.expedir-cell.fora-da-grelha { border-color: #fca5a5; background: #fff; }

/* Ecrã estreito: o número de colunas já vem limitado do servidor. O `repeat()`
   exige um inteiro literal como primeiro argumento — `min()` ali é inválido e
   fazia o browser descartar a declaração inteira, perdendo este colapso. */
@media (max-width: 640px) { .expedir-grid { grid-template-columns: repeat(var(--grelha-colunas-mobile, 2), 1fr); } }

/* Fluxograma (stepper) do detalhe do trabalho */
.stepper { display: flex; gap: .25rem; overflow-x: auto; padding: .25rem 0; }
.step { flex: 1 0 auto; min-width: 96px; text-align: center; position: relative; padding: 0 .25rem; }
.step + .step::before {
  content: ""; position: absolute; top: 11px; left: -50%; width: 100%;
  height: 2px; background: #e2e8f0; z-index: 0;
}
.step-dot {
  position: relative; z-index: 1; display: inline-flex; align-items: center;
  justify-content: center; width: 24px; height: 24px; border-radius: 50%;
  background: #e2e8f0; color: #fff; font-size: .8rem; line-height: 1;
}
.step-label { display: block; font-size: .72rem; margin-top: .35rem; color: #64748b; }
.step-concluido .step-dot { background: #059669; }
.step-concluido .step-label { color: #059669; }
.step-atual .step-dot { background: #ea580c; box-shadow: 0 0 0 3px #fed7aa; }
.step-atual .step-label { color: #ea580c; font-weight: 600; }
.step-futuro .step-dot { background: #cbd5e1; }
.countdown { font-weight: 600; }
.countdown-expirado { color: #dc2626; }
