/* laser-reflect 光学实验台视觉
   题材隐喻：俯视光学实验台 + 暗房遮光罩，深蓝紫渐变背景 + 漂浮尘埃微粒。 */

:root {
  --bg-deep: #0b1026;
  --bg-mid: #141b3a;
  --bg-panel: #1a2345;
  --metal: #2a3458;
  --metal-hi: #3a4670;
  --ink: #e6ecff;
  --ink-dim: #9aa5c8;
  --accent: #6db8ff;
  --accent-warm: #ffd166;
  --glow-white: rgba(238, 246, 255, 0.9);
  --glow-red: rgba(255, 77, 94, 0.9);
  --glow-green: rgba(55, 224, 138, 0.9);
  --glow-blue: rgba(77, 155, 255, 0.9);
}

* {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

html,
body {
  height: 100%;
}

body {
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, "PingFang SC", "Microsoft YaHei", sans-serif;
  background:
    radial-gradient(1200px 800px at 50% -10%, rgba(90, 120, 220, 0.25), transparent 60%),
    radial-gradient(1000px 700px at 85% 110%, rgba(160, 80, 200, 0.18), transparent 60%),
    linear-gradient(160deg, var(--bg-deep) 0%, var(--bg-mid) 55%, #0d1230 100%);
  color: var(--ink);
  min-height: 100%;
  overflow-x: hidden;
  -webkit-font-smoothing: antialiased;
  touch-action: manipulation;
}

/* 漂浮尘埃微粒背景 */
body::before {
  content: "";
  position: fixed;
  inset: 0;
  pointer-events: none;
  background-image:
    radial-gradient(2px 2px at 12% 28%, rgba(255, 255, 255, 0.5), transparent),
    radial-gradient(1.5px 1.5px at 68% 72%, rgba(180, 210, 255, 0.5), transparent),
    radial-gradient(2.5px 2.5px at 38% 12%, rgba(255, 220, 180, 0.4), transparent),
    radial-gradient(1.5px 1.5px at 88% 42%, rgba(200, 230, 255, 0.45), transparent),
    radial-gradient(2px 2px at 55% 88%, rgba(255, 255, 255, 0.4), transparent);
  animation: drift 22s linear infinite;
  opacity: 0.6;
}

@keyframes drift {
  0% { transform: translateY(0); }
  50% { transform: translateY(-16px); }
  100% { transform: translateY(0); }
}

#game-app {
  max-width: 760px;
  margin: 0 auto;
  padding: 12px 16px calc(68px + env(safe-area-inset-bottom));
  display: flex;
  flex-direction: column;
  gap: 14px;
  min-height: 100vh;
}

/* —— 顶部精密仪表条 —— */
.instrument-bar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  padding: 10px 14px;
  background: linear-gradient(180deg, var(--metal-hi), var(--metal));
  border-radius: 14px;
  border: 1px solid rgba(120, 150, 220, 0.25);
  box-shadow: 0 4px 18px rgba(0, 0, 0, 0.45), inset 0 1px 0 rgba(255, 255, 255, 0.06);
}

.stage-title {
  font-size: 17px;
  font-weight: 700;
  letter-spacing: 0.5px;
  flex: 1;
  text-align: center;
  background: linear-gradient(180deg, #fff, #bcd0ff);
  -webkit-background-clip: text;
  background-clip: text;
  -webkit-text-fill-color: transparent;
}

.stage-actions {
  display: flex;
  gap: 6px;
}

.bar-btn {
  appearance: none;
  border: 1px solid rgba(120, 150, 220, 0.3);
  background: linear-gradient(180deg, var(--metal-hi), var(--metal));
  color: var(--ink);
  padding: 6px 12px;
  border-radius: 9px;
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
  white-space: nowrap;
  box-shadow: 0 2px 6px rgba(0, 0, 0, 0.35);
  transition: transform 0.08s ease, box-shadow 0.08s ease;
}

.bar-btn:hover {
  box-shadow: 0 3px 10px rgba(0, 0, 0, 0.5);
}

.bar-btn:active {
  transform: translateY(2px);
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.5);
}

#back-home {
  text-decoration: none;
  color: var(--ink-dim);
  font-size: 13px;
}

/* —— 光学实验台框体 —— */
.bench-frame {
  border-radius: 18px;
  background: linear-gradient(180deg, #202a4d, #141a38);
  border: 1px solid rgba(120, 150, 220, 0.28);
  box-shadow: 0 10px 40px rgba(0, 0, 0, 0.55), inset 0 1px 0 rgba(255, 255, 255, 0.07);
  padding: 12px;
}

.bench-lid {
  position: relative;
  border-radius: 12px;
  background:
    radial-gradient(700px 500px at 50% 30%, rgba(70, 100, 180, 0.18), transparent 65%),
    #10162e;
  border: 1px solid rgba(100, 130, 200, 0.22);
  padding: 10px;
  overflow: hidden;
}

/* 元件托盘（左侧立式） */
.tray {
  position: absolute;
  left: 10px;
  top: 50%;
  transform: translateY(-50%);
  display: flex;
  flex-direction: column;
  gap: 8px;
  z-index: 3;
  pointer-events: none;
}

.tray-chip {
  width: 34px;
  height: 34px;
  display: grid;
  place-items: center;
  font-size: 18px;
  color: var(--ink-dim);
  background: rgba(40, 52, 90, 0.7);
  border: 1px solid rgba(120, 150, 220, 0.3);
  border-radius: 8px;
  box-shadow: 0 2px 6px rgba(0, 0, 0, 0.4);
}

/* 光学读数条 */
.readout {
  display: flex;
  gap: 10px;
  justify-content: center;
  margin-bottom: 10px;
  flex-wrap: wrap;
}

.readout-cell {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
  min-width: 60px;
  padding: 6px 10px;
  background: rgba(20, 28, 55, 0.85);
  border: 1px solid rgba(100, 130, 200, 0.22);
  border-radius: 9px;
}

.readout-label {
  font-size: 11px;
  color: var(--ink-dim);
  letter-spacing: 1px;
}

.readout-value {
  font-size: 20px;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
  color: var(--accent);
}

.readout-stars {
  font-size: 16px;
  letter-spacing: 2px;
  color: #5a6a9a;
}

.readout-stars.star-1 { color: var(--accent-warm); text-shadow: 0 0 8px rgba(255, 209, 102, 0.5); }
.readout-stars.star-2 { color: var(--accent-warm); text-shadow: 0 0 8px rgba(255, 209, 102, 0.5); }
.readout-stars.star-3 { color: var(--accent-warm); text-shadow: 0 0 10px rgba(255, 209, 102, 0.7); }

/* 光学台面 */
.board-wrap {
  position: relative;
  display: flex;
  justify-content: center;
  align-items: center;
  height: min(58vh, 520px);
}

.board {
  display: grid;
  position: absolute;
  z-index: 1;
}

.cell {
  background: rgba(18, 26, 52, 0.9);
  border: 1px solid rgba(80, 105, 160, 0.18);
  border-radius: 6px;
  display: grid;
  place-items: center;
  transition: background 0.12s ease;
  cursor: default;
  position: relative;
}

.cell.lit {
  background: rgba(30, 45, 80, 0.95);
}

.cell .piece.rotatable {
  cursor: pointer;
  border-radius: 6px;
}

.cell .piece.rotatable:hover {
  box-shadow: 0 0 0 2px rgba(109, 184, 255, 0.6);
}

.piece {
  display: block;
  width: 100%;
  height: 100%;
}

.piece-svg {
  width: 100%;
  height: 100%;
  display: block;
}

/* 镜子旋转：transition 由 inline transform 驱动（跨渲染累积 +90°，拧旋钮手感） */
.piece.rotatable {
  transition: transform 0.28s cubic-bezier(0.34, 1.56, 0.64, 1);
  will-change: transform;
}

.cell .piece.rotatable:active {
  box-shadow: 0 0 0 3px rgba(109, 184, 255, 0.85);
}

/* 光路 SVG 覆盖层：必须压在格子之上（z-index 2 > board 1），否则光被 90% 不透明格底吞掉 */
.rays-layer {
  position: absolute;
  inset: 0;
  z-index: 2;
  pointer-events: none;
}

/* 光束流入：pathLength=1 归一化，dashoffset 1→0 从源头"流"到终点（时长按像素距离伸缩，inline 覆盖） */
.rays-layer .beam-main {
  stroke-dasharray: 1;
  stroke-dashoffset: 1;
  animation: beam-draw 0.45s ease-out forwards;
}

.rays-layer .beam-glow {
  stroke-dasharray: 1;
  stroke-dashoffset: 1;
  animation: beam-draw 0.45s ease-out forwards;
}

@keyframes beam-draw {
  to { stroke-dashoffset: 0; }
}

/* 常态能量流动：沿光路匀速奔跑的白色亮段（物理像素 dash，速度全路一致） */
.rays-layer .ray-flow {
  animation: beam-flow 0.7s linear infinite;
}

@keyframes beam-flow {
  to { stroke-dashoffset: -24; } /* dash 周期 9+15=24px */
}

/* 抵达冲击波：光到达终点的一刻，光环扩散消散（delay=流入时长，both 填充保证到达前不可见） */
.rays-layer .ray-impact {
  transform-box: fill-box;
  transform-origin: center;
  animation: ray-impact 0.55s cubic-bezier(0.2, 1.3, 0.4, 1) both;
}

@keyframes ray-impact {
  0% { transform: scale(0.2); opacity: 0; }
  18% { transform: scale(0.75); opacity: 0.95; }
  55% { transform: scale(1.15); opacity: 0.8; }
  100% { transform: scale(2.1); opacity: 0; }
}

/* 感光核被点亮：延迟到光束抵达（≈0.42s）才 pop + 辉光淡入，与光路流入同步 */
.piece-target.hit {
  animation:
    target-pop 0.55s cubic-bezier(0.3, 1.5, 0.5, 1) 0.42s backwards,
    target-glow 0.45s ease-out 0.42s forwards;
}

/* 内芯（感光核）：默认熄灭缩成一点，命中后充能点亮并常亮呼吸——亮灭状态一眼可辨 */
.piece-target .target-core {
  opacity: 0;
  transform: scale(0.2);
  transform-box: fill-box;
  transform-origin: center;
}

.piece-target.hit .target-core {
  animation:
    target-charge 0.6s cubic-bezier(0.3, 1.5, 0.5, 1) 0.42s forwards,
    target-breathe 1.8s ease-in-out 1.1s infinite alternate;
}

@keyframes target-charge {
  0% { opacity: 0; transform: scale(0.2); }
  55% { opacity: 1; transform: scale(1.35); }
  100% { opacity: 1; transform: scale(1); }
}

@keyframes target-breathe {
  from { filter: drop-shadow(0 0 3px var(--hit-color, #fff)); }
  to { filter: drop-shadow(0 0 9px var(--hit-color, #fff)); }
}

@keyframes target-pop {
  0% { transform: scale(1); }
  35% { transform: scale(1.3); }
  70% { transform: scale(0.94); }
  100% { transform: scale(1); }
}

@keyframes target-glow {
  to { filter: drop-shadow(0 0 10px var(--hit-color, #fff)); }
}

/* 胜利瞬间：所有靶点同步闪两轮（.board 加 .win 类时触发），填满延迟结算的视觉空档 */
.board.win .piece-target.hit {
  animation:
    target-pop 0.55s cubic-bezier(0.3, 1.5, 0.5, 1) 0.42s 2 backwards,
    target-glow 0.45s ease-out 0.42s forwards;
}

/* 右下角仪器按钮组 */
.controls {
  display: flex;
  gap: 8px;
  justify-content: center;
  margin-top: 12px;
  flex-wrap: wrap;
}

.ctrl-btn {
  appearance: none;
  border: 1px solid rgba(120, 150, 220, 0.3);
  background: linear-gradient(180deg, var(--metal-hi), var(--metal));
  color: var(--ink);
  padding: 9px 16px;
  border-radius: 10px;
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
  box-shadow: 0 3px 8px rgba(0, 0, 0, 0.4), inset 0 1px 0 rgba(255, 255, 255, 0.07);
  transition: transform 0.08s ease, box-shadow 0.08s ease;
}

.ctrl-btn:hover {
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.5);
}

.ctrl-btn:active {
  transform: translateY(2px);
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.5);
}

.ctrl-accent {
  background: linear-gradient(180deg, #4a86d8, #3565b0);
  border-color: rgba(120, 180, 255, 0.5);
  color: #fff;
}

/* —— 选关抽屉 —— */
.level-panel {
  margin-top: 4px;
  background: rgba(16, 22, 46, 0.96);
  border: 1px solid rgba(120, 150, 220, 0.28);
  border-radius: 14px;
  padding: 14px;
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.level-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(56px, 1fr));
  gap: 8px;
}

.level-btn {
  appearance: none;
  border: 1px solid rgba(100, 130, 200, 0.25);
  background: rgba(30, 42, 78, 0.9);
  color: var(--ink);
  border-radius: 10px;
  padding: 10px 4px;
  cursor: pointer;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
  font-size: 15px;
  font-weight: 700;
  box-shadow: 0 2px 5px rgba(0, 0, 0, 0.35);
}

.level-btn:hover {
  border-color: rgba(120, 180, 255, 0.6);
}

.level-btn.current {
  border-color: var(--accent);
  box-shadow: 0 0 0 2px rgba(109, 184, 255, 0.4);
}

.level-btn.locked {
  opacity: 0.35;
  cursor: not-allowed;
}

.level-num {
  font-variant-numeric: tabular-nums;
}

.level-stars {
  font-size: 9px;
  color: var(--accent-warm);
  letter-spacing: 0;
}

/* —— 弹层 —— */
/* 说明弹层：有意模态，保留遮罩 */
.help-layer {
  position: fixed;
  inset: 0;
  z-index: 50;
  display: grid;
  place-items: center;
  background: rgba(5, 8, 20, 0.72);
  backdrop-filter: blur(4px);
  padding: 20px;
}

/* 胜利结算条：非遮挡——固定底部、无遮罩不糊盘面，光路全程可见 */
.result-layer {
  position: fixed;
  left: 0;
  right: 0;
  bottom: calc(12px + env(safe-area-inset-bottom));
  z-index: 40;
  display: flex;
  justify-content: center;
  pointer-events: none; /* 条外区域完全可点 */
  padding: 0 14px;
}

.result-card {
  pointer-events: auto;
  display: flex;
  align-items: center;
  gap: 16px;
  background: linear-gradient(180deg, rgba(35, 45, 85, 0.94), rgba(21, 28, 61, 0.94));
  border: 1px solid rgba(140, 170, 240, 0.4);
  border-radius: 16px;
  padding: 12px 18px;
  max-width: 620px;
  box-shadow: 0 12px 36px rgba(0, 0, 0, 0.55), 0 0 24px rgba(109, 184, 255, 0.15);
  animation: banner-rise 0.38s cubic-bezier(0.22, 1.2, 0.4, 1);
}

@keyframes banner-rise {
  from { transform: translateY(28px); opacity: 0; }
  to { transform: translateY(0); opacity: 1; }
}

.result-text {
  display: flex;
  flex-direction: column;
  gap: 2px;
  min-width: 0;
}

.help-card {
  background: linear-gradient(180deg, #232d55, #151c3d);
  border: 1px solid rgba(140, 170, 240, 0.35);
  border-radius: 18px;
  padding: 28px 24px;
  max-width: 380px;
  width: 100%;
  text-align: center;
  box-shadow: 0 18px 60px rgba(0, 0, 0, 0.6);
  display: flex;
  flex-direction: column;
  gap: 14px;
  align-items: center;
}

.result-stars {
  font-size: 22px;
  letter-spacing: 3px;
  color: #5a6a9a;
  flex-shrink: 0;
}

.result-stars {
  font-size: 34px;
  letter-spacing: 6px;
  color: #5a6a9a;
}

.result-stars.star-1,
.result-stars.star-2,
.result-stars.star-3 {
  color: var(--accent-warm);
  text-shadow: 0 0 16px rgba(255, 209, 102, 0.8);
}

.result-title {
  font-size: 18px;
  font-weight: 800;
  background: linear-gradient(180deg, #fff, #bcd0ff);
  -webkit-background-clip: text;
  background-clip: text;
  -webkit-text-fill-color: transparent;
  white-space: nowrap;
}

.result-sub {
  font-size: 14px;
  color: var(--ink-dim);
}

.help-body {
  font-size: 14px;
  line-height: 1.7;
  color: var(--ink-dim);
  text-align: left;
}

.result-actions {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
  justify-content: center;
}

.hidden {
  display: none !important;
}

/* —— 响应式 —— */
@media (max-width: 768px) {
  #game-app {
    padding-left: 10px;
    padding-right: 10px;
  }
  .tray {
    display: none;
  }
  .stage-title {
    font-size: 15px;
  }
  .bar-btn {
    padding: 5px 9px;
    font-size: 13px;
  }
  .result-card {
    flex-wrap: wrap;
    justify-content: center;
    gap: 10px;
    padding: 10px 14px;
  }
  .result-text {
    align-items: center;
    text-align: center;
  }
  .result-title {
    font-size: 16px;
  }
  .ctrl-btn {
    padding: 7px 12px;
    font-size: 13px;
  }
}

/* 动效降级 */
@media (prefers-reduced-motion: reduce) {
  body::before {
    animation: none;
  }
  .cell,
  .piece.rotatable {
    transition: none;
  }
  .rays-layer .beam-main,
  .rays-layer .beam-glow {
    animation: none;
    stroke-dashoffset: 0; /* 光路必须完整显示 */
  }
  .rays-layer .ray-flow,
  .rays-layer .ray-impact {
    animation: none;
    opacity: 0; /* 纯装饰动效，降级时隐藏 */
  }
  .piece-target.hit,
  .board.win .piece-target.hit {
    animation: none;
    filter: drop-shadow(0 0 8px var(--hit-color, #fff)); /* 静态辉光保留 */
  }
  .piece-target.hit .target-core {
    animation: none;
    /* 降级：已点亮靶的内芯静态常亮，保证亮灭状态差异不丢 */
    opacity: 1;
    transform: scale(1);
    filter: drop-shadow(0 0 6px var(--hit-color, #fff));
  }
  .result-card {
    animation: none;
  }
}
