/* Responsive fixes (loaded after app.css + portal.css). CSS only. */

/* Currency picker + exchange rates */
.cur-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); gap: 8px; }
.cur-chip { display: flex; align-items: center; gap: 10px; padding: 10px 12px; text-align: start; min-width: 0; }
.cur-chip .cur-sym { width: 34px; height: 34px; border-radius: 10px; display: grid; place-items: center; background: var(--surface-2, rgba(0,0,0,.05)); font-weight: 700; flex: none; font-size: 13px; }
.cur-chip .grow { display: flex; flex-direction: column; min-width: 0; }
.cur-chip .cur-name { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.fx-list { display: flex; flex-direction: column; gap: 6px; }
.fx-row { display: flex; align-items: center; gap: 10px; padding: 8px 10px; border: 1px solid var(--border, #e5e7eb); border-radius: 10px; flex-wrap: wrap; }
.fx-row.fx-local { border-color: var(--brand, #1D4ED8); background: color-mix(in srgb, var(--brand, #1D4ED8) 6%, transparent); }
.fx-row .fx-l { white-space: nowrap; }
.fx-row .fx-in { width: 130px; flex: none; }
.fx-row .fx-r { min-width: 0; }
.seg { display: inline-flex; border: 1px solid var(--border, #e5e7eb); border-radius: 10px; padding: 3px; gap: 3px; flex-wrap: wrap; max-width: 100%; }
.seg button { border: 0; background: transparent; padding: 8px 14px; border-radius: 8px; cursor: pointer; color: inherit; font: inherit; }
.seg button.on { background: var(--brand, #1D4ED8); color: #fff; }
.seg button.on .muted { color: rgba(255,255,255,.8); }
.eq-line { margin-top: 4px; }
.eq { display: block; font-size: 12px; font-weight: 400; color: var(--muted, #6b7280); }
.pay-by-cur .pay-cur { padding: 10px 14px; border: 1px solid var(--border, #e5e7eb); border-radius: 12px; min-width: 150px; }
.sheet .eq { white-space: nowrap; }
.cur-tag { display: inline-block; font-size: 11px; padding: 1px 6px; border-radius: 6px; background: var(--surface-2, rgba(0,0,0,.06)); margin-inline-start: 4px; vertical-align: middle; }
@media (max-width: 560px) {
  .cur-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .fx-row .fx-in { width: 110px; }
}

/* 1. Grid tracks must be allowed to shrink below their content (long select options, nowrap names) */
.grid > *, .form-grid > * { min-width: 0; }
.input, .select, .textarea { min-width: 0; max-width: 100%; }
@media (max-width: 640px) {
  .grid-2, .grid-3, .grid-4, .form-grid, .form-grid.cols-3 { grid-template-columns: minmax(0, 1fr); }
}

/* 2. Fixed-size controls must never be squeezed by long neighbours (top bar, modal head) */
.topbar > .btn, .topbar > .menu-btn, .topbar-brand, .modal-head > .btn, .g-head > .btn { flex-shrink: 0; }
.modal-head { gap: 10px; }
.modal-head h2 { min-width: 0; overflow-wrap: anywhere; }

/* 3. Alerts: text gets the room, a trailing button drops to its own line instead of pushing out */
.alert > .grow { min-width: 0; }
.alert > .btn { flex-shrink: 0; }
@media (max-width: 640px) {
  .alert { flex-wrap: wrap; }
  .alert > .grow { flex: 1 1 0; }
  .alert > .btn { flex-shrink: 1; min-width: 0; max-width: 100%; margin-inline-start: auto; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; display: inline-block; line-height: 34px; }
}

/* 4. Touch targets on phones */
@media (max-width: 640px) {
  .topbar .btn-icon { width: 42px; min-width: 42px; }
  .pill { min-height: 36px; }
  button.badge { min-height: 32px; padding: 4px 12px; }
  .oc-toggle { width: 30px; height: 30px; flex-shrink: 0; }
  .oc-tree .row > span:first-child { width: 30px !important; flex-shrink: 0; }
  .cell-input { height: 36px; }
  .cu-thumb .cu-x { width: 30px; height: 30px; }
  label.row > input[type="checkbox"], label.row > input[type="radio"], .check input { width: 20px; height: 20px; flex-shrink: 0; accent-color: var(--brand); }
}
.toast-x { min-width: 32px; min-height: 32px; }

/* 5. Hatchable badge + bottom nav: lift the badge above the tab bar, keep the last content clear of both */
@media (max-width: 900px) {
  body:has(.app.has-bottom-nav) #hb-badge { bottom: calc(72px + env(safe-area-inset-bottom, 0px)) !important; }
  .has-bottom-nav .content { padding-bottom: calc(124px + env(safe-area-inset-bottom, 0px)); }
}
/* …but while an overlay is open the bottom nav is covered anyway: badge back to its corner, clear of modal footers */
@media (max-width: 900px) {
  body:has(.app.has-bottom-nav):has(.modal-back, .dlg-back, .g-back, .sidebar.open, .cu-lb) #hb-badge { bottom: calc(14px + env(safe-area-inset-bottom, 0px)) !important; }
}
/* Guide popup is a bottom sheet on phones: leave a strip for the badge so its Next/Prev buttons stay tappable */
@media (max-width: 640px) {
  .g-back { padding-bottom: calc(52px + env(safe-area-inset-bottom, 0px)) !important; }
  .g-modal { max-height: calc(100vh - 60px) !important; max-height: calc(100dvh - 60px - env(safe-area-inset-bottom, 0px)) !important; }
}
/* Sidebar: wherever the sidebar sits under the bottom-corner badge (RTL at every width, LTR off-canvas on phones),
   reserve a sticky strip at its bottom so links scroll beneath the strip instead of beneath the badge */
[dir="rtl"] .sidebar::after { content: ""; display: block; flex-shrink: 0; position: sticky; bottom: 0; min-height: calc(52px + env(safe-area-inset-bottom, 0px)); background: var(--sidebar-bg); }
@media (max-width: 900px) {
  .sidebar::after { content: ""; display: block; flex-shrink: 0; position: sticky; bottom: 0; min-height: calc(52px + env(safe-area-inset-bottom, 0px)); background: var(--sidebar-bg); }
}

/* 6. Modals at every width: never taller than the screen, body scrolls inside, header/footer always visible */
.modal, .modal.wide { display: flex; flex-direction: column; max-height: calc(100vh - 12vh); max-height: calc(100dvh - 12vh); }
.modal-head, .modal-foot { flex-shrink: 0; }
.modal-body { flex: 1 1 auto; min-height: 0; overflow-y: auto; overscroll-behavior: contain; }
@media (max-width: 640px) {
  .modal, .modal.wide { max-height: calc(100vh - 78px); max-height: calc(100dvh - 78px); }
}

/* 7. Top bar on narrow desktops/tablets (sidebar visible, ~700-1000px for content): company name shrinks, e-mail hides */
.topbar > .dropdown:not(:last-child) { min-width: 0; flex: 0 1 auto; }
.org-switch { min-width: 0; max-width: min(320px, 100%); }
.org-switch > .ellipsis { min-width: 0; }
.topbar > .dropdown:last-child, .topbar > .lang-btn, .topbar > .guide-btn { flex-shrink: 0; }
@media (max-width: 1280px) {
  .user-chip > .hide-sm { display: none; }
}

/* 8. Room under the last content so the bottom-corner badge never sits on final buttons (e.g. form Save in LTR) */
@media (min-width: 901px) {
  .content { padding-bottom: 64px; }
}

/* 9. Toolbars on phones: wrapped action buttons (e.g. "Record leave" inside a <span>) take a full row */
@media (max-width: 640px) {
  .toolbar > span:has(> .btn) { flex-basis: 100%; }
  .toolbar > span > .btn { width: 100%; }
  .btn { max-width: 100%; }
}

/* Guide popup at tablet/desktop: keep its footer (Next/Prev) above the badge */
@media (min-width: 641px) {
  .g-back { padding-bottom: 60px !important; }
  .g-modal { max-height: calc(100vh - 80px) !important; max-height: calc(100dvh - 80px) !important; }
}

/* 10. Tablets: long names inside regular tables wrap instead of forcing a horizontal scroll that hides the last column */
@media (min-width: 641px) and (max-width: 1100px) {
  table.table:not(.sheet) td .ellipsis { white-space: normal; overflow-wrap: anywhere; }
  table.table:not(.sheet) td .badge { white-space: nowrap; }
}

/* 11. Long KPI values wrap instead of overflowing their card */
.kpi-value { overflow-wrap: anywhere; }

/* 12. Desktop scrollbars: the page no longer scrolls the window. The sidebar and the main area each scroll on their own.
   In RTL the outermost (far-right) scrollbar belongs to the sidebar menu, and the inner one (next to the menu) scrolls the page. */
@media screen and (min-width: 901px) {
  html:has(.app), html:has(.app) body { height: 100%; overflow: hidden; }
  .app { height: 100vh; height: 100dvh; min-height: 0; }
  .sidebar { height: 100vh; height: 100dvh; overflow-y: auto; }
  .main { height: 100vh; height: 100dvh; overflow-y: auto; overflow-x: hidden; }
  [dir="rtl"] .sidebar, [dir="rtl"] .main { direction: ltr; }
  [dir="rtl"] .sidebar > *, [dir="rtl"] .main > * { direction: rtl; }
}