/* ==========================================================================
   Local-First POS — shared styles (POS + Owner Dashboard)
   Reading-first minimalism (Information Architects):
   content hierarchy, system fonts, paper surface, one accent, no decoration.
   UX constraints from the brief: font >= 18px, touch targets >= 60px.
   ========================================================================== */

:root {
  --bg: #FAFAF9;
  --surface: #FFFFFF;
  --ink: #1A1A1A;
  --muted: #6B6B6B;
  --line: #E5E5E4;
  --accent: #1ECD18;          /* accent (user set in preview) */
  --danger: #B3261E;
  --ok: #1E6B3C;
  --radius: 10px;
  --font: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  --font-ar: system-ui, -apple-system, "Segoe UI", Tahoma, Arial, sans-serif;
}

* { box-sizing: border-box; }
html { font-size: 18px; }
html[dir="rtl"] body { font-family: var(--font-ar); }

body {
  margin: 0;
  font-family: var(--font);
  font-size: 1rem;
  line-height: 1.5;
  color: var(--ink);
  background: var(--bg);
  min-height: 100vh;
}

h1, h2, h3 { line-height: 1.2; margin: 0 0 .5rem; }
h1 { font-size: 1.35rem; }
h2 { font-size: 1.15rem; }
h3 { font-size: 1rem; }
p { margin: 0 0 .75rem; }
a { color: var(--accent); }
a:visited { color: #551A8B; }
small { color: var(--muted); }

button {
  font: inherit;
  color: inherit;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  min-height: 60px;                 /* brief: buttons >= 60px */
  padding: .5rem 1rem;
  cursor: pointer;
  transition: background .12s ease, border-color .12s ease;
}
button:hover { background: #F3F2F1; }
button:active { background: #E9E8E6; }
button:focus-visible { outline: 3px solid var(--accent); outline-offset: 2px; }
button:disabled { opacity: .45; cursor: not-allowed; }

.btn-primary {
  background: var(--ink);
  color: #fff;
  border-color: var(--ink);
  font-weight: 600;
}
.btn-primary:hover { background: #000; }
.btn-ghost { background: transparent; border-color: transparent; color: var(--accent); }
.btn-danger { border-color: var(--danger); color: var(--danger); }
.btn-small { min-height: 44px; padding: .35rem .8rem; font-size: .9rem; }

input[type="text"], input[type="number"], input[type="tel"], input[type="search"], select, textarea {
  font: inherit;
  color: var(--ink);
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: .6rem .8rem;
  min-height: 60px;                 /* brief: touch targets */
  width: 100%;
}
input:focus-visible, textarea:focus-visible { outline: 3px solid var(--accent); outline-offset: 1px; }
label { font-size: .9rem; font-weight: 600; display: block; margin: .8rem 0 .3rem; }
.hint { font-size: .85rem; color: var(--muted); }

/* ---------- header ---------- */
.topbar {
  display: flex;
  align-items: center;
  gap: .8rem;
  flex-wrap: wrap;
  padding: .7rem 1.2rem;
  background: var(--surface);
  border-bottom: 1px solid var(--line);
  position: sticky;
  top: 0;
  z-index: 20;
}
.topbar .brand { font-weight: 700; font-size: 1.05rem; }
.topbar .spacer { flex: 1; }
.topbar button { min-height: 44px; }

/* ---------- layout ---------- */
.layout {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 400px;
  gap: 1.2rem;
  padding: 1.2rem;
  max-width: 1400px;
  margin: 0 auto;
}
.panel {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 1rem;
}
@media (max-width: 900px) {
  .layout { grid-template-columns: 1fr; }
}

/* ---------- item grid (Visual POS) ---------- */
.searchbar { display: flex; gap: .6rem; margin-bottom: .8rem; }
.searchbar input { flex: 1; }
.chips { display: flex; gap: .5rem; flex-wrap: wrap; margin-bottom: .9rem; }
.chips button { min-height: 44px; padding: .3rem .9rem; font-size: .9rem; }
.chips button.active { background: var(--ink); color: #fff; border-color: var(--ink); }

.grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(150px, 1fr));
  gap: .7rem;
}
.item-btn {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: .35rem;
  padding: .6rem .5rem;
  min-height: 150px;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
}
.item-btn img, .item-btn .ph { width: 72px; height: 72px; border-radius: 10px; object-fit: cover; }
.item-btn .ph {
  display: flex; align-items: center; justify-content: center;
  background: #F0EFED; font-size: 2rem;
}
.item-btn .nm { font-size: .9rem; font-weight: 600; text-align: center; line-height: 1.25; }
.item-btn .pr { font-size: .9rem; color: var(--muted); }
.item-btn .stk { font-size: .75rem; color: var(--muted); }
.item-btn .stk.low { color: var(--danger); font-weight: 700; }
.item-btn:active { transform: scale(.98); }

/* ---------- cart ---------- */
.cart { display: flex; flex-direction: column; min-height: 60vh; }
.cart-head { display: flex; justify-content: space-between; align-items: baseline; }
.cart-list { flex: 1; overflow: auto; max-height: 52vh; margin: .6rem 0; }
.cart-line {
  display: flex; align-items: center; gap: .6rem;
  padding: .5rem .4rem;
  border-bottom: 1px solid var(--line);
  min-height: 60px;
}
.cart-line .nm { flex: 1; font-weight: 600; font-size: .95rem; }
.cart-line .qty { width: 3.2rem; text-align: center; font-weight: 700; }
.cart-line .am { min-width: 6.5rem; text-align: end; }
.cart-line .x { min-height: 44px; min-width: 44px; padding: 0; border: none; color: var(--muted); font-size: 1.2rem; }
.totals { border-top: 2px solid var(--ink); padding-top: .6rem; }
.totals .row { display: flex; justify-content: space-between; padding: .2rem 0; }
.totals .row.total { font-weight: 700; font-size: 1.2rem; border-top: 1px solid var(--line); margin-top: .3rem; padding-top: .5rem; }
.cart-actions { display: flex; flex-direction: column; gap: .6rem; margin-top: .8rem; }
.cart-actions .row2 { display: flex; gap: .6rem; }
.cart-actions .row2 button { flex: 1; }
.errbanner {
  background: #FDEAE8; color: var(--danger); border: 1px solid var(--danger);
  border-radius: var(--radius); padding: .6rem .8rem; margin-bottom: .6rem; font-weight: 600;
}
.emptybox {
  border: 1px dashed var(--line); border-radius: var(--radius);
  padding: 2rem 1rem; text-align: center; color: var(--muted); margin: .8rem 0;
}
.emptybox button { margin-top: .6rem; }

/* ---------- modal ---------- */
.overlay {
  position: fixed; inset: 0;
  background: rgba(26, 26, 26, .45);
  display: none; align-items: center; justify-content: center;
  padding: 1rem; z-index: 50;
}
.overlay.open { display: flex; }
.modal {
  background: var(--surface);
  border-radius: 14px;
  width: 100%; max-width: 560px;
  max-height: 88vh; overflow: auto;
  padding: 1.2rem;
}
.modal.wide { max-width: 720px; }
.modal-head { display: flex; justify-content: space-between; align-items: center; margin-bottom: .6rem; }
.modal-head h2 { margin: 0; }
.modal-head button { min-height: 44px; min-width: 44px; }

/* searchable list (replaces dropdowns — brief UX rule) */
.picklist { max-height: 40vh; overflow: auto; border: 1px solid var(--line); border-radius: var(--radius); }
.picklist button {
  display: flex; justify-content: space-between; align-items: center; gap: .6rem;
  width: 100%; min-height: 60px; border: none; border-bottom: 1px solid var(--line);
  border-radius: 0; text-align: start;
}
.picklist button:last-child { border-bottom: none; }
.picklist .sub { font-size: .8rem; color: var(--muted); }

/* ---------- receipt (80mm thermal, traditional KAGATIA TRADING header) ---------- */
.receipt {
  width: 300px; margin: 0 auto;
  font-family: ui-monospace, "SF Mono", Menlo, Consolas, monospace;
  font-size: 12px; line-height: 1.45; color: #000; background: #fff;
  padding: 10px 8px;
}
.receipt .center { text-align: center; }
.receipt .hr { border-top: 1px dashed #000; margin: 6px 0; }
.receipt .hr-solid { border-top: 1px solid #000; margin: 7px 0 5px; }
.receipt table { width: 100%; border-collapse: collapse; }
.receipt td { vertical-align: top; }
.receipt td.r { text-align: right; }
.receipt .tot { font-weight: 700; }
.receipt .big { font-size: 14px; font-weight: 700; }

/* --- traditional header (reused from the shop's own invoice) --- */
.inv-ar { text-align: center; font-size: 17px; font-weight: 700; line-height: 1.3; }
.inv-en { text-align: center; font-size: 14px; font-weight: 800; letter-spacing: .06em; text-transform: uppercase; }
.inv-slogan {
  color: #000; text-align: center;
  font-size: 9px; font-weight: 700; letter-spacing: .04em; text-transform: uppercase;
  border-top: 1px solid #000; border-bottom: 1px solid #000;
  padding: 2px 4px; margin: 5px 0 4px;
}
.inv-contact { text-align: center; font-size: 10px; letter-spacing: .02em; }
.inv-meta { display: flex; align-items: baseline; gap: .5rem; flex-wrap: wrap; margin-top: 6px; }
.inv-no { font-size: 13px; font-weight: 700; }
.inv-no b { color: #000; }
.inv-date { font-size: 10px; margin-left: auto; }
.inv-type { background: #fff; color: #000; font-size: 8px; font-weight: 700; letter-spacing: .08em; padding: 1px 5px; border: 1px solid #000; }
.inv-cust { display: flex; gap: .3rem; align-items: baseline; font-size: 11px; margin-top: 3px; }
.inv-cust .dots { flex: 1; border-bottom: 1px dotted #000; min-width: 40px; }
.inv-cust .cust-val { font-weight: 700; }
/* --- items table (thick border, dotted rows, thin dividers) --- */
.inv-items { width: 100%; border-collapse: collapse; border: 1px solid #000; margin-top: 6px; }
.inv-items th {
  border-bottom: 1px solid #000; border-right: 1px solid #000;
  font-size: 9px; text-transform: uppercase; letter-spacing: .04em;
  padding: 3px 4px; text-align: left;
}
.inv-items th:last-child { border-right: none; }
.inv-items td {
  border-bottom: 1px dotted #000; border-right: 1px solid #000;
  padding: 3px 4px; font-size: 11px; vertical-align: top;
}
.inv-items td:last-child { border-right: none; }
.inv-items tr:last-child td { border-bottom: none; }
.inv-items .num { text-align: right; white-space: nowrap; }
/* --- totals --- */
.inv-tot { width: 100%; border-collapse: collapse; margin-top: 6px; }
.inv-tot td { padding: 3px 2px; font-size: 12px; }
.inv-tot .r { text-align: right; }
.inv-tot .tot td { font-weight: 800; border-top: 1px solid #000; font-size: 13px; }
/* --- signatures + thanks --- */
.inv-sign { display: flex; justify-content: space-between; gap: 1rem; margin-top: 16px; font-size: 10px; }
.inv-sign span { display: inline-block; min-width: 96px; }
.inv-sign .dots { border-bottom: 1px dotted #000; display: block; height: 16px; margin-bottom: 2px; }
.inv-thx { text-align: center; margin-top: 8px; font-size: 11px; }

/* RTL receipt */
[dir="rtl"] .inv-date { margin-left: 0; margin-right: auto; }
[dir="rtl"] .inv-items th, [dir="rtl"] .inv-items td { text-align: right; }
[dir="rtl"] .inv-items .num, [dir="rtl"] .inv-tot .r { text-align: left; }

/* --- VAT optional toggle (cart) --- */
.vat-row { display: grid; grid-template-columns: 1fr auto auto; gap: .6rem; align-items: center; }
.switch {
  position: relative; width: 52px; height: 30px; padding: 0;
  border-radius: 999px; border: 1px solid var(--line); background: var(--line);
  cursor: pointer; min-height: 30px; transition: background .15s ease;
}
.switch .knob {
  position: absolute; top: 2px; left: 2px; width: 24px; height: 24px;
  border-radius: 50%; background: #fff; box-shadow: 0 1px 3px rgba(0,0,0,.3);
  transition: transform .15s ease;
}
.switch.on { background: var(--accent); border-color: var(--accent); }
.switch.on .knob { transform: translateX(22px); }
.switch:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
[dir="rtl"] .switch .knob { left: auto; right: 2px; }
[dir="rtl"] .switch.on .knob { transform: translateX(-22px); }

/* ---------- tables (dashboard) ---------- */
table.tbl { width: 100%; border-collapse: collapse; font-size: .9rem; }
.tbl th, .tbl td { text-align: start; padding: .5rem .4rem; border-bottom: 1px solid var(--line); }
.tbl th { font-size: .8rem; text-transform: uppercase; letter-spacing: .04em; color: var(--muted); }
.tbl td.num, .tbl th.num { text-align: end; }
.kpis { display: grid; grid-template-columns: repeat(auto-fit, minmax(170px, 1fr)); gap: .7rem; margin-bottom: 1rem; }
.kpi { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: .8rem 1rem; }
.kpi .v { font-size: 1.3rem; font-weight: 700; }
.kpi .l { font-size: .8rem; color: var(--muted); }

/* ---------- toast ---------- */
.toast-wrap { position: fixed; bottom: 1.2rem; left: 50%; transform: translateX(-50%); z-index: 90; display: flex; flex-direction: column; gap: .5rem; align-items: center; }
.toast {
  background: var(--ink); color: #fff; border-radius: 999px;
  padding: .6rem 1.1rem; font-size: .95rem;
  display: flex; align-items: center; gap: .8rem;
  box-shadow: 0 2px 10px rgba(0,0,0,.18);
  animation: rise .18s ease;
}
.toast.err { background: var(--danger); }
.toast button { min-height: 40px; padding: .3rem .8rem; color: var(--ink); background: #fff; }
@keyframes rise { from { opacity: 0; transform: translateY(8px); } to { opacity: 1; transform: none; } }

/* ---------- camera scanner ---------- */
.scan-stage {
  position: relative;
  border-radius: var(--radius);
  overflow: hidden;
  background: #000;
  aspect-ratio: 4 / 3;
}
.scan-stage video { width: 100%; height: 100%; object-fit: cover; display: block; }
.scan-focus {
  position: absolute; inset: 18%;
  border: 2px solid rgba(255,255,255,.85);
  border-radius: 12px;
  pointer-events: none;
}
.scan-focus::after {
  content: ''; position: absolute; left: 0; right: 0; top: 50%;
  height: 2px; background: rgba(0,238,255,.9); box-shadow: 0 0 8px rgba(0,238,255,.8);
}
.scan-hint {
  position: absolute; left: 0; right: 0; bottom: 0;
  padding: .4rem; text-align: center; color: #fff;
  background: rgba(0,0,0,.55); font-size: .85rem;
}
#scanTorch.on { background: var(--ink); color: #fff; border-color: var(--ink); }

/* ---------- print: 80mm thermal, only the receipt ---------- */
@media print {
  @page { size: 80mm auto; margin: 0; }
  html, body { background: #fff !important; }
  body * { visibility: hidden; }
  .receipt, .receipt * { visibility: visible; }
  .receipt {
    position: absolute; left: 0; top: 0;
    width: 72mm; margin: 0; padding: 2mm 3mm;
    box-shadow: none;
    -webkit-print-color-adjust: exact;
    print-color-adjust: exact;
  }
}

/* ==========================================================================
   POS Assistant (chat) + customer phone at checkout
   ========================================================================== */
.cust-phone { display: block; margin: .5rem 0 .25rem; font-size: .85rem; color: var(--muted); }
.cust-phone-input {
  width: 100%; padding: .6rem .7rem; font-size: 1rem; border: 1px solid var(--line);
  border-radius: var(--radius); background: var(--surface); color: var(--ink);
}
.cust-phone-input:focus { outline: 2px solid var(--accent); outline-offset: 1px; }

#chatRoot { position: fixed; inset: auto; z-index: 1000; pointer-events: none; }
.chat-fab {
  position: fixed; right: 1rem; bottom: 1rem; width: 3.5rem; height: 3.5rem;
  border-radius: 50%; border: none; font-size: 1.6rem; cursor: pointer;
  background: var(--accent); color: #fff; box-shadow: 0 4px 14px rgba(0,0,0,.22);
  pointer-events: auto; transition: transform .15s ease, opacity .15s ease;
}
html[dir="rtl"] .chat-fab { right: auto; left: 1rem; }
.chat-fab:active { transform: scale(.94); }
.chat-fab.hidden { opacity: 0; transform: scale(.6); pointer-events: none; }

.chat-panel {
  position: fixed; right: 1rem; bottom: 1rem; width: min(400px, calc(100vw - 1rem));
  height: min(560px, calc(100vh - 2rem)); display: flex; flex-direction: column;
  background: var(--surface); border: 1px solid var(--line); border-radius: 16px;
  box-shadow: 0 12px 40px rgba(0,0,0,.24); overflow: hidden;
  pointer-events: none; opacity: 0; transform: translateY(14px) scale(.98);
  transition: opacity .18s ease, transform .18s ease;
}
html[dir="rtl"] .chat-panel { right: auto; left: 1rem; }
.chat-panel.open { opacity: 1; transform: none; pointer-events: auto; }

.chat-head {
  display: flex; align-items: center; gap: .5rem; padding: .7rem .9rem;
  background: var(--ink); color: #fff;
}
.chat-title { font-weight: 600; font-size: 1rem; flex: 1; }
.chat-role { font-size: .8rem; opacity: .85; }
.chat-close { background: none; border: none; color: #fff; font-size: 1rem; cursor: pointer; padding: .2rem .4rem; }

.chat-body { flex: 1; overflow-y: auto; padding: .8rem; display: flex; flex-direction: column; gap: .5rem; }
.chat-row { display: flex; }
.chat-row.user { justify-content: flex-end; }
.chat-row.bot { justify-content: flex-start; }
.chat-bubble {
  max-width: 85%; padding: .55rem .75rem; border-radius: 12px; font-size: .95rem; line-height: 1.45;
  white-space: pre-line; word-break: break-word;
}
.chat-row.user .chat-bubble { background: var(--accent); color: #fff; border-bottom-right-radius: 4px; }
.chat-row.bot .chat-bubble { background: var(--bg); border: 1px solid var(--line); border-bottom-left-radius: 4px; }
html[dir="rtl"] .chat-row.user .chat-bubble { border-radius: 12px; border-bottom-left-radius: 4px; }
html[dir="rtl"] .chat-row.bot .chat-bubble { border-radius: 12px; border-bottom-right-radius: 4px; }

.chat-chips { display: flex; flex-wrap: wrap; gap: .4rem; padding: .4rem .8rem; border-top: 1px solid var(--line); }
.chat-chip {
  font-size: .82rem; padding: .35rem .65rem; border-radius: 999px; cursor: pointer;
  border: 1px solid var(--line); background: var(--bg); color: var(--ink);
}
.chat-chip:active { background: var(--accent); color: #fff; }

.chat-foot { display: flex; gap: .4rem; padding: .6rem .8rem; border-top: 1px solid var(--line); }
.chat-input {
  flex: 1; padding: .55rem .7rem; font-size: .95rem; border: 1px solid var(--line);
  border-radius: 999px; background: var(--bg); color: var(--ink);
}
.chat-input:focus { outline: 2px solid var(--accent); outline-offset: 1px; background: var(--surface); }
.chat-send {
  width: 2.6rem; border: none; border-radius: 50%; cursor: pointer;
  background: var(--accent); color: #fff; font-size: 1rem;
}
.chat-send:active { transform: scale(.94); }
