:root {
  --mf-blue: #149bd7;
  --mf-ink: #102532;
  --mf-line: #cddce5;
  --mf-muted: #5b6e79;
  --mf-paper: #ffffff;
  --mf-bg: #edf5f9;
  --mf-a: #cbe942;
  --mf-b: #58c5e8;
  --mf-touch: 48px;
}

* { box-sizing: border-box; }

html,
body { min-height: 100%; height: 100%; }

body {
  margin: 0;
  color: var(--mf-ink);
  background: var(--mf-bg);
  font-family: Arial, Helvetica, sans-serif;
}

button,
input { font: inherit; }

button { min-height: var(--mf-touch); }

.app {
  height: 100vh;
  min-height: 100vh;
  display: grid;
  grid-template-rows: auto minmax(0, 1fr);
  overflow: hidden;
}

.topbar {
  display: grid;
  grid-template-columns: minmax(260px, 1fr) auto;
  align-items: center;
  gap: 16px;
  padding: 12px 18px;
  color: #fff;
  background: #071018;
}

.brand,
.top-actions,
.toolbar,
.toolbar-group,
.segmented,
.model-actions,
.pair-list {
  display: flex;
  align-items: center;
}

.brand { min-width: 0; gap: 14px; }
.brand-logo { display: inline-flex; border-radius: 6px; }
.brand-logo:focus-visible { outline: 4px solid rgba(20,155,215,.45); outline-offset: 3px; }
.brand img { display: block; width: 142px; height: auto; }
.brand h1 { margin: 0; font-size: clamp(1.2rem, 2vw, 2rem); line-height: 1.05; }
.top-actions { justify-content: flex-end; gap: 8px; }

.icon-button,
.home-button {
  min-height: var(--mf-touch);
  border: 2px solid #263d51;
  border-radius: 8px;
  color: #fff;
  background: #122436;
}

.icon-button {
  width: var(--mf-touch);
  min-width: var(--mf-touch);
  display: inline-grid;
  place-items: center;
  padding: 0;
  cursor: pointer;
}

.icon-button:hover,
.icon-button:focus-visible,
.home-button:hover,
.home-button:focus-visible { border-color: #8fd8f3; color: #06111a; background: var(--mf-blue); }
.icon { width: 24px; height: 24px; pointer-events: none; }
.home-button { display: inline-flex; align-items: center; justify-content: center; padding: 0 14px; font-weight: 800; text-decoration: none; }

.multiples-factors-tool {
  min-height: 0;
  display: grid;
  grid-template-rows: auto minmax(0, 1fr);
  overflow: hidden;
}

.toolbar {
  position: relative;
  z-index: 10;
  min-height: 66px;
  gap: 10px;
  padding: 8px 14px;
  border-bottom: 1px solid var(--mf-line);
  background: #fff;
  overflow: visible;
}

.toolbar-group { gap: 8px; flex-wrap: wrap; }
.toolbar-group.questions { margin-left: auto; }
.toolbar button,
.toolbar summary,
.segmented label {
  min-height: var(--mf-touch);
  border: 1px solid #adbfca;
  border-radius: 8px;
  color: var(--mf-ink);
  background: #fff;
  font-weight: 800;
}

.toolbar button,
.toolbar summary { display: inline-flex; align-items: center; justify-content: center; padding: 0 14px; cursor: pointer; }
.toolbar button.primary { border-color: var(--mf-blue); color: #fff; background: var(--mf-blue); }
.toolbar button:disabled { opacity: .48; cursor: not-allowed; }
.toolbar details { position: relative; }
.toolbar summary { list-style: none; }
.toolbar summary::-webkit-details-marker { display: none; }

.menu-panel {
  position: absolute;
  z-index: 30;
  top: calc(100% + 8px);
  left: 0;
  width: min(340px, calc(100vw - 28px));
  max-height: min(620px, calc(100vh - 150px));
  overflow: auto;
  padding: 12px;
  border: 1px solid var(--mf-line);
  border-radius: 8px;
  background: #fff;
  box-shadow: 0 14px 32px rgba(7,16,24,.2);
}

.toolbar-group.questions .menu-panel { right: 0; left: auto; }
.choice-list { display: grid; gap: 8px; margin: 0; padding: 0; border: 0; }
.choice-list legend { margin-bottom: 8px; font-weight: 900; }
.choice-list label { display: grid; grid-template-columns: auto minmax(0,1fr); gap: 9px; align-items: start; padding: 8px; border-radius: 6px; }
.choice-list label:has(input:checked) { background: #e7f6fc; }
.choice-list label.is-unavailable { color: #667780; background: #f3f6f7; }
.choice-list small { display: block; margin-top: 3px; color: var(--mf-muted); font-weight: 400; }

.segmented { gap: 0; }
.segmented label { min-width: 92px; display: inline-flex; align-items: center; justify-content: center; padding: 0 12px; cursor: pointer; }
.segmented label:first-of-type { border-radius: 8px 0 0 8px; }
.segmented label:last-of-type { margin-left: -1px; border-radius: 0 8px 8px 0; }
.segmented input { position: absolute; opacity: 0; pointer-events: none; }
.segmented label:has(input:checked) { z-index: 1; border-color: var(--mf-blue); color: #fff; background: var(--mf-blue); }

.settings-grid { display: grid; grid-template-columns: repeat(2, minmax(0,1fr)); gap: 10px; }
.settings-grid label { display: grid; gap: 5px; color: var(--mf-muted); font-size: .88rem; font-weight: 800; }
.settings-grid input {
  width: 100%;
  min-height: 46px;
  padding: 8px 10px;
  border: 1px solid #9fb2bd;
  border-radius: 7px;
  color: var(--mf-ink);
  background: #fff;
  font-weight: 900;
}
.range-options { grid-column: 1 / -1; display: flex; gap: 8px; }
.range-options label { min-height: 44px; display: inline-flex; grid-template-columns: auto auto; align-items: center; padding: 6px 8px; color: var(--mf-ink); }
[hidden] { display: none !important; }
.sr-only { position: absolute; width: 1px; height: 1px; padding: 0; margin: -1px; overflow: hidden; clip: rect(0, 0, 0, 0); white-space: nowrap; border: 0; }

.workspace {
  min-width: 0;
  min-height: 0;
  display: grid;
  grid-template-columns: minmax(0, 1fr) minmax(260px, 330px);
  gap: 12px;
  padding: 12px;
  overflow: hidden;
}

.teaching-canvas,
.side-panel,
.prompt-card,
.model-panel,
.feedback,
.dependency-note {
  border: 1px solid var(--mf-line);
  border-radius: 10px;
  background: #fff;
}

.teaching-canvas {
  min-width: 0;
  min-height: 0;
  display: grid;
  grid-template-rows: auto minmax(0,1fr) auto;
  gap: 10px;
  padding: 12px;
}

.prompt-card { padding: 12px 14px; }
.prompt { margin: 0; font-size: clamp(1.15rem, 2vw, 1.65rem); font-weight: 900; line-height: 1.25; }
.prompt[data-density="compact"] { font-size: clamp(1.15rem, 1.65vw, 1.3rem); line-height: 1.2; }
.prompt[data-density="dense"] { font-size: clamp(1rem, 1.4vw, 1.1rem); line-height: 1.2; }
.model-panel {
  min-width: 0;
  min-height: 360px;
  display: grid;
  place-items: stretch;
  overflow: hidden;
  padding: 8px;
}
.model-host { width: 100%; height: 100%; min-height: 340px; display: grid; place-items: center; overflow: hidden; }
.model-host:has(.venn-tool--embedded) { place-items: stretch; }
.model-host .venn-tool--embedded { width: 100%; height: 100%; min-height: 0; }
.model-host .venn-tool--embedded .venn-workspace { height: 100%; grid-template-columns: minmax(165px, 220px) minmax(0, 1fr); }
.model-host .venn-tool--embedded .venn-board-panel { grid-template-rows: minmax(300px, 1fr) auto; }
.model-host .venn-tool--embedded .venn-card-tray { grid-template-columns: repeat(3, minmax(0, 1fr)); overflow: hidden; }
.model-host .venn-tool--embedded .venn-card-tray .venn-card { min-width: 0; height: 58px; min-height: 58px !important; }
.model-actions:empty { display: none; }
.teaching-canvas:has(.venn-tool--embedded) { grid-template-rows: auto minmax(0, 1fr); }
.model-host .question-visual { width: min(100%, 980px); height: 100%; min-height: 0; max-height: 100%; margin: 0; display: grid; grid-template-rows: auto minmax(0, 1fr) auto; align-items: stretch; overflow: hidden; }
.model-host .question-visual:has(> .model-caption-visually-hidden) { grid-template-rows: minmax(0, 1fr) auto; }
.model-host svg { width: 100%; height: 100%; min-height: 0; max-height: 100%; }
.model-actions { min-height: 52px; justify-content: center; gap: 8px; flex-wrap: wrap; }
.model-actions button,
.stepper button,
.candidate-row button { min-height: 48px; padding: 0 14px; border: 1px solid #a8bac5; border-radius: 8px; color: var(--mf-ink); background: #fff; font-weight: 900; cursor: pointer; }
.model-actions button[aria-pressed="true"] { box-shadow: 0 0 0 3px rgba(16, 37, 50, .28); }
.model-actions button.set-action-a,
.model-actions button.set-action-a[aria-pressed="true"] { border: 2px solid #526c7a; color: var(--mf-ink); background: var(--mf-a); }
.model-actions button.set-action-b,
.model-actions button.set-action-b[aria-pressed="true"] { border: 2px solid #526c7a; color: var(--mf-ink); background: var(--mf-b); }
.model-actions button.set-action-both,
.model-actions button.set-action-both[aria-pressed="true"] { border: 2px solid #526c7a; color: var(--mf-ink); background: linear-gradient(135deg, var(--mf-a) 0 50%, var(--mf-b) 50% 100%); }

.side-panel { min-height: 0; display: grid; align-content: start; gap: 12px; padding: 12px; overflow: auto; }
.side-panel h2,
.side-panel h3 { margin: 0; }
.side-panel p { margin: 0; line-height: 1.45; }
.stepper { display: grid; grid-template-columns: 48px minmax(58px,1fr) 48px; align-items: center; gap: 6px; }
.stepper output { min-height: 48px; display: grid; place-items: center; border: 1px solid var(--mf-line); border-radius: 8px; font-size: 1.3rem; font-weight: 900; }
.candidate-row { display: grid; grid-template-columns: minmax(0,1fr) auto; gap: 8px; }
.candidate-row input { min-width: 0; min-height: 48px; padding: 8px 10px; border: 1px solid #9fb2bd; border-radius: 8px; font-size: 1.15rem; font-weight: 900; }
.pair-list { min-height: 48px; align-items: flex-start; gap: 7px; flex-wrap: wrap; }
.pair-chip { padding: 7px 9px; border: 1px solid #ef9b9b; border-radius: 999px; background: #fff1f1; font-weight: 900; }
.feedback { min-height: 62px; display: flex; align-items: center; padding: 11px 13px; font-weight: 800; line-height: 1.35; }
.feedback[data-tone="correct"] { border-color: #3b8a62; background: #ecf8f1; }
.feedback[data-tone="incorrect"] { border-color: #c75d5d; background: #fff1f1; }
.feedback[data-tone="reveal"] { border-color: #72529c; background: #f7f2fb; }
.feedback[data-tone="exhausted"] { border-color: #9b7419; background: #fff8df; }
.dependency-note { padding: 12px; border-color: #c5b4df; background: #f7f2fb; }
.dependency-note strong { display: block; margin-bottom: 4px; }
.instructions-dialog { width: min(720px, calc(100vw - 32px)); max-height: calc(100vh - 32px); padding: 0; border: 0; border-radius: 10px; color: var(--mf-ink); box-shadow: 0 24px 64px rgba(7,16,24,.35); }
.instructions-dialog::backdrop { background: rgba(7,16,24,.58); }
.instructions-header { display: flex; align-items: center; justify-content: space-between; gap: 16px; padding: 14px 18px; color: #fff; background: #071018; }
.instructions-header h2 { margin: 0; }
.instructions-body { max-height: calc(100vh - 110px); padding: 18px; overflow: auto; line-height: 1.5; }
.instructions-body h3 { margin: 1.15em 0 .3em; }

@media (max-width: 899px), (max-height: 649px) {
  .app { height: auto; overflow: visible; }
  .multiples-factors-tool { overflow: visible; }
  .topbar { grid-template-columns: 1fr; }
  .top-actions { justify-content: flex-start; flex-wrap: wrap; }
  .toolbar { align-items: stretch; flex-direction: column; }
  .toolbar-group.questions { margin-left: 0; }
  .workspace { grid-template-columns: 1fr; overflow: visible; }
  .model-panel { min-height: 360px; }
  .side-panel { overflow: visible; }
  .model-host .venn-tool--embedded { height: auto; }
  .model-host .venn-tool--embedded .venn-workspace { height: auto; grid-template-columns: 1fr; overflow: visible; }
  .model-host .venn-tool--embedded .venn-tray-panel { min-height: 180px; }
  .model-host .venn-tool--embedded .venn-card-tray { max-height: none; overflow: visible; }
  .model-host .venn-tool--embedded .venn-board-panel { min-height: 560px; grid-template-rows: 430px auto; overflow: visible; }
}

@media (max-width: 520px) {
  .brand h1 { font-size: 1.15rem; }
  .prompt[data-density="compact"],
  .prompt[data-density="dense"] { font-size: 1.05rem; line-height: 1.25; }
  .toolbar-group > button,
  .toolbar-group > details { flex: 1 1 140px; }
  .toolbar summary { width: 100%; }
  .segmented { width: 100%; }
  .segmented label { flex: 1; }
  .settings-grid { grid-template-columns: 1fr; }
  .range-options { grid-column: auto; }
  .model-panel { min-height: 330px; padding: 2px; }
  .model-host { min-height: 320px; }
  .model-host:has(.venn-tool--embedded) { min-height: 760px; overflow: visible; }
  .model-host .venn-tool--embedded .venn-card-tray { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .model-host .venn-tool--embedded .venn-board-panel { min-height: 520px; grid-template-rows: 390px auto; }
  .model-host .venn-tool--embedded .statistics-venn-set-label { font-size: 18px !important; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { scroll-behavior: auto !important; transition-duration: .01ms !important; animation-duration: .01ms !important; }
}

@media print {
  @page { size: A4 landscape; margin: 8mm; }
  html,
  body,
  .app,
  .multiples-factors-tool,
  .workspace { width: 100%; min-height: 0; height: auto; padding: 0; background: #fff; }
  .topbar,
  .toolbar,
  .side-panel,
  .feedback,
  .model-actions,
  .instructions-dialog { display: none !important; }
  .workspace { display: block; }
  .teaching-canvas { height: 176mm; grid-template-rows: auto minmax(0,1fr); border: 0; padding: 0; }
  .prompt-card { border: 0; padding: 0 0 4mm; }
  .model-panel { min-height: 0; height: 150mm; border: 0; padding: 0; }
  .model-host { min-height: 0; }
}
