/*
  stage.css: the configurator frame.
  Owns the full-height stage (canvas, the first-load screen over it, the wait for a cabin), the
  top bar, the headline, the status line (the busy hairline, short notes), the charge readout
  and the turn and zoom tools. The options sheet and column live in panel.css. Phone layout first; the column layout
  starts at 900px wide and 600px tall.

  Read from js/ui/sheet.js: --sheet-h, --sheet-shift and --sheet-half (the phone sheet's height,
  how far it is lowered, and how far at its half stop). --sheet-rest is what of the sheet shows,
  never counted above the half stop: the camera controls (nav.css) ride on it, and the status
  line sits above them, so a sheet raised to full covers them rather than pushing them off the top.
*/

.cfg {
  --sheet-h: 320px;
  --sheet-shift: 0px;
  --sheet-visible: calc(var(--sheet-h) - var(--sheet-shift));
  --sheet-half: 0px;
  --sheet-rest: calc(var(--sheet-h) - max(var(--sheet-shift), var(--sheet-half)));
  /* The camera controls' row over the car on a phone (nav.css), and the room above the sheet. */
  --nav-h: 60px;
  --nav-gap: 10px;
  position: relative;
  height: 100vh;
  height: 100svh;
  min-height: 540px;
  max-height: 1400px;
  overflow: hidden;
  overflow: clip;
  isolation: isolate;
  background: var(--set);
}

.cfg__canvas {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  /* A sideways drag walks around the car; an up-and-down swipe still scrolls the page. */
  touch-action: pan-y;
  transition: opacity .6s var(--ease);
}
.cfg:not([data-phase="ready"]) .cfg__canvas { opacity: 0; }
/* The car fades up under the fading loader: one crossfade, the same length as the veil's. */
.cfg[data-phase="ready"] .cfg__canvas { transition-duration: .9s; }

/* ---------- The first load (ui/loader.js): the badge, the name, one bar, three stages ----------
   Opaque studio black over the whole stage, above the controls, until the car and its cabin are
   drawn; then it fades as the car fades up beneath it (the canvas transition above), one
   crossfade. The bar fills from real bytes; with none reported it travels and the stage names
   carry the news. data-live: the script is running (without it only the name and a note show). */

.veil {
  position: absolute;
  inset: 0;
  z-index: 3;
  display: grid;
  place-items: center;
  padding: max(24px, env(safe-area-inset-top, 0px)) var(--gutter) max(24px, env(safe-area-inset-bottom, 0px));
  background: radial-gradient(120% 70% at 50% 46%, #17171a 0%, var(--set) 62%);
  transition: opacity .9s var(--ease), visibility 0s linear .9s;
}
.cfg[data-phase="ready"] .veil { opacity: 0; visibility: hidden; pointer-events: none; }
.cfg[data-phase="ready"] .loader__card { transform: scale(.985); }

.loader__card {
  display: grid;
  justify-items: center;
  width: min(340px, 100%);
  transition: transform .9s var(--ease);
}
.loader__mark {
  width: 76px;
  height: 76px;
  fill: none;
  stroke: var(--brass);
  stroke-width: 1.5;
  stroke-linecap: round;
}
/* The ring breathes, slowly, so a stalled line still reads as alive. */
.loader[data-live] .loader__mark circle { animation: loader-breathe 3.2s ease-in-out infinite; }
@keyframes loader-breathe {
  0%, 100% { opacity: 1; }
  50% { opacity: .45; }
}
.loader__name {
  margin: 18px 0 0;
  font-size: 34px;
  font-weight: 640;
  font-stretch: 125%;
  letter-spacing: .26em;
  /* The tracking trails the last letter: pull it back so the name sits on the centre line. */
  margin-right: -.26em;
  line-height: 1;
  text-transform: uppercase;
  color: var(--text);
}

.loader__meter {
  display: none;
  align-items: center;
  gap: 12px;
  width: 100%;
  margin-top: 36px;
}
.loader[data-live] .loader__meter { display: flex; }
.loader__bar {
  position: relative;
  flex: 1;
  height: 3px;
  border-radius: 2px;
  overflow: hidden;
  background: rgba(236, 231, 222, .14);
}
.loader__fill {
  position: absolute;
  inset: 0 auto 0 0;
  width: 34%;
  border-radius: 2px;
  background: var(--brass);
  animation: travel 1.8s var(--ease) infinite;
}
.loader[data-known] .loader__fill {
  width: calc(var(--load, 0) * 100%);
  animation: none;
  transition: width .35s linear;
}
.loader__pct {
  min-width: 4ch;
  font-size: 15px;
  font-weight: 600;
  font-variant-numeric: tabular-nums;
  text-align: right;
  color: var(--text);
  visibility: hidden;
}
.loader[data-known] .loader__pct { visibility: visible; }

.loader__steps {
  display: none;
  justify-content: space-between;
  width: 100%;
  margin: 14px 0 0;
  padding: 0;
  list-style: none;
  font-size: 13px;
  letter-spacing: .02em;
}
.loader[data-live] .loader__steps { display: flex; }
.loader__steps li {
  display: flex;
  align-items: center;
  gap: 7px;
  color: var(--text-3);
  transition: color .4s var(--ease);
}
.loader__steps li::before {
  content: "";
  width: 6px;
  height: 6px;
  border-radius: 50%;
  border: 1px solid currentColor;
  transition: background-color .4s var(--ease), border-color .4s var(--ease);
}
.loader__steps [data-state="now"] { color: var(--text); }
.loader__steps [data-state="now"]::before { border-color: var(--brass); background: var(--brass); }
.loader__steps [data-state="done"] { color: var(--text-2); }
.loader__steps [data-state="done"]::before { background: currentColor; }

.loader__note {
  min-height: 2.9em;
  max-width: 32ch;
  margin: 20px 0 0;
  font-size: 13px;
  line-height: 1.45;
  text-align: center;
  text-wrap: balance;
  color: var(--text-2);
}
.loader__skip {
  min-height: 44px;
  margin-top: 6px;
  padding: 0 22px;
  border-radius: var(--r-chip);
  box-shadow: inset 0 0 0 1px var(--line-2);
  font-size: 14px;
  color: var(--text);
  transition: background-color .2s;
}
.loader__skip:active { background: var(--press); }
@media (hover: hover) { .loader__skip:hover { background: var(--press); } }
/* The car cannot be drawn: the bar and stages go, the words stay, in full light. */
.loader[data-failed] :is(.loader__meter, .loader__steps) { display: none; }
.loader[data-failed] .loader__note { color: var(--text); }
.loader[data-failed] .loader__mark circle { animation: none; }

@media (max-height: 620px) {
  .loader__mark { width: 60px; height: 60px; }
  .loader__name { margin-top: 14px; font-size: 28px; }
  .loader__meter { margin-top: 26px; }
}
@media (prefers-reduced-motion: reduce) {
  .veil, .loader__card { transition-duration: .25s; }
  .cfg[data-phase="ready"] .loader__card { transform: none; }
  .loader[data-live] .loader__mark circle { animation: none; }
  .loader__fill { animation: none; width: 100%; opacity: .35; }
  .loader[data-known] .loader__fill { opacity: 1; transition: none; }
}

/* ---------- The wait: a cabin still on its way (ui/waiting.js) ----------
   Over the last drawn view, never an empty cabin: the car dims and softens a little, the badge
   and a few words sit in the middle of the free stage, and the line fills with real bytes when
   the viewer reports them (data-progress, --wait), else it travels. It fades out as the camera
   moves in, so the cabin arrives with the move. A tap on it stays where you are. */

.wait {
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
  padding: var(--stage-top, 64px) var(--stage-right, 0px) var(--stage-bottom, 0px) var(--stage-left, 0px);
  background: radial-gradient(ellipse at 50% 50%, rgba(14, 15, 17, .62), rgba(14, 15, 17, .8) 70%);
  -webkit-backdrop-filter: blur(6px) saturate(.8);
  backdrop-filter: blur(6px) saturate(.8);
  opacity: 0;
  visibility: hidden;
  cursor: default;
  transition: opacity .55s var(--ease), visibility 0s linear .55s;
}
.cfg[data-waiting] .wait {
  opacity: 1;
  visibility: visible;
  transition: opacity .35s var(--ease), visibility 0s;
}
.wait__card {
  display: grid;
  justify-items: center;
  gap: 12px;
  min-width: min(260px, calc(100vw - 2 * var(--gutter)));
  padding: 22px 26px 18px;
  border-radius: var(--r-panel);
  background: var(--glass-bg);
  -webkit-backdrop-filter: var(--glass-fx);
  backdrop-filter: var(--glass-fx);
  box-shadow: var(--glass-edge);
  transform: translateY(8px) scale(.98);
  transition: transform .55s var(--ease);
}
.cfg[data-waiting] .wait__card { transform: none; }
.wait__mark {
  width: 52px;
  height: 52px;
  fill: none;
  stroke: var(--brass);
  stroke-width: 1.5;
  stroke-linecap: round;
}
/* The ring draws itself round, slowly, while the cabin comes: a sign of life, not a spinner. */
.wait__mark circle {
  stroke-dasharray: 170;
  stroke-dashoffset: 170;
  transform-origin: 32px 32px;
  transform: rotate(-90deg);
  animation: wait-ring 2.4s var(--ease) infinite;
}
@keyframes wait-ring {
  0% { stroke-dashoffset: 170; opacity: .35; }
  55% { stroke-dashoffset: 0; opacity: 1; }
  100% { stroke-dashoffset: 0; opacity: .35; }
}
.wait__text {
  margin: 0;
  font-size: 16px;
  font-weight: 560;
  letter-spacing: .01em;
  color: var(--text);
  text-align: center;
}
.wait__meter {
  display: flex;
  align-items: center;
  gap: 10px;
  width: 200px;
}
.wait__line {
  position: relative;
  flex: 1;
  height: 3px;
  border-radius: 2px;
  overflow: hidden;
  background: rgba(236, 231, 222, .16);
}
.wait__line::after {
  content: "";
  position: absolute;
  inset: 0;
  width: 40%;
  border-radius: 2px;
  background: var(--brass);
  animation: travel 1.6s var(--ease) infinite;
}
.wait[data-progress] .wait__line::after {
  animation: none;
  width: calc(var(--wait, 0) * 100%);
  transition: width .3s linear;
}
.wait__pct {
  min-width: 4ch;
  font-size: 13px;
  font-weight: 600;
  font-variant-numeric: tabular-nums;
  text-align: right;
  color: var(--text);
}
.wait__pct:empty { display: none; }
.wait__hint {
  margin: 2px 0 0;
  font-size: 12.5px;
  color: var(--text-3);
}
/* The overlay says it already: the busy line in the corner stands down meanwhile. */
.cfg[data-waiting] .status { opacity: 0; }
@media (prefers-reduced-motion: reduce) {
  .wait { -webkit-backdrop-filter: none; backdrop-filter: none; background: rgba(14, 15, 17, .66); }
  .wait__card { transform: none; transition: none; }
  .wait__mark circle { animation: none; stroke-dashoffset: 0; }
  .wait__line::after { animation: none; width: 100%; opacity: .5; }
  .wait[data-progress] .wait__line::after { opacity: 1; }
}

/* ---------- The layer of controls over the stage ---------- */

.cfg__ui {
  position: absolute;
  inset: 0;
  pointer-events: none;
}
.cfg__ui > * { pointer-events: auto; }

.bar {
  position: absolute;
  inset: 0 0 auto;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  height: calc(var(--bar-h) + env(safe-area-inset-top, 0px));
  padding: env(safe-area-inset-top, 0px) max(var(--gutter), env(safe-area-inset-right, 0px)) 0 max(var(--gutter), env(safe-area-inset-left, 0px));
  white-space: nowrap;
  /* A fall of studio black, so the name stays readable when the camera brings paint up under it. */
  background: linear-gradient(rgba(14, 15, 17, .92) 40%, rgba(14, 15, 17, 0));
  pointer-events: none;
}
.bar > * { pointer-events: auto; }

.wordmark {
  display: flex;
  align-items: center;
  gap: 10px;
  min-height: 44px;
  text-decoration: none;
}
.wordmark__mark {
  width: 26px;
  height: 26px;
  flex: none;
  fill: none;
  stroke: var(--brass);
  stroke-width: 2.6;
  stroke-linecap: round;
}
.wordmark__name {
  font-size: 15px;
  font-weight: 640;
  font-stretch: 125%;
  letter-spacing: .18em;
  text-transform: uppercase;
}
.bar__by {
  display: flex;
  align-items: center;
  min-height: 44px;
  font-size: 13px;
  color: var(--text-2);
  text-decoration: none;
}
.bar__by-long { display: none; }
@media (min-width: 480px) {
  .bar__by-long { display: inline; white-space: pre; }
}

/* The headline is read on a phone (the bar says the name) but only shown beside the car. */
.intro {
  position: absolute;
  width: 1px;
  height: 1px;
  overflow: hidden;
  clip: rect(0 0 0 0);
  white-space: nowrap;
}

/* ---------- Status: loading words, the busy hairline and short notes ---------- */

.status {
  position: absolute;
  left: 50%;
  bottom: calc(var(--sheet-rest) + var(--nav-gap) + var(--nav-h) + 10px);
  transform: translateX(-50%);
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 8px;
  max-width: calc(100% - 2 * var(--gutter));
  pointer-events: none;
  transition: bottom .4s var(--ease), opacity .3s;
}
.cfg.is-dragging .status { transition: opacity .3s; }
.status__text {
  font-size: 13px;
  color: var(--text-2);
  text-align: center;
  text-wrap: balance;
}
.status__text:empty { display: none; }
.status__line {
  position: relative;
  width: 120px;
  height: 1px;
  overflow: hidden;
  background: var(--line);
  opacity: 0;
  transition: opacity .3s;
}
.status__line::after {
  content: "";
  position: absolute;
  inset: 0;
  width: 40%;
  background: var(--brass);
  animation: travel 1.6s var(--ease) infinite;
}
@keyframes travel {
  from { transform: translateX(-100%); }
  to { transform: translateX(250%); }
}
.cfg[data-busy] .status__line { opacity: 1; }
/* The first load says everything in the loader; the status line waits for the car. */
.cfg:not([data-phase="ready"]) .status { opacity: 0; }

/* ---------- The charge readout, while the car is plugged in ---------- */

.meter {
  position: absolute;
  top: calc(var(--bar-h) + env(safe-area-inset-top, 0px) + 4px);
  left: 50%;
  display: flex;
  align-items: center;
  gap: 10px;
  min-height: 40px;
  padding: 0 16px 0 12px;
  border-radius: 999px;
  white-space: nowrap;
  opacity: 0;
  visibility: hidden;
  transform: translate(-50%, -6px);
  transition: opacity .35s var(--ease), transform .35s var(--ease), visibility 0s linear .35s;
  pointer-events: none;
}
.cfg[data-charging] .meter {
  opacity: 1;
  visibility: visible;
  transform: translate(-50%, 0);
  transition: opacity .35s var(--ease), transform .35s var(--ease), visibility 0s;
}
.meter__icon { width: 16px; height: 16px; color: var(--brass); }
.meter__bar {
  position: relative;
  width: 56px;
  height: 3px;
  border-radius: 2px;
  overflow: hidden;
  background: var(--line-2);
}
.meter__fill {
  --pct: 0%;
  position: absolute;
  inset: 0 auto 0 0;
  width: var(--pct);
  background: var(--brass);
}
.meter__pct { min-width: 3ch; font-size: 15px; font-weight: 600; font-variant-numeric: tabular-nums; }
.meter__word { font-size: 12.5px; color: var(--text-2); }

/* ---------- Turn and zoom: beside the car on the column layout only (a finger drags and pinches) ---------- */

.tools { display: none; }
.tool {
  display: grid;
  place-items: center;
  width: 44px;
  height: 44px;
  border-radius: 50%;
  color: var(--text-2);
  transition: color .2s, background-color .2s;
}
.tool svg { width: 20px; height: 20px; }
.tool--flip svg { transform: scaleX(-1); }
.tool:active { background: var(--press); color: var(--text); }

/* The controls appear with the car, never over a half-built stage. */
.cfg:not([data-phase="ready"]) .tools,
.cfg:not([data-phase="ready"]) .dock { opacity: 0; visibility: hidden; }
.tools, .dock { transition: opacity .5s var(--ease) .15s, visibility 0s; }

@media (min-width: 900px) and (min-height: 600px) {
  :root { --bar-h: 64px; --edge: 24px; }
  .bar { padding-inline: var(--edge); }
  .wordmark__name { font-size: 16px; }
  .cfg { --dock-w: clamp(360px, 29vw, 430px); }

  /* In line with the turn and zoom tools, just right of them, clear of the headline at any width. */
  .meter { top: auto; bottom: var(--edge); left: calc(var(--edge) + 246px); min-height: 50px; transform: translateY(6px); }
  .cfg[data-charging] .meter { transform: none; }

  .intro {
    position: absolute;
    top: calc(var(--bar-h) + 4px);
    left: var(--edge);
    width: min(28rem, calc(100% - 460px));
    height: auto;
    overflow: visible;
    clip: auto;
    white-space: normal;
    pointer-events: none;
    transition: opacity .4s var(--ease);
  }
  /* The headline steps aside when the camera goes in close, so it never sits over a dashboard. */
  .cfg:is([data-view="interior"], [data-view="cockpit"], [data-view="cabin34"], [data-view="cockpit-high"],
    [data-view="dash"], [data-view="rear"], [data-view^="detail-"], [data-view="ev-bay"], [data-view="frunk"]) .intro { opacity: 0; }
  .intro h1 {
    font-size: clamp(1.45rem, 1.1rem + .8vw, 2.05rem);
    font-weight: 520;
    font-stretch: 112%;
    line-height: 1.14;
    letter-spacing: -.004em;
    text-wrap: balance;
  }
  .intro p {
    margin-top: .7rem;
    max-width: 25rem;
    font-size: 15px;
    color: var(--text-2);
    text-wrap: pretty;
  }
  .intro .intro__from {
    margin-top: .55rem;
    font-size: 14px;
    font-weight: 560;
    font-stretch: 105%;
    letter-spacing: .01em;
    color: var(--text);
  }

  .status {
    left: var(--edge);
    bottom: 84px;
    transform: none;
    align-items: flex-start;
  }
  .status__text { text-align: left; }

  .tools {
    position: absolute;
    left: var(--edge);
    bottom: var(--edge);
    display: flex;
    gap: 2px;
    padding: 3px;
    border-radius: 999px;
  }
}

@media (hover: hover) {
  .tool:hover { color: var(--text); background: var(--press); }
  .wordmark:hover .wordmark__name, .bar__by:hover { color: var(--text); }
}
