/* ============================================
   Beat Buddy — UI Redesign
   Игровой мультяшный стиль для детей 3-15 лет
   ============================================ */

/* --- CSS Variables --- */
:root {
  /* Setup screen gradient */
  --setup-bg-start: #4FC3F7;
  --setup-bg-end: #B388FF;

  /* Session screen gradient */
  --session-bg-start: #303F9F;
  --session-bg-end: #4A148C;

  /* Stats screen gradient */
  --stats-bg-start: #FFD54F;
  --stats-bg-end: #FF8A65;

  /* Buttons */
  --btn-primary: #66BB6A;
  --btn-primary-hover: #57a85b;
  --btn-danger: #EF5350;
  --btn-danger-hover: #d84341;

  /* Accent */
  --accent: #2979FF;

  /* Cards */
  --card-bg: rgba(255, 255, 255, 0.92);

  /* Accuracy zones */
  --color-perfect: #4CAF50;
  --color-good: #FFC107;
  --color-miss: #F44336;

  /* Typography */
  --font-family: 'Nunito', 'Segoe UI', Arial, sans-serif;

  /* Shadows */
  --shadow-card: 0 8px 32px rgba(0, 0, 0, 0.15);
  --shadow-btn: 0 4px 12px rgba(0, 0, 0, 0.2);
}

/* --- Reset & Base --- */
*, *::before, *::after {
  box-sizing: border-box;
}

body {
  font-family: var(--font-family);
  font-size: 18px;
  font-weight: 700;
  margin: 0;
  padding: 0;
  color: #333;
  overflow-x: hidden;
}

h1 {
  font-size: 2.8rem;
  font-weight: 900;
  text-align: center;
  margin: 0 0 32px;
}

h2 {
  font-size: 1.8rem;
  font-weight: 900;
  text-align: center;
  margin: 0 0 24px;
}

/* ============================================
   SETUP SCREEN
   ============================================ */
#setup-screen {
  min-height: 100vh;
  background: linear-gradient(135deg, var(--setup-bg-start), var(--setup-bg-end));
  padding: 40px 20px;
}

#setup-screen h1 {
  color: #1a237e;
  text-shadow: 0 2px 12px rgba(255, 255, 255, 0.5);
}

/* Card */
.card {
  max-width: 480px;
  margin: 0 auto;
  background: var(--card-bg);
  border-radius: 24px;
  padding: 40px 32px;
  box-shadow: var(--shadow-card);
}

/* Setting groups */
.setting-group {
  margin-bottom: 28px;
}

.setting-group label {
  display: block;
  font-size: 1.1rem;
  font-weight: 700;
  margin-bottom: 10px;
  color: #444;
}

.setting-value {
  color: var(--accent);
  font-size: 1.4rem;
  font-weight: 900;
}

/* --- Custom Range Slider (BPM) --- */
input[type="range"] {
  -webkit-appearance: none;
  appearance: none;
  width: 100%;
  height: 12px;
  border-radius: 6px;
  background: linear-gradient(90deg, var(--setup-bg-start), var(--accent));
  outline: none;
  cursor: pointer;
}

input[type="range"]::-webkit-slider-thumb {
  -webkit-appearance: none;
  appearance: none;
  width: 32px;
  height: 32px;
  border-radius: 50%;
  background: #fff;
  border: 4px solid var(--accent);
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.2);
  cursor: pointer;
  transition: transform 0.15s ease;
}

input[type="range"]::-webkit-slider-thumb:hover {
  transform: scale(1.15);
}

input[type="range"]::-moz-range-thumb {
  width: 32px;
  height: 32px;
  border-radius: 50%;
  background: #fff;
  border: 4px solid var(--accent);
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.2);
  cursor: pointer;
}

/* --- Number Input --- */
input[type="number"] {
  width: 100%;
  padding: 14px 16px;
  font-family: var(--font-family);
  font-size: 1.2rem;
  font-weight: 700;
  border: 3px solid #ddd;
  border-radius: 16px;
  outline: none;
  transition: border-color 0.2s, box-shadow 0.2s;
}

input[type="number"]:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 4px rgba(41, 121, 255, 0.2);
}

/* --- Checkbox --- */
input[type="checkbox"] {
  width: 24px;
  height: 24px;
  margin-right: 10px;
  vertical-align: middle;
  cursor: pointer;
  accent-color: var(--accent);
}

/* --- Select --- */
select {
  width: 100%;
  padding: 14px 16px;
  font-family: var(--font-family);
  font-size: 1.2rem;
  font-weight: 700;
  border: 3px solid #ddd;
  border-radius: 16px;
  outline: none;
  background: #fff;
  cursor: pointer;
  transition: border-color 0.2s, box-shadow 0.2s;
  -webkit-appearance: none;
  appearance: none;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='8'%3E%3Cpath d='M1 1l5 5 5-5' stroke='%23666' stroke-width='2' fill='none'/%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: right 16px center;
  padding-right: 40px;
}

select:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 4px rgba(41, 121, 255, 0.2);
}

/* --- Buttons --- */
.primary-button {
  display: block;
  width: 100%;
  height: 72px;
  font-family: var(--font-family);
  font-size: 1.5rem;
  font-weight: 900;
  color: #fff;
  background: var(--btn-primary);
  border: none;
  border-radius: 20px;
  cursor: pointer;
  margin-top: 12px;
  box-shadow: 0 6px 0 #4a9e4e, var(--shadow-btn);
  transition: transform 0.1s, box-shadow 0.1s;
  letter-spacing: 0.5px;
}

.primary-button:hover {
  transform: translateY(-2px);
  box-shadow: 0 8px 0 #4a9e4e, 0 6px 16px rgba(0, 0, 0, 0.25);
}

.primary-button:active {
  transform: translateY(3px);
  box-shadow: 0 2px 0 #4a9e4e, 0 2px 4px rgba(0, 0, 0, 0.2);
}

/* Danger button (Stop) */
.danger-button {
  background: var(--btn-danger);
  box-shadow: 0 6px 0 #c62828, var(--shadow-btn);
}

.danger-button:hover {
  box-shadow: 0 8px 0 #c62828, 0 6px 16px rgba(0, 0, 0, 0.25);
}

.danger-button:active {
  box-shadow: 0 2px 0 #c62828, 0 2px 4px rgba(0, 0, 0, 0.2);
}

/* ============================================
   SESSION SCREEN
   ============================================ */
#session-screen {
  min-height: 100vh;
  height: 100dvh;
  background: linear-gradient(135deg, var(--session-bg-start), var(--session-bg-end));
  padding: clamp(10px, 1.8vh, 20px) clamp(10px, 1.6vw, 20px);
  display: flex;
  align-items: stretch;
}

.session-layout {
  width: min(96vw, 1000px);
  max-width: 1000px;
  height: calc(100dvh - clamp(20px, 3.6vh, 40px));
  max-height: calc(100dvh - clamp(20px, 3.6vh, 40px));
  min-height: 0;
  margin: 0 auto;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: clamp(6px, 1vh, 12px);
  position: relative;
}

/* Timer */
.timer {
  font-size: clamp(2.1rem, 7.2vh, 4.8rem);
  font-weight: 900;
  color: #fff;
  text-align: center;
  text-shadow: 0 0 20px rgba(255, 255, 255, 0.5), 0 0 40px rgba(255, 255, 255, 0.2);
  line-height: 1;
}

/* Streak counter */
.streak {
  display: flex;
  align-items: center;
  justify-content: center;
  flex-direction: column;
  gap: 6px;
  font-size: clamp(1.35rem, 4.6vh, 2.8rem);
  font-weight: 900;
  color: #FFD54F;
  text-align: center;
  line-height: 1;
  text-shadow: 0 0 12px rgba(255, 213, 79, 0.5);
  transition: opacity 0.3s ease, transform 0.3s ease;
}

.streak--hidden {
  opacity: 0;
  transform: scale(0.5);
  pointer-events: none;
}

.streak--visible {
  opacity: 1;
  transform: scale(1);
}

.streak-current {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
}

.streak-icon {
  font-size: 1em;
}

.streak-best {
  display: inline-block;
  font-size: clamp(0.82rem, 1.8vh, 1rem);
  font-weight: 700;
  opacity: 0.75;
}

.streak-best--hidden {
  display: none;
}

.speech-bubble {
  min-height: clamp(32px, 5.2vh, 52px);
  max-width: min(640px, 92vw);
  width: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  text-align: center;
  pointer-events: none;
  opacity: 1;
  transform: translateY(0) scale(1);
  filter: drop-shadow(0 10px 18px rgba(0, 0, 0, 0.2));
  transition: opacity 0.22s ease, transform 0.22s ease;
}

.speech-bubble span {
  display: inline-block;
  position: relative;
  max-width: 100%;
  padding: 12px 18px;
  border-radius: 999px;
  border: 3px solid rgba(255, 255, 255, 0.96);
  background: linear-gradient(180deg, rgba(255, 255, 255, 0.96), rgba(242, 248, 255, 0.95));
  color: #1c2938;
  text-wrap: balance;
  font-size: clamp(1rem, 2.9vw, 1.18rem);
  font-weight: 900;
  line-height: 1.25;
  letter-spacing: 0.01em;
  box-shadow: inset 0 -2px 0 rgba(255, 255, 255, 0.8), 0 10px 24px rgba(6, 18, 35, 0.24);
  animation: speech-pop-in 0.18s ease-out;
}

.speech-bubble span::before,
.speech-bubble span::after {
  content: "";
  position: absolute;
  border-radius: 50%;
  background: inherit;
  border: inherit;
}

.speech-bubble span::before {
  width: 18px;
  height: 18px;
  left: 20%;
  bottom: -13px;
}

.speech-bubble span::after {
  width: 11px;
  height: 11px;
  left: calc(20% - 13px);
  bottom: -25px;
}

.speech-bubble--positive span {
  border-color: rgba(130, 244, 179, 0.96);
  background: linear-gradient(180deg, rgba(237, 255, 244, 0.98), rgba(208, 246, 220, 0.95));
  color: #11412c;
}

.speech-bubble--miss span {
  border-color: rgba(255, 179, 179, 0.96);
  background: linear-gradient(180deg, rgba(255, 240, 240, 0.98), rgba(255, 220, 220, 0.95));
  color: #631d1d;
}

.speech-bubble--hidden {
  opacity: 0;
  transform: translateY(-8px) scale(0.98);
}

.speech-bubble--fade-out {
  opacity: 0;
  transform: translateY(8px) scale(0.98);
}

@keyframes speech-pop-in {
  0% {
    transform: scale(0.92);
  }
  100% {
    transform: scale(1);
  }
}

/* Bump animation on each increment */
.streak--bump {
  animation: streak-bump 0.25s ease-out;
}

@keyframes streak-bump {
  0% { transform: scale(1.3); }
  100% { transform: scale(1); }
}

/* Threshold effects */
.streak-5 {
  color: #FFEB3B;
  font-size: clamp(1.5rem, 5vh, 3.2rem);
  text-shadow: 0 0 16px rgba(255, 235, 59, 0.6);
}

.streak-10 {
  color: #FF9800;
  font-size: clamp(1.65rem, 5.4vh, 3.5rem);
  text-shadow: 0 0 20px rgba(255, 152, 0, 0.7);
}

.streak-20 {
  color: #FF5722;
  font-size: clamp(1.85rem, 6vh, 3.9rem);
  text-shadow: 0 0 28px rgba(255, 87, 34, 0.8);
  animation: streak-pulse 0.6s ease-in-out infinite;
}

@keyframes streak-pulse {
  0%, 100% { transform: scale(1); }
  50% { transform: scale(1.12); }
}

@media (prefers-reduced-motion: reduce) {
  .streak--bump,
  .streak-20 {
    animation: none;
  }

  .speech-bubble,
  .speech-bubble--hidden,
  .speech-bubble--fade-out {
    transition: none;
    transform: none;
  }

  .speech-bubble span {
    animation: none;
  }
}

/* Canvas container */
#rhythm-canvas-container {
  display: block;
  width: 100%;
  flex: 1 1 0%;
  min-height: 0;
  height: auto;
  margin-bottom: clamp(10px, 1.8vh, 18px);
  border-radius: 20px;
  box-shadow: 0 0 24px rgba(255, 255, 255, 0.15);
  overflow: hidden;
}

#rhythm-canvas-container canvas {
  display: block;
  width: 100%;
  height: 100%;
}

/* Metronome indicator — скрыт, не несёт ценности для ребёнка/педагога */
.indicator {
  display: none;
}

/* Session buttons */
#session-screen .primary-button {
  width: min(320px, 100%);
  max-width: 320px;
  height: clamp(50px, 7.4vh, 68px);
  margin-top: clamp(2px, 0.6vh, 8px);
  flex-shrink: 0;
  font-size: clamp(1.15rem, 2.6vh, 1.5rem);
}

/* ============================================
   STATS SCREEN
   ============================================ */
#stats-screen {
  min-height: 100vh;
  background: linear-gradient(135deg, var(--stats-bg-start), var(--stats-bg-end));
  padding: 40px 20px;
}

.stats-layout {
  max-width: 720px;
  margin: 0 auto;
  position: relative;
}

/* Title with pulse animation */
.stats-title {
  color: #fff;
  text-shadow: 0 2px 8px rgba(0, 0, 0, 0.15);
  animation: pulse-title 2s ease-in-out infinite;
}

@keyframes pulse-title {
  0%, 100% { transform: scale(1); }
  50% { transform: scale(1.05); }
}

@media (prefers-reduced-motion: reduce) {
  .stats-title {
    animation: none;
  }
}

/* Stats cards row */
.stats-container {
  display: flex;
  flex-direction: row;
  gap: 16px;
  margin: 32px 0;
}

.stat-item {
  flex: 1;
  background: var(--card-bg);
  border-radius: 20px;
  padding: 24px 16px;
  text-align: center;
  box-shadow: var(--shadow-card);
  border-top: 5px solid transparent;
  position: relative;
}

.stat-item--perfect {
  border-top-color: var(--color-perfect);
}

.stat-item--good {
  border-top-color: var(--color-good);
}

.stat-item--miss {
  border-top-color: var(--color-miss);
}

.stat-icon {
  font-size: 2rem;
  margin-bottom: 8px;
}

.stat-value {
  font-size: 3.5rem;
  font-weight: 900;
  line-height: 1.1;
}

.stat-perfect { color: var(--color-perfect); }
.stat-good { color: var(--color-good); }
.stat-miss { color: var(--color-miss); }

.stat-label {
  font-size: 1rem;
  color: #666;
  margin-top: 8px;
  font-weight: 700;
}

/* Decorative dots */
.stats-layout::before,
.stats-layout::after {
  content: '';
  position: absolute;
  border-radius: 50%;
  opacity: 0.15;
  pointer-events: none;
}

.stats-layout::before {
  width: 120px;
  height: 120px;
  background: #fff;
  top: -30px;
  right: -20px;
}

.stats-layout::after {
  width: 80px;
  height: 80px;
  background: #fff;
  bottom: 40px;
  left: -15px;
}

/* Secondary stats */
.stats-secondary {
  margin: 0 0 24px;
  padding: 16px;
  background: var(--card-bg);
  border-radius: 16px;
  box-shadow: var(--shadow-card);
  text-align: center;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.stat-item-small {
  font-size: 1.1rem;
  color: #555;
}

.stat-label-small {
  font-weight: 900;
}

/* Stats button */
#stats-screen .primary-button {
  max-width: 400px;
  margin: 0 auto;
}

/* ============================================
   RESPONSIVE
   ============================================ */

/* Mobile: stat cards stack */
@media (max-width: 768px) {
  .stats-container {
    flex-direction: column;
  }

  .stat-value {
    font-size: 3rem;
  }

  .timer {
    font-size: clamp(1.9rem, 6.5vh, 3.2rem);
  }

  h1 {
    font-size: 2.2rem;
  }
}

/* Large screens */
@media (max-height: 820px) {
  #session-screen {
    padding: 10px;
  }

  .session-layout {
    height: calc(100dvh - 20px);
    max-height: calc(100dvh - 20px);
    gap: 6px;
  }

  .timer {
    font-size: clamp(1.8rem, 6vh, 3.1rem);
  }

  .streak {
    font-size: clamp(1.2rem, 3.8vh, 2.1rem);
  }

  .speech-bubble {
    min-height: 28px;
  }

  #rhythm-canvas-container {
    border-radius: 16px;
  }

  #session-screen .primary-button {
    height: 46px;
    font-size: 1.1rem;
    border-radius: 16px;
  }
}
@media (max-height: 680px) {
  .streak-best {
    display: none;
  }
}
@media (min-width: 1200px) {
  .card {
    max-width: 540px;
  }

  .stats-layout {
    max-width: 840px;
  }

  .session-layout {
    max-width: 1000px;
  }
}

/* ============================================
   ACCESSIBILITY
   ============================================ */

/* Keyboard focus */
:focus-visible {
  outline: 3px solid var(--accent);
  outline-offset: 3px;
  border-radius: 4px;
}

.primary-button:focus-visible {
  outline-offset: 4px;
  border-radius: 20px;
}

input[type="range"]:focus-visible {
  outline-offset: 6px;
}
