* {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
  -webkit-tap-highlight-color: transparent;
  user-select: none;
  -webkit-user-select: none;
}

:root {
  --primary-color: #4f46e5;
  --primary-light: #6366f1;
  --accent-color: #10b981;
  --bg-gradient-top: #fce7f3;
  --bg-gradient-mid: #e0e7ff;
  --bg-gradient-bottom: #fef3c7;
  --board-bg: #ffffff;
  --dot-color: #d1d5db;
  --text-main: #1f2937;
  --text-muted: #6b7280;
  --btn-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1), 0 2px 4px -1px rgba(0, 0, 0, 0.06);
  --card-shadow: 0 10px 25px -5px rgba(0, 0, 0, 0.15), 0 8px 10px -6px rgba(0, 0, 0, 0.1);
}

html, body {
  width: 100%;
  height: 100%;
  overflow: hidden;
  font-family: 'Fredoka', -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "PingFang TC", "Microsoft JhengHei", sans-serif;
  background: linear-gradient(145deg, #ffd3b6 0%, #ffaaa5 30%, #a8e6cf 70%, #dcedc1 100%);
  color: var(--text-main);
  touch-action: manipulation;
}

#app-container {
  display: flex;
  flex-direction: column;
  width: 100%;
  height: 100%;
  max-width: 500px;
  margin: 0 auto;
  position: relative;
  background: linear-gradient(180deg, rgba(255,255,255,0.7) 0%, rgba(255,255,255,0.4) 100%);
  backdrop-filter: blur(16px);
  -webkit-backdrop-filter: blur(16px);
  box-shadow: 0 0 40px rgba(0,0,0,0.15);
}

/* 頂部 Header */
#game-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 12px 18px;
  padding-top: max(12px, env(safe-area-inset-top));
  z-index: 10;
}

.header-left {
  display: flex;
  align-items: center;
  gap: 10px;
}

#level-title {
  font-size: 1.15rem;
  font-weight: 700;
  color: #374151;
  letter-spacing: -0.5px;
  max-width: 170px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.badge {
  background: rgba(79, 70, 229, 0.1);
  color: var(--primary-color);
  padding: 4px 10px;
  border-radius: 20px;
  font-size: 0.85rem;
  font-weight: 600;
}

.header-right {
  display: flex;
  gap: 8px;
}

.icon-btn {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  border: none;
  background: white;
  box-shadow: var(--btn-shadow);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 1.1rem;
  cursor: pointer;
  transition: transform 0.15s cubic-bezier(0.34, 1.56, 0.64, 1), background 0.2s;
}

.icon-btn:active {
  transform: scale(0.9);
}

/* 主畫布區域 */
#canvas-wrapper {
  flex: 1;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  position: relative;
  padding: 8px 14px;
  min-height: 0;
}

#game-canvas {
  background: #ffffff;
  border-radius: 22px;
  box-shadow: 0 12px 32px -4px rgba(0, 0, 0, 0.12), 0 4px 12px -2px rgba(0, 0, 0, 0.06);
  border: 1px solid rgba(226, 232, 240, 0.8);
  max-width: 100%;
  max-height: 100%;
  display: block;
  touch-action: none;
}

.floating-hint {
  position: absolute;
  bottom: 18px;
  background: rgba(31, 41, 55, 0.85);
  color: white;
  padding: 6px 14px;
  border-radius: 20px;
  font-size: 0.85rem;
  pointer-events: none;
  opacity: 0.95;
  transition: opacity 0.4s ease, transform 0.4s ease;
  animation: pulseHint 2s infinite ease-in-out;
}

.floating-hint.fade-out {
  opacity: 0;
  transform: translateY(10px);
}

@keyframes pulseHint {
  0%, 100% { transform: translateY(0); }
  50% { transform: translateY(-4px); }
}

/* 底部操作欄 */
#game-footer {
  display: flex;
  justify-content: space-around;
  align-items: center;
  padding: 12px 18px;
  padding-bottom: max(16px, env(safe-area-inset-bottom));
  gap: 12px;
  z-index: 10;
}

.action-btn {
  flex: 1;
  height: 48px;
  border-radius: 14px;
  border: none;
  background: white;
  box-shadow: var(--btn-shadow);
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  font-size: 0.95rem;
  font-weight: 600;
  color: #374151;
  cursor: pointer;
  transition: all 0.15s cubic-bezier(0.34, 1.56, 0.64, 1);
}

.action-btn:active {
  transform: scale(0.95);
  background: #f3f4f6;
}

.hint-btn {
  background: linear-gradient(135deg, #fef3c7, #fffbeb);
  color: #b45309;
  border: 1px solid #fde68a;
}

.undo-btn {
  background: #ffffff;
}

.next-btn {
  background: linear-gradient(135deg, #e0e7ff, #ede9fe);
  color: var(--primary-color);
  border: 1px solid #c7d2fe;
}

/* 彈窗樣式 */
.modal-overlay {
  position: absolute;
  inset: 0;
  background: rgba(0, 0, 0, 0.45);
  backdrop-filter: blur(6px);
  -webkit-backdrop-filter: blur(6px);
  display: flex;
  justify-content: center;
  align-items: center;
  z-index: 100;
  padding: 20px;
  opacity: 1;
  transition: opacity 0.25s ease;
}

.modal-overlay.hidden {
  opacity: 0;
  pointer-events: none;
}

.modal-card {
  width: 100%;
  max-width: 380px;
  background: white;
  border-radius: 24px;
  padding: 24px;
  box-shadow: 0 20px 30px rgba(0,0,0,0.2);
  transform: translateY(0);
  transition: transform 0.25s cubic-bezier(0.34, 1.56, 0.64, 1);
}

.modal-overlay.hidden .modal-card {
  transform: translateY(20px) scale(0.95);
}

.modal-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 18px;
}

.modal-header h3 {
  font-size: 1.25rem;
  color: #1f2937;
}

.close-btn {
  width: 32px;
  height: 32px;
  border: none;
  background: #f3f4f6;
  border-radius: 50%;
  font-size: 1.3rem;
  line-height: 1;
  cursor: pointer;
  color: #6b7280;
}

.levels-grid {
  display: grid;
  grid-template-columns: repeat(5, 1fr);
  gap: 10px;
  max-height: 340px;
  overflow-y: auto;
  padding: 4px;
}

.level-card {
  aspect-ratio: 0.9;
  border-radius: 12px;
  border: 2px solid #e5e7eb;
  background: #f9fafb;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  cursor: pointer;
  transition: all 0.15s ease;
  position: relative;
  padding: 4px;
}

.level-card .lvl-num {
  font-size: 1.05rem;
  font-weight: 700;
  color: #1f2937;
}

.level-card .lvl-count {
  font-size: 0.65rem;
  color: #6b7280;
  font-weight: 500;
  margin-top: 2px;
}

.level-card.active {
  border-color: var(--primary-color);
  background: #ede9fe;
}

.level-card.active .lvl-num {
  color: var(--primary-color);
}

.level-card.cleared::after {
  content: '★';
  color: #f59e0b;
  font-size: 0.8rem;
  position: absolute;
  bottom: 2px;
}

.level-card:active {
  transform: scale(0.92);
}

/* 勝利彈窗 */
.victory-card {
  text-align: center;
}

.victory-banner .trophy-icon {
  font-size: 4rem;
  margin-bottom: 8px;
  animation: bounceTrophy 1s infinite alternate cubic-bezier(0.28, 0.84, 0.42, 1);
}

@keyframes bounceTrophy {
  0% { transform: translateY(0) scale(1); }
  100% { transform: translateY(-10px) scale(1.08); }
}

.victory-banner h2 {
  font-size: 1.6rem;
  color: #111827;
  margin-bottom: 4px;
}

.victory-banner p {
  color: #6b7280;
  font-size: 0.95rem;
  margin-bottom: 20px;
}

.victory-stats {
  display: flex;
  justify-content: space-around;
  background: #f9fafb;
  border-radius: 16px;
  padding: 14px;
  margin-bottom: 22px;
}

.stat-item {
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.stat-label {
  font-size: 0.8rem;
  color: #9ca3af;
}

.stat-val {
  font-size: 1.3rem;
  font-weight: 700;
  color: var(--primary-color);
}

.victory-actions {
  display: flex;
  gap: 12px;
}

.modal-btn {
  flex: 1;
  padding: 12px;
  border-radius: 14px;
  border: none;
  font-size: 1rem;
  font-weight: 600;
  cursor: pointer;
  transition: transform 0.15s;
}

.modal-btn:active {
  transform: scale(0.95);
}

.primary-btn {
  background: linear-gradient(135deg, #4f46e5, #6366f1);
  color: white;
  box-shadow: 0 4px 12px rgba(79, 70, 229, 0.35);
}

.secondary-btn {
  background: #f3f4f6;
  color: #374151;
}
