/* Ball Sort - an apothecary's rack. Dark lacquer, brass, parchment, glass. */

:root {
  --ink:        #12100C;
  --ink-2:      #1B160F;
  --ink-3:      #241D14;
  --parchment:  #F0E4CC;
  --parchment-2:#C3B294;
  --parchment-3:#8B7C64;
  --brass:      #C79A45;
  --brass-2:    #8A6C31;
  --crimson:    #A9342B;

  /* Geometry is published from JS at layout time; these are only a fallback. */
  --ball: 36px;
  --pad: 5px;
  --gap-y: 3px;
  --gap-x: 12px;
  --capacity: 4;

  --shell: 1px solid color-mix(in srgb, var(--brass) 34%, transparent);
}

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

html, body { height: 100%; }

body {
  margin: 0;
  font-family: 'IBM Plex Mono', ui-monospace, 'SFMono-Regular', Menlo, monospace;
  color: var(--parchment);
  background:
    radial-gradient(130% 90% at 50% -20%, #33281D 0%, #1D1710 46%, #0D0A07 100%);
  background-attachment: fixed;
  -webkit-font-smoothing: antialiased;
  overflow-x: hidden;
}

[hidden] { display: none !important; }

/* Film of dust over the whole cabinet. */
.grain {
  position: fixed;
  inset: 0;
  z-index: 1;
  pointer-events: none;
  opacity: .3;
  mix-blend-mode: overlay;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='140' height='140'%3E%3Cfilter id='n'%3E%3CfeTurbulence type='fractalNoise' baseFrequency='.85' numOctaves='3'/%3E%3C/filter%3E%3Crect width='140' height='140' filter='url(%23n)' opacity='.5'/%3E%3C/svg%3E");
}

/* -------------------------------------------------------------- layout -- */

.page {
  position: relative;
  z-index: 2;
  min-height: 100%;
  min-height: 100dvh;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: clamp(10px, 2.2vh, 26px);
  padding: clamp(14px, 3vh, 34px) 16px calc(18px + env(safe-area-inset-bottom));
  max-width: 720px;
  margin-inline: auto;
}

/* ----------------------------------------------------------- masthead -- */

.masthead { text-align: center; width: 100%; }

.backlink {
  display: inline-block;
  font-size: 10px;
  letter-spacing: .28em;
  text-transform: uppercase;
  color: var(--parchment-3);
  text-decoration: none;
  border-bottom: 1px solid transparent;
  padding-bottom: 2px;
  transition: color .2s, border-color .2s;
}
.backlink::before { content: '\27F5\2003'; }
.backlink:hover {
  color: var(--brass);
  border-bottom-color: color-mix(in srgb, var(--brass) 50%, transparent);
}

.title {
  font-family: 'Fraunces', Georgia, serif;
  font-variation-settings: 'SOFT' 50, 'opsz' 120, 'wght' 620;
  font-weight: 620;
  font-size: clamp(2.1rem, 9vw, 3.5rem);
  line-height: .95;
  margin: clamp(8px, 1.6vh, 16px) 0 0;
  letter-spacing: -.015em;
  color: var(--parchment);
  text-shadow: 0 1px 0 #000, 0 0 34px rgba(199, 154, 69, .18);
}

.stamp {
  display: flex;
  align-items: center;
  justify-content: center;
  flex-wrap: wrap;
  gap: 10px;
  margin: 10px 0 0;
  font-size: 10px;
  font-weight: 500;
  letter-spacing: .3em;
  text-transform: uppercase;
  color: var(--brass);
}
.stamp__rule {
  width: 18px;
  height: 1px;
  background: linear-gradient(90deg, transparent, var(--brass-2), transparent);
}

/* ------------------------------------------------------------ cabinet -- */

.cabinet {
  flex: 1 1 auto;
  width: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 0;
}

.board {
  width: 100%;
  max-width: 560px;
  height: 100%;
  display: flex;
  flex-direction: column;
  justify-content: center;
  gap: clamp(22px, 4vh, 40px);
}

.rack {
  position: relative;
  display: flex;
  justify-content: center;
  align-items: flex-end;
  gap: var(--gap-x);
}

/* The brass shelf each row of phials stands on. */
.rack::after {
  content: '';
  position: absolute;
  left: -4%;
  right: -4%;
  bottom: -9px;
  height: 2px;
  border-radius: 2px;
  background: linear-gradient(90deg,
    transparent, var(--brass-2) 10%, var(--brass) 50%, var(--brass-2) 90%, transparent);
  opacity: .45;
  box-shadow: 0 3px 10px rgba(0, 0, 0, .7);
}

/* -------------------------------------------------------------- phial -- */

.tube {
  position: relative;
  flex: 0 0 auto;
  width: calc(var(--ball) + var(--pad) * 2);
  height: calc(var(--capacity) * var(--ball) + (var(--capacity) - 1) * var(--gap-y) + var(--pad) * 2);
  padding: 0;
  border: 0;
  background: none;
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
  /* Tipping pivots at the base, the way a bottle does on a shelf. */
  transform-origin: 50% 100%;
  transition: transform .2s cubic-bezier(.3, 1.2, .5, 1);
}

/* Mid-pour the tube is driven entirely by the Web Animations API; a CSS
   transition here would fight it, and a leaning tube has to clear its
   neighbours. */
.tube.is-pouring {
  transition: none;
  z-index: 20;
}
.tube:focus-visible { outline: none; }
.tube:focus-visible .tube__glass {
  box-shadow:
    inset 0 0 0 1px rgba(240, 228, 204, .5),
    0 0 0 2px var(--brass);
}
.tube:hover { transform: translateY(-2px); }
.tube.is-source { transform: translateY(-4px); }

.tube__glass {
  position: absolute;
  inset: 0;
  border-radius:
    calc(var(--ball) * .18) calc(var(--ball) * .18)
    calc(var(--ball) * .72) calc(var(--ball) * .72);
  background:
    linear-gradient(102deg,
      rgba(255, 255, 255, .13) 0%,
      rgba(255, 255, 255, .03) 26%,
      rgba(255, 255, 255, 0) 48%,
      rgba(255, 255, 255, .02) 74%,
      rgba(255, 255, 255, .09) 100%),
    rgba(9, 7, 5, .55);
  box-shadow:
    inset 0 0 0 1px rgba(240, 228, 204, .17),
    inset 0 -14px 22px -14px rgba(0, 0, 0, .9),
    0 14px 22px -14px rgba(0, 0, 0, .95);
  transition: box-shadow .28s ease;
}

/* Specular streak down the left wall. */
.tube__glass::before {
  content: '';
  position: absolute;
  left: calc(var(--ball) * .13);
  top: calc(var(--ball) * .22);
  bottom: calc(var(--ball) * .3);
  width: 2px;
  border-radius: 2px;
  background: linear-gradient(180deg,
    rgba(255, 255, 255, .38), rgba(255, 255, 255, .06) 70%, transparent);
  pointer-events: none;
  z-index: 3;
}

/* The lip. */
.tube__glass::after {
  content: '';
  position: absolute;
  left: -1px;
  right: -1px;
  top: 0;
  height: calc(var(--ball) * .12);
  border-radius: inherit;
  border-bottom-left-radius: 0;
  border-bottom-right-radius: 0;
  background: linear-gradient(180deg, rgba(240, 228, 204, .22), transparent);
  pointer-events: none;
  z-index: 3;
}

.tube.is-source .tube__glass {
  box-shadow:
    inset 0 0 0 1px color-mix(in srgb, var(--brass) 75%, transparent),
    inset 0 -14px 22px -14px rgba(0, 0, 0, .9),
    0 0 26px -6px rgba(199, 154, 69, .55);
}

/* A caret hovering over every phial this pour could land in. */
.tube.is-target::before {
  content: '';
  position: absolute;
  left: 50%;
  top: calc(var(--ball) * -.6);
  width: 0;
  height: 0;
  border-left: 5px solid transparent;
  border-right: 5px solid transparent;
  border-top: 6px solid var(--brass);
  transform: translateX(-50%);
  animation: bob 1.1s ease-in-out infinite;
  z-index: 4;
}
.tube.is-target .tube__glass {
  box-shadow:
    inset 0 0 0 1px color-mix(in srgb, var(--brass) 45%, transparent),
    inset 0 -14px 22px -14px rgba(0, 0, 0, .9),
    0 10px 18px -14px rgba(0, 0, 0, .95);
}

@keyframes bob {
  0%, 100% { transform: translate(-50%, 0); opacity: .55; }
  50%      { transform: translate(-50%, 4px); opacity: 1; }
}

.tube.is-refused { animation: refuse .34s cubic-bezier(.36, .07, .19, .97); }
@keyframes refuse {
  10%, 90% { transform: translateX(-2px); }
  20%, 80% { transform: translateX(3px); }
  30%, 50%, 70% { transform: translateX(-5px); }
  40%, 60% { transform: translateX(5px); }
}

/* --------------------------------------------------------------- ball -- */

.ball {
  position: absolute;
  left: var(--pad);
  bottom: calc(var(--pad) + var(--i) * (var(--ball) + var(--gap-y)));
  width: var(--ball);
  height: var(--ball);
  border-radius: 50%;
  display: grid;
  place-items: center;
  font-size: calc(var(--ball) * .4);
  line-height: 1;
  color: rgba(0, 0, 0, .3);
  text-shadow: 0 1px 0 rgba(255, 255, 255, .16);
  user-select: none;
  background:
    radial-gradient(circle at 33% 26%, rgba(255, 255, 255, .6), rgba(255, 255, 255, 0) 44%),
    radial-gradient(circle at 50% 118%, var(--c-dark), rgba(0, 0, 0, 0) 62%),
    var(--c);
  box-shadow:
    inset 0 -.14em .34em rgba(0, 0, 0, .5),
    inset 0 .1em .2em rgba(255, 255, 255, .22),
    0 .1em .22em rgba(0, 0, 0, .45);
  transform-origin: 50% 100%;
  transition: transform .2s cubic-bezier(.3, 1.3, .5, 1), filter .2s ease;
  z-index: 1;
}

.ball.is-lifted {
  transform: translateY(calc(var(--ball) * -.62)) scale(1.04);
  filter: drop-shadow(0 8px 10px rgba(0, 0, 0, .55)) brightness(1.08);
  z-index: 2;
}

.fx {
  position: fixed;
  inset: 0;
  z-index: 40;
  pointer-events: none;
}

.ball--flying {
  position: fixed;
  transform-origin: 50% 50%;
  bottom: auto;
  z-index: 41;
  transition: none;
  filter: drop-shadow(0 10px 12px rgba(0, 0, 0, .5));
}

/* --------------------------------------------------------------- cork -- */

.tube__cork {
  position: absolute;
  left: calc(var(--pad) * .4);
  right: calc(var(--pad) * .4);
  top: calc(var(--ball) * -.16);
  height: calc(var(--ball) * .46);
  border-radius: calc(var(--ball) * .1);
  background:
    repeating-linear-gradient(112deg,
      rgba(0, 0, 0, .13) 0 2px, rgba(255, 255, 255, .06) 2px 4px),
    linear-gradient(180deg, #D3A76C 0%, #B8894F 42%, #8E6335 100%);
  box-shadow:
    inset 0 1px 0 rgba(255, 255, 255, .38),
    inset 0 -3px 6px rgba(0, 0, 0, .35),
    0 4px 8px -3px rgba(0, 0, 0, .8);
  opacity: 0;
  transform: translateY(-180%) rotate(-6deg);
  pointer-events: none;
  z-index: 5;
}

.tube.is-complete .tube__cork {
  opacity: 1;
  transform: translateY(0) rotate(0deg);
  transition:
    transform .42s cubic-bezier(.3, 1.5, .45, 1) .2s,
    opacity .1s linear .2s;
}

.tube.is-complete { cursor: default; }
.tube.is-complete:hover { transform: none; }

.tube.is-complete .tube__glass {
  box-shadow:
    inset 0 0 0 1px color-mix(in srgb, var(--brass) 68%, transparent),
    inset 0 -14px 22px -14px rgba(0, 0, 0, .9),
    0 0 22px -8px rgba(199, 154, 69, .6);
  animation: sealed .7s ease-out;
}
@keyframes sealed {
  0%   { box-shadow: inset 0 0 0 1px rgba(240, 228, 204, .17), 0 0 0 0 rgba(199, 154, 69, .6); }
  35%  { box-shadow: inset 0 0 0 2px var(--brass), 0 0 0 8px rgba(199, 154, 69, .22); }
  100% { box-shadow: inset 0 0 0 1px color-mix(in srgb, var(--brass) 68%, transparent), 0 0 22px -8px rgba(199, 154, 69, .6); }
}

/* ------------------------------------------------------------ console -- */

.console {
  width: 100%;
  max-width: 560px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 14px;
  padding-top: 8px;
  border-top: 1px solid rgba(199, 154, 69, .18);
}

.counter {
  margin: 0;
  display: flex;
  align-items: baseline;
  gap: 9px;
}
.counter__label {
  font-size: 10px;
  letter-spacing: .28em;
  text-transform: uppercase;
  color: var(--parchment-3);
}
.counter b {
  font-family: 'Fraunces', Georgia, serif;
  font-variation-settings: 'wght' 600;
  font-size: 1.5rem;
  color: var(--parchment);
  font-variant-numeric: tabular-nums;
  letter-spacing: .02em;
}

.actions { display: flex; gap: 8px; }

.btn {
  font: inherit;
  font-size: 10px;
  font-weight: 500;
  letter-spacing: .2em;
  text-transform: uppercase;
  color: var(--parchment-2);
  background: rgba(255, 255, 255, .015);
  border: var(--shell);
  border-radius: 2px;
  padding: 9px 12px;
  cursor: pointer;
  transition: color .18s, background .18s, border-color .18s, transform .1s;
}
.btn:hover:not(:disabled) {
  color: var(--ink);
  background: var(--brass);
  border-color: var(--brass);
}
.btn:active:not(:disabled) { transform: translateY(1px); }
.btn:disabled { opacity: .3; cursor: default; }
.btn:focus-visible { outline: 2px solid var(--brass); outline-offset: 2px; }
.btn[aria-pressed='false'] { color: var(--parchment-3); border-style: dashed; }

/* -------------------------------------------------------------- howto -- */

.howto {
  width: 100%;
  max-width: 560px;
  font-size: 12px;
  line-height: 1.65;
  color: var(--parchment-2);
}
.howto summary {
  cursor: pointer;
  list-style: none;
  font-size: 10px;
  letter-spacing: .28em;
  text-transform: uppercase;
  color: var(--parchment-3);
  padding: 4px 0;
  transition: color .2s;
}
.howto summary::-webkit-details-marker { display: none; }
.howto summary::before { content: '\FF0B\2002'; color: var(--brass-2); }
.howto[open] summary::before { content: '\FF0D\2002'; }
.howto summary:hover { color: var(--brass); }
.howto ul { margin: 10px 0; padding-left: 1.1em; }
.howto li { margin-bottom: 5px; }
.howto__note {
  color: var(--parchment-3);
  border-left: 1px solid var(--brass-2);
  padding-left: 12px;
}
.howto kbd {
  font: inherit;
  font-size: 10px;
  border: 1px solid rgba(199, 154, 69, .35);
  border-radius: 3px;
  padding: 1px 5px;
  color: var(--brass);
}

/* ---------------------------------------------------------------- win -- */

.win {
  position: fixed;
  inset: 0;
  z-index: 60;
  display: grid;
  place-items: center;
  padding: 20px;
  background: rgba(10, 8, 5, .72);
  backdrop-filter: blur(7px) saturate(.8);
  -webkit-backdrop-filter: blur(7px) saturate(.8);
  animation: fade .4s ease both;
}
.win.is-instant { animation: none; }
@keyframes fade { from { opacity: 0; } to { opacity: 1; } }

.win__card {
  position: relative;
  width: min(400px, 100%);
  text-align: center;
  padding: 46px 30px 30px;
  color: #2A2117;
  border-radius: 3px;
  background:
    radial-gradient(120% 90% at 50% 0%, #FBF2DF 0%, #EFE1C4 60%, #E2D2B2 100%);
  box-shadow:
    0 30px 60px -20px rgba(0, 0, 0, .8),
    inset 0 0 0 1px rgba(255, 255, 255, .5),
    inset 0 0 0 6px rgba(169, 52, 43, .07);
  animation: rise .5s cubic-bezier(.25, 1.2, .4, 1) both;
}
.win.is-instant .win__card { animation: none; }
@keyframes rise {
  from { opacity: 0; transform: translateY(22px) scale(.96); }
  to   { opacity: 1; transform: none; }
}

.seal {
  position: absolute;
  top: -26px;
  left: 50%;
  width: 62px;
  height: 62px;
  transform: translateX(-50%) rotate(-8deg);
  border-radius: 50%;
  display: grid;
  place-items: center;
  color: rgba(255, 226, 216, .78);
  font-size: 22px;
  background: radial-gradient(circle at 34% 28%, #C4463A, #8E2A22 62%, #6E1E18 100%);
  box-shadow:
    inset 0 -3px 8px rgba(0, 0, 0, .5),
    inset 0 3px 6px rgba(255, 255, 255, .25),
    0 8px 16px -6px rgba(0, 0, 0, .7);
  clip-path: polygon(
    50% 0%, 66% 6%, 82% 4%, 92% 18%, 100% 32%, 96% 50%, 100% 68%, 90% 82%,
    80% 96%, 62% 96%, 46% 100%, 30% 94%, 16% 90%, 8% 74%, 0% 56%, 4% 38%,
    2% 20%, 18% 10%, 34% 6%);
}

.win__card h2 {
  font-family: 'Fraunces', Georgia, serif;
  font-variation-settings: 'SOFT' 50, 'opsz' 140, 'wght' 700;
  font-size: 2.1rem;
  margin: 6px 0 12px;
  letter-spacing: -.01em;
}

.win__line { margin: 0 0 22px; font-size: 12px; line-height: 1.8; color: #574734; }
.win__line b { color: #2A2117; font-weight: 600; }

.win__next {
  margin: 0 0 22px;
  padding: 12px 0;
  border-top: 1px solid rgba(42, 33, 23, .16);
  border-bottom: 1px solid rgba(42, 33, 23, .16);
}
.win__next-label {
  display: block;
  font-size: 9px;
  letter-spacing: .3em;
  text-transform: uppercase;
  color: #8A7458;
  margin-bottom: 6px;
}
.win__clock {
  font-size: 1.5rem;
  font-weight: 500;
  font-variant-numeric: tabular-nums;
  letter-spacing: .06em;
  color: #2A2117;
}

.win__actions {
  display: flex;
  justify-content: center;
  align-items: center;
  gap: 8px;
}

.btn--ink {
  color: #574734;
  border-color: rgba(42, 33, 23, .3);
  background: transparent;
}
.btn--ink:hover:not(:disabled) {
  background: #2A2117;
  border-color: #2A2117;
  color: #EFE1C4;
}

/* ------------------------------------------------------------- motion -- */

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: .01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: .01ms !important;
  }
}

@media (max-width: 400px) {
  .stamp { gap: 7px; font-size: 9px; letter-spacing: .22em; }
  .console { flex-direction: column; align-items: stretch; gap: 10px; }
  .counter { justify-content: center; }
  .actions { justify-content: center; }
}

/* ---------------------------------------------------------------- ledger -- */

/* The written half of the page: what the game is, and the way out to the
   others. Below the fold by design — the game gets the first screen. */
.ledger {
  position: relative;
  z-index: 2;
  max-width: 720px;
  margin: 0 auto;
  padding: 40px 20px calc(56px + env(safe-area-inset-bottom));
  display: grid;
  gap: 34px;
  border-top: 1px solid rgba(199, 154, 69, .16);
  font-size: 13px;
  line-height: 1.85;
  color: var(--parchment-2);
}

.ledger h2 {
  font-family: 'Fraunces', Georgia, serif;
  font-variation-settings: 'SOFT' 50, 'opsz' 90, 'wght' 600;
  font-weight: 600;
  font-size: 1.25rem;
  line-height: 1.2;
  margin: 0 0 14px;
  color: var(--parchment);
  letter-spacing: -.01em;
}

.ledger p { margin: 0 0 14px; }
.ledger p:last-child { margin-bottom: 0; }
.ledger strong { color: var(--parchment); font-weight: 500; }
.ledger em { color: var(--brass); font-style: italic; }

.ledger__more ul {
  list-style: none;
  margin: 0;
  padding: 0;
  display: grid;
  gap: 10px;
}
.ledger__more li {
  padding-left: 14px;
  border-left: 1px solid rgba(199, 154, 69, .22);
}
.ledger a {
  color: var(--brass);
  text-decoration: none;
  border-bottom: 1px solid transparent;
  transition: border-color .2s;
}
.ledger a:hover { border-bottom-color: var(--brass); }
.ledger__home { margin-top: 18px; font-size: 11px; letter-spacing: .2em; text-transform: uppercase; }

/* Spans both columns: the legal line belongs to the page, not to either half
   of it. Quieter than the links above — it is there to be found, not read. */
.ledger__legal {
  grid-column: 1 / -1;
  display: flex;
  flex-wrap: wrap;
  gap: 6px 18px;
  padding-top: 18px;
  border-top: 1px solid rgba(199, 154, 69, .12);
  font-size: 10px;
  letter-spacing: .18em;
  text-transform: uppercase;
}
.ledger__legal a { color: var(--parchment-3); }
.ledger__legal a:hover { color: var(--brass); }

@media (min-width: 760px) {
  .ledger { max-width: 940px; grid-template-columns: 1.7fr 1fr; gap: 48px; }
}
