* {
  box-sizing: border-box;
}

html,
body {
  height: 100%;
}

body {
  margin: 0;
  overflow: hidden;
  font-family: "Noto Sans KR", "Malgun Gothic", system-ui, sans-serif;
  color: #eaf0ff;
  background: #16224a;
}

/* 난이도별 게임 영역 색상 */
body.tone-easiest .game {
  background: radial-gradient(circle at 50% 25%, #2e8b6b 0%, #1c5a48 55%, #123a30 100%);
}

body.tone-easy .game {
  background: radial-gradient(circle at 50% 25%, #2f7f96 0%, #1d5165 55%, #123442 100%);
}

body.tone-normal .game {
  background: radial-gradient(circle at 50% 25%, #3f63b8 0%, #263f7d 55%, #172850 100%);
}

body.tone-hard .game {
  background: radial-gradient(circle at 50% 25%, #7a58b0 0%, #4c3775 55%, #2f2249 100%);
}

body.tone-infinity .game {
  background: radial-gradient(circle at 50% 20%, #2b2b3d 0%, #17171f 60%, #08080c 100%);
}

.game {
  display: flex;
  flex-direction: column;
  height: 100%;
  min-height: 0;
  /* 아래 여백을 위쪽보다 넉넉하게 둡니다. */
  padding: clamp(8px, 2vh, 20px) clamp(18px, 7vw, 76px) clamp(26px, 6vh, 56px);
  gap: clamp(6px, 1.2vh, 14px);
  background: radial-gradient(circle at 50% 25%, #2b3c74 0%, #16224a 55%, #0d1530 100%);
}

.topbar {
  display: flex;
  align-items: center;
  gap: clamp(8px, 2vw, 22px);
  flex-wrap: wrap;
  flex: 0 0 auto;
}

.stat {
  margin: 0;
  font-size: clamp(0.95rem, 2.2vw, 1.2rem);
}

.stat strong {
  font-size: 1.15em;
}

#userStat strong {
  color: #ffd766;
}

#reset {
  padding: 0.5em 1.1em;
  border: 1px solid rgba(255, 255, 255, 0.35);
  border-radius: 10px;
  background: rgba(255, 255, 255, 0.12);
  color: inherit;
  font: inherit;
  cursor: pointer;
}

#reset {
  margin-left: auto;
}

#reset:hover,
#reset:focus-visible {
  border-color: var(--accent, #5b8cff);
}

/* 아이콘 버튼(역사 자료 표 열기) */
.icon-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  flex: 0 0 auto;
  width: clamp(34px, 5.5vh, 42px);
  height: clamp(34px, 5.5vh, 42px);
  padding: 0;
  border: 1px solid rgba(255, 255, 255, 0.35);
  border-radius: 10px;
  background: rgba(255, 255, 255, 0.12);
  color: inherit;
  cursor: pointer;
}

.icon-btn svg {
  width: 56%;
  height: 56%;
  fill: none;
  stroke: currentColor;
  stroke-width: 2;
  stroke-linecap: round;
  stroke-linejoin: round;
}

.icon-btn:hover,
.icon-btn:focus-visible {
  border-color: var(--accent, #5b8cff);
  outline: none;
}

.message {
  margin: 0;
  min-height: 1.4em;
  font-size: clamp(0.9rem, 2vw, 1.05rem);
  text-align: center;
  flex: 0 0 auto;
}

/* 카드판: 남는 화면을 모두 채웁니다. */
.board {
  flex: 1 1 auto;
  min-height: 0;
  display: grid;
  grid-template-columns: repeat(var(--cols, 4), minmax(0, 1fr));
  grid-auto-rows: minmax(0, 1fr);
  gap: clamp(5px, 1vw, 12px);
  background: none;
  padding: 0;
  /* 카드 클릭 시 포커스 표시가 경계를 넘어 스크롤바가 생기지 않게 막습니다. */
  overflow: hidden;
}

.card {
  position: relative;
  border: none;
  padding: 0;
  background: none;
  aspect-ratio: auto;
  min-height: 0;
  cursor: pointer;
  perspective: 800px;
  font: inherit;
  color: inherit;
  /* 카드 한 장 안쪽만 다시 그리게 해서, 뒤집을 때 화면 전체를 다시 그리지 않게 합니다. */
  contain: layout paint;
}

/* 기본 브라우저 외곽선 대신 카드 안쪽 테두리로 포커스를 보여 줍니다. */
.card:focus {
  outline: none;
}

.card:focus-visible .back {
  box-shadow: inset 0 0 0 2px var(--accent, #5b8cff);
}

.card-inner {
  position: absolute;
  inset: 0;
  transform-style: preserve-3d;
  transition: transform 0.45s ease;
  /* 뒤집을 준비를 미리 해 두어 첫 카드에서 끊기지 않게 합니다. */
  will-change: transform;
}

.card.flipped .card-inner {
  transform: rotateY(180deg);
}

.face {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 12px;
  backface-visibility: hidden;
  overflow: hidden;
  text-align: center;
  padding: 6%;
  word-break: keep-all;
  overflow-wrap: anywhere;
}

.back {
  font-size: clamp(1.4rem, 4vw, 2.4rem);
  font-weight: 700;
  color: #c9d6ff;
  background: linear-gradient(160deg, #33477f, #1d2a52);
  border: 1px solid rgba(255, 255, 255, 0.16);
}

.front {
  transform: rotateY(180deg);
  /* 같은 짝끼리 색을 맞추는 단계에서는 카드마다 정한 --front-bg·--front-edge 값을 씁니다. */
  background: var(--front-bg, #f4f7ff);
  color: #1b2544;
  border: 1px solid var(--front-edge, rgba(20, 30, 60, 0.18));
}

.front .event {
  font-size: clamp(0.7rem, 1.9vw, 1.15rem);
  font-weight: 700;
  line-height: 1.25;
}

.front .year {
  font-size: clamp(0.95rem, 2.6vw, 1.6rem);
  font-weight: 800;
  color: #2f4a9c;
}

/* 짝을 맞추면 카드 앞면 배경은 그대로 두고 테두리만 초록색으로 바꿉니다. */
.card.matched .front {
  border-color: #3f9a68;
  border-width: 2px;
}

/* 난이도 선택 화면 */
.difficulty {
  /* 난이도 설정은 별도 패널이 아니라 화면 전체를 사용합니다. */
  position: fixed;
  inset: 0;
  display: flex;
  flex-direction: column;
  /* 아래쪽에 넉넉한 여백을 두어 단계 목록이 화면 바닥에 붙지 않게 합니다. */
  padding: clamp(16px, 3vh, 30px) clamp(18px, 7vw, 76px) clamp(48px, 14vh, 160px);
  background: radial-gradient(circle at 50% 30%, #2b3c74 0%, #16224a 55%, #0d1530 100%);
  z-index: 10;
  /* 화면 전체를 쓰는 난이도 화면: 세로로만 스크롤하고 가로 막대는 두지 않습니다. */
  overflow-x: hidden;
  overflow-y: auto;
}

.difficulty[hidden] {
  display: none;
}

.difficulty-panel {
  /* 화면 전체를 차지합니다: 배경 상자·테두리 없이 제목과 단계만 놓습니다. */
  flex: 1 1 auto;
  min-height: 0;
  display: flex;
  flex-direction: column;
  gap: clamp(10px, 2vh, 22px);
}

.difficulty-title {
  margin: 0;
  font-size: clamp(1.3rem, 4vh, 2rem);
  text-align: center;
  flex: 0 0 auto;
}

.difficulty-options {
  /* 단계 버튼이 화면 높이를 나눠 채우며 한 줄에 하나씩 세로로 길게 놓입니다. */
  /* 제목과 단계 사이에 위쪽 여백을 두어 버튼 묶음을 아래로 내립니다. */
  margin-top: clamp(20px, 9vh, 96px);
  flex: 1 1 auto;
  min-height: 0;
  display: grid;
  grid-template-columns: 1fr;
  grid-auto-rows: minmax(0, 1fr);
  gap: clamp(12px, 2.2vh, 26px);
  /* 잠긴 단계 흔들림이 좌우로 움직여도 가로 스크롤 막대가 생기지 않게 합니다.
     좌우 여백을 두어 흔들림이 잘리지 않고 그대로 보입니다. */
  padding-inline: 6px;
  overflow-x: hidden;
  overflow-y: auto;
}

.difficulty-btn {
  position: relative;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 0.3em;
  min-height: clamp(52px, 9vh, 110px);
  padding: clamp(12px, 2.4vh, 22px) clamp(10px, 1.8vw, 16px);
  border: 1px solid var(--stage-edge, rgba(255, 255, 255, 0.22));
  border-radius: 16px;
  background: linear-gradient(155deg, var(--stage-a, rgba(255, 255, 255, 0.1)), var(--stage-b, rgba(255, 255, 255, 0.05)));
  color: inherit;
  font: inherit;
  text-align: center;
  cursor: pointer;
  transition: border-color 0.2s ease, box-shadow 0.2s ease, transform 0.2s ease;
}

.difficulty-btn:not(.locked):hover,
.difficulty-btn:not(.locked):focus-visible {
  border-color: var(--accent, #5b8cff);
  box-shadow: 0 0 0 1px var(--accent, #5b8cff), 0 10px 24px rgba(0, 0, 0, 0.35);
  transform: translateY(-2px);
  outline: none;
}

.difficulty-btn.locked:focus-visible {
  outline: 2px solid rgba(255, 255, 255, 0.55);
  outline-offset: 2px;
}

.difficulty-name {
  font-size: clamp(0.95rem, 2vw, 1.1rem);
  font-weight: 700;
}

.difficulty-info {
  font-size: clamp(0.75rem, 1.7vw, 0.88rem);
  opacity: 0.75;
}

/* 잠긴 단계를 눌렀을 때 왜 잠겼는지 알려 주는 안내 줄입니다. */
.stage-status {
  margin: 0;
  min-height: 1.2em;
  flex: 0 0 auto;
  font-size: clamp(0.74rem, 1.7vw, 0.86rem);
  font-weight: 700;
  text-align: center;
  color: #ffd766;
}

/* 잠긴 단계를 누르면 잠깐 흔들어 잠겨 있음을 보여 줍니다. */
.difficulty-btn.locked-bump {
  animation: lock-bump 0.45s ease;
}

@keyframes lock-bump {
  0%, 100% { transform: translateX(0); }
  20% { transform: translateX(-5px); }
  40% { transform: translateX(5px); }
  60% { transform: translateX(-3px); }
  80% { transform: translateX(3px); }
}

/* 난이도별 사각형 내부 색: 단계가 올라갈수록 짙고 강해집니다. */
.difficulty-btn[data-difficulty='easiest'] { --stage-a: #35a37c; --stage-b: #1a5a46; --stage-edge: #6fe3b0; --accent: #6fe3b0; }
.difficulty-btn[data-difficulty='easy'] { --stage-a: #3795ad; --stage-b: #164a5c; --stage-edge: #6fd6e3; --accent: #6fd6e3; }
.difficulty-btn[data-difficulty='normal'] { --stage-a: #4a72cc; --stage-b: #1d3060; --stage-edge: #7fa4ff; --accent: #7fa4ff; }
.difficulty-btn[data-difficulty='hard'] { --stage-a: #8a63c6; --stage-b: #3b2a5c; --stage-edge: #b795f0; --accent: #b795f0; }
.difficulty-btn[data-difficulty='infinity'] { --stage-a: #5a4826; --stage-b: #141018; --stage-edge: #ffd766; --accent: #ffd766; }

/* 잠긴 단계: 네 모서리에서 쇠사슬이 가운데로 모이고, 가운데에 자물쇠가 있습니다. */
.difficulty-btn.locked {
  cursor: not-allowed;
  border-style: dashed;
  color: rgba(234, 240, 255, 0.62);
  background: linear-gradient(155deg, #3b4560, #1a2136);
}

/* 인피니티는 숨겨진 단계: 열리기 전에는 화면에 나타나지 않습니다. */
.difficulty-btn[hidden] {
  display: none;
}

.stage-lock {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: inherit;
  overflow: hidden;
  pointer-events: none;
  background: radial-gradient(circle at 50% 50%, rgba(8, 12, 24, 0.6) 0%, rgba(8, 12, 24, 0.32) 46%, rgba(8, 12, 24, 0) 74%);
}

.stage-lock[hidden] {
  display: none;
}

/* 모서리에서 가운데로 모이는 쇠사슬 */
.stage-chains {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
}

.stage-chains line {
  stroke: #c3cee2;
  stroke-width: 3;
  stroke-linecap: round;
  stroke-dasharray: 5 4;
  /* 그림자 필터는 다시 그릴 때마다 비용이 커서 쓰지 않습니다. */
  vector-effect: non-scaling-stroke;
}

/* 가운데 자물쇠 아이콘 */
.stage-lock-badge {
  position: relative;
  display: block;
  width: 22px;
  height: 27px;
  filter: drop-shadow(0 2px 5px rgba(0, 0, 0, 0.6));
}

.stage-lock-shackle {
  position: absolute;
  top: 0;
  left: 50%;
  width: 13px;
  height: 13px;
  transform: translateX(-50%);
  border: 3px solid #d5deee;
  border-bottom: none;
  border-radius: 7px 7px 0 0;
}

.stage-lock-body {
  position: absolute;
  left: 0;
  bottom: 0;
  width: 100%;
  height: 17px;
  border-radius: 4px;
  background: linear-gradient(180deg, #e6edf9, #a8b4cb);
}

.stage-lock-body::after {
  content: "";
  position: absolute;
  top: 4px;
  left: 50%;
  width: 3px;
  height: 9px;
  transform: translateX(-50%);
  border-radius: 2px;
  background: #2b3a5c;
}

/* 난이도별 카드판 색조 */
body.tone-easiest .board { --accent: #6fe3b0; }
body.tone-easy .board { --accent: #6fd6e3; }
body.tone-normal .board { --accent: #7fa4ff; }
body.tone-hard .board { --accent: #b795f0; }
body.tone-infinity .board { --accent: #ffd766; }

body.tone-easiest .back { background: linear-gradient(160deg, #2f7f61, #1a4a38); }
body.tone-easy .back { background: linear-gradient(160deg, #2c7387, #184457); }
body.tone-normal .back { background: linear-gradient(160deg, #33477f, #1d2a52); }
body.tone-hard .back { background: linear-gradient(160deg, #63498f, #3a2a56); }
body.tone-infinity .back { background: linear-gradient(160deg, #3a3a4f, #1b1b26); }

body.tone-infinity .front { background: #fff8e0; }

@media (max-width: 720px) {
  .difficulty {
    padding: clamp(12px, 2.5vh, 22px) clamp(14px, 5vw, 40px) clamp(36px, 12vh, 110px);
  }

  .history-toggle-btn {
    top: clamp(12px, 2.5vh, 22px);
    right: clamp(14px, 5vw, 40px);
  }

  .history-table-wrap {
    top: calc(clamp(12px, 2.5vh, 22px) + clamp(34px, 5.5vh, 42px) + 10px);
    right: clamp(14px, 5vw, 40px);
    width: min(340px, calc(100% - 2 * clamp(14px, 5vw, 40px)));
  }

  .difficulty-btn {
    min-height: 50px;
  }
}

@media (max-height: 560px) {
  .difficulty-title {
    font-size: 1.1rem;
  }

  .difficulty-btn {
    min-height: 46px;
    padding: 8px;
  }

  .difficulty-name {
    font-size: 0.95rem;
  }

  .difficulty-info {
    font-size: 0.75rem;
  }
}

@media (max-width: 520px) {
  .board {
    gap: 4px;
  }

  .topbar {
    gap: 8px;
  }
}

@media (max-height: 520px) {
  .game {
    gap: 4px;
    padding: 6px 6px 18px;
  }

  .message {
    min-height: 1.1em;
    font-size: 0.8rem;
  }

  .front .event {
    font-size: clamp(0.6rem, 1.6vw, 0.9rem);
  }

  .front .year {
    font-size: clamp(0.8rem, 2.2vw, 1.2rem);
  }
}

/* 난이도 화면 오른쪽 위의 역사 자료 표 패널입니다. */
.history-toggle-btn {
  position: absolute;
  top: clamp(16px, 3vh, 30px);
  right: clamp(18px, 7vw, 76px);
  z-index: 3;
}

.history-table-wrap {
  position: absolute;
  top: calc(clamp(16px, 3vh, 30px) + clamp(34px, 5.5vh, 42px) + 10px);
  right: clamp(18px, 7vw, 76px);
  width: min(360px, calc(100% - 2 * clamp(18px, 7vw, 76px)));
  max-height: min(64vh, 460px);
  z-index: 2;
  display: flex;
  flex-direction: column;
  gap: 8px;
  padding: clamp(10px, 2vh, 14px);
  border: 1px solid rgba(255, 255, 255, 0.18);
  border-radius: 12px;
  background: rgba(9, 14, 28, 0.94);
  box-shadow: 0 16px 36px rgba(0, 0, 0, 0.45);
  overflow: hidden;
}

.history-table-wrap[hidden] {
  display: none;
}

.history-summary {
  margin: 0;
  font-size: clamp(0.76rem, 1.8vw, 0.86rem);
  opacity: 0.85;
}

.history-table-box {
  flex: 1 1 auto;
  min-height: 0;
  max-height: min(52vh, 380px);
  overflow: auto;
}

.history-table {
  width: 100%;
  border-collapse: collapse;
  table-layout: fixed;
  font-size: clamp(0.76rem, 1.8vw, 0.88rem);
}

.history-table th,
.history-table td {
  padding: 0.4em 0.55em;
  border-bottom: 1px solid rgba(255, 255, 255, 0.12);
  text-align: left;
  vertical-align: top;
  overflow-wrap: anywhere;
}

.history-table thead th {
  position: sticky;
  top: 0;
  z-index: 1;
  background: #26345f;
  font-weight: 700;
  opacity: 0.95;
}

.history-table .history-year {
  width: 4.6em;
  font-weight: 700;
  color: #ffd766;
  white-space: nowrap;
}

@media (prefers-reduced-motion: reduce) {
  .card-inner {
    transition: none;
  }

  body {
    transition: none;
  }

  .difficulty-btn.locked-bump {
    animation: none;
  }
}
