/*
 * css/ui.css
 * ตกแต่งหน้าตาของเกม Hungry Trash Robot ให้สวยงาม ทันสมัย แบบเด็กเล็ก
 * ใช้ธีมกระจกโปร่งแสง Glassmorphism สีสันสดใส และรองรับการทัช/ลากด้วยกล้อง
 */

:root {
  --primary-color: #ff6b81;
  --success-color: #2ed573;
  --warning-color: #ffa502;
  --danger-color: #ff4757;
  --accent-glow: #00ecff;
  --bg-overlay: rgba(255, 255, 255, 0.15);
  --border-color: rgba(255, 255, 255, 0.4);
  --text-dark: #2c3e50;
  --font-family: 'Mali', 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
}

* {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
  user-select: none;
}

html, body {
  width: 100%;
  height: 100%;
  overflow: hidden;
  background-color: #1a1a2e; /* สีมืดเพื่อให้วิดีโอและกราฟิก 3D ดูโดดเด่น */
  font-family: var(--font-family);
  color: var(--text-dark);
}

/* 1. วิดีโอกล้องเว็บแคม (ฉากหลังแบบกระจกเงา) */
#camera-video {
  position: absolute;
  top: 0;
  left: 0;
  width: 100vw;
  height: 100vh;
  object-fit: cover;
  transform: scaleX(-1); /* กลับข้างกล้องแบบกระจกเงา (Mirror) */
  opacity: 0.35;         /* ความจางตามข้อกำหนด ~0.35 */
  z-index: 1;
  pointer-events: none;
}

/* 2. สามมิติ Three.js Canvas */
#three-canvas {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  z-index: 2;
  pointer-events: none; /* ปล่อยให้คลิกทะลุไปใช้การคำนวณตำแหน่งผ่านกล้อง */
}

/* 3. กล้องย่อส่วนสำหรับ Debug */
#debug-canvas {
  position: absolute;
  top: 15px;
  right: 15px;
  width: 160px;
  height: 120px;
  border-radius: 12px;
  border: 3px solid var(--accent-glow);
  box-shadow: 0 4px 15px rgba(0, 236, 255, 0.5);
  background-color: #000;
  z-index: 100;
  display: block;
}

/* 4. UI Layer หลักครอบจอ */
.ui-screen {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  z-index: 10;
  display: none; /* ซ่อนเป็นค่าเริ่มต้น */
  flex-direction: column;
  justify-content: center;
  align-items: center;
  padding: 20px;
}

.ui-screen.active {
  display: flex; /* แสดงหน้าจอปัจจุบัน */
}

/* สไตล์หน้าต่างโปร่งแสง Glassmorphism */
.glass-panel {
  background: rgba(255, 255, 255, 0.7);
  backdrop-filter: blur(15px);
  -webkit-backdrop-filter: blur(15px);
  border: 4px solid var(--border-color);
  border-radius: 36px;
  padding: 40px;
  max-width: 650px;
  width: 90%;
  box-shadow: 0 15px 35px rgba(0, 0, 0, 0.2);
  text-shadow: 0 1px 2px rgba(255, 255, 255, 0.8);
}

.text-center {
  text-align: center;
}

/* หัวข้อและคำอธิบาย */
.game-title {
  font-size: 3rem;
  font-weight: 700;
  color: #2c3e50;
  margin-bottom: 10px;
  text-shadow: 0 4px 0px rgba(255, 255, 255, 1), 0 0 15px rgba(0, 236, 255, 0.3);
}

.game-subtitle {
  font-size: 1.5rem;
  color: #576574;
  margin-bottom: 35px;
}

/* สไตล์ของปุ่มการ์ตูนขนาดใหญ่สำหรับเด็ก */
.btn {
  font-family: var(--font-family);
  font-size: 1.6rem;
  font-weight: 700;
  color: white;
  border: none;
  border-radius: 50px;
  padding: 15px 40px;
  min-height: 80px; /* ขนาดขั้นต่ำ 80px ตามกำหนด */
  margin: 10px;
  cursor: pointer;
  box-shadow: 0 6px 0px rgba(0, 0, 0, 0.2), 0 10px 20px rgba(0, 0, 0, 0.15);
  transition: transform 0.1s ease, box-shadow 0.1s ease;
  position: relative;
  top: 0;
  display: inline-flex;
  align-items: center;
  justify-content: center;
}

.btn:active, .btn.dwell-hovering {
  top: 4px;
  box-shadow: 0 2px 0px rgba(0, 0, 0, 0.2), 0 4px 10px rgba(0, 0, 0, 0.15);
}

/* ปุ่มระหว่างระบบกำลังโหลด (ยังกดไม่ได้) */
.btn:disabled {
  opacity: 0.55;
  cursor: wait;
  filter: grayscale(0.4);
}

.btn-primary {
  background: linear-gradient(135deg, #ff6b81, #ff4757);
}
.btn-success {
  background: linear-gradient(135deg, #2ed573, #7bed9f);
}
.btn-danger {
  background: linear-gradient(135deg, #ff4757, #ff6b81);
}
.btn-secondary {
  background: linear-gradient(135deg, #747d8c, #2f3542);
}
.btn-large {
  width: 90%;
  max-width: 400px;
}

.btn-icon {
  margin-right: 12px;
  font-size: 2rem;
}

.mt-20 { margin-top: 20px; }
.mb-20 { margin-bottom: 20px; }

/* 5. กล่องตั้งค่าการควบคุม */
.settings-box {
  background: rgba(255, 255, 255, 0.6);
  border-radius: 24px;
  padding: 20px;
  margin-top: 30px;
  text-align: left;
  border: 2px solid var(--border-color);
}

.settings-box h3 {
  font-size: 1.2rem;
  margin-bottom: 12px;
  color: var(--text-dark);
  border-bottom: 2px dashed var(--border-color);
  padding-bottom: 6px;
}

.settings-row {
  display: flex;
  align-items: center;
  margin-bottom: 12px;
}

.settings-row:last-child {
  margin-bottom: 0;
}

.setting-label {
  margin-left: 15px;
  font-size: 1rem;
  color: #34495e;
  line-height: 1.3;
}

.setting-label small {
  color: #7f8c8d;
}

/* สวิตช์เปิด/ปิด (Toggle Switch) */
.switch {
  position: relative;
  display: inline-block;
  width: 60px;
  height: 34px;
  flex-shrink: 0;
}

.switch input { 
  opacity: 0;
  width: 0;
  height: 0;
}

.slider {
  position: absolute;
  cursor: pointer;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-color: #ccc;
  transition: .3s;
  border-radius: 34px;
}

.slider:before {
  position: absolute;
  content: "";
  height: 26px;
  width: 26px;
  left: 4px;
  bottom: 4px;
  background-color: white;
  transition: .3s;
  border-radius: 50%;
}

input:checked + .slider {
  background-color: var(--success-color);
}

input:checked + .slider:before {
  transform: translateX(26px);
}

.camera-select-row {
  margin-top: 15px;
  font-size: 1.1rem;
}

.camera-select-row select {
  font-family: var(--font-family);
  font-size: 1rem;
  padding: 6px 12px;
  border-radius: 12px;
  border: 2px solid var(--border-color);
  margin-left: 10px;
  background: white;
  width: 70%;
  max-width: 250px;
}

/* แบนเนอร์เตือนปัญหากล้อง/ระบบ (ไม่บล็อกการทำงานของหน้า) */
#camera-error-banner {
  position: absolute;
  bottom: 20px;
  left: 50%;
  transform: translateX(-50%);
  max-width: 90%;
  background: rgba(255, 71, 87, 0.95);
  color: white;
  font-family: var(--font-family);
  font-size: 1.1rem;
  font-weight: 700;
  padding: 14px 28px;
  border-radius: 20px;
  border: 3px solid rgba(255, 255, 255, 0.5);
  box-shadow: 0 8px 20px rgba(0, 0, 0, 0.3);
  text-align: center;
  z-index: 2000;
  display: none;
}

/* 6. หน้าจอแจ้งเตือนไม่พบมือ (Pause Overlay) */
#hand-lost-overlay {
  background-color: rgba(0, 0, 0, 0.7);
  z-index: 1000;
}

.lost-panel {
  background: linear-gradient(135deg, #ff4757, #ff6b81);
  border-radius: 30px;
  padding: 40px;
  color: white;
  max-width: 500px;
  border: 4px solid var(--border-color);
  box-shadow: 0 10px 30px rgba(255, 71, 87, 0.4);
}

.lost-panel h2 {
  font-size: 2.2rem;
  margin: 15px 0 10px 0;
}

.lost-panel p {
  font-size: 1.3rem;
}

.lost-icon {
  font-size: 4.5rem;
}

/* 7. Cursor มือการ์ตูนเรืองแสง */
#hand-cursor {
  position: absolute;
  width: 80px;
  height: 80px;
  margin-left: -40px; /* กึ่งกลาง */
  margin-top: -40px;  /* กึ่งกลาง */
  z-index: 9999;
  pointer-events: none; /* ห้ามตรวจจับเมาส์ */
  transition: transform 0.1s cubic-bezier(0.175, 0.885, 0.32, 1.275);
  display: flex;
  justify-content: center;
  align-items: center;
}

.cursor-icon {
  font-size: 3.5rem;
  filter: drop-shadow(0px 2px 5px rgba(0,0,0,0.3));
  transition: transform 0.1s ease;
}

/* เอฟเฟกต์เรืองแสงตามสถานะมือ */
.cursor-glow {
  position: absolute;
  width: 70px;
  height: 70px;
  border-radius: 50%;
  background: radial-gradient(circle, rgba(0,236,255,0.6) 0%, rgba(0,236,255,0) 70%);
  animation: pulse-glow 2s infinite alternate;
  pointer-events: none;
}

/* การควบคุม Dwell Loading Ring */
.dwell-ring {
  position: absolute;
  width: 90px;
  height: 90px;
  top: -5px;
  left: -5px;
  transform: rotate(-90deg);
  pointer-events: none;
  display: none; /* แสดงเฉพาะเมื่อ Dwell Mode เปิดอยู่ */
}

/* สถานะต่าง ๆ ของ Cursor */
#hand-cursor.hover .cursor-icon {
  transform: scale(1.2);
}

#hand-cursor.hover .cursor-glow {
  background: radial-gradient(circle, rgba(255,165,0,0.7) 0%, rgba(255,165,0,0) 70%);
}

#hand-cursor.grab .cursor-icon {
  transform: scale(0.9);
}

#hand-cursor.grab .cursor-glow {
  background: radial-gradient(circle, rgba(46,213,115,0.7) 0%, rgba(46,213,115,0) 70%);
}

/* 8. HUD แสดงผลคะแนนสะสมดาว (Progress Bar) */
.hud-top {
  position: absolute;
  top: 15px;
  left: 5%;
  width: 90%;
  display: flex;
  justify-content: space-between;
  align-items: center;
  z-index: 50;
  pointer-events: none;
}

.hud-top button {
  pointer-events: auto; /* คลิกปุ่มเมนูได้ */
}

.star-progress-container {
  display: flex;
  align-items: center;
  background: rgba(255, 255, 255, 0.85);
  backdrop-filter: blur(5px);
  border-radius: 30px;
  padding: 10px 25px;
  border: 3px solid var(--border-color);
  box-shadow: 0 8px 20px rgba(0,0,0,0.1);
  max-width: 600px;
  width: 75%;
}

.hud-label {
  font-size: 1.2rem;
  font-weight: 700;
  margin-right: 15px;
  white-space: nowrap;
}

.progress-bar-track {
  flex-grow: 1;
  height: 25px;
  background-color: #dfe4ea;
  border-radius: 20px;
  overflow: hidden;
  border: 2px solid #ced6e0;
  position: relative;
}

.progress-bar-fill {
  height: 100%;
  background: linear-gradient(90deg, #ffa502, #ffda79, #2ed573);
  border-radius: 18px;
  width: 0%;
  transition: width 0.5s cubic-bezier(0.175, 0.885, 0.32, 1.275);
  box-shadow: inset 0 -3px 5px rgba(0,0,0,0.15);
}

.score-badge {
  font-size: 1.3rem;
  font-weight: 700;
  color: var(--text-dark);
  margin-left: 15px;
  background-color: #ffa502;
  color: white;
  padding: 4px 12px;
  border-radius: 15px;
  text-shadow: 0 1px 2px rgba(0,0,0,0.2);
  white-space: nowrap;
}

.btn-small {
  min-height: 50px;
  height: 50px;
  width: 50px;
  padding: 0;
  margin: 0;
  border-radius: 50%;
  font-size: 1.3rem;
}

/* ข้อความใบ้เตือน (Toast) */
.game-toast {
  position: absolute;
  top: 90px;
  left: 50%;
  transform: translateX(-50%) translateY(20px);
  background: rgba(255, 71, 87, 0.9);
  color: white;
  padding: 12px 30px;
  border-radius: 20px;
  font-size: 1.3rem;
  font-weight: 700;
  box-shadow: 0 8px 15px rgba(0,0,0,0.2);
  opacity: 0;
  transition: opacity 0.3s, transform 0.3s;
  z-index: 60;
  pointer-events: none;
}

.game-toast.show {
  opacity: 1;
  transform: translateX(-50%) translateY(0);
}

/* 9. หน้าจอสอนเล่น (Tutorial Panel) */
.tutorial-panel {
  position: absolute;
  top: 100px;
  left: 50%;
  transform: translateX(-50%);
  background: rgba(255, 255, 255, 0.9);
  backdrop-filter: blur(10px);
  border: 4px solid var(--border-color);
  border-radius: 30px;
  padding: 20px 40px;
  max-width: 650px;
  width: 85%;
  box-shadow: 0 12px 25px rgba(0,0,0,0.15);
  pointer-events: none;
}

#tutorial-title {
  font-size: 2.2rem;
  color: var(--primary-color);
  margin-bottom: 5px;
}

#tutorial-instruction {
  font-size: 1.3rem;
  color: #576574;
}

.task-badge {
  display: inline-block;
  background-color: var(--accent-glow);
  color: #1e272e;
  font-weight: 700;
  font-size: 1.2rem;
  padding: 4px 15px;
  border-radius: 12px;
  margin-top: 10px;
}

/* แอนิเมชันสำหรับมือเงาสอนเล่น (Ghost Hand) */
#ghost-hand-demo {
  position: absolute;
  z-index: 100;
  pointer-events: none;
  display: none; /* แสดงผลเฉพาะเมื่อไม่มีการขยับ */
}

.ghost-hand {
  font-size: 3.5rem;
  opacity: 0.7;
  filter: drop-shadow(0 0 10px rgba(0, 236, 255, 0.8));
  animation: ghost-drag 3s infinite linear;
}

.ghost-instruction {
  background-color: rgba(0, 236, 255, 0.9);
  color: #1e272e;
  padding: 5px 12px;
  border-radius: 10px;
  font-weight: 700;
  font-size: 0.9rem;
  text-align: center;
  margin-top: 5px;
}

/* 10. ระดับความยาก (Level Selection) */
.level-title {
  font-size: 2rem;
  margin-bottom: 10px;
  color: var(--text-dark);
}

.level-desc {
  font-size: 1.2rem;
  color: #576574;
  margin-bottom: 25px;
}

.level-buttons {
  display: flex;
  flex-direction: column;
  align-items: center;
  margin-bottom: 15px;
}

.level-btn {
  width: 80%;
  max-width: 320px;
}

/* 11. หน้าจอฉลองชัยชนะ (Celebrate Screen) */
.trophy-icon {
  font-size: 5rem;
  animation: trophy-bounce 1s infinite alternate;
}

.victory-title {
  font-size: 2.8rem;
  color: #2ed573;
  margin: 15px 0 10px 0;
  text-shadow: 0 3px 0 white;
}

.victory-desc {
  font-size: 1.5rem;
  color: #576574;
  margin-bottom: 20px;
}

#victory-score-summary {
  font-size: 1.3rem;
  font-weight: 700;
  background-color: rgba(46, 213, 115, 0.15);
  border-radius: 20px;
  padding: 10px 20px;
  color: #20bf6b;
  display: inline-block;
}

/* 12. Floating Badge สัญลักษณ์ของหุ่นยนต์เหนือหัว */
.robot-badge {
  position: absolute;
  transform: translate(-50%, -100%);
  background-color: white;
  border: 3px solid var(--text-dark);
  border-radius: 50%;
  width: 60px;
  height: 60px;
  display: flex;
  justify-content: center;
  align-items: center;
  font-size: 2rem;
  box-shadow: 0 4px 10px rgba(0,0,0,0.2);
  z-index: 8;
  animation: badge-float 2s infinite alternate ease-in-out;
  pointer-events: none;
}

.badge-greeny { border-color: var(--success-color); }
.badge-bluey  { border-color: #2f3542; }
.badge-yelly  { border-color: var(--warning-color); }
.badge-reddy  { border-color: var(--danger-color); }

/* 13. ป้ายชื่อขยะตัวใหญ่ ลอยเหนือขยะชิ้นปัจจุบัน */
#trash-name-label {
  position: absolute;
  transform: translate(-50%, -100%);
  background: rgba(255, 255, 255, 0.92);
  color: var(--text-dark);
  font-family: var(--font-family);
  font-size: 1.8rem;
  font-weight: 700;
  padding: 6px 26px;
  border-radius: 25px;
  border: 3px solid var(--accent-glow);
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.25), 0 0 15px rgba(0, 236, 255, 0.4);
  white-space: nowrap;
  text-align: center;
  z-index: 9;
  pointer-events: none;
  display: none;
  animation: badge-float 2s infinite alternate ease-in-out;
}

/* --- แอนิเมชันต่าง ๆ (Keyframes) --- */

@keyframes pulse-glow {
  0% { transform: scale(0.9); opacity: 0.5; }
  100% { transform: scale(1.1); opacity: 0.9; }
}

@keyframes pulse {
  0% { transform: scale(1); }
  50% { transform: scale(1.03); }
  100% { transform: scale(1); }
}

.pulse {
  animation: pulse 2s infinite ease-in-out;
}

@keyframes bounce-in {
  0% { transform: scale(0.3); opacity: 0; }
  50% { transform: scale(1.05); }
  70% { transform: scale(0.9); opacity: 1; }
  100% { transform: scale(1); }
}

.bounce-in {
  animation: bounce-in 0.6s cubic-bezier(0.175, 0.885, 0.32, 1.275) forwards;
}

@keyframes trophy-bounce {
  0% { transform: translateY(0) scale(1); }
  100% { transform: translateY(-15px) scale(1.05); }
}

@keyframes badge-float {
  0% { margin-top: 0px; }
  100% { margin-top: -8px; }
}

/* แอนิเมชันสำหรับมือสาธิต Ghost Hand Drag */
@keyframes ghost-drag {
  0% { transform: translate(0, 0); opacity: 0; }
  15% { transform: translate(0, 0); opacity: 1; }
  75% { transform: translate(var(--drag-x, 250px), var(--drag-y, 350px)); opacity: 1; }
  90% { transform: translate(var(--drag-x, 250px), var(--drag-y, 350px)); opacity: 0; }
  100% { transform: translate(0, 0); opacity: 0; }
}
