:root {
  --bg: #f7f3ea;
  --surface: #ffffff;
  --sunk: #efe9dc;
  --fg: #2b2618;
  --muted: #6b6455;
  --line: #e3dccd;
  --accent: #655613;
  --accent-press: #4f430e;
  --on-accent: #ffffff;
  --soft: #ece5cf;
  --ok: #2f6f45;
  --ok-bg: #e3f1e7;
  --warn: #9a5a07;
  --warn-bg: #fbeedb;
  --bad: #a3271f;
  --bad-bg: #fbe7e5;
  --cam: #11100c;
  --serif: "Iowan Old Style", "Palatino Linotype", Palatino, Georgia, serif;
  --sans: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, system-ui, sans-serif;
  --nav-h: 64px;
  color-scheme: light;
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { height: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--fg);
  font: 16px/1.4 var(--sans);
  -webkit-text-size-adjust: 100%;
  overscroll-behavior-y: none;
}
button, input, textarea, select { font: inherit; color: inherit; }
button { cursor: pointer; border: 0; background: none; padding: 0; }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
.num { font-variant-numeric: tabular-nums; white-space: nowrap; }

#app { min-height: 100%; display: flex; flex-direction: column; }

.top {
  position: sticky;
  top: 0;
  z-index: 5;
  background: var(--bg);
  padding: calc(env(safe-area-inset-top, 0px) + 12px) max(16px, calc((100% - 640px) / 2)) 10px;
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 12px;
}
.top h1 { font: 600 22px/1.2 var(--serif); margin: 0; }
.top .sub { color: var(--muted); font-size: 14px; }

main {
  flex: 1;
  padding: 4px 16px calc(var(--nav-h) + env(safe-area-inset-bottom, 0px) + 140px);
  display: flex;
  flex-direction: column;
  gap: 12px;
  max-width: 640px;
  width: 100%;
  margin: 0 auto;
}
main.plain { padding-bottom: calc(var(--nav-h) + env(safe-area-inset-bottom, 0px) + 24px); }

.nav {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 10;
  background: var(--surface);
  border-top: 1px solid var(--line);
  padding-bottom: env(safe-area-inset-bottom, 0px);
  display: grid;
  grid-template-columns: repeat(3, 1fr);
}
.nav a {
  height: var(--nav-h);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 3px;
  font-size: 12px;
  font-weight: 600;
  color: var(--muted);
  text-decoration: none;
}
.nav a svg { width: 24px; height: 24px; }
.nav a[aria-current="page"] { color: var(--accent); }

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  min-height: 48px;
  padding: 0 18px;
  border-radius: 12px;
  background: var(--accent);
  color: var(--on-accent);
  font-weight: 600;
  text-align: center;
}
.btn:active { background: var(--accent-press); }
.btn[disabled] { opacity: .5; }
.btn.big { min-height: 64px; font-size: 18px; border-radius: 16px; }
.btn.ghost { background: var(--surface); color: var(--accent); border: 1px solid var(--line); }
.btn.ghost:active { background: var(--sunk); }
.btn.danger { background: var(--bad-bg); color: var(--bad); }
.btn.block { width: 100%; }
.btn svg { width: 22px; height: 22px; }
.link { color: var(--accent); font-weight: 600; }

.field {
  width: 100%;
  min-height: 48px;
  padding: 10px 14px;
  border-radius: 12px;
  border: 1px solid var(--line);
  background: var(--surface);
  font-size: 16px;
}
textarea.field { min-height: 96px; resize: vertical; }
label.lbl { display: flex; flex-direction: column; gap: 6px; font-size: 13px; font-weight: 600; color: var(--muted); }
.row { display: flex; align-items: center; justify-content: space-between; gap: 10px; }
.stack { display: flex; flex-direction: column; gap: 10px; }
.grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.muted { color: var(--muted); }
.small { font-size: 13px; }
.empty { text-align: center; color: var(--muted); padding: 28px 12px; }

.search-wrap { position: relative; }
.results {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 12px;
  overflow: hidden;
}
.result {
  width: 100%;
  text-align: left;
  display: flex;
  justify-content: space-between;
  gap: 12px;
  padding: 12px 14px;
  border-bottom: 1px solid var(--line);
}
.result:last-child { border-bottom: 0; }
.result:active { background: var(--sunk); }
.result .nm { font-weight: 600; }
.result .meta { font-size: 12px; color: var(--muted); }

.line {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 14px;
  padding: 12px 14px;
  display: flex;
  flex-direction: column;
  gap: 8px;
}
.line .nm { font-weight: 600; min-width: 0; overflow-wrap: anywhere; }
.line .coll { font-size: 12px; color: var(--muted); }
.qty { display: inline-flex; align-items: center; border: 1px solid var(--line); border-radius: 10px; overflow: hidden; }
.qty button { width: 40px; height: 36px; font-size: 20px; color: var(--accent); }
.qty button:active { background: var(--sunk); }
.qty span { min-width: 30px; text-align: center; font-weight: 600; }
.price-btn { text-align: right; padding: 6px 0 6px 10px; }
.price-btn s { color: var(--muted); font-size: 13px; margin-right: 6px; }
.price-btn .edited { color: var(--accent); font-weight: 600; }
.price-btn.missing { color: var(--warn); font-weight: 600; }

.dock {
  position: fixed;
  left: 0;
  right: 0;
  bottom: calc(var(--nav-h) + env(safe-area-inset-bottom, 0px));
  z-index: 9;
  background: var(--surface);
  border-top: 1px solid var(--line);
  padding: 10px 16px 12px;
}
.dock-in { max-width: 640px; margin: 0 auto; display: flex; flex-direction: column; gap: 8px; }
.tot { display: flex; flex-direction: column; gap: 2px; font-size: 14px; }
.tot .grand { font-size: 20px; font-weight: 700; }

.tag { display: inline-block; font-size: 11px; font-weight: 600; padding: 2px 8px; border-radius: 99px; background: var(--soft); color: var(--fg); }
.tag.warn { background: var(--warn-bg); color: var(--warn); }
.tag.bad { background: var(--bad-bg); color: var(--bad); }
.tag.ok { background: var(--ok-bg); color: var(--ok); }

.card { background: var(--surface); border: 1px solid var(--line); border-radius: 14px; padding: 14px; }
.big-num { font: 600 30px/1.1 var(--serif); }
.kpis { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.kpis .card { display: flex; flex-direction: column; gap: 2px; }
.sale { width: 100%; text-align: left; display: flex; flex-direction: column; gap: 4px; }
.sale.voided { opacity: .55; }
.sale.voided .num { text-decoration: line-through; }

.seg { display: grid; grid-template-columns: 1fr 1fr; background: var(--sunk); border-radius: 12px; padding: 4px; gap: 4px; }
.seg button { min-height: 42px; border-radius: 9px; font-weight: 600; color: var(--muted); }
.seg button[aria-pressed="true"] { background: var(--surface); color: var(--fg); box-shadow: 0 1px 3px rgba(43, 38, 24, .15); }

#sheet-root .scrim {
  position: fixed;
  inset: 0;
  z-index: 30;
  background: rgba(30, 26, 15, .4);
  display: flex;
  align-items: flex-end;
  justify-content: center;
}
.sheet {
  width: 100%;
  max-width: 640px;
  max-height: 92vh;
  overflow-y: auto;
  background: var(--bg);
  border-radius: 20px 20px 0 0;
  padding: 18px 16px calc(env(safe-area-inset-bottom, 0px) + 18px);
  display: flex;
  flex-direction: column;
  gap: 12px;
  animation: up .18s ease-out;
}
.sheet h2 { font: 600 20px/1.2 var(--serif); margin: 0; }
@keyframes up { from { transform: translateY(24px); opacity: .6; } to { transform: none; opacity: 1; } }

.scanner {
  position: fixed;
  inset: 0;
  z-index: 40;
  background: var(--cam);
  color: #fff;
  display: flex;
  flex-direction: column;
}
.scanner video { position: absolute; inset: 0; width: 100%; height: 100%; object-fit: cover; }
.scanner .frame {
  position: absolute;
  left: 10%;
  right: 10%;
  top: 30%;
  height: 22%;
  border: 2px solid rgba(255, 255, 255, .9);
  border-radius: 16px;
  box-shadow: 0 0 0 100vmax rgba(0, 0, 0, .35);
}
.scanner .frame::after { content: ""; position: absolute; left: 8%; right: 8%; top: 50%; height: 2px; background: #e5484d; }
.scanner .bar {
  position: relative;
  z-index: 2;
  padding: calc(env(safe-area-inset-top, 0px) + 14px) 16px 12px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-weight: 600;
}
.scanner .bar button { color: #fff; font-weight: 600; min-height: 40px; padding: 0 6px; }
.scanner .bottom {
  position: absolute;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 2;
  padding: 16px 16px calc(env(safe-area-inset-bottom, 0px) + 18px);
  display: flex;
  flex-direction: column;
  gap: 10px;
}
.scanner .hit { background: var(--ok-bg); color: var(--ok); border-radius: 12px; padding: 12px 14px; font-weight: 600; }
.scanner .miss { background: var(--warn-bg); color: var(--warn); border-radius: 12px; padding: 12px 14px; font-weight: 600; display: flex; flex-direction: column; gap: 8px; }
.scanner .btn.ghost { background: rgba(255, 255, 255, .12); color: #fff; border-color: rgba(255, 255, 255, .5); }
.scanner .miss .btn.ghost { background: var(--surface); color: var(--accent); border-color: var(--line); }
.scanner .count { font-size: 14px; opacity: .85; }

#toast {
  position: fixed;
  left: 50%;
  top: calc(env(safe-area-inset-top, 0px) + 12px);
  transform: translateX(-50%);
  z-index: 50;
  background: var(--fg);
  color: #fff;
  padding: 10px 16px;
  border-radius: 12px;
  font-weight: 600;
  font-size: 14px;
  max-width: calc(100% - 32px);
  opacity: 0;
  pointer-events: none;
  transition: opacity .2s;
}
#toast.show { opacity: 1; }
#toast.bad { background: var(--bad); }

.login { flex: 1; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 22px; padding: 24px 16px; }
.login img { width: 92px; height: auto; border-radius: 20px; }
.login h1 { font: 600 24px/1.2 var(--serif); margin: 0; }
.dots { display: flex; gap: 14px; }
.dots i { width: 14px; height: 14px; border-radius: 50%; border: 2px solid var(--accent); }
.dots i.on { background: var(--accent); }
.pad { display: grid; grid-template-columns: repeat(3, 76px); gap: 12px; }
.pad button { height: 64px; border-radius: 16px; background: var(--surface); border: 1px solid var(--line); font-size: 24px; font-weight: 600; }
.pad button:active { background: var(--sunk); }
.err { color: var(--bad); font-weight: 600; min-height: 22px; text-align: center; }

.map-row { display: grid; grid-template-columns: 1fr 1.4fr; gap: 10px; align-items: center; }
table.preview { width: 100%; border-collapse: collapse; font-size: 13px; }
table.preview td, table.preview th { padding: 6px 8px; border-bottom: 1px solid var(--line); text-align: left; vertical-align: top; }
table.preview th { color: var(--muted); font-weight: 600; font-size: 12px; }
.scroll-x { overflow-x: auto; }
.sheet-head { cursor: pointer; }
.sheet-head input { width: 20px; height: 20px; accent-color: var(--accent); vertical-align: -4px; margin: 0 6px 0 0; }
details > summary { cursor: pointer; padding: 4px 0; }
details[open] > summary { margin-bottom: 8px; }

@media (prefers-reduced-motion: reduce) { .sheet { animation: none; } #toast { transition: none; } }
