:root {
  color-scheme: light;
  font-family: "Microsoft YaHei UI", "Noto Sans CJK SC", "PingFang SC", sans-serif;
  color: #20304b;
  background: #f3f9ff;
  font-synthesis: none;
  text-rendering: optimizeLegibility;
  --ink: #20304b;
  --muted: #62738b;
  --teal: #008e9b;
  --teal-dark: #006d78;
  --line: #dce8f2;
}

* {
  box-sizing: border-box;
}

body {
  min-width: 320px;
  min-height: 100vh;
  margin: 0;
  background:
    radial-gradient(circle at 10% 6%, rgba(255, 229, 154, 0.38), transparent 23rem),
    radial-gradient(circle at 92% 14%, rgba(166, 235, 231, 0.38), transparent 25rem),
    #f3f9ff;
}

button,
input {
  font: inherit;
}

button:focus-visible,
input:focus-visible + .toggle-track {
  outline: 4px solid #142e57;
  outline-offset: 4px;
}

.page-shell {
  width: min(100% - 16px, 1360px);
  height: 100dvh;
  min-height: 420px;
  margin: 0 auto;
  padding: 8px 0;
  display: flex;
}

.game-panel {
  display: flex;
  flex: 1;
  flex-direction: column;
  min-height: 0;
  padding: clamp(8px, 1.2vw, 14px);
  border: 1px solid rgba(213, 228, 240, 0.9);
  border-radius: 24px;
  background: rgba(255, 255, 255, 0.95);
  box-shadow: 0 18px 48px rgba(50, 81, 116, 0.11);
}

.game-controls {
  display: flex;
  align-items: center;
  gap: clamp(8px, 1.2vw, 16px);
  margin-bottom: 9px;
}

.progress-area {
  flex: 1;
  min-width: 0;
}

.progress-labels {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 8px;
}

.section-label {
  color: #566982;
  font-size: 0.86rem;
  font-weight: 800;
}

#progress-text {
  color: #006d78;
  font-size: 1rem;
  font-weight: 900;
  font-variant-numeric: tabular-nums;
}

.progress-track {
  height: 8px;
  overflow: hidden;
  border-radius: 99px;
  background: #e9f0f6;
}

.progress-fill {
  display: block;
  width: 0;
  height: 100%;
  border-radius: inherit;
  background: linear-gradient(90deg, #25bdad, #59c9df);
  transition: width 380ms ease;
}

.game-action-buttons {
  display: flex;
  align-items: center;
  gap: 7px;
}

.restart-button {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-height: 38px;
  padding: 0 11px;
  border: 2px solid #c9dce8;
  border-radius: 15px;
  background: #fff;
  color: #29445f;
  cursor: pointer;
  font-size: 0.95rem;
  font-weight: 800;
  white-space: nowrap;
  transition: background-color 150ms ease, border-color 150ms ease, transform 150ms ease;
}

.admin-entry-button {
  text-decoration: none;
}

.restart-button:hover {
  border-color: #70b9c3;
  background: #f0fbfc;
}

.restart-button:active {
  transform: scale(0.97);
}

.restart-button span {
  display: inline-block;
  margin-right: 4px;
  color: #008e9b;
  font-size: 1.2rem;
  vertical-align: -1px;
}

.comfort-controls {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 8px 12px;
}

.toggle-control {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  color: #50647c;
  cursor: pointer;
  font-size: 0.78rem;
  font-weight: 700;
  user-select: none;
}

.toggle-control input {
  position: absolute;
  width: 1px;
  height: 1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  clip-path: inset(50%);
}

.toggle-track {
  display: inline-flex;
  align-items: center;
  width: 34px;
  height: 20px;
  padding: 3px;
  border-radius: 99px;
  background: #cbd6e1;
  transition: background-color 160ms ease;
}

.toggle-track span {
  width: 14px;
  height: 14px;
  border-radius: 50%;
  background: #fff;
  box-shadow: 0 1px 3px rgba(25, 45, 67, 0.22);
  transition: transform 160ms ease;
}

.toggle-control input:checked + .toggle-track {
  background: #178b8b;
}

.toggle-control input:checked + .toggle-track span {
  transform: translateX(14px);
}

.game-grid {
  display: grid;
  flex: 1;
  min-height: 0;
  /* 列数与行数由 game.js 按关卡布局写入，默认值与未设置时的 4×4 一致 */
  grid-template-columns: repeat(var(--board-cols, 4), minmax(0, 1fr));
  grid-template-rows: repeat(var(--board-rows, 4), minmax(0, 1fr));
  gap: clamp(6px, 0.9vw, 12px);
}

.tile-button {
  position: relative;
  display: grid;
  min-width: 0;
  height: 100%;
  min-height: 0;
  place-items: center;
  overflow: visible;
  padding: clamp(3px, 0.55vw, 8px);
  border: 3px solid #40aab1;
  border-radius: clamp(15px, 3vw, 23px);
  background: linear-gradient(145deg, #fff, #e8fafa);
  box-shadow: 0 5px 0 rgba(64, 170, 177, 0.3),
    0 7px 15px rgba(44, 72, 98, 0.07);
  color: #20304b;
  cursor: pointer;
  touch-action: manipulation;
  transition: transform 140ms ease, translate 140ms ease, scale 140ms ease,
    box-shadow 140ms ease, background-color 140ms ease, border-color 140ms ease,
    border-width 140ms ease;
}

/* 悬停效果排除已选中的卡片，避免 hover 的位移覆盖选中态的浮起效果 */
.tile-button:hover:not(:disabled):not(.is-selected) {
  transform: translateY(-3px);
  box-shadow: 0 8px 0 rgba(64, 170, 177, 0.28),
    0 13px 20px rgba(44, 72, 98, 0.11);
}

.tile-button:active:not(:disabled) {
  transform: translateY(1px) scale(0.98);
}

.tile-image-frame {
  position: absolute;
  z-index: 0;
  inset: clamp(6px, 0.7vw, 12px);
  display: flex;
  min-width: 0;
  min-height: 0;
  align-items: center;
  justify-content: center;
  overflow: hidden;
  border-radius: 11px;
  transition: inset 200ms ease;
}

.tile-image {
  display: block;
  width: 100%;
  height: 100%;
  object-fit: contain;
  object-position: center;
  pointer-events: none;
  user-select: none;
  transition: transform 180ms ease, filter 180ms ease;
}

/* 选中态用五重信号表达：强对比暖色边框、多层荧光光晕、浮起放大、实心角标、图案放大。
   全部只作用于选中卡自身，不改动其他卡片，避免干扰“选两张”时的棋盘扫视。
   位移与缩放走 translate／scale 独立属性：它们与操作 transform 的动画相乘叠加，
   不会被 success-pop、tile-clear 接管时吃掉，保证配对成功与消除的动画保持完整。
   荧光允许向外扩散压住相邻卡片边缘，靠 z-index 保证盖在上层。 */
.tile-button.is-selected {
  z-index: 3;
  --selected-lift: -8px;
  --selected-scale: 1.05;
  border-color: #f0a326;
  border-width: 6px;
  /* 斜条纹饱和琥珀橙：条纹纹理在窄彩环里也能形成明显色块，比纯色渐变更有冲击力 */
  background: repeating-linear-gradient(
    45deg,
    #ffc93c 0 9px,
    #ffab00 9px 18px
  );
  box-shadow: inset 0 0 0 3px rgba(255, 255, 255, 0.62),
    0 0 0 3px rgba(255, 206, 84, 0.5),
    0 0 14px 4px rgba(255, 178, 34, 0.62),
    0 0 28px 10px rgba(255, 153, 0, 0.26),
    0 10px 0 rgba(139, 85, 12, 0.3),
    0 16px 22px rgba(44, 72, 98, 0.16);
  translate: 0 var(--selected-lift);
  scale: var(--selected-scale);
  animation: select-pop 260ms ease-out, select-glow 1600ms ease-in-out 260ms infinite;
}

/* 角标保持 12px 空心点到 22px 实心圆的升级，与成功勾、失败叉形成点／勾／叉三态区分 */
.tile-button.is-selected::before {
  position: absolute;
  z-index: 2;
  top: 4px;
  right: 4px;
  width: 22px;
  height: 22px;
  border: 3px solid #fff;
  border-radius: 50%;
  background: #ba7517;
  content: "";
}

/* 选中时把图案框向内收，让彩环从约 8px 扩到约 16px；配合 frame 的 transition 平滑展开 */
.tile-button.is-selected .tile-image-frame {
  inset: clamp(11px, 1.3vw, 20px);
}

/* 选中卡的图案自身放大提亮，属于卡片内部信号，不占用棋盘间隙 */
.tile-button.is-selected .tile-image {
  transform: scale(1.08);
  filter: brightness(1.06) saturate(1.12);
}

.tile-button.is-success {
  z-index: 2;
  border-color: #168957;
  background: linear-gradient(145deg, #f4fff7, #d4fae5);
  box-shadow: 0 0 0 5px rgba(44, 191, 112, 0.21), 0 7px 0 rgba(20, 137, 87, 0.3),
    0 0 26px rgba(45, 205, 122, 0.48);
  animation: success-pop 550ms ease both;
}

.tile-button.is-success::before {
  position: absolute;
  z-index: 2;
  top: 6px;
  right: 7px;
  display: grid;
  width: 25px;
  height: 25px;
  place-items: center;
  border-radius: 50%;
  background: #138452;
  color: #fff;
  content: "✓";
  font-size: 0.95rem;
  font-weight: 900;
}

.tile-button.is-mismatch {
  z-index: 2;
  border-color: #d16a2e;
  background: #fff1e7;
  box-shadow: 0 0 0 5px rgba(240, 142, 63, 0.2), 0 7px 0 rgba(192, 97, 42, 0.28);
  animation: mismatch-pulse 460ms ease 2;
}

.tile-button.is-mismatch::before {
  position: absolute;
  z-index: 2;
  top: 6px;
  right: 7px;
  display: grid;
  width: 25px;
  height: 25px;
  place-items: center;
  border-radius: 50%;
  background: #c25a29;
  color: #fff;
  content: "×";
  font-size: 1.1rem;
  font-weight: 900;
}

.tile-button.is-clearing {
  animation: tile-clear 460ms cubic-bezier(0.2, 0.75, 0.25, 1) forwards;
  pointer-events: none;
}

.tile-button.is-removed {
  visibility: hidden;
  pointer-events: none;
}

.sparkle {
  position: absolute;
  z-index: 3;
  top: 50%;
  left: 50%;
  color: #e8ad2e;
  font-size: 1.05rem;
  line-height: 1;
  pointer-events: none;
  animation: sparkle-burst 680ms ease-out forwards;
}

.feedback {
  display: flex;
  align-items: center;
  gap: 12px;
  min-height: 50px;
  margin-top: 9px;
  padding: 7px 12px;
  border: 2px solid #dce9f1;
  border-radius: 19px;
  background: #f7fbff;
  color: #37516a;
  transition: background-color 180ms ease, border-color 180ms ease, transform 180ms ease;
}

.feedback-symbol {
  display: grid;
  flex: 0 0 28px;
  width: 28px;
  height: 28px;
  place-items: center;
  border-radius: 50%;
  background: #e5eef5;
  color: #5b7890;
  font-size: 0.9rem;
  font-weight: 900;
}

.feedback p {
  margin: 0;
  font-size: clamp(0.94rem, 2.7vw, 1.08rem);
  font-weight: 800;
  line-height: 1.4;
}

.feedback--success {
  border-color: #78d3a1;
  background: #edfff4;
  color: #12653f;
  animation: message-pop 360ms ease both;
}

.feedback--success .feedback-symbol {
  background: #15945b;
  color: #fff;
  font-size: 1.15rem;
}

.feedback--mismatch {
  border-color: #f0b27e;
  background: #fff7ee;
  color: #8e491f;
}

.feedback--mismatch .feedback-symbol {
  background: #c66c32;
  color: #fff;
  font-size: 1.15rem;
}

.feedback--complete {
  border-color: #f0ce6b;
  background: #fff9df;
  color: #72510d;
  animation: message-pop 450ms ease both;
}

.feedback--complete .feedback-symbol {
  background: #eab83e;
  color: #fff;
}

.level-dialog {
  width: min(92vw, 520px);
  max-height: min(82dvh, 620px);
  padding: 20px;
  border: 3px solid #53aeb3;
  border-radius: 24px;
  background: #fff;
  color: var(--ink);
  box-shadow: 0 24px 70px rgba(30, 57, 79, 0.3);
}

.level-dialog::backdrop {
  background: rgba(25, 43, 62, 0.56);
}

.level-dialog-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 14px;
}

.level-dialog-header h2 {
  margin: 0;
  font-size: 1.45rem;
}

.dialog-close-button {
  display: grid;
  width: 42px;
  height: 42px;
  place-items: center;
  border: 0;
  border-radius: 13px;
  background: #eaf4f6;
  color: #285366;
  cursor: pointer;
  font-size: 1.8rem;
  font-weight: 800;
  line-height: 1;
}

.game-level-list {
  display: grid;
  max-height: calc(min(82dvh, 620px) - 110px);
  gap: 10px;
  overflow-y: auto;
}

.game-level-option {
  min-height: 58px;
  padding: 10px 16px;
  border: 3px solid #54aeb3;
  border-radius: 16px;
  background: #e8fafa;
  color: #203d52;
  cursor: pointer;
  font: inherit;
  font-size: 1.1rem;
  font-weight: 850;
  text-align: left;
}

.game-level-option:hover,
.game-level-option[aria-current="true"] {
  border-color: #087d92;
  background: #d9f5f6;
  box-shadow: 0 0 0 3px rgba(21, 172, 191, 0.2);
}

.game-level-empty {
  margin: 8px 2px;
  color: #50647c;
  font-size: 1rem;
  font-weight: 700;
  line-height: 1.6;
}

@keyframes success-pop {
  0% { transform: scale(1); }
  42% { transform: scale(1.065); }
  72% { transform: scale(0.99); }
  100% { transform: scale(1); }
}

@keyframes mismatch-pulse {
  0%, 100% { transform: scale(1); }
  50% { transform: scale(0.97); }
}

/* 消除动画显式接管 translate／scale／transform：若只动 transform，它会与选中态的
   独立属性相乘，前半段几乎没有变化；显式接管后节奏重排为
   “保持浮起弹起 10% → 缩小、落回、淡出同步完成”。 */
@keyframes tile-clear {
  0% {
    opacity: 1;
    translate: 0 var(--selected-lift, 0px);
    scale: var(--selected-scale, 1);
    transform: scale(1) rotate(0);
  }
  40% {
    opacity: 1;
    translate: 0 var(--selected-lift, 0px);
    scale: var(--selected-scale, 1);
    transform: scale(1.1) rotate(2deg);
  }
  100% {
    opacity: 0;
    translate: 0 0;
    scale: 1;
    transform: scale(0.4) rotate(-7deg);
  }
}

/* 弹入动画同样走 translate／scale，避免与操作 transform 的消除动画抢同一属性 */
@keyframes select-pop {
  0% { translate: 0 0; scale: 1; }
  60% { translate: 0 -10px; scale: 1.075; }
  100% { translate: 0 var(--selected-lift); scale: var(--selected-scale); }
}

/* 荧光在强弱之间呼吸；0% 帧与选中态的声明值一致，避免动画开始时跳变 */
@keyframes select-glow {
  0%, 100% {
    box-shadow: inset 0 0 0 3px rgba(255, 255, 255, 0.62),
      0 0 0 3px rgba(255, 206, 84, 0.5),
      0 0 14px 4px rgba(255, 178, 34, 0.62),
      0 0 28px 10px rgba(255, 153, 0, 0.26),
      0 10px 0 rgba(139, 85, 12, 0.3),
      0 16px 22px rgba(44, 72, 98, 0.16);
  }
  50% {
    box-shadow: inset 0 0 0 3px rgba(255, 255, 255, 0.62),
      0 0 0 4px rgba(255, 206, 84, 0.78),
      0 0 24px 8px rgba(255, 178, 34, 0.88),
      0 0 46px 18px rgba(255, 153, 0, 0.44),
      0 10px 0 rgba(139, 85, 12, 0.3),
      0 16px 22px rgba(44, 72, 98, 0.16);
  }
}

@keyframes sparkle-burst {
  0% { opacity: 0; transform: translate(-50%, -50%) scale(0.25); }
  20% { opacity: 1; }
  100% { opacity: 0; transform: translate(calc(-50% + var(--spark-x)), calc(-50% + var(--spark-y))) scale(0.2) rotate(55deg); }
}

@keyframes message-pop {
  0% { transform: scale(0.98); }
  55% { transform: scale(1.015); }
  100% { transform: scale(1); }
}

@media (max-width: 560px) {
  .page-shell {
    width: calc(100% - 10px);
    padding: 5px 0;
  }

  .game-panel {
    padding: 6px;
    border-radius: 18px;
  }

  .game-controls {
    display: grid;
    grid-template-columns: minmax(0, 1fr) auto;
    gap: 5px 8px;
    margin-bottom: 6px;
  }

  .progress-area {
    grid-column: 1;
    grid-row: 1;
  }

  .progress-labels {
    margin-bottom: 0;
  }

  .progress-track {
    height: 5px;
    margin-top: 4px;
  }

  .section-label {
    display: none;
  }

  .comfort-controls {
    grid-column: 1 / -1;
    justify-content: flex-end;
    gap: 8px 12px;
    padding-top: 2px;
  }

  .game-action-buttons {
    grid-column: 2;
    grid-row: 1;
    gap: 4px;
  }

  .game-action-buttons .restart-button {
    white-space: nowrap;
  }

  .toggle-control {
    font-size: 0.72rem;
  }

  .restart-button {
    min-height: 34px;
    padding: 0 7px;
    font-size: 0.72rem;
  }

  .restart-button span {
    margin-right: 2px;
    font-size: 1rem;
  }

  .tile-button {
    border-width: 2px;
    border-radius: 12px;
  }

  /* 窄屏卡片更小，同步收窄选中态的边框、位移与角标，避免边框侵占图案 */
  .tile-button.is-selected {
    --selected-lift: -5px;
    --selected-scale: 1.04;
    border-width: 3px;
  }

  .tile-button.is-selected::before {
    top: 3px;
    right: 3px;
    width: 16px;
    height: 16px;
    border-width: 2px;
  }

  /* 窄屏卡片小，彩环加宽幅度同步收紧，避免图案被压得太小 */
  .tile-button.is-selected .tile-image-frame {
    inset: clamp(9px, 3vw, 14px);
  }

  .tile-button.is-success::before,
  .tile-button.is-mismatch::before {
    top: 3px;
    right: 3px;
    width: 20px;
    height: 20px;
    font-size: 0.82rem;
  }

  .feedback {
    gap: 9px;
    min-height: 44px;
    margin-top: 6px;
    padding: 6px 8px;
  }

  .feedback-symbol {
    flex-basis: 24px;
    width: 24px;
    height: 24px;
  }

  .level-dialog {
    padding: 15px;
    border-radius: 20px;
  }
}

@media (max-height: 520px) {
  .page-shell {
    min-height: 360px;
  }

  .progress-track {
    height: 6px;
  }

  .feedback {
    min-height: 36px;
    margin-top: 4px;
    padding: 4px 8px;
  }
}
