:where(html) {
  background: #fbf5ec;
  color-scheme: light;
}

:where(html[data-theme="dark"]) {
  background: #101722;
  color-scheme: dark;
}

:root {
  color-scheme: light;
  --paper: #fbf5ec;
  --ink: #413024;
  --edge: #d9cfc1;
  --focus: #925b36;
  --hover: #eee5d9;
  --muted: #756456;
  --mark: #413024;
  --mark-ink: #fbf5ec;
  --cloth: #ddcbb2;
  --control: rgba(251, 245, 236, 0.84);
  --control-hover: rgba(251, 245, 236, 0.96);
  --panel-bg: rgba(251, 245, 236, 0.94);
  --panel-shadow: 0 12px 32px rgba(45, 30, 18, 0.16), 0 2px 6px rgba(45, 30, 18, 0.08);
  --track: #d9cfc1;
  --gap: clamp(7px, 0.66vw, 13px);
  font-family: "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Noto Sans SC", system-ui, -apple-system, sans-serif;
  color: var(--ink);
  background: var(--paper);
  transition: background-color 0.35s ease, color 0.35s ease;
}

html[data-theme="dark"] {
  color-scheme: dark;
  --paper: #101722;
  --ink: #eae3d8;
  --edge: #2c384a;
  --focus: #8cb8ef;
  --hover: #1b2535;
  --muted: #9aa7b8;
  --mark: #eae3d8;
  --mark-ink: #101722;
  --cloth: #222c38;
  --control: rgba(16, 23, 34, 0.68);
  --control-hover: rgba(28, 39, 56, 0.9);
  --panel-bg: rgba(20, 29, 42, 0.94);
  --panel-shadow: 0 14px 36px rgba(0, 0, 0, 0.45), 0 2px 8px rgba(0, 0, 0, 0.25);
  --track: #2e3b4e;
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  min-height: 100svh;
  background: var(--paper);
  color: var(--ink);
  -webkit-tap-highlight-color: transparent;
  transition: background-color 0.35s ease, color 0.35s ease;
}

.symbols {
  position: absolute;
  width: 0;
  height: 0;
  overflow: hidden;
  pointer-events: none;
}

.sr {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border: 0;
}

main {
  padding: var(--gap) var(--gap) max(var(--gap), env(safe-area-inset-bottom));
  margin: auto;
  max-width: 1920px;
}

button {
  font: inherit;
  color: inherit;
  cursor: pointer;
  touch-action: manipulation;
}

button[hidden],
div[hidden] {
  display: none !important;
}

button:focus-visible,
#stage:focus-visible,
input[type="range"]:focus-visible {
  outline: 3px solid var(--focus);
  outline-offset: 2px;
}

#viewer {
  position: relative;
  max-width: min(100%, calc((100svh - 118px) * 1.62246));
  margin: auto;
}

.playfield {
  position: relative;
}

#stage {
  position: relative;
  aspect-ratio: 1517 / 935;
  max-height: calc(100svh - 118px);
  width: 100%;
  overflow: hidden;
  border-radius: clamp(12px, 1.32vw, 25px);
  background: var(--cloth);
  touch-action: none;
  user-select: none;
  -webkit-user-select: none;
  box-shadow: 0 2px 14px rgba(32, 22, 12, 0.08);
  transition: background-color 0.35s ease;
}

html[data-theme="dark"] #stage {
  box-shadow: 0 4px 24px rgba(0, 0, 0, 0.35);
}

#scene {
  display: block;
  width: 100%;
  height: 100%;
  position: absolute;
  inset: 0;
  visibility: hidden;
}

#stage[data-ready="true"] #scene {
  visibility: visible;
}

#stage[data-over="true"] {
  cursor: grab;
}

#stage[data-pressed="true"] {
  cursor: none;
}

/* Floating Icon Buttons */
.icon-button {
  width: 44px;
  height: 44px;
  padding: 0;
  border: 0;
  border-radius: 50%;
  background: transparent;
  display: grid;
  place-items: center;
  color: var(--ink);
  transition: background-color 0.18s ease, transform 0.15s ease, color 0.25s ease;
}

.icon-button:hover {
  background: var(--hover);
}

.icon-button:active {
  transform: scale(0.94);
}

.icon-button svg {
  width: 22px;
  height: 22px;
  fill: none;
  stroke: currentColor;
  stroke-width: 1.9;
  stroke-linecap: round;
  stroke-linejoin: round;
}

/* Theme Switch Floating Inside Top-Right of Stage */
#theme {
  position: absolute;
  top: clamp(9px, 1.1vw, 18px);
  right: clamp(9px, 1.1vw, 18px);
  z-index: 10;
  background: var(--control);
  backdrop-filter: blur(8px);
  -webkit-backdrop-filter: blur(8px);
  border: 1px solid rgba(128, 128, 128, 0.16);
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
}

#theme:hover {
  background: var(--control-hover);
}

#theme .sun {
  display: none;
}

#theme[aria-checked="true"] .moon {
  display: none;
}

#theme[aria-checked="true"] .sun {
  display: inline;
}

#retry {
  position: absolute;
  bottom: 14px;
  left: 14px;
  z-index: 6;
  background: var(--control);
}

/* Toolbar */
.toolbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px 14px;
  padding: 10px 0 2px;
  position: relative;
}

.choices {
  display: flex;
  align-items: center;
  min-width: 0;
  max-width: 100%;
  justify-content: flex-start;
  gap: clamp(4px, 0.62vw, 10px);
  flex-wrap: nowrap;
  overflow-x: auto;
  scrollbar-width: none;
  padding: 2px;
}

.choices::-webkit-scrollbar {
  display: none;
}

.choices button {
  --size: clamp(44px, 4.7vw, 60px);
  position: relative;
  flex: 0 0 var(--size);
  width: var(--size);
  height: var(--size);
  padding: 3px;
  display: grid;
  place-items: center;
  background: transparent;
  border: 2px solid transparent;
  border-radius: 50%;
  transition: border-color 0.2s ease, transform 0.18s cubic-bezier(0.22, 1, 0.36, 1);
}

.choices button:hover {
  transform: translateY(-2px);
}

.choices button:active {
  transform: scale(0.95);
}

.choices button[aria-pressed="true"] {
  border-color: var(--mark);
}

.choices button[aria-pressed="true"]::after {
  content: "";
  position: absolute;
  right: -1px;
  bottom: -1px;
  width: 18px;
  height: 18px;
  border-radius: 50%;
  background-color: var(--mark);
  border: 2px solid var(--paper);
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 16 16'%3E%3Cpath d='M4 8.3 6.8 11 12.2 5.2' fill='none' stroke='%23fbf5ec' stroke-width='2.1' stroke-linecap='round' stroke-linejoin='round'/%3E%3C/svg%3E");
  background-size: 12px 12px;
  background-position: center;
  background-repeat: no-repeat;
  box-shadow: 0 1px 4px rgba(0, 0, 0, 0.2);
}

html[data-theme="dark"] .choices button[aria-pressed="true"]::after {
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 16 16'%3E%3Cpath d='M4 8.3 6.8 11 12.2 5.2' fill='none' stroke='%23101722' stroke-width='2.1' stroke-linecap='round' stroke-linejoin='round'/%3E%3C/svg%3E");
}

.coat {
  display: block;
  width: 100%;
  height: 100%;
  border-radius: 50%;
  object-fit: cover;
  background: var(--cloth);
  box-shadow: inset 0 1px 3px rgba(0, 0, 0, 0.22), 0 1px 2px rgba(0, 0, 0, 0.08);
}

/* Right Tool Icons & Popover */
.tools {
  display: flex;
  align-items: center;
  gap: 3px;
  position: relative;
  flex-shrink: 0;
}

#sound .waves {
  display: none;
}

#sound[aria-checked="true"] .muted {
  display: none;
}

#sound[aria-checked="true"] .waves {
  display: inline;
}

#expand .exit-full {
  display: none;
}

#expand[aria-pressed="true"] .enter-full {
  display: none;
}

#expand[aria-pressed="true"] .exit-full {
  display: inline;
}

#proportions-toggle[aria-expanded="true"] {
  background: var(--hover);
}

#proportions-panel {
  position: absolute;
  bottom: calc(100% + 12px);
  right: 0;
  z-index: 20;
  padding: 14px 16px;
  border-radius: 16px;
  background: var(--panel-bg);
  backdrop-filter: blur(12px);
  -webkit-backdrop-filter: blur(12px);
  border: 1px solid var(--edge);
  box-shadow: var(--panel-shadow);
  display: flex;
  flex-direction: column;
  gap: 12px;
  min-width: 244px;
  animation: panelPop 0.18s cubic-bezier(0.22, 1, 0.36, 1);
}

@keyframes panelPop {
  from {
    opacity: 0;
    transform: translateY(6px) scale(0.97);
  }
  to {
    opacity: 1;
    transform: translateY(0) scale(1);
  }
}

.proportion-row {
  display: flex;
  align-items: center;
  gap: 10px;
}

.proportion-row svg {
  width: 28px;
  height: 28px;
  flex-shrink: 0;
  fill: var(--ink);
  color: var(--ink);
  opacity: 0.82;
}

.proportion-row input[type="range"] {
  -webkit-appearance: none;
  appearance: none;
  flex: 1;
  height: 6px;
  border-radius: 999px;
  background: var(--track);
  outline: none;
  cursor: pointer;
}

.proportion-row input[type="range"]::-webkit-slider-thumb {
  -webkit-appearance: none;
  appearance: none;
  width: 18px;
  height: 18px;
  border-radius: 50%;
  background: var(--ink);
  border: 2.5px solid var(--paper);
  box-shadow: 0 1px 4px rgba(0, 0, 0, 0.25);
  transition: transform 0.14s ease;
}

.proportion-row input[type="range"]::-webkit-slider-thumb:hover {
  transform: scale(1.12);
}

.proportion-row input[type="range"]::-moz-range-thumb {
  width: 18px;
  height: 18px;
  border-radius: 50%;
  background: var(--ink);
  border: 2.5px solid var(--paper);
  box-shadow: 0 1px 4px rgba(0, 0, 0, 0.25);
}

/* Footer */
.profile {
  margin-top: 6px;
  text-align: center;
  font-size: 13px;
  line-height: 26px;
  color: var(--muted);
}

.profile nav {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  flex-wrap: wrap;
  justify-content: center;
}

.profile [aria-current="page"] {
  color: var(--ink);
  font-weight: 500;
}

.profile .hint-text {
  font-size: 12px;
  opacity: 0.85;
}

/* Fullscreen Mode */
#viewer:fullscreen,
#viewer:-webkit-full-screen {
  max-width: none;
  width: 100vw;
  height: 100vh;
  background: var(--paper);
  padding: 16px;
  display: flex;
  flex-direction: column;
  justify-content: center;
}

#viewer:fullscreen .playfield,
#viewer:-webkit-full-screen .playfield {
  flex: 1;
  min-height: 0;
  display: grid;
  place-items: center;
}

#viewer:fullscreen #stage,
#viewer:-webkit-full-screen #stage {
  max-height: calc(100vh - 100px);
  max-width: min(100%, calc((100vh - 100px) * 1.62246));
}

/* Responsive Tweaks */
@media (max-width: 820px) {
  .toolbar {
    flex-direction: column;
    align-items: stretch;
    gap: 8px;
  }

  .choices {
    justify-content: flex-start;
  }

  .tools {
    justify-content: flex-end;
  }

  #viewer {
    max-width: 100%;
  }

  #stage {
    max-height: calc(100svh - 150px);
  }
}
