:root {
  --bg: #17121f;
  --panel: #241c33;
  --line: #3d3256;
  --text: #f1ecf8;
  --muted: #b3a8c8;
  --accent: #8fe0b4;
  --danger: #ff8a8a;
}

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

html, body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: system-ui, -apple-system, "Segoe UI", "Malgun Gothic", "Apple SD Gothic Neo", sans-serif;
  font-size: 15px;
  overflow: hidden;
}

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

button {
  font: inherit;
  color: var(--text);
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 8px;
  min-height: 44px;
  min-width: 44px;
  padding: 0 12px;
  cursor: pointer;
  touch-action: manipulation;
}
button:hover:not(:disabled) { border-color: var(--accent); }
button:focus-visible, input:focus-visible, textarea:focus-visible, select:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}
button:disabled { opacity: 0.55; cursor: default; }
button[aria-pressed="true"] { background: #2f5a49; border-color: var(--accent); }

#app {
  display: flex;
  flex-direction: column;
  height: 100%;
  max-width: 960px;
  margin: 0 auto;
  /* viewport-fit=cover: 노치·둥근 모서리 쪽 여백(아래는 캔버스가 끝까지, 버튼만 띄움) */
  padding: env(safe-area-inset-top) env(safe-area-inset-right) 0 env(safe-area-inset-left);
}

#topbar {
  flex: 0 0 48px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 6px;
  padding: 2px 6px;
  background: var(--panel);
  border-bottom: 1px solid var(--line);
}
#zone-buttons { display: flex; gap: 6px; }
#zone-buttons button { min-height: 44px; padding: 0 10px; }
#topbar { position: relative; z-index: 5; }
#stats {
  display: inline-flex;
  align-items: center;
  gap: 2px;
  min-width: 0;
  padding: 0 6px;
  font-size: 13px;
  white-space: nowrap;
  color: var(--muted);
  background: transparent;
  border-color: transparent;
}
#stats span { color: var(--text); font-weight: 600; }
#stats .sr-only { color: inherit; }
#stats .stat-icon {
  width: 14px;
  height: 18px;
  margin-left: 3px;
  image-rendering: pixelated;
  image-rendering: crisp-edges;
}
#stats-balloon {
  position: absolute;
  top: calc(100% + 8px);
  right: 6px;
  width: max-content;
  max-width: calc(100% - 12px);
  padding: 8px 10px;
  background: #fffdf4;
  color: #241c33;
  border-radius: 10px;
  font-size: 13px;
  line-height: 1.35;
  pointer-events: none;
  box-shadow: 0 2px 6px rgba(0, 0, 0, 0.45);
}
#stats-balloon::after {
  content: "";
  position: absolute;
  right: 40px;
  top: -6px;
  border: 6px solid transparent;
  border-top: 0;
  border-bottom-color: #fffdf4;
}

#stage {
  position: relative;
  flex: 1 1 auto;
  min-height: 0;
  overflow: hidden;
  background: #160f24;
}
#shop-canvas {
  display: block;
  width: 100%;
  height: 100%;
  touch-action: none;
  user-select: none;
  -webkit-user-select: none;
}

#npc-balloon {
  position: absolute;
  transform: translate(-50%, -100%);
  width: max-content;
  max-width: min(190px, calc(100% - 8px));
  padding: 8px 10px;
  background: #fffdf4;
  color: #241c33;
  border-radius: 10px;
  font-size: 13px;
  line-height: 1.35;
  pointer-events: none;
  box-shadow: 0 2px 6px rgba(0, 0, 0, 0.45);
  overflow-wrap: anywhere;
}
#npc-balloon::after {
  content: "";
  position: absolute;
  left: 50%;
  bottom: -6px;
  margin-left: -6px;
  border: 6px solid transparent;
  border-bottom: 0;
  border-top-color: #fffdf4;
}

#sit-popup, #guestbook-popup {
  position: absolute;
  transform: translate(-50%, -100%);
}
#sit-popup button, #guestbook-popup button { background: #2f5a49; border-color: var(--accent); }

/* 상단 행동 메뉴(D62): 앉으면 [홀][단체석] 바로 아래로 스르륵 내려옴. 가게 화면 위에 반투명으로 겹쳐 캔버스 크기는 그대로 */
#action-menu {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  z-index: 3;
  display: flex;
  justify-content: center;
  gap: 6px;
  padding: 6px;
  background: rgba(36, 28, 51, 0.72);
  border-bottom: 1px solid rgba(143, 224, 180, 0.25);
  -webkit-backdrop-filter: blur(4px);
  backdrop-filter: blur(4px);
  transform: translateY(-100%);
  visibility: hidden;
  transition: transform 220ms ease-out, visibility 0s linear 220ms;
}
#action-menu.open {
  transform: translateY(0);
  visibility: visible;
  transition: transform 220ms ease-out, visibility 0s;
}
#action-menu button {
  flex: 0 1 140px;
  min-width: 0;
  padding: 0 8px;
  white-space: nowrap;
  background: rgba(47, 90, 73, 0.9);
  border-color: var(--accent);
}

#toast {
  position: fixed;
  left: 50%;
  bottom: calc(72px + env(safe-area-inset-bottom));
  transform: translateX(-50%);
  max-width: min(92vw, 420px);
  padding: 10px 14px;
  background: #000d;
  border: 1px solid var(--line);
  border-radius: 10px;
  font-size: 14px;
  text-align: center;
  z-index: 50;
}

/* 방명록 (1.2단계 G9: 목록 먼저, 아래 고정 [글 남기기], 입력창 시트, 메모지 카드) */
#guestbook {
  padding: 0;
  width: min(92vw, 480px);
  height: min(90vh, 760px);
  max-width: none;
  max-height: none;
  background: var(--panel);
  color: var(--text);
  border: 1px solid var(--line);
  border-radius: 12px;
  overflow: hidden;
}
#guestbook[open] { display: block; }
#guestbook::backdrop { background: rgba(0, 0, 0, 0.6); }
.gb-frame {
  position: relative;
  display: flex;
  flex-direction: column;
  height: 100%;
}
@media (max-width: 599px) {
  #guestbook {
    width: 100vw;
    height: 100vh;
    height: 100dvh;
    margin: 0;
    border: 0;
    border-radius: 0;
  }
}
.gb-head {
  flex: 0 0 auto;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 8px 12px;
  border-bottom: 1px solid var(--line);
}
.gb-head h2 { margin: 0; font-size: 18px; }
.gb-scroll {
  flex: 1 1 auto;
  min-height: 0;
  overflow-y: auto;
  padding: 12px 12px 8px;
  background: #3a2c22;
  background-image: radial-gradient(rgba(255, 255, 255, 0.05) 1px, transparent 1px);
  background-size: 14px 14px;
}
#gb-write {
  flex: 0 0 auto;
  width: 100%;
  min-height: 48px;
  border-radius: 0;
  border-width: 1px 0 0;
  background: #2f5a49;
  border-color: var(--accent);
  font-weight: 600;
}
#gb-sheet {
  position: absolute;
  left: 0;
  right: 0;
  bottom: 0;
  max-height: 88%;
  overflow-y: auto;
  padding: 12px 14px 14px;
  background: var(--panel);
  border-top: 1px solid var(--accent);
  border-radius: 14px 14px 0 0;
  box-shadow: 0 -8px 24px rgba(0, 0, 0, 0.5);
  animation: gb-sheet-up 200ms ease-out;
}
@keyframes gb-sheet-up {
  from { transform: translateY(100%); }
  to { transform: translateY(0); }
}
#gb-sheet h3 { margin: 0 0 4px; font-size: 16px; }
#gb-form label { display: block; margin-top: 10px; font-size: 13px; color: var(--muted); }
#gb-form input, #gb-form textarea {
  width: 100%;
  margin-top: 4px;
  padding: 10px;
  font: inherit;
  color: var(--text);
  background: #120d1b;
  border: 1px solid var(--line);
  border-radius: 8px;
}
#gb-form textarea { resize: vertical; }
.gb-count { margin: 2px 0 0; font-size: 12px; color: var(--muted); text-align: right; }
.field-error, .form-error { margin: 4px 0 0; font-size: 13px; color: var(--danger); }
.field-error:empty, .form-error:empty { display: none; }
.gb-sheet-actions { display: flex; gap: 8px; margin-top: 12px; }
#gb-submit { flex: 1 1 0; background: #2f5a49; border-color: var(--accent); }
#gb-cancel { flex: 0 0 auto; min-width: 88px; }
#gb-form .form-error { margin-top: 8px; }

#gb-list { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 14px; }
#gb-more { width: 100%; margin-top: 14px; }
#gb-empty { color: #e8dccb; text-align: center; margin: 24px 0; }
#gb-list-error { margin-top: 8px; }

/* 메모지 카드 */
.gb-item {
  position: relative;
  padding: 12px 14px 12px;
  color: #2b2118;
  border: 2px solid transparent;
  border-radius: 4px;
  box-shadow: 0 2px 6px rgba(0, 0, 0, 0.4);
}
.gb-memo-0 { background: #fff3b0; }
.gb-memo-1 { background: #ffd6e0; }
.gb-memo-2 { background: #cfe8ff; }
.gb-memo-3 { background: #d4f5c9; }
.gb-memo-4 { background: #ffdcc2; }
.gb-tilt-0 { transform: rotate(-2deg); }
.gb-tilt-1 { transform: rotate(-1.2deg); }
.gb-tilt-2 { transform: rotate(-0.5deg); }
.gb-tilt-3 { transform: rotate(0deg); }
.gb-tilt-4 { transform: rotate(0.6deg); }
.gb-tilt-5 { transform: rotate(1.3deg); }
.gb-tilt-6 { transform: rotate(2deg); }
.gb-item-head { display: flex; justify-content: space-between; gap: 8px; font-size: 13px; padding-right: 36px; }
.gb-nick { font-weight: 700; overflow-wrap: anywhere; }
.gb-time { color: #5a4636; white-space: nowrap; }
.gb-content { margin: 6px 0 0; white-space: pre-wrap; overflow-wrap: anywhere; }
.gb-item button { color: #2b2118; }

.gb-new {
  border-color: #ffb347;
  box-shadow: 0 0 0 3px rgba(255, 179, 71, 0.65), 0 0 18px rgba(255, 179, 71, 0.8);
  animation: gb-pop 2500ms ease-out;
}
@keyframes gb-pop {
  0% { scale: 1; }
  12% { scale: 1.04; }
  30% { scale: 1; }
  100% { scale: 1; }
}

.gb-menu-open { z-index: 5; }
.gb-menu-btn {
  position: absolute;
  top: -2px;
  right: -2px;
  width: 44px;
  height: 44px;
  min-width: 44px;
  min-height: 44px;
  padding: 0;
  font-size: 22px;
  line-height: 1;
  background: transparent;
  border: 0;
  border-radius: 22px;
}
.gb-menu-btn:hover:not(:disabled) { background: rgba(0, 0, 0, 0.08); }
.gb-menu {
  position: absolute;
  top: 38px;
  right: 8px;
  z-index: 3;
  display: flex;
  flex-direction: column;
  min-width: 110px;
  background: #fffdf4;
  border: 1px solid #b9a88f;
  border-radius: 8px;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.35);
  overflow: hidden;
}
.gb-menu button {
  min-height: 44px;
  padding: 0 14px;
  text-align: left;
  background: transparent;
  border: 0;
  border-radius: 0;
}
.gb-menu button:hover, .gb-menu button:focus-visible { background: #f0e4cc; }

.gb-del-form, .gb-report-box { display: flex; gap: 6px; align-items: center; flex-wrap: wrap; margin-top: 8px; }
.gb-report-box > span { flex: 1 1 100%; font-size: 14px; }
.gb-del-password {
  flex: 1 1 120px;
  min-height: 44px;
  padding: 0 10px;
  font: inherit;
  color: #2b2118;
  background: rgba(255, 255, 255, 0.75);
  border: 1px solid #8a7660;
  border-radius: 8px;
}
.gb-del-confirm, .gb-report-confirm, .gb-del-cancel, .gb-report-cancel {
  font-size: 14px;
  background: rgba(255, 255, 255, 0.7);
  border: 1px solid #8a7660;
}
.gb-del-error, .gb-status { margin: 6px 0 0; font-size: 13px; color: #7a1f1f; }
.gb-del-error:empty, .gb-status:empty { display: none; }
.gb-status { color: #1f5a3a; }

@media (prefers-reduced-motion: reduce) {
  .gb-tilt-0, .gb-tilt-1, .gb-tilt-2, .gb-tilt-3, .gb-tilt-4, .gb-tilt-5, .gb-tilt-6 { transform: none; }
  #gb-sheet { animation: none; }
  .gb-new { animation: none; }
}

@media (prefers-reduced-motion: reduce) {
  * { transition: none !important; animation: none !important; }
}

/* 1.1단계: 확대 버튼, 로딩, 성능 표시 */
#zoom-controls {
  position: absolute;
  /* 아이폰 홈 바·둥근 모서리와 겹치지 않게(D61) */
  right: calc(8px + env(safe-area-inset-right));
  bottom: calc(8px + env(safe-area-inset-bottom));
  display: flex;
  flex-direction: column;
  gap: 6px;
  z-index: 3;
}
#zoom-controls button {
  width: 44px;
  height: 44px;
  padding: 0;
  font-size: 18px;
  line-height: 1;
  background: rgba(36, 28, 51, 0.88);
}
#zoom-controls #btn-zoom-fit { font-size: 12px; }
#loading {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  background: #160f24;
  color: var(--muted);
  z-index: 4;
}
#perf {
  position: absolute;
  left: 6px;
  bottom: 4px;
  font-size: 11px;
  color: #cfc6e0;
  background: rgba(0, 0, 0, 0.55);
  padding: 2px 6px;
  border-radius: 6px;
  pointer-events: none;
  z-index: 3;
}

/* ===== 콩&베 상담소 (2026-11-18) ===== */
#consult-popup {
  position: absolute;
  transform: translate(-50%, -100%);
}
#consult-popup button { background: #2f5a49; border-color: var(--accent); }

/* 홀·창가 꼬마 말풍선(NPC 말풍선과 같은 모양) */
#kb-balloon {
  position: absolute;
  transform: translate(-50%, -100%);
  width: max-content;
  max-width: min(170px, calc(100% - 8px));
  padding: 7px 10px;
  background: #fff6fb;
  color: #2b1c3a;
  border-radius: 10px;
  font-size: 13px;
  line-height: 1.35;
  pointer-events: none;
  box-shadow: 0 2px 6px rgba(0, 0, 0, 0.45);
  overflow-wrap: anywhere;
  z-index: 2;
}
#kb-balloon::after {
  content: "";
  position: absolute;
  left: 50%;
  bottom: -6px;
  margin-left: -6px;
  border: 6px solid transparent;
  border-bottom: 0;
  border-top-color: #fff6fb;
}

#consult {
  padding: 0;
  width: min(92vw, 480px);
  max-width: none;
  max-height: min(90vh, 760px);
  height: min(90vh, 760px);
  background: var(--panel);
  color: var(--text);
  border: 1px solid var(--line);
  border-radius: 12px;
  overflow: hidden;
}
#consult[open] { display: block; animation: cs-up 220ms ease-out; }
#consult::backdrop { background: rgba(0, 0, 0, 0.6); }
@keyframes cs-up {
  from { transform: translateY(100%); }
  to { transform: translateY(0); }
}
@media (max-width: 599px) {
  #consult {
    width: 100vw;
    max-height: none;
    height: 100vh;
    height: 100dvh;
    margin: 0;
    border: 0;
    border-radius: 0;
  }
}
.cs-frame { display: flex; flex-direction: column; height: 100%; }
.cs-head {
  flex: 0 0 auto;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 8px 12px;
  border-bottom: 1px solid var(--line);
}
.cs-head h2 { margin: 0; font-size: 18px; }
.cs-stage {
  flex: 0 0 auto;
  display: flex;
  align-items: flex-end;
  padding: 120px 8px 6px;
  background: linear-gradient(#2b2040, #3a2c52);
}
.cs-fig {
  position: relative;
  flex: 0 0 50%;
  margin: 0;
  display: flex;
  justify-content: center;
}
.cs-body {
  position: relative;
  width: 80%;
  max-width: 160px;
  aspect-ratio: 1 / 1;
}
.cs-flip .cs-body { scale: -1 1; }
.cs-img { display: block; width: 100%; height: 100%; }
.cs-hat { position: absolute; display: block; pointer-events: none; }
.cs-say {
  position: absolute;
  left: 50%;
  bottom: 68%;
  transform: translateX(-50%);
  width: max-content;
  max-width: calc(100% - 4px);
  padding: 7px 10px;
  background: #fffdf4;
  color: #241c33;
  border-radius: 10px;
  font-size: 13px;
  line-height: 1.35;
  box-shadow: 0 2px 6px rgba(0, 0, 0, 0.45);
  overflow-wrap: anywhere;
}
.cs-say::after {
  content: "";
  position: absolute;
  left: 50%;
  bottom: -6px;
  margin-left: -6px;
  border: 6px solid transparent;
  border-bottom: 0;
  border-top-color: #fffdf4;
}
.cs-low {
  flex: 1 1 auto;
  min-height: 0;
  overflow-y: auto;
  padding: 12px 14px calc(14px + env(safe-area-inset-bottom));
  display: flex;
  flex-direction: column;
  gap: 8px;
}
#cs-guide { margin: 0; font-size: 14px; color: var(--muted); }
#cs-asked { margin: 0; font-weight: 600; overflow-wrap: anywhere; }
.cs-row { display: flex; gap: 8px; }
#cs-question {
  flex: 1 1 auto;
  min-width: 0;
  min-height: 44px;
  padding: 0 10px;
  font: inherit;
  color: var(--text);
  background: #120d1b;
  border: 1px solid var(--line);
  border-radius: 8px;
}
#cs-ask, #cs-again, .cs-trust button { background: #2f5a49; border-color: var(--accent); }
#cs-ask { flex: 0 0 auto; }
.cs-react {
  align-self: center;
  max-width: 90%;
  padding: 6px 10px;
  background: #3d3256;
  border: 1px solid var(--line);
  border-radius: 10px;
  font-size: 13px;
  overflow-wrap: anywhere;
}
.cs-react[data-speaker="bebe"] { border-color: #b69cf0; }
.cs-react[data-speaker="kongju"] { border-color: #a8dc7b; }
#cs-closing { margin: 0; text-align: center; font-weight: 600; }
.cs-trust { display: flex; gap: 8px; }
.cs-trust button { flex: 1 1 0; }
#cs-result { margin: 0; text-align: center; font-weight: 600; color: var(--accent); }

/* 연출(동작 줄이기에서는 전역 규칙으로 모두 꺼짐) */
#cs-bebe.cs-rolling .cs-body { animation: cs-roll 1500ms ease-in-out; }
#cs-kongju.cs-wiggle .cs-body { animation: cs-wiggle 500ms ease-in-out 3; transform-origin: 50% 96%; }
.cs-chosen .cs-body { animation: cs-hop 500ms ease-out; filter: drop-shadow(0 0 8px rgba(255, 220, 120, 0.9)); }
@keyframes cs-roll {
  0% { transform: translateX(0) rotate(0deg); }
  35% { transform: translateX(14px) rotate(200deg); }
  70% { transform: translateX(-14px) rotate(-120deg); }
  100% { transform: translateX(0) rotate(360deg); }
}
@keyframes cs-wiggle {
  0%, 100% { transform: rotate(0deg); }
  25% { transform: rotate(5deg); }
  75% { transform: rotate(-5deg); }
}
@keyframes cs-hop {
  0%, 100% { transform: translateY(0); }
  40% { transform: translateY(-12px); }
}
