/* Pet the Rat — layout and controls */
:root {
  color-scheme: light;
  --bg: #fbf5ec; --text: #413024; --line: #d9cfc1; --ring: #925b36; --soft: #eee5d9;
  --quiet: #756456; --tick: #413024; --stage-bg: #ddcbb2; --chip: rgba(251, 245, 236, .81);
  --pad: clamp(7px, .66vw, 13px);
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  color: var(--text); background: var(--bg);
}
:root[data-theme=dark] {
  color-scheme: dark;
  --bg: #101722; --text: #dfebf3; --ring: #b4d9fa; --line: #526276; --soft: #293546;
  --tick: #425c75; --quiet: #a0afbf; --stage-bg: #303e50; --chip: rgba(18, 29, 43, .82);
}
*, *::before, *::after { box-sizing: border-box; }
body { margin: 0; -webkit-tap-highlight-color: transparent; }
main { max-width: 1920px; margin: 0 auto; padding: var(--pad) var(--pad) max(var(--pad), env(safe-area-inset-bottom)); }
button { font: inherit; color: inherit; cursor: pointer; touch-action: manipulation; }
button[hidden] { display: none; }
button:focus-visible, #stage:focus-visible { outline: 3px solid var(--ring); outline-offset: 2px; }
.visually-hidden, .sprites { position: absolute; width: 1px; height: 1px; margin: 0; overflow: hidden; clip-path: inset(50%); }

/* stage */
#viewer { position: relative; margin: 0 auto; max-width: min(100%, calc((100svh - 102px) * 1.62246)); }
#stage {
  position: relative; overflow: hidden; aspect-ratio: 1517 / 935; max-height: calc(100svh - 102px);
  border-radius: clamp(12px, 1.32vw, 25px); background: var(--stage-bg);
  touch-action: none; user-select: none; -webkit-user-select: none;
}
#scene { position: absolute; inset: 0; display: block; width: 100%; height: 100%; }
#stage:not([data-ready=true]) #scene { visibility: hidden; }
#stage[data-over=true] { cursor: default; }
#stage[data-pressed=true] { cursor: none; }
#retry { position: absolute; left: 14px; bottom: 14px; background: var(--bg); }

/* round icon buttons */
.round-button { display: grid; place-items: center; width: 44px; height: 44px; padding: 10px; border: 0; border-radius: 50%; background: transparent; }
.round-button svg { width: 23px; height: 23px; fill: none; stroke: currentColor; stroke-width: 1.85; stroke-linecap: round; stroke-linejoin: round; }
#theme {
  position: absolute; z-index: 2; top: max(9px, env(safe-area-inset-top)); right: max(9px, env(safe-area-inset-right));
  background-color: var(--chip); box-shadow: inset 0 0 0 1px var(--line);
}
#theme svg { width: 21px; height: 21px; stroke-width: 1.65; }
:root[data-theme=dark] #theme .moon, :root:not([data-theme=dark]) #theme .sun { display: none; }
#sound[aria-checked=false] .waves, #sound[aria-checked=true] .muted { display: none; }
#sound[aria-checked=true], #proportions-toggle[aria-expanded=true] { background: var(--soft); }
@media (hover: hover) {
  .round-button:hover { background-color: var(--soft); }
  #theme:hover { background-color: var(--bg); }
  .swatches button:hover:not([aria-pressed=true]) { border-color: #bbaa98; }
}

/* toolbar and colour swatches */
.toolbar, .swatches, .tools { display: flex; align-items: center; }
.toolbar { justify-content: space-between; gap: 4px 12px; padding-top: 8px; }
.swatches { min-width: 0; max-width: 100%; justify-content: center; gap: clamp(4px, .65vw, 10px); }
.swatches button {
  --size: clamp(46px, 6.4vw, 60px); position: relative; display: grid; min-width: 0; width: var(--size); aspect-ratio: 1;
  padding: 3px; border: 2px solid transparent; border-radius: 50%; background: transparent;
}
.swatches button[aria-pressed=true] { border-color: var(--text); }
.swatches button[aria-pressed=true]::after {
  content: ""; position: absolute; right: 0; bottom: 1px; width: 13px; height: 13px; border: 2px solid var(--bg); border-radius: 50%;
  background: var(--tick) url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 16 16'%3E%3Cpath d='m4.2 8.2 2.6 2.6 5-5.4' fill='none' stroke='%23fff' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'/%3E%3C/svg%3E") center / 100% no-repeat;
}
.coat { width: 100%; height: 100%; border-radius: inherit; background: #c9beb2 center / cover no-repeat; box-shadow: inset 0 0 0 1px #33281e3b; }
:root[data-theme=dark] .coat { box-shadow: inset 0 0 0 1px #b8c9df48; }
[data-rat=kuro] .coat { background-image: url(../img/rats/kuro-swatch.webp); }
[data-rat=mugi] .coat { background-image: url(../img/rats/mugi-swatch.webp); }
[data-rat=hana] .coat { background-image: url(../img/rats/hana-swatch.webp); }
[data-rat=shiro] .coat { background-image: url(../img/rats/shiro-swatch.webp); }
[data-rat=saba] .coat { background-image: url(../img/rats/saba-swatch.webp); }
[data-rat=hachi] .coat { background-image: url(../img/rats/hachi-swatch.webp); }
[data-rat=sabi] .coat { background-image: url(../img/rats/sabi-swatch.webp); }
[data-rat=gray] .coat { background-image: url(../img/rats/gray-swatch.webp); }
[data-rat=kiji] .coat { background-image: url(../img/rats/kiji-swatch.webp); }
[data-rat=siam] .coat { background-image: url(../img/rats/siam-swatch.webp); }
.tools { position: relative; flex: none; gap: 3px; }

/* size panel */
#proportions-panel {
  position: absolute; z-index: 3; right: 0; bottom: calc(100% + 6px); width: 264px; max-width: calc(100vw - 24px);
  padding: 7px 9px; color: var(--text); background: var(--bg); border: 1px solid var(--line); border-radius: 16px; box-shadow: 0 4px 18px #21170c24;
}
.slider-row { position: relative; display: grid; grid-template-columns: 32px minmax(0, 1fr) 32px; align-items: center; gap: 3px; }
.slider-row::after { content: ""; position: absolute; left: 50%; bottom: 6px; width: 2px; height: 5px; transform: translateX(-50%); border-radius: 1px; background: var(--quiet); pointer-events: none; }
.slider-row svg { width: 32px; height: 32px; fill: currentColor; pointer-events: none; }
#proportions-panel input { display: block; width: 100%; min-width: 0; height: 42px; margin: 0; accent-color: var(--ring); touch-action: none; cursor: pointer; }
#proportions-panel input:focus-visible { outline: 2px solid var(--ring); outline-offset: 1px; border-radius: 4px; }

/* footer */
.credits { text-align: center; font-size: .8rem; color: var(--quiet); }
.credits a, .credits [aria-current=page] { display: inline-block; padding: 5px 8px; }
.credits a { color: var(--quiet); text-decoration: underline; text-underline-offset: 3px; text-decoration-color: #a29183; }
.credits a:is(:hover, :focus-visible) { color: var(--text); text-decoration-color: currentColor; }

/* full screen */
#viewer:fullscreen, #viewer.is-full { display: flex; flex-direction: column; justify-content: center; width: 100%; height: 100%; max-width: none; padding: var(--pad); background: var(--bg); }
#viewer.is-full { position: fixed; inset: 0; z-index: 9; }
.is-full-page { overflow: hidden; }
#viewer:fullscreen #stage, #viewer.is-full #stage { width: 100%; min-height: 0; max-height: calc(100dvh - 92px); }

/* phones */
@media (min-width: 526px) { .tools { margin-inline-start: auto; } }
@media (max-width: 620px) { #stage { aspect-ratio: 1.56; } }
@media (max-width: 525px) and (orientation: portrait) {
  .toolbar { flex-direction: column; justify-content: center; gap: 2px; }
  .swatches { gap: 4px; }
  #viewer:not(.landscape) .swatches { max-width: min(100%, 316px); }
  #viewer:not(.landscape) .swatches button { --size: clamp(52px, 15vw, 60px); }
  #viewer:not(.landscape) #proportions-panel { right: 50%; transform: translateX(50%); }
}
@media (orientation: portrait) {
  #viewer:not(.landscape) .toolbar, #viewer:not(.landscape) .swatches { flex-wrap: wrap; }
}
@media (orientation: landscape) and (max-height: 540px) {
  .swatches button { --size: clamp(26px, calc((100vw - 202px) / 14), 48px); }
  .swatches { gap: clamp(2px, calc(3vw - 15px), 4px); }
  .toolbar { column-gap: 4px; }
  #viewer:not(.landscape):not(.is-full):not(:fullscreen) .toolbar {
    position: relative; left: 50%; transform: translateX(-50%); width: max-content; flex-flow: row nowrap; justify-content: center;
    max-width: calc(100vw - 2 * max(var(--pad), env(safe-area-inset-left), env(safe-area-inset-right)));
  }
}
@media (orientation: landscape) and (any-pointer: coarse) {
  #viewer:not(.landscape):not(.is-full):not(:fullscreen) { max-width: min(100%, calc((100svh - 94px) * 2)); }
  #stage { --top-crop: .1; aspect-ratio: 2 / 1; max-height: calc(100svh - 94px); }
}

/* rotated "landscape" mode on phones */
.playfield { display: contents; }
#viewer.landscape {
  position: fixed; inset: 0 auto auto 0; z-index: 9; display: grid; grid-template-columns: minmax(0, 1fr); grid-template-rows: minmax(0, 1fr) auto;
  width: 100vw; width: 100dvw; height: 100vh; height: 100dvh; max-width: none; margin: 0; background: var(--bg);
  padding: max(var(--pad), env(safe-area-inset-top)) max(var(--pad), env(safe-area-inset-right)) max(var(--pad), env(safe-area-inset-bottom)) max(var(--pad), env(safe-area-inset-left));
}
.landscape .playfield { display: grid; place-items: center; min-height: 0; container-type: size; }
#viewer.landscape #stage { --top-crop: .1; aspect-ratio: 2 / 1; width: min(100cqw, 200cqh); max-height: none; }
.landscape .toolbar { flex-flow: row nowrap; justify-content: space-between; gap: 4px; padding-top: 4px; }
.landscape .swatches { gap: clamp(2px, calc(3vmax - 15px), 4px); }
.landscape .swatches button { --size: clamp(26px, calc((100vmax - 202px) / 14), 48px); }
@media (orientation: portrait) {
  #viewer.landscape {
    width: 100vh; width: 100dvh; height: 100vw; height: 100dvw; transform-origin: 0 0;
    transform: translateX(100vw) rotate(90deg); transform: translateX(100dvw) rotate(90deg);
    padding: max(var(--pad), env(safe-area-inset-right)) max(var(--pad), env(safe-area-inset-bottom)) max(var(--pad), env(safe-area-inset-left)) max(var(--pad), env(safe-area-inset-top));
  }
}
.credits .site-links { margin-top: .15rem; }
.credits .site-links a { padding: 4px 6px; }
