/* The Working Stage (stage.js / stageModel.js, Working Presence 3.0).
 *
 * While Ghost works, the figure's scene says WHAT it is doing -- big, first
 * person, honest. Two presentations of the same hero element (one figure, one
 * decoder -- nothing is duplicated or moved):
 *
 *   Start   the greeting gives way to the stage copy under the figure.
 *   Chat    the hero folds into a band above the messages (figure left, the
 *           copy right) for as long as Ghost works or a voice session is live;
 *           it leaves again after the terminal beat.
 *
 * Hooks written by stage.js (never by hand): data-stage="live|off",
 * data-stage-mode, data-stage-takeover on #ghost-hero and #app-shell, and
 * data-mode / data-tone on #stage-copy. Only transform/opacity animate; the
 * figure itself carries the state's motion (ghostRig.js). No percentages, no
 * bars that imply progress -- the copy is the only progress there is.
 *
 * GEOMETRY CONTRACT (Phase C, mobile working-presence stability). The copy is a
 * grid of FOUR NAMED, FIXED-HEIGHT TRACKS -- icon / primary / secondary /
 * tertiary -- whose sizes are custom properties (--sc-*), never content. A slot
 * with nothing to say (no icon, no fact, no next step: `display: none` or
 * `hidden` on the element) leaves its track standing, so the block has the same
 * height and every line the same position in every state: content moves, the
 * screen does not. Line heights are fixed rem values (no `normal`, no unitless
 * fractions), the text clamps INSIDE its track, and nothing here sets a margin,
 * gap or size from a state attribute -- state selectors only recolour. The Start
 * scene and the chat band declare their own slot sizes (below); neither grows
 * with content.
 */

/* --- the copy ------------------------------------------------------------ */

/* TYPOGRAPHY CONTRACT (Phase C.1). ONE token set for the Start scene, declared once on
   .hero and consumed by the greeting (style.css .hero__greeting) AND the stage copy, so the
   greeting and the working primary can never drift apart again. Mobile and wide screens have
   their own values; inside a breakpoint nothing depends on state.

     PRIMARY    greeting / "Ich lese die Mail."   24/28 (wide 32/36)  w500  max 2 lines  centered
     SECONDARY  latest real fact                  16/24 (wide 18/26)  w400  max 1 line   centered
     TERTIARY   next known step / approval note   13/20 (wide 14/20)  w400  max 1 line   centered
     ICON       fixed track above the primary     32   (wide 40)       -- stays standing when empty

   --wp-block is the whole Start block (icon + primary + secondary + tertiary tracks);
   --wp-meta is what the idle voice status + voice activity occupy. The idle greeting box is
   --wp-block minus --wp-meta, so greeting + status takes exactly the room the stage copy takes. */
.hero {
  --wp-icon: 2rem;
  --wp-disc: 1.75rem;
  --wp-pri-size: 1.5rem;
  --wp-pri-lh: 1.75rem;
  --wp-pri-weight: 500; /* typography.md: avoid light weights on dark */
  --wp-pri-lines: 2;
  --wp-sec-size: var(--fs-body);
  --wp-sec-lh: 1.5rem;
  --wp-sec-lines: 1;
  --wp-ter-size: var(--fs-small);
  --wp-ter-lh: 1.25rem;
  --wp-ter-lines: 1;
  --wp-block: calc(var(--wp-icon) + var(--wp-pri-lines) * var(--wp-pri-lh) + var(--wp-sec-lines) * var(--wp-sec-lh) + var(--wp-ter-lines) * var(--wp-ter-lh));
  /* idle voice status (1.5rem) + voice activity (1.1rem, margin -0.25rem) + the two panel gaps */
  --wp-meta: calc(1.5rem + 2 * var(--s-4) + 0.85rem);
}

.stage-copy {
  --stage-accent: var(--orb-color-2, var(--light-cyan));
  /* Start-scene slots (phone first). Heights = lines x fixed line-height. */
  --sc-gap: 0rem; /* the line-heights carry the rhythm; a gap would be one more thing that can differ */
  --sc-icon: var(--wp-icon); /* the icon TRACK; the disc is a little smaller and centred in it */
  --sc-disc: var(--wp-disc);
  --sc-pri-size: var(--wp-pri-size);
  --sc-pri-lh: var(--wp-pri-lh);
  --sc-pri-lines: var(--wp-pri-lines);
  --sc-sec-size: var(--wp-sec-size);
  --sc-sec-lh: var(--wp-sec-lh);
  --sc-sec-lines: var(--wp-sec-lines);
  --sc-ter-size: var(--wp-ter-size);
  --sc-ter-lh: var(--wp-ter-lh);
  --sc-ter-lines: var(--wp-ter-lines);
  display: none;
  grid-template-areas: 'icon' 'pri' 'sec' 'ter';
  grid-template-rows:
    var(--sc-icon)
    calc(var(--sc-pri-lines) * var(--sc-pri-lh))
    calc(var(--sc-sec-lines) * var(--sc-sec-lh))
    calc(var(--sc-ter-lines) * var(--sc-ter-lh));
  grid-template-columns: minmax(0, 1fr);
  row-gap: var(--sc-gap);
  justify-items: center;
  align-content: start;
  width: 100%;
  max-width: 34rem;
  margin-top: var(--s-2);
  text-align: center;
}

/* Start: the copy replaces the greeting (which stays for screen readers) and the
   voice status sentence (the voice-status live region keeps announcing it). */
.hero[data-stage-takeover] .stage-copy {
  display: grid;
}

.hero[data-stage-takeover] .hero__greeting,
.hero[data-stage-takeover] .voice-status,
.hero[data-stage-takeover] .voice-activity {
  position: absolute;
  width: 1px;
  height: 1px;
  margin: -1px;
  padding: 0;
  overflow: hidden;
  clip: rect(0 0 0 0);
  clip-path: inset(50%);
  white-space: nowrap;
  border: 0;
}

/* The icon slot: its track is always there; the circle itself only when stage.js
   has put an icon in it (an icon-less state shows an empty slot, not a shorter block). */
.stage-copy__icon {
  grid-area: icon;
  display: grid;
  place-items: center;
  align-self: center;
  width: var(--sc-disc);
  height: var(--sc-disc);
  border-radius: 50%;
  color: var(--stage-accent);
  background: color-mix(in srgb, var(--stage-accent) 12%, transparent);
  box-shadow: inset 0 0 0 1px color-mix(in srgb, var(--stage-accent) 34%, transparent), 0 0 26px color-mix(in srgb, var(--stage-accent) 22%, transparent);
  transition: opacity var(--dur-2) var(--ease-out);
}

.stage-copy__icon:empty {
  display: none; /* the track (--sc-icon) keeps the space */
}

.stage-copy__icon .am {
  --am-size: calc(var(--sc-disc) * 0.6);
}

.stage-copy__primary,
.stage-copy__secondary,
.stage-copy__tertiary {
  display: -webkit-box;
  -webkit-box-orient: vertical;
  overflow: hidden;
  text-wrap: balance;
}

/* `stage.js` marks an empty line `hidden`: that must still mean "not rendered" (the author
   `display: -webkit-box` above would otherwise win over the UA rule). The TRACK is reserved by the
   grid container, so a non-rendered line costs the layout nothing. */
.stage-copy__primary[hidden],
.stage-copy__secondary[hidden],
.stage-copy__tertiary[hidden] {
  display: none;
}

.stage-copy__primary {
  grid-area: pri;
  max-width: 26ch;
  color: var(--ink-1);
  font-size: var(--sc-pri-size);
  font-weight: var(--wp-pri-weight);
  line-height: var(--sc-pri-lh);
  letter-spacing: -0.005em;
  -webkit-line-clamp: var(--sc-pri-lines);
  line-clamp: var(--sc-pri-lines);
}

.stage-copy__secondary {
  grid-area: sec;
  max-width: 34ch;
  color: var(--ink-2);
  font-size: var(--sc-sec-size);
  line-height: var(--sc-sec-lh);
  -webkit-line-clamp: var(--sc-sec-lines);
  line-clamp: var(--sc-sec-lines);
}

.stage-copy__secondary[data-tone='problem'] {
  color: color-mix(in srgb, var(--error) 55%, var(--ink-1));
}

.stage-copy__tertiary {
  grid-area: ter;
  max-width: 34ch;
  color: var(--ink-3);
  font-size: var(--sc-ter-size);
  line-height: var(--sc-ter-lh);
  -webkit-line-clamp: var(--sc-ter-lines);
  line-clamp: var(--sc-ter-lines);
}

/* A new primary line fades up (opacity/transform only). */
.stage-copy[data-swap] .stage-copy__primary {
  animation: stage-swap 320ms var(--ease-out) both;
}

/* Never from 0: the words must stay legible even if a frame clock is throttled. */
@keyframes stage-swap {
  from {
    opacity: 0.55;
    transform: translateY(6px);
  }
  to {
    opacity: 1;
    transform: none;
  }
}

/* Tone = the signal colours of D29, plus the honest neutral of "just working". */
.stage-copy[data-tone='decision'] {
  --stage-accent: var(--warn);
}

.stage-copy[data-tone='problem'] {
  --stage-accent: var(--error);
}

.stage-copy[data-tone='problem'] .stage-copy__primary {
  color: color-mix(in srgb, var(--error) 30%, var(--ink-1));
}

.stage-copy[data-tone='done'] {
  --stage-accent: var(--light-cyan);
}

.stage-copy[data-mode='working'] {
  --stage-accent: var(--light-cyan);
}

.stage-copy[data-mode='thinking'],
.stage-copy[data-mode='responding'] {
  --stage-accent: var(--light-violet);
}

/* --- Start scene slot sizes, wide screens ----------------------------------- */

@media (min-width: 769px) {
  .hero {
    --wp-icon: 2.5rem;
    --wp-disc: 2.25rem;
    --wp-pri-size: 2rem;
    --wp-pri-lh: 2.25rem;
    --wp-sec-size: var(--fs-lead);
    --wp-sec-lh: 1.625rem;
    --wp-ter-size: 0.875rem;
    --wp-meta: calc(1.5rem + 2 * var(--s-3) + 0.85rem); /* the wide voice panel uses the tighter gap */
  }

  /* two lines of the primary use the whole copy width (34rem) so the longer intent sentences fit */
  .stage-copy__primary {
    max-width: none;
  }
}

/* --- Desktop: the stage leads, the rest steps back ------------------------ */

/* While Ghost thinks / works / writes, the navigation and the "Heute" rail drop a
   little in contrast so they never compete with the stage; they return at full
   strength on hover/focus and whenever a decision waits (approval keeps the rail:
   "Braucht Aufmerksamkeit" is exactly what the owner needs then). */
@media (min-width: 769px) {
  #app-shell:is([data-stage-mode='thinking'], [data-stage-mode='working'], [data-stage-mode='responding']) :is(#sidebar, .rail) {
    opacity: 0.74;
    transition: opacity 420ms var(--ease-out);
  }

  #app-shell:is([data-stage-mode='thinking'], [data-stage-mode='working'], [data-stage-mode='responding']) :is(#sidebar, .rail):is(:hover, :focus-within) {
    opacity: 1;
  }
}

/* --- Chat: the hero folds into a band ------------------------------------- */

#app-shell[data-view='chat'][data-stage='live'] .hero {
  --orb-size: clamp(4.75rem, 13dvh, 8rem);
  --hero-top: 0px;
  order: -1;
  display: flex;
  flex-direction: row;
  align-items: center;
  justify-content: flex-start;
  gap: var(--s-4);
  padding: var(--s-2) var(--s-4) var(--s-3);
  text-align: left;
  animation: stage-band-in 380ms var(--ease-out) both;
}

@keyframes stage-band-in {
  from {
    opacity: 0.6;
    transform: translateY(-8px);
  }
  to {
    opacity: 1;
    transform: none;
  }
}

/* The band is the figure and its words -- nothing else of the hero. The voice
   status stays in the accessibility tree as the live region it is (visually
   hidden, NOT display:none: a screen reader must keep hearing "Ich höre zu" /
   "Ich arbeite daran" in the chat); the round control and the cue switch, which
   are not part of the band, are removed so nothing invisible can take focus. */
#app-shell[data-view='chat'][data-stage='live'] .hero :is(.hero__horizon, .hero__greeting) {
  display: none;
}

#app-shell[data-view='chat'][data-stage='live'] .hero .voice-panel {
  position: absolute;
  width: 1px;
  height: 1px;
  margin: -1px;
  padding: 0;
  overflow: hidden;
  clip: rect(0 0 0 0);
  clip-path: inset(50%);
  white-space: nowrap;
  border: 0;
}

#app-shell[data-view='chat'][data-stage='live'] .hero .voice-panel :is(.voice-cta, .voice-cues, .voice-activity) {
  display: none;
}

#app-shell[data-view='chat'][data-stage='live'] .hero > .orb {
  margin: 0;
  flex: none;
}

#app-shell[data-view='chat'][data-stage='live'] .hero::before {
  left: calc(var(--orb-size) * 0.5 + var(--s-4));
  top: 50%;
  width: calc(var(--orb-size) * 3.2);
}

/* In the band the copy shows for every live mode (calm voice states included),
   left aligned next to the figure. The band has its OWN compact slot sizes (no
   icon -- the figure carries it; two lines of primary, one fact, one next step
   on a wide screen): the block is a fixed-height unit, centred as a UNIT against
   the figure, top-anchored inside, so a line appearing or wrapping moves nothing
   that was already there. Its height (phone: 62px) stays below the figure's, so
   the band -- and the messages under it -- keep the height the figure gives them. */
#app-shell[data-view='chat'][data-stage='live'] .stage-copy {
  --sc-gap: var(--s-1);
  --band-icon-col: 1.5rem; /* the icon column is ALWAYS there: text starts at the same x with or without an icon */
  --sc-disc: 1.5rem;
  --sc-pri-size: 1.375rem;
  --sc-pri-lh: 1.625rem;
  --sc-pri-lines: 2;
  --sc-sec-size: var(--fs-body);
  --sc-sec-lh: 1.375rem;
  --sc-sec-lines: 1;
  --sc-ter-size: var(--fs-small);
  --sc-ter-lh: 1.125rem;
  --sc-ter-lines: 1;
  display: grid;
  grid-template-areas: 'icon pri' 'icon sec' 'icon ter';
  grid-template-columns: var(--band-icon-col) minmax(0, 1fr);
  column-gap: var(--s-2);
  grid-template-rows:
    calc(var(--sc-pri-lines) * var(--sc-pri-lh))
    calc(var(--sc-sec-lines) * var(--sc-sec-lh))
    calc(var(--sc-ter-lines) * var(--sc-ter-lh));
  flex: 1;
  min-width: 0;
  align-self: center;
  align-content: start;
  justify-items: start;
  margin-top: 0;
  text-align: left;
}

/* The band's icon: a small disc in its own column, level with the first line of the primary. */
#app-shell[data-view='chat'][data-stage='live'] .stage-copy__icon {
  align-self: start;
  justify-self: start;
  margin-top: calc((var(--sc-pri-lh) - var(--sc-disc)) / 2);
}

#app-shell[data-view='chat'][data-stage='live'] :is(.stage-copy__primary, .stage-copy__secondary, .stage-copy__tertiary) {
  max-width: none;
}

/* The header would say the same thing twice: the band leads. */
#app-shell[data-view='chat'][data-stage='live'] :is(.chat-head__orb, .chat-head__status) {
  display: none;
}

/* The desktop grid gets one more row for the band; without a band it is zero-high. */
@media (min-width: 1100px) {
  #app-shell[data-view='chat'] #stage {
    grid-template-rows: auto minmax(0, 1fr) auto;
  }

  #app-shell[data-view='chat'][data-stage='live'] #stage > .hero {
    grid-column: 1;
    grid-row: 1;
    align-self: start;
  }

  #app-shell[data-view='chat'] #stage > #chat-view {
    grid-row: 2;
  }

  #app-shell[data-view='chat'] #stage > #composer {
    grid-row: 3;
  }
}

/* Phone: a compact band -- the figure, the primary line and one fact; the next step is Start's detail
   (no track for it: the block stays 62px, under the figure's 68px). */
@media (max-width: 768px) {
  #app-shell[data-view='chat'][data-stage='live'] .hero {
    --orb-size: 4.25rem;
    gap: var(--s-3);
    padding: calc(var(--safe-top) + var(--s-2)) var(--s-4) var(--s-2);
  }

  #app-shell[data-view='chat'][data-stage='live'] .stage-copy {
    --sc-pri-size: 1.0625rem;
    --sc-pri-lh: 1.25rem;
    --sc-sec-size: var(--fs-small);
    --sc-sec-lh: 1.125rem;
    grid-template-areas: 'icon pri' 'icon sec';
    grid-template-rows:
      calc(var(--sc-pri-lines) * var(--sc-pri-lh))
      calc(var(--sc-sec-lines) * var(--sc-sec-lh));
  }

  #app-shell[data-view='chat'][data-stage='live'] .stage-copy__tertiary {
    display: none;
  }
}

/* Very narrow phones keep the icon slot but spend less width on it. */
@media (max-width: 359px) {
  #app-shell[data-view='chat'][data-stage='live'] .stage-copy {
    --band-icon-col: 1.25rem;
    --sc-disc: 1.25rem;
  }
}

/* --- Soft state changes: opacity + a few px of translate, never size or margin --------------- */

.hero[data-stage-takeover] .stage-copy {
  animation: stage-enter 280ms var(--ease-out) both;
}

.stage-copy__icon > .am {
  animation: stage-icon-in 260ms var(--ease-out) both;
}

@keyframes stage-enter {
  from {
    opacity: 0.4;
  }
  to {
    opacity: 1;
  }
}

@keyframes stage-icon-in {
  from {
    opacity: 0.35;
    transform: translateY(3px);
  }
  to {
    opacity: 1;
    transform: none;
  }
}

@media (prefers-reduced-motion: reduce) {
  .hero[data-stage-takeover] .stage-copy,
  .stage-copy__icon > .am,
  .stage-copy[data-swap] .stage-copy__primary,
  #app-shell[data-view='chat'][data-stage='live'] .hero {
    animation: none;
  }

  .stage-copy__icon {
    transition: none;
  }
}
