/* Sổ bán hàng — cùng tông màu với bảng giá Đức Hạnh */
:root {
  --brand: #7a2e1d;
  --brand-2: #9c4430;
  --brand-soft: #f3e4dc;
  --bg: #faf6f0;
  --card: #ffffff;
  --ink: #2b211c;
  --muted: #7b6d64;
  --line: #e8ddd2;
  --ok: #2f7a4b;
  --ok-soft: #e3f2e8;
  --warn: #a86400;
  --warn-soft: #fdf0d8;
  --bad: #b3261e;
  --bad-soft: #fbe3e1;
  --radius: 14px;
  --shadow: 0 1px 2px rgba(60, 30, 15, .06), 0 4px 14px rgba(60, 30, 15, .06);
  color-scheme: light;
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--ink);
  font: 16px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  padding-bottom: calc(90px + env(safe-area-inset-bottom));
}
[hidden] { display: none !important; }
.wrap { max-width: 860px; margin: 0 auto; padding: 0 16px; }
.sr-only { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }
.muted { color: var(--muted); }
.small { font-size: .875rem; }
.spacer { flex: 1; }
h2 { font-size: 1.1rem; margin: 0 0 12px; }

/* ---------- Thanh trên ---------- */
.topbar {
  position: sticky; top: 0; z-index: 20;
  background: var(--brand);
  color: #fff;
  padding-top: env(safe-area-inset-top);
  box-shadow: 0 2px 10px rgba(60, 20, 10, .2);
}
.topbar-in { display: flex; align-items: center; gap: 10px; padding-top: 10px; padding-bottom: 6px; }
.brand { display: flex; flex-direction: column; min-width: 0; }
.brand strong { font-size: 1.3rem; letter-spacing: .01em; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.eyebrow { font-size: .72rem; text-transform: uppercase; letter-spacing: .12em; opacity: .75; }

.tabs { display: flex; gap: 4px; overflow-x: auto; scrollbar-width: none; }
.tabs::-webkit-scrollbar { display: none; }
.tabs button {
  appearance: none; border: 0; background: none; color: rgba(255,255,255,.75);
  font: inherit; font-size: .95rem; font-weight: 600;
  padding: 10px 12px 12px; cursor: pointer; white-space: nowrap;
  display: flex; align-items: center; gap: 6px;
  border-bottom: 3px solid transparent;
}
.tabs button svg { width: 18px; height: 18px; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; }
.tabs button[aria-selected="true"] { color: #fff; border-bottom-color: #f5c9a8; }
@media (max-width: 560px) {
  .tabs { gap: 0; }
  .tabs button { flex: 1; flex-direction: column; gap: 2px; padding: 6px 2px 8px !important; font-size: .78rem; }
  .tabs button span { font-size: .78rem !important; }
  .tabs button svg { width: 20px; height: 20px; }
}

/* ---------- Lỗ lãi ---------- */
.year-nav h2 { margin: 0; font-size: 1.2rem; min-width: 90px; text-align: center; }
.r-note { margin: -4px 0 12px; }
.stat.profit strong { color: var(--ok); }
.stat.loss strong { color: var(--bad); }
.rlist { list-style: none; margin: 0; padding: 0; display: grid; gap: 10px; }
.rrow { background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); padding: 12px 14px; box-shadow: var(--shadow); }
.rrow.now { border-color: var(--brand-2); }
.rrow.empty-m { box-shadow: none; background: transparent; padding: 8px 14px; color: var(--muted); }
.r-head { display: flex; align-items: baseline; gap: 8px; margin-bottom: 8px; }
.r-head b { font-size: 1.05rem; }
.r-head .muted { font-size: .85rem; }
.r-head .pl { margin-left: auto; font-weight: 800; font-size: 1.1rem; font-variant-numeric: tabular-nums; white-space: nowrap; }
.pl.pos { color: var(--ok); } .pl.neg { color: var(--bad); } .pl.zero { color: var(--muted); }
.r-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 6px 14px; font-size: .9rem; }
.r-grid span { color: var(--muted); display: block; font-size: .78rem; font-weight: 600; }
.r-grid strong { font-variant-numeric: tabular-nums; }
.r-bars { display: grid; gap: 4px; margin: 4px 0 10px; }
.r-bar { height: 8px; border-radius: 4px; min-width: 2px; }
.r-bar.rev { background: var(--ok); }
.r-bar.cost { background: var(--warn); }
.r-legend { display: flex; gap: 14px; font-size: .78rem; color: var(--muted); margin: -4px 0 10px; }
.r-legend i { display: inline-block; width: 10px; height: 10px; border-radius: 3px; margin-right: 5px; vertical-align: -1px; }
.r-legend .rev { background: var(--ok); } .r-legend .cost { background: var(--warn); }

main { padding-top: 16px; }

/* ---------- Nút ---------- */
.btn {
  appearance: none; display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  font: inherit; font-weight: 600; font-size: .95rem;
  padding: 10px 16px; min-height: 44px;
  border-radius: 10px; border: 1px solid var(--line);
  background: var(--card); color: var(--ink);
  cursor: pointer; text-decoration: none; white-space: nowrap;
}
.btn:hover { border-color: #d6c6b8; }
.btn:active { transform: translateY(1px); }
.btn.primary { background: var(--brand); border-color: var(--brand); color: #fff; }
.btn.primary:hover { background: var(--brand-2); }
.btn.danger { background: var(--bad); border-color: var(--bad); color: #fff; }
.btn.ok { background: var(--ok); border-color: var(--ok); color: #fff; }
.btn.small { min-height: 36px; padding: 6px 12px; font-size: .875rem; }
.btn.light { background: rgba(255,255,255,.15); border-color: rgba(255,255,255,.35); color: #fff; }
.btn.ghost { background: transparent; border-color: transparent; color: var(--brand); padding-left: 8px; padding-right: 8px; }
.icon-btn { appearance: none; border: 0; background: none; font-size: 1.8rem; line-height: 1; cursor: pointer; color: var(--muted); padding: 4px 8px; }
.file-btn { position: relative; }
:focus-visible { outline: 3px solid #e7a77f; outline-offset: 2px; }

/* ---------- Ô nhập ---------- */
label { display: flex; flex-direction: column; gap: 4px; font-size: .875rem; font-weight: 600; color: #4a3a31; margin-bottom: 12px; }
input, select, textarea {
  font: inherit; font-size: 16px; font-weight: 400; color: var(--ink);
  border: 1px solid var(--line); border-radius: 10px;
  padding: 10px 12px; min-height: 44px; background: #fff; width: 100%;
}
textarea { resize: vertical; }
input:focus, select:focus, textarea:focus { outline: none; border-color: var(--brand-2); box-shadow: 0 0 0 3px rgba(156, 68, 48, .15); }
.two { display: grid; grid-template-columns: 1fr 1fr; gap: 0 12px; }
.three { display: grid; grid-template-columns: 1fr 1fr 1fr; gap: 0 12px; }
@media (max-width: 560px) {
  .two { grid-template-columns: 1fr; }
  .three { grid-template-columns: 1fr 1fr; }
  .three > :last-child { grid-column: 1 / -1; }
}
fieldset { border: 0; padding: 0; margin: 0 0 14px; }
legend { font-weight: 700; color: var(--brand); padding: 0; margin-bottom: 8px; font-size: .95rem; }
.hint { margin: -6px 0 10px; color: var(--ok); }

.search { position: relative; flex: 1; margin: 0; min-width: 0; }
.search svg { position: absolute; left: 12px; top: 50%; width: 18px; height: 18px; transform: translateY(-50%); fill: none; stroke: var(--muted); stroke-width: 2; stroke-linecap: round; }
.search input { padding-left: 38px; }

.row-tools { display: flex; gap: 10px; align-items: center; margin-bottom: 12px; }
.row-tools.wrap-row { flex-wrap: wrap; }
.month { flex-direction: row; align-items: center; gap: 8px; margin: 0; flex: 1; min-width: 0; white-space: nowrap; }
.month input { width: auto; flex: 1; min-width: 0; max-width: 220px; }
html, body { overflow-x: hidden; }

.chips { display: flex; gap: 8px; overflow-x: auto; padding-bottom: 4px; margin-bottom: 8px; scrollbar-width: none; }
.chips::-webkit-scrollbar { display: none; }
.chip {
  appearance: none; font: inherit; font-size: .875rem; font-weight: 600;
  border: 1px solid var(--line); background: var(--card); color: var(--ink);
  border-radius: 999px; padding: 6px 14px; min-height: 36px; cursor: pointer; white-space: nowrap;
}
.chip[aria-pressed="true"] { background: var(--brand); color: #fff; border-color: var(--brand); }
.chip .n { opacity: .7; margin-left: 4px; font-weight: 500; }

/* ---------- Thẻ thống kê ---------- */
.stats { display: grid; grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); gap: 10px; margin-bottom: 14px; }
.stat { background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); padding: 10px 12px; box-shadow: var(--shadow); min-width: 0; }
.stat span { display: block; font-size: .78rem; color: var(--muted); font-weight: 600; }
.stat strong { display: block; font-size: 1.15rem; font-variant-numeric: tabular-nums; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.stat small { display: block; font-size: .78rem; color: var(--muted); }
.stat.bad strong { color: var(--bad); }
.stat.warn strong { color: var(--warn); }
.stat.ok strong { color: var(--ok); }

/* ---------- Danh sách đơn ---------- */
.olist, .ilist { list-style: none; margin: 0; padding: 0; display: grid; gap: 10px; }
.order {
  background: var(--card); border: 1px solid var(--line); border-radius: var(--radius);
  padding: 12px 14px; box-shadow: var(--shadow); border-left: 5px solid var(--ok);
}
.order.is-debt { border-left-color: var(--warn); }
.order.is-overdue { border-left-color: var(--bad); }
.o-top { display: flex; gap: 10px; align-items: flex-start; }
.o-items { flex: 1; min-width: 0; }
.o-item { font-weight: 650; overflow-wrap: anywhere; }
.o-item .sz { display: inline-block; font-size: .78rem; font-weight: 700; background: var(--brand-soft); color: var(--brand); border-radius: 6px; padding: 1px 7px; margin-left: 6px; vertical-align: 1px; }
.o-item .q { color: var(--muted); font-weight: 500; font-size: .9rem; }
.o-total { text-align: right; font-weight: 750; font-size: 1.05rem; font-variant-numeric: tabular-nums; white-space: nowrap; }
.o-total small { display: block; font-size: .78rem; font-weight: 600; }
.o-meta { display: flex; flex-wrap: wrap; gap: 6px 14px; margin-top: 8px; font-size: .9rem; align-items: center; }
.o-meta a { color: var(--brand); font-weight: 600; text-decoration: none; }
.o-meta a:hover { text-decoration: underline; }
.badge { display: inline-flex; align-items: center; gap: 4px; font-size: .78rem; font-weight: 700; border-radius: 999px; padding: 3px 10px; }
.badge.paid { background: var(--ok-soft); color: var(--ok); }
.badge.debt { background: var(--warn-soft); color: var(--warn); }
.badge.overdue { background: var(--bad-soft); color: var(--bad); }
.o-addr { margin-top: 6px; font-size: .9rem; overflow-wrap: anywhere; }
.o-addr::before { content: "📍 "; }
.badge.printed { background: #eef0f3; color: #44505e; }
.debt-line { margin-top: 8px; font-size: .9rem; background: var(--warn-soft); border-radius: 8px; padding: 6px 10px; color: #6e4300; }
.order.is-overdue .debt-line { background: var(--bad-soft); color: #7d1a14; }
.codes { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 8px; }
.code {
  appearance: none; font: inherit; font-family: ui-monospace, "SF Mono", Menlo, Consolas, monospace; font-size: .85rem;
  background: #f6f1eb; border: 1px dashed #d8c7b8; border-radius: 8px; padding: 4px 10px; cursor: pointer; color: var(--ink);
  display: inline-flex; gap: 6px; align-items: center; min-height: 32px;
}
.code::before { content: "TB"; font-family: system-ui, sans-serif; font-size: .68rem; font-weight: 800; color: #fff; background: #ff5000; border-radius: 4px; padding: 1px 4px; }
.code:hover { border-style: solid; }
.o-note { margin-top: 6px; font-size: .875rem; color: var(--muted); overflow-wrap: anywhere; }
.o-actions { display: flex; gap: 8px; margin-top: 10px; flex-wrap: wrap; align-items: center; }
.o-date { font-size: .78rem; color: var(--muted); margin-left: auto; }

.empty { text-align: center; padding: 40px 16px; color: var(--muted); background: var(--card); border: 1px dashed var(--line); border-radius: var(--radius); }
.empty strong { display: block; color: var(--ink); margin-bottom: 4px; }

/* ---------- Nhập hàng ---------- */
.card-form { background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); padding: 16px; box-shadow: var(--shadow); margin-bottom: 16px; }
.card-form.editing { border-color: var(--brand-2); box-shadow: 0 0 0 3px rgba(156,68,48,.12), var(--shadow); }
.calc-result {
  display: flex; align-items: baseline; justify-content: space-between; gap: 12px;
  background: var(--brand-soft); border-radius: 12px; padding: 12px 14px; margin: 4px 0 12px;
}
.calc-result strong { font-size: 1.5rem; color: var(--brand); font-variant-numeric: tabular-nums; white-space: nowrap; }
.form-actions { display: flex; gap: 10px; align-items: center; }
.form-msg { color: var(--bad); font-size: .9rem; margin: 8px 0 0; min-height: 1em; }
.form-msg:empty { display: none; }

.irow {
  background: var(--card); border: 1px solid var(--line); border-radius: var(--radius);
  padding: 12px 14px; box-shadow: var(--shadow);
  display: grid; grid-template-columns: auto 1fr auto; gap: 4px 14px; align-items: center;
}
.i-date { text-align: center; background: var(--brand-soft); color: var(--brand); border-radius: 10px; padding: 6px 10px; min-width: 58px; line-height: 1.1; }
.i-date b { display: block; font-size: 1.25rem; }
.i-date span { font-size: .72rem; font-weight: 700; }
.i-mid { min-width: 0; }
.i-calc { font-variant-numeric: tabular-nums; font-weight: 600; }
.i-sub { font-size: .85rem; color: var(--muted); overflow-wrap: anywhere; }
.i-total { font-weight: 750; font-size: 1.05rem; font-variant-numeric: tabular-nums; text-align: right; white-space: nowrap; }
.i-acts { grid-column: 1 / -1; display: flex; justify-content: flex-end; gap: 6px; }

/* ---------- Hộp thoại ---------- */
dialog.editor {
  border: 0; padding: 0; border-radius: 18px; width: min(620px, calc(100vw - 24px));
  max-height: calc(100dvh - 24px); background: var(--bg); color: var(--ink);
  box-shadow: 0 20px 60px rgba(30, 10, 5, .35);
}
dialog.editor::backdrop { background: rgba(30, 15, 8, .45); }
dialog.editor form { display: flex; flex-direction: column; max-height: calc(100dvh - 24px); }
.editor-head { display: flex; align-items: center; justify-content: space-between; padding: 14px 16px 6px; }
.editor-head h2 { margin: 0; font-size: 1.2rem; }
.editor-body { padding: 8px 16px; overflow-y: auto; flex: 1; }
.editor-foot { display: flex; gap: 10px; padding: 12px 16px calc(12px + env(safe-area-inset-bottom)); border-top: 1px solid var(--line); background: var(--card); border-radius: 0 0 18px 18px; }
@media (max-width: 560px) {
  dialog.editor { width: 100vw; max-width: 100vw; max-height: 100dvh; height: 100dvh; margin: 0; border-radius: 0; }
  dialog.editor form { max-height: 100dvh; height: 100%; }
  .editor-foot { border-radius: 0; }
}

.item-row {
  display: grid; grid-template-columns: 1fr 90px; gap: 8px 10px;
  background: var(--card); border: 1px solid var(--line); border-radius: 12px; padding: 10px 10px 0; margin-bottom: 10px; position: relative;
}
.item-row label { margin-bottom: 10px; }
.item-row .name { grid-column: 1 / -1; padding-right: 34px; }
.item-row .rm { position: absolute; top: 4px; right: 4px; font-size: 1.4rem; }
.item-row .row2 { grid-column: 1 / -1; display: grid; grid-template-columns: 1fr 70px 1.3fr; gap: 0 10px; }

.seg { display: flex; gap: 0; margin-bottom: 12px; border: 1px solid var(--line); border-radius: 12px; overflow: hidden; background: #fff; }
.seg label { flex: 1; margin: 0; flex-direction: row; justify-content: center; cursor: pointer; position: relative; }
.seg input { position: absolute; opacity: 0; pointer-events: none; }
.seg span { display: block; width: 100%; text-align: center; padding: 11px 8px; font-size: .95rem; }
.seg label + label { border-left: 1px solid var(--line); }
.seg input:checked + span { background: var(--ok); color: #fff; }
.seg input[value="debt"]:checked + span { background: var(--warn); }
.seg input[value="cod"]:checked + span { background: var(--brand); }
#cod-box { background: var(--brand-soft); border-radius: 12px; padding: 12px 12px 2px; margin-bottom: 12px; }
#cod-box input { font-size: 1.25rem; font-weight: 700; color: var(--brand); }
.cod-tip { margin: -6px 0 10px; }
.badge.cod { background: var(--brand-soft); color: var(--brand); }
.order.is-cod { border-left-color: var(--brand); }
.seg input:focus-visible + span { outline: 3px solid #e7a77f; outline-offset: -3px; }
#debt-box { background: var(--warn-soft); border-radius: 12px; padding: 12px 12px 2px; margin-bottom: 12px; }

/* ---------- Nút nổi + toast ---------- */
.fab {
  position: fixed; right: 18px; bottom: calc(18px + env(safe-area-inset-bottom)); z-index: 30;
  width: 60px; height: 60px; border-radius: 50%; border: 0; cursor: pointer;
  background: var(--brand); color: #fff; font-size: 2rem; line-height: 1; box-shadow: 0 6px 20px rgba(122, 46, 29, .45);
  display: none;
}
@media (max-width: 700px) { .fab.show { display: block; } #o-add { display: none; } }

.toast {
  position: fixed; left: 50%; bottom: calc(24px + env(safe-area-inset-bottom)); z-index: 100;
  transform: translate(-50%, 20px); opacity: 0; pointer-events: none;
  background: #2b211c; color: #fff; padding: 10px 18px; border-radius: 999px; font-size: .9rem;
  transition: opacity .2s, transform .2s; max-width: calc(100vw - 32px); text-align: center;
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }

@media (max-width: 420px) {
  .stats { gap: 6px; }
  .stat { padding: 8px 9px; }
  .stat strong { font-size: 1rem; }
  .tabs button span { font-size: .9rem; }
  .tabs button { padding-left: 8px; padding-right: 8px; gap: 5px; }
  .month { font-size: .8rem; }
}
.check-row { flex-direction: row; align-items: center; gap: 10px; font-weight: 500; }
.check-row input { width: 22px; height: 22px; min-height: 0; flex: none; accent-color: var(--brand); }

/* ---------- Tem in ---------- */
dialog.small-dlg { width: min(460px, calc(100vw - 24px)); }
@media (max-width: 560px) { dialog.small-dlg { width: 100vw; } }
.pr-wrap { display: flex; flex-direction: column; max-height: calc(100dvh - 24px); }
@media (max-width: 560px) { .pr-wrap { height: 100%; max-height: 100dvh; } }
.pr-stage { background: #e9e2da; border-radius: 12px; padding: 18px; display: flex; justify-content: center; align-items: center; overflow: auto; margin-bottom: 8px; min-height: 120px; }
#pr-preview .label { box-shadow: 0 1px 4px rgba(0,0,0,.25); }
.label {
  background: #fff; color: #000; overflow: hidden; position: relative;
  font-family: Arial, "Helvetica Neue", Helvetica, sans-serif; line-height: 1.18;
  display: flex; flex-direction: column;
}
.label * { color: #000; }
.lb-shop { display: flex; justify-content: space-between; gap: 1mm; border-bottom: .25mm solid #000; padding-bottom: .5mm; margin-bottom: .6mm; font-size: .82em; }
.lb-shop b { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.lb-shop span { white-space: nowrap; }
.lb-to { font-weight: 700; font-size: 1.12em; }
.lb-to .ph { white-space: nowrap; }
.lb-addr { margin-top: .3mm; overflow-wrap: anywhere; }
.lb-items { margin-top: .5mm; font-size: .88em; overflow-wrap: anywhere; }
.lb-note { font-size: .82em; font-style: italic; overflow-wrap: anywhere; }
.lb-foot { margin-top: auto; padding-top: .5mm; display: flex; justify-content: space-between; align-items: flex-end; gap: 1mm; }
.lb-money { font-weight: 800; font-size: 1.3em; border: .3mm solid #000; padding: .2mm 1mm; white-space: nowrap; }
.lb-money .ps { font-size: .72em; margin-left: .6mm; letter-spacing: -.01em; }
.lb-money.paid { border-style: dashed; font-size: .95em; }
.lb-date { font-size: .75em; white-space: nowrap; }
#print-area { display: none; }

@media print {
  html, body { background: #fff !important; padding: 0 !important; margin: 0 !important; overflow: visible !important; }
  body > *:not(#print-area) { display: none !important; }
  #print-area { display: block !important; }
  #print-area .label { page-break-after: always; break-after: page; box-shadow: none; }
  #print-area .label:last-child { page-break-after: auto; break-after: auto; }
}
