/* ============================================================================
   Almond HR Portal - mobile interface
   ----------------------------------------------------------------------------
   Loaded only under 640px. The desktop layout is untouched.

   This is not the desktop UI shrunk down. Phones get a different structure,
   because staff use the app differently there: employees log the days they
   worked and read payslips; managers glance at figures rather than edit them.
   The design follows from that.

     - App shell    compact sticky header, navigation along the bottom where a
                    thumb reaches, safe-area aware for notches.
     - Tables       become stacked cards with labelled rows. A payroll table is
                    unreadable at 375px; a list of labelled values is not.
                    Exception: the Run Payroll grid stays a scrolling table,
                    because it is a spreadsheet you edit, not a list you read.
     - Modals       become bottom sheets that slide up, with a sticky footer so
                    the buttons are always reachable.
     - Type         larger, fewer sizes, more spacing. 16px inputs so iOS does
                    not zoom the page on focus.
   ========================================================================== */

@media (max-width: 640px) {

  /* ---- Foundations ------------------------------------------------------ */

  body {
    overflow-x: hidden;
    background: var(--bg);
    -webkit-text-size-adjust: 100%;
  }

  /* Grid and flex children default to min-width:auto and refuse to shrink
     below their content, which is what pushed fields past the screen edge. */
  .form-grid > *, .form-group, .card, .section { min-width: 0; }

  /* ---- App shell: header ------------------------------------------------ */

  .header {
    position: sticky; top: 0; z-index: 800;
    min-height: 0;
    padding: calc(10px + env(safe-area-inset-top)) 16px 10px;
    gap: 10px;
    box-shadow: 0 1px 8px rgba(15, 73, 63, 0.22);
  }
  .header-brand { gap: 10px; min-width: 0; }
  .header-logo { width: 26px; height: 26px; }
  .header-title {
    font-size: 15px; line-height: 1.2;
    white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
  }
  /* Compliance strapline is desktop reassurance, not phone content. */
  .header-badge { display: none; }

  .header-session, .session-panel {
    width: 100%; justify-content: space-between; gap: 8px; flex-wrap: wrap;
  }

  /* ---- App shell: bottom navigation ------------------------------------- */

  .tabs {
    position: fixed; bottom: 0; left: 0; right: 0; z-index: 900;
    display: flex; align-items: stretch;
    margin: 0; padding: 0 2px calc(2px + env(safe-area-inset-bottom));
    background: rgba(255, 255, 255, 0.96);
    -webkit-backdrop-filter: saturate(180%) blur(12px);
    backdrop-filter: saturate(180%) blur(12px);
    border-top: 1px solid var(--border);
    box-shadow: 0 -1px 12px rgba(15, 73, 63, 0.10);
    overflow: visible;
  }

  .tab {
    flex: 1 1 0; min-width: 0;
    display: flex; flex-direction: column; align-items: center; justify-content: center;
    gap: 3px;
    padding: 8px 2px 6px;
    margin: 0;
    font-family: var(--font-body);
    font-size: 10px; font-weight: 600; line-height: 1.15;
    color: var(--text-muted);
    border-bottom: none;
    border-radius: 10px;
    white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
    -webkit-tap-highlight-color: transparent;
  }
  /* mobile.js splits each tab's emoji from its words so they can be sized
     independently; without it the label is just smaller text. */
  .tab .m-tab-icon { font-size: 19px; line-height: 1; }
  .tab .m-tab-label {
    max-width: 100%; overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
  }
  .tab.active { color: var(--forest); background: var(--mint); font-weight: 700; }
  .tab.active .m-tab-icon { transform: translateY(-1px); }
  /* Tabs beyond the fifth live in the More sheet instead of being squeezed. */
  .tab.m-overflow { display: none; }

  /* ---- Content ---------------------------------------------------------- */

  .main {
    padding: 14px 12px calc(78px + env(safe-area-inset-bottom));
    max-width: none;
  }

  .card {
    padding: 16px 14px;
    margin-bottom: 14px;
    border-radius: 14px;
    box-shadow: 0 1px 6px rgba(56, 132, 121, 0.08);
  }
  .card h2, .card h3 { font-size: 16px; }

  .summary-grid, .schedule-layout, .contracts-layout { grid-template-columns: 1fr; }
  .schedule-stat { min-width: 0; }

  /* ---- Forms ------------------------------------------------------------ */

  .form-grid { grid-template-columns: 1fr; gap: 14px; }
  .form-group { gap: 6px; }
  .form-group label { font-size: 12.5px; font-weight: 600; }
  .field-note { font-size: 11.5px; line-height: 1.4; }

  /* 16px minimum: below it, iOS zooms the page when a field is focused and
     leaves the layout zoomed afterwards. Specificity must match the desktop
     rules these override. */
  input, select, textarea,
  .form-group input, .form-group select, .form-group textarea,
  .modal input, .modal select, .modal textarea {
    font-size: 16px;
    width: 100%;
    max-width: 100%;
    box-sizing: border-box;
    padding: 11px 12px;
    border-radius: 10px;
  }

  /* ---- Buttons ---------------------------------------------------------- */

  .btn {
    min-height: 44px;             /* platform guidance for a comfortable tap */
    padding: 11px 16px;
    font-size: 14px;
    border-radius: 11px;
    justify-content: center;
  }
  .btn-sm { min-height: 38px; padding: 8px 12px; font-size: 13px; }
  .btn:hover { transform: none; }   /* no hover lift on touch */

  /* Groups of buttons stack instead of overflowing. */
  .rec-actions, .card-actions, .toolbar {
    display: flex; flex-wrap: wrap; gap: 8px;
  }
  .rec-actions .btn, .card-actions .btn { flex: 1 1 auto; }

  /* ---- Tables as cards --------------------------------------------------
     mobile.js adds .m-cards to read-heavy tables and stamps each cell with
     the text of its column heading. */

  table.m-cards, table.m-cards tbody, table.m-cards tr, table.m-cards td {
    display: block; width: auto;
  }
  table.m-cards thead { display: none; }
  table.m-cards { border-collapse: separate; }

  table.m-cards tr {
    background: var(--white);
    border: 1px solid var(--border);
    border-radius: 12px;
    padding: 12px 14px;
    margin-bottom: 10px;
    box-shadow: 0 1px 4px rgba(56, 132, 121, 0.07);
  }

  table.m-cards td {
    display: flex; align-items: baseline; justify-content: space-between;
    gap: 14px;
    padding: 7px 0;
    border: none;
    border-bottom: 1px solid var(--mint);
    font-size: 13.5px;
    text-align: right;
    white-space: normal;
  }
  table.m-cards td:last-child { border-bottom: none; padding-bottom: 0; }
  table.m-cards td:first-child { padding-top: 0; }

  table.m-cards td::before {
    content: attr(data-label);
    flex: 0 0 42%;
    text-align: left;
    color: var(--text-muted);
    font-size: 12px; font-weight: 600;
    text-transform: uppercase; letter-spacing: 0.3px;
  }
  /* A cell with no heading (action buttons) spans the card. */
  table.m-cards td.m-nolabel { display: block; text-align: left; }
  table.m-cards td.m-nolabel::before { content: none; }
  /* The first column is the row's subject, so it reads as a card title. */
  table.m-cards td.m-title {
    display: block; text-align: left;
    font-family: var(--font-display);
    font-size: 15px; font-weight: 700; color: var(--charcoal);
    padding-bottom: 9px;
  }
  table.m-cards td.m-title::before { content: none; }
  table.m-cards td input, table.m-cards td select { width: auto; min-width: 90px; }

  /* The Run Payroll grid stays a table: it is an editing surface where
     columns must line up, so it scrolls sideways within its own box. */
  .payroll-table:not(.m-cards) { display: block; overflow-x: auto; -webkit-overflow-scrolling: touch; white-space: nowrap; }

  /* ---- Modals as bottom sheets ------------------------------------------ */

  .modal-overlay {
    align-items: flex-end;
    padding: 0;
    max-width: 100vw;
    overflow: hidden;
  }
  .modal {
    width: 100% !important;
    max-width: 100% !important;
    max-height: 92vh;
    margin: 0;
    padding: 8px 16px calc(16px + env(safe-area-inset-bottom));
    border-radius: 20px 20px 0 0;
    display: flex; flex-direction: column;
    transform: translateY(100%);
    transition: transform 0.28s cubic-bezier(0.32, 0.72, 0, 1);
    overflow: hidden;
  }
  .modal-overlay.show .modal { transform: translateY(0); }

  /* The grab handle that signals "this sheet slides". */
  .modal::before {
    content: '';
    flex: 0 0 auto;
    width: 38px; height: 4px;
    margin: 4px auto 12px;
    border-radius: 2px;
    background: var(--border);
  }
  .modal-title {
    flex: 0 0 auto;
    font-size: 17px;
    margin-bottom: 14px;
    padding-right: 4px;
  }
  /* Only the middle scrolls, so the title and actions stay put. */
  .modal > *:not(.modal-title):not(.modal-footer) {
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
  }
  .modal-footer {
    flex: 0 0 auto;
    display: flex; gap: 10px;
    margin-top: 14px;
    padding-top: 12px;
    border-top: 1px solid var(--border);
  }
  .modal-footer .btn { flex: 1 1 0; }

  /* ---- Misc ------------------------------------------------------------- */

  .empty { padding: 36px 18px; }
  .empty-icon { font-size: 34px; }
  .toast { left: 12px; right: 12px; bottom: calc(84px + env(safe-area-inset-bottom)); text-align: center; }

  .schedule-calendar { font-size: 12px; }
  .schedule-calendar td, .schedule-calendar th { padding: 5px 2px; }

  /* ---- The More sheet ---------------------------------------------------- */

  .m-more-sheet {
    position: fixed; inset: 0; z-index: 950;
    display: none;
    background: rgba(15, 73, 63, 0.35);
    -webkit-backdrop-filter: blur(2px); backdrop-filter: blur(2px);
  }
  .m-more-sheet.show { display: block; }
  .m-more-panel {
    position: absolute; left: 0; right: 0; bottom: 0;
    background: var(--white);
    border-radius: 20px 20px 0 0;
    padding: 8px 12px calc(14px + 62px + env(safe-area-inset-bottom));
    box-shadow: 0 -6px 24px rgba(15, 73, 63, 0.18);
    transform: translateY(100%);
    transition: transform 0.26s cubic-bezier(0.32, 0.72, 0, 1);
  }
  .m-more-sheet.show .m-more-panel { transform: translateY(0); }
  .m-more-panel::before {
    content: ''; display: block;
    width: 38px; height: 4px; margin: 4px auto 12px;
    border-radius: 2px; background: var(--border);
  }
  .m-more-item {
    display: flex; align-items: center; gap: 14px;
    padding: 14px 10px;
    font-family: var(--font-display); font-size: 15px; font-weight: 600;
    color: var(--charcoal);
    border-radius: 12px;
  }
  .m-more-item:active { background: var(--mint); }
  .m-more-item .m-tab-icon { font-size: 21px; }
}

/* Landscape phones: the bottom bar would eat too much of a short screen. */
@media (max-width: 900px) and (max-height: 450px) and (orientation: landscape) {
  .tabs { position: static; backdrop-filter: none; }
  .main { padding-bottom: 20px; }
}
