:root {
  color-scheme: light;
  --paper: #fbf5ec;
  --ink: #413024;
  --edge: #d9cfc1;
  --focus: #925b36;
  --hover: #eee5d9;
  --muted: #756456;
  --mark: #413024;
  --cloth: #ddcbb2;
  --control: rgba(251, 245, 236, 0.84);
  --gap: clamp(7px, 0.66vw, 13px);
  font-family: "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei UI", "Yu Gothic", system-ui, -apple-system, sans-serif;
  color: var(--ink);
  background: var(--paper);
}

:root[data-theme="dark"] {
  color-scheme: dark;
  --paper: #101722;
  --ink: #dfebf3;
  --edge: #526276;
  --focus: #b4d9fa;
  --hover: #293546;
  --muted: #a0afbf;
  --mark: #425c75;
  --cloth: #303e50;
  --control: rgba(18, 29, 43, 0.84);
}

* {
  box-sizing: border-box;
}

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

main {
  max-width: 1520px;
  margin: 0 auto;
  padding: clamp(7px, 1vw, 14px) clamp(8px, 1.3vw, 20px) clamp(8px, 1vw, 14px);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: flex-start;
  min-height: 100svh;
}

#viewer {
  position: relative;
  width: 100%;
  display: flex;
  flex-direction: column;
  gap: clamp(8px, 0.85vw, 12px);
}

.playfield {
  position: relative;
  width: 100%;
  display: flex;
  justify-content: center;
}

#stage {
  position: relative;
  width: 100%;
  max-height: calc(100svh - 114px);
  aspect-ratio: 1517 / 935;
  margin: 0 auto;
  border-radius: clamp(13px, 1.35vw, 25px);
  background: var(--cloth);
  overflow: hidden;
  box-shadow:
    0 0 0 1.5px var(--paper),
    0 0 0 3px #8a644588,
    0 10px 30px rgba(45, 29, 14, 0.09);
  user-select: none;
  touch-action: none;
  outline: none;
  cursor: grab;
}

:root[data-theme="dark"] #stage {
  box-shadow:
    0 0 0 1.5px var(--paper),
    0 0 0 3px #52688288,
    0 12px 34px rgba(0, 0, 0, 0.38);
}

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

#stage:focus-visible {
  box-shadow:
    0 0 0 2px var(--paper),
    0 0 0 4px var(--focus);
}

#scene {
  display: block;
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.breed-badge {
  position: absolute;
  left: clamp(12px, 1.4vw, 20px);
  bottom: clamp(10px, 1.2vw, 18px);
  padding: 5px 13px;
  border-radius: 999px;
  font-size: 0.82rem;
  font-weight: 500;
  letter-spacing: 0.03em;
  background: var(--control);
  color: var(--ink);
  backdrop-filter: blur(8px);
  box-shadow: inset 0 0 0 1px var(--edge), 0 3px 10px rgba(0, 0, 0, 0.08);
  pointer-events: none;
  opacity: 0;
  transform: translateY(4px);
  transition: opacity 0.35s ease, transform 0.35s ease;
}

.breed-badge.is-visible {
  opacity: 0.94;
  transform: translateY(0);
}

/* Floating Theme Button inside Viewer Top-Right */
#theme {
  position: absolute;
  z-index: 4;
  top: max(12px, env(safe-area-inset-top));
  right: max(14px, env(safe-area-inset-right));
  width: 44px;
  height: 44px;
  border-radius: 50%;
  background-color: var(--control);
  backdrop-filter: blur(6px);
  box-shadow: inset 0 0 0 1px var(--edge), 0 2px 8px rgba(35, 22, 12, 0.08);
}

#theme svg {
  width: 21px;
  height: 21px;
  stroke-width: 1.7;
}

:root[data-theme="dark"] #theme .moon,
:root:not([data-theme="dark"]) #theme .sun {
  display: none;
}

@media (hover: hover) {
  #theme:hover {
    background-color: var(--paper);
  }
}

/* Toolbar & Breed Swatches */
.toolbar {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: clamp(8px, 1vw, 14px);
  width: 100%;
  padding: 2px 2px 0;
}

.choices {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: clamp(6px, 0.72vw, 11px);
}

.choices button {
  position: relative;
  width: clamp(38px, 3.55vw, 48px);
  height: clamp(38px, 3.55vw, 48px);
  padding: 0;
  border: none;
  border-radius: 50%;
  background: transparent;
  cursor: pointer;
  display: grid;
  place-items: center;
  transition: transform 0.16s ease;
}

@media (hover: hover) {
  .choices button:hover {
    transform: translateY(-1.5px) scale(1.04);
  }
}

.choices button:focus-visible {
  outline: 2px solid var(--focus);
  outline-offset: 3px;
}

.choices button .coat {
  display: block;
  width: 84%;
  height: 84%;
  border-radius: 50%;
  background-size: cover;
  background-position: center;
  box-shadow: inset 0 0 0 1px rgba(65, 48, 36, 0.22), 0 1px 4px rgba(35, 22, 12, 0.12);
  transition: transform 0.16s ease, box-shadow 0.16s ease;
}

:root[data-theme="dark"] .choices button .coat {
  box-shadow: inset 0 0 0 1px rgba(184, 201, 223, 0.3), 0 2px 5px rgba(0, 0, 0, 0.35);
}

.choices button[aria-pressed="true"] {
  box-shadow: inset 0 0 0 2.2px var(--ink);
}

.choices button[aria-pressed="true"] .coat {
  width: 80%;
  height: 80%;
}

.choices button[aria-pressed="true"]::after {
  content: "";
  position: absolute;
  right: -1px;
  bottom: -1px;
  width: 15px;
  height: 15px;
  border-radius: 50%;
  background-color: var(--mark);
  border: 1.8px solid var(--paper);
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 12 12'%3E%3Cpath d='M2.5 6.3 4.9 8.6 9.6 3.6' fill='none' stroke='%23fbf5ec' stroke-width='1.7' stroke-linecap='round' stroke-linejoin='round'/%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: center;
  background-size: 10px 10px;
}

:root[data-theme="dark"] .choices button[aria-pressed="true"]::after {
  background-color: var(--ink);
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 12 12'%3E%3Cpath d='M2.5 6.3 4.9 8.6 9.6 3.6' fill='none' stroke='%23101722' stroke-width='1.8' stroke-linecap='round' stroke-linejoin='round'/%3E%3C/svg%3E");
}

/* Tool Icon Buttons */
.tools {
  display: flex;
  align-items: center;
  gap: 6px;
  margin-inline-start: auto;
}

.proportions-wrap {
  position: relative;
}

.icon-button {
  width: 44px;
  height: 44px;
  padding: 0;
  border: none;
  border-radius: 50%;
  background: transparent;
  color: var(--ink);
  display: grid;
  place-items: center;
  cursor: pointer;
  transition: background-color 0.16s ease, transform 0.14s ease;
}

.icon-button svg {
  width: 22px;
  height: 22px;
}

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

.icon-button:focus-visible {
  outline: 2px solid var(--focus);
  outline-offset: 2px;
}

#proportions-toggle[aria-expanded="true"],
#sound[aria-checked="true"],
#expand[aria-pressed="true"] {
  background-color: var(--hover);
}

#sound:not([aria-checked="true"]) .waves,
#sound[aria-checked="true"] .muted {
  display: none;
}

#expand:not([aria-pressed="true"]) .exit-full,
#expand[aria-pressed="true"] .enter-full {
  display: none;
}

/* Proportions Popover */
#proportions-panel {
  position: absolute;
  z-index: 6;
  bottom: calc(100% + 8px);
  right: 0;
  width: 268px;
  max-width: calc(100vw - 24px);
  padding: 8px 10px;
  background: var(--paper);
  color: var(--ink);
  border: 1px solid var(--edge);
  border-radius: 16px;
  box-shadow: 0 6px 22px rgba(33, 23, 12, 0.16);
}

:root[data-theme="dark"] #proportions-panel {
  box-shadow: 0 8px 26px rgba(0, 0, 0, 0.45);
}

.proportion-row {
  position: relative;
  display: grid;
  grid-template-columns: 32px minmax(0, 1fr) 32px;
  align-items: center;
  gap: 4px;
}

.proportion-row::after {
  content: "";
  position: absolute;
  left: 50%;
  bottom: 5px;
  width: 2px;
  height: 5px;
  transform: translateX(-50%);
  border-radius: 1px;
  background: var(--muted);
  pointer-events: none;
}

.proportion-row svg {
  width: 30px;
  height: 30px;
  fill: currentColor;
  pointer-events: none;
}

#proportions-panel input[type="range"] {
  display: block;
  min-width: 0;
  width: 100%;
  height: 40px;
  margin: 0;
  accent-color: var(--focus);
  touch-action: none;
  cursor: pointer;
}

/* Footer */
.profile {
  margin-top: 8px;
  font-size: 0.8rem;
  color: var(--muted);
  text-align: center;
}

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

.profile a {
  color: var(--muted);
  text-underline-offset: 3px;
}

.profile a:hover {
  color: var(--ink);
}

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

/* Fullscreen / Immersive Mode */
#viewer.is-full {
  background: var(--paper);
  padding: 10px 14px;
  height: 100dvh;
  justify-content: center;
}

#viewer.is-full #stage {
  max-height: calc(100dvh - 82px);
}

@media (max-width: 720px) {
  html, body {
    overscroll-behavior: none;
  }
  main {
    justify-content: center;
    padding: max(10px, env(safe-area-inset-top)) 10px max(12px, env(safe-area-inset-bottom));
  }
  #stage {
    aspect-ratio: 1.34 / 1;
    max-height: 62svh;
    border-radius: 18px;
  }
  .toolbar {
    flex-direction: column;
    align-items: center;
    gap: 10px;
  }
  .choices {
    justify-content: center;
    gap: 8px;
    max-width: 340px;
  }
  .choices button {
    width: 48px;
    height: 48px;
  }
  .tools {
    margin-inline: auto;
    gap: 14px;
  }
  #proportions-panel {
    right: auto;
    left: 50%;
    transform: translateX(-50%);
  }
  .hint-text,
  .hint-sep {
    display: none;
  }
}
