:root {
  --sp-bg: #fdf6ec;
  --sp-card: #ffffff;
  --sp-text: #3d2c1e;
  --sp-muted: #8b7355;
  --sp-border: #e8dcc8;
  --sp-accent: #e8834a;
  --sp-accent-strong: #8f4113;
  --sp-accent-soft: #fdebd4;
  --sp-radius: 14px;
  --sp-shadow: 0 8px 32px rgba(61, 44, 30, 0.08);
  --sp-font-ui: -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  --sp-font-display: "Georgia", "Times New Roman", serif;
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  min-height: 100vh;
  background: var(--sp-bg);
  color: var(--sp-text);
  font-family: var(--sp-font-ui);
}

button,
input,
textarea {
  font: inherit;
}

.hidden {
  display: none !important;
}

.story-pack-shell {
  padding: 32px 20px 48px;
}

.screen {
  width: min(100%, 960px);
  margin: 0 auto;
}

.panel {
  background: var(--sp-card);
  border: 1px solid var(--sp-border);
  border-radius: var(--sp-radius);
  box-shadow: var(--sp-shadow);
  padding: 24px;
}

.panel-header,
.section-heading,
.preview-topbar {
  margin-bottom: 20px;
}

.preview-topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
}

.preview-topbar-actions {
  display: flex;
  flex-wrap: wrap;
  justify-content: flex-end;
  gap: 10px;
}

.eyebrow {
  margin: 0 0 6px;
  color: var(--sp-muted);
  font-size: 0.75rem;
  font-weight: 700;
  letter-spacing: 0.08em;
  text-transform: uppercase;
}

h1,
h2,
h3,
p {
  margin-top: 0;
}

h1,
h2 {
  margin-bottom: 8px;
  font-family: var(--sp-font-display);
}

.subtitle {
  color: var(--sp-muted);
}

#setupHeading,
#previewHeading {
  outline: none;
}

.field-group,
.library-id-card,
.api-base-card,
.viewer-section,
.generation-history-section,
.scene-rail-section,
.replacement-section,
.library-section {
  margin-bottom: 20px;
}

.library-id-card,
.api-base-card {
  display: grid;
  grid-template-columns: minmax(0, 0.95fr) minmax(280px, 1.2fr);
  gap: 18px;
  align-items: end;
  padding: 16px;
  border: 1px solid var(--sp-border);
  border-radius: 16px;
  background:
    radial-gradient(circle at top left, rgba(232, 131, 74, 0.14), transparent 42%),
    linear-gradient(135deg, #fffdf8 0%, #fff 68%);
}

.api-base-card {
  background:
    radial-gradient(circle at bottom right, rgba(36, 92, 130, 0.12), transparent 44%),
    linear-gradient(135deg, #f7fbff 0%, #fff 68%);
}

.library-id-note {
  margin-bottom: 0;
  color: var(--sp-muted);
}

.library-id-controls {
  display: grid;
  gap: 10px;
}

.library-id-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
}

label {
  display: block;
  margin-bottom: 8px;
  font-weight: 600;
}

input,
textarea {
  width: 100%;
  padding: 10px 12px;
  border: 1px solid var(--sp-border);
  border-radius: 10px;
  background: #fff;
}

textarea {
  min-height: 76px;
  resize: vertical;
}

.upload-grid,
.replacement-grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 16px;
}

.upload-grid-single {
  grid-template-columns: 1fr;
}

.upload-card,
.scene-viewer,
.scene-chip,
.library-list {
  border: 1px solid var(--sp-border);
  border-radius: 12px;
  background: #fff;
}

.upload-card {
  padding: 16px;
}

.upload-card {
  display: grid;
  gap: 10px;
  background: linear-gradient(180deg, #fffdf8 0%, #fff 100%);
}

.upload-card-meta,
.draft-state-note {
  margin-bottom: 0;
  color: var(--sp-muted);
}

.selection-list,
.pairing-preview-list {
  margin: 0;
  padding-left: 20px;
  color: var(--sp-text);
}

.selection-list li,
.pairing-preview-list li {
  margin-bottom: 6px;
}

.pairing-preview-section {
  margin-bottom: 20px;
  padding: 16px;
  border: 1px solid var(--sp-border);
  border-radius: 12px;
  background: linear-gradient(180deg, #fffdf8 0%, #fff 100%);
}

.section-heading-compact {
  margin-bottom: 12px;
}

.message-banner {
  margin-bottom: 20px;
  padding: 12px 14px;
  border: 1px solid #f1b7b7;
  border-radius: 12px;
  background: #fff0f0;
  color: #922f2f;
  font-weight: 600;
}

.status-pill {
  display: inline-flex;
  align-items: center;
  min-height: 40px;
  padding: 8px 14px;
  border-radius: 999px;
  background: var(--sp-accent-soft);
  color: var(--sp-text);
}

.status-pill[data-state="ready"],
.status-pill[data-state="success"] {
  background: #e8f6ec;
  color: #235b34;
}

.status-pill[data-state="idle"] {
  background: #f4ede1;
  color: var(--sp-muted);
}

.status-pill[data-state="warning"] {
  background: #fff2dc;
  color: #855514;
}

.status-pill[data-state="error"] {
  background: #ffe1e1;
  color: #922f2f;
}

.status-pill[data-state="busy"] {
  background: #e9f0ff;
  color: #2f4f92;
}

.setup-actions button,
.preview-topbar button,
.library-card-open,
.library-card-delete,
.child-customization-actions button,
.replacement-grid button {
  border: 1px solid transparent;
  border-radius: 10px;
  padding: 10px 14px;
  background: var(--sp-accent-strong);
  color: #fff;
  cursor: pointer;
}

button:focus-visible,
input:focus-visible,
textarea:focus-visible {
  outline: 3px solid rgba(143, 65, 19, 0.28);
  outline-offset: 2px;
}

button:disabled {
  opacity: 0.55;
  cursor: not-allowed;
}

.scene-viewer {
  min-height: 280px;
  padding: 18px;
  background: linear-gradient(180deg, #fffdf9 0%, #f7efe3 100%);
}

.scene-stage-book {
  display: flex;
  justify-content: center;
  padding: 8px 0;
}

.scene-spread {
  position: relative;
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  width: min(100%, 940px);
  aspect-ratio: 2 / 1;
  overflow: hidden;
  border-radius: 4px 12px 12px 4px;
  background: #efe4d2;
  box-shadow:
    0 20px 44px rgba(61, 44, 30, 0.16),
    0 8px 24px rgba(61, 44, 30, 0.12),
    0 2px 8px rgba(61, 44, 30, 0.08);
  isolation: isolate;
}

.client-page-flip-book {
  margin: 0 auto;
}

.client-book-page {
  position: relative;
  min-width: 0;
  height: 100%;
  overflow: hidden;
  background: linear-gradient(135deg, #faf7f2 0%, #f0e8da 42%, #e8dcc8 100%);
}

.client-book-page--left {
  border-right: 1.5px solid rgba(0, 0, 0, 0.12);
  border-radius: 4px 0 0 4px;
}

.client-book-page--right {
  border-radius: 0 12px 12px 0;
}

.client-book-page--blank {
  pointer-events: none;
}

.client-book-cover-page {
  display: grid;
  place-items: center;
}

.client-book-spread-page {
  z-index: 2;
}

.client-spread-crop {
  position: absolute;
  inset: 0;
  overflow: hidden;
}

.client-spread-crop img {
  position: absolute;
  top: 0;
  width: 200%;
  height: 100%;
  object-fit: cover;
  max-width: none;
}

.client-spread-crop--left img {
  left: 0;
}

.client-spread-crop--right img {
  right: 0;
}

.scene-spread::before {
  content: "";
  position: absolute;
  top: 0;
  bottom: 0;
  left: 50%;
  width: 24px;
  transform: translateX(-50%);
  background: linear-gradient(90deg, rgba(80, 56, 35, 0) 0%, rgba(80, 56, 35, 0.18) 50%, rgba(80, 56, 35, 0) 100%);
  pointer-events: none;
  z-index: 1;
}

.scene-stage {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  overflow: hidden;
  border-radius: inherit;
  background: linear-gradient(135deg, #faf7f2 0%, #f0e8da 42%, #e8dcc8 100%);
}

.scene-stage.cover-page {
  position: relative;
  inset: auto;
  width: auto;
  height: 100%;
  z-index: 2;
}

.cover-page-left {
  grid-column: 1;
  border-radius: 4px 0 0 4px;
}

.cover-page-right {
  grid-column: 2;
  border-radius: 0 12px 12px 0;
}

.scene-page {
  position: relative;
  z-index: 2;
  min-width: 0;
  pointer-events: none;
}

.scene-page.left {
  border-right: 1.5px solid rgba(0, 0, 0, 0.12);
  border-radius: 4px 0 0 4px;
}

.scene-page.right {
  border-radius: 0 12px 12px 0;
}

.scene-page::before,
.scene-page::after {
  content: "";
  position: absolute;
  inset: 0;
  pointer-events: none;
}

.scene-page.left::before {
  background:
    linear-gradient(to right, rgba(255, 248, 235, 0.22) 0%, transparent 8%),
    linear-gradient(to left, rgba(60, 40, 20, 0.06) 0%, transparent 6%);
  border-radius: 4px 0 0 4px;
}

.scene-page.right::before {
  background:
    linear-gradient(to left, rgba(255, 248, 235, 0.18) 0%, transparent 8%),
    linear-gradient(to right, rgba(60, 40, 20, 0.05) 0%, transparent 6%);
  border-radius: 0 12px 12px 0;
}

.scene-page.left::after {
  background:
    linear-gradient(to right, rgba(255, 255, 255, 0.18) 0%, rgba(255, 255, 255, 0.06) 4%, transparent 14%),
    linear-gradient(to left, rgba(0, 0, 0, 0.28) 0%, rgba(0, 0, 0, 0.14) 3%, rgba(0, 0, 0, 0.06) 8%, transparent 25%);
  border-radius: 4px 0 0 4px;
}

.scene-page.right::after {
  background:
    linear-gradient(to left, rgba(255, 255, 255, 0.15) 0%, rgba(255, 255, 255, 0.05) 4%, transparent 14%),
    linear-gradient(to right, rgba(0, 0, 0, 0.22) 0%, rgba(0, 0, 0, 0.1) 3%, rgba(0, 0, 0, 0.04) 8%, transparent 25%);
  border-radius: 0 12px 12px 0;
}

.scene-stage-book.page-effects-off .scene-spread {
  box-shadow: none;
}

.scene-stage-book.page-effects-off .scene-spread::before,
.scene-stage-book.page-effects-off .scene-page::before,
.scene-stage-book.page-effects-off .scene-page::after,
.scene-stage-book.page-effects-off .client-book-page::before,
.scene-stage-book.page-effects-off .client-book-page::after {
  display: none;
}

.scene-stage-book.page-effects-off .scene-page.left,
.scene-stage-book.page-effects-off .client-book-page--left {
  border-right-color: transparent;
}

.scene-stage img {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  object-fit: cover;
  object-position: center;
}

.scene-page-image {
  z-index: 1;
}

.story-customizer__page-loading {
  align-items: center;
  background: rgba(37, 24, 15, 0.68);
  border-radius: 999px;
  color: #fffaf1;
  display: inline-flex;
  font-size: 1.2rem;
  font-weight: 800;
  gap: 0.8rem;
  left: 50%;
  padding: 0.9rem 1.2rem;
  position: absolute;
  top: 50%;
  transform: translate(-50%, -50%);
  white-space: nowrap;
  z-index: 4;
}

.story-customizer__page-spinner {
  animation: story-customizer-spin 0.8s linear infinite;
  border: 2px solid rgba(255, 250, 241, 0.35);
  border-radius: 50%;
  border-top-color: #fffaf1;
  height: 1.6rem;
  width: 1.6rem;
}

@keyframes story-customizer-spin {
  to { transform: rotate(360deg); }
}

.scene-viewer-caption {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 14px;
}

.scene-viewer-caption p {
  margin-bottom: 0;
}

.scene-viewer-meta {
  color: var(--sp-muted);
  font-size: 0.95rem;
}

.scene-rail {
  display: flex;
  flex-wrap: nowrap;
  gap: 8px;
  overflow-x: auto;
  overflow-y: hidden;
  padding: 2px 2px 8px;
  scrollbar-width: thin;
}

.scene-chip {
  position: relative;
  flex: 0 0 auto;
  min-width: 42px;
  min-height: 42px;
  padding: 0 13px;
  border: 1px solid var(--sp-border);
  border-radius: 999px;
  background: linear-gradient(180deg, #fff 0%, #fff8ef 100%);
  color: var(--sp-muted);
  font-weight: 700;
  text-align: center;
  cursor: pointer;
  transition:
    border-color 0.18s ease,
    box-shadow 0.18s ease,
    color 0.18s ease,
    transform 0.18s ease;
}

.scene-chip:hover:not(:disabled),
.scene-chip:focus-visible {
  border-color: var(--sp-accent);
  color: var(--sp-accent-strong);
  box-shadow: 0 0 0 3px rgba(232, 131, 74, 0.14);
  outline: none;
}

.scene-chip.is-active {
  border-color: var(--sp-accent);
  background: linear-gradient(180deg, #fff7ee 0%, #fdebd4 100%);
  color: var(--sp-accent-strong);
  box-shadow: 0 0 0 3px rgba(232, 131, 74, 0.18);
}

.scene-chip:disabled {
  cursor: wait;
  opacity: 0.7;
}

.scene-chip-number {
  display: block;
  font-size: 0.94rem;
  line-height: 1;
}

.scene-chip-cover {
  min-width: 64px;
  padding-inline: 14px;
}

.scene-chip-current {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border: 0;
}

.library-list {
  padding: 16px;
  display: grid;
  gap: 12px;
}

.library-card {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  padding: 16px;
  border: 1px solid var(--sp-border);
  border-radius: 12px;
  background: linear-gradient(180deg, #fff 0%, #fff9f2 100%);
}

.library-card-copy {
  min-width: 0;
}

.library-card h3 {
  margin-bottom: 6px;
}

.library-card p {
  margin-bottom: 0;
}

.library-card-meta {
  color: var(--sp-muted);
  font-size: 0.95rem;
}

.library-card-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  justify-content: flex-end;
}

.library-card-delete {
  background: #fff;
  color: #9b3c3c;
  border: 1px solid #efc7c7;
}

.placeholder-copy {
  margin-bottom: 0;
  color: var(--sp-muted);
}

.replacement-grid {
  grid-template-columns: repeat(2, minmax(0, 1fr));
}

.custom-retry-prompt-field {
  display: grid;
  gap: 8px;
  margin-bottom: 14px;
}

.custom-retry-prompt-field label,
.custom-retry-prompt-help {
  margin-bottom: 0;
}

.custom-retry-prompt-help {
  color: var(--sp-muted);
  font-size: 0.9rem;
}

.generation-history {
  padding: 10px;
  border: 1px solid var(--sp-border);
  border-radius: 12px;
  background: linear-gradient(90deg, #fffdf8 0%, #fff 48%, #fff8ef 100%);
}

.generation-history-compact {
  display: flex;
  align-items: center;
  gap: 8px;
}

.generation-history-current {
  display: grid;
  flex: 1 1 auto;
  min-width: 0;
  gap: 2px;
}

.generation-history-title {
  color: var(--sp-muted);
  font-size: 0.72rem;
  font-weight: 800;
  letter-spacing: 0.08em;
  text-transform: uppercase;
}

.generation-history-summary,
.generation-history-error {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.generation-history-summary {
  font-weight: 700;
}

.generation-history-arrow {
  flex: 0 0 auto;
  padding: 8px 10px;
  border: 1px solid var(--sp-border);
  border-radius: 10px;
  background: #fff;
  color: var(--sp-text);
  cursor: pointer;
  font-weight: 700;
}

.generation-history-arrow {
  width: 38px;
  font-size: 1.2rem;
  line-height: 1;
}

.generation-history-arrow:hover:not(:disabled),
.generation-history-arrow:focus-visible {
  border-color: var(--sp-accent);
  box-shadow: 0 0 0 3px rgba(232, 131, 74, 0.14);
  outline: none;
}

.generation-history-arrow:disabled {
  cursor: not-allowed;
  opacity: 0.55;
}

.generation-history-error {
  color: #922f2f;
  font-size: 0.85rem;
}

.child-customization-section {
  margin-bottom: 20px;
}

.child-customization-panel {
  display: grid;
  gap: 12px;
  padding: 16px;
  border: 1px solid var(--sp-border);
  border-radius: 12px;
  background: linear-gradient(180deg, #fffdf8 0%, #fff 100%);
}

.child-photo-preview {
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 140px;
  max-width: 100%;
  padding: 16px;
  border: 1px dashed var(--sp-border);
  border-radius: 12px;
  background: #f8f3eb;
  color: var(--sp-muted);
  overflow: hidden;
}

.child-photo-preview img {
  display: block;
  width: auto;
  max-width: 100%;
  max-height: 220px;
  object-fit: contain;
  border-radius: 10px;
}

.child-customization-actions {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 12px;
}

.preview-status {
  margin-bottom: 14px;
  color: #5f4932;
  padding: 12px 14px;
  border-radius: 12px;
  background: #f8f3eb;
}

.preview-status[data-state="error"] {
  background: #fff0f0;
  color: #922f2f;
}

.preview-status[data-state="busy"] {
  background: #e9f0ff;
  color: #2f4f92;
}

.preview-status[data-state="warning"] {
  background: #fff2dc;
  color: #855514;
}

.preview-status[data-state="success"] {
  background: #e8f6ec;
  color: #235b34;
}

.customization-progress-note {
  color: var(--sp-muted);
  font-size: 0.9rem;
}

.replacement-grid button {
  width: 100%;
}

.child-customization-actions button {
  width: 100%;
}

.hidden-file-input {
  display: none;
}

@media (max-width: 700px) {
  .story-pack-shell {
    padding: 20px 12px 32px;
  }

  .panel {
    padding: 18px;
  }

  .preview-topbar,
  .library-card {
    flex-direction: column;
    align-items: stretch;
  }

  .preview-topbar-actions {
    justify-content: stretch;
  }

  .upload-grid,
  .replacement-grid {
    grid-template-columns: 1fr;
  }

  .child-customization-actions {
    grid-template-columns: 1fr;
  }

  .scene-rail {
    gap: 6px;
  }

  .scene-chip {
    min-width: 38px;
    min-height: 38px;
    padding-inline: 11px;
  }

  .scene-chip-cover {
    min-width: 56px;
  }

  .scene-viewer-caption {
    flex-direction: column;
    align-items: flex-start;
  }

  .library-card-actions {
    justify-content: stretch;
  }

  .library-card-actions button {
    width: 100%;
  }
}
