/* ==========================================================================
   OrthoMetry PWA — responsive layer
   File: pwa/pwa-responsive.css
   --------------------------------------------------------------------------
   ADDITIVE ONLY. This stylesheet is loaded by the staged PWA build AFTER every
   renderer stylesheet (styles.css -> ui-v2.css -> ui-refresh.css ->
   studio-v2.css -> clinical-theme.css -> feature CSS). It never re-declares
   the desktop layout; it only does two things:

   1. Styles the PWA shell chrome that pwa/pwa-boot.js and pwa/pwa-auth.js
      create in the browser: #pwaMobileBar, #pwaMenuBtn, #pwaMobileTitle,
      #pwaRefreshBtn, #pwaDrawerBackdrop and the #pwaPairingGate dialog. Those
      ids do not exist in the Electron renderer, so those rules are inert on
      Desktop.
   2. Reflows the shared renderer DOM inside two explicit breakpoints:

         @media (max-width: 430px)                          phones
         @media (min-width: 431px) and (max-width: 1024px)   tablets

      plus @media print and @media (display-mode: standalone) (both shell-only),
      and (prefers-reduced-motion: reduce) nested inside the 1024px bound.

   Consequently, every layout-changing declaration below is either
   (a) scoped to a shell id / body.pwa-drawer-open that only the PWA boot can
   create, or (b) wrapped in an @media query that cannot match a Desktop
   window wider than 1024px. Desktop rendering above 1024px is untouched.

   Invariants:
   - No palette colours are hardcoded: only existing theme custom properties
     (var(--bg-card), var(--text-primary), ...) with neutral fallbacks.
   - No DOM ids or classes are renamed, moved or removed. Every selector is
     additive and targets markup that already exists in renderer/index.html.
   - Renderer/app.js writes an inline transform chain on every Studio slot
     image (translate -> scale -> scaleX -> scaleY -> rotate). This file never
     declares that property for slot images and never changes their transform
     origin; only sizing, object-fit and image-orientation are adjusted so the
     inline chain keeps winning.
   - !important is only used for PWA shell state toggles (hidden / visible),
     for renderer declarations that are themselves !important, and for the
     full-bleed and bottom-sheet geometry overrides where a renderer feature
     stylesheet could re-declare the property at an equal specificity later.
     Each case is annotated at the point of use.
   ========================================================================== */

/* ==========================================================================
   1. Pairing gate (created by pwa/pwa-auth.js)
   Scoped to shell ids only — never rendered on Desktop.
   ========================================================================== */

#pwaPairingGate {
  position: fixed;
  inset: 0;
  z-index: 1300;
  display: flex;
  align-items: center;
  justify-content: center;
  /* !important: pwa/pwa-auth.js sets an inline 20px padding when it builds
     the gate; the safe-area insets must still apply on notched devices. */
  padding: max(16px, env(safe-area-inset-top, 0px)) 16px max(16px, env(safe-area-inset-bottom, 0px)) !important;
  background: var(--bg-primary, #0b1220);
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
}

/* The auth script may hide the gate with the [hidden] attribute; the UA
   rule is weaker than the id selector above, so restate the hidden state. */
#pwaPairingGate[hidden] {
  display: none !important;
}

#pwaPairingForm {
  display: grid;
  gap: 14px;
  width: 100%;
  max-width: 420px;
  padding: 24px;
  border-radius: 18px;
  background: var(--bg-card, #0f172a);
  border: 1px solid var(--border-color, rgba(148, 163, 184, 0.25));
  box-shadow: 0 24px 60px rgba(2, 6, 23, 0.45);
  color: var(--text-primary, #e2e8f0);
}

#pwaPairingForm label {
  font-size: 12.5px;
  font-weight: 600;
  color: var(--text-secondary, #cbd5e1);
}

#pwaPairingClinicInput,
#pwaPairingCodeInput {
  width: 100%;
  min-height: 44px;
  padding: 10px 14px;
  border-radius: 10px;
  border: 1px solid var(--border-color, rgba(148, 163, 184, 0.35));
  background: var(--bg-input, var(--bg-secondary, #111927));
  color: var(--text-primary, #e2e8f0);
  font-family: inherit;
  font-size: 16px !important; /* !important: beats the auth script's inline 15px — >=16px stops iOS zoom */
  line-height: 1.3;
  outline: none;
}

#pwaPairingClinicInput:focus,
#pwaPairingCodeInput:focus {
  border-color: var(--accent, #0d9488);
  box-shadow: 0 0 0 3px var(--accent-ring, rgba(13, 148, 136, 0.25));
}

#pwaPairingCodeInput {
  letter-spacing: 0.28em;
  text-transform: uppercase;
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  text-align: center;
  font-size: 20px !important; /* !important: same inline 15px override as above */
  font-weight: 600;
}

/* pwa/pwa-auth.js spaces its fields with inline margins; the grid gap above
   already provides the rhythm, so fold the vertical margins away and keep
   the card inside a 667px-tall phone viewport. */
#pwaPairingForm > * {
  margin-block: 0 !important;
}

#pwaPairingError {
  /* Theme token: renderer/styles.css + ui-refresh.css define --danger per
     skin (Porcelain / Atrium / Noir), so the gate error follows the theme. */
  color: var(--danger, #f87171);
  min-height: 1.2em;
  font-size: 12.5px;
  line-height: 1.4;
}

#pwaPairingStatus {
  color: var(--text-muted, #94a3b8);
  font-size: 12.5px;
  line-height: 1.45;
}

#pwaPairingSignedInAs {
  color: var(--text-secondary, #cbd5e1);
  font-size: 12.5px;
  line-height: 1.45;
  overflow-wrap: anywhere;
}

#pwaPairingSubmit {
  min-height: 46px;
  width: 100%;
  border: 0;
  border-radius: 10px;
  background: var(--accent, #0d9488);
  color: var(--accent-ink, #ffffff); /* ink on the accent fill; ui-v2.css flips it per theme */
  font-family: inherit;
  font-size: 15px;
  font-weight: 600;
  cursor: pointer;
  touch-action: manipulation;
}

#pwaPairingSubmit:hover:not(:disabled) {
  background: var(--accent-hover, #0f766e);
}

#pwaPairingSubmit:disabled {
  opacity: 0.6;
  cursor: not-allowed;
}

#pwaPairingSignOutBtn {
  min-height: 44px;
  padding: 0 14px;
  border-radius: 10px;
  border: 1px solid var(--border-color, rgba(148, 163, 184, 0.35));
  background: transparent;
  color: var(--text-secondary, #cbd5e1);
  font-family: inherit;
  font-size: 13px;
  cursor: pointer;
  touch-action: manipulation;
}

/* ==========================================================================
   2. Shell chrome — every viewport up to 1024px
   (the bar/drawer exist only in the PWA build)
   ========================================================================== */

@media (max-width: 1024px) {
  #pwaMobileBar {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    z-index: 1100;
    display: flex;
    align-items: center;
    gap: 8px;
    box-sizing: border-box;
    height: calc(52px + env(safe-area-inset-top, 0px));
    padding-top: env(safe-area-inset-top, 0px);
    padding-left: max(10px, env(safe-area-inset-left, 0px));
    padding-right: max(10px, env(safe-area-inset-right, 0px));
    background: var(--bg-card, #0f172a);
    border-bottom: 1px solid var(--border-color, rgba(148, 163, 184, 0.25));
    box-shadow: var(--shadow-sm, 0 1px 3px rgba(2, 6, 23, 0.25));
    -webkit-backdrop-filter: blur(10px);
    backdrop-filter: blur(10px);
    color: var(--text-primary, #e2e8f0);
    -webkit-user-select: none;
    user-select: none;
    touch-action: manipulation;
  }

  /* Prefixed with #pwaMobileBar so these beat pwa-boot.js' own
     "#pwaMobileBar button { background/border }" fallback (1,0,1). */
  #pwaMobileBar #pwaMenuBtn,
  #pwaMobileBar #pwaRefreshBtn {
    flex: 0 0 auto;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 44px;
    height: 44px;
    min-width: 44px;
    min-height: 44px;
    padding: 0;
    border: 0;
    border-radius: 10px;
    background: transparent;
    color: inherit;
    font-family: inherit;
    font-size: 18px;
    line-height: 1;
    cursor: pointer;
    -webkit-tap-highlight-color: transparent;
    touch-action: manipulation;
  }

  #pwaMobileBar #pwaMenuBtn svg,
  #pwaMobileBar #pwaRefreshBtn svg {
    display: block;
    width: 22px;
    height: 22px;
  }

  #pwaMobileBar #pwaMenuBtn:active,
  #pwaMobileBar #pwaRefreshBtn:active {
    background: var(--bg-hover, rgba(148, 163, 184, 0.16));
  }

  /* Accent tint only where color-mix() is supported; the flat :active above
     remains the fallback so older Safari never loses the pressed state. */
  @supports (color: color-mix(in srgb, red, blue)) {
    #pwaMobileBar #pwaMenuBtn:hover,
    #pwaMobileBar #pwaRefreshBtn:hover {
      background: color-mix(in srgb, var(--accent, #0d9488) 12%, transparent);
    }
    #pwaMobileBar #pwaMenuBtn:active,
    #pwaMobileBar #pwaRefreshBtn:active {
      background: color-mix(in srgb, var(--accent, #0d9488) 24%, transparent);
    }
  }

  /* Also prefixed to outrank the boot fallback's own title rule. */
  #pwaMobileBar #pwaMobileTitle {
    flex: 1 1 auto;
    min-width: 0;
    font-size: 14px;
    font-weight: 600;
    letter-spacing: 0.02em;
    color: var(--text-primary, #e2e8f0);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
  }

  /* Respect an explicit hide from the boot script (UA [hidden] is weaker
     than the id rule above). */
  #pwaMobileBar[hidden] {
    display: none !important;
  }

  #pwaDrawerBackdrop {
    position: fixed;
    inset: 0;
    z-index: 1150;
    display: none;
    background: rgba(2, 6, 23, 0.55);
    -webkit-backdrop-filter: blur(2px);
    backdrop-filter: blur(2px);
  }

  /* !important: the boot script may leave an inline display:none on the scrim
     and only toggle body.pwa-drawer-open, so the state class must win. */
  body.pwa-drawer-open #pwaDrawerBackdrop {
    display: block !important;
  }

  body.pwa-drawer-open {
    overflow: hidden;
  }
}

/* ==========================================================================
   3. Shared responsive primitives — phones and tablets
   Tab rail, touch targets, overflow containment.
   ========================================================================== */

@media (max-width: 1024px) {
  /* ---- Scrollable, non-wrapping tab rail (patient detail + page tabs) ---- */
  .tabs {
    display: flex;
    flex-wrap: nowrap;
    align-items: center;
    gap: 8px;
    width: 100%;
    max-width: 100%;
    padding: 4px 4px 6px;
    margin-bottom: 16px;
    overflow-x: auto;
    overflow-y: hidden;
    overscroll-behavior-x: contain;
    -webkit-overflow-scrolling: touch;
    scroll-snap-type: x proximity;
    scrollbar-width: none;
    -ms-overflow-style: none;
  }

  .tabs::-webkit-scrollbar {
    display: none;
    width: 0;
    height: 0;
  }

  .tab {
    flex: 0 0 auto;
    min-height: 44px;
    padding: 0 14px;
    font-size: 12.5px; /* ui-v2 renders 9.5px tabs — unreadable on touch */
    white-space: nowrap;
    scroll-snap-align: start;
    touch-action: manipulation;
  }

  /* ---- Touch targets (Apple HIG / Material 44px minimum) ---- */
  .btn,
  .btn-sm,
  .btn-qa,
  .btn-close,
  .nav-item,
  .chip,
  .cal-nav-btn,
  select,
  input[type="text"],
  input[type="password"],
  input[type="date"],
  input[type="email"],
  input[type="number"],
  input[type="search"],
  input[type="tel"],
  textarea {
    min-height: 44px;
  }

  .btn-qa,
  .btn-close {
    min-width: 44px;
  }

  /* ---- No horizontal page overflow anywhere ---- */
  html,
  body {
    max-width: 100%;
    overflow-x: hidden;
  }

  img,
  svg,
  video,
  canvas,
  table,
  pre,
  iframe {
    max-width: 100%;
  }

  #mainContent > *,
  #mainContent .page,
  #mainContent .tab-content,
  #mainContent .card,
  #mainContent .table-card,
  #mainContent .table-scroll,
  #mainContent .kpi-grid > *,
  #mainContent .dash-grid > *,
  #mainContent .detail-grid > *,
  #mainContent .finance-grid > *,
  #mainContent .form-grid > *,
  #mainContent .studio-header > *,
  #mainContent .studio-controls-left,
  #mainContent .studio-controls-right {
    min-width: 0;
    max-width: 100%;
  }
}

/* ==========================================================================
   4. PHONE — max-width: 430px
   Off-canvas drawer, full-width sheets, stacked patient cards, single-column
   studio, stacked KPI/grid layouts.
   ========================================================================== */

@media (max-width: 430px) {
  /* ---- Reserve the fixed bar so content never hides under it ---- */
  body {
    padding-top: calc(52px + env(safe-area-inset-top, 0px));
  }

  /* !important: styles.css sets display:flex on the strip, and the Electron
     scoped rules re-show it; it is a Windows-only chrome element. */
  .app-window-titlebar {
    display: none !important;
  }

  /* ---- Sidebar becomes an off-canvas drawer ----
     The duplicated selectors mirror the renderer's own scoped rules
     (body.is-electron / body:not(.is-electron)) so the drawer geometry wins
     on specificity as well as source order. top/height need !important
     because styles.css declares them !important for body.is-electron. */
  #sidebar,
  body .sidebar,
  body.is-electron .sidebar,
  body:not(.is-electron) .sidebar {
    position: fixed;
    top: 0 !important;
    bottom: 0 !important;
    left: 0 !important;
    right: auto;
    width: min(86vw, 320px);
    max-width: 320px;
    height: 100vh !important; /* fallback for browsers without dynamic vh */
    height: 100dvh !important;
    margin: 0;
    padding-top: env(safe-area-inset-top, 0px);
    padding-bottom: env(safe-area-inset-bottom, 0px);
    padding-left: env(safe-area-inset-left, 0px);
    border: 0;
    border-radius: 0 18px 18px 0;
    overflow-x: hidden;
    overflow-y: auto;
    overscroll-behavior: contain;
    -webkit-overflow-scrolling: touch;
    z-index: 1200;
    transform: translateX(-100%);
    visibility: hidden;
    transition: transform 0.22s ease, visibility 0s linear 0.22s;
  }

  body.pwa-drawer-open #sidebar {
    transform: translateX(0);
    visibility: visible;
    transition: transform 0.22s ease, visibility 0s linear 0s;
  }

  #sidebar .nav-item {
    min-height: 44px;
  }

  #sidebar .sidebar-footer {
    margin-top: auto;
    padding-bottom: calc(10px + env(safe-area-inset-bottom, 0px));
  }

  /* ---- Main column: full width, its own scrollport under the bar ---- */
  #mainContent {
    /* !important: renderer scopes margin/height to body.is-electron and
       body:not(.is-electron) with higher specificity than a class selector. */
    margin-left: 0 !important;
    margin-top: 0 !important;
    width: 100% !important;
    max-width: 100% !important;
    height: auto !important;
    flex: 1 1 auto;
    min-width: 0;
    min-height: 0;
    padding-top: 12px;
    padding-right: max(12px, env(safe-area-inset-right, 0px));
    padding-bottom: calc(12px + env(safe-area-inset-bottom, 0px));
    padding-left: max(12px, env(safe-area-inset-left, 0px));
    border: 0 !important; /* renderer draws an 18px rounded desktop card */
    border-radius: 0 !important;
    background: var(--bg-primary, #f8fafc);
    overflow-x: hidden;
    overflow-y: auto;
    overscroll-behavior-y: contain;
    -webkit-overflow-scrolling: touch;
  }

  /* ---- Patients table -> stacked cards ----
     Renderer column order (renderPatientRow in renderer/app.js):
       1 Patient (name + phone/clinic + alerts)   6 Previous Visit
       2 Patient ID                                7 Next visit
       3 Age                                       8 Balance
       4 Status                                    9 Progress
       5 Clinic
     There is no dedicated actions cell: the whole row is tappable and opens
     the patient record, so the compact card keeps name + ID + age + status
     and drops the wide clinical/financial columns (5 onward). */
  #patientsTable,
  #patientsTable thead,
  #patientsTable tbody,
  #patientsTable tr,
  #patientsTable td {
    display: block;
  }

  #patientsTable thead {
    display: none;
  }

  #patientsTable {
    width: 100%;
    max-width: 100%;
    margin: 0;
  }

  #patientsTable tbody {
    padding: 10px;
  }

  #patientsTable tr.clickable {
    position: relative;
    margin: 0 0 10px;
    padding: 12px 14px;
    background: var(--bg-card, #ffffff);
    border: 1px solid var(--border-color, rgba(148, 163, 184, 0.28));
    border-radius: 14px;
    box-shadow: var(--shadow-sm, 0 1px 3px rgba(2, 6, 23, 0.12));
  }

  #patientsTable tr.clickable:last-child {
    margin-bottom: 0;
  }

  /* !important: renderer hovers slide rows 2px sideways (row transform on
     touch causes jitter and a 2px horizontal overflow). */
  #patientsTable tr:hover {
    transform: none !important;
    background: var(--bg-card, #ffffff);
    box-shadow: var(--shadow-sm, 0 1px 3px rgba(2, 6, 23, 0.12));
  }

  #patientsTable tr.virtual-spacer {
    margin: 0;
    padding: 0;
    border: 0;
    background: transparent;
    box-shadow: none;
  }

  #patientsTable tr.virtual-spacer td {
    display: none; /* the tr keeps its inline height, so the scroll range stays */
  }

  #patientsTable td {
    padding: 0;
    border: 0;
    font-size: 12.5px;
    line-height: 1.4;
    color: var(--text-secondary, #475569);
  }

  #patientsTable td:first-child {
    padding-right: 96px; /* room for the absolutely placed status pill */
    font-weight: 600;
  }

  #patientsTable td:nth-child(2),
  #patientsTable td:nth-child(3) {
    display: inline-block;
    margin-top: 8px;
    margin-right: 12px;
    font-size: 11px;
    color: var(--text-muted, #64748b);
  }

  #patientsTable td:nth-child(4) {
    position: absolute;
    top: 12px;
    right: 12px;
  }

  #patientsTable td:nth-child(n+5) {
    display: none;
  }

  #patientsTable .patient-cell,
  #patientsTable .patient-cell-copy {
    min-width: 0;
  }

  #patientsTable .patient-cell strong,
  #patientsTable .patient-cell small {
    max-width: 100%;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }

  #patientsTable .patient-alert-row {
    display: flex;
    flex-wrap: wrap;
    gap: 4px;
    margin-top: 8px;
  }

  /* The list keeps its own scrollport: renderer/app.js VirtualTable re-renders
     rows from container.scrollTop, so collapsing this height would truncate
     clinics with more than 40 patients. */
  .table-container {
    max-height: calc(100vh - 230px); /* fallback */
    max-height: calc(100dvh - 230px);
    border-radius: 14px;
  }

  .table-scroll {
    overflow-x: hidden;
  }

  .table-foot {
    padding: 10px 12px;
  }

  /* ---- Modals become bottom sheets ----
     display is intentionally NOT overridden: every overlay in
     renderer/index.html carries an inline display:none that JS flips to
     flex, and an !important here would force all of them open. The two
     renderer layouts are flex (styles.css) and grid (ui-v2.css); the
     declarations below align the sheet to the bottom in both. */
  .modal-overlay {
    align-items: flex-end;
    justify-items: stretch;
    padding: 0 !important;
  }

  .modal {
    /* !important: styles.css and ui-v2.css both define .modal with 90% / 92vw
       widths, and feature modals (.crop-modal, .clinic-modal) add their own
       widths; the sheet must always span the phone. */
    width: 100% !important;
    max-width: 100% !important;
    max-height: 92vh; /* fallback */
    max-height: 92dvh;
    margin: 0 !important;
    padding: 20px 18px calc(18px + env(safe-area-inset-bottom, 0px)) !important;
    border-radius: 16px 16px 0 0 !important;
    border-left: 0 !important;
    border-right: 0 !important;
    border-bottom: 0 !important;
    overflow-x: hidden;
    overflow-y: auto;
    overscroll-behavior: contain;
    -webkit-overflow-scrolling: touch;
  }

  .modal-actions {
    flex-wrap: wrap;
    gap: 8px;
  }

  .modal-actions .btn {
    flex: 1 1 auto;
  }

  /* ---- Desktop auth lock screen on a phone ---- */
  #authScreen {
    /* .auth-screen-overlay sets width:100vw; 100% avoids the scrollbar-width
       overflow that 100vw can introduce (the id already outranks it). */
    width: 100%;
    max-width: 100%;
    padding-top: max(16px, env(safe-area-inset-top, 0px));
    padding-right: 12px;
    padding-bottom: max(16px, env(safe-area-inset-bottom, 0px));
    padding-left: 12px;
  }

  #authScreen .auth-card {
    width: calc(100% - 12px);
    max-width: 460px;
    margin: 0 auto;
    padding: 26px 20px;
    border-radius: 16px;
  }

  #authScreen .form-input {
    min-height: 44px;
    font-size: 16px; /* stops iOS focus zoom */
  }

  #authScreen .btn-auth-submit {
    width: 100%;
    min-height: 46px;
  }

  #authScreen .auth-brand-logo {
    max-width: 100%;
  }

  /* ---- Page chrome & furniture ---- */
  .page-header {
    gap: 10px;
    margin-bottom: 16px;
  }

  .page-header h1 {
    font-size: 21px;
    line-height: 1.15;
  }

  .page-actions {
    width: 100%;
    flex-wrap: wrap;
    gap: 8px;
  }

  .page-actions .btn {
    flex: 1 1 auto;
  }

  .toolbar {
    width: 100%;
    flex-wrap: wrap;
    gap: 8px;
  }

  .search-field {
    width: 100%;
    /* renderer pins this to 36px; the 44px input needs a 44px shell */
    height: auto;
    min-height: 44px;
  }

  .filter-chips {
    width: 100%;
    max-width: 100%;
    flex-wrap: nowrap;
    overflow-x: auto;
    overscroll-behavior-x: contain;
    -webkit-overflow-scrolling: touch;
    scrollbar-width: none;
  }

  .filter-chips::-webkit-scrollbar {
    display: none;
    height: 0;
  }

  #filterClinic {
    width: 100%;
  }

  /* ---- KPI / dashboard grids ---- */
  /* !important: renderer media queries re-expand these grids; the phone must
     stay single/dual column to guarantee zero horizontal overflow. */
  .kpi-grid {
    grid-template-columns: repeat(2, minmax(0, 1fr)) !important;
    gap: 8px;
  }

  .stats-grid {
    grid-template-columns: repeat(2, minmax(0, 1fr)) !important;
    gap: 8px;
  }

  .overview-grid {
    grid-template-columns: repeat(2, minmax(0, 1fr)) !important;
    gap: 8px;
  }

  .dash-grid,
  .finance-grid,
  .detail-grid,
  .ceph-grid-layout,
  .editor-grid,
  .form-grid {
    grid-template-columns: minmax(0, 1fr) !important;
  }

  /* ---- Studio: single column mount, orientation-safe images ---- */
  #tab-studio .studio-header {
    flex-direction: column;
    align-items: stretch;
    gap: 12px;
    padding: 12px;
  }

  #tab-studio .studio-controls-left,
  #tab-studio .studio-controls-right {
    width: 100%;
    flex-wrap: wrap;
    gap: 8px;
  }

  #tab-studio .studio-select {
    width: 100%;
  }

  #tab-studio .ortho-composite-mount {
    padding: 10px;
    gap: 10px;
  }

  #tab-studio .mount-row-header {
    flex-wrap: wrap;
    gap: 6px;
  }

  #tab-studio .mount-row-header .row-desc {
    width: 100%;
    margin-left: 0;
  }

  #tab-studio .mount-grid,
  #tab-studio .row-1-grid,
  #tab-studio .row-2-grid,
  #tab-studio .row-3-grid,
  #tab-studio .row-4-grid,
  #tab-studio .extra-mount-grid {
    grid-template-columns: minmax(0, 1fr) !important;
    max-width: 100% !important;
    gap: 8px;
  }

  #tab-studio .slot-viewport {
    width: 100%;
    max-width: 100%;
  }

  #tab-studio .slot-image-wrapper {
    width: 100%;
    max-width: 100%;
    /* no min-height here: studio-v2.css sets min-height:0 inside the fixed
       height slot viewports, and re-introducing one would clip the photo */
  }

  /* Sizing + orientation only. The transform chain is written inline by
     renderer/app.js and is intentionally never declared here. */
  #tab-studio .slot-image-wrapper .slot-img {
    width: 100%;
    max-width: 100%;
    height: 100%;
    object-fit: contain;
    image-orientation: from-image;
  }

  #tab-studio .photo-slot:hover {
    transform: none;
  }

  #tab-studio .slot-quick-actions {
    gap: 6px;
  }

  /* ---- Chart containers keep their own horizontal scroll ---- */
  #odontogramContainer,
  #elasticsContainer,
  #modelsContainer {
    max-width: 100%;
    overflow-x: auto;
    overscroll-behavior-x: contain;
    -webkit-overflow-scrolling: touch;
  }

  /* ---- Pairing gate: the inline styles from pwa-auth are honoured above,
     only the card radius is softened for very small screens ---- */
  #pwaPairingForm {
    border-radius: 16px;
  }
}

/* ==========================================================================
   5. TABLET — 431px to 1024px
   Sidebar stays, narrowed; touch spacing; two-column KPI; sheet modals.
   ========================================================================== */

@media (min-width: 431px) and (max-width: 1024px) {
  /* ---- Reserve the bar only when the boot actually injected it ---- */
  @supports selector(body:has(#pwaMobileBar)) {
    body:has(#pwaMobileBar) {
      padding-top: calc(52px + env(safe-area-inset-top, 0px));
    }
  }

  @supports not selector(body:has(#pwaMobileBar)) {
    body {
      padding-top: calc(52px + env(safe-area-inset-top, 0px));
    }
  }

  /* !important: the titlebar is a Windows-only strip and must not eat space. */
  .app-window-titlebar {
    display: none !important;
  }

  /* ---- Narrow permanent rail ----
     !important on top/height because styles.css pins them for
     body.is-electron (38px offset) and ui-refresh offsets them again. */
  #sidebar,
  body .sidebar,
  body.is-electron .sidebar,
  body:not(.is-electron) .sidebar {
    top: calc(52px + env(safe-area-inset-top, 0px)) !important;
    bottom: 0 !important;
    left: 0 !important;
    right: auto;
    width: 220px;
    max-width: 220px;
    height: auto !important;
    margin: 0;
    border: 0;
    border-radius: 0 18px 18px 0;
    overflow-x: hidden;
    overflow-y: auto;
    overscroll-behavior: contain;
    touch-action: manipulation;
    z-index: 900;
    /* pwa-boot.js has a <=900px fallback that slides #sidebar off-canvas;
       the rail is permanently visible on tablets, so neutralise it. */
    transform: none;
  }

  #sidebar .nav-item {
    min-height: 44px;
  }

  #sidebar .sidebar-footer {
    margin-top: auto;
    padding-bottom: 10px;
  }

  /* !important: pwa-boot.js' fallback rule for #pwaMobileBar buttons sets
     display:inline-flex at (1,0,1) and would out-rank the id; the drawer
     trigger is meaningless while the rail is pinned open. */
  #pwaMenuBtn {
    display: none !important;
  }

  /* !important: overrides the phone drawer state if the class was toggled
     before the viewport grew past 430px. The body-qualified selector repeats
     the shell rule's specificity so this later declaration actually wins. */
  #pwaDrawerBackdrop,
  body.pwa-drawer-open #pwaDrawerBackdrop {
    display: none !important;
  }

  /* ---- Main column shifted by the rail ---- */
  #mainContent {
    margin-left: 220px !important;
    margin-top: 8px !important;
    width: auto;
    max-width: calc(100% - 220px);
    height: auto !important;
    flex: 1 1 auto;
    min-width: 0;
    min-height: 0;
    padding: 18px;
    border-radius: 18px;
    overflow-x: hidden;
    overflow-y: auto;
    overscroll-behavior-y: contain;
    -webkit-overflow-scrolling: touch;
  }

  /* ---- Grids: two columns where it reads well, stack the wide pairs ---- */
  .kpi-grid,
  .stats-grid {
    grid-template-columns: repeat(2, minmax(0, 1fr)) !important;
  }

  .dash-grid,
  .finance-grid,
  .detail-grid {
    grid-template-columns: minmax(0, 1fr) !important;
  }

  /* ---- Modals: centred sheet, never wider than the viewport ---- */
  .modal-overlay {
    padding: 16px !important;
  }

  .modal {
    width: min(720px, 92vw) !important;
    max-width: min(720px, 92vw) !important;
    max-height: 90vh; /* fallback */
    max-height: 90dvh;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
  }

  /* ---- Studio images keep their EXIF orientation, sizing untouched ---- */
  #tab-studio .slot-image-wrapper {
    width: 100%;
    max-width: 100%;
  }

  #tab-studio .slot-image-wrapper .slot-img {
    width: 100%;
    max-width: 100%;
    object-fit: contain;
    image-orientation: from-image;
  }

  #tab-studio .slot-quick-actions {
    gap: 6px;
  }

  #tab-studio .studio-controls-right {
    flex-wrap: wrap;
    gap: 8px;
  }

  #odontogramContainer,
  #elasticsContainer,
  #modelsContainer {
    max-width: 100%;
    overflow-x: auto;
    overscroll-behavior-x: contain;
    -webkit-overflow-scrolling: touch;
  }

  /* ---- Pairing gate: same card, a little more air ---- */
  #pwaPairingForm {
    max-width: 440px;
  }
}

/* ==========================================================================
   6. Accessibility, print and standalone polish
   ========================================================================== */

/* Scoped to the PWA breakpoint: an unscoped reduced-motion block would also
   rewrite #sidebar / .tabs transitions in a Desktop window wider than 1024px,
   and Desktop rendering above that width must stay byte-identical. */
@media (max-width: 1024px) and (prefers-reduced-motion: reduce) {
  #sidebar,
  body .sidebar,
  body.is-electron .sidebar,
  body:not(.is-electron) .sidebar {
    transition: none !important;
  }

  .tabs {
    scroll-behavior: auto;
  }
}

@media (display-mode: standalone) {
  /* Installed PWA: the web viewport reaches under the status bar, so the bar
     itself must reserve the inset. Height already includes it in section 2;
     only the padding is restated so browser-mode previews stay unchanged. */
  #pwaMobileBar {
    padding-top: env(safe-area-inset-top, 0px);
  }
}

@media print {
  #pwaMobileBar,
  #pwaDrawerBackdrop {
    display: none !important;
  }
}

/* ==========================================================================
   7. Phase 4A — inactivity-lock veil + iOS "Add to Home Screen" banner
   --------------------------------------------------------------------------
   Markup owners:
     #pwaLockOverlay      pwa/pwa-lock.js (RAM-only inactive lock)
     #pwaIosA2hsBanner    pwa/pwa-boot.js  (iOS install hint)
   Neither id exists in the Electron renderer, so every rule below is inert on
   Desktop. Palette is theme tokens only: Porcelain (light), Atrium and Noir
   flip through --bg-primary / --bg-card / --text-* without extra selectors.
   No existing selector above this line is restated or overridden.
   ========================================================================== */

/* ---- 7.1 Lock veil ----
   Fully OPAQUE on purpose: no alpha, no backdrop blur. A patient chart must
   never stay legible behind the keypad.
   The veil must out-rank EVERY renderer layer, not just the low ones: clinical
   modal 9999, toast 10000 and its container 10050, command palette 10060,
   patient row context menu 10080, auth screen and app titlebar 99999. A lock
   that a modal can paint over is a PHI-privacy defect, not a cosmetic one, so
   the value is pinned near the max 32-bit z-index instead of tracking the
   renderer's current top layer. */
#pwaLockOverlay,
.pwa-lock-overlay {
  position: fixed;
  inset: 0;
  z-index: 2147483000;
  box-sizing: border-box;
  display: flex;
  align-items: center;
  /* `safe` keeps the top of a tall card reachable when the overlay scrolls;
     browsers without the keyword keep the plain `center` above. */
  align-items: safe center;
  justify-content: center;
  justify-content: safe center;
  padding: max(16px, env(safe-area-inset-top, 0px)) max(16px, env(safe-area-inset-right, 0px)) max(16px, env(safe-area-inset-bottom, 0px)) max(16px, env(safe-area-inset-left, 0px));
  background: #05070c;
  background: var(--bg-primary, #05070c);
  color: var(--text-primary, #e2e8f0);
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  overscroll-behavior: contain;
}

/* !important: the id rule above (1,0,0) out-ranks the UA [hidden] rule, so the
   hidden state must be restated at equal specificity and forced. */
#pwaLockOverlay[hidden],
.pwa-lock-overlay[hidden] {
  display: none !important;
}

/* ---- 7.2 Lock card ---- */
.pwa-lock-card {
  box-sizing: border-box;
  width: 100%;
  max-width: 380px;
  padding: 28px 24px;
  border-radius: 20px;
  background: var(--bg-card, #0f172a);
  border: 1px solid var(--border-color, rgba(148, 163, 184, 0.25));
  box-shadow: 0 24px 60px rgba(2, 6, 23, 0.45);
  color: var(--text-primary, #e2e8f0);
  text-align: center;
}

.pwa-lock-brand {
  display: block;
  margin: 0 0 8px;
  font-size: 11px;
  font-weight: 700;
  letter-spacing: 0.18em;
  text-transform: uppercase;
  color: var(--accent, #0d9488);
}

.pwa-lock-title {
  margin: 0 0 6px;
  font-size: 20px;
  font-weight: 600;
  line-height: 1.2;
  color: var(--text-primary, #e2e8f0);
}

.pwa-lock-subtitle {
  margin: 0 0 16px;
  font-size: 13px;
  line-height: 1.45;
  color: var(--text-secondary, #cbd5e1);
}

/* ---- 7.3 Passcode dots ---- */
.pwa-lock-dots {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 10px;
  margin: 0 0 12px;
}

.pwa-lock-dot {
  box-sizing: border-box;
  width: 14px;
  height: 14px;
  border-radius: 50%;
  border: 1px solid var(--border-color, rgba(148, 163, 184, 0.45));
  background: transparent;
}

.pwa-lock-dot.is-filled {
  background: var(--accent, #0d9488);
  border-color: var(--accent, #0d9488);
}

/* ---- 7.4 Status line (pwa-lock.js marks it role="status") ----
   The min-height reserves exactly one line so a wrong code cannot make the
   keypad jump under the finger. */
.pwa-lock-status,
.pwa-lock-status[role=status] {
  min-height: 1.4em;
  margin: 0 0 12px;
  font-size: 12.5px;
  line-height: 1.4;
  color: var(--text-muted, #94a3b8);
}

.pwa-lock-status[data-state="error"],
.pwa-lock-status.is-error {
  color: var(--danger, #f87171);
}

/* ---- 7.5 Keypad ---- */
.pwa-lock-keypad {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 12px;
  width: 100%;
  max-width: 100%;
  margin: 0 0 12px;
}

.pwa-lock-key {
  box-sizing: border-box;
  min-width: 56px;
  min-height: 56px; /* >= 44x44 touch target */
  padding: 0 6px;
  border-radius: 14px;
  border: 1px solid var(--border-color, rgba(148, 163, 184, 0.35));
  background: var(--bg-input, var(--bg-secondary, #111927));
  color: var(--text-primary, #e2e8f0);
  font-family: inherit;
  font-size: 20px;
  font-weight: 600;
  line-height: 1;
  cursor: pointer;
  touch-action: manipulation;
  -webkit-tap-highlight-color: transparent;
  -webkit-user-select: none;
  user-select: none;
  appearance: none;
  transition: background 0.12s ease, border-color 0.12s ease, box-shadow 0.12s ease;
}

.pwa-lock-key:active {
  background: var(--bg-hover, rgba(148, 163, 184, 0.16));
  box-shadow: 0 0 0 3px var(--accent-ring, rgba(13, 148, 136, 0.25));
}

.pwa-lock-key:focus-visible {
  outline: none;
  border-color: var(--accent, #0d9488);
  box-shadow: 0 0 0 3px var(--accent-ring, rgba(13, 148, 136, 0.25));
}

.pwa-lock-key--biometric {
  background: var(--accent-ring, rgba(13, 148, 136, 0.25));
  border-color: var(--accent, #0d9488);
  color: var(--accent, #0d9488);
}

.pwa-lock-key--back {
  color: var(--text-muted, #94a3b8);
  font-size: 18px;
}

/* ---- 7.6 "Forgot passcode" - full-width text button ---- */
#pwaLockForgot,
.pwa-lock-forgot {
  display: block;
  box-sizing: border-box;
  width: 100%;
  min-height: 44px;
  margin: 4px 0 0;
  padding: 0 8px;
  border: 0;
  border-radius: 10px;
  background: transparent;
  color: var(--text-secondary, #cbd5e1);
  font-family: inherit;
  font-size: 13px;
  text-decoration: underline;
  text-underline-offset: 3px;
  cursor: pointer;
  touch-action: manipulation;
  transition: color 0.12s ease;
}

#pwaLockForgot:hover,
#pwaLockForgot:focus-visible,
.pwa-lock-forgot:hover,
.pwa-lock-forgot:focus-visible {
  color: var(--danger, #f87171);
}

#pwaLockForgot:focus-visible,
.pwa-lock-forgot:focus-visible {
  outline: none;
  box-shadow: 0 0 0 3px var(--accent-ring, rgba(13, 148, 136, 0.25));
}

/* ---- 7.7 iOS "Add to Home Screen" banner (pwa-boot.js) ----
   left/right insets instead of a viewport-width box: a fixed element sized to
   the full viewport adds a horizontal scrollbar on desktop-class Safari.
   The insets, not a percentage, are what size this box: the containing block of
   a fixed element is the viewport, so left/right already resolve width auto to
   viewport minus both insets. A percentage cap is measured against that same
   viewport rather than against the inset box, so `min(560px, 100%)` (and a bare
   `100%` in the phone block) can add the insets on top of a full-viewport width
   and overflow by ~24px, clipping the 44px dismiss target. Cap at an absolute
   560px only; `margin-left/right: auto` centres that cap on wide screens and
   stay inert while the insets are narrower. */
#pwaIosA2hsBanner,
.pwa-a2hs-banner {
  position: fixed;
  left: max(12px, env(safe-area-inset-left, 0px));
  right: max(12px, env(safe-area-inset-right, 0px));
  bottom: max(12px, env(safe-area-inset-bottom, 0px));
  z-index: 1500;
  box-sizing: border-box;
  display: flex;
  align-items: center;
  gap: 12px;
  max-width: 560px;
  margin-left: auto;
  margin-right: auto;
  padding: 12px 14px;
  border-radius: 16px;
  background: var(--bg-card, #0f172a);
  border: 1px solid var(--border-color, rgba(148, 163, 184, 0.25));
  box-shadow: 0 16px 40px rgba(2, 6, 23, 0.45);
  color: var(--text-primary, #e2e8f0);
  overflow: hidden;
}

/* !important: same UA-[hidden] vs (1,0,0) issue as the lock veil. */
#pwaIosA2hsBanner[hidden],
.pwa-a2hs-banner[hidden] {
  display: none !important;
}

.pwa-a2hs-icon {
  flex: 0 0 auto;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 40px;
  height: 40px;
  border-radius: 50%;
  background: var(--accent, #0d9488);
  color: var(--accent-ink, #ffffff);
  font-size: 20px;
  line-height: 1;
  -webkit-user-select: none;
  user-select: none;
}

.pwa-a2hs-copy {
  flex: 1 1 auto;
  display: flex;
  flex-direction: column;
  gap: 2px;
  min-width: 0;
  text-align: left;
}

.pwa-a2hs-title {
  font-size: 13px;
  font-weight: 600;
  line-height: 1.3;
  color: var(--text-primary, #e2e8f0);
  overflow-wrap: anywhere;
}

.pwa-a2hs-text {
  font-size: 12px;
  line-height: 1.4;
  color: var(--text-secondary, #cbd5e1);
  overflow-wrap: anywhere;
}

.pwa-a2hs-dismiss {
  flex: 0 0 auto;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  box-sizing: border-box;
  width: 44px;
  height: 44px;
  min-width: 44px;
  min-height: 44px;
  padding: 0;
  border: 0;
  border-radius: 10px;
  background: transparent;
  color: var(--text-muted, #94a3b8);
  font-family: inherit;
  font-size: 22px;
  line-height: 1;
  cursor: pointer;
  touch-action: manipulation;
  -webkit-tap-highlight-color: transparent;
}

.pwa-a2hs-dismiss:hover,
.pwa-a2hs-dismiss:focus-visible {
  color: var(--text-primary, #e2e8f0);
  background: var(--bg-hover, rgba(148, 163, 184, 0.16));
}

.pwa-a2hs-dismiss:focus-visible {
  outline: none;
  box-shadow: 0 0 0 3px var(--accent-ring, rgba(13, 148, 136, 0.25));
}

/* ---- 7.8 Phone safety (375–430px) ----
   Tighter card and keypad, still >= 44px touch targets, and no horizontal
   overflow: every flexible child is allowed to shrink and long banner text
   wraps inside the card instead of widening it. */
@media (max-width: 480px) {
  #pwaLockOverlay,
  .pwa-lock-overlay {
    padding: max(12px, env(safe-area-inset-top, 0px)) max(12px, env(safe-area-inset-right, 0px)) max(12px, env(safe-area-inset-bottom, 0px)) max(12px, env(safe-area-inset-left, 0px));
  }

  .pwa-lock-card {
    max-width: 100%;
    padding: 22px 16px;
    border-radius: 16px;
  }

  .pwa-lock-keypad {
    gap: 8px;
  }

  .pwa-lock-key {
    min-width: 0;
    min-height: 52px; /* still >= 44px */
    border-radius: 12px;
    font-size: 19px;
  }

  #pwaIosA2hsBanner,
  .pwa-a2hs-banner {
    gap: 8px;
    /* `none`, not `100%`: width auto already resolves to viewport minus the
       12px insets. A 100%-of-viewport cap would re-add those insets and
       overflow on 375–430px phones. */
    max-width: none;
    padding: 10px 8px 10px 12px;
    border-radius: 14px;
  }

  .pwa-a2hs-icon {
    width: 34px;
    height: 34px;
    font-size: 17px;
  }

  .pwa-a2hs-copy {
    min-width: 0;
    max-width: 100%;
  }

  .pwa-a2hs-title {
    font-size: 12.5px;
    max-width: 100%;
  }

  .pwa-a2hs-text {
    font-size: 11.5px;
    max-width: 100%;
  }
}

/* Unscoped is safe: none of these selectors exist on Desktop, so the
   Desktop-identical-above-1024px rule from section 6 still holds. */
@media (prefers-reduced-motion: reduce) {
  .pwa-lock-key,
  #pwaLockForgot,
  .pwa-lock-forgot,
  .pwa-a2hs-dismiss {
    transition: none !important;
  }
}
