:root {
  --bg: #f6f4f2;
  --card: #ffffff;
  --ink: #1d1a19;
  --muted: #6d6662;
  --line: #e6e1dd;
  --brand: #a3142a;
  --brand-ink: #ffffff;
  --brand-soft: #fbe9ec;
  --accent: #c98a12;
  --warn-bg: #fff4d6;
  --warn-ink: #7a5300;
  --bad-bg: #fde8e8;
  --bad-ink: #9b1c1c;
  --ok-ink: #1f6f43;
  --cost: #2456a6;
  --cost-bg: #e9f0fb;
  --chip: #efebe8;
  --shadow: 0 1px 2px rgba(0,0,0,.06), 0 2px 8px rgba(0,0,0,.04);
  --tabbar-h: 62px;
  --badge-gap: 0px; /* chỗ chừa cho badge "Powered by Netlify" (gói Free) */
  color-scheme: light;
}
body.nl-badge { --badge-gap: 58px; } /* chữ trên thanh tab cách badge 0,3cm; mép thanh tab vẫn nằm trên badge */
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #151312;
    --card: #211e1d;
    --ink: #f1ece9;
    --muted: #a59d98;
    --line: #36312f;
    --brand: #d33a52;
    --brand-soft: #3a1c22;
    --accent: #e3a83a;
    --warn-bg: #3a2f12;
    --warn-ink: #f3d27e;
    --bad-bg: #3d1a1a;
    --bad-ink: #f5a3a3;
    --ok-ink: #7fd3a2;
    --cost: #8fb4f0;
    --cost-bg: #1c2840;
    --chip: #2c2826;
    --shadow: none;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; }
body {
  background: var(--bg);
  color: var(--ink);
  font: 16px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  /* Khoảng dưới thanh tab: lấy cái LỚN HƠN giữa chỗ chừa badge Netlify và thanh vuốt của điện thoại
     (badge vốn đã nằm đè lên vùng thanh vuốt, nên không cộng dồn). */
  --safe-b: env(safe-area-inset-bottom, 0px);
  --bottom-space: max(var(--badge-gap), var(--safe-b));
  padding-bottom: calc(var(--tabbar-h) + var(--bottom-space));
  overflow-x: hidden;
}
button, input, select, textarea { font: inherit; color: inherit; }
svg { width: 24px; height: 24px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }
[hidden] { display: none !important; }

/* Top bar */
.topbar {
  position: sticky; top: 0; z-index: 20;
  display: flex; align-items: center; gap: 8px;
  padding: calc(8px + env(safe-area-inset-top)) 12px 8px;
  background: var(--brand); color: var(--brand-ink);
}
.topbar .brand { display: flex; align-items: center; gap: 8px; font-weight: 700; font-size: 18px; flex: 1; min-width: 0; }
.brand #title { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.logo { display: inline-grid; place-items: center; width: 30px; height: 30px; border-radius: 8px; background: rgba(255,255,255,.18); font-size: 13px; letter-spacing: .5px; }
.iconbtn { background: none; border: 0; color: inherit; padding: 6px; margin-left: -6px; border-radius: 10px; display: grid; place-items: center; }
.ownerbtn { display: flex; align-items: center; gap: 4px; border: 1px solid rgba(255,255,255,.45); background: transparent; color: inherit; border-radius: 999px; padding: 4px 10px 4px 8px; font-size: 14px; }
.ownerbtn svg { width: 18px; height: 18px; }
.ownerbtn[aria-pressed="true"] { background: #fff; color: var(--brand); border-color: #fff; font-weight: 600; }

/* Tab bar */
.tabbar {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 20;
  display: grid; grid-template-columns: repeat(6, 1fr); grid-template-rows: var(--tabbar-h); align-content: start;
  height: calc(var(--tabbar-h) + var(--bottom-space));
  padding-bottom: calc(var(--bottom-space));
  background: var(--card); border-top: 1px solid var(--line);
}
.tabbar a { position: relative; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 2px; color: var(--muted); text-decoration: none; font-size: 11.5px; }
.tabbar a svg { width: 23px; height: 23px; }
.tabbar a.active { color: var(--brand); font-weight: 600; }
.tabbar .dot { position: absolute; top: 8px; right: calc(50% - 18px); width: 8px; height: 8px; border-radius: 50%; background: var(--accent); }

main { max-width: 760px; margin: 0 auto; padding: 12px 16px 24px; }
.loading { padding: 40px 0; text-align: center; color: var(--muted); }

/* Search & filters */
.searchbox { position: sticky; top: calc(50px + env(safe-area-inset-top)); z-index: 10; background: var(--bg); padding: 4px 0 8px; margin: 0 -2px; }
.search {
  display: flex; align-items: center; gap: 8px;
  background: var(--card); border: 1.5px solid var(--line); border-radius: 12px; padding: 0 10px;
}
.search:focus-within { border-color: var(--brand); }
.search svg { width: 20px; height: 20px; color: var(--muted); flex: none; }
.search input { flex: 1; border: 0; outline: 0; background: transparent; padding: 11px 0; font-size: 16px; min-width: 0; }
.search input::-webkit-search-cancel-button { -webkit-appearance: none; display: none; }
.search .clear { border: 0; background: none; color: var(--muted); padding: 6px; font-size: 18px; line-height: 1; }
.chips { display: flex; gap: 6px; overflow-x: auto; padding: 8px 0 2px; scrollbar-width: none; }
.chips::-webkit-scrollbar { display: none; }
.chip { flex: none; border: 1px solid var(--line); background: var(--card); border-radius: 999px; padding: 6px 12px; font-size: 14px; white-space: nowrap; }
.chip.on { background: var(--brand); color: #fff; border-color: var(--brand); }
.filters { display: grid; grid-template-columns: 1fr 1fr; gap: 6px; margin-top: 6px; }
select, .input, textarea {
  width: 100%; border: 1.5px solid var(--line); background: var(--card); border-radius: 10px; padding: 9px 10px; font-size: 16px; min-width: 0;
}
select { padding-right: 6px; font-size: 14.5px; }
textarea { min-height: 120px; font-family: ui-monospace, Menlo, Consolas, monospace; font-size: 15px; }
.input:focus, select:focus, textarea:focus { outline: none; border-color: var(--brand); }
.count { color: var(--muted); font-size: 13px; margin: 6px 2px; }

/* List rows */
.list { display: flex; flex-direction: column; gap: 8px; }
.row {
  display: block; text-decoration: none; color: inherit;
  background: var(--card); border-radius: 12px; padding: 10px 12px; box-shadow: var(--shadow); border: 1px solid var(--line);
}
.row:active { background: var(--brand-soft); }
.row .t { display: flex; align-items: baseline; gap: 8px; flex-wrap: wrap; }
.row .code { font-weight: 700; font-size: 16.5px; }
.row .name { color: var(--ink); }
.row .sub { color: var(--muted); font-size: 13px; margin-top: 2px; display: flex; flex-wrap: wrap; gap: 4px 8px; align-items: center; }
.badge { display: inline-block; font-size: 11.5px; font-weight: 600; padding: 1px 7px; border-radius: 999px; background: var(--chip); color: var(--muted); white-space: nowrap; }
.badge.warn { background: var(--warn-bg); color: var(--warn-ink); }
.badge.brand { background: var(--brand-soft); color: var(--brand); }
.badge.cost { background: var(--cost-bg); color: var(--cost); }
.badge.ok { color: var(--ok-ink); }
.more { display: block; width: 100%; margin-top: 10px; }
.empty { text-align: center; color: var(--muted); padding: 30px 10px; }

/* Buttons */
.btn { display: inline-flex; align-items: center; justify-content: center; gap: 6px; border: 1.5px solid var(--line); background: var(--card); border-radius: 10px; padding: 9px 14px; font-weight: 600; font-size: 15px; text-decoration: none; color: var(--ink); }
.btn.primary { background: var(--brand); border-color: var(--brand); color: #fff; }
.btn.danger { color: var(--bad-ink); }
.btn.small { padding: 5px 10px; font-size: 13.5px; border-radius: 8px; }
.btn:disabled { opacity: .5; }
.btn svg { width: 18px; height: 18px; }
.btns { display: flex; gap: 8px; flex-wrap: wrap; }

/* Cards */
.card { background: var(--card); border: 1px solid var(--line); border-radius: 14px; padding: 12px 14px; box-shadow: var(--shadow); margin-bottom: 12px; }
.card h2, .card h3 { margin: 0 0 8px; }
h1 { font-size: 22px; margin: 4px 0 2px; line-height: 1.2; }
h2 { font-size: 17px; }
h3 { font-size: 15.5px; }
.muted { color: var(--muted); }
.small { font-size: 13px; }
.meta { display: flex; flex-wrap: wrap; gap: 6px; margin: 6px 0 4px; }
.note { background: var(--warn-bg); color: var(--warn-ink); border-radius: 10px; padding: 8px 10px; font-size: 14px; margin: 8px 0; }
.note.bad { background: var(--bad-bg); color: var(--bad-ink); }
.note.info { background: var(--chip); color: var(--ink); }

/* Batch box */
.batch { position: sticky; top: calc(50px + env(safe-area-inset-top)); z-index: 9; background: var(--bg); padding: 6px 0 8px; }
.batch .inner { background: var(--card); border: 1.5px solid var(--brand); border-radius: 12px; padding: 10px 12px; }
.batch label { font-weight: 600; font-size: 14px; display: block; margin-bottom: 6px; }
.batch .line { display: flex; gap: 6px; }
.batch .line .input { flex: 1; font-size: 18px; font-weight: 600; }
.batch .line select { width: 92px; flex: none; }
.batch .quick { display: flex; gap: 6px; margin-top: 8px; overflow-x: auto; scrollbar-width: none; }
.batch .quick .chip { padding: 4px 10px; font-size: 13.5px; }

/* Formula table */
table.ft { width: 100%; border-collapse: collapse; font-variant-numeric: tabular-nums; }
.ft th { text-align: right; font-size: 12px; color: var(--muted); font-weight: 600; padding: 4px 4px 6px; border-bottom: 1px solid var(--line); }
.ft th:first-child, .ft td:first-child { text-align: left; }
.ft td { text-align: right; padding: 7px 4px; border-bottom: 1px solid var(--line); vertical-align: top; }
.ft tr:last-child td { border-bottom: 0; }
.ft td .cn { font-weight: 700; }
.ft td .cd { display: block; font-size: 12px; color: var(--muted); font-weight: 400; line-height: 1.25; }
.ft td.big { font-weight: 700; font-size: 16.5px; }
.ft td.dim { color: var(--muted); }
.ft tr.tiny td { background: var(--warn-bg); }
.ft tr.tiny td.big { color: var(--warn-ink); }
.ft tr.binder td .cn { color: var(--muted); }
.ft tfoot td { font-weight: 700; border-top: 2px solid var(--line); border-bottom: 0; }
.layerhead { display: flex; justify-content: space-between; align-items: baseline; gap: 8px; margin-bottom: 4px; }
.layerhead h3 { margin: 0; }
.costbox { margin-top: 10px; background: var(--cost-bg); color: var(--cost); border-radius: 10px; padding: 8px 10px; font-size: 14px; }
.costbox b { font-size: 16px; }
.costbox details { margin-top: 4px; }
.costbox summary { cursor: pointer; font-size: 13px; }
.costbox ul { margin: 4px 0 0; padding-left: 18px; font-size: 13px; }

/* Price cards */
.pc .t { display: flex; justify-content: space-between; gap: 8px; align-items: flex-start; }
.pc .pname { font-weight: 600; }
.pc .pcode { color: var(--muted); font-size: 13px; }
.plines { margin-top: 6px; display: grid; gap: 4px; }
.pline { display: flex; justify-content: space-between; align-items: center; gap: 8px; font-size: 14.5px; }
.pline .lbl { color: var(--muted); }
.pline .val { font-weight: 700; font-variant-numeric: tabular-nums; white-space: nowrap; }
.pline.sell .val { color: var(--brand); font-size: 17px; }
.pline.cost .val, .pline.cost .lbl { color: var(--cost); }
.pline .addb { margin-left: 6px; }
.tier { border-top: 1px dashed var(--line); padding-top: 4px; }
.tier:first-child { border-top: 0; padding-top: 0; }

/* Bill */
.billline { background: var(--card); border: 1px solid var(--line); border-radius: 12px; padding: 10px; margin-bottom: 8px; }
.billline .top { display: flex; gap: 8px; align-items: center; }
.billline .stt { flex: none; width: 26px; height: 26px; border-radius: 50%; background: var(--brand-soft); color: var(--brand); display: grid; place-items: center; font-weight: 700; font-size: 13px; }
.billline .grid { display: grid; grid-template-columns: 80px 1fr; gap: 6px; margin-top: 6px; }
.billline .amount { text-align: right; font-weight: 700; margin-top: 6px; font-variant-numeric: tabular-nums; }
.billline .del { flex: none; border: 0; background: none; color: var(--muted); padding: 4px; }
.billline .del svg { width: 20px; height: 20px; }
.fieldlbl { display: block; font-size: 12px; color: var(--muted); margin: 0 0 2px 2px; }
.totalbar { position: sticky; bottom: calc(var(--tabbar-h) + var(--bottom-space)); z-index: 8; background: var(--bg); padding: 8px 0; }
.totalbar .inner { display: flex; align-items: center; justify-content: space-between; gap: 10px; background: var(--card); border: 1.5px solid var(--brand); border-radius: 12px; padding: 10px 12px; }
.totalbar .sum { font-size: 20px; font-weight: 800; color: var(--brand); font-variant-numeric: tabular-nums; }
.preview img { width: 100%; border-radius: 10px; border: 1px solid var(--line); display: block; }

/* Markdown */
.md h1 { font-size: 20px; }
.md h2 { font-size: 17px; margin: 18px 0 6px; }
.md table { width: 100%; border-collapse: collapse; font-size: 14px; margin: 6px 0; display: block; overflow-x: auto; }
.md th, .md td { border: 1px solid var(--line); padding: 6px; text-align: left; vertical-align: top; }
.md th { background: var(--chip); }
.md ul, .md ol { padding-left: 20px; }
.md li { margin: 3px 0; }

/* Dialog */
dialog { border: 0; border-radius: 16px; padding: 0; width: min(560px, calc(100vw - 24px)); max-height: calc(100vh - 40px); background: var(--card); color: var(--ink); }
dialog::backdrop { background: rgba(0,0,0,.45); }
dialog form { padding: 16px; display: grid; gap: 10px; }
dialog h2 { margin: 0; }
dialog .fl { display: grid; gap: 3px; }
dialog .fl > span { font-size: 13px; color: var(--muted); font-weight: 600; }
dialog .two { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
dialog .acts { display: flex; gap: 8px; justify-content: flex-end; flex-wrap: wrap; margin-top: 4px; }

/* Settings */
.setrow { display: flex; justify-content: space-between; align-items: center; gap: 12px; padding: 10px 0; border-bottom: 1px solid var(--line); }
.setrow:last-child { border-bottom: 0; }
.switch { position: relative; width: 50px; height: 30px; flex: none; }
.switch input { opacity: 0; width: 0; height: 0; }
.switch span { position: absolute; inset: 0; background: var(--line); border-radius: 999px; transition: .2s; }
.switch span::after { content: ""; position: absolute; left: 3px; top: 3px; width: 24px; height: 24px; border-radius: 50%; background: #fff; transition: .2s; box-shadow: 0 1px 3px rgba(0,0,0,.3); }
.switch input:checked + span { background: var(--brand); }
.switch input:checked + span::after { transform: translateX(20px); }
.ownlist .row { display: flex; justify-content: space-between; align-items: center; gap: 8px; }

/* Trợ lý AI */
.ailog { display: flex; flex-direction: column; gap: 8px; padding-bottom: 8px; }
.bubble { max-width: 88%; padding: 9px 12px; border-radius: 16px; line-height: 1.45; word-wrap: break-word; }
.bubble.me { align-self: flex-end; background: var(--brand); color: #fff; border-bottom-right-radius: 4px; }
.bubble.bot { align-self: flex-start; background: var(--card); border: 1px solid var(--line); border-bottom-left-radius: 4px; }
.bubble.md p { margin: 0 0 6px; }
.bubble.md p:last-child { margin-bottom: 0; }
.bubble.md ul, .bubble.md ol { margin: 4px 0; padding-left: 20px; }
.bubble.md h1, .bubble.md h2, .bubble.md h3 { font-size: 15.5px; margin: 6px 0 4px; }
.bubble.md table { font-size: 13.5px; }
.bubble.typing { color: var(--muted); font-style: italic; }
.toolchip { align-self: flex-start; font-size: 12.5px; color: var(--muted); background: var(--chip); border-radius: 999px; padding: 3px 10px; }
.toolchip a { color: var(--brand); font-weight: 600; }
.aimeta { align-self: flex-start; font-size: 11.5px; color: var(--muted); margin: -4px 0 0 6px; }
.aiwelcome { background: var(--card); border: 1px solid var(--line); border-radius: 14px; padding: 12px; }
.aiwelcome p { margin: 4px 0 6px; }
.chips.wrap { flex-wrap: wrap; overflow: visible; }
.aicompose { position: sticky; bottom: calc(var(--tabbar-h) + var(--bottom-space)); z-index: 8; background: var(--bg); padding: 6px 0 8px; }
.aicompose .inner { background: var(--card); border: 1.5px solid var(--brand); border-radius: 14px; padding: 8px; display: grid; gap: 6px; }
.aicompose textarea { min-height: 52px; font-family: inherit; font-size: 16px; border: 0; padding: 4px 6px; resize: none; }
.aicompose textarea:focus { border: 0; }
.aibtns { display: flex; gap: 6px; align-items: center; }
.aibtns #ainew { margin-left: auto; }
.two-in { display: grid; grid-template-columns: repeat(3, 1fr); gap: 6px; }
.two-in .fl { display: grid; gap: 2px; min-width: 0; }
.tabbar a span { font-size: 10.5px; white-space: nowrap; }

#toast {
  position: fixed; left: 50%; bottom: calc(var(--tabbar-h) + var(--bottom-space) + 16px); transform: translateX(-50%) translateY(20px);
  background: #222; color: #fff; padding: 10px 16px; border-radius: 12px; font-size: 14.5px; opacity: 0; pointer-events: none; transition: .25s; z-index: 50;
  max-width: calc(100vw - 32px); text-align: center;
}
#toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }

/* ---------- v1.2: tìm nhanh trong bill, chữ khách, mã màu mới ---------- */
.bquick { margin-bottom: 12px; }
.bqres { display: grid; gap: 6px; margin-top: 6px; }
.bqres:empty { display: none; }
.bqrow { display: flex; align-items: center; justify-content: space-between; gap: 8px; width: 100%; text-align: left; background: var(--card); border: 1px solid var(--line); border-radius: 10px; padding: 9px 10px; box-shadow: var(--shadow); }
.bqrow:active { background: var(--brand-soft); }
.bqrow .bqn { flex: 1; min-width: 0; font-weight: 600; font-size: 14.5px; line-height: 1.3; }
.bqrow .bqp { display: flex; flex-direction: column; align-items: flex-end; gap: 2px; color: var(--brand); font-weight: 700; white-space: nowrap; font-size: 14.5px; }
.bqrow.new { border-style: dashed; color: var(--ink); }
.bqrow.new svg { width: 18px; height: 18px; flex: none; color: var(--brand); }
.bqrow.new .muted { white-space: nowrap; }
.bfree { margin-top: 8px; background: var(--card); border: 1px solid var(--line); border-radius: 12px; padding: 8px 12px; }
.bfree summary { font-weight: 600; padding: 4px 0; cursor: pointer; }
.bfree[open] summary { margin-bottom: 8px; }
.bfree textarea { min-height: 100px; font-family: inherit; margin-bottom: 8px; }
.bfree .muted { margin-top: 6px; }
.rv { border: 1px solid var(--line); border-radius: 10px; padding: 8px; margin: 8px 0; display: grid; grid-template-columns: 1fr 70px; gap: 6px; align-items: end; }
.rv .rvh { grid-column: 1 / -1; display: flex; gap: 6px; align-items: center; justify-content: space-between; font-size: 13.5px; }
.rv .rvseg { color: var(--muted); font-style: italic; min-width: 0; overflow-wrap: anywhere; }
.rv .rvsel { grid-column: 1; }
.rv .rvsl { grid-column: 2; grid-row: 2; display: grid; gap: 2px; font-size: 12px; color: var(--muted); }
.rv .rvnew { grid-column: 1 / -1; display: grid; grid-template-columns: 1fr 120px; gap: 6px; }
.rv .rvnew[hidden] { display: none; }
.ctadd { margin: 2px 0 4px; }
.ctadd .btn svg { width: 16px; height: 16px; }
.btn.busy { opacity: .6; pointer-events: none; }
.bdrow { display: flex; gap: 8px; align-items: flex-start; padding: 6px 0; border-bottom: 1px solid var(--line); font-size: 14px; }
.rv.skip { opacity: .6; }
