/* =============================================================================
   components.css — reusable UI building blocks
   Class naming: plain, descriptive, no framework. A component is a class
   (".btn"), its variants are extra classes (".btn primary"). Add new
   components at the bottom, in their own commented section.
   ============================================================================= */

/* ---------- App shell ---------------------------------------------------------- */

.app-header {
  position: sticky; top: 0; z-index: 30;
  height: var(--header-h);
  display: flex; align-items: center; gap: var(--space-4);
  padding: 0 var(--space-5);
  background: var(--surface);
  border-bottom: 1px solid var(--line);
}
.brand { display: flex; align-items: center; gap: var(--space-2); font-weight: 700; letter-spacing: -0.02em; text-decoration: none; color: var(--ink); white-space: nowrap; flex-shrink: 0; }
.brand-mark {
  display: grid; place-items: center;
  width: 32px; height: 32px; border-radius: 9px;
  background: var(--brand); color: var(--on-brand);
  font-size: var(--text-sm); font-weight: 800;
}
.brand-sub { font-weight: 500; color: var(--ink-muted); font-size: var(--text-sm); }

/* Ten-plus modules fit at desktop width; below that the menu scrolls sideways (the active link is kept in view). */
.app-nav { display: flex; gap: var(--space-1); margin-left: var(--space-3); overflow-x: auto; scrollbar-width: thin; min-width: 0; }
.nav-link {
  appearance: none; border: 0; background: none; cursor: pointer; white-space: nowrap;
  padding: var(--space-2) 10px; border-radius: var(--radius-sm);
  font-weight: 600; font-size: var(--text-md); color: var(--ink-muted);
  white-space: nowrap; position: relative;
}
.nav-link:hover { background: var(--surface-sunk); color: var(--ink); }
.nav-link[aria-current="page"] { color: var(--brand-strong); background: var(--brand-soft); }
.nav-badge {
  display: inline-grid; place-items: center; min-width: 18px; height: 18px; padding: 0 5px;
  margin-left: 6px; border-radius: 99px; background: var(--accent); color: #fff;
  font-size: 11px; font-weight: 700;
}
.header-end { margin-left: auto; display: flex; align-items: center; gap: var(--space-3); }

.page { max-width: 1120px; margin: 0 auto; padding: var(--space-5); }
.page-narrow { max-width: 760px; }
.page-head { display: flex; align-items: flex-end; justify-content: space-between; gap: var(--space-4); margin-bottom: var(--space-5); flex-wrap: wrap; }
.page-head p { color: var(--ink-muted); margin-top: var(--space-1); }

/* ---------- Buttons ------------------------------------------------------------ */

.btn {
  appearance: none; cursor: pointer;
  display: inline-flex; align-items: center; justify-content: center; gap: var(--space-2);
  padding: 0.55rem 1rem; min-height: 40px;
  border-radius: var(--radius); border: 1px solid var(--line-strong);
  background: var(--surface); color: var(--ink);
  font-weight: 600; font-size: var(--text-md); line-height: 1.2;
  text-decoration: none; white-space: nowrap;
  transition: background 0.12s, border-color 0.12s, transform 0.05s;
}
.btn:hover { background: var(--surface-sunk); }
.btn:active { transform: translateY(1px); }
.btn:disabled, .btn[aria-busy="true"] { opacity: 0.55; cursor: not-allowed; transform: none; }
.btn.primary { background: var(--brand); border-color: var(--brand); color: var(--on-brand); }
.btn.primary:hover { background: var(--brand-strong); }
.btn.danger { color: var(--danger); border-color: #e7c3bf; }
.btn.danger:hover { background: var(--danger-soft); }
.btn.ghost { border-color: transparent; background: transparent; }
.btn.ghost:hover { background: var(--surface-sunk); }
.btn.small { min-height: 32px; padding: 0.3rem 0.7rem; font-size: var(--text-sm); border-radius: var(--radius-sm); }
.btn.block { width: 100%; }
.btn.large { min-height: 48px; font-size: var(--text-lg); }

/* ---------- Forms -------------------------------------------------------------- */

.field { display: block; }
.field + .field { margin-top: var(--space-4); }
.field > span.label { display: block; font-weight: 600; font-size: var(--text-sm); margin-bottom: var(--space-1); }
.field .hint { display: block; color: var(--ink-muted); font-size: var(--text-xs); margin-top: var(--space-1); }

.input, .select, .textarea {
  width: 100%; min-height: 42px; padding: 0.55rem 0.75rem;
  border: 1px solid var(--line-strong); border-radius: var(--radius);
  background: var(--surface); color: var(--ink);
}
.textarea { min-height: 84px; resize: vertical; }
.input:focus, .select:focus, .textarea:focus { outline: none; border-color: var(--brand); box-shadow: var(--focus-ring); }
.input[aria-invalid="true"] { border-color: var(--danger); }
.input.small, .select.small { min-height: 34px; padding: 0.3rem 0.55rem; font-size: var(--text-sm); }

.form-grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: var(--space-4); }
.form-grid .field + .field { margin-top: 0; }
.form-grid .span-2 { grid-column: span 2; }

.check { display: flex; align-items: center; gap: var(--space-2); cursor: pointer; font-weight: 500; }
.check input { width: 18px; height: 18px; accent-color: var(--brand); }

.form-error { color: var(--danger); font-size: var(--text-sm); margin-top: var(--space-3); min-height: 1.2em; }

/* ---------- Cards, banners, stats --------------------------------------------- */

.card {
  background: var(--surface); border: 1px solid var(--line);
  border-radius: var(--radius-lg); box-shadow: var(--shadow-sm);
}
.card-pad { padding: var(--space-5); }
.card-head { display: flex; align-items: center; justify-content: space-between; gap: var(--space-3); padding: var(--space-4) var(--space-5); border-bottom: 1px solid var(--line); }

.banner { display: flex; gap: var(--space-3); align-items: flex-start; padding: var(--space-3) var(--space-4); border-radius: var(--radius); font-size: var(--text-md); margin-bottom: var(--space-4); }
.banner.warn { background: var(--accent-soft); color: #6d4208; border: 1px solid #efd3a1; }
.banner.error { background: var(--danger-soft); color: #7d241c; border: 1px solid #e7c3bf; }
.banner.info { background: var(--info-soft); color: #1c4a70; border: 1px solid #bfd8ec; }

.stat-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(190px, 1fr)); gap: var(--space-4); }
.stat { padding: var(--space-4) var(--space-5); }
.stat-label { color: var(--ink-muted); font-size: var(--text-sm); font-weight: 600; }
.stat-value { font-size: var(--text-2xl); font-weight: 700; letter-spacing: -0.02em; margin-top: var(--space-1); font-variant-numeric: tabular-nums; }
.stat-note { color: var(--ink-muted); font-size: var(--text-xs); margin-top: var(--space-1); }

.empty { text-align: center; padding: var(--space-7) var(--space-5); color: var(--ink-muted); }
.empty strong { display: block; color: var(--ink); font-size: var(--text-lg); margin-bottom: var(--space-1); }

.loading { padding: var(--space-6); text-align: center; color: var(--ink-muted); }
.spinner {
  display: inline-block; width: 18px; height: 18px; vertical-align: -3px; margin-right: 8px;
  border: 2px solid var(--line-strong); border-top-color: var(--brand); border-radius: 50%;
  animation: spin 0.7s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* ---------- Badges ------------------------------------------------------------- */

.badge {
  display: inline-flex; align-items: center; gap: 6px;
  padding: 2px 10px; border-radius: 99px;
  font-size: var(--text-xs); font-weight: 700; letter-spacing: 0.01em; white-space: nowrap;
  background: var(--surface-sunk); color: var(--ink-muted);
}
.badge::before { content: ""; width: 6px; height: 6px; border-radius: 50%; background: currentColor; }
.badge.placed           { background: var(--info-soft);   color: var(--info); }
.badge.confirmed        { background: #ece6f7;            color: #5b3fa0; }
.badge.packed           { background: var(--accent-soft); color: #8a5208; }
.badge.out_for_delivery { background: #fde9d6;            color: #a54a06; }
.badge.delivered        { background: var(--ok-soft);     color: var(--ok); }
.badge.cancelled        { background: var(--danger-soft); color: var(--danger); }
.badge.pending          { background: var(--accent-soft); color: #8a5208; }
.badge.approved         { background: var(--ok-soft);     color: var(--ok); }
.badge.suspended        { background: var(--danger-soft); color: var(--danger); }
.badge.in-stock         { background: var(--ok-soft);     color: var(--ok); }
.badge.out-of-stock     { background: var(--danger-soft); color: var(--danger); }
.badge.inactive         { background: var(--surface-sunk); color: var(--ink-muted); }
.badge.role             { background: var(--brand-soft);  color: var(--brand-strong); }
.badge.role::before     { display: none; }

/* ---------- Tables ------------------------------------------------------------- */

.table-wrap { overflow-x: auto; }
.table th {
  text-align: left; padding: var(--space-3) var(--space-4);
  font-size: var(--text-xs); font-weight: 700; letter-spacing: 0.05em; text-transform: uppercase;
  color: var(--ink-muted); background: var(--surface-sunk); white-space: nowrap;
}
.table td { padding: var(--space-3) var(--space-4); border-top: 1px solid var(--line); vertical-align: middle; }
.table tbody tr:hover { background: #fbfaf6; }
.table tr.clickable { cursor: pointer; }
.thumb { width: 44px; height: 44px; border-radius: 8px; object-fit: cover; background: var(--surface-sunk); }
.thumb-empty { width: 44px; height: 44px; border-radius: 8px; background: var(--surface-sunk); display: grid; place-items: center; color: var(--ink-muted); font-size: var(--text-xs); }

/* ---------- Tabs / chips / toolbar -------------------------------------------- */

.toolbar { display: flex; align-items: center; gap: var(--space-3); flex-wrap: wrap; margin-bottom: var(--space-4); }
.toolbar .input, .toolbar .select { width: auto; min-width: 180px; }
.search { flex: 1 1 240px; max-width: 420px; }

.chips { display: flex; gap: var(--space-2); overflow-x: auto; padding-bottom: var(--space-1); scrollbar-width: thin; }
.chip {
  appearance: none; cursor: pointer; white-space: nowrap;
  padding: 0.35rem 0.9rem; border-radius: 99px;
  border: 1px solid var(--line-strong); background: var(--surface);
  font-size: var(--text-sm); font-weight: 600; color: var(--ink);
}
.chip:hover { background: var(--surface-sunk); }
.chip[aria-pressed="true"] { background: var(--brand); border-color: var(--brand); color: var(--on-brand); }

/* ---------- Product grid (restaurant catalogue) ------------------------------- */

.product-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(220px, 1fr)); gap: var(--space-4); }
.product-card { display: flex; flex-direction: column; overflow: hidden; }
.product-photo { aspect-ratio: 4 / 3; background: var(--surface-sunk); display: grid; place-items: center; color: var(--ink-muted); font-size: 2.2rem; }
.product-photo img { width: 100%; height: 100%; object-fit: cover; }
.product-body { padding: var(--space-3) var(--space-4) var(--space-4); display: flex; flex-direction: column; gap: var(--space-2); flex: 1; }
.product-name { font-weight: 650; line-height: 1.3; }
.product-price { font-size: var(--text-lg); font-weight: 700; font-variant-numeric: tabular-nums; }
.product-price small { font-size: var(--text-sm); font-weight: 500; color: var(--ink-muted); }
.product-actions { margin-top: auto; display: flex; gap: var(--space-2); align-items: center; }
.product-card.unavailable { opacity: 0.65; }

/* Quantity stepper */
.stepper { display: inline-flex; align-items: center; border: 1px solid var(--line-strong); border-radius: var(--radius); background: var(--surface); }
.stepper button { appearance: none; border: 0; background: none; width: 34px; height: 38px; cursor: pointer; font-size: 1.1rem; font-weight: 700; color: var(--brand-strong); }
.stepper button:hover { background: var(--surface-sunk); }
.stepper input { width: 52px; text-align: center; border: 0; background: none; font-variant-numeric: tabular-nums; font-weight: 600; min-height: 38px; }
.stepper input:focus { outline: none; }

/* ---------- Drawer (cart) & modal --------------------------------------------- */

.overlay { position: fixed; inset: 0; z-index: 50; background: rgba(20, 30, 24, 0.45); display: flex; }
.drawer {
  margin-left: auto; width: min(440px, 100%); height: 100%;
  background: var(--surface); box-shadow: var(--shadow-lg);
  display: flex; flex-direction: column;
}
.drawer-head { padding: var(--space-4) var(--space-5); border-bottom: 1px solid var(--line); display: flex; align-items: center; justify-content: space-between; }
.drawer-body { flex: 1; overflow-y: auto; padding: var(--space-4) var(--space-5); }
.drawer-foot { padding: var(--space-4) var(--space-5); border-top: 1px solid var(--line); background: var(--surface); }

.overlay.center { align-items: center; justify-content: center; padding: var(--space-4); }
.modal {
  width: min(620px, 100%); max-height: calc(100vh - 2 * var(--space-4));
  display: flex; flex-direction: column;
  background: var(--surface); border-radius: var(--radius-lg); box-shadow: var(--shadow-lg);
}
.modal.wide { width: min(860px, 100%); }
.modal-head { padding: var(--space-4) var(--space-5); border-bottom: 1px solid var(--line); display: flex; justify-content: space-between; align-items: center; gap: var(--space-3); }
.modal-body { padding: var(--space-5); overflow-y: auto; text-align: left; }
.modal-foot { padding: var(--space-4) var(--space-5); border-top: 1px solid var(--line); display: flex; justify-content: flex-end; gap: var(--space-3); flex-wrap: wrap; }
.icon-btn { appearance: none; border: 0; background: none; cursor: pointer; font-size: 1.4rem; line-height: 1; padding: 6px 10px; border-radius: var(--radius-sm); color: var(--ink-muted); }
.icon-btn:hover { background: var(--surface-sunk); color: var(--ink); }

.line { display: flex; gap: var(--space-3); align-items: flex-start; justify-content: space-between; padding: var(--space-3) 0; border-bottom: 1px solid var(--line); }
.line:last-child { border-bottom: 0; }

/* ---------- Order detail pieces ----------------------------------------------- */

.kv { display: grid; grid-template-columns: 130px 1fr; gap: var(--space-2) var(--space-4); font-size: var(--text-md); }
.kv dt { color: var(--ink-muted); }
.kv dd { margin: 0; }

.timeline { list-style: none; margin: 0; padding: 0; }
.timeline li { position: relative; padding: 0 0 var(--space-4) var(--space-5); font-size: var(--text-sm); }
.timeline li::before { content: ""; position: absolute; left: 4px; top: 6px; width: 9px; height: 9px; border-radius: 50%; background: var(--brand); }
.timeline li::after { content: ""; position: absolute; left: 8px; top: 18px; bottom: 0; width: 1px; background: var(--line-strong); }
.timeline li:last-child { padding-bottom: 0; }
.timeline li:last-child::after { display: none; }

/* Order cards (restaurant + delivery) */
.order-card { padding: var(--space-4) var(--space-5); }
.order-card + .order-card { margin-top: var(--space-3); }

/* ---------- Toasts ------------------------------------------------------------- */

.toast-region { position: fixed; z-index: 100; bottom: var(--space-4); left: 50%; transform: translateX(-50%); display: grid; gap: var(--space-2); width: min(440px, calc(100% - 2 * var(--space-4))); }
.toast { padding: var(--space-3) var(--space-4); border-radius: var(--radius); background: var(--ink); color: #fff; box-shadow: var(--shadow-lg); font-size: var(--text-md); }
.toast.error { background: var(--danger); }
.toast.success { background: var(--brand-strong); }

/* ---------- Auth pages --------------------------------------------------------- */

.auth-wrap { min-height: 100vh; display: grid; place-items: center; padding: var(--space-4); }
.auth-card { width: min(460px, 100%); padding: var(--space-6); }
.auth-card .brand { margin-bottom: var(--space-5); }
.seg { display: grid; grid-template-columns: 1fr 1fr; background: var(--surface-sunk); border-radius: var(--radius); padding: 4px; margin-bottom: var(--space-5); }
.seg button { appearance: none; border: 0; background: none; padding: 0.55rem; border-radius: 8px; font-weight: 650; cursor: pointer; color: var(--ink-muted); }
.seg button[aria-selected="true"] { background: var(--surface); color: var(--ink); box-shadow: var(--shadow-sm); }

/* ---------- Responsive --------------------------------------------------------- */

.header-user { white-space: nowrap; }
@media (max-width: 1400px) {
  /* Thirteen modules (reports, 019) need the room: drop the sub-title and shorten the name; the menu scrolls. */
  .brand-sub { display: none; }
  .header-user { max-width: 120px; overflow: hidden; text-overflow: ellipsis; }
}
@media (max-width: 720px) {
  .app-header { padding: 0 var(--space-3); gap: var(--space-2); }
  .brand-sub, .header-user { display: none; }
  .app-nav { margin-left: 0; }
  .page { padding: var(--space-4) var(--space-3); }
  .form-grid { grid-template-columns: 1fr; }
  .form-grid .span-2 { grid-column: auto; }
  .kv { grid-template-columns: 1fr; gap: 0; }
  .kv dd { margin-bottom: var(--space-2); }
  .modal { max-height: 100vh; border-radius: var(--radius-lg) var(--radius-lg) 0 0; align-self: flex-end; }
  .overlay.center { align-items: flex-end; padding: 0; }
  .product-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); gap: var(--space-3); }
  .stat-value { font-size: var(--text-xl); }
}

/* ---------- Pricing extras (market price strike-through, "best rate") --------- */

.price-strike { color: var(--ink-muted); text-decoration: line-through; font-weight: 400; font-size: var(--text-sm); margin-left: var(--space-1); }
.best-rate { color: var(--ok); font-size: var(--text-xs); font-weight: 700; }

/* ---------- Stock level pill (admin inventory) -------------------------------- */

.badge.low   { background: var(--accent-soft); color: #8a5208; }
.badge.ok    { background: var(--ok-soft); color: var(--ok); }
.badge.empty-stock { background: var(--danger-soft); color: var(--danger); }

/* ---------- Tab strip of links (settings sub-pages) ------------------------ */
.tabs { display: flex; gap: var(--space-1); border-bottom: 1px solid var(--line); margin-bottom: var(--space-5); overflow-x: auto; }
.tabs a { padding: 0.55rem 0.9rem; font-weight: 600; color: var(--ink-muted); text-decoration: none; border-bottom: 2px solid transparent; white-space: nowrap; margin-bottom: -1px; }
.tabs a:hover { color: var(--ink); }
.tabs a[aria-selected="true"] { color: var(--brand-strong); border-bottom-color: var(--brand); }

/* ---------- Permission matrix (roles.js) ----------------------------------- */
.matrix th, .matrix td { text-align: center; }
.matrix th:first-child, .matrix td:first-child { text-align: left; white-space: nowrap; }
.matrix input[type="checkbox"] { width: 18px; height: 18px; accent-color: var(--brand); }

/* ---------- Audit log changes ---------------------------------------------- */
.diff { margin: 0; padding: 0; list-style: none; font-size: var(--text-xs); }
.diff li { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; max-width: 420px; }
.diff .old { color: var(--danger); text-decoration: line-through; }
.diff .new { color: var(--ok); }

/* ---------- Price board (price-board.js, pricing-rules.js) ------------------ */
.flag { display: inline-block; font-size: var(--text-xs); font-weight: 600; padding: 2px 8px; border-radius: 999px; margin: 1px 2px 1px 0; white-space: nowrap; }
.flag.info   { background: var(--info-soft);   color: #1c4a70; }
.flag.warn   { background: var(--accent-soft); color: #8a5208; }
.flag.danger { background: var(--danger-soft); color: var(--danger); }
.board th, .board td { padding: var(--space-2) var(--space-3); }
.board td.name { min-width: 190px; }
.board td.name .tiny { white-space: normal; }
.board td.changed { color: var(--ink-muted); text-decoration: line-through; }
.board tr.edited { background: #fffbea; }
.board .input.small { padding: 0.3rem 0.5rem; }
.small-table th, .small-table td { padding: var(--space-2) var(--space-3); font-size: var(--text-sm); }
.explain h3 { margin-bottom: var(--space-2); }
.steps { margin: 0 0 var(--space-3) 1.2rem; padding: 0; font-size: var(--text-sm); display: grid; gap: var(--space-1); }
.spark { color: var(--brand-strong); display: block; margin-top: var(--space-1); }
.rule-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(300px, 1fr)); gap: var(--space-4); }
.rule-card h3 { margin: 0; }
.kv.compact { grid-template-columns: max-content 1fr; gap: var(--space-1) var(--space-3); font-size: var(--text-sm); margin-top: var(--space-3); }
.kv.compact dt { color: var(--ink-muted); }
.kv.compact dd { margin: 0; }
.check.small { font-size: var(--text-sm); }

/* ---------- Catalogue + (variants, product page, rule cards) ----------------- */
.product-card .product-name, .product-card .product-photo { color: inherit; text-decoration: none; }
.product-card .select.small { padding: 0.3rem 0.5rem; margin: var(--space-1) 0; font-size: var(--text-sm); }
.product-page { display: grid; grid-template-columns: minmax(0, 2fr) minmax(0, 3fr); gap: var(--space-6); align-items: start; }
.product-photo.large { border-radius: var(--radius-lg); font-size: 4rem; }
.product-price.large { font-size: var(--text-2xl); }
.rule-card.sub { background: #fbfaf6; }
@media (max-width: 720px) { .product-page { grid-template-columns: 1fr; } }

/* ---------- Launch control (launch.js) and simple product form ---------------- */
.switch { position: relative; display: inline-block; width: 44px; height: 24px; vertical-align: middle; }
.switch input { opacity: 0; width: 0; height: 0; }
.switch .slider { position: absolute; inset: 0; background: var(--line); border-radius: 999px; transition: background .15s; cursor: pointer; }
.switch .slider::before { content: ""; position: absolute; width: 18px; height: 18px; left: 3px; top: 3px; background: #fff; border-radius: 50%; transition: transform .15s; box-shadow: var(--shadow-sm); }
.switch input:checked + .slider { background: var(--brand); }
.switch input:checked + .slider::before { transform: translateX(20px); }
.switch input:focus-visible + .slider { outline: 2px solid var(--brand-strong); outline-offset: 2px; }
details.more { grid-column: 1 / -1; border: 1px dashed var(--line); border-radius: var(--radius); padding: var(--space-2) var(--space-3); }
details.more summary { cursor: pointer; font-weight: 600; color: var(--ink-muted); }
details.more[open] summary { margin-bottom: var(--space-3); }
details.more .form-grid { margin-top: var(--space-2); }

/* ---------- Receivables (payments.js, restaurant payments.js) ---------------- */
.aging td.bucket { font-variant-numeric: tabular-nums; }
.aging td.overdue { color: var(--danger); font-weight: 600; }
.money.negative { color: var(--brand-strong); }

/* ---------- Help centre (migration 018): FAQ list inside the apps ---------- */
.faq details { border-bottom: 1px solid var(--line); padding: var(--space-3) 0; }
.faq details:last-child { border-bottom: 0; }
.faq summary { cursor: pointer; font-weight: 600; }
.faq p { color: var(--ink-muted); margin-top: var(--space-2); }
.contact-links a { color: var(--brand); }

/* ---------- Reports (migration 019): CSS bar chart ---------- */
.bars { display: flex; align-items: flex-end; gap: 6px; height: 160px; }
.bar-col { flex: 1; display: flex; flex-direction: column; align-items: center; justify-content: flex-end; height: 100%; min-width: 0; }
.bar { width: 100%; max-width: 40px; background: var(--brand-soft); border-radius: 4px 4px 0 0; position: relative; min-height: 2px; display: flex; flex-direction: column; justify-content: flex-end; overflow: hidden; }
.bar-overdue { width: 100%; background: var(--brand); opacity: .85; }
.bar-label { font-size: 10px; color: var(--ink-muted); margin-top: 4px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; max-width: 100%; }
.danger-text { color: var(--danger); }
.nav-link[hidden] { display: none; }   /* a tab the account does not need (Approvals, migration 020) */
