:root {
  --paper: #f1e8d3;
  --paper-deep: #e7dabb;
  --surface: #fbf4e4;
  --surface-2: #fffdf7;
  --ink: #4b3a2e;
  --ink-soft: #8c7a63;
  --ink-faint: #b7a789;
  --accent: #b8776e;
  --accent-deep: #a5645b;
  --accent-ink: #fcf3ea;
  --teal: #6e8e93;
  --gold: #c7a15c;
  --line: rgba(75, 58, 46, 0.15);
  --shadow-soft: 0 6px 16px rgba(75, 58, 46, 0.16);
  --shadow-deep: 0 12px 22px rgba(75, 58, 46, 0.26);
  --radius-card: 10px;
  --radius-btn: 8px;
  --font-display: 'Nanum Pen Script', 'Noto Serif KR', cursive;
  --font-body: 'Noto Serif KR', 'Noto Sans KR', serif;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --grain: url("data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' width='140' height='140'><filter id='n'><feTurbulence type='fractalNoise' baseFrequency='0.9' numOctaves='2' stitchTiles='stitch'/><feColorMatrix type='matrix' values='0 0 0 0 0.29 0 0 0 0 0.23 0 0 0 0 0.18 0 0 0 0.06 0'/></filter><rect width='100%25' height='100%25' filter='url(%23n)'/></svg>");
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
[hidden] { display: none !important; }

html, body {
  height: 100%;
  margin: 0;
  overscroll-behavior: none;
  touch-action: manipulation;
}

body {
  font-family: var(--font-body);
  background: var(--paper);
  background-image: var(--grain);
  color: var(--ink);
  overflow: hidden;
}

.icon-sprite { position: absolute; width: 0; height: 0; overflow: hidden; }
svg use { fill: inherit; }

#app {
  position: relative;
  width: 100%;
  height: 100vh;
  height: 100dvh;
}

.screen {
  position: absolute;
  inset: 0;
  display: none;
  flex-direction: column;
  padding-top: var(--safe-top);
  padding-bottom: var(--safe-bottom);
  overflow: hidden;
}
.screen.active { display: flex; }

/* ---------------- HOME ---------------- */
#screen-home {
  align-items: center;
  justify-content: center;
  gap: 44px;
}

.home-header { text-align: center; }
.logo-mark {
  width: 40px; height: 40px;
  margin: 0 auto 8px;
  color: var(--accent);
}
.logo-mark svg { width: 100%; height: 100%; }
.app-title {
  font-family: var(--font-display);
  font-size: 46px;
  line-height: 1;
  margin: 0;
  color: var(--ink);
  font-weight: 400;
}
.app-subtitle {
  font-family: var(--font-body);
  margin: 10px 0 0;
  color: var(--ink-soft);
  font-size: 14px;
  font-style: italic;
}
.beta-badge {
  display: inline-block;
  margin: 12px 0 0;
  padding: 4px 12px;
  border: 1px solid var(--accent);
  border-radius: 999px;
  color: var(--accent-deep);
  font-size: 11px;
  letter-spacing: 0.02em;
  background: color-mix(in srgb, var(--accent) 10%, transparent);
}

.home-menu {
  display: flex;
  flex-direction: column;
  gap: 16px;
  width: min(320px, 84vw);
}

.menu-btn {
  display: flex;
  align-items: center;
  gap: 14px;
  padding: 17px 19px;
  border: 1px solid var(--line);
  border-radius: var(--radius-card);
  background: var(--surface-2);
  box-shadow: var(--shadow-soft);
  cursor: pointer;
  text-align: left;
  transition: transform .15s ease;
}
.menu-btn:nth-child(1) { transform: rotate(-0.6deg); }
.menu-btn:nth-child(2) { transform: rotate(0.5deg); }
.menu-btn:active { transform: scale(0.98); }
.menu-btn.primary {
  background: var(--accent);
  border-color: var(--accent-deep);
  color: var(--accent-ink);
}
.menu-btn-icon {
  width: 24px; height: 24px;
  flex-shrink: 0;
  color: var(--ink-soft);
}
.menu-btn.primary .menu-btn-icon { color: var(--accent-ink); }
.menu-btn-icon svg { width: 100%; height: 100%; }
.menu-btn-text { display: flex; flex-direction: column; gap: 2px; font-family: var(--font-body); }
.menu-btn-text strong { font-size: 16px; font-weight: 600; }
.menu-btn-text small { font-size: 12px; opacity: .8; font-weight: 400; }

.sound-toggle {
  position: absolute;
  top: calc(var(--safe-top) + 16px);
  right: 16px;
  width: 38px;
  height: 38px;
  border-radius: 50%;
  border: 1px solid var(--line);
  background: var(--surface-2);
  box-shadow: var(--shadow-soft);
  z-index: 2;
  color: var(--ink-soft);
}
.sound-toggle svg, .sound-toggle-inline svg { width: 17px; height: 17px; }
.sound-toggle .icon-off, .sound-toggle-inline .icon-off { display: none; }
.sound-toggle.muted .icon-on, .sound-toggle-inline.muted .icon-on { display: none; }
.sound-toggle.muted .icon-off, .sound-toggle-inline.muted .icon-off { display: inline; }

.settings-btn {
  position: absolute;
  top: calc(var(--safe-top) + 16px);
  right: 16px;
  width: 38px;
  height: 38px;
  border-radius: 50%;
  border: 1px solid var(--line);
  background: var(--surface-2);
  box-shadow: var(--shadow-soft);
  z-index: 2;
  color: var(--ink-soft);
}
.settings-btn svg { width: 18px; height: 18px; }

.continue-link {
  border: none;
  background: none;
  color: var(--ink-soft);
  font-family: var(--font-body);
  font-style: italic;
  font-size: 13px;
  text-decoration: underline;
  text-underline-offset: 3px;
  padding: 6px 10px;
  margin-top: 2px;
}

.settings-card {
  background: var(--surface-2);
  border-radius: 6px;
  padding: 26px 22px 22px;
  width: min(320px, 100%);
  box-shadow: var(--shadow-deep);
}
.settings-card h3 {
  font-family: var(--font-display);
  font-weight: 400;
  font-size: 26px;
  margin: 0 0 16px;
  color: var(--ink);
  text-align: center;
}
.settings-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 12px 0;
  border-bottom: 1px solid var(--line);
  font-family: var(--font-body);
  font-size: 13.5px;
  color: var(--ink);
}
.settings-row input[type="range"] {
  flex: 1;
  max-width: 150px;
  accent-color: var(--accent);
}
.settings-row-switch input[type="checkbox"] {
  width: 18px; height: 18px;
  accent-color: var(--accent);
}
.settings-note {
  font-family: var(--font-body);
  font-style: italic;
  font-size: 11.5px;
  color: var(--ink-faint);
  text-align: center;
  margin: 14px 0 18px;
}
.settings-row-link {
  width: 100%;
  background: none;
  border: none;
  border-bottom: 1px solid var(--line);
  cursor: pointer;
  color: var(--ink);
}
.settings-row-link span:first-child {
  display: flex;
  align-items: center;
  gap: 8px;
}
.settings-row-icon { width: 16px; height: 16px; color: var(--ink-soft); }
.trash-count-badge {
  min-width: 18px;
  height: 18px;
  padding: 0 5px;
  border-radius: 999px;
  background: var(--accent);
  color: var(--accent-ink);
  font-size: 10.5px;
  font-weight: 700;
  display: flex;
  align-items: center;
  justify-content: center;
}

/* ---------------- TRASH ---------------- */
.trash-card { display: flex; flex-direction: column; max-height: 82vh; }
.trash-note { margin: 0 0 14px; }
.trash-list {
  display: flex;
  flex-direction: column;
  gap: 10px;
  overflow-y: auto;
  margin-bottom: 14px;
}
.trash-item {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 8px;
  border: 1px solid var(--line);
  border-radius: var(--radius-btn);
  background: var(--surface);
}
.trash-item-thumb {
  width: 44px;
  height: 44px;
  object-fit: cover;
  border-radius: 4px;
  flex-shrink: 0;
}
.trash-item-info {
  flex: 1;
  min-width: 0;
  display: flex;
  flex-direction: column;
  gap: 2px;
}
.trash-item-title {
  font-family: var(--font-body);
  font-weight: 600;
  font-size: 12.5px;
  color: var(--ink);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.trash-item-meta {
  font-family: var(--font-body);
  font-style: italic;
  font-size: 10.5px;
  color: var(--ink-soft);
}
.trash-item-actions {
  display: flex;
  align-items: center;
  gap: 6px;
  flex-shrink: 0;
}
.trash-restore-btn {
  border: 1px solid var(--accent-deep);
  background: var(--accent);
  color: var(--accent-ink);
  border-radius: 999px;
  padding: 6px 12px;
  font-family: var(--font-body);
  font-size: 11.5px;
  font-weight: 600;
}
.trash-delete-btn {
  width: 26px;
  height: 26px;
  border-radius: 50%;
  border: 1px solid var(--line);
  background: var(--surface-2);
  color: var(--ink-soft);
  font-size: 11px;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
}
.trash-empty {
  text-align: center;
  color: var(--ink-faint);
  font-family: var(--font-body);
  font-style: italic;
  font-size: 13px;
  margin: 10px 0 18px;
}
.trash-footer { display: flex; flex-direction: column; gap: 8px; }
.trash-empty-btn {
  background: none;
  border: none;
  color: var(--accent-deep);
  font-family: var(--font-body);
  font-size: 12.5px;
  text-decoration: underline;
  text-underline-offset: 3px;
  padding: 4px;
}

/* ---------------- TOPBAR (shared) ---------------- */
.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px 12px;
  flex-shrink: 0;
}
.topbar h2 {
  margin: 0;
  font-family: var(--font-body);
  font-size: 17px;
  font-weight: 600;
}
.icon-btn {
  width: 36px;
  height: 36px;
  border-radius: 50%;
  border: 1px solid var(--line);
  background: var(--surface-2);
  color: var(--ink-soft);
  font-size: 15px;
  display: flex;
  align-items: center;
  justify-content: center;
}
.icon-btn.spacer { background: transparent; border-color: transparent; }
.sound-toggle-inline { color: var(--ink-soft); }

/* ---------------- SETUP ---------------- */
.setup-body {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 22px;
  padding: 8px 20px 24px;
  overflow-y: auto;
}

.photo-drop {
  width: 100%;
  max-width: 320px;
  aspect-ratio: 4 / 3;
  border-radius: 2px;
  background: var(--surface-2);
  box-shadow: var(--shadow-deep);
  display: flex;
  align-items: center;
  justify-content: center;
  overflow: hidden;
  padding: 10px 10px 26px;
  cursor: pointer;
  flex-shrink: 0;
  transform: rotate(-0.8deg);
}
.photo-drop.has-image {
  aspect-ratio: auto;
  max-height: 52vh;
}
.photo-drop img {
  max-width: 100%;
  max-height: 100%;
  width: auto;
  height: auto;
  display: block;
  object-fit: contain;
}
.photo-drop-empty {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 10px;
  color: var(--ink-faint);
  font-size: 13.5px;
  font-family: var(--font-body);
  width: 100%; height: 100%;
  justify-content: center;
  border: 1.5px dashed var(--line);
}
.photo-drop-icon { width: 26px; height: 26px; }

.title-field { width: 100%; max-width: 320px; }
.title-input {
  width: 100%;
  padding: 12px 14px;
  border: 1px solid var(--line);
  border-radius: var(--radius-btn);
  background: var(--surface-2);
  box-shadow: var(--shadow-soft);
  font-family: var(--font-body);
  font-style: italic;
  font-size: 15px;
  color: var(--ink);
}
.title-input::placeholder { color: var(--ink-faint); }
.title-input:focus { outline: 1.5px solid var(--accent); outline-offset: 1px; }

.piece-select { width: 100%; max-width: 380px; }
.piece-select-label {
  text-align: center;
  color: var(--ink-soft);
  font-size: 13px;
  font-style: italic;
  margin: 0 0 12px;
}
.piece-options {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 10px;
}
.piece-opt {
  border: 1px solid var(--line);
  background: var(--surface-2);
  border-radius: var(--radius-btn);
  padding: 12px 6px;
  font-family: var(--font-body);
  font-size: 18px;
  font-weight: 600;
  color: var(--ink);
  box-shadow: var(--shadow-soft);
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 4px;
}
.piece-opt small { font-size: 11px; font-weight: 400; color: var(--ink-soft); font-style: italic; }
.piece-opt.selected {
  border-color: var(--accent);
  background: #f6e6de;
  transform: translateY(-2px);
}

.cta-btn {
  width: 100%;
  max-width: 320px;
  padding: 15px;
  border: 1px solid var(--accent-deep);
  border-radius: var(--radius-btn);
  background: var(--accent);
  color: var(--accent-ink);
  font-family: var(--font-body);
  font-size: 15.5px;
  font-weight: 600;
  box-shadow: var(--shadow-soft);
  flex-shrink: 0;
}
.cta-btn:disabled { opacity: 0.4; }
.cta-btn.ghost {
  background: var(--surface-2);
  color: var(--ink);
  border-color: var(--line);
}

/* ---------------- PLAY ---------------- */
.play-topbar { padding: 10px 12px; }
.play-progress {
  flex: 1;
  display: flex;
  align-items: center;
  gap: 10px;
  margin: 0 10px;
}
.play-progress-bar {
  flex: 1;
  height: 7px;
  background: rgba(75,58,46,0.12);
  border-radius: 6px;
  overflow: hidden;
}
.play-progress-fill {
  height: 100%;
  width: 0%;
  background: linear-gradient(90deg, var(--teal), var(--gold));
  transition: width .3s ease;
}
#play-progress-text {
  font-family: var(--font-body);
  font-size: 12px;
  color: var(--ink-soft);
  white-space: nowrap;
  font-variant-numeric: tabular-nums;
}

.play-timer {
  font-family: var(--font-body);
  font-size: 13px;
  font-weight: 600;
  color: var(--ink-soft);
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
  margin-right: 8px;
}

.play-stage {
  flex: 1;
  position: relative;
  overflow: hidden;
  min-height: 0;
  touch-action: none;
}

.world {
  position: absolute;
  top: 0;
  left: 0;
  transform-origin: 0 0;
}

.fit-view-btn {
  position: absolute;
  right: 12px;
  bottom: 12px;
  z-index: 500;
  width: 40px;
  height: 40px;
  border-radius: 50%;
  border: 1px solid var(--line);
  background: var(--surface-2);
  color: var(--ink-soft);
  box-shadow: var(--shadow-soft);
  display: flex;
  align-items: center;
  justify-content: center;
}
.fit-view-btn svg { width: 19px; height: 19px; }
#board-shadow {
  position: absolute;
  top: 0; left: 0;
  opacity: 0.3;
  pointer-events: none;
  border-radius: 2px;
}
.board-frame {
  position: absolute;
  border-radius: 2px;
  box-shadow: inset 0 0 0 1.5px rgba(75,58,46,0.3);
  outline: 1.5px dashed rgba(75,58,46,0.25);
  outline-offset: 6px;
  pointer-events: none;
}

.piece {
  position: absolute;
  cursor: grab;
  touch-action: none;
  user-select: none;
  -webkit-user-drag: none;
  will-change: transform, left, top;
  filter: drop-shadow(0 3px 6px rgba(75,58,46,0.32));
}
.piece.dragging {
  cursor: grabbing;
  z-index: 999;
  filter: drop-shadow(0 10px 16px rgba(75,58,46,0.45));
}
.piece.locked {
  filter: drop-shadow(0 1px 2px rgba(75,58,46,0.18));
  cursor: default;
}
.piece.snapping-back {
  transition: left 200ms cubic-bezier(.34,1.56,.64,1), top 200ms cubic-bezier(.34,1.56,.64,1), transform 200ms cubic-bezier(.34,1.56,.64,1);
}

.play-loading {
  position: absolute;
  inset: 0;
  z-index: 700;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 14px;
  background: var(--paper);
  background-image: var(--grain);
}
.play-loading-spinner {
  width: 30px;
  height: 30px;
  border-radius: 50%;
  border: 3px solid var(--line);
  border-top-color: var(--accent);
  animation: play-loading-spin 0.8s linear infinite;
}
@keyframes play-loading-spin { to { transform: rotate(360deg); } }
#play-loading-text {
  font-family: var(--font-body);
  font-size: 13px;
  color: var(--ink-soft);
  margin: 0;
}

.play-hint {
  text-align: center;
  font-family: var(--font-body);
  font-style: italic;
  font-size: 12px;
  color: var(--ink-soft);
  margin: 0;
  padding: 8px 0 calc(10px + var(--safe-bottom));
  flex-shrink: 0;
}

/* ---------------- COMPLETE OVERLAY ---------------- */
.overlay {
  position: fixed;
  inset: 0;
  background: rgba(75, 58, 46, 0.55);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 1000;
  padding: 24px;
}
.complete-card {
  background: var(--surface-2);
  border-radius: 4px;
  padding: 30px 24px 26px;
  width: min(340px, 100%);
  text-align: center;
  box-shadow: var(--shadow-deep);
}
.complete-sparkle { font-size: 26px; }
.complete-card h2 {
  font-family: var(--font-display);
  font-weight: 400;
  font-size: 32px;
  margin: 2px 0 6px;
  color: var(--ink);
}
.complete-card p { color: var(--ink-soft); font-size: 13px; font-style: italic; margin: 0 0 18px; }
.complete-card img {
  width: 100%;
  border-radius: 2px;
  box-shadow: var(--shadow-deep);
  margin-bottom: 20px;
}
.complete-actions { display: flex; gap: 10px; }
.complete-actions .cta-btn { width: auto; max-width: none; flex: 1 1 0; min-width: 0; padding: 13px 10px; font-size: 14px; }

/* ---------------- GALLERY / ROOM ---------------- */
#screen-gallery { overflow: hidden; }
.room {
  flex: 1;
  overflow-y: auto;
  position: relative;
}
.room-wall {
  min-height: 100%;
  position: relative;
  background: var(--paper);
  background-image:
    var(--grain),
    radial-gradient(ellipse at 50% -10%, rgba(199,161,92,0.18), transparent 55%);
  padding: 44px 22px 90px;
}

.frames {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 34px 20px;
  position: relative;
  z-index: 1;
}
.frame {
  background: var(--surface-2);
  padding: 8px 8px 20px;
  border-radius: 2px;
  box-shadow: var(--shadow-deep);
  transform: rotate(var(--tilt, 0deg));
  position: relative;
}
.frame::before {
  content: "";
  position: absolute;
  left: 50%;
  top: -22px;
  width: 1px;
  height: 22px;
  background: linear-gradient(180deg, transparent, var(--ink-faint) 70%);
  transform: translateX(-50%);
}
.frame::after {
  content: "";
  position: absolute;
  left: 50%;
  top: -25px;
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: var(--gold);
  box-shadow: 0 1px 2px rgba(75,58,46,0.4);
  transform: translateX(-50%);
}
.frame img { width: 100%; display: block; border-radius: 1px; }
.frame-delete-btn {
  position: absolute;
  top: 2px;
  right: 2px;
  width: 24px;
  height: 24px;
  border-radius: 50%;
  border: 1px solid var(--line);
  background: rgba(251, 244, 228, 0.92);
  color: var(--ink-soft);
  font-size: 11px;
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 2;
  box-shadow: var(--shadow-soft);
}
.frame .frame-caption-title {
  display: block;
  text-align: center;
  font-family: var(--font-body);
  font-weight: 600;
  font-size: 12px;
  color: var(--ink);
  margin-top: 9px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.frame .frame-caption {
  display: block;
  text-align: center;
  font-family: var(--font-body);
  font-style: italic;
  font-size: 10px;
  color: var(--ink-soft);
  margin-top: 2px;
}
.room-empty {
  position: absolute;
  top: 50%; left: 50%;
  transform: translate(-50%, -50%);
  text-align: center;
  color: var(--ink-soft);
  font-family: var(--font-body);
  font-style: italic;
  font-size: 14px;
  line-height: 1.7;
  z-index: 2;
  width: 80%;
}

.lightbox {
  position: fixed;
  inset: 0;
  background: rgba(30, 22, 16, 0.86);
  z-index: 1100;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px;
}
.lightbox img { max-width: 100%; max-height: 100%; border-radius: 3px; box-shadow: var(--shadow-deep); }
.lightbox-close {
  position: absolute;
  top: calc(var(--safe-top) + 16px);
  right: 16px;
  background: var(--surface-2);
}
