:root {
  color-scheme: light;
  --paper: #f4f1e8;
  --ink: #172021;
  --muted: #77766e;
  --line: rgba(23, 32, 33, .14);
  --coral: #f06f58;
  --coral-dark: #a83e30;
  --mint: #5fc5a4;
  --mint-dark: #22735d;
  --violet: #806ee8;
  --violet-dark: #4b3ba5;
  font-family: Inter, ui-rounded, "SF Pro Rounded", "Segoe UI", system-ui, sans-serif;
  font-synthesis: none;
  -webkit-tap-highlight-color: transparent;
}

* { box-sizing: border-box; }

html, body {
  width: 100%;
  min-height: 100%;
  margin: 0;
  overflow: hidden;
  background: var(--paper);
  color: var(--ink);
}

button, a { font: inherit; }
button { color: inherit; }

.app {
  position: relative;
  width: 100%;
  height: 100vh;
  height: 100svh;
  min-height: 460px;
  overflow: hidden;
  isolation: isolate;
  background:
    radial-gradient(circle at 12% 88%, rgba(255,255,255,.58) 0 1px, transparent 1.5px) 0 0 / 18px 18px,
    var(--paper);
}

.app::before {
  content: "";
  position: absolute;
  inset: 0;
  z-index: -1;
  pointer-events: none;
  opacity: .3;
  background-image: url("data:image/svg+xml,%3Csvg viewBox='0 0 120 120' xmlns='http://www.w3.org/2000/svg'%3E%3Cfilter id='n'%3E%3CfeTurbulence type='fractalNoise' baseFrequency='.82' numOctaves='2' stitchTiles='stitch'/%3E%3C/filter%3E%3Crect width='100%25' height='100%25' filter='url(%23n)' opacity='.055'/%3E%3C/svg%3E");
}

.topbar {
  position: absolute;
  inset: max(18px, env(safe-area-inset-top)) max(18px, env(safe-area-inset-right)) auto max(18px, env(safe-area-inset-left));
  z-index: 30;
  display: flex;
  align-items: center;
  justify-content: space-between;
  pointer-events: none;
}

.wordmark {
  display: flex;
  align-items: center;
  gap: 7px;
  min-height: 44px;
  color: var(--ink);
  font-size: 11px;
  font-weight: 850;
  letter-spacing: .15em;
  text-decoration: none;
  pointer-events: auto;
}

.wordmark i { width: 3px; height: 3px; border-radius: 50%; background: currentColor; }

.menu-button {
  width: 48px;
  height: 48px;
  display: grid;
  grid-template-columns: repeat(3, 5px);
  place-content: center;
  gap: 4px;
  border: 1px solid var(--line);
  border-radius: 50%;
  background: rgba(255,255,255,.54);
  box-shadow: 0 5px 16px rgba(32, 33, 27, .06);
  cursor: pointer;
  pointer-events: auto;
  transition: transform .16s ease, background .16s ease;
}

.menu-button:hover { background: #fff; transform: rotate(5deg); }
.menu-button:active { transform: scale(.92); }
.menu-button span { width: 5px; height: 5px; border-radius: 50%; background: var(--ink); }
.menu-button:focus-visible, button:focus-visible, .magnet:focus-visible { outline: 3px solid #2b70d9; outline-offset: 3px; }

.toy {
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
  padding: 76px max(20px, env(safe-area-inset-right)) max(24px, env(safe-area-inset-bottom)) max(20px, env(safe-area-inset-left));
  opacity: 0;
  transform: scale(.975);
  transition: opacity .18s ease, transform .28s cubic-bezier(.22,.8,.3,1);
}

.toy[hidden] { display: none; }
.toy.is-active { opacity: 1; transform: none; }

.toy-heading {
  position: absolute;
  left: max(22px, calc(env(safe-area-inset-left) + 6px));
  top: clamp(80px, 13vh, 126px);
  display: flex;
  align-items: center;
  gap: 10px;
  writing-mode: vertical-rl;
  transform: rotate(180deg);
  color: var(--muted);
  opacity: .76;
  z-index: 2;
}

.toy-heading h1 { margin: 0; font-size: 10px; letter-spacing: .21em; text-transform: uppercase; }
.toy-number { font: 600 9px/1 ui-monospace, monospace; }

.hint {
  position: absolute;
  left: 50%;
  bottom: max(20px, calc(env(safe-area-inset-bottom) + 8px));
  margin: 0;
  color: var(--muted);
  font: 650 10px/1 ui-monospace, monospace;
  letter-spacing: .12em;
  text-transform: uppercase;
  transform: translateX(-50%);
  transition: opacity .4s ease, transform .4s ease;
  white-space: nowrap;
  z-index: 2;
}

.hint::before { content: ""; display: inline-block; width: 18px; height: 1px; margin: 0 8px 3px 0; background: currentColor; }
.hint.is-used { opacity: 0; transform: translate(-50%, 5px); }

/* Keyboard */
.toy--keys { --toy-accent: var(--coral); }

.keyboard {
  width: min(620px, calc(100vw - 96px));
  display: grid;
  grid-template-columns: repeat(5, minmax(56px, 1fr));
  gap: clamp(9px, 1.8vmin, 14px);
  perspective: 900px;
  user-select: none;
  touch-action: manipulation;
}

.key {
  position: relative;
  aspect-ratio: 1.08;
  padding: 0;
  border: 0;
  border-radius: clamp(14px, 2.5vmin, 22px);
  background: var(--key-edge, #c74c3c);
  box-shadow: 0 7px 0 var(--key-shadow, #9e3327), 0 12px 21px rgba(47, 33, 27, .14);
  cursor: pointer;
  transform: translateY(-3px);
  transition: transform 65ms ease-out, box-shadow 65ms ease-out;
  will-change: transform;
}

.key::before {
  content: "";
  position: absolute;
  inset: 3px 3px 7px;
  border-radius: inherit;
  background: var(--key-face, #f6826b);
  box-shadow: inset 0 2px rgba(255,255,255,.24);
}

.key span {
  position: relative;
  z-index: 1;
  display: grid;
  place-items: center;
  width: 100%;
  height: 100%;
  font-size: clamp(26px, 5.5vmin, 43px);
  filter: drop-shadow(0 1px 0 rgba(255,255,255,.3));
  transform: translateY(-1px);
  pointer-events: none;
}

.key:nth-child(4n + 2) { --key-face: #fd947d; --key-edge: #d45a47; }
.key:nth-child(4n + 3) { --key-face: #ee795f; --key-edge: #c44b39; }
.key:nth-child(7n) { --key-face: #ffab82; --key-edge: #d86b49; }
.key:hover { filter: brightness(1.035); }
.key.is-pressed, .key:active {
  transform: translateY(4px) scale(.985, .97);
  box-shadow: 0 1px 0 var(--key-shadow, #9e3327), 0 4px 9px rgba(47, 33, 27, .12);
}

/* Magnet */
.toy--magnet { --toy-accent: var(--mint); }
.magnet-wrap { position: relative; width: clamp(116px, 18vw, 148px); height: min(65vh, 530px); min-height: 320px; display: grid; place-items: center; }

.magnet {
  position: relative;
  z-index: 2;
  width: clamp(88px, 13vw, 110px);
  height: 100%;
  border: 2px solid rgba(23,32,33,.19);
  border-radius: 999px;
  background: rgba(255,255,255,.33);
  box-shadow: inset 0 0 0 8px rgba(255,255,255,.38), 0 18px 40px rgba(39,73,63,.08);
  touch-action: none;
  cursor: grab;
  user-select: none;
}

.magnet:active { cursor: grabbing; }
.magnet-well { position: absolute; inset: 19px; border-radius: 999px; background: rgba(32,91,74,.08); box-shadow: inset 0 2px 8px rgba(20,65,52,.08); }
.magnet-well::before, .magnet-well::after { content: ""; position: absolute; left: 50%; width: 9px; height: 9px; border-radius: 50%; background: var(--mint-dark); opacity: .24; transform: translateX(-50%); }
.magnet-well::before { top: 10px; }
.magnet-well::after { bottom: 10px; }

.magnet-bead {
  position: absolute;
  left: 50%;
  top: 18px;
  width: clamp(54px, 8vw, 68px);
  aspect-ratio: 1;
  border-radius: 50%;
  background: var(--mint);
  box-shadow: 0 7px 0 #369678, 0 12px 20px rgba(27,85,67,.2), inset 0 3px 0 rgba(255,255,255,.3);
  transform: translate(-50%, var(--bead-y, 0px));
  will-change: transform;
  pointer-events: none;
}

.magnet-bead span { position: absolute; left: 23%; top: 18%; width: 23%; height: 14%; border-radius: 50%; background: rgba(255,255,255,.34); transform: rotate(-30deg); }
.magnet-bead.is-hit-top { animation: bead-hit-top 170ms ease-out; }
.magnet-bead.is-hit-bottom { animation: bead-hit-bottom 170ms ease-out; }
.magnet.is-impact { animation: track-knock 125ms ease-out; }

@keyframes bead-hit-top {
  0% { transform: translate(-50%, var(--bead-y)) scale(1); }
  22% { transform: translate(-50%, calc(var(--bead-y) - 5px)) scale(1.1,.84); }
  55% { transform: translate(-50%, calc(var(--bead-y) + 4px)) scale(.94,1.07); }
  100% { transform: translate(-50%, var(--bead-y)) scale(1); }
}
@keyframes bead-hit-bottom {
  0% { transform: translate(-50%, var(--bead-y)) scale(1); }
  22% { transform: translate(-50%, calc(var(--bead-y) + 5px)) scale(1.1,.84); }
  55% { transform: translate(-50%, calc(var(--bead-y) - 4px)) scale(.94,1.07); }
  100% { transform: translate(-50%, var(--bead-y)) scale(1); }
}
@keyframes track-knock { 35% { transform: translateY(var(--knock, 2px)); } }

.magnet-field { position: absolute; left: 50%; width: 126px; height: 42px; transform: translateX(-50%); opacity: .2; }
.magnet-field--top { top: -24px; }
.magnet-field--bottom { bottom: -24px; transform: translateX(-50%) rotate(180deg); }
.magnet-field span { position: absolute; inset: auto 0 0; margin: auto; border: 1px solid var(--mint-dark); border-bottom: 0; border-radius: 50% 50% 0 0; }
.magnet-field span:nth-child(1) { width: 45px; height: 15px; }
.magnet-field span:nth-child(2) { width: 80px; height: 26px; }
.magnet-field span:nth-child(3) { width: 118px; height: 38px; }

/* Blob */
.toy--blob { --toy-accent: var(--violet); }
.blob-stage { position: absolute; inset: 0; overflow: hidden; touch-action: none; user-select: none; }
.blob-stage svg { display: block; width: 100%; height: 100%; cursor: none; touch-action: none; }

/* Menu */
.menu-scrim { position: absolute; inset: 0; z-index: 38; background: rgba(29,28,24,.12); backdrop-filter: blur(2px); opacity: 0; transition: opacity .18s ease; }
.menu-scrim.is-open { opacity: 1; }
.menu {
  position: absolute;
  z-index: 40;
  top: max(76px, calc(env(safe-area-inset-top) + 64px));
  right: max(18px, env(safe-area-inset-right));
  width: min(310px, calc(100vw - 36px));
  padding: 10px;
  border: 1px solid rgba(23,32,33,.12);
  border-radius: 24px;
  background: rgba(255,253,247,.94);
  box-shadow: 0 22px 70px rgba(32,31,26,.17), 0 2px 8px rgba(32,31,26,.05);
  backdrop-filter: blur(16px);
  opacity: 0;
  transform: translateY(-8px) scale(.97);
  transform-origin: top right;
  transition: opacity .16s ease, transform .2s cubic-bezier(.2,.8,.2,1);
}
.menu.is-open { opacity: 1; transform: none; }
.menu nav { display: grid; gap: 3px; }
.menu-item, .setting { width: 100%; border: 0; background: transparent; cursor: pointer; }
.menu-item { display: grid; grid-template-columns: 38px 1fr auto; gap: 10px; align-items: center; min-height: 52px; padding: 7px 11px 7px 7px; border-radius: 16px; text-align: left; font-size: 14px; font-weight: 720; }
.menu-item:hover { background: rgba(23,32,33,.055); }
.menu-item.is-selected { background: #fff; box-shadow: 0 2px 10px rgba(23,32,33,.06); }
.menu-item small { color: #99978e; font: 600 9px/1 ui-monospace, monospace; }
.menu-icon { width: 36px; height: 36px; display: grid; place-items: center; border-radius: 12px; color: #fff; font-weight: 800; }
.menu-icon--keys { background: var(--coral); }
.menu-icon--magnet { background: var(--mint); font-size: 22px; }
.menu-icon--blob { background: var(--violet); }
.menu-icon--blob::after { content: ""; width: 17px; height: 15px; border-radius: 48% 52% 58% 42%; background: white; transform: rotate(15deg); }
.menu-rule { height: 1px; margin: 10px 8px 7px; background: var(--line); }
.setting { display: grid; grid-template-columns: 1fr 34px 28px; gap: 10px; align-items: center; min-height: 43px; padding: 5px 12px; border-radius: 13px; text-align: left; font-size: 12px; font-weight: 700; }
.setting:hover { background: rgba(23,32,33,.045); }
.setting strong { color: var(--muted); font-size: 10px; text-transform: uppercase; }
.switch { position: relative; width: 32px; height: 19px; border-radius: 99px; background: #b6b4ab; transition: background .15s; }
.switch i { position: absolute; top: 3px; left: 3px; width: 13px; height: 13px; border-radius: 50%; background: #fff; box-shadow: 0 1px 3px rgba(0,0,0,.18); transition: transform .16s cubic-bezier(.2,.8,.2,1); }
.setting[aria-pressed="true"] .switch { background: var(--ink); }
.setting[aria-pressed="true"] .switch i { transform: translateX(13px); }
.menu-note { margin: 12px 12px 7px; color: #8c8a81; font: 500 9px/1.4 ui-monospace, monospace; text-transform: uppercase; letter-spacing: .06em; }

.sr-only { position: absolute !important; width: 1px; height: 1px; padding: 0; margin: -1px; overflow: hidden; clip: rect(0,0,0,0); white-space: nowrap; border: 0; }

@media (max-width: 650px) {
  .keyboard { width: min(82vw, 450px); grid-template-columns: repeat(4, 1fr); gap: 10px; }
  .key:nth-child(n + 17) { display: none; }
  .toy-heading { left: 13px; }
}

@media (max-width: 390px), (max-height: 620px) {
  .keyboard { width: min(80vw, 390px); grid-template-columns: repeat(4, 1fr); gap: 8px; }
  .key { border-radius: 13px; box-shadow: 0 5px 0 var(--key-shadow, #9e3327), 0 8px 14px rgba(47,33,27,.12); }
  .key:nth-child(n + 17) { display: none; }
  .magnet-wrap { height: min(61vh, 390px); min-height: 265px; }
}

@media (orientation: landscape) and (max-height: 580px) {
  .keyboard { width: min(65vw, 590px); grid-template-columns: repeat(5, 1fr); gap: 8px; }
  .key:nth-child(n) { display: block; }
  .key { aspect-ratio: 1.3; }
  .magnet-wrap { height: 70vh; min-height: 260px; }
  .hint { right: 18px; left: auto; bottom: 18px; transform: none; }
  .hint.is-used { transform: translateY(5px); }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { scroll-behavior: auto !important; }
  .toy, .menu, .menu-scrim { transition-duration: .01ms; }
  .magnet-bead.is-hit-top, .magnet-bead.is-hit-bottom, .magnet.is-impact { animation-duration: 100ms; }
}
