/*
 * Phone layout. Loaded last so it can adjust the earlier sheets on narrow screens.
 * Nothing is hidden here: list tables turn into labelled cards, button labels stay readable,
 * and long values wrap instead of pushing the page sideways.
 */

/* Long identifiers (keys, references, e-mail addresses) wrap inside their column. */
.details dd {
  min-width: 0;
  overflow-wrap: anywhere;
}

@media screen and (max-width: 760px) {
  /* Top bar: menu, clinic and the new-appointment button share a row; patient search gets its own. */
  .topbar {
    height: auto;
    min-height: 64px;
    flex-wrap: wrap;
    gap: 8px 10px;
    padding: 10px 16px;
  }

  .topbar .menu-button {
    margin-right: 0;
  }

  .topbar > div {
    flex: 1 1 0;
    min-width: 0;
    margin-right: 0;
  }

  .topbar > div strong,
  .topbar > div small {
    display: block;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }

  .topbar .btn {
    flex: 0 0 auto;
    padding: 8px 12px;
    font-size: 13px;
    white-space: nowrap;
  }

  .topbar .btn::after {
    content: none;
  }

  .topbar-search {
    order: 10;
    flex: 1 1 100%;
    margin: 0;
  }

  .messages {
    top: 120px;
    right: 14px;
    width: calc(100vw - 28px);
  }

  /* Button groups wrap onto full rows and keep their whole label. */
  .page-header {
    align-items: stretch;
  }

  .page-header > .btn {
    align-self: flex-start;
  }

  .page-header > div:last-child:not(:first-child),
  .header-actions,
  .workflow-actions,
  .form-actions {
    display: flex;
    flex-wrap: wrap;
    gap: 8px;
    width: 100%;
  }

  .page-header > div:last-child:not(:first-child) > .btn,
  .page-header > div:last-child:not(:first-child) > form,
  .header-actions .btn,
  .workflow-actions > .btn,
  .workflow-actions > form,
  .form-actions > .btn,
  .form-actions > form {
    flex: 1 1 auto;
  }

  .page-header > div:last-child:not(:first-child) > form .btn,
  .workflow-actions > form .btn,
  .form-actions > form .btn {
    width: 100%;
  }

  .btn {
    max-width: 100%;
    text-align: center;
  }

  .btn-tiny {
    padding: 7px 10px;
    font-size: 12px;
  }

  .inline-form .btn,
  .board-actions .btn {
    flex-shrink: 0;
  }

  /* Filter bars: two fields per row instead of one long column; text searches span the row. */
  .toolbar.filters {
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 10px;
    align-items: end;
  }

  .toolbar.filters > * {
    min-width: 0;
  }

  .toolbar.filters > :has(input[type="search"], input[type="text"], textarea),
  .toolbar.filters > .search {
    grid-column: 1 / -1;
  }

  .toolbar.filters > .btn,
  .toolbar.filters > a {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    min-height: 38px;
  }

  /* List tables: each row becomes a card, each cell shows its column heading. */
  .table-wrap.has-stack-table {
    overflow: visible;
    margin: 0;
  }

  table.stack-table,
  .stack-table > tbody,
  .stack-table > tfoot,
  .stack-table > tbody > tr,
  .stack-table > tfoot > tr,
  .stack-table > tbody > tr > td,
  .stack-table > tbody > tr > th,
  .stack-table > tfoot > tr > td,
  .stack-table > tfoot > tr > th {
    display: block;
    width: 100%;
  }

  .table-wrap table.stack-table {
    min-width: 0;
    white-space: normal;
  }

  /* Tables without a single heading row keep their grid but let text wrap before scrolling. */
  .table-wrap > table:not(.stack-table) {
    white-space: normal;
  }

  /* Headings stay available to screen readers; each cell repeats its own. */
  .stack-table > thead {
    position: absolute;
    width: 1px;
    height: 1px;
    overflow: hidden;
    clip-path: inset(50%);
    white-space: nowrap;
  }

  .stack-table > tbody > tr,
  .stack-table > tfoot > tr {
    margin-bottom: 10px;
    padding: 4px 14px;
    border: 1px solid var(--line);
    border-radius: 14px;
    background: rgba(255, 255, 255, 0.55);
  }

  .stack-table > tbody > tr:last-child,
  .stack-table > tfoot > tr:last-child {
    margin-bottom: 0;
  }

  .stack-table > tfoot {
    margin-top: 10px;
  }

  .stack-table > tbody > tr:hover {
    background: rgba(255, 255, 255, 0.55);
  }

  .stack-table > tbody > tr > *,
  .stack-table > tfoot > tr > * {
    position: relative;
    min-height: 38px;
    padding: 10px 0 10px 40%;
    border-bottom: 1px solid var(--line);
    text-align: right;
    overflow-wrap: anywhere;
  }

  .stack-table > tbody > tr > *:last-child,
  .stack-table > tfoot > tr > *:last-child {
    border-bottom: 0;
  }

  .stack-table > tbody > tr > *::before,
  .stack-table > tfoot > tr > *::before {
    content: attr(data-label);
    position: absolute;
    top: 12px;
    left: 0;
    width: calc(40% - 12px);
    color: var(--muted);
    font-size: 10px;
    font-weight: 700;
    letter-spacing: 0.07em;
    line-height: 1.3;
    text-align: left;
    text-transform: uppercase;
    white-space: normal;
  }

  /* Cells without a heading (row actions, "no records" messages) use the full card width. */
  .stack-table > tbody > tr > *:not([data-label]),
  .stack-table > tfoot > tr > *:not([data-label]) {
    padding-left: 0;
    text-align: left;
  }

  .stack-table > tbody > tr > *:not([data-label])::before,
  .stack-table > tfoot > tr > *:not([data-label])::before {
    content: none;
  }

  .stack-table td .person {
    justify-content: flex-end;
    text-align: left;
  }

  .stack-table td:not([data-label]) .person {
    justify-content: flex-start;
  }

  .stack-table td .inline-form,
  .stack-table td .board-actions,
  .stack-table td .patient-row-actions {
    display: flex;
    flex-wrap: wrap;
    gap: 6px;
    justify-content: flex-end;
    max-width: 100%;
  }

  .stack-table td:not([data-label]) .board-actions,
  .stack-table td:not([data-label]) .patient-row-actions {
    justify-content: flex-start;
  }

  .stack-table td .inline-form input,
  .stack-table td .inline-form select {
    flex: 1 1 120px;
    min-width: 0;
  }

  .stack-table td input,
  .stack-table td select,
  .stack-table td textarea {
    max-width: 100%;
  }

  .stack-table .board-actions {
    min-width: 0;
  }

  .board-table-fallback .board-actions {
    min-width: 0;
  }

  html[data-theme="dark"] .stack-table > tbody > tr,
  html[data-theme="dark"] .stack-table > tfoot > tr,
  html[data-theme="dark"] .stack-table > tbody > tr:hover {
    background: var(--dark-raised);
  }
}

/* Tablets and narrow desktops (the sidebar is still showing): the board's date buttons sit above
   the filters, the filters wrap instead of running off the right edge, and sub-navigation tabs
   wrap rather than being cut off. */
@media screen and (min-width: 761px) and (max-width: 1150px) {
  .board-controls {
    flex-direction: column;
    align-items: stretch;
  }

  .board-controls .date-navigation {
    flex-direction: row;
  }

  .board-controls .toolbar.filters {
    flex-wrap: wrap;
    align-items: flex-end;
    gap: 10px;
  }

  .board-controls .toolbar.filters > div {
    flex: 1 1 150px;
    min-width: 0;
  }

  .board-controls .toolbar.filters input,
  .board-controls .toolbar.filters select {
    width: 100%;
  }

  .subnav {
    flex-wrap: wrap;
  }
}
