/* Phone (iPhone web app) layer: loaded after app.css, changes nothing on desktop (safe-area insets are 0 there). */
:root { --sat: env(safe-area-inset-top, 0px); --sab: env(safe-area-inset-bottom, 0px); --sal: env(safe-area-inset-left, 0px); --sar: env(safe-area-inset-right, 0px); }

/* 100vh first (old browsers), then 100dvh: the bar that slides in and out on iPhone does not cut the page */
@media screen {
  body { min-height: 100vh; min-height: 100dvh; }
  .shell, .auth { min-height: 100vh; min-height: 100dvh; }
  .side { height: calc(100vh - var(--head) - var(--sat)); height: calc(100dvh - var(--head) - var(--sat)); }
  .modal-box { max-height: calc(100vh - 32px); max-height: calc(100dvh - 32px); }
}

/* the status bar is translucent (black-translucent): the topbar grows by the notch height and keeps its content below it */
.shell { grid-template-rows: calc(var(--head) + var(--sat)) 1fr; }
.topbar { height: calc(var(--head) + var(--sat)); padding-top: var(--sat); padding-inline: max(var(--s4), var(--sal), var(--sar)); }
.side { top: calc(var(--head) + var(--sat)); }
.main { padding-bottom: max(var(--s7), calc(var(--s4) + var(--sab))); }
.lic-banner { padding-inline: max(var(--s5), var(--sal), var(--sar)); }
.auth { padding-bottom: max(var(--s5), var(--sab)); }
#toast { bottom: max(var(--s4), calc(var(--sab) + var(--s2))); }
body.kb-open #toast { visibility: hidden; } /* a toast would float over the keyboard */

@media (max-width: 900px) {
  .topbar { padding-inline: max(var(--s2), var(--sal), var(--sar)); }
  .side { top: 0; height: auto; bottom: 0; padding-top: calc(var(--s5) + var(--sat)); padding-bottom: calc(var(--s4) + var(--sab)); padding-inline-start: max(var(--s3), var(--sar)); }
  .lic-banner { padding-inline: max(var(--s3), var(--sal), var(--sar)); }
  .main { padding-inline: max(var(--s3), var(--sal), var(--sar)); }
}

@media (max-width: 700px) {
  /* 16px stops iOS from zooming the page when a field gets focus */
  input:not([type=checkbox]):not([type=radio]):not([type=file]):not(.code), select, textarea { font-size: 16px !important; }

  /* dialog = bottom sheet: sticks to the bottom edge, scrolls inside, its last button row stays visible */
  .modal { place-items: end stretch; padding: 0; }
  .modal-box { max-width: none; max-height: 85vh; max-height: 85dvh; border-radius: var(--r-lg) var(--r-lg) 0 0; padding: var(--s4) max(var(--s4), var(--sal), var(--sar)) 0; overscroll-behavior: contain; }
  .modal-box .btn-row:last-child { position: sticky; bottom: 0; background: var(--surface); padding: var(--s2) 0 max(var(--s3), var(--sab)); box-shadow: 0 -8px 8px -8px rgba(10, 23, 48, .25); }
  .modal-box:has(.mx-host) { max-width: none; }
  .modal-box { padding-bottom: max(16px, var(--sab)); }
  .modal-box::before { content: ''; justify-self: center; width: 40px; height: 4px; border-radius: 2px; background: var(--line-strong); margin-top: -4px; } /* drag handle: decoration only */
  .modal-box .btn-row:last-child { padding-bottom: max(16px, var(--sab)); }
  .modal-box:has(.btn-row:last-child) { padding-bottom: 0; }

  /* tap targets >= 44px: dialog close, topbar buttons, table buttons, checkbox rows */
  .modal-h .btn, .topbar .btn, .topbar summary, .bell, .tbl td .btn, .pop a, .pop button { min-height: 44px; min-width: 44px; }
  .field.check label, .rc, .rc-opt { min-height: 44px; }
  .field.check label { width: 100%; justify-content: flex-start; gap: var(--s3); }
  .field.check label > span { flex: 1; min-width: 0; text-align: start; }
  .field.check input { width: 24px; height: 24px; }
  .tbl td:has(input[type=checkbox]) { min-height: 44px; } .tbl td input[type=checkbox] { width: 24px; height: 24px; }

  /* letter view: readable card instead of the shrunk A4 sheet (the sheet opens behind «نمای چاپی») */
  .lt-card { margin: var(--s3) 0; padding: var(--s4); border: 1px solid var(--line-strong); border-radius: var(--r-md); background: var(--surface); }
  .lt-meta { display: flex; flex-wrap: wrap; gap: var(--s1) var(--s4); margin: 0 0 var(--s3); color: var(--ink-3); font-size: var(--t-sm); }
  .lt-text { white-space: pre-wrap; overflow-wrap: anywhere; font-size: 16px; line-height: 2; }
  .lt-signs { display: flex; flex-wrap: wrap; gap: var(--s4); margin-top: var(--s4); padding-top: var(--s3); border-top: 1px solid var(--line); }
  .lt-signs > div { text-align: center; min-width: 120px; } .lt-signs b, .lt-signs span { display: block; font-size: var(--t-sm); }
  .lt-signs img { display: block; margin: 0 auto 4px; max-height: 60px; max-width: 140px; object-fit: contain; }
  .lt-paper { margin-top: var(--s3); }
  .lt-paper.closed { height: 0; overflow: hidden; visibility: hidden; margin: 0; } /* folded, but laid out: the sheet is measured and ready for print */
}

@media (max-width: 400px) {
  .topbar { gap: 2px; padding-inline: max(6px, var(--sal), var(--sar)); }
  .brand img { width: 30px; height: 30px; }
  .pos { display: none; } /* no room: one position = nothing to change; several = the switcher is the first row of the user menu */
  .pos-menu { display: grid; gap: 4px; padding: var(--s2) var(--s3); border-bottom: 1px solid var(--line); font-size: var(--t-xs); color: var(--ink-3); }
  .pos-menu select { min-height: 44px; max-width: 100%; }
  .badge { top: 4px; inset-inline-end: -2px; min-width: 16px; height: 16px; line-height: 16px; font-size: 10px; padding: 0 3px; }
  .avatar { width: 30px; height: 30px; }
  .page-h h1 { font-size: var(--t-lg); }
}

/* the position switcher lives in the user menu only on narrow phones */
.pos-menu { display: none; }
@media (max-width: 400px) { .pos-menu { display: grid; } }

/* Persian file picker (fileField in util.js): real input hidden but focusable */
.filepick { display: inline-flex; align-items: center; gap: var(--s3); flex-wrap: wrap; position: relative; max-width: 100%; }
.fp-input { position: absolute; width: 1px; height: 1px; min-height: 0 !important; padding: 0; margin: -1px; overflow: hidden; clip: rect(0 0 0 0); border: 0 !important; opacity: 0; }
.fp-input:focus-visible + label { outline: 2px solid var(--brand); outline-offset: 2px; }
.fp-name { color: var(--ink-3); font-size: var(--t-sm); overflow-wrap: anywhere; min-width: 0; }

/* print on a phone: the A4 sheet, not the phone card (the sheet is always drawn, folded shut on screen) */
@media print {
  .lt-card, .lt-paper-bar { display: none !important; }
  .lt-paper.closed { height: auto; overflow: visible; visibility: visible; margin: 0; }
}

/* date picker sheet (jalali-picker.js) */
.jp { display: grid; gap: var(--s3); }
.jp-head { display: flex; align-items: center; justify-content: space-between; gap: var(--s2); }
.jp-title { font-size: var(--t-md); }
.jp-nav { min-width: 44px; min-height: 44px; font-size: 22px; line-height: 1; padding: 0; }
.jp-grid { display: grid; grid-template-columns: repeat(7, 1fr); gap: 4px; direction: rtl; }
.jp-wd { text-align: center; color: var(--ink-3); font-size: var(--t-sm); padding: var(--s1) 0; }
.jp-day { min-height: 44px; min-width: 0; border: 1px solid transparent; border-radius: var(--r-md); background: var(--surface); color: var(--ink); font: inherit; font-size: 16px; cursor: pointer; }
.jp-day.now { border-color: var(--brand); }
.jp-day.on { background: var(--brand); color: #fff; font-weight: 700; }
.a2hs { position: fixed; inset-inline: max(var(--s2), var(--sal)); bottom: max(var(--s2), calc(var(--sab) + var(--s2))); z-index: 40; display: flex; align-items: center; gap: var(--s2); padding: var(--s2) var(--s3); background: var(--surface); border: 1px solid var(--line-strong); border-radius: var(--r-lg); box-shadow: 0 4px 16px rgba(10, 23, 48, .2); font-size: var(--t-sm); }
.a2hs.pushbar .btn:not(.quiet) { min-width: 44px; white-space: nowrap; }
@media (min-width: 700px) { .a2hs.pushbar { inset-inline: auto; inset-inline-start: var(--s4); max-width: 460px; } } /* desktop: a small bar in the corner, not the full width */
.a2hs span { flex: 1; min-width: 0; } .a2hs .nowrap { white-space: nowrap; } .a2hs .btn { min-width: 44px; min-height: 44px; }

/* permissions matrix on a phone: every feature row becomes a card, every group/position a labelled line (same checkboxes, same save) */
@media (max-width: 699px) {
  .mx-w { max-height: none; overflow: visible; }
  .mx, .mx tbody, .mx tr, .mx th, .mx td { display: block; width: 100%; }
  .mx thead { display: none; }
  .mx tr[data-code] { border: 1px solid var(--line); border-radius: var(--r-md); margin-bottom: var(--s3); background: var(--surface); overflow: hidden; }
  .mx .mx-f { position: static; max-width: none; min-width: 0; text-align: start; border-bottom: 1px solid var(--line); }
  .mx td { display: flex; align-items: center; justify-content: space-between; gap: var(--s3); text-align: start; padding: var(--s1) var(--s3); }
  .mx td::before { content: attr(data-label); flex: 1; min-width: 0; font-size: var(--t-sm); }
  .mx .mx-c { flex-direction: row; gap: var(--s2); }
  .mx .mx-g th { position: static; padding: var(--s2) 0; background: transparent; border: 0; }
}
