:root {
  --ink: #160f22;
  --sign: #f6c500;
  --red: #ff4d6d;
  --text: #fff7ec;
  --muted: #d4c6de;
  --comic: 'Luckiest Guy', 'Arial Black', 'Helvetica Neue', Arial, sans-serif;
  --ui: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif;
  --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);
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }

html, body {
  margin: 0;
  height: 100%;
  background: #1b1430;
  color: var(--text);
  font-family: var(--ui);
  overflow: hidden;
  overscroll-behavior: none;
  -webkit-user-select: none;
  user-select: none;
  -webkit-touch-callout: none;
  touch-action: none;
  -webkit-text-size-adjust: 100%;
}
body { position: fixed; inset: 0; }
#app { position: fixed; inset: 0; overflow: hidden; }
#game, #crack { position: absolute; inset: 0; width: 100%; height: 100%; display: block; }
#crack { pointer-events: none; }
#touch { position: absolute; inset: 0; touch-action: none; }
.hidden { display: none !important; }
button { font: inherit; color: inherit; cursor: pointer; touch-action: manipulation; }

/* ---------- HUD ---------- */
#hud { position: absolute; inset: 0; pointer-events: none; }
.hud-top {
  position: absolute;
  top: calc(8px + var(--sat));
  left: calc(12px + var(--sal));
  right: calc(12px + var(--sar));
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 8px;
}
.hud-score .big {
  font-family: var(--comic);
  font-size: clamp(22px, 7vh, 36px);
  line-height: 1;
  color: #fff;
  -webkit-text-stroke: 1px #000;
  text-shadow: 0 2px 0 #000, 0 0 12px rgba(0, 0, 0, .45);
}
.hud-score .sub { font-size: 12px; font-weight: 800; color: #ffe9c9; text-shadow: 0 1px 2px #000; margin-top: 3px; white-space: nowrap; }
.hud-mid { flex: 1; display: flex; justify-content: center; padding-top: 2px; }
.heart { font-size: 26px; line-height: 1; color: #4b3f55; text-shadow: 0 2px 0 #000; margin: 0 2px; }
.heart.on { color: var(--red); }
.hud-btns { display: flex; gap: 8px; pointer-events: auto; }
.round {
  width: 44px; height: 44px;
  border-radius: 50%;
  border: 2px solid rgba(255, 255, 255, .7);
  background: rgba(10, 8, 20, .45);
  color: #fff;
  font-size: 15px;
  font-weight: 900;
  display: grid;
  place-items: center;
  padding: 0;
  -webkit-backdrop-filter: blur(6px);
  backdrop-filter: blur(6px);
}
.herd {
  display: flex; align-items: center; gap: 8px;
  background: rgba(10, 8, 20, .55);
  padding: 4px 10px;
  border-radius: 14px;
  border: 2px solid rgba(255, 255, 255, .3);
}
.herd-icon { font-size: 18px; }
.herd .bar { width: clamp(70px, 16vw, 140px); height: 10px; background: rgba(255, 255, 255, .15); border-radius: 6px; overflow: hidden; }
.herd .bar i { display: block; height: 100%; background: #ffb347; }
.herd.danger { border-color: #ff4d4d; animation: pulse .6s infinite; }
.herd.danger .bar i { background: #ff4d4d; }
.herd b { font-family: var(--comic); font-weight: 400; font-size: 18px; min-width: 54px; }
.radio-label { font-family: var(--comic); font-size: 16px; background: rgba(10, 8, 20, .55); padding: 5px 12px; border-radius: 14px; }

.pedal {
  position: absolute;
  bottom: calc(10px + var(--sab));
  padding: 8px 14px 6px;
  border-radius: 14px;
  font-family: var(--comic);
  font-size: 15px;
  letter-spacing: 1px;
  color: rgba(255, 255, 255, .85);
  background: rgba(10, 8, 20, .35);
  border: 2px solid rgba(255, 255, 255, .35);
  transition: background .1s, transform .1s;
}
.pedal.left { left: calc(12px + var(--sal)); }
.pedal.right, .pedal.right2 { right: calc(12px + var(--sar)); }
.pedal.on { background: rgba(246, 197, 0, .9); color: #151515; border-color: #151515; transform: scale(1.06); }
.pedal.left.on, .pedal.right2.on { background: rgba(255, 77, 109, .9); color: #fff; }
#hud[data-input="tilt"] .touch-only, #hud[data-input="keys"] .touch-only { display: none; }
#hud[data-input="touch"] .tilt-only { display: none; }
#hud[data-input="touch"] #pedal-r { bottom: auto; top: 44%; }
#hud[data-input="keys"] .pedal { display: none; }

.horn {
  position: absolute;
  left: 50%;
  bottom: calc(8px + var(--sab));
  transform: translateX(-50%);
  width: 76px; height: 42px;
  border-radius: 21px;
  border: 2px solid rgba(255, 255, 255, .6);
  background: rgba(10, 8, 20, .45);
  color: #fff;
  font-family: var(--comic);
  font-size: 16px;
  letter-spacing: 1px;
  pointer-events: auto;
  padding: 4px 0 0;
}
.horn.on { background: var(--sign); color: #151515; border-color: #151515; transform: translateX(-50%) scale(1.08); }

#vignette {
  position: absolute; inset: 0;
  pointer-events: none;
  opacity: 0;
  transition: opacity .3s;
  box-shadow: inset 0 0 130px 40px rgba(255, 30, 30, .75);
}

/* ---------- toasts + BONK ---------- */
#toasts {
  position: absolute;
  top: calc(56px + var(--sat));
  left: 50%;
  transform: translateX(-50%);
  width: min(92vw, 640px);
  display: flex; flex-direction: column; align-items: center; gap: 6px;
  pointer-events: none;
}
.toast {
  font-family: var(--comic);
  font-size: clamp(16px, 5.4vh, 24px);
  padding: 7px 14px 4px;
  border-radius: 12px;
  background: rgba(15, 10, 28, .72);
  color: #fff;
  text-align: center;
  letter-spacing: .5px;
  animation: toastIn .25s ease-out;
  transition: opacity .35s, transform .35s;
}
.toast.out { opacity: 0; transform: translateY(-8px); }
.toast.good { color: #d9ff6b; }
.toast.warn { color: #ff9a76; }
.toast.level { color: var(--sign); }
.toast.tip { font-family: var(--ui); font-weight: 800; font-size: 15px; padding: 8px 14px; }
@keyframes toastIn { from { opacity: 0; transform: translateY(-10px) scale(.95); } }

#pop { position: absolute; inset: 0; pointer-events: none; }
.pop {
  position: absolute;
  top: 32%; left: 50%;
  display: grid; place-items: center;
  transform: translate(-50%, -50%);
  animation: pop 1.05s cubic-bezier(.2, 1.6, .4, 1) forwards;
}
.pop span {
  position: relative;
  font-family: var(--comic);
  font-size: clamp(56px, 22vh, 110px);
  color: var(--sign);
  -webkit-text-stroke: 4px #151515;
  text-shadow: 0 6px 0 #151515;
  transform: rotate(-8deg);
  padding: 0 .1em;
}
.pop .burst {
  position: absolute;
  width: 160%; height: 200%;
  background: radial-gradient(circle, #ffe14a 0%, #ff7a2b 70%);
  clip-path: polygon(100% 50%, 80.9% 58.3%, 93.3% 75%, 72.6% 72.6%, 75% 93.3%, 58.3% 80.9%, 50% 100%, 41.7% 80.9%, 25% 93.3%, 27.4% 72.6%, 6.7% 75%, 19.1% 58.3%, 0% 50%, 19.1% 41.7%, 6.7% 25%, 27.4% 27.4%, 25% 6.7%, 41.7% 19.1%, 50% 0%, 58.3% 19.1%, 75% 6.7%, 72.6% 27.4%, 93.3% 25%, 80.9% 41.7%);
}
.pop.boom { animation-duration: 1.6s; }
.pop.boom span { color: #ffe14a; font-size: clamp(64px, 26vh, 132px); -webkit-text-stroke: 5px #2a0a00; text-shadow: 0 7px 0 #2a0a00; }
.pop.boom .burst { width: 190%; height: 240%; background: radial-gradient(circle, #fff6c2 0%, #ff8a1a 40%, #c8280a 75%); }

#flash {
  position: absolute; inset: 0;
  pointer-events: none;
  opacity: 0;
  background: radial-gradient(circle at 50% 45%, #fffbe8 0%, #ffd27a 35%, #ff7a2a 70%, rgba(200, 40, 10, 0.6) 100%);
}
#flash.on { animation: flash .9s ease-out forwards; }
@keyframes flash {
  0% { opacity: 1; }
  25% { opacity: .85; }
  100% { opacity: 0; }
}

@keyframes pop {
  0% { transform: translate(-50%, -50%) scale(.2); opacity: 0; }
  14% { opacity: 1; }
  30% { transform: translate(-50%, -50%) scale(1.12); }
  78% { transform: translate(-50%, -50%) scale(1); opacity: 1; }
  100% { transform: translate(-50%, -50%) scale(1.05); opacity: 0; }
}

/* ---------- screens ---------- */
.screen {
  position: absolute; inset: 0;
  display: flex; align-items: center; justify-content: center;
  padding: calc(10px + var(--sat)) calc(16px + var(--sar)) calc(10px + var(--sab)) calc(16px + var(--sal));
  background: rgba(20, 12, 34, .62);
}
#s-title { background: linear-gradient(90deg, rgba(20, 12, 34, .9), rgba(20, 12, 34, .62) 55%, rgba(20, 12, 34, .25)); }
.title-wrap { display: flex; align-items: center; gap: clamp(12px, 5vw, 56px); width: 100%; max-width: 1000px; }
.title-left { display: flex; flex-direction: column; align-items: center; text-align: center; }
#t-sign { width: clamp(64px, 24vh, 130px); height: auto; filter: drop-shadow(0 6px 10px rgba(0, 0, 0, .5)); }
.logo {
  font-family: var(--comic);
  font-weight: 400;
  font-size: clamp(30px, 11.5vh, 66px);
  line-height: .95;
  margin: 6px 0 0;
  color: #fff;
  -webkit-text-stroke: 2px #151515;
  text-shadow: 0 5px 0 #151515, 0 10px 20px rgba(0, 0, 0, .4);
  letter-spacing: 1px;
}
.edition { font-family: var(--comic); color: var(--sign); font-size: clamp(14px, 4.5vh, 22px); margin-top: 4px; text-shadow: 0 2px 0 #151515; }
.title-right { flex: 1; display: flex; flex-direction: column; align-items: flex-start; gap: clamp(4px, 1.8vh, 10px); max-width: 430px; }
.mode-name { font-family: var(--comic); font-size: clamp(20px, 6.5vh, 32px); color: var(--sign); text-shadow: 0 3px 0 #151515; }
.mode-desc { font-size: clamp(12px, 3.8vh, 16px); color: var(--muted); font-weight: 700; }

.btn {
  font-family: var(--comic);
  font-size: clamp(16px, 5.2vh, 24px);
  letter-spacing: 1px;
  padding: .5em 1.1em .35em;
  border-radius: 14px;
  border: 3px solid #151515;
  transition: transform .08s, box-shadow .08s;
}
.btn:active { transform: translateY(3px); }
.btn.primary { background: var(--sign); color: #151515; box-shadow: 0 5px 0 #151515; }
.btn.primary:active { box-shadow: 0 2px 0 #151515; }
.btn.secondary { background: #fff7ec; color: #151515; box-shadow: 0 4px 0 #151515; font-size: clamp(14px, 4.4vh, 19px); }
.btn.link, .link { background: none; border: none; box-shadow: none; color: #fff; font-family: var(--ui); font-weight: 800; font-size: 14px; letter-spacing: 0; text-decoration: underline; padding: 6px; }
.row-small { display: flex; align-items: center; gap: 14px; font-size: 14px; font-weight: 800; color: #ffe9c9; flex-wrap: wrap; }
.howto { font-size: clamp(11px, 3.4vh, 14px); color: var(--muted); line-height: 1.35; font-weight: 600; }
.howto b { color: #fff; white-space: nowrap; }
.tip { font-size: 12px; color: #9fd8ff; font-weight: 700; }

#s-calib { background: rgba(20, 12, 34, .45); }
.calib { text-align: center; }
.calib-text { font-family: var(--comic); font-size: clamp(18px, 6.5vh, 30px); margin-top: 4px; text-shadow: 0 3px 0 #151515; }
/* live bubble level: the bubble drifts to the high side, like a real one */
.level { display: flex; justify-content: center; margin-bottom: clamp(4px, 2vh, 10px); }
.level-tube {
  position: relative; width: 170px; height: 30px; border-radius: 15px;
  background: linear-gradient(#e2fb8c, #a9d93f);
  border: 3px solid #151515;
  box-shadow: 0 4px 0 #151515, inset 0 3px 0 rgba(255, 255, 255, .45);
}
.level-mark {
  position: absolute; left: 50%; top: 3px; bottom: 3px; width: 44px; margin-left: -22px;
  border-left: 3px solid rgba(21, 21, 21, .7); border-right: 3px solid rgba(21, 21, 21, .7);
  transition: border-color .2s;
}
.level-bubble {
  position: absolute; left: 50%; top: 4px; width: 34px; height: 16px; margin-left: -17px; border-radius: 10px;
  background: radial-gradient(circle at 35% 35%, #fff, #f4ffd6 60%, #d9f28f);
  border: 2px solid rgba(21, 21, 21, .55);
  transition: box-shadow .2s;
}
.level.ok .level-bubble { box-shadow: 0 0 10px 3px rgba(110, 255, 140, .95); }
.level.ok .level-mark { border-color: #17772f; }
.calib-bar {
  width: min(260px, 50vw); height: 14px; margin: 0 auto clamp(4px, 1.6vh, 8px);
  border: 3px solid #151515; border-radius: 8px; background: rgba(255, 255, 255, .18); overflow: hidden;
}
#c-bar { height: 100%; background: var(--sign); transform-origin: left center; transform: scaleX(0); }
.calib-count {
  font-family: var(--comic);
  font-size: clamp(64px, 26vh, 124px);
  color: var(--sign);
  -webkit-text-stroke: 3px #151515;
  text-shadow: 0 6px 0 #151515;
  line-height: 1.1;
}
.calib-sub { font-size: clamp(14px, 4.6vh, 19px); color: #fff; font-weight: 800; text-shadow: 0 2px 0 #151515; }

.card {
  background: rgba(22, 15, 36, .92);
  border: 3px solid #151515;
  border-radius: 20px;
  padding: clamp(10px, 3vh, 20px) clamp(16px, 4vw, 28px);
  box-shadow: 0 8px 0 #151515, 0 20px 40px rgba(0, 0, 0, .4);
  display: flex; flex-direction: column; align-items: center;
  gap: clamp(6px, 2vh, 12px);
  max-height: 100%;
  overflow: auto;
}
.card h2, .modes-wrap h2 {
  font-family: var(--comic);
  font-weight: 400;
  margin: 0;
  font-size: clamp(22px, 7.5vh, 36px);
  color: var(--sign);
  text-shadow: 0 3px 0 #151515;
  text-align: center;
}
.sens-row { display: flex; align-items: center; gap: 10px; font-weight: 800; font-size: 14px; }
.seg { display: flex; border: 2px solid #151515; border-radius: 12px; overflow: hidden; }
.seg button { background: #fff7ec; border: none; padding: 7px 12px; font-weight: 800; color: #151515; font-family: var(--ui); }
.seg button + button { border-left: 2px solid #151515; }
.seg button.on { background: var(--sign); }

.over { width: min(94vw, 720px); }
.over-grid { display: flex; gap: clamp(14px, 5vw, 40px); align-items: center; justify-content: center; width: 100%; }
.over-score { text-align: center; }
.over-score .label { font-size: 12px; font-weight: 900; color: var(--muted); letter-spacing: 2px; }
.over-score .score { font-family: var(--comic); font-size: clamp(36px, 13vh, 62px); line-height: 1.05; }
.best { font-weight: 800; color: var(--muted); font-size: 14px; }
.best.new { color: var(--sign); font-family: var(--comic); font-weight: 400; font-size: 20px; animation: pulse 1s infinite; }
.stats { display: grid; grid-template-columns: 1fr 1fr; gap: 4px 18px; }
.stats div { display: flex; flex-direction: column; }
.stats span { font-size: 11px; font-weight: 900; color: var(--muted); text-transform: uppercase; letter-spacing: 1px; }
.stats b { font-family: var(--comic); font-weight: 400; font-size: clamp(16px, 5.2vh, 22px); }
.quip { font-style: italic; color: #ffe9c9; font-weight: 700; font-size: 14px; text-align: center; }
.unlock {
  background: linear-gradient(90deg, #ff7a45, #f6c500);
  color: #151515;
  font-weight: 800;
  padding: 6px 14px;
  border-radius: 12px;
  border: 2px solid #151515;
  text-align: center;
  font-size: 14px;
  animation: pulse 1.2s infinite;
}
.btn-row { display: flex; gap: 10px; align-items: center; flex-wrap: wrap; justify-content: center; }

.modes-wrap { display: flex; flex-direction: column; align-items: center; gap: clamp(6px, 2vh, 12px); width: 100%; max-width: 900px; }
.m-list { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 12px; width: 100%; }
.mode-card {
  text-align: left;
  background: rgba(22, 15, 36, .94);
  color: #fff;
  border: 3px solid #151515;
  border-radius: 18px;
  padding: clamp(8px, 2.4vh, 14px);
  box-shadow: 0 6px 0 #151515;
  display: flex; flex-direction: column; gap: 4px;
  font-family: var(--ui);
}
.mode-card:active { transform: translateY(3px); box-shadow: 0 3px 0 #151515; }
.m-icon { font-size: clamp(22px, 7vh, 32px); }
.m-title { font-family: var(--comic); font-size: clamp(17px, 5.6vh, 24px); color: var(--sign); }
.m-desc { font-size: clamp(11px, 3.4vh, 13px); color: var(--muted); font-weight: 700; line-height: 1.3; }
.m-best { font-size: 12px; font-weight: 900; color: #ffe9c9; margin-top: auto; padding-top: 4px; }

#s-rotate {
  position: absolute; inset: 0; z-index: 50;
  background: #1b1430;
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 12px;
  font-family: var(--comic); font-size: 26px; color: #fff; text-align: center;
}
.rotate-phone { font-size: 70px; animation: rot 2.2s ease-in-out infinite; }
.rotate-tip { font-family: var(--ui); font-size: 15px; font-weight: 700; color: var(--muted); max-width: 300px; line-height: 1.4; }
.rotate-tip b { color: #fff; }
@keyframes rot { 0%, 20% { transform: rotate(0); } 50%, 80% { transform: rotate(-90deg); } 100% { transform: rotate(0); } }

#loading {
  position: absolute; inset: 0; z-index: 60;
  background: #1b1430;
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 14px;
  font-family: var(--comic); font-size: 22px; text-align: center; padding: 20px;
}
.spinner { width: 46px; height: 46px; border-radius: 50%; border: 6px solid rgba(255, 255, 255, .2); border-top-color: var(--sign); animation: spin 1s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }
@keyframes pulse { 50% { transform: scale(1.04); } }

#err {
  position: absolute; left: 8px; bottom: 8px; z-index: 70;
  background: rgba(160, 20, 20, .92);
  color: #fff;
  font: 12px/1.3 ui-monospace, Menlo, Consolas, monospace;
  padding: 6px 8px;
  border-radius: 8px;
  max-width: 80vw;
  pointer-events: none;
}

@media (max-height: 360px) {
  .howto { display: none; }
}
@media (max-aspect-ratio: 1/1) {
  .m-list { grid-template-columns: 1fr; }
}
