/* ===== RESET & BASE ===== */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

:root {
  --blue: #2196F3;
  --blue-dark: #1976D2;
  --blue-light: #BBDEFB;
  --gray-tile: #607D8B;
  --green: #4CAF50;
  --orange: #FF9800;
  --red: #F44336;
  --text: #1a1a1a;
  --bg: #f0f4f8;
  --white: #ffffff;
  --shadow: 0 2px 8px rgba(0,0,0,0.12);
  --radius: 8px;
}

body {
  font-family: 'Segoe UI', system-ui, sans-serif;
  background: var(--bg);
  color: var(--text);
  min-height: 100vh;
}

/* ===== PAGES ===== */
.page { display: none; min-height: 100vh; }
.page.active { display: block; }

/* ===== SETUP PAGE ===== */
.setup-header {
  background: var(--white);
  border-bottom: 1px solid #dde3ea;
  padding: 14px 28px;
  display: flex;
  align-items: center;
  gap: 16px;
  box-shadow: var(--shadow);
}
.logo { display: flex; align-items: center; gap: 8px; }
.logo-icon { font-size: 24px; }
.logo-text { font-size: 20px; font-weight: 700; color: var(--blue); }
.logo-tagline { font-size: 13px; color: #666; }

.setup-main {
  max-width: 820px;
  margin: 32px auto;
  padding: 0 20px 80px;
}

.breadcrumb {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 15px;
  color: #777;
  margin-bottom: 24px;
  flex-wrap: wrap;
}
.breadcrumb .arrow { color: #bbb; }
.breadcrumb .active-crumb { font-weight: 700; color: var(--text); font-size: 18px; }
.game-badge {
  margin-left: auto;
  background: var(--blue);
  color: white;
  padding: 6px 16px;
  border-radius: 20px;
  font-size: 14px;
  font-weight: 600;
}

.setup-card {
  background: var(--white);
  border-radius: 12px;
  padding: 28px;
  box-shadow: var(--shadow);
  margin-bottom: 20px;
}

.form-group { margin-bottom: 20px; }
.form-label { display: block; font-size: 13px; font-weight: 600; color: #555; margin-bottom: 6px; }
.form-input {
  width: 100%;
  border: 1px solid #d0d7de;
  border-radius: var(--radius);
  padding: 10px 14px;
  font-size: 15px;
  outline: none;
  transition: border-color .2s;
}
.form-input:focus { border-color: var(--blue); box-shadow: 0 0 0 3px var(--blue-light); }
.form-input.small { width: 120px; }

.hint-toggle {
  display: flex;
  gap: 24px;
  margin-bottom: 20px;
}
.radio-label {
  display: flex;
  align-items: center;
  gap: 6px;
  cursor: pointer;
  font-size: 14px;
}
.radio-label input[type="radio"] { accent-color: var(--blue); width: 16px; height: 16px; }

.table-header {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 12px;
  padding: 8px 12px;
  background: #f5f7fa;
  border-radius: var(--radius);
  font-size: 13px;
  font-weight: 700;
  color: #555;
  margin-bottom: 8px;
}

/* Word rows */
.word-row {
  display: grid;
  grid-template-columns: 28px 1fr 1fr auto;
  gap: 10px;
  align-items: center;
  padding: 8px 0;
  border-bottom: 1px solid #f0f0f0;
}
.word-row:last-child { border-bottom: none; }
.row-num { font-size: 13px; color: #888; text-align: right; }
.word-row input {
  border: 1px solid #d0d7de;
  border-radius: 6px;
  padding: 8px 12px;
  font-size: 14px;
  outline: none;
  width: 100%;
}
.word-row input:focus { border-color: var(--blue); box-shadow: 0 0 0 2px var(--blue-light); }
.row-actions { display: flex; gap: 6px; }
.row-btn {
  background: none;
  border: none;
  cursor: pointer;
  color: #999;
  font-size: 16px;
  padding: 4px 6px;
  border-radius: 4px;
  transition: color .15s, background .15s;
}
.row-btn:hover { color: var(--red); background: #ffeef0; }

.add-btn {
  display: flex;
  align-items: center;
  gap: 8px;
  background: none;
  border: 2px dashed #c8d0da;
  border-radius: var(--radius);
  padding: 12px 18px;
  cursor: pointer;
  color: var(--blue);
  font-size: 14px;
  font-weight: 600;
  width: 100%;
  margin-top: 12px;
  transition: border-color .2s, background .2s;
}
.add-btn small { color: #999; font-weight: 400; margin-left: 4px; }
.add-btn:hover { border-color: var(--blue); background: #f0f7ff; }

.setup-footer {
  display: flex;
  align-items: flex-end;
  justify-content: space-between;
  gap: 16px;
  flex-wrap: wrap;
}

.play-btn {
  background: var(--blue);
  color: white;
  border: none;
  border-radius: var(--radius);
  padding: 14px 32px;
  font-size: 16px;
  font-weight: 700;
  cursor: pointer;
  box-shadow: 0 4px 12px rgba(33,150,243,0.35);
  transition: background .2s, transform .1s, box-shadow .2s;
}
.play-btn:hover { background: var(--blue-dark); box-shadow: 0 6px 18px rgba(33,150,243,0.45); transform: translateY(-1px); }
.play-btn:active { transform: translateY(0); }

/* ===== GAME PAGE ===== */
.game-wrapper {
  min-height: 100vh;
  background: var(--white);
  display: flex;
  flex-direction: column;
  align-items: center;
  position: relative;
  overflow: hidden;
}

.game-topbar {
  width: 100%;
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 18px 28px;
}

.timer-display {
  font-size: 28px;
  font-weight: 700;
  color: var(--text);
  font-variant-numeric: tabular-nums;
  min-width: 70px;
}
.timer-display.warning { color: var(--orange); }
.timer-display.danger  { color: var(--red); animation: pulse 0.6s ease-in-out infinite; }

@keyframes pulse { 0%,100% { opacity: 1; } 50% { opacity: 0.4; } }

.score-display {
  font-size: 26px;
  font-weight: 700;
  display: flex;
  align-items: center;
  gap: 6px;
}
.checkmark { color: var(--text); font-size: 22px; }

/* Clue */
.clue-area {
  text-align: center;
  font-size: clamp(28px, 5vw, 52px);
  font-weight: 400;
  line-height: 1.3;
  padding: 20px 32px;
  max-width: 720px;
  color: var(--text);
  flex-shrink: 0;
}

/* Celebration */
.celebration {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  pointer-events: none;
  opacity: 0;
  transition: opacity 0.3s;
  z-index: 10;
}
.celebration.show { opacity: 1; }
.celebration-text {
  font-size: clamp(48px, 10vw, 100px);
  font-weight: 300;
  color: rgba(0,0,0,0.08);
  user-select: none;
  letter-spacing: -2px;
}

/* Answer area */
.answer-area {
  display: flex;
  justify-content: center;
  padding: 16px 20px;
  width: 100%;
}
.answer-slots {
  display: flex;
  gap: 6px;
  flex-wrap: wrap;
  justify-content: center;
  padding: 12px 20px;
  border: 2px solid #d0d7de;
  border-radius: var(--radius);
  min-width: 200px;
  min-height: 80px;
  background: #fafbfc;
  transition: border-color .2s, box-shadow .2s;
}
.answer-slots.drag-over {
  border-color: var(--blue);
  box-shadow: 0 0 0 4px var(--blue-light);
}

/* Slot — empty placeholder */
.answer-slot {
  width: 64px;
  height: 64px;
  border-radius: 6px;
  border: 2px dashed #c8d0da;
  background: #f5f7fa;
  display: flex;
  align-items: center;
  justify-content: center;
  position: relative;
  transition: border-color .15s, background .15s;
}
.answer-slot.occupied { border: none; background: none; padding: 0; }
.answer-slot.drag-over-slot { border-color: var(--blue); background: var(--blue-light); }

/* Letter tile */
.letter-bank {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
  justify-content: center;
  padding: 20px;
  max-width: 600px;
}

.letter-tile {
  width: 64px;
  height: 64px;
  border-radius: 6px;
  background: var(--blue);
  color: white;
  font-size: 28px;
  font-weight: 700;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: grab;
  user-select: none;
  box-shadow: 0 4px 0 var(--blue-dark), 0 2px 8px rgba(0,0,0,0.18);
  transition: transform .1s, box-shadow .1s, opacity .2s;
  touch-action: none;
}
.letter-tile:active { cursor: grabbing; }
.letter-tile.dragging {
  opacity: 0.45;
  transform: scale(0.95);
  box-shadow: 0 2px 0 var(--blue-dark);
}
.letter-tile.in-slot {
  background: var(--blue);
  box-shadow: 0 3px 0 var(--blue-dark), 0 2px 6px rgba(0,0,0,0.15);
  cursor: grab;
}
.letter-tile.correct-tile {
  background: var(--green);
  box-shadow: 0 3px 0 #388E3C;
  animation: correctBounce 0.4s ease;
}
.letter-tile.wrong-tile {
  background: var(--red);
  animation: shake 0.35s ease;
}

@keyframes correctBounce {
  0%   { transform: scale(1); }
  40%  { transform: scale(1.2); }
  70%  { transform: scale(0.9); }
  100% { transform: scale(1); }
}
@keyframes shake {
  0%,100% { transform: translateX(0); }
  20%     { transform: translateX(-8px); }
  40%     { transform: translateX(8px); }
  60%     { transform: translateX(-5px); }
  80%     { transform: translateX(5px); }
}

/* Drag ghost tile */
.drag-ghost {
  position: fixed;
  pointer-events: none;
  z-index: 9999;
  opacity: 0.9;
  transform: scale(1.12) rotate(-4deg);
  transition: none;
  box-shadow: 0 8px 24px rgba(0,0,0,0.25);
}

/* Bottom bar */
.game-bottombar {
  width: 100%;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 16px 24px;
  margin-top: auto;
  border-top: 1px solid #eee;
}
.nav-menu-btn, .icon-btn {
  background: none;
  border: 1px solid #dde3ea;
  border-radius: var(--radius);
  width: 40px; height: 40px;
  font-size: 18px;
  cursor: pointer;
  display: flex; align-items: center; justify-content: center;
  transition: background .15s;
}
.nav-menu-btn:hover, .icon-btn:hover { background: #f0f4f8; }
.nav-progress { display: flex; align-items: center; gap: 12px; font-size: 15px; font-weight: 600; color: #555; }
.nav-arrow {
  background: none;
  border: none;
  font-size: 20px;
  cursor: pointer;
  color: #888;
  padding: 6px 10px;
  border-radius: var(--radius);
  transition: color .15s, background .15s;
}
.nav-arrow:hover { color: var(--blue); background: var(--blue-light); }
.nav-arrow:disabled { opacity: 0.3; cursor: default; }
.nav-right { display: flex; gap: 8px; }

/* ===== RESULT PAGE ===== */
#result-page.active {
  display: flex;
  align-items: center;
  justify-content: center;
  background: linear-gradient(135deg, #e3f2fd 0%, #f0f4f8 100%);
}
.result-wrapper {
  text-align: center;
  padding: 48px 32px;
  background: var(--white);
  border-radius: 20px;
  box-shadow: 0 8px 40px rgba(0,0,0,0.12);
  max-width: 480px;
  width: 90%;
  animation: slideUp 0.5s ease;
}
@keyframes slideUp { from { transform: translateY(40px); opacity: 0; } to { transform: translateY(0); opacity: 1; } }

.result-trophy { font-size: 72px; margin-bottom: 12px; }
.result-title { font-size: 28px; font-weight: 800; margin-bottom: 4px; color: var(--text); }
.result-subtitle { font-size: 15px; color: #666; margin-bottom: 24px; }
.result-score-big { font-size: 80px; font-weight: 900; color: var(--blue); line-height: 1; }
.result-score-label { font-size: 14px; color: #888; margin-bottom: 24px; margin-top: 4px; }
.result-stats {
  background: #f5f7fa;
  border-radius: 10px;
  padding: 16px;
  margin-bottom: 28px;
  font-size: 14px;
  color: #555;
  display: flex;
  flex-direction: column;
  gap: 8px;
}
.stat-row { display: flex; justify-content: space-between; }
.stat-label { color: #888; }
.stat-val { font-weight: 700; color: var(--text); }

.result-actions { display: flex; gap: 12px; justify-content: center; flex-wrap: wrap; }
.secondary-btn {
  background: #f0f4f8;
  color: var(--text);
  border: 1px solid #d0d7de;
  border-radius: var(--radius);
  padding: 14px 24px;
  font-size: 15px;
  font-weight: 600;
  cursor: pointer;
  transition: background .2s;
}
.secondary-btn:hover { background: #e0e8f0; }

/* ===== FLOATING POPUP ===== */
#floating-popup {
  position: fixed;
  top: 60px;
  right: 24px;
  width: 650px;
  max-width: calc(100vw - 32px);
  background: #fff;
  border-radius: 16px;
  box-shadow: 0 12px 48px rgba(0,0,0,0.28);
  z-index: 99999;
  overflow: hidden;
  user-select: none;
}

#popup-titlebar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 18px 24px;
  background: var(--blue);
  cursor: grab;
}
#popup-titlebar:active { cursor: grabbing; }

#popup-title {
  color: #fff;
  font-size: 20px;
  font-weight: 700;
}

#popup-close {
  background: rgba(255,255,255,0.2);
  border: none;
  color: #fff;
  width: 34px;
  height: 34px;
  border-radius: 50%;
  font-size: 16px;
  line-height: 1;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background .15s;
}
#popup-close:hover { background: rgba(255,255,255,0.4); }

#popup-body {
  padding: 20px;
}

#popup-img {
  width: 100%;
  border-radius: 10px;
  display: block;
}

/* ---- Show-popup button (appears when popup is closed) ---- */
#popup-show-btn {
  position: fixed;
  bottom: 28px;
  right: 28px;
  z-index: 99998;
  background: var(--blue);
  color: #fff;
  border: none;
  border-radius: 50px;
  padding: 14px 24px;
  font-size: 15px;
  font-weight: 700;
  cursor: pointer;
  box-shadow: 0 6px 20px rgba(33,150,243,0.45);
  display: none;          /* hidden by default; shown when popup closes */
  align-items: center;
  gap: 8px;
  transition: background .2s, transform .15s, box-shadow .2s;
}
#popup-show-btn:hover {
  background: var(--blue-dark);
  transform: translateY(-2px);
  box-shadow: 0 10px 28px rgba(33,150,243,0.5);
}
#popup-show-btn:active { transform: translateY(0); }

/* ===== RESPONSIVE ===== */
@media (max-width: 600px) {
  .setup-main { padding: 0 12px 80px; }
  .setup-card { padding: 16px; }
  .word-row { grid-template-columns: 22px 1fr auto; }
  .word-row .hint-input-wrap { display: none; }
  .word-row.no-hint-mode .hint-input-wrap { display: none; }
  .letter-tile { width: 52px; height: 52px; font-size: 22px; }
  .answer-slot { width: 52px; height: 52px; }
  .clue-area { font-size: 26px; padding: 12px 16px; }
  .timer-display { font-size: 22px; }
  .score-display { font-size: 20px; }
  .breadcrumb .game-badge { display: none; }
  .table-header { display: none; }
}
