:root {
  --sat: env(safe-area-inset-top, 0px);
  --sab: env(safe-area-inset-bottom, 0px);
  --sal: env(safe-area-inset-left, 0px);
  --sar: env(safe-area-inset-right, 0px);
  --ink: #2b2a4c;
  --paper: rgba(255,255,255,0.92);
  --primary: #ff8a3d;
  --primary-dark: #d9631a;
  --secondary: #5ec8ff;
  --secondary-dark: #2a9bd8;
  --sub: #5f6bc4;
  --sub-dark: #3f4a9e;
  --accent: #ffd23f;
  --good: #3ddc84;
  --bad: #ff5c7a;
  --font: "Fredoka", "Nunito", "Varela Round", "Quicksand", "Baloo 2", ui-rounded, "SF Pro Rounded", system-ui, -apple-system, "Segoe UI", sans-serif;
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body {
  margin: 0; padding: 0; height: 100%;
  overflow: hidden;
  background: #5ec8ff;
  font-family: var(--font);
  color: var(--ink);
  touch-action: none;
  -webkit-user-select: none; user-select: none;
  -webkit-text-size-adjust: 100%;
  overscroll-behavior: none;
}
canvas#game {
  position: fixed; inset: 0;
  width: 100%; height: 100%;
  display: block;
}

#ui { position: fixed; inset: 0; pointer-events: none; }
#ui > * { pointer-events: auto; }

/* ---------- screens ---------- */
.screen {
  position: absolute; inset: 0;
  display: none;
  flex-direction: column;
  align-items: center;
  padding: calc(var(--sat) + 24px) calc(var(--sar) + 20px) calc(var(--sab) + 24px) calc(var(--sal) + 20px);
  overflow-y: auto;
  animation: pop-in .35s cubic-bezier(.2,1.4,.4,1);
}
.screen.active { display: flex; }
.screen.overlay { background: rgba(43,42,76,0.35); backdrop-filter: blur(4px); -webkit-backdrop-filter: blur(4px); justify-content: center; }
@keyframes pop-in { from { opacity: 0; transform: scale(.94) translateY(10px);} to { opacity: 1; transform: none; } }

.spacer { flex: 1; }
.screen-title {
  margin: 8px 0 4px; font-size: 2rem; font-weight: 800; color: #fff;
  text-shadow: 0 3px 0 rgba(0,0,0,.18), 0 6px 18px rgba(0,0,0,.18);
}
.sub { margin: 0 0 16px; color: #fff; text-align: center; font-weight: 600; text-shadow: 0 2px 6px rgba(0,0,0,.25); }
.hint { font-size: .85rem; opacity: .75; }
.error { color: var(--bad); font-weight: 700; }

/* ---------- logo ---------- */
.logo {
  margin-top: clamp(2px, 2.4vh, 36px);
  display: flex; flex-direction: column; align-items: center; line-height: .95;
  font-weight: 900; letter-spacing: -.02em;
}
.logo-word { display: flex; }
.logo-word span {
  display: inline-block;
  font-size: clamp(3.4rem, 17vw, 5.6rem);
  color: var(--accent);
  -webkit-text-stroke: 2px #7a3e00;
  text-shadow: 0 4px 0 #b85c00, 0 8px 0 rgba(0,0,0,.15), 0 14px 24px rgba(0,0,0,.25);
  animation: bounce 2.2s ease-in-out infinite;
}
.logo-word2 span { color: #fff; -webkit-text-stroke: 2px #1e5f9c; text-shadow: 0 4px 0 #2a9bd8, 0 8px 0 rgba(0,0,0,.15), 0 14px 24px rgba(0,0,0,.25); }
.logo-word span:nth-child(1){animation-delay:0s} .logo-word span:nth-child(2){animation-delay:.1s}
.logo-word span:nth-child(3){animation-delay:.2s} .logo-word span:nth-child(4){animation-delay:.3s}
.logo-word span:nth-child(5){animation-delay:.4s}
.logo-word2 span:nth-child(1){animation-delay:.5s} .logo-word2 span:nth-child(2){animation-delay:.6s}
.logo-word2 span:nth-child(3){animation-delay:.7s} .logo-word2 span:nth-child(4){animation-delay:.8s}
@keyframes bounce { 0%,100%{transform:translateY(0) rotate(-2deg)} 50%{transform:translateY(-12px) rotate(2deg)} }
.tagline { margin: 8px 0 0; font-size: 1.3rem; font-weight: 700; color: #fff; text-shadow: 0 2px 6px rgba(0,0,0,.3); }
.note-emoji { display: inline-block; animation: wiggle 1.4s ease-in-out infinite; }
@keyframes wiggle { 0%,100%{transform:rotate(-12deg)} 50%{transform:rotate(12deg)} }
.best-line { color: #fff; font-weight: 700; text-shadow: 0 2px 6px rgba(0,0,0,.3); margin: 12px 0 0; min-height: 1.4em; }

/* ---------- birthday edition (temporary) ---------- */
.birthday { display: none; flex-direction: column; align-items: center; margin-top: 12px; }
.birthday.on { display: flex; }

.bunting { display: flex; gap: 7px; margin-bottom: 7px; }
.bunting i {
  display: block; width: 15px; height: 19px;
  clip-path: polygon(0 0, 100% 0, 50% 100%);
  transform-origin: top center;
  animation: flutter 2.4s ease-in-out infinite;
  filter: drop-shadow(0 2px 2px rgba(0,0,0,.25));
}
.bunting i:nth-child(1) { background: #ff5c7a; animation-delay: 0s; }
.bunting i:nth-child(2) { background: #ffd23f; animation-delay: .12s; }
.bunting i:nth-child(3) { background: #3ddc84; animation-delay: .24s; }
.bunting i:nth-child(4) { background: #5ec8ff; animation-delay: .36s; }
.bunting i:nth-child(5) { background: #c39bff; animation-delay: .48s; }
.bunting i:nth-child(6) { background: #ff8a3d; animation-delay: .60s; }
.bunting i:nth-child(7) { background: #ff5c7a; animation-delay: .72s; }
@keyframes flutter { 0%,100% { transform: rotate(-7deg); } 50% { transform: rotate(7deg); } }

.birthday-ribbon {
  position: relative; overflow: hidden;
  display: flex; align-items: center; gap: 9px;
  padding: 9px 20px; border-radius: 999px;
  background: linear-gradient(100deg, #ff8a3d, #ff5c7a 34%, #c39bff 68%, #5ec8ff);
  border: 3px solid #fff;
  color: #fff; font-weight: 900; font-size: clamp(.95rem, 4.2vw, 1.15rem);
  white-space: nowrap;
  text-shadow: 0 2px 3px rgba(0,0,0,.4);
  box-shadow: 0 5px 0 rgba(0,0,0,.16), 0 12px 26px rgba(0,0,0,.3);
  animation: sway 3.4s ease-in-out infinite;
}
@keyframes sway {
  0%,100% { transform: rotate(-2.2deg) translateY(0); }
  50%     { transform: rotate(2.2deg) translateY(-4px); }
}
/* light sweeping across the ribbon */
.birthday-ribbon::after {
  content: ""; position: absolute; top: 0; bottom: 0; width: 45%;
  background: linear-gradient(100deg, transparent, rgba(255,255,255,.5), transparent);
  animation: shimmer 3.6s ease-in-out infinite;
}
@keyframes shimmer { 0% { left: -55%; } 55%,100% { left: 125%; } }

.birthday-text { position: relative; z-index: 1; }
.birthday-text b { color: #fff6b0; }
.party { position: relative; z-index: 1; display: inline-block; animation: party-pop 1.7s ease-in-out infinite; }
@keyframes party-pop {
  0%,100% { transform: scale(1) rotate(-9deg); }
  50%     { transform: scale(1.22) rotate(9deg); }
}
.birthday-sub {
  margin-top: 8px; padding: 3px 14px; border-radius: 999px;
  font-size: .72rem; font-weight: 800; letter-spacing: .16em;
  text-transform: uppercase; color: #fff;
  background: rgba(43,42,76,.42);
  backdrop-filter: blur(4px); -webkit-backdrop-filter: blur(4px);
  text-shadow: 0 1px 3px rgba(0,0,0,.5);
}
@media (prefers-reduced-motion: reduce) {
  .bunting i, .birthday-ribbon, .birthday-ribbon::after, .party { animation: none; }
}
/* short screens: trim the banner so it does not crowd the buttons */
@media (max-height: 700px) {
  .bunting { display: none; }
  .birthday { margin-top: 6px; }
  .birthday-ribbon { padding: 7px 16px; }
  .birthday-sub { margin-top: 5px; font-size: .66rem; padding: 2px 12px; }
}

/* ---------- buttons ---------- */
.menu { display: flex; flex-direction: column; gap: 14px; width: min(100%, 340px); }
.row { display: flex; gap: 10px; justify-content: center; flex-wrap: wrap; width: 100%; margin-top: 12px; }
.btn {
  appearance: none; border: 0; cursor: pointer;
  font-family: inherit; font-weight: 800; font-size: 1.05rem;
  color: #fff; background: var(--secondary);
  padding: 14px 22px; border-radius: 999px;
  box-shadow: 0 5px 0 var(--secondary-dark), 0 10px 20px rgba(0,0,0,.2);
  transition: transform .08s, box-shadow .08s;
  min-height: 48px;
}
.btn:active { transform: translateY(4px); box-shadow: 0 1px 0 var(--secondary-dark), 0 4px 10px rgba(0,0,0,.2); }
.btn-big { font-size: 1.35rem; padding: 18px 28px; width: 100%; }
.btn-primary { background: var(--primary); box-shadow: 0 5px 0 var(--primary-dark), 0 10px 20px rgba(0,0,0,.2); }
.btn-primary:active { box-shadow: 0 1px 0 var(--primary-dark), 0 4px 10px rgba(0,0,0,.2); }
.btn-secondary { background: var(--secondary); }
.btn-ghost { background: rgba(255,255,255,.22); box-shadow: 0 4px 0 rgba(0,0,0,.15); backdrop-filter: blur(6px); -webkit-backdrop-filter: blur(6px); }
.btn-ghost:active { box-shadow: 0 1px 0 rgba(0,0,0,.15); }
/* Secondary action *inside a card*. The ghost style above is meant for use
   over the sky; on a near-white card it is white-on-white and invisible. */
.btn-sub {
  background: var(--sub);
  box-shadow: 0 4px 0 var(--sub-dark), 0 8px 18px rgba(0,0,0,.18);
  font-size: 1.02rem;
  padding: 12px 18px;
  width: 80%;
  min-height: 44px;
}
.btn-sub:active { box-shadow: 0 1px 0 var(--sub-dark), 0 3px 9px rgba(0,0,0,.18); }
.btn-chip { background: #fff; color: var(--ink); font-size: .95rem; padding: 10px 16px; box-shadow: 0 3px 0 rgba(0,0,0,.12); }
.btn-chip:active { box-shadow: 0 1px 0 rgba(0,0,0,.12); }

/* ---------- cards ---------- */
.card {
  background: var(--paper);
  border-radius: 28px;
  padding: 26px 22px;
  width: min(100%, 360px);
  display: flex; flex-direction: column; gap: 12px; align-items: center;
  text-align: center;
  box-shadow: 0 18px 40px rgba(0,0,0,.25);
  margin: auto 0;
}
.card h2 { margin: 0; font-size: 1.6rem; }
.card p { margin: 0; line-height: 1.4; }
.card-icon { font-size: 3.2rem; line-height: 1; animation: bounce 2s ease-in-out infinite; }

/* ---------- trainer levels ---------- */
.levels { display: flex; flex-direction: column; gap: 10px; width: min(100%, 380px); }
.level {
  display: flex; align-items: center; gap: 14px;
  background: var(--paper); border-radius: 22px; padding: 14px 16px;
  border: 0; text-align: left; font-family: inherit; color: var(--ink); cursor: pointer;
  box-shadow: 0 5px 0 rgba(0,0,0,.12), 0 10px 20px rgba(0,0,0,.15);
  transition: transform .08s;
}
.level:active { transform: translateY(3px); }
.level .lv-icon { font-size: 2rem; width: 46px; text-align: center; }
.level .lv-name { font-weight: 800; font-size: 1.1rem; }
.level .lv-desc { font-size: .85rem; opacity: .75; }
.level .lv-best { margin-left: auto; font-weight: 800; color: var(--primary-dark); white-space: nowrap; }

/* ---------- calibration ---------- */
.cal-ring { position: relative; width: 200px; height: 200px; margin: 10px 0; }
.cal-ring svg { width: 100%; height: 100%; transform: rotate(-90deg); }
.ring-bg { fill: rgba(255,255,255,.25); stroke: rgba(255,255,255,.5); stroke-width: 8; }
.ring-fg { fill: none; stroke: var(--accent); stroke-width: 9; stroke-linecap: round; stroke-dasharray: 327; stroke-dashoffset: 327; transition: stroke-dashoffset .1s linear; }
.cal-note { position: absolute; inset: 0; display: grid; place-items: center; font-size: 3rem; font-weight: 900; color: #fff; text-shadow: 0 3px 8px rgba(0,0,0,.3); }
.cal-result { color: #fff; font-weight: 700; text-align: center; text-shadow: 0 2px 6px rgba(0,0,0,.3); min-height: 1.5em; }
.presets { display: flex; flex-direction: column; gap: 8px; margin-top: 12px; width: min(100%, 340px); }
.presets .btn { width: 100%; }

/* ---------- settings ---------- */
.settings-list { background: var(--paper); border-radius: 24px; width: min(100%, 380px); overflow: hidden; box-shadow: 0 12px 30px rgba(0,0,0,.2); }
.setting { display: flex; align-items: center; justify-content: space-between; gap: 12px; padding: 14px 18px; border-bottom: 1px solid rgba(0,0,0,.06); font-weight: 700; }
.setting:last-child { border-bottom: 0; }
.setting input[type=checkbox] { width: 48px; height: 28px; appearance: none; background: #cfd3e0; border-radius: 999px; position: relative; transition: background .2s; cursor: pointer; }
.setting input[type=checkbox]::after { content: ""; position: absolute; top: 3px; left: 3px; width: 22px; height: 22px; border-radius: 50%; background: #fff; transition: transform .2s; box-shadow: 0 2px 4px rgba(0,0,0,.2); }
.setting input[type=checkbox]:checked { background: var(--good); }
.setting input[type=checkbox]:checked::after { transform: translateX(20px); }
.setting select { font-family: inherit; font-weight: 700; padding: 8px 10px; border-radius: 12px; border: 2px solid #dfe3ee; background: #fff; color: var(--ink); }
.swatches { display: flex; gap: 8px; }
.swatch { width: 32px; height: 32px; border-radius: 50%; border: 3px solid #fff; background: var(--c); box-shadow: 0 2px 6px rgba(0,0,0,.25); cursor: pointer; transition: transform .15s; }
.swatch.selected { transform: scale(1.2); border-color: var(--ink); }

/* ---------- how to ---------- */
.howto { background: var(--paper); border-radius: 24px; padding: 8px 18px; width: min(100%, 380px); box-shadow: 0 12px 30px rgba(0,0,0,.2); margin-bottom: 14px; }
.howto-item { display: flex; gap: 14px; align-items: center; padding: 10px 0; border-bottom: 1px solid rgba(0,0,0,.06); }
.howto-item:last-child { border-bottom: 0; }
.howto-item span { font-size: 1.8rem; width: 40px; text-align: center; flex-shrink: 0; }
.howto-item p { margin: 0; line-height: 1.35; font-size: .95rem; }

/* ---------- HUD ---------- */
#hud { position: absolute; inset: 0; pointer-events: none; }
#hud[hidden] { display: none; }
.hud-top {
  position: absolute; left: 0; right: 0; top: 0;
  padding: calc(var(--sat) + 10px) calc(var(--sar) + 14px) 0 calc(var(--sal) + 14px);
  display: flex; align-items: center; justify-content: space-between;
}
.hud-btn {
  pointer-events: auto; width: 44px; height: 44px; border-radius: 50%; border: 0;
  background: rgba(255,255,255,.85); color: var(--ink); font-weight: 900; font-size: .9rem;
  box-shadow: 0 3px 0 rgba(0,0,0,.15); font-family: inherit;
}
#score {
  font-size: 3rem; font-weight: 900; color: #fff;
  -webkit-text-stroke: 2px #2b2a4c;
  text-shadow: 0 4px 0 #2b2a4c, 0 8px 16px rgba(0,0,0,.3);
  transition: transform .15s cubic-bezier(.2,1.6,.4,1);
}
#score.bump { transform: scale(1.35); }
.mic-meter { width: 44px; height: 44px; border-radius: 50%; background: rgba(255,255,255,.85); position: relative; overflow: hidden; box-shadow: 0 3px 0 rgba(0,0,0,.15); }
.mic-meter::before { content: "🎤"; position: absolute; inset: 0; display: grid; place-items: center; font-size: 1.2rem; z-index: 1; }
.mic-meter i { position: absolute; left: 0; right: 0; bottom: 0; height: 0%; background: linear-gradient(to top, var(--good), var(--accent)); transition: height .08s; }
.mic-meter.off::before { content: "👆"; }

.hud-bottom {
  position: absolute; left: 0; right: 0; bottom: 0;
  padding: 0 0 calc(var(--sab) + 10px);
  display: flex; justify-content: center;
}
.note-panel {
  background: rgba(255,255,255,.88); border-radius: 22px; padding: 8px 18px 10px;
  display: flex; flex-direction: column; align-items: center; min-width: 170px;
  box-shadow: 0 6px 18px rgba(0,0,0,.2);
}
.note-name { font-size: 1.9rem; font-weight: 900; line-height: 1; min-height: 1.9rem; transition: color .1s; }
.note-name.in-tune { color: #1fa85c; }
.tuner { position: relative; width: 150px; height: 8px; margin-top: 6px; border-radius: 999px;
  background: linear-gradient(to right, var(--bad), var(--accent) 35%, var(--good) 50%, var(--accent) 65%, var(--bad)); }
.tuner::after { content: ""; position: absolute; left: 50%; top: -3px; width: 2px; height: 14px; background: var(--ink); transform: translateX(-50%); border-radius: 2px; }
.tuner-needle { position: absolute; top: -5px; left: 50%; width: 18px; height: 18px; margin-left: -9px; border-radius: 50%; background: #fff; border: 3px solid var(--ink); box-shadow: 0 2px 4px rgba(0,0,0,.3); transition: transform .08s, opacity .2s; opacity: .35; }
.target-hint { font-size: .85rem; font-weight: 800; margin-top: 6px; min-height: 1.1em; color: var(--primary-dark); }

.toast {
  position: absolute; left: 50%; top: 32%; transform: translate(-50%, -50%) scale(.6);
  font-size: 2.4rem; font-weight: 900; color: #fff; opacity: 0; pointer-events: none;
  -webkit-text-stroke: 2px #2b2a4c; text-shadow: 0 4px 0 #2b2a4c, 0 8px 20px rgba(0,0,0,.3);
  white-space: nowrap;
}
.toast.show { animation: toast 1s ease-out forwards; }
@keyframes toast { 0%{opacity:0;transform:translate(-50%,-50%) scale(.6)} 15%{opacity:1;transform:translate(-50%,-50%) scale(1.15)} 30%{transform:translate(-50%,-50%) scale(1)} 100%{opacity:0;transform:translate(-50%,-90%) scale(1)} }

/* ---------- game over ---------- */
.over-title { font-size: 2rem; font-weight: 900; }
.over-score { font-size: 4rem; font-weight: 900; line-height: 1; color: var(--primary); }
.over-stats { display: flex; gap: 18px; font-weight: 700; font-size: .95rem; opacity: .85; flex-wrap: wrap; justify-content: center; }
.over-stats b { color: var(--secondary-dark); }
.over-best { font-weight: 800; color: var(--ink); }
.over-best.new { color: #d97b00; animation: bounce 1s ease-in-out infinite; }

/* Short phones: shrink the header so the middle of the screen keeps some air.
   Above this the default sizes already leave a comfortable gap. */
@media (max-height: 760px) {
  .logo-word span { font-size: clamp(2.5rem, 13.5vw, 4.2rem); }
  .tagline { font-size: 1.12rem; margin-top: 6px; }
  .btn-big { font-size: 1.2rem; padding: 14px 24px; }
  .menu { gap: 10px; }
  .row { margin-top: 9px; }
  .best-line { margin-top: 8px; font-size: .92rem; }
  .birthday { margin-top: 8px; }
  .birthday-sub { margin-top: 6px; }
}
@media (max-height: 640px) {
  .card { padding: 18px 16px; }
}
@media (orientation: landscape) and (max-height: 500px) {
  .screen { padding-top: calc(var(--sat) + 8px); }
  .logo { margin-top: 0; }
  .logo-word span { font-size: clamp(2rem, 9vh, 3rem); }
}
