/* assets/css/mobile.css — Responsive layer. Load AFTER style.css / theme-light.css.
   Desktop rendering is intentionally left untouched: everything below either lives
   inside a max-width media query or is a no-op on large screens. */

:root {
  --topbar-h: 56px;
  --drawer-w: min(84vw, 300px);
}

/* ─── Global hygiene (all sizes) ──────────────── */
html {
  -webkit-text-size-adjust: 100%;
}

body {
  overflow-x: hidden;
}

img,
svg,
video,
canvas {
  max-width: 100%;
  height: auto;
}

/* Mobile chrome — hidden until the phone breakpoint */
.app-topbar,
.sidebar-overlay,
.sidebar-close {
  display: none;
}

/* Touch devices: hover lifts read as stuck states */
@media (hover: none) {
  .btn:hover,
  .card:hover {
    transform: none;
  }

  a,
  button,
  .btn,
  .nav-item {
    -webkit-tap-highlight-color: rgba(88, 166, 255, 0.15);
  }
}

/* ─── Tablet: narrower rail, tighter gutters ──── */
@media (max-width: 1024px) {
  /* Grid children default to min-width:auto, so a wide table stretches its
     track past the viewport instead of scrolling inside its own wrapper. */
  [style*="display: grid"] > * {
    min-width: 0;
  }
}

@media (max-width: 1024px) and (min-width: 769px) {
  .sidebar {
    width: 220px;
  }

  .main-content {
    margin-left: 220px;
    width: calc(100% - 220px);
    padding: 1.5rem;
  }

  /* Two data columns side by side stop being readable below ~1000px */
  [style*="grid-template-columns: 1fr 1fr"],
  [style*="grid-template-columns: 2fr 1fr"] {
    grid-template-columns: 1fr !important;
  }
}

/* ─── Phone: drawer navigation ────────────────── */
@media (max-width: 768px) {

  /* Top app bar */
  .app-topbar {
    display: flex;
    align-items: center;
    gap: 0.75rem;
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    height: calc(var(--topbar-h) + env(safe-area-inset-top));
    padding: env(safe-area-inset-top) 0.75rem 0 0.75rem;
    background: var(--bg-dark);
    background: color-mix(in srgb, var(--bg-dark) 88%, transparent);
    backdrop-filter: blur(14px);
    -webkit-backdrop-filter: blur(14px);
    border-bottom: 1px solid var(--border);
    z-index: 90;
  }

  .topbar-btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 42px;
    height: 42px;
    flex: 0 0 42px;
    border: 1px solid var(--border);
    border-radius: 0.6rem;
    background: transparent;
    color: var(--text-main);
    font-size: 1.05rem;
    cursor: pointer;
    transition: var(--transition);
  }

  .topbar-btn:active {
    background: var(--primary-glow);
    border-color: var(--primary);
  }

  .topbar-title {
    font-weight: 600;
    font-size: 1rem;
    letter-spacing: -0.01em;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    display: flex;
    align-items: center;
    gap: 0.5rem;
  }

  .topbar-title i {
    color: var(--primary);
  }

  /* Drawer */
  .sidebar {
    width: var(--drawer-w);
    height: 100vh;
    height: 100dvh;
    padding-top: env(safe-area-inset-top);
    padding-bottom: env(safe-area-inset-bottom);
    transform: translateX(-100%);
    transition: transform 0.25s cubic-bezier(0.4, 0, 0.2, 1);
    z-index: 100;
  }

  .sidebar.open {
    transform: translateX(0);
  }

  .sidebar-header {
    justify-content: space-between;
    padding: 1.1rem 1rem;
  }

  .sidebar-close {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 36px;
    height: 36px;
    border: 0;
    border-radius: 0.5rem;
    background: transparent;
    color: var(--text-muted);
    font-size: 1rem;
    cursor: pointer;
  }

  /* Comfortable tap targets in the drawer */
  .nav-item {
    padding: 0.95rem 1.25rem;
    font-size: 0.98rem;
    gap: 1rem;
  }

  .sidebar-footer {
    padding: 1rem !important;
    gap: 0.75rem !important;
  }

  .sidebar-footer a {
    min-width: 44px;
    min-height: 34px;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    font-size: 0.8rem !important;
  }

  .sidebar-overlay {
    display: block;
    position: fixed;
    inset: 0;
    background: rgba(2, 6, 12, 0.6);
    backdrop-filter: blur(2px);
    -webkit-backdrop-filter: blur(2px);
    opacity: 0;
    pointer-events: none;
    transition: opacity 0.25s ease;
    z-index: 95;
  }

  .sidebar-overlay.active {
    opacity: 1;
    pointer-events: auto;
  }

  body.nav-open {
    overflow: hidden;
  }

  /* Content area sits below the fixed bar */
  .main-content {
    margin-left: 0;
    width: 100%;
    padding: calc(var(--topbar-h) + env(safe-area-inset-top) + 1rem) 1rem
             calc(2rem + env(safe-area-inset-bottom)) 1rem;
  }

  /* The old in-flow hamburger is replaced by the top bar */
  .mobile-toggle {
    display: none !important;
  }

  /* ── Typography ── */
  /* Long identifiers (emails, invoice numbers, tokens) must not widen the page.
     break-word, not anywhere: it leaves table intrinsic widths alone. */
  td,
  th,
  p,
  h1,
  h2,
  h3,
  .card-header {
    overflow-wrap: break-word;
  }

  h2[style*="font-size: 1.8rem"] {
    font-size: 1.4rem !important;
    margin-bottom: 1.25rem !important;
  }

  div[style*="font-size: 1.8rem"] {
    font-size: 1.35rem !important;
  }

  h1 {
    font-size: 1.6rem;
  }

  h3 {
    font-size: 1.05rem;
  }

  /* ── Cards & spacing ── */
  .card {
    padding: 1.1rem;
    margin-bottom: 1rem;
    border-radius: 0.9rem;
  }

  .card-header {
    flex-wrap: wrap;
    gap: 0.6rem;
    row-gap: 0.75rem;
  }

  /* Any inline flex row is allowed to wrap rather than overflow */
  [style*="display: flex"] {
    flex-wrap: wrap;
  }

  /* ── Inline grids collapse ── */
  [style*="grid-template-columns: 1fr 1fr"],
  [style*="grid-template-columns: 2fr 1fr"],
  [style*="minmax(200px"],
  [style*="minmax(250px"],
  [style*="minmax(280px"],
  [style*="minmax(300px"],
  [style*="minmax(320px"] {
    grid-template-columns: 1fr !important;
  }

  /* Stat tiles stay two-up — six stacked tiles is a scroll marathon */
  [style*="minmax(160px"],
  [style*="minmax(180px"] {
    grid-template-columns: repeat(2, minmax(0, 1fr)) !important;
    gap: 0.75rem !important;
  }

  /* Colour pickers are small enough to stay three-up */
  #custom-colors {
    grid-template-columns: repeat(3, minmax(0, 1fr)) !important;
  }

  /* ── Forms: 16px inputs stop iOS from zooming on focus ── */
  .form-control {
    font-size: 16px;
    padding: 0.7rem 0.85rem;
  }

  select.form-control {
    /* room for the native chevron */
    padding-right: 2rem;
  }

  textarea.form-control {
    min-height: 8rem;
  }

  input[type="checkbox"],
  input[type="radio"] {
    width: 20px !important;
    height: 20px !important;
  }

  /* ── Buttons: 44px minimum touch target ── */
  .btn {
    min-height: 44px;
    padding: 0.65rem 1rem;
  }

  /* Icon-only action buttons stay square instead of stretching */
  .btn.btn-icon {
    min-width: 44px;
    padding: 0.65rem;
  }

  .card-header .btn {
    flex: 0 1 auto;
  }

  /* ── Tables → stacked cards (class applied by mobile.js) ── */
  table.table-stack,
  table.table-stack thead,
  table.table-stack tbody,
  table.table-stack tr,
  table.table-stack td {
    display: block;
    width: 100%;
  }

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

  table.table-stack tr {
    border: 1px solid rgba(128, 128, 128, 0.28) !important;
    border-radius: 0.75rem;
    padding: 0.4rem 0.9rem;
    margin-bottom: 0.75rem;
  }

  table.table-stack tr:last-child {
    margin-bottom: 0;
  }

  table.table-stack td {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 1rem;
    padding: 0.5rem 0 !important;
    border: 0 !important;
    border-bottom: 1px solid rgba(128, 128, 128, 0.16) !important;
    text-align: right !important;
    font-size: 0.9rem;
  }

  table.table-stack td:last-child {
    border-bottom: 0 !important;
  }

  table.table-stack td::before {
    content: attr(data-label);
    flex: 0 0 40%;
    text-align: left;
    font-size: 0.7rem;
    font-weight: 600;
    letter-spacing: 0.05em;
    text-transform: uppercase;
    color: var(--text-muted);
  }

  /* Cells the labeller found no header for (spacers, action columns) */
  table.table-stack td[data-label=""]::before,
  table.table-stack td:not([data-label])::before {
    display: none;
  }

  table.table-stack td.cell-empty {
    display: none;
  }

  /* Full-bleed rows: form fields and action clusters */
  table.table-stack td.cell-input,
  table.table-stack td.cell-actions {
    flex-direction: column;
    align-items: stretch;
    gap: 0.35rem;
    text-align: left !important;
  }

  table.table-stack td.cell-input::before,
  table.table-stack td.cell-actions::before {
    flex: none;
  }

  table.table-stack td.cell-actions > div {
    display: flex;
    gap: 0.5rem;
    flex-wrap: wrap;
  }

  table.table-stack td.cell-actions .btn {
    flex: 1 1 auto;
  }

  /* A message row (colspan empty state) reads as plain text, not a card */
  table.table-stack tr.row-message {
    border: 0 !important;
    padding: 0;
  }

  table.table-stack tr.row-message td {
    justify-content: center;
    text-align: center !important;
    border-bottom: 0 !important;
  }

  /* Tables that keep scrolling get a comfortable rhythm + momentum */
  [style*="overflow-x: auto"] {
    -webkit-overflow-scrolling: touch;
  }

  table:not(.table-stack) th,
  table:not(.table-stack) td {
    padding: 0.6rem 0.5rem !important;
    white-space: nowrap;
  }

  /* Invoice totals block spans the card instead of a fixed 300px rail */
  .invoice-totals {
    width: 100% !important;
  }

  /* ── Modals ── */
  .confirm-box {
    width: calc(100% - 2rem);
    padding: 1.5rem 1.25rem;
  }

  .confirm-box .confirm-actions {
    flex-direction: column-reverse;
  }

  .confirm-box .confirm-actions button {
    width: 100%;
    min-height: 44px;
  }

  /* Email-template preview overlay */
  #preview-modal {
    padding: 0.75rem !important;
  }

  .toast-container {
    top: auto;
    bottom: calc(1rem + env(safe-area-inset-bottom));
    left: 1rem;
    right: 1rem;
  }

  .toast {
    max-width: none;
  }

  /* ── Alerts ── */
  .alert {
    font-size: 0.875rem;
    padding: 0.8rem 1rem;
  }

  /* ── Standalone auth / public pages (no sidebar) ──
     These centre their card with flexbox, which clips the top of anything
     taller than the viewport. Block flow + auto margins scrolls properly. */
  body.page-standalone {
    display: block !important;
    padding: 1.25rem !important;
    padding-top: calc(1.25rem + env(safe-area-inset-top)) !important;
    padding-bottom: calc(1.25rem + env(safe-area-inset-bottom)) !important;
  }

  body.page-standalone .card {
    margin: 1.25rem auto !important;
    padding: 1.25rem !important;
    max-width: 100% !important;
  }

  body.page-standalone .register-container,
  body.page-standalone .invoice-container {
    max-width: 100% !important;
  }

  /* Invoice document: the print-style masthead needs phone proportions */
  body.page-standalone h1 {
    font-size: 1.75rem !important;
  }

  body.page-standalone .invoice-container h2 {
    font-size: 1.1rem !important;
  }

  body.page-standalone [style*="margin-bottom: 3rem"] {
    margin-bottom: 1.5rem !important;
  }

  body.page-standalone [style*="margin-top: 4rem"] {
    margin-top: 2rem !important;
  }

  body.page-standalone [style*="text-align: right"] .btn,
  body.page-standalone form[style*="display: inline"] {
    width: 100%;
  }

  body.page-standalone .no-print .btn {
    width: 100%;
    margin-left: 0 !important;
    margin-bottom: 0.5rem;
    justify-content: center;
  }
}

/* ─── Very small phones ───────────────────────── */
@media (max-width: 360px) {
  .main-content {
    padding-left: 0.75rem;
    padding-right: 0.75rem;
  }

  [style*="minmax(160px"],
  [style*="minmax(180px"] {
    grid-template-columns: 1fr !important;
  }

  table.table-stack td::before {
    flex-basis: 45%;
  }
}

/* ─── Landscape phones: reclaim vertical space ── */
@media (max-width: 900px) and (max-height: 480px) and (orientation: landscape) {
  .app-topbar {
    height: calc(48px + env(safe-area-inset-top));
  }

  .main-content {
    padding-top: calc(48px + env(safe-area-inset-top) + 0.75rem);
  }

  .nav-item {
    padding: 0.6rem 1.25rem;
  }
}
