:root {
  --paper: #fbf5ec;
  --ink: #413024;
  --muted: #756456;
  --edge: #d9cfc1;
  --hover: #eee5d9;
  --focus: #925b36;
  --cloth: #ddcbb2;
  --surface: rgba(251, 245, 236, 0.92);
  --shadow: 0 10px 30px rgba(65, 48, 36, 0.1);
}

:root[data-theme="dark"] {
  --paper: #101722;
  --ink: #dfebf3;
  --muted: #a0afbf;
  --edge: #526276;
  --hover: #293546;
  --focus: #b4d9fa;
  --cloth: #303e50;
  --surface: rgba(22, 31, 46, 0.92);
  --shadow: 0 12px 32px rgba(0, 0, 0, 0.45);
}

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

html, body {
  margin: 0;
  padding: 0;
  min-height: 100%;
  background: var(--paper);
  color: var(--ink);
  font-family: "PingFang SC", "Hiragino Sans GB", "Hiragino Maru Gothic ProN", "Microsoft YaHei", system-ui, sans-serif;
  transition: background-color 0.35s ease, color 0.35s ease;
  -webkit-font-smoothing: antialiased;
  user-select: none;
  -webkit-user-select: none;
}

body {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: clamp(10px, 1.8vw, 22px);
}

main {
  width: 100%;
  max-width: min(1060px, calc((100vh - 156px) * 1.622));
}

#viewer {
  display: flex;
  flex-direction: column;
  gap: clamp(12px, 1.5vw, 18px);
}

.playfield {
  width: 100%;
}

#stage {
  position: relative;
  width: 100%;
  aspect-ratio: 1517 / 935;
  border-radius: clamp(12px, 1.32vw, 25px);
  overflow: hidden;
  background: var(--cloth);
  box-shadow: var(--shadow);
  touch-action: none;
  outline: none;
  transition: box-shadow 0.35s ease;
}

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

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

#scene {
  display: block;
  width: 100%;
  height: 100%;
}

/* Floating top-right theme toggle */
#theme {
  position: absolute;
  top: clamp(10px, 1.4vw, 18px);
  right: clamp(10px, 1.4vw, 18px);
  width: clamp(38px, 3.8vw, 44px);
  height: clamp(38px, 3.8vw, 44px);
  border-radius: 50%;
  border: 1px solid rgba(255, 255, 255, 0.35);
  background: var(--surface);
  color: var(--ink);
  display: grid;
  place-items: center;
  cursor: pointer;
  backdrop-filter: blur(8px);
  -webkit-backdrop-filter: blur(8px);
  box-shadow: 0 4px 14px rgba(0, 0, 0, 0.14);
  transition: transform 0.2s ease, background-color 0.25s ease, color 0.25s ease;
  z-index: 5;
}

#theme:hover {
  transform: scale(1.06);
  background: var(--hover);
}

#theme:focus-visible {
  outline: 2px solid var(--focus);
  outline-offset: 2px;
}

#theme svg {
  width: 20px;
  height: 20px;
}

#theme .icon-sun { display: none; }
:root[data-theme="dark"] #theme .icon-moon { display: none; }
:root[data-theme="dark"] #theme .icon-sun { display: block; }

/* Bottom Toolbar */
.toolbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 14px;
  padding: 2px 4px;
}

.choices {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: clamp(8px, 1.1vw, 13px);
}

.swatch {
  --size: clamp(42px, 5.2vw, 52px);
  position: relative;
  width: var(--size);
  height: var(--size);
  padding: 0;
  border-radius: 50%;
  border: 2.5px solid transparent;
  background-size: cover;
  background-position: center;
  background-color: var(--cloth);
  cursor: pointer;
  box-shadow: inset 0 1px 3px rgba(255, 255, 255, 0.3), 0 2px 6px rgba(0, 0, 0, 0.12);
  transition: transform 0.18s ease, border-color 0.2s ease, box-shadow 0.2s ease;
}

.swatch:hover {
  transform: translateY(-2px) scale(1.05);
}

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

.swatch.is-selected {
  border-color: var(--ink);
  box-shadow: 0 0 0 2px var(--paper), 0 0 0 4px var(--ink);
}

.swatch.is-selected::after {
  content: "✓";
  position: absolute;
  right: -3px;
  bottom: -3px;
  width: 18px;
  height: 18px;
  border-radius: 50%;
  background: var(--ink);
  color: var(--paper);
  font-size: 11px;
  font-weight: 700;
  display: grid;
  place-items: center;
  border: 1.5px solid var(--paper);
}

/* Right-aligned minimal tool buttons */
.tools {
  display: flex;
  align-items: center;
  gap: 6px;
  flex-shrink: 0;
}

.popover-anchor {
  position: relative;
}

.tool-btn {
  width: 42px;
  height: 42px;
  border-radius: 50%;
  border: 1px solid transparent;
  background: transparent;
  color: var(--ink);
  display: grid;
  place-items: center;
  cursor: pointer;
  transition: background-color 0.2s ease, transform 0.15s ease;
}

.tool-btn:hover {
  background: var(--hover);
}

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

.tool-btn svg {
  width: 21px;
  height: 21px;
}

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

#sound .icon-sound-on { display: none; }
#sound[aria-checked="true"] .icon-sound-off { display: none; }
#sound[aria-checked="true"] .icon-sound-on { display: block; }

#expand .icon-shrink { display: none; }
#expand[aria-pressed="true"] .icon-expand { display: none; }
#expand[aria-pressed="true"] .icon-shrink { display: block; }

/* Proportions Popover */
.proportions-panel {
  position: absolute;
  bottom: calc(100% + 12px);
  right: 0;
  width: 248px;
  padding: 14px 16px;
  border-radius: 16px;
  background: var(--surface);
  border: 1px solid var(--edge);
  box-shadow: var(--shadow);
  backdrop-filter: blur(12px);
  -webkit-backdrop-filter: blur(12px);
  display: flex;
  flex-direction: column;
  gap: 12px;
  z-index: 20;
}

.proportions-panel[hidden] {
  display: none;
}

.slider-row {
  display: grid;
  grid-template-columns: 36px 1fr 40px;
  align-items: center;
  gap: 10px;
  font-size: 0.84rem;
  color: var(--ink);
}

.slider-label {
  color: var(--muted);
  font-weight: 500;
}

.slider-val {
  text-align: right;
  font-variant-numeric: tabular-nums;
  color: var(--muted);
  font-size: 0.78rem;
}

.slider-wrap {
  position: relative;
  display: flex;
  align-items: center;
}

.slider-tick {
  position: absolute;
  left: 50%;
  top: 50%;
  width: 2px;
  height: 10px;
  background: var(--edge);
  transform: translate(-50%, -50%);
  pointer-events: none;
}

input[type="range"] {
  -webkit-appearance: none;
  appearance: none;
  width: 100%;
  height: 4px;
  border-radius: 99px;
  background: var(--edge);
  outline: none;
  cursor: pointer;
  position: relative;
  z-index: 1;
}

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

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

/* Minimal Footer */
.profile {
  margin-top: 14px;
  font-size: 0.8rem;
  color: var(--muted);
}

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

.profile .sep {
  opacity: 0.45;
}

.profile .author-link {
  color: var(--ink);
  text-decoration: none;
  border-bottom: 1px solid var(--edge);
  padding-bottom: 1px;
  transition: color 0.2s ease, border-color 0.2s ease;
}

.profile .author-link:hover {
  color: var(--focus);
  border-color: var(--focus);
}

.profile .author-link:focus-visible {
  outline: 2px solid var(--focus);
  outline-offset: 2px;
  border-radius: 3px;
}

/* Fullscreen mode */
body.is-full {
  padding: 12px;
}

body.is-full main {
  max-width: min(1400px, 96vw);
}

/* Responsive mobile layout */
@media (max-width: 720px) {
  .toolbar {
    flex-direction: column;
    gap: 14px;
  }

  .choices {
    justify-content: center;
    max-width: 340px;
  }

  .swatch {
    --size: clamp(46px, 13vw, 54px);
  }

  .proportions-panel {
    right: auto;
    left: 50%;
    transform: translateX(-50%);
  }
}

@media (max-width: 620px) {
  #stage {
    aspect-ratio: 1.56;
  }
}
