/*
 * 3D model viewer (js/viewer.js). Every colour is a custom property on
 * .viewer, taken from the site theme (css/site.css + theme.css): --backdrop,
 * --surface, --ink, --muted, --rule, --focus. A page can still override any
 * of them, e.g.
 *   .viewer { --viewer-model-color: var(--accent); }
 * Without a theme (the standalone dev demo) they fall back to system colours.
 * --viewer-model-color and --viewer-grid-color are read by the script at mount
 * through a canvas, which only understands plain colours: keep those two free
 * of color-mix() and friends.
 * Spacing uses a 4px scale; controls are 44px touch targets.
 */
.viewer {
  --viewer-bg: var(--backdrop, Canvas);
  --viewer-bg-glow: var(--surface, Canvas);
  --viewer-fg: var(--ink, CanvasText);
  --viewer-muted: var(--muted, GrayText);
  --viewer-surface: color-mix(in srgb, var(--surface, Canvas) 94%, transparent);
  --viewer-surface-hover: var(--backdrop, ButtonFace);
  --viewer-border: color-mix(in srgb, var(--rule, GrayText) 85%, var(--ink, CanvasText));
  --viewer-focus: var(--focus, Highlight);
  --viewer-grid-color: var(--muted, GrayText);
  --viewer-scrim: color-mix(in srgb, var(--backdrop, Canvas) 85%, transparent);

  position: relative;
  display: block;
  width: 100%;
  aspect-ratio: 4 / 3;
  overflow: hidden;
  border-radius: var(--radius, 8px);
  color: var(--viewer-fg);
  background: radial-gradient(ellipse at 50% 38%, var(--viewer-bg-glow), var(--viewer-bg) 72%);
  font-size: 0.875rem;
  line-height: 1.5;
  isolation: isolate;
  container-type: inline-size;
}

/* A soft highlight behind the model: the backdrop, nudged lighter. */
@supports (color: oklch(from red l c h)) {
  .viewer {
    --viewer-bg-glow: oklch(from var(--backdrop, Canvas) calc(l + 0.05) c h);
  }
}

.viewer:fullscreen,
.viewer--expanded {
  width: 100vw;
  height: 100vh;
  height: 100dvh;
  aspect-ratio: auto;
  border-radius: 0;
}

/* In-page fallback where the Fullscreen API is missing (iPhone Safari). */
.viewer--expanded {
  position: fixed;
  inset: 0;
  z-index: 1000;
}

.viewer__canvas,
.viewer__poster {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
}

.viewer__canvas {
  display: block;
  cursor: grab;
  outline: none;
  opacity: 0;
}

.viewer__canvas:active {
  cursor: grabbing;
}

.viewer[data-state="ready"] .viewer__canvas {
  opacity: 1;
}

.viewer__canvas:focus-visible {
  box-shadow: inset 0 0 0 3px var(--viewer-focus);
  border-radius: inherit;
}

.viewer__poster {
  object-fit: contain;
  pointer-events: none;
  transition: opacity 200ms ease-out;
}

.viewer[data-state="ready"] .viewer__poster {
  opacity: 0;
}

/* Status: loading progress and errors, centred over the poster. */
.viewer__status {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 12px;
  padding: 24px;
  text-align: center;
}

.viewer__status[hidden] {
  display: none;
}

.viewer[data-state="error"] .viewer__status {
  background: var(--viewer-scrim);
}

.viewer__message {
  max-width: 36ch;
  margin: 0;
  padding: 4px 12px;
  border-radius: var(--radius, 8px);
  background: var(--viewer-surface);
  color: var(--viewer-fg);
  font-weight: 500;
}

.viewer[data-state="error"] .viewer__message {
  padding: 0;
  background: none;
  font-size: 1rem;
}

.viewer__progress {
  width: min(240px, 60%);
  height: 8px;
  overflow: hidden;
  border: 0;
  border-radius: var(--radius-sm, 4px);
  background: var(--viewer-border);
  color: var(--viewer-fg);
  accent-color: var(--viewer-fg);
  appearance: none;
  /* A halo keeps the track legible when it sits on top of a busy poster image. */
  box-shadow: 0 0 0 4px var(--viewer-surface);
}

.viewer__progress::-webkit-progress-bar {
  background: var(--viewer-border);
}

.viewer__progress::-webkit-progress-value {
  border-radius: var(--radius-sm, 4px);
  background: var(--viewer-fg);
  transition: width 150ms ease-out;
}

.viewer__progress::-moz-progress-bar {
  border-radius: var(--radius-sm, 4px);
  background: var(--viewer-fg);
}

/* No value attribute: the amount of work is unknown, so sweep instead of filling. */
.viewer__progress:indeterminate {
  background: linear-gradient(90deg, var(--viewer-border) 0 35%, var(--viewer-fg) 35% 65%, var(--viewer-border) 65%) 0 0 / 300% 100%;
  animation: viewer-sweep 1.4s linear infinite;
}

.viewer__progress:indeterminate::-webkit-progress-bar {
  background: transparent;
}

.viewer__progress:indeterminate::-moz-progress-bar {
  background: transparent;
}

@keyframes viewer-sweep {
  from { background-position: 100% 0; }
  to { background-position: 0 0; }
}

/* Controls */
.viewer__toolbar {
  position: absolute;
  right: 8px;
  bottom: 8px;
  display: flex;
  gap: 4px;
  padding: 4px;
  border: 1px solid var(--viewer-border);
  border-radius: calc(var(--radius, 8px) + 4px);
  background: var(--viewer-surface);
}

/* In a small viewer a horizontal bar would sit on top of the model; stack it along the edge instead. */
@container (max-width: 520px) {
  .viewer__toolbar {
    flex-direction: column;
  }
}

.viewer:not([data-state="ready"]) .viewer__toolbar {
  display: none;
}

.viewer__button,
.viewer__retry {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: 44px;
  min-height: 44px;
  margin: 0;
  border: 1px solid transparent;
  border-radius: var(--radius, 8px);
  background: transparent;
  color: var(--viewer-fg);
  font: inherit;
  font-weight: 600;
  cursor: pointer;
  transition: background-color 150ms ease-out, transform 150ms ease-out;
}

.viewer__button {
  padding: 0;
}

.viewer__retry {
  padding: 0 16px;
  border-color: var(--viewer-fg);
  background: var(--viewer-fg);
  color: var(--viewer-bg-glow);
}

.viewer__retry[hidden] {
  display: none;
}

.viewer__button:hover {
  background: var(--viewer-surface-hover);
  border-color: var(--viewer-border);
}

.viewer__retry:hover {
  opacity: 0.88;
}

.viewer__button:active,
.viewer__retry:active {
  transform: scale(0.96);
}

.viewer__button:focus-visible,
.viewer__retry:focus-visible {
  outline: 3px solid var(--viewer-focus);
  outline-offset: 2px;
}

.viewer__button:disabled {
  opacity: 0.45;
  cursor: not-allowed;
}

/* Toggle that is on: inverted, so the state does not rest on colour alone. */
.viewer__button[aria-pressed="true"] {
  background: var(--viewer-fg);
  color: var(--viewer-bg-glow);
}

.viewer__icon {
  width: 20px;
  height: 20px;
  fill: none;
  stroke: currentColor;
  stroke-width: 2;
  stroke-linecap: round;
  stroke-linejoin: round;
}

@media (prefers-reduced-motion: reduce) {
  .viewer__poster,
  .viewer__button,
  .viewer__retry,
  .viewer__progress::-webkit-progress-value {
    transition: none;
  }

  .viewer__progress:indeterminate {
    animation: none;
    background: var(--viewer-muted);
  }
}
