/* Standard popups are centered in the viewport, including phones and custom
   accounting dialogs. Full-page document editors keep their page layout. */
body dialog[open]:not(.order-compose,.verify-page) {
  position:fixed; inset:0 !important; margin:auto !important; transform:none !important;
  max-height:calc(100dvh - 24px); box-sizing:border-box;
}
/* Form sections keep their DOM order, labels and keyboard navigation on phones. */
.product-editor-layout { display:grid; gap:18px; min-width:0; }
.product-editor-layout fieldset { margin:0; padding:14px; border:1px solid var(--line,#dce3ed); border-radius:10px; min-width:0; }
.product-editor-layout legend { padding:0 6px; font-weight:600; }
.product-editor-layout label { min-width:0; }
.product-editor-info > label { align-self:start; align-content:start; }
.product-editor-layout input,.product-editor-layout select { width:100%; min-width:0; box-sizing:border-box; }
.product-editor-layout > p { margin:0; }
.product-editor-carton,.product-editor-prices { grid-template-columns:repeat(2,minmax(0,1fr)); }
.product-editor-image small { display:block; margin-top:8px; }
/* Giao việc nhanh: đầu / thân / chân như khuôn .modal-head / .modal-body / .modal-foot (rà đồng bộ đợt 3, C2). */
.task-quick-head { display:flex; align-items:center; justify-content:space-between; gap:12px; padding:18px 22px; }
.task-quick-head > .icon-btn { flex:none; }
.task-quick-body { display:flex; flex-direction:column; gap:20px; padding:18px 22px; min-width:0; }
.task-quick-foot { display:flex; flex-wrap:wrap; justify-content:flex-end; gap:8px; padding:14px 22px 18px; border-top:1px solid var(--line); background:var(--surface-2); }
.task-quick-foot [data-discard] { order:-1; margin-right:auto; }

@media (min-width: 900px) {
  dialog.landscape-dialog,.sup-drawer.landscape-drawer {
    /* A few legacy editors set their portrait width inline. Override only the
       dimensions of forms selected by the layout controller. */
    /* 16:9 là KHUNG TỐI ĐA: form ngắn co theo nội dung, form dài cuộn bên trong (đầu/chân form luôn nhìn thấy). */
    --landscape-w:min(1280px,calc(100vw - 48px),calc((100dvh - 48px) * 16 / 9));
    width:var(--landscape-w) !important;
    /* fit-content: popup căn giữa bằng inset:0 sẽ giãn hết màn hình nếu để height:auto. */
    height:fit-content; max-width:calc(100vw - 48px) !important;
    max-height:min(calc(100dvh - 48px),calc(var(--landscape-w) * 9 / 16)) !important; padding:0; overflow:hidden; box-sizing:border-box;
  }
  /* Ngăn kéo NCC/đơn mua (div, không phải dialog) giữ khung 16:9 cố định như trước. */
  .sup-drawer.landscape-drawer { aspect-ratio:16 / 9; height:auto; }
  .sup-drawer.landscape-drawer > form.landscape-dialog-form { height:100%; }
  /* Chiều cao không còn cố định: khung là cột flex để phần thân co lại và cuộn, không đẩy chân form ra ngoài. */
  dialog[open].landscape-dialog { display:flex; flex-direction:column; }
  /* Còn nội dung bên dưới (máy ẩn thanh cuộn): bóng mờ ở mép chân form. */
  .landscape-dialog .landscape-dialog-foot.landscape-more { box-shadow:0 -10px 14px -10px rgba(15,23,42,.35); }
  dialog.landscape-dialog > .landscape-dialog-form,
  .landscape-dialog .pos-modal-content > form.landscape-dialog-form,
  .sup-drawer.landscape-drawer > form.landscape-dialog-form {
    display:flex; flex-direction:column; flex:1 1 auto; max-height:100%; min-height:0; margin:0; overflow:hidden;
  }
  dialog[open].landscape-dialog.landscape-dialog-form { display:flex; flex-direction:column; }
  dialog.acct-drawer.landscape-dialog { top:50%; left:50%; right:auto; bottom:auto; transform:translate(-50%,-50%); margin:0; }
  .landscape-dialog .pos-modal-content { display:flex; flex-direction:column; flex:1 1 auto; min-height:0; max-height:100%; padding:0; overflow:hidden; }
  .landscape-dialog .pos-modal-content > form.landscape-dialog-form { flex:1; padding:0; }
  .landscape-dialog .pos-modal-close { position:static; }
  .sup-drawer.landscape-drawer { top:50%; left:50%; right:auto; bottom:auto; transform:translate(-50%,-50%); display:flex; flex-direction:column; border-radius:14px; }
  .sup-drawer.landscape-drawer > form.landscape-dialog-form { flex:1; gap:0; }
  .sup-drawer.landscape-drawer .landscape-dialog-head { padding:18px 22px; }
  .sup-drawer.landscape-drawer .landscape-dialog-body { display:grid; align-content:start; grid-auto-rows:max-content; gap:16px; padding:0 22px 18px; }
  .sup-drawer.landscape-drawer .landscape-dialog-foot { padding:14px 22px; margin:0; }
  .landscape-dialog .landscape-dialog-head,.landscape-dialog .landscape-dialog-foot {
    flex:0 0 auto; position:static; min-width:0;
  }
  .landscape-dialog .landscape-dialog-head h2 { overflow-wrap:anywhere; }
  .landscape-dialog .landscape-dialog-head > button { flex-shrink:0; }
  .landscape-dialog .landscape-dialog-body {
    flex:1 1 auto; min-height:0; min-width:0; max-height:none; overflow:auto; overscroll-behavior:contain;
  }
  .landscape-dialog .landscape-dialog-foot { display:flex; flex-wrap:wrap; gap:8px; justify-content:flex-end; }
  .landscape-dialog .form-grid.cols-2 { grid-template-columns:repeat(3,minmax(0,1fr)); }
  .landscape-dialog .landscape-dialog-body.form-grid { grid-template-columns:repeat(3,minmax(0,1fr)); align-content:start; }
  .landscape-dialog .landscape-dialog-body.form-grid > :not(label) { grid-column:1 / -1; }
  .landscape-dialog .form-grid.cols-2 > .span-2 { grid-column:1 / -1; }
  .landscape-dialog .form-grid > p { grid-column:1 / -1; }
  .landscape-dialog .care-form { grid-template-columns:repeat(3,minmax(0,1fr)); align-content:start; }
  .landscape-dialog .care-form > p,
  .landscape-dialog .care-form > label:has(textarea),
  .landscape-dialog .care-form > .care-field:has(.care-seg) { grid-column:1 / -1; }
  .task-quick-dialog.landscape-dialog .task-quick-body { display:grid; grid-template-columns:repeat(2,minmax(0,1fr)); align-content:start; gap:20px; padding:18px 22px; }
  .task-quick-dialog.landscape-dialog .task-quick-body > .error { grid-column:1 / -1; }
  .landscape-dialog label { min-width:0; }
  .landscape-dialog .product-editor-layout { grid-template-columns:minmax(0,2fr) minmax(0,1fr); align-content:start; }
  .landscape-dialog .product-editor-info.form-grid.cols-2 { grid-template-columns:repeat(2,minmax(0,1fr)); }
  .landscape-dialog .product-editor-info > .span-2 { grid-column:1 / -1; }
  .landscape-dialog .product-editor-image { align-content:start; }
  .landscape-dialog .product-editor-carton,.landscape-dialog .product-editor-prices { grid-column:1 / -1; grid-template-columns:repeat(4,minmax(0,1fr)); }
  .landscape-dialog .product-editor-layout > p { grid-column:1 / -1; }
  .landscape-dialog .product-editor-link { grid-column:1 / -1; }
}
@media (max-width:899px) {
  .landscape-dialog-head,.sup-drawer-head {
    position:sticky; top:0; z-index:4; background:var(--surface,#fff);
  }
  .pos-form-modal.landscape-dialog .modal-head,.pos-readable-modal.landscape-dialog .modal-head { padding:0 40px 0 0; }
  .pos-form-modal.landscape-dialog .modal-body,.pos-readable-modal.landscape-dialog .modal-body { padding:0; }
  .pos-form-modal.landscape-dialog .modal-foot,.pos-readable-modal.landscape-dialog .modal-foot { padding:14px 0 0; }
}
/* Tạo nhanh từ link (sản phẩm) / từ MST (khách hàng): ô link + nút trên một hàng, xuống dòng trên điện thoại hẹp. */
.product-link-row,.tax-lookup-row { display:flex; gap:8px; align-items:center; min-width:0; }
.product-link-row input,.tax-lookup-row input { flex:1 1 auto; min-width:0; }
.product-link-row .btn,.tax-lookup-row .btn { flex:0 0 auto; white-space:nowrap; }
@media (max-width:420px){ .product-link-row,.tax-lookup-row { flex-wrap:wrap; } .product-link-row .btn,.tax-lookup-row .btn { width:100%; } }
.tax-lookup { margin:0 0 12px; padding:12px; border:1px dashed var(--line,#dce3ed); border-radius:10px; }
.tax-lookup .warn { color:var(--danger,#b42318); }

/* CRM dialogs with the standard header/body/footer, including short confirmations. */
@media screen {
  dialog.modal .modal-head { gap:12px; }
  dialog.modal .modal-head > :first-child { min-width:0; overflow-wrap:anywhere; }
  dialog.modal .modal-head > .icon-btn { flex:none; }
}
@media screen and (max-width: 899px) {
  dialog.modal[open]:not(#print-preview,#image-dialog,.order-compose,.verify-page):has(> .modal-head + .modal-body + .modal-foot, > form > .modal-head + .modal-body + .modal-foot) {
    display:flex; flex-direction:column; overflow:hidden;
  }
  dialog.modal[open] > form:has(> .modal-head + .modal-body + .modal-foot) {
    display:flex; flex-direction:column; min-height:0;
  }
  dialog.modal[open]:not(#print-preview,#image-dialog,.order-compose,.verify-page) .modal-head:has(+ .modal-body + .modal-foot),
  dialog.modal[open]:not(#print-preview,#image-dialog,.order-compose,.verify-page) .modal-body + .modal-foot {
    flex:none; position:static;
  }
  dialog.modal[open]:not(#print-preview,#image-dialog,.order-compose,.verify-page) .modal-head + .modal-body:has(+ .modal-foot) {
    min-height:0; overflow:auto; overscroll-behavior:contain; scroll-padding-block:12px;
  }
  dialog.modal .modal-head > .icon-btn { min-width:44px; min-height:44px; }
  dialog.modal .modal-foot .btn { max-width:100%; white-space:normal; overflow-wrap:anywhere; }
}

/* Popup tự dựng dùng chung khuôn .modal (rà đồng bộ đợt 3, C2/C3): Giao việc nhanh, Két (.cash-dialog),
   Đối chiếu sao kê / hộp kế toán (.acct-quick), ngăn kéo NCC (.sup-drawer). Chỉ đổi hình thức, giữ DOM và sự kiện. */
@media screen {
  :is(.task-quick-head, .cash-dialog-form > header, .bank-head, .sup-drawer-head) h2, .acct-quick form > h2 { color:var(--blue); font-size:var(--text-heading); }
  :is(.cash-dialog-form > header, .bank-head, .sup-drawer-head) > .icon-btn { flex:none; width:32px; height:32px; }
  .cash-dialog { border:0; box-shadow:var(--shadow-lg); }
  .cash-dialog-form > header { border-bottom-color:var(--line); }
  .cash-dialog-foot { gap:8px; border-top-color:var(--line); background:var(--surface-2); }
  .cash-field :is(input, select, textarea) { border-color:var(--line-strong); border-radius:var(--radius-sm); }
  .acct-quick { box-shadow:var(--shadow-lg); }
  .acct-quick-foot { flex-wrap:wrap; position:sticky; bottom:0; z-index:2; margin:4px -20px -20px; padding:14px 22px 18px; border-top:1px solid var(--line); background:var(--surface-2); }
  .sup-drawer-foot { justify-content:flex-end; }
  .sup-drawer-foot > [data-left] { margin-right:auto; }
}
/* Điện thoại: rộng hết màn hình, bo 12px hai góc trên như .modal; × 44×44; chân dính đáy; thanh dưới ẩn khi ngăn kéo NCC mở. */
@media screen and (max-width: 850px) {
  :is(.task-quick-dialog, .cash-dialog, .acct-quick, .sup-drawer) { width:100vw !important; max-width:100vw !important; border-radius:12px 12px 0 0; }
  :is(.task-quick-head, .cash-dialog-form > header, .bank-head, .sup-drawer-head) h2, .acct-quick form > h2 { font-size:18px; }
  :is(.task-quick-head, .cash-dialog-form > header, .bank-head, .sup-drawer-head) > .icon-btn { width:44px; height:44px; min-width:44px; min-height:44px; }
  .task-quick-head { padding:12px 16px 0; }
  .task-quick-body { gap:16px; padding:16px; }
  .task-quick-foot, .cash-dialog-foot { position:sticky; bottom:0; z-index:3; }
  .task-quick-foot { padding:10px 16px calc(10px + env(safe-area-inset-bottom)); background:var(--surface); }
  .cash-dialog-foot, .acct-quick-foot { background:var(--surface); padding-bottom:calc(12px + env(safe-area-inset-bottom)); }
  :is(.task-quick-foot, .acct-quick-foot) .btn { flex:1 1 auto; }
  .sup-form > .sup-drawer-foot { bottom:-18px; margin-inline:-20px; padding:12px 20px calc(12px + env(safe-area-inset-bottom)); }
  .sup-drawer > .sup-form:last-child > .sup-drawer-foot:last-child { margin-bottom:-18px; }
  .cash-field :is(input, select, textarea) { border-radius:10px; }
  body:has(.sup-drawer:not([hidden])) :is(.crm-mobile-nav, #reminder-launcher, #chat-launcher, #approval-launcher) { display:none !important; }
}
