/* Story studio — ads-style layout with sidebar builder */

.page.page-story {
  --story-builder-width: 400px;
  --story-builder-gap: 0px;
  width: 100%;
  height: 100vh;
  max-width: none;
  min-height: 100vh;
  margin: 0;
  padding: 0 0 0 calc(var(--story-builder-width) + var(--story-builder-gap));
  transition: padding-left 0.3s ease;
  overflow: hidden;
  background:
    radial-gradient(circle at top left, rgba(73, 115, 172, 0.16), transparent 26%),
    #0d0d12;
}
.page.page-story.is-builder-collapsed {
  padding-left: 0;
}

/* ── Builder sidebar ── */
.story-builder-card {
  position: fixed;
  top: 60px;
  left: 0;
  bottom: 0;
  width: var(--story-builder-width);
  max-height: none;
  overflow: visible;
  border: 1px solid rgba(138, 163, 196, 0.22);
  border-left: 0;
  border-radius: 0;
  padding: 0;
  background-color: #0d0d12;
  box-shadow: 0 22px 56px rgba(0, 0, 0, 0.28);
  display: flex;
  flex-direction: column;
  transition: left 0.3s ease;
  z-index: 100;
  animation: builderSlideInLeft 640ms cubic-bezier(0.22, 1, 0.36, 1) 120ms backwards;
}
.story-builder-card.is-collapsed {
  left: calc(-1 * var(--story-builder-width));
}
.story-builder-card-inner {
  flex: 1;
  overflow-y: auto;
  overscroll-behavior: contain;
  padding: 26px 20px 10px;
  display: grid;
  align-content: start;
  align-items: start;
  grid-auto-rows: max-content;
  gap: 16px;
}

/* ── Builder head ── */
.story-builder-head {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 12px;
}
.story-builder-head h1 {
  margin: 0;
  color: #f7f9ff;
  font-size: 1.3rem;
  line-height: 1.08;
}

/* ── Builder sections ── */
.story-builder-section {
  display: grid;
  gap: 14px;
  padding: 16px;
  border: 1px solid rgba(107, 132, 167, 0.18);
  border-radius: 18px;
}
.story-builder-section[hidden] {
  display: none;
}
.story-builder-section-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
}
.story-builder-section-title {
  margin: 0;
  color: #f6f9ff;
  font-size: 1.0rem;
  font-weight: 500;
}

/* ── Builder inputs ── */
.story-builder-input {
  width: 100%;
  border: 1px solid rgba(104, 66, 170, 0.62);
  border-radius: 14px;
  background: rgba(13, 19, 31, 0.94);
  color: #eef2ff;
  font: inherit;
  font-weight: 400;
  font-size: 14px;
  padding: 10px 14px;
  min-height: 42px;
}
.story-builder-input::placeholder {
  color: rgba(160, 176, 204, 0.5);
}
.story-builder-input:focus {
  outline: none;
  border-color: rgba(121, 170, 255, 0.7);
  box-shadow: 0 0 0 2px rgba(121, 170, 255, 0.12);
}

/* ── Builder field ── */
.story-builder-field {
  display: grid;
  gap: 6px;
}
.story-builder-field-label {
  color: #9fb0cc;
  font-size: 0.82rem;
  font-weight: 500;
}

/* ── Create button ── */
.story-builder-create-btn {
  width: 100%;
  min-height: 52px;
  border: 1px solid rgba(80, 136, 235, 0.4);
  border-radius: 16px;
  background-color: #6d28d9 !important;
  color: #ffffff;
  font: inherit;
  font-size: 0.98rem;
  font-weight: 600;
  cursor: pointer;
  box-shadow: 0 18px 30px rgba(19, 54, 115, 0.28);
  transition: transform 0.18s ease, filter 0.18s ease, box-shadow 0.18s ease;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 10px;
}
.story-builder-create-btn:hover:not(:disabled) {
  filter: brightness(1.04);
  transform: translateY(-1px);
}
.story-builder-create-btn:disabled {
  opacity: 0.35;
  cursor: not-allowed;
  filter: grayscale(0.5);
  transform: none;
  box-shadow: none;
}
.story-builder-status {
  margin: 0;
  font-size: 0.82rem;
  color: #9fb0cc;
  min-height: 1.2em;
}

/* ── Ad options (advertisement genre) ── */
.story-ad-upload-zone {
  position: relative;
  border: 2px dashed rgba(138, 163, 196, 0.25);
  border-radius: 12px;
  background: rgba(13, 19, 31, 0.5);
  cursor: pointer;
  overflow: hidden;
  transition: border-color 0.18s, background 0.18s;
  min-height: 90px;
  display: grid;
  place-items: center;
}
.story-ad-upload-zone:hover,
.story-ad-upload-zone.is-dragover {
  border-color: rgba(121, 170, 255, 0.5);
  background: rgba(20, 30, 50, 0.6);
}
.story-ad-upload-placeholder {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 6px;
  padding: 16px 10px;
  color: rgba(160, 176, 204, 0.6);
  font-size: 0.78rem;
  text-align: center;
  pointer-events: none;
}
.story-ad-upload-placeholder svg {
  opacity: 0.4;
}
.story-ad-upload-preview {
  width: 100%;
  max-height: 160px;
  object-fit: contain;
  display: block;
}
.story-ad-upload-preview.is-hidden,
.story-ad-upload-remove.is-hidden,
.story-ad-upload-placeholder.is-hidden {
  display: none;
}
.story-ad-upload-remove {
  position: absolute;
  top: 6px;
  right: 6px;
  width: 24px;
  height: 24px;
  border: none;
  border-radius: 50%;
  background: rgba(0, 0, 0, 0.6);
  color: #fff;
  font-size: 1rem;
  line-height: 1;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background 0.15s;
}
.story-ad-upload-remove:hover {
  background: rgba(220, 60, 60, 0.8);
}

.story-ad-select-btn {
  width: 100%;
  min-height: 42px;
  border: 1px solid rgba(138, 163, 196, 0.22);
  border-radius: 12px;
  background: rgba(13, 19, 31, 0.5);
  color: rgba(160, 176, 204, 0.85);
  font: inherit;
  font-size: 0.82rem;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  transition: border-color 0.18s, background 0.18s, color 0.18s;
}
.story-ad-select-btn:hover {
  border-color: rgba(121, 170, 255, 0.5);
  color: #e0eaff;
  background: rgba(20, 30, 50, 0.6);
}

/* ── Workspace (project list) ── */
.story-workspace {
  position: fixed;
  left: calc(var(--story-builder-width) + var(--story-builder-gap));
  right: 0;
  top: 60px;
  bottom: 0;
  overflow-y: auto;
  padding: 24px;
  transition: left 0.3s ease;
}
.page.page-story.is-builder-collapsed .story-workspace {
  left: 0;
}

.story-workspace-head {
  margin: 0 0 20px;
  display: flex;
  align-items: center;
  justify-content: space-between;
}
.story-workspace-head h2 {
  margin: 0;
  color: #f7f9ff;
  font-size: 1.2rem;
}

/* Auth gate */
.story-auth-gate {
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: calc(100vh - 200px);
  padding: 40px 20px;
}

.story-auth-gate.is-hidden {
  display: none;
}

/* Override ad-studio animation opacity for story page */
.story-auth-gate .ad-auth-gate-text {
  opacity: 1;
}

.story-auth-gate .ad-auth-gate-cards .ad-gate-card {
  opacity: 1;
}

/* Contain cards within scrollable workspace */
.story-auth-gate .ad-auth-gate-cards {
  min-height: 320px;
  overflow: hidden;
}

.story-auth-gate .ad-gate-card-main {
  top: 5%;
  left: 2%;
  width: 55%;
}

.story-auth-gate .ad-gate-card-tr {
  top: 0;
  right: 0;
  width: 48%;
}

.story-auth-gate .ad-gate-card-bl {
  bottom: 0;
  left: 0;
  width: 46%;
}

.story-workspace-empty {
  display: grid;
  place-items: center;
  min-height: 300px;
  color: #9fb0cc;
  text-align: center;
}
.story-workspace-empty[hidden] {
  display: none;
}
.story-workspace-empty h2 {
  margin: 0 0 8px;
  color: #c8d5e8;
  font-size: 1.1rem;
}
.story-workspace-empty p {
  margin: 0;
  font-size: 0.88rem;
}

/* ── Project grid ── */
.story-project-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(220px, 1fr));
  gap: 16px;
}
.story-project-grid[hidden] {
  display: none;
}

.story-project-card {
  position: relative;
  border: 1px solid rgba(0, 0, 0, 0.0);
  border-radius: 14px;
  background: rgba(16, 24, 36, 0.6);
  overflow: hidden;
  cursor: pointer;
  transition: border-color 0.18s, transform 0.12s, box-shadow 0.18s;
}
.story-project-card:hover {
  border-color: rgba(69, 102, 158, 0.5);
  transform: translateY(-2px);
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.3);
}
.story-project-card.is-active {
  border-color: rgba(104, 66, 170, 0.8);
  box-shadow: 0 0 0 1px rgba(104, 66, 170, 0.4), 0 8px 24px rgba(0, 0, 0, 0.3);
}

.story-project-card-cover {
  position: relative;
  width: 100%;
  aspect-ratio: 16 / 9;
  background: rgba(20, 28, 44, 0.8);
  display: grid;
  place-items: center;
  overflow: hidden;
}
.story-project-card-cover img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}
/* Portrait projects: card cover stays 16:9 box, image shown via background
   so we can force "contain" sizing without relying on <img> rules. */
.story-project-card.is-portrait .story-project-card-cover {
  background: #0a1018;
}
.story-project-card-cover-bg {
  position: absolute;
  inset: 0;
  background-color: #0a1018;
  background-repeat: no-repeat;
  background-position: center center;
  background-size: contain;
}
.story-project-card-aspect {
  position: absolute;
  left: 1px;
  bottom: 1px;
  padding: 2px 8px;
  border-radius: 6px;
  background: rgba(8, 12, 22, 0.78);
  backdrop-filter: blur(6px);
  /*border: 1px solid rgba(138, 163, 196, 0.3);*/
  color: #ecf4ff;
  font-size: 0.5rem;
  font-weight: 600;
  letter-spacing: 0.04em;
  font-variant-numeric: tabular-nums;
}
.story-project-card-cover-empty {
  color: rgba(160, 176, 204, 0.3);
  font-size: 2rem;
}

.story-project-card-body {
  padding: 12px 14px;
  display: grid;
  gap: 4px;
}
.story-project-card-name {
  color: #f7f9ff;
  font-size: 0.88rem;
  font-weight: 500;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.story-project-card-meta {
  color: #9fb0cc;
  font-size: 0.75rem;
}

.story-project-card-delete {
  position: absolute;
  top: 8px;
  right: 8px;
  width: 28px;
  height: 28px;
  border: 1px solid rgba(255, 255, 255, 0.1);
  border-radius: 50%;
  background: rgba(0, 0, 0, 0.5);
  color: #9fb0cc;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  opacity: 0;
  transition: opacity 0.15s, background 0.15s, color 0.15s;
}
.story-project-card:hover .story-project-card-delete {
  opacity: 1;
}
.story-project-card-delete:hover {
  background: rgba(220, 60, 60, 0.7);
  color: #fff;
}

/* ── Auth view ── */
.story-auth-view {
  position: fixed;
  left: calc(var(--story-builder-width) + var(--story-builder-gap));
  right: 0;
  top: 60px;
  bottom: 0;
  display: grid;
  place-items: center;
  transition: left 0.3s ease;
}
.story-auth-view[hidden] {
  display: none;
}
.page.page-story.is-builder-collapsed .story-auth-view {
  left: 0;
}

/* ── Full HD / smaller-screen scaling ── */
@media (max-height: 1200px) {
  .page.page-story {
    --story-builder-width: 360px;
  }
  .story-builder-card {
    top: 52px;
  }
  .story-workspace,
  .story-auth-view {
    top: 52px;
  }
}

/* ── Responsive ── */
@media (max-width: 1260px) {
  .page.page-story {
    width: min(1440px, calc(100% - 44px));
    height: auto;
    max-width: none;
    min-height: 100vh;
    margin: 0 auto;
    padding: 50px 0 42px;
    overflow: visible;
  }
  .story-builder-card {
    position: static;
    left: auto;
    bottom: auto;
    width: auto;
    max-height: none;
    overflow: visible;
    border-left: 1px solid rgba(255, 255, 255, 0.08);
    border-radius: 20px;
    transition: none;
  }
  .story-builder-card.is-collapsed {
    left: auto;
  }
  .ad-builder-toggle {
    display: none;
  }
  .story-workspace,
  .story-auth-view {
    position: static;
    left: auto;
    right: auto;
    top: auto;
    bottom: auto;
    min-height: 400px;
    margin-top: 20px;
  }
}

/* ── Mobile "สร้างโปรเจ็คใหม่" button + builder overlay ─────────────
   Mirrors the ad-studio mobile flow: on mobile the builder card is
   hidden by default and a single button surfaces "create new" — the
   card slides in as a full-screen overlay only when the user opts in.
   Keeps the project list as the primary mobile view. */

.story-mobile-new-btn {
  display: none;
  align-items: center;
  justify-content: center;
  gap: 8px;
  width: calc(100% - 20px);
  margin: 10px auto 16px;
  padding: 12px 16px;
  border: 0;
  border-radius: 12px;
  background: linear-gradient(135deg, #8b5cf6, #7c3aed);
  color: #ffffff;
  font-family: inherit;
  font-size: 0.95rem;
  font-weight: 600;
  line-height: 1;
  cursor: pointer;
  box-shadow: 0 8px 20px rgba(124, 58, 237, 0.3);
  transition: filter 0.15s, box-shadow 0.15s;
}

.story-mobile-new-btn:hover {
  filter: brightness(1.08);
}

.story-mobile-new-btn:active {
  filter: brightness(0.95);
}

.story-mobile-new-btn.is-hidden {
  display: none !important;
}

.story-builder-mobile-close {
  display: none;
  position: absolute;
  top: 12px;
  right: 12px;
  width: 36px;
  height: 36px;
  padding: 0;
  border: 0;
  border-radius: 10px;
  background: rgba(255, 255, 255, 0.06);
  color: #e2efff;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  z-index: 10;
  transition: background 0.15s, color 0.15s;
}

.story-builder-mobile-close:hover {
  background: rgba(255, 255, 255, 0.12);
}

@media (max-width: 1024px) {
  /* Hide the builder card by default; reveal only when user clicks
     the mobile button below. */
  .story-builder-card {
    display: none;
  }

  .story-mobile-new-btn {
    display: flex;
  }

  .story-builder-mobile-close {
    display: inline-flex;
  }

  /* Hide the desktop sidebar collapse toggle — it has no meaning
     when the whole card is a modal overlay. */
  #story-builder-toggle {
    display: none;
  }

  /* Overlay state — opens when "สร้างโปรเจ็คใหม่" button clicked.
     Slides over the workspace so the user can fill the form without
     losing their place in the project list. */
  .page.page-story.is-mobile-builder-open .story-builder-card {
    display: flex;
    flex-direction: column;
    position: fixed;
    top: 60px;
    left: 0;
    right: 0;
    bottom: 0;
    width: 100%;
    z-index: 5000;
    margin: 0;
    border-radius: 0;
    border: 0;
    overflow-y: auto;
  }

  .page.page-story.is-mobile-builder-open::before {
    content: "";
    position: fixed;
    inset: 60px 0 0 0;
    background: rgba(0, 0, 0, 0.55);
    backdrop-filter: blur(4px);
    z-index: 4900;
  }

  /* Lock the underlying workspace scroll while overlay is open. */
  body.story-mobile-builder-open {
    overflow: hidden;
  }

  .page.page-story,
  .page.page-story.is-builder-collapsed {
    width: 100%;
    padding-left: 14px;
    padding-right: 14px;
    margin-left: 0;
    margin-right: 0;
  }
}
