:root {
  --bg: #f6f7f6;
  --card: #ffffff;
  --text: #1c2321;
  --muted: #64706c;
  --line: #e3e7e5;
  --accent: #0f766e;
  --accent-soft: #d8efec;
  --danger: #b42318;
  --warn-bg: #fff4d6;
  --warn-text: #6b4e00;
  --radius: 16px;
  --safe-b: env(safe-area-inset-bottom, 0px);
  --safe-t: env(safe-area-inset-top, 0px);
  color-scheme: light dark;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #101413;
    --card: #1a201e;
    --text: #e8eeec;
    --muted: #9aa6a2;
    --line: #2a3330;
    --accent: #2dd4bf;
    --accent-soft: #123a35;
    --danger: #f97066;
    --warn-bg: #3a2f10;
    --warn-text: #f5d77a;
  }
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }

html, body { margin: 0; background: var(--bg); color: var(--text); }

body {
  font: 16px/1.4 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  min-height: 100dvh;
  -webkit-text-size-adjust: 100%;
}

body.noscroll { overflow: hidden; }

h1 { font-size: 1.6rem; margin: .5rem 0; }
h2 { font-size: 1.15rem; margin: 0; }
small { color: var(--muted); font-size: .85rem; }
.muted { color: var(--muted); }
.error { color: var(--danger); min-height: 1.2em; margin: .25rem 0; }

button { font: inherit; color: inherit; cursor: pointer; }
input, select {
  font: inherit; color: var(--text); background: var(--card);
  border: 1px solid var(--line); border-radius: 12px; padding: .75rem; width: 100%;
}
input:focus, select:focus { outline: 2px solid var(--accent); outline-offset: 1px; }
label { display: grid; gap: .35rem; font-size: .9rem; color: var(--muted); }

.btn {
  display: block; width: 100%; padding: .85rem 1rem; border-radius: 14px;
  border: 1px solid var(--line); background: var(--card); font-weight: 600;
}
.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
@media (prefers-color-scheme: dark) { .btn.primary { color: #062421; } }
.btn.big { padding: 1.05rem; font-size: 1.05rem; }
.btn.link { border: 0; background: none; color: var(--accent); font-weight: 500; }
.btn.danger { color: var(--danger); }
.btn:active, .pad button:active, .cat-grid button:active { transform: scale(.97); }

.stack { display: grid; gap: .8rem; }

.screen {
  max-width: 460px; margin: 0 auto;
  padding: calc(1.5rem + var(--safe-t)) 1.25rem calc(2rem + var(--safe-b));
  display: grid; gap: 1rem;
}
.screen.center { text-align: center; min-height: 100dvh; align-content: center; }
.logo { font-size: 3.5rem; }
.back { justify-self: start; border: 0; background: none; color: var(--accent); padding: 0; font-size: 1rem; }

.qr { display: grid; place-items: center; background: #fff; border-radius: var(--radius); padding: 1rem; }
.qr img { width: min(260px, 70vw); height: auto; image-rendering: pixelated; }
.code {
  font: 600 1.05rem/1.5 ui-monospace, "SF Mono", Menlo, monospace;
  text-align: center; letter-spacing: .04em; word-break: normal; overflow-wrap: normal; margin: 0;
}
.warn { background: var(--warn-bg); color: var(--warn-text); padding: 1rem; border-radius: var(--radius); font-size: .92rem; }

.scanner { display: grid; gap: .5rem; }
.scanner video { width: 100%; border-radius: var(--radius); background: #000; aspect-ratio: 1; object-fit: cover; }

/* PIN e teclado numérico */
.dots { display: flex; justify-content: center; gap: .9rem; margin: .5rem 0; }
.dots i { width: 14px; height: 14px; border-radius: 50%; border: 2px solid var(--muted); }
.dots i.on { background: var(--accent); border-color: var(--accent); }
.pad { display: grid; grid-template-columns: repeat(3, 1fr); gap: .6rem; max-width: 320px; margin: 0 auto; width: 100%; }
.pad button {
  height: 64px; border-radius: 16px; border: 0; background: var(--card);
  font-size: 1.5rem; font-weight: 500; box-shadow: 0 1px 0 var(--line);
}
.pad.small { max-width: none; gap: .45rem; }
.pad.small button { height: 46px; font-size: 1.3rem; background: var(--bg); box-shadow: none; }

/* Telas principais */
.main { max-width: 560px; margin: 0 auto; padding: var(--safe-t) 0 calc(90px + var(--safe-b)); }

.hero { padding: 1.5rem 1.25rem 1rem; }
.total { font-size: 2.3rem; font-weight: 700; letter-spacing: -.02em; }
.bal { color: var(--muted); }
.bal b { color: var(--text); }
.sync { margin-left: .4rem; font-size: .75rem; color: var(--muted); }
.sync.off { color: var(--warn-text); }

.list { list-style: none; margin: 0; padding: 0 .75rem; }
.list .day { font-size: .8rem; color: var(--muted); padding: 1rem .5rem .35rem; text-transform: capitalize; }
.list .row {
  display: grid; grid-template-columns: 2.5rem 1fr auto; align-items: center; gap: .6rem;
  background: var(--card); padding: .7rem .8rem; border-radius: 14px; margin-bottom: .4rem; cursor: pointer;
}
.row .ic { font-size: 1.5rem; text-align: center; }
.row .t { display: grid; min-width: 0; }
.row .t b { font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.row .v { font-weight: 600; font-variant-numeric: tabular-nums; }
.empty { text-align: center; color: var(--muted); padding: 2.5rem 1rem; list-style: none; }

.bar-head { display: flex; align-items: center; justify-content: space-between; padding: 1.25rem; gap: 1rem; }
.bar-head button { border: 0; background: var(--card); width: 40px; height: 40px; border-radius: 50%; font-size: 1.4rem; }
.bar-head button:disabled { opacity: .3; }
.card { background: var(--card); border-radius: var(--radius); padding: 1rem; margin: 0 .75rem .75rem; }
.card .total { font-size: 1.9rem; }
.split { display: flex; justify-content: space-between; gap: 1rem; color: var(--muted); font-size: .9rem; flex-wrap: wrap; }
.split b { color: var(--text); }
.cats { list-style: none; padding: 0; margin: .5rem 0 0; display: grid; gap: .7rem; }
.cats li { display: grid; grid-template-columns: 1fr auto; row-gap: .3rem; }
.bar { grid-column: 1 / -1; height: 8px; background: var(--bg); border-radius: 4px; overflow: hidden; }
.bar i { display: block; height: 100%; background: var(--accent); border-radius: 4px; width: 0; }

.tabs {
  position: fixed; bottom: 0; left: 0; right: 0; display: flex; justify-content: center;
  background: var(--card); border-top: 1px solid var(--line); padding-bottom: var(--safe-b);
}
.tabs button {
  flex: 1; max-width: 180px; border: 0; background: none; padding: .55rem 0 .6rem;
  display: grid; justify-items: center; font-size: 1.25rem; opacity: .55;
}
.tabs button span { font-size: .72rem; }
.tabs button.on { opacity: 1; color: var(--accent); }

.fab {
  position: fixed; right: max(1.25rem, calc(50vw - 260px)); bottom: calc(76px + var(--safe-b));
  width: 64px; height: 64px; border-radius: 50%; border: 0; background: var(--accent); color: #fff;
  font-size: 2.2rem; line-height: 1; box-shadow: 0 6px 20px rgb(15 118 110 / .35);
}
@media (prefers-color-scheme: dark) { .fab { color: #062421; } }

/* Folha de novo gasto */
.sheet-wrap {
  position: fixed; inset: 0; background: rgb(0 0 0 / .35); display: flex; align-items: flex-end; justify-content: center; z-index: 10;
  animation: fade .15s ease-out;
}
.sheet {
  background: var(--card); width: 100%; max-width: 560px; border-radius: 22px 22px 0 0;
  padding: calc(.5rem + var(--safe-t)) 1rem calc(1rem + var(--safe-b)); display: grid; gap: .5rem;
  max-height: 100dvh; overflow-y: auto; animation: up .2s ease-out;
}
.sheet-top { display: flex; justify-content: space-between; }
.x { border: 0; background: none; font-size: 1.1rem; padding: .3rem .5rem; color: var(--muted); }
.x.danger { color: var(--danger); font-size: .95rem; }
.amount { text-align: center; font-size: 2.4rem; font-weight: 700; font-variant-numeric: tabular-nums; letter-spacing: -.02em; }
.amount.zero { color: var(--muted); }
.shake { animation: shake .35s; }
.chips { display: grid; gap: .45rem; }
.seg { display: grid; grid-template-columns: 1fr 1fr; background: var(--bg); border-radius: 12px; padding: 3px; }
.seg button { border: 0; background: none; padding: .5rem .3rem; border-radius: 10px; font-size: .88rem; color: var(--muted); }
.seg button.on { background: var(--card); color: var(--text); font-weight: 600; box-shadow: 0 1px 3px rgb(0 0 0 / .12); }
.more summary { color: var(--accent); font-size: .9rem; cursor: pointer; padding: .2rem 0; }
.row2 { display: grid; grid-template-columns: 1.4fr 1fr; gap: .5rem; margin-top: .5rem; }
.hint { text-align: center; margin: 0; color: var(--muted); font-size: .85rem; }
.cat-grid { display: grid; grid-template-columns: repeat(4, 1fr); gap: .45rem; }
.cat-grid button {
  border: 1px solid var(--line); background: var(--card); border-radius: 14px; padding: .55rem .2rem;
  display: grid; justify-items: center; gap: .15rem; font-size: .72rem; font-weight: 500;
}
.cat-grid button span { font-size: 1.55rem; }
.cat-grid button.on { border-color: var(--accent); background: var(--accent-soft); }

#toast {
  position: fixed; left: 50%; transform: translateX(-50%); bottom: calc(150px + var(--safe-b));
  background: #1c2321; color: #fff; padding: .7rem 1rem; border-radius: 12px; z-index: 20;
  display: flex; gap: 1rem; align-items: center; width: max-content; max-width: calc(100vw - 2rem); box-shadow: 0 6px 20px rgb(0 0 0 / .25);
}
#toast[hidden] { display: none; }
#toast button { border: 0; background: none; color: #5eead4; font-weight: 700; }

@keyframes up { from { transform: translateY(40px); opacity: 0; } }
@keyframes fade { from { opacity: 0; } }
@keyframes shake { 25% { transform: translateX(-8px); } 50% { transform: translateX(8px); } 75% { transform: translateX(-4px); } }
@media (prefers-reduced-motion: reduce) { * { animation: none !important; } }
