:root {
  color-scheme: dark;
}

* {
  box-sizing: border-box;
  touch-action: manipulation;
}

html, body {
  margin: 0;
  padding: 0;
  height: 100dvh;
  overflow: hidden;
  background: #0d0b09;
  color: #f5f5f5;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Apple SD Gothic Neo", "Noto Sans KR", sans-serif;
  -webkit-font-smoothing: antialiased;
  -webkit-tap-highlight-color: transparent;
  user-select: none;
  -webkit-user-select: none;
  overscroll-behavior: none;
}

main {
  height: 100%;
  max-width: 480px;
  margin: 0 auto;
  display: flex;
  flex-direction: column;
  padding:
    max(60px, env(safe-area-inset-top))
    16px
    max(20px, env(safe-area-inset-bottom));
  gap: 16px;
}

/* 점수바: SCORE | BEST | New 버튼 */
.score-bar {
  display: flex;
  gap: 8px;
  align-items: stretch;
}

.score-box {
  flex: 1;
  background: #161616;
  border: 1px solid #2a2a2a;
  border-radius: 12px;
  padding: 8px 12px;
  text-align: center;
  min-width: 0;
}

.score-label {
  display: block;
  font-size: 11px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: #a0a0a0;
}

.score-val,
.best-val {
  display: block;
  font-size: 22px;
  font-weight: 800;
  font-variant-numeric: tabular-nums;
  letter-spacing: -0.02em;
  color: #f5f5f5;
  margin-top: 2px;
  line-height: 1.1;
}

.new-btn {
  flex: 0 0 auto;
  font: inherit;
  color: #fff;
  background: #4f9eff;
  border: none;
  border-radius: 12px;
  padding: 0 18px;
  min-height: 56px;
  cursor: pointer;
  font-weight: 700;
  font-size: 14px;
  -webkit-tap-highlight-color: transparent;
}

.new-btn:active { background: #3d8ae8; }

/* ── Bead Abacus ─────────────────────────────────────────────── */
#game {
  flex: 1;
  min-height: 0;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.ab-info {
  display: flex;
  align-items: center;
  gap: 8px;
  min-height: 26px;
  font-size: 13px;
  font-weight: 700;
  color: #c9c3b8;
}
.ab-q { font-variant-numeric: tabular-nums; white-space: nowrap; }
.ab-mode {
  padding: 3px 10px;
  border-radius: 999px;
  font-size: 12px;
  letter-spacing: 0.04em;
  text-transform: uppercase;
  background: #2b2116;
  color: #ffd08a;
  border: 1px solid #6b4a26;
}
.ab-mode[data-kind="set"] { background: #10272a; color: #7fe6d6; border-color: #23605a; }
.ab-lives { margin-left: auto; display: inline-flex; gap: 5px; }
.ab-lives i {
  width: 18px; height: 11px;
  clip-path: polygon(0 50%, 25% 0, 75% 0, 100% 50%, 75% 100%, 25% 100%);
  background: linear-gradient(#ffd08a, #c47e34);
  transition: opacity .25s, transform .25s;
}
.ab-lives i.lost { opacity: 0.18; transform: scale(0.8); filter: grayscale(1); }

.ab-timebar {
  height: 6px;
  border-radius: 999px;
  background: #1d1a16;
  overflow: hidden;
}
.ab-timebar i {
  display: block;
  height: 100%;
  background: linear-gradient(90deg, #e2a450, #ffd08a);
  transform-origin: left center;
  border-radius: 999px;
}
.ab-timebar.low i { background: linear-gradient(90deg, #ff6b6b, #ffa07a); }

.ab-prompt {
  margin: 0;
  text-align: center;
  font-size: 14px;
  font-weight: 600;
  color: #e8e1d4;
  height: 2.7em;               /* always two lines high: the frame never jumps */
  line-height: 1.35;
  display: flex;
  align-items: center;
  justify-content: center;
  overflow: hidden;
}
.ab-prompt.legend { color: #ffd08a; font-size: 13px; }

.ab-stage {
  position: relative;
  flex: 1;
  min-height: 150px;
}
.ab-canvas {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  touch-action: none;
}
.ab-stage.shake { animation: ab-shake .32s ease; }
@keyframes ab-shake {
  0%, 100% { transform: none; }
  25% { transform: translateX(-6px); }
  50% { transform: translateX(5px); }
  75% { transform: translateX(-3px); }
}
.ab-float {
  position: absolute;
  left: 50%;
  top: 0;
  transform: translate(-50%, -4px);
  margin: 0;
  padding: 4px 12px;
  border-radius: 999px;
  font-size: 14px;
  font-weight: 800;
  white-space: nowrap;
  pointer-events: none;
  opacity: 0;
  background: rgba(10, 10, 10, 0.85);
  font-variant-numeric: tabular-nums;
}
.ab-float.show { animation: ab-pop 1.4s ease forwards; }
.ab-float.hi { color: #5ee0a0; border: 1px solid rgba(94, 224, 160, 0.5); }
.ab-float.lo { color: #ff8a8a; border: 1px solid rgba(255, 107, 107, 0.5); }
.ab-float.more { color: #ffd08a; border: 1px solid rgba(255, 208, 138, 0.5); }
@keyframes ab-pop {
  0% { opacity: 0; transform: translate(-50%, 6px); }
  12% { opacity: 1; transform: translate(-50%, -4px); }
  80% { opacity: 1; }
  100% { opacity: 0; transform: translate(-50%, -10px); }
}

.ab-panel { flex: 0 0 auto; height: 104px; position: relative; }
.ab-keys {
  display: grid;
  grid-template-columns: repeat(6, 1fr);
  grid-template-rows: 1fr 1fr;
  gap: 6px;
  height: 100%;
}
.ab-key {
  font: inherit;
  font-size: 20px;
  font-weight: 800;
  color: #f4ede2;
  background: linear-gradient(#2a241e, #1c1814);
  border: 1px solid #3d342a;
  border-radius: 12px;
  min-height: 44px;
  cursor: pointer;
  font-variant-numeric: tabular-nums;
  -webkit-tap-highlight-color: transparent;
  transition: transform .08s, background .08s;
}
.ab-key.ab-fn { color: #ffd08a; font-size: 18px; }
.ab-key.down, .ab-key:active { transform: scale(0.94); background: #3a3026; }
.ab-setpanel {
  position: absolute;
  inset: 0;
  display: none;
  align-items: center;
  justify-content: center;
}
.ab-clear {
  font: inherit;
  font-size: 16px;
  font-weight: 800;
  color: #7fe6d6;
  background: #10272a;
  border: 1px solid #23605a;
  border-radius: 999px;
  min-height: 52px;
  padding: 0 32px;
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
}
.ab-clear:active { transform: scale(0.96); }
.ab-panel[data-mode="set"] .ab-keys { visibility: hidden; }
.ab-panel[data-mode="set"] .ab-setpanel { display: flex; }

/* start card */
.ab-start { gap: 14px; }
.ab-start-top { display: flex; gap: 14px; align-items: center; text-align: left; }
.ab-start-top figure { margin: 0; display: flex; flex-direction: column; align-items: center; gap: 4px; flex: 0 0 auto; }
.ab-start-top figcaption { font-size: 11px; color: #c9c3b8; text-align: center; line-height: 1.3; }
.ab-start-top figcaption b { color: #ffd08a; font-size: 13px; }
.ab-intro { margin: 0; font-size: 14px; line-height: 1.45; color: #e8e1d4; }
.ab-plan { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 8px; text-align: left; width: 100%; }
.ab-plan li { display: flex; gap: 10px; align-items: center; font-size: 13px; line-height: 1.35; color: #d8d2c6; }
.ab-plan .k {
  flex: 0 0 24px; height: 24px; border-radius: 50%;
  display: inline-flex; align-items: center; justify-content: center;
  font-size: 12px; color: #0a0a0a; background: #ffd08a;
}
.ab-plan .k.set { background: #7fe6d6; }
.ab-plan .k.life { background: #c9c3b8; }
.overlay-card .start-btn { background: #e2a450; border-color: #e2a450; color: #1a120c; font-weight: 800; }

/* result summary */
.ab-sum { display: flex; gap: 8px; width: 100%; }
.ab-sum div { flex: 1; background: #1d1a16; border-radius: 10px; padding: 8px 4px; display: flex; flex-direction: column; gap: 2px; }
.ab-sum span { font-size: 11px; color: #a8a196; line-height: 1.2; }
.ab-sum b { font-size: 18px; color: #ffd08a; font-variant-numeric: tabular-nums; }

@media (max-height: 600px) {
  #game { gap: 6px; }
  .ab-info { min-height: 22px; }
  .ab-timebar { height: 5px; }
  .ab-panel { height: 88px; }
  .ab-key { min-height: 40px; }
  .ab-prompt { font-size: 13px; }
}

/* 좌상단 ← 홈, 우상단 🏆 명예의 전당 — 모든 게임 공통 chrome */
.home-link,
.hof-link {
  position: fixed;
  top: max(12px, env(safe-area-inset-top));
  z-index: 10;
  width: 40px;
  height: 40px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  border-radius: 999px;
  background: rgba(0, 0, 0, 0.35);
  color: #fff;
  text-decoration: none;
  font-size: 20px;
  font-weight: 600;
  line-height: 1;
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
  backdrop-filter: blur(4px);
  -webkit-backdrop-filter: blur(4px);
  transition: background 0.15s ease;
  border: none;
  cursor: pointer;
  font-family: inherit;
}

.home-link { left:  max(12px, env(safe-area-inset-left)); }
.hof-link  { right: max(12px, env(safe-area-inset-right)); }

@media (hover: hover) {
  .home-link:hover,
  .hof-link:hover {
    background: rgba(0, 0, 0, 0.55);
  }
}

/* 오버레이 (게임 종료 / 완료 시 점수 + 저장 버튼) */
.overlay {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.55);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 20;
  padding: 20px;
  backdrop-filter: blur(2px);
  -webkit-backdrop-filter: blur(2px);
}

.overlay[hidden] { display: none; }

.overlay-card {
  background: #161616;
  border: 1px solid #2a2a2a;
  border-radius: 18px;
  padding: 24px 22px;
  display: flex;
  flex-direction: column;
  gap: 12px;
  align-items: center;
  text-align: center;
  min-width: 240px;
  max-width: 320px;
  width: 100%;
}

.overlay-card h2 {
  margin: 0;
  font-size: 22px;
  font-weight: 800;
  letter-spacing: -0.02em;
}

.overlay-score {
  margin: 0;
  font-size: 56px;
  font-weight: 800;
  font-variant-numeric: tabular-nums;
  letter-spacing: -0.04em;
  line-height: 1;
}

.overlay-card button {
  font: inherit;
  color: #fff;
  background: rgba(255, 255, 255, 0.10);
  border: 1px solid rgba(255, 255, 255, 0.22);
  border-radius: 999px;
  padding: 12px 20px;
  min-height: 44px;
  cursor: pointer;
  font-weight: 600;
  font-size: 14px;
  width: 100%;
  -webkit-tap-highlight-color: transparent;
}

.overlay-card button:active {
  background: rgba(255, 255, 255, 0.18);
}

.overlay-card .save-btn {
  background: #4f9eff;
  border-color: #4f9eff;
}

.overlay-card .save-btn:active {
  background: #3d8ae8;
}

/* small phones: keep the start card clear of the title bar */
@media (max-width: 360px), (max-height: 600px) {
  .ab-start { gap: 10px; padding: 18px 16px; }
  .ab-start-top { gap: 10px; }
  .ab-start-top svg { width: 40px; height: 74px; }
  .ab-intro { font-size: 13px; line-height: 1.4; }
  .ab-plan { gap: 6px; }
  .ab-plan li { font-size: 12.5px; gap: 8px; }
  .ab-plan .k { flex-basis: 20px; height: 20px; font-size: 11px; }
}
