:root {
  --bg: #f4f6f9;
  --surface: #ffffff;
  --border: #e2e6ea;
  --text: #1f2933;
  --muted: #6b7280;
  --primary: #0d6efd;
  --danger: #dc3545;
  --success: #198754;
  --radius: 10px;
  --shadow: 0 1px 3px rgba(0, 0, 0, 0.08);
}

* { box-sizing: border-box; }

body {
  margin: 0;
  font-family: "Segoe UI", system-ui, -apple-system, sans-serif;
  background: var(--bg);
  color: var(--text);
}

.topbar {
  display: flex;
  align-items: center;
  gap: 24px;
  padding: 14px 28px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}

.brand { font-weight: 700; font-size: 18px; letter-spacing: 0.3px; }

.nav { display: flex; gap: 6px; flex: 1; }
.nav a {
  padding: 8px 14px;
  border-radius: 8px;
  color: var(--muted);
  text-decoration: none;
  font-weight: 600;
}
.nav a:hover { background: var(--bg); color: var(--text); }
.nav a.active { background: var(--primary); color: #fff; }

.userbox { display: flex; align-items: center; gap: 12px; }

.container { max-width: 1080px; margin: 28px auto; padding: 0 24px; }

.cards { display: grid; grid-template-columns: repeat(auto-fit, minmax(220px, 1fr)); gap: 16px; margin-top: 20px; }

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 18px;
}
.card-title { color: var(--muted); font-size: 14px; font-weight: 600; }
.card-value { font-size: 30px; font-weight: 700; margin-top: 6px; }

.auth-wrap {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
}
.auth-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 36px;
  width: 100%;
  max-width: 360px;
}
.auth-card h1 { font-size: 22px; margin: 0 0 4px; }
.subtitle { color: var(--muted); margin: 0 0 24px; }
.auth-link { text-align: center; margin-top: 16px; font-size: 14px; color: var(--muted); }
.auth-link a { color: var(--primary); text-decoration: none; font-weight: 600; }
.auth-link a:hover { text-decoration: underline; }
.form-help { display: block; font-size: 12px; color: var(--muted); margin-top: 4px; }

label { display: block; font-size: 14px; font-weight: 600; margin: 12px 0 6px; }
input[type="text"], input[type="password"], input[type="email"], input[type="number"], input[type="date"], select {
  width: 100%;
  padding: 10px 12px;
  border: 1px solid var(--border);
  border-radius: 8px;
  font-size: 15px;
}
input:focus, select:focus { outline: 2px solid var(--primary); outline-offset: -1px; }

.btn {
  display: inline-block;
  border: none;
  border-radius: 8px;
  padding: 10px 16px;
  font-size: 15px;
  font-weight: 600;
  cursor: pointer;
  text-decoration: none;
}
.btn-primary { background: var(--primary); color: #fff; }
.btn-primary:hover { filter: brightness(1.08); }
.btn-block { width: 100%; margin-top: 20px; }
.btn-ghost { background: transparent; color: var(--muted); border: 1px solid var(--border); }
.btn-ghost:hover { background: var(--bg); }
.btn-danger { background: var(--danger); color: #fff; }
.btn-success { background: var(--success); color: #fff; }
.btn-warning { background: #e67e22; color: #fff; }

.alert { padding: 12px 16px; border-radius: 8px; font-size: 14px; margin-bottom: 16px; }
.alert-danger { background: #fdecea; color: #b02a37; border: 1px solid #f5c6cb; }

.muted { color: var(--muted); }

.page-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 18px;
}
.page-head h2 { margin: 0; }

.container-narrow { max-width: 640px; }
.form-card { padding: 24px; }
.form-card .btn-block { margin-top: 24px; }

.row-2 { display: grid; grid-template-columns: 1fr 1fr; gap: 16px; }

.checkbox { display: flex; align-items: center; gap: 8px; font-weight: 500; margin-top: 16px; }
.checkbox input { width: auto; }

.table {
  width: 100%;
  border-collapse: collapse;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  overflow: hidden;
  box-shadow: var(--shadow);
}
.table-wrap { overflow-x: auto; padding-bottom: 12px; scrollbar-width: none; -ms-overflow-style: none; }
.table-wrap::-webkit-scrollbar { display: none; }

/* Tabela compacta (telas de listagem com muitas colunas) */
.table-compact th, .table-compact td { padding: 6px 8px; font-size: 12px; }
.table-compact .td-actions { gap: 4px; }
.table-compact .btn-sm { padding: 4px 8px; font-size: 12px; }
.table-compact .badge { padding: 2px 8px; font-size: 11px; }
.table-compact .move-btns { gap: 2px; }
.table-compact .posicao-input { padding: 3px 4px; font-size: 12px; }
.table th, .table td {
  padding: 12px 14px;
  text-align: left;
  border-bottom: 1px solid var(--border);
  font-size: 14px;
}
.table thead th {
  background: #f8f9fa;
  color: var(--muted);
  font-size: 12px;
  text-transform: uppercase;
  letter-spacing: 0.4px;
}
.table tbody tr:last-child td { border-bottom: none; }
.table tbody tr:hover { background: #fafbfc; }

.th-actions { text-align: right; }
.td-actions { display: flex; gap: 8px; justify-content: flex-end; }
.td-actions form { margin: 0; }

.btn-sm { padding: 6px 10px; font-size: 13px; }

.move-btns { display: flex; gap: 4px; }
.move-btns form { margin: 0; }
.move-btns button:disabled { opacity: 0.35; cursor: not-allowed; }

.posicao-form { margin: 0; }
.posicao-input {
  width: 52px;
  min-width: 52px;
  padding: 4px 6px;
  font-size: 13px;
  text-align: center;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: #fff;
  color: var(--text);
}
.posicao-input:focus {
  outline: none;
  border-color: var(--primary);
  box-shadow: 0 0 0 2px rgba(13, 110, 253, 0.15);
}
.posicao-input.inativa { color: #adb5bd; }

.badge {
  display: inline-block;
  padding: 3px 10px;
  border-radius: 999px;
  font-size: 12px;
  font-weight: 600;
}
.badge-success { background: #d1e7dd; color: #0f5132; }
.badge-warning { background: #fff3cd; color: #997404; }
.badge-muted { background: #e9ecef; color: #495057; }
.badge-alerta { background: #f8d7da; color: #842029; }
.badge-fixa { background: #e0f0ff; color: #0a58ca; }
.badge-variavel { background: #fff3cd; color: #997404; }

.consumo-table td, .consumo-table th { padding: 8px 12px; }
.consumo-table .num { text-align: right; white-space: nowrap; }
.consumo-detalhe td { background: #f8f9fa; padding: 4px 12px 10px; }
.consumo-lista {
  list-style: none;
  margin: 6px 0 0;
  padding: 0;
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(280px, 1fr));
  gap: 2px 24px;
}
.consumo-lista li {
  display: flex;
  justify-content: space-between;
  gap: 12px;
  padding: 3px 0;
  border-bottom: 1px dashed #e9ecef;
  font-size: 13px;
}
.consumo-lista .num { white-space: nowrap; font-variant-numeric: tabular-nums; }

.pessoa {
  border: 1px solid var(--border);
  border-radius: var(--radius);
  margin: 14px 0;
  padding: 12px 14px;
  background: var(--surface);
}
.pessoa-cab {
  display: flex;
  flex-wrap: wrap;
  align-items: baseline;
  justify-content: space-between;
  gap: 6px 16px;
}
.pessoa-nome { font-weight: 700; font-size: 16px; }
.pessoa-nome .muted { font-weight: 400; font-size: 12px; margin-left: 6px; }
.pessoa-valores { display: flex; flex-wrap: wrap; gap: 6px 18px; font-size: 13px; }
.pessoa-valores span { white-space: nowrap; }
.pessoa-valores strong { font-variant-numeric: tabular-nums; }
.consumo-bar-lg { height: 16px; margin: 8px 0 2px; }
.consumo-sub { margin-top: 8px; }
.consumo-sub td { padding: 4px 8px; font-size: 13px; }
.consumo-sub td:last-child { text-align: right; white-space: nowrap; }
.consumo-detalhe-block { margin-top: 8px; padding: 8px 10px; background: #f8f9fa; border-radius: 6px; }
.pessoa-nao-vinculada { background: #fffaf0; border-color: #f0d9b0; }
.pessoa-total {
  display: flex;
  flex-wrap: wrap;
  justify-content: space-between;
  gap: 6px 18px;
  margin-top: 14px;
  padding: 10px 14px;
  border-top: 2px solid var(--border);
  font-weight: 700;
}
.pessoa-total span { white-space: nowrap; }
.saldo-pos { color: var(--success); font-weight: 700; }
.saldo-neg { color: var(--danger); font-weight: 700; }
.consumo-bar { background: #e9ecef; border-radius: 6px; height: 12px; min-width: 90px; overflow: hidden; }
.consumo-bar-fill { background: var(--success); height: 100%; border-radius: 6px; }
.consumo-bar-fill.alta { background: var(--danger); }

.alert-success {
  background: #d1e7dd;
  color: #0f5132;
  border: 1px solid #badbcc;
}

.month-nav { display: flex; align-items: center; gap: 16px; }
.month-nav h2 { margin: 0; }

.card-green { color: var(--success); }
.card-red { color: var(--danger); }
.card-orange { color: #e67e22; }
.card-sub { font-size: 12px; color: var(--muted); margin-top: 4px; }

.grid-2 { display: grid; grid-template-columns: 1fr 1fr; gap: 20px; margin-top: 24px; }

.panel {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 18px;
}
.panel h3 { margin: 0 0 14px; font-size: 16px; }
.panel .table { box-shadow: none; }

.row-pago { background: #f1fbf4; }
.row-pendente { background: #fef7f7; }
.row-adiada { background: #fffde7; }

.row-inativa td { background: #f0f0f0; color: #8a8f98; }
.row-inativa td:first-child { font-style: italic; }

.badge-danger { background: #fdecea; color: #b02a37; }

.container-wide { max-width: 1400px; }

.matriz-wrap { overflow-x: auto; padding-bottom: 12px; scrollbar-width: none; -ms-overflow-style: none; }
.matriz-wrap::-webkit-scrollbar { display: none; }
.matriz-wrap h3 { margin: 26px 0 12px; }

.table-matriz { width: max-content; min-width: 100%; overflow: visible; }
.table-matriz th, .table-matriz td { white-space: nowrap; text-align: right; }
.table-matriz td:first-child, .table-matriz th:first-child { text-align: left; min-width: 220px; }

.sticky-col { position: sticky; left: 0; background: #fff; z-index: 1; }
.table-matriz thead .sticky-col { background: #f8f9fa; }
.table-matriz tbody tr:hover .sticky-col { background: #fafbfc; }

.col-atual { background: #eef4ff; }
.cell-zero { color: #c0c5cc; }
.cell-pago { background: #e6f7ec; font-weight: 600; }
.cell-pendente { background: #fdeceb; }
.cell-receita { background: #eef6ff; }
.cell-despesa { background: #fff3e6; }
.cell-sobra-pos { background: #e6f7ec; font-weight: 700; }
.cell-sobra-neg { background: #fdeceb; font-weight: 700; }
.strong { font-weight: 700; }
.small { font-size: 12px; }

.cell-legend {
  display: inline-block;
  padding: 2px 10px;
  border-radius: 6px;
  font-size: 12px;
  margin-right: 10px;
}
.col-atual-legend { background: #eef4ff; }

/* ===== Edição de valor por mês na Matriz (clicar na célula) ===== */
.cell-edit {
  display: block;
  width: 100%;
  border: 0;
  background: transparent;
  font: inherit;
  color: inherit;
  text-align: right;
  padding: 0.25rem 0.4rem;
  cursor: pointer;
  border-radius: 4px;
}
.cell-edit:hover {
  background: rgba(255, 255, 255, 0.7);
  outline: 1px dashed #3388dd;
  outline-offset: -1px;
}
.cell-sobrescrito::after {
  content: "\25CF";
  display: inline-block;
  margin-left: 5px;
  font-size: 10px;
  vertical-align: middle;
  color: #e07b00;
}
.cell-sobrescrito {
  font-weight: 650;
  color: #8a4d00;
}
.cell-sobrescrito-legend { background: #fff0d6; color: #a55a00; }
.cell-input {
  width: 100%;
  box-sizing: border-box;
  text-align: right;
  font: inherit;
  background: #fff;
  border: 1px solid #3388dd;
  border-radius: 4px;
  padding: 0.2rem 0.375rem;
}

.chart-box { position: relative; height: 260px; }

/* ===== Barra de rolagem horizontal sempre visível (Matriz e tabelas) ===== */
.matriz-scrollbar,
.table-scrollbar {
  position: sticky;
  bottom: 0;
  height: 20px;
  overflow-x: auto;
  overflow-y: hidden;
  background: var(--surface);
  border: 1px solid var(--border);
  border-top: none;
  border-radius: 0 0 var(--radius) var(--radius);
  box-shadow: var(--shadow);
  z-index: 5;
}
.matriz-scrollbar-track,
.table-scrollbar-track { height: 1px; }

/* ===== Responsivo ===== */
@media (max-width: 768px) {
  .container { padding: 0 12px; margin: 16px auto; }
  .topbar { flex-wrap: wrap; padding: 10px 14px; gap: 10px; }
  .nav { flex-wrap: wrap; }
  .page-head { flex-wrap: wrap; gap: 10px; }
  .row-2 { grid-template-columns: 1fr; }
  .grid-2 { grid-template-columns: 1fr; }
  .month-nav { flex-wrap: wrap; }
  .td-actions { flex-wrap: wrap; }
  .cards { grid-template-columns: 1fr; }
  .form-card { padding: 16px; }
  .auth-card { padding: 24px; }
}
