:root {
  --indigo: #1B2340; --pink: #D98A7A; --gold: #F2B544; --paper: #E4E6E1; --ink: #2B2621; --pencil: #5f5b56;
  --mincho: "Zen Old Mincho", "Hiragino Mincho ProN", "Yu Mincho", "YuMincho", serif;
  --hand: "Klee One", "Klee", "Yu Gothic", "Hiragino Sans", sans-serif;
  --sat: env(safe-area-inset-top, 0px); --sab: env(safe-area-inset-bottom, 0px);
  --sal: env(safe-area-inset-left, 0px); --sar: env(safe-area-inset-right, 0px);
  --fs: 15px;
}
* {
  box-sizing: border-box; margin: 0; padding: 0;
  user-select: none; -webkit-user-select: none; -webkit-touch-callout: none; -webkit-user-drag: none;
  touch-action: none; -webkit-tap-highlight-color: transparent;
}
html, body { width: 100%; height: 100%; overflow: hidden; background: #0d1020; overscroll-behavior: none; }
body { font-family: var(--hand); color: var(--ink); position: fixed; inset: 0; }
#app { position: absolute; inset: 0; overflow: hidden; background: #0d1020; }
canvas { position: absolute; display: block; }
#forest { left: 0; top: 0; }
#paper { left: 0; }
.hidden { display: none !important; }

/* ---------- recorder push keys ---------- */
.key {
  font-family: var(--hand); font-weight: 600; font-size: var(--fs); line-height: 1.1;
  color: #e8e2d2; white-space: nowrap; cursor: pointer;
  padding: 0.62em 0.9em 0.58em;
  border: 2px solid transparent; border-radius: 3px;
  background: linear-gradient(180deg, #47423c, #2a2622 70%, #24211d) padding-box,
              linear-gradient(180deg, #d9d6cf, #8c8983 45%, #5d5a55) border-box;
  box-shadow: 0 2px 3px rgba(0, 0, 0, 0.35), inset 0 1px 0 rgba(255, 255, 255, 0.14);
  text-shadow: 0 -1px 0 rgba(0, 0, 0, 0.7);
  transition: transform 0.06s ease-out, box-shadow 0.06s ease-out;
  min-width: 0; max-width: 100%; overflow: hidden; text-overflow: clip;
}
.key:active, .key.pressed { transform: translateY(2px); box-shadow: 0 0 1px rgba(0, 0, 0, 0.5), inset 0 1px 2px rgba(0, 0, 0, 0.5); }
.key.on { transform: translateY(2px); color: var(--gold); box-shadow: 0 0 1px rgba(0, 0, 0, 0.5), inset 0 2px 3px rgba(0, 0, 0, 0.55); background: linear-gradient(180deg, #2c2925, #1f1c19) padding-box, linear-gradient(180deg, #d9d6cf, #8c8983 45%, #5d5a55) border-box; }
.key.big { font-size: calc(var(--fs) * 1.25); padding: 0.62em 1.6em 0.58em; }
.key.sq { padding: 0.45em 0.55em; line-height: 0; }
.key svg { width: 1.3em; height: 1.3em; fill: #e8e2d2; }
.key svg .ln { fill: none; stroke: #e8e2d2; stroke-width: 1.6; stroke-linecap: round; }
.pencil { font-family: var(--hand); color: var(--pencil); font-size: calc(var(--fs) * 0.85); }
.pencil.small { font-size: calc(var(--fs) * 0.78); margin-top: 0.5em; }

/* ---------- screens ---------- */
.screen { position: absolute; inset: 0; z-index: 10; }
#title { pointer-events: none; }
#title .title-head, #title .title-controls { position: absolute; left: var(--px); width: var(--pw); text-align: center; pointer-events: none; }
#title .title-head { top: calc(var(--py) + 6px); }
#title h1 { font-family: var(--mincho); font-weight: 700; color: var(--ink); font-size: clamp(22px, calc(var(--fs) * 2.1), 40px); letter-spacing: 0.08em; }
#title .sub { font-family: var(--hand); color: #66615b; font-size: calc(var(--fs) * 0.9); margin-top: 2px; transform: rotate(-1.2deg); }
#title .title-controls { bottom: calc(10px + var(--sab)); display: flex; flex-direction: column; align-items: center; gap: 7px; padding: 0 10px; }
#title .title-controls button { pointer-events: auto; }
.forests { display: flex; gap: 6px; justify-content: center; width: 100%; }
.forests .key { flex: 1 1 0; font-size: calc(var(--fs) * 0.86); padding: 0.66em 0.3em 0.6em; }
#forest-desc { min-height: 1.2em; background: rgba(228, 230, 225, 0.86); padding: 0 6px; border-radius: 2px; }
#first-note { background: rgba(228, 230, 225, 0.86); padding: 0 6px; }
.subrow { display: flex; gap: 6px; justify-content: center; flex-wrap: nowrap; width: 100%; }
.subrow .key { flex: 0 1 auto; font-size: calc(var(--fs) * 0.88); padding: 0.6em 0.7em 0.56em; }
.subrow .key.sq { padding: 0.42em 0.5em; }

#intro { left: var(--px); width: var(--pw); top: var(--py); height: var(--ph); right: auto; bottom: auto; display: flex; align-items: center; justify-content: center; padding: 0 12px; }
.slip { background: #f1f2ee; padding: 14px 18px 10px; max-width: 100%; box-shadow: 0 2px 8px rgba(0, 0, 0, 0.35); transform: rotate(-0.6deg); }
.slip p:first-child { font-family: var(--hand); font-size: calc(var(--fs) * 1.08); line-height: 1.6; color: var(--ink); }

#howto { display: flex; align-items: center; justify-content: center; background: rgba(10, 12, 26, 0.55); }
.sheet { background: var(--paper); width: min(92vw, 520px); max-height: 92vh; overflow: hidden; padding: 14px 16px 12px; box-shadow: 0 3px 12px rgba(0, 0, 0, 0.4); }
.sheet h2, .panel h2, #shelf h2 { font-family: var(--mincho); font-weight: 700; font-size: calc(var(--fs) * 1.3); color: var(--ink); text-align: center; margin-bottom: 8px; }
.sheet ol { list-style: none; display: flex; flex-direction: column; gap: 8px; }
.sheet li { display: flex; align-items: center; gap: 10px; font-size: calc(var(--fs) * 0.95); line-height: 1.45; }
.sheet li svg { flex: 0 0 auto; width: min(26vw, 110px); height: auto; }
.sheet svg .pp { fill: #f1f2ee; stroke: #9a968f; stroke-width: 1; }
.sheet svg .nd { stroke: #2b2621; stroke-width: 1; }
.sheet svg .gd { fill: none; stroke: var(--gold); stroke-width: 3; stroke-linecap: round; }
.sheet svg .pk { fill: none; stroke: var(--pink); stroke-width: 2.4; stroke-linecap: round; }
.sheet svg .bk { fill: none; stroke: #2b2621; stroke-width: 2.2; opacity: 0.8; }
.sheet svg .fg { fill: rgba(43, 38, 33, 0.18); stroke: #2b2621; stroke-width: 1; }
.sheet svg .ar { stroke: #2b2621; stroke-width: 1.2; }
.joy { text-align: center; font-family: var(--hand); font-size: calc(var(--fs) * 1.02); color: #8a5d0c; margin-top: 10px; }
.sheet .pencil.small { text-align: center; }

#pause, #vol { display: flex; align-items: center; justify-content: center; background: rgba(10, 12, 26, 0.6); }
#vol { position: absolute; inset: 0; z-index: 30; }
.panel { background: var(--paper); padding: 16px 18px; min-width: min(80vw, 300px); max-width: 92vw; display: flex; flex-direction: column; align-items: center; gap: 10px; box-shadow: 0 3px 12px rgba(0, 0, 0, 0.4); }
.panel label { display: flex; align-items: center; gap: 10px; width: 100%; font-size: var(--fs); }
.panel label span { width: 4em; }
.panel input[type=range] { flex: 1; accent-color: #8a6a2a; height: 28px; }

/* ---------- play HUD ---------- */
#hud { position: absolute; z-index: 5; display: flex; align-items: center; justify-content: center; gap: 1.4em; font-size: var(--fs); color: #e8e2d2; pointer-events: none; }
#hud-ex { display: flex; align-items: center; gap: 0.35em; }
#hud-ex.pair { color: var(--gold); }
#pair-mark { width: 2.1em; height: 1em; fill: var(--gold); }
#hud-sec { color: #f3d79a; }
.iconbtn { position: absolute; z-index: 6; width: 40px; height: 40px; border: none; background: rgba(10, 12, 22, 0.25); border-radius: 3px; cursor: pointer; }
.iconbtn svg { width: 22px; height: 22px; fill: rgba(232, 226, 210, 0.85); }
.floatlabel { position: absolute; z-index: 5; color: var(--gold); font-size: calc(var(--fs) * 0.78); pointer-events: none; white-space: nowrap; transition: opacity 1s; text-shadow: 0 0 4px rgba(0, 0, 0, 0.8); }
#hawkarc { position: absolute; z-index: 5; width: 34px; height: 34px; pointer-events: none; }
#hawkarc .ring { fill: none; stroke: rgba(232, 226, 210, 0.25); stroke-width: 3; }
#hawkarc .arc { fill: none; stroke: #e8e2d2; stroke-width: 3; stroke-linecap: round; }

/* ---------- result ---------- */
#result { display: flex; flex-direction: column; align-items: center; pointer-events: none; }
#result button { pointer-events: auto; }
.roll-wrap { position: absolute; left: var(--px); top: var(--py); width: var(--pw); height: var(--ph); display: flex; align-items: center; justify-content: center; }
#roll { position: static; max-width: 100%; max-height: 100%; box-shadow: 0 3px 10px rgba(0, 0, 0, 0.45); clip-path: inset(0 100% 0 0); }
#roll.unroll { animation: unroll 1.5s cubic-bezier(.3, .1, .2, 1) forwards; }
@keyframes unroll { from { clip-path: inset(0 100% 0 0); } to { clip-path: inset(0 0 0 0); } }
.result-text { position: absolute; left: var(--rx); top: var(--ry); width: var(--rw); text-align: center; color: #efe8d8; }
#res-line1 { font-size: calc(var(--fs) * 1.0); }
#res-line2 { color: #d8cfbd; font-size: calc(var(--fs) * 0.82); margin-top: 2px; }
.result-btns { position: absolute; left: var(--rx); width: var(--rw); bottom: calc(10px + var(--sab)); }

/* ---------- shelf ---------- */
#shelf { background: linear-gradient(180deg, #2a241f, #1c1814); display: flex; flex-direction: column; align-items: center; padding: calc(12px + var(--sat)) 12px calc(12px + var(--sab)); }
#shelf h2 { color: #e8e2d2; }
#shelf-list { flex: 1; width: 100%; max-width: 760px; overflow: hidden; position: relative; }
#shelf-list .inner { display: grid; grid-template-columns: repeat(auto-fill, minmax(96px, 1fr)); gap: 14px 10px; padding: 6px 4px 20px; }
.rollitem { position: relative; height: 112px; cursor: pointer; }
.rollitem .cyl { position: absolute; left: 8px; right: 8px; top: 8px; height: 46px; border-radius: 4px / 23px; background: linear-gradient(180deg, #c9cbc5, #eef0ea 35%, #d8dad4 60%, #a9aba5); box-shadow: 0 3px 4px rgba(0, 0, 0, 0.5); }
.rollitem .cyl::after { content: ""; position: absolute; right: -4px; top: 0; width: 12px; height: 46px; border-radius: 50%; background: radial-gradient(circle at 50% 50%, #8d8a83 0 22%, #e4e6e1 24% 100%); box-shadow: inset 0 0 2px rgba(0, 0, 0, 0.4); }
.rollitem .tag { position: absolute; left: 14px; right: 14px; top: 60px; background: #e9e1c9; padding: 3px 4px 4px; transform: rotate(-1.5deg); box-shadow: 0 1px 2px rgba(0, 0, 0, 0.5); text-align: center; }
.rollitem .tag .d { font-size: 12px; color: #3a342d; white-space: nowrap; }
.rollitem .tag .fn { font-size: 9px; color: #6b6259; white-space: nowrap; overflow: hidden; }
.rollitem .tag canvas { position: static; width: 54px; height: 14px; display: block; margin: 1px auto 0; }
.rollitem .shelfboard { position: absolute; left: -6px; right: -6px; top: 52px; height: 5px; background: #5a4634; box-shadow: 0 2px 2px rgba(0, 0, 0, 0.6); z-index: -1; }
#shelf-empty { color: #cfc6b4; margin: 20px; text-align: center; }
#shelf .subrow { margin-top: 8px; }

@media (max-height: 420px) {
  .sheet { padding: 8px 14px 6px; max-height: 96vh; }
  .sheet h2 { margin-bottom: 4px; font-size: calc(var(--fs) * 1.1); }
  .sheet ol { gap: 4px; }
  .sheet li { font-size: calc(var(--fs) * 0.85); line-height: 1.3; }
  .sheet li svg { width: 64px; }
  .joy { margin-top: 4px; }
  .sheet .pencil.small { margin-top: 2px; }
}
