:root {
  --bg: #1d1813;
  --bg-elev: #28211a;
  --bg-card: #241e18;
  --bg-chip: rgba(255, 245, 228, 0.06);
  --border: rgba(235, 205, 155, 0.16);
  --border-strong: rgba(235, 205, 155, 0.3);
  --fg: #f4ece0;
  --fg-muted: #b6a691;
  --accent: #d9a44b;
  --accent-bright: #f0be67;
  --accent-dark: #8c611e;
  --danger: #e26d5c;
  --ok: #8bc37a;

  /* 바둑판 나무색 팔레트 */
  --wood-light: #e2ba76;
  --wood-mid: #cfa15a;
  --wood-deep: #b5823c;
  --wood-frame: #6a4620;
  --wood-frame-dark: #432a11;

  /* kit.js 테마 연동 */
  --kit-bg: #1d1813;
  --kit-fg: #f4ece0;
  --kit-sheet: #26201a;

  --safe-t: env(safe-area-inset-top, 0px);
  --safe-r: env(safe-area-inset-right, 0px);
  --safe-b: env(safe-area-inset-bottom, 0px);
  --safe-l: env(safe-area-inset-left, 0px);
}

*,
*::before,
*::after {
  box-sizing: border-box;
  -webkit-tap-highlight-color: transparent;
}

html,
body {
  margin: 0;
  padding: 0;
  width: 100%;
  height: 100%;
  overflow: hidden;
  background:
    radial-gradient(circle at 50% 0%, #2d241b 0%, var(--bg) 62%, #14100c 100%);
  color: var(--fg);
  font-family: -apple-system, BlinkMacSystemFont, "Apple SD Gothic Neo", "Pretendard", "Noto Sans KR", system-ui, sans-serif;
  -webkit-font-smoothing: antialiased;
  user-select: none;
  -webkit-user-select: none;
  touch-action: manipulation;
}

button {
  font: inherit;
  color: inherit;
  background: none;
  border: none;
  padding: 0;
  cursor: pointer;
  touch-action: manipulation;
}

button:disabled {
  opacity: 0.42;
  cursor: not-allowed;
}

button:focus-visible {
  outline: 2px solid var(--accent-bright);
  outline-offset: 2px;
}

/* ---------- 레이아웃 (320px ~ 데스크톱 중앙 480px) ---------- */
.app {
  width: 100%;
  max-width: 480px;
  min-width: 320px;
  height: 100vh;
  height: 100dvh;
  margin: 0 auto;
  padding:
    calc(var(--safe-t) + 10px)
    calc(var(--safe-r) + 12px)
    calc(var(--safe-b) + 12px)
    calc(var(--safe-l) + 12px);
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  gap: 10px;
}

/* ---------- 상단 HUD ---------- */
.hud {
  display: flex;
  flex-direction: column;
  gap: 8px;
  flex-shrink: 0;
}

.hud-top {
  display: grid;
  grid-template-columns: 1.25fr 1fr 1fr;
  gap: 7px;
}

.hud-chip {
  background: var(--bg-chip);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 7px 10px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  min-height: 50px;
  gap: 2px;
  text-align: center;
}

.hud-stage-btn {
  transition: background-color 0.15s ease, border-color 0.15s ease, transform 0.1s ease;
}

.hud-stage-btn:hover {
  background: rgba(255, 245, 228, 0.1);
  border-color: var(--border-strong);
}

.hud-stage-btn:active {
  transform: scale(0.97);
}

.hud-label {
  font-size: 11px;
  font-weight: 500;
  color: var(--fg-muted);
  letter-spacing: 0.02em;
  line-height: 1.1;
}

.hud-value {
  font-size: 17px;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
  line-height: 1.2;
  color: var(--fg);
}

.hud-moves-box.warn .hud-value {
  color: var(--accent-bright);
}

.hud-moves-box.danger {
  border-color: rgba(226, 109, 92, 0.55);
  background: rgba(226, 109, 92, 0.12);
}

.hud-moves-box.danger .hud-value {
  color: var(--danger);
}

.hud-actions {
  display: grid;
  grid-template-columns: 1fr 1fr 1fr 44px;
  gap: 7px;
}

.act-btn {
  min-height: 40px;
  padding: 0 8px;
  border-radius: 10px;
  background: var(--bg-elev);
  border: 1px solid var(--border);
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 5px;
  font-size: 13px;
  font-weight: 600;
  color: var(--fg);
  transition: background-color 0.15s ease, border-color 0.15s ease, transform 0.1s ease;
  white-space: nowrap;
}

.act-btn:hover:not(:disabled) {
  background: #332a21;
  border-color: var(--border-strong);
}

.act-btn:active:not(:disabled) {
  transform: scale(0.96);
}

.act-btn.active {
  border-color: var(--accent);
  background: rgba(217, 164, 75, 0.16);
  color: var(--accent-bright);
}

.act-btn.is-muted {
  color: var(--fg-muted);
  opacity: 0.78;
}

.act-icon-only {
  padding: 0;
}

.act-ico {
  font-size: 15px;
  line-height: 1;
}

.act-txt {
  display: inline-flex;
  align-items: center;
  gap: 4px;
}

.badge {
  font-style: normal;
  font-size: 11px;
  font-weight: 700;
  min-width: 18px;
  height: 18px;
  padding: 0 5px;
  border-radius: 999px;
  background: rgba(217, 164, 75, 0.22);
  color: var(--accent-bright);
  display: inline-flex;
  align-items: center;
  justify-content: center;
  font-variant-numeric: tabular-nums;
}

/* ---------- 중앙 7x7 바둑판 ---------- */
.board-stage {
  position: relative;
  flex: 1 1 auto;
  min-height: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
}

.board-wrap {
  position: relative;
  width: min(100%, 440px, calc(100dvh - var(--safe-t) - var(--safe-b) - 196px));
  aspect-ratio: 1 / 1;
  padding: 10px;
  border-radius: 16px;
  background:
    repeating-linear-gradient(
      90deg,
      rgba(95, 58, 18, 0.055) 0px,
      rgba(95, 58, 18, 0.055) 2px,
      transparent 2px,
      transparent 11px
    ),
    repeating-linear-gradient(
      88deg,
      rgba(255, 240, 200, 0.045) 0px,
      rgba(255, 240, 200, 0.045) 3px,
      transparent 3px,
      transparent 19px
    ),
    linear-gradient(145deg, var(--wood-light) 0%, var(--wood-mid) 52%, var(--wood-deep) 100%);
  border: 2px solid var(--wood-frame);
  box-shadow:
    0 14px 32px rgba(0, 0, 0, 0.52),
    0 3px 0 var(--wood-frame-dark),
    inset 0 1px 1px rgba(255, 244, 214, 0.48),
    inset 0 -2px 6px rgba(74, 45, 14, 0.35);
  display: flex;
  align-items: center;
  justify-content: center;
}

#board-canvas {
  display: block;
  width: 100%;
  height: 100%;
  border-radius: 8px;
  cursor: pointer;
  touch-action: none;
}

.board-toast {
  position: absolute;
  bottom: 8px;
  left: 50%;
  transform: translate(-50%, 8px);
  padding: 6px 14px;
  border-radius: 999px;
  background: rgba(24, 19, 14, 0.9);
  border: 1px solid var(--border-strong);
  color: var(--fg);
  font-size: 13px;
  font-weight: 600;
  white-space: nowrap;
  pointer-events: none;
  opacity: 0;
  transition: opacity 0.18s ease, transform 0.18s ease;
  box-shadow: 0 6px 18px rgba(0, 0, 0, 0.45);
  z-index: 5;
}

.board-toast.show {
  opacity: 1;
  transform: translate(-50%, 0);
}

/* ---------- 하단 스테이지 정보 (별 기준) ---------- */
.stage-info {
  flex-shrink: 0;
  background: var(--bg-chip);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 9px 12px;
}

.info-row {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 3px;
  text-align: center;
}

.info-goal {
  font-size: 12px;
  color: var(--fg-muted);
  line-height: 1.3;
}

.info-par {
  font-size: 13px;
  font-weight: 600;
  color: var(--accent-bright);
  letter-spacing: 0.01em;
  font-variant-numeric: tabular-nums;
  line-height: 1.3;
}

/* ---------- 오버레이 공통 (.open 표시) ---------- */
.overlay {
  position: fixed;
  inset: 0;
  z-index: 100;
  padding:
    calc(var(--safe-t) + 16px)
    calc(var(--safe-r) + 14px)
    calc(var(--safe-b) + 16px)
    calc(var(--safe-l) + 14px);
  background: rgba(14, 11, 8, 0.78);
  backdrop-filter: blur(8px);
  -webkit-backdrop-filter: blur(8px);
  display: flex;
  align-items: center;
  justify-content: center;
  opacity: 0;
  visibility: hidden;
  pointer-events: none;
  transition: opacity 0.22s ease, visibility 0.22s ease;
}

.overlay.open {
  opacity: 1;
  visibility: visible;
  pointer-events: auto;
}

.overlay-card {
  width: 100%;
  max-width: 420px;
  max-height: calc(100dvh - var(--safe-t) - var(--safe-b) - 32px);
  overflow-y: auto;
  background: linear-gradient(180deg, #2b231b 0%, var(--bg-card) 100%);
  border: 1px solid var(--border-strong);
  border-radius: 20px;
  padding: 20px 18px 18px;
  box-shadow: 0 22px 54px rgba(0, 0, 0, 0.65);
  transform: translateY(10px) scale(0.98);
  transition: transform 0.24s cubic-bezier(0.2, 0.85, 0.25, 1);
}

.overlay.open .overlay-card {
  transform: translateY(0) scale(1);
}

/* ---------- 시작 오버레이 ---------- */
.brand {
  text-align: center;
  margin-bottom: 14px;
}

.brand-stones {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  margin-bottom: 6px;
}

.stone-dot {
  width: 18px;
  height: 18px;
  border-radius: 50%;
  display: inline-block;
}

.stone-black {
  background: radial-gradient(circle at 34% 30%, #555 0%, #171717 55%, #050505 100%);
  box-shadow: 0 2px 5px rgba(0, 0, 0, 0.65), inset 0 1px 1px rgba(255, 255, 255, 0.2);
}

.stone-white {
  background: radial-gradient(circle at 35% 30%, #ffffff 0%, #e7e1d5 68%, #bfb5a2 100%);
  box-shadow: 0 2px 5px rgba(0, 0, 0, 0.5);
}

.brand-title {
  margin: 0;
  font-size: 30px;
  font-weight: 800;
  letter-spacing: 0.12em;
  text-indent: 0.12em;
  color: var(--fg);
}

.brand-sub {
  margin: 6px 0 0;
  font-size: 13px;
  color: var(--fg-muted);
  line-height: 1.45;
}

.best-record {
  display: flex;
  align-items: center;
  justify-content: space-evenly;
  background: rgba(0, 0, 0, 0.22);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 10px 12px;
  margin-bottom: 14px;
}

.record-item {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
}

.record-label {
  font-size: 11px;
  color: var(--fg-muted);
}

.record-value {
  font-size: 16px;
  font-weight: 700;
  color: var(--accent-bright);
  font-variant-numeric: tabular-nums;
}

.record-divider {
  width: 1px;
  height: 26px;
  background: var(--border);
}

.stage-section {
  margin-bottom: 16px;
}

.stage-section-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  font-size: 12px;
  color: var(--fg-muted);
  margin-bottom: 8px;
  padding: 0 2px;
}

.stage-legend {
  color: var(--accent);
  font-weight: 600;
}

.stage-grid {
  display: grid;
  grid-template-columns: repeat(5, minmax(0, 1fr));
  gap: 7px;
}

.stage-btn {
  min-height: 50px;
  padding: 5px 2px;
  border-radius: 10px;
  background: rgba(255, 245, 228, 0.05);
  border: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 2px;
  transition: background-color 0.14s ease, border-color 0.14s ease, transform 0.1s ease;
}

.stage-btn:hover:not(:disabled) {
  background: rgba(255, 245, 228, 0.11);
  border-color: var(--border-strong);
}

.stage-btn:active:not(:disabled) {
  transform: scale(0.95);
}

.stage-btn.selected,
.stage-btn.current {
  border-color: var(--accent-bright);
  background: rgba(217, 164, 75, 0.2);
  box-shadow: 0 0 0 1px rgba(240, 190, 103, 0.35);
}

.stage-btn.cleared {
  border-color: rgba(217, 164, 75, 0.45);
  background: rgba(217, 164, 75, 0.1);
}

.stage-btn.locked {
  opacity: 0.38;
}

.stage-num {
  font-size: 14px;
  font-weight: 700;
  line-height: 1.1;
  font-variant-numeric: tabular-nums;
}

.stage-stars {
  font-size: 10px;
  line-height: 1;
  letter-spacing: -0.04em;
  color: var(--accent-bright);
  min-height: 11px;
}

.stage-stars.empty {
  color: rgba(255, 245, 228, 0.24);
}

/* ---------- 결과 오버레이 ---------- */
.result-card {
  text-align: center;
  max-width: 360px;
  padding: 24px 20px 20px;
}

.result-stage-label {
  margin: 0 0 4px;
  font-size: 12px;
  font-weight: 700;
  letter-spacing: 0.12em;
  color: var(--accent);
}

.result-title {
  margin: 0 0 10px;
  font-size: 26px;
  font-weight: 800;
  color: var(--fg);
}

.result-card.failed .result-title {
  color: var(--danger);
}

.result-stars {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  min-height: 44px;
  margin-bottom: 10px;
  font-size: 34px;
  line-height: 1;
  color: var(--accent-bright);
  text-shadow: 0 3px 12px rgba(240, 190, 103, 0.35);
}

.result-stars .star-off {
  color: rgba(255, 245, 228, 0.18);
  text-shadow: none;
}

.result-msg {
  margin: 0 0 8px;
  font-size: 14px;
  color: var(--fg);
  line-height: 1.5;
}

.result-meta {
  font-size: 12px;
  color: var(--fg-muted);
  margin-bottom: 18px;
  min-height: 18px;
}

/* ---------- 버튼 공통 ---------- */
.overlay-actions {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.btn-primary,
.btn-secondary,
.btn-ghost {
  width: 100%;
  min-height: 46px;
  border-radius: 12px;
  font-size: 15px;
  font-weight: 700;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  transition: transform 0.1s ease, filter 0.15s ease, background-color 0.15s ease;
}

.btn-primary {
  background: linear-gradient(180deg, var(--accent-bright) 0%, var(--accent) 100%);
  color: #1f160c;
  box-shadow: 0 6px 16px rgba(217, 164, 75, 0.28);
}

.btn-primary:hover:not(:disabled) {
  filter: brightness(1.06);
}

.btn-secondary {
  background: rgba(255, 245, 228, 0.08);
  border: 1px solid var(--border-strong);
  color: var(--fg);
}

.btn-secondary:hover:not(:disabled) {
  background: rgba(255, 245, 228, 0.14);
}

.btn-ghost {
  min-height: 40px;
  font-size: 13px;
  font-weight: 600;
  color: var(--fg-muted);
}

.btn-ghost:hover:not(:disabled) {
  color: var(--fg);
  background: rgba(255, 245, 228, 0.05);
}

.btn-primary:active:not(:disabled),
.btn-secondary:active:not(:disabled),
.btn-ghost:active:not(:disabled) {
  transform: scale(0.98);
}

.hidden {
  display: none !important;
}

/* ---------- 소형 화면 (320px~375px) 대응 ---------- */
@media (max-width: 375px) {
  .app {
    padding:
      calc(var(--safe-t) + 8px)
      calc(var(--safe-r) + 8px)
      calc(var(--safe-b) + 8px)
      calc(var(--safe-l) + 8px);
    gap: 8px;
  }

  .hud-chip {
    padding: 5px 6px;
    min-height: 44px;
  }

  .hud-value {
    font-size: 15px;
  }

  .act-btn {
    min-height: 36px;
    font-size: 12px;
    padding: 0 5px;
    gap: 3px;
  }

  .board-wrap {
    padding: 7px;
    border-radius: 13px;
    width: min(100%, 440px, calc(100dvh - var(--safe-t) - var(--safe-b) - 172px));
  }

  .overlay-card {
    padding: 16px 14px 14px;
  }

  .brand-title {
    font-size: 26px;
  }

  .stage-grid {
    gap: 5px;
  }

  .stage-btn {
    min-height: 44px;
  }
}

/* ---------- 짧은 세로 화면 대응 ---------- */
@media (max-height: 640px) {
  .app {
    gap: 6px;
  }

  .hud {
    gap: 6px;
  }

  .hud-chip {
    min-height: 42px;
    padding: 4px 8px;
  }

  .act-btn {
    min-height: 36px;
  }

  .stage-info {
    padding: 6px 10px;
  }

  .board-wrap {
    width: min(100%, 440px, calc(100dvh - var(--safe-t) - var(--safe-b) - 166px));
  }
}

/* ---------- 데스크톱 여백 ---------- */
@media (min-width: 560px) {
  .app {
    padding-top: calc(var(--safe-t) + 18px);
    padding-bottom: calc(var(--safe-b) + 18px);
  }
}

/* stage select cells */
.stage-cell {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 2px;
  min-height: 52px;
  border: 1px solid rgba(107, 74, 30, 0.35);
  border-radius: 10px;
  background: #f3dfbb;
  color: #4a3312;
  font: inherit;
  cursor: pointer;
  padding: 6px 2px;
}
.stage-cell .stage-num { font-weight: 700; font-size: 16px; }
.stage-cell .stage-stars { font-size: 10px; color: #c98a12; min-height: 12px; }
.stage-cell.cleared { background: #ffe9b8; border-color: #c98a12; }
.stage-cell.current { outline: 2px solid #2ea043; }
.stage-cell:active { transform: scale(0.95); }
