:root {
  --bg0: #151c27; --bg1: #1f2a3a; --text: #eef3f8; --muted: #9fb0c3; --soft: #c9d6e4; --accent: #f2d36b; --gas: #56d6c0;
  --ok: #8be08b; --bad: #ff7a59; --panel: #18212e; --tile: #243042; --line: #3a4d66;
  --font: "PingFang SC", "Hiragino Sans GB", "Noto Sans CJK SC", "Source Han Sans SC", "Microsoft YaHei", system-ui, sans-serif;
}
* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; height: 100%; background: var(--bg0); color: var(--text); font-family: var(--font); overflow: hidden; }
body { touch-action: none; user-select: none; -webkit-user-select: none; }
button { font-family: inherit; color: inherit; cursor: pointer; }
.hidden { display: none !important; }
#app { position: fixed; inset: 0; }
.screen { position: absolute; inset: env(safe-area-inset-top) env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left); }

/* ---------- 首屏 ---------- */
#menu { display: flex; align-items: stretch; gap: 18px; padding: 14px 18px; background: radial-gradient(120% 140% at 15% 20%, #22324a 0%, var(--bg0) 60%); }
.menu-left { width: 32%; min-width: 210px; display: flex; flex-direction: column; justify-content: center; }
.menu-left h1 { font-size: 30px; margin: 0 0 4px; letter-spacing: 2px; }
.menu-left h1 span { color: var(--accent); }
.menu-left p { margin: 4px 0; color: var(--muted); font-size: 13px; line-height: 1.6; }
.menu-left .proto { color: var(--soft); white-space: nowrap; }
.menu-left .note { font-size: 12px; margin-top: 10px; color: #7f91a6; }
.levels { flex: 1; display: grid; grid-template-columns: repeat(3, 1fr); grid-auto-rows: minmax(0, 1fr); gap: 8px; align-content: center; max-height: 360px; margin: auto 0; }
.lv { position: relative; min-height: 64px; border: 1px solid #33445a; border-radius: 12px; background: linear-gradient(180deg, #243247, #1c2738); padding: 6px; text-align: left; display: flex; flex-direction: column; gap: 4px; overflow: hidden; }
.lv .thumb { width: 100%; aspect-ratio: 96 / 54; height: auto; max-height: 58%; object-fit: contain; border-radius: 6px; image-rendering: pixelated; background: #1a2433; }
.lv-text { display: grid; grid-template-columns: auto 1fr; column-gap: 6px; align-items: baseline; min-height: 0; }
.lv b { font-size: 20px; font-variant-numeric: tabular-nums; color: var(--accent); }
.lv span { font-size: 14px; }
.lv small { grid-column: 1 / -1; color: var(--muted); font-size: 12px; line-height: 1.35; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden; }
.lv.done::after { content: "✓"; position: absolute; right: 10px; top: 8px; color: var(--ok); font-size: 18px; }
.lv:active { transform: translateY(1px); }
.menu-btn { margin-top: 14px; align-self: flex-start; min-height: 44px; padding: 0 16px; border-radius: 22px; border: 1px solid #3c5068; background: #22303f; font-size: 14px; }

/* ---------- 游戏 ---------- */
#game { display: flex; background: linear-gradient(180deg, var(--bg0), var(--bg1)); }
/* min-width/min-height 0：窗口变窄时舞台区跟着缩，不会被画布撑宽、把右侧面板挤出屏幕（画布尺寸由 ResizeObserver 重算） */
#stage-wrap { position: relative; flex: 1; min-width: 0; min-height: 0; display: flex; align-items: center; justify-content: center; --cx: 0px; --cy: 0px; --cw: 100%; --ch: 100%; }
#stage { display: block; border-radius: 10px; box-shadow: 0 0 0 1px #2a3850; transition: box-shadow .2s; }
#game.running #stage { box-shadow: 0 0 0 2px rgba(255, 255, 255, .4); }
#hud { position: absolute; left: calc(var(--cx) + 8px); top: calc(var(--cy) + 8px); width: calc(var(--cw) - 16px); pointer-events: none; display: flex; align-items: flex-start; gap: 8px; }
#hud .goal { max-width: 66%; background: rgba(15, 21, 30, .8); border: 1px solid #2f3e53; border-radius: 10px; padding: 6px 10px; font-size: 13px; line-height: 1.45; }
#hud .goal-line b { color: var(--accent); margin-right: 6px; font-variant-numeric: tabular-nums; }
#hud .intro { color: var(--soft); font-size: 13px; margin-top: 2px; }
/* 说明会挡住倾倒架、蜡烛、可放位置时先收起，只留目标；点"说明"展开（按钮看着小，点按范围上下各扩 12 像素） */
#hud .goal.collapsed .intro { display: none; }
#hud .more { position: relative; flex: none; pointer-events: auto; margin-top: 5px; padding: 3px 9px; border-radius: 10px; border: 1px solid #3c5068; background: rgba(15, 21, 30, .85); color: var(--soft); font-size: 12px; line-height: 18px; }
#hud .more::before { content: ""; position: absolute; inset: -10px -6px; }
#hud .more[hidden] { display: none; }
#hud .timer { margin-left: auto; flex: none; min-width: 68px; text-align: center; background: rgba(15, 21, 30, .8); border: 1px solid #2f3e53; border-radius: 10px; padding: 6px 8px; font-size: 13px; font-variant-numeric: tabular-nums; color: var(--text); }
#game.result #hud, #game.stopping #hud { display: none; }

#panel { width: 168px; flex: none; background: var(--panel); border-left: 1px solid #263346; display: flex; flex-direction: column; padding: 8px 8px 10px; gap: 6px; }
.panel-top { display: flex; gap: 4px; }
.icon-btn { position: relative; min-width: 44px; min-height: 44px; border-radius: 10px; border: 1px solid #33445a; background: #213044; font-size: 13px; padding: 0 6px; display: inline-flex; align-items: center; justify-content: center; }
.icon-btn.mute { margin-left: auto; padding: 0 4px; }
.icon-btn.notes.plus::after { content: "+1"; position: absolute; right: -6px; top: -6px; background: var(--accent); color: #3a2a06; font-size: 12px; font-weight: 700; border-radius: 9px; padding: 0 5px; animation: plus 1s ease-in-out infinite; }
@keyframes plus { 50% { transform: scale(1.25); } }
.hint { font-size: 13px; line-height: 1.45; height: calc(13px * 1.45 * 2); overflow: hidden; color: var(--muted); flex: none; }
.hint b { color: #ffffff; font-weight: 600; }
.hint b.warn { color: var(--accent); }
/* 段落最后一行不要只剩一个字（支持 text-wrap: pretty 的浏览器才生效，不支持就照旧换行） */
.hint, #card, #toast, #hud .goal, .lv small { text-wrap: pretty; }
.tray { flex: 1; overflow-y: auto; display: grid; grid-template-columns: 1fr 1fr; gap: 5px; align-content: start; }
.tile { position: relative; height: 48px; border-radius: 10px; border: 1px solid #33445a; background: var(--tile); display: flex; flex-direction: column; align-items: flex-start; justify-content: space-between; padding: 3px 4px 3px 6px; font-size: 12px; touch-action: none; }
.tile svg { width: 30px; height: 25px; align-self: center; }
.tile .name { font-size: 12px; line-height: 16px; white-space: nowrap; }
.tile .n { position: absolute; right: 3px; bottom: 3px; background: #0f151e; color: #ffffff; font-size: 12px; line-height: 16px; padding: 0 5px; border-radius: 8px; font-variant-numeric: tabular-nums; }
.tile.sel { border-color: var(--accent); box-shadow: 0 0 0 2px rgba(242, 211, 107, .35); transform: translateY(-2px); }
.tile.empty { opacity: .35; }
.tile.shake { animation: shake .15s 3; }
.tile.ghost { position: fixed; width: 56px; pointer-events: none; opacity: .9; z-index: 5; border-color: var(--accent); }
@keyframes shake { 25% { transform: translateX(-4px); } 75% { transform: translateX(4px); } }
.speed { flex: none; min-height: 44px; border-radius: 12px; border: 1px solid #33445a; background: #213044; font-size: 14px; }
.speed.on { background: #3a3420; border-color: var(--accent); color: var(--accent); box-shadow: inset 0 2px 4px rgba(0, 0, 0, .45); }
.legend { flex: none; font-size: 12px; line-height: 1.5; color: var(--soft); background: #1e2b3c; border: 1px solid var(--line); border-radius: 10px; padding: 6px 7px; }
.legend .sw { display: inline-block; vertical-align: middle; margin-right: 5px; }
.legend .sw.gas { width: 9px; height: 13px; background: rgba(86, 214, 192, .7); border-radius: 2px; }
.legend .sw.line { width: 14px; border-top: 2px dashed rgba(240, 250, 255, .85); }
.go { flex: none; min-height: 56px; border-radius: 14px; border: none; font-size: 17px; font-weight: 600; letter-spacing: 2px; background: linear-gradient(180deg, #ffe08a, #f0b84a); color: #3a2a06; box-shadow: 0 3px 0 #a87a22; }
.go:active { transform: translateY(2px); box-shadow: 0 1px 0 #a87a22; }
/* 面板矮（手机浏览器带工具栏、刘海安全区）时托盘改三列：图标左上、份数右上、名字在下；开始按钮 44 像素 */
#panel.compact .tray { grid-template-columns: repeat(3, 1fr); gap: 4px; }
#panel.compact .tile { align-items: center; justify-content: flex-end; padding: 2px 2px 3px; }
#panel.compact .tile svg { position: absolute; left: 3px; top: 4px; width: 22px; height: 19px; }
#panel.compact .tile .n { top: 3px; bottom: auto; right: 2px; padding: 0 3px; }
#panel.compact .go { min-height: 44px; }
#panel.tight { gap: 4px; padding-top: 6px; padding-bottom: 6px; }
#panel.tight .tray { gap: 3px; }
#panel.tight .tile { height: 44px; }
.go.stop { background: linear-gradient(180deg, #3a4b62, #2c3a4e); color: var(--text); box-shadow: 0 3px 0 #1a2433; }

/* 宽度按内容定：靠近舞台右边时整条往左挪（下面 showActionBar 里夹住），不会被挤成一竖条 */
#actionbar { position: absolute; width: max-content; display: flex; gap: 6px; padding: 5px; border-radius: 12px; background: rgba(18, 25, 36, .94); border: 1px solid var(--line); transform: translate(-50%, -100%); }
#actionbar button { min-width: 48px; min-height: 44px; border-radius: 9px; border: 1px solid var(--line); background: #24344a; font-size: 13px; display: inline-flex; align-items: center; gap: 4px; padding: 0 8px; }
#actionbar .flip svg { width: 24px; height: 20px; }
#toast { position: absolute; left: calc(var(--cx) + 8px); bottom: calc(100% - var(--cy) - var(--ch) + 8px); max-width: calc(var(--cw) * 0.42); pointer-events: auto; display: flex; gap: 8px; align-items: flex-start; background: rgba(15, 21, 30, .92); border: 1px solid var(--line); color: var(--text); border-radius: 12px; padding: 8px 12px; font-size: 13px; line-height: 1.5; }
#toast b { color: var(--accent); margin-right: 6px; }
#toast[data-pos="br"], #toast[data-pos="tr"], #toast[data-pos="mr"] { left: auto; right: calc(100% - var(--cx) - var(--cw) + 8px); }
#toast[data-pos="tr"] { bottom: auto; top: calc(var(--cy) + 8px); }
#toast[data-pos="mr"] { bottom: auto; top: calc(var(--cy) + var(--ch) * 0.3); }
#toast .i { flex: none; width: 18px; height: 18px; border-radius: 50%; border: 1.5px solid var(--soft); color: var(--soft); font-size: 12px; font-weight: 700; display: grid; place-items: center; font-style: italic; margin-top: 1px; }

#card { position: absolute; left: calc(var(--cx) + 8px); width: calc(var(--cw) - 16px); top: calc(var(--cy) + 8px); background: rgba(23, 33, 49, .96); border-radius: 14px; border: 1px solid #34465e; box-shadow: 0 8px 28px rgba(0, 0, 0, .45); padding: 9px 14px 9px 10px; display: flex; gap: 12px; align-items: flex-start; animation: drop .25s ease-out; }
#card.bottom { top: auto; bottom: calc(100% - var(--cy) - var(--ch) + 8px); }
@keyframes drop { from { opacity: 0; transform: translateY(-12px); } }
#card .badge { flex: none; width: 40px; height: 40px; border-radius: 50%; display: grid; place-items: center; font-size: 22px; font-weight: 700; }
#card.ok { border-color: rgba(242, 201, 76, .6); }
#card.bad { border-color: rgba(255, 122, 89, .6); }
#card.stop { border-color: var(--line); }
#card.stop .badge { background: #2c3a4e; color: var(--soft); font-size: 16px; }
#card.ok .badge { background: radial-gradient(circle, #fff6c9, #f2c94c); color: #3a2a06; box-shadow: 0 0 0 3px rgba(242, 201, 76, .35); animation: pop .4s ease-out; }
#card.bad .badge { background: var(--bad); color: #2a0f06; clip-path: polygon(50% 4%, 97% 92%, 3% 92%); border-radius: 0; }
@keyframes pop { 0% { transform: scale(.4); } 70% { transform: scale(1.15); } }
#card h2 { font-size: 16px; margin: 0; display: inline; }
#card .sub { font-size: 12px; color: var(--muted); display: inline; margin-left: 8px; }
#card p { margin: 4px 0 0; font-size: 13px; line-height: 1.55; }
#card .kind { font-size: 12px; color: var(--muted); margin-top: 2px; }
.result-btn { flex: none; min-height: 46px; border-radius: 12px; border: 1px solid var(--line); background: #22324a; font-size: 14px; }
.result-btn.primary { background: linear-gradient(180deg, #ffe08a, #f0b84a); color: #3a2a06; border: none; font-weight: 600; box-shadow: 0 3px 0 #a87a22; }

/* ---------- 实验笔记 ---------- */
#notebook { display: flex; flex-direction: column; padding: 12px 20px; background: #f4efe2; color: #2b2a26; }
#notebook header { display: flex; align-items: center; gap: 12px; }
#notebook header h2 { margin: 0; font-size: 20px; }
#notebook header button { min-height: 44px; padding: 0 14px; border-radius: 22px; border: 1px solid #c9bfa8; background: #fffaf0; color: #2b2a26; font-size: 14px; }
.nb-list { flex: 1; overflow-y: auto; touch-action: pan-y; margin-top: 8px; padding-bottom: 10px; display: grid; grid-template-columns: 1fr 1fr; gap: 10px; align-content: start; }
.nb { background: #fffdf7; border: 1px solid #e3dac6; border-radius: 10px; padding: 10px 12px; font-size: 13px; line-height: 1.6; box-shadow: 0 1px 0 #e9e1cf; }
.nb h3 { margin: 0 0 4px; font-size: 14px; }
.nb .tag { display: inline-block; font-size: 12px; padding: 0 6px; border-radius: 6px; background: #efe6d2; color: #6b5f45; margin-left: 4px; }
.nb .tag.rule { background: #e8eef8; color: #3f5677; }
.nb details { margin-top: 4px; color: #4f4a3e; }
.nb summary { cursor: pointer; color: #8a6d2f; min-height: 44px; display: flex; align-items: center; flex-wrap: wrap; }
.nb-empty { color: #8a8170; font-size: 14px; }

#rotate { display: none; position: fixed; inset: 0; background: var(--bg0); color: var(--text); font-size: 18px; flex-direction: column; gap: 10px; align-items: center; justify-content: center; z-index: 9; }
#rotate small { font-size: 13px; color: var(--muted); }
@media (orientation: portrait) and (max-width: 700px) { #rotate { display: flex; } }
