/* ═══════════════════════════════════════════════════════════
   HAND OF THE KING — Castle Defense Gesture Game
   CSS: Fullscreen layout, three canvas layers, side panel
   ═══════════════════════════════════════════════════════════ */

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

/* Hide any canvas elements injected directly into body by MediaPipe FaceMesh.
   The library appends its internal WebGL processing canvas to document.body
   with no styles — left visible it appears as a full-colour video overlay. */
body > canvas { display: none !important; visibility: hidden !important; opacity: 0 !important; }

:root {
  --panel-w: 220px;
  --accent:  #ff6b35;
  --accent2: #00d4ff;
  --bg:      #0e0e1a;
  --panel-bg: rgba(14, 14, 26, 0.92);
  --border:  rgba(255,255,255,0.08);
  --text:    #e8e8f0;
  --text-dim: #b8bdd0;
  --red:     #ff3355;
  --gold:    #ffd700;
  --green:   #00e589;
}

html, body {
  width: 100%; height: 100%;
  overflow: hidden;
  background: var(--bg);
  font-family: 'Courier New', 'SF Mono', monospace;
  color: var(--text);
  user-select: none;
}

/* ── MAIN LAYOUT ─────────────────────────────────────────── */

#game-wrapper {
  position: relative;
  width: calc(100vw - var(--panel-w));
  height: 100vh;
  float: left;
  overflow: hidden;
  background: #000;
}

/* ── VIDEO (bottom layer) ────────────────────────────────── */

#webcam {
  position: absolute;
  inset: 0;
  width: 100%; height: 100%;
  object-fit: cover;
  opacity: 0;               /* hidden by default — only shows when cam toggle is active */
  transform: scaleX(-1);   /* mirror */
  filter: saturate(0.4) brightness(0.65);
  pointer-events: none;
  z-index: 0;
  transition: opacity 0.3s ease;
}
/* Show when cam view toggle is active */
#webcam.cam-active {
  opacity: 0.55;
  filter: saturate(0.6) brightness(0.75);
}

/* ── GAME CANVAS (middle layer) ──────────────────────────── */

#game-canvas {
  position: absolute;
  inset: 0;
  width: 100%; height: 100%;
  pointer-events: none;
  z-index: 1;
  will-change: transform;  /* own GPU layer — prevents stale composite after powerup */
  transform: translateZ(0);
}

/* ── HAND OVERLAY (top layer) ────────────────────────────── */

#hand-overlay {
  position: absolute;
  inset: 0;
  width: 100%; height: 100%;
  pointer-events: none;
  z-index: 2;
  will-change: transform;
  transform: translateZ(0);
}

/* ── SIDE PANEL ──────────────────────────────────────────── */

#side-panel {
  position: fixed;
  top: 0; right: 0;
  width: var(--panel-w);
  height: 100vh;
  background: var(--panel-bg);
  border-left: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  z-index: 10;
  backdrop-filter: blur(12px);
  overflow-y: auto;   /* scroll if content overflows */
  overflow-x: hidden;
}

.panel-header {
  padding: 14px 16px 10px;
  border-bottom: 1px solid var(--border);
}

.game-title {
  font-size: 13px;
  font-weight: 700;
  letter-spacing: 2px;
  text-transform: uppercase;
  color: var(--accent);
  text-shadow: 0 0 12px rgba(255,107,53,0.5);
}

.game-subtitle {
  font-size: 9px;
  color: var(--text-dim);
  letter-spacing: 1px;
  margin-top: 2px;
}

/* ── CASTLE HP BAR ───────────────────────────────────────── */

.hp-block {
  padding: 10px 16px;
  border-bottom: 1px solid var(--border);
}

.hp-label {
  font-size: 9px;
  text-transform: uppercase;
  letter-spacing: 1.5px;
  color: var(--text-dim);
  margin-bottom: 5px;
}

.hp-bar-track {
  height: 10px;
  background: rgba(255,255,255,0.06);
  border-radius: 5px;
  overflow: hidden;
  border: 1px solid rgba(255,255,255,0.1);
}

.hp-bar-fill {
  height: 100%;
  border-radius: 5px;
  background: linear-gradient(90deg, #ff3355, #ff6633);
  transition: width 0.3s ease;
  box-shadow: 0 0 8px rgba(255,51,85,0.6);
}

.hp-value {
  font-size: 11px;
  color: var(--red);
  text-align: right;
  margin-top: 3px;
}

/* ── SCORE / WAVE BLOCK ──────────────────────────────────── */

.stats-block {
  padding: 10px 16px;
  border-bottom: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.stat-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.stat-key {
  font-size: 9px;
  text-transform: uppercase;
  letter-spacing: 1px;
  color: var(--text-dim);
}

.stat-val {
  font-size: 13px;
  font-weight: 700;
  color: var(--gold);
  text-shadow: 0 0 8px rgba(255,215,0,0.4);
}

.stat-val.blue { color: var(--accent2); text-shadow: 0 0 8px rgba(0,212,255,0.4); }
.stat-val.green { color: var(--green); text-shadow: 0 0 8px rgba(0,229,137,0.4); }
.stat-val.red { color: var(--red); }

/* ── WAVE PROGRESS ───────────────────────────────────────── */

.wave-block {
  padding: 10px 16px;
  border-bottom: 1px solid var(--border);
}

.wave-label {
  font-size: 9px;
  text-transform: uppercase;
  letter-spacing: 1.5px;
  color: var(--text-dim);
  margin-bottom: 5px;
}

.wave-bar-track {
  height: 6px;
  background: rgba(255,255,255,0.06);
  border-radius: 3px;
  overflow: hidden;
}

.wave-bar-fill {
  height: 100%;
  border-radius: 3px;
  background: linear-gradient(90deg, var(--accent2), #aa88ff);
  transition: width 0.5s ease;
}

.wave-info {
  font-size: 10px;
  color: var(--text-dim);
  margin-top: 4px;
}

/* ── GESTURE STATE ───────────────────────────────────────── */

.gesture-block {
  padding: 10px 16px;
  border-bottom: 1px solid var(--border);
  flex: 0 0 auto;  /* don't grow — fixed height */
}

.gesture-title {
  font-size: 9px;
  text-transform: uppercase;
  letter-spacing: 1.5px;
  color: var(--text-dim);
  margin-bottom: 8px;
}

.hand-state {
  display: flex;
  flex-direction: column;
  gap: 5px;
  margin-bottom: 8px;
}

.hand-row {
  display: flex;
  align-items: center;
  gap: 6px;
}

.hand-tag {
  font-size: 8px;
  padding: 2px 5px;
  border-radius: 3px;
  font-weight: 700;
  letter-spacing: 1px;
  min-width: 18px;
  text-align: center;
}

.hand-tag.L { background: rgba(0,212,255,0.15); color: var(--accent2); border: 1px solid rgba(0,212,255,0.3); }
.hand-tag.R { background: rgba(255,107,53,0.15); color: var(--accent);  border: 1px solid rgba(255,107,53,0.3); }

.hand-action {
  font-size: 10px;
  color: var(--text);
}

.hand-action.active { color: var(--accent); font-weight: 700; }

/* ── KILL FEED ───────────────────────────────────────────── */

.killfeed-block {
  padding: 8px 16px;
  border-bottom: 1px solid var(--border);
  flex: 1 1 0;     /* take all remaining space */
  min-height: 60px;
  overflow: hidden;
}

.killfeed-title {
  font-size: 9px;
  text-transform: uppercase;
  letter-spacing: 1.5px;
  color: var(--text-dim);
  margin-bottom: 6px;
}

.killfeed-list {
  display: flex;
  flex-direction: column;
  gap: 3px;
  max-height: 100%;
  overflow: hidden;
}

.killfeed-entry {
  font-size: 9px;
  color: var(--text-dim);
  animation: kfIn 0.2s ease;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.killfeed-entry .kf-icon { margin-right: 4px; }
.killfeed-entry .kf-throw { color: #ff6b35; }
.killfeed-entry .kf-bolt  { color: #00d4ff; }
.killfeed-entry .kf-blow  { color: #aaffaa; }
.killfeed-entry .kf-slam  { color: #ffdd00; }

@keyframes kfIn {
  from { opacity: 0; transform: translateX(8px); }
  to   { opacity: 1; transform: translateX(0); }
}

/* ── CONTROLS HINT ───────────────────────────────────────── */

.hint-block {
  padding: 8px 16px 12px;
  border-top: 1px solid var(--border);
}

.hint-line {
  font-size: 8px;
  color: var(--text-dim);
  line-height: 1.6;
}

.hint-line span { color: var(--text); }

/* ── OVERLAY STATES ──────────────────────────────────────── */

#overlay-screen {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  z-index: 5;
  background: rgba(0,0,0,0.72);   /* dark enough to hide camera on menus */
  backdrop-filter: blur(3px);
  pointer-events: none;
}
#overlay-screen > * { pointer-events: all; }

/* During WAVE_CLEAR/PLAYING: lighter so you can still see the game behind */
#overlay-screen.playing-overlay {
  background: rgba(0,0,0,0.30);
  backdrop-filter: blur(1px);
}

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

.overlay-title {
  font-size: 42px;
  font-weight: 900;
  letter-spacing: 4px;
  text-transform: uppercase;
  color: var(--accent);
  text-shadow: 0 0 40px rgba(255,107,53,0.8), 0 4px 0 rgba(0,0,0,0.5);
  margin-bottom: 10px;
}

.overlay-sub {
  font-size: 14px;
  color: var(--text-dim);
  letter-spacing: 2px;
  margin-bottom: 30px;
}

.overlay-wave-banner {
  font-size: 28px;
  font-weight: 700;
  color: var(--gold);
  text-shadow: 0 0 20px rgba(255,215,0,0.6);
  margin-bottom: 8px;
}

.overlay-wave-sub {
  font-size: 12px;
  color: var(--text-dim);
  letter-spacing: 1px;
  margin-bottom: 24px;
}

.overlay-enemies {
  font-size: 12px;
  color: var(--text-dim);
  margin-bottom: 20px;
}

.overlay-enemies span { color: var(--red); font-weight: 700; }

.btn-start {
  padding: 14px 40px;
  font-size: 14px;
  font-family: inherit;
  font-weight: 700;
  letter-spacing: 3px;
  text-transform: uppercase;
  background: transparent;
  border: 2px solid var(--accent);
  color: var(--accent);
  border-radius: 6px;
  cursor: pointer;
  transition: all 0.2s;
  box-shadow: 0 0 20px rgba(255,107,53,0.45);
}

.btn-start:hover {
  background: rgba(255,107,53,0.15);
  box-shadow: 0 0 36px rgba(255,107,53,0.75);
  transform: scale(1.04);
}

/* Dwell loading animation — applied via JS when hand hovers */
.btn-dwell-active {
  position: relative;
  overflow: hidden;
}
.btn-dwell-active::after {
  content: '';
  position: absolute;
  left: 0; bottom: 0;
  height: 4px;
  width: var(--dwell-progress, 0%);
  background: linear-gradient(90deg, #00e589, #ffdd00);
  box-shadow: 0 0 8px #00e589;
  transition: width 0.05s linear;
  border-radius: 0 2px 2px 0;
}

/* Camera bounds overlay */
#cam-bounds-overlay {
  position: absolute;
  inset: 0;
  pointer-events: none;
  z-index: 3;
  display: none;
}
#cam-bounds-overlay.visible { display: block; }

/* Safe-zone border */
.cam-bounds-box {
  position: absolute;
  border: 2px dashed rgba(0,212,255,0.6);
  border-radius: 4px;
  box-shadow: inset 0 0 30px rgba(0,212,255,0.06), 0 0 0 1px rgba(0,212,255,0.15);
}

/* Danger edge strips — always present, just hidden until cam view on */
.cam-edge-zone {
  position: absolute;
  background: repeating-linear-gradient(
    45deg,
    rgba(255,60,40,0.13) 0px, rgba(255,60,40,0.13) 10px,
    rgba(255,60,40,0.04) 10px, rgba(255,60,40,0.04) 20px
  );
  border: 1px solid rgba(255,60,40,0.25);
  transition: background 0.15s, border-color 0.15s;
}
.cam-edge-zone.warn {
  background: repeating-linear-gradient(
    45deg,
    rgba(255,40,20,0.5) 0px, rgba(255,40,20,0.5) 10px,
    rgba(255,40,20,0.2) 10px, rgba(255,40,20,0.2) 20px
  );
  border-color: rgba(255,60,40,0.8);
}
.cam-corner-label {
  position: absolute;
  font-family: monospace;
  font-size: 8px;
  color: rgba(0,212,255,0.45);
  letter-spacing: 2px;
  text-transform: uppercase;
  pointer-events: none;
}

/* Camera toggle button */
#cam-toggle-btn {
  position: fixed;
  bottom: 14px; right: 228px;
  z-index: 30;
  background: rgba(0,0,0,0.6);
  border: 1px solid #333;
  color: #aaa;
  font-family: monospace;
  font-size: 9px;
  padding: 4px 8px;
  border-radius: 4px;
  cursor: pointer;
  letter-spacing: 1px;
}
#cam-toggle-btn:hover { border-color: #00d4ff; color: #00d4ff; }

/* ── WAVE MISSIONS (side panel) ─────────────────────────── */

.missions-block {
  padding: 8px 16px;
  border-bottom: 1px solid var(--border);
  flex: 0 0 auto;
}

.missions-title {
  font-size: 9px;
  text-transform: uppercase;
  letter-spacing: 1.5px;
  color: var(--text-dim);
  margin-bottom: 6px;
}

.mission-row {
  display: flex;
  align-items: center;
  gap: 5px;
  font-size: 8px;
  padding: 3px 4px;
  border-radius: 3px;
  color: #999;
  transition: color 0.4s, background 0.4s, box-shadow 0.4s;
}

.mission-row.done {
  color: var(--green);
  background: rgba(0,229,137,0.08);
  box-shadow: 0 0 6px rgba(0,229,137,0.2);
  animation: missionComplete 0.4s ease;
}

@keyframes missionComplete {
  0%   { background: rgba(0,229,137,0.35); box-shadow: 0 0 16px rgba(0,229,137,0.6); }
  100% { background: rgba(0,229,137,0.08); box-shadow: 0 0 6px  rgba(0,229,137,0.2); }
}

.mission-check {
  font-size: 9px;
  min-width: 12px;
}

/* ── OPTIONS DRAWER (in-game subtle panel) ───────────────── */

/* Drawer slides IN from the RIGHT EDGE of the game area (left of side panel) */
#options-drawer {
  position: fixed;
  top: 0;
  right: calc(var(--panel-w) + 280px); /* starts hidden off-canvas to the left of panel */
  height: 100vh;
  width: 280px;
  background: rgba(6,6,16,0.96);
  border: 1px solid rgba(255,107,53,0.2);
  backdrop-filter: blur(16px);
  z-index: 9;  /* below side panel z:10 */
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.18s ease, right 0.22s cubic-bezier(0.4,0,0.2,1);
  overflow-y: auto;
}

#options-drawer.open {
  right: var(--panel-w);  /* sits flush to left of side panel */
  opacity: 1;
  pointer-events: all;
}

#options-drawer .drawer-header {
  padding: 14px 16px 10px;
  border-bottom: 1px solid rgba(255,107,53,0.15);
  display: flex;
  justify-content: space-between;
  align-items: center;
}

#options-drawer .drawer-title {
  font-size: 11px;
  font-weight: 700;
  letter-spacing: 2px;
  color: var(--accent);
}

#options-drawer .drawer-close {
  background: transparent;
  border: 1px solid #333;
  color: #aaa;
  font-size: 12px;
  padding: 3px 8px;
  border-radius: 4px;
  cursor: pointer;
  font-family: monospace;
}

#options-drawer .drawer-close:hover { border-color: var(--accent); color: var(--accent); }

#options-drawer .drawer-body {
  padding: 12px 16px;
  display: flex;
  flex-direction: column;
  gap: 14px;
}

#options-drawer .drawer-section-title {
  font-size: 8px;
  letter-spacing: 2px;
  color: var(--accent);
  text-transform: uppercase;
  margin-bottom: 6px;
}

/* ── WAVE CLEAR RESULTS ──────────────────────────────────── */

.wave-results {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 6px;
  margin-bottom: 24px;
}

.result-row {
  display: flex;
  gap: 20px;
  font-size: 11px;
}

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

.result-num {
  font-size: 22px;
  font-weight: 700;
  color: var(--gold);
}

.result-label {
  font-size: 9px;
  color: var(--text-dim);
  letter-spacing: 1px;
}

/* ── DEATH SCREEN ────────────────────────────────────────── */

.death-title {
  color: var(--red);
  text-shadow: 0 0 40px rgba(255,51,85,0.8), 0 4px 0 rgba(0,0,0,0.5);
}

/* ── SCORE POP ANIMATIONS ───────────────────────────────── */

.score-pop {
  position: absolute;
  font-size: 14px;
  font-weight: 700;
  font-family: 'Courier New', monospace;
  pointer-events: none;
  z-index: 4;
  animation: scorePop 1.2s ease forwards;
  text-shadow: 0 0 8px currentColor;
}

@keyframes scorePop {
  0%   { opacity: 1; transform: translateY(0) scale(1); }
  20%  { opacity: 1; transform: translateY(-20px) scale(1.2); }
  100% { opacity: 0; transform: translateY(-70px) scale(0.8); }
}

/* ── FPS / STATUS ────────────────────────────────────────── */

#fps-badge {
  position: fixed;
  top: 8px; left: 8px;
  font-size: 9px;
  color: var(--text-dim);
  background: rgba(0,0,0,0.5);
  padding: 3px 7px;
  border-radius: 4px;
  z-index: 20;
  letter-spacing: 1px;
}

#tracking-status {
  position: fixed;
  top: 8px; left: 70px;
  font-size: 9px;
  color: var(--text-dim);
  background: rgba(0,0,0,0.5);
  padding: 3px 8px;
  border-radius: 4px;
  z-index: 20;
  letter-spacing: 1px;
  display: flex;
  align-items: center;
  gap: 5px;
}

.status-dot {
  width: 6px; height: 6px;
  border-radius: 50%;
  background: #ff3355;
}
.status-dot.active { background: #00e589; box-shadow: 0 0 5px #00e589; animation: pulse 2s infinite; }
.status-dot.loading { background: #ffdd00; animation: pulse 1s infinite; }

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

/* ── BALLISTA AIM INDICATOR HUD ──────────────────────────── */

.ballista-charging {
  position: absolute;
  top: 50%; left: 50%;
  transform: translate(-50%, -50%);
  pointer-events: none;
  z-index: 3;
}

/* ── RESPONSIVE ──────────────────────────────────────────── */

@media (max-width: 768px) {
  :root { --panel-w: 0px; }
  #side-panel { display: none; }
  #game-wrapper { width: 100vw; }
}
