:root {
  --ink: #23201c;
  --ink-2: #5c554c;
  --ink-3: #8b8279;
  --paper: #f2ede4;
  --paper-2: #fbf8f2;
  --line: #ddd4c6;
  --accent: #a8552a;
  --shadow: 0 1px 2px rgba(35,32,28,.06), 0 8px 28px rgba(35,32,28,.10);
  --safe-t: env(safe-area-inset-top, 0px);
  --safe-b: env(safe-area-inset-bottom, 0px);
  --panel-w: min(430px, 38vw);
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  background: var(--paper);
  color: var(--ink);
  font: 400 16px/1.6 ui-serif, "Iowan Old Style", "Source Serif 4", Georgia, "Noto Serif TC", serif;
  overflow: hidden;
  overscroll-behavior: none;
  -webkit-font-smoothing: antialiased;
}
h1, h2, h3, .tag, button { font-family: ui-sans-serif, system-ui, "Inter", "Helvetica Neue", "Noto Sans TC", sans-serif; }

#stage { position: fixed; inset: 0; width: 100%; height: 100%; display: block; touch-action: none; }

/* ---------------------------------------------------------- masthead */
#masthead {
  position: fixed; z-index: 3;
  top: calc(var(--safe-t) + 26px); left: 34px; right: 34px;
  pointer-events: none;
  transition: opacity .38s ease, transform .38s ease;
}
#masthead h1 {
  margin: 0; max-width: 12em; font-size: clamp(27px, 4.4vw, 46px); font-weight: 620;
  letter-spacing: -.02em; line-height: 1;
}
#masthead .sub {
  margin: .7em 0 0; max-width: 30em; color: var(--ink-2);
  font-size: clamp(14px, 1.5vw, 17px);
}
body.focus #masthead { opacity: 0; transform: translateY(-10px); }

/* ---------------------------------------------------------- 3D labels */
#labels { position: fixed; inset: 0; z-index: 2; pointer-events: none; }
.lbl {
  position: absolute; transform: translate(-50%, 0);
  text-align: center; white-space: nowrap;
  transition: opacity .3s ease;
}
.lbl b {
  display: block; font-family: ui-sans-serif, system-ui, sans-serif;
  font-size: 14px; font-weight: 600; letter-spacing: -.005em; color: var(--ink);
}
.lbl i {
  display: block; font-size: 12px; font-style: italic; color: var(--ink-3);
  margin-top: 1px;
}
.lbl u {
  display: block; margin: 6px auto 0; width: 1px; height: 14px;
  background: linear-gradient(var(--line), transparent); text-decoration: none;
}
/* tag visibility is owned by app.js: inline opacity outranks a rule here */

/* ---------------------------------------------------------- buttons */
.chrome-btn, #back, #mute, #credits-toggle, .replay {
  -webkit-tap-highlight-color: transparent;
}
#back, #mute {
  position: fixed; z-index: 6;
  display: inline-flex; align-items: center; gap: 7px;
  padding: 9px 15px 9px 11px;
  background: var(--paper-2); color: var(--ink);
  border: 1px solid var(--line); border-radius: 999px;
  font-size: 13.5px; font-weight: 550; cursor: pointer;
  box-shadow: var(--shadow);
  transition: opacity .3s ease, transform .3s ease, background .15s ease;
}
#back:hover, #mute:hover { background: #fff; }
#back svg, #mute svg { width: 17px; height: 17px; fill: none; stroke: currentColor; stroke-width: 1.9; stroke-linecap: round; stroke-linejoin: round; }
#back { top: calc(var(--safe-t) + 22px); left: 26px; opacity: 0; transform: translateX(-8px); pointer-events: none; }
body.focus #back { opacity: 1; transform: none; pointer-events: auto; }
#mute { top: calc(var(--safe-t) + 22px); right: 26px; padding: 9px 11px; }
#mute .off { display: none; }
#mute[aria-pressed="true"] .on { display: none; }
#mute[aria-pressed="true"] .off { display: block; }
#mute[aria-pressed="true"] { color: var(--ink-3); }

/* ---------------------------------------------------------- panel */
#panel {
  position: fixed; z-index: 5; top: 0; right: 0;
  width: var(--panel-w); height: 100%;
  background: var(--paper-2);
  border-left: 1px solid var(--line);
  overflow-y: auto; overscroll-behavior: contain;
  transform: translateX(100%);
  transition: transform .5s cubic-bezier(.22,.7,.25,1);
  -webkit-overflow-scrolling: touch;
}
body.focus #panel { transform: none; }
.panel-inner { padding: calc(var(--safe-t) + 68px) 34px calc(var(--safe-b) + 48px); }
.grab { display: none; }
#panel .tag {
  margin: 0 0 .8em; display: inline-block;
  padding: 3px 10px; border-radius: 999px;
  background: #efe6d8; color: var(--accent);
  font-size: 11px; font-weight: 600; letter-spacing: .08em; text-transform: uppercase;
}
#panel h2 { margin: 0; font-size: 30px; font-weight: 620; letter-spacing: -.02em; line-height: 1.12; }
#panel .latin { margin: .25em 0 0; font-style: italic; color: var(--ink-2); font-size: 16px; }
#panel .aka { margin: .5em 0 0; color: var(--ink-3); font-size: 13.5px; font-family: ui-sans-serif, system-ui, sans-serif; }
#panel .aka:empty { display: none; }
#panel .size {
  margin: 1em 0 0; padding-top: .9em; border-top: 1px solid var(--line);
  color: var(--ink-2); font-size: 14px; font-family: ui-sans-serif, system-ui, sans-serif;
}
.replay {
  margin: 18px 0 4px; display: inline-flex; align-items: center; gap: 8px;
  padding: 10px 18px 10px 14px; cursor: pointer;
  background: var(--ink); color: var(--paper-2);
  border: 0; border-radius: 999px; font-size: 14px; font-weight: 560;
  transition: background .15s ease, transform .12s ease;
}
.replay:hover { background: #3a352e; }
.replay:active { transform: scale(.97); }
.replay svg { width: 17px; height: 17px; fill: none; stroke: currentColor; stroke-width: 1.9; stroke-linecap: round; stroke-linejoin: round; }
#panel section { margin-top: 30px; }
#panel h3 {
  margin: 0 0 .7em; font-size: 11px; font-weight: 650;
  letter-spacing: .14em; text-transform: uppercase; color: var(--ink-3);
}
#panel p { margin: 0; color: var(--ink); }
#panel dl { margin: 0; }
#panel dt {
  font-family: ui-sans-serif, system-ui, sans-serif;
  font-size: 12px; font-weight: 650; letter-spacing: .06em;
  text-transform: uppercase; color: var(--accent); margin-top: 1.1em;
}
#panel dt:first-child { margin-top: 0; }
#panel dd { margin: .25em 0 0; }
#panel .note { padding: 18px 20px; background: #f2ebdf; border-radius: 12px; }
#panel .note h3 { color: var(--ink-2); }

/* ---------------------------------------------------------- credits */
#credits {
  position: fixed; z-index: 4; left: 26px; bottom: calc(var(--safe-b) + 20px);
  max-width: min(520px, calc(100vw - 52px));
  font-size: 12.5px; color: var(--ink-2);
}
#credits-toggle {
  background: none; border: 0; padding: 4px 0; cursor: pointer;
  color: var(--ink-3); font-size: 11.5px; font-weight: 600;
  letter-spacing: .1em; text-transform: uppercase;
  border-bottom: 1px solid var(--line);
}
#credits-toggle:hover { color: var(--ink); }
#credits-body { margin-top: 10px; }
#credits-body p { margin: 0 0 .5em; }
#credits ul { margin: 0 0 .6em; padding-left: 1.1em; }
#credits li { margin: .2em 0; }
#credits a { color: var(--ink-2); }
#credits .small { color: var(--ink-3); font-size: 11.5px; }
body.focus #credits { opacity: 0; pointer-events: none; }

/* ---------------------------------------------------------- loading */
#loading {
  position: fixed; inset: 0; z-index: 20; display: grid; place-content: center;
  gap: 14px; justify-items: center; background: var(--paper);
  transition: opacity .5s ease; text-align: center;
}
#loading.done { opacity: 0; pointer-events: none; }
#loading p { margin: 0; color: var(--ink-3); font-size: 13px; letter-spacing: .04em; }
.bar { width: 160px; height: 2px; background: var(--line); overflow: hidden; border-radius: 2px; }
.bar i { display: block; height: 100%; width: 0; background: var(--accent); transition: width .3s ease; }


/* ------------------------------------------------- onboarding coach marks */
/* One gesture at a time. Each card clears the moment the reader performs
   that gesture, then the next appears; the bar at the bottom is permanent. */
#coach {
  position: fixed; z-index: 7; top: 50%; left: 50%;
  transform: translate(-50%, -50%);
  display: flex; flex-direction: column; align-items: center; gap: 14px;
  padding: 26px 40px 22px; border-radius: 20px;
  background: rgba(251, 248, 242, .93);
  -webkit-backdrop-filter: blur(10px); backdrop-filter: blur(10px);
  border: 1px solid var(--line); color: var(--ink);
  box-shadow: 0 18px 52px rgba(35, 32, 28, .16);
  pointer-events: none; white-space: nowrap;
  transition: opacity .28s ease, transform .28s ease;
}
#coach .txt {
  font-family: ui-sans-serif, system-ui, sans-serif;
  font-size: 19px; font-weight: 560; letter-spacing: .01em;
}
#coach .txt b { color: var(--accent); font-weight: 750; letter-spacing: .06em; }
#coach.hidden { opacity: 0; transform: translate(-50%, -46%); }
#coach.pop { animation: coachPop .36s ease-out; }
body.focus #coach { opacity: 0; transform: translate(-50%, -46%); }
@keyframes coachPop {
  from { opacity: 0; transform: translate(-50%, -50%) scale(.94); }
  to   { opacity: 1; transform: translate(-50%, -50%) scale(1); }
}

/* pointer glyph */
.mice { width: 74px; height: auto; display: block; color: var(--ink-2); overflow: visible; }
.mice .shell { fill: none; stroke: currentColor; stroke-width: 3; }
.mice .sep { stroke: currentColor; stroke-width: 2.2; opacity: .55; }
.mice .btn { fill: transparent; transition: fill .2s ease; }
.mice .wheel { fill: currentColor; opacity: .4; }
.mice .dot, .mice .dot2 { fill: currentColor; opacity: .85; }
.mice .dot2 { opacity: 0; }
.mice .arw {
  fill: none; stroke: var(--accent); stroke-width: 3.4;
  stroke-linecap: round; stroke-linejoin: round; opacity: 0;
}
.mice.rotate .arw-l, .mice.rotate .arw-r { opacity: 1; }
.mice.zoom   .arw-u, .mice.zoom   .arw-d { opacity: 1; }
.mice.pan .arw-l, .mice.pan .arw-r, .mice.pan .arw-u, .mice.pan .arw-d { opacity: .9; }
.mice.rotate .btn-l { fill: var(--accent); }
.mice.pan    .btn-r { fill: var(--accent); }
.mice.zoom   .wheel { fill: var(--accent); opacity: 1; }
.mice.click  .btn-l { fill: var(--accent); animation: mClick 1.4s ease-in-out infinite; }
.mice.rotate .mbody { animation: mDragX 1.7s ease-in-out infinite; }
.mice.pan    .mbody { animation: mPan 2.4s ease-in-out infinite; }
.mice.zoom   .wheel { animation: mWheel 1.3s ease-in-out infinite; }
.mice.touchi.zoom .dot  { animation: pinchL 1.5s ease-in-out infinite; }
.mice.touchi.zoom .dot2 { opacity: .85; animation: pinchR 1.5s ease-in-out infinite; }
.mice.touchi.pan  .dot  { transform: translateX(-11px); }
.mice.touchi.pan  .dot2 { opacity: .85; transform: translateX(11px); }
.mice.touchi.click .dot2 { opacity: .85; animation: tapPulse 1.4s ease-out infinite; }
@keyframes mDragX { 0%, 100% { transform: translateX(-5px); } 50% { transform: translateX(5px); } }
@keyframes mPan {
  0%, 100% { transform: translate(-4px, -3px); } 25% { transform: translate(4px, -3px); }
  50%      { transform: translate(4px, 3px); }   75% { transform: translate(-4px, 3px); }
}
@keyframes mWheel { 0%, 100% { transform: translateY(-3px); } 50% { transform: translateY(3px); } }
@keyframes mClick { 0%, 70%, 100% { opacity: 1; } 82% { opacity: .25; } }
@keyframes tapPulse { 0% { transform: scale(.6); opacity: .8; } 100% { transform: scale(2.1); opacity: 0; } }
@keyframes pinchL { 0%, 100% { transform: translateX(-8px); } 50% { transform: translateX(-20px); } }
@keyframes pinchR { 0%, 100% { transform: translateX(8px); }  50% { transform: translateX(20px); } }

/* ------------------------------------------------- permanent control bar */
#hintbar {
  position: fixed; z-index: 4; bottom: calc(var(--safe-b) + 20px);
  left: 50%; transform: translateX(-50%);
  padding: 7px 16px; border-radius: 999px;
  background: rgba(251, 248, 242, .82);
  -webkit-backdrop-filter: blur(8px); backdrop-filter: blur(8px);
  border: 1px solid var(--line);
  font-family: ui-sans-serif, system-ui, sans-serif;
  font-size: 11.5px; letter-spacing: .02em; color: var(--ink-3);
  pointer-events: none; white-space: nowrap;
}
#hintbar b { color: var(--ink-2); font-weight: 600; }
#hintbar span { opacity: .45; margin: 0 .55em; }

/* ---------------------------------------------------------- portrait */
@media (max-width: 860px), (orientation: portrait) {
  :root { --panel-w: 100%; }
  #masthead { top: calc(var(--safe-t) + 18px); left: 20px; right: 20px; }
  #masthead .sub br { display: none; }
  #back { left: 16px; top: calc(var(--safe-t) + 16px); }
  #mute { right: 16px; top: calc(var(--safe-t) + 16px); }
  #credits { left: 20px; bottom: calc(var(--safe-b) + 46px); }
  #hintbar { bottom: calc(var(--safe-b) + 12px); font-size: 10.5px; padding: 6px 12px; }
  #coach { padding: 22px 28px 18px; gap: 11px; }
  #coach .txt { font-size: 16px; }
  .mice { width: 62px; }

  #panel {
    top: auto; bottom: 0; height: min(66svh, 640px); width: 100%;
    border-left: 0; border-top: 1px solid var(--line);
    border-radius: 20px 20px 0 0;
    box-shadow: 0 -10px 40px rgba(35,32,28,.13);
    transform: translateY(100%);
  }
  .panel-inner { padding: 6px 22px calc(var(--safe-b) + 40px); }
  .grab {
    display: block; width: 38px; height: 4px; margin: 8px auto 18px;
    border-radius: 4px; background: var(--line);
  }
  #panel h2 { font-size: 26px; }
}

@media (prefers-reduced-motion: reduce) {
  * { animation-duration: .01ms !important; transition-duration: .12s !important; }
  #coach.pop { animation: none; }
  .mice .mbody, .mice .wheel, .mice .dot, .mice .dot2, .mice .btn { animation: none !important; }
}
