/* =========================================================
   FINIO - THEME SYSTEM (FIXED)
   - Tema controlado por: html[data-theme="dark"|"light"]
   - Evita FOUC: não coloca "dark default" em :root
   - Fallback: prefers-color-scheme quando não existir data-theme
   ========================================================= */

/* ---------------------------
   TOKENS COMUNS (NÃO TEMÁTICOS)
   --------------------------- */
:root {
  --accent: #f7931e;
  --focus: #f7931e33;

  /* Chips (fallbacks neutros; serão sobrescritos pelo tema) */
  /* Reforçados para melhor leitura mesmo no fallback */
  --chip-ok-bg: #163f2a;
  --chip-ok-ink: #63f19a;
  --chip-ok-border: #2fbf6d;

  --chip-warn-bg: #3a2a10;
  --chip-warn-ink: #ffd166;
  --chip-warn-border: #d49b17;

  --chip-bad-bg: #3d1111;
  --chip-bad-ink: #ff8a8a;
  --chip-bad-border: #d33a3a;

  /* Dashboard (fallbacks neutros; serão sobrescritos pelo tema) */
  --dash-receita-bg: #132b18;
  --dash-receita-ink: #49f77c;

  --dash-despesa-bg: #2b1313;
  --dash-despesa-ink: #f76767;

  --dash-saldo-bg: #1d1d2b;
  --dash-saldo-ink: #73a8ff;

  --dash-transf-bg: #1b262f;
  --dash-transf-ink: #f9d15b;

  --panel-bg: #111111;
  --panel-inner-bg: #1a1a1a;

  /* Chart (fallbacks; serão sobrescritos pelo tema) */
  --chart-receita: #49f77c;
  --chart-despesa: #f76767;
  --chart-saldo: #73a8ff;
  --chart-ticks: #aaaaaa;
  --chart-grid: #222222;
  --chart-legend: #cccccc;
}

/* ---------------------------
   TEMA DARK (EXPLÍCITO)
   --------------------------- */
html[data-theme="dark"] {
  --bg: #0b0b0b;
  --card: #121212;
  --ink: #eaeaea;
  --muted: #9a9a9a;

  --surface-1: #111111;
  --surface-2: #1a1a1a;
  --surface-3: #0e0e0e;

  --border: #222222;
  --border-strong: #333333;

  --panel-bg: var(--surface-1);
  --panel-inner-bg: var(--surface-2);

  --dash-receita-bg: #132b18;
  --dash-receita-ink: #49f77c;

  --dash-despesa-bg: #2b1313;
  --dash-despesa-ink: #f76767;

  --dash-saldo-bg: #1d1d2b;
  --dash-saldo-ink: #73a8ff;

  --dash-transf-bg: #1b262f;
  --dash-transf-ink: #f9d15b;

  --chart-receita: #49f77c;
  --chart-despesa: #f76767;
  --chart-saldo: #73a8ff;
  --chart-ticks: #aaaaaa;
  --chart-grid: #222222;
  --chart-legend: #cccccc;

  /* ===== Chips (status) - reforçados no DARK ===== */
  --chip-ok-bg: #143d27;       /* mais visível que o antigo */
  --chip-ok-ink: #63f19a;      /* verde mais vivo */
  --chip-ok-border: #2fbf6d;   /* borda mais nítida */

  --chip-warn-bg: #3a2a10;     /* âmbar mais presente */
  --chip-warn-ink: #ffd166;    /* texto mais forte */
  --chip-warn-border: #d49b17; /* borda âmbar */

  --chip-bad-bg: #3d1111;
  --chip-bad-ink: #ff8a8a;
  --chip-bad-border: #d33a3a;

  /* informa ao browser o esquema */
  color-scheme: dark;
}

/* ---------------------------
   TEMA LIGHT (EXPLÍCITO)
   --------------------------- */
html[data-theme="light"] {
  --bg: #f5f6f8;
  --card: #ffffff;
  --ink: #1f2328;
  --muted: #5b6673;

  --surface-1: #ffffff;
  --surface-2: #f2f4f7;
  --surface-3: #ffffff;

  --border: #d7dde5;
  --border-strong: #c9d2dd;

  --panel-bg: #ffffff;
  --panel-inner-bg: #f2f4f7;

  --dash-receita-bg: #e9f7ee;
  --dash-receita-ink: #0f7a3d;

  --dash-despesa-bg: #fdecec;
  --dash-despesa-ink: #b42318;

  --dash-saldo-bg: #eef3ff;
  --dash-saldo-ink: #1d4ed8;

  --dash-transf-bg: #eef6ff;
  --dash-transf-ink: #8a6a00;

  --chart-receita: #0f7a3d;
  --chart-despesa: #b42318;
  --chart-saldo: #1d4ed8;
  --chart-ticks: #4b5563;
  --chart-grid: #e5e7eb;
  --chart-legend: #374151;

  /* ===== Chips (status) - reforçados no LIGHT ===== */
  --chip-ok-bg: #dff4e7;       /* um pouco mais “presente” */
  --chip-ok-ink: #0a6a32;      /* texto mais escuro e legível */
  --chip-ok-border: #56c27f;   /* borda mais forte */

  --chip-warn-bg: #fff0c2;     /* amarelo mais visível */
  --chip-warn-ink: #8a5a00;    /* âmbar escuro para leitura */
  --chip-warn-border: #f1b400; /* borda bem definida */

  --chip-bad-bg: #fdecec;
  --chip-bad-ink: #b42318;
  --chip-bad-border: #f04438;

  color-scheme: light;
}

/* ---------------------------------------------------------
   FALLBACK AUTOMÁTICO:
   - Se não existir data-theme no HTML, usa prefers-color-scheme
   - Isso evita "padrão dark hardcoded" e reduz piscadas
   --------------------------------------------------------- */
html:not([data-theme]) {
  /* fallback base (claro) */
  --bg: #f5f6f8;
  --card: #ffffff;
  --ink: #1f2328;
  --muted: #5b6673;

  --surface-1: #ffffff;
  --surface-2: #f2f4f7;
  --surface-3: #ffffff;

  --border: #d7dde5;
  --border-strong: #c9d2dd;

  --panel-bg: #ffffff;
  --panel-inner-bg: #f2f4f7;

  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  html:not([data-theme]) {
    --bg: #0b0b0b;
    --card: #121212;
    --ink: #eaeaea;
    --muted: #9a9a9a;

    --surface-1: #111111;
    --surface-2: #1a1a1a;
    --surface-3: #0e0e0e;

    --border: #222222;
    --border-strong: #333333;

    --panel-bg: var(--surface-1);
    --panel-inner-bg: var(--surface-2);

    /* chips reforçados no fallback dark */
    --chip-ok-bg: #143d27;
    --chip-ok-ink: #63f19a;
    --chip-ok-border: #2fbf6d;

    --chip-warn-bg: #3a2a10;
    --chip-warn-ink: #ffd166;
    --chip-warn-border: #d49b17;

    --chip-bad-bg: #3d1111;
    --chip-bad-ink: #ff8a8a;
    --chip-bad-border: #d33a3a;

    color-scheme: dark;
  }
}

/* =========================
   BASE
   ========================= */
* { box-sizing: border-box; }

html, body { height: 100%; }

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

/* topbar */
.topbar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  background: var(--surface-1);
  padding: 12px 20px;
  border-bottom: 1px solid var(--border);
}

.logo {
  font-weight: 700;
  color: var(--accent);
  font-size: 1.3em;
}

.nav a {
  color: var(--ink);
  text-decoration: none;
  margin-left: 12px;
  font-weight: 500;
}
.nav a:hover { color: var(--accent); }

.container {
  max-width: 1100px;
  margin: 20px auto;
  padding: 0 20px;
}

.card {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 16px;
  margin-bottom: 16px;
}

h1, h2, h3 { color: var(--accent); }

.footer {
  text-align: center;
  padding: 20px;
  border-top: 1px solid var(--border);
  color: var(--muted);
}

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

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

/* --- TABELAS --- */
table {
  width: 100%;
  border-collapse: collapse;
  margin-top: 12px;
}

th, td {
  padding: 8px 10px;
  border-bottom: 1px solid var(--border-strong);
  vertical-align: middle;
  color: var(--ink);
}

th {
  font-weight: 600;
  color: var(--muted);
  text-transform: uppercase;
  font-size: 13px;
}

/* colgroup larguras da tabela de lançamentos */
table.finio col.col-data       { width: 8%; }
table.finio col.col-tipo       { width: 10%; }
table.finio col.col-categoria  { width: 12%; }
table.finio col.col-descricao  { width: 32%; }
table.finio col.col-forma      { width: 10%; }
table.finio col.col-valor      { width: 10%; }
table.finio col.col-status     { width: 10%; }
table.finio col.col-acoes      { width: 8%; }

/* alinhamentos utilitários */
.t-left   { text-align: left; }
.t-center { text-align: center; }
.t-right  { text-align: right; }

/* Coluna de valores (LINHAS) */
td.valor {
  text-align: right;
  font-variant-numeric: tabular-nums;
  font-family:
    ui-monospace, SFMono-Regular, Menlo, Monaco,
    Consolas, "Liberation Mono", "Courier New", monospace;
  padding-right: 18px;
}

/* Coluna de status (linhas) */
td.status { text-align: center; }

/* Chips de status */
.chip {
  display: inline-block;
  padding: 3px 8px;
  border-radius: 8px;
  font-size: 12px;
  border: 1px solid transparent;

  /* reforço de legibilidade */
  font-weight: 600;
  letter-spacing: 0.2px;
}
.chip.ok   { background: var(--chip-ok-bg); color: var(--chip-ok-ink); border-color: var(--chip-ok-border); }
.chip.warn { background: var(--chip-warn-bg); color: var(--chip-warn-ink); border-color: var(--chip-warn-border); }
.chip.bad  { background: var(--chip-bad-bg); color: var(--chip-bad-ink); border-color: var(--chip-bad-border); }

/* Ação */
.table-action { color: var(--accent); text-decoration: none; font-weight: 600; }
.table-action:hover { text-decoration: underline; }

/* Botão padrão */
.btn {
  display: inline-block;
  background: var(--accent);
  color: #000;
  padding: 8px 12px;
  border-radius: 8px;
  text-decoration: none;
  font-weight: 600;
  border: 0;
  cursor: pointer;
}
.btn:hover { filter: brightness(1.08); }

/* Botão “secundário” (usado para toggle) */
.btn-ghost {
  background: transparent;
  border: 1px solid var(--border);
  color: var(--ink);
}
.btn-ghost:hover {
  border-color: var(--accent);
  color: var(--accent);
}

/* --- Dropdown (submenu) --- */
.dropdown { position: relative; display: inline-block; }

.dropbtn {
  color: var(--ink);
  text-decoration: none;
  margin-left: 12px;
  font-weight: 500;
}

.dropdown-content {
  display: none;
  position: absolute;
  background-color: var(--surface-2);
  min-width: 160px;
  border: 1px solid var(--border-strong);
  border-radius: 6px;
  z-index: 10;
  top: 25px;
  left: 0;
  box-shadow: 0 4px 8px rgba(0,0,0,0.18);
}

.dropdown-content a {
  color: var(--ink);
  padding: 8px 12px;
  text-decoration: none;
  display: block;
}

.dropdown-content a:hover {
  background-color: rgba(0,0,0,0.06);
  color: var(--accent);
}

.dropdown:hover .dropdown-content { display: block; }
.dropbtn:hover { color: var(--accent); }

/* --- Botões de filtro (Receita/Despesa) --- */
.tipo-btn {
  background: var(--surface-2);
  color: var(--ink);
  border: 1px solid var(--border-strong);
  transition: 0.2s;
}
.tipo-btn:hover {
  background: var(--accent);
  color: #000;
}
.tipo-btn.receita.ativo {
  background: var(--chip-ok-bg);
  color: var(--chip-ok-ink);
  border-color: var(--chip-ok-border);
}
.tipo-btn.despesa.ativo {
  background: var(--chip-bad-bg);
  color: var(--chip-bad-ink);
  border-color: var(--chip-bad-border);
}
.tipo-btn.ativo { filter: brightness(1.02); }

/* Campos padrão (inputs e selects) */
.input-fin {
  width: 100%;
  padding: 8px 10px;
  background: var(--surface-3);
  border: 1px solid var(--border-strong);
  border-radius: 6px;
  color: var(--ink);
  font-size: 14px;
}

.input-fin:focus {
  outline: none;
  border-color: var(--accent);
  box-shadow: 0 0 4px var(--focus);
}

/* ===== FIX: força cores do select/option (corrige “fundo preto” no tema claro) ===== */
select.input-fin {
  background-color: var(--surface-3);
  color: var(--ink);
}
select.input-fin option {
  background-color: var(--card);
  color: var(--ink);
}
select.input-fin optgroup {
  background-color: var(--card);
  color: var(--ink);
}

/* --- LINKS --- */
.link { color: var(--accent); text-decoration: none; }
.link:hover { text-decoration: underline; }

.link-danger { color: var(--chip-bad-ink); text-decoration: none; }
.link-danger:hover { text-decoration: underline; }

/* --- MODAL PADRÃO --- */
.modal-fin {
  display: none;
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: rgba(0,0,0,0.65);
  z-index: 200;
  align-items: center;
  justify-content: center;
}
html[data-theme="light"] .modal-fin { background: rgba(0,0,0,0.35); }

.modal-content-fin {
  background: var(--card);
  border: 1px solid var(--border-strong);
  border-radius: 12px;
  padding: 20px;
  width: 90%;
  max-height: 80vh;
  overflow-y: auto;
  box-shadow: 0 0 25px rgba(0,0,0,0.18);
}

/* --- BOTÕES --- */
.btn-danger {
  background: var(--chip-bad-bg);
  color: var(--chip-bad-ink);
  border: 1px solid var(--chip-bad-border);
}
.btn-danger:hover { filter: brightness(1.03); }

/* ====== Layouts de topo reutilizáveis ====== */
.toolbar-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 10px;
  flex-wrap: wrap;
}

.toolbar-left { display: flex; gap: 8px; align-items: center; }
.toolbar-right { display: flex; gap: 10px; align-items: center; }

.fluxo-filter-form {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
  align-items: center;
}

/* header de Contas: título + botão */
.contas-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 10px;
}

/* Tabelas com rolagem horizontal suave no mobile */
.table-wrapper {
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
}

/* ====== Mobile (máx 768px) ====== */
@media (max-width: 768px) {
  .toolbar-row { flex-direction: column; align-items: stretch; }
  .toolbar-left, .toolbar-right { width: 100%; }

  .toolbar-right { flex-direction: column; align-items: stretch; }

  .toolbar-right #frmPeriodo,
  .toolbar-right #busca,
  .toolbar-right .btn { width: 100%; }

  .toolbar-right .btn { text-align: center; }

  .fluxo-filter-form { flex-direction: column; align-items: stretch; }
  .fluxo-filter-form > div { width: 100%; }
  .fluxo-filter-form .btn { width: 100%; text-align: center; }

  .contas-header { flex-direction: column; align-items: stretch; }
  .contas-header .btn { width: 100%; text-align: center; }
}
