/* ---------------------------------------------------------------------------
   Injected into every idea page by the gallery (see ideas/views.py).

   Deliberately conservative: these pages are self-contained apps with their own
   layouts, and the job here is to make them reachable and usable on a phone,
   not to restyle them. Anything that could move an app's own pixels is either
   scoped to small screens or gated on what the page actually contains.
--------------------------------------------------------------------------- */

/* --- the "back to gallery" control ------------------------------------- */
.ig-back {
  position: fixed;
  z-index: 2147483000;                       /* above the app's own chrome */
  left: max(10px, env(safe-area-inset-left));
  bottom: max(10px, env(safe-area-inset-bottom));
  display: inline-flex;
  align-items: center;
  gap: 7px;
  height: 40px;
  padding: 0 15px 0 12px;
  border-radius: 999px;
  border: 1px solid rgba(255, 255, 255, .18);
  background: rgba(18, 18, 24, .82);
  -webkit-backdrop-filter: blur(10px);
  backdrop-filter: blur(10px);
  color: #f4f5f8;
  font: 600 14px/1 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  text-decoration: none;
  box-shadow: 0 6px 20px -6px rgba(0, 0, 0, .7);
  transition: padding .25s ease, opacity .25s ease, background .2s ease;
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
}
.ig-back:hover, .ig-back:focus-visible { background: rgba(30, 30, 40, .95); outline: none; }
.ig-back svg { width: 16px; height: 16px; flex: none; }
.ig-back span { white-space: nowrap; max-width: 40vw; overflow: hidden; text-overflow: ellipsis; }

/* Collapses to a disc once you start using the page, so it stops covering it. */
.ig-back.is-tucked { padding: 0; width: 40px; justify-content: center; opacity: .55; }
.ig-back.is-tucked span { display: none; }
.ig-back.is-tucked:hover, .ig-back.is-tucked:focus-visible { opacity: 1; }

@media print { .ig-back { display: none; } }

/* --- scrolling ---------------------------------------------------------- */
/* Set by the script only when a page locks scrolling but is taller than the
   screen — i.e. content is unreachable. Full-screen canvas apps are untouched. */
html.ig-unlocked, html.ig-unlocked body {
  overflow-y: auto !important;
  height: auto !important;
  min-height: 100% !important;
  -webkit-overflow-scrolling: touch;
}

/* --- reflow -------------------------------------------------------------
   Applied by the script to the specific elements that overflow, so a layout
   built for a desktop window becomes a single readable column instead of a
   scaled-down photograph of itself. Scoped under html.ig-reflow and driven by
   classes, so rotating back to a wide screen takes it all off again.
------------------------------------------------------------------------- */
html.ig-reflow .ig-fluid {
  width: 100% !important;
  min-width: 0 !important;
  max-width: 100% !important;
}

html.ig-reflow .ig-wrap {
  flex-wrap: wrap !important;
}

html.ig-reflow .ig-stack {
  grid-template-columns: 1fr !important;
}

/* Children of a stacked grid must not keep a fixed column span. */
html.ig-reflow .ig-stack > * {
  grid-column: auto !important;
  min-width: 0 !important;
}

/* Media inside a reflowed page should never be the thing that keeps it wide. */
html.ig-reflow img,
html.ig-reflow svg:not(:root),
html.ig-reflow video {
  max-width: 100%;
  height: auto;
}

/* --- last-resort panning ------------------------------------------------ */
/* Set by the script only when a page could not be fitted AND is clipping the
   overflow, so the remainder can be reached by swiping sideways. */
html.ig-panx, html.ig-panx body {
  overflow-x: auto !important;
  -webkit-overflow-scrolling: touch;
}

/* --- document-style pages (no canvas: the manuals, mostly) -------------- */
@media (max-width: 900px) {
  html[data-ig-kind="doc"] img,
  html[data-ig-kind="doc"] svg { max-width: 100%; height: auto; }

  /* Wide tables and code scroll inside their own box instead of stretching
     the page sideways. */
  html[data-ig-kind="doc"] table,
  html[data-ig-kind="doc"] pre {
    display: block;
    max-width: 100%;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
  }

  html[data-ig-kind="doc"] { -webkit-text-size-adjust: 100%; }
}

/* --- touch niceties ----------------------------------------------------- */
/* Kills the 300ms double-tap delay on controls without disabling pinch-zoom
   for the page as a whole. */
@media (pointer: coarse) {
  button, [role="button"], input[type="button"], input[type="submit"],
  select, label, summary { touch-action: manipulation; }
}

/* --- on-screen keys (see idea-mobile-input.js) --------------------------
   Half of these pages are played on a computer keyboard, which a phone does
   not have. The keys the page listens for are read out of its own source and
   offered here. Hidden until asked for, because a page with its own on-screen
   piano does not need a second one covering it.
----------------------------------------------------------------------- */
.ig-keys-toggle {
  position: fixed;
  z-index: 2147483000;
  right: max(10px, env(safe-area-inset-right));
  bottom: max(10px, env(safe-area-inset-bottom));
  display: inline-flex;
  align-items: center;
  gap: 6px;
  height: 40px;
  padding: 0 14px;
  border-radius: 999px;
  border: 1px solid rgba(255, 255, 255, .18);
  background: rgba(18, 18, 24, .82);
  -webkit-backdrop-filter: blur(10px);
  backdrop-filter: blur(10px);
  color: #f4f5f8;
  font: 600 14px/1 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  box-shadow: 0 6px 20px -6px rgba(0, 0, 0, .7);
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
  cursor: pointer;
}
.ig-keys-toggle.is-open { background: rgba(90, 120, 255, .92); border-color: rgba(255, 255, 255, .35); }
.ig-keys-toggle.is-new { animation: ig-pulse 1.6s ease-in-out 3; }

@keyframes ig-pulse {
  0%, 100% { box-shadow: 0 6px 20px -6px rgba(0, 0, 0, .7); }
  50%      { box-shadow: 0 0 0 8px rgba(90, 120, 255, .28), 0 6px 20px -6px rgba(0, 0, 0, .7); }
}
@media (prefers-reduced-motion: reduce) { .ig-keys-toggle.is-new { animation: none; } }

.ig-keys {
  position: fixed;
  z-index: 2147482999;
  left: 0;
  right: 0;
  bottom: 0;
  display: flex;
  align-items: flex-end;
  gap: 10px;
  padding: 10px 10px calc(58px + env(safe-area-inset-bottom));
  background: rgba(10, 10, 14, .93);
  -webkit-backdrop-filter: blur(14px);
  backdrop-filter: blur(14px);
  border-top: 1px solid rgba(255, 255, 255, .12);
  box-shadow: 0 -8px 30px -10px rgba(0, 0, 0, .8);
  /* The pad is the one place on these pages where a swipe must never scroll:
     a finger sliding across it is a glissando, not a gesture at the page. */
  touch-action: none;
  -webkit-user-select: none;
  user-select: none;
}
.ig-keys[hidden] { display: none; }

.ig-keys-grid {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  flex: 1;
  min-width: 0;
  justify-content: center;
  max-height: 40vh;
  overflow-y: auto;
}

.ig-key {
  flex: 0 0 auto;
  min-width: 40px;
  height: 44px;
  padding: 0 10px;
  border-radius: 10px;
  border: 1px solid rgba(255, 255, 255, .16);
  background: rgba(42, 44, 56, .95);
  color: #eef0f6;
  font: 600 15px/1 ui-monospace, SFMono-Regular, Menlo, monospace;
  -webkit-tap-highlight-color: transparent;
  touch-action: none;
  cursor: pointer;
}
.ig-key.is-down { background: #5a78ff; border-color: #93a8ff; color: #fff; transform: translateY(1px); }
.ig-key-wide { min-width: 110px; }

/* The arrow cluster keeps its shape so a thumb finds it without looking. */
.ig-keys-pad {
  flex: 0 0 auto;
  display: grid;
  grid-template-columns: repeat(3, 44px);
  grid-template-rows: repeat(2, 44px);
  gap: 6px;
}
.ig-keys-pad .ig-key { width: 100%; min-width: 0; padding: 0; }
.ig-keys-cell { display: contents; }
.ig-keys-cell.ig-keys-left  > .ig-key { grid-area: 2 / 1; }
.ig-keys-cell.ig-keys-up    > .ig-key { grid-area: 1 / 2; }
.ig-keys-cell.ig-keys-down  > .ig-key { grid-area: 2 / 2; }
.ig-keys-cell.ig-keys-right > .ig-key { grid-area: 2 / 3; }

/* With the pad open the back pill would sit under it. */
html.ig-keys-open .ig-back { display: none; }

/* --- the sound nudge ---------------------------------------------------- */
.ig-audio {
  position: fixed;
  z-index: 2147483001;
  left: 50%;
  transform: translateX(-50%);
  top: max(12px, env(safe-area-inset-top));
  height: 40px;
  padding: 0 18px;
  border-radius: 999px;
  border: 1px solid rgba(255, 255, 255, .22);
  background: rgba(200, 60, 60, .92);
  color: #fff;
  font: 600 14px/1 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  box-shadow: 0 6px 20px -6px rgba(0, 0, 0, .7);
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
  cursor: pointer;
}

@media print { .ig-keys, .ig-keys-toggle, .ig-audio { display: none; } }
