.dungeon-toggle:not([hidden]) {
  display: inline-flex;
  align-items: center;
  gap: 7px;
  min-inline-size: calc(12ch + 34px);
  min-height: 38px;
  padding: 3px 6px;
  border: 1px solid var(--frame);
  background: var(--surface);
  box-shadow: 2px 2px 0 var(--frame);
  color: var(--gold);
  font: inherit;
  line-height: 1.2;
  white-space: nowrap;
  cursor: pointer;
}

.dungeon-toggle:hover { background: var(--panel); }
.dungeon-toggle:focus-visible { outline: 2px dashed var(--gold); outline-offset: 3px; }
.dungeon-toggle:active { transform: translate(1px, 1px); }
.dungeon-toggle-icon {
  width: 13px;
  height: 15px;
  flex: none;
  background: currentColor;
  clip-path: polygon(0 100%, 0 34%, 15% 34%, 15% 16%, 31% 16%, 31% 0,
    69% 0, 69% 16%, 85% 16%, 85% 34%, 100% 34%, 100% 100%, 63% 100%,
    63% 49%, 37% 49%, 37% 100%);
}

:root[data-theme="dungeon"] {
  color-scheme: dark;
  --paper: #16131e;
  --surface: #211b2d;
  --ink: #f8e9dc;
  --gold: #ffc582;
  --frame: #9d7eb7;
  --muted: #d5c4da;
  --line: #594664;
  --panel: #31243e;
  --accent: #573b61;
  --shadow: 5px 5px 0 #0e0b15, 6px 6px 0 #594664;
}

[data-theme="dungeon"] body {
  background:
    radial-gradient(ellipse at 50% 0, #39264466, transparent 730px),
    repeating-linear-gradient(0deg, transparent 0 23px, #0b091018 23px 24px),
    var(--paper);
}

[data-theme="dungeon"] .dungeon-toggle {
  border-color: var(--gold);
  background: var(--accent);
  box-shadow: 2px 2px 0 #a46a48;
}

[data-theme="dungeon"] .dungeon-toggle-icon {
  clip-path: polygon(42% 0, 58% 0, 58% 25%, 75% 25%, 75% 42%, 100% 42%, 100% 58%,
    75% 58%, 75% 75%, 58% 75%, 58% 100%, 42% 100%, 42% 75%, 25% 75%,
    25% 58%, 0 58%, 0 42%, 25% 42%, 25% 25%, 42% 25%);
}

[data-theme="dungeon"] :is(h1, h2) {
  text-shadow: 3px 3px 0 #47304e;
}

[data-theme="dungeon"] .home {
  background: radial-gradient(ellipse at 78% 48%, #67446f44, transparent 69%);
}

[data-theme="dungeon"] :is(.site-header:not(.is-docked), .vision, .research, .news) {
  border-color: var(--line);
}

[data-theme="dungeon"] :is(.button, .studies, .contact, .outfit-board) {
  border-color: var(--frame);
  box-shadow: var(--shadow);
}

[data-theme="dungeon"] .button {
  background: var(--panel);
  color: var(--gold);
}

[data-theme="dungeon"] .button:hover { background: var(--accent); }

[data-theme="dungeon"] :is(.button:hover, .button:active) {
  box-shadow: 2px 2px 0 #9d7eb7;
}

[data-theme="dungeon"] .home-portrait {
  background: #211b29 url("dungeon-chamber.webp") center / 100% 100% no-repeat;
  box-shadow: var(--shadow), inset 0 0 0 3px #130f1988;
}

[data-theme="dungeon"] .home-portrait::before,
[data-theme="dungeon"] .outfit-portrait::before { background: #100c16aa; }

/* Recolor the bead inside its frame without tinting the hands or face. */
[data-theme="dungeon"] :is(.home-character-upper, .portrait-nazar)::after {
  content: '';
  position: absolute;
  background: url("dungeon-can-home.webp?v=hornless") center / 100% 100% no-repeat;
  clip-path: polygon(50.48% 50.87%, 55.63% 50.87%, 55.63% 51.90%, 56.59% 51.90%, 56.59% 53.01%, 57.72% 53.01%, 57.72% 57.99%, 56.67% 57.99%, 56.67% 59.02%, 55.71% 59.02%, 55.71% 60.05%, 50.48% 60.05%, 50.48% 59.02%, 49.44% 59.02%, 49.44% 57.99%, 48.47% 57.99%, 48.47% 53.09%, 49.44% 53.09%, 49.44% 51.98%, 50.48% 51.98%);
  filter: grayscale(1) sepia(1) saturate(5) hue-rotate(315deg) brightness(1.3);
  image-rendering: pixelated;
  pointer-events: none;
}

[data-theme="dungeon"] .home-character-upper::after {
  inset: 0;
  width: 100%;
  height: 100%;
}

[data-theme="dungeon"] .portrait-nazar::after {
  top: 50%;
  left: 50%;
  height: 179%;
  aspect-ratio: 1244 / 1264;
  transform: translate(-49.9%, -46%);
}

[data-theme="dungeon"] :is(.home-character, .outfit-character) {
  filter: drop-shadow(2px 1px 0 #c37c5177);
}

[data-theme="dungeon"] .home-eyelids::before,
[data-theme="dungeon"] .home-eyelids::after {
  background: linear-gradient(#b56f69 46%, #512b36 46% 60%, #b56f69 60%);
}

[data-theme="dungeon"] .header-mage {
  filter: hue-rotate(175deg) saturate(.85) brightness(1.14)
    drop-shadow(0 0 5px #b190df88);
}

[data-theme="dungeon"] .header-portal {
  --portal-edge: #8753c1;
  --portal-light: #ffdda1;
  --portal-spark: #bb83e8;
  --portal-depth: #271638;
}

[data-theme="dungeon"] .rubick-dog {
  filter: drop-shadow(1px 0 0 #ba6b6988);
}

[data-theme="dungeon"] .rubick-ball {
  filter: hue-rotate(248deg) brightness(1.12) drop-shadow(0 0 3px #cda5ef);
}

[data-theme="dungeon"] .outfit-preview { --outfit-backdrop: #211b2a; }
[data-theme="dungeon"] .outfit-preview[data-outfit="hoodie"] { --outfit-backdrop: #1b222d; }
[data-theme="dungeon"] .outfit-preview[data-outfit="suit"] { --outfit-backdrop: #222138; }
[data-theme="dungeon"] .outfit-preview[data-outfit="beach"] { --outfit-backdrop: #30212a; }
[data-theme="dungeon"] .outfit-portrait {
  background-image: linear-gradient(0deg, #100d17aa, transparent 37%, #100d1770),
    linear-gradient(#9b789122 1px, transparent 1px),
    linear-gradient(90deg, #9b789122 1px, transparent 1px);
  background-size: 100% 100%, 14px 14px, 14px 14px;
}

/* Both wardrobes use the same head-to-boot canvas and floor line. */
[data-theme="dungeon"] .outfit-body::before,
[data-theme="dungeon"] .outfit-body::after {
  inset: 0;
  height: auto;
  background: var(--outfit-sprite) center / 100% 100% no-repeat;
}
[data-theme="dungeon"] .outfit-body::before {
  clip-path: inset(0 0 12%);
  transform: translateY(var(--outfit-breath-offset));
}
[data-theme="dungeon"] .outfit-body::after { clip-path: inset(87.08% 0 0); }

[data-theme="dungeon"] .outfit-head {
  inset: 0 auto auto 24.02%;
  width: 51.96%;
  height: auto;
  aspect-ratio: 170 / 155;
  background: url("wardrobe-shared-head.webp?v=clean-edge") center / contain no-repeat;
  clip-path: none;
}
[data-theme="dungeon"] .outfit-head .outfit-eyelids::before,
[data-theme="dungeon"] .outfit-head .outfit-eyelids::after {
  top: 59.4%;
  width: 9.4%;
  height: 10.4%;
  background: linear-gradient(#dc997a 46%, #633228 46% 62%, #dc997a 62%);
}
[data-theme="dungeon"] .outfit-head .outfit-eyelids::before { left: 42.35%; }
[data-theme="dungeon"] .outfit-head .outfit-eyelids::after { left: 67.1%; }

[data-theme="dungeon"] [data-outfit="original"] { --outfit-sprite: var(--dungeon-original, none); }
[data-theme="dungeon"] [data-outfit="hoodie"] { --outfit-sprite: var(--dungeon-hoodie, none); }
[data-theme="dungeon"] [data-outfit="suit"] { --outfit-sprite: var(--dungeon-suit, none); }
[data-theme="dungeon"] [data-outfit="beach"] { --outfit-sprite: var(--dungeon-beach, none); }

[data-theme="dungeon"] .outfit-options [for] .outfit-icon {
  background: url("dungeon-front-outfit-icons.png") var(--dungeon-icon-position, left top) / 200% 200% no-repeat;
}

[data-theme="dungeon"] [for="outfit-hoodie"] { --dungeon-icon-position: right top; }
[data-theme="dungeon"] [for="outfit-suit"] { --dungeon-icon-position: left bottom; }
[data-theme="dungeon"] [for="outfit-beach"] { --dungeon-icon-position: right bottom; }

[data-theme="dungeon"] .topic-link:where(:not([data-topic="hci"])) .topic-icon {
  background: url("dungeon-topics.png?v=clear-symbols") var(--dungeon-icon-x, 0) 0 / 112px 22px no-repeat;
}

[data-theme="dungeon"] [data-topic="psychology"] { --dungeon-icon-x: -28px; }
[data-theme="dungeon"] [data-topic="biosignals"] { --dungeon-icon-x: -56px; }
[data-theme="dungeon"] [data-topic="xr-vr"] { --dungeon-icon-x: -84px; }

@media (min-width: 821px) and (max-width: 1400px) {
  .site-header:has(.dungeon-toggle:not([hidden])) {
    grid-template-columns: minmax(0, 1fr);
    justify-items: center;
  }
  .site-header:not(.is-docked):has(.dungeon-toggle:not([hidden])) .site-nav { padding: 0; border: 0; }
}

@media (max-width: 820px) {
  .site-nav:has(.dungeon-toggle:not([hidden])) { row-gap: 5px; }
}

/* Positions and texture offsets are relative to the 240 × 220 chamber artwork. */
.chamber-torch { display: none; }
[data-theme="dungeon"] .chamber-torch {
  position: absolute;
  display: block;
  top: 28.636%;
  left: var(--torch-left);
  width: 6.667%;
  height: 11.364%;
  pointer-events: none;
}
.chamber-torch-left {
  --torch-left: 9.167%;
  --wall-texture: 16.964%;
  --torch-delay: -.1s;
  --flame-period: 1.02s;
  --inner-period: .81s;
  --core-period: .66s;
  --glow-period: 1.38s;
  --ember-period: 1.42s;
  --second-ember-period: 1.78s;
}
.chamber-torch-right {
  --torch-left: 84.583%;
  --wall-texture: 97.768%;
  --torch-delay: -.63s;
  --flame-period: 1.29s;
  --inner-period: .93s;
  --core-period: .74s;
  --glow-period: 1.67s;
  --ember-period: 1.61s;
  --second-ember-period: 2.03s;
}

/* Sample the adjacent brickwork over the original flame. Keep the sconce intact. */
.chamber-torch::before {
  content: '';
  position: absolute;
  inset: 0;
  background: url("dungeon-chamber.webp") var(--wall-texture) 32.308% / 1500% 880% no-repeat;
  clip-path: polygon(45% 0, 60% 0, 60% 24%, 72% 24%, 72% 36%, 82% 36%, 82% 44%,
    95% 44%, 95% 60%, 100% 60%, 100% 84%, 87.5% 84%, 87.5% 96%, 94% 96%, 94% 100%,
    6.25% 100%, 6.25% 76%, 0 76%, 0 60%, 12.5% 60%, 12.5% 48%, 18.75% 48%, 18.75% 38%,
    37.5% 38%, 37.5% 24%, 43.75% 24%, 43.75% 10%, 45% 10%);
}

.chamber-torch-glow {
  position: absolute;
  inset: -85% -148% -73%;
  background: radial-gradient(ellipse at 50% 56%, #ffc16a5c, #f3782a24 45%, transparent 71%);
  opacity: .56;
  animation: chamber-torch-glow var(--glow-period) ease-in-out var(--torch-delay) infinite;
}
.chamber-flame {
  position: absolute;
  inset: -15% 0 0;
  background: #d8683d;
  clip-path: polygon(50% 0, 59% 0, 59% 20%, 66% 20%, 66% 35%, 75% 35%, 75% 55%, 88% 55%,
    88% 48%, 94% 48%, 94% 68%, 100% 68%, 100% 84%, 88% 84%, 88% 100%, 12% 100%,
    12% 87%, 0 87%, 0 69%, 12% 69%, 12% 56%, 25% 56%, 25% 46%, 32% 46%, 32% 59%,
    44% 59%, 44% 32%, 50% 32%);
  animation: chamber-flame-shape var(--flame-period) steps(1, end) var(--torch-delay) infinite;
}
.chamber-flame::before, .chamber-flame::after {
  content: '';
  position: absolute;
  bottom: 0;
  transform-origin: center bottom;
}
.chamber-flame::before {
  inset: 21% 16% 2%;
  background: #ffab4d;
  clip-path: polygon(50% 0, 59% 0, 59% 19%, 69% 19%, 69% 38%, 80% 38%, 80% 58%, 100% 58%,
    100% 83%, 90% 83%, 90% 100%, 10% 100%, 10% 85%, 0 85%, 0 63%, 15% 63%, 15% 50%,
    35% 50%, 35% 29%, 50% 29%);
  animation: chamber-flame-inner var(--inner-period) steps(1, end) var(--torch-delay) infinite;
}
.chamber-flame::after {
  inset: 54% 30% 3%;
  background: #ffe5a2;
  clip-path: polygon(50% 0, 67% 0, 67% 21%, 83% 21%, 83% 49%, 100% 49%, 100% 86%,
    83% 86%, 83% 100%, 17% 100%, 17% 84%, 0 84%, 0 53%, 17% 53%, 17% 30%, 50% 30%);
  animation: chamber-flame-inner var(--core-period) steps(1, end) reverse infinite;
}
.chamber-ember {
  position: absolute;
  top: 53%;
  left: 39%;
  width: clamp(2px, 8%, 4px);
  aspect-ratio: 1;
  background: #ffd378;
  opacity: 0;
  --ember-drift: -12px;
  animation: chamber-ember-rise var(--ember-period) steps(12, end) var(--torch-delay) infinite;
}
.chamber-ember:last-child {
  top: 43%;
  left: 63%;
  background: #f58a41;
  --ember-drift: 10px;
  animation-duration: var(--second-ember-period);
  animation-delay: calc(var(--torch-delay) - .74s);
}

@keyframes chamber-flame-shape {
  24%, 76% {
    clip-path: polygon(35% 5%, 44% 5%, 44% 19%, 56% 19%, 56% 32%, 69% 32%, 69% 51%,
      81% 51%, 81% 61%, 94% 61%, 94% 72%, 100% 72%, 100% 90%, 87% 90%, 87% 100%, 12% 100%,
      12% 88%, 0 88%, 0 70%, 12% 70%, 12% 54%, 25% 54%, 25% 39%, 37% 39%, 37% 26%, 35% 26%);
  }
  49% {
    clip-path: polygon(62% 8%, 72% 8%, 72% 29%, 65% 29%, 65% 42%, 78% 42%, 78% 58%, 89% 58%,
      89% 50%, 95% 50%, 95% 72%, 100% 72%, 100% 86%, 86% 86%, 86% 100%, 12% 100%, 12% 87%,
      0 87%, 0 68%, 13% 68%, 13% 50%, 22% 50%, 22% 58%, 39% 58%, 39% 44%, 51% 44%, 51% 24%, 62% 24%);
  }
}
@keyframes chamber-flame-inner {
  0%, 100% { transform: none; }
  26% { transform: translateX(-9%) scaleY(.84); }
  51% { transform: translateX(8%) scaleY(1.12); }
  77% { transform: scaleY(.92); }
}
@keyframes chamber-torch-glow {
  0%, 100% { opacity: .48; }
  27%, 69% { opacity: .83; }
  46%, 86% { opacity: .58; }
}
@keyframes chamber-ember-rise {
  0% { opacity: 0; transform: translate(0, 0); }
  17% { opacity: .94; }
  54% { opacity: .72; transform: translate(var(--ember-drift), -25px); }
  83%, 100% { opacity: 0; transform: translate(calc(var(--ember-drift) * .7), -44px); }
}
@media (prefers-reduced-motion: reduce) {
  .chamber-torch :is(.chamber-torch-glow, .chamber-flame, .chamber-ember),
  .chamber-flame::before, .chamber-flame::after { animation: none; }
}
