/* ==============================================================
   聪明格 KenKen · 游戏专属样式
   选择器统一以 .g-kenken / .kk-* 前缀限定
   ============================================================== */

.g-kenken {
  --kk-line: rgba(255, 255, 255, 0.075);
  --kk-cage: rgba(255, 188, 128, 0.9);
  gap: 10px;
}

/* ------------------------------ 棋盘 ------------------------------ */

.kk-board-holder {
  position: relative;
  width: min(100%, 420px);
  width: min(100%, 420px, calc(100dvh - 330px));
  margin: 0 auto;
  flex: none;
}

.kk-board {
  position: relative;
  width: 100%;
  aspect-ratio: 1 / 1;
  display: grid;
  border: 1px solid var(--line-2);
  border-radius: 14px;
  overflow: hidden;
  background: linear-gradient(180deg, rgba(255, 255, 255, 0.055), rgba(255, 255, 255, 0.018));
  box-shadow: 0 22px 48px -28px rgba(0, 0, 0, 0.92), inset 0 1px 0 rgba(255, 255, 255, 0.05);
  touch-action: manipulation;
  transition: box-shadow 0.35s ease;
}
.kk-board.done {
  box-shadow: 0 0 0 2px color-mix(in srgb, var(--ok) 45%, transparent),
    0 22px 48px -28px rgba(0, 0, 0, 0.92);
}

.kk-cell {
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
  min-width: 0;
  min-height: 0;
  padding: 0;
  border-radius: 0;
  border-right: 1px solid var(--kk-line);
  border-bottom: 1px solid var(--kk-line);
  background: transparent;
  color: var(--text);
  overflow: hidden;
  transition: background 0.12s ease;
  touch-action: manipulation;
}
.kk-cell.r { border-right-color: transparent; }
.kk-cell.b { border-bottom-color: transparent; }
.kk-cell:focus { outline: none; }

/* 笼底色（相邻笼由生成时贪心染不同色） */
.kk-cell.kk-t1 { background: rgba(255, 158, 92, 0.06); }
.kk-cell.kk-t2 { background: rgba(255, 255, 255, 0.035); }
.kk-cell.kk-t3 { background: rgba(255, 209, 102, 0.05); }
.kk-cell.kk-t4 { background: rgba(122, 162, 255, 0.045); }
.kk-cell.kk-t5 { background: rgba(183, 140, 255, 0.04); }

/* 虚线笼边界（SVG） */
.kk-cages {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  pointer-events: none;
  z-index: 2;
  overflow: visible;
}
.kk-cages path {
  fill: none;
  stroke: var(--kk-cage);
  stroke-width: 1.6;
  stroke-dasharray: 5 4;
  stroke-linecap: round;
  stroke-linejoin: round;
  vector-effect: non-scaling-stroke;
}

.kk-cage {
  position: absolute;
  top: 2px;
  left: 3px;
  z-index: 3;
  line-height: 1;
  font-weight: 700;
  color: #ffd2a8;
  letter-spacing: 0.01em;
  pointer-events: none;
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
}
.kk-cage.on { color: #fff3e6; }
.kk-cage.bad { color: #ff8a94; }

.kk-v {
  position: relative;
  z-index: 1;
  margin-top: 4px;
  font-weight: 600;
  line-height: 1;
  color: #ffe1c9;
}
.kk-cell.sel .kk-v { color: #fff7f0; }
.kk-cell.conflict .kk-v { color: #ff6b7a; }
.kk-cell.conflict.sel .kk-v { color: #ffa3ac; }

/* 选中 / 同行列 / 同数字 / 同笼 */
.kk-cell.peer { background: rgba(255, 255, 255, 0.045); }
.kk-cell.same { background: color-mix(in srgb, var(--accent) 12%, transparent); }
.kk-cell.cage-sel { background: color-mix(in srgb, var(--accent) 14%, transparent); }
.kk-cell.sel { background: color-mix(in srgb, var(--accent) 28%, transparent); }
.kk-cell.sel::after {
  content: "";
  position: absolute;
  inset: 0;
  border: 2px solid var(--accent);
  border-radius: 5px;
  pointer-events: none;
  z-index: 4;
}
.kk-cell.last { background: color-mix(in srgb, var(--accent) 20%, transparent); }
.kk-cell.over { background: rgba(255, 92, 108, 0.05); }

/* 冲突 / 笼运算不符 */
.kk-cell.conflict { background: rgba(255, 92, 108, 0.16); }
.kk-cell.cage-bad { background: rgba(255, 92, 108, 0.075); }
.kk-cell.cage-bad.conflict { background: rgba(255, 92, 108, 0.18); }

/* 笔记 */
.kk-notes {
  position: absolute;
  inset: 1px 1px 1px 2px;
  display: none;
  grid-template-columns: repeat(3, 1fr);
  grid-auto-rows: 1fr;
  color: #a89b91;
  line-height: 1;
  pointer-events: none;
  z-index: 1;
}
.kk-notes i {
  display: flex;
  align-items: center;
  justify-content: center;
  font-style: normal;
}
.kk-cell.note .kk-notes { display: grid; }
.kk-cell.note .kk-v { display: none; }

/* 字体尺寸按难度缩放 */
.kk-n4 .kk-v { font-size: min(9vw, 36px); }
.kk-n5 .kk-v { font-size: min(7.2vw, 29px); }
.kk-n6 .kk-v { font-size: min(5.8vw, 23px); }
.kk-n4 .kk-cage { font-size: min(3.3vw, 13.5px); }
.kk-n5 .kk-cage { font-size: min(2.9vw, 12px); }
.kk-n6 .kk-cage { font-size: min(2.5vw, 11px); }
.kk-n4 .kk-notes { font-size: min(2.4vw, 10.5px); }
.kk-n5 .kk-notes { font-size: min(2.1vw, 9.5px); }
.kk-n6 .kk-notes { font-size: min(1.9vw, 8.8px); }

/* 暂停遮罩 */
.kk-pause-mask {
  position: absolute;
  inset: 0;
  z-index: 6;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 12px;
  border-radius: 14px;
  background: rgba(8, 11, 18, 0.86);
  backdrop-filter: blur(6px);
  -webkit-backdrop-filter: blur(6px);
  animation: kkFade 0.18s ease both;
}
.kk-pause-t { font-size: 15px; font-weight: 700; letter-spacing: 0.08em; }

/* ------------------------------ 顶部状态 ------------------------------ */

.kk-hud {
  display: flex;
  align-items: center;
  gap: 8px;
  min-height: 30px;
}
.kk-hud .badge { font-size: 11px; padding: 4px 10px; }
.kk-hints {
  font-size: 11px;
  color: var(--faint);
  letter-spacing: 0.02em;
  white-space: nowrap;
}
.kk-time {
  margin-left: auto;
  font-size: 18px;
  font-weight: 800;
  letter-spacing: 0.01em;
  font-variant-numeric: tabular-nums;
  line-height: 1.1;
}

/* ------------------------------ 笼状态条 ------------------------------ */

.kk-cagebar {
  display: flex;
  align-items: center;
  gap: 8px;
  flex: none;
  min-height: 30px;
  padding: 0 11px;
  border-radius: 11px;
  border: 1px solid var(--line);
  background: var(--surface);
  font-size: 11.5px;
  color: var(--dim);
  transition: border-color 0.15s, background 0.15s;
}
.kk-cagebar .kk-cage-v {
  margin-left: auto;
  font-size: 13px;
  font-weight: 800;
  color: var(--text);
  letter-spacing: 0.02em;
  font-variant-numeric: tabular-nums;
}
.kk-cagebar .kk-cage-s {
  min-width: 50px;
  text-align: right;
  font-size: 10.5px;
  color: var(--faint);
}
.kk-cagebar.bad {
  border-color: color-mix(in srgb, var(--err) 50%, transparent);
  background: color-mix(in srgb, var(--err) 10%, transparent);
}
.kk-cagebar.bad .kk-cage-t,
.kk-cagebar.bad .kk-cage-v,
.kk-cagebar.bad .kk-cage-s { color: #ff8a94; }

/* ------------------------------ 工具行 ------------------------------ */

.kk-tools {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 8px;
  flex: none;
}
.kk-tool {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  min-height: 42px;
  padding: 0 6px;
  border-radius: 12px;
  border: 1px solid var(--line);
  background: var(--surface);
  color: var(--dim);
  font-size: 12.5px;
  font-weight: 600;
  transition: transform 0.08s, background 0.15s, color 0.15s, border-color 0.15s;
  touch-action: manipulation;
}
.kk-tool:active { transform: scale(0.96); background: var(--surface-2); }
.kk-tool.on {
  color: var(--accent);
  border-color: color-mix(in srgb, var(--accent) 48%, transparent);
  background: color-mix(in srgb, var(--accent) 13%, transparent);
}
.kk-tool-i { display: inline-flex; width: 17px; height: 17px; flex: none; }
.kk-tool-i svg { width: 17px; height: 17px; }

/* ------------------------------ 数字键盘 ------------------------------ */

.kk-pad {
  display: grid;
  gap: 7px;
  flex: none;
}
.kk-pad.p4 { grid-template-columns: repeat(5, 1fr); }
.kk-pad.p5 { grid-template-columns: repeat(6, 1fr); }
.kk-pad.p6 { grid-template-columns: repeat(4, 1fr); }
.kk-key {
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 48px;
  border-radius: 12px;
  border: 1px solid var(--line);
  background: var(--surface);
  color: var(--text);
  transition: transform 0.08s, background 0.12s, border-color 0.12s, opacity 0.15s;
  touch-action: manipulation;
}
.kk-key:active { transform: scale(0.95); background: var(--surface-3); }
.kk-key.done { opacity: 0.32; }
.kk-key-v {
  font-size: 20px;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
  line-height: 1;
}
.kk-key .kk-remain {
  position: absolute;
  top: 4px;
  right: 6px;
  font-size: 8.5px;
  font-weight: 600;
  color: var(--faint);
  line-height: 1;
}
.kk-key.erase .kk-key-v { font-size: 12.5px; font-weight: 600; color: var(--dim); }
.kk-key.erase { border-style: dashed; }

/* ------------------------------ 开始页 ------------------------------ */

.kk-start { gap: 12px; }
.kk-startp { display: flex; flex-direction: column; }
.kk-start .seg-item { min-height: 42px; font-size: 13px; }
.kk-desc {
  margin-top: 9px;
  font-size: 11.5px;
  color: var(--faint);
  letter-spacing: 0.01em;
}
.kk-oplegend {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 6px;
  margin-top: 11px;
}
.kk-oplegend span {
  padding: 6px 2px;
  border-radius: 9px;
  border: 1px solid var(--line);
  background: var(--surface);
  color: var(--faint);
  font-size: 10px;
  text-align: center;
  white-space: nowrap;
}
.kk-oplegend b { color: var(--accent); font-weight: 800; margin-right: 1px; }
.kk-bestlist { display: flex; flex-direction: column; margin-top: 2px; }
.kk-brow {
  display: grid;
  grid-template-columns: 1fr auto auto;
  align-items: center;
  gap: 10px;
  padding: 6px 2px;
  border-bottom: 1px solid var(--line);
  border-radius: 8px;
}
.kk-brow:last-child { border-bottom: 0; }
.kk-brow .n { font-size: 12.5px; color: var(--dim); }
.kk-brow .b { font-size: 12.5px; font-weight: 700; font-variant-numeric: tabular-nums; }
.kk-brow .c { font-size: 10.5px; color: var(--faint); min-width: 36px; text-align: right; }
.kk-brow.cur { background: color-mix(in srgb, var(--accent) 9%, transparent); }
.kk-brow.cur .n { color: var(--text); font-weight: 600; }
.kk-start-btn { margin-top: 14px; min-height: 46px; }
.kk-foot { margin-top: auto; padding-top: 8px; text-align: center; font-size: 11px; }

/* ------------------------------ 生成中 ------------------------------ */

.kk-genwrap { align-items: center; justify-content: center; }
.kk-gen {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 12px;
  padding: 26px;
  text-align: center;
}
.kk-spinner {
  width: 34px;
  height: 34px;
  border-radius: 50%;
  border: 2.5px solid rgba(255, 255, 255, 0.13);
  border-top-color: var(--accent);
  animation: kkSpin 0.8s linear infinite;
}
.kk-gen-t { font-size: 14px; font-weight: 700; letter-spacing: 0.04em; }

/* ------------------------------ 结算 ------------------------------ */

.kk-result {
  flex: none;
  border-radius: var(--radius);
  border: 1px solid color-mix(in srgb, var(--ok) 32%, var(--line));
  background: linear-gradient(165deg,
      color-mix(in srgb, var(--ok) 11%, transparent),
      rgba(255, 255, 255, 0.02));
  padding: 13px var(--pad) 14px;
  display: flex;
  flex-direction: column;
  gap: 11px;
  animation: kkUp 0.32s cubic-bezier(0.22, 0.9, 0.3, 1) both;
}
.kk-result-head {
  display: flex;
  align-items: flex-end;
  justify-content: space-between;
  gap: 12px;
}
.kk-result-k {
  display: flex;
  align-items: center;
  gap: 7px;
  font-size: 11.5px;
  color: var(--dim);
  letter-spacing: 0.04em;
}
.kk-dot {
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: var(--ok);
  box-shadow: 0 0 8px var(--ok);
  flex: none;
}
.kk-result-time {
  margin-top: 3px;
  font-size: 30px;
  font-weight: 800;
  letter-spacing: -0.02em;
  line-height: 1.1;
  font-variant-numeric: tabular-nums;
}
.kk-result-meta {
  text-align: right;
  font-size: 11.5px;
  line-height: 1.7;
  color: var(--dim);
  font-variant-numeric: tabular-nums;
}
.kk-best { color: var(--dim); }
.kk-best.new { color: var(--ok); font-weight: 700; }
.kk-result .btn-row { gap: 8px; }
.kk-result-sub { display: flex; gap: 8px; }
.kk-result-sub .btn { flex: 1; }

/* ------------------------------ 动画 ------------------------------ */

@keyframes kkSpin { to { transform: rotate(360deg); } }
@keyframes kkFade { from { opacity: 0; } to { opacity: 1; } }
@keyframes kkUp {
  from { opacity: 0; transform: translateY(10px); }
  to { opacity: 1; transform: none; }
}

/* ------------------------------ 小屏 / 矮屏适配 ------------------------------ */

@media (max-height: 710px) {
  .g-kenken { gap: 8px; }
  .kk-tool { min-height: 38px; }
  .kk-key { min-height: 44px; }
  .kk-pad { gap: 6px; }
  .kk-hud { min-height: 26px; }
  .kk-cagebar { min-height: 27px; }
  .kk-result { padding: 11px var(--pad) 12px; gap: 9px; }
  .kk-result-time { font-size: 26px; }
  .kk-start { gap: 10px; }
  .kk-start .panel { padding: 12px; }
  .kk-brow { padding: 5px 2px; }
  .kk-start-btn { margin-top: 10px; }
  .kk-foot { display: none; }
  .kk-oplegend { margin-top: 8px; }
}

@media (max-width: 365px) {
  .g-kenken { gap: 8px; }
  .kk-key { min-height: 45px; }
  .kk-tool { font-size: 12px; }
  .kk-cagebar { font-size: 11px; }
  .kk-oplegend span { font-size: 9.5px; }
}

@media (prefers-reduced-motion: reduce) {
  .kk-spinner { animation-duration: 2s; }
  .kk-result { animation: none; }
}
