* { box-sizing: border-box; }
[hidden] { display: none !important; }

html, body {
  margin: 0;
  height: 100%;
  overflow: hidden;
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  background: #2f6b4f;
  -webkit-user-select: none;
  user-select: none;
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation; /* no double-tap zoom, so taps respond instantly */
}
button {
  font: inherit;
  border: 0;
  padding: 0;
  margin: 0;
  background: none;
  cursor: pointer;
  touch-action: manipulation;
}
button:focus-visible, input:focus-visible { outline: 4px solid #ffd84a; outline-offset: 2px; }
svg { display: block; width: 100%; height: 100%; }

/* ---------- berries ---------- */
.berry {
  position: absolute;
  left: 50%;
  top: calc(4% + (5 - var(--p)) * 10%); /* higher note = higher berry */
  height: 46%;
  height: min(46cqh, 78cqw); /* never wider than its square (newer browsers) */
  aspect-ratio: 1;
  transform: translateX(-50%);
  border-radius: 50%;
  background: radial-gradient(circle at 35% 30%, #ffb0a2 0 10%, #e8261c 42%, #b3140f 100%);
  box-shadow: 0 2px 0 rgba(90, 20, 10, .25);
  pointer-events: none;
}

/* ---------- start screen ---------- */
.start {
  position: fixed;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 2vh;
  background: linear-gradient(#bfe6ff 0%, #e6f6d8 62%, #7fc46a 62%, #5aa64e 100%);
}
.start-btn {
  position: relative;
  width: min(62vmin, 340px);
  height: min(62vmin, 340px);
  border-radius: 50%;
  background: #fff8e7;
  box-shadow: 0 10px 0 #d9c9a3, 0 18px 30px rgba(0, 0, 0, .2);
  animation: breathe 1.8s ease-in-out infinite;
}
.start-btn:active { transform: translateY(6px); box-shadow: 0 4px 0 #d9c9a3; }
.start-teddy { position: absolute; inset: 8%; }
.start-teddy .t-arm { animation: wave 1.8s ease-in-out infinite; transform-origin: 84px 80px; }
.start-badge {
  position: absolute;
  right: -2%;
  bottom: -2%;
  width: 34%;
  height: 34%;
  padding: 6%;
  border-radius: 50%;
  background: #e8261c;
  box-shadow: 0 5px 0 #a3150e;
}
.title {
  margin: 0;
  font-size: clamp(28px, 7vmin, 56px);
  font-weight: 800;
  letter-spacing: .02em;
  color: #c11f15;
  text-shadow: 0 2px 0 #fff;
}
@keyframes breathe { 50% { transform: scale(1.04); } }
@keyframes wave { 50% { transform: rotate(-18deg); } }

/* ---------- about popup ---------- */
.about-btn {
  position: absolute;
  top: calc(12px + env(safe-area-inset-top));
  right: calc(12px + env(safe-area-inset-right));
  width: 56px;
  height: 56px;
  padding: 13px;
  border-radius: 50%;
  background: #fff8e7;
  box-shadow: 0 4px 0 #d9c9a3;
}
.about-btn:active { transform: translateY(3px); box-shadow: 0 1px 0 #d9c9a3; }
.about {
  position: fixed;
  inset: 0;
  z-index: 10;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px;
  background: rgba(20, 50, 35, .6);
}
.about-card {
  position: relative;
  width: min(100%, 440px);
  max-height: 100%;
  overflow-y: auto;
  padding: 16px 16px 20px;
  border-radius: 24px;
  background: #fff8e7;
  text-align: center;
  color: #3a2a20;
  box-shadow: 0 16px 40px rgba(0, 0, 0, .35);
}
.about-photo {
  display: block;
  width: 100%;
  height: auto;
  aspect-ratio: 1;
  border-radius: 16px;
  background: #e8dcc0;
  object-fit: cover;
}
.about-card h2 { margin: 14px 0 6px; font-size: 24px; color: #c11f15; }
.about-card p { margin: 0; font-size: 18px; line-height: 1.4; }
.about-card .about-contact { margin-top: 12px; font-size: 16px; }
.about-contact a { display: inline-block; padding: 8px 8px; color: #2f6b4f; font-weight: 600; overflow-wrap: anywhere; }
.about-close {
  position: absolute;
  top: 8px;
  right: 8px;
  z-index: 1;
  width: 52px;
  height: 52px;
  border-radius: 50%;
  padding: 15px;
  background: #e8261c;
  box-shadow: 0 4px 0 #8f120c;
}
@media (max-height: 560px) {
  .about-card { width: min(100%, 300px); }
}

/* ---------- main screen ---------- */
.app {
  height: 100vh;
  height: 100dvh;
  display: flex;
  flex-direction: column;
  padding: env(safe-area-inset-top) env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left);
}
.bar {
  flex: none;
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: center;
  gap: 8px 14px;
  padding: 8px 12px;
}
.teddy { width: 68px; height: 68px; flex: none; }
.teddy svg { transform-origin: 50% 100%; overflow: visible; }
.teddy .t-arm { transform-origin: 84px 80px; }

.btn {
  width: 68px;
  height: 68px;
  flex: none;
  padding: 14px;
  border-radius: 50%;
  background: #4d8f6c;
  box-shadow: 0 5px 0 #1f4a36;
}
.btn:active { transform: translateY(4px); box-shadow: 0 1px 0 #1f4a36; }
.btn-play { background: #e8261c; box-shadow: 0 5px 0 #8f120c; }
.btn-play:active { box-shadow: 0 1px 0 #8f120c; }
.btn-play.playing { background: #f2a22e; box-shadow: 0 5px 0 #a8660d; }
.btn-clear.undo { background: #5a7fe0; box-shadow: 0 5px 0 #2f4a99; animation: breathe .8s ease-in-out infinite; }

.btn-about { width: 48px; height: 48px; padding: 11px; background: #fff8e7; box-shadow: 0 5px 0 #d9c9a3; }
.btn-about:active { box-shadow: 0 1px 0 #d9c9a3; }

.tempo { display: flex; align-items: center; gap: 6px; flex: 0 1 300px; min-width: 140px; }
.tempo-icon { width: 44px; height: 44px; flex: none; }
.tempo input {
  flex: 1;
  min-width: 0;
  height: 48px;
  margin: 0;
  background: transparent;
  -webkit-appearance: none;
  appearance: none;
  touch-action: pan-y;
}
.tempo input::-webkit-slider-runnable-track { height: 14px; border-radius: 7px; background: #1f4a36; }
.tempo input::-moz-range-track { height: 14px; border-radius: 7px; background: #1f4a36; }
.tempo input::-webkit-slider-thumb {
  -webkit-appearance: none;
  width: 40px; height: 40px; margin-top: -13px;
  border-radius: 50%; border: 4px solid #fff; background: #e8261c;
}
.tempo input::-moz-range-thumb {
  width: 34px; height: 34px;
  border-radius: 50%; border: 4px solid #fff; background: #e8261c;
}

.pitches { display: flex; gap: 4px; padding: 4px; border-radius: 16px; background: #1f4a36; }
.pitch {
  position: relative;
  width: 46px;
  height: 60px;
  border-radius: 12px;
  background: #3d7a5c;
}
.pitch .berry { height: 40%; height: min(40cqh, 70cqw); }
.pitch.on { background: #fff8e7; box-shadow: 0 0 0 3px #ffd84a; }

.sets { display: flex; gap: 4px; padding: 4px; border-radius: 16px; background: #1f4a36; }
.set { width: 46px; height: 60px; padding: 7px; border-radius: 12px; background: #3d7a5c; }
.set.on { background: #fff8e7; box-shadow: 0 0 0 3px #ffd84a; }

/* ---------- the grid ---------- */
.gridwrap {
  flex: 1;
  min-height: 0;
  overflow-x: auto;
  overflow-y: hidden;
  padding: 0 8px 8px;
}
.grid {
  --cw: clamp(60px, 11vw, 112px); /* width of the character column */
  position: relative;
  display: flex;
  flex-direction: column;
  height: 100%;
  min-width: calc(var(--cw) + 16 * 38px); /* on narrow phones the grid scrolls sideways */
  border-radius: 16px;
  overflow: hidden;
  background: #fff;
}
.row { flex: 1; min-height: 0; display: flex; background: var(--row); }
.row + .row { border-top: 2px solid rgba(255, 255, 255, .9); }

.char {
  position: sticky;
  left: 0;
  z-index: 2;
  flex: none;
  width: var(--cw);
  padding: 3px;
  background: var(--row);
  border-right: 3px solid rgba(0, 0, 0, .12);
}
.char { overflow: hidden; -webkit-touch-callout: none; }
.char > * { pointer-events: none; }
/* Jail bars: slide down over a character that has been silenced */
.bars {
  position: absolute;
  inset: 0;
  border-top: 6px solid #4a4f57;
  border-bottom: 6px solid #4a4f57;
  background: repeating-linear-gradient(90deg, transparent 0, transparent 7%, #4a4f57 7%, #4a4f57 13%, transparent 13%, transparent 20%);
  transform: translateY(-105%);
  transition: transform .25s cubic-bezier(.3, 1.4, .6, 1);
}
.row.jailed .bars { transform: none; }
.row.jailed .char-art { opacity: .75; filter: grayscale(.5); }
.row.jailed .cell { opacity: .4; }
.grid.paintable { touch-action: none; } /* lets a finger slide along a row to paint berries */
.char-art { display: block; width: 100%; height: 100%; transform-origin: 50% 100%; }
.row.selected .char { background: #fff8e7; box-shadow: inset 0 0 0 4px #ffd84a; }

.cell, .pitch { container-type: size; }
.cell {
  position: relative;
  flex: 1;
  min-width: 0;
  border-left: 1px solid rgba(0, 0, 0, .07);
}
.cell.alt { background: rgba(0, 0, 0, .05); }
.cell.beat { border-left: 2px solid rgba(0, 0, 0, .16); }
.cell:active { background: rgba(255, 255, 255, .7); }

.playhead {
  --s: 0;
  position: absolute;
  top: 0;
  bottom: 0;
  left: var(--cw);
  width: calc((100% - var(--cw)) / 16);
  transform: translateX(calc(var(--s) * 100%));
  background: rgba(255, 216, 74, .45);
  box-shadow: inset 0 0 0 2px rgba(255, 190, 30, .8);
  pointer-events: none;
  z-index: 1;
}

/* Small screens: tighter top bar */
@media (max-height: 520px), (max-width: 620px) {
  .bar { gap: 6px 8px; padding: 6px 8px; }
  .teddy, .btn { width: 52px; height: 52px; }
  .btn { padding: 11px; }
  .btn-about { width: 44px; height: 44px; padding: 10px; }
  /* keep the heart on the top line beside play, not on a line of its own */
  .tempo, .pitches, .btn-clear { order: 1; }
  .pitch { width: 38px; height: 48px; }
  .set { width: 38px; height: 48px; padding: 5px; }
  .sets { order: 1; }
  .tempo-icon { width: 34px; height: 34px; }
  .tempo { min-width: 160px; }
}

@media (prefers-reduced-motion: reduce) {
  .start-btn, .start-teddy .t-arm, .btn-clear.undo { animation: none; }
}
