@font-face {
  font-family: "Black Han Sans";
  src: url("fonts/BlackHanSans-Regular.ttf") format("truetype");
  font-display: block;
}

:root {
  --ink: #2a2350;
  --ink-2: #4b4374;
  --paper: #ffd33d;
  --paper-deep: #f2bf1d;
  --sheet: #fdfdfb;
  --red: #e5413b;
  --green: #2fb45a;
  --font-display: "Black Han Sans", "Malgun Gothic", sans-serif;
  --font-ui: "Pretendard", "Apple SD Gothic Neo", "Malgun Gothic", "Noto Sans KR", system-ui, sans-serif;
  --lift: 0 2px 3px rgba(42, 35, 80, 0.22), 0 8px 18px -6px rgba(42, 35, 80, 0.38);
  --lift-high: 0 4px 6px rgba(42, 35, 80, 0.2), 0 18px 34px -10px rgba(42, 35, 80, 0.5);
  --ease-out: cubic-bezier(0.16, 1, 0.3, 1);
  --safe-t: env(safe-area-inset-top, 0px);
  --safe-b: env(safe-area-inset-bottom, 0px);
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }

/* 화학식 아래 첨자: 큰 글꼴에 없는 ₂ 대신 같은 글꼴 숫자를 내려 쓴다 */
.f-sub { font-size: 0.56em; position: relative; top: 0.2em; margin-left: 0.02em; font-family: inherit; }

html, body {
  margin: 0;
  height: 100%;
  overflow: hidden;
  background: var(--paper);
  color: var(--ink);
  font-family: var(--font-ui);
  -webkit-tap-highlight-color: transparent;
  overscroll-behavior: none;
}

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

.app {
  position: fixed;
  inset: 0;
  touch-action: none;
  user-select: none;
  -webkit-user-select: none;
}

.layer { position: absolute; inset: 0; width: 100%; height: 100%; }
#hands, .tags { pointer-events: none; }

.video-src {
  position: absolute;
  left: 0;
  top: 0;
  width: 2px;
  height: 2px;
  opacity: 0;
  pointer-events: none;
}

.icon {
  width: 22px;
  height: 22px;
  fill: none;
  stroke: currentColor;
  stroke-width: 2.2;
  stroke-linecap: round;
  stroke-linejoin: round;
  flex: none;
}

button { font: inherit; color: inherit; cursor: pointer; }
button:focus-visible { outline: 3px solid var(--ink); outline-offset: 3px; }

/* ---------- 위 줄 ---------- */
.topbar {
  position: fixed;
  top: calc(10px + var(--safe-t));
  left: 12px;
  right: 12px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  pointer-events: none;
}
.topbar > * { pointer-events: auto; }

.chip {
  display: inline-flex;
  align-items: center;
  gap: 7px;
  min-height: 42px;
  padding: 10px 15px 10px 11px;
  border-radius: 999px;
  background: var(--sheet);
  box-shadow: var(--lift);
  font-size: 14px;
  font-weight: 800;
  white-space: nowrap;
}
.chip .icon { width: 20px; height: 20px; }
.chip-dot {
  width: 9px;
  height: 9px;
  border-radius: 50%;
  background: #b9b3cf;
  box-shadow: 0 0 0 2px var(--sheet), 0 0 0 3.5px rgba(42, 35, 80, 0.18);
}
.chip[data-state="on"] .chip-dot { background: var(--green); }
.chip[data-state="wait"] .chip-dot { background: var(--paper-deep); animation: blink 1.1s ease-in-out infinite; }
@keyframes blink { 50% { opacity: 0.35; } }

.actions { display: flex; align-items: center; gap: 8px; }

.dex-btn {
  display: inline-flex;
  align-items: baseline;
  gap: 8px;
  height: 44px;
  padding: 0 17px;
  border: 3px solid #fff;
  border-radius: 999px;
  background: var(--paper);
  color: var(--ink);
  box-shadow: var(--lift);
  font-family: var(--font-display);
  font-size: 19px;
  line-height: 38px;
  white-space: nowrap;
  transition: transform 160ms var(--ease-out), box-shadow 160ms var(--ease-out);
}
.dex-num { font-size: 15px; font-variant-numeric: tabular-nums; }
.dex-num b { font-weight: 400; font-size: 19px; }
.dex-btn:hover { transform: translateY(-2px) rotate(-2deg); box-shadow: var(--lift-high); }
.dex-btn.is-bump { animation: bump 520ms var(--ease-out); }
@keyframes bump {
  0% { transform: scale(1); }
  35% { transform: scale(1.22) rotate(-6deg); }
  100% { transform: scale(1); }
}

.icon-btn {
  display: grid;
  place-items: center;
  width: 44px;
  height: 44px;
  border: 0;
  border-radius: 50%;
  background: var(--sheet);
  color: var(--ink);
  box-shadow: var(--lift);
  transition: transform 160ms var(--ease-out);
}
.icon-btn:hover { transform: translateY(-2px); }
.icon-btn:active { transform: scale(0.94); }

/* ---------- 아래 스티커 시트 ---------- */
.sheet {
  position: fixed;
  left: 50%;
  bottom: 0;
  transform: translateX(-50%);
  width: min(100%, 600px);
  padding: 12px 10px calc(10px + var(--safe-b));
  border-radius: 26px 26px 0 0;
  background: var(--sheet);
  box-shadow: 0 -2px 3px rgba(42, 35, 80, 0.12), 0 -12px 28px -10px rgba(42, 35, 80, 0.4);
}
.sheet-tiles { display: grid; grid-template-columns: repeat(6, 1fr); gap: 2px; }

.tile {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 7px;
  padding: 6px 0 2px;
  border: 0;
  background: none;
  touch-action: none;
}
.tile-ball {
  display: grid;
  place-items: center;
  width: 50px;
  height: 50px;
  border: 3.5px solid #fff;
  border-radius: 50%;
  background:
    radial-gradient(circle at 34% 28%, rgba(255, 255, 255, 0.8) 0 9%, rgba(255, 255, 255, 0) 30%),
    radial-gradient(circle at 60% 72%, rgba(0, 0, 0, 0) 40%, rgba(20, 10, 40, 0.28) 100%),
    var(--ball);
  color: var(--ball-ink);
  font-family: var(--font-display);
  font-size: 21px;
  line-height: 1;
  outline: 2px dashed rgba(42, 35, 80, 0.3);
  outline-offset: 3px;
  box-shadow: 0 2px 2px rgba(42, 35, 80, 0.18), 0 5px 10px -4px rgba(42, 35, 80, 0.35);
  transition: transform 200ms var(--ease-out), box-shadow 200ms var(--ease-out);
}
.tile-name { font-size: 13px; font-weight: 800; color: var(--ink); letter-spacing: -0.01em; }
.tile:hover .tile-ball, .tile.is-hover .tile-ball {
  transform: translateY(-7px) rotate(-8deg);
  box-shadow: 0 4px 4px rgba(42, 35, 80, 0.16), 0 14px 18px -8px rgba(42, 35, 80, 0.5);
}
.tile.is-hover .tile-ball { outline-color: var(--ink); }
.tile:active .tile-ball { transform: scale(0.92); }
.tile:focus-visible { outline: none; }
.tile:focus-visible .tile-ball { outline: 3px solid var(--ink); outline-offset: 3px; }

/* ---------- 도움말·알림 ---------- */
.hint, .toast {
  position: fixed;
  left: 50%;
  transform: translateX(-50%);
  margin: 0;
  max-width: calc(100% - 32px);
  padding: 11px 18px;
  border-radius: 999px;
  background: var(--ink);
  color: var(--paper);
  box-shadow: var(--lift);
  font-size: 15px;
  font-weight: 800;
  text-align: center;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  pointer-events: none;
}
.hint { bottom: calc(116px + var(--safe-b)); animation: rise 380ms var(--ease-out); }
.toast { top: calc(66px + var(--safe-t)); background: var(--sheet); color: var(--ink); }
@keyframes rise { from { opacity: 0; transform: translate(-50%, 8px); } }

/* ---------- 분자 이름표 ---------- */
.tag {
  position: absolute;
  left: 0;
  top: 0;
  display: inline-flex;
  align-items: baseline;
  gap: 7px;
  padding: 6px 12px 7px;
  border-radius: 999px;
  background: var(--sheet);
  color: var(--ink);
  box-shadow: var(--lift);
  white-space: nowrap;
  will-change: transform;
}
.tag-formula { font-family: var(--font-display); font-weight: 400; font-size: 20px; line-height: 1; }
.tag-text { font-size: 13.5px; font-weight: 800; }
.tag.is-done { background: var(--paper); border: 3px solid #fff; }
.tag.is-wip { background: rgba(253, 253, 251, 0.94); outline: 2px dashed rgba(42, 35, 80, 0.45); outline-offset: -5px; padding: 8px 14px; }
.tag.is-wip .tag-text { font-size: 13px; }
.tag.is-flash { background: var(--red); color: #fff; }

/* ---------- 시작 스티커 ---------- */
.start {
  position: fixed;
  inset: 0;
  display: grid;
  place-items: center;
  padding: 80px 18px 130px;
  pointer-events: none;
}
.start[hidden] { display: none; }
.start-card {
  width: min(100%, 430px);
  padding: 30px 26px 22px;
  border: 7px solid #fff;
  border-radius: 30px;
  background: var(--sheet);
  box-shadow: var(--lift-high);
  transform: rotate(-1.5deg);
  pointer-events: auto;
  touch-action: manipulation;
}
.start-card h1 {
  margin: 0 0 12px;
  font-family: var(--font-display);
  font-weight: 400;
  font-size: clamp(32px, 8.5vw, 46px);
  line-height: 1.08;
  letter-spacing: -0.01em;
  text-wrap: balance;
}
.start-lead { margin: 0 0 22px; font-size: 16.5px; line-height: 1.55; color: var(--ink-2); font-weight: 600; text-wrap: pretty; }
.btn-primary {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 10px;
  width: 100%;
  min-height: 60px;
  padding: 10px 20px;
  border: 0;
  border-radius: 999px;
  background: var(--ink);
  color: var(--paper);
  font-family: var(--font-display);
  font-size: 20px;
  box-shadow: var(--lift);
  transition: transform 160ms var(--ease-out), box-shadow 160ms var(--ease-out);
}
.btn-primary:hover { transform: translateY(-2px); box-shadow: var(--lift-high); }
.btn-primary:active { transform: scale(0.98); }
.btn-primary:disabled { cursor: progress; opacity: 0.78; transform: none; }
.btn-primary:focus-visible { outline-color: var(--paper-deep); }
.btn-quiet {
  display: block;
  margin: 8px auto 0;
  padding: 12px 14px;
  border: 0;
  background: none;
  color: var(--ink);
  font-size: 15.5px;
  font-weight: 800;
  text-decoration: underline;
  text-decoration-thickness: 2px;
  text-underline-offset: 5px;
}
.cam-error {
  margin: 12px 0 0;
  padding: 11px 14px;
  border-radius: 16px;
  background: #fde3e1;
  color: #6b1612;
  font-size: 14.5px;
  font-weight: 700;
  line-height: 1.5;
}
.start-note { margin: 14px 0 0; font-size: 13px; line-height: 1.5; color: var(--ink-2); font-weight: 600; text-align: center; text-wrap: balance; }

/* ---------- 새 물질 발견 ---------- */
.discovery {
  position: fixed;
  top: calc(70px + var(--safe-t));
  left: 50%;
  width: min(calc(100% - 36px), 390px);
  transform: translateX(-50%);
  pointer-events: none;
  z-index: 5;
}
.discovery[hidden] { display: none; }
.discovery.is-low { top: auto; bottom: calc(132px + var(--safe-b)); }
.disc-card {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  gap: 6px;
  padding: 22px 24px 20px;
  border: 7px solid #fff;
  border-radius: 30px;
  background:
    linear-gradient(112deg, rgba(255, 255, 255, 0) 30%, rgba(255, 140, 210, 0.55) 40%, rgba(120, 225, 255, 0.6) 48%, rgba(255, 245, 140, 0.6) 56%, rgba(255, 255, 255, 0) 66%) no-repeat,
    var(--paper);
  background-size: 260% 100%, auto;
  background-position: 130% 0, 0 0;
  box-shadow: var(--lift-high);
  transform: rotate(-2deg);
}
.discovery.is-in .disc-card {
  animation: slap 560ms var(--ease-out) both, foil 1500ms 120ms ease-out both;
}
@keyframes slap {
  0% { opacity: 0; transform: scale(0.5) rotate(-16deg); }
  60% { opacity: 1; transform: scale(1.05) rotate(0deg); }
  100% { transform: scale(1) rotate(-2deg); }
}
@keyframes foil { from { background-position: 130% 0, 0 0; } to { background-position: -40% 0, 0 0; } }
.discovery.is-fly {
  transition: transform 600ms cubic-bezier(0.55, 0, 0.75, 0.2), opacity 600ms ease-in;
  transform: translateX(-50%) translate(var(--fx), var(--fy)) scale(var(--fs));
  opacity: 0.2;
}
.disc-no { font-size: 14px; font-weight: 900; color: var(--ink-2); font-variant-numeric: tabular-nums; letter-spacing: 0.02em; }
.disc-formula { font-family: var(--font-display); font-weight: 400; font-size: 78px; line-height: 0.95; letter-spacing: -0.01em; }
.disc-name { font-family: var(--font-display); font-size: 30px; line-height: 1.1; }
.disc-fact { margin: 4px 0 6px; font-size: 16px; line-height: 1.5; font-weight: 700; color: var(--ink); text-wrap: pretty; }
.disc-stamp { padding: 5px 12px; border-radius: 999px; background: var(--ink); color: var(--paper); font-size: 13.5px; font-weight: 800; }

/* ---------- 도감 ---------- */
.dex {
  position: fixed;
  inset: 0;
  z-index: 8;
  display: flex;
  align-items: flex-end;
  justify-content: center;
  background: rgba(42, 35, 80, 0);
  transition: background 260ms ease;
  touch-action: auto;
}
.dex[hidden] { display: none; }
.dex.is-open { background: rgba(42, 35, 80, 0.45); }
.dex-page {
  display: flex;
  flex-direction: column;
  width: min(100%, 560px);
  max-height: min(88vh, 860px);
  padding: 20px 18px calc(16px + var(--safe-b));
  border-radius: 28px 28px 0 0;
  background:
    radial-gradient(circle, rgba(160, 120, 0, 0.12) 1.6px, transparent 2px) 0 0 / 22px 22px,
    var(--paper);
  box-shadow: var(--lift-high);
  transform: translateY(100%);
  transition: transform 300ms var(--ease-out);
  user-select: text;
  -webkit-user-select: text;
}
.dex.is-open .dex-page { transform: none; }
.dex-head { display: flex; align-items: center; gap: 12px; margin-bottom: 14px; }
.dex-head h2 { margin: 0; font-family: var(--font-display); font-weight: 400; font-size: 30px; line-height: 1; }
.dex-total { margin-right: auto; font-family: var(--font-display); font-size: 19px; font-variant-numeric: tabular-nums; color: var(--ink-2); }
.dex-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(150px, 1fr));
  gap: 12px;
  margin: 0;
  padding: 4px 4px 12px;
  list-style: none;
  overflow-y: auto;
  scrollbar-width: thin;
  scrollbar-color: var(--ink) transparent;
}
.dex-grid::-webkit-scrollbar { width: 8px; }
.dex-grid::-webkit-scrollbar-thumb { background: var(--ink); border-radius: 8px; }
.slot {
  display: flex;
  flex-direction: column;
  gap: 3px;
  min-height: 158px;
  padding: 12px 13px;
  border: 2.5px dashed rgba(42, 35, 80, 0.4);
  border-radius: 22px;
}
.slot-no { font-size: 12px; font-weight: 900; color: var(--ink-2); font-variant-numeric: tabular-nums; }
.slot-formula { font-family: var(--font-display); font-weight: 400; font-size: 36px; line-height: 1.05; margin-top: 2px; }
.slot-unknown { color: rgba(42, 35, 80, 0.32); }
.slot-name { font-size: 15.5px; font-weight: 900; }
.slot-fact { font-size: 13px; line-height: 1.45; font-weight: 600; color: var(--ink-2); }
.slot.is-got {
  border: 5px solid #fff;
  background: var(--sheet);
  box-shadow: var(--lift);
  transform: rotate(-1.2deg);
}
.slot.is-got:nth-child(2n) { transform: rotate(1deg); }
.slot.is-got:nth-child(3n) { transform: rotate(-0.4deg); }
.dex-foot { display: flex; align-items: center; flex-wrap: wrap; gap: 6px 12px; padding-top: 12px; font-size: 13px; font-weight: 700; color: var(--ink-2); }
.btn-sheet {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  height: 44px;
  padding: 0 18px 0 14px;
  border: 0;
  border-radius: 999px;
  background: var(--sheet);
  color: var(--ink);
  box-shadow: var(--lift);
  font-size: 15px;
  font-weight: 800;
}
.btn-sheet .icon { width: 19px; height: 19px; }

@media (min-width: 820px) {
  .dex { justify-content: flex-end; align-items: stretch; }
  .dex-page {
    max-height: none;
    height: 100%;
    width: 520px;
    border-radius: 28px 0 0 28px;
    padding-top: calc(22px + var(--safe-t));
    transform: translateX(100%);
  }
  .tile-ball { width: 56px; height: 56px; font-size: 23px; }
  .hint { bottom: calc(126px + var(--safe-b)); }
}

@media (max-width: 440px) {
  .topbar { left: 10px; right: 10px; }
  .chip .icon { display: none; }
  .chip { padding: 10px 13px 10px 12px; }
  .dex-btn { padding: 0 13px; gap: 6px; }
  .actions { gap: 6px; }
}

@media (max-width: 360px) {
  .tile-ball { width: 44px; height: 44px; font-size: 19px; }
  .chip { padding: 10px 11px; font-size: 13px; }
  .dex-btn { padding: 0 11px; font-size: 17px; }
}

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