/* =====================================================================
   responsive.css
   The mobile layout is a different layout, not a squeezed desktop one:
   the sidebar becomes a bottom navigation bar, tables become stacked
   cards, and every tap target is at least 44px.
   Tested widths: 375, 390, 414, 768, 1024, 1280+.
   ===================================================================== */

/* Mobile-only chrome, hidden on desktop.
   These get explicit display values per element rather than a generic
   `.mobile-only { display: initial }` utility, which would flatten the
   flex/inline-flex layout of the elements it is applied to. */
.bottom-nav       { display: none; }
.topbar-brand     { display: none; }
.topbar-search-btn { display: none !important; }

/* ---------------------------------------------------------------------
   Large tablet / small desktop
   --------------------------------------------------------------------- */
@media (max-width: 1180px) {
  :root { --sidebar-w: 220px; }
  .grid-4 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
}

@media (max-width: 1000px) {
  .grid-3 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
}

/* ---------------------------------------------------------------------
   Mobile / small tablet — the sidebar becomes a bottom nav
   --------------------------------------------------------------------- */
@media (max-width: 900px) {
  .app { grid-template-columns: 1fr; }

  .sidebar { display: none; }

  .desktop-only { display: none !important; }
  .topbar-search-btn { display: inline-flex !important; }

  .view {
    padding: 16px 14px calc(24px + var(--bottomnav-h) + env(safe-area-inset-bottom));
  }

  .topbar { padding: 0 14px; gap: 8px; }

  /* On mobile the search collapses to an icon that opens a search sheet */
  .topbar-search { display: none; }

  .topbar-brand {
    display: flex;
    align-items: center;
    gap: 9px;
    min-width: 0;
    flex: 1;
  }

  .bottom-nav {
    display: grid;
    grid-template-columns: repeat(5, 1fr);
    position: fixed;
    left: 0; right: 0; bottom: 0;
    height: calc(var(--bottomnav-h) + env(safe-area-inset-bottom));
    padding-bottom: env(safe-area-inset-bottom);
    background: rgba(255,255,255,.96);
    backdrop-filter: blur(10px);
    border-top: 1px solid var(--line);
    z-index: 50;
  }

  .bottom-nav a, .bottom-nav button {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 3px;
    border: 0;
    background: none;
    font: inherit;
    font-size: 10.5px;
    font-weight: 550;
    color: var(--ink-500);
    text-decoration: none;
    cursor: pointer;
    min-height: 44px;
    padding: 6px 2px;
  }

  .bottom-nav a.is-active { color: var(--primary-700); }
  .bottom-nav svg { width: 21px; height: 21px; }

  /* "More" sheet */
  .sheet-backdrop {
    position: fixed;
    inset: 0;
    background: rgba(15,23,42,.4);
    z-index: 90;
    display: flex;
    align-items: flex-end;
  }

  .sheet {
    width: 100%;
    background: var(--surface);
    border-radius: 16px 16px 0 0;
    padding: 8px 10px calc(14px + env(safe-area-inset-bottom));
    max-height: 80vh;
    overflow-y: auto;
    animation: sheet-up .18s ease-out;
  }

  .sheet-handle {
    width: 38px; height: 4px;
    border-radius: 999px;
    background: var(--line-strong);
    margin: 8px auto 12px;
  }

  .sheet .nav-item { min-height: 46px; font-size: 15px; }

  @keyframes sheet-up {
    from { transform: translateY(16px); opacity: .6; }
    to   { transform: none; opacity: 1; }
  }

  /* Modals become full-height sheets on phones */
  .modal-backdrop { padding: 0; align-items: flex-end; }

  .modal {
    max-width: 100%;
    border-radius: 16px 16px 0 0;
    max-height: 94vh;
  }

  .modal-footer {
    padding-bottom: calc(14px + env(safe-area-inset-bottom));
    position: sticky;
    bottom: 0;
  }

  .modal-footer .btn { flex: 1; height: 44px; }

  .toast-host { left: 12px; right: 12px; bottom: calc(var(--bottomnav-h) + 14px); }
  .toast { max-width: none; }

  /* Comfortable tap targets */
  .btn { height: 42px; }
  .btn-sm { height: 36px; }
  .input, .select, .textarea { min-height: 44px; font-size: 16px; }

  .view-header { align-items: stretch; }

  /* Detail screens can carry six actions; two per row stays tappable
     instead of squeezing them all onto one line. */
  .view-header .view-actions { display: flex; flex-wrap: wrap; gap: 8px; }
  .view-header .view-actions .btn { flex: 1 1 calc(50% - 4px); min-width: 0; }

  /* Toolbar stacks: full-width search, filters two-up underneath */
  .toolbar { flex-direction: column; align-items: stretch; }
  .toolbar-search { flex: none; }
  .toolbar-filters { display: grid; grid-template-columns: 1fr 1fr; }
  .filter-select { width: 100%; min-width: 0; }
  .toolbar-filters [data-action="clear-filters"] { grid-column: 1 / -1; }

  /* ------------------------------------------------------------------
     Tables -> stacked cards.
     Markup adds data-label="Column name" to each <td>.
     ------------------------------------------------------------------ */
  .table-cards thead { display: none; }

  .table-cards, .table-cards tbody, .table-cards tr, .table-cards td { display: block; width: 100%; }

  .table-cards tr {
    border: 1px solid var(--line);
    border-radius: var(--radius);
    margin-bottom: 10px;
    padding: 4px 0;
    background: var(--surface);
  }

  .table-cards td {
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: 14px;
    border-bottom: 0;
    padding: 7px 14px;
    text-align: right;
  }

  .table-cards td::before {
    content: attr(data-label);
    font-size: 12.5px;
    color: var(--ink-500);
    font-weight: 550;
    text-align: left;
    flex: none;
  }

  /* The first cell reads as the card's title */
  .table-cards td.cell-primary {
    display: block;
    text-align: left;
    font-weight: 600;
    font-size: 15px;
    padding-top: 11px;
  }
  .table-cards td.cell-primary::before { display: none; }

  .kv { grid-template-columns: 110px 1fr; }
  .bar-row { grid-template-columns: 92px 1fr 70px; font-size: 13px; }

  /* ------------------------------------------------------------------
     Calendar on a phone.
     Month keeps the grid but drops to coloured dots — a readable
     overview — with the full detail in the day panel underneath.
     Week becomes a stacked agenda rather than seven unreadable columns.
     ------------------------------------------------------------------ */
  .cal-bar { flex-direction: column; align-items: stretch; gap: 10px; }
  .cal-nav { justify-content: space-between; }
  .cal-title { margin-left: 0; font-size: 15px; }
  .cal-views { display: grid; grid-template-columns: repeat(3, 1fr); }

  .cal-grid { grid-auto-rows: minmax(54px, auto); }
  .cal-cell { padding: 4px 2px; text-align: center; }
  .cal-chips { display: none; }
  .cal-dots { display: flex; justify-content: center; flex-wrap: wrap; padding: 3px 0 0; }
  .cal-daynum { font-size: 12px; padding: 0; }
  .cal-cell.is-today .cal-daynum { margin: 0 auto; }
  .cal-grid-head div { padding: 7px 2px; font-size: 10.5px; }

  .cal-week { grid-template-columns: 1fr; }
  .cal-col { border-right: 0; border-bottom: 1px solid var(--line); }
  .cal-col:last-child { border-bottom: 0; }
  .cal-col-body { min-height: 0; }
  .cal-chip { font-size: 13px; padding: 7px 9px; }
  .cal-chip-meta { white-space: normal; }

  .cal-agenda-time { width: 62px; }

  .status-tiles { display: grid; grid-template-columns: repeat(2, 1fr); }
}

/* ---------------------------------------------------------------------
   Phones
   --------------------------------------------------------------------- */
@media (max-width: 640px) {
  h1 { font-size: 19px; }

  .grid-4, .grid-3, .grid-2 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .grid-stack-mobile { grid-template-columns: 1fr !important; }

  .form-row { grid-template-columns: 1fr; }

  .stat { padding: 13px; }
  .stat-value { font-size: 22px; }

  /* Login: the marketing panel is desktop-only real estate */
  .auth-page { grid-template-columns: 1fr; }
  .auth-aside { display: none; }
  .auth-panel { padding: 26px 18px 40px; align-items: flex-start; }
  .auth-card { max-width: 100%; padding-top: 24px; }
}

@media (max-width: 380px) {
  .grid-4, .grid-3, .grid-2 { grid-template-columns: 1fr; }
  .bottom-nav a, .bottom-nav button { font-size: 10px; }
}

/* ---------------------------------------------------------------------
   Accessibility / print
   --------------------------------------------------------------------- */
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: .01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: .01ms !important;
  }
}

/* ---------------------------------------------------------------------
   Invoice document on small screens
   --------------------------------------------------------------------- */
@media (max-width: 900px) {
  .invoice-doc { padding: 20px 16px; }
  .inv-head { flex-direction: column; gap: 16px; }
  .inv-title-block { text-align: left; }
  .inv-meta { flex-direction: column; gap: 18px; }
  .inv-meta-block:last-child { text-align: left; min-width: 0; }
  .inv-totals-wrap { flex-direction: column; gap: 20px; }
  .inv-totals { width: 100%; }
  .inv-items { font-size: 12.5px; }
  .inv-items thead th, .inv-items tbody td { padding: 8px 6px; }

  /* The line-item editor stacks into labelled rows */
  .item-table thead { display: none; }
  .item-table, .item-table tbody, .item-table tr, .item-table td { display: block; width: 100%; }
  .item-table tr {
    border: 1px solid var(--line);
    border-radius: var(--radius);
    padding: 8px;
    margin-bottom: 10px;
    background: var(--surface);
  }
  .item-table td { display: flex; align-items: center; gap: 10px; padding: 4px 0; text-align: left; }
  .item-table td::before {
    content: attr(data-label);
    font-size: 12px;
    color: var(--ink-500);
    font-weight: 600;
    width: 88px;
    flex: none;
  }
  .item-table td.text-right { justify-content: flex-start; }
  .item-table .input { text-align: left !important; }
}

/* ---------------------------------------------------------------------
   Print — the invoice document only.
   Everything that belongs to the application is removed, so what comes
   out of the printer is the document a customer would expect.
   --------------------------------------------------------------------- */
@media print {
  .sidebar, .topbar, .bottom-nav, .toast-host, .view-actions,
  .no-print, .modal-backdrop, .sheet-backdrop { display: none !important; }

  body { background: #fff; }
  .app { grid-template-columns: 1fr; }
  .view { padding: 0; max-width: none; }
  .card { box-shadow: none; border-color: #ddd; }

  @page { size: A4; margin: 14mm; }

  .invoice-doc {
    border: 0;
    box-shadow: none;
    padding: 0;
    max-width: none;
    font-size: 11pt;
  }

  .inv-items thead { display: table-header-group; }   /* repeat header across pages */
  .inv-items tr { break-inside: avoid; page-break-inside: avoid; }
  .inv-totals-wrap, .inv-payments { break-inside: avoid; page-break-inside: avoid; }
  a { text-decoration: none; color: inherit; }
}
