/* ========== Theme ========== */
:root,
:root[data-theme="light"] {
  --bg: #f4f5f7;
  --surface: #ffffff;
  --surface-2: #eff0f3;
  --surface-3: #e3e5ea;
  --tile: #f1f2f5;
  --border: rgba(17, 17, 20, 0.1);
  --border-strong: rgba(17, 17, 20, 0.2);
  --text: #111114;
  --text-2: #3f3f46;
  --text-3: #71717a;
  --accent: #ff4100;
  --accent-hover: #e63a00;
  --accent-soft: rgba(255, 65, 0, 0.09);
  --chip-on-bg: #111114;
  --chip-on-text: #ffffff;
  --seg-on: #ffffff;
  --stage-bg: #1d1e23;
  --overlay: rgba(17, 17, 20, 0.48);
  --shadow: 0 1px 2px rgba(17, 17, 20, 0.05), 0 6px 20px rgba(17, 17, 20, 0.06);
  --shadow-pop: 0 18px 50px rgba(17, 17, 20, 0.22);
  --gold: #e69500;
}
:root[data-theme="dark"] {
  --bg: #0c0c0e;
  --surface: #151518;
  --surface-2: #1e1e23;
  --surface-3: #2a2a31;
  --tile: #1e1e23;
  --border: rgba(255, 255, 255, 0.09);
  --border-strong: rgba(255, 255, 255, 0.2);
  --text: #fafafa;
  --text-2: #d0d0d6;
  --text-3: #9a9aa5;
  --accent: #ff4100;
  --accent-hover: #ff561a;
  --accent-soft: rgba(255, 65, 0, 0.16);
  --chip-on-bg: #fafafa;
  --chip-on-text: #111114;
  --seg-on: #34343c;
  --stage-bg: #000000;
  --overlay: rgba(0, 0, 0, 0.62);
  --shadow: none;
  --shadow-pop: 0 18px 50px rgba(0, 0, 0, 0.6);
  --gold: #f5b73b;
}

/* ========== Base ========== */
* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 500 15px/1.45 Pretendard, -apple-system, BlinkMacSystemFont, "Apple SD Gothic Neo", "Malgun Gothic", sans-serif;
  letter-spacing: -0.01em;
  -webkit-font-smoothing: antialiased;
  display: flex;
  flex-direction: column;
  height: 100dvh;
  overflow: hidden;
  overscroll-behavior: none;
  touch-action: manipulation;
  -webkit-text-size-adjust: 100%;
}
button { font: inherit; color: inherit; cursor: pointer; -webkit-tap-highlight-color: transparent; -webkit-user-select: none; user-select: none; }
h2 { margin: 0; font-size: 17px; font-weight: 700; letter-spacing: -0.02em; }
p { margin: 0; }
.hid { display: none !important; }
.defs { position: absolute; width: 0; height: 0; overflow: hidden; }
.ico { width: 20px; height: 20px; flex: none; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }
.ico.big { width: 40px; height: 40px; stroke-width: 1.6; }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
::-webkit-scrollbar { width: 8px; height: 8px; }
::-webkit-scrollbar-thumb { background: var(--surface-3); border-radius: 8px; }

:root[data-theme="light"] .when-dark, :root[data-theme="dark"] .when-light { display: none; }

/* ========== Top bar ========== */
.top {
  flex: none;
  height: 56px;
  padding: 0 16px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}
.brand { display: flex; align-items: center; gap: 10px; text-decoration: none; color: inherit; }
.logo { width: 32px; height: 32px; display: block; }
.brand-name { font-size: 20px; font-weight: 800; letter-spacing: -0.03em; }
.top-right { display: flex; align-items: center; gap: 8px; }

/* ========== Buttons ========== */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 7px;
  height: 44px;
  padding: 0 16px;
  border-radius: 12px;
  border: 1px solid var(--border-strong);
  background: var(--surface);
  font-size: 15.5px;
  font-weight: 700;
  white-space: nowrap;
  transition: background 0.12s, border-color 0.12s, transform 0.06s;
}
.btn:hover { background: var(--surface-2); }
.btn:active { transform: scale(0.98); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn.primary:hover { background: var(--accent-hover); border-color: var(--accent-hover); }
.btn.sm { height: 34px; padding: 0 11px; border-radius: 10px; font-size: 14px; gap: 5px; }
.btn.sm .ico { width: 16px; height: 16px; }
.btn.wide { width: 100%; }
.btn[disabled] { opacity: 0.45; pointer-events: none; }
.icon-btn {
  width: 38px; height: 38px; flex: none;
  display: inline-flex; align-items: center; justify-content: center;
  border: 0; border-radius: 10px; background: transparent; color: var(--text-2);
}
.icon-btn:hover { background: var(--surface-2); }
.icon-btn.solid { background: var(--text); color: var(--surface); border-radius: 50%; }
.icon-btn.solid:hover { background: var(--text-2); }
.icon-btn.solid .ico { width: 18px; height: 18px; }
.text-btn { border: 0; background: none; padding: 6px 8px; border-radius: 8px; font-size: 14px; font-weight: 600; color: var(--text-3); }
.text-btn:hover { background: var(--surface-2); color: var(--text); }
.badge {
  min-width: 22px; height: 22px; padding: 0 6px; border-radius: 11px;
  display: inline-flex; align-items: center; justify-content: center;
  background: var(--surface-3); font-size: 13px; font-weight: 700; font-variant-numeric: tabular-nums;
}
.badge.has { background: var(--accent); color: #fff; }
.tray-open { height: 38px; padding: 0 12px; font-size: 15px; }

/* ========== Layout ========== */
.work {
  flex: 1;
  min-height: 0;
  display: grid;
  grid-template-columns: 372px minmax(0, 1fr) 316px;
  gap: 14px;
  padding: 14px;
}
.panel {
  min-height: 0;
  display: flex;
  flex-direction: column;
  background: var(--surface);
  border-radius: 16px;
  box-shadow: var(--shadow);
  overflow: hidden;
}
:root[data-theme="dark"] .panel { border: 1px solid var(--border); }
.panel-head { flex: none; display: flex; align-items: center; gap: 8px; padding: 14px 16px 12px; }
.panel-head .count { color: var(--text-3); font-size: 14px; font-weight: 600; font-variant-numeric: tabular-nums; margin-right: auto; }
.sheet-close { display: none; }
.grabber { display: none; }

/* ========== Picker ========== */
.seg { display: flex; padding: 3px; border-radius: 11px; background: var(--surface-2); }
.seg button {
  flex: 1; height: 34px; border: 0; border-radius: 8px; background: transparent;
  font-size: 14.5px; font-weight: 600; color: var(--text-3); white-space: nowrap;
}
.seg button.on { background: var(--seg-on); color: var(--text); box-shadow: 0 1px 2px rgba(17, 17, 20, 0.12); }
.picker > .seg { flex: none; margin: 0 16px 10px; }
.chips {
  flex: none; display: flex; gap: 6px; padding: 0 34px 10px 16px; overflow-x: auto; scrollbar-width: none; overscroll-behavior-x: contain;
  -webkit-mask-image: linear-gradient(90deg, #000 calc(100% - 34px), transparent);
  mask-image: linear-gradient(90deg, #000 calc(100% - 34px), transparent);
}
.chips.more-left {
  -webkit-mask-image: linear-gradient(90deg, transparent, #000 26px, #000 calc(100% - 34px), transparent);
  mask-image: linear-gradient(90deg, transparent, #000 26px, #000 calc(100% - 34px), transparent);
}
.chips::-webkit-scrollbar { display: none; }
.chip {
  flex: none; height: 32px; padding: 0 12px; border-radius: 16px;
  border: 1px solid var(--border-strong); background: transparent;
  font-size: 14px; font-weight: 600; color: var(--text-2); white-space: nowrap;
}
.chip:hover { background: var(--surface-2); }
.chip.on { background: var(--chip-on-bg); border-color: var(--chip-on-bg); color: var(--chip-on-text); }
.grid-scroll { flex: 1; min-height: 0; overflow-y: auto; overscroll-behavior: contain; padding: 0 12px 16px; }
.grp {
  position: sticky; top: 0; z-index: 1; margin: 0; padding: 12px 4px 8px;
  display: flex; align-items: baseline; gap: 6px; background: var(--surface);
  font-size: 14.5px; font-weight: 700; letter-spacing: -0.01em;
}
.grp-box:first-child .grp { padding-top: 4px; }
.grp-box + .grp-box { margin-top: 6px; }
.grp span { font-size: 13.5px; font-weight: 600; color: var(--text-3); font-variant-numeric: tabular-nums; }
.st.sk { aspect-ratio: 1 / 1.08; pointer-events: none; animation: pulse 1.2s ease-in-out infinite; }
@keyframes pulse { 50% { opacity: 0.45; } }
.grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(98px, 1fr)); gap: 8px; }
.st {
  display: flex; flex-direction: column; align-items: center; gap: 4px;
  padding: 8px 6px 7px; border-radius: 12px; border: 2px solid transparent; background: var(--tile);
}
.st:hover { border-color: var(--border-strong); }
.st.on { border-color: var(--accent); background: var(--accent-soft); }
.st img { width: 100%; height: auto; aspect-ratio: 9 / 7; object-fit: contain; display: block; }
.price b { font-weight: inherit; }
.price { display: inline-flex; align-items: center; gap: 3px; font-size: 13.5px; font-weight: 700; color: var(--text-2); font-variant-numeric: tabular-nums; }
.price .ico { width: 14px; height: 14px; color: var(--gold); }
.state { display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 12px; padding: 48px 16px; color: var(--text-3); font-size: 15px; font-weight: 600; text-align: center; }

/* ========== Stage ========== */
.center { min-width: 0; min-height: 0; display: flex; flex-direction: column; gap: 10px; }
.stage-wrap { flex: 1; min-height: 0; display: flex; align-items: center; justify-content: center; }
.stage {
  position: relative; flex: none; width: 270px; height: 480px;
  border-radius: 18px; overflow: hidden; background: var(--stage-bg);
  box-shadow: var(--shadow); touch-action: none; user-select: none; -webkit-user-select: none;
}
.stage.has-bg { cursor: grab; }
.stage.has-bg.dragging { cursor: grabbing; }
.stage canvas { position: absolute; inset: 0; width: 100%; height: 100%; display: block; pointer-events: none; }
.stage-add {
  position: absolute; left: 50%; top: 50%; transform: translate(-50%, -50%);
  display: inline-flex; align-items: center; gap: 8px; height: 48px; padding: 0 20px;
  border: 0; border-radius: 24px; background: #ffffff; color: #111114;
  font-size: 16px; font-weight: 700; white-space: nowrap; box-shadow: 0 6px 20px rgba(0, 0, 0, 0.35);
}
.stage.has-bg .stage-add { display: none; }
.stage-busy { position: absolute; inset: 0; display: flex; align-items: center; justify-content: center; background: rgba(0, 0, 0, 0.28); }
.spinner { width: 34px; height: 34px; border-radius: 50%; border: 3px solid rgba(255, 255, 255, 0.3); border-top-color: #fff; animation: spin 0.8s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

/* ========== Timeline ========== */
.timeline {
  flex: none; display: flex; align-items: center; gap: 10px;
  width: 100%; max-width: 620px; margin: 0 auto; padding: 8px 12px 8px 8px;
  background: var(--surface); border-radius: 14px; box-shadow: var(--shadow);
}
:root[data-theme="dark"] .timeline { border: 1px solid var(--border); }
.timeline.off { opacity: 0.5; pointer-events: none; }
.film { position: relative; flex: 1; min-width: 0; }
.film-track { position: relative; height: 56px; border-radius: 9px; background: var(--surface-2); cursor: ew-resize; touch-action: none; }
.film-track canvas { width: 100%; height: 100%; display: block; border-radius: 9px; opacity: 0; transition: opacity 0.2s; }
.film.ready .film-track canvas { opacity: 1; }
.film:not(.ready) .film-track { animation: pulse 1.2s ease-in-out infinite; }
.playhead {
  position: absolute; top: -3px; bottom: -3px; left: 0; width: 4px; margin-left: -2px; border-radius: 2px;
  background: #ffffff; box-shadow: 0 0 0 1.5px var(--accent), 0 1px 6px rgba(0, 0, 0, 0.5); pointer-events: none;
}
.time { flex: none; min-width: 88px; text-align: right; font-size: 14px; font-weight: 600; color: var(--text-2); font-variant-numeric: tabular-nums; }
#btnPlay .when-playing, .playing #btnPlay .when-paused { display: none; }
.playing #btnPlay .when-playing { display: block; }
.actions { flex: none; display: flex; gap: 8px; width: 100%; max-width: 620px; margin: 0 auto; }
.actions .btn { flex: 1; min-width: 0; padding: 0 8px; }
.pick-open { display: none; }

/* ========== Tray ========== */
.tray-scroll { flex: 1; min-height: 0; overflow-y: auto; overscroll-behavior: contain; padding: 2px 12px 12px; }
.tray-grid { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 8px; }
.shot {
  position: relative; aspect-ratio: 9 / 16; border-radius: 10px; overflow: hidden; background: var(--surface-2);
  cursor: grab; touch-action: pan-y; -webkit-touch-callout: none; -webkit-user-select: none; user-select: none;
}
.shot img { width: 100%; height: 100%; object-fit: cover; display: block; pointer-events: none; -webkit-user-drag: none; }
.shot.lift { opacity: 0.3; }
.shot.drop { outline: 3px solid var(--accent); outline-offset: -3px; }
.shot.new { animation: pop 0.35s ease; }
@keyframes pop { from { transform: scale(0.82); opacity: 0.3; } }
.ghost {
  position: fixed; z-index: 70; left: 0; top: 0; width: 86px; aspect-ratio: 9 / 16; border-radius: 10px; object-fit: cover;
  pointer-events: none; box-shadow: var(--shadow-pop);
}
.badge.bump { animation: bump 0.32s ease; }
@keyframes bump { 40% { transform: scale(1.35); } }
.shot .num {
  position: absolute; left: 5px; top: 5px; min-width: 20px; height: 20px; padding: 0 5px; border-radius: 10px;
  display: flex; align-items: center; justify-content: center;
  background: rgba(0, 0, 0, 0.62); color: #fff; font-size: 13px; font-weight: 700; font-variant-numeric: tabular-nums;
}
.shot button {
  position: absolute; width: 28px; height: 28px; border: 0; border-radius: 50%;
  display: flex; align-items: center; justify-content: center; background: rgba(0, 0, 0, 0.62); color: #fff;
}
.shot button:hover { background: rgba(0, 0, 0, 0.85); }
.shot button .ico { width: 15px; height: 15px; stroke-width: 2.4; }
.shot .del { right: 5px; top: 5px; }
.shot .dl { right: 5px; bottom: 5px; }
.tray-foot { flex: none; padding: 12px; border-top: 1px solid var(--border); }

/* ========== Collage ========== */
.backdrop { position: fixed; inset: 0; z-index: 25; background: var(--overlay); }
.modal { position: fixed; inset: 0; z-index: 40; display: flex; align-items: center; justify-content: center; padding: 12px; background: var(--overlay); }
.modal-card {
  width: min(940px, 100%); max-height: 100%; display: flex; flex-direction: column;
  background: var(--surface); border-radius: 18px; box-shadow: var(--shadow-pop); overflow: hidden;
}
.collage-body { flex: 1; min-height: 0; display: grid; grid-template-columns: minmax(0, 1fr) 280px; gap: 16px; padding: 0 16px 16px; }
.collage-preview { min-height: 0; display: flex; align-items: center; justify-content: center; padding: 12px; border-radius: 12px; background: var(--surface-2); overflow: hidden; }
.collage-preview canvas { max-width: 100%; max-height: 68vh; max-height: min(68dvh, 720px); display: block; border-radius: 4px; }
.collage-opts { display: flex; flex-direction: column; gap: 14px; }
.opt { display: flex; flex-direction: column; gap: 6px; }
.opt-name { font-size: 14px; font-weight: 600; color: var(--text-2); }
.collage-opts .btn { margin-top: auto; }

.shot-card { width: min(460px, 100%); }
.shot-view { min-height: 0; overflow: auto; display: flex; justify-content: center; padding: 0 16px 16px; }
.shot-view img { display: block; max-width: 100%; max-height: 74vh; max-height: 74dvh; border-radius: 10px; -webkit-touch-callout: default; }

/* ========== Toast ========== */
.toast {
  position: fixed; left: 50%; bottom: 176px; transform: translateX(-50%); z-index: 60;
  max-width: calc(100vw - 32px); padding: 11px 16px; border-radius: 12px;
  background: #111114; color: #fff; font-size: 15px; font-weight: 600; text-align: center; box-shadow: var(--shadow-pop);
}
:root[data-theme="dark"] .toast { background: #fafafa; color: #111114; }

/* ========== Medium: the tray becomes a side drawer ========== */
@media (min-width: 1180px) {
  .tray-open { display: none; }
}
@media (max-width: 1179px) {
  .work { grid-template-columns: 340px minmax(0, 1fr); }
  .tray {
    position: fixed; z-index: 30; top: 0; right: 0; bottom: 0; width: min(360px, 100%);
    border-radius: 0; box-shadow: var(--shadow-pop); transform: translateX(105%); transition: transform 0.22s ease;
  }
  .tray.open { transform: none; }
  .tray .sheet-close { display: inline-flex; }
}

/* ========== Phone: picker and tray slide up from the bottom ========== */
@media (max-width: 759px) {
  body { font-size: 16px; }
  .top {
    height: calc(52px + env(safe-area-inset-top)); padding: env(safe-area-inset-top) max(12px, env(safe-area-inset-right)) 0 max(12px, env(safe-area-inset-left));
  }
  .work {
    display: flex; flex-direction: column; gap: 0;
    padding: 10px max(12px, env(safe-area-inset-right)) calc(10px + env(safe-area-inset-bottom)) max(12px, env(safe-area-inset-left));
  }
  .center { flex: 1; gap: 8px; }
  .pick-open { display: inline-flex; }
  .picker, .tray {
    position: fixed; z-index: 30; left: 0; right: 0; top: auto; bottom: 0; width: auto; height: 82vh; height: 82dvh;
    border-radius: 20px 20px 0 0; box-shadow: var(--shadow-pop); transform: translateY(105%); transition: transform 0.24s ease;
  }
  .picker.open, .tray.open { transform: none; }
  .picker .sheet-close { display: inline-flex; }
  .grabber { display: block; flex: none; height: 20px; touch-action: none; }
  .grabber::before { content: ""; display: block; width: 40px; height: 4px; margin: 8px auto 0; border-radius: 2px; background: var(--surface-3); }
  .tray .panel-head { padding-top: 4px; touch-action: none; }
  .picker .panel-head { position: absolute; right: 8px; top: 18px; padding: 0; z-index: 2; }
  .picker .panel-head h2, .picker .panel-head .count { display: none; }
  .picker > .seg { margin: 0 58px 10px 14px; }

  /* While choosing a sticker the sheet takes the lower half and the stage stays in view above it. */
  .picker { height: 54vh; height: 54dvh; }
  body.picking .work { padding-bottom: calc(54vh + 8px); padding-bottom: calc(54dvh + 8px); }
  body.picking .timeline, body.picking .actions, body.picking .top, body.picking .stage-add { display: none; }
  body.picking .work { padding-top: calc(10px + env(safe-area-inset-top)); }
  .backdrop.clear { background: transparent; }

  .grid { grid-template-columns: repeat(4, minmax(0, 1fr)); gap: 6px; }
  .st { padding: 6px 4px 6px; border-radius: 11px; }
  .grid-scroll { padding: 0 10px calc(16px + env(safe-area-inset-bottom)); }
  .chip { height: 36px; padding: 0 14px; font-size: 15.5px; border-radius: 18px; }
  .seg button { height: 38px; font-size: 15.5px; }
  .grp { font-size: 15.5px; }
  .grp span { font-size: 14.5px; }
  .price { font-size: 14.5px; }
  .timeline { padding: 7px 9px 7px 7px; gap: 8px; }
  .film-track { height: 52px; }
  .time { display: none; }
  .icon-btn.solid { width: 40px; height: 40px; }
  .actions .btn { height: 50px; font-size: 16px; gap: 5px; }
  .actions .btn .ico { width: 18px; height: 18px; }
  .shot button { width: 34px; height: 34px; }
  .tray-foot { padding-bottom: calc(12px + env(safe-area-inset-bottom)); }
  .toast { bottom: calc(158px + env(safe-area-inset-bottom)); }
  .modal { padding: 0; align-items: flex-end; }
  .modal-card { border-radius: 20px 20px 0 0; max-height: 94vh; max-height: 94dvh; }
  .shot-card { width: 100%; }
  .shot-view { padding-bottom: calc(16px + env(safe-area-inset-bottom)); }
  .collage-body { display: flex; flex-direction: column; overflow-y: auto; gap: 12px; padding: 0 12px calc(12px + env(safe-area-inset-bottom)); }
  .collage-preview { flex: none; }
  .collage-preview canvas { max-height: 40vh; max-height: 40dvh; }
  .opt { flex-direction: row; align-items: center; gap: 10px; }
  .opt-name { flex: none; width: 64px; font-size: 15.5px; }
  .opt .seg { flex: 1; }
}
