:root {
  --bg: #f4f5f7;
  --surface: #ffffff;
  --surface-2: #eef0f3;
  --text: #16191d;
  --muted: #5d6673;
  --border: #dde1e6;
  --primary: #1f3a5f;
  --primary-text: #ffffff;
  --accent: #b8860b;
  --ok: #1e7a46;
  --ok-bg: #e3f4ea;
  --warn: #8a5a00;
  --warn-bg: #fdf1d8;
  --danger: #b42318;
  --danger-bg: #fde7e5;
  --info: #1f5fa8;
  --info-bg: #e3eefb;
  --radius: 12px;
  --shadow: 0 1px 2px rgba(0,0,0,.06), 0 2px 8px rgba(0,0,0,.05);
  --tabbar-h: 62px;
  --topbar-bg: #1f3a5f;
  --topbar-text: #ffffff;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f1216;
    --surface: #181c22;
    --surface-2: #222830;
    --text: #e8ebef;
    --muted: #9aa4b1;
    --border: #2d343e;
    --primary: #5b8fd6;
    --primary-text: #0b1220;
    --accent: #e0b04a;
    --ok: #5fd08f;
    --ok-bg: #143523;
    --warn: #f0c060;
    --warn-bg: #3a2c0c;
    --danger: #ff8a7f;
    --danger-bg: #3d1513;
    --info: #8cbcf5;
    --info-bg: #142a45;
    --shadow: none;
    --topbar-bg: #16263d;
    --topbar-text: #e8ebef;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; }
body {
  background: var(--bg);
  color: var(--text);
  font: 16px/1.4 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  padding-bottom: calc(var(--tabbar-h) + env(safe-area-inset-bottom));
  overscroll-behavior-y: contain;
}
button, input, select, textarea { font: inherit; color: inherit; }
svg { width: 24px; height: 24px; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; }

.topbar {
  position: sticky; top: 0; z-index: 10;
  display: flex; align-items: center; gap: 8px;
  padding: calc(10px + env(safe-area-inset-top)) 16px 10px;
  background: var(--topbar-bg); color: var(--topbar-text);
}
.topbar h1 { font-size: 19px; margin: 0; flex: 1; font-weight: 650; }

main { padding: 12px 16px 24px; max-width: 760px; margin: 0 auto; }

.tabbar {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 20;
  display: grid; grid-template-columns: repeat(4, 1fr);
  height: calc(var(--tabbar-h) + env(safe-area-inset-bottom));
  padding-bottom: env(safe-area-inset-bottom);
  background: var(--surface); border-top: 1px solid var(--border);
}
.tabbar button {
  border: 0; background: none; color: var(--muted);
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 2px;
  font-size: 12px; cursor: pointer;
}
.tabbar button.active { color: var(--primary); font-weight: 600; }

.card {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  box-shadow: var(--shadow); padding: 14px; margin-bottom: 12px;
}
.card h2 { font-size: 15px; margin: 0 0 10px; color: var(--muted); font-weight: 600; text-transform: uppercase; letter-spacing: .04em; }
.section-title { font-size: 13px; color: var(--muted); text-transform: uppercase; letter-spacing: .05em; font-weight: 600; margin: 18px 4px 8px; }

.stats { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; margin-bottom: 12px; }
.stat { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 10px; text-align: center; }
.stat b { display: block; font-size: 22px; }
.stat span { font-size: 12px; color: var(--muted); }

.row { display: flex; align-items: center; gap: 8px; }
.row.wrap { flex-wrap: wrap; }
.grow { flex: 1; min-width: 0; }
.muted { color: var(--muted); }
.small { font-size: 13px; }
.right { text-align: right; }
.mono { font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; font-size: 13px; }
.ellipsis { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.stack > * + * { margin-top: 10px; }

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  min-height: 44px; padding: 0 16px; border-radius: 10px; border: 1px solid var(--border);
  background: var(--surface); color: var(--text); font-weight: 600; cursor: pointer;
}
.btn svg { width: 20px; height: 20px; }
.btn-primary { background: var(--primary); color: var(--primary-text); border-color: var(--primary); }
.btn-danger { color: var(--danger); border-color: var(--danger); background: transparent; }
.btn-block { width: 100%; }
.btn-lg { min-height: 54px; font-size: 17px; }
.btn-sm { min-height: 34px; padding: 0 10px; font-size: 14px; }
.btn-ghost-light { background: rgba(255,255,255,.15); color: #fff; border-color: rgba(255,255,255,.4); }
.btn:disabled { opacity: .5; }
.btn-row { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }

.seg { display: grid; grid-template-columns: 1fr 1fr; background: var(--surface-2); border-radius: 10px; padding: 3px; gap: 3px; }
.seg button { border: 0; background: none; min-height: 40px; border-radius: 8px; font-weight: 600; color: var(--muted); cursor: pointer; }
.seg button.on { background: var(--surface); color: var(--text); box-shadow: var(--shadow); }
.seg button.on.sale { color: var(--ok); }
.seg button.on.memo { color: var(--info); }

label.field { display: block; }
label.field > span { display: block; font-size: 13px; color: var(--muted); margin: 0 0 4px 2px; }
input[type=text], input[type=search], input[type=tel], input[type=email], input[type=number], input[type=date], select, textarea {
  width: 100%; min-height: 44px; padding: 10px 12px; border-radius: 10px;
  border: 1px solid var(--border); background: var(--surface); outline: none;
}
input:focus, select:focus, textarea:focus { border-color: var(--primary); box-shadow: 0 0 0 3px color-mix(in srgb, var(--primary) 25%, transparent); }
textarea { min-height: 70px; resize: vertical; }
.grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.check { display: flex; gap: 10px; align-items: flex-start; padding: 6px 0; }
.check input { width: 20px; height: 20px; margin-top: 2px; flex: none; }

.chips { display: flex; gap: 6px; overflow-x: auto; padding: 2px 0 10px; scrollbar-width: none; }
.chip { flex: none; border: 1px solid var(--border); background: var(--surface); border-radius: 999px; padding: 6px 12px; font-size: 14px; cursor: pointer; }
.chip.on { background: var(--primary); color: var(--primary-text); border-color: var(--primary); }

.pill { display: inline-block; font-size: 12px; font-weight: 600; padding: 2px 8px; border-radius: 999px; background: var(--surface-2); color: var(--muted); white-space: nowrap; }
.pill-ok { background: var(--ok-bg); color: var(--ok); }
.pill-info { background: var(--info-bg); color: var(--info); }
.pill-warn { background: var(--warn-bg); color: var(--warn); }
.pill-danger { background: var(--danger-bg); color: var(--danger); }

.list { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); overflow: hidden; box-shadow: var(--shadow); }
.li { display: flex; gap: 10px; padding: 12px 14px; border-top: 1px solid var(--border); align-items: center; }
.li:first-child { border-top: 0; }
.li.tap { cursor: pointer; }
.li.tap:active { background: var(--surface-2); }
.li .title { font-weight: 600; }
.li .sub { font-size: 13px; color: var(--muted); }
.li .amt { font-weight: 650; text-align: right; white-space: nowrap; }

.line { padding: 12px 14px; border-top: 1px solid var(--border); }
.line:first-child { border-top: 0; }
.line .price-row { display: grid; grid-template-columns: 1fr 1fr 40px; gap: 8px; align-items: end; margin-top: 8px; }
.line .price-row input { min-height: 40px; padding: 8px 10px; text-align: right; }
.icon-btn { width: 40px; height: 40px; border: 0; background: none; color: var(--muted); border-radius: 8px; display: grid; place-items: center; cursor: pointer; }
.icon-btn:active { background: var(--surface-2); }

.totals { display: grid; grid-template-columns: 1fr auto; gap: 4px 12px; font-size: 15px; }
.totals .grand { font-size: 20px; font-weight: 700; }

.empty { text-align: center; padding: 32px 16px; color: var(--muted); }
.empty svg { width: 48px; height: 48px; stroke-width: 1.3; margin-bottom: 8px; }

.picker { display: flex; align-items: center; gap: 10px; width: 100%; min-height: 56px; padding: 8px 12px; text-align: left;
  border: 1px solid var(--border); border-radius: 10px; background: var(--surface); cursor: pointer; }
.picker svg { flex: none; color: var(--muted); }
.picker .grow { min-width: 0; }
.scan-cta { display: grid; grid-template-columns: 1fr auto; gap: 8px; }
.manual { display: grid; grid-template-columns: 1fr auto; gap: 8px; }

/* Signature */
.sig-wrap { position: relative; border: 1px dashed var(--border); border-radius: 10px; background: #fff; touch-action: none; }
.sig-wrap canvas { display: block; width: 100%; height: 150px; }
.sig-wrap .sig-hint { position: absolute; left: 12px; bottom: 8px; font-size: 12px; color: #8a929c; pointer-events: none; }
.sig-img { max-width: 260px; max-height: 110px; background: #fff; border-radius: 8px; border: 1px solid var(--border); }

/* Scanner overlay */
.scanner { position: fixed; inset: 0; z-index: 50; background: #000; color: #fff; }
.scanner video { position: absolute; inset: 0; width: 100%; height: 100%; object-fit: cover; }
.scan-frame { position: absolute; left: 50%; top: 45%; width: min(70vw, 300px); aspect-ratio: 1; transform: translate(-50%, -50%);
  border-radius: 18px; box-shadow: 0 0 0 9999px rgba(0,0,0,.45); border: 3px solid rgba(255,255,255,.9); }
.scan-frame.hit { border-color: #4ade80; }
.scan-frame.miss { border-color: #f87171; }
.scan-top { position: absolute; top: 0; left: 0; right: 0; padding: calc(16px + env(safe-area-inset-top)) 16px 16px; text-align: center; font-weight: 600; text-shadow: 0 1px 3px rgba(0,0,0,.7); }
.scan-bottom { position: absolute; bottom: 0; left: 0; right: 0; padding: 16px 16px calc(20px + env(safe-area-inset-bottom)); display: flex; gap: 10px; align-items: center; justify-content: flex-end; }
.scan-bottom .pill { margin-right: auto; background: rgba(255,255,255,.2); color: #fff; font-size: 14px; padding: 6px 12px; }

/* Bottom sheet */
.backdrop { position: fixed; inset: 0; background: rgba(0,0,0,.4); z-index: 30; }
.sheet { position: fixed; left: 0; right: 0; bottom: 0; z-index: 31; max-height: 88vh; overflow-y: auto;
  background: var(--bg); border-radius: 18px 18px 0 0; padding: 8px 16px calc(20px + env(safe-area-inset-bottom));
  max-width: 760px; margin: 0 auto; }
.sheet::before { content: ""; display: block; width: 40px; height: 5px; border-radius: 3px; background: var(--border); margin: 0 auto 12px; }
.sheet h3 { margin: 0 0 12px; font-size: 18px; }
.kv { display: grid; grid-template-columns: auto 1fr; gap: 6px 14px; font-size: 15px; }
.kv dt { color: var(--muted); }
.kv dd { margin: 0; text-align: right; word-break: break-word; }

.toast { position: fixed; left: 50%; bottom: calc(var(--tabbar-h) + 16px + env(safe-area-inset-bottom)); transform: translateX(-50%);
  z-index: 60; background: #222; color: #fff; padding: 10px 16px; border-radius: 10px; max-width: calc(100vw - 32px); font-size: 15px; box-shadow: 0 4px 16px rgba(0,0,0,.3); }
.toast.err { background: var(--danger); color: #fff; }

/* Print */
.print-only { display: none; }
@media print {
  body { background: #fff; color: #000; padding: 0; }
  body > *:not(#print) { display: none !important; }
  #print { display: block; font-size: 12px; color: #000; }
  #print h1 { font-size: 20px; margin: 0; }
  #print table { width: 100%; border-collapse: collapse; margin-top: 12px; }
  #print th, #print td { border-bottom: 1px solid #ccc; padding: 5px 4px; text-align: left; vertical-align: top; }
  #print th.num, #print td.num { text-align: right; }
  #print .hdr { display: flex; justify-content: space-between; gap: 20px; }
  #print .terms { margin-top: 16px; font-size: 11px; white-space: pre-wrap; }
  #print .sig { margin-top: 20px; }
  #print .sig img { max-height: 80px; }
}

/* ---------- v2: sign-in, sync, office ---------- */
.sync-pill { border: 0; border-radius: 999px; padding: 4px 10px; font-size: 12px; font-weight: 600; cursor: pointer;
  background: rgba(255,255,255,.18); color: var(--topbar-text); }
.sync-pill.ok::before { content: "● "; color: #4ade80; }
.sync-pill.warn { background: #f5c04a; color: #2b1d00; }
.sync-pill.err { background: #ff8a7f; color: #2b0400; }

.banner { border-radius: 10px; padding: 10px 12px; font-size: 14px; background: var(--surface-2); border: 1px solid var(--border); }
.banner.info { background: var(--info-bg); color: var(--info); border-color: transparent; }
.banner.info b { color: inherit; }
.banner.warn { background: var(--warn-bg); color: var(--warn); border-color: transparent; }

.card.incoming { border: 2px solid var(--accent); }
.card.incoming svg { color: var(--accent); flex: none; }
.tap { cursor: pointer; }

.tickmark { flex: none; width: 26px; height: 26px; border-radius: 50%; border: 2px solid var(--border); display: grid; place-items: center; color: transparent; }
.tickmark svg { width: 16px; height: 16px; stroke-width: 3; }
.tickmark.on { background: var(--ok); border-color: var(--ok); color: #fff; }
input.tick { width: 22px; height: 22px; flex: none; }

.login { max-width: 380px; margin: 8vh auto 0; text-align: center; }
.login h2 { margin: 12px 0 18px; }
.login form { text-align: left; }
.login img { border-radius: 16px; }

.li .stack > * + * { margin-top: 6px; }

[hidden] { display: none !important; }
