/*
 * Sonic page layout (every page except the dashboard).
 * The pages keep the original Sonic design (styles.css + sonic.css); this file only adds:
 *   1. Tajawal everywhere (sonic.css asked for a local "Tajawal_Bold" file that is not shipped, so text fell back to Arial).
 *   2. Equal field rows and card-footer buttons (classes set by assets/js/sonic-layout.js).
 */

/* 1. Tajawal everywhere ---------------------------------------------------- */
html body,
html body :is(button, input, select, textarea, optgroup, .btn, .form-control, .form-select,
  .form-label, .form-check-label, .input-group-text, .table, th, td, .dropdown-menu, .dropdown-item,
  .select2-container, .select2-results__option, .select2-selection__rendered, .select2-search__field,
  .badge, .nav-link, h1, h2, h3, h4, h5, h6, .h1, .h2, .h3, .h4, .h5, .h6,
  .card-title, .modal-title, .tooltip, .popover, .toast, .alert, .breadcrumb,
  .apexcharts-text, .apexcharts-legend-text, .swal2-popup, .swal2-title, .swal2-html-container, .dataTables_wrapper) {
  font-family: "Tajawal", "Segoe UI", Tahoma, Arial, sans-serif !important;
}
html body :is(.barcode-code, .serial-code, .serial-detail-code, .invoice-serial-badge, code, kbd, pre) {
  font-family: ui-monospace, SFMono-Regular, Consolas, monospace !important;
}

/* 2. Equal field rows (sonic-layout.js sets --sonic-eq-k = fields on that line) */
/* Each line of fields splits the row width equally and is always full; the gaps are the same everywhere. */
.sonic-eq-row.sonic-eq-row {
  --bs-gutter-x: 0;
  --bs-gutter-y: 0;
  --sonic-eq-gap: 1rem;
  display: flex !important;
  flex-wrap: wrap !important;
  gap: var(--sonic-eq-gap) !important;
  align-items: flex-start;
}
.sonic-eq-row.sonic-eq-row { min-width: 0; max-width: 100%; }
.sonic-eq-row.sonic-eq-row.sonic-eq-row-fill { flex: 1 1 100% !important; width: 100%; }
.sonic-eq-row.sonic-eq-row > .sonic-eq-cell {
  flex: 0 0 calc((100% - var(--sonic-eq-minus, 0px) - (var(--sonic-eq-k, 1) - 1) * var(--sonic-eq-gap)) / var(--sonic-eq-k, 1) - .05px) !important;
  width: auto !important;
  max-width: none !important;
  min-width: 0;
  grid-column: auto !important;
  order: 0;
  margin-top: 0;
  padding-left: 0;
  padding-right: 0;
}
.sonic-eq-row.sonic-eq-row > :is(.sonic-eq-full, .sonic-card-footer) { flex: 0 0 100% !important; }
.sonic-eq-row.sonic-eq-row > .sonic-eq-moved { display: none !important; }
/* One even gap between fields: the cells' own Bootstrap margins (mb-3, mt-2...) are not added to it. */
.sonic-eq-row.sonic-eq-row > .sonic-eq-cell.sonic-eq-cell { margin-top: 0 !important; margin-bottom: 0 !important; }
/* A checkbox or a button sharing a line with fields: its natural width; the fields share the rest. */
.sonic-eq-row.sonic-eq-row > .sonic-eq-cell.sonic-eq-auto { flex: 0 0 var(--sonic-eq-w, auto) !important; }
.sonic-eq-row.sonic-eq-row > .sonic-eq-cell.sonic-eq-measure { flex: 0 0 auto !important; width: max-content !important; max-width: 100% !important; }
/* General / detailed switcher: as wide as its buttons, alone on its line. */
.sonic-eq-row.sonic-eq-row > .sonic-eq-cell.sonic-eq-toggle {
  flex: 0 0 var(--sonic-eq-w, auto) !important;
  margin-inline-end: calc(100% - var(--sonic-eq-w, 100%)) !important;
}
.sonic-eq-row.sonic-eq-row > .sonic-eq-cell.sonic-eq-toggle.sonic-eq-measure { margin-inline-end: 0 !important; }
/* The wrapper of a search field whose button moved to the footer: the field fills the cell. */
.sonic-eq-single.sonic-eq-single { display: block !important; }
/* Statistic cards: the cards of a line share its height. */
.sonic-eq-row.sonic-eq-stats { align-items: stretch; }
.sonic-eq-row.sonic-eq-stats > .sonic-eq-cell { display: flex !important; flex-direction: column; }
.sonic-eq-row.sonic-eq-stats > .sonic-eq-cell > * { flex: 1 1 auto; margin-bottom: 0 !important; margin-top: 0 !important; }
/* Controls fill their cell; labels sit on one line above them. */
.sonic-eq-row > .sonic-eq-field :is(.form-control, .form-select, .select2-container, .input-group):not(.select2-hidden-accessible):not(.input-group > *) { width: 100% !important; max-width: none; }
.sonic-eq-row > .sonic-eq-field .input-group { flex-wrap: nowrap; }
.sonic-eq-row > .sonic-eq-field > .form-label { display: block; }
/* A checkbox or a stand-alone button in a line of fields sits on the controls' line. */
.sonic-eq-row.sonic-eq-row > :is(.sonic-eq-check, .sonic-eq-action) {
  align-self: flex-end;
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: .5rem;
  min-height: calc(1.5em + .75rem + 2px);
}
.sonic-eq-row > .sonic-eq-check > .form-check { margin-bottom: 0; }

/* 3. Card-footer buttons -------------------------------------------------- */
.sonic-card-footer.sonic-card-footer {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: flex-end;
  gap: .5rem;
  margin-top: 1rem;
  padding-top: 1rem;
  border-top: var(--bs-card-border-width, 1px) solid var(--bs-border-color);
  width: auto;
  float: none;
  max-width: none;
}
/* In a row, a wrapping flex or a grid the footer takes a whole line (class set by sonic-layout.js). */
.sonic-card-footer.sonic-card-footer-line {
  flex: 0 0 100% !important;
  width: 100% !important;
  max-width: 100% !important;
  grid-column: 1 / -1 !important;
  margin-inline-start: 0 !important;
}
.sonic-eq-row.sonic-eq-row > .sonic-card-footer { margin-top: 0; }
.sonic-card-footer > .d-flex, .sonic-card-footer > div:not(.form-check):not(.input-group) { display: contents; }
.sonic-card-footer .btn { width: auto; margin: 0; }
/* The footer closes its card: it reaches the card edges like .card-footer. */
.sonic-card-footer.sonic-card-footer-edge {
  margin-left: calc(-1 * var(--sonic-footer-left, 0px));
  margin-right: calc(-1 * var(--sonic-footer-right, 0px));
  margin-bottom: calc(-1 * var(--sonic-footer-bottom, 0px));
  padding: .875rem var(--sonic-footer-left, 1.5rem) .875rem var(--sonic-footer-right, 1.5rem);
  padding-inline-start: var(--sonic-footer-right, 1.5rem);
  padding-inline-end: var(--sonic-footer-left, 1.5rem);
  background: var(--bs-card-cap-bg, transparent);
  border-bottom-left-radius: var(--sonic-footer-radius, 0);
  border-bottom-right-radius: var(--sonic-footer-radius, 0);
}

/* 4. Side-by-side cards end on the same line -------------------------------- */
.sonic-card-row > [class*="col"] { display: flex; flex-direction: column; }
.sonic-card-row > [class*="col"] > :last-child { flex-grow: 1; }
.sonic-card-row > [class*="col"] > form:last-child { display: flex; flex-direction: column; }
.sonic-card-row > [class*="col"] > form:last-child > .card:last-of-type { flex-grow: 1; }

/* 5. Small fixes of the original design ------------------------------------ */
/* Sale invoice, delivery box: in a narrow column the title and the "تفعيل البيع بالتوصيل" switch no longer overlap. */
.invoice-delivery-top { flex-wrap: wrap; }
.invoice-delivery-top > :not(.invoice-delivery-toggle) { min-width: 0; flex: 1 1 150px; }
/* Sale invoice, "تم التجهيز": the switch keeps its own width beside its text. */
.invoice-preparation-toggle.form-switch { padding-inline-start: 16px; }
.invoice-preparation-toggle.form-switch .form-check-input { float: none; margin: 0; }
/* Cashier: with the addition-rate column on, the totals strip still wraps on tablets and phones. */
@media (max-width: 1199.98px) { .cashier-checkout-grid.cashier-checkout-grid[data-addition-rate="1"] { grid-template-columns: repeat(3, minmax(0, 1fr)); } }
@media (max-width: 767.98px) { .cashier-checkout-grid.cashier-checkout-grid[data-addition-rate="1"] { grid-template-columns: 1fr 1fr; } }
/* An edge footer inside a flex/grid line: its width grows by the card padding it reaches into. */
.sonic-card-footer.sonic-card-footer-line.sonic-card-footer-edge {
  margin-left: calc(-1 * var(--sonic-footer-left, 0px)) !important;
  margin-right: calc(-1 * var(--sonic-footer-right, 0px)) !important;
  flex: 0 0 calc(100% + var(--sonic-footer-left, 0px) + var(--sonic-footer-right, 0px)) !important;
  width: calc(100% + var(--sonic-footer-left, 0px) + var(--sonic-footer-right, 0px)) !important;
  max-width: none !important;
}
/* Receipt/payment voucher, balance card: "last payment" and "last movement" are two lines, not run together. */
.voucher-balance-grid > [class*="col"]:has(> .voucher-meta-line) { flex-direction: column; }
/* Cards that stick to the screen on large screens only (sticky-xl-top + an inline "top"): below that
   breakpoint the offset pushed the card down over the next one. */
@media (max-width: 1199.98px) { .sticky-xl-top[style*="top"] { top: auto !important; } }
@media (max-width: 991.98px) { .sticky-lg-top[style*="top"] { top: auto !important; } }
/* Report settings on tablets/phones: the page grid no longer widens to the table inside it. */
.report-settings-shell > * { min-width: 0; }
@media (max-width: 991.98px) { .report-settings-shell { grid-template-columns: minmax(0, 1fr) !important; } }
.report-settings-shell main div:has(> table) { max-width: 100%; overflow-x: auto; }
/* Card headings with buttons (title | print) wrap on phones instead of squeezing the title. */
@media (max-width: 575.98px) {
  .card > .d-flex.justify-content-between, .card-header.d-flex { flex-wrap: wrap; gap: .5rem; }
}
