/**
 * Heart 2 Heart — cross-device / cross-browser responsive foundation
 *
 * Breakpoints (space-based, not device-named):
 *   xs  < 480
 *   sm  480–767
 *   md  768–1023
 *   lg  1024–1439
 *   xl  1440–1919
 *   wide ≥ 1920
 */

:root {
  --h2h-bp-xs: 480px;
  --h2h-bp-sm: 768px;
  --h2h-bp-md: 1024px;
  --h2h-bp-lg: 1440px;
  --h2h-bp-xl: 1920px;
  --h2h-touch: 44px;
  --h2h-page-max: none;
  --h2h-safe-top: env(safe-area-inset-top, 0px);
  --h2h-safe-right: env(safe-area-inset-right, 0px);
  --h2h-safe-bottom: env(safe-area-inset-bottom, 0px);
  --h2h-safe-left: env(safe-area-inset-left, 0px);
  --h2h-vh: 100vh;
}

/* Prefer dynamic viewport height (Safari URL bar). Keep 100vh as fallback above. */
@supports (height: 100dvh) {
  :root { --h2h-vh: 100dvh; }
}

/* ---------- Global overflow & box model ---------- */
html {
  overflow-x: clip;
  -webkit-text-size-adjust: 100%;
  text-size-adjust: 100%;
}

body {
  overflow-x: clip;
  min-height: 100vh;
  min-height: var(--h2h-vh);
}

*,
*::before,
*::after {
  box-sizing: border-box;
}

img,
video,
canvas,
svg,
iframe {
  max-width: 100%;
  height: auto;
}

/* Prefer contain for logos that must not stretch oddly */
img[src*="logo"] {
  object-fit: contain;
}

/* ---------- Readable typography ---------- */
body,
.admin-main,
.h2h-billing,
.h2h-accounting {
  font-size: clamp(0.9375rem, 0.9rem + 0.15vw, 1rem);
  line-height: 1.45;
}

h1, .admin-topbar__title, .ac-header h1, .ae-header h1, .h2h-brand-header h1 {
  font-size: clamp(1.35rem, 1.15rem + 0.8vw, 1.75rem);
  line-height: 1.2;
}

/* ---------- Touch targets ---------- */
@media (pointer: coarse) {
  .admin-sidebar__link,
  .admin-topbar__btn,
  .admin-topbar__menu,
  .site-nav__toggle,
  .h2h-btn,
  .ae-btn,
  .ac-btn,
  .ai-btn,
  button.btn,
  a.btn,
  .ae-menu > summary,
  .ac-menu > summary {
    min-height: var(--h2h-touch);
    min-width: var(--h2h-touch);
  }

  input[type="checkbox"],
  input[type="radio"] {
    width: 1.15rem;
    height: 1.15rem;
  }

  .h2h-receipt-preview__tools button {
    width: var(--h2h-touch) !important;
    height: var(--h2h-touch) !important;
  }
}

/* ---------- Admin shell safe areas & viewport ---------- */
body.admin-page .admin-sidebar {
  height: var(--h2h-vh);
  max-height: var(--h2h-vh);
  padding-top: var(--h2h-safe-top);
  padding-bottom: var(--h2h-safe-bottom);
  padding-left: var(--h2h-safe-left);
}

body.admin-page .admin-topbar {
  padding-top: max(0.75rem, var(--h2h-safe-top));
  position: sticky;
  top: 0;
  z-index: 50;
  backdrop-filter: saturate(1.1) blur(6px);
  -webkit-backdrop-filter: saturate(1.1) blur(6px);
}

body.admin-page .admin-main {
  padding-bottom: max(1.5rem, calc(1rem + var(--h2h-safe-bottom)));
  min-width: 0;
}

body.admin-page .admin-layout__body {
  min-width: 0;
  max-width: 100%;
}

/* Wide content containers — fill the admin content column */
body.admin-page .admin-main,
body.admin-page .admin-main.container,
body.admin-page .ae-page,
body.admin-page .ac-page,
body.admin-page .ai-page,
body.admin-page .h2h-billing {
  width: 100%;
  max-width: none;
  box-sizing: border-box;
}

/* ---------- Table containers: scroll locally, not the page ---------- */
.admin-table-wrapper,
.ae-table-wrap,
.ac-table-wrap,
.ai-table-wrap,
.h2h-table-wrap,
.admin-table-wrap {
  width: 100%;
  max-width: 100%;
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
  overscroll-behavior-x: contain;
}

.admin-table,
.ae-table,
.ac-table,
.h2h-table {
  min-width: 0;
}

/* Prefer wrapping over forcing 600px min on legacy tables inside wrappers */
.admin-table-wrapper .admin-table {
  min-width: 640px;
}

@media (max-width: 700px) {
  .admin-table-wrapper .admin-table {
    min-width: 0;
  }
}

/* Global card-table pattern — tablets + phones (visible change below ~900px) */
@media (max-width: 900px) {
  .h2h-table.h2h-table--cards thead {
    display: none;
  }

  .h2h-table.h2h-table--cards tr {
    display: block;
    border: 1px solid #e2e8f0;
    border-radius: 10px;
    margin-bottom: 0.75rem;
    padding: 0.75rem;
    background: #fff;
  }

  .h2h-table.h2h-table--cards td {
    display: flex;
    justify-content: space-between;
    gap: 0.75rem;
    border: 0;
    padding: 0.3rem 0;
    text-align: right;
  }

  .h2h-table.h2h-table--cards td::before {
    content: attr(data-label);
    font-weight: 700;
    color: #64748b;
    text-align: left;
    flex: 0 0 40%;
  }

  .h2h-table.h2h-table--cards td[data-label="Actions"],
  .h2h-table.h2h-table--cards td.h2h-actions {
    flex-direction: column;
    align-items: stretch;
    text-align: left;
  }

  .h2h-table.h2h-table--cards td[data-label="Actions"]::before,
  .h2h-table.h2h-table--cards td.h2h-actions::before {
    flex: none;
    margin-bottom: 0.25rem;
  }

  .h2h-table.h2h-table--cards .h2h-btn {
    min-height: 44px;
    justify-content: center;
  }

  /* Legacy admin-table card mode */
  .admin-table.admin-table--cards thead {
    display: none;
  }

  .admin-table.admin-table--cards tr {
    display: block;
    border: 1px solid #e2e8f0;
    border-radius: 10px;
    margin-bottom: 0.75rem;
    padding: 0.75rem;
    background: #fff;
  }

  .admin-table.admin-table--cards td {
    display: flex;
    justify-content: space-between;
    gap: 0.75rem;
    border: 0;
    padding: 0.3rem 0;
    text-align: right;
  }

  .admin-table.admin-table--cards td::before {
    content: attr(data-label);
    font-weight: 700;
    color: #64748b;
    text-align: left;
    flex: 0 0 40%;
  }

  .admin-table.admin-table--cards td[data-label="Actions"],
  .admin-table.admin-table--cards td[data-label="Enrolled Classes"] {
    flex-direction: column;
    align-items: stretch;
    text-align: left;
  }

  .admin-table.admin-table--cards td[data-label="Actions"]::before,
  .admin-table.admin-table--cards td[data-label="Enrolled Classes"]::before {
    flex: none;
    margin-bottom: 0.25rem;
  }

  .admin-table.admin-table--cards td[data-label="Enrolled Classes"] {
    min-width: 0 !important;
  }
}

/* ---------- Forms ---------- */
.admin-form input,
.admin-form select,
.admin-form textarea,
.h2h-form-grid input,
.h2h-form-grid select,
.h2h-form-grid textarea,
.ae-field input,
.ae-field select,
.ae-field textarea {
  max-width: 100%;
  font-size: 16px; /* prevents iOS Safari zoom on focus */
}

@media (min-width: 768px) {
  .admin-form input,
  .admin-form select,
  .admin-form textarea,
  .h2h-form-grid input,
  .h2h-form-grid select,
  .h2h-form-grid textarea {
    font-size: inherit;
  }
}

.h2h-form-grid,
.ae-grid-2,
.ac-grid-2 {
  min-width: 0;
}

@media (max-width: 767px) {
  .h2h-form-grid {
    grid-template-columns: 1fr !important;
  }

  .form-row,
  .admin-form .form-row {
    flex-direction: column !important;
  }

  .form-row > *,
  .admin-form .form-row > .form-group {
    width: 100% !important;
    max-width: 100% !important;
    flex: 1 1 100% !important;
    min-width: 0 !important;
  }

  .h2h-btn--primary,
  .ae-btn--primary,
  .ac-btn--primary,
  button[type="submit"].btn-primary {
    width: 100%;
    justify-content: center;
  }
}

/* ---------- KPI / card grids ---------- */
.ae-kpi-strip,
.ac-kpi-strip,
.ai-kpi-strip,
.h2h-cards,
.h2h-acct-primary,
.h2h-acct-secondary,
.admin-kpi-grid {
  grid-template-columns: repeat(auto-fit, minmax(min(100%, 220px), 1fr));
}

@media (min-width: 1440px) {
  .ae-kpi-strip,
  .ac-kpi-strip {
    grid-template-columns: repeat(6, minmax(0, 1fr));
  }
}

@media (max-width: 1100px) {
  .ae-kpi-strip,
  .ac-kpi-strip {
    grid-template-columns: repeat(3, minmax(0, 1fr));
  }
}

@media (max-width: 700px) {
  .ae-kpi-strip,
  .ac-kpi-strip,
  .ai-kpi-strip,
  .h2h-cards,
  .h2h-acct-primary,
  .h2h-acct-secondary {
    grid-template-columns: 1fr;
  }
}

/* ---------- Modals / dialogs ---------- */
dialog,
.h2h-modal,
.modal {
  max-width: min(960px, calc(100vw - 1.5rem));
  max-height: calc(var(--h2h-vh) - 2rem);
}

@media (max-width: 767px) {
  dialog {
    width: calc(100% - 1rem);
    max-height: calc(var(--h2h-vh) - var(--h2h-safe-top) - var(--h2h-safe-bottom) - 1rem);
    margin: auto;
    padding-bottom: max(1rem, var(--h2h-safe-bottom));
  }
}

/* ---------- Sticky actions / toasts / floating UI safe area ---------- */
.ac-sticky-actions,
.ae-sticky-actions,
.h2h-sticky-actions,
.toast,
.toast-container,
.flash-toast,
[role="status"].toast {
  padding-bottom: max(0.75rem, var(--h2h-safe-bottom));
  padding-left: max(0.75rem, var(--h2h-safe-left));
  padding-right: max(0.75rem, var(--h2h-safe-right));
}

/* Safari: avoid gap bugs when flex children shrink */
.admin-layout__body,
.admin-main,
.ae-session-card,
.h2h-receipt-review {
  min-width: 0;
}

/* Date/time inputs: keep usable width across WebKit */
input[type="date"],
input[type="time"],
input[type="datetime-local"] {
  max-width: 100%;
  min-height: 2.5rem;
}

/* Prefer Select File wording styling consistency (no platform chrome assumption) */
input[type="file"] {
  max-width: 100%;
}

/* ---------- Reduced motion ---------- */
@media (prefers-reduced-motion: reduce) {
  *,
  *::before,
  *::after {
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.01ms !important;
    scroll-behavior: auto !important;
  }
}

/* ---------- Landscape phones ---------- */
@media (max-height: 500px) and (orientation: landscape) {
  .admin-topbar {
    position: static;
  }

  body.admin-page .admin-sidebar {
    padding-top: 0;
  }

  .h2h-receipt-preview {
    min-height: 220px !important;
  }
}

/* ---------- Print: hide chrome ---------- */
@media print {
  .admin-sidebar,
  .admin-sidebar-backdrop,
  .admin-topbar,
  .admin-topbar__menu,
  .no-print,
  .site-header,
  .site-footer {
    display: none !important;
  }

  body.admin-page .admin-layout__body,
  body.admin-page .admin-main {
    margin: 0 !important;
    padding: 0 !important;
    max-width: none !important;
  }
}
