/* Phone refinements shared by lists, forms, and the existing clinical workspace.
   Keep intrinsic content visible; only organ tabs and PDF pages scroll sideways. */
html {
  scroll-padding-bottom: calc(var(--savebar-height, 0px) + 20px);
}
html:has(dialog[open]) {
  overflow: hidden;
}
dialog {
  overscroll-behavior: contain;
}
.editor-main {
  padding-bottom: calc(var(--savebar-height, 120px) + 28px);
}
#notice {
  bottom: calc(var(--savebar-height, 0px) + 16px);
  overflow-wrap: anywhere;
}
.measure-footer {
  flex-wrap: wrap;
}
.measurement-dialog .measure-footer > [data-action="measure-apply"] {
  flex-basis: 100%;
  margin-top: 0;
}

@media (max-width: 1023px) {
  .main,
  .editor-main {
    padding-left: max(16px, env(safe-area-inset-left));
    padding-right: max(16px, env(safe-area-inset-right));
  }
  .app-header {
    padding-left: max(16px, env(safe-area-inset-left));
    padding-right: max(16px, env(safe-area-inset-right));
    padding-top: max(10px, env(safe-area-inset-top));
  }
  .app-header.nav-open nav {
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 6px;
    margin: 4px 0 0;
    padding: 12px 0 4px;
    border-top: 1px solid var(--line);
  }
  .app-header .nav-link,
  .shell:has(.editor-main) .nav-link {
    white-space: normal;
    overflow-wrap: anywhere;
    display: flex;
    align-items: center;
    min-height: 48px;
    padding: 12px;
    border: 1px solid transparent;
    border-radius: 10px;
  }
  .shell:has(.editor-main) .nav-link.active {
    background: var(--selected);
    border-color: var(--line);
  }
  .editor-main .savebar {
    padding-left: max(16px, env(safe-area-inset-left));
    padding-right: max(16px, env(safe-area-inset-right));
  }
  [data-mobile-keyboard] .savebar {
    visibility: hidden;
    pointer-events: none;
  }
  dialog {
    max-height: calc(var(--visible-height, 100dvh) - max(12px, env(safe-area-inset-top)));
  }
  [data-mobile-keyboard] dialog {
    position: fixed;
    top: auto;
    bottom: var(--keyboard-inset, 0px);
    margin-bottom: 0;
  }
}

@media (max-width: 767px) {
  .main, dialog {
    overflow-wrap: anywhere;
  }
  .main :is(label, .field, .actions, .section-title > div, .patient-identity > div),
  dialog :is(label, .field, .choice, .sheet-head h2) {
    min-width: 0;
  }
  input, select, textarea {
    min-width: 0;
    max-width: 100%;
    font-size: 16px;
  }
  select {
    width: 100%;
    min-height: 48px;
    padding: 10px;
    border: 1px solid var(--line);
    border-radius: var(--radius);
    color: var(--ink);
    background: var(--surface);
  }
  button, .button, .choice > span {
    overflow-wrap: anywhere;
  }
  .page-heading .actions:empty {
    display: none;
  }
  .intro-stats {
    gap: 12px;
  }
  .stat span {
    min-height: 34px;
  }
  .toolbar .filters {
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 6px;
  }
  .filter {
    font-size: 13px;
    min-height: 44px;
  }
  .editor-main .patient-context h1 {
    overflow-wrap: anywhere;
    font-size: 19px;
  }
  .editor-main .section-title {
    flex-wrap: wrap;
  }
  .editor-main .section-title .text-button {
    white-space: normal;
    font-size: 12px;
  }
  .editor-main .map-actions {
    flex-wrap: wrap;
  }
  .editor-main .map-actions > button {
    flex-basis: 150px;
  }
  .editor-main .measurements:has(> :nth-child(4)):not(:has(> :nth-child(5))) {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }
  .editor-main .measure-value strong {
    max-width: 100%;
    overflow-wrap: anywhere;
  }
  .editor-main .savebar .actions {
    gap: 8px;
  }
  .editor-main .savebar button {
    padding-inline: 8px;
  }
  .editor-main .savebar .savebar-conclusion {
    flex: 0 0 44px;
  }
  .study-selection .choice-group {
    grid-template-columns: minmax(0, 1fr);
  }
  .study-selection .choice span {
    font-size: 14px;
    padding: 10px 12px;
  }
  .sheet-head {
    gap: 12px;
    align-items: flex-start;
  }
  .sheet-head .icon-button {
    flex: 0 0 44px;
  }
  dialog {
    padding-left: max(16px, env(safe-area-inset-left));
    padding-right: max(16px, env(safe-area-inset-right));
    scroll-padding-block: 12px 100px;
  }
  .dialog-actions {
    position: static;
    flex-wrap: wrap;
    gap: 8px;
    padding-bottom: 0;
  }
  .dialog-actions button {
    flex: 1 1 120px;
    min-width: 0;
  }
  .measurement-dialog .measure-footer {
    position: sticky;
    bottom: calc(-1 * max(20px, env(safe-area-inset-bottom)));
    background: var(--surface);
    border-top: 1px solid var(--line);
    padding: 10px 0 max(20px, env(safe-area-inset-bottom));
    gap: 6px;
    z-index: 1;
  }
  .measurement-dialog .measure-footer button {
    min-width: 0;
    min-height: 44px;
  }
  .measurement-dialog .measure-footer .wide {
    padding-block: 8px;
  }
  .ai-usage-controls > label {
    width: 100%;
  }
  .settings > .actions,
  .settings form > .actions,
  .ai-save-row {
    align-items: stretch;
    flex-direction: column;
  }
  .print-theme-option {
    min-height: 44px;
  }
}

/* Small phones and landscape: the keypad stays usable without tiny targets. */
@media (max-width: 1023px) and (max-height: 700px) {
  .measurement-dialog {
    max-height: calc(var(--visible-height, 100dvh) - 12px);
    padding-top: 12px;
  }
  .measurement-dialog .sheet-head,
  .measurement-dialog .measure-tabs,
  .measurement-dialog .number-display {
    margin-bottom: 4px;
  }
  .measurement-dialog .measure-tabs button {
    min-height: 52px;
    padding: 6px 8px;
  }
  .measurement-dialog .unit-switch {
    margin: 6px auto;
  }
  .measurement-dialog .number-display input {
    min-height: 48px;
    font-size: 34px;
  }
  .measurement-dialog .numpad {
    gap: 6px;
  }
  .measurement-dialog .numpad button {
    min-height: 44px;
    padding: 6px;
    font-size: 22px;
  }
  .measurement-dialog .measure-footer {
    margin-top: 8px;
  }
}
@media (min-width: 600px) and (max-width: 1023px) and (max-height: 500px) {
  .measurement-dialog {
    width: calc(100% - 32px);
    max-width: 680px;
    padding: 12px 20px;
    margin: auto;
    border-radius: 20px;
  }
  .measurement-dialog .sheet-head {
    margin-bottom: 4px;
  }
  #measure-editor {
    display: grid;
    grid-template-columns: minmax(0, 1fr) minmax(0, 1fr);
    column-gap: 20px;
    align-items: start;
  }
  .measure-tabs, .unit-switch, .unit-label, .number-display, #measure-error {
    grid-column: 1;
  }
  .measurement-dialog .numpad {
    grid-column: 2;
    grid-row: 1 / 5;
  }
  .measurement-dialog .measure-footer {
    position: static;
    grid-column: 1 / -1;
    padding: 8px 0 0;
    margin-top: 0;
    flex-wrap: nowrap;
  }
  .measurement-dialog .measure-footer > [data-action="measure-apply"] {
    flex: 1;
  }
  .editor-main .savebar {
    padding-block: 8px;
  }
}

/* Use labelled records on phones in either orientation. */
@media (max-width: 767px), (max-width: 1023px) and (max-height: 500px) {
  .table-wrap, .table-wrap > table, .table-wrap tbody {
    display: block;
    width: 100%;
  }
  .table-wrap {
    overflow: visible;
  }
  .table-wrap thead {
    display: none;
  }
  .table-wrap td:not(:first-child):not(:last-child)::before {
    content: attr(data-label);
    display: block;
    font-size: 11px;
    color: var(--muted);
    margin-bottom: 4px;
  }
  .table-wrap > table > tbody > tr {
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 12px 16px;
    padding: 20px 16px;
    border-top: 1px solid var(--line);
  }
  .table-wrap td {
    display: block;
    min-width: 0;
    margin: 0;
    border: 0;
    padding: 0;
    font-size: 13px;
    overflow-wrap: anywhere;
  }
  .table-wrap td:first-child,
  .table-wrap td:last-child {
    grid-column: 1 / -1;
  }
  .table-wrap td .actions {
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }
  .table-wrap td .actions > :last-child:nth-child(odd) {
    grid-column: 1 / -1;
  }
  .table-wrap td:last-child > .button,
  .table-wrap td:last-child > button {
    width: 100%;
  }
  .table-wrap .badge {
    white-space: normal;
  }
  .table-wrap a.patient-name {
    min-height: 44px;
    display: flex;
    align-items: center;
  }
  .table-wrap td:last-child[data-label]:not(:has(button, a))::before {
    content: attr(data-label);
    display: block;
    color: var(--muted);
    font-size: 11px;
    margin-bottom: 4px;
  }
}
