:root {
  --bg: #121517;
  --panel: #fbf7ef;
  --panel-deep: #efe4d4;
  --ink: #171d21;
  --muted: #687078;
  --line: rgba(23, 29, 33, 0.14);
  --accent: #0e8a78;
  --accent-dark: #09695d;
  --warm: #d84e45;
  --gold: #d4a234;
  --guess-hue: 180;
  --guess-lightness: 50%;
}

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

[hidden] {
  display: none !important;
}

html, body {
  margin: 0;
  padding: 0;
  width: 100%;
  height: 100%;
  min-height: 100vh;
  min-height: 100dvh;
  color: var(--ink);
  background: #0c0f12;
  font-family: 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Arial, sans-serif;
  overflow-x: hidden;
}

body {
  display: flex;
  flex-direction: column;
}

/* Background Animated Blobs */
.blobs {
  position: fixed;
  inset: 0;
  z-index: 0;
  pointer-events: none;
  overflow: hidden;
}

.blob {
  position: absolute;
  border-radius: 50%;
  filter: blur(90px);
  opacity: 0.32;
  animation: float 18s infinite ease-in-out;
}

.blob-1 {
  width: 380px;
  height: 380px;
  background: #e63946;
  top: -100px;
  left: -80px;
  animation-delay: 0s;
}

.blob-2 {
  width: 340px;
  height: 340px;
  background: #4361ee;
  bottom: -80px;
  right: -60px;
  animation-delay: -6s;
}

.blob-3 {
  width: 280px;
  height: 280px;
  background: #06d6a0;
  top: 45%;
  left: 50%;
  animation-delay: -12s;
}

@keyframes float {
  0%, 100% { transform: translate(0, 0) scale(1); }
  33% { transform: translate(30px, -40px) scale(1.1); }
  66% { transform: translate(-20px, 20px) scale(0.95); }
}

@keyframes slideInUp {
  from { opacity: 0; transform: translateY(16px); }
  to { opacity: 1; transform: translateY(0); }
}

button, input {
  font: inherit;
}

/* Main Container: Perfectly Centered on Mobile */
.app {
  position: relative;
  z-index: 1;
  width: 100%;
  min-height: 100vh;
  min-height: 100dvh;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 10px;
}

/* Card Sections */
.game,
.mode-selection,
.invite-screen,
.leaderboard-screen {
  position: relative;
  z-index: 2;
  width: 100%;
  max-width: 420px;
  margin: auto;
  padding: 16px 18px;
  border: 1px solid rgba(23, 29, 33, 0.08);
  border-radius: 20px;
  background: var(--panel);
  box-shadow: 0 20px 50px rgba(0, 0, 0, 0.35);
  animation: slideInUp 0.35s ease-out;
}

/* Topbar */
.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 12px;
  width: 100%;
}

.topbar > div:first-child {
  flex: 1 1 auto;
  min-width: 0;
}

.eyebrow,
.small-label {
  margin: 0 0 4px;
  color: var(--muted);
  font-size: 11px;
  font-weight: 700;
  letter-spacing: 0.12em;
  text-transform: uppercase;
}

h1 {
  margin: 0;
  font-size: clamp(20px, 5.5vw, 25px);
  line-height: 1.15;
  letter-spacing: -0.02em;
  font-weight: 800;
  color: var(--ink);
}

h2 {
  margin: 0;
  font-size: clamp(32px, 8vw, 42px);
  font-weight: 900;
}

/* User Profile Chip */
.user-chip {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 3px 10px 3px 4px;
  background: rgba(255, 255, 255, 0.75);
  backdrop-filter: blur(10px);
  border-radius: 999px;
  border: 1px solid var(--line);
  flex-shrink: 0;
}

.avatar-small {
  width: 24px;
  height: 24px;
  border-radius: 50%;
  background: var(--accent);
  color: white;
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 700;
  font-size: 12px;
  background-size: cover;
  background-position: center;
}

#headerName {
  font-size: 12px;
  font-weight: 700;
  color: var(--ink);
  max-width: 90px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

/* Mode Selection Screen */
.mode-buttons {
  display: grid;
  gap: 12px;
  margin-top: 14px;
}

.mode-button {
  position: relative;
  overflow: hidden;
  display: grid;
  grid-template-columns: auto 1fr;
  grid-template-rows: auto auto;
  gap: 8px 14px;
  padding: 14px 16px;
  border: 2px solid transparent;
  border-radius: 16px;
  color: var(--ink);
  background:
    linear-gradient(180deg, rgba(255, 255, 255, 0.72), rgba(255, 255, 255, 0.52)),
    var(--panel-deep);
  box-shadow: 0 6px 20px rgba(23, 29, 33, 0.08);
  cursor: pointer;
  text-align: left;
  transition: all 180ms ease;
}

.mode-button:hover {
  border-color: var(--accent);
  transform: translateY(-2px);
}

.mode-button:active {
  transform: translateY(0);
}

.mode-icon {
  grid-row: 1 / -1;
  display: grid;
  place-items: center;
  width: 46px;
  height: 46px;
  border-radius: 12px;
  font-size: 22px;
  background: linear-gradient(135deg, rgba(14, 138, 120, 0.18), rgba(6, 214, 160, 0.08));
  border: 1px solid rgba(14, 138, 120, 0.25);
}

.mode-title {
  font-size: 16px;
  font-weight: 800;
}

.mode-desc {
  grid-column: 2;
  font-size: 12px;
  color: var(--muted);
}

/* Back / Home Buttons */
.back-button {
  width: 36px;
  min-height: 36px;
  height: 36px;
  padding: 0;
  border-radius: 50%;
  font-size: 16px;
  font-weight: 700;
  background: rgba(23, 29, 33, 0.08);
  color: var(--ink);
  box-shadow: none;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
}

.back-button:hover {
  background: rgba(23, 29, 33, 0.16);
}

.home-button {
  background: rgba(23, 29, 33, 0.1);
  color: var(--ink);
  box-shadow: none;
  margin-top: 8px;
}

.home-button:hover {
  background: rgba(23, 29, 33, 0.18);
  color: var(--ink);
}

/* VS Banner in Game */
.vs-banner {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 6px 12px;
  margin-bottom: 10px;
  background: rgba(255, 255, 255, 0.85);
  backdrop-filter: blur(12px);
  border-radius: 12px;
  border: 1px solid var(--line);
}

.vs-player {
  display: flex;
  align-items: center;
  gap: 8px;
  min-width: 0;
}

.vs-avatar {
  width: 28px;
  height: 28px;
  border-radius: 50%;
  background: var(--accent);
  color: white;
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 800;
  font-size: 13px;
  background-size: cover;
  background-position: center;
  flex-shrink: 0;
}

.vs-name {
  font-size: 13px;
  font-weight: 700;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  max-width: 90px;
}

.vs-badge {
  font-size: 11px;
  font-weight: 900;
  letter-spacing: 1px;
  color: #fff;
  background: linear-gradient(135deg, #e63946, #d4a234);
  padding: 3px 8px;
  border-radius: 999px;
  flex-shrink: 0;
}

/* Circular Timer */
.circular-timer-wrap {
  position: relative;
  width: 46px;
  height: 46px;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
}

.circular-timer-svg {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  transform: rotate(-90deg);
}

.timer-circle-bg {
  fill: none;
  stroke: rgba(23, 29, 33, 0.1);
  stroke-width: 4;
}

.timer-circle-progress {
  fill: none;
  stroke: var(--accent);
  stroke-width: 4;
  stroke-linecap: round;
  stroke-dasharray: 113.1;
  stroke-dashoffset: 0;
  transition: stroke-dashoffset 0.95s linear, stroke 0.3s ease;
}

.circular-timer-wrap .timer {
  font-size: 17px;
  font-weight: 900;
  color: var(--ink);
  border: none;
  background: transparent;
  box-shadow: none;
  width: auto;
  height: auto;
}

/* Target Color Box - Perfectly Square & Centered */
.color-stage {
  position: relative;
  display: grid;
  place-items: center;
  width: clamp(140px, 24vh, 195px);
  aspect-ratio: 1 / 1;
  min-height: unset;
  height: auto;
  margin: 4px auto 10px auto;
  overflow: hidden;
  border: 1px solid rgba(23, 29, 33, 0.12);
  border-radius: 20px;
  color: rgba(255, 255, 255, 0.95);
  text-align: center;
  box-shadow: inset 0 -24px 50px rgba(0, 0, 0, 0.15), 0 8px 24px rgba(0, 0, 0, 0.08);
  transition: background 180ms ease;
}

.color-stage span {
  position: relative;
  max-width: 85%;
  padding: 6px 12px;
  border-radius: 8px;
  background: rgba(18, 21, 23, 0.35);
  color: inherit;
  font-weight: 800;
  font-size: 13px;
  line-height: 1.25;
}

.color-stage.hidden-color {
  color: var(--muted);
  background:
    linear-gradient(45deg, rgba(23, 29, 33, 0.06) 25%, transparent 25%),
    linear-gradient(-45deg, rgba(23, 29, 33, 0.06) 25%, transparent 25%),
    linear-gradient(180deg, #efe8dc, #ded4c2);
  background-size: 24px 24px, 24px 24px, auto;
}

.color-stage.hidden-color span {
  background: rgba(251, 247, 239, 0.85);
  color: var(--muted);
}

/* Controls & Sliders */
.controls, .result {
  margin-top: 10px;
}

.controls:not([hidden]) {
  display: grid;
  gap: 8px;
}

.slider-row {
  display: grid;
  gap: 2px;
  padding: 6px 10px;
  border: 1px solid var(--line);
  border-radius: 12px;
  background: rgba(255, 255, 255, 0.6);
}

.slider-label-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-weight: 700;
  font-size: 13px;
}

.slider-badge {
  font-size: 11px;
  font-weight: 800;
  padding: 2px 7px;
  border-radius: 999px;
  background: rgba(23, 29, 33, 0.08);
  color: var(--ink);
}

/* Range Inputs */
input[type="range"] {
  width: 100%;
  height: 26px;
  margin: 0;
  appearance: none;
  -webkit-appearance: none;
  background: transparent;
  cursor: pointer;
}

input[type="range"]::-webkit-slider-runnable-track {
  height: 12px;
  border-radius: 999px;
  border: 1px solid rgba(23, 29, 33, 0.16);
}

input[type="range"]::-moz-range-track {
  height: 12px;
  border-radius: 999px;
  border: 1px solid rgba(23, 29, 33, 0.16);
}

#hueSlider::-webkit-slider-runnable-track {
  background: linear-gradient(to right,
    #ff0000 0%,
    #ffff00 17%,
    #00ff00 33%,
    #00ffff 50%,
    #0000ff 67%,
    #ff00ff 83%,
    #ff0000 100%
  );
  box-shadow: inset 0 2px 4px rgba(0, 0, 0, 0.3);
}

#lightnessSlider::-webkit-slider-runnable-track {
  background: linear-gradient(to right,
    #000000 0%,
    hsl(var(--guess-hue), 82%, 40%) 50%,
    #ffffff 100%
  );
  box-shadow: inset 0 2px 4px rgba(0, 0, 0, 0.3);
}

input[type="range"]::-webkit-slider-thumb {
  width: 28px;
  height: 28px;
  margin-top: -8px;
  appearance: none;
  -webkit-appearance: none;
  border: 3px solid white;
  border-radius: 50%;
  background: hsl(var(--guess-hue), 82%, var(--guess-lightness));
  box-shadow: 0 4px 10px rgba(0, 0, 0, 0.4), 0 0 10px hsla(var(--guess-hue), 82%, 50%, 0.5);
}

input[type="range"]::-moz-range-thumb {
  width: 24px;
  height: 24px;
  border: 3px solid white;
  border-radius: 50%;
  background: hsl(var(--guess-hue), 82%, var(--guess-lightness));
}

/* Preview Row */
.preview-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin-top: 4px;
}

.guess-preview {
  width: 80px;
  height: 42px;
  border-radius: 8px;
  border: 1px solid rgba(23, 29, 33, 0.18);
  box-shadow: inset 0 -10px 20px rgba(0,0,0,0.15);
}

/* General Buttons */
button {
  min-height: 44px;
  padding: 0 20px;
  border: 0;
  border-radius: 12px;
  color: white;
  background: var(--accent);
  box-shadow: 0 8px 20px rgba(14, 138, 120, 0.28);
  cursor: pointer;
  font-weight: 800;
  font-size: 15px;
  transition: all 180ms ease;
}

button:hover {
  background: var(--accent-dark);
  transform: translateY(-1px);
}

button:active {
  transform: translateY(0);
}

button:disabled {
  opacity: 0.6;
  cursor: default;
}

#submitButton {
  flex: 1;
}

/* Result Screen */
.result {
  padding-top: 10px;
  border-top: 1px solid var(--line);
  text-align: center;
}

.result p:last-of-type {
  margin: 6px 0 14px;
  color: var(--muted);
  font-size: 13px;
  line-height: 1.35;
}

.result-actions {
  display: grid;
  grid-template-columns: 1fr;
  gap: 8px;
}

/* Invite Screen */
.invite-content {
  margin-top: 14px;
  width: 100%;
}

.invite-text {
  margin: 0 0 10px;
  font-weight: 700;
  font-size: 14px;
}

.invite-link-container {
  display: flex;
  gap: 8px;
  width: 100%;
  margin-bottom: 12px;
}

.invite-link {
  flex: 1 1 auto;
  min-width: 0; /* Prevents overflow */
  width: 100%;
  padding: 10px 12px;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: rgba(255, 255, 255, 0.8);
  font-size: 13px;
  font-family: monospace;
}

.copy-button {
  flex-shrink: 0;
  padding: 0 14px;
  white-space: nowrap;
}

.waiting-text {
  margin: 12px 0 0;
  color: var(--muted);
  font-size: 13px;
  text-align: center;
  font-weight: 600;
}

/* Leaderboard Screen */
.leaderboard-container {
  margin-top: 12px;
  width: 100%;
}

.leaderboard-list {
  display: flex;
  flex-direction: column;
  gap: 8px;
  max-height: 52vh;
  overflow-y: auto;
  padding-right: 2px;
}

.lb-item {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 12px;
  border-radius: 12px;
  background: rgba(255, 255, 255, 0.75);
  border: 1px solid var(--line);
}

.lb-rank {
  font-size: 15px;
  font-weight: 800;
  width: 24px;
  text-align: center;
}

.lb-rank-1 { font-size: 20px; color: #f59e0b; }
.lb-rank-2 { font-size: 18px; color: #94a3b8; }
.lb-rank-3 { font-size: 18px; color: #d97706; }

.lb-avatar {
  width: 30px;
  height: 30px;
  border-radius: 50%;
  background: var(--accent);
  color: white;
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 800;
  font-size: 13px;
  background-size: cover;
  flex-shrink: 0;
}

.lb-info {
  flex: 1 1 auto;
  min-width: 0;
}

.lb-name {
  font-weight: 700;
  font-size: 13px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.lb-date {
  font-size: 10px;
  color: var(--muted);
}

.lb-score {
  font-size: 15px;
  font-weight: 800;
  color: var(--accent-dark);
  flex-shrink: 0;
}

.lb-empty {
  text-align: center;
  color: var(--muted);
  padding: 24px 10px;
  font-size: 13px;
}

/* Small Screen Optimizations */
@media (max-height: 600px) {
  .app {
    padding: 6px;
  }
  .game, .mode-selection, .invite-screen, .leaderboard-screen {
    padding: 12px 14px;
  }
  .color-stage {
    width: 115px;
    height: auto;
    aspect-ratio: 1 / 1;
    min-height: unset;
  }
  .mode-buttons {
    gap: 8px;
    margin-top: 8px;
  }
  .mode-button {
    padding: 10px 12px;
  }
  .mode-icon {
    width: 38px;
    height: 38px;
    font-size: 18px;
  }
  .slider-row {
    padding: 4px 8px;
  }
}