/* Responsive overrides load after component/appearance styles so later
   desktop refinements cannot silently undo the phone layout. */
@media (max-width: 68rem) {
  :root { --topbar-height: calc(3.75rem + env(safe-area-inset-top)); }
  .application { height: var(--workspace-height, 100dvh); }
  .shell { grid-template-rows: var(--topbar-height) minmax(0, 1fr); }
  .shell__body { padding-inline: env(safe-area-inset-left) env(safe-area-inset-right); }
  .drawer, .scrim { bottom: auto; height: calc(var(--workspace-height, 100dvh) - var(--topbar-height)); }
  .drawer { padding-left: env(safe-area-inset-left); padding-bottom: env(safe-area-inset-bottom); }
  .topbar { padding-top: env(safe-area-inset-top); padding-left: max(.5rem, env(safe-area-inset-left)); padding-right: max(.5rem, env(safe-area-inset-right)); }
  .main__middle { overscroll-behavior: contain; }
  .main__footer { padding-bottom: max(.5rem, env(safe-area-inset-bottom)); }
  .chat-preview { top: var(--topbar-height); bottom: auto; height: calc(var(--workspace-height, 100dvh) - var(--topbar-height)); padding-bottom: env(safe-area-inset-bottom); }

  /* Reserve a place in the top bar for help instead of covering form actions. */
  .application:has(.kintari-launcher:not([style])) .topbar { padding-right: calc(3.5rem + env(safe-area-inset-right)); }
  .application:has(.shell[data-view]) .kintari-launcher:not([style]) {
    top: calc(.5rem + env(safe-area-inset-top)); right: max(.5rem, env(safe-area-inset-right)); bottom: auto; left: auto;
    width: 2.75rem; height: 2.75rem; min-height: 2.75rem; padding: .25rem;
    gap: 0; font-size: 0; box-shadow: none;
  }
  .application:has(.shell[data-view]) .kintari-launcher:not([style]) .kintari-avatar { width: 2rem; height: 2rem; }
  .kintari-guide { top: calc(var(--topbar-height) + .5rem); bottom: auto; max-height: calc(var(--workspace-height, 100dvh) - var(--topbar-height) - 1rem); }
  /* Keep the highlighted chapter target visible above the tour panel. */
  .kintari-guide[data-target] { top: auto; bottom: max(.75rem, env(safe-area-inset-bottom)); max-height: 50dvh; }
  .topbar { gap: .25rem; }
  .topbar__tools { gap: .125rem; min-width: 0; }
  .topbar__brand { flex: 0 0 auto; }
  .topbar__brand .brand__name { display: none; }
  .main__header { min-width: 0; }

  /* Two compact rows keep the draft, attachment actions, model and Send visible. */
  .shell[data-view='chat'] .composer__toolbar { flex-wrap: nowrap; gap: .125rem; }
  .shell[data-view='chat'] .chat-model-picker { flex: 1 1 0; max-width: none; }
  .shell[data-view='chat'] .chat-model-picker__current { flex: 1; }
  .shell[data-view='chat'] .chat-model-picker__current strong { max-width: 100%; }
  .composer__actions { flex: 0 0 auto; gap: .125rem; }
  .chat-attachments__toolbar { gap: .25rem; }
  .chat-attachments__toolbar .ui-action > span { display: none; }
  .chat-attachments__toolbar .ui-action { width: 2.75rem; padding: .5rem; }
  .shell[data-view='chat'] .chat-attachments__settings-toggle { margin-left: auto; }
  .shell[data-view='chat'] .composer__field { max-height: min(8rem, 24dvh); }
  .shell[data-view='chat'] .main__footer { padding-bottom: max(.5rem, env(safe-area-inset-bottom)); }
  .chat-model-picker__popover { max-height: calc(var(--workspace-height, 100dvh) - var(--topbar-height) - 5rem); overflow-y: auto; }
  .composer__more-menu { max-height: calc(var(--workspace-height, 100dvh) - var(--topbar-height) - 5rem); }
  .composer--enhanced .chat-project-files, .composer--enhanced .chat-attachments__options {
    position: absolute; bottom: calc(100% + .5rem); left: 0; right: 0; z-index: 24;
    max-height: calc(var(--workspace-height, 100dvh) - var(--topbar-height) - 14rem);
    overflow-y: auto; margin: 0; background: var(--surface-raised); box-shadow: 0 8px 24px #0002;
  }

  /* Long labels and editable attribute rows need to reflow, not clip. */
  .xml-attrs__row { grid-template-columns: minmax(0, 1fr) auto; }
  .xml-attrs__row > :first-child { grid-column: 1 / -1; }
  .xml-attrs__row .field { min-width: 0; }
  .tei-header dl { grid-template-columns: minmax(0, 1fr); overflow-wrap: anywhere; }
  .tei-header dd { margin-left: 0; }
  .crm-form__actions, .skills-dialog__footer { flex-wrap: wrap; }
  dialog { max-width: calc(100vw - 1rem); max-height: calc(var(--workspace-height, 100dvh) - 1rem); }
  .skills-dialog, .chat-copy-dialog, .command-dialog { max-height: calc(var(--workspace-height, 100dvh) - 1rem); }
  .command-dialog { margin-block: .5rem; }
}

@media (pointer: coarse) {
  /* Keep compact icons but give fingers a reliable target. */
  .button, .shell:not([data-view='chat']) .main .button,
  .shell[data-view='chat'] .chat-attachments__toolbar .button,
  .chat-model-picker > summary, .chat-menu > summary, .composer__more > summary,
  .chat-model-picker__option, .panel__item, .apps__tabs > button,
  .conversation__button, .conversation__actions .button { min-height: 44px; }
  .button--icon, .ui-action, .chat-menu > summary, .composer__more > summary { min-width: 44px; }
  .composer__more > summary { display: grid; place-items: center; }
  /* Native iOS form focus must not zoom the page. Keep browser zoom enabled. */
  .application input:where(:not([type='checkbox']):not([type='radio']):not([type='range']):not([type='file'])),
  .application select, .application textarea, .application .field,
  .shell[data-view='chat'] textarea.composer__field,
  .skills-dialog .skills-code { font-size: 16px; }
  .conversation__actions, .tree__zip { opacity: 1; }
}

@media (max-width: 68rem) and (max-height: 500px) {
  .shell[data-view='chat'] { --header-height: 2.75rem; }
  .shell[data-view='chat'] .composer--enhanced { padding-block: .25rem; }
  .shell[data-view='chat'] .composer__field { min-height: 2.5rem; }
  .shell[data-view='chat'] .main__footer { padding-top: .25rem; }
}
