/* ══════════════════════════════════════════
   STORY MODAL
══════════════════════════════════════════ */

.modal-overlay {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, .88);
  z-index: 10000000; /* above the sidebar (9999999) so the modal isn't covered */
  align-items: center;
  justify-content: center;
  padding: 16px;
  backdrop-filter: blur(6px);
  -webkit-backdrop-filter: blur(6px);
}

.modal-overlay.open { display: flex; }

.modal {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 18px;
  padding: 22px;
  width: 100%;
  max-width: 860px;
  display: flex;
  flex-direction: column;
  gap: 14px;
  max-height: 95vh;
  max-height: 95dvh; /* dvh excludes the mobile address bar so content isn't cut off */
  overflow-y: auto;
  overflow-x: hidden;
  box-shadow: var(--shadow-md);
}

.modal-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  flex-shrink: 0;
}

.modal-title { font-size: 14px; font-weight: 700; }

.modal-close {
  background: var(--surface2);
  border: 1px solid var(--border);
  color: var(--muted);
  font-size: 15px;
  cursor: pointer;
  width: 30px;
  height: 30px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background .15s, color .15s;
  line-height: 1;
}

.modal-close:hover { background: var(--surface3); color: var(--text); }

.modal-body {
  display: grid;
  grid-template-columns: minmax(0, 1fr) minmax(0, 1fr);
  gap: 16px;
  align-items: start;
}

/* let the story modal shrink to narrow viewports (flex-item min-width:auto
   would otherwise keep it as wide as its widest content) */
#storyModal .modal { min-width: 0; }

.modal-left { display: flex; flex-direction: column; gap: 12px; min-width: 0; }

.story-preview {
  width: 100%;
  aspect-ratio: 9/16;
  border-radius: 12px;
  overflow: hidden;
  background: repeating-conic-gradient(#222 0% 25%, #1a1a1a 0% 50%) 0 0/20px 20px;
  border: 1px solid var(--border);
}

.story-preview canvas { width: 100%; height: 100%; display: block; touch-action: none; }

.story-right { display: flex; flex-direction: column; gap: 10px; min-width: 0; }

/* Desktop: fixed header (close ✕) + fixed preview/actions on the right; only the
   options column on the left scrolls — so Download is always reachable. */
@media (min-width: 701px) {
  #storyModal .modal { overflow: hidden; }
  #storyModal .modal-body { flex: 1; min-height: 0; align-items: stretch; }
  #storyModal .modal-left { overflow-y: auto; min-height: 0; padding-right: 8px; }
  /* right column never scrolls — the preview shrinks to fill the space left
     over by the editor + action buttons */
  #storyModal .story-right { min-height: 0; overflow: hidden; }
  #storyModal .story-preview { flex: 1 1 0; min-height: 0; align-self: center; width: auto; height: auto; aspect-ratio: 9 / 16; margin: 0 auto; max-width: 100%; }
  #storyModal .story-editor { flex: 0 0 auto; }
  #storyModal .modal-actions { flex: 0 0 auto; padding-top: 4px; }
}
.story-editor {
  display: flex; flex-direction: column; gap: 8px;
  background: var(--surface2); border: 1px solid var(--border);
  border-radius: 8px; padding: 10px 12px;
}
.story-toolbar { display: flex; align-items: center; flex-wrap: wrap; gap: 6px; }
.story-toolbar button[data-act] {
  background: var(--surface3); border: 1px solid var(--border); color: var(--text);
  font-size: 13px; font-weight: 700; min-width: 38px; padding: 6px 10px;
  border-radius: 7px; cursor: pointer; transition: background .15s, border-color .15s;
}
.story-toolbar button[data-act]:hover { background: var(--orange); border-color: var(--orange); color: #fff; }
.story-toolbar button[data-act]:active { transform: scale(.95); }
.story-hinttext { font-size: 9px; color: var(--muted); line-height: 1.3; }

/* right-click menu for custom-layout elements */
.ctx-menu {
  display: none; position: fixed; z-index: 10000002;
  background: var(--surface); border: 1px solid var(--border);
  border-radius: 10px; padding: 5px; min-width: 150px;
  box-shadow: var(--shadow-md);
}
.ctx-menu button {
  display: block; width: 100%; text-align: left;
  background: none; border: none; color: var(--text);
  font-size: 13px; padding: 8px 12px; border-radius: 6px; cursor: pointer;
}
.ctx-menu button:hover { background: var(--surface3); }

.modal-actions { display: flex; gap: 10px; flex-shrink: 0; }
.modal-actions button { flex: 1; padding: 10px; }

.picker-label {
  font-size: 10px;
  font-weight: 700;
  letter-spacing: .07em;
  text-transform: uppercase;
  color: var(--muted);
  margin-bottom: 7px;
}

.color-swatch {
  width: 30px;
  height: 30px;
  border-radius: 50%;
  cursor: pointer;
  border: 2px solid transparent;
  transition: transform .15s, border-color .15s, box-shadow .15s;
  flex-shrink: 0;
}

.color-swatch:hover { transform: scale(1.12); }
.color-swatch.active { border-color: var(--orange); transform: scale(1.12); box-shadow: 0 0 0 2px var(--orange-dim); }

.layout-grid {
  display: grid;
  grid-template-columns: repeat(5, minmax(0, 1fr));
  gap: 6px;
}

.layout-btn {
  background: var(--surface2);
  border: 2px solid var(--border);
  border-radius: 6px;
  cursor: pointer;
  padding: 0;
  overflow: hidden;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 3px;
  padding-bottom: 5px;
  transition: border-color .15s;
}

.layout-btn:hover { border-color: var(--muted); }
.layout-btn.active { border-color: var(--orange); }
.layout-btn canvas { width: 100%; display: block; }

.layout-btn span {
  font-size: 9px;
  font-weight: 700;
  letter-spacing: .04em;
  color: var(--muted);
  text-transform: uppercase;
}
