/* Patternwise tokens: dark mode only changes these values. Figure colours use the Okabe–Ito palette, and every
   shape also differs by outline, so no rule depends on colour alone. */
:root {
  --g-patternwise: #c2457a;
  --pw-ink: #1d2730;
  --pw-red: #d55e00;
  --pw-blue: #0072b2;
  --pw-green: #009e73;
  --pw-purple: #a8509a;
  --pw-cell: #ffffff;
  --pw-line: #dcdcd8;
  --pw-guide: #c3c8c2;
  --pw-tile: #f3f4f1;
  --pw-right: #2f8f4e;
  --pw-wrong: #c2453b;
  --pw-good-bg: #ecf6ef;
  --pw-bad-bg: #fbeeec;
  --pw-hint: var(--hint-context);
}
:root[data-theme='dark'] {
  --g-patternwise: #e07aa3;
  --pw-ink: #eceff0;
  --pw-red: #ff8f4d;
  --pw-blue: #5cb3ec;
  --pw-green: #3fd0a4;
  --pw-purple: #e08ad2;
  --pw-cell: #1d1f20;
  --pw-line: #34393b;
  --pw-guide: #4b5255;
  --pw-tile: #262b2d;
  --pw-right: #5cc27f;
  --pw-wrong: #ef7a6f;
  --pw-good-bg: #1b2a20;
  --pw-bad-bg: #2e1d1b;
}

.patternwise-shell {
  --game-accent: var(--g-patternwise);
}
.patternwise-shell .shell-stage {
  justify-content: safe center;
  padding: var(--space-2) var(--space-4) var(--space-3);
  overflow-y: auto;
}
.patternwise-shell .shell-board-area {
  width: 100%;
}

/* Sizes: --w is the column width, --h the height left for figures after bars, prompt and gaps. Each form sets
   --fig (context panels) and --opt (option buttons) so the whole question fits a 375 × 667 phone. */
.pw-board-wrap {
  --gap: 8px;
  --w: min(560px, calc(100vw - 32px));
  --h: calc(100dvh - 290px - env(safe-area-inset-top) - env(safe-area-inset-bottom));
  --fig: min(96px, calc(var(--h) / 4.4));
  --opt: min(104px, calc((var(--w) - (var(--opt-cols) - 1) * var(--gap)) / var(--opt-cols)), calc(var(--h) / 3));
  display: flex;
  width: var(--w);
  flex-direction: column;
  align-items: center;
  gap: var(--space-3);
  margin: 0 auto;
}
.pw-board-wrap[data-form='matrix'] {
  --fig: min(112px, calc((var(--w) - 2 * var(--gap)) / 3), calc(var(--h) / 5));
  --opt: var(--fig);
}
.pw-board-wrap[data-form='series'] {
  --fig: min(100px, calc((var(--w) - (var(--seq, 5) - 1) * var(--gap)) / var(--seq, 5)));
}
.pw-board-wrap[data-form='odd'] {
  --opt: min(116px, calc((var(--w) - 2 * var(--gap)) / 3), calc(var(--h) / 2.4));
}
.pw-board-wrap[data-form='mirror'] {
  --fig: min(150px, calc(var(--h) / 2.6));
}

.pw-prompt {
  max-width: 34em;
  margin: 0;
  color: var(--text);
  font-size: 15px;
  line-height: 1.45;
  text-align: center;
  text-wrap: balance;
}
.pw-tag {
  display: inline-block;
  margin-right: 6px;
  padding: 1px 8px;
  border: 1px solid var(--game-accent);
  border-radius: var(--radius-pill);
  color: var(--game-accent);
  font-size: 12px;
  font-weight: 700;
  letter-spacing: 0.04em;
  vertical-align: 1px;
}

/* Context panels. */
.pw-context {
  display: grid;
  gap: var(--gap);
  justify-content: center;
  align-items: center;
}
.pw-series {
  grid-template-columns: repeat(var(--seq), var(--fig));
}
.pw-matrix {
  grid-template-columns: repeat(3, var(--fig));
}
.pw-analogy {
  grid-template-columns: var(--fig) 22px var(--fig);
}
.pw-op {
  color: var(--text-muted);
  font-size: 20px;
  font-weight: 800;
  text-align: center;
}
.pw-fig {
  display: grid;
  width: var(--fig);
  aspect-ratio: 1;
  place-items: center;
  border: 1.5px solid var(--pw-line);
  border-radius: 12px;
  background: var(--pw-cell);
}
.pw-fig.hint {
  border-color: var(--pw-hint);
  box-shadow: 0 0 0 3px var(--pw-hint);
}
.pw-fig.q {
  border: 2px dashed var(--game-accent);
  color: var(--game-accent);
  font-size: calc(var(--fig) * 0.38);
}
.pw-fig.solved {
  border-color: var(--pw-right);
  box-shadow: 0 0 0 2px var(--pw-right);
}
.pw-art {
  display: block;
  width: 88%;
  height: auto;
  aspect-ratio: 1;
  overflow: visible;
}

/* Options. */
.pw-options {
  display: grid;
  grid-template-columns: repeat(var(--opt-cols), var(--opt));
  gap: var(--gap);
  justify-content: center;
}
/* Five figures: 3 + 2 on phones, centred; one row of five on wider screens. */
.pw-board-wrap[data-form='odd'] .pw-options {
  display: flex;
  max-width: calc(3 * var(--opt) + 2 * var(--gap));
  flex-wrap: wrap;
}
.pw-option {
  position: relative;
  display: grid;
  width: var(--opt);
  aspect-ratio: 1;
  place-items: center;
  padding: 0;
  border: 2px solid var(--pw-line);
  border-radius: 12px;
  background: var(--pw-cell);
  color: var(--text);
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
  transition:
    border-color 140ms ease-out,
    box-shadow 140ms ease-out,
    opacity 140ms ease-out;
}
.pw-option .pw-art {
  width: 80%;
}
.pw-option:focus-visible {
  outline: 3px solid var(--text);
  outline-offset: 2px;
}
.pw-option[aria-disabled='true'] {
  cursor: default;
}
.pw-key {
  position: absolute;
  top: 3px;
  left: 7px;
  color: var(--text-muted);
  font-size: 12px;
  font-weight: 700;
}
.pw-option.hint {
  border-color: var(--pw-hint);
  box-shadow: 0 0 0 3px var(--pw-hint);
}
.pw-option.right {
  border-color: var(--pw-right);
  box-shadow: 0 0 0 3px var(--pw-right);
}
.pw-option.right .pw-key,
.pw-option.wrong .pw-key {
  color: var(--text);
}
.pw-option.right .pw-key::after {
  content: ' ✓';
  color: var(--pw-right);
}
.pw-option.wrong {
  border-color: var(--pw-wrong);
  opacity: 0.55;
}
.pw-option.wrong .pw-key::after {
  content: ' ✗';
  color: var(--pw-wrong);
}
.pw-option.wrong.flash {
  animation: pw-shake 320ms ease-out;
}
.pw-option.right.flash {
  animation: pw-pop 420ms ease-out;
}
@keyframes pw-shake {
  25% {
    transform: translateX(-4px);
  }
  50% {
    transform: translateX(4px);
  }
  75% {
    transform: translateX(-2px);
  }
}
@keyframes pw-pop {
  40% {
    transform: scale(1.06);
  }
}
@media (prefers-reduced-motion: reduce) {
  .pw-option.flash {
    animation: none;
  }
}

/* Feedback: wrong reason, revealed hints, or the explanation after answering. */
.pw-feedback {
  display: grid;
  width: 100%;
  gap: var(--space-2);
  justify-items: start;
  padding: var(--space-3) var(--space-4);
  border: 1px solid var(--pw-line);
  border-radius: 12px;
  background: var(--surface);
  font-size: 14px;
}
.pw-feedback.good {
  border-color: var(--pw-right);
  background: var(--pw-good-bg);
}
.pw-feedback.bad {
  border-color: var(--pw-wrong);
  background: var(--pw-bad-bg);
}
.pw-feedback p,
.pw-feedback ul,
.pw-feedback ol {
  margin: 0;
}
.pw-feedback ul,
.pw-feedback ol {
  display: grid;
  gap: 2px;
  padding-left: 1.3em;
}
.pw-verdict {
  color: var(--pw-right);
  font-size: 16px;
  font-weight: 800;
}
.pw-feedback:not(.good) .pw-verdict {
  color: var(--text);
}
.pw-verdict small {
  color: var(--text-muted);
  font-size: 13px;
  font-weight: 600;
}
.pw-reason b {
  color: var(--pw-wrong);
}
.pw-reason span {
  color: var(--text-muted);
}
.pw-feedback .shell-pill {
  justify-self: center;
  min-height: 44px;
}

/* Picture rules: a small question (figures, options, feedback) or a 3 × 3 matrix, sized from the stage width. */
.patternwise-shell .pw-demo {
  display: flex;
  width: 100%;
  flex-direction: column;
  align-items: center;
  gap: 6px;
}
.pw-demo-row {
  position: relative;
  display: flex;
  width: 100%;
  justify-content: center;
  gap: 3%;
}
.pw-demo .pw-fig {
  width: 18%;
  border-radius: 8px;
}
.pw-demo .pw-fig.q {
  font-size: 20px;
}
.pw-demo .pw-art {
  width: 86%;
}
.pw-demo-row .pw-option {
  width: 22%;
  cursor: default;
}
.pw-demo-row .pw-key {
  font-size: 11px;
}
.pw-demo-row .shell-demo-mark {
  top: -8px;
  right: 4%;
}
.pw-demo-foot {
  display: flex;
  width: 100%;
  min-height: 64px;
  align-items: center;
  gap: 6px;
}
.pw-demo .pw-feedback {
  min-height: 60px;
  flex: 1;
  align-content: center;
  padding: 6px 10px;
  font-size: 12.5px;
  line-height: 1.35;
}
.pw-demo .pw-verdict {
  font-size: 14px;
}
.pw-demo .pw-demo-empty {
  visibility: hidden;
}
.pw-demo-tool {
  flex: none;
  width: 56px;
  cursor: default;
}
.pw-demo-matrix {
  position: relative;
  display: grid;
  width: 74%;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 6px;
}
.pw-demo-matrix .pw-fig {
  width: 100%;
}
.pw-demo-tag {
  padding: 2px 10px;
  border-radius: 999px;
  background: var(--pw-tile);
  color: var(--text);
  font-size: 14px;
  font-weight: 700;
}
.pw-demo-tag.empty {
  visibility: hidden;
}

@media (min-width: 640px) {
  .pw-board-wrap[data-form='odd'] .pw-options {
    max-width: none;
  }
  .pw-board-wrap[data-form='odd'] {
    --opt: min(116px, calc((var(--w) - 4 * var(--gap)) / 5));
  }
}
@media (max-width: 520px) {
  .pw-series.long {
    grid-template-columns: repeat(3, var(--fig));
  }
  .pw-board-wrap[data-form='series']:has(.pw-series.long) {
    --fig: min(92px, calc((var(--w) - 2 * var(--gap)) / 3), calc(var(--h) / 4.4));
  }
}
/* Wide screens: the matrix and its options sit side by side. */
@media (min-width: 900px) {
  .pw-board-wrap[data-form='matrix'] {
    --w: min(900px, calc(100vw - 64px));
    --fig: min(120px, calc(var(--h) / 3.6));
    display: grid;
    grid-template-areas:
      'prompt prompt'
      'context options'
      'feedback feedback';
    grid-template-columns: auto auto;
    justify-content: center;
    align-items: center;
    gap: var(--space-4) 40px;
  }
  .pw-board-wrap[data-form='matrix'] .pw-prompt {
    grid-area: prompt;
    justify-self: center;
  }
  .pw-board-wrap[data-form='matrix'] .pw-context {
    grid-area: context;
  }
  .pw-board-wrap[data-form='matrix'] .pw-options {
    grid-area: options;
  }
  .pw-board-wrap[data-form='matrix'] .pw-feedback {
    grid-area: feedback;
  }
  .pw-board-wrap[data-form='odd'] {
    --w: min(720px, calc(100vw - 64px));
    --opt: min(132px, calc((var(--w) - 4 * var(--gap)) / 5));
  }
}
/* Phone held sideways: figures size from the left column of the two-column shell (see landscape.css), and the
   context and options sit side by side when they fit. */
@media (orientation: landscape) and (max-height: 540px) {
  .pw-board-wrap {
    --w: var(--fit-w);
    --h: calc(var(--fit-h) - 64px);
    --fig: min(96px, calc(var(--h) / 2.2));
    flex-direction: row;
    flex-wrap: wrap;
    justify-content: center;
    align-content: center;
  }
  .pw-prompt,
  .pw-feedback {
    flex-basis: 100%;
  }
  .pw-board-wrap[data-form='matrix'] {
    --fig: min(96px, calc(var(--h) / 3), calc((var(--w) - 5 * var(--gap) - 16px) / 6));
  }
  .pw-board-wrap[data-form='series'] {
    --fig: min(84px, calc(var(--h) / 2.4), calc((var(--w) - (var(--seq, 5) - 1) * var(--gap)) / var(--seq, 5)));
  }
}
