/* ===== 基礎重設 ===== */
*, *::before, *::after {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

:root {
  --primary: #7c3aed;
  --primary-light: #a78bfa;
  --accent: #06b6d4;
  --accent-light: #67e8f9;
  --success: #10b981;
  --warning: #f59e0b;
  --danger: #ef4444;
  --bg-dark: rgba(0, 0, 0, 0.75);
  --bg-panel: rgba(15, 10, 30, 0.8);
  --border: rgba(167, 139, 250, 0.2);
  --text-primary: #f1f5f9;
  --text-secondary: #94a3b8;
  --text-accent: #a78bfa;
  --glow-purple: 0 0 20px rgba(124, 58, 237, 0.5);
  --glow-cyan: 0 0 20px rgba(6, 182, 212, 0.5);
  --radius: 12px;
  --radius-sm: 8px;
  --transition: all 0.2s ease;
}

html, body {
  width: 100%;
  height: 100%;
  overflow: hidden;
  background: #000;
  font-family: 'Segoe UI', 'PingFang TC', 'Microsoft JhengHei', system-ui, sans-serif;
  color: var(--text-primary);
}

/* ===== 主要容器 ===== */
#app {
  position: relative;
  width: 100vw;
  height: 100vh;
  overflow: hidden;
}

#video {
  display: none;
}

#canvas {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  display: block;
}

/* ===== UI 覆蓋層 ===== */
#ui-overlay {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  pointer-events: none;
  z-index: 10;
  display: grid;
  grid-template-columns: 1fr auto;
  grid-template-rows: auto 1fr auto;
  gap: 12px;
  padding: 16px;
}

/* ===== 玻璃擬態面板 ===== */
.panel {
  background: var(--bg-panel);
  backdrop-filter: blur(16px) saturate(150%);
  -webkit-backdrop-filter: blur(16px) saturate(150%);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 14px 16px;
  box-shadow: var(--glow-purple), inset 0 1px 0 rgba(255,255,255,0.05);
  pointer-events: auto;
}

/* ===== 頂部標題欄 ===== */
#header {
  grid-column: 1 / -1;
  grid-row: 1;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 10px 18px;
}

#title-section {
  display: flex;
  align-items: center;
  gap: 10px;
}

#title-section .icon {
  font-size: 22px;
  filter: drop-shadow(0 0 8px rgba(167, 139, 250, 0.8));
}

#title-section h1 {
  font-size: 18px;
  font-weight: 700;
  background: linear-gradient(135deg, var(--primary-light), var(--accent-light));
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
  letter-spacing: 0.02em;
}

#status-bar {
  display: flex;
  gap: 20px;
  align-items: center;
}

.status-item {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
}

.status-label {
  font-size: 10px;
  color: var(--text-secondary);
  text-transform: uppercase;
  letter-spacing: 0.08em;
}

.status-value {
  font-size: 16px;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
  color: var(--accent-light);
  text-shadow: 0 0 10px rgba(6, 182, 212, 0.6);
}

/* ===== 手勢顯示 ===== */
#gesture-display {
  grid-column: 1;
  grid-row: 2;
  align-self: start;
  justify-self: start;
  min-width: 200px;
  text-align: center;
  transition: var(--transition);
}

#gesture-icon {
  font-size: 52px;
  line-height: 1;
  margin-bottom: 8px;
  filter: drop-shadow(0 0 12px rgba(167, 139, 250, 0.8));
  transition: transform 0.3s cubic-bezier(0.34, 1.56, 0.64, 1);
}

#gesture-display.detected #gesture-icon {
  transform: scale(1.1);
}

#gesture-name {
  font-size: 16px;
  font-weight: 700;
  color: var(--primary-light);
  margin-bottom: 4px;
}

#gesture-description {
  font-size: 12px;
  color: var(--text-secondary);
  line-height: 1.4;
}

#gesture-confidence {
  font-size: 11px;
  color: var(--success);
  margin-top: 4px;
  font-weight: 600;
}

/* ===== 控制面板 ===== */
#controls {
  grid-column: 2;
  grid-row: 1 / 3;
  align-self: start;
  width: 220px;
  display: flex;
  flex-direction: column;
  gap: 14px;
}

.control-group {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.group-label {
  font-size: 11px;
  font-weight: 600;
  color: var(--text-accent);
  text-transform: uppercase;
  letter-spacing: 0.1em;
}

/* ===== 特效按鈕 ===== */
#effect-buttons {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 6px;
}

.effect-btn {
  padding: 8px 6px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: rgba(255, 255, 255, 0.03);
  color: var(--text-secondary);
  font-size: 12px;
  font-weight: 500;
  cursor: pointer;
  transition: var(--transition);
  text-align: center;
  white-space: nowrap;
}

.effect-btn:hover {
  background: rgba(124, 58, 237, 0.2);
  border-color: var(--primary);
  color: var(--primary-light);
  box-shadow: var(--glow-purple);
}

.effect-btn.active {
  background: linear-gradient(135deg, rgba(124, 58, 237, 0.4), rgba(6, 182, 212, 0.2));
  border-color: var(--primary-light);
  color: var(--text-primary);
  box-shadow: var(--glow-purple);
}

/* ===== 切換開關 ===== */
.toggle-group {
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.toggle-item {
  display: flex;
  align-items: center;
  gap: 10px;
  cursor: pointer;
  user-select: none;
}

.toggle-item input[type="checkbox"] {
  display: none;
}

.toggle-track {
  width: 36px;
  height: 20px;
  background: rgba(255, 255, 255, 0.1);
  border-radius: 10px;
  position: relative;
  transition: background 0.2s ease;
  border: 1px solid var(--border);
  flex-shrink: 0;
}

.toggle-track::after {
  content: '';
  position: absolute;
  width: 14px;
  height: 14px;
  border-radius: 50%;
  background: var(--text-secondary);
  top: 2px;
  left: 2px;
  transition: transform 0.2s ease, background 0.2s ease;
}

.toggle-item input:checked + .toggle-track {
  background: rgba(124, 58, 237, 0.4);
  border-color: var(--primary);
}

.toggle-item input:checked + .toggle-track::after {
  transform: translateX(16px);
  background: var(--primary-light);
  box-shadow: 0 0 8px rgba(124, 58, 237, 0.8);
}

.toggle-label {
  font-size: 13px;
  color: var(--text-secondary);
  transition: color 0.2s;
}

.toggle-item:hover .toggle-label {
  color: var(--text-primary);
}

/* ===== 滑桿 ===== */
.slider-group {
  display: flex;
  align-items: center;
  gap: 8px;
}

.slider-label {
  font-size: 12px;
  color: var(--text-secondary);
  white-space: nowrap;
  min-width: 52px;
}

input[type="range"] {
  -webkit-appearance: none;
  appearance: none;
  flex: 1;
  height: 4px;
  background: rgba(255, 255, 255, 0.1);
  border-radius: 2px;
  outline: none;
  cursor: pointer;
}

input[type="range"]::-webkit-slider-thumb {
  -webkit-appearance: none;
  appearance: none;
  width: 14px;
  height: 14px;
  border-radius: 50%;
  background: var(--primary-light);
  box-shadow: 0 0 8px rgba(124, 58, 237, 0.8);
  cursor: pointer;
  transition: transform 0.1s;
}

input[type="range"]::-webkit-slider-thumb:hover {
  transform: scale(1.2);
}

.slider-value {
  font-size: 12px;
  color: var(--accent-light);
  font-weight: 600;
  min-width: 20px;
  text-align: center;
}

/* ===== Grok AI 面板 ===== */
#grok-panel {
  grid-column: 2;
  grid-row: 3;
  width: 220px;
  display: flex;
  flex-direction: column;
  gap: 10px;
  border-color: rgba(6, 182, 212, 0.25);
  background: rgba(5, 15, 30, 0.85);
}

#grok-header {
  display: flex;
  flex-direction: column;
  gap: 4px;
}

#grok-title {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 13px;
  font-weight: 700;
  background: linear-gradient(135deg, #67e8f9, #a78bfa);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
}

#grok-icon-label {
  font-size: 16px;
  -webkit-text-fill-color: initial;
  filter: drop-shadow(0 0 6px rgba(6, 182, 212, 0.8));
}

#grok-status {
  display: flex;
  align-items: center;
  gap: 5px;
}

#grok-dot {
  width: 7px;
  height: 7px;
  border-radius: 50%;
  flex-shrink: 0;
  transition: background 0.3s, box-shadow 0.3s;
}

#grok-dot.idle    { background: #475569; }
#grok-dot.waiting { background: #f59e0b; box-shadow: 0 0 8px #f59e0b; animation: pulse-dot 1s ease-in-out infinite; }
#grok-dot.loading { background: #06b6d4; box-shadow: 0 0 8px #06b6d4; animation: pulse-dot 0.6s ease-in-out infinite; }
#grok-dot.done    { background: #10b981; box-shadow: 0 0 8px #10b981; }
#grok-dot.error   { background: #ef4444; box-shadow: 0 0 8px #ef4444; }

@keyframes pulse-dot {
  0%, 100% { opacity: 1; transform: scale(1); }
  50%       { opacity: 0.5; transform: scale(0.7); }
}

#grok-status-text {
  font-size: 10px;
  color: var(--text-secondary);
  line-height: 1.3;
}

#grok-body {
  min-height: 70px;
  max-height: 120px;
  overflow-y: auto;
  border-radius: var(--radius-sm);
  background: rgba(255, 255, 255, 0.03);
  border: 1px solid var(--border);
  padding: 8px 10px;
}

#grok-message-wrap {
  position: relative;
}

#grok-loading {
  display: flex;
  gap: 5px;
  align-items: center;
  padding: 4px 0;
}

#grok-loading.hidden { display: none; }

.bounce-dot {
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: var(--accent);
  animation: bounce-anim 1.2s ease-in-out infinite;
  box-shadow: 0 0 6px var(--accent);
}

.bounce-dot:nth-child(2) { animation-delay: 0.2s; }
.bounce-dot:nth-child(3) { animation-delay: 0.4s; }

@keyframes bounce-anim {
  0%, 80%, 100% { transform: scale(0.6); opacity: 0.4; }
  40%           { transform: scale(1);   opacity: 1;   }
}

#grok-message {
  font-size: 12px;
  color: var(--text-secondary);
  line-height: 1.6;
  white-space: pre-wrap;
  word-break: break-word;
  transition: color 0.3s;
}

#grok-message.has-response {
  color: var(--text-primary);
}

#grok-message.has-error {
  color: #f87171;
}

#grok-input-row {
  display: flex;
  gap: 6px;
}

#grok-input {
  flex: 1;
  padding: 6px 10px;
  background: rgba(255, 255, 255, 0.05);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  color: var(--text-primary);
  font-size: 12px;
  outline: none;
  transition: border-color 0.2s, box-shadow 0.2s;
  font-family: inherit;
}

#grok-input:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 2px rgba(6, 182, 212, 0.2);
}

#grok-input::placeholder {
  color: var(--text-secondary);
  opacity: 0.6;
}

#grok-send {
  padding: 6px 10px;
  background: linear-gradient(135deg, var(--primary), var(--accent));
  border: none;
  border-radius: var(--radius-sm);
  color: white;
  font-size: 14px;
  font-weight: 700;
  cursor: pointer;
  transition: var(--transition);
  line-height: 1;
}

#grok-send:hover {
  opacity: 0.85;
  box-shadow: var(--glow-cyan);
}

#grok-send:disabled {
  opacity: 0.4;
  cursor: not-allowed;
}

/* AI 訊息出現動畫 */
@keyframes slide-in-message {
  from { opacity: 0; transform: translateY(4px); }
  to   { opacity: 1; transform: translateY(0); }
}

#grok-message.animate-in {
  animation: slide-in-message 0.3s ease forwards;
}

/* ===== 手勢指南 ===== */
#gesture-guide {
  grid-column: 1;
  grid-row: 3;
  align-self: end;
}

#gesture-guide h3 {
  font-size: 12px;
  font-weight: 600;
  color: var(--text-accent);
  text-transform: uppercase;
  letter-spacing: 0.1em;
  margin-bottom: 10px;
}

.gesture-list {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 6px;
}

.gesture-item {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
  padding: 8px 4px;
  border-radius: var(--radius-sm);
  border: 1px solid transparent;
  transition: var(--transition);
  cursor: default;
}

.gesture-item:hover, .gesture-item.active {
  background: rgba(124, 58, 237, 0.15);
  border-color: var(--border);
}

.gesture-item.active {
  background: rgba(124, 58, 237, 0.25);
  border-color: var(--primary);
}

.g-icon {
  font-size: 24px;
  filter: drop-shadow(0 0 6px rgba(167, 139, 250, 0.6));
}

.g-name {
  font-size: 11px;
  font-weight: 600;
  color: var(--text-primary);
}

.g-effect {
  font-size: 10px;
  color: var(--text-secondary);
  text-align: center;
}

/* ===== 載入畫面 ===== */
#loading {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  background: radial-gradient(ellipse at center, #0f0720 0%, #050215 100%);
  z-index: 100;
  transition: opacity 0.5s ease;
}

#loading.fade-out {
  opacity: 0;
  pointer-events: none;
}

.loader-content {
  text-align: center;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 16px;
}

.spinner-container {
  position: relative;
  width: 80px;
  height: 80px;
}

.spinner-ring {
  position: absolute;
  inset: 0;
  border-radius: 50%;
  border: 3px solid transparent;
  border-top-color: var(--primary);
  animation: spin 1.2s linear infinite;
}

.spinner-ring-2 {
  inset: 8px;
  border-top-color: var(--accent);
  animation-delay: -0.4s;
  animation-duration: 1.5s;
}

.spinner-ring-3 {
  inset: 16px;
  border-top-color: var(--primary-light);
  animation-delay: -0.8s;
  animation-duration: 0.9s;
}

@keyframes spin {
  to { transform: rotate(360deg); }
}

.loader-content h2 {
  font-size: 20px;
  font-weight: 700;
  background: linear-gradient(135deg, var(--primary-light), var(--accent-light));
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
}

#loading-status {
  font-size: 14px;
  color: var(--text-secondary);
}

#loading-progress {
  width: 250px;
  height: 4px;
  background: rgba(255, 255, 255, 0.1);
  border-radius: 2px;
  overflow: hidden;
}

#loading-bar {
  width: 0%;
  height: 100%;
  background: linear-gradient(90deg, var(--primary), var(--accent));
  border-radius: 2px;
  transition: width 0.3s ease;
  box-shadow: 0 0 8px rgba(124, 58, 237, 0.6);
}

/* ===== 錯誤覆蓋層 ===== */
#error-overlay {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  background: rgba(0, 0, 0, 0.9);
  z-index: 200;
}

#error-overlay.hidden {
  display: none;
}

.error-content {
  text-align: center;
  padding: 40px;
  max-width: 400px;
  background: var(--bg-panel);
  border: 1px solid rgba(239, 68, 68, 0.3);
  border-radius: var(--radius);
  backdrop-filter: blur(16px);
}

.error-icon {
  font-size: 48px;
  display: block;
  margin-bottom: 16px;
}

.error-content h2 {
  font-size: 20px;
  color: var(--danger);
  margin-bottom: 12px;
}

.error-content p {
  font-size: 14px;
  color: var(--text-secondary);
  line-height: 1.6;
  margin-bottom: 20px;
}

#error-retry {
  padding: 10px 24px;
  background: var(--danger);
  border: none;
  border-radius: var(--radius-sm);
  color: white;
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
  transition: var(--transition);
}

#error-retry:hover {
  background: #dc2626;
  box-shadow: 0 0 16px rgba(239, 68, 68, 0.5);
}

/* ===== 脈衝動畫（偵測到手時） ===== */
@keyframes pulse-glow {
  0%, 100% { box-shadow: var(--glow-purple), inset 0 1px 0 rgba(255,255,255,0.05); }
  50% { box-shadow: 0 0 30px rgba(124, 58, 237, 0.7), 0 0 60px rgba(124, 58, 237, 0.3), inset 0 1px 0 rgba(255,255,255,0.05); }
}

#gesture-display.detected {
  animation: pulse-glow 2s ease-in-out infinite;
}

/* ===== 響應式設計 ===== */
@media (max-width: 768px) {
  #ui-overlay {
    grid-template-columns: 1fr;
    padding: 8px;
    gap: 8px;
  }

  #controls {
    grid-column: 1;
    grid-row: auto;
    width: 100%;
  }

  #gesture-guide .gesture-list {
    grid-template-columns: repeat(6, 1fr);
  }

  #header h1 {
    display: none;
  }
}

/* ===== 自訂捲動條 ===== */
::-webkit-scrollbar {
  width: 6px;
}

::-webkit-scrollbar-track {
  background: rgba(255, 255, 255, 0.05);
}

::-webkit-scrollbar-thumb {
  background: var(--primary);
  border-radius: 3px;
}
