:root {
  --ink: #0b0a09;
  --stone: #2a2926;
  --stone-2: #3a3833;
  --bronze: #c9a24a;
  --bronze-hi: #f1d58a;
  --bronze-lo: #7a5a1e;
  --rope: #b3202a;
  --paper: #efe6d2;
  --muted: #bdb4a2;
  --jade: #5fc79a;
  --blood: #e2323c;
  /* 楷体：霞鹜文楷屏幕版（按字切片，只下载用到的字），缺失时退到系统楷体 */
  --serif: "LXGW WenKai Screen", "Kaiti SC", "STKaiti", "KaiTi", "BiauKai", "Noto Sans SC", "PingFang SC", sans-serif;
  --brush: "Ma Shan Zheng", "STKaiti", "KaiTi", var(--serif);
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body {
  background: var(--ink);
  color: var(--paper);
  font-family: var(--serif);
  font-weight: 400;
  overflow: hidden;
  -webkit-font-smoothing: antialiased;
  user-select: none;
  -webkit-user-select: none;
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;   /* no double-tap zoom on phones */
}
button { font: inherit; color: inherit; cursor: pointer; }

#bg {
  position: fixed; inset: 0;
  background:
    radial-gradient(ellipse at 50% 55%, rgba(11,10,9,.35) 0%, rgba(11,10,9,.88) 70%, var(--ink) 100%),
    url("../assets/img/chamber.jpg") center 40% / cover no-repeat;
  filter: saturate(.8);
  transition: filter .8s, opacity .8s;
}
body.listening #bg { filter: saturate(.4) brightness(.35) blur(2px); }

.screen { position: fixed; inset: 0; display: none; }
.screen.active { display: flex; }

/* ------------------------------------------------ title */
#title, #result { align-items: center; justify-content: center; flex-direction: column; padding: 16px; }
.title-card {
  width: min(560px, 100%);
  text-align: center;
  padding: 36px 28px 28px;
  background: linear-gradient(180deg, rgba(20,18,15,.82), rgba(12,11,10,.9));
  border: 1px solid rgba(201,162,74,.35);
  box-shadow: 0 30px 80px rgba(0,0,0,.6), inset 0 0 0 4px rgba(201,162,74,.06);
  max-height: calc(100dvh - 32px - env(safe-area-inset-top) - env(safe-area-inset-bottom));
  overflow: auto;
}
.kicker { margin: 0; letter-spacing: .4em; color: var(--bronze); font-size: 14px; }
h1 {
  font-family: var(--brush);
  font-weight: 400;
  font-size: clamp(64px, 16vw, 112px);
  line-height: 1.05;
  margin: 8px 0 12px;
  background: linear-gradient(180deg, var(--bronze-hi), var(--bronze) 55%, var(--bronze-lo));
  -webkit-background-clip: text; background-clip: text; color: transparent;
  text-shadow: 0 2px 0 rgba(0,0,0,.2);
  letter-spacing: .06em;
}
.lede { color: var(--paper); opacity: .85; line-height: 1.9; margin: 0 0 18px; }
.phones {
  display: flex; gap: 10px; align-items: center; justify-content: center;
  font-size: 14px; color: var(--muted); margin: 0 auto 22px; max-width: 420px; text-align: left;
}
.phones-ico { font-size: 22px; }
.menu { display: flex; flex-direction: column; gap: 10px; align-items: center; }
.btn {
  min-width: min(220px, 100%); padding: 12px 22px;
  background: rgba(255,255,255,.03);
  border: 1px solid rgba(201,162,74,.45);
  color: var(--paper); letter-spacing: .2em; font-size: 16px;
  transition: background .2s, transform .1s, border-color .2s;
}
.btn:hover { background: rgba(201,162,74,.12); border-color: var(--bronze); }
.btn:active { transform: translateY(1px); }
.btn.primary {
  background: linear-gradient(180deg, #a3261f, #6e1512);
  border-color: #d2463b; font-weight: 700;
}
.btn.primary:hover { background: linear-gradient(180deg, #b82c24, #7d1814); }
.btn.ghost { border-color: transparent; color: var(--muted); font-size: 14px; }
.loading { margin-top: 14px; font-size: 14px; color: var(--muted); }
.loading .bar { height: 3px; background: rgba(255,255,255,.08); margin-bottom: 6px; }
.loading .bar i { display: block; height: 100%; width: 0; background: var(--bronze); transition: width .2s; }
.howto { margin-top: 22px; text-align: left; font-size: 14px; color: var(--muted); }
.howto summary { cursor: pointer; text-align: center; color: var(--bronze); letter-spacing: .3em; }
.howto ul { padding-left: 1.2em; line-height: 1.9; }
.howto b { color: var(--paper); }
kbd {
  display: inline-block; min-width: 1.6em; padding: 0 .35em; margin: 0 1px;
  border: 1px solid rgba(255,255,255,.25); border-bottom-width: 2px; border-radius: 3px;
  font: 12px/1.5 ui-monospace, Menlo, monospace; text-align: center; color: var(--paper);
}
.credit { position: fixed; bottom: 8px; left: 0; right: 0; text-align: center; font-size: 12px; color: rgba(255,255,255,.3); padding: 0 16px; }

/* ------------------------------------------------ game */
/* Game layout: HUD on top, the board takes whatever space is left, controls and
   the examiner below it. Short landscape screens move them into a side column. */
#game.active {
  display: grid;
  grid-template-columns: minmax(0, 1fr);
  grid-template-rows: auto minmax(0, 1fr) auto;
  grid-template-areas: "hud" "stage" "side";
  padding: env(safe-area-inset-top) env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left);
}
.hud { grid-area: hud; }
.hud {
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
  padding: 12px 16px; position: relative; z-index: 5;
  background: linear-gradient(180deg, rgba(0,0,0,.7), transparent);
}
.level-name { font-family: var(--brush); font-size: 30px; color: var(--bronze-hi); line-height: 1; }
.level-sub { font-size: 14px; color: var(--muted); letter-spacing: .2em; margin-top: 4px; }
.coins { display: flex; gap: 8px; }
.coin-ico {
  width: 22px; height: 22px; border-radius: 50%;
  border: 2px solid var(--bronze-lo); position: relative; opacity: .45;
  background: radial-gradient(circle, transparent 28%, rgba(201,162,74,.15) 30%);
}
.coin-ico::after { content: ""; position: absolute; inset: 6px; border: 1.5px solid var(--bronze-lo); }
.coin-ico.now { opacity: 1; border-color: var(--bronze); box-shadow: 0 0 10px rgba(201,162,74,.6); }
.coin-ico.ok { opacity: 1; border-color: var(--jade); background: rgba(95,199,154,.25); }
.coin-ico.bad { opacity: 1; border-color: var(--blood); background: rgba(226,50,60,.3); }
.hud-right { display: flex; align-items: center; gap: 10px; }
.penalties { display: flex; align-items: center; gap: 6px; font-family: var(--brush); font-size: 22px; color: var(--blood); }
#pens { display: flex; gap: 4px; }
.pen { width: 18px; height: 18px; border: 1.5px solid rgba(226,50,60,.5); transform: rotate(45deg); }
.pen.hit { background: var(--blood); border-color: var(--blood); box-shadow: 0 0 8px var(--blood); }
.icon-btn { background: none; border: 1px solid rgba(255,255,255,.15); width: 36px; height: 36px; font-size: 16px; }
.icon-btn:hover { border-color: var(--bronze); }

.stage {
  grid-area: stage; position: relative; min-height: 0;
  display: flex; align-items: center; justify-content: center;
  padding: 28px 0 8px;
  container-type: size;          /* the board sizes itself to this box */
}
.floor-wrap {
  position: relative;
  /* leave room on both sides for the vertical incense stick, keep it square */
  width: min(100cqw - 128px, 100cqh, 780px);
  aspect-ratio: 1;
}
#floor { width: 100%; height: 100%; display: block; overflow: visible; }
#floor .slab { fill: #4a4842; stroke: #1b1a18; stroke-width: 1.2; }
#floor .slab-edge { fill: #2c2b27; }
#floor .joint { stroke: rgba(0,0,0,.35); stroke-width: .5; }
#floor .rope { stroke: var(--rope); stroke-width: 1.6; stroke-linecap: round; filter: drop-shadow(0 .6px 0 rgba(0,0,0,.5)); }
#floor .sector { fill: transparent; cursor: pointer; transition: fill .15s; }
@media (hover: hover) { #floor .sector:hover { fill: rgba(241,213,138,.10); } }
#floor .sector.sel { fill: rgba(241,213,138,.28); }
#floor .sector.correct { fill: rgba(95,199,154,.38); }
#floor .sector.wrong { fill: rgba(226,50,60,.40); }
#floor .sector.off { fill: rgba(0,0,0,.45); cursor: not-allowed; }
#floor.locked .sector { cursor: default; }
#floor.locked .sector:hover:not(.sel):not(.correct):not(.wrong) { fill: transparent; }
#floor .glyph {
  font-family: var(--brush); fill: var(--bronze); font-size: 22px;
  text-anchor: middle; dominant-baseline: central; pointer-events: none;
  paint-order: stroke; stroke: rgba(0,0,0,.55); stroke-width: 1.3px;
}
#floor .glyph.small { font-size: 11px; fill: rgba(241,213,138,.65); stroke-width: .8px; }
#floor .key { font: 6px ui-monospace, Menlo, monospace; fill: rgba(255,255,255,.35); text-anchor: middle; pointer-events: none; }
#floor .cushion { fill: #d9ccab; stroke: #8f8469; stroke-width: 1; }
#floor .cushion-dot { fill: #b3202a; }
#floor .head { transition: none; }
#floor .head-shape { fill: #1b1512; stroke: var(--bronze-hi); stroke-width: .8; }
#floor .nose { fill: var(--bronze-hi); }
#floor .coin-mark circle { fill: var(--bronze-hi); stroke: #5b4214; stroke-width: .8; }
#floor .coin-mark rect { fill: #3a3833; }
#floor .coin-mark { animation: drop .5s ease-out; }
#floor .coin-path { fill: none; stroke: rgba(241,213,138,.6); stroke-width: .8; stroke-dasharray: 2 2; }
#floor .pulse { fill: none; stroke: var(--bronze-hi); stroke-width: 1; animation: pulse 1.1s ease-out forwards; }
@keyframes drop { from { transform: scale(2.2); opacity: 0; } to { transform: scale(1); opacity: 1; } }
@keyframes pulse { from { r: 3; opacity: 1; } to { r: 22; opacity: 0; } }

.incense {
  position: absolute; right: -54px; top: 50%; transform: translateY(-50%);
  display: flex; flex-direction: column; align-items: center; gap: 6px;
  font-size: 13px; color: var(--muted);
}
.incense .stick { width: 6px; height: 150px; background: rgba(255,255,255,.06); position: relative; border-radius: 3px; }
/* The unburnt stick shrinks from the top; the ember sits on its tip. */
.incense .stick i {
  position: absolute; left: 0; right: 0; bottom: 0; height: calc(var(--p, 1) * 100%);
  background: linear-gradient(180deg, #ff5a1f 0 3px, #ffb46b 3px 5px, #a0682f 5px);
  border-radius: 3px 3px 1px 1px;
  box-shadow: 0 -3px 12px 2px rgba(255,110,40,.8);
}
.incense .stick i::before {
  content: ""; position: absolute; left: 50%; top: -14px; width: 3px; height: 14px;
  transform: translateX(-50%);
  background: linear-gradient(0deg, rgba(220,220,220,.35), transparent);
  filter: blur(1px); animation: smoke 1.6s ease-in-out infinite;
}
@keyframes smoke { 0%,100% { transform: translateX(-50%) skewX(8deg); opacity: .7; } 50% { transform: translateX(-30%) skewX(-10deg); opacity: .35; } }
#incense-t { font-variant-numeric: tabular-nums; color: var(--paper); }
.incense.low #incense-t { color: var(--blood); font-weight: 700; }
.incense.low .stick i { animation: flicker .3s infinite alternate; }
@keyframes flicker { to { box-shadow: 0 -3px 18px 4px rgba(255,60,20,.95); } }

.side {
  grid-area: side; position: relative; z-index: 5;
  display: flex; flex-direction: column; align-items: center; gap: 10px;
  padding: 6px 16px max(12px, env(safe-area-inset-bottom));
}
.side-controls { display: flex; gap: 10px; }
.turn-btn {
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  width: 66px; height: 54px; font-size: 22px; line-height: 1;
  background: rgba(0,0,0,.45); border: 1px solid rgba(201,162,74,.35); color: var(--bronze-hi);
  touch-action: none;
}
.turn-btn small { font-size: 12px; color: var(--muted); margin-top: 4px; }
.turn-btn.held { background: rgba(201,162,74,.2); }
.turn-btn:disabled { opacity: .3; cursor: default; }

.phase {
  position: absolute; top: 4px; left: 0; right: 0; text-align: center;
  font-size: 15px; letter-spacing: .3em; color: var(--bronze-hi);
  text-shadow: 0 1px 6px #000; pointer-events: none; z-index: 3;
}

/* eyelids — close while listening */
.lids { position: fixed; inset: 0; pointer-events: none; z-index: 2; }
.lid { position: absolute; left: 0; right: 0; height: 50%; background: #000; transition: transform .9s cubic-bezier(.6,0,.3,1), opacity .9s; opacity: 0; }
.lid.top { top: 0; transform: translateY(-100%); border-radius: 0 0 50% 50% / 0 0 30% 30%; }
.lid.bottom { bottom: 0; transform: translateY(100%); border-radius: 50% 50% 0 0 / 30% 30% 0 0; }
body.listening .lid { opacity: .78; }
body.listening .lid.top { transform: translateY(-38%); }
body.listening .lid.bottom { transform: translateY(38%); }

.examiner {
  display: flex; align-items: center; gap: 12px;
  margin: 0; padding: 10px 14px;
  width: min(640px, 100%);
  background: linear-gradient(90deg, rgba(20,16,10,.92), rgba(20,16,10,.75));
  border: 1px solid rgba(201,162,74,.3);
  position: relative; z-index: 5;
}
.examiner img { width: 52px; height: 52px; object-fit: cover; border: 1px solid var(--bronze-lo); flex: none; }
.examiner.talking img { box-shadow: 0 0 0 2px var(--bronze), 0 0 16px rgba(241,213,138,.5); }
.speech { min-width: 0; }
.who { font-size: 12px; letter-spacing: .3em; color: var(--bronze); }
.line { font-size: 17px; line-height: 1.5; min-height: 1.5em; }

.banner {
  position: fixed; inset: 0; display: flex; flex-direction: column; align-items: center; justify-content: center;
  background: radial-gradient(ellipse, rgba(0,0,0,.65), rgba(0,0,0,.2) 70%);
  z-index: 8; pointer-events: none; text-align: center; padding: 16px;
}
.banner .big {
  font-family: var(--brush); font-size: clamp(56px, 13vw, 120px); line-height: 1.1;
  color: var(--bronze-hi); text-shadow: 0 4px 30px rgba(0,0,0,.8);
  animation: stamp .45s cubic-bezier(.2,1.6,.4,1);
}
.banner.over-clip { z-index: 21; background: none; justify-content: flex-end; padding-bottom: 12vh; }
.banner .big.red { color: var(--blood); }
.banner .small { margin-top: 10px; font-size: 16px; color: var(--paper); max-width: 520px; line-height: 1.8; text-shadow: 0 1px 8px #000; }
@keyframes stamp { from { transform: scale(1.8); opacity: 0; } to { transform: scale(1); opacity: 1; } }

body.hurt #game { animation: shake .45s; }
body.hurt::after { content: ""; position: fixed; inset: 0; background: rgba(200,0,0,.35); z-index: 7; pointer-events: none; animation: fade .6s forwards; }
@keyframes shake { 0%,100% { transform: none; } 20% { transform: translate(-8px, 4px); } 40% { transform: translate(7px,-5px); } 60% { transform: translate(-5px,3px); } 80% { transform: translate(3px,-2px); } }
@keyframes fade { to { opacity: 0; } }

/* ------------------------------------------------ result */
.stats { display: grid; grid-template-columns: repeat(3, 1fr); gap: 10px; margin: 8px 0 24px; }
.stat { border: 1px solid rgba(201,162,74,.25); padding: 10px 6px; }
.stat b { display: block; font-size: 26px; color: var(--bronze-hi); font-family: var(--brush); font-weight: 400; }
.stat span { font-size: 13px; color: var(--muted); }
.stats .wide { grid-column: 1 / -1; text-align: left; font-size: 14px; color: var(--muted); line-height: 1.8; }

/* ------------------------------------------------ clip & modal */
.clip {
  position: fixed; inset: 0; z-index: 20; background: #000;
  display: flex; align-items: center; justify-content: center; cursor: pointer;
}
.clip video { width: 100%; height: 100%; object-fit: contain; }
.clip-skip { position: absolute; bottom: 14px; right: 18px; font-size: 13px; color: rgba(255,255,255,.45); }
.modal { position: fixed; inset: 0; z-index: 30; background: rgba(0,0,0,.6); display: flex; align-items: center; justify-content: center; padding: 16px; }
.modal-card { width: min(440px, 100%); max-height: calc(100dvh - 32px); overflow: auto; background: #15130f; border: 1px solid rgba(201,162,74,.4); padding: 22px; }
.modal-card h2 { margin: 0 0 14px; font-family: var(--brush); font-weight: 400; font-size: 30px; color: var(--bronze-hi); }
.row { display: flex; justify-content: space-between; align-items: center; gap: 12px; padding: 10px 0; border-bottom: 1px solid rgba(255,255,255,.06); font-size: 14px; }
.row select, .row input[type=range] { max-width: 55%; }
select { background: #222; color: var(--paper); border: 1px solid #444; padding: 4px; }
.note { font-size: 13px; color: var(--muted); line-height: 1.7; }
[hidden] { display: none !important; }

.skip-intro {
  position: fixed; left: 50%; top: 68%; transform: translateX(-50%); z-index: 25;
  padding: 12px 30px; font-size: 18px; font-weight: 700; letter-spacing: .2em;
  background: rgba(0,0,0,.72); color: var(--bronze-hi);
  border: 1px solid var(--bronze);
  box-shadow: 0 6px 24px rgba(0,0,0,.6);
}
.skip-intro:hover { background: rgba(201,162,74,.2); }

.track-status { display: block; font-size: 12px; color: var(--bronze); margin-top: 2px; }
.row.sub { font-size: 13px; color: var(--muted); border-bottom: 1px solid rgba(255,255,255,.06); }
.mini { flex: none; padding: 4px 10px; font-size: 13px; background: none; border: 1px solid rgba(201,162,74,.45); color: var(--bronze-hi); }
.mini:disabled { opacity: .35; cursor: default; }

.diff { display: flex; gap: 6px; margin-bottom: 4px; }
.diff button {
  display: flex; flex-direction: column; align-items: center; gap: 2px;
  width: 112px; padding: 8px 0; font-size: 16px; letter-spacing: .1em;
  background: rgba(255,255,255,.03); border: 1px solid rgba(201,162,74,.3); color: var(--muted);
}
.diff button small { font-size: 12px; letter-spacing: 0; opacity: .8; }
.diff button.on { color: var(--bronze-hi); border-color: var(--bronze); background: rgba(201,162,74,.14); }
#pens { flex-wrap: wrap; max-width: 170px; }

.dev-badge { margin: 0 0 8px; font: 12px ui-monospace, Menlo, monospace; color: #7fd1ff; letter-spacing: .1em; }

.icon-btn { display: inline-flex; align-items: center; justify-content: center; color: var(--paper); }
.title-corner { position: fixed; top: max(14px, env(safe-area-inset-top)); right: max(16px, env(safe-area-inset-right)); z-index: 5; }
.cam-btn.on { color: var(--bronze-hi); border-color: var(--bronze); background: rgba(201,162,74,.18); box-shadow: 0 0 10px rgba(241,213,138,.35); position: relative; }
.cam-btn.on::after { content: ""; position: absolute; top: 4px; right: 4px; width: 6px; height: 6px; border-radius: 50%; background: var(--jade); box-shadow: 0 0 6px var(--jade); }
.cam-btn.busy { opacity: .6; animation: flicker .5s infinite alternate; }
.cam-btn.err { color: var(--blood); border-color: rgba(226,50,60,.6); }

.cam-btn { width: auto; gap: 6px; padding: 0 12px; font-size: 14px; letter-spacing: .1em; }
.cam-btn.on::after { top: 3px; right: 3px; }
@media (max-width: 640px) { .hud .cam-btn span { display: none; } .hud .cam-btn { padding: 0 8px; } }

/* ------------------------------------------------ phones & tablets */
.touch-only { display: none; }
@media (hover: none) {
  #floor .key, .kbd-only { display: none; }     /* keyboard hints mean nothing on touch */
  .touch-only { display: list-item; }
}

/* Portrait (phones and tablets): the board uses the full width and the incense
   becomes a bar above it instead of a stick beside it. */
@media (orientation: portrait) {
  .stage { padding: 36px 12px 8px; }
  .floor-wrap { width: min(100cqw, 100cqh, 900px); }
  .incense { right: 0; top: -26px; transform: none; flex-direction: row; }
  .incense .stick { width: 110px; height: 5px; }
  .incense .stick i { right: auto; top: 0; height: 100%; width: calc(var(--p, 1) * 100%); background: linear-gradient(270deg, #ff8a3d 0 4px, #8a5a2b 4px); }
  .incense .stick i::before { display: none; }
  .phase { top: 8px; }
}

/* Narrow screens (phones): two-row HUD, tighter spacing. */
@media (max-width: 640px) {
  .hud { padding: 10px 12px; flex-wrap: wrap; row-gap: 6px; }
  .hud-mid { order: 3; width: 100%; display: flex; justify-content: center; }
  .level-name { font-size: 24px; }
  .stage { padding-left: 0; padding-right: 0; }
  .side { padding-left: 12px; padding-right: 12px; }
  .line { font-size: 15px; }
  .credit { position: static; margin-top: 10px; }
  .diff { width: 100%; max-width: 360px; }
  .diff button { flex: 1; width: auto; min-width: 0; }
  .diff button small { font-size: 11px; }
}

/* Short landscape (phones on their side): board left, controls + examiner right. */
@media (orientation: landscape) and (max-height: 560px) {
  #game.active {
    grid-template-columns: minmax(0, 1fr) minmax(230px, 36%);
    grid-template-rows: auto minmax(0, 1fr);
    grid-template-areas: "hud hud" "stage side";
  }
  .hud { padding: 6px 12px; gap: 8px; }
  .level-name { font-size: 20px; }
  .level-sub { font-size: 12px; margin-top: 2px; }
  .coin-ico { width: 16px; height: 16px; }
  .coin-ico::after { inset: 4px; }
  .pen { width: 13px; height: 13px; }
  .penalties { font-size: 18px; }
  .icon-btn { height: 32px; }
  .icon-btn:not(.cam-btn) { width: 32px; }
  .hud .cam-btn span { display: none; }
  .hud .cam-btn { padding: 0 8px; }
  .stage { padding: 22px 0 6px; }
  .floor-wrap { width: min(100cqw - 110px, 100cqh); }
  .incense { right: -46px; }
  .incense .stick { height: min(120px, 40cqh); }
  .phase { top: 2px; font-size: 13px; }
  .side { justify-content: center; padding: 6px max(12px, env(safe-area-inset-right)) max(8px, env(safe-area-inset-bottom)) 0; }
  .turn-btn { width: 60px; height: 46px; font-size: 20px; }
  .examiner { padding: 8px 10px; gap: 10px; }
  .examiner img { width: 40px; height: 40px; }
  .line { font-size: 14px; }

  /* Title: brand on the left, menu on the right. */
  #title .title-card {
    width: min(860px, 100%);
    display: grid; grid-template-columns: 1fr 1fr; gap: 20px; align-items: center;
    padding: 16px 20px;
  }
  #title h1 { font-size: 64px; margin: 4px 0 8px; }
  .lede { font-size: 14px; line-height: 1.7; margin-bottom: 10px; }
  .phones { margin-bottom: 0; font-size: 12px; }
  .howto { margin-top: 10px; }
  .credit { display: none; }
  .title-corner { top: auto; bottom: 10px; }
  #result .title-card { padding: 14px 20px; }
  #result h1 { font-size: 56px; margin: 0 0 6px; }
  .stats { margin: 4px 0 12px; }
}
