:root {
  color-scheme: light;
  --paper: #fbf5ec;
  --ink: #413024;
  --edge: #d9cfc1;
  --hover: #eee5d9;
  --focus: #925b36;
  --muted: #756456;
  --control: rgba(251,245,236,.84);
  --screen-height: 100vh;
  --footer-gap: 10px;
  --top: max(7px, env(safe-area-inset-top));
  --bottom: max(7px, env(safe-area-inset-bottom));
  --left: max(7px, env(safe-area-inset-left));
  --right: max(7px, env(safe-area-inset-right));
  background: var(--paper);
  color: var(--ink);
  font-family: "PingFang SC","Microsoft YaHei UI","Microsoft YaHei","Noto Sans CJK SC",system-ui,sans-serif;
}
@supports (height:100svh) { :root { --screen-height:100svh; } }
:root[data-theme="dark"] {
  color-scheme: dark;
  --paper: #141923;
  --ink: #eaedf3;
  --edge: #465264;
  --hover: #293241;
  --focus: #c4d9f7;
  --muted: #aab6c8;
  --control: rgba(23,31,44,.88);
}
* { box-sizing:border-box; }
body {
  min-height: var(--screen-height);
  margin: 0;
  padding: var(--top) var(--right) var(--bottom) var(--left);
  display: grid;
  place-items: center;
  -webkit-tap-highlight-color: transparent;
}
main { width:100%; min-width:0; }
#viewer {
  --field-width: max(1px, calc((var(--screen-height) - var(--top) - var(--bottom) - 80px - var(--footer-gap)) * .5625));
  display:flex;
  flex-direction:column;
  align-items:center;
  width:100%;
}
.playfield { position:relative; width:min(100%,var(--field-width)); }
#nose-gutter { display:none; }
@media (pointer:coarse) and (orientation:portrait) {
  #nose-gutter {
    display:block; position:absolute; top:46%; bottom:23%; left:50%;
    width:100vw; transform:translateX(-50%); touch-action:none;
  }
}
#stage {
  position:relative;
  width:100%;
  aspect-ratio:9 / 16;
  overflow:hidden;
  isolation:isolate;
  border-radius:clamp(12px,1.32vw,23px);
  background:#b9d4da center / 100% 100% no-repeat;
  touch-action:none;
  user-select:none;
  -webkit-user-select:none;
  -webkit-touch-callout:none;
  cursor:default;
}
.backdrop, #scene, #hand-layer {
  pointer-events:none;
  display:block;
  position:absolute;
  inset:0;
  width:100%;
  height:100%;
}
.backdrop { object-fit:fill; }
#night-backdrop, :root[data-theme="dark"] #day-backdrop { visibility:hidden; }
:root[data-theme="dark"] #stage[data-ready="true"][data-stall-ready] #night-backdrop { visibility:visible; }
#touch-point {
  position:absolute; z-index:4; pointer-events:none;
  width:8px; height:8px; box-sizing:border-box; transform:translate(-50%,-50%);
  border:1.5px solid rgba(255,255,255,.95); border-radius:50%;
  box-shadow:0 0 0 1px rgba(48,33,24,.62);
}
#stage::before {
  content:""; position:absolute; inset:0; z-index:2; pointer-events:none;
  background:#b9d4da url("../assets/swatches/stage-before.webp") center / 100% 100% no-repeat;
}
:root[data-theme="dark"] #stage { background-color:#0b1019; filter:brightness(1.025); }
:root[data-theme="dark"] #stage::before { background-color:#0b1019; background-image:url("../assets/swatches/night-stage-before.webp"); }

:root[data-coat]:not([data-coat="bay"]) #stage::before { background-image:var(--coat-day-poster); }
:root[data-theme="dark"][data-coat]:not([data-coat="bay"]) #stage::before { background-image:var(--coat-night-poster); }
:root[data-coat="silver"] { --coat-day-poster:url("../assets/swatches/data-coat-silver.webp"); --coat-night-poster:url("../assets/swatches/data-coat-silver-2.webp"); }
:root[data-coat="black"] { --coat-day-poster:url("../assets/swatches/data-coat-black.webp"); --coat-night-poster:url("../assets/swatches/data-coat-black-2.webp"); }
:root[data-coat="copper"] { --coat-day-poster:url("../assets/swatches/data-coat-copper.webp"); --coat-night-poster:url("../assets/swatches/data-coat-copper-2.webp"); }
:root[data-coat="white"] { --coat-day-poster:url("../assets/swatches/data-coat-white.webp"); --coat-night-poster:url("../assets/swatches/data-coat-white-2.webp"); }
:root[data-coat="pinto"] { --coat-day-poster:url("../assets/swatches/data-coat-pinto.webp"); --coat-night-poster:url("../assets/swatches/data-coat-pinto-2.webp"); }
:root[data-theme="light"] #stage[data-ready][data-day-ready]::before,
:root[data-theme="dark"] #stage[data-ready][data-stall-ready]::before { display:none; }
:root[data-theme="dark"] #stage:not([data-stall-ready]) :is(.horse-layer,#hand-layer) { opacity:0; }
:root[data-theme="dark"] .horse-layer { filter:url(#moonlit-coat); }
:root[data-theme="dark"][data-coat="copper"] .horse-layer { filter:url(#moonlit-coat) brightness(.78); }
:root[data-theme="dark"]:is([data-coat="white"],[data-coat="pinto"]) .horse-layer { filter:url(#moonlit-coat) brightness(.85); }
:root[data-theme="dark"] #hand-layer {
  filter:url(#moonlight) brightness(.78);
}
button,a { -webkit-tap-highlight-color:transparent; }
button { color:inherit; font:inherit; touch-action:manipulation; }
button:not(:disabled), a { cursor:pointer; }
button:focus-visible, a:focus-visible, #stage:focus-visible {
  outline:3px solid var(--focus); outline-offset:2px;
}
.icon-button {
  display:grid;
  place-items:center;
  flex:none;
  width:44px;
  height:44px;
  padding:10px;
  border:0;
  border-radius:50%;
  background:transparent;
}
.icon-button svg {
  display:block; width:23px; height:23px; fill:none; stroke:currentColor;
  stroke-width:1.85; stroke-linecap:round; stroke-linejoin:round;
}
@media (hover:hover) { .icon-button:hover { background:var(--hover); } }
.icon-button:active { background:var(--hover); }
#theme {
  position:absolute; z-index:4; top:7px; right:7px;
  width:48px; height:48px; border-radius:50%; background:transparent;
  isolation:isolate;
}
#theme::before {
  content:""; position:absolute; inset:2px; z-index:-1; border-radius:50%;
  background:var(--control);
  box-shadow:inset 0 0 0 1px var(--edge);
}
#theme svg { width:21px; height:21px; stroke-width:1.65; }
@media(hover:hover) { #theme:hover::before { background:var(--paper); } }
:root[data-theme="dark"] #theme .moon, :root:not([data-theme="dark"]) #theme .sun,
#sound[aria-checked="false"] .waves, #sound[aria-checked="true"] .muted,
#expand[aria-pressed="false"] .exit, #expand[aria-pressed="true"] .full { display:none; }
.toolbar {
  position:relative;
  display:flex;
  align-items:center;
  justify-content:flex-end;
  width:min(100%, max(var(--field-width), 366px));
  gap:4px;
  padding:6px 0 0;
}
#sound[aria-checked="true"] { background:var(--hover); }
.profile { margin-top:var(--footer-gap); text-align:center; font-size:.8rem; line-height:1.3; }
.profile nav { display:flex; align-items:center; justify-content:center; gap:8px; }
.profile a {
  padding:5px 0; color:var(--muted);
  text-underline-offset:3px; text-decoration-color:#a29183;
}
.profile a:is(:hover,:focus-visible) { color:var(--ink); text-decoration-color:currentColor; }
.sr, .symbols { position:absolute; overflow:hidden; clip-path:inset(50%); width:1px; height:1px; padding:0; margin:0; }
html.is-full-page { overflow:hidden; }
html.is-full-page .profile { display:none; }
#viewer.full {
  --field-width: max(1px, calc((var(--screen-height) - var(--top) - var(--bottom) - 58px) * .5625));
  position:fixed;
  inset:0;
  z-index:10;
  width:100%;
  height:var(--screen-height);
  padding:var(--top) var(--right) var(--bottom) var(--left);
  justify-content:center;
  background:var(--paper);
}
@supports(height:100dvh) { #viewer.full { --screen-height:100dvh; } }
@media(orientation:landscape) and (max-height:500px) {
  #viewer { --field-width:max(1px,calc((var(--screen-height) - var(--top) - var(--bottom) - 76px - var(--footer-gap))*.5625)); }
  .toolbar { padding-top:3px; }
}
/* Keep the six coat samples in one row at every viewport size. */
.coat-choices {
  flex:1; min-width:0; max-width:322px; margin-right:auto;
  display:grid; grid-template-columns:repeat(6,minmax(0,1fr)); gap:2px;
}
.coat-choices button {
  position:relative; display:grid; place-items:center; width:100%; aspect-ratio:1;
  padding:3px; border:2px solid transparent; border-radius:50%; background:transparent;
  max-width:46px; justify-self:center;
}
.coat-choices button[data-pending]::before {
  content:""; position:absolute; inset:-2px; pointer-events:none;
  border:2px solid var(--edge); border-top-color:var(--focus);
  border-radius:50%; animation:scene-spin .8s linear infinite;
}
.coat-choices .swatch {
  width:100%; aspect-ratio:1; border-radius:50%;
  background:#b99a7a url("../assets/swatches/coat-choices-swatch.webp") var(--sample) center / 600% 100% no-repeat;
  box-shadow:inset 0 0 0 1px var(--edge);
}
.coat-choices button[aria-pressed="true"] { border-color:var(--ink); }
.coat-choices button[aria-pressed="true"]::after {
  content:""; position:absolute; right:-1px; bottom:0; width:13px; height:13px;
  border:2px solid var(--paper); border-radius:50%;
  background:var(--ink) url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 14 14'%3E%3Cpath d='m3 7 2.4 2.4L11 4.4' fill='none' stroke='%23fff' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'/%3E%3C/svg%3E") center/100% no-repeat;
}
:root[data-theme="dark"] .coat-choices button[aria-pressed="true"]::after { background-color:#52667a; }
@media (hover:hover) { .coat-choices button:hover { border-color:var(--muted); } }
.coat-tools { display:flex; }
@media (max-width:355px) {
  #viewer { --field-width:max(1px,calc((var(--screen-height) - var(--top) - var(--bottom) - 126px - var(--footer-gap))*.5625)); }
  #viewer.full { --field-width:max(1px,calc((var(--screen-height) - var(--top) - var(--bottom) - 104px)*.5625)); }
  .toolbar { flex-wrap:wrap; justify-content:center; gap:0; }
  .coat-choices { flex:0 1 100%; max-width:292px; margin:0 auto; }
  .coat-tools { flex:1 0 100%; justify-content:flex-end; }
}
.scene-retry{position:absolute;z-index:6;bottom:12px;right:12px;min-width:44px;min-height:44px;font:24px system-ui;border-radius:50%;border:1px solid var(--edge);color:var(--ink);background:var(--paper)}
#nose-cue { position:absolute; z-index:5; pointer-events:none; width:0; height:0; }
#nose-cue[hidden]{display:none}
#nose-cue svg{position:absolute;left:-15px;top:-15px;filter:drop-shadow(0 2px 3px #38271e99)}
#nose-cue .ripple{transform-origin:15px 15px;animation:poke-ring 1.35s ease-out infinite}
@keyframes poke-ring{0%,20%{opacity:1;transform:scale(.75)}95%,100%{opacity:.15;transform:scale(1.15)}}

#scene-status{
  position:absolute;inset:0;z-index:6;display:grid;place-items:center;
  background:var(--paper);color:var(--ink);
}
.scene-spinner{
  width:34px;height:34px;border:3px solid var(--edge);border-top-color:var(--focus);
  border-radius:50%;animation:scene-spin .8s linear infinite;
}

#read-percent {
  position:absolute; top:calc(50% + 25px); inset-inline:0; text-align:center;
  font:500 14px/1.2 system-ui; font-variant-numeric:tabular-nums; color:var(--ink);
}
#stage:is([data-scene-error],[data-backdrop-error],[data-ready]) #read-percent {display:none}
@keyframes scene-spin{to{transform:rotate(1turn)}}
#scene-status .scene-retry{position:static;display:none}
#stage:is([data-scene-error],[data-backdrop-error]) .scene-spinner{display:none}
#stage:is([data-scene-error],[data-backdrop-error]) .scene-retry{display:block}
:root[data-theme="light"] #stage[data-ready][data-day-ready]:not([data-scene-error]) #scene-status,
:root[data-theme="dark"] #stage[data-ready][data-stall-ready]:not([data-scene-error]) #scene-status{display:none}
@media (pointer:coarse){
  #nose-cue svg{overflow:visible}
  #nose-cue .tap-finger{transform-origin:15px 15px;animation:nose-tap 1.6s ease-in-out infinite}
  #nose-cue .ripple{animation:tap-wave 1.6s ease-out infinite}
}
@keyframes nose-tap{
  0%,12%,58%,100%{transform:translate(8px,-11px) scale(1.05)}
  28%,33%{transform:translate(0,0) scale(.94)}
}
@keyframes tap-wave{
  0%,26%{opacity:0;transform:scale(.55)}
  29%{opacity:1;transform:scale(.60)}
  57%,100%{opacity:0;transform:scale(1.42)}
}
