:root {
  --bg-primary: #D9D9D9;
  --bg-secondary: #FFFFFF;
  --bg-cell-hover: #EDF2F7;
  --text-primary: #2E2E2E;
  --text-secondary: #555555;
  --text-muted: #888888;
  --jmonte-color: #1D6AA0;
  --jmonte-bg: rgba(29, 106, 160, 0.08);
  --jmonte-border: rgba(29, 106, 160, 0.5);
  --border-color: #D0D5DD;
  --danger: #DC3545;
  --success: #28A745;
  --warning: #E67E22;
  --purple: #7C4DFF;
  --yellow-soft: #FFF8DB;
  --blue-soft: #E6F0FA;
  --purple-soft: #F0EBFF;
}

* { margin: 0; padding: 0; box-sizing: border-box; }

body {
  font-family: 'Plus Jakarta Sans', 'Inter', -apple-system, sans-serif;
  background: var(--bg-primary);
  color: var(--text-primary);
  min-height: 100vh;
}

/* Top bar */
#top-bar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 20px 32px;
  background: var(--bg-secondary);
  border-bottom: 1px solid var(--border-color);
}
#top-bar h1 { font-size: 22px; color: var(--jmonte-color); }
#top-bar .subtitle { font-size: 13px; color: var(--text-muted); margin-top: 2px; }
.actions { display: flex; gap: 10px; }

.btn {
  padding: 8px 16px;
  font-size: 13px;
  font-weight: 600;
  border: none;
  border-radius: 6px;
  cursor: pointer;
  font-family: inherit;
  transition: all 0.15s;
}
.btn-primary { background: var(--jmonte-color); color: white; }
.btn-primary:hover { background: #155785; }
.btn-primary.editing { background: var(--warning); }
.btn-success { background: var(--success); color: white; }
.btn-success:hover { background: #1f8237; }
.btn-ghost { background: transparent; color: var(--jmonte-color); border: 1px solid var(--jmonte-border); }
.btn-ghost:hover { background: var(--jmonte-bg); }

/* Summary */
#summary-bar {
  display: grid;
  grid-template-columns: repeat(5, 1fr);
  gap: 14px;
  padding: 18px 32px;
  background: var(--bg-primary);
}
.summary-card {
  background: var(--bg-secondary);
  border: 1px solid var(--border-color);
  border-radius: 10px;
  padding: 16px 18px;
  display: flex;
  flex-direction: column;
  gap: 4px;
}
.summary-label { font-size: 12px; color: var(--text-muted); text-transform: uppercase; letter-spacing: 0.5px; font-weight: 600; }
.summary-value { font-size: 28px; font-weight: 700; font-variant-numeric: tabular-nums; }
.summary-sub { font-size: 11px; color: var(--text-muted); }
.summary-card.total .summary-value { color: var(--text-primary); }
.summary-card.ok .summary-value { color: var(--success); }
.summary-card.up .summary-value { color: var(--warning); }
.summary-card.down .summary-value { color: var(--danger); }
.summary-card.cost .summary-value { color: var(--jmonte-color); font-size: 22px; }

/* Filter */
#filter-bar {
  margin: 0 32px 14px;
  background: var(--bg-secondary);
  border: 1px solid var(--border-color);
  border-radius: 10px;
  padding: 16px 18px;
}
.filter-header { display: flex; justify-content: space-between; align-items: center; margin-bottom: 12px; }
.filter-title { font-weight: 600; font-size: 14px; }
.filter-actions { display: flex; gap: 8px; }
.filter-chips { display: flex; flex-wrap: wrap; gap: 8px; }
.chip {
  padding: 6px 14px;
  border-radius: 999px;
  border: 1px solid var(--border-color);
  background: var(--bg-secondary);
  font-size: 12px;
  font-weight: 600;
  cursor: pointer;
  color: var(--text-secondary);
  transition: all 0.15s;
}
.chip.active { background: var(--jmonte-color); color: white; border-color: var(--jmonte-color); }
.chip:hover:not(.active) { background: var(--jmonte-bg); }

/* Tabs */
#tab-bar {
  display: flex;
  gap: 0;
  padding: 0 32px;
  background: var(--bg-primary);
  border-bottom: 1px solid var(--border-color);
}
.tab-btn {
  padding: 12px 24px;
  font-size: 13px;
  font-weight: 600;
  font-family: inherit;
  color: var(--text-muted);
  background: transparent;
  border: none;
  border-bottom: 3px solid transparent;
  cursor: pointer;
  transition: all 0.2s;
}
.tab-btn:hover { color: var(--text-secondary); }
.tab-btn.active { color: var(--jmonte-color); border-bottom-color: var(--jmonte-color); }

/* Table area */
#table-area { padding: 20px 32px 40px; }
#section-title { font-size: 16px; font-weight: 600; margin-bottom: 12px; color: var(--text-primary); }

.table-wrap {
  background: var(--bg-secondary);
  border-radius: 10px;
  border: 1px solid var(--border-color);
  overflow: auto;
  max-height: 75vh;
}
#main-table { width: 100%; border-collapse: collapse; font-size: 13px; }
#main-table thead th {
  position: sticky; top: 0; z-index: 3;
  background: #F5F7FA;
  color: var(--text-secondary);
  padding: 10px 8px;
  text-align: center;
  font-weight: 600;
  font-size: 11px;
  letter-spacing: 0.4px;
  border-bottom: 2px solid var(--border-color);
  white-space: nowrap;
}
#main-table thead th:first-child { text-align: left; padding-left: 14px; min-width: 140px; position: sticky; left: 0; z-index: 4; }
#main-table thead th.col-total { background: #EEF2F7; }
#main-table tbody td {
  padding: 8px;
  text-align: center;
  border-bottom: 1px solid var(--border-color);
  font-variant-numeric: tabular-nums;
  font-weight: 600;
}
#main-table tbody td.unit {
  text-align: left; padding-left: 14px;
  position: sticky; left: 0;
  background: var(--bg-secondary);
  z-index: 1;
  border-right: 1px solid var(--border-color);
}
#main-table tbody td.unit .unit-num { color: var(--text-muted); font-weight: 500; margin-right: 6px; }
#main-table tbody td.unit .row-label { display: block; font-size: 10px; color: var(--text-muted); text-transform: uppercase; letter-spacing: 0.5px; font-weight: 700; }

tr.row-real td { background: var(--blue-soft); color: var(--jmonte-color); }
tr.row-real td.unit { background: var(--blue-soft); }
tr.row-real td.clickable { cursor: pointer; text-decoration: underline dotted; }
tr.row-real td.clickable:hover { background: #D5E6F5; }

tr.row-ideal td { background: var(--yellow-soft); color: #8a6d00; }
tr.row-ideal td.unit { background: var(--yellow-soft); }
tr.row-ideal.editable td.editable-cell { cursor: pointer; text-decoration: underline; }
tr.row-ideal.editable td.editable-cell:hover { background: #FFF0B0; }
td input.ideal-input {
  width: 50px; padding: 2px 4px;
  font-size: 13px; font-weight: 600; text-align: center;
  border: 2px solid var(--warning); border-radius: 4px;
  font-family: inherit;
}

tr.row-desvio td { background: var(--purple-soft); }
tr.row-desvio td.unit { background: var(--purple-soft); }
.badge {
  display: inline-block;
  min-width: 28px;
  padding: 2px 8px;
  border-radius: 999px;
  font-size: 11px;
  font-weight: 700;
}
.badge.zero { background: #D0D5DD; color: var(--text-secondary); }
.badge.up { background: var(--warning); color: white; }
.badge.down { background: var(--danger); color: white; }

/* Borda grossa envolvendo o bloco de cada unidade (REAL+IDEAL+DESVIO) */
tr.row-real td { border-top: 2px solid #9AA5B1; }
tr.row-desvio td { border-bottom: 2px solid #9AA5B1; }
tr.row-real td:first-child,
tr.row-ideal td:first-child,
tr.row-desvio td:first-child { border-left: 2px solid #9AA5B1; }
tr.row-real td:last-child,
tr.row-ideal td:last-child,
tr.row-desvio td:last-child { border-right: 2px solid #9AA5B1; }
td.col-total { background: #F9FAFB; font-weight: 700; }

/* Modal */
.modal {
  position: fixed; inset: 0;
  background: rgba(0,0,0,0.5);
  display: flex; align-items: center; justify-content: center;
  z-index: 100;
}
.modal.hidden { display: none; }
.modal-content {
  background: var(--bg-secondary);
  border-radius: 12px;
  width: 90%; max-width: 560px;
  max-height: 80vh;
  display: flex; flex-direction: column;
  box-shadow: 0 20px 60px rgba(0,0,0,0.25);
}
.modal-header {
  padding: 16px 20px;
  border-bottom: 1px solid var(--border-color);
  display: flex; justify-content: space-between; align-items: center;
}
.modal-header h3 { font-size: 16px; color: var(--jmonte-color); }
.modal-close { background: none; border: none; font-size: 24px; cursor: pointer; color: var(--text-muted); }
.modal-body { padding: 16px 20px; overflow-y: auto; }
.func-item {
  padding: 10px 12px;
  border-bottom: 1px solid var(--border-color);
  display: flex; justify-content: space-between;
}
.func-item:last-child { border-bottom: none; }
.func-name { font-weight: 600; }
.func-cargo { color: var(--text-muted); font-size: 12px; }

.summary-card.ok:hover, .summary-card.up:hover, .summary-card.down:hover {
  background: var(--bg-cell-hover);
  box-shadow: 0 2px 8px rgba(0,0,0,0.08);
}

.positions-table { width: 100%; border-collapse: collapse; font-size: 13px; }
.positions-table th, .positions-table td {
  padding: 8px 10px;
  text-align: left;
  border-bottom: 1px solid var(--border-color);
}
.positions-table th {
  background: #F5F7FA;
  color: var(--text-secondary);
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.4px;
  font-weight: 600;
}
.positions-table td:nth-child(3),
.positions-table td:nth-child(4),
.positions-table td:nth-child(5),
.positions-table th:nth-child(3),
.positions-table th:nth-child(4),
.positions-table th:nth-child(5) { text-align: center; font-variant-numeric: tabular-nums; }

/* Toast */
.toast {
  position: fixed; bottom: 24px; right: 24px;
  background: var(--text-primary); color: white;
  padding: 12px 20px; border-radius: 8px;
  z-index: 200;
  box-shadow: 0 8px 24px rgba(0,0,0,0.2);
  font-size: 13px; font-weight: 600;
}
.toast.hidden { display: none; }
.toast.success { background: var(--success); }
.toast.error { background: var(--danger); }

.hidden { display: none; }

/* Busca */
.search-wrap { position: relative; flex: 1; max-width: 340px; margin: 0 20px; }
#search-input {
  width: 100%;
  padding: 8px 14px;
  font-size: 13px;
  border: 1px solid var(--border-color);
  border-radius: 6px;
  font-family: inherit;
}
#search-input:focus { outline: none; border-color: var(--jmonte-color); }
.search-results {
  position: absolute; top: 100%; left: 0; right: 0;
  background: var(--bg-secondary);
  border: 1px solid var(--border-color);
  border-radius: 6px;
  margin-top: 4px;
  max-height: 360px;
  overflow-y: auto;
  z-index: 50;
  box-shadow: 0 8px 24px rgba(0,0,0,0.1);
}
.search-result { padding: 10px 14px; border-bottom: 1px solid var(--border-color); cursor: pointer; }
.search-result:hover { background: var(--bg-cell-hover); }
.search-result .r-name { font-weight: 600; font-size: 13px; }
.search-result .r-meta { font-size: 11px; color: var(--text-muted); margin-top: 2px; }
.search-empty { padding: 12px; text-align: center; color: var(--text-muted); font-size: 12px; }

/* Historico */
#historico-area { padding: 20px 32px 40px; display: flex; flex-direction: column; gap: 16px; }
.historico-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 16px; }
.card { background: var(--bg-secondary); border: 1px solid var(--border-color); border-radius: 10px; padding: 18px; }
.card h3 { font-size: 14px; margin-bottom: 12px; color: var(--text-secondary); }
.diff-header { display: flex; justify-content: space-between; align-items: center; margin-bottom: 14px; flex-wrap: wrap; gap: 10px; }
.diff-controls { display: flex; gap: 12px; align-items: center; font-size: 12px; }
.diff-controls select { padding: 5px 8px; border: 1px solid var(--border-color); border-radius: 4px; font-family: inherit; }
.diff-body { display: grid; grid-template-columns: 1fr 1fr; gap: 16px; }
.diff-label { font-size: 13px; margin-bottom: 8px; }
.diff-label.up { color: var(--success); }
.diff-label.down { color: var(--danger); }
.diff-list { max-height: 320px; overflow-y: auto; border: 1px solid var(--border-color); border-radius: 6px; }
.diff-item { padding: 8px 12px; border-bottom: 1px solid var(--border-color); font-size: 12px; }
.diff-item:last-child { border-bottom: none; }
.diff-item .di-name { font-weight: 600; }
.diff-item .di-meta { color: var(--text-muted); font-size: 11px; }
.diff-empty { padding: 20px; text-align: center; color: var(--text-muted); font-size: 12px; }

/* Print (Exportar PDF) */
@media print {
  #top-bar .actions, .search-wrap, #filter-bar, #tab-bar, #historico-area, .modal { display: none !important; }
  body { background: white; }
  .table-wrap { max-height: none; overflow: visible; border: none; }
  #main-table { font-size: 10px; }
  #main-table thead th { position: static; }
  #main-table tbody td.unit { position: static; }
  .summary-card { page-break-inside: avoid; }
  #section-title { margin-top: 12px; }
}

/* Toggle de Grupo no topo */
.group-toggle {
  display: flex;
  gap: 0;
  background: var(--bg-primary);
  border-radius: 8px;
  padding: 4px;
}
.grp-btn {
  padding: 7px 16px;
  font-size: 12px;
  font-weight: 600;
  font-family: inherit;
  background: transparent;
  color: var(--text-secondary);
  border: none;
  border-radius: 6px;
  cursor: pointer;
  letter-spacing: 0.3px;
}
.grp-btn.active { background: var(--jmonte-color); color: white; }
.grp-btn:hover:not(.active) { background: rgba(0,0,0,0.04); color: var(--text-primary); }

/* Produtividade */
#produtividade-area { padding: 20px 32px 40px; }
#produtividade-table th, #produtividade-table td { font-size: 12px; }
#produtividade-table tr.top-produtivo td { background: rgba(40,167,69,0.08); }
#produtividade-table tr.low-produtivo td { background: rgba(220,53,69,0.08); }
.prod-badge {
  display: inline-block;
  padding: 2px 10px;
  border-radius: 999px;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
}
.prod-badge.good { background: var(--success); color: white; }
.prod-badge.mid { background: #D0D5DD; color: var(--text-secondary); }
.prod-badge.bad { background: var(--danger); color: white; }
