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

html, body {
  width: 100%;
  height: 100%;
  overflow: hidden;
  background: #18132c;
  font-family: 'Trebuchet MS', 'Segoe UI', system-ui, -apple-system, sans-serif;
  color: #fff6e8;
  user-select: none;
  -webkit-user-select: none;
}

#canvas-container {
  position: fixed;
  inset: 0;
  width: 100%;
  height: 100%;
}

#canvas-container canvas {
  display: block;
  width: 100%;
  height: 100%;
}

/* Overlay root */
#ui-layer {
  position: fixed;
  inset: 0;
  pointer-events: none;
  z-index: 10;
}

/* Top bar */
#top-bar {
  position: absolute;
  top: 16px;
  left: 18px;
  right: 18px;
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 12px;
}

#title-card {
  background: rgba(29, 20, 44, 0.82);
  border: 2px solid rgba(244, 195, 118, 0.55);
  border-radius: 14px;
  padding: 9px 15px;
  box-shadow: 0 4px 0 rgba(16, 10, 26, 0.85), 0 10px 24px rgba(0, 0, 0, 0.35);
  backdrop-filter: blur(6px);
  -webkit-backdrop-filter: blur(6px);
}

#title-card h1 {
  font-family: Georgia, 'Palatino Linotype', 'Book Antiqua', serif;
  font-size: 1.08rem;
  font-weight: 700;
  letter-spacing: 0.02em;
  color: #ffe3a8;
  text-shadow: 0 2px 0 #1a1026;
}

#title-card p {
  font-size: 0.76rem;
  color: #d6c5e8;
  margin-top: 2px;
}

/* Controls */
#controls-bar {
  display: flex;
  align-items: center;
  gap: 7px;
  pointer-events: auto;
  flex-wrap: wrap;
  justify-content: flex-end;
}

.ui-btn {
  appearance: none;
  background: rgba(34, 24, 52, 0.86);
  color: #ffebc2;
  border: 2px solid rgba(244, 195, 118, 0.5);
  border-radius: 999px;
  padding: 6px 12px;
  font-size: 0.78rem;
  font-weight: 700;
  letter-spacing: 0.01em;
  cursor: pointer;
  box-shadow: 0 3px 0 rgba(16, 10, 26, 0.9);
  transition: transform 0.12s ease, background 0.15s ease, border-color 0.15s ease;
  backdrop-filter: blur(6px);
  -webkit-backdrop-filter: blur(6px);
}

.ui-btn:hover {
  background: rgba(58, 40, 86, 0.95);
  border-color: #ffd27d;
  transform: translateY(-1px);
}

.ui-btn:active {
  transform: translateY(2px);
  box-shadow: 0 1px 0 rgba(16, 10, 26, 0.9);
}

.ui-btn.is-active {
  background: rgba(96, 56, 28, 0.92);
  border-color: #ffc266;
  color: #fff5df;
}

/* 3D Tracked Speech Bubble Layer */
#bubble-layer {
  position: absolute;
  inset: 0;
  pointer-events: none;
  overflow: hidden;
}

.speech-bubble {
  position: absolute;
  left: 0;
  top: 0;
  max-width: 270px;
  min-width: 135px;
  background: #fff8e8;
  color: #261816;
  border: 2.5px solid #2b1a17;
  border-radius: 16px;
  padding: 10px 13px 10px;
  box-shadow: 0 4px 0 #2b1a17, 0 10px 22px rgba(12, 6, 24, 0.48);
  transform: translate(-50%, -100%) scale(1);
  transform-origin: 50% 100%;
  transition: opacity 0.2s ease;
  pointer-events: none;
  z-index: 20;
}

.speech-bubble.pop-in {
  animation: bubblePop 0.34s cubic-bezier(0.34, 1.56, 0.64, 1);
}

.speech-bubble.quip-bubble {
  background: #fffdf4;
  z-index: 25;
}

@keyframes bubblePop {
  0% {
    opacity: 0;
    transform: translate(-50%, -88%) scale(0.68);
  }
  100% {
    opacity: 1;
    transform: translate(-50%, -100%) scale(1);
  }
}

/* Bubble pointer tail */
.speech-bubble::after {
  content: '';
  position: absolute;
  left: 50%;
  bottom: -11px;
  transform: translateX(-50%);
  width: 0;
  height: 0;
  border-left: 9px solid transparent;
  border-right: 9px solid transparent;
  border-top: 11px solid #2b1a17;
}

.speech-bubble::before {
  content: '';
  position: absolute;
  left: 50%;
  bottom: -7px;
  transform: translateX(-50%);
  width: 0;
  height: 0;
  border-left: 7px solid transparent;
  border-right: 7px solid transparent;
  border-top: 9px solid #fff8e8;
  z-index: 1;
}

.bubble-speaker {
  display: inline-block;
  font-size: 0.68rem;
  font-weight: 800;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  padding: 2px 7px;
  border-radius: 999px;
  margin-bottom: 5px;
  color: #ffffff;
  background: #2b6cb0;
  box-shadow: 0 1px 0 rgba(0, 0, 0, 0.25);
}

.bubble-text {
  font-size: 0.87rem;
  line-height: 1.34;
  font-weight: 600;
  color: #281915;
}

/* Hover tooltip label */
#hover-tag {
  position: absolute;
  left: 0;
  top: 0;
  transform: translate(-50%, -135%);
  background: rgba(26, 17, 40, 0.9);
  color: #ffe6b0;
  border: 1.5px solid rgba(255, 210, 125, 0.65);
  border-radius: 999px;
  padding: 3px 10px;
  font-size: 0.72rem;
  font-weight: 700;
  white-space: nowrap;
  pointer-events: none;
  opacity: 0;
  transition: opacity 0.14s ease;
  z-index: 30;
  box-shadow: 0 3px 8px rgba(0, 0, 0, 0.4);
}

#hover-tag.visible {
  opacity: 1;
}

/* Bottom narrator ribbon */
#narrator-wrap {
  position: absolute;
  bottom: 16px;
  left: 50%;
  transform: translateX(-50%);
  width: min(680px, calc(100vw - 28px));
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 7px;
}

#narrator-box {
  width: 100%;
  background: linear-gradient(180deg, rgba(35, 24, 54, 0.90) 0%, rgba(24, 16, 39, 0.94) 100%);
  border: 2px solid rgba(243, 193, 114, 0.62);
  border-radius: 16px;
  padding: 11px 18px 10px;
  box-shadow: 0 5px 0 rgba(12, 7, 22, 0.9), 0 12px 28px rgba(0, 0, 0, 0.48);
  text-align: center;
  pointer-events: auto;
  backdrop-filter: blur(8px);
  -webkit-backdrop-filter: blur(8px);
}

#narrator-text {
  font-family: Georgia, 'Palatino Linotype', 'Book Antiqua', serif;
  font-style: italic;
  font-size: 0.95rem;
  line-height: 1.4;
  color: #fff1d0;
  text-shadow: 0 1px 2px rgba(0, 0, 0, 0.65);
}

#story-footer {
  margin-top: 7px;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 12px;
  flex-wrap: wrap;
}

#beat-dots {
  display: flex;
  align-items: center;
  gap: 6px;
}

.beat-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: rgba(255, 226, 173, 0.26);
  border: 1px solid rgba(255, 214, 140, 0.45);
  cursor: pointer;
  transition: transform 0.18s ease, background 0.18s ease;
}

.beat-dot:hover {
  transform: scale(1.25);
  background: rgba(255, 226, 173, 0.6);
}

.beat-dot.active {
  background: #ffc45c;
  border-color: #fff0cc;
  transform: scale(1.28);
  box-shadow: 0 0 8px rgba(255, 190, 82, 0.75);
}

#hint-line {
  font-size: 0.72rem;
  color: #cbb8df;
  letter-spacing: 0.01em;
}

/* Loading / fallback notice */
#loading-msg {
  position: fixed;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  background: #18132c;
  color: #ffe5ad;
  font-family: Georgia, serif;
  font-size: 1.1rem;
  z-index: 50;
  transition: opacity 0.45s ease;
}

#loading-msg.hidden {
  opacity: 0;
  pointer-events: none;
}

@media (max-width: 640px) {
  #top-bar {
    top: 10px;
    left: 10px;
    right: 10px;
    flex-direction: column;
    align-items: stretch;
  }
  #controls-bar {
    justify-content: flex-start;
  }
  .speech-bubble {
    max-width: 215px;
    padding: 8px 10px;
  }
  .bubble-text {
    font-size: 0.8rem;
  }
  #narrator-text {
    font-size: 0.86rem;
  }
}
