/*
 * The interactive 3D demos: jaw motion (dark section), bite position and
 * matching. Each demo is a .lp-window with one canvas; empty placeholder
 * elements ([data-view]) mark where each 3D view is drawn, so the layout
 * below decides the views' sizes and the scripts just follow it.
 */

/* ---------- shared ---------- */

.td-viewport {
  position: relative;
  overflow: hidden;
}
.td-viewport canvas {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  display: block;
  pointer-events: none; /* the placeholders below take the pointer */
}
[data-view] { position: relative; min-width: 0; min-height: 0; }
.td-grab { cursor: grab; }
.td-grab.is-grabbing { cursor: grabbing; }

/* Small label in a corner of a view. */
.td-view-label {
  position: absolute;
  left: 10px;
  top: 8px;
  font-size: 11.5px;
  font-weight: 600;
  letter-spacing: 0.02em;
  opacity: 0.7;
}

/* Spinner text until the models are loaded; error text if loading fails. */
.td-loading {
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
  font-size: 14px;
  opacity: 0.6;
  transition: opacity 0.6s var(--lp-ease);
  pointer-events: none;
}
.is-ready .td-loading { opacity: 0; }
.is-failed .td-loading { opacity: 0.6; }

/* Pill buttons in the control bars. */
.td-chip {
  appearance: none;
  border: 1px solid currentColor;
  border-color: rgba(127, 140, 160, 0.35);
  background: transparent;
  color: inherit;
  font: inherit;
  font-size: 13px;
  font-weight: 600;
  padding: 7px 13px;
  border-radius: 999px;
  cursor: pointer;
  white-space: nowrap;
  transition: background 0.25s, border-color 0.25s, color 0.25s;
}
.td-chip:focus-visible { outline: 2px solid var(--lp-blue); outline-offset: 2px; }

/* ---------- jaw motion ---------- */

.lp-motion { overflow: hidden; }
.lp-motion::before {
  /* faint blue / violet light behind the viewer */
  content: "";
  position: absolute;
  inset: 30% -10% 0;
  background:
    radial-gradient(40% 40% at 35% 55%, rgba(59, 130, 246, 0.22), transparent 70%),
    radial-gradient(35% 35% at 70% 60%, rgba(139, 92, 246, 0.18), transparent 70%);
  filter: blur(30px);
  pointer-events: none;
}
.jm-stage {
  position: relative;
  max-width: 1180px;
  margin: clamp(40px, 7vh, 80px) auto 0;
}
.jm-window { background: #0c0f14; color: #e7ebf3; }

.jm-viewport {
  aspect-ratio: 16 / 8.6;
  display: grid;
  grid-template-columns: minmax(0, 1fr) minmax(0, 0.46fr);
  background:
    radial-gradient(70% 90% at 38% 40%, #1d2430 0%, #0c0f14 75%);
}
.jm-side {
  display: grid;
  grid-template-rows: 1fr 1fr auto;
  border-left: 1px solid rgba(255, 255, 255, 0.06);
}
.jm-side [data-view] + [data-view] { border-top: 1px solid rgba(255, 255, 255, 0.06); }

.jm-drag-hint {
  position: absolute;
  left: 16px;
  bottom: 12px;
  margin: 0;
  font-size: 12px;
  opacity: 0.45;
  pointer-events: none;
}

/* Heat-map legend (colours filled by jaw-motion-demo.js from the app palette). */
.jm-legend { padding: 10px 14px 14px; border-top: 1px solid rgba(255, 255, 255, 0.06); }
.jm-legend-bar { display: flex; height: 8px; border-radius: 4px; overflow: hidden; }
.jm-legend-bar span { flex: 1; }
.jm-legend-labels { position: relative; height: 16px; margin-top: 5px; font-size: 11px; opacity: 0.65; }
.jm-legend-labels span { position: absolute; transform: translateX(-50%); white-space: nowrap; }
.jm-legend-labels span:nth-child(1) { left: 10%; }
.jm-legend-labels span:nth-child(2) { left: 30%; }
.jm-legend-labels span:nth-child(3) { left: auto; right: 0; transform: none; }

.jm-controls {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 10px 14px;
  padding: 12px 16px;
  border-top: 1px solid rgba(255, 255, 255, 0.07);
  background: #12161d;
}
.jm-play {
  flex: none;
  width: 36px;
  height: 36px;
  border-radius: 50%;
  border: 0;
  display: grid;
  place-items: center;
  background: #fff;
  color: #0c0f14;
  cursor: pointer;
}
.jm-play svg { width: 16px; height: 16px; }
.jm-play .is-pause, .jm-play[aria-pressed="true"] .is-play { display: none; }
.jm-play[aria-pressed="true"] .is-pause { display: block; }
.jm-timeline {
  flex: 1 1 140px;
  height: 22px;
  display: flex;
  align-items: center;
  cursor: pointer;
  touch-action: none;
}
.jm-timeline-track {
  position: relative;
  width: 100%;
  height: 4px;
  border-radius: 2px;
  background: rgba(255, 255, 255, 0.14);
  overflow: hidden;
}
.jm-timeline-track span {
  position: absolute;
  inset: 0;
  transform-origin: 0 50%;
  transform: scaleX(0);
  background: linear-gradient(90deg, #f97316, #fb923c); /* the app's orange motion colour */
}
.jm-chips, .jm-toggles { display: flex; flex-wrap: wrap; gap: 6px; }
.jm-chips .td-chip[aria-pressed="true"] { background: #f97316; border-color: #f97316; color: #fff; }
.jm-toggles .td-chip[aria-pressed="true"] { background: rgba(255, 255, 255, 0.12); border-color: rgba(255, 255, 255, 0.35); }
.jm-toggles .td-chip::before {
  content: "";
  display: inline-block;
  width: 8px;
  height: 8px;
  margin-right: 7px;
  border-radius: 50%;
  background: var(--dot);
  opacity: 0.35;
  vertical-align: 1px;
}
.jm-toggles .td-chip[aria-pressed="true"]::before { opacity: 1; }

.jm-caption {
  margin: 16px 0 0;
  text-align: center;
  font-size: 13px;
  color: #7d879b;
}

/* Three short facts under the viewer. */
.jm-facts {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: clamp(20px, 3vw, 44px);
  max-width: 1180px;
  margin: clamp(44px, 7vh, 72px) auto 0;
}
.jm-facts h3 { margin: 0 0 8px; font-size: 18px; letter-spacing: -0.01em; color: #f5f7fb; }
.jm-facts p { margin: 0; font-size: 15px; line-height: 1.55; color: #a3acbf; }
.jm-facts h3::before {
  content: "";
  display: block;
  width: 28px;
  height: 3px;
  margin-bottom: 14px;
  border-radius: 2px;
  background: var(--accent, #f97316);
}

/* ---------- bite position ---------- */

.bp-window { background: #fff; }
.bp-viewport {
  aspect-ratio: 4 / 3;
  background: radial-gradient(80% 90% at 40% 40%, #ffffff 0%, #e9edf4 80%);
}
.bp-viewport [data-view="side"] { position: absolute; inset: 0; }
.bp-viewport [data-view="occlusal"] {
  position: absolute;
  right: 12px;
  bottom: 12px;
  width: 31%;
  aspect-ratio: 1 / 0.92;
  /* No background here: the canvas below paints the inset (see bite-position-demo.js). */
  border-radius: 4px;
  outline: 1px solid rgba(23, 32, 58, 0.1);
  box-shadow: 0 10px 24px -14px rgba(15, 23, 42, 0.35);
  pointer-events: none;
}
.bp-gap-legend {
  position: absolute;
  left: 10px;
  right: 10px;
  bottom: 8px;
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 10.5px;
  color: var(--lp-muted);
}
.bp-gap-legend i {
  flex: 1;
  height: 5px;
  border-radius: 3px;
  background: linear-gradient(90deg, #ff1a0d, #ff73ad, #ffad61, #fff214, #80f247, #0dc71f, #1a73ff);
}

/* Axis label and the "first contact" state. */
.bp-tag {
  position: absolute;
  right: 14px;
  top: 12px;
  display: inline-flex;
  align-items: center;
  gap: 8px;
  padding: 6px 11px;
  border-radius: 999px;
  background: rgba(20, 184, 166, 0.12);
  color: #0f766e;
  font-size: 12px;
  font-weight: 600;
  pointer-events: none;
}
.bp-tag::before { content: ""; width: 8px; height: 8px; border-radius: 50%; background: #14b8a6; }
.bp-contact-flag {
  position: absolute;
  left: 50%;
  top: 14px;
  transform: translate(-50%, -6px);
  padding: 6px 12px;
  border-radius: 8px;
  background: rgba(17, 20, 26, 0.85);
  color: #fde047; /* the app's yellow-on-dark hints */
  font-size: 12.5px;
  font-weight: 600;
  opacity: 0;
  transition: opacity 0.3s, transform 0.3s var(--lp-ease);
  pointer-events: none;
}
.is-contact .bp-contact-flag { opacity: 1; transform: translate(-50%, 0); }

.bp-controls {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto auto auto;
  align-items: center;
  gap: 10px 18px;
  padding: 14px 18px;
  border-top: 1px solid var(--lp-line);
}
.bp-slider { display: grid; gap: 6px; font-size: 12px; font-weight: 600; color: var(--lp-muted); }
.bp-slider input {
  -webkit-appearance: none;
  appearance: none;
  width: 100%;
  height: 6px;
  border-radius: 3px;
  background: linear-gradient(90deg, #14b8a6 var(--fill, 0%), rgba(23, 32, 58, 0.12) var(--fill, 0%));
  outline-offset: 6px;
}
.bp-slider input::-webkit-slider-thumb {
  -webkit-appearance: none;
  width: 20px;
  height: 20px;
  border-radius: 50%;
  background: #fff;
  border: 2px solid #14b8a6;
  box-shadow: 0 2px 8px rgba(15, 23, 42, 0.25);
  cursor: grab;
}
.bp-slider input::-moz-range-thumb {
  width: 18px;
  height: 18px;
  border-radius: 50%;
  background: #fff;
  border: 2px solid #14b8a6;
  cursor: grab;
}
.bp-readout { display: grid; gap: 2px; min-width: 76px; }
.bp-readout span { font-size: 11.5px; color: var(--lp-muted); }
.bp-readout strong { font-size: 17px; font-variant-numeric: tabular-nums; letter-spacing: -0.01em; }
.bp-first-contact { color: #0f766e; border-color: rgba(20, 184, 166, 0.45); }
.bp-first-contact:hover { background: rgba(20, 184, 166, 0.1); }

/* ---------- matching ---------- */

.mt-viewport {
  aspect-ratio: 16 / 11;
  background: radial-gradient(80% 90% at 50% 45%, #ffffff 0%, #e8ecf3 80%);
}
/* Hint in the app's style: yellow text on a dark capsule, top centre. */
.mt-hint {
  position: absolute;
  left: 50%;
  top: 14px;
  transform: translateX(-50%);
  padding: 7px 14px;
  border-radius: 8px;
  background: rgba(17, 20, 26, 0.85);
  color: #fde047;
  font-size: 13px;
  font-weight: 600;
  white-space: nowrap;
  pointer-events: none;
}
.mt-hint:empty { display: none; }
.mt-badge {
  position: absolute;
  right: 14px;
  bottom: 14px;
  display: inline-flex;
  align-items: center;
  gap: 8px;
  padding: 7px 12px;
  border-radius: 999px;
  background: #fff;
  box-shadow: 0 8px 20px -10px rgba(15, 23, 42, 0.4);
  font-size: 12.5px;
  font-weight: 600;
  opacity: 0;
  transform: translateY(6px);
  transition: opacity 0.35s, transform 0.35s var(--lp-ease);
  pointer-events: none;
}
.mt-badge::before { content: ""; width: 10px; height: 10px; border-radius: 50%; background: #22c55e; }
.is-matched .mt-badge { opacity: 1; transform: none; }

/* ---------- responsive ---------- */

@media (max-width: 920px) {
  .jm-viewport {
    aspect-ratio: 3 / 4.3;
    grid-template-columns: 1fr 1fr;
    grid-template-rows: minmax(0, 1fr) auto;
  }
  .jm-viewport [data-view="main"] { grid-column: 1 / -1; }
  .jm-side {
    grid-column: 1 / -1;
    grid-template-columns: 1fr 1fr;
    grid-template-rows: auto auto;
    border-left: 0;
    border-top: 1px solid rgba(255, 255, 255, 0.06);
  }
  .jm-side [data-view] { aspect-ratio: 1 / 0.8; }
  .jm-side [data-view] + [data-view] { border-top: 0; border-left: 1px solid rgba(255, 255, 255, 0.06); }
  .jm-legend { grid-column: 1 / -1; }
  .jm-drag-hint { display: none; }
  .jm-facts { grid-template-columns: 1fr; }
  .bp-controls { grid-template-columns: 1fr 1fr; }
  .bp-slider { grid-column: 1 / -1; }
}

@media (max-width: 560px) {
  .jm-controls { padding: 10px 12px; }
  .jm-chips, .jm-toggles { width: 100%; }
  .bp-viewport { aspect-ratio: 1 / 1; }
  .bp-viewport [data-view="occlusal"] { width: 40%; }
}
