/* ==========================================================================
   Structure viewer (js/viewer) – structure and layout only.
   Colours, type, spacing, radii and motion come exclusively from the contract tokens (css/tokens.css).
   The canvases read --viewer-bg, --line, --line-strong, --ink-3, --accent and --font-mono at runtime.
   Mobile-first: base = phone (< 720px), then tablet (>= 720px) and desktop (>= 1080px).
   ========================================================================== */

.mcv {
  position: relative;
  display: flex;
  flex-direction: column;
  width: 100%;
  min-width: 0;
  background: var(--surface);
  color: var(--ink);
  font-family: var(--font);
  font-size: var(--fs-sm);
  border: 0;
  border-radius: var(--r-2);
  overflow: hidden;
}
.mcv [hidden] { display: none !important; }
.mcv *, .mcv *::before, .mcv *::after { box-sizing: border-box; }

/* fullscreen (native) and pseudo-fullscreen fallback (iOS Safari): dynamic viewport height */
.mcv:fullscreen { width: 100vw; height: 100dvh; border: 0; border-radius: 0; }
.mcv.mcv-max { position: fixed; inset: 0; z-index: 300; width: auto; height: 100dvh; border: 0; border-radius: 0; }
.mcv:fullscreen .mcv-stage, .mcv.mcv-max .mcv-stage { flex: 1 1 auto; height: auto; }

.mcv-sr {
  position: absolute !important; width: 1px; height: 1px; padding: 0; margin: -1px;
  overflow: hidden; clip: rect(0 0 0 0); clip-path: inset(50%); white-space: nowrap; border: 0;
}
.mcv-icon { width: 18px; height: 18px; flex: none; display: block; }
.mcv-spacer { flex: 1 1 0; min-width: 0; }

/* ---------- buttons ---------- */
.mcv-btn {
  display: inline-flex; align-items: center; justify-content: center; gap: var(--sp-1);
  min-height: 36px; min-width: 36px; padding: var(--sp-1) var(--sp-2);
  font: inherit; font-size: var(--fs-sm); line-height: 1.2;
  color: var(--ink-2); background: transparent;
  border: 1px solid transparent; border-radius: var(--r-1);
  cursor: pointer; touch-action: manipulation; -webkit-tap-highlight-color: transparent;
  transition: background-color var(--dur-1) var(--ease-out), color var(--dur-1) var(--ease-out), border-color var(--dur-1) var(--ease-out);
}
.mcv-btn:disabled { opacity: .45; cursor: default; }
.mcv-btn[aria-pressed="true"] { color: var(--accent-ink); background: var(--accent-soft); border-color: var(--accent); }
.mcv-btn-primary { color: var(--accent-ink); background: var(--accent); border-color: var(--accent); }
@media (hover: hover) and (pointer: fine) {
  .mcv-btn:not(:disabled):hover { color: var(--ink); background: var(--surface-2); }
  .mcv-btn[aria-pressed="true"]:not(:disabled):hover { color: var(--accent-ink); background: var(--accent-soft); }
  .mcv-btn-primary:not(:disabled):hover { color: var(--accent-ink); background: var(--accent); }
}
.mcv :focus-visible { outline: 2px solid var(--focus); outline-offset: 2px; }
.mcv-stage:focus-visible { outline-offset: -2px; }
.mcv-lbl { display: none; } /* phone: icon-only toolbar buttons (labelled via aria-label) */

/* ---------- toolbar ---------- */
.mcv-toolbar {
  display: flex; flex-wrap: wrap; align-items: center; gap: var(--sp-1);
  padding: var(--sp-1); min-width: 0;
  border-bottom: 0; background: var(--surface);
}
.mcv-modes {
  display: inline-flex; gap: 2px; padding: 2px;
  background: var(--surface-2); border: 0; border-radius: var(--r-1);
}
.mcv-tools { display: inline-flex; flex-wrap: wrap; gap: var(--sp-1); }

/* ---------- stage ---------- */
.mcv-stage {
  position: relative; flex: none; height: min(360px, 62dvh); min-height: 220px;
  background: var(--viewer-bg); overflow: hidden;
}
.mcv-gl, .mcv-2d { position: absolute; inset: 0; }
.mcv-gl-canvas, .mcv-2d-canvas { display: block; width: 100%; height: 100%; touch-action: none; user-select: none; -webkit-user-select: none; }
.mcv-2d-canvas { cursor: crosshair; }

.mcv-help {
  position: absolute; left: var(--sp-2); right: var(--sp-2); bottom: var(--sp-2);
  font-size: var(--fs-xs); color: var(--ink-3); pointer-events: none;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}

/* progress while the 3D model is meshed in slices */
.mcv-progress { position: absolute; left: 0; right: 0; top: 0; z-index: 6; height: 3px; background: var(--line); pointer-events: none; }
.mcv-progress-bar { height: 100%; width: 100%; background: var(--accent); transform-origin: 0 50%; transform: scaleX(0); transition: transform var(--dur-2) var(--ease-out); }

/* ---------- tooltip ---------- */
.mcv-tip {
  position: absolute; left: 0; top: 0; z-index: 7; pointer-events: none;
  display: grid; gap: 2px; max-width: min(260px, calc(100% - var(--sp-4)));
  padding: var(--sp-1) var(--sp-2);
  background: var(--surface); color: var(--ink);
  border: 0; box-shadow: inset 0 -3px 0 var(--line-strong); font-size: var(--fs-sm);
}
.mcv-tip strong { font-weight: 600; }
.mcv-tip-coords { font-family: var(--font-mono); font-size: var(--fs-xs); color: var(--accent); }
.mcv-tip-props { font-family: var(--font-mono); font-size: var(--fs-xs); color: var(--ink-2); overflow-wrap: anywhere; }

/* ---------- legend (phone: overlay covering the view) ---------- */
.mcv-legend {
  position: absolute; inset: var(--sp-1); z-index: 8;
  display: flex; flex-direction: column; min-height: 0;
  background: var(--surface); color: var(--ink);
  border: 0; border-radius: var(--r-2);
}
.mcv-legend-head { display: flex; align-items: center; gap: var(--sp-1); padding: var(--sp-1) var(--sp-1) 0 var(--sp-3); min-height: 44px; }
.mcv-legend-title { margin: 0; font-size: var(--fs-sm); font-weight: 600; }
.mcv-legend-hint { margin: 0 var(--sp-3) var(--sp-2); font-size: var(--fs-xs); color: var(--ink-3); }
.mcv-legend-list { list-style: none; margin: 0; padding: 0 var(--sp-1) var(--sp-1); overflow-y: auto; flex: 1 1 auto; min-height: 0; overscroll-behavior: contain; }
.mcv-legend-item {
  display: flex; align-items: center; gap: var(--sp-2); width: 100%; min-height: 40px;
  padding: var(--sp-1) var(--sp-2); font: inherit; font-size: var(--fs-sm); text-align: left;
  color: var(--ink); background: transparent; border: 1px solid transparent; border-radius: var(--r-1); cursor: pointer;
  transition: background-color var(--dur-1) var(--ease-out), border-color var(--dur-1) var(--ease-out);
}
.mcv-legend-item[aria-pressed="true"] { background: var(--accent-soft); border-color: var(--accent); }
@media (hover: hover) and (pointer: fine) {
  .mcv-legend-item:hover { background: var(--surface-2); }
  .mcv-legend-item[aria-pressed="true"]:hover { background: var(--accent-soft); }
}
.mcv-swatch { display: block; width: 20px; height: 20px; flex: none; image-rendering: pixelated; border: 0; background: var(--surface-2); }
.mcv-legend-name { flex: 1 1 auto; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.mcv-legend-count { font-family: var(--font-mono); font-size: var(--fs-xs); font-variant-numeric: tabular-nums; color: var(--ink-2); }

/* ---------- overlays ---------- */
.mcv-msg {
  position: absolute; inset: 0; z-index: 9; display: grid; place-items: center; padding: var(--sp-4);
  background: color-mix(in srgb, var(--viewer-bg) 90%, transparent); color: var(--ink);
}
.mcv-msg-box { max-width: 440px; display: grid; gap: var(--sp-2); justify-items: center; text-align: center; }
.mcv-msg-title { margin: 0; font-size: var(--fs-md); font-weight: 600; }
.mcv-msg-detail { margin: 0; font-size: var(--fs-sm); color: var(--ink-2); }
.mcv-msg.is-error .mcv-msg-title { color: var(--danger); }
.mcv-msg-actions { display: flex; flex-wrap: wrap; justify-content: center; gap: var(--sp-2); margin-top: var(--sp-1); }
.mcv-msg-actions .mcv-btn { border-color: var(--line-strong); }
.mcv-spinner { width: 28px; height: 28px; border: 3px solid var(--line-strong); border-top-color: var(--accent); animation: mcv-spin .8s linear infinite; }
@keyframes mcv-spin { to { transform: rotate(360deg); } }

.mcv-drop {
  position: absolute; inset: var(--sp-2); z-index: 10; display: grid; place-items: center; text-align: center; pointer-events: none;
  padding: var(--sp-4); color: var(--ink);
  background: color-mix(in srgb, var(--viewer-bg) 92%, transparent);
  border: 2px dashed var(--accent); border-radius: var(--r-2);
}
.mcv-drop .mcv-icon { width: 32px; height: 32px; margin: 0 auto; color: var(--accent); }
.mcv-drop p { margin: var(--sp-2) 0 0; }
.mcv-hint { font-size: var(--fs-xs); color: var(--ink-2); }

.mcv-notice {
  position: absolute; top: var(--sp-2); left: var(--sp-2); right: var(--sp-2); z-index: 9;
  display: flex; align-items: flex-start; gap: var(--sp-2); padding: var(--sp-2) var(--sp-2) var(--sp-2) var(--sp-3);
  font-size: var(--fs-sm); color: var(--ink); background: var(--warn-soft);
  border: 1px solid var(--warn); border-radius: var(--r-1);
}
.mcv-notice > .mcv-icon { margin-top: 2px; color: var(--warn); }
.mcv-notice > span { flex: 1 1 auto; min-width: 0; }
.mcv-notice-close { margin: calc(var(--sp-1) * -1) 0; }

/* ---------- controls (layer / slice) ---------- */
.mcv-controls {
  display: flex; flex-wrap: wrap; align-items: center; gap: var(--sp-1) var(--sp-2);
  padding: var(--sp-1) var(--sp-2); min-width: 0;
  border-top: 0; background: var(--surface);
}
.mcv-stepper { display: flex; align-items: center; gap: var(--sp-1); flex: 1 1 100%; min-width: 0; }
.mcv-range { flex: 1 1 auto; min-width: 0; height: 36px; margin: 0; accent-color: var(--accent); cursor: pointer; touch-action: manipulation; }
.mcv-out { flex: 1 1 auto; min-width: 0; font-family: var(--font-mono); font-size: var(--fs-xs); font-variant-numeric: tabular-nums; color: var(--ink-2); }
.mcv-upto { display: inline-flex; align-items: center; gap: var(--sp-2); min-height: 36px; font-size: var(--fs-sm); color: var(--ink-2); cursor: pointer; }
.mcv-check { width: 18px; height: 18px; margin: 0; accent-color: var(--accent); }
.mcv-axis-wrap { display: inline-flex; flex: 1 1 auto; min-width: 0; }
.mcv-axis {
  width: 100%; min-height: 36px; padding: var(--sp-1) var(--sp-2);
  font: inherit; font-size: var(--fs-sm); color: var(--ink); background: var(--surface-2);
  border: 0; box-shadow: inset 0 -3px 0 var(--line-strong);
}

/* ---------- info line ---------- */
.mcv-info {
  display: flex; flex-wrap: wrap; align-items: center; gap: var(--sp-1) var(--sp-2);
  padding: var(--sp-1) var(--sp-2); min-width: 0;
  border-top: 0; font-size: var(--fs-sm);
}
.mcv-info:empty { display: none; }
.mcv-name { max-width: 100%; font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.mcv-chip {
  display: inline-flex; align-items: center; gap: var(--sp-1); padding: 2px var(--sp-2);
  font-family: var(--font-mono); font-size: var(--fs-xs); font-variant-numeric: tabular-nums;
  color: var(--ink-2); background: var(--surface-2); border: 0; border-radius: var(--r-1);
}
.mcv-chip .mcv-icon { width: 14px; height: 14px; }
.mcv-chip-demo { color: var(--info); background: var(--info-soft); border-color: var(--info); }
.mcv-chip-local { color: var(--ok); background: var(--ok-soft); border-color: var(--ok); }
.mcv-note { display: none; font-size: var(--fs-xs); color: var(--ink-3); }

/* ---------- touch: 44px targets ---------- */
@media (pointer: coarse) {
  .mcv-btn, .mcv-legend-item, .mcv-upto, .mcv-axis { min-height: 44px; }
  .mcv-btn { min-width: 44px; }
  .mcv-range { height: 44px; }
  .mcv-check { width: 22px; height: 22px; }
}

/* ---------- tablet ---------- */
@media (min-width: 720px) {
  .mcv { height: 520px; }
  .mcv-stage { flex: 1 1 auto; height: auto; min-height: 0; }
  .mcv-lbl { display: inline; }
  .mcv-toolbar { padding: var(--sp-1) var(--sp-2); gap: var(--sp-2); }
  .mcv-legend { inset: var(--sp-2) var(--sp-2) var(--sp-2) auto; width: 260px; }
  .mcv-stepper { flex: 1 1 240px; }
  .mcv-out { flex: 0 1 auto; min-width: 14ch; }
  .mcv-axis-wrap { flex: 0 0 auto; }
  .mcv-axis { width: auto; }
  .mcv-name { max-width: 40ch; }
  .mcv-note { display: inline; }
}

/* ---------- desktop ---------- */
@media (min-width: 1080px) {
  .mcv { height: 600px; }
  .mcv-legend { width: 280px; }
}

/* ---------- reduced motion ---------- */
@media (prefers-reduced-motion: reduce) {
  .mcv-spinner { animation-duration: 3s; }
  .mcv-progress-bar, .mcv-btn, .mcv-legend-item { transition: none; }
}
