:root {
  color-scheme: dark;
  --bg: #15100a;
  --panel: #221810;
  --line: #3d2c1a;
  --gold: #d8b04a;
  --gold-dim: #9c7c34;
  --sand: #dcbf86;
  --sand2: #cfae6f;
  --ink: #4a2e14;
  --me: #f8f0db;
  --me-edge: #3a230e;
  --foe: #23439a;
  --foe-edge: #efe0b0;
  --text: #f3e9d6;
  --muted: #bfae92;
  --ok: #8fd19e;
  --warn: #f2b35d;
}

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

html, body {
  margin: 0;
  padding: 0;
  height: 100dvh;
  overflow: hidden;
  background: var(--bg);
  color: var(--text);
  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;
}

body {
  background:
    radial-gradient(120% 60% at 50% 0%, #2b1d10 0%, rgba(43, 29, 16, 0) 70%),
    var(--bg);
}

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: 14px;
}

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

.score-box {
  flex: 1;
  background: var(--panel);
  border: 1px solid var(--line);
  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: var(--muted);
}

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

.new-btn {
  flex: 0 0 auto;
  font: inherit;
  color: #1b1208;
  background: var(--gold);
  border: none;
  border-radius: 12px;
  padding: 0 18px;
  min-height: 56px;
  cursor: pointer;
  font-weight: 800;
  font-size: 14px;
  -webkit-tap-highlight-color: transparent;
}

.new-btn:active { background: #c39c3c; }

#game {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 0;   /* 작은 폰에서 아래 조작 요소가 화면 밖으로 밀리지 않게 */
}

/* ─── Senet layout ─────────────────────────────────────── */
.sn-wrap {
  width: 100%;
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.sn-head {
  display: grid;
  grid-template-columns: 1fr auto 1fr;
  align-items: center;
  gap: 6px;
  font-size: 13px;
}
.sn-mid { text-align: center; color: var(--muted); white-space: nowrap; font-size: 12px; }
.sn-mid b { color: var(--text); font-variant-numeric: tabular-nums; }
.sn-side { display: flex; align-items: center; gap: 4px; min-width: 0; }
.sn-side.foe { justify-content: flex-end; }
.sn-side-t { color: var(--muted); white-space: nowrap; }
.sn-side-t b { color: var(--text); font-size: 15px; font-variant-numeric: tabular-nums; }
.sn-mini { width: 22px; height: 22px; display: inline-block; flex: 0 0 auto; }
.sn-mini svg { width: 100%; height: 100%; display: block; }

/* board frame: dark wood with a gold inlay */
.sn-frame {
  padding: 6px;
  border-radius: 10px;
  background: linear-gradient(180deg, #4a2f19, #2e1c0d);
  box-shadow: inset 0 0 0 2px var(--gold-dim), inset 0 0 0 3px #1c1107, 0 6px 18px rgba(0, 0, 0, 0.45);
}
.sn-board {
  position: relative;
  display: grid;
  grid-template-columns: repeat(10, 1fr);
  grid-template-rows: repeat(3, 1fr);
  aspect-ratio: 10 / 3.3;
  gap: 0;
  border-radius: 4px;
  overflow: visible;
}

.sn-sq {
  position: relative;
  appearance: none;
  font: inherit;
  margin: 0;
  padding: 0;
  border: 0;
  background: var(--sand);
  box-shadow: inset 0 0 0 1px rgba(74, 46, 20, 0.55);
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
  min-width: 0;
  min-height: 0;
}
.sn-sq.alt { background: var(--sand2); }
.sn-sq.sp15 { background: #c8c48d; }
.sn-sq.sp26 { background: #e8c768; }
.sn-sq.sp27 { background: #7fb4c4; }
.sn-sq.sp28, .sn-sq.sp29, .sn-sq.sp30 { background: #e2b989; }
.sn-n {
  position: absolute;
  left: 1px;
  top: 1px;
  z-index: 7;               /* above the highlight frames, last-move outlines and pieces */
  padding: 1px 2px;
  border-radius: 3px;
  background: rgba(236, 216, 170, 0.85);
  font-size: 9px;
  line-height: 1;
  font-weight: 700;
  color: #4a2e14;
  font-variant-numeric: tabular-nums;
  pointer-events: none;
}
.sn-g {
  position: absolute;
  inset: 16% 16% 16% 16%;
  pointer-events: none;
}
.sn-g svg { width: 100%; height: 100%; display: block; fill: none; stroke: var(--ink); stroke-width: 3; stroke-linecap: round; stroke-linejoin: round; opacity: 0.8; }
.sn-g svg .fill { fill: var(--ink); }
.sp27 .sn-g svg { stroke: #134a66; }

/* last move */
.sn-sq.last-from::after,
.sn-sq.last-to::after {
  content: '';
  position: absolute;
  inset: 3px;
  border-radius: 4px;
  border: 2px dashed rgba(74, 46, 20, 0.55);
  pointer-events: none;
}
.sn-sq.last-to::after { border-style: solid; border-color: rgba(74, 46, 20, 0.7); }

/* legal targets */
.sn-sq.dest::before {
  content: '';
  position: absolute;
  left: 50%;
  top: 50%;
  width: 34%;
  aspect-ratio: 1;
  transform: translate(-50%, -50%);
  border-radius: 50%;
  background: #12322f;
  box-shadow: 0 0 0 3px #fff, 0 0 0 5px #12322f;
  z-index: 3;
  pointer-events: none;
}
.sn-sq.dest.back::before { background: #6b2a12; box-shadow: 0 0 0 3px #fff, 0 0 0 5px #6b2a12; }
.sn-sq.hit::before {
  content: '⇄';
  position: absolute;
  right: 1px;
  top: 1px;
  min-width: 15px;
  height: 15px;
  line-height: 15px;
  border-radius: 8px;
  font-size: 11px;
  font-weight: 900;
  color: #fff;
  background: #12322f;
  box-shadow: 0 0 0 1.5px #fff;
  z-index: 5;
  pointer-events: none;
}
.sn-sq.hit.back::before { background: #6b2a12; }
.sn-sq.hit { box-shadow: inset 0 0 0 2px #12322f, inset 0 0 0 4px #fff; }
.sn-sq.can { box-shadow: inset 0 0 0 3px #fff, inset 0 0 0 5px #12322f; }

/* AI is about to move: source + target */
.sn-sq.src { box-shadow: inset 0 0 0 3px var(--foe), inset 0 0 0 5px #fff; }
.sn-sq.tgt::before {
  content: '';
  position: absolute;
  left: 50%;
  top: 50%;
  width: 70%;
  aspect-ratio: 1;
  transform: translate(-50%, -50%);
  border-radius: 50%;
  border: 3px solid var(--foe);
  box-shadow: 0 0 0 2px #fff;
  z-index: 3;
  pointer-events: none;
}

/* off-board target */
.sn-board.dest-off::after {
  content: '➜';
  position: absolute;
  right: -6px;
  bottom: 4%;
  transform: translateX(100%);
  font-size: 18px;
  font-weight: 900;
  color: #fff;
  text-shadow: 0 0 4px #000;
  pointer-events: none;
}

/* path hint */
.sn-path {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  pointer-events: none;
  z-index: 2;
  opacity: 0;
  transition: opacity 0.35s ease;
  overflow: visible;
}
.sn-path.on { opacity: 1; }
.sn-path polyline {
  fill: none;
  stroke: rgba(120, 34, 10, 0.55);
  stroke-width: 2.2;
  stroke-linecap: round;
  stroke-linejoin: round;
  stroke-dasharray: 1 3;
  vector-effect: non-scaling-stroke;
}
.sn-path marker path { fill: rgba(120, 34, 10, 0.8); }
.sn-path .chev { fill: none; stroke: rgba(120, 34, 10, 0.85); stroke-width: 2; vector-effect: non-scaling-stroke; stroke-linecap: round; stroke-linejoin: round; }

/* pieces */
.sn-pcs { position: absolute; inset: 0; pointer-events: none; z-index: 4; }
.pc {
  position: absolute;
  width: 10%;
  height: 33.3333%;
  display: flex;
  align-items: center;
  justify-content: center;
  pointer-events: none;
}
.pc svg { width: 82%; height: 82%; display: block; overflow: visible; }
.pc .sh, .sn-mini .sh { fill: rgba(0, 0, 0, 0.28); }
.pc.me .body, .sn-mini.me .body { fill: var(--me); stroke: var(--me-edge); stroke-width: 2.6; }
.pc.me .band, .sn-mini.me .band { fill: none; stroke: #b0893f; stroke-width: 2.2; stroke-linecap: round; }
.pc.foe .body, .sn-mini.foe .body { fill: var(--foe); stroke: var(--foe-edge); stroke-width: 2.6; }
.pc.foe .band, .sn-mini.foe .band { fill: none; stroke: #9db5f0; stroke-width: 2.2; stroke-linecap: round; }
.pc.moving { z-index: 6; }
.pc.moving svg { filter: drop-shadow(0 4px 3px rgba(0, 0, 0, 0.45)); transform: translateY(-6%); }
.pc.can svg { animation: sn-bob 0.9s ease-in-out infinite; }
@keyframes sn-bob {
  0%, 100% { transform: translateY(0); }
  50% { transform: translateY(-9%); }
}

/* key of the special squares (only where there is room) */
.sn-key {
  display: none;
  grid-template-columns: 1fr 1fr;
  gap: 6px 10px;
  font-size: 12px;
  color: var(--muted);
}
.sn-ki { display: flex; align-items: center; gap: 6px; min-width: 0; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.sn-ki b { color: var(--text); font-variant-numeric: tabular-nums; }
.sn-kg {
  flex: 0 0 auto;
  width: 22px;
  height: 22px;
  border-radius: 4px;
  padding: 2px;
  background: var(--sand);
}
.sn-ki.sp15 .sn-kg { background: #c8c48d; }
.sn-ki.sp26 .sn-kg { background: #e8c768; }
.sn-ki.sp27 .sn-kg { background: #7fb4c4; }
.sn-ki.sp28 .sn-kg, .sn-ki.sp29 .sn-kg, .sn-ki.sp30 .sn-kg { background: #e2b989; }
.sn-kg svg { width: 100%; height: 100%; display: block; fill: none; stroke: var(--ink); stroke-width: 3.2; stroke-linecap: round; stroke-linejoin: round; }
.sn-kg svg .fill { fill: var(--ink); }
.sn-ki.sp27 .sn-kg svg { stroke: #134a66; }

/* legend + path toggle */
.sn-legend {
  display: flex;
  align-items: center;
  gap: 12px;
  font-size: 13px;
  color: var(--muted);
}
.sn-lg { display: inline-flex; align-items: center; gap: 4px; }
.sn-pathbtn {
  margin-left: auto;
  font: inherit;
  font-size: 13px;
  color: var(--text);
  background: transparent;
  border: 1px solid var(--line);
  border-radius: 999px;
  min-height: 30px;
  padding: 0 12px;
  cursor: pointer;
}
.sn-pathbtn[aria-pressed="true"] { border-color: var(--gold); color: var(--gold); }

/* sticks + throw */
.sn-ctl {
  display: grid;
  grid-template-columns: auto 1fr auto;
  align-items: center;
  gap: 12px;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 14px;
  padding: 10px 12px;
}
.sn-sticks { display: flex; gap: 6px; height: 52px; align-items: center; }
.stk {
  width: 11px;
  height: 52px;
  border-radius: 6px;
  background: #3b2412;
  box-shadow: inset 0 0 0 1.5px #160c04;
  position: relative;
  transition: background 0.08s linear;
}
.stk i {
  position: absolute;
  left: 50%;
  width: 6px;
  height: 6px;
  margin-left: -3px;
  top: 8px;
  border-radius: 50%;
  background: transparent;
  box-shadow: 0 30px 0 transparent;
}
.stk.light { background: #f1dfb2; box-shadow: inset 0 0 0 1.5px #7a5a2a; }
.stk.light i { background: #7a5a2a; box-shadow: 0 30px 0 #7a5a2a; }
.sn-sticks.rolling .stk { animation: sn-flip 0.15s linear infinite; }
.sn-sticks.rolling .stk:nth-child(2) { animation-duration: 0.19s; }
.sn-sticks.rolling .stk:nth-child(3) { animation-duration: 0.13s; }
.sn-sticks.rolling .stk:nth-child(4) { animation-duration: 0.17s; }
@keyframes sn-flip {
  0% { transform: translateY(0) scaleX(1); }
  50% { transform: translateY(-6px) scaleX(0.2); }
  100% { transform: translateY(0) scaleX(1); }
}
.sn-res { display: flex; flex-direction: column; align-items: center; min-width: 0; line-height: 1.1; }
.sn-res b { font-size: 34px; font-weight: 900; font-variant-numeric: tabular-nums; min-height: 38px; }
.sn-res b.me { color: var(--me); }
.sn-res b.foe { color: #a9bff5; }
.sn-res small { font-size: 12px; color: var(--muted); text-align: center; min-height: 14px; }
.sn-res em { font-style: normal; font-size: 12px; font-weight: 700; color: var(--gold); min-height: 14px; margin-top: 2px; text-align: center; }
.sn-throw {
  font: inherit;
  font-size: 16px;
  font-weight: 800;
  color: #1b1208;
  background: var(--gold);
  border: none;
  border-radius: 12px;
  min-height: 52px;
  min-width: 96px;
  padding: 0 16px;
  cursor: pointer;
}
.sn-throw:disabled { background: #4a3a24; color: #a8977a; cursor: default; }
.sn-throw.ready { animation: sn-pulse 1.4s ease-in-out infinite; }
@keyframes sn-pulse {
  0%, 100% { box-shadow: 0 0 0 0 rgba(216, 176, 74, 0.55); }
  50% { box-shadow: 0 0 0 7px rgba(216, 176, 74, 0); }
}

.sn-msg {
  margin: 0;
  min-height: 42px;
  font-size: 14px;
  line-height: 1.45;
  text-align: center;
  color: var(--text);
}
.sn-msg.ok { color: var(--ok); }
.sn-msg.warn { color: var(--warn); }
.sn-msg.info { color: #e8d39b; }

@media (min-height: 720px) {
  .sn-wrap { gap: 14px; }
  .sn-board { aspect-ratio: 10 / 4.3; }
}
/* tall phones: let the board and the play area grow into the spare height */
@media (min-height: 800px) {
  .sn-wrap { gap: 20px; }
  .sn-board { aspect-ratio: 10 / 5.4; }
  .pc svg { width: 88%; height: 88%; }
}
@media (min-height: 720px) {
  .sn-key { display: grid; }
  .sn-sticks, .stk { height: 60px; }
  .stk { width: 13px; }
  .stk i { box-shadow: 0 36px 0 transparent; }
  .stk.light i { box-shadow: 0 36px 0 #7a5a2a; }
  .sn-msg { font-size: 15px; }
}
@media (max-width: 400px) {
  .sn-frame { margin: 0 -8px; padding: 5px; }
}

@media (max-height: 640px) {
  .sn-wrap { gap: 8px; }
  .sn-ctl { padding: 8px 10px; }
  .sn-sticks, .stk { height: 44px; }
  .stk i { box-shadow: 0 24px 0 transparent; }
  .stk.light i { box-shadow: 0 24px 0 #7a5a2a; }
}

@media (prefers-reduced-motion: reduce) {
  .pc.can svg, .sn-throw.ready, .sn-sticks.rolling .stk { animation: none; }
}

/* 좌상단 ← 홈, 우상단 🏆 명예의 전당 — 모든 게임 공통 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: var(--panel);
  border: 1px solid var(--gold-dim);
  border-radius: 18px;
  padding: 24px 22px;
  display: flex;
  flex-direction: column;
  gap: 12px;
  align-items: center;
  text-align: center;
  min-width: 240px;
  max-width: 340px;
  width: 100%;
}

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

.overlay-score {
  margin: 0;
  font-size: 56px;
  font-weight: 800;
  font-variant-numeric: tabular-nums;
  letter-spacing: -0.04em;
  line-height: 1;
}
.overlay-sub { margin: 0; font-size: 13px; color: var(--muted); line-height: 1.45; }
.overlay-howto {
  margin: 0; font-size: 14px; line-height: 1.55; color: #e3d6bd;
  text-align: left; white-space: pre-line; max-height: 52vh;
  overflow-y: auto; touch-action: pan-y;
}
.overlay-howto b { color: var(--gold); }

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

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

.overlay-card .save-btn,
.overlay-card .start-btn {
  background: var(--gold);
  border-color: var(--gold);
  color: #1b1208;
}

.overlay-card .save-btn:active,
.overlay-card .start-btn:active { background: #c39c3c; }

/* short screens: compact cards (kept last so it beats the base card rules above) */
@media (max-height: 640px) {
  .overlay-card { padding: 14px 16px; gap: 8px; }
  .overlay-card h2 { font-size: 19px; }
  .overlay-howto { font-size: 13px; line-height: 1.45; max-height: 32vh; }
  .overlay-card button { min-height: 40px; padding: 9px 16px; }
}
