/* =============================================================================
   Signed-in SPA app shell (ADR 0005)
   =============================================================================
   Layered on /static/site.css (design tokens, .game-grid, .game-card) and
   /tools/static/tools.css (tool panels). This file owns only the shell: a
   fixed top bar and a single content region that fills the viewport and scrolls
   internally only when a view overflows. No document scroll.
   ============================================================================= */

html,
body {
  height: 100%;
  margin: 0;
  overflow: hidden; /* the shell owns scrolling; the document never scrolls */
  /* Kill the rubber-band bounce and pull-to-refresh so an installed PWA feels
     like a native app, not a web page in a frame. */
  overscroll-behavior: none;
}

/* Off-screen but announced: the route-change live region. */
.app-live {
  position: absolute;
  width: 1px;
  height: 1px;
  margin: -1px;
  padding: 0;
  border: 0;
  overflow: hidden;
  clip: rect(0 0 0 0);
  clip-path: inset(50%);
  white-space: nowrap;
}

.app-shell {
  display: flex;
  flex-direction: column;
  height: 100dvh; /* dynamic viewport: survives mobile URL-bar collapse */
}

/* --- Fixed top bar ------------------------------------------------------- */
.app-bar {
  flex: 0 0 auto;
  display: flex;
  align-items: center;
  gap: var(--space-4);
  /* Pad every edge by its notch / camera / status-bar inset so the bar clears
     them when the PWA runs standalone behind a translucent status bar
     (viewport-fit=cover). The top inset clears the status bar / Dynamic Island
     in portrait; the left/right insets clear the camera cutout in landscape, so
     the brand is never sliced by the notch. env() resolves to 0 in a normal
     browser tab, so this is a no-op there. */
  padding: calc(var(--space-2) + env(safe-area-inset-top, 0px))
    calc(var(--space-4) + env(safe-area-inset-right, 0px)) var(--space-2)
    calc(var(--space-4) + env(safe-area-inset-left, 0px));
  background: var(--bg-elevated, rgba(19, 19, 25, 0.92));
  border-bottom: 1px solid var(--border);
  backdrop-filter: blur(10px);
  -webkit-backdrop-filter: blur(10px);
  /* The menu is always the top layer of the shell. backdrop-filter already
     makes this bar a stacking context; an explicit z-index lifts the whole
     subtree (bar + dropdown panel) above any positioned view content and the
     consent banner (--z-toast: 300). It stays below the immersive game lock
     (400), which deliberately owns the full screen during live play. */
  position: relative;
  z-index: 350;
}
.app-brand {
  display: inline-flex;
  align-items: center;
  gap: var(--space-2);
  font-weight: 700;
  color: var(--text-base, var(--text-primary));
  text-decoration: none;
  white-space: nowrap;
}
.app-brand img {
  width: 1.6rem;
  height: 1.6rem;
}

/* Primary tabs (home / games / tools). Active state is weight + underline +
   accent, never color alone. */
.app-tabs {
  display: flex;
  gap: var(--space-1);
  margin-right: auto; /* push the menu button to the far right */
}
.app-tab {
  appearance: none;
  background: none;
  border: 1px solid transparent;
  border-radius: var(--radius-md);
  color: var(--text-secondary);
  font: inherit;
  padding: var(--space-2) var(--space-3);
  min-height: var(--hit-target-min);
  cursor: pointer;
  text-decoration: none;
}
.app-tab:hover,
.app-tab:focus-visible {
  color: var(--text-primary);
  border-color: var(--accent-cyan);
}
.app-tab[aria-current='page'] {
  color: var(--text-primary);
  font-weight: 700;
  box-shadow: inset 0 -2px 0 0 var(--accent-cyan);
}

/* --- Right-side menu (merch / idea / bug / profile / signout) ------------- */
.app-menu {
  position: relative;
}
.app-menu-btn {
  appearance: none;
  background: none;
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  color: var(--text-secondary);
  font: inherit;
  padding: var(--space-2) var(--space-3);
  min-height: var(--hit-target-min);
  cursor: pointer;
}
.app-menu-btn:hover,
.app-menu-btn:focus-visible {
  color: var(--text-primary);
  border-color: var(--accent-cyan);
}
.app-menu-panel {
  position: absolute;
  right: 0;
  top: calc(100% + var(--space-2));
  min-width: 12rem;
  display: flex;
  flex-direction: column;
  gap: 2px;
  padding: var(--space-2);
  background: var(--bg-overlay, #15151c);
  border: 1px solid var(--border-strong, var(--border));
  border-radius: var(--radius-md);
  box-shadow: 0 12px 40px rgba(0, 0, 0, 0.45);
  z-index: var(--z-toast, 60);
}
.app-menu-panel[hidden] {
  display: none;
}
.app-menu-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--space-3);
  background: none;
  border: none;
  border-radius: var(--radius-sm, 6px);
  color: var(--text-secondary);
  font: inherit;
  text-align: left;
  text-decoration: none;
  padding: var(--space-2) var(--space-3);
  min-height: var(--hit-target-min);
  cursor: pointer;
  width: 100%;
}
.app-menu-item:hover,
.app-menu-item:focus-visible {
  color: var(--text-primary);
  background: var(--bg-elevated);
}
/* the little up-right arrow marks "opens in a new tab" without relying on color */
.app-ext::after {
  content: ' \2197';
  color: var(--text-muted);
}

/* --- Content region: fills the rest, scrolls only on overflow ------------ */
.app-view {
  flex: 1 1 auto;
  min-height: 0; /* critical: lets the flex child scroll instead of growing */
  overflow-y: auto;
  padding: var(--space-4);
  /* Match the bar: keep content off the landscape camera cutout. The longhands
     override the shorthand above; .is-immersive resets all four to 0. */
  padding-left: calc(var(--space-4) + env(safe-area-inset-left, 0px));
  padding-right: calc(var(--space-4) + env(safe-area-inset-right, 0px));
}
/* A view can opt out of padding/scroll when it manages its own surface
   (an in-panel game canvas, for example). */
.app-view.is-immersive {
  padding: 0;
  overflow: hidden;
}

.app-view-head {
  display: flex;
  align-items: center;
  gap: var(--space-3);
  flex-wrap: wrap;
  margin-bottom: var(--space-4);
}
.app-view-head h1 {
  font-size: clamp(1.4rem, 4vw, 2rem);
  margin: 0;
  letter-spacing: -0.02em;
}
.app-back {
  appearance: none;
  background: none;
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  color: var(--text-secondary);
  font: inherit;
  font-size: var(--font-size-sm);
  padding: var(--space-1) var(--space-3);
  min-height: var(--hit-target-min);
  cursor: pointer;
  text-decoration: none;
}
.app-back:hover,
.app-back:focus-visible {
  color: var(--text-primary);
  border-color: var(--accent-cyan);
}

/* In-panel game host: the game mounts here; the leaderboard sits below it. */
.app-game-mount {
  display: block;
}
.app-game-leaderboard {
  margin-top: var(--space-5);
}

/* A "coming soon" launcher tile is non-interactive. */
.game-card.is-coming-soon {
  opacity: 0.6;
  cursor: not-allowed;
}

/* Dashboard (home) lists reuse the profile/dashboard look but live in-shell. */
.app-stat-cols {
  display: grid;
  gap: var(--space-4);
  grid-template-columns: 1fr;
}
@media (min-width: 46rem) {
  .app-stat-cols {
    grid-template-columns: 1fr 1fr;
  }
}
.app-stat-list {
  list-style: none;
  margin: 0;
  padding: 0;
  display: flex;
  flex-direction: column;
  gap: var(--space-2);
}
.app-stat-list li {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: var(--space-3);
  padding: var(--space-2) var(--space-3);
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
}
.app-stat-list .unverified {
  font-size: 0.7rem;
  color: var(--text-muted);
  border: 1px solid var(--border);
  border-radius: 999px;
  padding: 0 var(--space-2);
}

/* --- Profile account card ------------------------------------------------ */
.app-account {
  margin: 0;
  display: flex;
  flex-direction: column;
  gap: var(--space-2);
}
.app-account-row {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: var(--space-3);
  padding: var(--space-2) var(--space-3);
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
}
.app-account-row dt {
  margin: 0;
  color: var(--text-secondary);
  font-size: var(--font-size-sm);
}
.app-account-row dd {
  margin: 0;
  color: var(--text-primary);
  word-break: break-word;
  text-align: right;
}

/* A view's action row (e.g. profile's sign-out). */
.app-view-actions {
  margin-top: var(--space-5);
  display: flex;
  gap: var(--space-3);
  flex-wrap: wrap;
}
.app-action {
  appearance: none;
  background: none;
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  color: var(--text-secondary);
  font: inherit;
  padding: var(--space-2) var(--space-4);
  min-height: var(--hit-target-min);
  cursor: pointer;
}
.app-action:hover,
.app-action:focus-visible {
  color: var(--text-primary);
  border-color: var(--accent-magenta, var(--accent-cyan));
}
.app-action[disabled] {
  opacity: 0.5;
  cursor: progress;
}

/* --- Profile edit forms -------------------------------------------------- */
.app-form {
  margin-top: var(--space-4);
  display: flex;
  flex-direction: column;
  gap: var(--space-2);
}
.app-label {
  color: var(--text-secondary);
  font-size: var(--font-size-sm);
}
.app-input {
  appearance: none;
  width: 100%;
  box-sizing: border-box;
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  color: var(--text-primary);
  font: inherit;
  padding: var(--space-2) var(--space-3);
  min-height: var(--hit-target-min);
}
.app-input:focus-visible {
  outline: none;
  border-color: var(--accent-magenta, var(--accent-cyan));
}
.app-form .app-action {
  align-self: flex-start;
}
/* Status line: text carries meaning; the colored bar is a redundant signal
   (never the sole one), so it passes the color-independence rule. */
.app-form-status {
  margin: var(--space-3) 0 0;
  padding: var(--space-2) var(--space-3);
  border-left: 3px solid var(--border);
  color: var(--text-secondary);
  font-size: var(--font-size-sm);
}
.app-form-status[data-kind='success'] {
  border-left-color: var(--accent-cyan, #4ade80);
  color: var(--text-primary);
}
.app-form-status[data-kind='error'] {
  border-left-color: var(--accent-magenta, #f87171);
  color: var(--text-primary);
}

/* --- Mobile bottom tab bar (ADR 0006) ----------------------------------- */
/* Hidden by default; the desktop top bar owns navigation. Revealed only on a
   narrow viewport, where the top bar's tabs and menu button hide. */
.app-bottom-nav {
  display: none;
}

/* Below 40rem we switch to a native-feeling bottom tab bar in the thumb zone.
   The top bar keeps only the brand; tabs and the menu button move to the
   bottom (the menu opens from the "more" entry as a bottom sheet). */
@media (max-width: 40rem) {
  /* The menu panel (a child of this bar) becomes a fixed bottom sheet below.
     backdrop-filter would make the bar a containing block for that fixed
     child, anchoring it to the bar at the top instead of the viewport bottom.
     The bar is a flex sibling (nothing scrolls behind it) so the blur is purely
     cosmetic here; drop it on mobile so fixed positioning stays viewport-relative. */
  .app-bar {
    backdrop-filter: none;
    -webkit-backdrop-filter: none;
    /* The bar holds only the brand on mobile (tabs + menu button hide, the menu
       panel is a fixed bottom sheet). Center its content and zero the gap so the
       wordmark sits dead center: the empty .app-menu sibling collapses to ~0. */
    justify-content: center;
    gap: 0;
    /* Drop the bar down a touch more so the status bar / notch sits within the
       bar's own background, reading as native app chrome rather than content
       crammed under the cutout. */
    padding-top: calc(var(--space-3) + env(safe-area-inset-top, 0px));
  }
  .app-tabs {
    display: none;
  }
  .app-menu-btn {
    display: none;
  }
  .app-brand {
    margin: 0; /* centered by the bar's justify-content on mobile */
  }

  .app-bottom-nav {
    flex: 0 0 auto;
    display: flex;
    align-items: stretch;
    gap: var(--space-1);
    padding: var(--space-1) calc(var(--space-2) + env(safe-area-inset-right, 0px))
      var(--space-1) calc(var(--space-2) + env(safe-area-inset-left, 0px));
    /* keep the bar clear of the home indicator / gesture area on phones */
    padding-bottom: calc(var(--space-1) + env(safe-area-inset-bottom, 0px));
    background: var(--bg-elevated, rgba(19, 19, 25, 0.92));
    border-top: 1px solid var(--border);
    backdrop-filter: blur(10px);
    -webkit-backdrop-filter: blur(10px);
    /* same layer intent as the top bar: above content + consent, below the
       immersive game lock (400) so a live game still owns the screen. */
    position: relative;
    z-index: 350;
  }
  .app-bottom-tab {
    flex: 1 1 0;
    display: flex;
    align-items: center;
    justify-content: center;
    appearance: none;
    background: none;
    border: none;
    border-top: 2px solid transparent; /* active indicator, not color alone */
    border-radius: 0;
    color: var(--text-secondary);
    font: inherit;
    min-height: var(--hit-target-min);
    padding: var(--space-2);
    cursor: pointer;
    text-decoration: none;
  }
  .app-bottom-tab:hover,
  .app-bottom-tab:focus-visible {
    color: var(--text-primary);
  }
  .app-bottom-tab[aria-current='page'] {
    color: var(--text-primary);
    font-weight: 700;
    border-top-color: var(--accent-cyan);
  }

  /* The menu becomes a bottom sheet anchored above the bottom bar. */
  .app-menu-panel {
    position: fixed;
    left: var(--space-3);
    right: var(--space-3);
    top: auto;
    bottom: calc(var(--hit-target-min) + env(safe-area-inset-bottom, 0px) + var(--space-3));
    min-width: 0;
    z-index: 360;
  }
}

/* Tabs and the menu must wrap, not scroll, on narrow viewports. */
@media (max-width: 30rem) {
  .app-bar {
    gap: 0; /* keep the centered wordmark dead center (see 40rem block) */
    /* Tighten the side padding on the smallest screens, but DO NOT drop the
       safe-area insets: a flat value here would re-expose the status bar /
       Dynamic Island behind the logo when installed as a PWA on a phone. The
       top keeps the larger space-3 inset so the notch sits on the bar. */
    padding: calc(var(--space-3) + env(safe-area-inset-top, 0px))
      calc(var(--space-2) + env(safe-area-inset-right, 0px)) var(--space-2)
      calc(var(--space-2) + env(safe-area-inset-left, 0px));
  }
  .app-tab {
    padding: var(--space-2);
  }
}

/* Respect reduced motion: no shell transitions or menu animation. */
@media (prefers-reduced-motion: reduce) {
  * {
    transition: none !important;
    animation: none !important;
  }
}
