/* Polysemous — warm paper, plain ink, and four physical sorting wells. */

*, *::before, *::after { box-sizing: border-box; }

:root {
  --bg: #FBF7EF;
  --tile: #FFFFFF;
  --tile-hi: #F4EEE2;
  --ink: #23201B;
  --muted: #6E675C;
  --line: #E4DCCB;
  --line-hi: #CFC3AD;
  --recess: #F1EADD;

  /* Group colors appear only after a group has been solved or in results. */
  --c1: #8A6118;
  --c2: #4A6741;
  --c3: #33527E;
  --c4: #9E3A31;
  /* Stars are their own currency, so they get their own colour — not --c1,
     which already means "the easiest group". */
  --star: #A2740B;

  --c1-bg: #F6EEDC;
  --c2-bg: #EDF1E7;
  --c3-bg: #E9EDF5;
  --c4-bg: #F8EAE7;

  --font: "Inter", -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto,
          "Helvetica Neue", Arial, sans-serif;
  --col: 672px;
  --gap: 10px;
  --slot-h: clamp(46px, 9vw, 60px);
  --r: 7px;
  --well-r: 13px;
  --chip-fs-max: 16px;
  --chip-fs-min: 9px;
  --ease: cubic-bezier(.2, .8, .3, 1.1);
}

@media (min-width: 420px) {
  :root { --chip-fs-max: 18px; }
}

html { -webkit-text-size-adjust: 100%; }

body {
  margin: 0;
  min-width: 320px;
  padding: max(12px, env(safe-area-inset-top)) 14px
           calc(22px + env(safe-area-inset-bottom));
  display: flex;
  justify-content: center;
  background: var(--bg);
  color: var(--ink);
  font-family: var(--font);
  line-height: 1.5;
}

#page {
  width: 100%;
  max-width: var(--col);
}

.sr-only,
#status.is-sr {
  position: absolute;
  width: 1px;
  height: 1px;
  margin: -1px;
  padding: 0;
  overflow: hidden;
  clip: rect(0 0 0 0);
  clip-path: inset(50%);
  white-space: nowrap;
  border: 0;
}

/* Header */

#header { padding: 2px 0 11px; }

#title {
  margin: 0;
  font-size: clamp(23px, 4vw, 28px);
  font-weight: 650;
  letter-spacing: -.035em;
  line-height: 1.05;
  text-align: center;
}

#meta {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 10px;
  margin: 7px 0 0;
  color: var(--muted);
  font-size: 12px;
  letter-spacing: .09em;
  text-transform: uppercase;
}

#scoreBox {
  position: relative;
  display: inline-flex;
  align-items: baseline;
  gap: 6px;
}

#score {
  color: var(--ink);
  font-size: 19px;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
  letter-spacing: -.01em;
  transition: color .2s ease;
}

#score.is-hit { color: var(--ink); }

.score__drop {
  position: absolute;
  left: 0;
  top: 100%;
  margin-top: 1px;
  color: var(--ink);
  font-size: 13px;
  font-weight: 700;
  letter-spacing: 0;
  pointer-events: none;
  text-transform: none;
  white-space: nowrap;
}

/* Stars

   One glyph, three sizes: the header tally, the mark on a solved row, and the
   four-slot record in the result panel. The burst rings live on ::before and
   ::after and are only ever drawn inside the no-preference block — at rest they
   would be a visible circle sitting on the page. */

#stars {
  display: inline-flex;
  align-items: baseline;
  gap: 1px;
  margin-left: 1px;
}

.star {
  position: relative;
  display: inline-block;
  color: var(--star);
  font-size: 15px;
  line-height: 1;
  letter-spacing: 0;
}

.star--big { font-size: 14px; vertical-align: -1px; margin-right: 3px; }

.star--result {
  font-size: 30px;
  line-height: 1;
  margin: 0 2px;
}

.star--result.is-empty { color: var(--line-hi); }

#howto {
  max-width: 46em;
  margin: 0 auto 18px;
  color: var(--muted);
  font-size: 13px;
  line-height: 1.45;
  text-align: center;
}

/* Semantic board regions */

.board-section { margin: 0 0 18px; }

.board-section__header {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 7px;
  padding: 0 2px;
}

.board-section__title {
  margin: 0;
  font-size: 12px;
  font-weight: 700;
  letter-spacing: .1em;
  line-height: 1.2;
  text-transform: uppercase;
}

.board-section__hint {
  color: var(--muted);
  font-size: 11px;
  letter-spacing: .04em;
}

/* Word bank: intentionally not a grid. Word-sized pieces wrap like a bank. */

#pool {
  display: flex;
  flex-wrap: wrap;
  align-content: flex-start;
  gap: 8px;
  min-height: 58px;
  padding: 10px 0;
  border-block: 1px solid var(--line);
  transition: background-color .15s ease, outline-color .15s ease;
}

#pool > .chip,
#pool > .hole {
  flex: 0 0 auto;
  width: auto;
  max-width: 100%;
  height: 42px;
  min-width: 74px;
  padding-inline: 12px;
}

/* The gap a lifted word leaves behind. It holds the same word at the same size
   behind `visibility: hidden`, which is the only way to reserve exactly the
   width the chip had — a generic placeholder would be the min-width and the
   bank would reflow around it, which is the thing this exists to prevent.
   A recess, not a wireframe: the bank should read as having a hole in it, not
   as a form waiting to be filled. */
.hole {
  display: grid;
  place-items: center;
  padding: 4px;
  border: 1px solid transparent;
  border-radius: var(--r);
  background: var(--recess);
}

.hole > .chip__word { visibility: hidden; }

#pool:empty::after {
  content: "Drop words here";
  display: grid;
  place-items: center;
  width: 100%;
  min-height: 42px;
  color: var(--muted);
  font-size: 12px;
  letter-spacing: .04em;
  text-transform: uppercase;
}

body.is-holding #pool {
  background: rgba(255, 255, 255, .48);
  outline: 2px solid var(--line-hi);
  outline-offset: 2px;
}

#pool.is-target {
  background: var(--tile);
  outline-color: var(--ink);
}

/* The four-well field is the page's one bold spatial move. */

#buckets {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: var(--gap);
}

.bucket {
  position: relative;
  min-width: 0;
  padding: 48px 9px 9px;
  border: 1px solid var(--line-hi);
  border-radius: var(--well-r);
  background: var(--recess);
  transition: border-color .15s ease, outline-color .15s ease,
              background-color .15s ease;
}

/* Current game markup has no header text; updated markup can replace this
   fallback with the semantic .well__header below. */
.bucket::before {
  content: "Sorting well";
  position: absolute;
  top: 14px;
  left: 12px;
  color: var(--muted);
  font-size: 10px;
  font-weight: 700;
  letter-spacing: .08em;
  line-height: 1;
  text-transform: uppercase;
}

.bucket:has(.well__header)::before { content: none; }

.well__header {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  min-height: 46px;
  padding: 0 10px 0 12px;
  display: flex;
  align-items: center;
  gap: 7px;
  border-bottom: 1px solid var(--line);
}

.bucket__tab {
  position: absolute;
  z-index: 1;
  top: 3px;
  right: 4px;
  width: auto;
  min-width: 54px;
  min-height: 40px;
  height: 40px;
  padding: 0 10px;
  border: 1px solid transparent;
  border-radius: 8px;
  background: transparent;
  color: var(--muted);
  font-size: 0;
  line-height: 1;
  display: grid;
  place-items: center;
}

.bucket__tab::after {
  content: "";
  font-size: 12px;
  font-weight: 650;
  letter-spacing: .01em;
}

.bucket__tab:disabled { opacity: 1; }

.bucket.is-full .bucket__tab {
  border-color: var(--ink);
  background: var(--ink);
  color: var(--bg);
  cursor: pointer;
}

.bucket.is-full .bucket__tab::after { content: "Check"; }

.bucket.is-full .bucket__tab:hover {
  background: #3A352D;
  border-color: #3A352D;
}

body.is-holding .bucket:not(.is-full) .bucket__tab {
  border-color: var(--line-hi);
  background: var(--tile);
  color: var(--ink);
  cursor: pointer;
}

body.is-holding .bucket:not(.is-full) .bucket__tab::after { content: "Place"; }

.bucket__slots {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  grid-template-rows: repeat(2, var(--slot-h));
  gap: 7px;
  min-width: 0;
  border-radius: calc(var(--well-r) - 4px);
  transition: outline-color .15s ease;
}

.slot {
  min-width: 0;
  min-height: 0;
  border: 1px solid #DED4C1;
  border-radius: var(--r);
  background: rgba(255, 255, 255, .5);
}

.slot:not(:empty) {
  border-color: transparent;
  background: transparent;
}

body.is-holding .bucket:not(.is-full) {
  border-color: var(--muted);
  outline: 2px solid var(--line-hi);
  outline-offset: 1px;
}

.bucket.is-target {
  border-color: var(--ink);
  background: var(--tile-hi);
  outline: 2px solid var(--ink);
  outline-offset: 1px;
}

/* Keep a lone well centered as solved wells leave the field. */
#buckets > .bucket:last-child:nth-child(odd) {
  grid-column: 1 / -1;
  width: calc(50% - var(--gap) / 2);
  justify-self: center;
}

/* Chips */

.chip {
  -webkit-appearance: none;
  appearance: none;
  display: grid;
  place-items: center;
  width: 100%;
  height: 100%;
  min-width: 0;
  min-height: 0;
  margin: 0;
  padding: 4px;
  border: 1px solid var(--line);
  border-radius: var(--r);
  background: var(--tile);
  color: var(--ink);
  font-family: var(--font);
  cursor: grab;
  touch-action: none;
  -webkit-tap-highlight-color: transparent;
  -webkit-user-select: none;
  user-select: none;
  transition: background-color .13s ease, color .13s ease,
              border-color .13s ease, outline-color .13s ease;
}

.chip__word {
  display: block;
  width: 100%;
  min-width: 0;
  overflow: hidden;
  overflow-wrap: anywhere;
  color: inherit;
  font-size: var(--chip-fs-max);
  font-weight: 600;
  letter-spacing: .035em;
  line-height: 1.15;
  text-align: center;
  text-transform: uppercase;
  white-space: nowrap;
  pointer-events: none;
}

/* Bank chips are intrinsically sized, while well slots are fixed. Keep words
   legible when they cross between those two geometries, even before the next
   fitChips pass. */
.slot .chip__word,
#drag .chip__word {
  font-size: clamp(var(--chip-fs-min), 2.1vw, 14px) !important;
  letter-spacing: 0 !important;
}

.chip:hover:not(.is-held) {
  border-color: var(--line-hi);
  background: var(--tile-hi);
}

.chip:disabled { opacity: 1; cursor: default; }

.chip.is-held {
  border-color: var(--ink);
  background: var(--ink);
  color: var(--tile);
  cursor: grabbing;
}

.chip.is-ghost { opacity: .28; }

.chip.is-swap {
  border-color: var(--ink);
  outline: 2px solid var(--ink);
  outline-offset: 1px;
}

.chip:focus-visible,
button:focus-visible,
.bucket__slots:focus-visible,
#pool:focus-visible {
  outline: 2px solid var(--ink);
  outline-offset: 2px;
}

/* Pointer-following clone: deliberately flat. */
#drag {
  position: fixed;
  z-index: 60;
  margin: 0;
  border-color: var(--ink);
  background: var(--ink);
  color: var(--tile);
  opacity: .96;
  pointer-events: none;
  transform: translate(-50%, -50%) scale(1.04);
}

/* Solved groups and naming */

#solved:not(:empty) { margin-bottom: 14px; }

.group {
  position: relative;
  overflow: hidden;
  margin-bottom: 8px;
  padding: 11px 13px 12px 17px;
  border-radius: var(--r);
  background: var(--bg);
}

.group::before {
  content: "";
  position: absolute;
  top: 0;
  bottom: 0;
  left: 0;
  width: 5px;
  background: currentColor;
  transform-origin: 50% 0;
}

.group__word {
  display: block;
  font-size: 17px;
  font-weight: 700;
  letter-spacing: .02em;
  line-height: 1.2;
}

.group__word:empty { display: none; }

.group__list {
  display: block;
  margin-top: 3px;
  color: var(--ink);
  font-size: 13px;
  letter-spacing: .04em;
  line-height: 1.4;
  opacity: .74;
  text-transform: uppercase;
}

.group[data-group="1"] { --group-ink: var(--c1); color: var(--c1); background: var(--c1-bg); }
.group[data-group="2"] { --group-ink: var(--c2); color: var(--c2); background: var(--c2-bg); }
.group[data-group="3"] { --group-ink: var(--c3); color: var(--c3); background: var(--c3-bg); }
.group[data-group="4"] { --group-ink: var(--c4); color: var(--c4); background: var(--c4-bg); }

.group__ask {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-top: 9px;
}

.group__input {
  flex: 1 1 auto;
  min-width: 0;
  min-height: 40px;
  padding: 8px 10px;
  border: 1px solid currentColor;
  border-radius: 6px;
  background: rgba(255, 255, 255, .72);
  color: var(--ink);
  font-family: var(--font);
  font-size: 15px;
  font-weight: 600;
  letter-spacing: .04em;
  text-transform: uppercase;
  -webkit-appearance: none;
  appearance: none;
}

.group__input::placeholder {
  color: var(--muted);
  font-weight: 400;
  letter-spacing: .02em;
  opacity: .8;
  text-transform: none;
}

.group__input:focus-visible {
  outline: 2px solid currentColor;
  outline-offset: 1px;
}

.group__go {
  flex: 0 0 auto;
  min-height: 40px;
  padding: 0 14px;
  border: 1px solid var(--group-ink, var(--ink));
  border-radius: 6px;
  background: var(--group-ink, var(--ink));
  color: var(--bg);
  font-size: 14px;
  font-weight: 600;
}

.group__go:hover { opacity: .86; }

.group__miss {
  display: block;
  margin-top: 2px;
  color: var(--muted);
  font-size: 12.5px;
  letter-spacing: .02em;
  text-transform: none;
}

/* Status and controls */

#status {
  min-height: 1.35em;
  margin: 8px 0 10px;
  color: var(--muted);
  font-size: 14px;
  text-align: center;
}

#controls {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: var(--gap);
}

button {
  min-height: 44px;
  padding: 0 14px;
  border: 1px solid var(--line-hi);
  border-radius: 22px;
  background: transparent;
  color: var(--ink);
  font-family: var(--font);
  font-size: 14px;
  font-weight: 500;
  letter-spacing: .01em;
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
  transition: background-color .14s ease, border-color .14s ease,
              color .14s ease, opacity .14s ease;
}

button:not(.chip):not(.bucket__tab):not(.group__go):hover:not(:disabled) {
  background: var(--tile-hi);
}

button:not(.chip):not(.bucket__tab):not(.group__go):disabled {
  opacity: .45;
  cursor: default;
}

.btn--go {
  border-color: var(--ink);
  background: var(--ink);
  color: var(--bg);
}

.btn--go:hover:not(:disabled) {
  border-color: #3A352D;
  background: #3A352D;
}

.btn--go:disabled {
  border-color: var(--line);
  background: transparent;
  color: var(--muted);
  opacity: 1;
}

/* Results */

#result { padding: 4px 0 8px; text-align: center; }

#resultTitle {
  margin: 12px 0 0;
  font-size: 22px;
  font-weight: 600;
}

#resultScore {
  margin: 10px 0 0;
  font-size: 40px;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
  letter-spacing: -.02em;
  line-height: 1;
}

#resultScore .out-of {
  margin-left: 2px;
  color: var(--muted);
  font-size: 17px;
  font-weight: 500;
  letter-spacing: 0;
}

#resultStars {
  margin: 9px 0 0;
  line-height: 1;
}

#resultStrip {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 4px;
  margin: 16px 0 12px;
}

.strip__row { display: flex; gap: 4px; }
.strip__cell { width: 18px; height: 18px; border-radius: 3px; }
.strip__cell[data-group="1"] { background: var(--c1); }
.strip__cell[data-group="2"] { background: var(--c2); }
.strip__cell[data-group="3"] { background: var(--c3); }
.strip__cell[data-group="4"] { background: var(--c4); }

#resultLine {
  margin: 0 0 16px;
  color: var(--muted);
  font-size: 14px;
}

#result .btn--go { min-width: 160px; }

/* State machine */

body[data-state="done"] #howto,
body[data-state="done"] .board-section,
body[data-state="done"] #status,
body[data-state="done"] #controls { display: none; }

body.is-naming .board-section,
body.is-naming #controls {
  opacity: .38;
  pointer-events: none;
}

/* Toast */

#toast {
  position: fixed;
  z-index: 50;
  left: 50%;
  bottom: calc(18px + env(safe-area-inset-bottom));
  padding: 9px 16px;
  border-radius: 20px;
  background: var(--ink);
  color: var(--bg);
  font-size: 13.5px;
  opacity: 0;
  pointer-events: none;
  transform: translate(-50%, 14px);
  transition: opacity .18s ease, transform .18s var(--ease);
}

#toast.is-up { opacity: 1; transform: translate(-50%, 0); }

/* Motion */

@keyframes pop { 0% { transform: scale(1); } 45% { transform: scale(1.07); } 100% { transform: scale(1); } }
@keyframes rightPop { 0% { transform: translateY(0); } 40% { transform: translateY(-9px); } 70% { transform: translateY(2px); } 100% { transform: translateY(0); } }
@keyframes leave { to { opacity: 0; transform: scale(.9); } }
@keyframes shake { 10%, 90% { transform: translateX(-3px); } 30%, 70% { transform: translateX(5px); } 50% { transform: translateX(-5px); } }
@keyframes groupIn { from { opacity: 0; transform: translateY(-7px) scale(.985); } }
@keyframes barIn { from { transform: scaleY(0); } }
@keyframes resultIn { from { opacity: 0; transform: translateY(10px); } }
@keyframes fadeStep { to { opacity: 0; } }
@keyframes ready { 0%, 100% { transform: scale(1); } 50% { transform: scale(1.04); } }
@keyframes scoreDrop { 0% { opacity: 0; transform: translateY(-7px); } 22% { opacity: 1; transform: translateY(0); } 100% { opacity: 0; transform: translateY(15px); } }
@keyframes starPop {
  0%   { opacity: 0; transform: scale(0) rotate(-70deg); }
  50%  { opacity: 1; transform: scale(1.55) rotate(10deg); }
  72%  { transform: scale(.86) rotate(-4deg); }
  88%  { transform: scale(1.08) rotate(1deg); }
  100% { opacity: 1; transform: scale(1) rotate(0); }
}
@keyframes starRing {
  0%   { opacity: 0; transform: translate(-50%, -50%) scale(.2); }
  25%  { opacity: .9; }
  100% { opacity: 0; transform: translate(-50%, -50%) scale(2.2); }
}
@keyframes starSparks {
  0% {
    opacity: 0;
    box-shadow: 0 -4px 0 0 var(--star), 4px 0 0 0 var(--star),
                0 4px 0 0 var(--star), -4px 0 0 0 var(--star);
  }
  22% { opacity: 1; }
  100% {
    opacity: 0;
    box-shadow: 0 -15px 0 -1.5px var(--star), 15px 0 0 -1.5px var(--star),
                0 15px 0 -1.5px var(--star), -15px 0 0 -1.5px var(--star);
  }
}

@media (prefers-reduced-motion: no-preference) {
  .chip.is-pop { animation: pop .19s var(--ease); }
  .chip.is-right { animation: rightPop .42s var(--ease) both; }
  .chip.is-leaving { animation: leave .2s ease-in both; }
  .chip.is-wrong { animation: shake .4s ease-in-out; }
  .bucket.is-full .bucket__tab { animation: ready 1.6s var(--ease) 2; }
  .score__drop { animation: scoreDrop .95s cubic-bezier(.3, .5, .4, 1) both; }

  .star.is-pop { animation: starPop .58s var(--ease) both; }

  .star.is-pop::before,
  .star.is-pop::after {
    content: "";
    position: absolute;
    left: 50%;
    top: 45%;
    pointer-events: none;
  }

  .star.is-pop::before {
    width: 20px;
    height: 20px;
    border: 2px solid var(--star);
    border-radius: 50%;
    transform-origin: 50% 50%;
    animation: starRing .55s ease-out .04s both;
  }

  .star.is-pop::after {
    width: 3px;
    height: 3px;
    margin: -1.5px 0 0 -1.5px;
    border-radius: 50%;
    animation: starSparks .6s ease-out .06s both;
  }
  .group.is-new { animation: groupIn .32s var(--ease) both; }
  .group.is-new::before { animation: barIn .38s var(--ease) .06s both; }
  #result.is-new { animation: resultIn .34s var(--ease) both; }
}

@media (prefers-reduced-motion: reduce) {
  .score__drop { animation: fadeStep .85s linear .1s both; }
  .chip.is-right { animation: fadeStep .01s linear both; opacity: 1; }
  .chip.is-leaving { animation: fadeStep .01s linear both; }
  .chip.is-wrong { animation: fadeStep .01s linear; }
  .chip.is-pop { animation: fadeStep .01s linear; }
}

@media (max-width: 380px) {
  :root {
    --gap: 7px;
    --slot-h: 46px;
  }

  body { padding-left: 9px; padding-right: 9px; }
  #title { font-size: 23px; }
  #howto { font-size: 12.5px; }
  .board-section { margin-bottom: 15px; }
  #pool { gap: 6px; padding-block: 8px; }
  #pool > .chip,
  #pool > .hole { min-width: 68px; height: 40px; padding-inline: 9px; }
  .bucket { padding: 46px 6px 6px; border-radius: 11px; }
  .bucket::before { left: 8px; font-size: 9px; letter-spacing: .06em; }
  .bucket__slots { gap: 5px; }
  .bucket__tab { right: 2px; min-width: 50px; padding-inline: 7px; }
  .well__header { padding-left: 8px; }
  button { padding-inline: 8px; font-size: 13px; }
}
