:root {
  --bg: #f5f6f4;
  --surface: #ffffff;
  --surface-2: #eef1ee;
  --text: #1c2420;
  --muted: #5d6a63;
  --border: #d7ddd9;
  --primary: #1f6f5c;
  --primary-text: #ffffff;
  --positive: #1d7a45;
  --negative: #b3261e;
  --warning-bg: #fff4d6;
  --warning-text: #6b4e00;
  --focus: #2d8cf0;
  --radius: 10px;
  --shadow: 0 1px 2px rgb(0 0 0 / 6%), 0 2px 8px rgb(0 0 0 / 4%);
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #121614;
    --surface: #1b211e;
    --surface-2: #232b27;
    --text: #e6ece8;
    --muted: #9aa8a0;
    --border: #334039;
    --primary: #4fb597;
    --primary-text: #0b1a15;
    --positive: #5cc98b;
    --negative: #f2857d;
    --warning-bg: #3a3112;
    --warning-text: #f1d98b;
    --shadow: none;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }

html { -webkit-text-size-adjust: 100%; }

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

h1, h2, h3 { line-height: 1.2; margin: 0 0 .5rem; }
h1 { font-size: 1.35rem; }
h2 { font-size: 1.1rem; }
h3 { font-size: .95rem; color: var(--muted); text-transform: uppercase; letter-spacing: .04em; }
p { margin: 0 0 .75rem; }
.muted { color: var(--muted); }
.small { font-size: .85rem; }

/* Topo e navegação */
.topbar {
  position: sticky; top: 0; z-index: 10;
  display: flex; align-items: center; justify-content: space-between; gap: 1rem;
  padding: .6rem max(16px, env(safe-area-inset-left));
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}
.brand { display: flex; align-items: center; gap: .5rem; font-size: 1rem; white-space: nowrap; }
.tabs { display: flex; gap: .25rem; }
.tabs a {
  display: flex; align-items: center; gap: .35rem;
  padding: .45rem .8rem; border-radius: 999px;
  color: var(--muted); text-decoration: none; font-weight: 500;
}
.tabs a:hover { background: var(--surface-2); color: var(--text); }
.tabs a[aria-current="page"] { background: var(--primary); color: var(--primary-text); }
.badge {
  min-width: 1.3rem; padding: 0 .35rem; border-radius: 999px;
  background: var(--negative); color: #fff; font-size: .72rem; line-height: 1.3rem; text-align: center;
}

main {
  max-width: 1180px; margin: 0 auto;
  padding: 1.25rem 16px 2rem;
  outline: none;
}

/* Cartões e formulários */
.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 1rem;
  margin-bottom: 1rem;
}
.grid { display: grid; gap: .85rem; }
.grid-2 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
.grid-3 { grid-template-columns: repeat(3, minmax(0, 1fr)); }
.span-2 { grid-column: span 2; }
.layout-2 { display: grid; grid-template-columns: minmax(0, 3fr) minmax(0, 2fr); gap: 1rem; align-items: start; }

label { display: grid; gap: .3rem; font-weight: 500; font-size: .9rem; }
input, select, button, textarea { font: inherit; color: inherit; }
input, select {
  width: 100%; min-height: 2.5rem;
  padding: .45rem .6rem;
  border: 1px solid var(--border); border-radius: 8px;
  background: var(--surface);
}
input:focus-visible, select:focus-visible, button:focus-visible, a:focus-visible, summary:focus-visible {
  outline: 2px solid var(--focus); outline-offset: 2px;
}
input[aria-invalid="true"] { border-color: var(--negative); }

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: .4rem;
  min-height: 2.5rem; padding: .45rem 1rem;
  border: 1px solid var(--border); border-radius: 8px;
  background: var(--surface-2); cursor: pointer; font-weight: 500; text-decoration: none;
}
.btn:hover { filter: brightness(.97); }
.btn-primary { background: var(--primary); border-color: var(--primary); color: var(--primary-text); }
.btn-danger { color: var(--negative); }
.btn-small { min-height: 2rem; padding: .2rem .6rem; font-size: .85rem; }
.actions { display: flex; flex-wrap: wrap; gap: .5rem; align-items: center; }

.segmented { display: flex; border: 1px solid var(--border); border-radius: 8px; overflow: hidden; }
.segmented label { flex: 1; display: block; margin: 0; }
.segmented input { position: absolute; opacity: 0; width: 1px; height: 1px; }
.segmented span { display: block; padding: .5rem; text-align: center; cursor: pointer; font-weight: 500; }
.segmented input:checked + span { background: var(--primary); color: var(--primary-text); }
.segmented input:focus-visible + span { outline: 2px solid var(--focus); outline-offset: -2px; }

details > summary { cursor: pointer; font-weight: 500; padding: .25rem 0; }
details[open] > summary { margin-bottom: .75rem; }

.hint {
  display: flex; flex-wrap: wrap; gap: .5rem; align-items: center;
  padding: .5rem .7rem; border-radius: 8px;
  background: var(--surface-2); font-size: .88rem;
}
.notice { padding: .7rem .9rem; border-radius: 8px; background: var(--warning-bg); color: var(--warning-text); margin-bottom: 1rem; }

/* Modo online: faixa sob o topo e linha sendo gravada no Supabase. */
.mode-banner { display: flex; flex-wrap: wrap; gap: .5rem 1rem; align-items: center; justify-content: space-between; padding: .5rem 1rem; background: var(--warning-bg); color: var(--warning-text); border-bottom: 1px solid var(--border); font-size: .9rem; }
.mode-banner[hidden] { display: none; }
.mode-banner .actions { display: flex; gap: .5rem; }
.is-saving { opacity: .6; pointer-events: none; }

.errors { color: var(--negative); margin: 0; padding-left: 1.2rem; }

/* Valores */
.amount { font-variant-numeric: tabular-nums; white-space: nowrap; text-align: right; }
.pos { color: var(--positive); }
.neg { color: var(--negative); }

/* Resumo */
.kpis { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 1rem; margin-bottom: 1rem; }
.kpi { margin: 0; }
.period { display: grid; gap: .75rem; max-width: 36rem; }
.period > .btn { justify-self: start; }
.period [hidden] { display: none; }
.period-fields { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: .75rem; }
.period-label { font-size: 1.1rem; font-weight: 650; margin-bottom: .5rem; }
.period-label::first-letter { text-transform: uppercase; }
.kpi .label { color: var(--muted); font-size: .85rem; font-weight: 500; }
.kpi .value { font-size: 1.5rem; font-weight: 650; font-variant-numeric: tabular-nums; }

/* Tabelas */
.table-wrap { overflow-x: auto; }
table { width: 100%; border-collapse: collapse; }
th, td { padding: .5rem; border-bottom: 1px solid var(--border); text-align: left; vertical-align: middle; }
th { font-size: .8rem; color: var(--muted); font-weight: 600; white-space: nowrap; }
tfoot td { font-weight: 650; border-bottom: none; }
.review-table input, .review-table select { min-height: 2.2rem; padding: .3rem .45rem; font-size: .9rem; }
.review-table .col-date { min-width: 9rem; }
.review-table .col-desc { min-width: 12rem; }
.review-table .col-cat { min-width: 12rem; }
.review-table .col-pay { min-width: 10rem; }
.review-table .col-value { min-width: 7rem; }
.review-table .col-parc { min-width: 14rem; }
.parc-fields { display: flex; gap: .3rem; }
.parc-fields input[type="text"] { flex: 0 0 4rem; }
.parc-fields input[type="month"] { flex: 1 1 auto; min-width: 0; }
.review-table tr.is-pending { background: color-mix(in srgb, var(--warning-bg) 55%, transparent); }
.status { display: inline-block; padding: .1rem .5rem; border-radius: 999px; font-size: .75rem; font-weight: 600; white-space: nowrap; }
.status-pendente { background: var(--warning-bg); color: var(--warning-text); }
.status-revisada { background: var(--surface-2); color: var(--positive); }
.stack { display: grid; gap: .3rem; }
.preview-table td { font-size: .9rem; }
.preview-table tr.is-duplicate td { color: var(--muted); text-decoration: line-through; }
.preview-table tr.is-duplicate td:last-child { text-decoration: none; }
.filters { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); gap: .75rem; margin-bottom: 1rem; }

.list { list-style: none; margin: 0; padding: 0; }
.list li { display: flex; justify-content: space-between; gap: 1rem; padding: .5rem 0; border-bottom: 1px solid var(--border); }
.list li:last-child { border-bottom: none; }

.reader-list { display: grid; gap: .6rem; margin-bottom: 1rem; }
.reader { display: flex; gap: .7rem; align-items: flex-start; padding: .7rem; border: 1px solid var(--border); border-radius: 8px; cursor: pointer; font-weight: 400; }
.reader input { width: auto; min-height: 0; margin-top: .25rem; }
.reader strong { display: block; }

.cat-row { display: grid; grid-template-columns: 6rem minmax(0, 1fr) 11rem auto; gap: .5rem; align-items: center; padding: .4rem 0; border-bottom: 1px solid var(--border); }

#toast {
  position: fixed; left: 50%; bottom: 1.5rem; transform: translateX(-50%); z-index: 20;
  max-width: calc(100% - 32px);
  padding: .65rem 1rem; border-radius: 8px;
  background: var(--text); color: var(--bg); box-shadow: var(--shadow);
}
#toast.error { background: var(--negative); color: #fff; }

/* Celular */
@media (max-width: 760px) {
  body { padding-bottom: calc(4.2rem + env(safe-area-inset-bottom)); }
  .brand span { font-size: .95rem; }
  .tabs {
    position: fixed; left: 0; right: 0; bottom: 0; z-index: 10;
    justify-content: space-around; gap: 0;
    padding: .35rem .25rem calc(.35rem + env(safe-area-inset-bottom));
    background: var(--surface); border-top: 1px solid var(--border);
  }
  .tabs a { flex-direction: column; gap: .1rem; padding: .4rem .5rem; font-size: .8rem; border-radius: 8px; }
  .grid-2, .grid-3, .layout-2, .kpis, .filters { grid-template-columns: 1fr; }
  .span-2 { grid-column: auto; }
  .kpi .value { font-size: 1.3rem; }

  /* A tabela de revisão vira uma lista de cartões. */
  .review-table thead { display: none; }
  .review-table, .review-table tbody, .review-table tr, .review-table td { display: block; width: 100%; }
  .review-table tr { border: 1px solid var(--border); border-radius: var(--radius); margin-bottom: .75rem; padding: .5rem .75rem; background: var(--surface); }
  .review-table td { border: none; padding: .3rem 0; }
  .review-table td[data-label]::before { content: attr(data-label); display: block; font-size: .78rem; color: var(--muted); font-weight: 600; margin-bottom: .15rem; }
  .review-table td[class^="col-"] { min-width: 0; }

  .preview-table thead { display: none; }
  .preview-table, .preview-table tbody, .preview-table tr, .preview-table td { display: block; width: 100%; }
  .preview-table tr { border-bottom: 1px solid var(--border); padding: .5rem 0; }
  .preview-table td { border: none; padding: .1rem 0; }
  .preview-table td[data-label]::before { content: attr(data-label) ": "; color: var(--muted); font-size: .8rem; }

  .cat-row { grid-template-columns: 5rem minmax(0, 1fr); }
  .cat-row > :nth-child(n+3) { grid-column: span 2; }
}
