/* ================= tokens ================= */
:root {
  --bg: #f3f4f6; --surface: #ffffff; --surface-2: #f8fafc; --text: #0f172a; --muted: #64748b; --line: #e2e8f0;
  --accent: #1d4ed8; --accent-ink: #ffffff; --accent-soft: #e0e7ff; --ok: #15803d; --ok-soft: #dcfce7;
  --warn: #b45309; --warn-soft: #fef3c7; --danger: #b91c1c; --danger-soft: #fee2e2;
  --shadow: 0 1px 2px rgba(15,23,42,.06), 0 6px 20px rgba(15,23,42,.06);
  --radius: 14px;
  --toast-bg: #0f172a; --toast-ink: #ffffff; --toast-action: #93c5fd;
  --safe-top: env(safe-area-inset-top, 0px); --safe-bottom: env(safe-area-inset-bottom, 0px);
  --safe-left: env(safe-area-inset-left, 0px); --safe-right: env(safe-area-inset-right, 0px);
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #0b1120; --surface: #111a2e; --surface-2: #0f172a; --text: #e5e9f2; --muted: #94a3b8; --line: #1f2a44;
    --accent: #4f7cff; --accent-ink: #ffffff; --accent-soft: #1b2a55; --ok: #4ade80; --ok-soft: #12321f;
    --warn: #fbbf24; --warn-soft: #3a2a0a; --danger: #f87171; --danger-soft: #3b1414;
    --shadow: 0 1px 2px rgba(0,0,0,.4), 0 6px 20px rgba(0,0,0,.3);
    --toast-bg: #e5e9f2; --toast-ink: #0b1120; --toast-action: #1d4ed8;
    color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #0b1120; --surface: #111a2e; --surface-2: #0f172a; --text: #e5e9f2; --muted: #94a3b8; --line: #1f2a44;
  --accent: #4f7cff; --accent-ink: #ffffff; --accent-soft: #1b2a55; --ok: #4ade80; --ok-soft: #12321f;
  --warn: #fbbf24; --warn-soft: #3a2a0a; --danger: #f87171; --danger-soft: #3b1414;
  --shadow: 0 1px 2px rgba(0,0,0,.4), 0 6px 20px rgba(0,0,0,.3);
  --toast-bg: #e5e9f2; --toast-ink: #0b1120; --toast-action: #1d4ed8;
  color-scheme: dark;
}

/* ================= base ================= */
* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html { -webkit-text-size-adjust: 100%; text-size-adjust: 100%; }
body {
  margin: 0; min-height: 100vh; min-height: 100dvh; background: var(--bg); color: var(--text);
  font: 16px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, "Noto Sans", Arial, sans-serif;
  overscroll-behavior-y: none;
}
button, input, select, textarea { font: inherit; color: inherit; }
button { touch-action: manipulation; }
a { color: inherit; text-decoration: none; }
svg.i { width: 20px; height: 20px; flex: none; }
[hidden] { display: none !important; }
.muted { color: var(--muted); }
.small { font-size: 13px; }

/* ================= app bar ================= */
.appbar {
  position: sticky; top: 0; z-index: 20; display: flex; align-items: center; gap: 8px;
  padding: calc(8px + var(--safe-top)) calc(12px + var(--safe-right)) 8px calc(12px + var(--safe-left));
  background: var(--bg);
  background: color-mix(in srgb, var(--bg) 88%, transparent);
  -webkit-backdrop-filter: saturate(1.6) blur(14px); backdrop-filter: saturate(1.6) blur(14px);
  border-bottom: 1px solid var(--line); min-height: 56px;
}
.appbar .title { flex: 1; min-width: 0; display: flex; align-items: center; gap: 10px; }
.appbar .title img { width: 34px; height: 34px; border-radius: 9px; flex: none; }
.appbar .title > div { min-width: 0; flex: 1; }
.appbar .title b { display: block; font-size: 17px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.appbar .title small { display: block; font-size: 12px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.icon-btn {
  width: 42px; height: 42px; display: inline-grid; place-items: center; border: 0; border-radius: 12px;
  background: transparent; color: var(--text); cursor: pointer;
}
.icon-btn:active { background: var(--line); }

.chip {
  display: inline-flex; align-items: center; gap: 6px; height: 32px; padding: 0 11px; border-radius: 999px;
  border: 1px solid var(--line); background: var(--surface); font-size: 13px; color: var(--muted); cursor: pointer; white-space: nowrap;
}
.chip svg.i { width: 16px; height: 16px; }
.chip.s-ok { color: var(--ok); }
.chip.s-syncing { color: var(--accent); }
.chip.s-error, .chip.s-locked { color: var(--danger); border-color: var(--danger); border-color: color-mix(in srgb, var(--danger) 40%, var(--line)); }
.chip.s-offline, .chip.s-off { color: var(--warn); }
.chip.s-syncing svg { animation: spin 1.1s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

/* ================= page ================= */
.page { max-width: 760px; margin: 0 auto; padding: 14px calc(14px + var(--safe-right)) calc(110px + var(--safe-bottom)) calc(14px + var(--safe-left)); }
.card { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); box-shadow: var(--shadow); padding: 16px; margin-bottom: 14px; }
.card h3 { margin: 0 0 10px; font-size: 15px; display: flex; align-items: center; gap: 8px; }
.card h3 .tag { margin-left: auto; }

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 8px; min-height: 46px; padding: 10px 16px;
  border-radius: 12px; border: 1px solid var(--line); background: var(--surface); color: var(--text);
  font-weight: 600; font-size: 15px; cursor: pointer; transition: transform .08s ease, background .15s ease;
}
.btn:active { transform: scale(.98); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
.btn.danger { color: var(--danger); }
.btn.block { width: 100%; }
.btn.big { min-height: 56px; font-size: 16px; }
.btn:disabled { opacity: .55; }
.btn-row { display: flex; gap: 10px; flex-wrap: wrap; }
.btn-row > .btn { flex: 1 1 140px; }
.link-btn { border: 0; background: none; color: var(--accent); font-weight: 600; font-size: 13px; padding: 4px 2px; cursor: pointer; }

/* ================= forms ================= */
.field { margin-bottom: 12px; position: relative; }
.field:last-child { margin-bottom: 0; }
.field > label, .lbl { display: flex; align-items: center; gap: 6px; font-size: 13px; color: var(--muted); margin-bottom: 5px; font-weight: 500; }
.field > label .tag { margin-left: auto; }
.inp {
  width: 100%; min-height: 48px; padding: 11px 13px; border-radius: 11px; border: 1px solid var(--line);
  background: var(--surface-2); color: var(--text); font-size: 16px; /* 16px stops iPhone zooming in */
  outline: none; -webkit-appearance: none; appearance: none;
}
.inp:focus { border-color: var(--accent); box-shadow: 0 0 0 3px rgba(29, 78, 216, .22); box-shadow: 0 0 0 3px color-mix(in srgb, var(--accent) 22%, transparent); background: var(--surface); }
textarea.inp { min-height: 96px; resize: vertical; line-height: 1.4; }
select.inp { background-image: linear-gradient(45deg, transparent 50%, var(--muted) 50%), linear-gradient(135deg, var(--muted) 50%, transparent 50%);
  background-position: calc(100% - 18px) 21px, calc(100% - 13px) 21px; background-size: 5px 5px; background-repeat: no-repeat; padding-right: 34px; }
input[type="date"].inp, input[type="datetime-local"].inp { display: block; min-width: 0; text-align: left; }
input[type="date"].inp::-webkit-date-and-time-value, input[type="datetime-local"].inp::-webkit-date-and-time-value { text-align: left; }
.inp.upper { text-transform: uppercase; }
.grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.grid3 { display: grid; grid-template-columns: 1fr 1fr 1fr; gap: 10px; }
.grid-state { display: grid; grid-template-columns: 1fr 88px; gap: 10px; }
.hint { font-size: 12.5px; color: var(--muted); margin-top: 5px; }
.hint.ok { color: var(--ok); } .hint.warn { color: var(--warn); } .hint.bad { color: var(--danger); }
.tag { display: inline-flex; align-items: center; gap: 4px; font-size: 11.5px; font-weight: 600; padding: 2px 8px; border-radius: 999px; background: var(--accent-soft); color: var(--accent); }
.tag.manual { background: var(--warn-soft); color: var(--warn); }
.tag svg.i { width: 12px; height: 12px; }
details.more { border-top: 1px solid var(--line); margin-top: 14px; padding-top: 10px; }
details.more > summary { cursor: pointer; font-weight: 600; font-size: 14px; color: var(--accent); list-style: none; padding: 6px 0; }
details.more > summary::-webkit-details-marker { display: none; }
details.more[open] > summary { margin-bottom: 8px; }

/* suggestions under an input */
.sugg { position: absolute; left: 0; right: 0; top: 100%; margin-top: 4px; z-index: 30; background: var(--surface);
  border: 1px solid var(--line); border-radius: 12px; box-shadow: var(--shadow); overflow: hidden; }
.sugg button { display: block; width: 100%; text-align: left; border: 0; background: none; padding: 11px 13px; cursor: pointer; border-bottom: 1px solid var(--line); }
.sugg button:last-child { border-bottom: 0; }
.sugg button:active, .sugg button:hover { background: var(--accent-soft); }
.sugg b { display: block; font-size: 14.5px; } .sugg small { color: var(--muted); font-size: 12.5px; }

/* money summary */
.sum { margin-top: 12px; border-top: 1px dashed var(--line); padding-top: 10px; font-size: 14.5px; }
.sum div { display: flex; justify-content: space-between; padding: 3px 0; }
.sum .tot { font-weight: 700; font-size: 17px; border-top: 1px solid var(--line); margin-top: 6px; padding-top: 8px; }

/* ================= list ================= */
.hero-actions { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; margin-bottom: 14px; }
.search { position: relative; margin-bottom: 12px; }
.search svg { position: absolute; left: 13px; top: 14px; color: var(--muted); }
.search .inp { padding-left: 42px; }
.month { font-size: 12.5px; font-weight: 700; color: var(--muted); text-transform: uppercase; letter-spacing: .05em; margin: 18px 4px 8px; }
.inv-item { display: flex; align-items: center; gap: 12px; background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius);
  padding: 12px 14px; margin-bottom: 8px; box-shadow: var(--shadow); }
.inv-item:active { transform: scale(.99); }
.inv-item .no { min-width: 46px; height: 46px; padding: 0 6px; border-radius: 12px; background: var(--accent-soft); color: var(--accent); font-weight: 800; display: grid; place-items: center; font-size: 15px; }
.inv-item .mid { flex: 1; min-width: 0; }
.inv-item .mid b { display: block; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; font-size: 15px; }
.inv-item .mid small { color: var(--muted); font-size: 13px; }
.inv-item .amt { font-weight: 700; white-space: nowrap; }
.empty-state { text-align: center; padding: 40px 20px; color: var(--muted); }
.empty-state svg { width: 46px; height: 46px; opacity: .6; }
.banner { display: flex; gap: 12px; align-items: center; background: var(--accent-soft); border-radius: var(--radius); padding: 12px 14px; margin-bottom: 14px; }
.banner.warn { background: var(--warn-soft); }
.banner p { margin: 0; flex: 1; font-size: 14px; }

/* ================= editor ================= */
.seg { display: flex; gap: 4px; padding: 4px; margin: 12px auto 0; background: var(--line); border-radius: 12px; max-width: 760px;
  width: calc(100% - 28px - var(--safe-left) - var(--safe-right)); }
.seg button { flex: 1; min-height: 40px; border: 0; border-radius: 9px; background: transparent; font-weight: 600; color: var(--muted); cursor: pointer; }
.seg button.on { background: var(--surface); color: var(--text); box-shadow: 0 1px 3px rgba(0,0,0,.12); }
.editor .pane.bill-pane { display: none; }
.editor.tab-bill .pane.details-pane { display: none; }
.editor.tab-bill .pane.bill-pane { display: block; }
.bill-hint { display: flex; gap: 8px; align-items: center; font-size: 13.5px; color: var(--muted); margin: 0 2px 10px; }
.bill-fit { position: relative; overflow: hidden; border-radius: 6px; box-shadow: var(--shadow); background: #fff; }
.bill-scale { transform-origin: 0 0; width: 794px; }

.bottombar {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 25; display: flex; align-items: center; gap: 12px;
  padding: 10px calc(14px + var(--safe-right)) calc(10px + var(--safe-bottom)) calc(14px + var(--safe-left));
  background: var(--surface);
  background: color-mix(in srgb, var(--surface) 92%, transparent);
  -webkit-backdrop-filter: saturate(1.6) blur(14px); backdrop-filter: saturate(1.6) blur(14px);
  border-top: 1px solid var(--line);
}
.bottombar .tot { flex: 1; min-width: 0; }
.bottombar .tot small { display: block; color: var(--muted); font-size: 12px; }
.bottombar .tot b { font-size: 21px; }
.bottombar .btn { min-width: 128px; }
.fab-wrap { max-width: 760px; margin: 0 auto; display: flex; gap: 12px; width: 100%; align-items: center; }

@media (min-width: 1100px) {
  .seg { display: none; }
  .editor { display: grid; grid-template-columns: minmax(380px, 460px) 1fr; gap: 20px; max-width: 1400px; margin: 0 auto; align-items: start; }
  .editor .page { max-width: none; margin: 0; }
  .editor .pane.bill-pane, .editor.tab-bill .pane.details-pane { display: block; }
  .editor .bill-pane .page { position: sticky; top: 64px; }
  .fab-wrap { max-width: 1400px; }
}

/* tap-to-edit on the bill */
.bill .tap { cursor: pointer; border-radius: 2px; box-shadow: inset 0 -1.5px 0 #f1d77a; }
.bill .tap:hover, .bill .tap:active { background: #fff3b0; }
.bill .tap.empty { display: inline-block; min-width: 46px; min-height: 1.2em; vertical-align: bottom; }
.bill .tap.empty::before { content: attr(data-ph); color: #b8bdc6; font-weight: normal; }
.bill .tap.blk { display: block; min-height: 1.35em; }
.bill .tap.flash { animation: flash 1s ease; }
@keyframes flash { 0%, 40% { background: #ffe066; } 100% { background: transparent; } }

/* ================= the bill (always black on white) ================= */
.bill-page { width: 794px; padding: 38px; background: #fff; color: #000; }
.bill { font-family: "Times New Roman", Tinos, "Liberation Serif", "Noto Serif", Georgia, serif; font-size: 13px; color: #000; line-height: 1.35; }
.bill table { width: 100%; border-collapse: collapse; }
.bill table + table { margin-top: -1px; }
.bill td, .bill th { border: 1px solid #000; padding: 3px 6px; vertical-align: top; font-weight: normal; text-align: left; }
.bill .doc-title { text-align: center; font-size: 17px; font-weight: bold; margin-bottom: 5px; }
.bill .foot { text-align: center; font-size: 12px; margin-top: 5px; }
.bill .sm { font-size: 12px; }
.bill .val { font-weight: bold; font-size: 14px; }
.bill .v { font-weight: bold; font-size: 14px; min-height: 1.3em; }
.bill .hcell { height: 40px; }
.bill .r { text-align: right; } .bill .b { font-weight: bold; }
.bill .ml { white-space: pre-line; }
.bill .items th { text-align: center; font-size: 12px; }
.bill .items tbody td { border-top: none; border-bottom: none; }
.bill .items tr.first td { padding-top: 10px; }
.bill .items tr.tax td { padding-top: 2px; }
.bill .items tr.fill td { height: 160px; }
.bill .items tr.total td { border-top: 1px solid #000; border-bottom: 1px solid #000; padding-top: 6px; padding-bottom: 6px; }
.bill .words { display: flex; justify-content: space-between; }
.bill .taxtbl th { text-align: center; font-size: 12px; }
.bill .taxtbl td { text-align: right; }
.bill .taxtbl td:first-child { text-align: left; }
.bill .sign { height: 86px; }

/* ================= sheets ================= */
.sheet-wrap { position: fixed; inset: 0; z-index: 50; display: flex; align-items: flex-end; justify-content: center; }
.sheet-backdrop { position: absolute; inset: 0; background: rgba(2,6,23,.45); animation: fade .2s ease; }
.sheet {
  position: relative; width: 100%; max-width: 560px; max-height: 92vh; max-height: 92dvh; overflow: auto; -webkit-overflow-scrolling: touch;
  background: var(--surface); border-radius: 20px 20px 0 0; padding: 10px 18px calc(18px + var(--safe-bottom));
  box-shadow: 0 -10px 40px rgba(0,0,0,.25); animation: up .24s cubic-bezier(.2,.8,.2,1);
}
.sheet::before { content: ""; display: block; width: 40px; height: 5px; border-radius: 9px; background: var(--line); margin: 0 auto 12px; }
.sheet h3 { margin: 0 0 6px; font-size: 18px; }
.sheet p { margin: 0 0 12px; }
.sheet .btn-row { margin-top: 14px; }
.sheet-wrap.closing .sheet { animation: down .18s ease forwards; }
.sheet-wrap.closing .sheet-backdrop { animation: fade .18s ease reverse forwards; }
@keyframes up { from { transform: translateY(100%); } }
@keyframes down { to { transform: translateY(100%); } }
@keyframes fade { from { opacity: 0; } }
@media (min-width: 700px) {
  .sheet-wrap { align-items: center; }
  .sheet { border-radius: 20px; padding-bottom: 18px; }
  .sheet::before { display: none; }
}
.parsed { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 10px; min-height: 8px; }
.parsed span { font-size: 12.5px; background: var(--ok-soft); color: var(--ok); padding: 3px 9px; border-radius: 999px; }
.parsed span.miss { background: var(--line); color: var(--muted); }
.share-status { display: flex; align-items: center; gap: 10px; padding: 14px 0; color: var(--muted); }
.spinner { width: 22px; height: 22px; border-radius: 50%; border: 3px solid var(--line); border-top-color: var(--accent); animation: spin .8s linear infinite; }
.share-actions { display: grid; gap: 10px; }

/* ================= toast ================= */
#toast { position: fixed; left: 50%; bottom: calc(92px + var(--safe-bottom)); transform: translate(-50%, 20px); z-index: 60;
  background: var(--toast-bg); color: var(--toast-ink); padding: 11px 16px; border-radius: 12px; font-size: 14.5px; box-shadow: 0 8px 30px rgba(0,0,0,.3);
  opacity: 0; pointer-events: none; transition: opacity .2s ease, transform .2s ease; display: flex; gap: 14px; align-items: center; max-width: calc(100% - 28px); }
#toast.show { opacity: 1; transform: translate(-50%, 0); pointer-events: auto; }
#toast button { border: 0; background: none; color: var(--toast-action); font-weight: 700; cursor: pointer; padding: 4px; }

/* PDF render stage (off screen) */
#pdfStage { position: fixed; left: -10000px; top: 0; width: 794px; background: #fff; }

/* ================= print ================= */
#printArea { display: none; }
@media print {
  @page { size: A4; margin: 0; }
  html, body { background: #fff !important; }
  body > *:not(#printArea) { display: none !important; }
  #printArea { display: block !important; }
  #printArea .bill-page { width: 210mm; padding: 10mm; }
}
@media (prefers-reduced-motion: reduce) { * { animation: none !important; transition: none !important; } }

@media (max-width: 380px) {
  .chip.sync span { display: none; }
  .chip.sync { padding: 0 9px; }
  .hero-actions .btn.big { font-size: 15px; padding: 10px 8px; }
}

/* ================= passcode lock ================= */
.lock { max-width: 380px; margin: 0 auto; min-height: 100vh; min-height: 100dvh; display: flex; flex-direction: column; justify-content: center;
  text-align: center; padding: calc(24px + var(--safe-top)) 24px calc(24px + var(--safe-bottom)); }
.lock img { width: 72px; height: 72px; border-radius: 18px; margin: 0 auto 14px; box-shadow: var(--shadow); }
.lock h1 { font-size: 22px; margin: 0 0 4px; }
.lock form { display: grid; gap: 12px; margin-top: 18px; }
.lock .inp { text-align: center; font-size: 18px; letter-spacing: .08em; }
.lock .hint { min-height: 1.2em; }
.sr-only { position: absolute; width: 1px; height: 1px; padding: 0; margin: -1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; border: 0; }
