/* ============ Mercado — receipt-ledger design system ============ */
:root {
  --paper: #F7F5EF;
  --paper-2: #FFFFFF;
  --ink: #1C1A16;
  --ink-60: #6E6A61;
  --ink-15: #E3E0D7;
  --green: #0E7A46;
  --green-dark: #0A5D36;
  --tag: #F5C518;      /* price-tag yellow: only for "sem categoria" */
  --red: #C0392B;
  --mono: "IBM Plex Mono", ui-monospace, monospace;
  --sans: "Archivo", system-ui, sans-serif;
  --safe-b: env(safe-area-inset-bottom, 0px);
}

* { box-sizing: border-box; margin: 0; }
html { -webkit-text-size-adjust: 100%; }
body {
  font-family: var(--sans);
  background: var(--paper);
  color: var(--ink);
  min-height: 100dvh;
  padding-bottom: calc(64px + var(--safe-b));
}
button { font: inherit; color: inherit; background: none; border: 0; cursor: pointer; }
:focus-visible { outline: 3px solid var(--green); outline-offset: 2px; border-radius: 4px; }

/* ---------- app frame ---------- */
#app { max-width: 560px; margin: 0 auto; padding: 16px 16px 24px; }

.screen-title {
  font-variation-settings: "wdth" 115;
  font-weight: 850;
  text-transform: uppercase;
  letter-spacing: .04em;
  font-size: 1.35rem;
  margin: 8px 0 16px;
}
.screen-title small {
  display: block; font-weight: 500; letter-spacing: 0; text-transform: none;
  font-variation-settings: "wdth" 100;
  color: var(--ink-60); font-size: .85rem; margin-top: 2px;
}

/* ---------- tab bar ---------- */
#tabbar {
  position: fixed; inset: auto 0 0 0;
  display: grid; grid-template-columns: repeat(3, 1fr);
  background: var(--paper-2);
  border-top: 2px solid var(--ink);
  padding-bottom: var(--safe-b);
  z-index: 20;
}
.tab {
  display: flex; flex-direction: column; align-items: center; gap: 2px;
  padding: 10px 0 8px; color: var(--ink-60); position: relative;
  min-height: 56px;
}
.tab svg { width: 22px; height: 22px; fill: currentColor; }
.tab span { font-size: .72rem; font-weight: 600; letter-spacing: .02em; }
.tab[aria-current="true"] { color: var(--green-dark); }
.tab[aria-current="true"]::before {
  content: ""; position: absolute; top: -2px; left: 20%; right: 20%;
  height: 3px; background: var(--green);
}
#badge {
  position: absolute; top: 6px; right: calc(50% - 26px);
  background: var(--tag); color: var(--ink);
  font-style: normal; font-family: var(--mono); font-size: .68rem; font-weight: 600;
  min-width: 18px; height: 18px; border-radius: 9px;
  display: flex; align-items: center; justify-content: center; padding: 0 5px;
  border: 1.5px solid var(--ink);
}

/* ---------- buttons ---------- */
.btn {
  display: flex; align-items: center; justify-content: center; gap: 8px;
  width: 100%; min-height: 52px; padding: 14px 18px;
  background: var(--green); color: #fff;
  font-weight: 700; font-size: 1rem; letter-spacing: .01em;
  border-radius: 10px;
}
.btn:active { background: var(--green-dark); }
.btn.secondary { background: transparent; color: var(--ink); border: 2px solid var(--ink); }
.btn.quiet { background: transparent; color: var(--ink-60); min-height: 44px; font-weight: 600; }
.btn.danger { background: transparent; color: var(--red); border: 2px solid var(--red); }
.btn[disabled] { opacity: .5; }

/* ---------- forms ---------- */
label.field { display: block; margin-bottom: 14px; }
label.field > span {
  display: block; font-size: .78rem; font-weight: 700; text-transform: uppercase;
  letter-spacing: .06em; color: var(--ink-60); margin-bottom: 6px;
}
input, select {
  width: 100%; min-height: 48px; padding: 10px 12px;
  font: inherit; color: var(--ink);
  background: var(--paper-2); border: 2px solid var(--ink); border-radius: 8px;
}
input[type="number"], input[inputmode="decimal"] { font-family: var(--mono); }
select { appearance: none; background-image: linear-gradient(45deg, transparent 50%, var(--ink) 50%), linear-gradient(135deg, var(--ink) 50%, transparent 50%); background-position: calc(100% - 18px) 55%, calc(100% - 13px) 55%; background-size: 5px 5px; background-repeat: no-repeat; }

/* ---------- the ledger (signature element) ---------- */
.ledger {
  background: var(--paper-2);
  border: 2px solid var(--ink);
  border-radius: 12px;
  padding: 14px 14px 10px;
  font-family: var(--mono);
  font-size: .84rem;
}
.ledger-head {
  font-family: var(--sans); font-weight: 800; text-transform: uppercase;
  letter-spacing: .03em; font-size: .95rem;
  display: flex; justify-content: space-between; align-items: baseline; gap: 8px;
  padding-bottom: 8px; border-bottom: 2px solid var(--ink); margin-bottom: 6px;
}
.ledger-head time { font-family: var(--mono); font-weight: 500; font-size: .75rem; color: var(--ink-60); text-transform: none; }

.line {
  display: grid; grid-template-columns: 1fr auto; align-items: baseline; gap: 8px;
  padding: 7px 0; border-bottom: 1px dashed var(--ink-15);
}
.line:last-child { border-bottom: 0; }
.line .name { text-transform: uppercase; overflow-wrap: anywhere; }
.line .meta { display: block; color: var(--ink-60); font-size: .72rem; margin-top: 2px; }
.line .val { font-weight: 600; font-variant-numeric: tabular-nums; white-space: nowrap; }

.totals { border-top: 3px double var(--ink); margin-top: 6px; padding-top: 8px; }
.totals .line { border: 0; padding: 3px 0; }
.totals .grand { font-size: 1.05rem; font-weight: 600; }
.totals .grand .val { font-size: 1.2rem; }

/* category chip on a ledger line */
.chip {
  font-family: var(--sans); font-size: .7rem; font-weight: 700;
  padding: 3px 8px; border-radius: 6px; margin-top: 4px;
  background: var(--paper); border: 1.5px solid var(--ink-15); color: var(--ink-60);
  min-height: 28px;
}
.chip.none { background: var(--tag); border-color: var(--ink); color: var(--ink); }

/* ---------- month bars ---------- */
.bars { display: grid; gap: 10px; margin: 16px 0; }
.bar-row { display: grid; gap: 4px; }
.bar-top { display: flex; justify-content: space-between; font-size: .8rem; font-weight: 700; }
.bar-top .amount { font-family: var(--mono); font-weight: 600; font-variant-numeric: tabular-nums; }
.bar-track { height: 14px; background: var(--paper-2); border: 1.5px solid var(--ink); border-radius: 7px; overflow: hidden; }
.bar-fill { height: 100%; background: var(--green); border-radius: 5px 0 0 5px; }
.bar-row.uncat .bar-fill { background: repeating-linear-gradient(-45deg, var(--tag) 0 6px, #E0B200 6px 12px); }

.month-total {
  background: var(--ink); color: var(--paper);
  border-radius: 12px; padding: 16px;
  display: flex; justify-content: space-between; align-items: baseline;
  font-family: var(--mono);
}
.month-total b { font-family: var(--sans); text-transform: uppercase; letter-spacing: .05em; font-size: .8rem; }
.month-total .big { font-size: 1.6rem; font-weight: 600; font-variant-numeric: tabular-nums; }

.month-nav { display: grid; grid-template-columns: 44px 1fr 44px; gap: 8px; align-items: center; margin-bottom: 14px; }
.month-nav h2 { text-align: center; font-size: 1rem; text-transform: capitalize; }
.month-nav button { min-height: 44px; font-size: 1.2rem; border: 2px solid var(--ink); border-radius: 8px; background: var(--paper-2); }

/* ---------- scanner ---------- */
.scan-wrap { position: relative; border-radius: 12px; overflow: hidden; border: 2px solid var(--ink); background: #000; aspect-ratio: 3/4; }
.scan-wrap video { width: 100%; height: 100%; object-fit: cover; }
.scan-frame {
  position: absolute; inset: 12% 15%; border: 3px solid #fff; border-radius: 14px;
  box-shadow: 0 0 0 999px rgba(0,0,0,.35);
  pointer-events: none;   /* taps must reach the video for tap-to-focus */
}
.scan-line {
  position: absolute; left: 6%; right: 6%; height: 2px; background: var(--tag); top: 8%;
  animation: sweep 2.2s ease-in-out infinite alternate;
}
@keyframes sweep { to { top: 90%; } }
@media (prefers-reduced-motion: reduce) { .scan-line { animation: none; top: 50%; } }
.scan-hint { text-align: center; color: var(--ink-60); font-size: .85rem; margin: 12px 0; }

/* ---------- lists, cards, misc ---------- */
.card { background: var(--paper-2); border: 2px solid var(--ink); border-radius: 12px; padding: 14px; margin-bottom: 12px; }
.receipt-item { display: flex; justify-content: space-between; gap: 8px; align-items: center; width: 100%; text-align: left; padding: 12px 4px; border-bottom: 1px dashed var(--ink-15); min-height: 52px; }
.receipt-item:last-child { border-bottom: 0; }
.receipt-item .store { font-weight: 700; }
.receipt-item .sub { color: var(--ink-60); font-size: .78rem; }
.receipt-item .amt { font-family: var(--mono); font-weight: 600; white-space: nowrap; }

.stack { display: grid; gap: 10px; }
.row2 { display: grid; grid-template-columns: 1fr 110px; gap: 8px; }
.row3 { display: grid; grid-template-columns: 1fr 92px 40px; gap: 8px; align-items: end; }
.iconbtn { min-width: 40px; min-height: 48px; border: 2px solid var(--ink-15); border-radius: 8px; color: var(--red); font-weight: 800; }

.empty { text-align: center; color: var(--ink-60); padding: 32px 12px; }
.empty b { display: block; color: var(--ink); margin-bottom: 4px; }

.error-box { background: #FBEAE8; border: 2px solid var(--red); color: var(--red); border-radius: 10px; padding: 12px; font-weight: 600; margin: 12px 0; }

/* login */
.login { min-height: 80dvh; display: flex; flex-direction: column; justify-content: center; }
.brand {
  font-variation-settings: "wdth" 120; font-weight: 900; font-size: 2.2rem;
  text-transform: uppercase; letter-spacing: .02em; margin-bottom: 2px;
}
.brand-sub { color: var(--ink-60); margin-bottom: 28px; font-family: var(--mono); font-size: .8rem; }

/* sheet (category picker) */
#sheet {
  position: fixed; inset: 0; background: rgba(28,26,22,.45); z-index: 30;
  display: flex; align-items: flex-end;
}
#sheet .panel {
  background: var(--paper); width: 100%; max-width: 560px; margin: 0 auto;
  border-radius: 16px 16px 0 0; padding: 16px 16px calc(16px + var(--safe-b));
  border-top: 3px solid var(--ink);
  max-height: 70dvh; overflow: auto;
}
#sheet h3 { margin-bottom: 4px; }
#sheet p { color: var(--ink-60); font-size: .85rem; margin-bottom: 12px; font-family: var(--mono); }
.cat-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.cat-grid button {
  min-height: 48px; border: 2px solid var(--ink); border-radius: 10px;
  background: var(--paper-2); font-weight: 700; font-size: .88rem; padding: 8px;
}
.cat-grid button:active { background: var(--green); color: #fff; }

/* toast */
#toast {
  position: fixed; left: 50%; bottom: calc(84px + var(--safe-b));
  transform: translateX(-50%) translateY(20px);
  background: var(--ink); color: var(--paper);
  padding: 10px 16px; border-radius: 10px; font-weight: 600; font-size: .9rem;
  opacity: 0; pointer-events: none; transition: opacity .2s, transform .2s;
  z-index: 40; max-width: 90vw; text-align: center;
}
#toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }
@media (prefers-reduced-motion: reduce) { #toast { transition: none; } }

/* ---------- camera diagnosis (scan screen, failure only) ---------- */
.diag { background: var(--paper-2); border: 2px dashed var(--ink-15); border-radius: 10px; padding: 12px; margin: 12px 0; font-size: .8rem; }
.diag > strong { display: block; margin-bottom: 8px; font-size: .85rem; }
.diag-row { display: flex; justify-content: space-between; gap: 10px; padding: 4px 0; border-bottom: 1px dashed var(--ink-15); }
.diag-row:last-child { border-bottom: 0; }
.diag-row span { color: var(--ink-60); white-space: nowrap; }
.diag-row code { font-family: var(--mono); font-size: .75rem; text-align: right; word-break: break-word; }
.diag-actions { display: grid; gap: 8px; }

/* ---------- camera controls (scan screen) ---------- */
.cam-ctl { display: grid; gap: 10px; margin: 10px 0; }
.cam-row { display: flex; align-items: center; gap: 10px; }
.cam-row > span { color: var(--ink-60); font-size: .8rem; min-width: 42px; }
.cam-row input[type=range] { flex: 1; accent-color: var(--green); min-height: 44px; }
.chip { min-width: 44px; min-height: 44px; border: 2px solid var(--ink-15); border-radius: 8px; font-family: var(--mono); font-weight: 600; }
.chip.on, .btn.secondary.on { border-color: var(--green); color: var(--green); }
.cam-note { font-size: .78rem; color: var(--ink-60); text-align: center; margin: 0; }
.cam-info { font-family: var(--mono); font-size: .7rem; color: var(--ink-60); text-align: center; word-break: break-word; }
