:root {
  --bg: #ece7dc;
  --panel: #fffdf8;
  --ink: #1c1915;
  --muted: #6d6458;
  --line: #d9d0c2;
  --accent: #d4552a;
  --phone: #1a1a1a;
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  font-family: "IBM Plex Sans KR", sans-serif;
  color: var(--ink);
  background:
    radial-gradient(circle at 12% 0%, #ffe2b8 0%, transparent 42%),
    radial-gradient(circle at 88% 8%, #cfeee4 0%, transparent 36%),
    var(--bg);
  min-height: 100vh;
}

.top {
  display: flex;
  justify-content: space-between;
  gap: 16px;
  padding: 20px 24px 12px;
}

.top h1 {
  margin: 0 0 4px;
  font-size: 28px;
}

.top p {
  margin: 0;
  color: var(--muted);
  font-size: 14px;
}

.top-actions {
  display: flex;
  gap: 8px;
}

button {
  font: inherit;
  border: 1px solid var(--ink);
  background: #fff;
  color: var(--ink);
  padding: 8px 12px;
  cursor: pointer;
}

button:disabled {
  opacity: 0.65;
  cursor: wait;
}

button.primary {
  background: var(--accent);
  color: #fff;
  border-color: #9e3418;
}

.layout {
  display: grid;
  grid-template-columns: minmax(360px, 540px) 1fr;
  gap: 16px;
  padding: 0 24px 24px;
}

.panel {
  background: var(--panel);
  border: 1px solid var(--line);
  min-height: 70vh;
}

.tabs {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  padding: 12px;
  border-bottom: 1px solid var(--line);
}

.tabs button {
  background: #f6f1e8;
  border-color: var(--line);
}

.tabs button.active {
  background: var(--ink);
  color: #fff;
}

.tab {
  display: none;
  padding: 14px;
  max-height: calc(100vh - 170px);
  overflow: auto;
}

.tab.active {
  display: block;
}

.section-title {
  margin: 18px 0 10px;
  font-size: 13px;
  color: var(--muted);
  letter-spacing: 0.02em;
}

.field {
  margin-bottom: 12px;
}

.field label {
  display: block;
  font-size: 12px;
  color: var(--muted);
  margin-bottom: 6px;
}

.field input[type="text"],
.field input[type="number"],
.field textarea,
.field select {
  width: 100%;
  font: inherit;
  padding: 8px 10px;
  border: 1px solid var(--line);
  background: #fff;
}

.field input:disabled,
.field select:disabled {
  opacity: 0.45;
  cursor: not-allowed;
  background: #f3f0ea;
}

.field textarea {
  min-height: 60px;
}

.slide-card,
.sound-card {
  border: 1px solid var(--line);
  background: #fff;
  padding: 12px;
  margin-bottom: 10px;
  cursor: pointer;
}

.slide-card.active {
  border-color: var(--accent);
  box-shadow: 0 0 0 2px rgba(212, 85, 42, 0.18);
}

.slide-card h3,
.sound-card h3 {
  margin: 0 0 8px;
  font-size: 14px;
}

.pick-row {
  display: flex;
  gap: 8px;
  align-items: center;
  margin-bottom: 8px;
}

.pick-thumb {
  width: 56px;
  height: 56px;
  object-fit: cover;
  border: 1px solid var(--line);
  background: #efe9de;
}

.slide-actions {
  display: flex;
  gap: 6px;
  margin-top: 8px;
}

.check {
  display: flex;
  gap: 8px;
  align-items: center;
  margin-bottom: 10px;
}

.preview-wrap {
  display: flex;
  flex-direction: column;
}

.preview-bar {
  display: flex;
  justify-content: space-between;
  padding: 0 8px 12px;
  font-size: 13px;
}

.phone-stage {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 12px;
  min-height: 720px;
  padding: 16px;
  background: #ddd4c4;
  border: 1px solid var(--line);
}

.phone {
  width: 360px;
  height: 740px;
  max-height: calc(100vh - 220px);
  background: var(--phone);
  border-radius: 36px;
  padding: 14px 12px 16px;
  position: relative;
  box-shadow: 0 24px 60px rgba(0, 0, 0, 0.28);
}

.phone-notch {
  position: absolute;
  top: 10px;
  left: 50%;
  transform: translateX(-50%);
  width: 108px;
  height: 22px;
  background: #0d0d0d;
  border-radius: 14px;
  z-index: 3;
}

.phone-screen {
  position: relative;
  width: 100%;
  height: 100%;
  border-radius: 24px;
  overflow: hidden;
  background: #fff9c4;
}

#preview {
  width: 100%;
  height: 100%;
  border: 0;
  display: block;
  background: #fff9c4;
}

.resize-layer {
  position: absolute;
  inset: 0;
  z-index: 4;
  pointer-events: none;
}

.resize-box {
  position: absolute;
  border: 2px solid rgba(212, 85, 42, 0.95);
  box-shadow: 0 0 0 1px rgba(255, 255, 255, 0.7);
  pointer-events: none;
  transform: none;
}

.resize-box.bottom {
  border-color: rgba(31, 111, 91, 0.95);
}

.resize-handle {
  position: absolute;
  right: -10px;
  bottom: -10px;
  width: 20px;
  height: 20px;
  background: var(--accent);
  border: 2px solid #fff;
  border-radius: 4px;
  pointer-events: auto;
  cursor: nwse-resize;
}

.resize-box.bottom .resize-handle {
  background: #1f6f5b;
}

.btn-danger {
  background: #f4e6e4;
  border-color: #b42318;
  color: #b42318;
}

.icon-btn {
  width: 36px;
  height: 36px;
  padding: 0;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  font-size: 16px;
  line-height: 1;
}

.size-panel {
  width: 360px;
  background: var(--panel);
  border: 1px solid var(--line);
  padding: 10px 12px;
}

.size-panel input[type="range"] {
  width: 100%;
}

.deploy-box {
  margin-top: 12px;
  padding: 12px;
  border: 1px solid var(--line);
  background: var(--panel);
}

.deploy-box .row {
  display: flex;
  gap: 8px;
}

.deploy-box input {
  flex: 1;
  font: inherit;
  padding: 8px;
  border: 1px solid var(--line);
}

.deploy-sheet[hidden] {
  display: none !important;
}

.deploy-sheet {
  position: fixed;
  inset: 0;
  z-index: 80;
  display: flex;
  align-items: flex-end;
  justify-content: center;
}

.deploy-sheet-backdrop {
  position: absolute;
  inset: 0;
  border: 0;
  background: rgba(0, 0, 0, 0.4);
  padding: 0;
  cursor: pointer;
}

.deploy-sheet-card {
  position: relative;
  z-index: 1;
  width: min(520px, 100%);
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 18px 18px 0 0;
  padding: 18px 16px calc(16px + env(safe-area-inset-bottom));
  box-shadow: 0 -12px 40px rgba(0, 0, 0, 0.18);
  animation: sheetUp 0.28s ease-out;
}

.deploy-sheet-card strong {
  display: block;
  font-size: 16px;
  margin-bottom: 6px;
}

.deploy-sheet-card input {
  width: 100%;
  font: inherit;
  font-size: 14px;
  padding: 10px 12px;
  border: 1px solid var(--line);
  background: #fff;
  margin: 10px 0 12px;
}

.deploy-sheet-actions {
  display: grid;
  grid-template-columns: 1fr 1fr 1fr;
  gap: 8px;
}

.deploy-sheet-actions a,
.deploy-sheet-actions button {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-height: 44px;
  border: 1px solid var(--ink);
  background: #fff;
  color: var(--ink);
  text-decoration: none;
  font: inherit;
  cursor: pointer;
}

@keyframes sheetUp {
  from {
    transform: translateY(24px);
    opacity: 0.6;
  }
  to {
    transform: translateY(0);
    opacity: 1;
  }
}

.modal[hidden] {
  display: none;
}

html.picker-open,
body.picker-open {
  overflow: hidden;
  overscroll-behavior: none;
}

.modal {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.45);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 50;
  padding: 20px;
}

.modal-card {
  width: min(720px, 100%);
  max-height: min(80vh, 100%);
  display: flex;
  flex-direction: column;
  overflow: hidden;
  background: #fff;
  border: 1px solid #000;
  padding: 14px;
}

.modal-head {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 12px;
  flex-shrink: 0;
}

.gallery {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(96px, 1fr));
  gap: 10px;
  align-content: start;
  flex: 1 1 auto;
  min-height: 0;
  max-height: min(70vh, 640px);
  overflow-x: hidden;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  overscroll-behavior: contain;
  padding: 2px 2px 16px;
}

.gallery-item {
  position: relative;
  width: 100%;
  /* iOS Safari aspect-ratio+grid 겹침 방지 */
  height: 0;
  padding-bottom: 100%;
  border: 2px solid transparent;
  padding-left: 0;
  padding-right: 0;
  padding-top: 0;
  background: #efe9de;
  overflow: hidden;
  cursor: pointer;
  isolation: isolate;
}

.gallery-item img {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  object-fit: cover;
  display: block;
  pointer-events: none;
}

.gallery-item.selected {
  border-color: var(--accent);
}

.sound-gallery {
  display: grid;
  gap: 8px;
  align-content: start;
  flex: 1 1 auto;
  min-height: 0;
  max-height: min(70vh, 640px);
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  overscroll-behavior: contain;
  padding-bottom: 16px;
}

.sound-item {
  display: flex;
  align-items: center;
  gap: 10px;
  width: 100%;
  text-align: left;
  padding: 12px;
  border: 2px solid var(--line);
  background: #fff;
}

.sound-item.selected {
  border-color: var(--accent);
  background: #fff4ef;
}

.sound-item .swatch {
  width: 34px;
  height: 34px;
  border-radius: 50%;
  background: linear-gradient(135deg, #ffb4a2, #6dd3c2);
}

.modal-upload {
  margin-bottom: 12px;
  flex-shrink: 0;
}

.upload-btn {
  display: inline-flex;
  align-items: center;
  padding: 8px 12px;
  border: 1px dashed var(--line);
  background: #f7f2e9;
  cursor: pointer;
  font-size: 13px;
}

.extract-bar {
  margin-top: 10px;
  padding: 12px;
  border: 1px solid var(--line);
  background: #fff7f2;
  border-radius: 10px;
}

.extract-bar .primary {
  width: 100%;
  min-height: 48px;
  font-weight: 700;
}

.extract-bar .hint {
  margin: 8px 0 0;
}

.secondary-block {
  margin-top: 12px;
  padding-top: 12px;
  border-top: 1px dashed var(--line);
}

.grid-2 {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
}

.common-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
  margin-bottom: 16px;
}

.common-item {
  display: flex;
  gap: 8px;
  align-items: center;
  border: 1px solid var(--line);
  background: #fff;
  padding: 8px;
}

.common-item.is-off {
  opacity: 0.55;
}

.common-item .feature-toggle {
  display: flex;
  align-items: center;
  gap: 6px;
  margin: 6px 0;
  font-size: 13px;
  cursor: pointer;
  user-select: none;
}

.common-item .feature-toggle input {
  width: 16px;
  height: 16px;
  accent-color: var(--ink);
}

.common-item button:disabled {
  opacity: 0.45;
  cursor: not-allowed;
}

.hint {
  color: var(--muted);
  font-size: 12px;
}

.mobile-switch {
  display: none;
}

@media (max-width: 960px) {
  .layout {
    grid-template-columns: 1fr;
  }
}

@media (max-width: 768px) {
  body {
    padding-bottom: calc(16px + env(safe-area-inset-bottom));
  }

  .top {
    flex-direction: column;
    align-items: stretch;
    gap: 12px;
    padding: 14px 14px 10px;
    padding-top: max(14px, env(safe-area-inset-top));
    position: sticky;
    top: 0;
    z-index: 30;
    background:
      linear-gradient(to bottom, rgba(236, 231, 220, 0.96), rgba(236, 231, 220, 0.92));
    backdrop-filter: blur(8px);
  }

  .top h1 {
    font-size: 22px;
  }

  .top p {
    font-size: 13px;
  }

  .top-actions {
    display: grid;
    grid-template-columns: 1fr 1.4fr;
    gap: 8px;
  }

  .top-actions button {
    min-height: 44px;
    padding: 10px 12px;
    font-size: 14px;
  }

  .mobile-switch {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 0;
    position: sticky;
    top: 0;
    z-index: 20;
    margin: 0 14px 10px;
    border: 1px solid var(--line);
    background: var(--panel);
    border-radius: 12px;
    overflow: hidden;
  }

  .mobile-switch button {
    border: 0;
    border-radius: 0;
    background: transparent;
    min-height: 44px;
    font-weight: 600;
  }

  .mobile-switch button.active {
    background: var(--ink);
    color: #fff;
  }

  .layout {
    padding: 0 14px 14px;
    gap: 12px;
  }

  body.mobile-view-edit .preview-wrap {
    display: none;
  }

  body.mobile-view-preview .panel {
    display: none;
  }

  .panel {
    min-height: auto;
    border-radius: 12px;
  }

  .tab {
    max-height: none;
    padding: 12px;
    padding-bottom: 24px;
  }

  .pick-row {
    flex-wrap: wrap;
  }

  .pick-row button {
    min-height: 40px;
    flex: 1 1 auto;
  }

  .common-grid {
    grid-template-columns: 1fr;
  }

  .common-item {
    flex-wrap: wrap;
  }

  .slide-actions {
    gap: 8px;
  }

  .icon-btn {
    width: 44px;
    height: 44px;
    font-size: 18px;
  }

  .preview-wrap {
    gap: 8px;
  }

  .preview-bar {
    padding: 4px 2px 8px;
    align-items: center;
  }

  .preview-bar a {
    min-height: 40px;
    display: inline-flex;
    align-items: center;
    padding: 0 10px;
    border: 1px solid var(--line);
    background: #fff;
    text-decoration: none;
    color: var(--ink);
    border-radius: 8px;
    font-size: 13px;
  }

  .phone-stage {
    min-height: 0;
    padding: 12px 8px 16px;
    border-radius: 12px;
  }

  .phone {
    width: min(100%, 360px);
    height: auto;
    aspect-ratio: 9 / 19.2;
    max-height: min(78vh, 720px);
    border-radius: 28px;
    padding: 10px 8px 12px;
  }

  .phone-notch {
    width: 84px;
    height: 18px;
    top: 8px;
  }

  .phone-screen {
    border-radius: 20px;
  }

  .size-panel {
    width: min(100%, 360px);
  }

  .deploy-box {
    border-radius: 12px;
  }

  .deploy-box .row {
    flex-direction: column;
  }

  .deploy-box .row button {
    min-height: 44px;
  }

  .deploy-sheet-actions {
    grid-template-columns: 1fr;
  }

  .modal {
    padding: 0;
    align-items: stretch;
  }

  .modal-card {
    width: 100%;
    max-height: 100dvh;
    height: 100dvh;
    border: 0;
    border-radius: 0;
    padding: 14px;
    padding-top: max(14px, env(safe-area-inset-top));
    padding-bottom: max(14px, env(safe-area-inset-bottom));
    overflow: hidden;
  }

  .modal-head button,
  .upload-btn {
    min-height: 44px;
  }

  .gallery {
    /* 폰: 한 줄 3장 → 썸네일 크게 */
    grid-template-columns: repeat(3, minmax(0, 1fr));
    gap: 10px;
    max-height: none;
    flex: 1 1 auto;
    min-height: 0;
  }

  .sound-gallery {
    max-height: none;
    flex: 1 1 auto;
    min-height: 0;
  }

  .sound-item {
    min-height: 52px;
  }

  .field input[type="text"],
  .field input[type="number"],
  .field textarea,
  .field select {
    font-size: 16px;
    padding: 10px 12px;
  }

  .grid-2 {
    gap: 10px;
  }
}

@media (max-width: 420px) {
  .top-actions {
    grid-template-columns: 1fr;
  }

  .grid-2 {
    grid-template-columns: 1fr;
  }
}
