/* ============================================================
   css/motility-3d/motility-3d.css — 3D motility lab (eye-movements.html)
   Scoped under .m3d / .motility-*; tokens from css/shared/design-tokens.css.
   The stage backdrop matches the 3D Anatomy page (sage studio) so the
   ivory sclera separates from it.
   ============================================================ */

.eye-movements-page {
  --m3d-ink: #14181a;
  --m3d-muted: #535c57;
  --m3d-line: #e3e6dc;
  --m3d-line-strong: #cdd2c6;
  --m3d-panel: #ffffff;
  --m3d-panel-2: #f8f9f5;
  --m3d-teal: var(--accent, #1f4b4c);
  --m3d-teal-soft: #e4eeee;
  --m3d-contract: #e8472b;
  --m3d-relax: #3f8cff;
  --m3d-tone: #b3453a;
  --m3d-silent: #8d9398;
  --m3d-gold: #c9920e;
}

/* ── Model switch ─────────────────────────────────────────── */
.motility-model-switch {
  display: flex; gap: 6px; justify-content: center;
  margin: 14px auto 10px; padding: 5px; width: fit-content; max-width: calc(100% - 24px);
  background: var(--surface-sunken, #f1f3f5); border: 1px solid var(--border, #dee2e6); border-radius: 14px;
}
.motility-model-switch button {
  display: grid; gap: 1px; text-align: center; min-width: 150px;
  padding: 8px 18px; border: 0; border-radius: 10px; background: transparent; color: var(--text-soft, #495057);
  font: inherit; cursor: pointer; transition: background .18s, color .18s, box-shadow .18s;
}
.motility-model-switch button span { font-weight: 700; font-size: 14px; }
.motility-model-switch button small { font-size: 11px; opacity: .75; }
.motility-model-switch button:hover { color: var(--text, #212529); }
.motility-model-switch button.active { background: var(--surface, #fff); color: var(--m3d-teal); box-shadow: 0 1px 3px rgba(0,0,0,.12); }
.motility-model-switch button:focus-visible { outline: 2px solid var(--m3d-teal); outline-offset: 2px; }

.motility3d-root { width: 100%; padding: 0 20px 48px; box-sizing: border-box; }
.motility3d-root.is-loading { min-height: 60vh; background: radial-gradient(circle at 50% 40%, #e4eae6, #d3dcd8 60%, #b9c6c1); border-radius: 18px; }
html.app-embedded .motility-model-switch { margin-top: 8px; }

/* ── Lab shell ────────────────────────────────────────────── */
.m3d { max-width: 1480px; margin: 0 auto; color: var(--m3d-ink); }
.m3d-head { display: flex; align-items: end; justify-content: space-between; gap: 12px; margin: 4px 2px 12px; }
.m3d-title { margin: 0; font-size: 22px; letter-spacing: -.01em; }
.m3d-sub { margin: 2px 0 0; color: var(--m3d-muted); font-size: 13px; }

.m3d-main { display: grid; grid-template-columns: minmax(0, 1fr) 380px; gap: 16px; align-items: start; }
.m3d-stagecol { min-width: 0; position: sticky; top: 8px; }

.m3d-viewport {
  position: relative; height: min(72vh, 720px); min-height: 420px; overflow: hidden;
  border-radius: 18px; border: 1px solid var(--m3d-line);
  background: radial-gradient(ellipse at 50% 42%, #e4eae6 0%, #d3dcd8 58%, #b9c6c1 100%);
  touch-action: none;
}
.m3d-canvas { position: absolute; inset: 0; width: 100%; height: 100%; display: block; cursor: grab; }
.m3d-canvas:active { cursor: grabbing; }
.m3d-loading, .m3d-fallback { position: absolute; inset: 0; display: grid; place-items: center; color: var(--m3d-muted); font-size: 14px; padding: 24px; text-align: center; }

/* Floating chrome */
.m3d-chrome { position: absolute; inset: 0; pointer-events: none; }
.m3d-chrome > * { pointer-events: auto; }
.m3d-tools { position: absolute; display: flex; flex-wrap: wrap; gap: 6px; }
.m3d-tools-views { top: 12px; inset-inline-start: 12px; }
.m3d-tools-layers { top: 12px; inset-inline-end: 12px; display: block; background: rgba(255,255,255,.88); backdrop-filter: blur(10px); border: 1px solid rgba(20,24,26,.1); border-radius: 12px; font-size: 12px; }
.m3d-tools-layers summary { cursor: pointer; padding: 7px 12px; font-weight: 700; list-style: none; }
.m3d-tools-layers summary::-webkit-details-marker { display: none; }
.m3d-tools-layers summary::after { content: ' ▾'; opacity: .6; }
.m3d-layer-list { display: grid; gap: 4px; padding: 2px 12px 10px; min-width: 170px; }

.m3d-seg { display: inline-flex; gap: 2px; padding: 3px; background: rgba(255,255,255,.88); backdrop-filter: blur(10px); border: 1px solid rgba(20,24,26,.1); border-radius: 10px; }
.m3d-side .m3d-seg { background: var(--m3d-panel-2); backdrop-filter: none; flex-wrap: wrap; }
.m3d-seg button { border: 0; background: transparent; padding: 5px 10px; border-radius: 7px; font: inherit; font-size: 12px; font-weight: 600; color: var(--m3d-muted); cursor: pointer; }
.m3d-seg button.on { background: var(--m3d-teal); color: #fff; }
.m3d-seg button:hover:not(.on) { background: rgba(31,75,76,.08); color: var(--m3d-ink); }
.m3d-seg-sm button { padding: 4px 8px; font-size: 11px; }

.m3d-check { display: flex; align-items: center; gap: 7px; font-size: 12px; cursor: pointer; }
.m3d-check input { accent-color: var(--m3d-teal); margin: 0; }

.m3d-legend { position: absolute; bottom: 12px; inset-inline-start: 12px; display: flex; flex-wrap: wrap; gap: 10px; margin: 0; padding: 6px 10px; list-style: none; font-size: 11px; font-weight: 600; max-width: 44%; background: rgba(255,255,255,.86); backdrop-filter: blur(10px); border-radius: 10px; border: 1px solid rgba(20,24,26,.08); }
.m3d-legend li { display: flex; align-items: center; gap: 5px; }
.m3d-legend li::before { content: ''; width: 18px; height: 7px; border-radius: 4px; background: var(--c); }
.m3d-legend [data-k="contracting"] { --c: linear-gradient(90deg, #ff3d1f, #ff9a3c); }
.m3d-legend [data-k="relaxing"] { --c: var(--m3d-relax); }
.m3d-legend [data-k="tone"] { --c: var(--m3d-tone); }
.m3d-legend [data-k="silent"] { --c: var(--m3d-silent); }

.m3d-readout { position: absolute; bottom: 12px; inset-inline-end: 12px; display: grid; gap: 2px; padding: 7px 11px; font: 600 11px/1.45 'IBM Plex Mono', ui-monospace, monospace; background: rgba(255,255,255,.88); backdrop-filter: blur(10px); border-radius: 10px; border: 1px solid rgba(20,24,26,.08); max-width: 52%; }
.m3d[dir="rtl"] .m3d-readout { font-family: inherit; }
.m3d-readout b { color: var(--m3d-teal); margin-inline-end: 4px; }
.m3d-ro-dev { border-top: 1px solid var(--m3d-line); padding-top: 3px; margin-top: 2px; font-family: inherit; }

/* Labels pinned to the muscles */
.m3d-label-layer { position: absolute; inset: 0; pointer-events: none; overflow: hidden; }
.m3d-mlabel { position: absolute; left: 0; top: 0; display: inline-flex; gap: 4px; align-items: baseline; padding: 2px 6px; border-radius: 999px; font: 700 10px/1.3 system-ui, sans-serif; white-space: nowrap; background: rgba(255,255,255,.9); color: #3b2a26; border: 1px solid rgba(0,0,0,.08); box-shadow: 0 1px 3px rgba(0,0,0,.12); transition: background .2s, color .2s; direction: ltr; }
.m3d-mlabel i { font-style: normal; font-weight: 600; opacity: .85; }
.m3d-mlabel[data-activity="tone"] { opacity: .72; transform-origin: center; }
.m3d-mlabel[data-activity="contracting"] { background: #ff4a2b; color: #fff; border-color: transparent; }
.m3d-mlabel[data-activity="relaxing"] { background: #dbe8ff; color: #1c4fa3; }
.m3d-mlabel[data-activity="silent"] { background: #e3e5e7; color: #5b6166; text-decoration: line-through; }
.m3d-mlabel[data-muscle="lps"] { font-weight: 600; opacity: .7; }
.m3d-mlabel[hidden], .m3d-yoketag[hidden], .m3d-ficktag[hidden] { display: none; }
.m3d-yoketag { position: absolute; left: 0; top: 0; padding: 3px 9px; border-radius: 999px; background: #fff6dc; color: #7a5600; border: 1px solid #e8c766; font: 700 11px/1.3 system-ui, sans-serif; white-space: nowrap; }
.m3d-ficktag { position: absolute; left: 0; top: 0; font: 800 13px/1 system-ui, sans-serif; text-shadow: 0 0 4px #fff, 0 0 2px #fff; }
.m3d-fick-X { color: #e53935; } .m3d-fick-Y { color: #1e88e5; } .m3d-fick-Z { color: #2e7d32; }

/* ── Side panel ───────────────────────────────────────────── */
.m3d-side { background: var(--m3d-panel); border: 1px solid var(--m3d-line); border-radius: 18px; padding: 12px 14px 16px; font-size: 13px; max-height: calc(100vh - 24px); overflow: auto; position: sticky; top: 8px; }
.m3d-tabs { display: grid; grid-template-columns: repeat(4, 1fr); gap: 3px; padding: 3px; margin-bottom: 12px; background: var(--m3d-panel-2); border: 1px solid var(--m3d-line); border-radius: 12px; position: sticky; top: -12px; z-index: 1; }
.m3d-tabs button { border: 0; background: transparent; padding: 8px 4px; border-radius: 9px; font: inherit; font-weight: 700; font-size: 13px; color: var(--m3d-muted); cursor: pointer; }
.m3d-tabs button[aria-selected="true"] { background: var(--m3d-teal); color: #fff; }
.m3d-side h3 { margin: 2px 0 6px; font-size: 15px; }
.m3d-side h4 { margin: 0 0 6px; font-size: 13px; }
.m3d-hint { margin: 0 0 10px; color: var(--m3d-muted); font-size: 12px; line-height: 1.5; }
.m3d-muted { color: var(--m3d-muted); }
.m3d-row { display: flex; align-items: center; justify-content: space-between; gap: 8px; margin: 10px 0; }
.m3d-wrap { flex-wrap: wrap; }
.m3d-lab { font-weight: 700; font-size: 12px; }
.m3d-actions { display: flex; flex-wrap: wrap; align-items: center; gap: 8px; margin: 12px 0; }
.m3d-btn { border: 1px solid var(--m3d-line-strong); background: #fff; color: var(--m3d-ink); padding: 8px 14px; border-radius: 10px; font: inherit; font-weight: 700; font-size: 12.5px; cursor: pointer; }
.m3d-btn:hover { border-color: var(--m3d-teal); }
.m3d-btn.primary { background: var(--m3d-teal); border-color: var(--m3d-teal); color: #fff; }
.m3d-btn.on { background: var(--m3d-teal-soft); border-color: var(--m3d-teal); color: var(--m3d-teal); }
.m3d-btn.sm { padding: 5px 10px; font-size: 11.5px; }
.m3d-btn:focus-visible, .m3d-seg button:focus-visible, .m3d-tabs button:focus-visible, .m3d-gaze-grid button:focus-visible, .m3d-chips button:focus-visible { outline: 2px solid var(--m3d-teal); outline-offset: 2px; }

.m3d-gaze-grid { display: grid; grid-template-columns: repeat(3, 1fr); gap: 5px; }
.m3d-gaze-grid button { display: grid; gap: 2px; padding: 8px 4px; min-height: 52px; border: 1px solid var(--m3d-line); background: var(--m3d-panel-2); border-radius: 10px; font: inherit; font-size: 11.5px; font-weight: 700; color: var(--m3d-ink); cursor: pointer; text-align: center; }
.m3d-gaze-grid button small { font-size: 10px; font-weight: 600; color: var(--m3d-muted); direction: ltr; }
.m3d-gaze-grid button:hover { border-color: var(--m3d-teal); }
.m3d-gaze-grid button.on { background: var(--m3d-teal); border-color: var(--m3d-teal); color: #fff; }
.m3d-gaze-grid button.on small { color: rgba(255,255,255,.8); }
.m3d-gaze-grid-sm button { min-height: 34px; font-size: 10.5px; }
.m3d-grid-note { margin-top: 6px; font-size: 11px; }

.m3d-pad { position: relative; height: 130px; margin: 6px 0 4px; border-radius: 12px; border: 1px solid var(--m3d-line); background: radial-gradient(circle at 50% 50%, #fff, #eef2ef); cursor: crosshair; touch-action: none; }
.m3d-pad-cross { position: absolute; inset: 0; background: linear-gradient(var(--m3d-line-strong), var(--m3d-line-strong)) center / 1px 100% no-repeat, linear-gradient(var(--m3d-line-strong), var(--m3d-line-strong)) center / 100% 1px no-repeat; }
.m3d-pad-dot { position: absolute; width: 16px; height: 16px; margin: -8px 0 0 -8px; border-radius: 50%; background: var(--m3d-gold); box-shadow: 0 0 0 4px rgba(201,146,14,.25); left: 50%; top: 50%; }
.m3d-pad:focus-visible { outline: 2px solid var(--m3d-teal); outline-offset: 2px; }

.m3d-range { display: grid; gap: 4px; margin: 10px 0; font-size: 12px; font-weight: 700; }
.m3d-range span { display: flex; justify-content: space-between; }
.m3d-range output { font-family: 'IBM Plex Mono', ui-monospace, monospace; color: var(--m3d-teal); }
.m3d-range input { accent-color: var(--m3d-teal); width: 100%; }

.m3d-chips { display: grid; grid-template-columns: repeat(3, 1fr); gap: 5px; margin-bottom: 8px; }
.m3d-chips button { display: grid; grid-template-columns: auto 1fr; align-items: center; column-gap: 6px; padding: 7px 8px; border: 1px solid var(--m3d-line); border-radius: 10px; background: var(--m3d-panel-2); font: inherit; font-weight: 800; font-size: 12px; cursor: pointer; text-align: start; }
.m3d-chips button i { width: 9px; height: 9px; border-radius: 50%; background: var(--chip); grid-row: span 2; }
.m3d-chips button small { font-size: 10px; font-weight: 600; line-height: 1.25; color: var(--m3d-muted); grid-column: 2; }
.m3d-chips button.on { border-color: var(--chip); box-shadow: inset 0 0 0 1px var(--chip); background: #fff; }

.m3d-card { border: 1px solid var(--m3d-line); background: var(--m3d-panel-2); border-radius: 12px; padding: 10px 12px; margin: 10px 0; }
.m3d-card p { margin: 0 0 6px; line-height: 1.5; font-size: 12.5px; }
.m3d-card small { font-weight: 600; color: var(--m3d-muted); }
.m3d-cue { border-color: #f1d78a; background: #fffaeb; }
.m3d-pearl { border-color: #c9dcdb; background: var(--m3d-teal-soft); }
.m3d-dec { display: grid; grid-template-columns: 78px 1fr 38px; align-items: center; gap: 8px; margin: 5px 0; font-size: 11.5px; font-weight: 700; }
.m3d-dec > div { height: 8px; border-radius: 4px; background: #e6e9e3; overflow: hidden; }
.m3d-dec i { display: block; height: 100%; border-radius: 4px; transition: width .25s; }
.m3d-dec.v i { background: #d94b59; } .m3d-dec.t i { background: #8e44ad; } .m3d-dec.h i { background: #2a80b9; }
.m3d-dec b { text-align: end; font-family: 'IBM Plex Mono', ui-monospace, monospace; font-size: 11px; }
.m3d-facts { display: grid; grid-template-columns: max-content 1fr; gap: 5px 10px; margin: 10px 0; font-size: 12px; }
.m3d-facts dt { font-weight: 700; color: var(--m3d-muted); }
.m3d-facts dd { margin: 0; line-height: 1.45; }

.m3d-law { border: 1px solid var(--m3d-line); border-radius: 12px; padding: 10px 12px; margin: 10px 0; background: var(--m3d-panel-2); }
.m3d-law header { display: flex; align-items: center; justify-content: space-between; gap: 8px; margin-bottom: 6px; }
.m3d-law h4 { margin: 0; }
.m3d-law p { margin: 4px 0; font-size: 12.5px; line-height: 1.5; }
.m3d-dev { margin-top: 10px; padding: 8px 10px; border-radius: 10px; background: #fff6dc; border: 1px solid #efd58c; font-weight: 700; font-size: 12.5px; }

/* ── Below the model ──────────────────────────────────────── */
.m3d-below { display: grid; grid-template-columns: minmax(0, 1fr) minmax(0, 1.25fr); gap: 16px; margin-top: 16px; }
.m3d-caption { background: var(--m3d-panel); border: 1px solid var(--m3d-line); border-inline-start: 4px solid var(--m3d-teal); border-radius: 14px; padding: 12px 16px; min-height: 96px; }
.m3d-caption h3 { margin: 0 0 6px; font-size: 15px; color: var(--m3d-teal); }
.m3d-caption p { margin: 0 0 6px; font-size: 13.5px; line-height: 1.6; }
.m3d-bars { background: var(--m3d-panel); border: 1px solid var(--m3d-line); border-radius: 14px; padding: 12px 16px; }
.m3d-bars h3 { margin: 0; font-size: 15px; }
.m3d-bar-grid { display: grid; grid-template-columns: minmax(120px, max-content) 1fr 1fr; gap: 6px 14px; align-items: center; }
.m3d-bh { font-size: 11px; font-weight: 800; color: var(--m3d-muted); text-transform: uppercase; letter-spacing: .04em; }
.m3d-bm { display: grid; grid-template-columns: auto auto; column-gap: 6px; align-items: center; justify-content: start; }
.m3d-bm i { width: 9px; height: 9px; border-radius: 50%; background: var(--chip); grid-row: span 2; }
.m3d-bm b { font-size: 12px; } .m3d-bm small { font-size: 10.5px; color: var(--m3d-muted); grid-column: 2; }
.m3d-bc { display: grid; grid-template-columns: 1fr 44px 58px; align-items: center; gap: 6px; font-size: 11px; direction: ltr; }
.m3d-track { position: relative; height: 10px; background: #edf0ea; border-radius: 5px; overflow: hidden; }
.m3d-fill { position: absolute; inset-block: 0; left: 0; border-radius: 5px; background: var(--m3d-tone); transition: width .08s linear, background .2s; }
.m3d-tone { position: absolute; top: -2px; bottom: -2px; width: 2px; background: #1b2426; opacity: .55; }
.m3d-bc[data-activity="contracting"] .m3d-fill { background: linear-gradient(90deg, #ff3d1f, #ff9a3c); }
.m3d-bc[data-activity="relaxing"] .m3d-fill { background: var(--m3d-relax); }
.m3d-bc[data-activity="silent"] .m3d-fill { background: var(--m3d-silent); }
.m3d-bc output { font-family: 'IBM Plex Mono', ui-monospace, monospace; font-weight: 700; text-align: end; }
.m3d-bc em { font-style: normal; color: var(--m3d-muted); font-family: 'IBM Plex Mono', ui-monospace, monospace; text-align: end; }
.m3d-note { margin: 10px 0 0; font-size: 11px; color: var(--m3d-muted); line-height: 1.5; }

/* ── Responsive ───────────────────────────────────────────── */
@media (max-width: 1100px) {
  .m3d-main { grid-template-columns: minmax(0, 1fr) 330px; }
  .m3d-below { grid-template-columns: minmax(0, 1fr); }
}
@media (max-width: 860px) {
  .motility3d-root { padding: 0 10px 32px; }
  .m3d-main { grid-template-columns: minmax(0, 1fr); }
  .m3d-stagecol { position: sticky; top: 0; z-index: 2; }
  .m3d-viewport { height: 46vh; min-height: 300px; border-radius: 14px; }
  .m3d-side { position: static; max-height: none; }
  .m3d-readout { display: none; }
  .m3d-legend { gap: 4px 8px; font-size: 10px; padding: 4px 8px; bottom: 8px; inset-inline-start: 8px; max-width: calc(100% - 16px); }
  .m3d-legend li::before { width: 12px; height: 6px; }
  .m3d-tools-views { top: 8px; inset-inline-start: 8px; }
  .m3d-tools-layers { top: 48px; inset-inline-end: 8px; }
  .m3d-title { font-size: 18px; }
  .m3d-bar-grid { grid-template-columns: minmax(64px, max-content) 1fr 1fr; gap: 6px 8px; }
  .m3d-bm small { display: none; }
  .m3d-bc { grid-template-columns: 1fr 40px; }
  .m3d-bc em { display: none; }
  .motility-model-switch button { min-width: 0; padding: 7px 12px; }
}
@media (max-width: 480px) {
  .m3d-seg button { padding: 5px 7px; font-size: 11px; }
  .m3d-mlabel i { display: none; }
}
@media (prefers-reduced-motion: reduce) {
  .m3d-fill, .m3d-dec i { transition: none; }
}

.m3d-near { font-weight: 600; font-size: 11px; color: var(--m3d-muted); }

/* The page opens on the 3D model: keep the 2D diagram out of the first paint
   until it is actually chosen (no flash while the modules boot). */
.eye-movements-page:not(.motility-2d-active) #tabContentAnatomy { display: none; }
