*,*::before,*::after { box-sizing: border-box; margin: 0; padding: 0; }

:root {
  --bg: #0a0a14;
  --bg-2: #14142a;
  --fg: #f5f3ff;
  --muted: #8a87a8;
  --accent: #ffd24a;
  --good: #5ee2a0;
  --bad: #ff5a72;
  --combo: #ff9f4a;
  --frame-radius: 36px;
}

html, body {
  height: 100%;
  overscroll-behavior: none;
  -webkit-tap-highlight-color: transparent;
  -webkit-user-select: none;
  user-select: none;
  touch-action: manipulation;
}
html { background: #05050c; overflow: hidden; }

body {
  /* On mobile, lock body to the visual viewport so iOS Safari can't
     scroll the page to bring the input into view (which pushes the
     header off the top and the input behind the keyboard). */
  position: fixed;
  inset: 0;
  background: radial-gradient(ellipse at top, #1a1a35 0%, var(--bg) 60%);
  color: var(--fg);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", system-ui, sans-serif;
  font-size: 16px;
  overflow: hidden;
  display: flex;
  flex-direction: column;
  height: 100vh;
  height: 100dvh;
  height: var(--app-h, 100dvh);
}

/* Desktop: phone-shaped frame, centered (overrides mobile position:fixed) */
@media (min-width: 600px) {
  html { overflow: auto; }
  body {
    position: relative;
    inset: auto;
    max-width: 420px;
    height: min(90dvh, 880px);
    min-height: 0;
    margin: max(2dvh, 16px) auto;
    border-radius: var(--frame-radius);
    box-shadow: 0 30px 80px rgba(0,0,0,0.5), 0 0 0 1px rgba(255,255,255,0.05) inset;
  }
  .screen { border-radius: var(--frame-radius); }
}

#app {
  flex: 1;
  display: flex;
  flex-direction: column;
  transform: translate(var(--sx, 0), var(--sy, 0));
  will-change: transform;
}

/* Header */
header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 16px 20px 8px;
  font-variant-numeric: tabular-nums;
}
.timer-wrap {
  flex: 1;
  height: 8px;
  background: rgba(255,255,255,0.08);
  border-radius: 4px;
  margin-right: 16px;
  overflow: hidden;
}
.timer-bar {
  height: 100%;
  background: linear-gradient(90deg, var(--good), var(--accent));
  width: 100%;
  transform-origin: left;
  transition: transform 0.1s linear, background 0.3s;
}
.timer-bar.low { background: var(--bad); }
.timer-wrap.bonus { box-shadow: 0 0 0 2px var(--good), 0 0 16px rgba(94,226,160,0.6); }
.timer-wrap.damage {
  box-shadow: 0 0 0 2px var(--bad), 0 0 18px rgba(255,90,114,0.7);
  animation: timer-jolt 0.35s;
}
@keyframes timer-jolt {
  0%,100% { transform: translateX(0); }
  20% { transform: translateX(-6px); }
  50% { transform: translateX(5px); }
  80% { transform: translateX(-3px); }
}
.combo {
  font-weight: 800;
  font-size: 22px;
  color: var(--combo);
  min-width: 70px;
  text-align: right;
  transition: transform 0.15s, color 0.15s;
}
.combo.pop { transform: scale(1.4); }
.combo .x { font-size: 14px; opacity: 0.8; margin-left: 2px; }

.mute-btn {
  background: transparent;
  border: none;
  color: var(--muted);
  font-size: 18px;
  padding: 4px 10px;
  margin-right: 8px;
  cursor: pointer;
  line-height: 1;
}
.mute-btn:active { transform: scale(0.9); }

/* Main play area */
main {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 12px 20px;
  position: relative;
  min-height: 0;
}
.kana {
  font-size: clamp(80px, min(42vw, 32dvh), 260px);
  line-height: 1;
  font-weight: 500;
  text-shadow: 0 8px 30px rgba(255,210,74,0.18);
  transition: transform 0.15s, color 0.2s, font-size 0.2s;
}
.kana.correct { color: var(--good); transform: scale(1.15); }
.kana.wrong { color: var(--bad); animation: nope 0.3s; }
@keyframes nope {
  0%,100% { transform: translateX(0); }
  25% { transform: translateX(-12px); }
  75% { transform: translateX(12px); }
}
.score {
  margin-top: clamp(8px, 2dvh, 24px);
  font-size: 18px;
  color: var(--muted);
  letter-spacing: 0.5px;
}
.score b { color: var(--fg); font-weight: 700; }

/* Input */
.input-row { padding: 0 20px clamp(12px, 2dvh, 28px); }
#romaji {
  width: 100%;
  padding: 18px 20px;
  font-size: 24px;
  text-align: center;
  background: var(--bg-2);
  color: var(--fg);
  border: 2px solid rgba(255,255,255,0.08);
  border-radius: 14px;
  outline: none;
  font-family: inherit;
  font-weight: 600;
  letter-spacing: 4px;
  text-transform: lowercase;
  transition: border-color 0.15s, background 0.15s;
}
#romaji:focus { border-color: var(--accent); background: #1c1c38; }
#romaji.wrong { border-color: var(--bad); animation: nope 0.3s; }

/* Floating bursts */
.burst {
  position: absolute;
  pointer-events: none;
  font-weight: 900;
  font-size: 48px;
  color: var(--combo);
  text-shadow: 0 0 20px rgba(255,159,74,0.6);
  animation: burst 0.7s ease-out forwards;
}
@keyframes burst {
  0% { transform: translate(-50%, -50%) scale(0.3); opacity: 0; }
  20% { transform: translate(-50%, -50%) scale(1.3); opacity: 1; }
  100% { transform: translate(-50%, -120%) scale(1); opacity: 0; }
}

/* Ghost-hint after wrong */
.ghost-hint {
  position: absolute;
  left: 50%;
  top: 50%;
  pointer-events: none;
  font-size: clamp(64px, 18vw, 120px);
  font-weight: 700;
  letter-spacing: 6px;
  color: var(--bad);
  text-shadow: 0 0 24px rgba(255,90,114,0.35);
  animation: ghost 1.6s ease-out forwards;
}
@keyframes ghost {
  0%   { transform: translate(-50%, -50%) scale(0.9); opacity: 0; filter: blur(0); }
  20%  { transform: translate(-50%, -60%) scale(1);   opacity: 0.55; filter: blur(0); }
  100% { transform: translate(-50%, -160%) scale(1.1); opacity: 0; filter: blur(8px); }
}

/* Modal screens (start, end, info) */
.screen {
  position: absolute;
  inset: 0;
  background: rgba(10,10,20,0.94);
  backdrop-filter: blur(8px);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 24px;
  z-index: 10;
  text-align: center;
}
.screen h1 {
  font-size: 56px;
  font-weight: 900;
  letter-spacing: -1px;
  margin-bottom: 8px;
  background: linear-gradient(135deg, var(--accent), var(--combo));
  -webkit-background-clip: text;
  background-clip: text;
  -webkit-text-fill-color: transparent;
}
.screen h1.small { font-size: 36px; }
.screen .tag {
  color: var(--muted);
  margin-bottom: 36px;
  font-size: 16px;
  max-width: 340px;
  line-height: 1.6;
}
.screen .tag.tight { margin-bottom: 0; }
.screen .tag + .tag { margin-top: 16px; }
.screen .tag b { color: var(--fg); }
.screen .big {
  font-size: 64px;
  font-weight: 900;
  color: var(--accent);
  margin: 8px 0 4px;
  line-height: 1;
}

/* Stat list (end screen) */
.stat-list {
  width: 100%;
  max-width: 280px;
  margin: 8px auto 0;
  display: flex;
  flex-direction: column;
  gap: 6px;
}
.stat-row {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  font-size: 16px;
  color: var(--muted);
  font-variant-numeric: tabular-nums;
}
.stat-row .v { color: var(--fg); font-weight: 700; font-size: 18px; }
.stat-group {
  border-top: 1px solid rgba(255,255,255,0.08);
  margin-top: 10px;
  padding-top: 10px;
  display: flex;
  flex-direction: column;
  gap: 6px;
}
.stat-row.best .l { font-size: 13px; letter-spacing: 0.6px; text-transform: uppercase; opacity: 0.6; }
.stat-row.best .v { font-size: 16px; color: var(--accent); }

/* Best line (start screen) */
.best-line {
  color: var(--muted);
  font-size: 13px;
  margin-bottom: 24px;
  letter-spacing: 0.6px;
  text-transform: uppercase;
  display: flex;
  gap: 18px;
  align-items: baseline;
}
.best-line span { display: inline-flex; gap: 6px; align-items: baseline; }
.best-line b {
  color: var(--accent);
  font-weight: 700;
  font-size: 16px;
  text-transform: none;
  letter-spacing: 0;
}

/* Combo × suffix — keeps digit and multiplier in same font */
.combo-x::after {
  content: '×';
  font-size: 0.78em;
  opacity: 0.7;
  margin-left: 2px;
  font-weight: inherit;
}

/* Buttons */
.btn {
  display: inline-block;
  padding: 18px 40px;
  margin-top: 28px;
  font-size: 18px;
  font-weight: 700;
  background: var(--accent);
  color: #1a1a2e;
  border: none;
  border-radius: 14px;
  cursor: pointer;
  font-family: inherit;
  letter-spacing: 0.3px;
  box-shadow: 0 6px 24px rgba(255,210,74,0.3);
  transition: transform 0.1s;
}
.btn:active { transform: scale(0.96); }
.btn.ghost {
  background: transparent;
  color: var(--muted);
  box-shadow: none;
  margin-top: 12px;
  padding: 12px 24px;
  font-size: 14px;
  font-weight: 500;
}

/* Ad slot */
.ad-slot {
  width: 100%;
  max-width: 320px;
  min-height: 100px;
  margin: 24px auto 0;
  border-radius: 8px;
  background: rgba(255,255,255,0.03);
}

/* Tip card */
.tip-card {
  width: 100%;
  max-width: 320px;
  margin: 24px auto 0;
  padding: 18px 20px;
  border-radius: 14px;
  background: linear-gradient(135deg, rgba(255,159,74,0.12), rgba(255,210,74,0.08));
  border: 1px solid rgba(255,159,74,0.25);
  text-align: center;
}
.tip-card .heart { font-size: 24px; }
.tip-card p {
  font-size: 14px;
  color: var(--muted);
  margin: 6px 0 14px;
  line-height: 1.5;
}
.tip-link {
  display: inline-block;
  padding: 10px 22px;
  background: rgba(255,255,255,0.06);
  border: 1px solid rgba(255,255,255,0.15);
  border-radius: 10px;
  color: var(--fg);
  text-decoration: none;
  font-weight: 600;
  font-size: 14px;
}
.tip-link:hover { background: rgba(255,255,255,0.1); }

/* Footer / legal */
.footer-legal {
  position: absolute;
  bottom: 8px;
  left: 0;
  right: 0;
  text-align: center;
  font-size: 11px;
  color: rgba(255,255,255,0.25);
  z-index: 11;
}
.footer-legal a {
  color: inherit;
  text-decoration: none;
  margin: 0 8px;
  cursor: pointer;
}

/* Tip modal — embeds the Ko-fi widget */
.screen.tip-modal {
  padding: 0;
  background: rgba(10,10,20,0.96);
}
.screen.tip-modal .close {
  position: absolute;
  top: 12px;
  right: 12px;
  width: 36px;
  height: 36px;
  border-radius: 50%;
  background: rgba(255,255,255,0.12);
  color: var(--fg);
  border: none;
  font-size: 22px;
  line-height: 1;
  cursor: pointer;
  z-index: 2;
  font-family: inherit;
}
.screen.tip-modal .close:active { transform: scale(0.9); }
.screen.tip-modal iframe {
  width: 100%;
  max-width: 380px;
  height: 100%;
  max-height: 712px;
  border: 0;
  border-radius: 14px;
  background: #f9f9f9;
}

/* Hide initially-hidden screens before JS boots (FOUC guard) */
[hidden] { display: none !important; }
