/*
 * Phone and tablet layout.
 * Every rule lives inside a "screen and (max-width: ...)" query or the print block,
 * so desktop (992px and up) and printing keep today's styles. MobileLayoutTests checks this.
 */

/* Elements added for phones stay hidden until a phone query shows them. */
.mobile-topbar,
.side-nav-backdrop,
.mobile-action-bar,
.mc-more {
  display: none;
}

/* ---------- Shell: drawer menu, top bar and page header (below 992px) ---------- */
@media screen and (max-width: 991.98px) {
  :root {
    --side-nav-width: 0px;
    --header-hieght: 56px;
  }

  /* body is a flex row; without this the page grows to its widest content. */
  .main-content {
    min-width: 0;
  }

  .side-nav {
    top: 0;
    inset-inline-start: 0;
    width: min(300px, 85vw);
    height: 100vh;
    height: 100dvh;
    z-index: 1045;
    box-shadow: 0 0 24px rgba(17, 24, 39, 0.18);
    transform: translateX(-100%);
    visibility: hidden;
    transition: transform 0.25s ease, visibility 0.25s;
  }

  [dir="rtl"] .side-nav {
    transform: translateX(100%);
  }

  body.side-nav-open .side-nav {
    transform: none;
    visibility: visible;
  }

  .side-nav-backdrop {
    position: fixed;
    inset: 0;
    z-index: 1040;
    background: rgba(17, 24, 39, 0.4);
  }

  body.side-nav-open .side-nav-backdrop {
    display: block;
  }

  html.side-nav-lock {
    overflow: hidden;
  }

  .mobile-topbar {
    display: flex;
    align-items: center;
    position: fixed;
    top: 0;
    inset-inline: 0;
    height: var(--header-hieght);
    padding-inline: 12px;
    background: #F9FAFB;
    border-bottom: 1px solid #EEF0F2;
    z-index: 1000;
  }

  .mobile-nav-toggle {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 40px;
    height: 40px;
    padding: 0;
    border: 1px solid #E5E7EB;
    border-radius: 50%;
    background: #fff;
    color: #354359;
    font-size: 22px;
  }

  body.cashier-fullscreen-active .mobile-topbar {
    display: none;
  }

  /* The page header scrolls with the page: title on one line, buttons in one scrollable row. */
  .header {
    position: static;
    height: auto;
    padding: 12px 0;
  }

  body.has-header-bell .header .inner-header {
    padding-inline-end: 0;
  }

  .inner-header {
    flex-wrap: wrap;
    gap: 8px;
  }

  .inner-header > h1 {
    flex: 1 1 100%;
    margin-bottom: 0;
  }

  .inner-header > :not(h1) {
    flex: 1 1 100%;
    min-width: 0;
    flex-wrap: nowrap !important;
    overflow-x: auto;
    padding-bottom: 2px;
  }

  .inner-header > :not(h1) > * {
    flex-shrink: 0;
  }
}

/* ---------- Content: tables and lists (below 992px) ---------- */
@media screen and (max-width: 991.98px) {
  /* ledger.css stops the page from scrolling sideways, so wide tables scroll inside themselves. */
  .main-content table.ledger-table:not(.bootstrap-table table):not(.table-responsive > table) {
    display: block;
    width: 100%;
    overflow-x: auto;
  }

  /* Absolutely positioned bits (select2's hidden <select>s, checkboxes) escape a scroll
     box that isn't positioned and widen the whole page. */
  .table-responsive,
  .barcode-table-wrap {
    position: relative;
  }

  /* The footer is absolutely positioned; pin both edges so it never sits off screen. */
  .footer {
    left: 0;
    right: 0;
    width: auto;
  }

  .dataTables_wrapper .bottom .d-flex {
    flex-wrap: wrap;
    gap: 8px;
  }

  .dataTables_wrapper .pagination {
    flex-wrap: wrap;
  }
}

/* ---------- Content: forms, toolbars and dialogs (phones) ---------- */
@media screen and (max-width: 767.98px) {
  /* 16px stops iOS from zooming in when a field gets focus. */
  input,
  select,
  textarea {
    font-size: 16px !important;
  }

  /* List toolbars: search on its own line, buttons wrap under it. */
  .main-content .d-flex.justify-content-between:has(> div #search_input) {
    flex-wrap: wrap;
    gap: 8px;
  }

  .main-content .d-flex.justify-content-between:has(> div #search_input) > div:has(#search_input) {
    flex: 1 1 100%;
    width: 100% !important;
    max-width: none !important;
  }

  .main-content .d-flex.justify-content-between:has(> div #search_input) > .d-flex {
    flex-wrap: wrap;
  }

  /* Client and date block on invoices, quotations, orders and memos. */
  .doc-party-row {
    flex-direction: column;
    gap: 12px;
  }

  .doc-party-row > .d-flex {
    flex-wrap: wrap;
  }

  .doc-party-row .party-selector-column,
  .doc-party-row .clientUser-selector {
    flex: 1 1 100%;
    min-width: 0;
  }

  /* The client validation message sits above the date instead of beside it. */
  .doc-party-row > .d-flex:last-child {
    flex-direction: column;
    align-items: stretch !important;
    gap: 0 !important;
  }

  .doc-party-row .doc-date-col {
    max-width: none !important;
  }

  /* Report footers. */
  .statement-footer .states {
    flex-wrap: wrap;
    gap: 12px;
  }

  .statement-footer .footer-item {
    flex: 1 1 40%;
    min-width: 0;
    margin: 0;
  }

  /* Company logo picker in settings. */
  .company-logo-upload {
    grid-template-columns: minmax(0, 1fr);
  }

  .company-logo-upload-header,
  .company-logo-upload-body {
    flex-direction: column;
    align-items: stretch;
    min-width: 0;
  }

  .company-logo-upload-header > *,
  .company-logo-upload-body > * {
    flex: 0 0 auto;
    width: 100%;
    min-width: 0;
  }

  /* Select2 lists open full width instead of running off the screen. */
  body > .select2-container--open,
  .modal > .select2-container--open {
    left: 8px !important;
    right: 8px !important;
    width: auto !important;
  }

  body > .select2-container--open .select2-dropdown,
  .modal > .select2-container--open .select2-dropdown {
    width: 100% !important;
    min-width: 0 !important;
  }

  .select2-results__option {
    white-space: normal;
  }

  /* Bigger touch targets for the small icon buttons. */
  .delete-row,
  .serial-numbers-btn,
  .item-info-btn,
  .remove-payment,
  .modal .close-btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    min-width: 40px;
    min-height: 40px;
  }

  input[type="checkbox"]:not(.form-check-input) {
    width: 20px;
    height: 20px;
  }
}

/* ---------- Invoice, quotation and order lines as cards (phones) ----------
 * mobile.js labels each cell from the table header and adds the mc-lines class,
 * so without the script the grid stays a normal scrolling table.
 * Columns: 1 number, 2 item, 3 description, 4 unit, 5 price, 6 price with VAT,
 * 7 quantity, 8 tax rate, 9 discount, 10 total, 11 tax amount, 12 net, 13 actions. */
@media screen and (max-width: 767.98px) {
  table.mc-lines,
  table.mc-lines tbody {
    display: block;
    width: 100% !important;
    min-width: 0 !important;
    border: 0 !important;
  }

  table.mc-lines thead {
    display: none;
  }

  /* Cards never need sideways scrolling; select2's hidden <select>s would otherwise add some. */
  .mc-lines-wrap {
    overflow: visible !important;
  }

  table.mc-lines tbody tr {
    display: grid;
    grid-template-columns: minmax(0, 1fr) minmax(0, 1fr);
    grid-template-areas:
      "num act"
      "item item"
      "unit qty"
      "price net";
    gap: 10px 12px;
    margin-bottom: 12px;
    padding: 12px;
    border: 1px solid #E5E7EB;
    border-radius: 12px;
    background: #fff;
  }

  table.mc-lines tbody tr.mc-expanded {
    grid-template-areas:
      "num act"
      "item item"
      "unit qty"
      "price net"
      "desc desc"
      "pvat tax"
      "disc total"
      "taxamt taxamt";
  }

  table.mc-lines tbody td {
    display: block;
    width: auto !important;
    min-width: 0 !important;
    max-width: none !important;
    padding: 0 !important;
    border: 0 !important;
    box-shadow: none !important;
    background: transparent !important;
    overflow: visible;
    white-space: normal;
    text-align: start;
    font-size: 14px;
  }

  table.mc-lines tbody td::before {
    content: attr(data-label);
    display: block;
    margin-bottom: 4px;
    color: #6B7280;
    font-size: 12px;
  }

  table.mc-lines tbody td:nth-child(1) {
    grid-area: num;
    align-self: center;
    font-weight: 700;
    color: #354359;
  }

  table.mc-lines tbody td:nth-child(1)::before {
    content: "#";
    display: inline;
    margin: 0;
    font-size: inherit;
    color: inherit;
  }

  table.mc-lines tbody td:nth-child(2) { grid-area: item; }
  table.mc-lines tbody td:nth-child(3) { grid-area: desc; }
  table.mc-lines tbody td:nth-child(4) { grid-area: unit; }
  table.mc-lines tbody td:nth-child(5) { grid-area: price; }
  table.mc-lines tbody td:nth-child(6) { grid-area: pvat; }
  table.mc-lines tbody td:nth-child(7) { grid-area: qty; }
  table.mc-lines tbody td:nth-child(8) { grid-area: tax; }
  table.mc-lines tbody td:nth-child(9) { grid-area: disc; }
  table.mc-lines tbody td:nth-child(10) { grid-area: total; }
  table.mc-lines tbody td:nth-child(11) { grid-area: taxamt; }
  table.mc-lines tbody td:nth-child(12) { grid-area: net; }

  table.mc-lines tbody td:nth-child(13) {
    grid-area: act;
  }

  table.mc-lines tbody td:nth-child(13)::before {
    content: none;
  }

  table.mc-lines tbody td:nth-child(13) > .d-flex {
    justify-content: flex-end !important;
    gap: 4px !important;
  }

  /* Description, price with VAT, tax rate, discount, total and tax amount wait behind "More". */
  table.mc-lines tbody tr:not(.mc-expanded) > td:is(:nth-child(3), :nth-child(6), :nth-child(8), :nth-child(9), :nth-child(10), :nth-child(11)) {
    display: none;
  }

  /* Beat the pixel widths the grid script writes on every input. */
  table.mc-lines input:not([type="hidden"]),
  table.mc-lines select:not(.select2-hidden-accessible),
  table.mc-lines .select2-container {
    width: 100% !important;
    min-width: 0 !important;
    max-width: none !important;
  }

  /* ledger.css gives grid selects min-width: 100%, which stretches select2's hidden <select>. */
  table.mc-lines select.select2-hidden-accessible {
    min-width: 0 !important;
  }

  table.mc-lines input[readonly] {
    background: #F9FAFB;
  }

  .mc-more {
    display: inline-flex;
    align-items: center;
    gap: 4px;
    min-height: 40px;
    padding: 0 12px;
    border: 1px solid #E5E7EB;
    border-radius: 999px;
    background: #fff;
    color: #354359;
    font-size: 13px;
  }

  tr.mc-expanded .mc-more i {
    transform: rotate(180deg);
  }
}

/* ---------- Sales and purchase invoice lists as cards (phones) ----------
 * mobile.js copies each column name into data-col, so the layout follows names, not positions. */
@media screen and (max-width: 767.98px) {
  .main-content table.mc-list {
    display: block !important;
    width: 100% !important;
    overflow: visible !important;
    border: 0;
    box-shadow: none !important;
    background: transparent;
  }

  table.mc-list thead {
    display: none;
  }

  table.mc-list tbody {
    display: block;
  }

  table.mc-list tbody tr {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    gap: 6px 12px;
    margin-bottom: 10px;
    padding: 12px 14px;
    border: 1px solid #E5E7EB;
    border-radius: 12px;
    background: #fff;
  }

  table.mc-list tbody td {
    display: block;
    width: auto !important;
    min-width: 0 !important;
    padding: 0 !important;
    border: 0 !important;
    background: transparent !important;
    text-align: start !important;
    white-space: normal;
    font-size: 14px;
  }

  /* Number, total and menu on top; party below; date and status chips last. */
  table.mc-list td[data-col="invoiceCodeString"] {
    order: 1;
    flex: 1 1 auto;
    font-weight: 700;
  }

  table.mc-list td[data-col="netAmount"] {
    order: 2;
    flex: 0 0 auto;
    font-size: 15px;
    font-weight: 700;
  }

  table.mc-list td[data-col="actions"] {
    order: 3;
    flex: 0 0 auto;
  }

  table.mc-list td[data-col="clientName"] {
    order: 4;
    flex: 1 1 100%;
    color: #354359;
  }

  table.mc-list td[data-col="invoiceDate"] {
    order: 5;
    flex: 1 1 auto;
    color: #6B7280;
    font-size: 13px;
  }

  table.mc-list td:is([data-col="invoiceType"], [data-col="invoiceStatus"], [data-col="zatcaStatus"], [data-col="supplierInvoiceNumber"]) {
    order: 6;
    flex: 0 0 auto;
    font-size: 12px;
  }

  table.mc-list td[data-col="supplierInvoiceNumber"]::before {
    content: attr(data-label) ": ";
    color: #6B7280;
  }

  table.mc-list td:is([data-col="branchName"], [data-col="sellerName"], [data-col="createdByName"], [data-col="totalAfterDiscount"], [data-col="vatAmount"], [data-col="notes"]),
  table.mc-list td:empty {
    display: none;
  }

  table.mc-list td.dataTables_empty {
    flex: 1 1 100%;
    text-align: center !important;
  }
}

/* ---------- Bottom action bar (phones) ----------
 * mobile.js copies the page's save/post buttons into this bar; each copy clicks the original. */
@media screen and (max-width: 767.98px) {
  .mobile-action-bar {
    display: flex;
    align-items: center;
    gap: 8px;
    position: fixed;
    inset-inline: 0;
    bottom: 0;
    z-index: 990;
    padding: 10px 12px calc(10px + env(safe-area-inset-bottom));
    overflow-x: auto;
    background: #fff;
    border-top: 1px solid #E5E7EB;
    box-shadow: 0 -4px 16px rgba(17, 24, 39, 0.08);
  }

  .mobile-action-bar .btn {
    flex: 0 0 auto;
    min-height: 44px;
    margin: 0 !important;
    white-space: nowrap;
  }

  .mobile-action-total {
    display: flex;
    flex-direction: column;
    margin-inline-end: auto;
    color: #6B7280;
    font-size: 12px;
    white-space: nowrap;
  }

  .mobile-action-total strong {
    color: #111827;
    font-size: 16px;
  }

  body.has-mobile-action-bar {
    padding-bottom: 76px;
  }

  body.has-mobile-action-bar .footer {
    bottom: 76px;
  }

  body.has-mobile-action-bar #toast-container.toast-bottom-center {
    bottom: 84px;
  }

  /* The bar holds these buttons on phones. */
  body.has-mobile-action-bar .header .inner-header .submit-btn,
  body.has-mobile-action-bar .header .inner-header button[type="submit"][form] {
    display: none !important;
  }

  /* Out of the way while the on-screen keyboard is up. */
  body.mobile-typing .mobile-action-bar {
    display: none;
  }
}

@media screen and (max-width: 575.98px) {
  .modal-content.white-modal {
    width: 100% !important;
    max-width: 100% !important;
  }
}

@media print {
  .mobile-topbar,
  .side-nav-backdrop,
  .mobile-action-bar {
    display: none !important;
  }
}
