/* =========================================================
   Books & Cloud — Mobile-First Design (MFD)
   v10 — full mobile rebuild for premium app feel
   Verify load: visit /assets/css/responsive.css and check "v10"
   =========================================================
   Strategy:
   - This file LAYERS on top of style.css (which targets desktop).
   - We keep the desktop styles for ≥1080px and apply progressive
     overrides for phones / tablets below.
   - !important is used surgically on layout properties (grid,
     flex, position) where desktop style.css would otherwise win.
   - All interactive elements meet 44×44px touch target minimum.
   - Hover-only effects are paired with :focus-visible + :active.
   ========================================================= */

/* ---------------------------------------------------------
   GLOBAL — overflow & touch hardening
   --------------------------------------------------------- */
html, body {
  max-width: 100vw;
  overflow-x: hidden;
}
img, svg, video, canvas { max-width: 100%; height: auto; }

/* Smooth horizontal scrolling for carousel rails */
.h-scroll {
  display: flex;
  gap: 16px;
  overflow-x: auto;
  scroll-snap-type: x mandatory;
  -webkit-overflow-scrolling: touch;
  scrollbar-width: none;
  scroll-padding-inline: 20px;
  padding: 4px 20px 24px;
  margin: 0 -20px;
}
.h-scroll::-webkit-scrollbar { display: none; }
.h-scroll > * { scroll-snap-align: start; flex-shrink: 0; }

/* ---------------------------------------------------------
   Wide desktop (≥1600px) — luxurious spacing
   --------------------------------------------------------- */
@media (min-width: 1600px) {
  :root { --container: 1480px; }
  .hero { padding: 120px 0 160px; }
  .hero-inner { gap: 140px; }
}

/* ---------------------------------------------------------
   Tablet (≤1080px) — single-column hero, hide desktop nav
   --------------------------------------------------------- */
@media (max-width: 1080px) {
  .header-inner {
    grid-template-columns: auto 1fr auto auto !important;
    gap: 16px;
    padding: 18px 28px;
  }
  .main-nav { display: none; }
  .search-form { min-width: 180px; }
  .container { padding: 0 28px; }

  .hero { padding: 60px 0 100px; }
  .hero-inner {
    grid-template-columns: 1fr !important;
    gap: 80px;
    padding: 0 28px;
  }
  .artifact-display { padding: 20px 0; }

  .footer-inner { grid-template-columns: 1fr 1fr; padding: 70px 28px 40px; gap: 40px; }
  .book-detail { grid-template-columns: 1fr; gap: 60px; padding: 50px 0; }
  .book-detail .cover { max-width: 340px; margin: 0 auto; }

  .section { padding: 80px 0; }
  .manifesto { padding: 90px 0; }
}

/* =========================================================
   Mobile phone (≤900px) — the main mobile design
   ========================================================= */
@media (max-width: 900px) {

  /* ---- Type & spacing baselines ---- */
  body {
    font-size: 16px;
    line-height: 1.65;
    /* Account for fixed bottom nav */
    padding-bottom: 64px;
  }
  h1 { font-size: clamp(2.2rem, 9vw, 3rem); line-height: 1.05; letter-spacing: -0.02em; }
  h2 { font-size: clamp(1.7rem, 6.5vw, 2.3rem); line-height: 1.1; }
  h3 { font-size: 1.25rem; line-height: 1.25; }
  .container { padding: 0 20px !important; }
  .section { padding: 56px 0 !important; }

  /* Disable the "fixed paper grain" — improves scroll perf on phones */
  body { background-attachment: scroll; }

  /* ---- Lock body scroll when drawer is open ---- */
  body.nav-open {
    overflow: hidden !important;
    position: fixed;
    width: 100%;
    inset: 0;
  }
  body.nav-open::before {
    content: '';
    position: fixed; inset: 0;
    background: rgba(28, 27, 24, 0.55);
    -webkit-backdrop-filter: blur(4px);
    backdrop-filter: blur(4px);
    z-index: 89;
    animation: bnc-fade-in 0.3s ease forwards;
  }
  @keyframes bnc-fade-in { from { opacity: 0; } to { opacity: 1; } }

  /* ---- Header — compact & sticky ---- */
  .header-inner {
    grid-template-columns: auto 1fr auto !important;
    gap: 8px !important;
    padding: 12px 16px !important;
    align-items: center;
  }
  .logo { gap: 8px; }
  .logo svg { width: 30px; height: 30px; }
  .logo-text { font-size: 1.1rem; }
  .logo-cloud { font-size: 0.6em; }

  /* Hide top-of-header search form on phones; we move search into the drawer */
  .search-form { display: none !important; }

  /* Hide the desktop "Sign in" pill — it's in the drawer now */
  .header-actions .btn-ghost { display: none !important; }

  .header-actions { gap: 2px; }

  /* All header buttons → 44px touch target */
  .lang-btn, .currency-btn {
    min-width: 44px; min-height: 44px;
    padding: 6px 9px; gap: 4px;
    font-size: 0.7rem;
    justify-content: center;
  }
  .lang-btn svg, .currency-btn svg { width: 18px; height: 18px; }

  .cart-btn {
    min-width: 44px; min-height: 44px;
    padding: 8px;
    display: inline-flex; align-items: center; justify-content: center;
  }
  .cart-btn svg { width: 22px; height: 22px; }
  .cart-count {
    top: 2px; right: 2px;
    min-width: 18px; height: 18px; line-height: 18px;
    font-size: 0.62rem;
  }

  /* User avatar smaller */
  .avatar-btn { width: 40px; height: 40px; }

  /* ---- Hamburger button — animates to X via body.nav-open ---- */
  .mobile-menu-btn {
    display: inline-flex !important;
    align-items: center; justify-content: center;
    position: relative;
    width: 44px; height: 44px;
    padding: 0;
    z-index: 101;
    border: none; background: none; cursor: pointer;
    -webkit-tap-highlight-color: transparent;
  }
  .mobile-menu-btn span {
    position: absolute; left: 10px;
    width: 24px; height: 1.6px;
    background: var(--ink);
    margin: 0;
    transform-origin: center;
    transition: transform 0.35s cubic-bezier(0.16, 1, 0.3, 1),
                opacity 0.2s ease,
                top 0.35s cubic-bezier(0.16, 1, 0.3, 1);
  }
  .mobile-menu-btn span:nth-child(1) { top: 15px; }
  .mobile-menu-btn span:nth-child(2) { top: 21px; }
  .mobile-menu-btn span:nth-child(3) { top: 27px; }
  body.nav-open .mobile-menu-btn span:nth-child(1) { top: 21px; transform: rotate(45deg); }
  body.nav-open .mobile-menu-btn span:nth-child(2) { opacity: 0; }
  body.nav-open .mobile-menu-btn span:nth-child(3) { top: 21px; transform: rotate(-45deg); }

  /* ---- Side drawer — slides from the right ---- */
  .main-nav {
    display: flex !important;
    flex-direction: column !important;
    gap: 0 !important;
    position: fixed !important;
    top: 0 !important; bottom: 0 !important; right: 0 !important; left: auto !important;
    width: min(86vw, 360px) !important;
    background: var(--cream) !important;
    padding: 80px 0 40px !important;
    margin: 0 !important;
    box-shadow: -10px 0 40px rgba(28, 27, 24, 0.18) !important;
    transform: translateX(100%) !important;
    transition: transform 0.4s cubic-bezier(0.16, 1, 0.3, 1) !important;
    z-index: 100 !important;
    overflow-y: auto !important;
    -webkit-overflow-scrolling: touch;
    justify-self: auto !important;
  }
  .main-nav.open { transform: translateX(0) !important; }

  .main-nav a {
    padding: 16px 28px !important;
    border-bottom: 1px solid var(--hairline-2);
    font-family: var(--sans);
    font-size: 0.78rem;
    font-weight: 500;
    color: var(--ink);
    letter-spacing: 0.22em;
    text-transform: uppercase;
    min-height: 52px;
    display: flex; align-items: center;
    transition: background 0.2s, color 0.2s, padding-inline-start 0.25s;
  }
  .main-nav a:first-of-type { border-top: 1px solid var(--hairline-2); }
  .main-nav a:hover,
  .main-nav a:active,
  .main-nav a:focus-visible {
    background: var(--cream-2);
    color: var(--gold-deep);
    padding-inline-start: 36px !important;
  }
  .main-nav a::after { display: none; }

  /* In-drawer search (rendered into the drawer in the markup) */
  .drawer-search {
    display: block !important;
    margin: 0 28px 20px;
    border: 1px solid var(--hairline);
    border-radius: 0;
    background: transparent;
    overflow: hidden;
  }
  .drawer-search input {
    width: 100%;
    border: none;
    background: transparent;
    padding: 14px 16px;
    font: 400 16px var(--sans);
    color: var(--ink);
    outline: none;
    letter-spacing: .04em;
  }
  .drawer-search input::placeholder { color: var(--brown-2); }
  .drawer-search:focus-within { border-color: var(--gold); }

  /* In-drawer sign-in CTA (mirrors the desktop btn-ghost) */
  .drawer-cta {
    display: block !important;
    margin: 8px 28px 0;
    text-align: center;
    padding: 14px;
    border: 1px solid var(--green);
    color: var(--green) !important;
    font: 500 0.74rem/1 var(--sans);
    letter-spacing: 0.22em;
    text-transform: uppercase;
    min-height: 48px;
    line-height: 18px;
  }

  /* RTL — drawer slides from LEFT */
  html[dir="rtl"] .main-nav {
    right: auto !important; left: 0 !important;
    box-shadow: 10px 0 40px rgba(28, 27, 24, 0.18) !important;
    transform: translateX(-100%) !important;
  }
  html[dir="rtl"] .main-nav.open { transform: translateX(0) !important; }

  /* ---- Announcement banner — gentler on mobile ---- */
  .announcement-banner-inner { padding: 12px 18px; gap: 12px; }
  .announcement-banner .label { padding-right: 12px; font-size: 0.6rem; }
  .announcement-banner a { font-size: 0.86rem; }
  .announcement-banner .scroll-inner { gap: 40px; padding-right: 40px; animation-duration: 35s; }
  .announcement-banner .see-all { display: none; }

  /* ---- HERO ---- */
  .hero { padding: 28px 0 56px !important; overflow: hidden; }
  .hero-inner {
    grid-template-columns: 1fr !important;
    padding: 0 20px !important;
    gap: 36px !important;
  }
  .hero-text { max-width: 100%; }
  .hero-eyebrow {
    margin-bottom: 18px !important;
    font-size: 0.6rem !important;
    letter-spacing: 0.3em;
  }
  .hero-eyebrow::before { width: 18px; }
  .hero h1 {
    font-size: clamp(2.4rem, 11vw, 3.4rem) !important;
    line-height: 1.04 !important;
    margin-bottom: 20px;
  }
  .hero p.lead {
    font-size: 1.02rem !important;
    line-height: 1.6;
    max-width: none !important;
    margin-bottom: 24px !important;
  }
  .hero-cta {
    gap: 12px !important;
    flex-direction: column !important;
    align-items: stretch !important;
  }
  .hero-cta .btn-gold {
    width: 100%;
    text-align: center;
    padding: 16px 22px !important;
    font-size: 0.78rem;
    min-height: 52px;
    letter-spacing: 0.2em;
  }
  .hero-cta .btn-link {
    text-align: center;
    padding: 12px;
    font-size: 0.78rem;
    min-height: 44px;
    align-items: center; justify-content: center;
  }

  /* Artifact carousel — keep it elegant but compact */
  .artifact-display { padding: 0 !important; max-width: 100%; }
  .artifact-frame { padding: 10px; max-width: 320px; }
  .artifact-label {
    margin-top: 22px;
    padding-left: 14px;
    max-width: 100%;
  }
  .artifact-label h3 { font-size: 1.2rem; }
  .artifact-label .attribution { font-size: 0.9rem; }
  .artifact-nav {
    gap: 12px;
    padding-left: 14px;
    margin-top: 24px;
  }
  .artifact-nav button {
    width: 44px; height: 44px;
  }

  /* ---- Section heads ---- */
  .section-head {
    grid-template-columns: 1fr !important;
    gap: 8px !important;
    margin-bottom: 28px !important;
    padding-bottom: 14px;
  }
  .section-head h2 {
    font-size: clamp(1.7rem, 6.5vw, 2.2rem);
    margin-top: 4px !important;
  }
  .section-head .subtitle { font-size: 0.6rem; }
  .section-head a.view-all {
    padding-bottom: 0;
    justify-self: start;
    font-size: 0.68rem;
    margin-top: 4px;
    min-height: 32px;
    display: inline-flex; align-items: center;
  }

  /* ---- Manifesto ---- */
  .manifesto { padding: 56px 0 !important; }
  .manifesto blockquote { font-size: 1.2rem !important; line-height: 1.5; }
  .manifesto-rule { margin-bottom: 28px; }
  .manifesto-inner { padding: 0 24px !important; }

  /* ---- Vibe wing — chips become a horizontal scroll rail ---- */
  .vibe-wing { padding: 56px 0 !important; }
  .vibe-wing::before { inset: 16px; }
  .vibe-wing .lead { font-size: 1rem; margin-bottom: 24px; }

  .vibe-chips {
    display: flex !important;
    flex-wrap: nowrap !important;
    overflow-x: auto;
    scroll-snap-type: x proximity;
    -webkit-overflow-scrolling: touch;
    scrollbar-width: none;
    gap: 10px !important;
    margin: 0 -20px 24px !important;
    padding: 4px 20px 16px !important;
  }
  .vibe-chips::-webkit-scrollbar { display: none; }
  .vibe-chip {
    scroll-snap-align: start;
    flex-shrink: 0;
    padding: 12px 18px !important;
    font-size: 0.72rem;
    min-height: 44px;
    align-items: center;
  }

  /* ---- Book grid (default fallback before rail) ---- */
  .book-grid {
    grid-template-columns: repeat(2, 1fr) !important;
    gap: 32px 14px !important;
  }
  .book-meta { padding-top: 14px; margin-top: 14px; }
  .book-meta .catalog-id { font-size: 0.6rem; margin-bottom: 6px; }
  .book-meta .t { font-size: 0.95rem; line-height: 1.25; margin-bottom: 2px; }
  .book-meta .a { font-size: 0.82rem; margin-bottom: 8px; }
  .book-meta .p { font-size: 0.72rem; }

  /* Book card — disable flip on mobile (you can't hover on touch).
     Tap goes straight to detail. Keeps the gallery look without
     the broken interaction. */
  .book-card {
    -webkit-tap-highlight-color: transparent;
    cursor: pointer;
  }
  .book-card .book-card-inner {
    transform: none !important;
    transition: transform 0.4s ease;
  }
  .book-card:active .book-card-inner { transform: scale(0.98); }
  .book-card .book-back { display: none !important; }

  /* Make the entire front face a tappable link (the <a> is already
     there in the markup, just stretch it to fill) */
  .book-card .book-front a {
    position: absolute; inset: 0;
    display: block;
  }

  /* ---- Horizontal scroll rails for "Featured" and "Newest" ----
     Pages can opt-in by adding the .book-rail class to the .book-grid */
  .book-grid.book-rail {
    display: flex !important;
    grid-template-columns: none !important;
    overflow-x: auto;
    scroll-snap-type: x mandatory;
    -webkit-overflow-scrolling: touch;
    scrollbar-width: none;
    gap: 16px !important;
    margin: 0 -20px !important;
    padding: 4px 20px 24px !important;
  }
  .book-grid.book-rail::-webkit-scrollbar { display: none; }
  .book-grid.book-rail .book-card {
    flex-shrink: 0;
    width: 58vw;
    max-width: 220px;
    scroll-snap-align: start;
  }

  /* ---- Buttons — all touch-friendly ---- */
  .btn-gold, .btn-ghost, .btn-outline {
    padding: 14px 22px;
    font-size: 0.76rem;
    min-height: 48px;
    letter-spacing: 0.2em;
  }
  .btn-block { width: 100%; }
  /* On mobile, the gallery reflex of "hover swap" is replaced with a
     tighter tap feedback */
  .btn-gold:active, .btn-ghost:active, .btn-outline:active {
    transform: translateY(1px);
  }

  /* ---- Forms ---- */
  .form-page { padding: 36px 24px !important; margin: 28px 16px !important; }
  .form-page::before { inset: 8px; }
  .form-page h1 { font-size: clamp(2rem, 8vw, 2.6rem) !important; }
  .form-field input,
  .form-field textarea,
  .form-field select {
    font-size: 16px !important; /* prevent iOS auto-zoom on focus */
    padding: 14px 16px !important;
    min-height: 48px;
  }

  /* ---- Profile ---- */
  .profile-header {
    grid-template-columns: 1fr !important;
    text-align: center;
    gap: 22px !important;
  }
  .profile-avatar { margin: 0 auto; width: 130px; height: 130px; }
  .stats-grid { grid-template-columns: repeat(2, 1fr) !important; }
  .stats-grid .stat-card { padding: 22px 18px; }
  .stats-grid .stat-card .value { font-size: 1.8rem; }

  /* Bookshelf tabs scroll horizontally */
  .shelf-tabs {
    overflow-x: auto;
    flex-wrap: nowrap;
    white-space: nowrap;
    scrollbar-width: none;
    -ms-overflow-style: none;
    margin: 28px -20px;
    padding: 0 20px;
  }
  .shelf-tabs::-webkit-scrollbar { display: none; }
  .shelf-tabs a {
    padding: 14px 0;
    margin-right: 22px;
    font-size: 0.7rem;
    flex-shrink: 0;
    min-height: 44px;
    display: inline-flex; align-items: center;
  }

  /* ---- Book detail ---- */
  .book-detail h1 { font-size: 1.85rem !important; margin-bottom: 12px; }
  .book-detail .author { font-size: 1.05rem; margin-bottom: 18px; }
  .book-detail .price-lg {
    font-size: 1.8rem;
    margin: 22px 0;
    padding-top: 22px;
  }
  .book-detail .description { font-size: 1rem; margin: 18px 0; }
  .book-detail .actions {
    flex-direction: column;
    gap: 10px;
    margin: 24px 0;
  }
  .book-detail .actions .btn-gold,
  .book-detail .actions .btn-ghost,
  .book-detail .actions .btn-outline { width: 100%; }

  /* ---- Footer ---- */
  .footer-inner {
    grid-template-columns: 1fr 1fr !important;
    gap: 32px 24px !important;
    padding: 48px 20px 28px !important;
  }
  .footer-brand { grid-column: span 2; text-align: center; }
  .footer-logo { font-size: 1.5rem; }
  .footer-bottom { padding: 22px 20px; font-size: 0.66rem; text-align: center; }

  /* ---- Cart drawer — full width on mobile ---- */
  .cart-drawer { width: 100% !important; max-width: 100% !important; }
  .cart-drawer-header,
  .cart-drawer-footer,
  .cart-drawer-body { padding-left: 22px; padding-right: 22px; }
  .cart-drawer-header { padding-top: 22px; padding-bottom: 16px; }
  .cart-drawer-header h2 { font-size: 1.4rem; }
  .cart-drawer-header button { min-width: 44px; min-height: 44px; font-size: 1.8rem; }

  /* ---- Lang/currency dropdowns — keep within viewport ---- */
  .lang-dropdown, .currency-dropdown {
    min-width: 180px;
    right: 0; left: auto;
  }
  html[dir="rtl"] .lang-dropdown,
  html[dir="rtl"] .currency-dropdown { right: auto; left: 0; }

  /* ---- v11: switchers move into the drawer on phones ----
     Reason: on a ~390px phone, the row [logo USD EN cart avatar ☰]
     overflows the viewport, and `overflow-x: hidden` on body silently
     clips the rightmost item — which is the hamburger when logged in.
     Hiding the in-header switchers fixes both the overflow AND the
     "hamburger disappears when logged in" symptom. The dropdowns above
     are still styled because the rules are cheap and harmless; they
     just don't render now that their parent is display:none. */
  .header-actions > .currency-menu,
  .header-actions > .lang-menu { display: none !important; }

  /* Mirror copies inside the drawer */
  .main-nav .drawer-prefs {
    display: block !important;
    margin: 18px 0 0;
    padding: 18px 0 0;
    border-top: 1px solid var(--hairline-2);
  }
  .drawer-pref-group { padding: 6px 0 14px; }
  .drawer-pref-group + .drawer-pref-group {
    margin-top: 6px;
    border-top: 1px solid var(--hairline-2);
    padding-top: 14px;
  }
  .drawer-pref-label {
    display: block;
    padding: 0 28px 8px;
    font: 500 0.62rem/1 var(--sans);
    letter-spacing: 0.22em;
    text-transform: uppercase;
    color: var(--brown-2);
  }
  /* Override the generic .main-nav a styles for in-drawer pref rows.
     These are settings rows, not nav links — different visual rhythm. */
  .main-nav .drawer-pref-opt {
    display: flex !important;
    align-items: center;
    justify-content: space-between;
    padding: 12px 28px !important;
    min-height: 44px;
    border: none !important;
    font-size: 0.82rem !important;
    letter-spacing: 0.04em !important;
    text-transform: none !important;
    color: var(--ink) !important;
  }
  .main-nav .drawer-pref-opt:first-of-type { border-top: none !important; }
  .main-nav .drawer-pref-opt.is-active {
    color: var(--gold-deep) !important;
    font-weight: 600;
  }
  .main-nav .drawer-pref-opt:hover,
  .main-nav .drawer-pref-opt:active,
  .main-nav .drawer-pref-opt:focus-visible {
    background: var(--cream-2);
    padding-inline-start: 36px !important;
  }
  .drawer-pref-check { color: var(--gold-deep); font-weight: 700; }
}

/* ---------------------------------------------------------
   Very small phones (≤380px) — graceful degrade
   --------------------------------------------------------- */
@media (max-width: 380px) {
  .header-inner { padding: 10px 12px !important; gap: 2px !important; }
  .logo-text { font-size: 0.95rem; }
  .logo-cloud { display: none; }            /* "CLOUD" text drops to save space */
  .lang-btn-label, .currency-btn-label { display: none; }
  .lang-btn, .currency-btn { padding: 6px; min-width: 40px; }

  .book-grid { grid-template-columns: 1fr !important; }
  .book-grid.book-rail .book-card { width: 70vw; }

  .hero h1 { font-size: 2rem !important; }
  .footer-inner { grid-template-columns: 1fr !important; }
  .footer-brand { grid-column: span 1; }
}

/* ---------------------------------------------------------
   Reduced motion — respect user setting
   --------------------------------------------------------- */
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.01ms !important;
  }
  .book-card:hover .book-card-inner { transform: none !important; }
  .main-nav { transition: none !important; }
  .announcement-banner .scroll-inner { animation: none !important; }
}

/* ---------------------------------------------------------
   Hover/focus parity — make :hover effects also fire on
   :focus-visible (keyboard users) and :active (taps).
   These are *additive* — they don't replace existing hover.
   --------------------------------------------------------- */
@media (hover: none) {
  /* On true touch devices, kill the flip-on-hover entirely so
     it can't fire from a sticky :hover after a tap */
  .book-card:hover .book-card-inner { transform: none !important; }
  .tilt-on-hover:hover { transform: none !important; }
}

/* ---------------------------------------------------------
   Safe-area padding — so iOS notch / Android nav bar don't
   eat the bottom-nav buttons or the cart-drawer footer
   --------------------------------------------------------- */
@supports (padding: max(0px)) {
  @media (max-width: 900px) {
    body { padding-bottom: max(64px, calc(64px + env(safe-area-inset-bottom))); }
    .bottom-nav { padding-bottom: env(safe-area-inset-bottom); }
    .cart-drawer-footer { padding-bottom: max(24px, env(safe-area-inset-bottom)); }
  }
}
