:root {
  color-scheme: light;
  font-family: "Hiragino Kaku Gothic ProN", "Yu Gothic", Meiryo, sans-serif;
  color: #382a21;
  background: #edc997;
  --ink: #44352a;
  --serif: "Hiragino Mincho ProN", "Yu Mincho", "Noto Serif CJK JP", serif;
  --accent: #af503d;
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body {
  width: 100%;
  height: 100%;
  margin: 0;
  overflow: hidden;
  overscroll-behavior: none;
  touch-action: manipulation;
}
button { font: inherit; color: inherit; -webkit-tap-highlight-color: transparent; }
button:not(:disabled) { cursor: pointer; }
button:focus-visible { outline: 2px solid var(--accent); outline-offset: 4px; }
.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  overflow: hidden;
  clip-path: inset(50%);
  white-space: nowrap;
}

.desk {
  position: relative;
  isolation: isolate;
  height: 100vh;
  height: 100svh;
  overflow: hidden;
  touch-action: none;
  user-select: none;
  background: #e9bd84;
  --desk-position: center center;
  --desk-size: cover;
  --lens-offset: 0px;
  --bare-size: 0px;
  --board-size: min(70svh, 48vw, 43rem);
  --mirror-size: min(22vw, 36svh, 19rem);
}

.world { position: absolute; inset: 0; background: #e9bd84 url("assets/morning-desk.jpg") var(--desk-position) / var(--desk-size) no-repeat; }
.world.is-reversed { transform: scaleX(-1); }
.world.is-reversed .readable { transform: scaleX(-1); }
.readable { display: inline-block; }

.desk-tools {
  position: absolute;
  top: max(1.35rem, env(safe-area-inset-top));
  right: max(1.45rem, env(safe-area-inset-right));
  display: flex;
  align-items: stretch;
  gap: 0.42rem;
  z-index: 6;
}
.settings-button {
  padding: 0.48rem 0.85rem;
  border: 1px solid #94714c8c;
  border-radius: 4px;
  background: #ffefd7;
  box-shadow: 0 2px 4px #69452426;
  font-family: var(--serif);
  font-size: 0.88rem;
}
.share-star { padding-inline: 0.62rem; color: #82531a; }
.desk.mirror-in-motion .world,
.desk.mirror-in-motion .lens-grip { pointer-events: none; }

.paper {
  position: absolute;
  top: 51%;
  left: 43%;
  width: calc(var(--board-size) + clamp(2rem, 6vw, 5rem));
  padding: clamp(0.78rem, 2.3svh, 1.65rem) clamp(0.85rem, 3vw, 2.5rem) clamp(0.44rem, 1.9svh, 1.22rem);
  transform: translate(-50%, -50%) perspective(1450px) rotateX(7deg) rotateZ(-1deg);
  transform-origin: center 65%;
  background-color: #fff9e9;
  background-image: radial-gradient(ellipse at 20% 5%, #fffefa7a, transparent 65%), radial-gradient(#8d70551c 0.4px, transparent 0.65px);
  background-size: auto, 4px 5px;
  border: 1px solid #b99b74;
  border-radius: 2px 3px 2px 3px;
  box-shadow: 9px 15px 22px #5c341a3b, 2px 4px 3px #7652373d;
}

.paper-tape {
  position: absolute;
  z-index: 1;
  left: 50%;
  top: -0.55rem;
  width: clamp(2.3rem, 7vw, 5.1rem);
  height: 0.95rem;
  opacity: 0.65;
  transform: translateX(-50%) rotate(4deg);
  background: #e9ca83bd;
  clip-path: polygon(2% 0, 100% 7%, 96% 98%, 0 88%);
  pointer-events: none;
}

.paper-header { position: relative; z-index: 2; padding-top: 0.88rem; text-align: left; }
.step-count-anchor { position: absolute; top: -0.12rem; right: 0; }
.world.is-reversed .step-count-anchor { right: auto; left: 0; }
.step-count {
  position: absolute;
  z-index: 6;
  top: var(--counter-top, 0px);
  left: var(--counter-right, 0px);
  translate: -100% 0;
  padding: 0.1rem 0.21rem;
  border-radius: 2px;
  background: #fff9e9;
  font: 700 0.72rem/1.05 var(--serif);
  font-variant-numeric: tabular-nums;
  color: #705546;
  white-space: nowrap;
  pointer-events: none;
}
.desk.mirror-in-motion .step-count { visibility: hidden; }
.paper-title {
  display: block;
  margin: 0 0 clamp(0.38rem, 1.15svh, 0.7rem);
  font: 700 clamp(1rem, 2.95svh, 1.65rem)/1.36 var(--serif);
  letter-spacing: 0.025em;
}
.world.is-reversed .paper-title { text-align: right; }
.optic-character { display: inline-block; line-height: 1; }
.instruction-area { display: flex; align-items: flex-start; gap: 0.45rem; min-height: clamp(1.75rem, 4.7svh, 2.1rem); position: relative; }
.instruction-button {
  background: #f4e5c9;
  border: 1px solid #b69465;
  padding: 0.23rem 0.56rem;
  border-radius: 4px;
  font-family: var(--serif);
  font-size: clamp(0.78rem, 1.95svh, 1rem);
  white-space: nowrap;
}
.instruction-button[aria-expanded="true"] { background: #ead6ae; }
.instruction-panel { flex: 1; min-width: 0; }
.instruction {
  display: grid;
  grid-template-columns: minmax(0, 1fr);
  padding: 0.28rem 0.45rem;
  border-radius: 4px;
  border: 1px solid #b69465;
  background: #fff4d9;
  box-shadow: 2px 4px 10px #5b381a3b;
  font: 700 clamp(0.78rem, 1.95svh, 1rem)/1.35 var(--serif);
  width: max-content;
  max-width: 100%;
  pointer-events: none;
}
.instruction > * { grid-area: 1 / 1; align-self: start; min-width: 0; }
/* Keep the paper in place while the card opens or changes mode. */
.instruction[hidden], .instruction > [hidden] { display: grid !important; visibility: hidden; }
#sound-information { display: grid; grid-template-columns: max-content minmax(0, 1fr); align-items: center; gap: 0.22rem 0.42rem; }
.sound-order { display: flex; gap: 0.23rem; align-items: center; }
.world.is-reversed .sound-order { flex-direction: row-reverse; }
.world.is-reversed .sound-order > span { transform: scaleX(-1); }
.sound-order .note-dot { width: 0.6rem; height: 0.6rem; }
.sound-order > span { color: #876b50; font-size: 0.8em; }
.sound-legend {
  grid-column: 1 / -1;
  display: grid;
  grid-template-columns: repeat(7, minmax(0, 1fr));
  gap: 0.18rem;
  margin: 0;
  padding: 0.22rem 0 0;
  border-top: 1px solid #b6946560;
  list-style: none;
}
.sound-legend li {
  display: grid;
  justify-items: center;
  gap: 0.14rem;
  font-size: 0.68rem;
  white-space: nowrap;
}
.sound-legend .note-dot { width: 0.53rem; height: 0.53rem; }
.sound-instruction-note { margin: 0; font-size: 0.68rem; line-height: 1.2; }
.maze-area { display: flex; justify-content: center; padding-top: 0; }
.board-ruler { position: relative; width: var(--board-size); margin: clamp(0.8rem, 2.8svh, 1.26rem) 0 clamp(0.8rem, 2.75svh, 1.26rem); }
.opening-label {
  position: absolute;
  z-index: 2;
  color: #705546;
  font: 700 clamp(0.58rem, 1.63svh, 0.88rem)/1.1 var(--serif);
  letter-spacing: 0.03em;
  white-space: nowrap;
  pointer-events: none;
}
.start-label { left: 15%; bottom: calc(100% + 0.2rem); translate: -50% 0; }
.exit-label { left: 55%; top: calc(100% + 0.19rem); translate: -50% 0; }

.board {
  --cols: 10;
  --rows: 10;
  position: relative;
  display: grid;
  width: 100%;
  aspect-ratio: 1 / 1;
  grid-template: repeat(var(--rows), minmax(0, 1fr)) / repeat(var(--cols), minmax(0, 1fr));
  background: #fffae9b0;
  touch-action: none;
  cursor: default;
}
.cell {
  position: relative;
  display: grid;
  place-items: center;
  outline: 0;
  min-width: 0;
  min-height: 0;
  font: 700 clamp(0.72rem, calc(var(--board-size) * 0.052), 2rem)/1 var(--serif);
}
.cell.start, .cell.exit { cursor: pointer; }
.cell.start:focus-visible { outline: 2px solid var(--accent); outline-offset: -4px; }
.cell::after {
  content: "";
  position: absolute;
  z-index: 2;
  inset: 0;
  border-top: var(--wt, 0px) solid var(--ink);
  border-right: var(--wr, 0px) solid var(--ink);
  border-bottom: var(--wb, 0px) solid var(--ink);
  border-left: var(--wl, 0px) solid var(--ink);
  pointer-events: none;
}
.cell {
  --grid-h: inset 0 -1px 0 #98795627;
  --grid-v: inset -1px 0 0 #98795627;
  box-shadow: var(--grid-h), var(--grid-v);
}
.cell:nth-child(10n) { --grid-v: inset 0 0 0 transparent; }
.cell:nth-last-child(-n+10) { --grid-h: inset 0 0 0 transparent; }
.glyph { z-index: 1; pointer-events: none; }
.cell.trace-picked .glyph { color: #9b3427; }
.trace-line {
  position: absolute;
  z-index: 0;
  inset: 0;
  display: none;
  pointer-events: none;
  --pencil: #5675669e;
}
.cell.traced .trace-line { display: block; }
.board.route-confirmed .trace-line { --pencil: #5b8062be; }
.trace-line::before, .trace-line i { position: absolute; background: var(--pencil); }
.trace-line::before {
  content: "";
  left: 43%;
  top: 43%;
  width: 14%;
  height: 14%;
  border-radius: 50%;
}
.trace-line i { display: none; }
.trace-n .segment-n, .trace-e .segment-e, .trace-s .segment-s, .trace-w .segment-w { display: block; }
.segment-n { left: 43%; top: -1px; bottom: 50%; width: 14%; }
.segment-s { left: 43%; top: 50%; bottom: -1px; width: 14%; }
.segment-e { top: 43%; left: 50%; right: -1px; height: 14%; }
.segment-w { top: 43%; right: 50%; left: -1px; height: 14%; }

.note-trigger {
  z-index: 1;
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
  margin: 0;
  padding: 0;
  border: 0;
  background: none;
}
.note-dot { display: inline-block; flex-shrink: 0; border-radius: 50%; background: var(--pitch); }
.note-trigger .note-dot {
  height: 42%;
  width: 42%;
  border: 1px solid #46342522;
  box-shadow: 0 1px 1px #4839261d;
  transition: scale 100ms ease, opacity 100ms ease;
}
.note-trigger:active .note-dot, .note-trigger.sounding .note-dot { scale: 1.17; opacity: 0.84; }
.note-dot[data-pitch="do"] { --pitch: #ba3f36; }
.note-dot[data-pitch="re"] { --pitch: #cf7640; }
.note-dot[data-pitch="mi"] { --pitch: #d0a032; }
.note-dot[data-pitch="fa"] { --pitch: #658851; }
.note-dot[data-pitch="so"] { --pitch: #397e75; }
.note-dot[data-pitch="la"] { --pitch: #526d9b; }
.note-dot[data-pitch="si"] { --pitch: #876c94; }

.mirror {
  position: absolute;
  z-index: 1;
  right: clamp(1.5rem, 9.2vw, 12rem);
  top: 50%;
  width: var(--mirror-size);
  aspect-ratio: 0.805;
  padding: 0;
  border: 0;
  border-radius: 45%;
  background: none;
  transform: translateY(-53%) perspective(900px) rotateY(-9deg) rotateZ(2.2deg) rotateX(-4deg);
  transform-origin: center 87%;
}
.mirror-stand {
  position: absolute;
  left: 25%;
  right: 25%;
  bottom: 2%;
  height: 17%;
  background: linear-gradient(100deg, #a26737, #ebc992 35%, #9b6535 73%, #71441d);
  border-radius: 36% 45% 44% 41%;
  box-shadow: 2px 7px 9px #5e351d42, inset 0 -5px 0 #85502499;
}
.mirror-stand::before {
  content: "";
  position: absolute;
  left: 39%;
  right: 39%;
  top: -48%;
  height: 80%;
  background: linear-gradient(90deg, #9d6937, #e7b87b, #885025);
  border-radius: 40%;
}
.mirror-frame {
  position: absolute;
  inset: 0 0 13%;
  display: block;
  border-radius: 50% 48% 49% 46%;
  background: #bd875b url("assets/morning-desk.jpg") center 83% / auto 290%;
  background-blend-mode: multiply;
  box-shadow: inset 0 0 0 1px #714319bc, inset 0 2px 3px #fff1be, inset -1px -2px 6px #684321c2, 4px 6px 10px #53341338;
}
.mirror-glass {
  position: absolute;
  inset: 4.8% 5.5% 5.6%;
  display: block;
  overflow: hidden;
  border-radius: inherit;
  background: #e2ddd0;
  box-shadow: 0 0 0 1px #805727c2;
}
.portrait { width: 100%; height: 100%; object-fit: cover; display: block; }
.reflection-glasses {
  position: absolute;
  left: var(--eye-x, 50%);
  top: var(--eye-y, 46%);
  width: var(--eye-width, 50%);
  height: 14.5%;
  z-index: 1;
  translate: -50% -50%;
  transform: translateY(var(--reflection-y, 0%)) rotate(var(--eye-angle, -1deg));
  pointer-events: none;
  color: #453429e5;
}
.spectacle {
  position: absolute;
  top: 0;
  width: 44%;
  height: 100%;
  border: clamp(1px, 0.24vw, 3px) solid currentColor;
  border-radius: 48% 51% 46% 46%;
  background: #fff8e510;
}
.spectacle-left { left: 0; }
.spectacle-right { right: 0; }
.spectacle-bridge { position: absolute; inset: 35% 43% auto; border-top: clamp(1px, 0.17vw, 2px) solid currentColor; border-radius: 70% 70% 0 0; height: 20%; }
.glass-light {
  position: absolute;
  inset: 0;
  z-index: 2;
  background: linear-gradient(116deg, #ffffef54, transparent 35%, transparent 73%, #fffef024);
  box-shadow: inset 0 0 0 1px #fffffa7a, inset 0 0 9px #50381e40;
  pointer-events: none;
}

.bare-band {
  position: absolute;
  z-index: 4;
  inset: 0 0 auto;
  height: var(--bare-size);
  pointer-events: none;
  backdrop-filter: blur(1.4px) saturate(0.92);
  -webkit-backdrop-filter: blur(1.4px) saturate(0.92);
  background: #d0ac8116;
}
.bare-band.is-below { inset: auto 0 0; }
.lens {
  --frame-edge: clamp(3px, 0.51vw, 8px);
  position: absolute;
  z-index: 5;
  inset: calc(var(--visible-top, 0px) + env(safe-area-inset-top, 0px) + 10px) 0
    calc(var(--visible-bottom, 0px) + env(safe-area-inset-bottom, 0px) + 10px);
  pointer-events: none;
}
.lens::before {
  content: "";
  position: absolute;
  inset: var(--lens-offset) 0 calc(0px - var(--lens-offset));
  border: var(--frame-edge) solid #4b3428ec;
  border-radius: clamp(9px, 2vw, 24px);
  box-shadow: inset 0 0 0 1px #fff0c9a8, inset 0 0 25px #573b2530, 0 0 0 1px #56392480;
}
.lens-grip {
  position: absolute;
  inset-inline: clamp(7px, 1.2vw, 18px);
  height: 30px;
  padding: 0;
  border: 0;
  border-radius: 0;
  background: none;
  cursor: ns-resize;
  pointer-events: auto;
  touch-action: none;
}
.lens-grip-top { top: calc(var(--frame-edge) / 2 - 11px + var(--lens-offset)); }
.lens-grip-bottom { bottom: calc(var(--frame-edge) / 2 - 11px - var(--lens-offset)); }

.modal {
  max-height: calc(100svh - 1.5rem);
  overflow: auto;
  overscroll-behavior: contain;
  touch-action: manipulation;
  width: min(calc(100% - 2rem), 21rem);
  border: 1px solid #9d7655;
  border-radius: 8px;
  padding: 1.2rem 1.35rem 1.35rem;
  color: var(--ink);
  background: #fff6e2;
  box-shadow: 0 10px 35px #462c1e54;
}
.modal::backdrop { background: #34201565; }
.modal-heading { display: flex; justify-content: space-between; align-items: center; gap: 1rem; margin-bottom: 0.85rem; }
.modal h2 { margin: 0; font: 700 1.35rem/1.45 var(--serif); }
.close { display: grid; place-items: center; width: 2rem; height: 2rem; border: 0; border-radius: 3px; background: transparent; font-size: 1.5rem; line-height: 1; }
.close:hover { background: #e8d5b5; }
.modal-button { width: 100%; display: block; padding: 0.55rem; margin-top: 0.45rem; background: #f9ecd4; border: 1px solid #b79369; border-radius: 3px; }
.ending { text-align: center; padding-block: 2.8rem 2.3rem; }
.ending .close { position: absolute; right: 0.55rem; top: 0.4rem; }

@media (min-aspect-ratio: 1/1) and (max-aspect-ratio: 5/4) {
  .paper { left: 42%; }
  .mirror { right: 5.5vw; }
}
@media (max-aspect-ratio: 1/1), (max-width: 690px) and (orientation: portrait) {
  .desk {
    --board-size: min(83vw, 63svh, 37rem);
    --mirror-size: min(26vw, 17.4svh, 9rem);
    --desk-position: center bottom;
    --desk-size: auto 108%;
  }
  .paper {
    top: 55.7%;
    left: 50%;
    z-index: 2;
    width: calc(var(--board-size) + 1.62rem);
    padding-inline: 0.76rem;
    transform: translate(-50%, -50%) perspective(1100px) rotateX(5.5deg) rotateZ(-0.75deg);
  }
  .paper-title { font-size: clamp(0.94rem, 3.8vw, 1.45rem); }
  .mirror { top: max(3.05rem, calc(env(safe-area-inset-top) + 2.25rem)); right: max(1.2rem, 6.5vw); transform: perspective(760px) rotateY(-7deg) rotateZ(2deg) rotateX(-4deg); }
  .board-ruler { margin-block: clamp(0.67rem, 2.35svh, 1.28rem); }
  .desk-tools { top: max(0.75rem, env(safe-area-inset-top)); right: max(1.2rem, env(safe-area-inset-right)); }
}
@media (max-aspect-ratio: 1/1) and (max-height: 730px) {
  .desk { --board-size: min(82vw, 54svh, 37rem); --mirror-size: min(25vw, 14.1svh, 8rem); }
  .paper { top: 57.9%; }
}
@media (orientation: landscape) and (max-height: 520px) {
  .desk { --board-size: min(64svh, 48vw); --mirror-size: min(19vw, 45svh); }
  .paper { left: 45%; width: max(calc(var(--board-size) + 1.5rem), min(55vw, 18rem)); padding: 0.55rem 0.68rem 0.2rem; }
  .paper-title { font-size: 0.91rem; letter-spacing: 0; }
  .board-ruler { margin-block: 0.66rem 0.57rem; }
  .instruction-area { min-height: 1.4rem; }
  .instruction-button { padding: 0.13rem 0.42rem; font-size: 0.72rem; }
  .desk-tools { top: 0.82rem; right: 0.9rem; }
  .mirror { right: 7vw; }
}
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { transition-duration: 0s !important; }
}

.modal button, .modal a { touch-action: manipulation; }
.cell.exit:focus-visible { outline: 2px solid var(--accent); outline-offset: -4px; }
.volume-label { display: flex; align-items: center; justify-content: space-between; gap: 0.7rem; font-size: 0.86rem; }
.volume-label output { font-variant-numeric: tabular-nums; }
.volume-row { display: flex; align-items: center; gap: 0.6rem; margin: 0.3rem 0 0.8rem; }
.volume-row input { margin: 0; min-width: 0; flex: 1; accent-color: #9c6747; }
.volume-row button { width: auto; margin: 0; white-space: nowrap; }
.volume-row button[aria-pressed="true"] { background: #8d5e40; color: #fff5dc; }
.settings-links { display: grid; gap: 0.48rem; margin: 0.3rem 0 0.8rem; }
.settings-links a { color: #70452e; font-size: 0.89rem; }
.settings-links small { display: block; margin: 0.22rem 0 0; color: #685245; font-size: 0.75rem; line-height: 1.4; }
.panel-note { margin: 0.8rem 0 0; color: #685245; font-size: 0.75rem; line-height: 1.45; }
.record-totals { display: grid; gap: 0.6rem; font-variant-numeric: tabular-nums; }
.record-totals div { display: flex; justify-content: space-between; gap: 0.5rem; }
.record-totals dd { margin: 0; font-weight: 700; }
.share-button { display: inline-block; width: auto; margin-top: 1rem; padding-inline: 1rem; color: #70452e; text-decoration: none; }
.connection-status {
  position: absolute; z-index: 2; right: 0.7rem; bottom: 0.65rem; margin: 0;
  max-width: calc(100% - 1.4rem); padding: 0.22rem 0.4rem; font-size: 0.7rem;
  background: #fff1d9; color: #873e32;
}
.world.is-reversed .connection-status { transform: scaleX(-1); }
@media (max-height: 370px) {
  .modal { padding-block: 0.65rem; }
  .modal-heading { margin-bottom: 0.45rem; }
  .settings-links { gap: 0.28rem; }
  .desk { --board-size: min(50svh, 79vw); }
}
@media (max-height: 370px) and (orientation: landscape) {
  .desk { --board-size: min(56svh, 48vw); }
}
@media (max-height: 370px) and (orientation: portrait) {
  .paper { top: 52%; }
}
