/* =========================================================================
   RESPONSIVE & DEVICE-ADAPTIVE LAYER
   Loaded last on every page (after page themes). Layout adapts to the
   available width; device *capabilities* (touch, safe areas, installed
   mode) are handled with media/feature queries, never user-agent checks.
     <=760px   phones: compact header, categories + account in the menu
     761-1080  tablets: wrapped category bar, role links in the menu
     >1080     desktop: everything inline
   ========================================================================= */

img, video { max-width: 100%; }

/* ---- Accessibility ------------------------------------------------------ */
.skip-link {
  position: absolute; left: 8px; top: -60px; z-index: 100;
  background: var(--color-primary); color: #fff;
  padding: 10px 16px; border-radius: var(--radius-md); font-weight: 700;
}
.skip-link:focus { top: 8px; text-decoration: none; }
#main-content:focus { outline: none; }

a:focus-visible, button:focus-visible, summary:focus-visible, .btn:focus-visible {
  outline: 3px solid var(--color-gold);
  outline-offset: 2px;
}

.sr-only {
  position: absolute !important; width: 1px; height: 1px; padding: 0; margin: -1px;
  overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; border: 0;
}

@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;
  }
}

/* ---- Safe areas (notched phones, installed app) ------------------------- */
body {
  padding-left: env(safe-area-inset-left);
  padding-right: env(safe-area-inset-right);
}
.site-footer { padding-bottom: env(safe-area-inset-bottom); }
.toast-container { bottom: calc(var(--space-5) + env(safe-area-inset-bottom)); }

/* ---- Touch devices: no iOS focus-zoom, comfortable targets -------------- */
@media (hover: none) and (pointer: coarse) {
  /* iOS Safari zooms the page into any focused field under 16px. */
  input[type="text"], input[type="email"], input[type="tel"], input[type="password"],
  input[type="number"], input[type="search"], select, textarea {
    font-size: 16px;
  }
  .btn { min-height: 44px; }
  .btn--sm { min-height: 40px; }
  .tab { min-height: 44px; }
  .gc-card:hover { transform: none; }
}

/* ---- Header: install button --------------------------------------------- */
.header-install { display: inline-flex; align-items: center; gap: 6px; }
@media (max-width: 1280px) {
  .header-install { padding-left: 0; padding-right: 0; width: 43px; justify-content: center; }
  .header-install__label {
    position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap;
  }
}

/* ---- Header: menu drawer (tablet + phone) ------------------------------- */
.nav-drawer { display: none; }

@media (max-width: 1080px) {
  /* brand | actions | menu button, brand shrinking first */
  .site-header__inner { grid-template-columns: minmax(0, 1fr) auto auto; }
  .site-header__actions { order: 2; }
  .site-header__burger { order: 3; }
  .brand { min-width: 0; }
  .brand__text { min-width: 0; }

  body.nav-open .site-nav-center {
    top: 100%;
    max-height: calc(100vh - var(--site-header-h, 64px));
    max-height: calc(100dvh - var(--site-header-h, 64px));
    overflow-y: auto;
    overscroll-behavior: contain;
    gap: 0;
    align-items: stretch;
    white-space: normal;
    box-shadow: 0 14px 28px rgba(16, 36, 58, 0.14);
    padding: var(--space-2) max(var(--space-5), env(safe-area-inset-right))
             calc(var(--space-5) + env(safe-area-inset-bottom)) max(var(--space-5), env(safe-area-inset-left));
    z-index: 40;
  }
  body.nav-open .site-nav-center > a,
  .nav-drawer > a {
    display: flex; align-items: center;
    min-height: 48px; padding: 0 4px;
    border-bottom: 1px solid var(--color-border);
    font-size: 15px; font-weight: 600; color: var(--color-text);
  }
  .site-header__burger span { transition: transform 160ms ease, opacity 160ms ease; }
  body.nav-open .site-header__burger span:nth-child(1) { transform: translateY(6px) rotate(45deg); }
  body.nav-open .site-header__burger span:nth-child(2) { opacity: 0; }
  body.nav-open .site-header__burger span:nth-child(3) { transform: translateY(-6px) rotate(-45deg); }
}

/* Signed-out tablets have nothing in the menu (categories and
   Sign in/Register are already visible), so no menu button. */
@media (min-width: 761px) and (max-width: 1080px) {
  .site-header__burger--anon { display: none; }
}

@media (max-width: 760px) {
  .site-header__inner {
    height: 60px;
    padding: 0 max(var(--space-3), env(safe-area-inset-right)) 0 max(var(--space-3), env(safe-area-inset-left));
    gap: var(--space-2);
  }
  .brand { gap: 8px; }
  .brand__mark { width: 40px; height: 40px; }
  .brand__text { max-width: none; }
  .brand__text strong {
    font-size: clamp(14px, 4.2vw, 18px);
    display: block; white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
  }
  .brand__text small { display: none; }

  /* Categories, account, Register, Sign out and Install move into the menu. */
  .site-categories,
  .site-header__actions .header-register,
  .site-header__actions .header-install,
  .profile-menu__name,
  .profile-menu__chevron { display: none !important; }
  /* Phones: the profile button is just the avatar. */
  .profile-menu__toggle { height: 40px; width: 40px; padding: 0; justify-content: center; }
  .profile-menu__dropdown { position: fixed; top: calc(var(--site-header-h, 60px) + 6px); right: 12px; width: min(280px, calc(100vw - 24px)); }

  .site-header__actions { gap: var(--space-2); }
  .site-header__actions .btn--sm { height: 40px; padding: 0 14px; font-size: 13.5px; white-space: nowrap; }
  .site-header__burger, .cart-link { width: 40px; height: 40px; }

  .nav-drawer { display: flex; flex-direction: column; }
  .nav-drawer__heading {
    margin: var(--space-3) 0 0; padding: 0 4px 6px;
    font-size: 11.5px; font-weight: 700; letter-spacing: 0.12em; text-transform: uppercase;
    color: var(--color-text-muted);
  }
  .nav-drawer__actions { display: grid; gap: var(--space-2); margin-top: var(--space-4); }
  .nav-drawer__actions .btn { min-height: 48px; justify-content: center; gap: 8px; }
  .nav-drawer__account {
    display: flex; align-items: center; gap: 10px;
    min-height: 48px; padding: 0 4px; margin-bottom: var(--space-1);
    font-weight: 600; color: var(--color-text);
  }
}

@media (max-width: 360px) {
  .brand__mark { width: 34px; height: 34px; }
  .site-header__actions .btn--sm { padding: 0 10px; }
}

/* ---- Install dialog, account app panel ---------------------------------- */
.install-dialog { max-width: 460px; }
.install-dialog__head { display: flex; align-items: center; gap: var(--space-3); }
.install-dialog__head img { border-radius: 12px; flex: 0 0 auto; }
.install-dialog__head h2 { font-size: 20px; }
.install-steps { margin: 0; padding-left: 1.25em; display: grid; gap: var(--space-2); line-height: 1.5; }
.install-steps svg { vertical-align: -3px; }
.account-app { display: flex; gap: var(--space-3); align-items: center; }
.account-app img { flex: 0 0 auto; border-radius: 12px; }

/* ---- Modals on small screens -------------------------------------------- */
@media (max-width: 620px) {
  .modal-backdrop { padding: var(--space-3); align-items: end; }
  .modal {
    padding: var(--space-4);
    max-height: 90vh; max-height: 90dvh;
    padding-bottom: calc(var(--space-4) + env(safe-area-inset-bottom));
  }
}

/* ---- Stats and key/value tables never overflow -------------------------- */
.stat-row { grid-template-columns: repeat(auto-fit, minmax(min(150px, 100%), 1fr)); }
.stat { min-width: 0; }
.stat__value { font-size: clamp(19px, 4.6vw, 25px); overflow-wrap: anywhere; }

table.data--kv th { width: 38%; white-space: normal; }
table.data--kv td { overflow-wrap: anywhere; }

/* ---- Catalogue: grid can't exceed narrow screens; long names wrap ------- */
.gc-grid { grid-template-columns: repeat(auto-fill, minmax(min(210px, 100%), 1fr)); }
.gc-card__name { overflow-wrap: anywhere; }
@media (max-width: 480px) {
  .gc-grid { gap: var(--space-4); }
}

/* ---- Buyer order history: table on wide screens, cards on phones ------- */
@media (max-width: 640px) {
  .table-wrap--cards { border: none; border-radius: 0; overflow: visible; }
  table.data--cards, table.data--cards tbody { display: block; background: transparent; }
  table.data--cards thead {
    position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0);
  }
  table.data--cards tr {
    display: grid;
    grid-template-columns: minmax(0, 1fr) auto;
    gap: 10px var(--space-3);
    padding: var(--space-4);
    margin-bottom: var(--space-3);
    background: var(--color-surface);
    border: 1px solid var(--color-border);
    border-radius: var(--radius-md);
  }
  table.data--cards tbody tr:hover { background: var(--color-surface); }
  table.data--cards td { display: block; padding: 0; border: none; text-align: left; min-width: 0; }
  table.data--cards td[data-label]::before {
    content: attr(data-label);
    display: block; margin-bottom: 2px;
    font-size: 11px; font-weight: 700; letter-spacing: 0.06em; text-transform: uppercase;
    color: var(--color-text-muted);
  }
  table.data--cards .order-row__ref { font-weight: 700; font-size: 14px; overflow-wrap: anywhere; }
  table.data--cards .order-row__ref::before { display: none; }
  table.data--cards .order-row__status { grid-column: 2; grid-row: 1; justify-self: end; }
  table.data--cards .order-row__status::before { display: none; }
  table.data--cards .order-row__product { grid-column: 1 / -1; overflow-wrap: anywhere; }
  table.data--cards .order-row__total { text-align: right; font-weight: 700; }
  table.data--cards .order-row__action { grid-column: 1 / -1; }
  table.data--cards .order-row__action .btn { width: 100%; min-height: 44px; justify-content: center; }
  table.data--cards td[colspan] { grid-column: 1 / -1; }
}
