/* ==========================================================================
   base.css — 디자인 토큰 / 리셋 / 타이포그래피 / 공통 컨트롤
   ========================================================================== */

:root {
  /* 색상 — 다크(기본) */
  --bg:            #0a0e18;
  --bg-elev:       #111726;
  --bg-elev-2:     #18203300;
  --surface:       #121a2b;
  --surface-2:     #18223a;
  --surface-3:     #1f2b47;
  --line:          #243250;
  --line-soft:     #1c2740;

  --text:          #e8edf9;
  --text-dim:      #9fb0d0;
  /* 가장 흐린 글자색. 작은 글씨(.7~.8rem)에 많이 쓰므로 어느 바탕(bg·surface·surface-2·surface-3)
     위에서도 명암비 4.5:1 을 넘겨야 한다 — 예전 #6d80a6 은 surface-2 위에서 3.98 이라 읽기 힘들었다
     (tools/contrast-qa.mjs 가 이 조건을 계속 확인한다) */
  --text-mute:     #8798bd;

  /**
   * 강조색은 쓰임새마다 색이 다르다 — 하나로 쓰면 작은 글씨에서 반드시 안 읽히는 자리가 생긴다.
   *   --accent       테두리·포커스 링·글로우·큰 글자 (밝고 선명한 쪽)
   *   --accent-on    그 위에 흰 작은 글씨를 올릴 배경 (흰 글자 대비 4.5 이상)
   *   --accent-text  글자색으로 쓰는 강조색 (어느 바탕에서도 4.5 이상)
   * (tools/contrast-qa.mjs 가 이 조건을 계속 확인한다)
   */
  --accent:        #5b8cff;
  --accent-on:     #3568e0;
  --accent-text:   #8fb3ff;
  --accent-2:      #7c5cff;
  --accent-ink:    #ffffff;
  --accent-soft:   rgba(91, 140, 255, .14);

  --good:          #2fd98a;
  --good-soft:     rgba(47, 217, 138, .14);
  --warn:          #ffb028;
  --warn-soft:     rgba(255, 176, 40, .14);
  --bad:           #ff5c6c;
  --bad-soft:      rgba(255, 92, 108, .14);
  --gold:          #ffcc4d;

  /* 플레이 영역(그라운드) — games.css 와 캔버스 게임(stage.js themeColors)이 같이 쓴다 */
  --field-bg:      #070a12;
  --field-line:    color-mix(in oklab, var(--accent) 70%, var(--line));
  --field-ring:    var(--accent-soft);
  --field-grid:    rgba(159, 176, 208, .28);
  /* 판 위의 칸 — 판 바탕(--field-bg)과 눈으로 구분돼야 한다. 예전 #121a2b 은 명암비 1.14 라
     숫자 위치 기억·카드 짝 맞추기처럼 칸이 작은 게임에서 칸 경계가 잘 보이지 않았다 */
  --field-cell:    #1a2540;

  /* 모양 */
  --r-sm: 8px;
  --r-md: 12px;
  --r-lg: 18px;
  --r-xl: 24px;
  --r-pill: 999px;

  --shadow-1: 0 1px 2px rgba(0,0,0,.35);
  --shadow-2: 0 6px 20px -8px rgba(0,0,0,.55);
  --shadow-3: 0 20px 50px -20px rgba(0,0,0,.7);

  --ring: 0 0 0 3px rgba(91,140,255,.45);

  /* 레이아웃 */
  --shell: 1120px;
  --gutter: 16px;
  --header-h: 116px;

  --font: "Pretendard Variable", Pretendard, -apple-system, BlinkMacSystemFont,
          "Segoe UI", "Malgun Gothic", "Apple SD Gothic Neo", Roboto, sans-serif;
  --mono: ui-monospace, SFMono-Regular, "SF Mono", "Cascadia Mono", Menlo,
          Consolas, "Liberation Mono", monospace;

  /* 숫자 폭 고정: 타이머가 흔들리지 않게 */
  --num: "tnum" 1, "lnum" 1;

  color-scheme: dark;
}

:root[data-theme="light"] {
  --bg:            #f4f6fc;
  --bg-elev:       #ffffff;
  --surface:       #ffffff;
  --surface-2:     #f0f3fb;
  --surface-3:     #e4e9f7;
  --line:          #d9e0f0;
  --line-soft:     #e8edf8;

  --text:          #131a2b;
  --text-dim:      #4d5c7d;
  /* 흰 바탕뿐 아니라 판 바탕(--field-bg)·눌린 칩 위에서도 4.5:1 을 넘겨야 한다 — 작은 글씨에 많이 쓴다 */
  --text-mute:     #535f74;

  --accent:        #3a6df0;
  --accent-on:     #3a6df0;
  --accent-text:   #2a57c9;
  --accent-2:      #6a45f0;
  --accent-soft:   rgba(58, 109, 240, .10);

  /* 상태 색도 작은 글씨(하단 스탯 값)로 쓰인다 — surface-2 위에서 4.5:1 을 넘도록 한 단계 진하게 */
  --good:          #066536;
  --good-soft:     rgba(6, 101, 54, .12);
  --warn:          #9a5c00;
  --warn-soft:     rgba(154, 92, 0, .12);
  --bad:           #c92637;
  --bad-soft:      rgba(201, 38, 55, .12);
  --gold:          #c99400;

  /* 밝은 테마에선 바탕과 판의 명도 차가 작아 선을 진하게 */
  /* 판 바탕 — 칸(--field-cell #f7f9fe)과 명암비 1.2 이상이 되도록 한 톤 낮춘다 */
  --field-bg:      #dce3f2;
  --field-line:    var(--accent);
  --field-ring:    rgba(58, 109, 240, .18);
  --field-grid:    rgba(60, 80, 120, .30);
  --field-cell:    #f7f9fe;

  --shadow-1: 0 1px 2px rgba(20,30,60,.08);
  --shadow-2: 0 6px 18px -8px rgba(20,30,60,.18);
  --shadow-3: 0 20px 46px -22px rgba(20,30,60,.28);

  color-scheme: light;
}

/* -------------------------------------------------------------- 리셋 */
*, *::before, *::after { box-sizing: border-box; }

html { -webkit-text-size-adjust: 100%; }

body {
  margin: 0;
  min-height: 100dvh;
  background: var(--bg);
  color: var(--text);
  font-family: var(--font);
  font-size: 15px;
  line-height: 1.6;
  -webkit-font-smoothing: antialiased;
  text-rendering: optimizeLegibility;
  overflow-wrap: anywhere;
}

/* 은은한 배경 그라디언트 (게임 캔버스 렌더에 영향 없음) */
body::before {
  content: "";
  position: fixed;
  inset: 0;
  pointer-events: none;
  z-index: -1;
  background:
    radial-gradient(1100px 520px at 12% -8%, rgba(91,140,255,.16), transparent 60%),
    radial-gradient(900px 480px at 92% 2%, rgba(124,92,255,.13), transparent 62%);
}
:root[data-theme="light"] body::before {
  background:
    radial-gradient(1100px 520px at 12% -8%, rgba(58,109,240,.10), transparent 60%),
    radial-gradient(900px 480px at 92% 2%, rgba(106,69,240,.08), transparent 62%);
}

h1, h2, h3, h4 { margin: 0; line-height: 1.25; font-weight: 700; letter-spacing: -.02em; }
h1 { font-size: clamp(1.35rem, 1.1rem + 1.2vw, 1.85rem); }
h2 { font-size: 1.06rem; }
p  { margin: 0; }

a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }

img, svg, canvas { display: block; max-width: 100%; }

button, input, select, textarea { font: inherit; color: inherit; }

/* 포커스: 마우스 클릭엔 안 보이고 키보드엔 보이게 */
:focus { outline: none; }
:focus-visible {
  outline: none;
  box-shadow: var(--ring);
}
/* 모서리가 없는 글자 링크만 살짝 둥글게 — 버튼·알약 모양은 제 모양 그대로 둔다 */
a:focus-visible { border-radius: var(--r-sm); }

::selection { background: var(--accent); color: var(--accent-ink); }

/* 스크롤바 */
* { scrollbar-width: thin; scrollbar-color: var(--surface-3) transparent; }
*::-webkit-scrollbar { width: 10px; height: 10px; }
*::-webkit-scrollbar-thumb {
  background: var(--surface-3);
  border-radius: var(--r-pill);
  border: 3px solid transparent;
  background-clip: content-box;
}

.skip-link {
  position: absolute; left: -9999px; top: 0; z-index: 100;
  background: var(--accent); color: var(--accent-ink);
  padding: 10px 16px; border-radius: 0 0 var(--r-md) 0; font-weight: 600;
}
.skip-link:focus { left: 0; }
/* 건너뛰기로 포커스만 받는 본문 — 테두리를 그리지 않는다 (main.js initSkipLink) */
#stage:focus { outline: none; }

.noscript {
  position: fixed; inset: auto 0 0 0; z-index: 90;
  background: var(--bad); color: #fff; padding: 14px; text-align: center; font-weight: 600;
}

.sr-only {
  position: absolute; width: 1px; height: 1px; padding: 0; margin: -1px;
  overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; border: 0;
}

/* -------------------------------------------------------------- 버튼 */
.btn {
  --btn-bg: var(--surface-2);
  --btn-fg: var(--text);
  --btn-line: var(--line);
  appearance: none;
  display: inline-flex; align-items: center; justify-content: center; gap: 7px;
  min-height: 38px;
  padding: 0 16px;
  border: 1px solid var(--btn-line);
  border-radius: var(--r-md);
  background: var(--btn-bg);
  color: var(--btn-fg);
  font-weight: 650;
  font-size: .9rem;
  letter-spacing: -.01em;
  cursor: pointer;
  user-select: none;
  white-space: nowrap;
  touch-action: manipulation;              /* 두 번 탭 확대 방지 */
  -webkit-tap-highlight-color: transparent;
  transition: background .14s, border-color .14s, transform .08s, color .14s, box-shadow .14s;
}
.btn:hover { background: var(--surface-3); }
.btn:active { transform: translateY(1px); }
.btn[disabled] { opacity: .45; cursor: not-allowed; transform: none; }

.btn-primary {
  /* 흰 글자를 올리는 배경이므로 --accent 가 아니라 --accent-on (밝은 파랑 위의 흰 글자는 잘 안 읽힌다) */
  --btn-bg: linear-gradient(180deg, var(--accent-on), color-mix(in oklab, var(--accent-on) 82%, #000));
  --btn-fg: var(--accent-ink);
  --btn-line: transparent;
  box-shadow: 0 6px 18px -10px color-mix(in oklab, var(--accent) 80%, transparent);
}
.btn-primary:hover { filter: brightness(1.08); background: var(--btn-bg); }

.btn-ghost { --btn-bg: transparent; --btn-line: var(--line); }
.btn-danger { --btn-fg: var(--bad); --btn-line: color-mix(in oklab, var(--bad) 45%, transparent); }
.btn-danger:hover { background: var(--bad-soft); }

.btn-lg { min-height: 46px; padding: 0 24px; font-size: 1rem; }
.btn-sm { min-height: 30px; padding: 0 11px; font-size: .8rem; border-radius: var(--r-sm); }

.icon-btn {
  appearance: none;
  display: inline-grid; place-items: center;
  width: 38px; height: 38px; flex: none;
  border: 1px solid var(--line);
  border-radius: var(--r-md);
  background: var(--surface-2);
  color: var(--text-dim);
  cursor: pointer;
  transition: background .14s, color .14s, border-color .14s;
}
.icon-btn:hover { background: var(--surface-3); color: var(--text); }

.text-btn {
  appearance: none; border: 0; background: none; padding: 2px 4px;
  color: var(--text-dim); font-size: .84rem; font-weight: 600;
  cursor: pointer; border-radius: var(--r-sm);
}
.text-btn:hover { color: var(--accent); }
.text-btn.danger:hover { color: var(--bad); }

/* 테마 아이콘 토글 */
.ico-sun { display: none; }
:root[data-theme="light"] .ico-moon { display: none; }
:root[data-theme="light"] .ico-sun { display: block; }

/* -------------------------------------------------------------- 칩/배지 */
.chip {
  display: inline-flex; align-items: center; gap: 5px;
  padding: 3px 10px;
  border-radius: var(--r-pill);
  background: var(--accent-soft);
  color: var(--accent);
  border: 1px solid color-mix(in oklab, var(--accent) 28%, transparent);
  font-size: .74rem; font-weight: 700; letter-spacing: -.01em;
  white-space: nowrap;
}
.chip-muted { background: var(--surface-2); color: var(--text-mute); border-color: var(--line); }
.chip-good  { background: var(--good-soft); color: var(--good); border-color: color-mix(in oklab, var(--good) 30%, transparent); }
.chip-warn  { background: var(--warn-soft); color: var(--warn); border-color: color-mix(in oklab, var(--warn) 30%, transparent); }
.chip-bad   { background: var(--bad-soft);  color: var(--bad);  border-color: color-mix(in oklab, var(--bad) 30%, transparent); }

/* -------------------------------------------------------------- 토스트 */
.toast-host {
  position: fixed; z-index: 80;
  left: 50%; bottom: calc(18px + env(safe-area-inset-bottom));
  transform: translateX(-50%);
  display: flex; flex-direction: column; gap: 8px; align-items: center;
  pointer-events: none;
  width: min(92vw, 420px);
}
.toast {
  pointer-events: auto;
  width: 100%;
  display: flex; align-items: center; gap: 10px;
  padding: 11px 15px;
  border-radius: var(--r-md);
  background: var(--bg-elev);
  border: 1px solid var(--line);
  box-shadow: var(--shadow-3);
  font-size: .88rem; font-weight: 600;
  animation: toast-in .22s cubic-bezier(.2,.9,.3,1);
}
.toast.is-out { animation: toast-out .18s ease forwards; }
.toast-dot { width: 8px; height: 8px; border-radius: 50%; background: var(--accent); flex: none; }
.toast[data-kind="good"] .toast-dot { background: var(--good); }
.toast[data-kind="warn"] .toast-dot { background: var(--warn); }
.toast[data-kind="bad"]  .toast-dot { background: var(--bad); }

@keyframes toast-in  { from { opacity: 0; transform: translateY(12px) scale(.97); } }
@keyframes toast-out { to   { opacity: 0; transform: translateY(6px)  scale(.98); } }

/* -------------------------------------------------------------- 유틸 */
.num { font-variant-numeric: tabular-nums lining-nums; font-feature-settings: var(--num); }
.mono { font-family: var(--mono); }
.hidden { display: none !important; }

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: .001ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: .001ms !important;
  }
}
