/* ==========================================================================
   DocForce - phone layout
   Everything here sits inside a max-width media query, so desktop and tablet
   (wider than 768px) render exactly as before. Loaded after design-system.css.
   Grid columns on the smallest screens are handled in the markup with
   MudBlazor's own xs/sm breakpoints (xs = under 600px).
   ========================================================================== */

@media (max-width: 768px) {

  html {
    -webkit-text-size-adjust: 100%;
  }

  /* --- Page frame ------------------------------------------------------- */

  /* .container has no rule of its own. The layout wraps every page in one, and the admin
     pages nest a second inside it, so gutters go on what sits directly inside: the main
     menu's cards, and the inner container of the admin pages. Page bars stay full width. */
  .container > .df-card {
    margin-left: max(12px, env(safe-area-inset-left));
    margin-right: max(12px, env(safe-area-inset-right));
  }

  .container > .df-card:first-child {
    margin-top: 12px;
  }

  .container > .container {
    padding: 12px max(12px, env(safe-area-inset-right)) 24px max(12px, env(safe-area-inset-left));
    box-sizing: border-box;
  }

  /* ...but not twice: cards inside the inner container already get its padding. */
  .container > .container > .df-card {
    margin-left: 0;
    margin-right: 0;
  }

  .df-card {
    padding: 14px;
    margin-bottom: 14px;
  }

  .df-card-header {
    margin-bottom: 12px;
    padding-bottom: 10px;
  }

  .df-card-header h2 { font-size: 19px; }
  .df-card-header h3 { font-size: 17px; }

  .df-layout {
    padding: 0 12px 24px;
    gap: 0;
  }

  .df-layout-main {
    flex: 1 1 100%;
  }

  /* --- Top bars --------------------------------------------------------- */

  .df-topbar {
    gap: 8px;
    padding: 8px max(12px, env(safe-area-inset-right)) 8px max(12px, env(safe-area-inset-left));
    min-height: 52px;
    box-sizing: border-box;
  }

  .df-topbar .df-hamburger {
    font-size: 28px;
    min-width: 30px;
    text-align: center;
    flex: 0 0 auto;
  }

  .df-topbar-title {
    flex: 0 1 auto;
    min-width: 0;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }

  /* The product name in the main bar is short and must stay whole; page titles may shrink. */
  .df-topbar:not(.df-pagebar) .df-topbar-title {
    flex-shrink: 0;
  }

  /* Page-level bar (patient name, page title): a slimmer strip under the main bar. */
  .df-pagebar {
    min-height: 42px;
    padding-top: 4px;
    padding-bottom: 4px;
    background: var(--df-primary);
  }

  /* The signed-in name and the "User Name:" labels are noise on a phone. */
  .df-topbar-user,
  .df-role-switch,
  .df-role-chip {
    display: none;
  }

  .df-topbar > form {
    margin-left: auto !important;
  }

  .df-topbar > form {
    flex: 0 0 auto;
  }

  .df-topbar .df-btn-icon {
    color: #fff;
    min-height: 36px;
    padding: 6px 8px !important;
    white-space: nowrap;
  }

  /* The dead hamburger (only the main menu has a slide-out menu). */
  body:not(:has(#mySidenav)) .df-topbar .df-hamburger[onclick] {
    display: none;
  }

  /* Patient search in the top bar: takes the space that is left, so the menu button and Log out always fit */
  .df-topsearch {
    flex: 1 1 0;
    min-width: 70px;
    max-width: none;
    margin-left: 2px;
  }

  .df-topsearch .mud-input input {
    font-size: 16px;   /* 16px stops iOS zooming the page when the box is tapped */
  }

  /* --- Slide-out menu --------------------------------------------------- */

  .df-sidenav {
    max-width: 86vw;
    padding-top: 64px;
  }

  .df-sidenav a,
  .df-sidenav a.df-btn {
    padding: 15px 24px;
    font-size: 17px;
  }

  .df-sidenav .df-closebtn {
    padding: 0 8px;
    line-height: 1.2;
  }

  /* --- Patient record: section switcher becomes a sticky, swipeable strip - */

  .df-layout-icons {
    flex: 0 0 calc(100% + 24px);
    width: calc(100% + 24px);
    max-width: none;
    position: sticky;
    top: calc(var(--df-topbar-h, 52px) + var(--df-pagebar-h, 0px));
    z-index: 800;
    margin: 0 -12px;
    padding: 0 12px;
    box-sizing: border-box;
    background: var(--df-bg);
  }

  .df-layout-icons > .df-card {
    display: flex !important;
    flex-wrap: nowrap;
    gap: 6px;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    scrollbar-width: none;
    padding: 8px !important;
    margin: 8px 0 12px;
    border-radius: 12px;
  }

  .df-layout-icons > .df-card::-webkit-scrollbar {
    display: none;
  }

  .df-layout-icons > .df-card > div {
    margin: 0 !important;
    flex: 0 0 auto;
  }

  .df-layout-icons .mud-button-root {
    width: auto;
    min-height: 40px;
    white-space: nowrap;
    padding: 6px 14px;
    border-radius: 20px;
  }

  /* --- Buttons and inputs: finger-sized, no iOS focus-zoom -------------- */

  .df-btn,
  a.df-btn,
  input.df-btn {
    min-height: 44px;
    padding: 10px 18px;
    font-size: 15px;
  }

  .df-input,
  input.df-input,
  select.df-input,
  textarea.df-input {
    height: 46px !important;
    font-size: 16px !important;
  }

  textarea.df-input {
    height: auto !important;
  }

  .mud-input-slot,
  .mud-input > input,
  .mud-input > textarea,
  .mud-select-input {
    font-size: 16px !important;
  }

  .df-fee-input {
    max-width: none;
  }

  /* A button that sits beside a field on desktop: full row on a phone. */
  .mud-grid-item.d-flex > .df-btn {
    flex: 1 1 auto;
  }

  /* Button rows (Save, Save + Cancel, Rename + Delete) share the width */
  .df-actions .df-btn,
  .df-form-actions .df-btn {
    flex: 1 1 auto;
  }

  /* --- Prescription pad header ------------------------------------------ */

  .df-rx-header {
    gap: 12px;
  }

  .df-rx-icon {
    width: 40px;
    height: 40px;
  }

  .df-rx-meta {
    flex: 1 1 100%;
    text-align: left;
    display: grid;
    grid-template-columns: 1fr auto;
    column-gap: 12px;
    font-size: 13px;
  }

  .df-rx-meta > div:first-child {
    grid-column: 1 / -1;
  }

  /* --- Tables ----------------------------------------------------------- */

  .df-table th,
  .df-table td {
    padding: 10px 8px;
  }

  /* Wide report tables scroll sideways inside their card instead of breaking the page. */
  .df-table-scroll {
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    margin: 0 -4px;
    padding-bottom: 6px;
    border-radius: 8px;
  }

  .df-table-scroll .df-table {
    min-width: 520px;
  }

  .df-table-scroll .df-table th,
  .df-table-scroll .df-table td {
    white-space: nowrap;
  }

  /* Record lists (Users, Locations): each row becomes a small card, label: value */
  .df-table-stack,
  .df-table-stack tbody,
  .df-table-stack tr,
  .df-table-stack td {
    display: block;
    width: 100%;
    box-sizing: border-box;
  }

  .df-table-stack {
    border: none;
    background: transparent;
  }

  .df-table-stack thead {
    display: none;
  }

  .df-table-stack tr {
    background: var(--df-surface);
    border: 1px solid var(--df-border);
    border-radius: 10px;
    padding: 6px 12px;
    margin-bottom: 10px;
  }

  .df-table-stack td {
    display: flex;
    gap: 12px;
    justify-content: space-between;
    align-items: center;
    padding: 6px 0;
    border: none;
    text-align: right;
    word-break: break-word;
  }

  .df-table-stack td[data-label]::before {
    content: attr(data-label);
    flex: 0 0 76px;
    text-align: left;
    color: var(--df-text-muted);
    font-weight: 600;
    font-size: 12.5px;
  }

  .df-table-stack td.df-td-actions {
    display: block;
    padding-top: 10px;
    text-align: left;
  }

  .df-table-stack tr:hover td {
    background: transparent;
  }

  /* Patient search results: name + (id, phone) on the left, Select on the right */
  .df-table-list,
  .df-table-list tbody,
  .df-table-list tr,
  .df-table-list td {
    display: block;
    box-sizing: border-box;
  }

  .df-table-list {
    border: none;
    background: transparent;
    width: 100%;
  }

  .df-table-list thead {
    display: none;
  }

  .df-table-list tr {
    display: grid;
    grid-template-columns: auto 1fr auto;
    grid-template-areas:
      "name name act"
      "id   phone act";
    align-items: center;
    column-gap: 12px;
    row-gap: 2px;
    background: var(--df-surface);
    border: 1px solid var(--df-border);
    border-radius: 10px;
    padding: 10px 12px;
    margin-bottom: 8px;
  }

  .df-table-list td {
    border: none;
    padding: 0;
  }

  .df-table-list td.df-c-name {
    grid-area: name;
    font-weight: 700;
    font-size: 15px;
    word-break: break-word;
  }

  .df-table-list td.df-c-id,
  .df-table-list td.df-c-phone {
    color: var(--df-text-muted);
    font-size: 13px;
  }

  .df-table-list td.df-c-id { grid-area: id; }
  .df-table-list td.df-c-id::before { content: "ID "; }
  .df-table-list td.df-c-phone { grid-area: phone; }
  .df-table-list td.df-c-act { grid-area: act; }

  .df-table-list tr:hover td {
    background: transparent;
  }

  /* An empty-result row is a single message cell, not a grid row. */
  .df-table-list td[colspan] {
    grid-column: 1 / -1;
  }

  .df-table-list tr:has(td[colspan]) {
    display: block;
  }

  .df-pager {
    justify-content: space-between;
  }

  .df-pager .df-btn {
    min-height: 40px;
    padding: 8px 12px;
  }

  /* --- Dialogs and pickers ---------------------------------------------- */

  .mud-dialog {
    margin: 12px;
    width: calc(100% - 24px);
    max-height: calc(100dvh - 24px);
  }

  .mud-dialog .mud-dialog-content {
    padding: 8px 16px;
  }

  .mud-dialog .mud-dialog-actions {
    padding: 8px 16px 14px;
  }

  .mud-dialog .mud-dialog-actions .df-btn {
    flex: 1 1 0;
  }

  /* Tab headers (Users, System, Fees) stay on one swipeable row */
  .mud-tabs-tabbar .mud-tab {
    min-width: 0;
    padding: 6px 14px;
  }

  /* --- Sign-in pages ---------------------------------------------------- */

  .df-login-page {
    min-height: 100vh;
    min-height: 100dvh;
    padding: 16px;
    align-items: flex-start;
    padding-top: 12vh;
  }

  .df-login-card {
    padding: 26px 18px;
  }
}

/* Very small phones */
@media (max-width: 380px) {
  .df-card {
    padding: 12px;
  }

  .df-topbar-title {
    font-size: 15px;
  }
}

/* Phones held sideways: keep the sticky bars from eating the screen */
@media (max-width: 900px) and (max-height: 480px) {
  .df-layout-icons {
    position: static;
  }
}

/* Phones, staging site only: the STAGING strip takes room, so the product name gives way and the search box keeps a usable width */
@media (max-width: 480px) {
  .df-topbar:has(.df-env-topbar) > .df-topbar-title { display: none; }
  .df-topbar .df-env-topbar { flex: 0 0 auto; }
}

/* Appointment calendar on a phone: small cells with just the number of appointments; the day's list is underneath */
@media (max-width: 600px) {
  .df-cal-months { grid-template-columns: 1fr; gap: 16px; }
  .df-cal-day { min-height: 46px; padding: 4px; }
  .df-cal-chips { display: none; }
  .df-cal-total { display: block; }
}

/* Patient screen tabs on a phone: smaller, so several fit and it is clear the strip continues (swipe sideways) */
@media (max-width: 768px) {
  .df-layout-icons .mud-button-root {
    min-height: 36px;
    padding: 4px 12px;
    font-size: 12.5px;
    letter-spacing: .02em;
  }
  .df-layout-icons .mud-button-icon-start { margin-right: 6px; }
  .df-layout-icons .mud-button-icon-start .mud-icon-root { font-size: 1.15rem; }
}
