/* K RM 1.63.5 — responsive presentation for large iPhone/Android phones.
   Loaded after the existing theme; no form or API behavior is changed. */
@media (max-width: 720px) {
  html, body, .app-shell, .app-shell > main { min-width: 0; max-width: 100%; }
  .app-shell > main {
    width: 100%;
    padding: 16px 14px calc(104px + env(safe-area-inset-bottom));
  }
  .app-shell > main > header {
    display: flex;
    flex-wrap: wrap;
    align-items: flex-start;
    gap: 12px;
    margin-bottom: 18px;
  }
  .app-shell > main > header > :first-child { min-width: 0; flex: 1 1 100%; }
  .app-shell > main > header h1 { overflow-wrap: anywhere; font-size: clamp(23px, 6vw, 30px); }
  .app-shell > main > header .header-actions,
  .app-shell > main > header > :is(button, a.primary, a.secondary) {
    width: 100%;
    min-width: 0;
  }
  .app-shell > main > header .header-actions { display: flex; flex-wrap: wrap; gap: 8px; }
  .app-shell > main > header .header-actions > :not([hidden]) { flex: 1 1 auto; }
  .app-shell > main > header > :is(button, a.primary, a.secondary) { display: inline-flex; justify-content: center; align-items: center; }
  .app-shell > main .section-heading { display: grid; grid-template-columns: minmax(0, 1fr); gap: 10px; }
  .app-shell > main .section-heading > * { min-width: 0; }
  .app-shell > main .filters { display: flex; flex-wrap: wrap; width: 100%; gap: 8px; margin-top: 0; }
  .app-shell > main .filters :is(.search, select) { min-width: 0; width: 100%; flex: 1 1 100%; }
  .app-shell > main .filters .search input { width: calc(100% - 25px); min-width: 0; }
  .app-shell > main :is(.ticket, .record-card, .module-card, .insight-card) { min-width: 0; }
  .app-shell > main :is(.ticket, .record-card) { gap: 10px; }
  .app-shell > main :is(.ticket h3, .record-card h3) { overflow-wrap: anywhere; }
  .app-shell > main .stats { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .app-shell > main .module-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .app-shell > main .courtesy-status-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .app-shell > main .courtesy-status-grid > :last-child:nth-child(odd) { grid-column: 1 / -1; }
  .app-shell > main .critical-grid { grid-template-columns: minmax(0, 1fr); }
  .app-shell > main :is(.table-wrap, .responsive-table, .data-table-wrapper) { max-width: 100%; overflow-x: auto; -webkit-overflow-scrolling: touch; }

  /* Navigation remains available while a page scrolls; the long menu scrolls sideways. */
  .app-shell .sidebar {
    height: calc(70px + env(safe-area-inset-bottom));
    padding: 6px 8px max(6px, env(safe-area-inset-bottom));
  }
  .app-shell .sidebar nav { overflow-x: auto; overflow-y: hidden; scroll-padding-inline: 8px; }
  .app-shell .sidebar nav .nav-item { flex: 0 0 76px; min-height: 55px; }
  .app-shell > main > header .krm-header-user {
    position: static;
    order: -1;
    width: 100%;
    margin: 0 0 4px;
    justify-content: space-between;
  }
  .krm-header-user .krm-header-version { display: inline-block; white-space: normal; font-size: 10px; }

  /* Inputs stay at 16 px on iOS, avoiding focus zoom inside long forms. */
  :is(input:not([type="checkbox"]):not([type="radio"]), textarea, select) { font-size: 16px; }
  :is(button, .primary, .secondary, .close-btn) { min-height: 44px; }
  .overlay.modal-overlay { align-items: flex-end; padding: max(8px, env(safe-area-inset-top)) 0 0; }
  .overlay.modal-overlay .modal {
    width: 100% !important;
    max-width: none !important;
    min-height: 0 !important;
    max-height: calc(100dvh - max(8px, env(safe-area-inset-top)));
    padding: 18px 16px calc(16px + env(safe-area-inset-bottom));
    border-radius: 18px 18px 0 0 !important;
    overflow-x: hidden;
    overflow-y: auto;
    overscroll-behavior: contain;
  }
  .overlay .modal :is(input, select, textarea) { max-width: 100%; }
  .overlay .modal .form-row { grid-template-columns: minmax(0, 1fr); }
  .overlay .modal-actions { position: sticky; bottom: calc(-16px - env(safe-area-inset-bottom)); z-index: 3; gap: 8px; padding: 10px 0 max(8px, env(safe-area-inset-bottom)); background: #fff; }
  .overlay .modal-actions > * { min-width: 0; flex: 1 1 0; }
  .overlay .practice-cost-row { grid-template-columns: minmax(0, 1fr) minmax(80px, 100px) 44px; gap: 6px; }
  .overlay .practice-cost-row .cost-remove { min-width: 44px; padding: 0; }
  .overlay:not(#detailOverlay) .detail-panel { width: 100%; max-width: 100%; height: 100dvh; padding-bottom: max(16px, env(safe-area-inset-bottom)); overflow-x: hidden; }
  .toast { left: 12px; right: 12px; width: auto; max-width: none; bottom: calc(82px + env(safe-area-inset-bottom)); text-align: center; overflow-wrap: anywhere; transform: translateY(30px); }
  .toast.show { transform: none; }
}

/* Menu completo su telefono: evita tab orizzontali tagliati e conserva tutte le voci. */
@media (max-width: 720px) {
  body.krm-menu-open { overflow:hidden; }
  .app-shell .sidebar {
    position:fixed; inset:0 0 auto; width:100%; height:62px; padding:8px 14px;
    display:flex; flex-direction:row; align-items:center; justify-content:space-between;
    background:#173a32; border:0; border-bottom:1px solid #41675b; overflow:visible;
    z-index:var(--z-sidebar);
  }
  .app-shell .sidebar.menu-open { z-index:901; }
  .app-shell .sidebar .brand { display:flex; padding:0; margin:0; }
  .app-shell .sidebar .brand-version { display:none; }
  .app-shell .sidebar .krm-mobile-menu-toggle {
    display:inline-flex; align-items:center; min-height:44px; padding:8px 13px;
    border:1px solid #8bb2a4; border-radius:10px; background:#245b4d;
    color:#fff; font-size:15px; font-weight:750; cursor:pointer;
  }
  .app-shell .sidebar nav {
    display:none; position:fixed; inset:62px auto 0 0; width:min(330px,90vw); height:auto;
    padding:14px 10px max(24px,env(safe-area-inset-bottom)); background:#173a32;
    overflow-x:hidden; overflow-y:auto; overscroll-behavior:contain;
    scroll-snap-type:none; align-content:start; flex-direction:column; gap:5px;
    box-shadow:8px 14px 28px rgba(0,0,0,.22);
  }
  .app-shell .sidebar.menu-open nav { display:flex; }
  .app-shell .sidebar nav .nav-item {
    flex:0 0 auto; width:100%; min-height:46px; padding:10px 12px;
    display:flex; flex-direction:row; justify-content:flex-start; gap:12px;
    font-size:14px; line-height:1.3; text-align:left; white-space:normal;
  }
  .app-shell .sidebar nav .nav-item span { width:24px; flex:0 0 24px; text-align:center; font-size:19px; }
  .app-shell .sidebar nav .nav-item.active { box-shadow:inset 4px 0 #d8b76d; }
  .krm-mobile-menu-backdrop:not([hidden]) {
    display:block; position:fixed; inset:62px 0 0; width:100%; height:calc(100dvh - 62px);
    z-index:900; border:0; background:rgba(9,29,25,.55); cursor:pointer;
  }
  .app-shell > main { padding-top:84px; padding-bottom:max(28px,env(safe-area-inset-bottom)); }
}

@media (max-width: 620px) {
  #detailOverlay { padding: 0; }
  #detailPanel.detail-panel { width: 100%; max-width: 100%; height: 100dvh; }
  #detailPanel > .detail-top { padding: max(16px, env(safe-area-inset-top)) 16px 10px; }
  #detailPanel .detail-top h2 { font-size: clamp(23px, 6vw, 28px); overflow-wrap: anywhere; }
  #detailPanel > .detail-summary { padding: 0 14px 12px; }
  #detailPanel .detail-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  #detailPanel .detail-grid strong { overflow-wrap: anywhere; }
  #detailPanel .ticket-detail-body { min-width: 0; padding: 14px 12px 18px; overscroll-behavior: contain; }
  #detailPanel .ticket-detail-main > :is(.ticket-update, .assignment-box, .ticket-practice-details),
  #detailPanel .ticket-detail-side > .practice-costs-detail { padding: 16px; }
  #detailPanel .phase-mode-switch { width: 100%; }
  #detailPanel .phase-mode-switch button,
  #detailPanel .phase-batch-toolbar button { min-height: 44px; }
  #detailPanel .practice-phase.is-active > summary { padding: 12px; }
  #detailPanel .practice-phase.is-active .phase-heading strong { font-size: 16px; }
  #detailPanel > .detail-actions { grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 8px; padding: 8px 12px max(8px, env(safe-area-inset-bottom)); }
  #detailPanel .detail-actions .ticket-server-confirm { grid-column: 1 / -1; }
  #detailPanel .detail-actions button { min-width: 0; min-height: 44px; padding: 7px 8px; font-size: 12px; white-space: normal; }
}

@media (max-width: 380px) {
  .app-shell > main { padding-inline: 10px; }
  .app-shell > main .stats article { padding: 10px; }
  .overlay .practice-cost-row { grid-template-columns: minmax(0, 1fr) 80px 44px; }
}

@media (max-height: 500px) and (orientation: landscape) {
  .overlay.modal-overlay .modal { max-height: calc(100dvh - 8px); }
  #detailPanel.detail-panel { height: 100dvh; }
}

/* K RM 1.63.6 — one vertical scroll for the entire ticket on phones.
   The panel itself scrolls; its header, workflow, chat and actions are in flow. */
@media (max-width: 720px) {
  #detailPanel.detail-panel {
    display: block;
    height: 100dvh;
    max-height: 100dvh;
    overflow-x: hidden;
    overflow-y: auto;
    overscroll-behavior-y: contain;
    -webkit-overflow-scrolling: touch;
    scrollbar-gutter: auto;
  }
  #detailPanel > :is(.detail-top, .detail-summary, .ticket-detail-body, .detail-actions) {
    position: static;
    flex: none;
    top: auto;
    bottom: auto;
  }
  #detailPanel .ticket-detail-body {
    height: auto;
    min-height: 0;
    overflow: visible;
    scrollbar-gutter: auto;
    overscroll-behavior: auto;
  }
  #detailPanel .phase-batch-toolbar { position: static; }
  #detailPanel .ticket-chat .chat-log { max-height: none; overflow: visible; }
  #detailPanel > .detail-actions {
    padding-bottom: max(16px, env(safe-area-inset-bottom));
  }
}
