:root {
  --bg: #05060b;
  --text-primary: rgba(244, 241, 235, 0.94);
  --text-secondary: rgba(226, 224, 220, 0.64);
  --text-muted: rgba(214, 214, 218, 0.42);
  --glass-bg: rgba(11, 13, 21, 0.56);
  --glass-strong: rgba(10, 12, 20, 0.78);
  --glass-border: rgba(255, 255, 255, 0.1);
  --glass-border-hover: rgba(255, 255, 255, 0.2);
  --active-bg: rgba(255, 244, 222, 0.14);
  --active-border: rgba(255, 232, 194, 0.34);
  --gold-soft: #f5d8a8;
  --serif: 'Cormorant Garamond', Georgia, 'Times New Roman', serif;
  --sans: 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
  --mono: 'JetBrains Mono', ui-monospace, SFMono-Regular, Menlo, monospace;
}

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

html,
body {
  margin: 0;
  padding: 0;
  width: 100%;
  height: 100%;
  overflow: hidden;
  background: var(--bg);
  color: var(--text-primary);
  font-family: var(--sans);
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  user-select: none;
}

#soul-canvas {
  position: fixed;
  inset: 0;
  width: 100%;
  height: 100%;
  display: block;
  touch-action: none;
  cursor: crosshair;
}

/* --- WebGL Fallback --- */
.fallback {
  position: fixed;
  inset: 0;
  display: grid;
  place-items: center;
  background: radial-gradient(circle at 50% 50%, #111626, #05060b);
  z-index: 40;
}

.fallback[hidden] {
  display: none;
}

.fallback-card {
  max-width: 420px;
  padding: 32px;
  text-align: center;
  border-radius: 20px;
  background: var(--glass-strong);
  border: 1px solid var(--glass-border);
}

.fallback-card h1 {
  margin: 0 0 10px;
  font-family: var(--serif);
  font-weight: 500;
  letter-spacing: 0.28em;
}

.fallback-card p {
  margin: 0;
  color: var(--text-secondary);
  line-height: 1.6;
  font-size: 14px;
}

/* --- Top Header --- */
.hud-top {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  padding: 24px 28px;
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 16px;
  pointer-events: none;
  z-index: 10;
  transition: opacity 0.45s ease, transform 0.45s ease;
}

.brand {
  display: flex;
  flex-direction: column;
  gap: 3px;
}

.brand-title {
  margin: 0;
  font-family: var(--serif);
  font-size: 22px;
  font-weight: 500;
  letter-spacing: 0.34em;
  color: rgba(248, 244, 236, 0.94);
  text-shadow: 0 2px 18px rgba(0, 0, 0, 0.55);
}

.brand-sub {
  margin: 0;
  font-family: var(--serif);
  font-style: italic;
  font-size: 14px;
  letter-spacing: 0.03em;
  color: var(--text-secondary);
  text-shadow: 0 2px 12px rgba(0, 0, 0, 0.6);
}

.top-actions {
  display: flex;
  align-items: center;
  gap: 8px;
  pointer-events: auto;
}

.pill-btn {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  height: 34px;
  padding: 0 14px;
  border-radius: 999px;
  border: 1px solid var(--glass-border);
  background: var(--glass-bg);
  backdrop-filter: blur(18px);
  -webkit-backdrop-filter: blur(18px);
  color: var(--text-secondary);
  font-family: var(--sans);
  font-size: 12px;
  font-weight: 400;
  letter-spacing: 0.03em;
  cursor: pointer;
  transition: background 0.25s ease, border-color 0.25s ease, color 0.25s ease, transform 0.2s ease;
}

.pill-btn:hover {
  color: var(--text-primary);
  border-color: var(--glass-border-hover);
  background: rgba(18, 22, 34, 0.68);
}

.pill-btn.is-active {
  color: var(--gold-soft);
  background: var(--active-bg);
  border-color: var(--active-border);
}

.pill-btn.icon-only {
  width: 34px;
  padding: 0;
  justify-content: center;
}

/* Animated Sound Bars */
.sound-bars {
  display: inline-flex;
  align-items: flex-end;
  gap: 2px;
  height: 11px;
}

.sound-bars i {
  display: block;
  width: 2px;
  height: 4px;
  border-radius: 2px;
  background: currentColor;
  opacity: 0.65;
  transition: height 0.25s ease;
}

.sound-bars i:nth-child(2) {
  height: 8px;
}

.sound-bars i:nth-child(3) {
  height: 5px;
}

.pill-btn.is-active .sound-bars i {
  opacity: 1;
  animation: waveBar 1.25s ease-in-out infinite;
}

.pill-btn.is-active .sound-bars i:nth-child(2) {
  animation-delay: 0.22s;
}

.pill-btn.is-active .sound-bars i:nth-child(3) {
  animation-delay: 0.44s;
}

@keyframes waveBar {
  0%,
  100% {
    height: 3px;
  }
  50% {
    height: 11px;
  }
}

/* --- Bottom-Left Poetic Field Note --- */
.field-caption {
  position: fixed;
  left: 28px;
  bottom: 92px;
  max-width: 440px;
  padding: 12px 18px 12px 0;
  pointer-events: none;
  z-index: 9;
  transition: opacity 0.4s ease, transform 0.4s ease;
}

.field-caption::before {
  content: '';
  position: absolute;
  inset: -28px -36px -36px -32px;
  background: radial-gradient(ellipse at 22% 72%, rgba(5, 6, 11, 0.72) 0%, rgba(5, 6, 11, 0.36) 54%, transparent 82%);
  pointer-events: none;
  z-index: -1;
}

.field-note {
  margin: 0 0 6px;
  font-family: var(--serif);
  font-style: italic;
  font-size: 19px;
  line-height: 1.38;
  font-weight: 400;
  color: rgba(248, 244, 238, 0.92);
  text-shadow: 0 2px 18px rgba(0, 0, 0, 0.85);
  transition: opacity 0.22s ease;
}

.field-note.is-swapping {
  opacity: 0;
}

.field-meta {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 7px;
  font-size: 11px;
  letter-spacing: 0.025em;
  color: rgba(226, 224, 228, 0.68);
  text-shadow: 0 1px 12px rgba(0, 0, 0, 0.92);
}

.gesture-hint {
  transition: opacity 0.8s ease;
}

.gesture-hint.is-subtle {
  opacity: 0.78;
}

.imprint-count {
  font-family: var(--mono);
  font-size: 10.5px;
  color: rgba(245, 216, 168, 0.82);
}

/* --- Bottom Floating Control Dock --- */
.dock {
  position: fixed;
  left: 50%;
  bottom: 24px;
  transform: translateX(-50%);
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 6px 8px;
  border-radius: 999px;
  border: 1px solid var(--glass-border);
  background: var(--glass-bg);
  backdrop-filter: blur(22px);
  -webkit-backdrop-filter: blur(22px);
  box-shadow: 0 18px 50px rgba(0, 0, 0, 0.48), inset 0 1px 0 rgba(255, 255, 255, 0.06);
  z-index: 12;
  max-width: calc(100vw - 24px);
  transition: opacity 0.45s ease, transform 0.45s ease;
}

.dock-group {
  display: flex;
  align-items: center;
  gap: 3px;
}

.dock-sep {
  width: 1px;
  height: 18px;
  margin: 0 4px;
  background: rgba(255, 255, 255, 0.11);
  flex-shrink: 0;
}

.dock-btn {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  height: 32px;
  padding: 0 12px;
  border-radius: 999px;
  border: 1px solid transparent;
  background: transparent;
  color: var(--text-secondary);
  font-family: var(--sans);
  font-size: 12px;
  font-weight: 400;
  letter-spacing: 0.02em;
  white-space: nowrap;
  cursor: pointer;
  transition: background 0.2s ease, color 0.2s ease, border-color 0.2s ease;
}

.dock-btn:hover {
  color: var(--text-primary);
  background: rgba(255, 255, 255, 0.06);
}

.dock-btn.is-active {
  color: rgba(255, 248, 236, 0.98);
  background: var(--active-bg);
  border-color: var(--active-border);
}

.dock-btn.accent-btn.is-active {
  color: var(--gold-soft);
  background: rgba(245, 216, 168, 0.16);
  border-color: rgba(245, 216, 168, 0.42);
}

/* --- Traits Popover (right-anchored above Traits button to avoid caption overlap) --- */
.traits-panel {
  position: fixed;
  right: max(20px, calc(50% - 375px));
  bottom: 80px;
  transform: translateY(10px);
  width: min(336px, calc(100vw - 32px));
  padding: 18px 20px 16px;
  border-radius: 20px;
  border: 1px solid var(--glass-border);
  background: var(--glass-strong);
  backdrop-filter: blur(26px);
  -webkit-backdrop-filter: blur(26px);
  box-shadow: 0 24px 64px rgba(0, 0, 0, 0.65);
  z-index: 15;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.25s ease, transform 0.25s ease;
}

.traits-panel.is-open {
  opacity: 1;
  pointer-events: auto;
  transform: translateY(0);
}

.traits-header {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  margin-bottom: 14px;
}

.traits-title {
  font-family: var(--serif);
  font-size: 17px;
  letter-spacing: 0.04em;
  color: var(--text-primary);
}

.traits-hint {
  font-size: 11px;
  color: var(--text-muted);
}

.trait-row {
  display: flex;
  flex-direction: column;
  gap: 5px;
  margin-bottom: 13px;
}

.trait-label-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-size: 12px;
  color: var(--text-primary);
}

.trait-val {
  font-family: var(--mono);
  font-size: 11px;
  color: var(--gold-soft);
}

.trait-desc {
  font-size: 11px;
  color: var(--text-muted);
}

.traits-panel input[type='range'] {
  -webkit-appearance: none;
  appearance: none;
  width: 100%;
  height: 3px;
  border-radius: 999px;
  background: rgba(255, 255, 255, 0.14);
  outline: none;
  cursor: pointer;
}

.traits-panel input[type='range']::-webkit-slider-thumb {
  -webkit-appearance: none;
  appearance: none;
  width: 13px;
  height: 13px;
  border-radius: 50%;
  background: #f8ebcf;
  border: 1px solid rgba(255, 255, 255, 0.5);
  box-shadow: 0 0 10px rgba(245, 216, 168, 0.45);
  transition: transform 0.15s ease;
}

.traits-panel input[type='range']::-webkit-slider-thumb:hover {
  transform: scale(1.16);
}

.traits-panel input[type='range']::-moz-range-thumb {
  width: 13px;
  height: 13px;
  border-radius: 50%;
  background: #f8ebcf;
  border: 1px solid rgba(255, 255, 255, 0.5);
}

.traits-footer {
  display: flex;
  gap: 8px;
  margin-top: 14px;
  padding-top: 12px;
  border-top: 1px solid rgba(255, 255, 255, 0.08);
}

.mini-action {
  flex: 1;
  height: 32px;
  border-radius: 10px;
  border: 1px solid var(--glass-border);
  background: rgba(255, 255, 255, 0.04);
  color: var(--text-primary);
  font-family: var(--sans);
  font-size: 12px;
  cursor: pointer;
  transition: background 0.2s ease, border-color 0.2s ease, opacity 0.2s ease;
}

.mini-action:hover:not(:disabled) {
  background: rgba(255, 255, 255, 0.1);
  border-color: var(--glass-border-hover);
}

.mini-action:disabled {
  opacity: 0.35;
  cursor: default;
}

/* --- Essay / Vision Drawer --- */
.drawer-scrim {
  position: fixed;
  inset: 0;
  background: rgba(4, 5, 10, 0.42);
  backdrop-filter: blur(2px);
  -webkit-backdrop-filter: blur(2px);
  z-index: 18;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.3s ease;
}

body.drawer-open .drawer-scrim {
  opacity: 1;
  pointer-events: auto;
}

body.drawer-open .dock,
body.drawer-open .field-caption {
  opacity: 0.2;
  pointer-events: none;
}

body.panel-open .field-caption {
  opacity: 0.28;
}

.vision-drawer {
  position: fixed;
  top: 20px;
  right: 20px;
  bottom: 20px;
  width: min(410px, calc(100vw - 32px));
  border-radius: 22px;
  border: 1px solid var(--glass-border);
  background: rgba(9, 11, 18, 0.88);
  backdrop-filter: blur(28px);
  -webkit-backdrop-filter: blur(28px);
  box-shadow: 0 28px 80px rgba(0, 0, 0, 0.68);
  z-index: 20;
  transform: translateX(calc(100% + 36px));
  opacity: 0;
  pointer-events: none;
  transition: transform 0.38s cubic-bezier(0.22, 1, 0.36, 1), opacity 0.3s ease;
  overflow: hidden;
}

.vision-drawer.is-open {
  transform: translateX(0);
  opacity: 1;
  pointer-events: auto;
}

.vision-inner {
  height: 100%;
  overflow-y: auto;
  padding: 26px 28px 28px;
  user-select: text;
}

.vision-top {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 14px;
}

.vision-kicker {
  font-family: var(--mono);
  font-size: 10.5px;
  letter-spacing: 0.14em;
  text-transform: uppercase;
  color: var(--gold-soft);
}

.close-btn {
  width: 30px;
  height: 30px;
  border-radius: 999px;
  border: 1px solid var(--glass-border);
  background: rgba(255, 255, 255, 0.04);
  color: var(--text-secondary);
  font-size: 18px;
  line-height: 1;
  cursor: pointer;
  display: grid;
  place-items: center;
  transition: color 0.2s ease, background 0.2s ease;
}

.close-btn:hover {
  color: var(--text-primary);
  background: rgba(255, 255, 255, 0.1);
}

.vision-heading {
  margin: 0 0 16px;
  font-family: var(--serif);
  font-size: 27px;
  font-weight: 500;
  line-height: 1.18;
  color: rgba(250, 246, 238, 0.96);
}

.vision-inner p {
  margin: 0 0 14px;
  font-size: 13.5px;
  line-height: 1.7;
  color: rgba(232, 230, 226, 0.78);
}

.vision-inner strong {
  color: rgba(250, 245, 236, 0.94);
  font-weight: 500;
}

.vision-list {
  margin: 16px 0 20px;
  padding: 0 0 0 16px;
  display: flex;
  flex-direction: column;
  gap: 12px;
  font-size: 13px;
  line-height: 1.65;
  color: rgba(226, 224, 220, 0.74);
}

.vision-list li::marker {
  color: rgba(245, 216, 168, 0.55);
}

.vision-keys {
  display: flex;
  flex-wrap: wrap;
  gap: 10px 14px;
  padding-top: 16px;
  border-top: 1px solid rgba(255, 255, 255, 0.08);
  font-size: 11px;
  color: var(--text-muted);
}

.vision-keys kbd {
  display: inline-block;
  padding: 1px 5px;
  margin-right: 3px;
  border-radius: 4px;
  border: 1px solid rgba(255, 255, 255, 0.16);
  background: rgba(255, 255, 255, 0.06);
  font-family: var(--mono);
  font-size: 10px;
  color: var(--text-secondary);
}

/* --- Hide UI Mode --- */
body.ui-hidden .hud-top,
body.ui-hidden .field-caption,
body.ui-hidden .dock {
  opacity: 0;
  pointer-events: none;
  transform: translateY(8px);
}

body.ui-hidden .dock {
  transform: translateX(-50%) translateY(16px);
}

.restore-ui {
  position: fixed;
  right: 24px;
  bottom: 24px;
  height: 32px;
  padding: 0 14px;
  border-radius: 999px;
  border: 1px solid var(--glass-border);
  background: var(--glass-bg);
  backdrop-filter: blur(16px);
  -webkit-backdrop-filter: blur(16px);
  color: var(--text-secondary);
  font-size: 11.5px;
  cursor: pointer;
  z-index: 16;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.35s ease, color 0.2s ease, border-color 0.2s ease;
}

body.ui-hidden .restore-ui {
  opacity: 0.72;
  pointer-events: auto;
}

.restore-ui:hover {
  opacity: 1;
  color: var(--text-primary);
  border-color: var(--glass-border-hover);
}

/* --- Responsive Adjustments --- */
@media (max-width: 960px) {
  .dock {
    flex-wrap: wrap;
    justify-content: center;
    border-radius: 22px;
    padding: 8px 10px;
    bottom: 14px;
  }

  .dock-sep {
    display: none;
  }

  .field-caption {
    left: 20px;
    right: 20px;
    bottom: 122px;
    max-width: none;
  }

  .field-note {
    font-size: 16px;
  }

  .traits-panel {
    right: 16px;
    bottom: 148px;
  }

  body.panel-open .field-caption {
    opacity: 0;
  }
}

@media (max-width: 560px) {
  .hud-top {
    padding: 16px 16px;
  }

  .brand-title {
    font-size: 18px;
  }

  .brand-sub {
    font-size: 12px;
  }

  .pill-btn {
    padding: 0 10px;
    font-size: 11px;
  }

  .dock-btn {
    padding: 0 9px;
    font-size: 11px;
    height: 29px;
  }

  .field-caption {
    bottom: 138px;
  }

  .gesture-hint {
    display: none;
  }
}
