:root {
  --bg: #0b1026;
  --night: #141a3a;
  --panel: #161d42;
  --line: #2c3770;
  --text: #f3ead8;
  --muted: #a9b0cf;
  --orange: #ffb45a;
  --gold: #ffd45a;
  --red: #e8454b;
  --ok: #7ed6a0;
  --font: "Apple SD Gothic Neo", system-ui, -apple-system, sans-serif;
}

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

html, body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: var(--font);
  -webkit-text-size-adjust: 100%;
  overflow: hidden;
}

#app {
  position: relative;
  display: flex;
  flex-direction: column;
  height: 100vh;
  height: 100dvh;
  max-width: 100vw;
  overflow: hidden;
}

canvas.ico, canvas.ava, .pixel {
  image-rendering: pixelated;
  image-rendering: crisp-edges;
}

/* 데모 표기 */
.demo-strip {
  display: flex;
  justify-content: flex-end;
  align-items: center;
  gap: 10px;
  padding: 4px 12px;
  font-size: 11px;
  color: var(--muted);
  background: #070a1c;
  flex: none;
  z-index: 30;
}
.back-link {
  color: var(--orange);
  text-decoration: none;
  display: inline-flex;
  align-items: center;
  min-height: 44px;
  min-width: 44px;
  margin: -14px -8px;
  padding: 0 8px;
}
.back-link:hover, .back-link:focus-visible { text-decoration: underline; }

/* 상단 고정 HUD */
.hud {
  flex: none;
  padding: 8px 12px 8px;
  background: linear-gradient(#10163a, #0d1330);
  border-bottom: 2px solid #232c60;
  z-index: 20;
}
.hud-row {
  display: flex;
  align-items: center;
  gap: 8px;
}
.balance {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 6px 12px 6px 8px;
  border-radius: 999px;
  background: #241c10;
  border: 2px solid #6a4a1a;
  color: var(--gold);
  font-size: 14px;
  min-height: 40px;
}
.balance canvas { width: 24px; height: 24px; }
.balance b { font-size: 20px; min-width: 1.2em; text-align: right; }
.balance span { color: #e8c98a; font-size: 13px; }
.balance.bump { animation: bump .4s ease; }
@keyframes bump { 50% { transform: scale(1.12); } }
.who {
  flex: 1;
  display: flex;
  align-items: center;
  gap: 6px;
  min-width: 0;
  font-size: 14px;
  color: var(--muted);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.who canvas { width: 16px; height: 24px; flex: none; }
.who b { color: var(--text); }
.progress { margin-top: 6px; }
.progress p { margin: 0 0 4px; font-size: 13px; color: var(--text); }
.bar { height: 6px; border-radius: 3px; background: #232c60; overflow: hidden; }
.bar i { display: block; height: 100%; width: 0; background: linear-gradient(90deg, var(--orange), var(--gold)); transition: width .4s ease; }

/* 버튼 */
.btn {
  appearance: none;
  font: inherit;
  font-size: 15px;
  min-height: 44px;
  min-width: 44px;
  padding: 10px 16px;
  border-radius: 10px;
  border: 2px solid #3a4688;
  background: #1f2858;
  color: var(--text);
  cursor: pointer;
  box-shadow: inset 0 -3px 0 rgba(0,0,0,.25);
  touch-action: manipulation;
}
.btn:hover { background: #26306a; }
.btn:focus-visible, .slot:focus-visible, .shop-card:focus-visible, .pick:focus-visible, .person-btn:focus-visible {
  outline: 3px solid var(--gold);
  outline-offset: 2px;
}
.btn.primary { background: #c8642a; border-color: #ffb45a; color: #fff8ec; }
.btn.primary:hover { background: #d8743a; }
.btn.small { padding: 6px 12px; font-size: 14px; }
.btn.ghost { background: transparent; border-color: #3a4688; }
.btn.toggle[aria-pressed="true"] { background: #3a3010; border-color: var(--gold); color: var(--gold); }
.btn.wide { width: 100%; }
.btn.danger { background: #5a1f2a; border-color: #e8454b; }
.btn[disabled] { opacity: .5; cursor: not-allowed; }

/* 지도 무대 */
.stage {
  position: relative;
  flex: 1;
  min-height: 0;
  overflow: hidden;
}
#map {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  display: block;
  touch-action: none;
  cursor: pointer;
}
.guide {
  position: absolute;
  left: 50%;
  top: 8px;
  transform: translateX(-50%);
  margin: 0;
  max-width: calc(100% - 24px);
  padding: 6px 12px;
  border-radius: 999px;
  background: rgba(10, 14, 36, .78);
  border: 1px solid #3a4688;
  font-size: 13px;
  text-align: center;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  pointer-events: none;
}
@media (max-width: 400px) { .guide { font-size: 12px; } }

.door-bar {
  position: absolute;
  right: 12px;
  bottom: 12px;
  width: min(300px, calc(100% - 180px));
  padding: 10px;
  border-radius: 12px;
  background: rgba(16, 20, 50, .92);
  border: 2px solid var(--orange);
  box-shadow: 0 0 24px rgba(255, 160, 80, .3);
  z-index: 5;
}
.door-bar p { margin: 0 0 8px; font-size: 14px; color: var(--gold); }
.door-btns { display: flex; gap: 8px; flex-wrap: wrap; }
.door-btns .btn { flex: 1 1 100px; }

.dpad {
  position: absolute;
  left: 12px;
  bottom: 12px;
  width: 156px;
  height: 156px;
  display: none;
  z-index: 5;
  touch-action: none;
}
.dpad .d {
  position: absolute;
  width: 52px;
  height: 52px;
  border-radius: 12px;
  border: 2px solid #3a4688;
  background: rgba(20, 26, 66, .82);
  padding: 0;
  touch-action: none;
  -webkit-user-select: none;
  user-select: none;
}
.dpad .d.on { background: #c8642a; border-color: var(--orange); }
.dpad .up { left: 52px; top: 0; }
.dpad .down { left: 52px; bottom: 0; }
.dpad .left { left: 0; top: 52px; }
.dpad .right { right: 0; top: 52px; }
.dpad i { display: block; width: 0; height: 0; margin: auto; border: 9px solid transparent; }
.dpad .up i { border-bottom: 12px solid var(--text); border-top-width: 0; }
.dpad .down i { border-top: 12px solid var(--text); border-bottom-width: 0; }
.dpad .left i { border-right: 12px solid var(--text); border-left-width: 0; }
.dpad .right i { border-left: 12px solid var(--text); border-right-width: 0; }
.key-hint {
  position: absolute;
  left: 12px;
  bottom: 12px;
  margin: 0;
  font-size: 12px;
  color: var(--muted);
  background: rgba(10, 14, 36, .7);
  padding: 6px 10px;
  border-radius: 8px;
  pointer-events: none;
}
@media (max-width: 760px), (pointer: coarse) {
  .dpad { display: block; }
  .key-hint { display: none; }
}
@media (max-width: 360px) {
  .dpad { width: 144px; height: 144px; }
  .dpad .d { width: 48px; height: 48px; }
  .dpad .up, .dpad .down { left: 48px; }
  .dpad .left, .dpad .right { top: 48px; }
}

/* 패널 */
.panel {
  position: absolute;
  inset: 0;
  z-index: 10;
  overflow-y: auto;
  overflow-x: hidden;
  background: rgba(8, 11, 30, .94);
  padding: 12px 12px 32px;
  -webkit-overflow-scrolling: touch;
}
.panel-inner { max-width: 560px; margin: 0 auto; }
.panel-head {
  display: flex;
  align-items: center;
  gap: 10px;
  margin-bottom: 10px;
}
.panel-head h2 { margin: 0; font-size: 19px; flex: 1; min-width: 0; }
.panel .lead { margin: 4px 0 14px; color: var(--muted); font-size: 14px; line-height: 1.55; }
.panel .fine { font-size: 12px; color: var(--muted); line-height: 1.5; margin: 8px 0 0; }
.panel h3 { font-size: 15px; margin: 18px 0 8px; color: var(--gold); }
.panel h3 small { color: var(--muted); font-weight: normal; margin-left: 6px; font-size: 12px; }
.row { display: flex; gap: 8px; flex-wrap: wrap; margin-top: 12px; }
.row .btn { flex: 1 1 140px; }
.error {
  min-height: 1.4em;
  margin: 10px 0 0;
  color: #ff9a9a;
  font-size: 14px;
}
.error:empty { min-height: 0; margin: 0; }

/* 트리 */
.tree-wrap {
  position: relative;
  width: min(320px, 86vw);
  margin: 6px auto 0;
  aspect-ratio: 120 / 150;
}
.tree-wrap > canvas {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  border-radius: 12px;
  border: 2px solid #2c3770;
  image-rendering: pixelated;
}
.slot {
  position: absolute;
  width: 44px;
  height: 44px;
  margin: -22px 0 0 -22px;
  border-radius: 50%;
  border: 2px dashed rgba(255, 230, 170, .35);
  background: rgba(10, 20, 30, .25);
  padding: 0;
  cursor: pointer;
}
.slot.filled {
  border: 2px solid rgba(255, 212, 90, .8);
  background: rgba(30, 20, 10, .55);
  box-shadow: 0 0 14px rgba(255, 200, 100, .55);
  animation: twinkle 2.4s ease-in-out infinite;
}
.slot.fresh { animation: pop .6s ease; }
@keyframes pop { 0% { transform: scale(.3); } 70% { transform: scale(1.2); } 100% { transform: scale(1); } }
@keyframes twinkle { 50% { box-shadow: 0 0 22px rgba(255, 220, 120, .9); } }
.slot canvas { width: 32px; height: 32px; display: block; margin: auto; }
.slot .slot-by {
  position: absolute;
  left: 50%;
  top: 100%;
  transform: translate(-50%, 1px);
  font-size: 11px;
  background: #ffd45a;
  color: #2a1a10;
  border-radius: 6px;
  padding: 0 5px;
  white-space: nowrap;
  pointer-events: none;
}
.slot.sel { outline: 3px solid #fff; }
.tree-meta { text-align: center; margin: 10px 0 0; font-size: 14px; }
.slot-info {
  margin: 8px auto 0;
  min-height: 3em;
  max-width: 420px;
  text-align: center;
  font-size: 14px;
  color: var(--muted);
  line-height: 1.5;
}

/* 고르기 목록 */
.picks { display: grid; grid-template-columns: 1fr; gap: 8px; }
.pick, .shop-card, .person-btn {
  appearance: none;
  font: inherit;
  color: var(--text);
  display: flex;
  align-items: center;
  gap: 10px;
  min-height: 56px;
  width: 100%;
  padding: 8px 12px;
  border-radius: 12px;
  border: 2px solid #2c3770;
  background: #141b44;
  text-align: left;
  cursor: pointer;
}
.pick canvas, .shop-card canvas { width: 36px; height: 36px; flex: none; }
.pick .t, .shop-card .t { display: flex; flex-direction: column; min-width: 0; flex: 1; }
.pick .t b, .shop-card .t b { font-size: 15px; overflow-wrap: anywhere; }
.pick .t small, .shop-card .t small { color: var(--muted); font-size: 12px; margin-top: 2px; }
.pick[aria-pressed="true"], .shop-card[aria-pressed="true"], .person-btn[aria-pressed="true"] {
  border-color: var(--gold);
  background: #2a2414;
}
.pick.off { opacity: .55; }

.sticky-bar {
  position: sticky;
  bottom: -32px;
  margin: 14px -12px -32px;
  padding: 10px 12px 20px;
  background: #080b1e;
  border-top: 2px solid #232c60;
  z-index: 2;
}

/* 샵 */
.shop-grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 8px; }
@media (min-width: 560px) { .shop-grid { grid-template-columns: repeat(3, minmax(0, 1fr)); } }
.shop-card { flex-direction: column; align-items: flex-start; min-height: 104px; position: relative; }
.shop-card canvas { width: 40px; height: 40px; }
.shop-card .price { color: var(--gold); font-size: 13px; }
.shop-card .stock { color: var(--muted); font-size: 12px; }
.shop-card.soldout { opacity: .6; }
.tag {
  display: inline-block;
  font-size: 12px;
  padding: 1px 8px;
  border-radius: 6px;
  background: #5a1f2a;
  color: #ffd0d0;
  border: 1px solid #e8454b;
}
.shop-card .tag { position: absolute; right: 8px; top: 8px; }
.bag { font-size: 13px; color: var(--muted); margin: 10px 0 0; line-height: 1.5; }

/* 사진관 */
.people-grid { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); gap: 8px; }
.person-btn { flex-direction: column; justify-content: center; gap: 4px; padding: 8px 4px; text-align: center; min-height: 72px; }
.person-btn canvas { width: 24px; height: 36px; }
.photo-box { text-align: center; margin-top: 14px; }
.photo-box canvas {
  width: min(280px, 76vw);
  height: auto;
  border-radius: 4px;
  box-shadow: 0 10px 30px rgba(0, 0, 0, .5);
  transform: rotate(-1.5deg);
}
a.btn { display: inline-flex; align-items: center; justify-content: center; text-decoration: none; }

/* 집 안 */
.interior { width: 100%; height: auto; aspect-ratio: 160 / 60; border-radius: 12px; border: 2px solid #3a2a20; display: block; image-rendering: pixelated; }
.bubble {
  position: relative;
  margin: 12px 0 0;
  padding: 10px 12px;
  border-radius: 12px;
  background: #f6efe0;
  color: #2a1a10;
  font-size: 14px;
  line-height: 1.5;
}
.obj-grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 8px; }
.obj-card {
  display: flex;
  flex-direction: column;
  gap: 4px;
  padding: 10px;
  border-radius: 12px;
  background: #241a14;
  border: 2px solid #5a3a26;
  min-height: 100px;
}
.obj-card canvas { width: 36px; height: 36px; }
.obj-card small { color: #e8c98a; font-size: 12px; }
.obj-card b { font-size: 14px; overflow-wrap: anywhere; line-height: 1.4; }

/* 우체통, 메뉴 */
.letter {
  background: #f6efe0;
  color: #2a1a10;
  border-radius: 12px;
  padding: 14px;
  line-height: 1.6;
  font-size: 14px;
}
.letter p { margin: 0 0 8px; }
.rules { margin: 0; padding-left: 18px; line-height: 1.7; font-size: 14px; }

/* 시작 화면 */
.start {
  position: absolute;
  inset: 0;
  z-index: 15;
  overflow-y: auto;
  background: radial-gradient(ellipse at 50% 20%, #26306a 0%, #0b1026 70%);
  padding: 20px 16px 32px;
}
.start-inner { max-width: 560px; margin: 0 auto; text-align: center; }
#startArt { width: min(320px, 80vw); height: auto; image-rendering: pixelated; border-radius: 12px; border: 2px solid #2c3770; }
.eyebrow { color: var(--orange); font-size: 13px; margin: 14px 0 4px; }
.start h1 { font-size: 24px; margin: 0 0 6px; }
.start .sub { color: var(--muted); font-size: 14px; margin: 0 0 16px; line-height: 1.5; }
.people { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 10px; }
.people .person-btn { min-height: 84px; font-size: 16px; }
.people .person-btn canvas { width: 32px; height: 48px; }

/* 알림 */
.toast {
  position: absolute;
  left: 50%;
  top: 64px;
  transform: translate(-50%, -8px);
  max-width: calc(100% - 24px);
  width: max-content;
  padding: 10px 14px;
  border-radius: 12px;
  background: #fff3dc;
  color: #2a1a10;
  font-size: 14px;
  line-height: 1.45;
  box-shadow: 0 8px 24px rgba(0, 0, 0, .45);
  opacity: 0;
  pointer-events: none;
  transition: opacity .2s, transform .2s;
  z-index: 40;
  text-align: center;
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }
.toast.bad { background: #ffe1e1; color: #5a1010; }

#fx {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  pointer-events: none;
  z-index: 50;
}

@media (min-width: 900px) {
  .hud { padding: 10px 24px; }
  .hud-row, .progress { max-width: 1100px; margin-left: auto; margin-right: auto; }
  .progress { margin-top: 6px; }
  .demo-strip { padding: 4px 24px; }
  .panel { padding: 20px 20px 40px; }
}

@media (prefers-reduced-motion: reduce) {
  .slot.filled, .balance.bump, .slot.fresh { animation: none; }
}
