:root {
  color-scheme: dark;

  --bg: #000;
  --fg: #f6f6f6;
  --muted: rgba(255, 255, 255, 0.72);
  --soft: rgba(255, 255, 255, 0.42);
  --faint: rgba(255, 255, 255, 0.16);
  --edge: rgba(255, 255, 255, 0.8);
  --panel: rgba(0, 0, 0, 0.84);
  --panel-soft: rgba(0, 0, 0, 0.58);

  --mono: Monaco, "Lucida Console", "Courier New", monospace;

  --ui-font: clamp(10px, 0.72vw, 13px);
  --label-font: clamp(10px, 0.78vw, 13px);
  --message-font: clamp(10px, 1vw, 13px);
  --message-line: 1.55;

  --button-height: clamp(24px, 2.3vw, 28px);
  --hud-width: min(350px, calc(100vw - 28px));
}

* {
  box-sizing: border-box;
}

html,
body {
  width: 100%;
  height: 100%;
  margin: 0;
  overflow: hidden;
  background: #000;
  color: var(--fg);
}

body {
  font-family: var(--mono);
  -webkit-font-smoothing: none;
  font-smooth: never;
  text-rendering: geometricPrecision;
}

.shell {
  position: relative;
  width: 100vw;
  height: 100vh;
  min-height: 100svh;
  overflow: hidden;
  background:
    radial-gradient(circle at 52% 42%, rgba(255,255,255,0.06), transparent 34rem),
    radial-gradient(circle at 30% 76%, rgba(255,255,255,0.03), transparent 26rem),
    #000;
}

#scene {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  display: block;
  cursor: grab;
  touch-action: none;
}

#scene:active {
  cursor: grabbing;
}

/* ----------------------------- */
/* FLOATING LABELS               */
/* ----------------------------- */

.labels {
  position: absolute;
  inset: 0;
  z-index: 3;
  pointer-events: none;
}

.tag {
  position: absolute;
  left: 0;
  top: 0;
  pointer-events: auto;
  --message-width: 38ch;
}

.tagButton,
.hudToggle,
.hudPanel button {
  appearance: none;
  border-radius: 0;
  border: 1px solid rgba(255,255,255,0.84);
  background: rgba(0,0,0,0.88);
  color: #fff;
  font-family: var(--mono);
  font-size: var(--label-font);
  line-height: 1;
  text-transform: lowercase;
  letter-spacing: 0.02em;
  padding: 0 8px;
  cursor: pointer;
  box-shadow: 1px 1px 0 rgba(255,255,255,0.12);

  height: var(--button-height);

  -webkit-font-smoothing: none;
  font-smooth: never;
  text-rendering: geometricPrecision;
  image-rendering: pixelated;

  text-shadow:
    1px 0 0 rgba(255,255,255,0.28),
    0 1px 0 rgba(255,255,255,0.12);
}

.tagButton {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  overflow: hidden;
  min-width: clamp(46px, 4.5vw, 54px);
}

#tag-body .tagButton {
  width: clamp(46px, 4.5vw, 54px);
}

#tag-soul .tagButton {
  width: clamp(46px, 4.5vw, 54px);
}

#tag-spirit .tagButton {
  width: clamp(60px, 5.6vw, 68px);
}

.tagButton:hover,
.tagButton:focus-visible,
.hudToggle:hover,
.hudToggle:focus-visible,
.hudPanel button:hover,
.hudPanel button:focus-visible,
.hudPanel button[data-active="true"] {
  background: #fff;
  color: #000;
  outline: none;
}

/* Attached poetry / prose text */
.tagMessage {
  display: none;
  position: absolute;
  left: 0;
  top: calc(100% + 8px);

  width: min(var(--message-width), 48vw);
  max-width: calc(100vw - 32px);

  color: rgba(255,255,255,0.88);
  font-family: var(--mono);
  font-size: var(--message-font);
  line-height: var(--message-line);
  white-space: pre-wrap;
  overflow-wrap: break-word;
  word-break: normal;
  letter-spacing: 0.01em;

  -webkit-font-smoothing: none;
  font-smooth: never;
  text-rendering: geometricPrecision;
  image-rendering: pixelated;

  text-shadow:
    1px 0 0 rgba(255,255,255,0.18),
    0 1px 0 rgba(255,255,255,0.08);
}

.tag[data-open="true"] .tagMessage {
  display: block;
}

/* Wider desktop line lengths */
#tag-body {
  --message-width: 44ch;
}

#tag-soul {
  --message-width: 42ch;
}

#tag-spirit {
  --message-width: 38ch;
}

/* ----------------------------- */
/* HUD TOGGLE                    */
/* ----------------------------- */

.hudToggle {
  position: absolute;
  left: max(14px, env(safe-area-inset-left));
  bottom: max(14px, env(safe-area-inset-bottom));
  z-index: 5;

  opacity: 0.25;
  backdrop-filter: blur(8px);
}

.hudToggle:hover,
.hudToggle:focus-visible {
  opacity: 0.72;
}

/* ----------------------------- */
/* HUD PANEL                     */
/* ----------------------------- */

.hudPanel {
  position: absolute;
  left: max(14px, env(safe-area-inset-left));
  bottom: calc(max(14px, env(safe-area-inset-bottom)) + 42px);
  z-index: 5;

  width: var(--hud-width);
  max-height: calc(100svh - 78px);
  overflow: auto;

  border: 1px solid rgba(255,255,255,0.24);
  background: linear-gradient(180deg, var(--panel), var(--panel-soft));
  backdrop-filter: blur(14px);
  padding: clamp(10px, 1.2vw, 13px);
  box-shadow: 0 20px 60px rgba(0,0,0,0.55);

  scrollbar-width: thin;
  scrollbar-color: rgba(255,255,255,0.28) transparent;
}

.hudHeader {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 10px;
}

.hudHeader p,
.sectionTitle,
.panelLabel,
.checkboxLine,
.panelNote,
.readout,
.hudPanel output {
  font-family: var(--mono);
  -webkit-font-smoothing: none;
  font-smooth: never;
  text-rendering: geometricPrecision;
  text-shadow: 1px 0 0 rgba(255,255,255,0.14);
}

.hudHeader p {
  margin: 0;
  color: var(--muted);
  font-size: clamp(9px, 0.72vw, 11px);
  letter-spacing: 0.08em;
  text-transform: uppercase;
  line-height: 1.35;
}

.hudClose {
  min-width: 34px;
  padding-inline: 0;
}

.hudGrid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px 14px;
  margin-bottom: 12px;
}

.readout {
  display: flex;
  justify-content: space-between;
  gap: 12px;
  border-bottom: 1px solid var(--faint);
  padding-bottom: 6px;
  color: var(--soft);
  font-size: var(--ui-font);
  min-width: 0;
}

.readout span,
.readout output {
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
}

.readout output {
  color: #fff;
  flex-shrink: 0;
}

.sectionTitle {
  margin: 10px 0 8px;
  color: var(--muted);
  font-size: clamp(9px, 0.72vw, 11px);
  text-transform: uppercase;
  letter-spacing: 0.08em;
}

.panelLabel,
.checkboxLine,
.panelNote {
  font-size: var(--ui-font);
  color: var(--muted);
}

.panelLabel {
  display: block;
  margin-bottom: 6px;
}

input[type="range"] {
  width: 100%;
  margin: 0 0 12px;
  accent-color: #fff;
}

.controlsRow {
  display: flex;
  gap: 8px;
  margin-bottom: 8px;
}

.controlsRow button {
  flex: 1;
  min-width: 0;
  height: var(--button-height);
  font-size: var(--ui-font);
}

.controlsRow-tight button {
  min-width: 0;
}

.controlsRow-stack {
  flex-wrap: wrap;
}

.controlsRow-stack button {
  min-width: calc(50% - 4px);
}

.checkboxLine {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-top: 8px;
  line-height: 1.35;
}

.checkboxLine input {
  flex: 0 0 auto;
  accent-color: #fff;
}

.panelNote {
  margin: 10px 0 0;
  opacity: 0.74;
  line-height: 1.45;
}

/* ----------------------------- */
/* SOURCE GHOST                  */
/* ----------------------------- */

#reference {
  position: absolute;
  right: max(16px, env(safe-area-inset-right));
  bottom: max(16px, env(safe-area-inset-bottom));
  z-index: 2;

  width: min(230px, 34vw);
  opacity: 0;
  transform: translateY(10px);
  transition: opacity 180ms ease, transform 180ms ease;

  filter: invert(1) grayscale(1) contrast(1.12);
  pointer-events: none;
  border: 1px solid rgba(255,255,255,0.2);
  background: #000;
}

#reference.visible {
  opacity: 0.64;
  transform: translateY(0);
}

/* ----------------------------- */
/* TABLET / SMALL DESKTOP        */
/* ----------------------------- */

@media (max-width: 900px) {
  :root {
    --message-font: clamp(9.5px, 1.35vw, 12px);
    --ui-font: clamp(9.5px, 1.25vw, 12px);
    --label-font: clamp(10px, 1.4vw, 12px);
  }

  .tagMessage {
    width: min(var(--message-width), 58vw);
    max-width: calc(100vw - 28px);
  }

  #tag-body {
    --message-width: 38ch;
  }

  #tag-soul {
    --message-width: 36ch;
  }

  #tag-spirit {
    --message-width: 34ch;
  }
}

/* ----------------------------- */
/* MOBILE / 4:3 IFRAME-FRIENDLY  */
/* ----------------------------- */

@media (max-width: 700px) {
  :root {
    --hud-width: min(310px, calc(100vw - 24px));
    --ui-font: clamp(9px, 2.8vw, 11.5px);
    --label-font: clamp(9.5px, 3vw, 12px);
    --message-font: clamp(9px, 2.7vw, 11px);
    --button-height: clamp(23px, 6vw, 26px);
  }

  .shell {
    background:
      radial-gradient(circle at 52% 44%, rgba(255,255,255,0.06), transparent 24rem),
      radial-gradient(circle at 30% 76%, rgba(255,255,255,0.03), transparent 20rem),
      #000;
  }

  .hudPanel {
    left: max(12px, env(safe-area-inset-left));
    bottom: calc(max(12px, env(safe-area-inset-bottom)) + 40px);
    width: var(--hud-width);
    max-height: calc(100svh - 68px);
    padding: 10px;
  }

  .hudToggle {
    left: max(12px, env(safe-area-inset-left));
    bottom: max(12px, env(safe-area-inset-bottom));
  }

  .hudGrid {
    grid-template-columns: 1fr 1fr;
    gap: 7px 10px;
  }

  .controlsRow {
    gap: 7px;
  }

  .controlsRow-stack button {
    min-width: calc(50% - 4px);
  }

  .tagButton {
    min-width: 44px;
  }

  #tag-body .tagButton {
    width: 44px;
  }

  #tag-soul .tagButton {
    width: 44px;
  }

  #tag-spirit .tagButton {
    width: 58px;
  }

  .tagMessage {
    width: min(var(--message-width), 70vw);
    max-width: calc(100vw - 24px);
    line-height: 1.5;
  }

  #tag-body {
    --message-width: 32ch;
  }

  #tag-soul {
    --message-width: 31ch;
  }

  #tag-spirit {
    --message-width: 30ch;
  }

  #reference {
    width: min(180px, 42vw);
  }
}

/* Very narrow embeds / phones */
@media (max-width: 430px) {
  :root {
    --ui-font: clamp(8.5px, 3vw, 10.5px);
    --label-font: clamp(9px, 3.2vw, 11px);
    --message-font: clamp(8.5px, 3vw, 10.5px);
  }

  .tagMessage {
    width: min(var(--message-width), 76vw);
    max-width: calc(100vw - 20px);
  }

  #tag-body {
    --message-width: 28ch;
  }

  #tag-soul {
    --message-width: 28ch;
  }

  #tag-spirit {
    --message-width: 27ch;
  }

  .hudPanel {
    width: calc(100vw - 20px);
    left: 10px;
  }

  .hudToggle {
    left: 10px;
  }

  .hudHeader p {
    max-width: 22ch;
  }
}

/* Specifically for 4:3-ish iframes and squarer screens */
@media (max-aspect-ratio: 4/3) {
  .shell {
    background:
      radial-gradient(circle at 52% 44%, rgba(255,255,255,0.06), transparent 24rem),
      radial-gradient(circle at 30% 76%, rgba(255,255,255,0.03), transparent 20rem),
      #000;
  }

  .tagMessage {
    width: min(var(--message-width), 64vw);
  }

  #reference {
    display: none;
  }
}

/* Short viewport protection */
@media (max-height: 560px) {
  .hudPanel {
    max-height: calc(100svh - 56px);
    bottom: 46px;
  }

  .panelNote {
    display: none;
  }

  .hudGrid {
    margin-bottom: 8px;
  }

  input[type="range"] {
    margin-bottom: 8px;
  }
}

/* Extra-small mobile / tiny iframe protection */
@media (max-width: 380px), (max-height: 520px) {
  :root {
    --ui-font: clamp(7.5px, 2.45vw, 9.5px);
    --label-font: clamp(8px, 2.65vw, 10px);
    --message-font: clamp(7.5px, 2.55vw, 9.5px);
    --button-height: clamp(20px, 5.4vw, 23px);
  }

  .tagButton,
  .hudToggle,
  .hudPanel button {
    padding-inline: 6px;
  }

  #tag-body .tagButton,
  #tag-soul .tagButton {
    width: clamp(38px, 11vw, 44px);
    min-width: clamp(38px, 11vw, 44px);
  }

  #tag-spirit .tagButton {
    width: clamp(50px, 14vw, 58px);
    min-width: clamp(50px, 14vw, 58px);
  }

  .tagMessage {
    width: min(var(--message-width), 80vw);
    max-width: calc(100vw - 18px);
    line-height: 1.42;
  }

  #tag-body {
    --message-width: 24ch;
  }

  #tag-soul {
    --message-width: 24ch;
  }

  #tag-spirit {
    --message-width: 23ch;
  }

  .hudPanel {
    width: calc(100vw - 18px);
    left: 9px;
    bottom: 42px;
    padding: 8px;
    max-height: calc(100svh - 52px);
  }

  .hudHeader p {
    font-size: 8px;
    max-width: 24ch;
  }

  .hudGrid {
    gap: 6px 8px;
    margin-bottom: 8px;
  }

  .controlsRow {
    gap: 6px;
    margin-bottom: 6px;
  }

  .sectionTitle {
    font-size: 8px;
    margin: 8px 0 6px;
  }

  .panelNote {
    display: none;
  }
}

@media (max-width: 320px), (max-height: 440px) {
  :root {
    --ui-font: 7px;
    --label-font: 7.5px;
    --message-font: 7px;
    --button-height: 19px;
  }

  .tagMessage {
    width: min(var(--message-width), 84vw);
    line-height: 1.35;
  }

  #tag-body,
  #tag-soul,
  #tag-spirit {
    --message-width: 21ch;
  }

  .hudPanel {
    padding: 7px;
    bottom: 38px;
  }

  .hudToggle {
    opacity: 0.04;
  }
}