/* Pocket Piano.

   One octave and a bit, in the warm book palette the sandpit already uses.
   The design constraint is the player: two years old, on a tablet, with a
   finger that does not aim. So the keys are enormous, the black keys are
   reachable from either side, and nothing on screen can be in the wrong
   state. */

:root {
  --sand-50: #f7f0e2;
  --sand-100: #efe4cd;
  --sand-200: #e8d9bd;
  --ink-900: #3a2e20;
  --ink-500: #6f5a3f;
  --white-key: #fffaf0;
  --white-key-down: #e8d9bd;
  --black-key: #3a2e20;
  --black-key-down: #5c4a35;
  --record: #c0563a;
  --record-live: #e0472a;
  --radius: 16px;
  --shadow-key: 0 3px 0 rgba(90, 70, 50, 0.28), 0 8px 18px rgba(90, 70, 50, 0.18);
  --shadow-key-down: 0 1px 0 rgba(90, 70, 50, 0.3);
}

* { box-sizing: border-box; }

html, body {
  height: 100%;
  margin: 0;
  overscroll-behavior: none;
  -webkit-tap-highlight-color: transparent;
  -webkit-user-select: none;
  user-select: none;
}

body {
  display: flex;
  flex-direction: column;
  background: var(--sand-100);
  color: var(--ink-900);
  font-family: ui-rounded, "SF Pro Rounded", ui-sans-serif, -apple-system,
    "Segoe UI", Roboto, sans-serif;
  touch-action: none; /* we handle the drag ourselves */
  overflow: hidden;
}

/* ---- lid ------------------------------------------------------------- */

.lid {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  padding: calc(14px + env(safe-area-inset-top)) clamp(16px, 4vw, 32px) 10px;
}

.lid__name {
  margin: 0;
  font-size: clamp(19px, 3.2vw, 26px);
  font-weight: 700;
  letter-spacing: -0.015em;
}

/* ---- record ---------------------------------------------------------- */

.rec {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  min-height: 44px; /* thumb */
  padding: 0 16px;
  border: 1px solid rgba(90, 70, 50, 0.22);
  border-radius: 999px;
  background: var(--sand-50);
  color: var(--ink-900);
  font: 600 15px/1 inherit;
  cursor: pointer;
  transition: background 0.15s ease, border-color 0.15s ease, transform 0.1s ease;
}
.rec:active { transform: scale(0.95); }
.rec:focus-visible { outline: 3px solid var(--record); outline-offset: 2px; }

.rec__dot {
  width: 12px;
  height: 12px;
  border-radius: 50%;
  background: var(--record);
  transition: background 0.15s ease, box-shadow 0.15s ease;
}
.rec[aria-pressed="true"] {
  background: #fff2ec;
  border-color: var(--record-live);
}
.rec[aria-pressed="true"] .rec__dot {
  background: var(--record-live);
  box-shadow: 0 0 0 0 rgba(224, 71, 42, 0.6);
  animation: pulse 1.4s ease-out infinite;
}
@keyframes pulse {
  70% { box-shadow: 0 0 0 12px rgba(224, 71, 42, 0); }
  100% { box-shadow: 0 0 0 0 rgba(224, 71, 42, 0); }
}

/* ---- keys ------------------------------------------------------------ */

/* The row is the hit area. flex:1 makes every key the same width, which is
   what a small hand needs: no hunting for the C. */
.keys {
  flex: 1;
  display: flex;
  position: relative;
  padding: 0 clamp(8px, 2vw, 18px) calc(12px + env(safe-area-inset-bottom));
  gap: clamp(3px, 0.6vw, 6px);
  min-height: 0;
}

.key {
  flex: 1;
  position: relative;
  display: flex;
  align-items: flex-end;
  justify-content: center;
  min-width: 0;
  border: 0;
  border-radius: 0 0 10px 10px;
  background: var(--white-key);
  color: var(--ink-500);
  font: 600 clamp(10px, 1.6vw, 13px)/1 inherit;
  padding-bottom: 10px;
  cursor: pointer;
  box-shadow: var(--shadow-key);
  transition: background 0.06s ease, box-shadow 0.06s ease;
  touch-action: none;
}

/* The label sits at the bottom so the top of every key stays a clean target. */
.key__name { pointer-events: none; }

.key[data-down="true"] {
  background: var(--white-key-down);
  box-shadow: var(--shadow-key-down);
  transform: translateY(2px);
}

/* Black keys are absolutely positioned so they overlap the white ones
   without breaking the flex geometry. --left and --shift are set per key in
   JS: --left places the key on the seam between two white keys, and
   --shift is the extra nudge that keeps it centred there. A black key is
   narrower than the gap it straddles, so centring it on the seam -- not on
   either key -- is what makes the pair read as one piano. */
.key--black {
  position: absolute;
  top: 0;
  flex: none;
  width: 6.2%;
  min-width: 20px;
  max-width: 44px;
  height: 56%;
  z-index: 2;
  border-radius: 0 0 8px 8px;
  background: var(--black-key);
  color: transparent;
  padding-bottom: 0;
  box-shadow: 0 3px 0 rgba(0, 0, 0, 0.3), 0 8px 14px rgba(0, 0, 0, 0.28);
  /* JS sets --seam to the midpoint between the two white keys this one
     belongs to, as a percentage of the row. Centre on that, not on a key. */
  left: var(--seam, 0%);
  transform: translate(-50%, 0);
}
.key--black[data-down="true"] {
  background: var(--black-key-down);
  box-shadow: 0 1px 0 rgba(0, 0, 0, 0.3);
  /* translate only, never scale: scaling a key would resize its hit area and
     move the seam the neighbours are aligned to. */
  transform: translate(-50%, 2px);
}

/* ---- gate ------------------------------------------------------------ */

/* A soft hint, not a modal. It sits under the keys and never blocks a tap,
   because a start gate that swallows the first note is a gate that gets
   tapped through and a note that never sounds. */
.gate {
  position: fixed;
  left: 50%;
  bottom: calc(20px + env(safe-area-inset-bottom));
  transform: translateX(-50%);
  padding: 10px 18px;
  border-radius: 999px;
  background: rgba(58, 46, 32, 0.82);
  color: #fdf3e0;
  font: 600 14px/1 inherit;
  pointer-events: none;
  transition: opacity 0.4s ease, transform 0.4s ease;
  z-index: 3;
}
.gate[data-open="false"] {
  opacity: 0;
  transform: translate(-50%, 10px);
}
.gate__text { margin: 0; }

/* Wider screens: cap the key width so a desktop does not get 21 enormous
   keys, and give the lid a little more room. */
@media (min-width: 900px) {
  .keys { max-width: 1200px; margin-inline: auto; width: 100%; }
  /* White keys only. Capping a black key to the same width as a white one
     stops it reading as the same instrument, and this rule is why
     .key--black needs `flex: none` -- otherwise it wins and they go fat. */
  .key:not(.key--black) { max-width: 120px; }
}

@media (prefers-reduced-motion: reduce) {
  .rec[aria-pressed="true"] .rec__dot { animation: none; }
  .key, .rec, .gate { transition: none; }
}
