:root {
  --bg: #f6f5f1;
  --surface: #ffffff;
  --surface-2: #f1efe8;
  --border: rgba(40,40,35,0.12);
  --border-strong: rgba(40,40,35,0.22);
  --text: #1f1e1b;
  --text-2: #6b6a63;
  --text-3: #9a988e;
  --ok: #1d9e6f;
  --ok-bg: #e4f5ec;
  --danger: #d8442f;
  --danger-bg: #fbeae7;
  --warn: #b9740b;
  --warn-bg: #fbf0dc;
  --info: #1f6fb8;
  --info-bg: #e7f1fb;
  --radius-md: 8px;
  --radius-lg: 14px;
  --shell-max: 1180px;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #181816;
    --surface: #232220;
    --surface-2: #2b2a27;
    --border: rgba(255,255,255,0.10);
    --border-strong: rgba(255,255,255,0.18);
    --text: #ece9e2;
    --text-2: #ada99e;
    --text-3: #76736a;
    --ok: #3ecb93;
    --ok-bg: #163328;
    --danger: #f0816d;
    --danger-bg: #3a2220;
    --warn: #e0a73f;
    --warn-bg: #3a2f17;
    --info: #5aa3e8;
    --info-bg: #1d2c3c;
  }
}

* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; }
body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  background: var(--bg);
  color: var(--text);
  -webkit-font-smoothing: antialiased;
}

#app { max-width: var(--shell-max); margin: 0 auto; min-height: 100vh; display: flex; flex-direction: column; }

/* Topbar */
.topbar {
  display: flex; align-items: center; justify-content: space-between;
  padding: 14px 20px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}
.brand { display: flex; align-items: center; gap: 9px; font-size: 17px; font-weight: 600; }
.brand i { font-size: 21px; color: var(--ok); }
.menu-toggle { display: none; background: none; border: 1px solid var(--border); border-radius: var(--radius-md); width: 38px; height: 38px; font-size: 18px; color: var(--text); cursor: pointer; }

/* Nav */
nav {
  display: flex; gap: 2px; padding: 0 14px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  overflow-x: auto;
  scrollbar-width: none;
}
nav::-webkit-scrollbar { display: none; }
nav button {
  background: none; border: none; border-bottom: 2px solid transparent;
  padding: 13px 16px; font-size: 13.5px; font-weight: 500; color: var(--text-2);
  cursor: pointer; white-space: nowrap; display: flex; align-items: center; gap: 7px;
  transition: color .15s, border-color .15s;
}
nav button i { font-size: 16px; }
nav button.active { color: var(--text); border-bottom-color: var(--ok); }
nav button:hover:not(.active) { color: var(--text); }

main { flex: 1; padding: 22px 24px 60px; }

.page { display: none; animation: fadeIn .2s ease; }
.page.active { display: block; }
@keyframes fadeIn { from { opacity: 0; transform: translateY(4px); } to { opacity: 1; transform: translateY(0); } }

.page-title {
  font-size: 19px; font-weight: 600; margin: 0 0 18px;
  display: flex; align-items: center; gap: 10px;
}
.page-title i { color: var(--ok); font-size: 20px; }

/* Metric cards */
.metric-row { display: grid; grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); gap: 12px; margin-bottom: 22px; }
.metric-card { background: var(--surface); border-radius: var(--radius-lg); border: 1px solid var(--border); padding: 16px 18px; }
.metric-card .label { font-size: 12.5px; color: var(--text-2); margin-bottom: 7px; }
.metric-card .value { font-size: 23px; font-weight: 600; }
.metric-card .value.green { color: var(--ok); }
.metric-card .value.red { color: var(--danger); }
.metric-card .value.amber { color: var(--warn); }

/* Chart card */
.chart-card { background: var(--surface); border-radius: var(--radius-lg); border: 1px solid var(--border); padding: 18px; margin-bottom: 18px; }
.chart-card h3 { font-size: 14px; font-weight: 600; color: var(--text-2); margin: 0 0 14px; }
.chart-legend { display: flex; flex-wrap: wrap; gap: 14px; margin-bottom: 12px; font-size: 12.5px; color: var(--text-2); }
.chart-legend span { display: flex; align-items: center; gap: 5px; }
.chart-legend .dot { width: 10px; height: 10px; border-radius: 2px; }
.chart-wrap { position: relative; }

/* Entry */
.entry-grid { display: grid; grid-template-columns: repeat(2, 1fr); gap: 14px; }
.product-form-grid { display: grid; grid-template-columns: repeat(4, 1fr); gap: 12px; }
.entry-section { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius-lg); padding: 18px; }
.entry-section h3 { font-size: 13.5px; font-weight: 600; color: var(--text-2); margin: 0 0 13px; display: flex; align-items: center; gap: 7px; }
.entry-section h3 i { font-size: 16px; }

.form-row { display: flex; flex-direction: column; gap: 6px; margin-bottom: 11px; }
.form-row label { font-size: 12.5px; color: var(--text-2); font-weight: 500; }
.form-row input, .form-row select {
  font-size: 14px; width: 100%; padding: 9px 11px; border-radius: var(--radius-md);
  border: 1px solid var(--border-strong); background: var(--surface-2); color: var(--text);
}
.form-row input:focus, .form-row select:focus { outline: none; border-color: var(--ok); box-shadow: 0 0 0 3px var(--ok-bg); }

.btn-save {
  width: 100%; padding: 11px; border-radius: var(--radius-md); background: var(--text);
  color: var(--surface); font-size: 14px; font-weight: 600; border: none; cursor: pointer;
  margin-top: 4px; display: flex; align-items: center; justify-content: center; gap: 6px;
  transition: opacity .15s;
}
.btn-save:hover { opacity: 0.85; }

/* Lists */
.list-card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius-lg); overflow: hidden; margin-top: 18px; }
.list-header { padding: 13px 18px; border-bottom: 1px solid var(--border); font-size: 13.5px; font-weight: 600; color: var(--text-2); display: flex; align-items: center; gap: 8px; }
.table-scroll { overflow-x: auto; }
table { width: 100%; border-collapse: collapse; font-size: 13.5px; min-width: 420px; }
thead th { padding: 9px 16px; text-align: left; font-size: 11px; font-weight: 600; text-transform: uppercase; letter-spacing: 0.04em; color: var(--text-3); background: var(--surface-2); border-bottom: 1px solid var(--border); white-space: nowrap; }
tbody td { padding: 11px 16px; border-bottom: 1px solid var(--border); }
tbody tr:last-child td { border-bottom: none; }
tbody tr:hover { background: var(--surface-2); }
.num { text-align: right; }
td.num { font-weight: 600; }

.badge { display: inline-block; padding: 3px 9px; border-radius: 5px; font-size: 11px; font-weight: 600; }
.badge-success { background: var(--ok-bg); color: var(--ok); }
.badge-danger { background: var(--danger-bg); color: var(--danger); }
.badge-warning { background: var(--warn-bg); color: var(--warn); }
.badge-info { background: var(--info-bg); color: var(--info); }

.empty-state { text-align: center; padding: 40px 20px; color: var(--text-3); font-size: 13.5px; }

.filter-bar { display: flex; gap: 9px; margin-bottom: 14px; align-items: center; flex-wrap: wrap; }
.filter-bar input { font-size: 13px; padding: 8px 11px; border-radius: var(--radius-md); border: 1px solid var(--border-strong); background: var(--surface-2); color: var(--text); }
.filter-to { font-size: 12.5px; color: var(--text-3); }
.btn-filter, .btn-clear { font-size: 13px; padding: 8px 14px; border-radius: var(--radius-md); border: 1px solid var(--border-strong); cursor: pointer; font-weight: 500; }
.btn-filter { background: var(--surface-2); color: var(--text); }
.btn-clear { background: none; color: var(--text-2); }

.total-row { padding: 12px 18px; background: var(--surface-2); font-size: 14px; font-weight: 600; display: flex; justify-content: space-between; border-top: 1px solid var(--border); }

.del-btn { background: none; border: none; color: var(--danger); cursor: pointer; font-size: 16px; padding: 3px 5px; opacity: 0.65; }
.del-btn:hover { opacity: 1; }
.edit-btn { background: none; border: none; color: var(--text-2); cursor: pointer; font-size: 13.5px; padding: 3px 7px; opacity: 0.8; white-space: nowrap; }
.edit-btn:hover { opacity: 1; color: var(--text); }

/* Search-as-you-type picker (customer / product / supplier / barcode) */
.search-pick-wrap { position: relative; }
.search-pick-input { width: 100%; }
.search-pick-results { position: absolute; top: calc(100% + 4px); left: 0; right: 0; background: var(--surface); border: 1px solid var(--border-strong); border-radius: var(--radius-md); max-height: 260px; overflow-y: auto; z-index: 50; box-shadow: 0 8px 24px rgba(0,0,0,0.18); display: none; }
.search-pick-results.open { display: block; }
.search-pick-item { padding: 10px 14px; cursor: pointer; border-bottom: 1px solid var(--border); font-size: 13.5px; }
.search-pick-item:last-child { border-bottom: none; }
.search-pick-item:hover, .search-pick-item.active { background: var(--surface-2); }
.search-pick-item .spi-main { font-weight: 600; }
.search-pick-item .spi-sub { font-size: 12px; color: var(--text-2); margin-top: 2px; }
.search-pick-item .spi-stock { float: right; font-size: 11.5px; color: var(--text-3); }
.search-pick-chip { display: inline-flex; align-items: center; gap: 6px; background: var(--surface-2); border: 1px solid var(--border-strong); border-radius: 8px; padding: 8px 12px; font-size: 13.5px; font-weight: 600; margin-top: 6px; }
.search-pick-chip button { background: none; border: none; color: var(--text-2); cursor: pointer; font-size: 14px; display: flex; }
.search-pick-empty { padding: 14px; text-align: center; color: var(--text-2); font-size: 13px; }
.metric-card-btn { cursor: pointer; border: 1px solid transparent; font-family: inherit; text-align: left; transition: border-color 0.15s; }
.metric-card-btn:hover { border-color: var(--border-strong); }

.dues-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 16px; }
.dues-grid .list-card { margin-top: 0; }

/* Products */
.products-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(230px, 1fr)); gap: 14px; margin-top: 16px; }
.product-card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius-lg); padding: 16px; display: flex; flex-direction: column; gap: 8px; }
.product-card .pname { font-size: 14.5px; font-weight: 600; }
.product-card .pmeta { font-size: 12.5px; color: var(--text-2); display: flex; justify-content: space-between; }
.product-card .pbarcode-img { background: #fff; border-radius: var(--radius-md); padding: 8px; margin: 4px 0; display: flex; justify-content: center; }
.product-card .price-row { display: flex; justify-content: space-between; font-size: 13px; }
.product-card .price-row .v { font-weight: 600; }
.product-card .stock-low { color: var(--danger); font-weight: 600; }
.product-card .actions { display: flex; gap: 6px; margin-top: 4px; }
.product-card .actions button { flex: 1; font-size: 12px; padding: 7px; border-radius: var(--radius-md); border: 1px solid var(--border-strong); background: var(--surface-2); color: var(--text); cursor: pointer; display: flex; align-items: center; justify-content: center; gap: 4px; }
.product-card .actions button:hover { background: var(--border); }
.product-card .actions button.danger { color: var(--danger); }

/* Modal */
.modal-overlay { position: fixed; inset: 0; background: rgba(0,0,0,0.5); display: flex; align-items: center; justify-content: center; z-index: 100; padding: 20px; }
.modal-box { background: var(--surface); border-radius: var(--radius-lg); padding: 24px; max-width: 360px; width: 100%; position: relative; text-align: center; max-height: 90vh; overflow-y: auto; }
.modal-box-wide { max-width: 520px; text-align: left; }
.modal-box-pos { max-width: 340px; text-align: left; padding: 16px; }
.modal-close { position: absolute; top: 12px; right: 12px; background: none; border: none; font-size: 18px; color: var(--text-2); cursor: pointer; }
#modal-barcode-content { background: #fff; border-radius: var(--radius-md); padding: 16px; text-align: center; font-family: 'Roboto', 'Segoe UI', sans-serif; }
#modal-barcode-content .mp-name { color: #111; font-weight: 600; margin-bottom: 8px; font-family: 'Roboto', 'Segoe UI', sans-serif; }
#modal-barcode-content .mp-price { color: #111; margin-top: 6px; font-weight: 700; font-family: 'Roboto', 'Segoe UI', sans-serif; }
.label-editor { display: grid; grid-template-columns: 1fr 1fr; gap: 16px; margin: 8px 0 18px; }
.label-preview-wrap { display: flex; align-items: center; justify-content: center; background: var(--surface-2); border-radius: var(--radius-md); padding: 10px; }
.label-controls .form-row { margin-bottom: 10px; }
.modal-actions { display: flex; gap: 10px; }
.btn-secondary {
  padding: 11px 16px; border-radius: var(--radius-md); background: var(--surface-2);
  color: var(--text); font-size: 14px; font-weight: 600; border: 1px solid var(--border-strong); cursor: pointer;
  display: flex; align-items: center; justify-content: center; gap: 6px;
}
.btn-secondary:hover { background: var(--border); }
@media (max-width: 560px) {
  .label-editor { grid-template-columns: 1fr; }
}

/* Cart / multi-product sale entry */
.cart-add-row { display: flex; gap: 10px; align-items: flex-end; flex-wrap: wrap; margin-bottom: 8px; }
.cart-add-row .form-row { margin-bottom: 0; min-width: 110px; }
.btn-add-item {
  padding: 9px 16px; border-radius: var(--radius-md); background: var(--ok-bg); color: var(--ok);
  font-size: 13.5px; font-weight: 600; border: 1px solid transparent; cursor: pointer;
  display: flex; align-items: center; gap: 6px; white-space: nowrap; height: 38px;
}
.btn-add-item:hover { opacity: 0.85; }
.cart-table-wrap { margin: 14px 0; border: 1px solid var(--border); border-radius: var(--radius-md); overflow: hidden; }
.cart-table-wrap table { width: 100%; font-size: 13.5px; min-width: 0; }
.cart-table-wrap thead th { background: var(--surface-2); padding: 8px 12px; font-size: 11px; }
.cart-table-wrap tbody td { padding: 9px 12px; }
.cart-row-remove { background: none; border: none; color: var(--danger); cursor: pointer; opacity: 0.7; }
.cart-row-remove:hover { opacity: 1; }
.cart-summary { background: var(--surface-2); border-radius: var(--radius-md); padding: 14px 16px; margin-top: 4px; }
.cart-summary-row { display: flex; justify-content: space-between; font-size: 15px; font-weight: 600; margin-bottom: 10px; }
.cart-summary-row.due-preview { color: var(--warn); font-size: 13.5px; font-weight: 600; }
.pay-toggle { display: flex; gap: 6px; flex-wrap: wrap; }
.pay-btn {
  flex: 1; padding: 9px 10px; border-radius: var(--radius-md); border: 1px solid var(--border-strong);
  background: var(--surface); color: var(--text-2); font-size: 12.5px; font-weight: 600; cursor: pointer;
  display: flex; align-items: center; justify-content: center; gap: 5px; white-space: nowrap;
}
.pay-btn.active { background: var(--text); color: var(--surface); border-color: var(--text); }

/* POS bill preview / print */
.pos-preview-wrap { background: var(--surface-2); border-radius: var(--radius-md); padding: 14px; display: flex; justify-content: center; margin-bottom: 16px; overflow-x: auto; }
.pos-bill { background: #fff; color: #111; font-family: 'Courier New', monospace; padding: 14px 12px; box-sizing: border-box; }
.pos-bill .pb-center { text-align: center; }
.pos-bill .pb-name { font-weight: 700; word-break: break-word; }
.pos-bill .pb-sub { font-size: 12px; margin: 2px 0; word-break: break-word; }
.pos-bill .pb-title { font-weight: 700; text-decoration: underline; margin: 10px 0; }
.pos-bill .pb-meta { font-size: 12px; margin: 2px 0; word-break: break-word; }
.pos-bill .pb-rule { border-top: 1px dashed #000; margin: 8px 0; }
.pos-bill table.pb-items { width: 100%; font-size: 12px; border-collapse: collapse; table-layout: fixed; }
.pos-bill table.pb-items th { text-align: left; padding: 3px 2px; font-weight: 700; white-space: nowrap; overflow: hidden; }
.pos-bill table.pb-items th.num, .pos-bill table.pb-items td.num { text-align: right; white-space: nowrap; }
.pos-bill table.pb-items td { padding: 3px 2px; vertical-align: top; }
.pos-bill table.pb-items td:first-child { word-break: break-word; overflow-wrap: break-word; }
.pos-bill table.pb-items th:nth-child(1) { width: 40%; }
.pos-bill table.pb-items th:nth-child(2) { width: 15%; }
.pos-bill table.pb-items th:nth-child(3) { width: 20%; }
.pos-bill table.pb-items th:nth-child(4) { width: 25%; }
.pos-bill .pb-totals div { display: flex; justify-content: space-between; font-size: 12.5px; padding: 2px 0; gap: 8px; }
.pos-bill .pb-total-line { font-weight: 700; font-size: 15px; display: flex; justify-content: space-between; padding: 4px 0; gap: 8px; }
.pos-bill .pb-footer { text-align: center; font-weight: 700; margin-top: 12px; font-size: 12.5px; word-break: break-word; }
.pos-bill .pb-paid-line { font-size: 12px; }

/* Customer cards / detail */
.cust-detail-head { display: flex; justify-content: space-between; align-items: flex-start; margin-bottom: 16px; padding-bottom: 14px; border-bottom: 1px solid var(--border); }
.cust-detail-head .cd-name { font-size: 17px; font-weight: 600; }
.cust-detail-head .cd-meta { font-size: 13px; color: var(--text-2); margin-top: 4px; }
.cust-metric-row { display: grid; grid-template-columns: repeat(3, 1fr); gap: 10px; margin-bottom: 16px; }
.bill-history-item { border: 1px solid var(--border); border-radius: var(--radius-md); padding: 12px 14px; margin-bottom: 8px; cursor: pointer; }
.bill-history-item:hover { background: var(--surface-2); }
.bill-history-item .bh-top { display: flex; justify-content: space-between; font-size: 13.5px; font-weight: 600; }
.bill-history-item .bh-sub { font-size: 12px; color: var(--text-2); margin-top: 4px; }

@media print {
  @page { margin: 0; }
  body { margin: 0; }
}

/* ───── Responsive breakpoints ───── */
@media (max-width: 880px) {
  .entry-grid { grid-template-columns: 1fr; }
  .product-form-grid { grid-template-columns: 1fr 1fr; }
  .dues-grid { grid-template-columns: 1fr; }
  .cust-metric-row { grid-template-columns: 1fr; }
}

@media (max-width: 640px) {
  main { padding: 16px 14px 50px; }
  .topbar { padding: 12px 16px; }
  .brand span { font-size: 15px; }
  .menu-toggle { display: inline-flex; align-items: center; justify-content: center; }
  nav { padding: 0 8px; }
  nav button { padding: 11px 12px; font-size: 12.5px; }
  .page-title { font-size: 17px; }
  .metric-row { grid-template-columns: repeat(2, 1fr); gap: 9px; }
  .metric-card { padding: 13px 14px; }
  .metric-card .value { font-size: 19px; }
  .product-form-grid { grid-template-columns: 1fr; }
  .products-grid { grid-template-columns: repeat(auto-fill, minmax(160px, 1fr)); }
  table { min-width: 360px; }
  .cart-add-row { flex-direction: column; align-items: stretch; }
  .cart-add-row .form-row { min-width: 0; }
  .btn-add-item { width: 100%; justify-content: center; }
  .pay-toggle { flex-direction: column; }
}

@media (max-width: 420px) {
  .metric-row { grid-template-columns: 1fr 1fr; }
}
