/* Screen-only phone layouts: printed invoices keep the original A4 layout. */
@media screen {
  #mobileMenuButton, #mobileMenuClose, #mobileNavBackdrop { display: none; }
  .workspace, .content, #pageContent { min-width: 0; }
  .table-scroll, .table-wrap, .ci-table-wrap { max-width: 100%; }
  .invoice-preview-scroll { max-width: 100%; overflow-x: auto; }
}

@media screen and (min-width: 761px) and (max-width: 1100px) {
  .sidebar { width: 210px; }
  .workspace { margin-left: 210px; }
  .workspace > header { padding-inline: 20px; gap: 16px; }
  .content { padding-inline: 20px; }
  .account b { max-width: 150px; overflow: hidden; text-overflow: ellipsis; }
  .page-head { gap: 16px; }
  .page-head #pageActions { flex-wrap: wrap; justify-content: flex-end; }
  .invoice-grid.columns-4 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
}

@media screen and (max-width: 760px) {
  html, body { width: 100%; overflow-x: hidden; -webkit-text-size-adjust: 100%; }
  body { overscroll-behavior-y: none; }
  button, input, textarea, select, label.image-upload { -webkit-tap-highlight-color: transparent; }
  button, label.image-upload { touch-action: manipulation; }
  button, .primary, .secondary, .danger { min-height: 44px; }
  input:not([type="hidden"]):not([type="checkbox"]):not([type="radio"]), select, textarea {
    font-size: 16px !important;
    min-height: 44px;
    max-width: 100%;
  }
  textarea { line-height: 1.5; }
  input[type="checkbox"], input[type="radio"] { width: 22px; height: 22px; flex: none; }
  .primary, .secondary, .danger { font-size: 14px; padding: 11px 14px; }
  .mini { min-height: 44px; min-width: 44px; font-size: 13px !important; }
  button:focus-visible, label.image-upload:focus-within {
    outline: 3px solid #93b8ff;
    outline-offset: 2px;
  }

  /* A short login page keeps both fields reachable above the keyboard. */
  .login-screen { display: block; min-height: 100vh; min-height: 100dvh; }
  .login-visual { min-height: 0; padding: calc(26px + env(safe-area-inset-top)) 24px 26px; }
  .login-brand { gap: 12px; }
  .login-message { margin-top: 25px; }
  .login-message em { font-size: 11px; }
  .login-message h1 { font-size: 26px; line-height: 1.35; letter-spacing: -.5px; margin: 10px 0 0; }
  .login-message p, .login-foot { display: none; }
  .login-form-wrap { padding: 26px max(24px, env(safe-area-inset-right)) calc(30px + env(safe-area-inset-bottom)) max(24px, env(safe-area-inset-left)); }
  .login-form { max-width: 440px; }
  .login-form h2 { font-size: 26px; }
  .login-form .subtext { margin-bottom: 24px; font-size: 14px; }
  .login-form label { font-size: 14px; }
  .login-form input { min-height: 50px; }
  .login-button { min-height: 50px; align-items: center; }
  .form-error { font-size: 13px; line-height: 1.5; }
  .security-note { font-size: 12px; line-height: 1.6; }

  /* The drawer exposes every section, including administrator controls. */
  body.mobile-nav-open { overflow: hidden; }
  .sidebar {
    inset: 0 auto 0 0;
    width: min(304px, calc(100vw - 44px));
    height: 100vh;
    height: 100dvh;
    padding: calc(18px + env(safe-area-inset-top)) 16px calc(18px + env(safe-area-inset-bottom));
    transform: translateX(-110%);
    visibility: hidden;
    overflow-y: auto;
    overscroll-behavior: contain;
    z-index: 42;
    transition: transform .18s ease, visibility .18s;
  }
  .mobile-nav-open .sidebar { transform: translateX(0); visibility: visible; }
  #mobileNavBackdrop { position: fixed; inset: 0; z-index: 41; background: #07132980; border: 0; padding: 0; }
  .mobile-nav-open #mobileNavBackdrop { display: block; }
  .sidebar .brand { display: flex; padding: 0 40px 25px 8px; min-height: 66px; }
  .sidebar .brand b { font-size: 16px; }
  .sidebar .brand small { font-size: 10px; }
  .sidebar .brand-mark { width: 40px; height: 40px; flex: none; }
  #mobileMenuClose {
    display: grid;
    place-items: center;
    position: absolute;
    top: calc(17px + env(safe-area-inset-top));
    right: 10px;
    width: 44px;
    height: 44px;
    border: 0;
    border-radius: 8px;
    background: #ffffff12;
    color: #fff;
    font-size: 26px;
  }
  .sidebar nav { display: flex; flex-direction: column; gap: 7px; width: 100%; }
  .sidebar nav button { display: flex; align-items: center; min-height: 49px; padding: 12px 14px; text-align: left; font-size: 16px; }
  .sidebar nav button i { display: inline-block; width: 31px; font-size: 20px; }
  .sidebar nav p { display: block; font-size: 12px; margin: 20px 14px 4px; }
  #usersNav:not(.hidden) { display: flex; }
  .sidebar .sync-box { display: block; margin-top: 28px; }
  .sync-box div, .sync-box small { font-size: 12px; }
  .workspace { margin-left: 0; padding-bottom: env(safe-area-inset-bottom); min-height: 100dvh; }
  .workspace > header {
    height: auto;
    min-height: 112px;
    display: grid;
    grid-template-columns: 44px minmax(0, 1fr) auto;
    gap: 8px 9px;
    padding: calc(8px + env(safe-area-inset-top)) max(14px, env(safe-area-inset-right)) 10px max(14px, env(safe-area-inset-left));
    top: 0;
    z-index: 30;
  }
  #mobileMenuButton { display: grid; place-items: center; grid-column: 1; grid-row: 1; width: 44px; border: 1px solid #dce5f2; border-radius: 10px; color: #15376c; background: #f6f8fc; font-size: 23px; }
  .mobile-brand { display: block; grid-column: 2; grid-row: 1; min-width: 0; font-size: 13px; font-weight: 900; letter-spacing: .6px; }
  .workspace > header .search { display: flex; grid-column: 1 / -1; grid-row: 2; width: 100%; min-width: 0; padding: 0 12px; height: 44px; }
  .search input { min-width: 0; height: 42px; }
  .account { grid-column: 3; grid-row: 1; gap: 3px; }
  .account > div:not(.avatar), .account .online-dot { display: none; }
  .account .avatar { display: none; }
  .account button { margin: 0; min-width: 44px; padding: 9px 6px; color: #465e80; font-size: 13px; }

  .content { padding: 20px max(14px, env(safe-area-inset-right)) calc(28px + env(safe-area-inset-bottom)) max(14px, env(safe-area-inset-left)); }
  .page-head { flex-direction: column; align-items: stretch; gap: 14px; margin-bottom: 18px; }
  .page-head h1 { font-size: 24px; line-height: 1.3; overflow-wrap: anywhere; }
  .page-head > div:first-child p { display: block; margin-bottom: 7px; font-size: 12px; }
  .page-head > div:first-child span { display: block; font-size: 13px; line-height: 1.65; }
  .page-head #pageActions { flex-direction: row; gap: 8px; flex-wrap: wrap; }
  #pageActions button { flex: 1 1 auto; }
  .stats { grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 10px; }
  .stat { min-width: 0; padding: 14px 11px; gap: 9px; }
  .stat-icon { width: 34px; height: 38px; flex: none; }
  .stat p { font-size: 12px; }
  .stat b { font-size: 21px; }
  .stat span { font-size: 11px; line-height: 1.5; }
  .welcome-grid { grid-template-columns: minmax(0, 1fr); }
  .panel { min-width: 0; padding: 17px; }
  .panel h2 { font-size: 17px; }
  .panel > p, .security-list p { font-size: 13px; line-height: 1.7; }
  .hero-panel { min-height: 0; }
  .quick-list { grid-template-columns: minmax(0, 1fr); margin-top: 20px; }
  .quick-list button { padding: 16px; }
  .quick-list button b { font-size: 16px; }
  .quick-list button small { font-size: 13px; line-height: 1.6; }

  /* Wide catalog tables retain rowspans and scroll without moving the page. */
  .table-panel { padding: 0; overflow: hidden; }
  .panel-head { padding: 16px; }
  .panel-head p { font-size: 12px; line-height: 1.6; }
  .table-scroll, .table-wrap { overflow-x: auto; overscroll-behavior-x: contain; -webkit-overflow-scrolling: touch; }
  .table-panel table { font-size: 13px; }
  .table-panel th { color: #53657d; font-size: 12px; white-space: nowrap; }
  .table-panel td { color: #435773; padding: 14px 12px; }
  .table-panel .cell-note { font-size: 12px; line-height: 1.5; }
  .table-panel .badge { font-size: 11px; }
  .catalog-table td:last-child[rowspan] { position: sticky; right: 0; background: #fff; box-shadow: -5px 0 8px #142f6b0a; }
  .catalog-thumb { width: 105px; height: 78px; }
  .row-actions { gap: 8px; }
  .empty { padding: 36px 18px; line-height: 1.7; font-size: 14px; }

  /* Each order line is a labeled card, with two independent photo panels. */
  .invoice-editor { gap: 14px; }
  .editor-section { min-width: 0; padding: 16px; }
  .section-title { gap: 14px; margin-bottom: 16px; }
  .section-title > div { min-width: 0; }
  .section-title b { font-size: 17px; }
  .section-title span { font-size: 12px; line-height: 1.7; }
  .invoice-grid.columns-3, .invoice-grid.columns-4 { grid-template-columns: minmax(0, 1fr); gap: 16px; }
  .invoice-grid .wide { grid-column: auto; }
  .invoice-field > span, .customer-picker { font-size: 13px; }
  .check-field { font-size: 13px; line-height: 1.6; min-height: 48px; }
  .goods-section { padding: 16px 0 0; }
  .goods-section .section-title { padding: 0 14px; }
  .goods-add-actions { width: 100%; }
  .goods-add-actions button { flex: 1 1 auto; }
  .goods-editor-wrap { overflow: visible; border-top: 0; }
  .goods-editor { display: block; min-width: 0; width: 100%; }
  .goods-editor thead { display: none; }
  .goods-editor tbody { display: grid; gap: 14px; padding: 0 12px 14px; }
  .goods-editor .invoice-item-row { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 14px 10px; padding: 14px 12px; border: 1px solid #dce5f1; border-radius: 12px; background: #f8faff; }
  .goods-editor .invoice-item-row > td { display: block; padding: 0; border: 0; width: auto; min-width: 0; max-width: none; white-space: normal; overflow: visible; }
  .goods-editor .invoice-item-row > td::before { content: attr(data-label); display: block; margin-bottom: 7px; font-size: 12px; font-weight: 750; color: #4b607c; line-height: 1.5; }
  .goods-editor .invoice-item-row > td:first-child { grid-column: 1; width: 80px; }
  .goods-editor .invoice-item-row > td:first-child input { min-width: 0; text-align: center; }
  .goods-editor .invoice-item-row > td:nth-child(2) { grid-column: 1 / -1; }
  .goods-editor .invoice-item-row > td:nth-child(5) { grid-column: 1 / -1; }
  .goods-editor .invoice-item-row > td:nth-child(10) { grid-column: 1 / -1; display: flex; align-items: center; justify-content: space-between; padding-top: 10px; border-top: 1px solid #dce5f1; }
  .goods-editor .invoice-item-row > td:nth-child(10)::before { margin: 0; font-size: 14px; }
  .goods-editor .invoice-item-row > td:last-child { grid-column: 2; grid-row: 1; justify-self: end; align-self: end; }
  .goods-editor .invoice-item-row > td:last-child::before { display: none; }
  .goods-editor input, .goods-editor select, .goods-editor textarea { min-width: 0; width: 100%; padding: 10px; background: #fff; }
  .goods-editor textarea { min-width: 0; min-height: 84px; }
  .goods-editor .line-number { min-width: 0; }
  .goods-editor .item-spec-label { font-size: 12px; margin-top: 12px; line-height: 1.5; }
  .goods-editor .item-spec-label textarea { min-height: 74px; }
  .goods-editor .line-total { padding: 0; font-size: 19px; }
  .goods-editor .cell-note, .catalog-price-warning { font-size: 12px; line-height: 1.6; }
  .item-image-box { width: 100%; min-width: 0; gap: 7px 4px; }
  .item-image-preview, .item-image-empty { width: 100%; height: 114px; object-fit: contain; text-align: center; padding: 4px; font-size: 12px; }
  .image-upload, .image-paste, .image-clear { display: flex; align-items: center; justify-content: center; min-height: 44px; min-width: 0; padding: 8px 2px !important; font-size: 12px !important; }
  .goods-editor .image-library-picker { padding: 8px !important; }
  .image-save-hint { font-size: 11px; min-height: 44px; }
  .calculated-total { min-height: 76px; }
  .calculated-total span { font-size: 12px; }
  .stamp-notice { align-items: flex-start; padding: 12px; gap: 10px; }
  .stamp-notice p { font-size: 12px; margin: 0; }
  .stamp-mini { width: 90px; }
  .editor-bottom-actions { flex-wrap: wrap; padding: 0 0 20px; }
  .editor-bottom-actions button { flex: 1 1 140px; }
  .print-toolbar { min-width: 0; flex-wrap: wrap; align-items: center; gap: 12px; font-size: 13px; line-height: 1.6; }
  .invoice-preview-scroll { border-radius: 8px; -webkit-overflow-scrolling: touch; overscroll-behavior-x: contain; }

  /* Dialogs fill the safe viewport and scroll independently from the page. */
  .modal-backdrop, .pe-backdrop, .gps-backdrop { padding: env(safe-area-inset-top) env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left); align-items: stretch; }
  .modal, .pe-dialog, .gps-dialog { width: 100%; max-width: 100%; max-height: calc(100dvh - env(safe-area-inset-top) - env(safe-area-inset-bottom)); border-radius: 0; overscroll-behavior: contain; }
  .modal { padding: 20px 18px; height: 100%; }
  .modal-head { gap: 12px; }
  .modal-head > button, .pe-close, .gps-close { width: 44px; height: 44px; min-width: 44px; flex: none; }
  .modal-head h2, .pe-heading h2, .gps-header h2 { font-size: 21px; line-height: 1.4; }
  .modal-head p, .pe-heading p, .gps-header p { font-size: 13px; line-height: 1.7; }
  .modal label { font-size: 13px; }
  .form-row { grid-template-columns: minmax(0, 1fr); }
  .modal-actions { position: sticky; bottom: -20px; margin: 0 -18px -20px; padding: 14px 18px; background: #fff; z-index: 1; }
  .modal-actions button { flex: 1; }
  .address-preview b, .address-preview p, .user-limit { font-size: 13px; }
  .pe-heading, .gps-header { padding: 20px 16px 15px; }
  .pe-body { padding: 20px 16px; }
  .pe-fields, .pe-photos { grid-template-columns: minmax(0, 1fr); gap: 17px; }
  .pe-wide { grid-column: auto; }
  .pe-field, .pe-variant-grid .pe-field { font-size: 13px; }
  .pe-field small { font-size: 12px; line-height: 1.6; }
  .pe-variant-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 14px 10px; }
  .pe-variant-grid .pe-field:nth-child(4) { grid-column: 1 / -1; }
  .pe-variants-heading, .pe-variant-row { padding: 14px 12px; }
  .pe-variants-heading p, .pe-variant-empty, .pe-price-message { font-size: 13px; }
  .pe-variant-actions { width: 100%; }
  .pe-variant-actions button { flex: 1 1 130px; font-size: 13px; }
  .pe-remove-variant { min-width: 52px; font-size: 13px !important; }
  .pe-photo { padding: 14px; }
  .pe-photo h3 { font-size: 15px; }
  .pe-image-box { height: 210px; }
  .pe-photo-actions button { flex: 1 1 auto; font-size: 13px; }
  .pe-photo-status, .pe-photo-note, .pe-reference-note { font-size: 13px; line-height: 1.7; }
  .pe-footer { padding: 14px 16px; gap: 10px; }
  .pe-footer > button { flex: 1; }
  .pe-message { font-size: 13px; }
  .gps-search { padding-inline: 16px; }
  .gps-search > div { gap: 8px; }
  .gps-currency, .gps-message { margin-inline: 16px; font-size: 13px; }
  .gps-results { padding-inline: 16px; }
  .gps-product-summary { gap: 14px; }
  .gps-photos { width: 100%; gap: 10px; }
  .gps-photo { width: calc(50% - 5px); }
  .gps-photo > div { height: 112px; }
  .gps-photo figcaption { font-size: 12px; line-height: 1.5; }
  .gps-product-info { flex-basis: 100%; }
  .gps-product-info h3 { font-size: 17px; }
  .gps-product-info p, .gps-sku { font-size: 13px; }
  .gps-product-action .btn, .gps-variant-row .btn { font-size: 14px; }
  .gps-variant-row { font-size: 14px; }
  .gps-footer { padding: 13px 16px; align-items: center; }
  .gps-footer span { font-size: 12px; }
  .ci-card { padding: 16px; }
  .ci-heading { flex-direction: column; }
  .ci-heading p, .ci-card p { font-size: 14px; }
  .ci-metrics { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .toast { right: max(14px, env(safe-area-inset-right)); left: max(14px, env(safe-area-inset-left)); bottom: calc(16px + env(safe-area-inset-bottom)); font-size: 14px; line-height: 1.6; z-index: 1300; overflow-wrap: anywhere; }
}

@media screen and (max-width: 359px) {
  .mobile-brand { font-size: 11px; letter-spacing: 0; }
  .stat { align-items: flex-start; gap: 6px; }
  .stat-icon { width: 25px; height: 32px; }
  .stat b { font-size: 19px; }
  .goods-editor .invoice-item-row > td:nth-child(6), .goods-editor .invoice-item-row > td:nth-child(7) { grid-column: 1 / -1; }
  .item-image-preview, .item-image-empty { height: 156px; }
  .image-save-hint { min-height: 0; }
}

@media screen and (prefers-reduced-motion: reduce) {
  .sidebar { transition: none; }
}
