/* Bounded app rows: content scrolls, navigation and actions stay reachable. */
html, body, #root { min-width: 0 !important; width: 100%; }
#root .app-shell { height: 100vh; height: 100dvh; grid-template-rows: minmax(0, 1fr); min-height: 0; }
#root .app-shell > .workspace { height: 100%; min-height: 0; min-width: 0; }
#root .sidebar { height: 100%; min-height: 0; overflow: hidden; }
#root .sidebar > .brand, #root .sidebar > .account-menu-root { flex: 0 0 auto; }
#root .sidebar > .nav-list { flex: 1 1 auto; min-height: 0; overflow-y: auto; overscroll-behavior: contain; }
#root .sidebar .nav-list > * { flex-shrink: 0; }
#root .sidebar .account-menu-root { margin-top: 8px; }
#root .workspace > .topbar { flex-shrink: 0; min-width: 0; }
#root .workspace:is(.dashboard-workspace,.integrations-workspace,.settings-workspace) { overflow: auto; overscroll-behavior: contain; }
#root .entity-card, #root .messages-layout { grid-template-rows: minmax(0, 1fr); }
#root :is(.entity-main,.entity-inspector,.inbox-pane,.dialog-pane,.draft-pane) { min-height: 0; min-width: 0; }
#root :is(.entity-main,.entity-inspector,.dialog-pane,.draft-pane) > :is(header,footer,.dialog-head,.dialog-order-actions,.message-composer,.draft-head,.draft-actions) { flex-shrink: 0; }
#root :is(.entity-table-wrap,.entity-inspector-scroll,.messages-feed,.draft-scroll,.conversation-list) { min-height: 0; overscroll-behavior: contain; }
#root .message-composer { flex: 0 0 auto; min-width: 0; }
#root .message-composer textarea { min-width: 0; max-height: min(144px, 24dvh); }
#root .dialog-order-actions { flex-wrap: wrap; }
#root .dialog-order-actions > button { min-width: 0; white-space: normal; }
#root :is(.table-footer,.entity-table-footer,.operations-footer,.warehouse-pagination) { flex-shrink: 0; flex-wrap: wrap; gap: 8px; }

/* One viewport budget for dialogs, including short laptop windows. */
.modal-backdrop { padding: 12px !important; grid-template-rows: minmax(0,1fr); overflow: auto; }
.modal-backdrop > :is(.modal,[class$="-modal"],.order-modal-window,.new-order-workspace) {
  min-height: 0 !important; max-height: calc(100vh - 24px) !important; max-height: calc(100dvh - 24px) !important;
  max-width: calc(100vw - 24px) !important; overflow-y: auto;
}
.modal-backdrop > :is(.modal,[class$="-modal"],.order-modal-window,.new-order-workspace) > :is(header,footer,.modal-head,.modal-actions,.np-modal-head,.np-modal-actions,.inspector-head,.inspector-actions) { flex-shrink: 0; }
.modal-backdrop > :is(.modal,[class$="-modal"],.order-modal-window) > :is([class$="-body"],.inspector-scroll,.account-profile-scroll) { min-height: 0 !important; flex: 1 1 auto; overflow-y: auto; overscroll-behavior: contain; }
.modal-backdrop .np-ttn-modal, .modal-backdrop .np-settings-modal { display: flex; flex-direction: column; overflow: hidden; }
.modal-backdrop :is(.np-ttn-body,.np-settings-body) { min-height: 0 !important; max-height: none !important; flex: 1 1 auto; overflow: auto; }
.modal-backdrop :is(.np-modal-head,.np-modal-actions) { flex: 0 0 auto; }
.modal-backdrop .np-modal-actions { flex-wrap: wrap; }
.modal-backdrop .np-modal-actions > div { flex: 1 1 220px; }

@media (max-height: 700px) {
  #root .sidebar { padding-top: 12px; padding-bottom: 10px; }
  #root .workspace > .topbar { height: 70px; }
  .modal-backdrop .np-modal-head { min-height: 68px; padding-block: 10px; }
  .modal-backdrop .np-modal-actions { min-height: 54px; padding-block: 8px; }
}
@media (max-width: 1200px) {
  #root .app-shell, #root .app-shell.messages-mode { grid-template-columns: 180px minmax(0,1fr); }
  #root .workspace { padding-inline: 12px; }
  #root .messages-layout { grid-template-columns: 230px minmax(300px,1fr) 300px; overflow-x: auto; }
  #root .topbar { gap: 10px; }
  #root .topbar > div { min-width: 0; }
}
@media (max-width: 1050px) {
  #root .messages-layout { display: flex; overflow-x: auto; scroll-snap-type: x proximity; }
  #root .messages-layout > .inbox-pane { flex: 0 0 240px; }
  #root .messages-layout > .dialog-pane { flex: 1 0 360px; }
  #root .messages-layout > .draft-pane { flex: 0 0 320px; }
  #root .messages-layout > * { height: 100%; scroll-snap-align: start; }
  #root .entity-card { position: relative; grid-template-columns: minmax(0,1fr); }
  #root .entity-card > .entity-inspector { position: absolute; inset: 0 0 0 auto; width: min(340px,100%); z-index: 15; box-shadow: -8px 0 24px #17213a20; }
  #root .products-card .entity-table { min-width: 640px; }
}
@media (max-width: 700px) {
  #root .app-shell, #root .app-shell.messages-mode { grid-template-columns: minmax(0,1fr); grid-template-rows: 104px minmax(0,1fr); }
  #root .sidebar { position: relative; padding: 6px 8px; display: grid; grid-template-columns: minmax(0,1fr) auto; grid-template-rows: 42px minmax(0,1fr); gap: 4px; border-right: 0; border-bottom: 1px solid var(--border); overflow: visible; }
  #root .sidebar > .brand { padding: 0; height: 42px; min-height: 0; font-size: 12px; }
  #root .sidebar > .nav-list { grid-column: 1 / -1; grid-row: 2; flex-direction: row; align-items: center; overflow-x: auto; overflow-y: hidden; gap: 4px; }
  #root .sidebar .nav-list > :is(button,a) { height: 36px; white-space: nowrap; padding: 0 10px; margin: 0; }
  #root .sidebar .nav-list .lite-switch { width: auto; min-width: 112px; margin: 0; }
  #root .sidebar > .account-menu-root { grid-column: 2; grid-row: 1; margin: 0; align-self: center; width: 146px; }
  #root .workspace { padding: 0 8px 8px; }
  #root .workspace > .topbar { height: auto; min-height: 72px; padding-block: 10px; flex-wrap: wrap; gap: 8px; }
  #root .topbar h1 { font-size: 20px; }
  #root .topbar p { font-size: 11px; }
  #root .topbar-actions { flex-wrap: wrap; gap: 6px; }
  #root .messages-layout > .inbox-pane { flex-basis: 100%; }
  #root .messages-layout > .dialog-pane { flex: 0 0 100%; }
  #root .messages-layout > .draft-pane { flex-basis: 100%; }
  #root .messages-layout { scroll-snap-type: x mandatory; }
  #root .dialog-head { min-width: 0; }
  #root .orders-card .table-wrap > table { min-width: 900px; }
  #root .entity-table-wrap > table { min-width: 600px; }
  #root .dashboard-reference-grid { height: auto; grid-template-columns: minmax(0,1fr); grid-template-rows: none; }
  #root .dashboard-reference-grid > * { grid-column: 1 !important; grid-row: auto !important; min-height: 240px; }
  #root .overview-metrics { display: grid; grid-template-columns: repeat(2,minmax(0,1fr)); height: auto; }
  #root .integrations-shell, #root .settings-shell { display: flex; flex-direction: column; height: auto; min-height: 0; overflow: visible; }
  #root .integrations-shell > *, #root .settings-shell > * { min-width: 0; max-width: 100%; flex: 0 0 auto; overflow: auto; }
  #root .warehouse-tabs { overflow-x: auto; flex-wrap: nowrap; flex-shrink: 0; }
  #root .warehouse-tabs > * { flex-shrink: 0; white-space: nowrap; }
  .modal-backdrop .np-ttn-grid, .modal-backdrop .np-ttn-payment-controls, .modal-backdrop .np-ttn-summary { grid-template-columns: minmax(0,1fr); }
  .modal-backdrop .np-modal-head { padding: 10px; gap: 8px; }
  .modal-backdrop .np-modal-head h2 { font-size: 17px; }
  .modal-backdrop .np-ttn-body { padding: 12px; }
  .modal-backdrop .np-modal-actions { padding: 8px 10px; }
  .modal-backdrop .np-modal-actions > div { flex-basis: 100%; }
  .modal-backdrop .order-modal-window .inspector-scroll { grid-template-columns: minmax(0,1fr); grid-template-areas: "comments" "client" "delivery" "products" "history"; padding: 10px; }
  .modal-backdrop .modal-status-footer { max-height: 30dvh; overflow: auto; }
}
/* POS has its own shell; it must not retain the former 700px floor. */
#root .pos-shell { min-height: 0; min-width: 0; height: 100vh; height: 100dvh; }
#root .pos-workspace { min-height: 0; }
#root .pos-statusbar { overflow-x: auto; }
#root .pos-statusbar > * { flex-shrink: 0; }
@media (max-width: 1050px) {
  #root .pos-shell { grid-template-rows: auto minmax(0,1fr) 50px; }
  #root .pos-topbar { grid-template-columns: minmax(0,1fr) minmax(0,1fr); }
  #root .pos-topbar > * { min-width: 0; min-height: 54px; }
  #root .pos-workspace { overflow-x: auto; grid-template-columns: minmax(300px,1fr) 350px; }
}
@media (max-height: 450px) {
  #root .workspace { overflow: auto; }
  #root .workspace > :is(.entity-card,.orders-card,.warehouse-shell,.messages-layout) { min-height: 220px; flex-shrink: 0; }
  .modal-backdrop > :is(.modal,[class$="-modal"],.order-modal-window,.new-order-workspace) { overflow-y: auto; }
}
.viewport-chat-navigation { display: none; }
@media (max-width: 1050px) {
  #root .viewport-chat-navigation { display: flex; flex: 0 0 auto; gap: 6px; padding: 0 0 8px; }
  #root .viewport-chat-navigation button { flex: 1; min-height: 34px; padding: 6px 10px; border: 1px solid var(--border); border-radius: 8px; color: var(--text-secondary); background: var(--surface); font: inherit; font-size: 12px; cursor: pointer; }
  #root .viewport-chat-navigation button[aria-pressed="true"] { color: var(--primary); background: var(--primary-soft); border-color: var(--primary); }
}
/* Popovers remain outside the independently scrolling navigation. */
#root .sidebar { overflow: visible; }
#root .sidebar .account-popover { max-height: calc(100dvh - 100px); overflow-y: auto; }
@media (max-width: 700px) {
  #root .sidebar .user-card { height: 42px; padding-block: 4px; }
  #root .sidebar .account-popover { top: calc(100% + 8px); bottom: auto; left: auto; right: 0; max-width: calc(100vw - 16px); max-height: calc(100dvh - 72px); }
}
