html,
body {
  width: 100%;
  height: 100%;
  margin: 0;
  overflow: hidden;
  background: #fff;
}

html[data-theme="night"],
html[data-theme="night"] body {
  background: #000;
  color-scheme: dark;
}

#board {
  display: block;
  width: 100%;
  height: 100%;
  user-select: none;
}

#board[data-pointer="ready"] {
  cursor: grab;
}

#board[data-pointer="dragging"] {
  cursor: grabbing;
}

#appearance-tools {
  position: fixed;
  top: max(20px, env(safe-area-inset-top));
  right: max(24px, env(safe-area-inset-right));
  z-index: 1;
  display: flex;
  align-items: center;
  gap: 24px;
}

#planet-control {
  --planet-track: #e4e7eb;
  --planet-on: #65cb63;
  display: flex;
  align-items: center;
  gap: 8px;
  color: #65758b;
}

html[data-theme="night"] #planet-control {
  --planet-track: #313131;
  color: #aaa;
}

#planet-control label {
  display: grid;
  place-items: center;
  width: 24px;
  height: 32px;
  cursor: pointer;
}

#planet-control label svg {
  width: 20px;
  height: 20px;
  fill: none;
  stroke: currentColor;
  stroke-width: 14;
  stroke-linecap: round;
  stroke-linejoin: round;
}

#planet-control:has([aria-checked="true"]) label {
  color: #429457;
}

html[data-theme="night"] #planet-control:has([aria-checked="true"]) label {
  color: #83d08a;
}

.visually-hidden {
  position: absolute;
  width: 1px;
  height: 1px;
  overflow: hidden;
  clip-path: inset(50%);
  white-space: nowrap;
}

/* Adapted from zakisheriff/Liquid-Glass's GlassSwitch (MIT). */
#planet-switch {
  --planet-lift: 0;
  --planet-knob-x: 21px;
  position: relative;
  flex: none;
  width: 48px;
  height: 24px;
  padding: 0;
  border: 0;
  border-radius: 999px;
  background: none;
  cursor: pointer;
  touch-action: none;
  user-select: none;
  -webkit-tap-highlight-color: transparent;
}

#planet-switch::before {
  content: "";
  position: absolute;
  inset: -10px -6px;
}

.planet-switch-visual {
  position: absolute;
  top: 50%;
  left: 0;
  width: 64px;
  height: 28px;
  transform: translateY(-50%) scale(0.75);
  transform-origin: left center;
  pointer-events: none;
}

.planet-switch-track,
.planet-switch-fill {
  position: absolute;
  inset: 0;
  border-radius: 999px;
  pointer-events: none;
}

.planet-switch-track {
  background: var(--planet-track);
  box-shadow: inset 0 1px 2px #0000000a;
}

.planet-switch-fill {
  background: var(--planet-on);
  opacity: 0;
}

.planet-switch-visual > .planet-switch-track,
.planet-switch-visual > .planet-switch-fill {
  --hole: calc(23px * var(--planet-lift));
  --gap: rgb(0 0 0 / calc(1 - var(--planet-lift)));
  mask-image: linear-gradient(90deg, #000 calc(var(--planet-knob-x) - var(--hole) - 7px),
    var(--gap) calc(var(--planet-knob-x) - var(--hole) + 5px),
    var(--gap) calc(var(--planet-knob-x) + var(--hole) - 5px),
    #000 calc(var(--planet-knob-x) + var(--hole) + 7px));
}

.planet-switch-thumb {
  position: absolute;
  top: 50%;
  left: -8px;
  width: 58px;
  height: 38px;
  border-radius: 999px;
  pointer-events: none;
  transform: translate(0, -50%) scale(0.655);
  transform-origin: center;
  background: rgb(255 255 255 / calc(var(--planet-lift) * 0.12));
  backdrop-filter: blur(0.5px) saturate(1.6);
  box-shadow: 0 calc(2px + var(--planet-lift) * 8px) calc(6px + var(--planet-lift) * 20px) #0003,
    inset 0 0 0 1px rgb(255 255 255 / calc(var(--planet-lift) * 0.8));
}

.planet-switch-face {
  position: absolute;
  inset: 0;
  border-radius: inherit;
  background: #fff;
  box-shadow: inset 0 1px 1px #fff;
  opacity: calc(1 - var(--planet-lift));
}

.planet-switch-lens {
  position: absolute;
  inset: 0;
  overflow: hidden;
  border-radius: inherit;
  opacity: var(--planet-lift);
  filter: url(#planet-switch-lens);
}

.planet-switch-scene {
  position: absolute;
  inset: 0;
  transform: scale(0.72);
}

.planet-switch-copy {
  position: absolute;
  top: 5px;
  left: 8px;
  width: 64px;
  height: 28px;
}

#planet-switch:focus-visible {
  outline: 2px solid #7193e0;
  outline-offset: 6px;
}

#planet-switch[aria-busy="true"] {
  opacity: 0.6;
}

#planet-switch:disabled,
#shuffle-orbs:disabled,
#shader-edit:disabled {
  opacity: 0.4;
  cursor: default;
}

#mode-status {
  position: fixed;
  top: calc(max(20px, env(safe-area-inset-top)) + 48px);
  right: max(24px, env(safe-area-inset-right));
  z-index: 2;
  max-width: calc(100vw - 48px);
  box-sizing: border-box;
  margin: 0;
  padding: 8px 12px;
  border: 1px solid #dce3eb;
  border-radius: 12px;
  background: #fff;
  color: #65758b;
  font: 14px/20px system-ui, sans-serif;
}

html[data-theme="night"] #mode-status {
  border-color: #383838;
  background: #181818;
  color: #aaa;
}

#mode-status[data-error="true"],
#shader-selection-count[data-error="true"],
.shader-preview-error {
  color: #be4b32;
}

.shader-preview-error {
  margin: 8px 0 0;
  font-size: 12px;
}

@media (prefers-reduced-transparency: reduce) {
  .planet-switch-thumb { backdrop-filter: none; }
  .planet-switch-face { opacity: 1; }
  .planet-switch-lens { visibility: hidden; }
}

#theme-switch {
  --thumb-offset: 0px;
  position: relative;
  z-index: 1;
  isolation: isolate;
  box-sizing: border-box;
  width: 64px;
  height: 32px;
  display: grid;
  grid-template-columns: 1fr 1fr;
  place-items: center;
  padding: 3px;
  appearance: none;
  border: 1px solid #dce3eb;
  border-radius: 999px;
  background-color: #eef2f6;
  box-shadow: inset 0 1px 1px #fff, 0 4px 12px #2a3d5714;
  cursor: pointer;
  transition: background-color 360ms, border-color 360ms, box-shadow 360ms;
}

#theme-switch::before {
  content: "";
  position: absolute;
  top: 3px;
  left: 3px;
  z-index: -1;
  box-sizing: border-box;
  width: 24px;
  height: 24px;
  border: 1px solid #fff;
  border-radius: 50%;
  background-color: #fff;
  box-shadow: 0 2px 6px #36517133;
  transform: translateX(var(--thumb-offset));
  transition: transform 400ms cubic-bezier(0.22, 1.29, 0.36, 1),
    background-color 360ms, border-color 360ms, box-shadow 360ms;
}

#theme-switch svg {
  width: 14px;
  height: 14px;
  fill: none;
  stroke: currentColor;
  stroke-width: 1.7;
  stroke-linecap: round;
  stroke-linejoin: round;
  transition: color 360ms, transform 400ms cubic-bezier(0.22, 1.29, 0.36, 1);
}

.theme-sun {
  color: #9a5e13;
}

.theme-moon {
  color: #65758b;
  transform: rotate(-23deg) scale(0.83);
}

#theme-switch[aria-checked="true"] {
  --thumb-offset: 32px;
  border-color: #383838;
  background-color: #191919;
  box-shadow: inset 0 1px 1px #ffffff14, 0 4px 12px #0006;
}

#theme-switch[aria-checked="true"]::before {
  border-color: #fafafa;
  background-color: #e9e9e9;
  box-shadow: 0 2px 8px #0006;
}

#theme-switch[aria-checked="true"] .theme-sun {
  color: #9c9c9c;
  transform: rotate(42deg) scale(0.83);
}

#theme-switch[aria-checked="true"] .theme-moon {
  color: #242424;
  transform: rotate(0) scale(1);
}

#theme-switch:active::before {
  transform: translateX(var(--thumb-offset)) scale(0.91);
}

#theme-switch:focus-visible {
  outline: 2px solid #7193e0;
  outline-offset: 3px;
}

#theme-switch[aria-checked="true"]:focus-visible {
  outline-color: #c4c4c4;
}

#shader-book,
#shuffle-orbs,
#shader-dialog {
  --book-surface: #fff;
  --book-text: #293547;
  --book-muted: #65758b;
  --book-border: #dce3eb;
  --book-inset: #eef2f6;
  color: var(--book-text);
  background: var(--book-surface);
}

html[data-theme="night"] #shader-book,
html[data-theme="night"] #shuffle-orbs,
html[data-theme="night"] #shader-dialog {
  --book-surface: #181818;
  --book-text: #e9e9e9;
  --book-muted: #aaa;
  --book-border: #383838;
  --book-inset: #272727;
}

#game-tools {
  position: fixed;
  right: max(24px, env(safe-area-inset-right));
  bottom: max(24px, env(safe-area-inset-bottom));
  z-index: 1;
  display: flex;
  gap: 8px;
}

#shader-book,
#shuffle-orbs {
  display: grid;
  place-items: center;
  width: 40px;
  height: 40px;
  padding: 8px;
  box-sizing: border-box;
  border: 1px solid var(--book-border);
  border-radius: 12px;
  box-shadow: 0 4px 12px #2a3d5714;
  cursor: pointer;
  transition: transform 300ms cubic-bezier(0.22, 1, 0.36, 1);
}

#shader-book:hover,
#shuffle-orbs:hover {
  transform: translateY(-2px);
}

#shader-book svg,
#shuffle-orbs svg,
#shader-close svg {
  width: 18px;
  height: 18px;
  fill: none;
  stroke: currentColor;
  stroke-width: 1.5;
  stroke-linecap: round;
  stroke-linejoin: round;
}

#shader-edit svg {
  width: 18px;
  height: 18px;
  fill: none;
  stroke: currentColor;
  stroke-width: 14;
  stroke-linecap: round;
  stroke-linejoin: round;
}

#shader-book svg,
#shuffle-orbs svg {
  stroke-width: 14;
}

#shader-dialog {
  box-sizing: border-box;
  width: min(720px, calc(100% - 32px));
  max-height: calc(100dvh - 48px);
  overflow: hidden;
  padding: 24px;
  border: 1px solid var(--book-border);
  border-radius: 24px;
  box-shadow: 0 24px 80px #0003;
  font-family: ui-sans-serif, system-ui, sans-serif;
  overscroll-behavior: contain;
}

#shader-dialog[open] {
  display: flex;
  flex-direction: column;
}

#shader-dialog::backdrop {
  background: #0006;
  backdrop-filter: blur(8px);
}

.shader-header {
  flex-shrink: 0;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  margin-bottom: 16px;
}

.shader-body {
  min-height: 0;
  overflow: auto;
  overscroll-behavior: contain;
  scrollbar-gutter: stable;
  padding: 4px 12px 8px 4px;
}

.shader-body::-webkit-scrollbar,
.shader-entry pre::-webkit-scrollbar {
  width: 6px;
  height: 6px;
}

.shader-body::-webkit-scrollbar-track,
.shader-entry pre::-webkit-scrollbar-track,
.shader-entry pre::-webkit-scrollbar-corner {
  background: transparent;
}

.shader-body::-webkit-scrollbar-thumb,
.shader-entry pre::-webkit-scrollbar-thumb {
  border-radius: 999px;
  background: var(--book-border);
}

.shader-body::-webkit-scrollbar-thumb:hover,
.shader-entry pre::-webkit-scrollbar-thumb:hover {
  background: var(--book-muted);
}

.shader-body::-webkit-scrollbar-button,
.shader-entry pre::-webkit-scrollbar-button {
  display: none;
  width: 0;
  height: 0;
}

@supports not selector(::-webkit-scrollbar) {
  .shader-body,
  .shader-entry pre {
    scrollbar-width: thin;
    scrollbar-color: var(--book-border) transparent;
  }
}

#shader-title {
  margin: 0;
  font-size: 18px;
  line-height: 28px;
  font-weight: 600;
}

.shader-credit {
  color: var(--book-muted);
  font-size: 12px;
  line-height: 16px;
  text-wrap: pretty;
}

#shader-close,
#shader-edit {
  flex: 0 0 auto;
  display: grid;
  place-items: center;
  width: 32px;
  height: 32px;
  padding: 4px;
  border: 0;
  border-radius: 8px;
  background: var(--book-inset);
  color: inherit;
  cursor: pointer;
}

#shader-edit {
  background: transparent;
}

#shader-edit:hover,
#shader-edit[aria-pressed="true"] {
  background: var(--book-inset);
}

.shader-actions {
  display: flex;
  align-items: center;
  gap: 8px;
}

#shader-edit[aria-pressed="true"] {
  color: #7193e0;
  box-shadow: inset 0 0 0 1px currentColor;
}

#shader-editor:not([hidden]) {
  flex-shrink: 0;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding-top: 16px;
}

#shader-selection-count {
  color: var(--book-muted);
  font-size: 12px;
  line-height: 16px;
}

#shader-cancel,
#shader-apply {
  padding: 8px 12px;
  border: 1px solid var(--book-border);
  border-radius: 8px;
  background: var(--book-inset);
  color: var(--book-text);
  font: inherit;
  font-size: 14px;
  line-height: 20px;
  cursor: pointer;
}

#shader-apply {
  border-color: var(--book-text);
  background: var(--book-text);
  color: var(--book-surface);
}

#shader-apply:disabled {
  opacity: 0.4;
  cursor: default;
}

#shader-list {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  align-items: start;
  gap: 12px;
}

.shader-entry {
  position: relative;
  min-width: 0;
  padding: 12px;
  border: 1px solid var(--book-border);
  border-radius: 12px;
}

.shader-choice {
  display: none;
  position: absolute;
  top: 8px;
  right: 8px;
  padding: 4px;
  cursor: pointer;
}

#shader-dialog[data-editing="true"] .shader-choice {
  display: block;
}

#shader-dialog[data-editing="true"] .shader-entry {
  cursor: pointer;
}

.shader-choice input {
  display: block;
  width: 16px;
  height: 16px;
  margin: 0;
  accent-color: #7193e0;
  cursor: inherit;
}

#shader-dialog[data-editing="true"] .shader-entry[data-selected="true"] {
  border-color: #7193e0;
  box-shadow: inset 0 0 0 1px #7193e0;
}

.shader-entry[data-active="true"] {
  border-color: var(--book-muted);
  background: var(--book-inset);
}

.shader-preview {
  display: block;
  width: 64px;
  max-width: 100%;
  height: auto;
  aspect-ratio: 1;
  margin: 0 auto 8px;
}

.shader-number {
  margin: 0 0 4px;
  color: var(--book-muted);
  font-size: 12px;
  line-height: 16px;
}

.shader-info h2 {
  margin: 0 0 8px;
  font-size: 14px;
  line-height: 20px;
  font-weight: 600;
}

.shader-entry summary {
  width: fit-content;
  font-size: 12px;
  line-height: 16px;
  cursor: pointer;
}

.shader-entry pre {
  max-height: 240px;
  overflow: auto;
  padding: 12px;
  margin: 12px 0 0;
  border-radius: 8px;
  background: var(--book-inset);
  font: 12px/20px ui-monospace, monospace;
  tab-size: 2;
}

.shader-entry:has(details[open]) {
  grid-column: 1 / -1;
  display: grid;
  grid-template-columns: 64px minmax(0, 1fr);
  align-items: center;
  gap: 12px;
}

.shader-entry:has(details[open]) .shader-preview {
  margin: 0;
}

.shader-entry:has(details[open]) details {
  grid-column: 1 / -1;
}

.shader-credit {
  margin: 24px 0 0;
}

.shader-credit a {
  color: inherit;
  text-underline-offset: 4px;
}

#shader-book:focus-visible,
#shuffle-orbs:focus-visible,
#shader-dialog :focus-visible {
  outline: 2px solid #7193e0;
  outline-offset: 4px;
}

@media (max-width: 600px) {
  #shader-dialog {
    padding: 16px;
  }

  #shader-list {
    grid-template-columns: repeat(3, minmax(0, 1fr));
  }
}

@media (max-width: 400px) {
  #shader-list {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }
}

@media (prefers-reduced-motion: reduce) {
  #shader-book,
  #shuffle-orbs {
    transition: none;
  }
}
