/* Receipt Scanner – themes: ink (default), paper, graphite */
:root, [data-theme="ink"] {
  --bg: #0e1622; --bg2: #121c2b; --surface: #182435; --surface2: #1f2e43; --line: #2a3b54;
  --text: #e9eef5; --muted: #93a3b8; --accent: #f07d00; --accent-2: #ff9a2e; --accent-ink: #1a1006;
  --ok: #3fbf7f; --warn: #f2b84b; --err: #ef5b5b; --info: #5aa7ff;
  --shadow: 0 8px 24px rgba(0,0,0,.35);
  color-scheme: dark;
}
[data-theme="paper"] {
  --bg: #f4efe6; --bg2: #efe8dc; --surface: #fbf8f2; --surface2: #f1ebe0; --line: #ddd3c3;
  --text: #1f2633; --muted: #6b7280; --accent: #e06f00; --accent-2: #f08a1c; --accent-ink: #fff;
  --ok: #1f9d5c; --warn: #b7791f; --err: #c53030; --info: #2b6cb0;
  --shadow: 0 6px 18px rgba(60,40,10,.12);
  color-scheme: light;
}
[data-theme="graphite"] {
  --bg: #161718; --bg2: #1b1c1e; --surface: #232427; --surface2: #2c2d31; --line: #3a3b40;
  --text: #ececec; --muted: #9b9ca3; --accent: #f07d00; --accent-2: #ff9a2e; --accent-ink: #1a1006;
  --ok: #4cc38a; --warn: #f2b84b; --err: #f06363; --info: #6cb2ff;
  --shadow: 0 8px 24px rgba(0,0,0,.45);
  color-scheme: dark;
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; height: 100%; }
body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  background: var(--bg); color: var(--text); font-size: 16px; line-height: 1.4;
  overscroll-behavior-y: none;
}
button, input, select, textarea { font: inherit; color: inherit; }
a { color: var(--accent); }
.hidden { display: none !important; }

/* ---------- layout ---------- */
.app { min-height: 100%; display: flex; flex-direction: column; }
.topbar {
  position: sticky; top: 0; z-index: 20;
  padding: calc(env(safe-area-inset-top) + 10px) 16px 10px;
  background: color-mix(in srgb, var(--bg) 88%, transparent); backdrop-filter: blur(12px);
  border-bottom: 1px solid var(--line); display: flex; align-items: center; gap: 10px;
}
.brand { display: flex; align-items: center; gap: 10px; font-weight: 700; letter-spacing: .2px; }
.brand img { width: 30px; height: 30px; }
.brand small { display: block; font-weight: 500; color: var(--muted); font-size: 12px; }
.topbar .spacer { flex: 1; }
.sync-pill { font-size: 12px; padding: 4px 10px; border-radius: 999px; background: var(--surface2); color: var(--muted); }
.sync-pill.busy { color: var(--accent); }

main { flex: 1; padding: 16px 16px calc(env(safe-area-inset-bottom) + 88px); max-width: 860px; width: 100%; margin: 0 auto; }

.tabbar {
  position: fixed; bottom: 0; left: 0; right: 0; z-index: 30;
  display: flex; justify-content: space-around;
  padding: 6px 8px calc(env(safe-area-inset-bottom) + 6px);
  background: color-mix(in srgb, var(--bg2) 94%, transparent); backdrop-filter: blur(14px);
  border-top: 1px solid var(--line);
}
.tabbar a {
  flex: 1; display: flex; flex-direction: column; align-items: center; gap: 2px; padding: 6px 4px;
  color: var(--muted); text-decoration: none; font-size: 11px; font-weight: 600; border-radius: 12px;
}
.tabbar a svg { width: 24px; height: 24px; }
.tabbar a.active { color: var(--accent); }
.tabbar a .badge { position: absolute; transform: translate(14px, -4px); }

@media (min-width: 900px) {
  .app { flex-direction: row; }
  .tabbar {
    position: sticky; top: 0; height: 100vh; width: 220px; flex-direction: column; justify-content: flex-start;
    gap: 4px; padding: 90px 12px 12px; border-top: 0; border-right: 1px solid var(--line);
  }
  .tabbar a { flex: 0; flex-direction: row; gap: 12px; font-size: 14px; padding: 10px 14px; }
  .tabbar a.active { background: var(--surface2); }
  .content { flex: 1; display: flex; flex-direction: column; min-width: 0; }
  main { padding-bottom: 40px; }
}

/* ---------- components ---------- */
h1 { font-size: 24px; margin: 4px 0 14px; }
h2 { font-size: 17px; margin: 22px 0 10px; color: var(--muted); text-transform: uppercase; letter-spacing: .6px; font-weight: 700; }
.card {
  background: var(--surface); border: 1px solid var(--line); border-radius: 16px; padding: 16px; margin-bottom: 12px;
}
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  min-height: 46px; padding: 0 18px; border-radius: 12px; border: 1px solid var(--line);
  background: var(--surface2); font-weight: 600; cursor: pointer; text-decoration: none; color: var(--text);
}
.btn:active { transform: scale(.98); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
.btn.danger { color: var(--err); }
.btn.ghost { background: transparent; }
.btn.small { min-height: 36px; padding: 0 12px; font-size: 14px; border-radius: 10px; }
.btn.block { width: 100%; }
.btn[disabled] { opacity: .5; pointer-events: none; }
.btn svg { width: 20px; height: 20px; }
.row { display: flex; gap: 10px; align-items: center; flex-wrap: wrap; }
.row > * { flex: 1; }
.stack > * + * { margin-top: 10px; }

label.field { display: block; margin-bottom: 12px; }
label.field > span { display: block; font-size: 13px; color: var(--muted); margin-bottom: 5px; font-weight: 600; }
input[type=text], input[type=email], input[type=password], input[type=number], input[type=date], input[type=time],
select, textarea {
  width: 100%; min-height: 46px; padding: 10px 12px; border-radius: 12px;
  border: 1px solid var(--line); background: var(--bg2); outline: none; font-size: 16px; /* 16px avoids iOS zoom */
}
textarea { min-height: 140px; resize: vertical; }
input:focus, select:focus, textarea:focus { border-color: var(--accent); box-shadow: 0 0 0 3px color-mix(in srgb, var(--accent) 25%, transparent); }
.hint { font-size: 13px; color: var(--muted); }
.chips { display: flex; gap: 6px; flex-wrap: wrap; margin: 6px 0 12px; }
.chip { font-size: 12px; padding: 4px 9px; border-radius: 999px; background: var(--surface2); border: 1px solid var(--line); cursor: pointer; }

.switch { display: flex; align-items: center; justify-content: space-between; gap: 12px; padding: 10px 0; }
.switch input { appearance: none; width: 50px; height: 30px; border-radius: 30px; background: var(--line); position: relative; flex: none; cursor: pointer; transition: .2s; }
.switch input::after { content: ""; position: absolute; top: 3px; left: 3px; width: 24px; height: 24px; border-radius: 50%; background: #fff; transition: .2s; }
.switch input:checked { background: var(--accent); }
.switch input:checked::after { left: 23px; }

.seg { display: flex; background: var(--bg2); border: 1px solid var(--line); border-radius: 12px; padding: 3px; gap: 3px; }
.seg button { flex: 1; border: 0; background: transparent; padding: 9px 6px; border-radius: 9px; font-weight: 600; color: var(--muted); cursor: pointer; }
.seg button.active { background: var(--surface2); color: var(--text); box-shadow: var(--shadow); }

.badge { display: inline-block; min-width: 18px; padding: 1px 6px; border-radius: 999px; background: var(--accent); color: var(--accent-ink); font-size: 11px; font-weight: 700; text-align: center; }
.status { font-size: 12px; font-weight: 700; padding: 3px 8px; border-radius: 999px; white-space: nowrap; }
.status.processing { background: color-mix(in srgb, var(--info) 18%, transparent); color: var(--info); }
.status.ready { background: color-mix(in srgb, var(--warn) 18%, transparent); color: var(--warn); }
.status.sent { background: color-mix(in srgb, var(--ok) 18%, transparent); color: var(--ok); }
.status.error, .status.empty { background: color-mix(in srgb, var(--err) 18%, transparent); color: var(--err); }
.status.queued { background: var(--surface2); color: var(--muted); }

/* ---------- scan home ---------- */
.scan-hero { text-align: center; padding: 26px 16px 18px; }
.scan-btn {
  width: 168px; height: 168px; border-radius: 50%; margin: 10px auto 18px; display: grid; place-items: center;
  background: radial-gradient(circle at 30% 25%, var(--accent-2), var(--accent)); color: var(--accent-ink);
  box-shadow: 0 12px 40px color-mix(in srgb, var(--accent) 45%, transparent); cursor: pointer; border: 0;
}
.scan-btn svg { width: 72px; height: 72px; }
.stats { display: grid; grid-template-columns: repeat(3, 1fr); gap: 10px; }
.stat { background: var(--surface); border: 1px solid var(--line); border-radius: 14px; padding: 12px; }
.stat b { display: block; font-size: 20px; }
.stat span { font-size: 12px; color: var(--muted); }

/* ---------- editor (full screen) ---------- */
.editor {
  position: fixed; inset: 0; z-index: 100; background: #05080d; display: flex; flex-direction: column; color: #fff;
}
.editor header, .editor footer { padding: calc(env(safe-area-inset-top) + 8px) 14px 8px; display: flex; gap: 8px; align-items: center; }
.editor footer { padding: 10px 14px calc(env(safe-area-inset-bottom) + 12px); flex-wrap: wrap; }
.editor header .title { flex: 1; font-weight: 700; text-align: center; }
.editor .stage { flex: 1; position: relative; overflow: hidden; touch-action: none; }
.editor canvas { position: absolute; left: 0; top: 0; }
.editor .btn { background: rgba(255,255,255,.1); border-color: rgba(255,255,255,.15); color: #fff; }
.editor .btn.primary { background: var(--accent); border-color: var(--accent); color: #1a1006; }
.editor .seg { background: rgba(255,255,255,.08); border-color: rgba(255,255,255,.12); width: 100%; }
.editor .seg button { color: rgba(255,255,255,.7); }
.editor .seg button.active { background: rgba(255,255,255,.18); color: #fff; }
.editor .preview-wrap { flex: 1; overflow: auto; display: flex; justify-content: center; align-items: flex-start; padding: 12px; }
.editor .preview-wrap img { max-width: 100%; box-shadow: 0 10px 40px rgba(0,0,0,.6); border-radius: 4px; }
.editor .meta { width: 100%; display: grid; grid-template-columns: 1fr 1.4fr; gap: 8px; }
.editor .meta select, .editor .meta input { background: rgba(255,255,255,.08); border-color: rgba(255,255,255,.15); color: #fff; min-height: 42px; }
.editor .pages-info { font-size: 13px; opacity: .75; width: 100%; text-align: center; }
.editor .busy { position: absolute; inset: 0; display: grid; place-items: center; background: rgba(0,0,0,.55); font-weight: 600; }

/* ---------- receipts ---------- */
.summary-bar { display: flex; align-items: center; gap: 12px; }
.summary-bar .total { flex: 1; }
.summary-bar .total b { font-size: 22px; display: block; }
.list { display: flex; flex-direction: column; gap: 10px; }
.item {
  display: flex; gap: 12px; align-items: center; background: var(--surface); border: 1px solid var(--line);
  border-radius: 14px; padding: 10px; cursor: pointer;
}
.item img, .item .ph { width: 54px; height: 72px; object-fit: cover; object-position: top; border-radius: 8px; background: var(--surface2); flex: none; }
.item .info { flex: 1; min-width: 0; }
.item .info .m { font-weight: 700; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.item .info .d { font-size: 13px; color: var(--muted); }
.item .amt { text-align: right; }
.item .amt b { display: block; font-size: 17px; }
.empty-state { text-align: center; color: var(--muted); padding: 40px 10px; }

.sheet-backdrop { position: fixed; inset: 0; background: rgba(0,0,0,.5); z-index: 60; }
.sheet {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 61; max-height: 92vh; overflow: auto;
  background: var(--bg); border-radius: 20px 20px 0 0; padding: 10px 16px calc(env(safe-area-inset-bottom) + 18px);
  box-shadow: var(--shadow); max-width: 720px; margin: 0 auto;
}
.sheet .grab { width: 40px; height: 5px; border-radius: 5px; background: var(--line); margin: 4px auto 12px; }
.sheet .preview { width: 100%; max-height: 260px; object-fit: contain; background: var(--surface2); border-radius: 12px; margin-bottom: 12px; }
.grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
details summary { cursor: pointer; color: var(--muted); font-size: 14px; margin: 8px 0; }
pre.ocr { white-space: pre-wrap; font-size: 12px; background: var(--bg2); padding: 10px; border-radius: 10px; max-height: 240px; overflow: auto; }

/* ---------- tables (admin / history) ---------- */
.table { width: 100%; border-collapse: collapse; font-size: 14px; }
.table th, .table td { padding: 9px 6px; border-bottom: 1px solid var(--line); text-align: left; vertical-align: middle; }
.table th { color: var(--muted); font-size: 12px; text-transform: uppercase; letter-spacing: .4px; }

/* ---------- login ---------- */
.login { min-height: 100vh; display: grid; place-items: center; padding: 24px; }
.login .card { width: 100%; max-width: 400px; padding: 26px; }
.login .brand { justify-content: center; margin-bottom: 18px; font-size: 20px; }
.login .brand img { width: 44px; height: 44px; }
.divider { display: flex; align-items: center; gap: 10px; color: var(--muted); font-size: 13px; margin: 16px 0; }
.divider::before, .divider::after { content: ""; flex: 1; height: 1px; background: var(--line); }
.qr { display: block; margin: 10px auto; border-radius: 10px; background: #fff; padding: 6px; }
code.secret { display: block; text-align: center; font-size: 14px; word-break: break-all; padding: 8px; background: var(--bg2); border-radius: 8px; }

/* ---------- toast ---------- */
.toast {
  position: fixed; left: 50%; transform: translateX(-50%); bottom: calc(env(safe-area-inset-bottom) + 92px); z-index: 200;
  background: var(--surface2); border: 1px solid var(--line); padding: 12px 16px; border-radius: 12px; box-shadow: var(--shadow);
  font-weight: 600; max-width: 90vw; animation: toastIn .2s ease-out;
}
.toast.err { border-color: var(--err); color: var(--err); }
.toast.ok { border-color: var(--ok); }
@keyframes toastIn { from { opacity: 0; transform: translate(-50%, 10px); } }
.spin { width: 18px; height: 18px; border: 2px solid currentColor; border-right-color: transparent; border-radius: 50%; display: inline-block; animation: spin .8s linear infinite; vertical-align: middle; }
@keyframes spin { to { transform: rotate(360deg); } }

/* ---------- PIN keypad ---------- */
.pin-wrap { text-align: center; }
.pin-wrap h1 { font-size: 20px; margin: 4px 0 6px; }
.pin-dots { display: flex; justify-content: center; gap: 14px; margin: 22px 0 8px; min-height: 18px; }
.pin-dots i { width: 14px; height: 14px; border-radius: 50%; border: 2px solid var(--muted); display: block; transition: .15s; }
.pin-dots i.on { background: var(--accent); border-color: var(--accent); }
.pin-dots.shake { animation: shake .35s; }
@keyframes shake { 20%, 60% { transform: translateX(-8px); } 40%, 80% { transform: translateX(8px); } }
.pin-msg { min-height: 1.3em; font-size: 14px; color: var(--err); margin-bottom: 8px; }
.keypad { display: grid; grid-template-columns: repeat(3, 72px); gap: 14px 22px; justify-content: center; margin: 8px auto 14px; }
.keypad button {
  width: 72px; height: 72px; border-radius: 50%; border: 1px solid var(--line); background: var(--surface2);
  font-size: 28px; font-weight: 500; cursor: pointer; display: grid; place-items: center;
}
.keypad button:active { background: var(--accent); color: var(--accent-ink); }
.keypad button.fn { background: transparent; border-color: transparent; font-size: 15px; font-weight: 600; color: var(--muted); }
.keypad button.fn:active { color: var(--accent); background: transparent; }
.lock-btn { background: transparent; border: 0; color: var(--muted); padding: 6px; cursor: pointer; display: grid; place-items: center; }
.lock-btn svg { width: 22px; height: 22px; }
.privacy { display: flex; gap: 10px; align-items: flex-start; font-size: 13px; color: var(--muted); }
.privacy svg { width: 20px; height: 20px; flex: none; color: var(--ok); }
