/* ===== Reset & Base ===== */
*, *::before, *::after {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
  -webkit-tap-highlight-color: transparent;
}

:root {
  /* Apple Music / iOS dark palette */
  --bg:           #000000;            /* OLED black */
  --surface:      #1C1C1E;            /* iOS grouped background */
  --surface2:     #2C2C2E;            /* iOS secondary fill */
  --surface3:     #3A3A3C;            /* iOS tertiary fill */

  --accent:       #FF375F;            /* Apple Music pink-red */
  --accent-dark:  #CC0033;            /* Pressed state */
  --accent-glow:  rgba(255,55,95,.18);

  --red:          #FF453A;            /* iOS system red  — wrong */
  --orange:       #FF9F0A;            /* iOS system orange — revealed */
  --blue:         #0A84FF;            /* iOS system blue — filled slot */
  --green:        #32D74B;            /* iOS system green — correct */

  --text:         #FFFFFF;
  --text-2:       rgba(235,235,245,.6);   /* iOS secondary label */
  --text-3:       rgba(235,235,245,.3);   /* iOS tertiary label */
  --separator:    rgba(84,84,88,.65);     /* iOS separator */

  --safe-bottom:  env(safe-area-inset-bottom, 0px);
  --game-width:   480px;
  --radius-card:  16px;
  --radius-btn:   12px;
}

html, body {
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, 'SF Pro Display', 'SF Pro Text',
               'PingFang SC', 'Helvetica Neue', 'Microsoft YaHei', sans-serif;
  font-size: 16px;
  line-height: 1.5;
  overflow-x: hidden;
  -webkit-font-smoothing: antialiased;
}

@media (min-width: 600px) {
  body { background: #0a0a0a; }
}

/* ===== View System ===== */
.view {
  display: none;
  min-height: 100dvh;
  flex-direction: column;
}
.view.active { display: flex; }

/* ===== Start View ===== */
.start-container {
  display: flex;
  flex-direction: column;
  gap: 16px;
  padding: 32px 16px calc(32px + var(--safe-bottom));
  max-width: var(--game-width);
  margin: 0 auto;
  width: 100%;
}

.start-header {
  text-align: center;
  padding: 12px 0 4px;
}

.logo {
  font-size: 52px;
  margin-bottom: 10px;
  filter: drop-shadow(0 0 20px rgba(255,55,95,.5));
}

h1 {
  font-size: 28px;
  font-weight: 700;
  letter-spacing: -0.5px;
  /* Gradient text — Apple Music vibe */
  background: linear-gradient(135deg, #FF375F 0%, #FF6584 100%);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
}

.subtitle {
  color: var(--text-2);
  font-size: 14px;
  margin-top: 6px;
}

.config-card {
  background: var(--surface);
  border-radius: var(--radius-card);
  padding: 16px;
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.config-section {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.config-label {
  font-size: 11px;
  font-weight: 600;
  color: var(--text-2);
  text-transform: uppercase;
  letter-spacing: 0.8px;
}

.btn-group {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}

.chip {
  background: var(--surface2);
  color: var(--text-2);
  border: 1px solid var(--separator);
  border-radius: 20px;
  padding: 8px 14px;
  font-size: 14px;
  font-weight: 500;
  cursor: pointer;
  transition: all 0.15s ease;
  min-height: 38px;
  white-space: nowrap;
}
.chip:active { transform: scale(0.93); }
.chip.active {
  background: var(--accent);
  color: #fff;
  border-color: var(--accent);
  font-weight: 600;
}

.difficulty-group {
  display: grid;
  grid-template-columns: 1fr 1fr 1fr;
  gap: 8px;
}
.difficulty-chip {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
  padding: 10px 8px;
  border-radius: var(--radius-btn);
}
.diff-name { font-size: 15px; font-weight: 600; }
.diff-time { font-size: 11px; opacity: 0.75; }

.text-input {
  background: var(--surface2);
  border: 1px solid var(--separator);
  border-radius: var(--radius-btn);
  color: var(--text);
  font-size: 15px;
  padding: 10px 14px;
  width: 100%;
  outline: none;
  transition: border-color 0.15s;
}
.text-input:focus { border-color: var(--accent); }
.text-input::placeholder { color: var(--text-3); }

.primary-btn {
  background: var(--accent);
  color: #fff;
  border: none;
  border-radius: 50px;
  font-size: 17px;
  font-weight: 700;
  padding: 16px 24px;
  width: 100%;
  cursor: pointer;
  transition: transform 0.12s, opacity 0.12s;
  min-height: 54px;
  letter-spacing: 0.2px;
}
.primary-btn:active {
  transform: scale(0.96);
  opacity: 0.85;
}

.secondary-btn {
  background: transparent;
  color: var(--text-2);
  border: 1px solid var(--separator);
  border-radius: 50px;
  font-size: 15px;
  font-weight: 500;
  padding: 14px 24px;
  width: 100%;
  cursor: pointer;
  transition: all 0.12s;
  min-height: 50px;
}
.secondary-btn:active { background: var(--surface2); }

/* ===== Loading View ===== */
.loading-container {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 16px;
  padding: 32px;
}
.spinner {
  width: 44px;
  height: 44px;
  border: 2.5px solid var(--surface2);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }
.loading-text { font-size: 17px; font-weight: 600; }
.loading-sub  { font-size: 14px; color: var(--text-2); text-align: center; }

/* ===== Game View ===== */
#view-game {
  flex-direction: column;
  align-items: center;
  height: 100dvh;
  overflow: hidden;
  background: #0a0a0a;
}

.game-inner {
  width: 100%;
  max-width: var(--game-width);
  height: 100dvh;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  background: var(--bg);
}

/* Game Header */
.game-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 14px 16px 10px;
  font-size: 13px;
  font-weight: 600;
  color: var(--text-2);
  flex-shrink: 0;
  /* Frosted glass on iOS */
  backdrop-filter: saturate(180%) blur(20px);
  -webkit-backdrop-filter: saturate(180%) blur(20px);
  background: rgba(0,0,0,.75);
  border-bottom: 0.5px solid var(--separator);
}
#score-display {
  color: var(--accent);
  font-size: 15px;
  font-weight: 700;
}

/* Album Cover — centred square */
.cover-wrapper {
  position: relative;
  width: min(45vw, 168px);
  aspect-ratio: 1 / 1;
  flex-shrink: 0;
  align-self: center;
  overflow: hidden;
  border-radius: 12px;
  background: var(--surface);
  margin: 12px auto 0;
  box-shadow:
    0 0 0 0.5px var(--separator),
    0 12px 40px rgba(0,0,0,.7),
    0 4px 12px rgba(0,0,0,.5);
  transition: box-shadow 0.6s ease;
}
.cover-wrapper.revealed {
  box-shadow:
    0 0 0 1.5px var(--accent),
    0 12px 48px rgba(255,55,95,.3),
    0 4px 12px rgba(0,0,0,.5);
}
@media (min-width: 600px) {
  .cover-wrapper { width: min(42%, 196px); }
}
/* Mobile: full-screen-width album cover, capped so layout stays intact */
@media (max-width: 599px) {
  .cover-wrapper {
    width: 100%;
    max-height: min(100vw, calc(100dvh - 420px));
    margin: 0;
    border-radius: 0;
    align-self: stretch;
  }
}
/* Casual mode has no char-pool / slots, cover can be larger */
@media (max-width: 599px) {
  #view-game.casual-mode .cover-wrapper {
    max-height: min(100vw, calc(100dvh - 240px));
  }
}

.album-cover {
  width: 100%;
  height: 100%;
  object-fit: cover;
  transition: filter 0.5s ease;
}
.album-cover.blurred {
  filter: blur(20px) brightness(0.55) saturate(1.6);
  transform: scale(1.08);
}
.cover-overlay {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 38px;
  transition: opacity 0.4s;
  background: rgba(0,0,0,.15);
}
.cover-overlay.hidden { opacity: 0; pointer-events: none; }

/* Player */
.player-section {
  padding: 10px 16px 6px;
  flex-shrink: 0;
}
.progress-bar-wrapper {
  display: flex;
  align-items: center;
  gap: 10px;
  margin-bottom: 8px;
}
.progress-bar {
  flex: 1;
  height: 3px;
  background: var(--surface2);
  border-radius: 2px;
  overflow: hidden;
}
.progress-fill {
  height: 100%;
  background: var(--accent);
  border-radius: 2px;
  transition: width 0.3s linear;
  width: 0%;
}
.time-text {
  font-size: 12px;
  color: var(--text-2);
  min-width: 32px;
  text-align: right;
  font-variant-numeric: tabular-nums;
}
.player-controls {
  display: flex;
  align-items: center;
  gap: 10px;
}
.replay-btn {
  background: var(--surface2);
  color: var(--text);
  border: 1px solid var(--separator);
  border-radius: 20px;
  padding: 6px 14px;
  font-size: 14px;
  cursor: pointer;
  transition: all 0.12s;
  min-height: 34px;
}
.replay-btn:active {
  transform: scale(0.92);
  background: var(--surface3);
}
.replay-btn:disabled { opacity: 0.35; cursor: not-allowed; }
.play-status {
  font-size: 13px;
  color: var(--text-2);
  flex: 1;
}

/* Answer Slots */
.slots-section {
  padding: 8px 16px 4px;
  flex-shrink: 0;
}
.answer-slots {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  justify-content: center;
}
.slot {
  min-width: 44px;
  height: 50px;
  padding: 0 8px;
  border-radius: 10px;
  border: 1.5px dashed var(--separator);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 18px;
  font-weight: 700;
  cursor: pointer;
  transition: all 0.15s ease;
  user-select: none;
  white-space: nowrap;
}
.slot.filled {
  border: 1.5px solid var(--blue);
  background: rgba(10,132,255,.12);
  color: var(--text);
}
.slot.filled:active { transform: scale(0.9); }
.slot.correct {
  border-color: var(--green);
  background: rgba(50,215,75,.18);
  color: var(--green);
  animation: correctPop 0.4s ease forwards;
}
.slot.wrong {
  border-color: var(--red);
  background: rgba(255,69,58,.15);
  color: var(--red);
  animation: shake 0.65s ease forwards;
}
.slot.revealed {
  border-color: var(--orange);
  background: rgba(255,159,10,.15);
  color: var(--orange);
}

/* Spacer */
.game-spacer { flex: 1; min-height: 0; }

/* Next Section */
.next-section {
  padding: 8px 16px 8px;
  flex-shrink: 0;
  display: flex;
  flex-direction: column;
  gap: 8px;
}
.feedback-banner {
  text-align: center;
  font-size: 14px;
  font-weight: 600;
  padding: 9px 12px;
  border-radius: var(--radius-btn);
  line-height: 1.4;
}
.feedback-banner.correct {
  color: var(--green);
  background: rgba(50,215,75,.12);
}
.feedback-banner.wrong-retry {
  color: var(--red);
  background: rgba(255,69,58,.1);
}
.feedback-banner.wrong-revealed {
  color: var(--orange);
  background: rgba(255,159,10,.1);
}

/* Char Pool — always at bottom, fixed rows */
.char-pool-section {
  flex-shrink: 0;
  padding: 7px 10px calc(7px + var(--safe-bottom));
  background: var(--bg);
  border-top: 0.5px solid var(--separator);
}
/* Default (Chinese-only): uniform 8-col grid on mobile, 12-col on desktop */
.char-pool {
  display: grid;
  grid-template-columns: repeat(8, 1fr);
  gap: 5px;
}
@media (min-width: 600px) {
  .char-pool { grid-template-columns: repeat(12, 1fr); gap: 4px; }
}

/* When pool contains English words: switch to flex-wrap so word tokens can be wider */
.char-pool.has-en {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
}

.char-btn {
  width: 100%;
  aspect-ratio: 1 / 1;
  border-radius: 8px;
  border: 1px solid var(--separator);
  background: var(--surface);
  color: var(--text);
  font-size: clamp(13px, 3.8vw, 17px);
  font-weight: 600;
  cursor: pointer;
  transition: all 0.12s ease;
  user-select: none;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 0;
  white-space: nowrap;
}
@media (min-width: 600px) { .char-btn { font-size: 15px; } }

/* English-mode buttons: fixed height, auto width to fit the word */
.char-pool.has-en .char-btn {
  width: auto;
  aspect-ratio: unset;
  height: 42px;
  min-width: 42px;
  padding: 0 10px;
  font-size: clamp(12px, 3vw, 14px);
}
@media (min-width: 600px) {
  .char-pool.has-en .char-btn { font-size: 14px; }
}
.char-btn:active {
  transform: scale(0.86);
  background: rgba(255,55,95,.18);
  border-color: var(--accent);
}
.char-btn.used {
  opacity: 0.2;
  cursor: not-allowed;
  text-decoration: line-through;
}
.char-btn.locked {
  opacity: 0.35;
  cursor: not-allowed;
  pointer-events: none;
}

/* Score float */
.score-float {
  position: fixed;
  font-size: 26px;
  font-weight: 800;
  color: var(--accent);
  pointer-events: none;
  z-index: 100;
  animation: scoreFloat 1.2s ease forwards;
  text-shadow: 0 2px 8px rgba(0,0,0,.5);
}

/* ===== Result View ===== */
.result-container {
  display: flex;
  flex-direction: column;
  gap: 20px;
  padding: 28px 16px calc(28px + var(--safe-bottom));
  max-width: var(--game-width);
  margin: 0 auto;
  width: 100%;
}
.result-title {
  text-align: center;
  font-size: 26px;
  font-weight: 800;
  letter-spacing: -0.5px;
}
.score-display-large {
  display: flex;
  align-items: center;
  background: var(--surface);
  border-radius: var(--radius-card);
  padding: 24px 16px;
}
.score-block { flex: 1; text-align: center; }
.score-number {
  font-size: 50px;
  font-weight: 800;
  color: var(--accent);
  line-height: 1;
  letter-spacing: -1px;
}
.score-label { font-size: 13px; color: var(--text-2); margin-top: 6px; }
.score-divider {
  width: 0.5px;
  height: 56px;
  background: var(--separator);
  margin: 0 16px;
}
.new-record {
  text-align: center;
  font-size: 20px;
  font-weight: 700;
  color: var(--accent);
  background: var(--accent-glow);
  border-radius: var(--radius-btn);
  padding: 14px;
  animation: recordPop 0.5s cubic-bezier(0.175,.885,.32,1.275) forwards;
}
.new-record.hidden { display: none; }

.history-list { display: flex; flex-direction: column; gap: 8px; }
.history-item {
  display: flex;
  align-items: center;
  gap: 12px;
  background: var(--surface);
  border-radius: var(--radius-btn);
  padding: 10px 14px;
}
.history-cover {
  width: 42px;
  height: 42px;
  border-radius: 8px;
  object-fit: cover;
  flex-shrink: 0;
  background: var(--surface2);
}
.history-info { flex: 1; min-width: 0; }
.history-song {
  font-size: 15px;
  font-weight: 600;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.history-artist {
  font-size: 13px;
  color: var(--text-2);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.history-score {
  font-size: 17px;
  font-weight: 700;
  flex-shrink: 0;
  min-width: 32px;
  text-align: right;
}
.history-score.s3 { color: var(--accent); }
.history-score.s1 { color: var(--orange); }
.history-score.s0 { color: var(--text-3); }

.result-actions { display: flex; flex-direction: column; gap: 12px; }

/* ===== Landscape Tip ===== */
.landscape-tip {
  display: none;
  position: fixed;
  inset: 0;
  background: var(--bg);
  z-index: 9999;
  align-items: center;
  justify-content: center;
}
.landscape-tip p { font-size: 20px; color: var(--text-2); }
@media (orientation: landscape) and (max-height: 500px) {
  .landscape-tip { display: flex; }
}

/* ===== Animations ===== */
@keyframes shake {
  0%, 100% { transform: translateX(0); }
  15%  { transform: translateX(-6px); }
  30%  { transform: translateX(6px); }
  45%  { transform: translateX(-6px); }
  60%  { transform: translateX(6px); }
  75%  { transform: translateX(-4px); }
  90%  { transform: translateX(4px); }
}
@keyframes correctPop {
  0%   { transform: scale(1); }
  40%  { transform: scale(1.18); }
  70%  { transform: scale(0.94); }
  100% { transform: scale(1); }
}
@keyframes scoreFloat {
  0%   { opacity: 0; transform: translateX(-50%) translateY(0) scale(.6); }
  20%  { opacity: 1; transform: translateX(-50%) translateY(-12px) scale(1.2); }
  60%  { opacity: 1; transform: translateX(-50%) translateY(-36px) scale(1); }
  100% { opacity: 0; transform: translateX(-50%) translateY(-72px) scale(.9); }
}
@keyframes recordPop {
  0%   { transform: scale(.7); opacity: 0; }
  60%  { transform: scale(1.1); opacity: 1; }
  100% { transform: scale(1); opacity: 1; }
}
@keyframes slideUp {
  from { transform: translateY(20px); opacity: 0; }
  to   { transform: translateY(0); opacity: 1; }
}

/* ===== Mode Group (2-col grid like difficulty) ===== */
.mode-group {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
}

/* ===== Exit Game Button (Casual Mode header) ===== */
.exit-game-btn {
  background: transparent;
  color: var(--text-2);
  border: none;
  font-size: 13px;
  font-weight: 500;
  cursor: pointer;
  padding: 4px 2px;
  border-radius: 8px;
  transition: opacity 0.12s;
  white-space: nowrap;
  min-width: 48px;
  text-align: left;
}
.exit-game-btn:active { opacity: 0.45; }

/* ===== Casual Mode: Reveal Section ===== */
.reveal-section {
  padding: 14px 16px;
  flex-shrink: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 10px;
}

.reveal-info-btn {
  background: var(--surface);
  color: var(--text);
  border: 1px solid var(--separator);
  border-radius: 50px;
  font-size: 15px;
  font-weight: 600;
  padding: 12px 28px;
  cursor: pointer;
  transition: all 0.15s;
  min-height: 46px;
}
.reveal-info-btn:active {
  transform: scale(0.96);
  background: var(--surface2);
}

.reveal-details {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 5px;
  text-align: center;
  padding: 0 16px;
  animation: slideUp 0.3s ease both;
}

.reveal-song-name {
  font-size: 22px;
  font-weight: 700;
  letter-spacing: -0.3px;
  color: var(--text);
}

.reveal-artist-name {
  font-size: 14px;
  color: var(--text-2);
  font-weight: 500;
}

/* ===== Casual Mode: hide challenge-only sections ===== */
#view-game.casual-mode .slots-section,
#view-game.casual-mode .char-pool-section {
  display: none;
}
