:root {
  --surround: #7f7f7f; /* neutral mid-grey surround to stabilise adaptation */
  --box-size: 240px;
  --glyph-size: 120px;
  --glyph-font: system-ui, sans-serif;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  min-height: 100%;
  background: var(--surround);
  color: #111;
  font: 16px/1.5 system-ui, sans-serif;
}

#app { max-width: 720px; margin: 0 auto; padding: 2rem 1rem; }

.screen { background: #f4f4f4; padding: 1.5rem; border-radius: 8px; }
.hidden { display: none !important; }
.muted { color: #666; font-size: 0.9rem; }
.warning { color: #6b4e00; background: #fff6d6; border: 1px solid #e0c870; padding: .5rem .75rem; border-radius: 4px; }
.error { color: #8b1a1a; background: #fde8e8; border: 1px solid #e0a0a0; padding: .5rem .75rem; border-radius: 4px; white-space: pre-wrap; }
kbd { border: 1px solid #999; border-radius: 3px; padding: 0 .3em; font-size: .85em; background: #fff; }

button {
  font: inherit;
  padding: .6rem 1.2rem;
  border-radius: 6px;
  border: 1px solid #888;
  background: #fff;
  cursor: pointer;
  min-width: 9rem;
}
button:hover { background: #eee; }
button.primary { background: #2b6cb0; color: #fff; border-color: #2b6cb0; }
button.primary:hover { background: #245a94; }
button:disabled { opacity: .5; cursor: default; }
button.link { background: none; border: none; color: #2b6cb0; text-decoration: underline; padding: 0; min-width: 0; font-size: inherit; }

.session-form fieldset { border: 1px solid #ccc; border-radius: 6px; margin: 0 0 .75rem; padding: .5rem .75rem; }
.session-form legend { font-weight: 600; padding: 0 .3rem; }
.session-form label { display: inline-block; margin-right: 1rem; }

/* Trial screen: the box sits on the neutral surround, not on the panel. */
#screen-trial { background: transparent; padding: 0; }
#progress { text-align: center; color: #222; margin-bottom: 1rem; }

#stage {
  display: flex;
  justify-content: center;
  align-items: center;
  gap: 1rem;
  padding: 3rem 0;
}

/* Fixed-width slots either side of the box so feedback never shifts the stimulus. */
.feedback {
  width: 3rem;
  height: 3rem;
  display: flex;
  justify-content: center;
  align-items: center;
  position: relative;
}
.fb-icon {
  position: absolute;
  width: 2.5rem;
  height: 2.5rem;
  fill: none;
  stroke: #555;
  stroke-width: 2.5;
  stroke-linecap: round;
  stroke-linejoin: round;
  opacity: 0;
  pointer-events: none;
}
/* Only the icon matching the slot's current kind is animated; others stay hidden. */
#feedback.lighter .fb-up,
#feedback.darker .fb-down,
#feedback.lighter-coarse .fb-up2,
#feedback.darker-coarse .fb-down2,
#feedback.clamp-up .fb-up,
#feedback.clamp-down .fb-down{ animation-duration: .5s; animation-timing-function: ease-out; animation-fill-mode: forwards; }

#feedback.lighter .fb-up, #feedback.lighter-coarse .fb-up2 { animation-name: fb-up; }
#feedback.darker .fb-down, #feedback.darker-coarse .fb-down2 { animation-name: fb-down; }
#feedback.clamp-up .fb-up, #feedback.clamp-down .fb-down { animation-name: fb-shake; }

@keyframes fb-up    { 0% { opacity: 1; transform: translateY(0); }     100% { opacity: 0; transform: translateY(-12px); } }
@keyframes fb-down  { 0% { opacity: 1; transform: translateY(0); }     100% { opacity: 0; transform: translateY(12px); } }
@keyframes fb-shake {
  0%   { opacity: 1; transform: translateX(0); }
  20%  { transform: translateX(-6px); }
  40%  { transform: translateX(6px); }
  60%  { transform: translateX(-4px); }
  80%  { transform: translateX(4px); }
  100% { opacity: 0; transform: translateX(0); }
}

@media (prefers-reduced-motion: reduce) {
  @keyframes fb-up    { 0% { opacity: 1; } 100% { opacity: 0; } }
  @keyframes fb-down  { 0% { opacity: 1; } 100% { opacity: 0; } }
  @keyframes fb-shake { 0% { opacity: 1; } 100% { opacity: 0; } }
}

/* Pressed state: :active for pointer, .pressed mirrored from the keyboard. */
#controls button:active,
#controls button.pressed { background: #d6d6d6; transform: translateY(1px); box-shadow: inset 0 1px 2px rgba(0,0,0,.25); }
#controls button.primary:active,
#controls button.primary.pressed { background: #1f4f85; }
#controls button.coarse { min-width: 8rem; font-size: .9rem; }

#box {
  width: var(--box-size);
  height: var(--box-size);
  display: flex;
  justify-content: center;
  align-items: center;
  background: var(--bg);
  color: var(--fg);
  user-select: none;
  /* Trials change behind a fade to the surround so no intermediate colours are shown. */
  transition: opacity .25s ease-in-out;
}
#box.fading { opacity: 0; }
@media (prefers-reduced-motion: reduce) { #box { transition: none; } }

#glyph {
  font-family: var(--glyph-font);
  font-size: var(--glyph-size);
  line-height: 1;
  font-weight: 400;
  /* Avoid subpixel colour fringing which would add spurious contrast cues. */
  -webkit-font-smoothing: antialiased;
  text-rendering: geometricPrecision;
}

#controls { display: flex; flex-direction: column; align-items: center; gap: 1rem; }
#controls .row { display: flex; justify-content: center; gap: .75rem; flex-wrap: wrap; }
/* Darker and lighter pairs stay together, so on narrow screens they wrap as two rows. */
#controls .pair { display: flex; gap: .75rem; flex-wrap: nowrap; }
#controls .row:last-child button { min-width: 14rem; }

.hint {
  text-align: center;
  color: #222;
  background: #f4f4f4;
  border-radius: 6px;
  padding: .5rem .75rem;
  margin: 1rem auto 0;
  max-width: 28rem;
  /* Fades in with the stimulus (same duration as the box fade). */
  animation: hint-in .25s ease-in-out;
}
@keyframes hint-in { from { opacity: 0; } to { opacity: 1; } }
@media (prefers-reduced-motion: reduce) { .hint { animation: none; } }

#debug { text-align: center; margin-top: 1rem; font-family: monospace; }

#results { width: 100%; border-collapse: collapse; margin-top: 1rem; font-size: .85rem; }
#results th, #results td { border-bottom: 1px solid #ccc; padding: .25rem .5rem; text-align: left; }
#results td.swatch span { display: inline-block; width: 1.2em; height: 1.2em; vertical-align: middle; border: 1px solid #999; }
