:root {
  --bg: #f5f5f5; --panel: #ffffff; --ink: #111111; --muted: #595959; --line: #d9d9d9;
  --brand: #111111; --brand-ink: #ffffff; --accent: #111111; --accent-ink: #ffffff; --soft: #ececec;
  --good: #111111; --bad: #b3261e; --warn: #111111; --info: #111111;
  --good-bg: #ececec; --bad-bg: #fbe9e7; --warn-bg: #ececec; --info-bg: #ececec; --mute-bg: #ececec;
  --radius: 10px;
  font-family: -apple-system, BlinkMacSystemFont, "SF Pro Text", "Segoe UI", system-ui, Roboto, "Helvetica Neue", Arial, sans-serif;
  font-size: 15px;
}
* { box-sizing: border-box; }
html, body { margin: 0; background: var(--bg); color: var(--ink); }
html { -webkit-text-size-adjust: 100%; text-size-adjust: 100%; }
a, button, input, select, textarea, label { -webkit-tap-highlight-color: transparent; touch-action: manipulation; }
body { min-height: 100dvh; line-height: 1.45; }
h1 { font-size: 1.35rem; margin: 0; line-height: 1.25; }
h2 { font-size: 1.02rem; margin: 0 0 .7rem; }
h3 { font-size: .92rem; margin: 0 0 .4rem; }
a { color: var(--accent); }
p { margin: 0 0 .5rem; }
.num { font-variant-numeric: tabular-nums; text-align: right; white-space: nowrap; }
.muted { color: var(--muted); }
.small { font-size: .85rem; }
.neg { color: var(--bad); } .pos { color: var(--good); }
.hide { display: none !important; }

/* Bố cục */
.shell { display: grid; grid-template-columns: 216px minmax(0, 1fr); min-height: 100dvh; }
.side { background: var(--brand); color: var(--brand-ink); padding: 16px 10px; position: sticky; top: 0; height: 100dvh; overflow-y: auto; display: flex; flex-direction: column; }
.side .brand { font-weight: 700; padding: 4px 10px 14px; font-size: 1.05rem; }
.side a { display: block; color: inherit; text-decoration: none; padding: 9px 10px; border-radius: 8px; opacity: .88; }
.side a:hover { background: rgba(255,255,255,.08); opacity: 1; }
.side a.on { background: rgba(255,255,255,.16); opacity: 1; font-weight: 600; }
.side .who { margin-top: auto; padding: 12px 10px 0; font-size: .85rem; opacity: .85; }
main { padding: 22px 26px 60px; min-width: 0; max-width: 1180px; width: 100%; }
.head { display: flex; flex-wrap: wrap; gap: 10px; align-items: center; justify-content: space-between; margin-bottom: 16px; }
.actions { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; }
.tabbar { display: none; }

/* Khối */
.card { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); padding: 16px; margin-bottom: 14px; }
.grid { display: grid; gap: 12px; grid-template-columns: repeat(auto-fit, minmax(170px, 1fr)); margin-bottom: 14px; }
.grid.two { grid-template-columns: repeat(auto-fit, minmax(300px, 1fr)); }
.kpi { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); padding: 13px 15px; min-width: 0; text-decoration: none; color: inherit; display: block; }
a.kpi:hover { border-color: var(--accent); }
.kpi b { display: block; font-size: 1.28rem; font-variant-numeric: tabular-nums; margin-top: 2px; overflow-wrap: anywhere; }
.kpi span { color: var(--muted); font-size: .85rem; }
.kpi small { display: block; color: var(--muted); margin-top: 2px; }
.note { padding: 10px 13px; border-radius: 8px; background: var(--warn-bg); color: var(--ink); margin-bottom: 14px; font-size: .92rem; }
.note.info { background: var(--info-bg); }
.note.bad { background: var(--bad-bg); }

/* Bảng */
.table-wrap { overflow-x: auto; }
table { width: 100%; border-collapse: collapse; font-size: .93rem; }
th { text-align: left; font-weight: 600; color: var(--muted); font-size: .82rem; padding: 7px 9px; border-bottom: 1px solid var(--line); white-space: nowrap; }
th.num { text-align: right; }
td { padding: 9px; border-bottom: 1px solid var(--line); vertical-align: top; overflow-wrap: anywhere; }
tr:last-child td { border-bottom: 0; }
tr.total td { font-weight: 700; border-top: 2px solid var(--line); }
.clickrow { cursor: pointer; }
.clickrow:hover td { background: var(--soft); }

/* Điều khiển */
button, .btn { font: inherit; border: 1px solid var(--line); background: var(--panel); color: var(--ink); padding: 8px 13px; border-radius: 8px; cursor: pointer; text-decoration: none; display: inline-block; min-height: 38px; }
button:hover, .btn:hover { border-color: var(--accent); }
button.primary, .btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); font-weight: 600; }
button.danger { color: var(--bad); }
button.mini { padding: 3px 9px; min-height: 30px; font-size: .85rem; }
.btn.mini-link { padding: 3px 9px; min-height: 30px; font-size: .85rem; }
button:disabled { opacity: .55; cursor: not-allowed; }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
input, select, textarea { font: inherit; width: 100%; padding: 8px 10px; border: 1px solid var(--line); border-radius: 8px; background: var(--panel); color: var(--ink); min-height: 38px; }
label { display: block; font-size: .87rem; color: var(--muted); margin-bottom: 10px; }
label > input, label > select, label > textarea { margin-top: 3px; color: var(--ink); font-size: 1rem; }
label.check { display: flex; gap: 8px; align-items: center; color: var(--ink); }
label.check input { width: auto; min-height: 0; margin: 0; }
.form-row { display: grid; grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); gap: 0 10px; }
.hint { display: block; font-size: .8rem; margin-top: 3px; color: var(--muted); }
.chips { display: flex; flex-wrap: wrap; gap: 6px; margin-bottom: 12px; }
.chip { border-radius: 999px; padding: 5px 12px; min-height: 34px; font-size: .88rem; background: var(--panel); }
.chip.on { background: var(--brand); color: var(--brand-ink); border-color: var(--brand); }
.search { max-width: 340px; flex: 1 1 200px; }
.tag { display: inline-block; padding: 1px 8px; border-radius: 999px; font-size: .78rem; white-space: nowrap; background: var(--mute-bg); }
.tag.ton, .tag.good { background: var(--good-bg); color: var(--good); }
.tag.dat, .tag.info { background: var(--info-bg); color: var(--info); }
.tag.giu, .tag.warn { background: var(--warn-bg); color: var(--warn); }
.tag.loi, .tag.bad { background: var(--bad-bg); color: var(--bad); }
dl.kv { display: grid; grid-template-columns: auto 1fr; gap: 3px 16px; margin: 0; }
dl.kv dt { color: var(--muted); } dl.kv dd { margin: 0; text-align: right; font-variant-numeric: tabular-nums; }
dl.kv .sum { font-weight: 700; border-top: 1px solid var(--line); padding-top: 5px; margin-top: 3px; }
.empty { color: var(--muted); padding: 22px 8px; text-align: center; }

/* Hộp thoại */
dialog { border: 1px solid var(--line); border-radius: 12px; background: var(--panel); color: var(--ink); padding: 18px; width: min(560px, calc(100vw - 24px)); max-height: calc(100dvh - 24px); }
dialog::backdrop { background: rgba(0,0,0,.45); }
dialog h2 { font-size: 1.1rem; }
.dlg-foot { display: flex; justify-content: flex-end; gap: 8px; margin-top: 6px; }
.err { color: var(--bad); font-size: .9rem; margin-bottom: 8px; }
#toast { position: fixed; left: 50%; bottom: 84px; transform: translateX(-50%); background: var(--brand); color: var(--brand-ink); padding: 9px 16px; border-radius: 8px; opacity: 0; pointer-events: none; transition: opacity .18s; z-index: 50; max-width: calc(100vw - 32px); }
#toast.show { opacity: 1; }

/* Đăng nhập */
.login { min-height: 100dvh; display: grid; place-items: center; padding: 16px; }
.login form { width: min(380px, 100%); background: var(--panel); border: 1px solid var(--line); border-radius: 14px; padding: 24px; }
.login h1 { margin-bottom: 4px; }
.login button { width: 100%; margin-top: 4px; }

/* Tạo đơn */
.pick { max-height: 340px; overflow-y: auto; border: 1px solid var(--line); border-radius: 8px; margin-top: 8px; }
.pick button { display: flex; justify-content: space-between; gap: 12px; width: 100%; text-align: left; border: 0; border-bottom: 1px solid var(--line); border-radius: 0; background: transparent; }
.pick button:hover { background: var(--soft); }
.pick button:last-child { border-bottom: 0; }
.line { display: grid; grid-template-columns: minmax(0, 1fr) 130px 34px; gap: 8px; align-items: center; padding: 8px 0; border-bottom: 1px solid var(--line); }
.line input { min-height: 34px; text-align: right; }
.line input.line-sub { text-align: left; grid-column: 1 / -1; font-size: .9rem; }

/* Danh mục */
.chips.cats .chip { font-size: .84rem; min-height: 32px; padding: 4px 10px; }

.chip-label { align-self: center; font-size: .82rem; color: var(--muted); margin-right: 2px; flex: none; }
.taglist { display: flex; flex-wrap: wrap; gap: 4px; margin-top: 4px; }
.tagbtn { border: 0; background: var(--info-bg); color: var(--info); border-radius: 999px; padding: 1px 8px; font-size: .76rem; min-height: 22px; font-weight: 400; }
.tagbtn:hover { text-decoration: underline; }

.sortsel { width: auto; max-width: 220px; }
dl.kv.detail { margin: 8px 0 12px; }
dl.kv.detail dd { overflow-wrap: anywhere; }
.rline { border: 1px solid var(--line); border-radius: 8px; padding: 10px 12px; margin-bottom: 10px; }
.rline-head { display: flex; justify-content: space-between; align-items: center; margin-bottom: 6px; }
.print-only { display: none; }

.hist-head { margin-top: 12px; }
.hist { max-height: 220px; overflow-y: auto; border-top: 1px solid var(--line); }
.hist-row { padding: 6px 0; border-bottom: 1px solid var(--line); font-size: .9rem; overflow-wrap: anywhere; }

/* Xây dựng cấu hình */
.build-actions { margin-bottom: 12px; }
.slots { border-top: 1px solid var(--line); }
.slot { padding: 8px 10px; border-bottom: 1px solid var(--line); }
.slot:nth-child(even) { background: var(--soft); }
.slot-head { display: flex; align-items: center; gap: 12px; min-height: 36px; }
.slot-name { width: 170px; flex: none; font-weight: 600; font-size: .92rem; }
.slot-add { width: 30px; height: 30px; min-height: 30px; padding: 0; background: var(--accent); border-color: var(--accent); color: var(--accent-ink); font-size: 1.15rem; font-weight: 700; line-height: 1; border-radius: 6px; }
.slot-line { display: grid; grid-template-columns: minmax(0, 1fr) 130px 34px; gap: 8px; align-items: center; padding: 7px 0 3px; margin-left: 182px; }
.slot-line input { min-height: 34px; text-align: right; }
.slot-info { min-width: 0; overflow-wrap: anywhere; }
.build-total { display: flex; flex-wrap: wrap; gap: 8px 28px; justify-content: flex-end; padding-top: 14px; text-align: right; }
.build-total b { display: block; font-size: 1.25rem; font-variant-numeric: tabular-nums; }

/* Hóa đơn */
.invoice { background: #fff; color: #111; padding: 28px; margin: 0 auto; border: 1px solid var(--line); }
.invoice.a4 { max-width: 760px; }
.invoice.k80 { width: 302px; padding: 10px; font-size: 12px; }
.invoice h1 { font-size: 1.3em; text-align: center; margin: 12px 0; color: #111; }
.invoice table { font-size: 1em; }
.invoice th, .invoice td { border-bottom: 1px solid #ccc; color: #111; padding: 5px 4px; }
.invoice .shop { text-align: center; }
.invoice .sign { display: grid; grid-template-columns: 1fr 1fr; text-align: center; margin-top: 26px; min-height: 80px; }
.invoice.k80 .sign { display: none; }
.invoice .qr { text-align: center; margin-top: 12px; }
.invoice .qr img { width: 160px; height: auto; }
.invoice .totals { margin: 10px 0 0 auto; max-width: 280px; }
.invoice dl.kv dt { color: #333; }
.invoice.k80 .totals { max-width: none; }
@media print {
  :root { --bg: #fff !important; --panel: #fff !important; --ink: #111 !important; --muted: #444 !important; --line: #bbb !important; --neg: #111; color-scheme: light !important; }
  .print-only { display: block; }
  .print-only.sign { display: grid; grid-template-columns: 1fr 1fr; text-align: center; margin-top: 26px; min-height: 90px; }
  .statement { border: 0; padding: 0; }
  .statement h1 { text-align: center; margin: 10px 0; }
  .no-print, .side, .tabbar, #toast { display: none !important; }
  html, body { background: #fff; }
  .shell { display: block; } main { padding: 0; max-width: none; }
  .invoice { border: 0; padding: 0; }
  @page { margin: 10mm; }
}

/* Điện thoại */
@media (max-width: 760px) {
  .shell { display: block; padding-bottom: calc(64px + env(safe-area-inset-bottom)); }
  .side { display: none; }
  main { padding: 14px 16px 30px; }
  .tabbar { display: grid; grid-template-columns: repeat(5, 1fr); position: fixed; left: 0; right: 0; bottom: 0; background: var(--brand); padding-bottom: env(safe-area-inset-bottom); z-index: 20; }
  .tabbar a, .tabbar button { color: var(--brand-ink); text-decoration: none; text-align: center; font-size: .78rem; padding: 12px 2px; background: none; border: 0; border-radius: 0; min-height: 48px; opacity: .8; }
  .tabbar a, .tabbar button { display: flex; align-items: center; justify-content: center; line-height: 1.2; }
  .tabbar a.on { opacity: 1; font-weight: 700; background: rgba(255,255,255,.14); }
  .menu-sheet a { display: block; padding: 12px 6px; border-bottom: 1px solid var(--line); text-decoration: none; color: var(--ink); }
  .grid { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .grid.two { grid-template-columns: minmax(0, 1fr); }
  .kpi b { font-size: 1.08rem; }
  .card { padding: 13px; }
  /* Bảng thành thẻ để không phải cuộn ngang */
  table.cards thead { display: none; }
  table.cards, table.cards tbody, table.cards tr, table.cards td { display: block; width: 100%; }
  table.cards tr { border-bottom: 1px solid var(--line); padding: 9px 0; }
  table.cards tr:last-child { border-bottom: 0; }
  table.cards td { border: 0; padding: 2px 0; display: flex; justify-content: space-between; gap: 14px; text-align: right; }
  table.cards td::before { content: attr(data-l); color: var(--muted); font-size: .85rem; text-align: left; flex: none; }
  table.cards td.title { font-weight: 600; text-align: left; }
  table.cards td.title::before { content: none; }
  table.cards td:empty { display: none; }
  /* iPhone tự phóng to khi chạm ô nhập có chữ nhỏ hơn 16px */
  input, select, textarea, label > input, label > select, label > textarea, .line input.line-sub { font-size: 16px; }
  .search { max-width: none; flex-basis: 100%; }
  .chips.cats { flex-wrap: nowrap; overflow-x: auto; padding-bottom: 4px; }
  .chips.cats .chip { flex: none; }
  .slot-name { width: auto; flex: 1; }
  .slot-line { margin-left: 0; grid-template-columns: minmax(0, 1fr) 110px 34px; }
  .build-actions button { flex: 1 1 45%; }
  .line { grid-template-columns: minmax(0, 1fr) 110px 44px; }

  /* Chuẩn iPhone: vùng chạm tối thiểu 44 điểm, tránh tai thỏ và thanh Home */
  .slot-line { grid-template-columns: minmax(0, 1fr) 110px 44px; }
  button, .btn, button.mini, .btn.mini-link, .chip, .chips.cats .chip, .slot-add { min-height: 44px; }
  button.mini, .slot-add { min-width: 44px; }
  input, select, textarea, .line input, .slot-line input { min-height: 44px; }
  input[type="date"] { -webkit-appearance: none; appearance: none; display: block; }
  select { -webkit-appearance: menulist; }
  label.check input { min-height: 0; width: 22px; height: 22px; }
  main { padding: calc(14px + env(safe-area-inset-top)) calc(16px + env(safe-area-inset-right)) 30px calc(16px + env(safe-area-inset-left)); }
  .tabbar { padding-left: env(safe-area-inset-left); padding-right: env(safe-area-inset-right); }
  #toast { bottom: calc(72px + env(safe-area-inset-bottom)); max-width: calc(100vw - 32px); }
  dialog { max-height: calc(100dvh - 24px - env(safe-area-inset-top) - env(safe-area-inset-bottom)); }
  .login { padding-top: calc(16px + env(safe-area-inset-top)); padding-bottom: calc(16px + env(safe-area-inset-bottom)); }
}
.theme-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(140px, 1fr)); gap: 8px; }
.theme-opt { display: flex; align-items: center; gap: 10px; min-height: 44px; justify-content: flex-start; }
.theme-opt.on { border: 2px solid var(--ink); font-weight: 700; }
.swatch { width: 22px; height: 22px; border-radius: 50%; border: 1px solid var(--line); flex: none; }
.line-qty { grid-column: 1 / -1; display: flex; align-items: flex-end; justify-content: space-between; gap: 12px; }
.line-qty label { flex: 0 1 170px; font-size: .85rem; color: var(--muted); }
.line-qty input { text-align: right; }
.slot-qty { grid-column: 1 / -1; font-size: .85rem; color: var(--muted); }
