:root {
  --bg: #FAF7F2;
  --surface: #FFFFFF;
  --text-1: #1C0F04;
  --text-2: #6B5C4E;
  --text-3: #9C8E7E;
  --accent: #B85E1A;
  --accent-hover: #A35217;
  --accent-10: rgba(184,94,26,0.07);
  --accent-surface: #FDF0E3;
  --positive: #1A7A35;
  --positive-bg: #EDFBF0;
  --negative: #C43026;
  --negative-bg: #FDECEB;
  --warning: #A87B10;
  --warning-bg: #FFF8E6;
  --border: #E8E0D5;
  --border-light: #F0EBE4;
  --shadow: 0 1px 3px rgba(28,15,4,0.05), 0 1px 2px rgba(28,15,4,0.03);
  --shadow-md: 0 4px 12px rgba(28,15,4,0.07);
  --radius: 12px;
  --radius-sm: 8px;
  --font: system-ui, -apple-system, 'Segoe UI', sans-serif;
  --cat-1: #2a78d6;
  --cat-2: #eb6834;
  --cat-3: #1baf7a;
  --chart-grid: #E8E0D5;
  --chart-axis: #B0A294;
  --chart-muted: #9C8E7E;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:where(:not([data-theme="light"])) {
    --bg: #141210; --surface: #211E1A; --text-1: #F0E8DF; --text-2: #B0A294; --text-3: #756859;
    --accent: #D47830; --accent-hover: #E08A3C; --accent-10: rgba(212,120,48,0.1); --accent-surface: #2D2218;
    --positive: #2DA04A; --positive-bg: #1A2E1F; --negative: #E04A42; --negative-bg: #2E1A18;
    --warning: #D4A020; --warning-bg: #2E2818; --border: #38322A; --border-light: #2E2820;
    --shadow: 0 1px 3px rgba(0,0,0,0.2), 0 1px 2px rgba(0,0,0,0.1); --shadow-md: 0 4px 12px rgba(0,0,0,0.25);
    --cat-1: #3987e5; --cat-2: #d95926; --cat-3: #199e70;
    --chart-grid: #38322A; --chart-axis: #756859; --chart-muted: #756859;
    color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #141210; --surface: #211E1A; --text-1: #F0E8DF; --text-2: #B0A294; --text-3: #756859;
  --accent: #D47830; --accent-hover: #E08A3C; --accent-10: rgba(212,120,48,0.1); --accent-surface: #2D2218;
  --positive: #2DA04A; --positive-bg: #1A2E1F; --negative: #E04A42; --negative-bg: #2E1A18;
  --warning: #D4A020; --warning-bg: #2E2818; --border: #38322A; --border-light: #2E2820;
  --shadow: 0 1px 3px rgba(0,0,0,0.2), 0 1px 2px rgba(0,0,0,0.1); --shadow-md: 0 4px 12px rgba(0,0,0,0.25);
  --cat-1: #3987e5; --cat-2: #d95926; --cat-3: #199e70;
  --chart-grid: #38322A; --chart-axis: #756859; --chart-muted: #756859;
  color-scheme: dark;
}
:root[data-theme="light"] {
  --bg: #FAF7F2; --surface: #FFFFFF; --text-1: #1C0F04; --text-2: #6B5C4E; --text-3: #9C8E7E;
  --accent: #B85E1A; --accent-hover: #A35217; --accent-10: rgba(184,94,26,0.07); --accent-surface: #FDF0E3;
  --positive: #1A7A35; --positive-bg: #EDFBF0; --negative: #C43026; --negative-bg: #FDECEB;
  --warning: #A87B10; --warning-bg: #FFF8E6; --border: #E8E0D5; --border-light: #F0EBE4;
  --shadow: 0 1px 3px rgba(28,15,4,0.05), 0 1px 2px rgba(28,15,4,0.03); --shadow-md: 0 4px 12px rgba(28,15,4,0.07);
  --cat-1: #2a78d6; --cat-2: #eb6834; --cat-3: #1baf7a;
  --chart-grid: #E8E0D5; --chart-axis: #B0A294; --chart-muted: #9C8E7E;
  color-scheme: light;
}

*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }
html, body { height: 100%; }
body { font-family: var(--font); color: var(--text-1); background: var(--bg); line-height: 1.5; -webkit-font-smoothing: antialiased; }
button { font: inherit; cursor: pointer; border: none; background: none; color: inherit; }
input, select, textarea { font: inherit; color: inherit; background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius-sm); padding: 10px 14px; outline: none; width: 100%; }
input:focus, select:focus, textarea:focus { border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-10); }
a { color: var(--accent); }
@media (prefers-reduced-motion: reduce) { *, *::before, *::after { animation-duration: 0.01ms !important; transition-duration: 0.01ms !important; } }

/* ---------- Login screen ---------- */
#login-screen {
  min-height: 100dvh;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px;
}
.login-card {
  background: var(--surface);
  border-radius: var(--radius);
  box-shadow: var(--shadow-md);
  padding: 40px 32px;
  max-width: 360px;
  width: 100%;
  text-align: center;
}
.login-brand { font-family: Georgia, 'Times New Roman', serif; font-size: 32px; font-weight: 700; color: var(--accent); margin-bottom: 4px; }
.login-sub { font-size: 14px; color: var(--text-3); margin-bottom: 32px; }
.btn-google {
  display: inline-flex; align-items: center; justify-content: center; gap: 10px;
  width: 100%; padding: 12px 20px; border-radius: var(--radius-sm);
  border: 1px solid var(--border); background: var(--surface); color: var(--text-1);
  font-size: 14px; font-weight: 600; transition: background 0.15s, border-color 0.15s;
}
.btn-google:hover { background: var(--accent-10); border-color: var(--accent); }
.login-error { margin-top: 16px; font-size: 13px; color: var(--negative); }

/* ---------- App shell ---------- */
#app { display: flex; min-height: 100dvh; position: relative; }
#sidebar { display: none; flex-direction: column; width: 240px; min-width: 240px; background: var(--surface); border-right: 1px solid var(--border); position: fixed; top: 0; left: 0; bottom: 0; z-index: 20; }
.brand { padding: 28px 24px 24px; font-family: Georgia, 'Times New Roman', serif; font-size: 26px; font-weight: 700; color: var(--accent); letter-spacing: -0.5px; }
.brand small { display: block; font-family: var(--font); font-size: 12px; font-weight: 400; color: var(--text-3); letter-spacing: 0.3px; margin-top: 2px; }
.side-nav { display: flex; flex-direction: column; gap: 2px; padding: 0 12px; flex: 1; }
.nav-btn { display: flex; align-items: center; gap: 12px; padding: 11px 14px; border-radius: var(--radius-sm); font-size: 14px; font-weight: 500; color: var(--text-2); transition: background 0.15s, color 0.15s; text-align: left; width: 100%; }
.nav-btn:hover { background: var(--accent-10); color: var(--text-1); }
.nav-btn.active { background: var(--accent-10); color: var(--accent); font-weight: 600; }
.nav-btn svg { width: 20px; height: 20px; flex-shrink: 0; }
.side-footer { padding: 12px; border-top: 1px solid var(--border-light); }
.user-chip { display: flex; align-items: center; gap: 10px; padding: 8px 10px; }
.user-avatar { width: 32px; height: 32px; border-radius: 50%; background: var(--accent-surface); color: var(--accent); display: flex; align-items: center; justify-content: center; font-size: 13px; font-weight: 700; flex-shrink: 0; }
.user-email { font-size: 12px; color: var(--text-2); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; flex: 1; }
.logout-btn { font-size: 12px; color: var(--text-3); padding: 4px; flex-shrink: 0; }
.logout-btn:hover { color: var(--negative); }

main { flex: 1; overflow-y: auto; overflow-x: hidden; padding-bottom: 80px; min-height: 100dvh; }
.view { display: none; padding: 20px; max-width: 900px; margin: 0 auto; }
.view.active { display: block; }

#bnav { position: fixed; bottom: 0; left: 0; right: 0; display: flex; align-items: center; justify-content: space-around; height: 64px; padding-bottom: env(safe-area-inset-bottom, 0px); background: var(--surface); border-top: 1px solid var(--border); z-index: 30; }
.bnav-btn { display: flex; flex-direction: column; align-items: center; gap: 2px; padding: 6px 12px; font-size: 11px; font-weight: 500; color: var(--text-3); transition: color 0.15s; }
.bnav-btn svg { width: 22px; height: 22px; }
.bnav-btn.active { color: var(--accent); }

.view-header { display: flex; align-items: center; justify-content: space-between; flex-wrap: wrap; gap: 12px; margin-bottom: 24px; }
.view-header h1 { font-size: 22px; font-weight: 700; letter-spacing: -0.3px; }
.mobile-brand { font-family: Georgia, 'Times New Roman', serif; font-size: 20px; font-weight: 700; color: var(--accent); letter-spacing: -0.5px; }
@media (max-width: 767px) { .mobile-brand { display: block; margin-bottom: 4px; } }
@media (min-width: 768px) { .mobile-brand { display: none; } }

.filter-row { display: flex; gap: 6px; flex-wrap: wrap; }
.chip { padding: 6px 14px; border-radius: 20px; border: 1px solid var(--border); font-size: 13px; font-weight: 500; color: var(--text-2); transition: all 0.15s; white-space: nowrap; }
.chip:hover { border-color: var(--accent); color: var(--accent); }
.chip.active { background: var(--accent); color: #fff; border-color: var(--accent); }

.card { background: var(--surface); border-radius: var(--radius); box-shadow: var(--shadow); padding: 20px; }
.card + .card { margin-top: 16px; }
.card-title { font-size: 14px; font-weight: 600; color: var(--text-2); margin-bottom: 16px; text-transform: uppercase; letter-spacing: 0.4px; }

.hero-fig { background: var(--surface); border-radius: var(--radius); box-shadow: var(--shadow); padding: 24px 20px; margin-bottom: 16px; text-align: center; }
.hero-label { display: block; font-size: 13px; color: var(--text-3); margin-bottom: 4px; }
.hero-value { display: block; font-size: 48px; font-weight: 600; letter-spacing: -1.5px; line-height: 1.1; color: var(--text-1); }

.stat-grid { display: grid; grid-template-columns: repeat(2, 1fr); gap: 12px; margin-bottom: 20px; }
.stat-tile { background: var(--surface); border-radius: var(--radius); box-shadow: var(--shadow); padding: 16px; }
.stat-label { display: block; font-size: 12px; color: var(--text-3); margin-bottom: 6px; }
.stat-value { display: block; font-size: 24px; font-weight: 600; letter-spacing: -0.5px; line-height: 1.2; font-variant-numeric: tabular-nums; }
.stat-value.clr-pos { color: var(--positive); }
.stat-value.clr-warn { color: var(--warning); }
.stat-value.clr-neg { color: var(--negative); }

.chart-grid { display: grid; grid-template-columns: 1fr; gap: 16px; margin-bottom: 16px; }
.chart-card { background: var(--surface); border-radius: var(--radius); box-shadow: var(--shadow); padding: 20px; }
.chart-card h3 { font-size: 14px; font-weight: 600; color: var(--text-2); margin-bottom: 16px; text-transform: uppercase; letter-spacing: 0.4px; }
.chart-svg { width: 100%; height: auto; display: block; }

.debtor-item { display: flex; align-items: center; justify-content: space-between; padding: 14px 0; border-bottom: 1px solid var(--border-light); }
.debtor-item:last-child { border-bottom: none; }
.debtor-name { font-size: 14px; font-weight: 500; }
.debtor-detail { font-size: 12px; color: var(--text-3); margin-top: 2px; }
.debtor-amount { font-size: 16px; font-weight: 600; color: var(--negative); font-variant-numeric: tabular-nums; }

.section-gap { margin-top: 20px; }

.fornada-card { background: var(--surface); border-radius: var(--radius); box-shadow: var(--shadow); overflow: hidden; margin-bottom: 12px; }
.fornada-header { display: flex; align-items: center; justify-content: space-between; padding: 16px 20px; cursor: pointer; transition: background 0.15s; }
.fornada-header:hover { background: var(--accent-10); }
.fornada-id { font-weight: 600; color: var(--accent); font-size: 14px; }
.fornada-date { font-size: 13px; color: var(--text-3); margin-left: 8px; }
.fornada-summary { font-size: 13px; color: var(--text-2); }
.fornada-chevron { color: var(--text-3); transition: transform 0.2s; font-size: 18px; display: inline-block; }
.fornada-card.open .fornada-chevron { transform: rotate(180deg); }
.fornada-body { display: none; padding: 0 20px 20px; border-top: 1px solid var(--border-light); }
.fornada-card.open .fornada-body { display: block; }

.detail-section { margin-top: 20px; }
.detail-section h4 { font-size: 12px; font-weight: 600; text-transform: uppercase; letter-spacing: 0.5px; color: var(--text-3); margin-bottom: 10px; padding-bottom: 6px; border-bottom: 1px solid var(--border-light); }
.ing-row { display: flex; justify-content: space-between; align-items: baseline; padding: 6px 0; font-size: 14px; gap: 12px; }
.ing-row .name { flex: 1; min-width: 0; }
.ing-row .qty { color: var(--text-2); text-align: right; white-space: nowrap; font-variant-numeric: tabular-nums; }
.ing-row .cost { font-weight: 500; text-align: right; white-space: nowrap; min-width: 70px; font-variant-numeric: tabular-nums; }
.ing-row.total { font-weight: 600; border-top: 1px solid var(--border-light); padding-top: 10px; margin-top: 4px; }

.info-pills { display: flex; gap: 8px; flex-wrap: wrap; margin-top: 16px; }
.info-pill { display: inline-flex; align-items: center; gap: 6px; padding: 6px 12px; background: var(--accent-10); border-radius: 20px; font-size: 13px; color: var(--text-2); }
.info-pill strong { font-weight: 600; color: var(--text-1); }

.prod-row { display: flex; gap: 8px; flex-wrap: wrap; margin-top: 12px; }
.prod-badge { padding: 4px 12px; border-radius: 6px; background: var(--accent-surface); font-size: 13px; font-weight: 500; color: var(--accent); }

.margin-cards { display: grid; grid-template-columns: 1fr; gap: 12px; margin-top: 20px; }
.margin-card { border: 1px solid var(--border); border-radius: var(--radius-sm); padding: 16px; position: relative; }
.margin-card.positive { border-left: 3px solid var(--positive); }
.margin-card.negative { border-left: 3px solid var(--negative); }
.margin-head { display: flex; align-items: baseline; justify-content: space-between; margin-bottom: 12px; }
.margin-size { font-size: 16px; font-weight: 600; }
.margin-detail { font-size: 12px; color: var(--text-3); }
.margin-row { display: flex; justify-content: space-between; font-size: 13px; padding: 3px 0; color: var(--text-2); }
.margin-row span:last-child { font-variant-numeric: tabular-nums; }
.margin-result { display: flex; justify-content: space-between; font-size: 15px; font-weight: 600; padding-top: 10px; margin-top: 8px; border-top: 1px solid var(--border-light); }
.margin-result .pos { color: var(--positive); }
.margin-result .neg { color: var(--negative); }

.btn-primary { display: inline-flex; align-items: center; justify-content: center; gap: 6px; padding: 10px 20px; background: var(--accent); color: #fff; border-radius: var(--radius-sm); font-size: 14px; font-weight: 600; transition: background 0.15s; }
.btn-primary:hover { background: var(--accent-hover); }
.btn-primary:disabled { opacity: 0.6; cursor: default; }
.btn-primary svg { width: 16px; height: 16px; }
.btn-secondary { display: inline-flex; align-items: center; justify-content: center; gap: 6px; padding: 10px 20px; background: var(--accent-10); color: var(--accent); border-radius: var(--radius-sm); font-size: 14px; font-weight: 600; }
.btn-ghost { font-size: 13px; color: var(--text-3); padding: 6px 10px; }
.btn-ghost:hover { color: var(--negative); }
.btn-block { width: 100%; }

.sale-item { display: flex; align-items: center; gap: 12px; padding: 14px 0; border-bottom: 1px solid var(--border-light); }
.sale-item:last-child { border-bottom: none; }
.sale-date { font-size: 12px; color: var(--text-3); white-space: nowrap; min-width: 48px; font-variant-numeric: tabular-nums; }
.sale-info { flex: 1; min-width: 0; }
.sale-client { font-size: 14px; font-weight: 500; }
.sale-desc { font-size: 12px; color: var(--text-2); margin-top: 1px; }
.sale-amount { font-size: 15px; font-weight: 600; text-align: right; white-space: nowrap; font-variant-numeric: tabular-nums; }
.sale-status { margin-left: 4px; }
.badge { display: inline-block; padding: 3px 10px; border-radius: 12px; font-size: 11px; font-weight: 600; white-space: nowrap; }
.badge-paid { background: var(--positive-bg); color: var(--positive); }
.badge-pending { background: var(--warning-bg); color: var(--warning); cursor: pointer; }
.badge-pending:hover { opacity: 0.8; }

.venda-summary { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; margin-bottom: 20px; }
.venda-summary .stat-tile { margin: 0; }

.config-section { margin-bottom: 32px; }
.config-section h2 { font-size: 16px; font-weight: 600; margin-bottom: 16px; padding-bottom: 8px; border-bottom: 1px solid var(--border); }
.ing-card { background: var(--surface); border-radius: var(--radius); box-shadow: var(--shadow); padding: 16px; margin-bottom: 10px; display: flex; align-items: center; justify-content: space-between; gap: 12px; }
.ing-card-info { flex: 1; min-width: 0; }
.ing-card-name { font-size: 14px; font-weight: 500; }
.ing-card-detail { font-size: 12px; color: var(--text-2); margin-top: 2px; }
.ing-card-cost { text-align: right; }
.ing-card-cost .cost-val { font-size: 16px; font-weight: 600; color: var(--accent); }
.ing-card-cost .cost-unit { font-size: 11px; color: var(--text-3); }
.ing-card-actions { display: flex; gap: 4px; }
.icon-btn { width: 30px; height: 30px; display: flex; align-items: center; justify-content: center; border-radius: 6px; color: var(--text-3); }
.icon-btn:hover { background: var(--accent-10); color: var(--accent); }
.icon-btn svg { width: 16px; height: 16px; }

.form-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.form-group { display: flex; flex-direction: column; gap: 4px; }
.form-group.full { grid-column: 1 / -1; }
.form-label { font-size: 12px; font-weight: 500; color: var(--text-2); }
.form-actions { display: flex; gap: 8px; grid-column: 1 / -1; margin-top: 4px; }
.field-error { font-size: 12px; color: var(--negative); }

.size-card { background: var(--surface); border-radius: var(--radius); box-shadow: var(--shadow); padding: 16px; margin-bottom: 10px; display: flex; align-items: center; justify-content: space-between; flex-wrap: wrap; gap: 12px; }
.size-name { font-size: 16px; font-weight: 600; }
.size-weights { font-size: 12px; color: var(--text-2); }
.size-price { font-size: 20px; font-weight: 600; color: var(--accent); }

.empty-action { text-align: center; padding: 24px; color: var(--text-3); font-size: 14px; }
.loading-state { text-align: center; padding: 40px; color: var(--text-3); font-size: 14px; }


.autocomplete-wrap { position: relative; }
.autocomplete-list { position: absolute; top: 100%; left: 0; right: 0; background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius-sm); box-shadow: var(--shadow-md); max-height: 180px; overflow-y: auto; z-index: 10; margin-top: 4px; display: none; }
.autocomplete-list.show { display: block; }
.autocomplete-item { padding: 10px 14px; font-size: 14px; cursor: pointer; }
.autocomplete-item:hover, .autocomplete-item.highlighted { background: var(--accent-10); }
.autocomplete-item.create-new { color: var(--accent); font-weight: 600; border-top: 1px solid var(--border-light); }

.form-panel { display: none; margin-bottom: 16px; }
.form-panel.show { display: block; }

#toast { position: fixed; bottom: 80px; left: 50%; transform: translateX(-50%) translateY(20px); background: var(--text-1); color: var(--bg); padding: 10px 24px; border-radius: var(--radius-sm); font-size: 14px; font-weight: 500; opacity: 0; pointer-events: none; transition: opacity 0.25s, transform 0.25s; z-index: 50; max-width: 90vw; text-align: center; }
#toast.show { opacity: 1; transform: translateX(-50%) translateY(0); pointer-events: auto; }
#toast.error { background: var(--negative); color: #fff; }

.btn-danger { display: inline-flex; align-items: center; justify-content: center; gap: 6px; padding: 10px 20px; background: var(--negative); color: #fff; border-radius: var(--radius-sm); font-size: 14px; font-weight: 600; transition: opacity 0.15s; }
.btn-danger:hover { opacity: 0.88; }

.modal-overlay { position: fixed; inset: 0; background: rgba(11,11,11,0.45); display: none; align-items: center; justify-content: center; z-index: 100; padding: 20px; }
.modal-overlay.show { display: flex; }
.modal-card { background: var(--surface); border-radius: var(--radius); box-shadow: var(--shadow-md); padding: 24px; max-width: 380px; width: 100%; }
.modal-title { font-size: 16px; font-weight: 700; margin-bottom: 8px; }
.modal-message { font-size: 14px; color: var(--text-2); line-height: 1.5; margin-bottom: 20px; }
.modal-actions { display: flex; justify-content: flex-end; gap: 8px; }

.card-actions-row { display: flex; gap: 4px; flex-shrink: 0; }

@media (min-width: 768px) {
  #sidebar { display: flex; }
  main { margin-left: 240px; padding-bottom: 24px; }
  #bnav { display: none; }
  .view { padding: 32px; }
  .stat-grid { grid-template-columns: repeat(4, 1fr); }
  .chart-grid { grid-template-columns: 1fr 1fr; }
  .margin-cards { grid-template-columns: 1fr 1fr; }
}
