/* layout.css — app shell: sidebar + topbar + content */

/* min-height:100svh (small viewport, capped to visible — never the LARGE iOS viewport
   that reserves phantom blank scroll) and NO 100vh fallback (that fallback IS the large
   viewport). align-content:start pins the grid tracks to the top so the grid never
   stretches dead space; any unused min-height sits at the bottom, inside the visible
   screen, and can't be scrolled into. */
/* 100dvh (not svh): fill the CURRENT viewport so the body's gray never shows through the ~toolbar-height
   gap on mobile as the URL bar expands/collapses (the "gray bar at the bottom on load" bug). */
.app{ display:grid; grid-template-columns:var(--sidebar-w) 1fr; min-height:100svh; min-height:100dvh; align-content:start; }

/* ---------- Sidebar ---------- */
.sidebar{
  /* height:100% (of its grid row), NOT a viewport unit — a viewport-height sidebar forces
     the whole shell row to a full screen even when the page is short, which is the extra
     blank scroll. 100% fills the content-sized row and adds no reserved height. */
  position:sticky; top:0; height:100%; overflow-y:auto;
  /* a distinct, slightly-elevated plane — not the same flat white as the cards */
  background:linear-gradient(180deg,#ffffff,#fbfcfe);
  border-right:1px solid var(--line); box-shadow:1px 0 0 rgba(13,27,42,.02);
  display:flex; flex-direction:column; padding:var(--s4) var(--s3);
}
.sidebar__brand{ display:flex; align-items:center; gap:var(--s3); padding:var(--s2) var(--s3) var(--s5); }
.sidebar__brand img{ height:42px; width:42px; border-radius:var(--r-sm); object-fit:contain; box-shadow:var(--shadow-sm); }
.sidebar__brand b{ font-weight:900; font-size:var(--fs-lg); letter-spacing:-.02em; line-height:1.15; }
.sidebar__brand .sub{ display:block; font-size:10px; font-weight:700; color:var(--green-600); letter-spacing:.14em; text-transform:uppercase; }

.nav__group{ margin-bottom:var(--s4); }
.nav__label{ font-size:10px; font-weight:800; letter-spacing:.12em; text-transform:uppercase; color:var(--ink-400); padding:0 var(--s3); margin-bottom:var(--s2); }
.nav__item{
  display:flex; align-items:center; gap:var(--s3); padding:10px var(--s3);
  border-radius:var(--r-md); color:var(--ink-700); font-weight:600; font-size:var(--fs-md);
  transition:background .15s, color .15s;
}
.nav__item:hover{ background:var(--surface-2); color:var(--ink-900); }
.nav__item.is-active{ background:var(--blue-50); color:var(--blue-700); box-shadow:inset 3px 0 0 var(--blue-500); }
.nav__item .ic{ width:20px; text-align:center; font-size:17px; }
.nav__item .count{ margin-left:auto; font-size:var(--fs-xs); color:var(--ink-400); font-weight:700; }
/* "Start here" on-ramp — a solid blue CTA in the sidebar (matches the primary button elsewhere).
   A little gap above it so its blue fill doesn't touch the (also-highlighted) Home item. */
.nav__item--start{ background:var(--blue-500); color:#fff; box-shadow:var(--shadow-sm); margin-top:6px; }
.nav__item--start:hover{ background:var(--blue-600); color:#fff; }
.nav__item--start .ic{ color:#fff; }
.nav__item--start.is-active{ background:var(--blue-600); color:#fff; box-shadow:var(--shadow-sm); }
.sidebar__foot{ margin-top:auto; padding:var(--s3); }

/* ---------- Topbar ---------- */
.topbar{
  position:sticky; top:0; z-index:20; height:var(--topbar-h); flex-shrink:0;
  display:flex; align-items:center; gap:var(--s4);
  padding:4px var(--s6); background:rgba(255,255,255,.86); backdrop-filter:blur(10px);
  border-bottom:1px solid var(--line);
}
.search{ flex:1; max-width:520px; position:relative; }
.search input{
  width:100%; height:42px; padding:0 var(--s4) 0 42px; border-radius:var(--r-pill);
  border:1px solid var(--line-strong); background:var(--surface-2); font-size:var(--fs-md); color:var(--ink-900);
}
.search input:focus{ background:var(--surface); border-color:var(--blue-300); box-shadow:none; }
.search .ic{ position:absolute; left:14px; top:50%; transform:translateY(-50%); color:var(--ink-400); }
.topbar__spacer{ flex:1; }
.topbar__cta .ic{ font-size:14px; }
/* Phones: the "Start here" CTA crowds out the search. Drop it AND the flex spacer that competes
   with the search for space, and lift the search max-width so it fills the whole bar (menu → you).
   Scoped under .topbar for enough specificity to beat the .btn display rule (loaded later). */
@media (max-width:640px){
  .topbar .topbar__cta{ display:none; }
  .topbar .topbar__spacer{ display:none; }
  .topbar .search{ max-width:none; }
}

/* ---- Profile menu (top-right) ---- */
.profile{ position:relative; flex:none; }
/* RELEASE-GATE: header search removal — when the search is gone (owner view), keep the profile
   pinned to the right edge. Scoped to .topbar--nosearch so the searched (non-owner) view is
   byte-identical (a global margin-left:auto would collapse the search's flex-grow). */
.topbar--nosearch .profile{ margin-left:auto; }
.profile__btn{ display:inline-flex; align-items:center; justify-content:center; padding:0; border:0; background:none; cursor:pointer; border-radius:50%; }
.profile__btn:focus-visible{ outline:none; box-shadow:none; }
.profile__avatar{ width:36px; height:36px; border-radius:50%; display:inline-flex; align-items:center; justify-content:center;
  font-weight:800; font-size:15px; color:#fff; background:linear-gradient(135deg,var(--blue-500,#1f70df),#4a90ff); box-shadow:0 3px 10px rgba(13,27,42,.22); }
.profile__btn:hover .profile__avatar{ filter:brightness(1.06); }
.profile__avatar--lg{ width:44px; height:44px; font-size:18px; flex:none; }
.profile__menu{ position:absolute; right:0; top:calc(100% + 10px); z-index:60; width:264px;
  background:var(--surface); border:1px solid var(--line); border-radius:16px; box-shadow:0 24px 60px -22px rgba(13,27,42,.5); overflow:hidden; }
.profile__menu[hidden]{ display:none; }
.profile__head{ display:flex; gap:12px; align-items:center; padding:16px; border-bottom:1px solid var(--line); }
.profile__id{ min-width:0; }
.profile__email{ font-weight:700; font-size:14px; color:var(--ink-900); overflow:hidden; text-overflow:ellipsis; white-space:nowrap; }
.profile__plan{ font-size:12px; font-weight:700; margin-top:2px; color:var(--ink-400); }
.profile__plan.is-purchased{ color:#16834e; }
.profile__plan.is-trial{ color:var(--blue-500,#1f70df); }
.profile__items{ padding:6px; }
.profile__item{ display:flex; align-items:center; gap:10px; width:100%; box-sizing:border-box; padding:10px 12px; border:0; background:none;
  font:inherit; font-size:14px; font-weight:600; color:var(--ink-900); text-decoration:none; border-radius:10px; cursor:pointer; text-align:left; }
.profile__item:hover{ background:var(--surface-2); }
.profile__item .ico{ width:18px; height:18px; color:var(--ink-400); }
.profile__item--danger{ color:#c0392b; }
.profile__item--danger:hover{ background:#fdecea; }
.profile__item--danger .ico{ color:#c0392b; }

/* Appearance (dark-mode) segmented control — profile menu + reused in Account */
.profile__appearance{ padding:10px 12px 12px; border-top:1px solid var(--line); }
.profile__aplbl{ font-size:11px; font-weight:800; letter-spacing:.08em; text-transform:uppercase; color:var(--ink-400); margin-bottom:8px; }
.profile__seg{ display:flex; gap:4px; background:var(--surface-3); border-radius:var(--r-pill); padding:3px; }
.profile__seg-btn{ flex:1; border:0; background:transparent; color:var(--ink-500); font:inherit; font-size:13px; font-weight:700;
  padding:7px 8px; border-radius:var(--r-pill); cursor:pointer; transition:background .15s, color .15s; }
.profile__seg-btn:hover{ color:var(--ink-900); }
.profile__seg-btn.is-on{ background:var(--surface); color:var(--blue-600); box-shadow:var(--shadow-sm); }
.profile__seg-btn:focus-visible{ outline:none; box-shadow:none; }

/* ---- live search results dropdown ---- */
.search__results{ position:absolute; top:calc(100% + 8px); left:0; right:0; z-index:60;
  background:var(--surface); border:1px solid var(--line); border-radius:var(--r-md); box-shadow:var(--shadow-lg);
  padding:var(--s2); display:none; max-height:min(70vh,460px); overflow-y:auto; }
.search__results.show{ display:block; animation:fadeIn .14s ease both; }
.search__empty{ padding:var(--s4); color:var(--ink-500); font-size:var(--fs-sm); }
.sres{ display:flex; align-items:center; gap:var(--s3); padding:9px var(--s3); border-radius:var(--r-sm); color:var(--ink-900); }
.sres:hover{ background:var(--blue-50); }
.sres__ic{ width:30px; height:30px; flex:none; display:flex; align-items:center; justify-content:center;
  border-radius:var(--r-sm); background:var(--surface-3); color:var(--blue-600); }
.sres__ic .ico{ width:17px; height:17px; }
.sres__main{ min-width:0; flex:1; display:flex; flex-direction:column; line-height:1.25; }
.sres__pri{ font-weight:600; white-space:nowrap; overflow:hidden; text-overflow:ellipsis; }
.sres__pri.thai{ font-size:var(--fs-lg); }
.sres__sec{ font-size:var(--fs-xs); color:var(--ink-500); white-space:nowrap; overflow:hidden; text-overflow:ellipsis; }
.sres__type{ flex:none; font-size:10px; font-weight:800; letter-spacing:.06em; text-transform:uppercase;
  color:var(--ink-400); background:var(--surface-2); border:1px solid var(--line); padding:2px 8px; border-radius:var(--r-pill); }

/* ---- sidebar progress stat ---- */
.sidebar__stat{ display:flex; flex-direction:column; gap:2px; padding:var(--s3) var(--s4);
  border-radius:var(--r-md); background:var(--blue-50); border:1px solid var(--blue-100); color:var(--ink-900); }
.sidebar__stat:hover{ border-color:var(--blue-200); }
.ss-streak{ font-weight:700; display:inline-flex; align-items:center; gap:6px; }
.ss-streak .ico{ color:var(--amber-500); }
.ss-streak b{ font-family:var(--font-display); }
.ss-sub{ font-size:var(--fs-xs); display:inline-flex; align-items:center; gap:4px; }
.ss-sub .ico{ width:.9em; height:.9em; }

/* ---------- Main / content ---------- */
.main{ min-width:0; display:flex; flex-direction:column; }
/* the canvas: deepened plane + the whisper-faint two-corner color mesh (--canvas-tint).
   One rule, whole app — white cards pop off it and the page reads "designed", not blank. */
.content{ padding:var(--s5) var(--s6); flex:1 1 auto;
  background:var(--canvas-tint); background-color:var(--surface-2); }
.container{ max-width:var(--content-max); margin:0 auto; width:100%; }
.page{ animation:fadeIn .28s ease both; }

/* ===== iOS "gray bar" — kill the toolbar-collapse gesture on non-scroll surfaces (once and for all) =====
   The ~100px gray strip is iOS Safari retracting its toolbar and exposing body's --surface-2 when a
   touch-drag lands on a surface with NO in-page scroller to consume it — the topbar/search sit OUTSIDE
   .content, and the fit-pages (games / exercises / review / speaking) don't scroll at all. `touch-action`
   is GESTURE-ONLY: it changes no size or position, so the CONTAINER-IS-GOD game fits are untouched.
   → Block the pan gesture on the non-scroll surfaces; RE-ALLOW it on every real scroller. */
.topbar{ touch-action:none; }                              /* search header — never a scroller → don't leak the gesture */
.content, .sidebar{ touch-action:pan-y; }                  /* the genuine scrollers keep scrolling */
.rv-face, .rv-build__scroll, .qx__chat, .search__results{ touch-action:pan-y; } /* inner scrollers that live inside fit-pages / the topbar */
@media (min-width:1024px){
  /* fit-pages: .content is overflow:hidden here (nothing to scroll), so block the gesture on it —
     the inner scrollers above stay pan-y and keep working. */
  .content:has(.qx-page), .content:has(.rv-page), .content:has(.sp-page){ touch-action:none; }
}
/* reclaim canvas on very wide monitors */
@media (min-width:1750px){ :root{ --content-max:1600px; } }

/* footer */
.footer{ border-top:1px solid var(--line); padding:var(--s6); color:var(--ink-400); font-size:var(--fs-sm); }
.footer a{ color:var(--ink-500); }

/* ---------- Locked shell: independent scroll zones (>=1024 only) ----------
   The shell is pinned to the small-viewport height (100svh already accounts for the
   browser address bar, so no manual chrome trim) and CLIPS. The sidebar and the content
   each own their scroll, so a tall sidebar can scroll WITHOUT dragging the game zone, and
   the body never scrolls. Games then get a fixed, top-anchored zone that never scrolls;
   browse pages scroll inside .content. Small screens (<1024, off-canvas drawer) are left
   on the normal document-flow model on purpose — CONTAINER-IS-GOD scales game contents to
   fit there instead. */
@media (min-width:1024px){
  /* Lock the shell to the viewport with position:fixed — there is then NO document/body scroll for
     iOS to hijack. Root cause of the gray bar: the header is a non-scroll "dead zone", so a touch
     drag on it can't scroll .content and instead triggers the browser's ~100px "collapse the address
     bar" body scroll, dragging the whole shell DOWN and exposing the gray body at the bottom (only
     until you scroll back). A fixed inset:0 shell is glued to the visible viewport: nothing to drag,
     no gap, no phantom scroll, at any toolbar state. .sidebar and .content are the only scrollers;
     overscroll-behavior stops the rubber-band/collapse from ever starting. */
  html, body{ height:100%; overflow:hidden; overscroll-behavior:none; }
  .app{ position:fixed; inset:0; overflow:hidden; grid-template-rows:100%; min-height:0; }
  .topbar{ position:static; }                        /* no document scroll to stick to */
  .sidebar{ position:static; height:100%; min-height:0; overflow-y:auto; }
  .main{ height:100%; min-height:0; overflow:hidden; }
  .content{ flex:1 1 auto; min-height:0; overflow-y:auto; overscroll-behavior:contain; }
  /* a game round owns a fixed, non-scrolling zone; the qx fit engine keeps it un-clipped. */
  .content:has(.qx-page){ overflow:hidden; }
  /* Review isn't a .qx-page but wants the same fixed zone; the card flexes to fit (_w4_review.css). */
  .content:has(.rv-page){ overflow:hidden; }
  .content:has(.rv-page) > .container{ height:100%; min-height:0; display:flex; flex-direction:column; }
}

/* ---------- Desktop sidebar collapse (rail) ----------
   Persistent sidebar lives at >=1024 (tablet-landscape + laptop/desktop).
   The rail toggle (#menuBtn) hides it to reclaim canvas. */
@media (min-width:1024px){
  .app.rail{ grid-template-columns:1fr; }
  /* Hide the GRID CHILD (#navHost), not just the .sidebar inside it — otherwise the empty
     wrapper keeps occupying column 1 and .main is bumped into an implicit second row (a full
     viewport down = blank/"white" screen above the fold). */
  .app.rail #navHost{ display:none; }
  .app.rail .container{ max-width:1560px; }
}

/* ---------- Off-canvas: phones + tablet portrait + small laptops (<1024) ----------
   Below 1024 the sidebar slides in as a drawer (hamburger). This covers
   phones, iPad portrait (768/834), and small laptops up to 1023 — none of
   which comfortably hold a 248px fixed sidebar + content. */
.menu-btn{ display:inline-flex; }
@media (max-width:1023px){
  /* Block, NOT grid: the sidebar is an off-canvas drawer here, so a grid reserves an
     empty track + (with min-height:100svh) stretches dead space above/below .main —
     that was the ~10% header offset and the phantom page of blank scroll on iPad. */
  .app{ display:block; }
  .sidebar{
    position:fixed; left:0; top:0; z-index:50;
    width:min(280px, 86vw); max-width:280px;
    padding-left:calc(var(--s3) + var(--safe-l));
    padding-top:calc(var(--s4) + var(--safe-t));
    transform:translateX(-100%);
    transition:transform .25s ease; box-shadow:var(--shadow-lg);
  }
  body.nav-open .sidebar{ transform:none; }
  body.nav-open::after{ content:""; position:fixed; inset:0; background:rgba(13,27,42,.4); z-index:40; }
  .menu-btn{ display:inline-flex; }
  .content{ padding:var(--s4); }
  .topbar{ padding:0 max(var(--s4), var(--safe-l)) 0 max(var(--s4), var(--safe-l)); }
}


/* ============================================================
   DARK MODE OVERRIDES — element-level fixes a token flip can't reach
   (dark_mode_spec_2026-07 §3). Light output is unchanged; these only
   apply under OS-dark (no explicit choice) or data-theme="dark".
   ============================================================ */
@media (prefers-color-scheme: dark){
  :root:not([data-theme="light"]) .sidebar{ background:linear-gradient(180deg,#1b2732,#141d27); }
  :root:not([data-theme="light"]) .topbar{ background:rgba(14,22,32,.86); }
  :root:not([data-theme="light"]) .nav__item.is-active{ background:color-mix(in srgb,var(--blue-500) 20%,var(--surface)); }
  :root:not([data-theme="light"]) .profile__plan.is-purchased{ color:var(--green-600); }
  :root:not([data-theme="light"]) .profile__item--danger, :root:not([data-theme="light"]) .profile__item--danger .ico{ color:var(--rose-500); }
  :root:not([data-theme="light"]) .profile__item--danger:hover{ background:color-mix(in srgb, var(--rose-500) 16%, var(--mix-base)); }
}
:root[data-theme="dark"] .sidebar{ background:linear-gradient(180deg,#1b2732,#141d27); }
:root[data-theme="dark"] .topbar{ background:rgba(14,22,32,.86); }
:root[data-theme="dark"] .nav__item.is-active{ background:color-mix(in srgb,var(--blue-500) 20%,var(--surface)); }
:root[data-theme="dark"] .profile__plan.is-purchased{ color:var(--green-600); }
:root[data-theme="dark"] .profile__item--danger, :root[data-theme="dark"] .profile__item--danger .ico{ color:var(--rose-500); }
:root[data-theme="dark"] .profile__item--danger:hover{ background:color-mix(in srgb, var(--rose-500) 16%, var(--mix-base)); }
