/* Shared responsive layout; loaded after the active theme. */
html, body { height: 100%; }
body { height: 100dvh; }
.shell, .app-frame, .main-panel { min-width: 0; }
.view, .panel, .op-section, .field, .kpi-grid > *, .dk-grid > * { min-width: 0; }
img, video, canvas { max-width: 100%; }
.field input, .field select, .field textarea { min-width: 0; max-width: 100%; }
.table-wrap, .sp-table-wrap { max-width: 100%; overscroll-behavior-x: contain; }
.table-wrap:focus-visible, .sp-table-wrap:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
.mobile-menu-toggle, .sidebar-close, .sidebar-backdrop { display: none; }
.sidebar-group.collapsed .sidebar-group-body { visibility: hidden; }
.app-modal { display: flex; flex-direction: column; min-width: 0; max-height: calc(100dvh - 40px); }
.app-modal > h3, .app-modal-footer { flex-shrink: 0; }
.app-modal #modalHtml { min-height: 0; overflow: auto; overscroll-behavior: contain; }
.app-modal-footer { padding-top: 14px; gap: 8px; }

@media (max-width: 1100px) {
  body { padding: max(8px, env(safe-area-inset-top)) max(8px, env(safe-area-inset-right)) max(8px, env(safe-area-inset-bottom)) max(8px, env(safe-area-inset-left)); }
  .app-frame { flex-direction: row; gap: 0; }
  .main-panel { height: 100%; width: 100%; border-radius: 18px; }
  .topbar { padding: 12px; }
  .topbar-pill { gap: 8px; }
  .topbar-profile-text, .topbar-divider { display: none; }
  .topbar-nav-btn, .topbar-bell { width: 40px; height: 40px; flex-shrink: 0; }
  .mobile-menu-toggle { display: flex; }
  .sidebar {
    position: fixed; inset: 0 auto 0 0; z-index: 36; width: min(300px, calc(100vw - 48px)); height: 100dvh;
    display: flex; flex-direction: column; flex-wrap: nowrap; align-items: stretch; gap: 0;
    padding: max(16px, env(safe-area-inset-top)) 12px max(16px, env(safe-area-inset-bottom));
    background: var(--surface); border: 1px solid var(--border); border-radius: 0 20px 20px 0;
    transform: translateX(-100%); visibility: hidden; transition: transform var(--motion-fast) ease;
  }
  .sidebar-open .sidebar { transform: translateX(0); visibility: visible; }
  .sidebar-brand { position: relative; padding: 0 42px 14px; margin: 0; }
  .sidebar-brand .logo-mark { height: 64px; }
  .sidebar-close { display: flex; position: absolute; right: 0; top: 0; }
  .sidebar-scroll { display: block; flex: 1 1 auto; overflow-x: hidden; overflow-y: auto; overscroll-behavior: contain; }
  .sidebar-group { display: block; margin-bottom: 6px; }
  .sidebar-group[hidden], .sidebar-item[hidden] { display: none; }
  .sidebar-group-label { display: flex; min-height: 40px; }
  .sidebar-group-body, .sidebar-group.collapsed .sidebar-group-body { display: block; max-height: 600px; overflow: hidden; }
  .sidebar-group.collapsed .sidebar-group-body { max-height: 0; }
  .sidebar-item { min-height: 44px; white-space: normal; margin-bottom: 2px; }
  .sidebar-item .ic { display: flex; }
  .sidebar-backdrop:not([hidden]) { display: block; position: fixed; inset: 0; z-index: 35; width: 100%; height: 100%; border: 0; border-radius: 0; padding: 0; background: var(--text-primary); opacity: .4; cursor: default; }
  .view { padding: 20px; }
  .page-head > *, .page-head-search, .page-head-filters { min-width: 0; max-width: 100%; }
  .page-head-search { flex-shrink: 1; }
  .period-toggle { max-width: 100%; flex-wrap: wrap; border-radius: 16px; }
  .period-toggle button { min-height: 36px; }
  .kpi-grid, .kpi-grid.kpi-grid-4, .kpi-grid.kpi-grid-5, .kpi-grid.pd-kpi-5 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .kpi-grid.pm-kpi-3 { grid-template-columns: repeat(3, minmax(0, 1fr)); }
  .topbar .profile-menu { right: 12px; max-width: calc(100vw - 40px); }
  .notif-panel { max-height: calc(100dvh - 150px); }
  .np-list { min-height: 0; }
}

/* The desktop flex-basis would become a 360px height in a stacked header. */
@media (max-width: 780px) {
  .page-head > div:first-child { flex: 0 1 auto; }
}

@media (max-width: 760px) {
  .view { padding: 16px; }
  .form-grid, .code-grid, .receipt-meta, .op-info-grid.cols-2, .op-info-grid.cols-3,
  .rf-grid-3, .rf-acct, .pf-grid-2, .pd-info-grid, .op-summary, .op-form-grid,
  .cp-grid-3, .cp-grid-4, .tsx-head, .pp-inv, .cpp-trial, .cpp-asg-top { grid-template-columns: minmax(0, 1fr); }
  .form-grid > *, .code-grid > * { min-width: 0; grid-column: auto; }
  .theme-grid, .pf-color-list, .pf-size-list { grid-template-columns: repeat(auto-fit, minmax(min(240px, 100%), 1fr)); }
  .page-head, .section-head, .op-section-head, .pd-head { flex-wrap: wrap; gap: 12px; }
  .page-head { align-items: stretch; }
  .page-head-search { width: 100%; gap: 8px; }
  .page-head-actions { border-left: 0; padding-left: 0; }
  .page-head button, .page-head input, .page-head select { max-width: 100%; }
  .btn-zap { max-width: 100%; white-space: normal; overflow-wrap: anywhere; }
  .filters { flex-wrap: wrap; }
  .filters > * { min-width: 0; max-width: 100%; }
  .panel { overflow-x: visible; }
  .panel, .op-section { padding: 16px; }
  .panel > .table-wrap, .op-section > .table-wrap { width: 100%; }
  .table-wrap.sticky-actions th:last-child, .table-wrap.sticky-actions td:last-child { position: static; box-shadow: none; }
  .form-footer { gap: 8px; }
  .form-footer button { min-height: 44px; }
  .thumb-toggle, .icon-edit-btn, .profile-menu-item { min-width: 44px; min-height: 44px; }
  .op-kv > div { gap: 12px; }
  .op-kv b, .pd-info-grid .op-kv b { overflow-wrap: anywhere; }
  .sd-meta, .sd-sec .sd-meta.sd-meta-4 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .sd-head, .total-bar { flex-wrap: wrap; }
  .app-modal.wide, .app-modal:has(.sd-head), .app-modal:has(#spBody) { width: 100%; max-width: 100%; }
  .modal-overlay { padding: max(8px, env(safe-area-inset-top)) max(8px, env(safe-area-inset-right)) max(8px, env(safe-area-inset-bottom)) max(8px, env(safe-area-inset-left)); }
  .app-modal { padding: 16px; max-height: calc(100dvh - 16px); }
  .app-modal:has(.sd-head) { max-height: calc(100dvh - 16px); }
  .app-modal:has(.sd-head) #modalHtml { margin: 0; padding: 0; }
  .app-modal-footer button { min-height: 44px; max-width: 100%; }
  .app-toast { width: max-content; max-width: calc(100vw - 32px); }
  .auth-screen { padding: max(8px, env(safe-area-inset-top)) 8px max(8px, env(safe-area-inset-bottom)); }
  .auth-left { min-width: 0; padding: 16px 12px; gap: 20px; }
  .auth-form { min-width: 0; }
  .auth-demo, .profile-menu-head { overflow-wrap: anywhere; }
  .auth-title { font-size: 28px !important; }
}

@media (max-width: 600px) {
  .topbar { padding: 8px; }
  .topbar-pill { flex-wrap: wrap; border-radius: 18px; gap: 6px; }
  .topbar-search { display: flex; order: 10; flex: 1 0 100%; min-height: 40px; }
  .topbar-profile { margin-left: auto; min-height: 40px; }
  .topbar-avatar { width: 32px; height: 32px; }
  .topbar-search input[type="text"] { font-size: 16px; }
  .notif-panel { top: 120px; left: 16px; right: 16px; width: auto; }
  .view { padding: 16px 12px; }
  .view h1 { overflow-wrap: anywhere; }
  .kpi-grid, .kpi-grid.kpi-grid-2, .kpi-grid.kpi-grid-4, .kpi-grid.kpi-grid-5,
  .kpi-grid.pd-kpi-5, .kpi-grid.pm-kpi-3, .dk-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 8px; }
  .kpi-value { font-size: clamp(16px, 4.4vw, 22px); white-space: nowrap; }
  .dk-val { overflow-wrap: anywhere; }
  .period-toggle { gap: 2px; }
  .period-toggle button { flex: 1 1 auto; white-space: normal; }
  .auth-row { flex-wrap: wrap; gap: 12px; }
  .field input:not([type="checkbox"]):not([type="radio"]), .field select, .field textarea,
  .auth-form input[type="text"], .auth-form input[type="password"] { font-size: 16px; min-height: 44px; }
  .app-modal .code-grid, .app-modal .sd-meta, .app-modal .sd-sec .sd-meta.sd-meta-4 { grid-template-columns: minmax(0, 1fr); }
  .app-modal-footer { flex-wrap: wrap; }
  .app-modal-footer button { flex: 1 1 auto; }
  .app-modal.has-primary #modalClose { margin-right: 0; }
}

@media (max-width: 380px) {
  .dk-grid { grid-template-columns: minmax(0, 1fr); }
  .kpi-value { font-size: 15px; }
}

@media (max-width: 340px) {
  .kpi-grid, .kpi-grid.kpi-grid-2, .kpi-grid.kpi-grid-4, .kpi-grid.kpi-grid-5,
  .kpi-grid.pd-kpi-5, .kpi-grid.pm-kpi-3 { grid-template-columns: minmax(0, 1fr); }
}

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