:root {
  --bg-top: #8ec9f0;
  --bg-bottom: #fdf3c7;
  --panel: #ffffffee;
  --ink: #3a3160;
  --ink-soft: #6b6488;
  --accent: #ff8fb1;
  --accent-2: #6bd67a;
  --accent-3: #ffd93d;
  --radius: 18px;
  --shadow: 0 8px 20px rgba(58, 49, 96, 0.18);
  color-scheme: light;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  overflow: hidden;
  overscroll-behavior: none;
}

body {
  font-family: "Comic Sans MS", "Chalkboard SE", "Baloo 2", "Trebuchet MS", "Segoe UI", sans-serif;
  background: linear-gradient(180deg, var(--bg-top), var(--bg-bottom));
  color: var(--ink);
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
  overscroll-behavior: none;
  -webkit-user-select: none;
  user-select: none;
}

input, textarea {
  -webkit-user-select: text;
  user-select: text;
}

#app {
  position: relative;
  width: 100%;
  height: 100%;
}

.hidden { display: none !important; }

/* ---------- Screens ---------- */
.screen {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px;
  padding-top: max(16px, env(safe-area-inset-top, 0px));
  padding-bottom: max(16px, env(safe-area-inset-bottom, 0px));
  overflow: auto;
}

#screen-game {
  display: flex;
  flex-direction: column;
  align-items: stretch;
  justify-content: flex-start;
  padding: 8px;
  gap: 8px;
}

.card {
  background: var(--panel);
  border-radius: 24px;
  box-shadow: var(--shadow);
  padding: 28px 24px;
  max-width: 420px;
  width: 100%;
  text-align: center;
  position: relative;
  z-index: 1;
}

.card-wide { max-width: 480px; }

.title {
  font-size: clamp(1.8rem, 6vw, 2.6rem);
  margin: 0 0 4px;
  color: var(--ink);
  text-shadow: 2px 2px 0 #fff, 4px 4px 0 rgba(58,49,96,0.08);
}

.subtitle {
  margin: 0 0 18px;
  color: var(--ink-soft);
  font-size: 1.05rem;
}

/* ---------- Buttons ---------- */
.btn {
  display: block;
  width: 100%;
  border: none;
  border-radius: 999px;
  font-family: inherit;
  font-weight: bold;
  cursor: pointer;
  margin: 10px 0;
  transition: transform 0.08s ease, box-shadow 0.08s ease;
  box-shadow: 0 4px 0 rgba(0,0,0,0.15);
}
.btn:active { transform: translateY(3px); box-shadow: 0 1px 0 rgba(0,0,0,0.15); }

.btn-big {
  font-size: 1.4rem;
  padding: 16px;
}

.btn-primary {
  background: linear-gradient(180deg, #7de08b, var(--accent-2));
  color: #16401f;
}

.btn-secondary {
  background: #ffffff;
  color: var(--ink);
  font-size: 1.05rem;
  padding: 12px;
  border: 2px solid #e3ddfb;
  box-shadow: none;
}

.icon-btn {
  font-size: clamp(1.05rem, 3.2vw, 1.4rem);
  background: #ffffffcc;
  border: none;
  border-radius: 12px;
  padding: clamp(4px, 1.2vw, 6px) clamp(7px, 2vw, 10px);
  cursor: pointer;
  box-shadow: 0 3px 0 rgba(0,0,0,0.12);
  flex-shrink: 0;
  line-height: 1;
}
.icon-btn:active { transform: translateY(2px); box-shadow: none; }

/* ---------- Start screen extras ---------- */
.menu-options {
  display: flex;
  flex-direction: column;
  gap: 10px;
  align-items: flex-start;
  background: #f5f2ff;
  border-radius: 14px;
  padding: 12px 16px;
  margin-bottom: 16px;
}

.toggle {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 1rem;
  cursor: pointer;
  user-select: none;
}
.toggle input { width: 20px; height: 20px; accent-color: var(--accent-2); }

.difficulty-select {
  width: 100%;
}
.difficulty-label {
  font-size: 0.8rem;
  font-weight: bold;
  color: var(--ink-soft);
  text-transform: uppercase;
  letter-spacing: 0.03em;
  margin-bottom: 6px;
}
.difficulty-tabs {
  margin-bottom: 0;
}
.difficulty-tabs .mode-tab[data-difficulty="hard"].active {
  background: linear-gradient(180deg, #ffb199, var(--accent));
  color: #5a1030;
}

.high-score {
  margin-top: 14px;
  font-size: 1.1rem;
  font-weight: bold;
  color: #b5850a;
}

.how-to {
  margin-top: 16px;
  font-size: 0.85rem;
  color: var(--ink-soft);
}

/* ---------- Mode tabs ---------- */
.mode-tabs {
  display: flex;
  gap: 8px;
  background: #f5f2ff;
  border-radius: 999px;
  padding: 5px;
  margin-bottom: 10px;
}
.mode-tab {
  flex: 1;
  border: none;
  background: transparent;
  padding: 10px 6px;
  border-radius: 999px;
  font-family: inherit;
  font-weight: bold;
  font-size: 0.95rem;
  color: var(--ink-soft);
  cursor: pointer;
}
.mode-tab.active {
  background: linear-gradient(180deg, #7de08b, var(--accent-2));
  color: #16401f;
  box-shadow: 0 3px 0 rgba(0,0,0,0.12);
}

.mode-hint {
  font-size: 0.85rem;
  color: var(--ink-soft);
  margin-bottom: 10px;
}

/* ---------- Player profiles ---------- */
.profile-list {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
  justify-content: center;
  margin-bottom: 12px;
  max-height: 34vh;
  overflow-y: auto;
  padding: 2px;
}

.profile-chip {
  position: relative;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
  width: 84px;
  padding: 10px 6px 8px;
  border-radius: 16px;
  background: #f5f2ff;
  border: 3px solid transparent;
  cursor: pointer;
  font-family: inherit;
}
.profile-chip.selected {
  border-color: var(--accent-2);
  background: #e7fbea;
}
.profile-chip .order-badge {
  position: absolute;
  top: -6px;
  left: -6px;
  background: var(--accent);
  color: #fff;
  font-size: 0.7rem;
  font-weight: bold;
  border-radius: 999px;
  width: 20px;
  height: 20px;
  display: flex;
  align-items: center;
  justify-content: center;
}
.profile-chip .avatar { font-size: 1.8rem; line-height: 1; }
.profile-chip .name { font-size: 0.8rem; font-weight: bold; color: var(--ink); max-width: 100%; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.profile-chip .best { font-size: 0.68rem; color: var(--ink-soft); }
.profile-chip .delete-btn {
  position: absolute;
  top: -6px;
  right: -6px;
  background: #fff;
  border: 2px solid #eee;
  border-radius: 999px;
  width: 20px;
  height: 20px;
  font-size: 0.65rem;
  line-height: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  color: var(--ink-soft);
}

#add-player-row { margin-bottom: 10px; }

.add-player-panel {
  background: #f5f2ff;
  border-radius: 16px;
  padding: 12px;
  margin-bottom: 12px;
  text-align: left;
}
.name-input {
  width: 100%;
  font-family: inherit;
  font-size: 1rem;
  padding: 10px 12px;
  border-radius: 10px;
  border: 2px solid #e3ddfb;
  margin-bottom: 10px;
  box-sizing: border-box;
}
.emoji-grid {
  display: grid;
  grid-template-columns: repeat(6, 1fr);
  gap: 6px;
  margin-bottom: 10px;
}
.emoji-btn {
  font-size: 1.4rem;
  padding: 6px 0;
  border-radius: 10px;
  border: 2px solid transparent;
  background: #ffffff;
  cursor: pointer;
}
.emoji-btn.selected { border-color: var(--accent-2); background: #e7fbea; }
.add-player-actions { display: flex; gap: 8px; }
.add-player-actions .btn { margin: 0; }

.family-error {
  color: #c0392b;
  font-size: 0.85rem;
  margin: -4px 0 12px;
  font-weight: bold;
}

/* ---------- Pass-the-device screen ---------- */
.turn-progress {
  font-size: 0.85rem;
  color: var(--ink-soft);
  font-weight: bold;
  margin-bottom: 6px;
  text-transform: uppercase;
  letter-spacing: 0.03em;
}
.turn-avatar { font-size: 4rem; line-height: 1; margin-bottom: 4px; }

/* ---------- Player bar in-game ---------- */
.player-bar {
  display: flex;
  align-items: center;
  gap: clamp(3px, 1vw, 6px);
  background: #ffffffcc;
  border-radius: 999px;
  padding: clamp(3px, 1vw, 4px) clamp(7px, 2vw, 12px);
  font-weight: bold;
  font-size: clamp(0.68rem, 2.4vw, 0.85rem);
  flex-shrink: 1;
  min-width: 0;
}
.player-bar-avatar { font-size: clamp(0.9rem, 2.8vw, 1.1rem); flex-shrink: 0; }
#player-bar-name {
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

/* ---------- Leaderboard ---------- */
.leaderboard-list {
  display: flex;
  flex-direction: column;
  gap: 8px;
  margin: 16px 0;
}
.leaderboard-row {
  display: flex;
  align-items: center;
  gap: 10px;
  background: #f5f2ff;
  border-radius: 14px;
  padding: 10px 14px;
  text-align: left;
}
.leaderboard-row.winner {
  background: linear-gradient(90deg, #fff6d6, #ffe9a8);
  border: 2px solid var(--accent-3);
}
.leaderboard-rank { font-size: 1.3rem; width: 28px; text-align: center; }
.leaderboard-avatar { font-size: 1.5rem; }
.leaderboard-info { flex: 1; }
.leaderboard-info .name { font-weight: bold; }
.leaderboard-info .detail { font-size: 0.75rem; color: var(--ink-soft); }
.leaderboard-score { font-weight: bold; font-size: 1.1rem; }

#screen-leaderboard { overflow: hidden; }
#leaderboard-confetti-canvas {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  z-index: 0;
}

/* ---------- HUD ---------- */
.hud {
  display: flex;
  align-items: center;
  gap: clamp(4px, 2vw, 10px);
  background: var(--panel);
  border-radius: 16px;
  padding: clamp(5px, 1.5vw, 8px) clamp(8px, 2.5vw, 14px);
  box-shadow: var(--shadow);
  flex-wrap: nowrap;
}

.hud-item {
  display: flex;
  flex-direction: column;
  align-items: center;
  min-width: clamp(36px, 9vw, 54px);
  flex-shrink: 0;
}
.hud-label { font-size: clamp(0.55rem, 1.8vw, 0.7rem); color: var(--ink-soft); text-transform: uppercase; letter-spacing: 0.03em; white-space: nowrap; }
.hud-value { font-size: clamp(0.9rem, 3.2vw, 1.25rem); font-weight: bold; color: var(--ink); }

.hud-menu {
  margin-left: auto;
  position: relative;
  flex-shrink: 0;
}

.hud-menu-dropdown {
  position: absolute;
  top: calc(100% + 8px);
  right: 0;
  display: flex;
  flex-direction: column;
  gap: 4px;
  background: var(--panel);
  border-radius: 14px;
  padding: 6px;
  box-shadow: var(--shadow);
  z-index: 10;
  min-width: 140px;
}

.hud-menu-item {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 0.95rem;
  font-weight: bold;
  background: transparent;
  border: none;
  border-radius: 10px;
  padding: 8px 10px;
  cursor: pointer;
  text-align: left;
  color: var(--ink);
}
.hud-menu-item:active { background: #ece7ff; }

/* ---------- Play area ---------- */
.play-area {
  flex: 1;
  min-height: 0;
  display: flex;
  gap: 10px;
  justify-content: center;
  align-items: flex-start;
}

.board-wrap {
  position: relative;
  height: 100%;
  max-height: 100%;
  aspect-ratio: 1 / 2;
  background: #2b2560;
  border-radius: 14px;
  box-shadow: var(--shadow);
  padding: 6px;
  display: flex;
}

#board-canvas {
  width: 100%;
  height: 100%;
  border-radius: 8px;
  display: block;
  touch-action: none;
}

#level-confetti-canvas {
  position: absolute;
  inset: 6px;
  border-radius: 8px;
  pointer-events: none;
  z-index: 1;
}

.side-panel {
  display: flex;
  flex-direction: column;
  gap: clamp(5px, 1.2vw, 8px);
  width: clamp(84px, 14vw, 130px);
  height: 100%;
  flex-shrink: 0;
}

.side-panel .fab-drop:first-of-type {
  margin-top: auto;
}
.side-panel .fab-drop:last-of-type {
  margin-bottom: 50px;
}

.panel-box {
  background: var(--panel);
  border-radius: 14px;
  padding: clamp(6px, 1.4vw, 8px) clamp(6px, 1.6vw, 10px);
  box-shadow: var(--shadow);
  text-align: center;
}
.panel-box h3 {
  margin: 0 0 clamp(3px, 0.8vw, 6px);
  font-size: clamp(0.6rem, 1.5vw, 0.75rem);
  text-transform: uppercase;
  color: var(--ink-soft);
  letter-spacing: 0.03em;
}

#next-canvas {
  width: 100%;
  max-width: clamp(56px, 9vw, 100px);
  height: auto;
  aspect-ratio: 1/1;
  display: block;
  margin: 0 auto;
}

.current-piece {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: clamp(3px, 0.8vw, 6px);
  font-weight: bold;
  font-size: clamp(0.7rem, 1.6vw, 0.95rem);
}
.swatch {
  width: clamp(12px, 2vw, 18px);
  height: clamp(12px, 2vw, 18px);
  border-radius: 5px;
  display: inline-block;
  box-shadow: inset 0 0 0 2px rgba(255,255,255,0.5);
  flex-shrink: 0;
}

.progress-track {
  background: #eee6ff;
  border-radius: 999px;
  height: clamp(7px, 1.2vw, 12px);
  overflow: hidden;
}
.progress-fill {
  height: 100%;
  width: 0%;
  background: linear-gradient(90deg, var(--accent-3), var(--accent-2));
  border-radius: 999px;
  transition: width 0.3s ease;
}
.progress-label {
  margin-top: clamp(3px, 0.8vw, 6px);
  font-size: clamp(0.6rem, 1.4vw, 0.75rem);
  color: var(--ink-soft);
}

/* ---------- Toast / banner ---------- */
.toast {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  background: linear-gradient(160deg, #fffdf6, #ffedb8);
  color: #6b4a12;
  padding: 14px 28px;
  border-radius: 999px;
  font-weight: bold;
  font-size: 1.3rem;
  border: 3px solid #ffd93d;
  box-shadow: 0 6px 0 rgba(0,0,0,0.15), 0 10px 26px rgba(0,0,0,0.3);
  animation: pop-in 0.3s cubic-bezier(0.34, 1.56, 0.64, 1);
  pointer-events: none;
  white-space: nowrap;
  z-index: 5;
}

.level-banner {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-direction: column;
  background: rgba(43, 37, 96, 0.55);
  color: #fff;
  text-align: center;
  border-radius: 8px;
  animation: pop-in 0.3s ease;
  pointer-events: none;
  z-index: 6;
}
.level-banner .big { font-size: 2rem; font-weight: bold; }
.level-banner .small { font-size: 1.1rem; margin-top: 4px; }

@keyframes pop-in {
  from { opacity: 0; transform: translate(-50%, -50%) scale(0.6); }
  to { opacity: 1; transform: translate(-50%, -50%) scale(1); }
}
.level-banner {
  animation-name: pop-in-center;
}
@keyframes pop-in-center {
  from { opacity: 0; transform: scale(0.85); }
  to { opacity: 1; transform: scale(1); }
}

/* ---------- Floating fast-drop button ---------- */
.fab-drop {
  display: flex;
  width: 60px;
  height: 60px;
  margin: 8px auto 0;
  border-radius: 999px;
  border: none;
  font-size: 1.8rem;
  line-height: 1;
  background: linear-gradient(180deg, #ffd0dc, var(--accent));
  color: #5a1030;
  box-shadow: 0 5px 0 rgba(0,0,0,0.2), 0 6px 14px rgba(0,0,0,0.25);
  cursor: pointer;
  touch-action: manipulation;
  z-index: 3;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
}
.fab-drop:active { transform: translateY(3px); box-shadow: 0 2px 0 rgba(0,0,0,0.2); }

.fab-rotate {
  background: linear-gradient(180deg, #cdeaff, #6bc7ff);
  color: #103a5a;
}

/* ---------- Overlay ---------- */
.overlay {
  position: absolute;
  inset: 0;
  background: rgba(43, 37, 96, 0.45);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 5;
  padding: 16px;
}

/* ---------- Stats (game over / win) ---------- */
.stats {
  display: flex;
  justify-content: space-around;
  gap: 8px;
  margin: 16px 0;
  flex-wrap: wrap;
}
.stat {
  display: flex;
  flex-direction: column;
  background: #f5f2ff;
  border-radius: 12px;
  padding: 10px 14px;
  min-width: 90px;
}
.stat-label { font-size: 0.75rem; color: var(--ink-soft); }
.stat-value { font-size: 1.4rem; font-weight: bold; }

/* ---------- Win screen confetti ---------- */
#screen-win { overflow: hidden; }
#confetti-canvas {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  z-index: 0;
}

/* ---------- Small screens ---------- */
@media (max-width: 480px) {
  .hud-value { font-size: 1.05rem; }
}

@media (max-height: 620px) {
  .card { padding: 18px 16px; }
  .title { font-size: 1.5rem; }
}

/* "Now Falling" is only shown on desktop/web - hidden on mobile & tablet to save space */
@media (max-width: 1024px) {
  .now-falling-box { display: none; }
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg-top: #1b1a3a;
    --bg-bottom: #2c2350;
    --panel: #2a2550ee;
    --ink: #f1ecff;
    --ink-soft: #b9b0e6;
  }
  body:not([data-theme]) .menu-options,
  body:not([data-theme]) .stat,
  body:not([data-theme]) .progress-track,
  body:not([data-theme]) .btn-secondary,
  body:not([data-theme]) .mode-tabs,
  body:not([data-theme]) .profile-chip,
  body:not([data-theme]) .add-player-panel,
  body:not([data-theme]) .leaderboard-row,
  body:not([data-theme]) .emoji-btn {
    background: #362f66;
  }
  body:not([data-theme]) .profile-chip.selected,
  body:not([data-theme]) .emoji-btn.selected {
    background: #2c4a33;
  }
  body:not([data-theme]) .name-input {
    background: #241f47;
    color: var(--ink);
    border-color: #4a4180;
  }
}
