/* ============================================================
   2D 로켓 발사 · 풍동 유동 시뮬레이터 — 계기판 다크 테마
   외부 폰트·CDN 없음. 시스템 폰트 스택만 사용한다.
   ============================================================ */

:root {
  /* 계약 토큰 */
  --bg: #0d1117;
  --panel: #161b22;
  --line: #30363d;
  --fg: #c9d1d9;
  --dim: #8b949e;
  --accent: #58a6ff;

  /* 파생 토큰 (위 6개에서만 유도) */
  --line-soft: #232a33;
  --fg-hi: #e6edf3;
  --raise: #1f2630;
  --raise-hi: #283040;
  --accent-hi: #79b8ff;
  --accent-soft: rgba(88, 166, 255, .16);
  --on-accent: #0d1117;

  --r1: 4px;
  --r2: 6px;
  --pad-x: 14px;

  --font-ui: Pretendard, 'Malgun Gothic', system-ui, -apple-system, 'Segoe UI', 'Noto Sans KR', sans-serif;
  --font-mono: ui-monospace, Consolas, 'Cascadia Mono', 'D2Coding', monospace;

  /* 지수 감쇠 이징 — 바운스/일래스틱 금지 */
  --ease: cubic-bezier(.22, 1, .36, 1);
  --t: .14s;

  color-scheme: dark;
}

/* ── 레이아웃 ─────────────────────────────────────────────── */

/*
 * border-box 전역 적용. content-box 였을 때 #hud 의 max-height:calc(100% - 16px) 에
 * padding 14px + border 2px 가 더해져 정확히 8px 씩 화면 밖으로 삐져나왔다(실측).
 */
*, *::before, *::after { box-sizing: border-box; }

html, body, #app { height: 100%; margin: 0; }

body {
  overflow: hidden;
  background: var(--bg);
  color: var(--fg);
  font-family: var(--font-ui);
  font-size: 12px;
  -webkit-font-smoothing: antialiased;
  /* 모바일: 더블탭 확대만 끈다(핀치 확대는 접근성상 유지). 탭 지연 300ms 도 같이 사라진다. */
  touch-action: manipulation;
  -webkit-text-size-adjust: 100%;
  text-size-adjust: 100%;
  overscroll-behavior: none;
}

#app { display: flex; }

#panel {
  width: 340px;
  flex: 0 0 340px;
  overflow-y: auto;
  overscroll-behavior: contain;
  background: var(--panel);
  border-right: 1px solid var(--line);
}

#stage {
  flex: 1;
  position: relative;
  min-width: 0;
  background: var(--bg);
}

canvas#sim {
  width: 100%;
  height: 100%;
  display: block;
  touch-action: none;
}

/* ── 패널 머리말 ──────────────────────────────────────────── */

#brand {
  padding: 14px var(--pad-x) 12px;
  border-bottom: 1px solid var(--line);
}

#brand h1 {
  margin: 0;
  font-size: 13px;
  font-weight: 650;
  letter-spacing: -.01em;
  color: var(--fg-hi);
}

#brand h1::before {
  content: '';
  display: inline-block;
  width: 3px;
  height: 11px;
  margin-right: 7px;
  vertical-align: -1px;
  border-radius: 1px;
  background: var(--accent);
}

/* 작성자 표기 — 패널 최상단에 크게 */
#brand #author {
  margin: 0 0 8px;
  font-size: 20px;
  font-weight: 700;
  letter-spacing: .02em;
  line-height: 1.2;
  color: var(--fg-hi);
}

#brand p {
  margin: 5px 0 0;
  font-size: 10.5px;
  line-height: 1.45;
  letter-spacing: .01em;
  color: var(--dim);
}

/* ── 그룹 (details/summary) ───────────────────────────────── */

#groups > details,
#trackerHelp {
  border-bottom: 1px solid var(--line);
}

#groups > details > summary,
#trackerHelp > summary {
  position: sticky;
  top: 0;
  z-index: 2;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  padding: 9px var(--pad-x);
  background: var(--panel);
  color: var(--dim);
  font-size: 10.5px;
  font-weight: 700;
  letter-spacing: .1em;
  text-transform: uppercase;
  cursor: pointer;
  user-select: none;
  list-style: none;
  transition: color var(--t) var(--ease), background var(--t) var(--ease);
}

#groups > details > summary::-webkit-details-marker,
#trackerHelp > summary::-webkit-details-marker { display: none; }
#groups > details > summary::marker,
#trackerHelp > summary::marker { content: ''; }

#groups > details > summary:hover,
#trackerHelp > summary:hover { color: var(--fg-hi); background: var(--raise); }

/* 열림/닫힘 셰브런 */
#groups > details > summary::after,
#trackerHelp > summary::after {
  content: '';
  flex: none;
  width: 5px;
  height: 5px;
  margin-right: 2px;
  border-right: 1.5px solid currentColor;
  border-bottom: 1.5px solid currentColor;
  transform: rotate(-45deg);
  opacity: .65;
  transition: transform var(--t) var(--ease);
}

#groups > details[open] > summary::after,
#trackerHelp[open] > summary::after { transform: rotate(45deg); }

#groups > details[open] { padding-bottom: 6px; }

/* ── 컨트롤 행: [라벨 | 값] 2열, 슬라이더는 아래 한 줄 전체 ── */

#groups .row,
#groups > details > div {
  display: grid;
  grid-template-columns: 1fr auto;
  align-items: baseline;
  column-gap: 10px;
  row-gap: 6px;
  padding: 7px var(--pad-x) 9px;
}

#groups .row.tog,
#groups .row.sel { align-items: center; }

#groups label,
#groups .lab {
  font-size: 11.5px;
  line-height: 1.35;
  color: var(--dim);
  cursor: default;
}

#groups .row.tog label,
#groups .row.tog .lab { cursor: pointer; }

/* 값 리드아웃 — 등폭 + 자릿수 고정 */
#groups .val {
  font-family: var(--font-mono);
  font-size: 11.5px;
  color: var(--fg-hi);
  text-align: right;
  white-space: nowrap;
  font-variant-numeric: tabular-nums;
  min-width: 8ch; /* 값 폭이 바뀌어도 라벨이 흔들리지 않게 예약 ("1.225 kg/m³") */
}

/* ★ = 값 변경 시 유동 사전계산 재실행 */
#groups .star {
  margin-left: 5px;
  font-style: normal;
  font-size: 9px;
  vertical-align: .12em;
  color: var(--accent);
  opacity: .85;
}

/* 한 줄 전체를 쓰는 위젯 */
#groups input[type="range"],
#groups select,
#groups textarea,
#groups .row > button { grid-column: 1 / -1; }

/* ── 슬라이더 ─────────────────────────────────────────────── */

#panel input[type="range"] {
  -webkit-appearance: none;
  appearance: none;
  display: block;
  width: 100%;
  height: 16px;
  margin: 0;
  background: transparent;
  cursor: pointer;
}

#panel input[type="range"]::-webkit-slider-runnable-track {
  height: 3px;
  border-radius: 2px;
  background: var(--line);
}

#panel input[type="range"]::-moz-range-track {
  height: 3px;
  border-radius: 2px;
  background: var(--line);
}

#panel input[type="range"]::-webkit-slider-thumb {
  -webkit-appearance: none;
  appearance: none;
  width: 13px;
  height: 13px;
  margin-top: -5px;
  border: 2px solid var(--panel);
  border-radius: 50%;
  background: var(--accent);
  transition: transform .12s var(--ease), box-shadow .12s var(--ease), background .12s var(--ease);
}

#panel input[type="range"]::-moz-range-thumb {
  width: 13px;
  height: 13px;
  border: 2px solid var(--panel);
  border-radius: 50%;
  background: var(--accent);
  transition: transform .12s var(--ease), box-shadow .12s var(--ease);
}

#panel input[type="range"]:hover::-webkit-slider-thumb { box-shadow: 0 0 0 4px var(--accent-soft); }
#panel input[type="range"]:hover::-moz-range-thumb { box-shadow: 0 0 0 4px var(--accent-soft); }
#panel input[type="range"]:active::-webkit-slider-thumb { transform: scale(1.1); background: var(--accent-hi); }
#panel input[type="range"]:active::-moz-range-thumb { transform: scale(1.1); background: var(--accent-hi); }
#panel input[type="range"]:focus { outline: none; }
#panel input[type="range"]:focus-visible::-webkit-slider-thumb { box-shadow: 0 0 0 3px var(--accent-soft), 0 0 0 5px var(--accent); }
#panel input[type="range"]:focus-visible::-moz-range-thumb { box-shadow: 0 0 0 3px var(--accent-soft), 0 0 0 5px var(--accent); }

/* ── 토글 스위치 (진짜 checkbox) ──────────────────────────── */

#panel input[type="checkbox"] {
  -webkit-appearance: none;
  appearance: none;
  position: relative;
  flex: none;
  width: 30px;
  height: 17px;
  margin: 0;
  justify-self: end;
  border: 1px solid var(--line);
  border-radius: 9px;
  background: var(--bg);
  cursor: pointer;
  transition: background var(--t) var(--ease), border-color var(--t) var(--ease);
}

#panel input[type="checkbox"]::after {
  content: '';
  position: absolute;
  top: 2px;
  left: 2px;
  width: 11px;
  height: 11px;
  border-radius: 50%;
  background: var(--dim);
  transition: transform var(--t) var(--ease), background var(--t) var(--ease);
}

#panel input[type="checkbox"]:hover { border-color: var(--dim); }
#panel input[type="checkbox"]:checked { background: var(--accent); border-color: var(--accent); }
#panel input[type="checkbox"]:checked::after { transform: translateX(13px); background: var(--on-accent); }

/* ── select ───────────────────────────────────────────────── */

#panel select {
  -webkit-appearance: none;
  appearance: none;
  width: 100%;
  box-sizing: border-box;
  padding: 6px 27px 6px 9px;
  font: inherit;
  font-size: 12px;
  color: var(--fg);
  background-color: var(--bg);
  background-image: url("data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' width='10' height='6' viewBox='0 0 10 6'><path d='M1 1l4 4 4-4' fill='none' stroke='%238b949e' stroke-width='1.5' stroke-linecap='round' stroke-linejoin='round'/></svg>");
  background-repeat: no-repeat;
  background-position: right 9px center;
  border: 1px solid var(--line);
  border-radius: var(--r1);
  cursor: pointer;
  transition: border-color var(--t) var(--ease), box-shadow var(--t) var(--ease);
}

#panel select:hover { border-color: var(--dim); }
#panel select option { background: var(--panel); color: var(--fg); }

/* ── textarea ─────────────────────────────────────────────── */

#panel textarea {
  width: 100%;
  box-sizing: border-box;
  min-height: 66px;
  padding: 7px 9px;
  font-family: var(--font-mono);
  font-size: 11.5px;
  line-height: 1.55;
  tab-size: 2;
  color: var(--fg);
  background: var(--bg);
  border: 1px solid var(--line);
  border-radius: var(--r1);
  resize: vertical;
  transition: border-color var(--t) var(--ease), box-shadow var(--t) var(--ease);
}

#panel textarea:hover { border-color: var(--dim); }

#panel select:focus,
#panel textarea:focus {
  outline: none;
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-soft);
}

/* ── 버튼 ─────────────────────────────────────────────────── */

#panel button {
  display: block;
  box-sizing: border-box;
  /* 폼 컨트롤은 display:block 이어도 shrink-to-fit 이므로 폭을 명시해야 한다 */
  width: 100%;
  padding: 8px 12px;
  font: inherit;
  font-size: 12px;
  font-weight: 600;
  letter-spacing: .01em;
  color: var(--fg);
  background: var(--raise);
  border: 1px solid var(--line);
  border-radius: var(--r1);
  cursor: pointer;
  transition: background var(--t) var(--ease), border-color var(--t) var(--ease), color var(--t) var(--ease);
}

/* 좌우 14px 여백을 margin 으로 주므로 그만큼 폭에서 뺀다 */
#groups > details > button { width: calc(100% - var(--pad-x) * 2); margin: 6px var(--pad-x) 8px; }
#groups .row > button { width: 100%; margin: 0; }

#panel button:hover { background: var(--raise-hi); border-color: #3d4652; color: var(--fg-hi); }
#panel button:active { background: var(--raise); transform: translateY(1px); }

#panel button.accent {
  color: var(--on-accent);
  background: var(--accent);
  border-color: var(--accent);
}

#panel button.accent:hover { background: var(--accent-hi); border-color: var(--accent-hi); color: var(--on-accent); }
#panel button.accent:active { background: var(--accent); }

#panel button[disabled] {
  opacity: .45;
  cursor: not-allowed;
  transform: none;
}

/* ── Tracker 사용 절차 ────────────────────────────────────── */

#trackerHelp ol {
  margin: 0;
  padding: 2px 16px 14px 30px;
  font-size: 11.5px;
  line-height: 1.65;
  color: var(--dim);
}

#trackerHelp li { margin-bottom: 8px; }
#trackerHelp li:last-child { margin-bottom: 0; }
#trackerHelp li::marker { color: var(--accent); font-variant-numeric: tabular-nums; }

#trackerHelp code {
  padding: 0 4px;
  font-family: var(--font-mono);
  font-size: 11px;
  color: var(--fg);
  background: var(--bg);
  border: 1px solid var(--line-soft);
  border-radius: 3px;
}

/* ── HUD (스테이지 우상단 고정) ───────────────────────────── */

#hud {
  position: absolute;
  top: 12px;
  right: 12px;
  z-index: 3;
  pointer-events: none;
  display: flex;
  flex-direction: column;
  min-width: 208px;
  max-height: calc(100% - 24px);
  overflow: hidden;
  padding: 9px 12px;
  font-family: var(--font-mono);
  font-size: 11.5px;
  line-height: 1.55;
  font-variant-numeric: tabular-nums;
  color: var(--fg);
  background: rgba(13, 17, 23, .72);
  -webkit-backdrop-filter: blur(2px);
  backdrop-filter: blur(2px);
  border: 1px solid var(--line);
  border-radius: var(--r2);
}

#hudRows {
  display: grid;
  grid-template-columns: auto 1fr;
  column-gap: 16px;
  align-content: start;
  min-height: 0;
}

#hud.empty { display: none; }
#hud .hudk, #hud .k { color: var(--dim); white-space: nowrap; }
#hud .hudv, #hud .v { color: var(--fg-hi); text-align: right; white-space: nowrap; }
#hud .hudgap, #hud .sp { grid-column: 1 / -1; height: 7px; }

/* 행 단위 div 로 굽는 경우의 폴백 */
#hudRows > div { grid-column: 1 / -1; display: flex; justify-content: space-between; gap: 16px; }

/* 계기판 펼치기/접기 버튼 (좁은 화면 전용) */
#hud .hudmore {
  flex: 0 0 auto;
  pointer-events: auto;
  margin: 0 0 6px;
  padding: 6px 8px;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: rgba(88, 166, 255, .16);
  color: var(--accent);
  font: inherit;
  font-size: 10.5px;
  white-space: nowrap;
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
}

#hud .hudmore:active { background: rgba(88, 166, 255, .32); }

/* 좁은 화면: 폭을 제한하고, 잘라내지 말고 행 영역만 스크롤 */
#hud.narrow {
  top: 8px;
  right: 8px;
  min-width: 0;
  max-width: min(58%, 220px);
  padding: 7px 9px;
  font-size: 10px;
  line-height: 1.5;
  max-height: 46%;
  pointer-events: auto;
}

#hud.narrow #hudRows {
  column-gap: 8px;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
}

#hud.narrow.expanded {
  max-width: min(72%, 300px);
  max-height: calc(100% - 16px);
}

/* 세로가 짧은 화면(가로로 눕힌 폰)에서는 펼쳐도 캔버스를 다 덮지 않게 */
@media (max-height: 500px) {
  #hud.narrow { max-height: 62%; }
  #hud.narrow.expanded { max-height: calc(100% - 16px); max-width: min(46%, 260px); }
}

/* ── 진행 오버레이 (유동 사전계산 · 캡처) ─────────────────── */

#busy {
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  z-index: 5;
  display: none; /* JS 가 'flex' 로 켠다 */
  flex-direction: column;
  gap: 11px;
  box-sizing: border-box;
  min-width: 272px;
  padding: 16px 18px;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 8px;
  box-shadow: 0 14px 36px rgba(1, 4, 9, .55);
}

#busyText {
  font-size: 12px;
  letter-spacing: .01em;
  color: var(--fg-hi);
}

#busyBarOuter {
  height: 4px;
  border-radius: 2px;
  background: var(--bg);
  border: 1px solid var(--line-soft);
  overflow: hidden;
}

#busyBar {
  width: 0%; /* JS 인라인 스타일로 갱신 */
  height: 100%;
  border-radius: 2px;
  background: var(--accent);
  transition: width .12s linear;
}

/* ── 자체 검증 결과 오버레이 ──────────────────────────────── */

#selftest {
  position: absolute;
  inset: 0;
  z-index: 6;
  display: none; /* JS 가 'block' 으로 켠다 */
  margin: 0;
  padding: 52px 22px 18px;
  overflow: auto;
  white-space: pre;
  tab-size: 2;
  font-family: var(--font-mono);
  font-size: 12px;
  line-height: 1.6;
  color: var(--fg);
  background: rgba(13, 17, 23, .94);
}

/* 자체 검증 리포트 닫기 바 — 없으면 시뮬레이터가 영영 가려진다 */
#selftestBar {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  z-index: 7;
  display: none;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 8px 14px;
  background: var(--panel);
  border-bottom: 1px solid var(--line);
  font-size: 12px;
  color: var(--dim);
}

#selftestBar #selftestSummary {
  font-family: var(--font-mono);
  color: var(--fg);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

#selftestClose {
  flex: 0 0 auto;
  padding: 5px 12px;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: var(--accent);
  color: #0d1117;
  font: inherit;
  font-weight: 600;
  cursor: pointer;
}

#selftestClose:hover { filter: brightness(1.1); }

/* 치명적 오류 배너 — 캔버스가 조용히 까맣게 죽지 않게 */
#fatal {
  position: absolute;
  top: 12px;
  left: 12px;
  right: 12px;
  z-index: 8;
  display: none;
  max-height: 45%;
  overflow: auto;
  padding: 12px 14px;
  border: 1px solid #ff6b81;
  border-radius: 8px;
  background: rgba(60, 14, 22, .96);
  color: #ffd7dd;
  font-family: var(--font-mono);
  font-size: 12px;
  line-height: 1.5;
  white-space: pre-wrap;
}

/* ── 포커스 링 ────────────────────────────────────────────── */

#panel button:focus-visible,
#panel input[type="checkbox"]:focus-visible,
#panel summary:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

/* ── 스크롤바 ─────────────────────────────────────────────── */

#panel, #selftest { scrollbar-width: thin; scrollbar-color: #2b323c transparent; }
#panel::-webkit-scrollbar, #selftest::-webkit-scrollbar { width: 10px; height: 10px; }
#panel::-webkit-scrollbar-track, #selftest::-webkit-scrollbar-track { background: transparent; }
#panel::-webkit-scrollbar-thumb {
  background: #2b323c;
  border: 3px solid var(--panel);
  border-radius: 5px;
}
#selftest::-webkit-scrollbar-thumb {
  background: #2b323c;
  border: 3px solid var(--bg);
  border-radius: 5px;
}
#panel::-webkit-scrollbar-thumb:hover,
#selftest::-webkit-scrollbar-thumb:hover { background: #3d4652; }

/* ── 좁은 화면: 패널을 위로 접는다 ────────────────────────── */

@media (max-width: 900px) {
  #panel { width: 300px; flex-basis: 300px; }
}

@media (max-width: 640px) {
  #app { flex-direction: column; }
  #panel {
    width: auto;
    flex: 0 0 42%;
    max-height: 42%;
    border-right: none;
    border-bottom: 1px solid var(--line);
  }
  #stage { flex: 1 1 auto; min-height: 0; }
}

/* 가로로 눕힌 폰: 세로가 짧으므로 패널을 다시 옆으로 */
@media (max-width: 900px) and (orientation: landscape) and (max-height: 500px) {
  #app { flex-direction: row; }
  #panel {
    width: 260px;
    flex: 0 0 260px;
    max-height: 100%;
    border-right: 1px solid var(--line);
    border-bottom: none;
  }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { transition-duration: .01ms !important; }
}
