/* Story Builder - Element Styles */

/* Base element container */
.element {
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  transform-origin: center center;
  outline: none;
  cursor: move;
  user-select: none;
}

.element:focus {
  outline: none;
}

.element.selected {
  /* Selection should not change z-index, only add visual border */
}

.element.selected::before {
  content: '';
  position: absolute;
  inset: -2px;
  border: 2px solid var(--accent);
  border-radius: 12px;
  pointer-events: none;
}

/* Element content wrapper */
/* This wrapper handles vertical alignment using flexbox */
.element-content {
  width: 100%;
  height: 100%;
  display: flex;
  align-items: center; /* default vertical alignment (will be overridden by inline styles) */
  justify-content: center; /* default horizontal alignment (will be overridden by inline styles) */
  position: relative;
}

/* Text elements */
.text-element {
  min-width: 100px;
  min-height: 40px;
}

.text-inner {
  width: 100%;
  height: 100%;
  padding: 3% 4%;
  border: none;
  border-radius: 0;
  background: rgba(255,255,255,.95);
  -webkit-backdrop-filter: blur(4px);
  backdrop-filter: blur(4px);
  /* Use flex layout for vertical alignment */
  display: flex;
  flex-direction: column;
  /* justify-content set via inline styles for vertical alignment */
  overflow: visible;
  cursor: grab !important;
  position: relative;
  z-index: 1;
  pointer-events: auto;
}

/* When editing, maintain flex layout for vertical alignment */
.text-inner.editing {
  display: flex;
  flex-direction: column;
  /* justify-content set via inline styles for vertical alignment */
  padding: 3% 4%;
  /* Existing editing styles */
  border-color: var(--accent);
  background: white;
  outline: none;
  cursor: text !important;
  user-select: text;
}

/* Text styling options */
.text-inner.no-shadow {
  box-shadow: none;
}

.text-inner.rounded {
  border-radius: 10px;
}

.text-inner.no-blur {
  backdrop-filter: none !important;
  -webkit-backdrop-filter: none !important;
  background: transparent !important;
}

/* NEW: text-content wrapper for actual text content */
/* Uses block layout to allow inline formatting (b, i, span, etc.) */
.text-content {
  display: block;
  width: 100%;
  white-space: pre-wrap;
  word-wrap: break-word;
  outline: none;
}

.text-content:focus {
  outline: none;
}

.text-content[contenteditable="true"]:focus {
  outline: none;
}

/* Remove default link styling in text boxes */
.text-content a {
  color: currentColor;      /* Use current text color (respects parent span and global color) */
  text-decoration: none;    /* No underline */
}

.text-content a:visited {
  color: currentColor;      /* No purple visited state */
}

.text-content a:hover {
  color: currentColor;      /* No color change on hover */
  text-decoration: none;    /* Ensure no underline on hover */
}

/* Show pointer cursor for links only in preview mode */
.stage.preview-mode .text-content a {
  cursor: pointer;
}

/* Force underline to inherit color from nested content */
.text-content a u,
.text-content u {
  text-decoration-color: inherit;
}

/* Ensure the underline color cascades properly from innermost span */
.text-content a span u,
.text-content span u {
  color: inherit;
}

/* List styling in text boxes */
.text-content ul,
.text-content ol {
  margin: 0;
  padding-left: 1.5em;
  text-align: left;
}

.text-content li {
  margin: 0.8em 0;
  text-align: left;
}

/* Preserve list formatting during editing */
.text-content.editing ul,
.text-content.editing ol {
  margin: 0;
  padding-left: 1.5em;
}

.text-content.editing li {
  margin: 0.8em 0;
}

/* Image elements */
.image-element {
  min-width: 50px;
  min-height: 50px;
}

.image-inner {
  width: 100%;
  height: 100%;
  object-fit: contain;
  border-radius: 10px;
  box-shadow: 0 2px 8px rgba(0,0,0,.1);
  pointer-events: none;
}

/* Image styling options */
.image-inner.no-shadow {
  box-shadow: none;
}

.image-inner.no-rounded {
  border-radius: 0;
}

/* Embed elements */
.embed-element {
  min-width: 100px;
  min-height: 50px;
}

.embed-element .element-content {
  background: #f5f5f5;
  border: 1px solid #ddd;
  border-radius: 10px;
  box-shadow: 0 2px 8px rgba(0,0,0,.1);
  overflow: hidden;
}

/* Embed styling options */
.embed-element .element-content.no-shadow {
  box-shadow: none;
}

.embed-element .element-content.no-rounded {
  border-radius: 0;
}

.embed-element .element-content.no-rounded .embed-inner {
  border-radius: 0;
}

.embed-inner {
  width: 100%;
  height: 100%;
  border: none;
  border-radius: 10px;
  pointer-events: none; /* Disabled during editing */
}

/* HTML embeds (Instagram, Twitter) */
.embed-inner.embed-html {
  overflow: auto;
  display: flex;
  align-items: center;
  justify-content: center;
}

/* Instagram specific styling */
.embed-html iframe.instagram-media-rendered {
  max-width: 100% !important;
  max-height: 100% !important;
}

/* Twitter specific styling */
.embed-html .twitter-tweet-rendered {
  margin: 0 auto !important;
}

.embed-element.selected .embed-inner {
  pointer-events: none; /* Keep disabled when selected */
}

/* Allow interaction when not in edit mode (could be toggled via class) */
.stage.preview-mode .embed-element .embed-inner {
  pointer-events: auto;
}

/* Diagram-box elements */
.diagram-box-element {
  min-width: 100px;
  min-height: 100px;
}

.diagram-box-inner {
  width: 100%;
  height: 100%;
  border-radius: 0;
  background: transparent;
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
  pointer-events: none;
}

.diagram-box-inner.rounded {
  border-radius: 10px;
}

.diagram-box-placeholder {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 100%;
  height: 100%;
  opacity: 0.4;
}

.diagram-placeholder-icon {
  width: 48px;
  height: 48px;
  filter: grayscale(100%);
  opacity: 0.6;
}

/* Handles for selected elements */
.handles {
  position: absolute;
  inset: 0;
  pointer-events: none;
}

.element.selected .handles {
  pointer-events: auto;
}

/* Hide handles when editing text */
.element.editing-text .handles {
  pointer-events: none;
}

.element.editing-text .handle {
  opacity: 0 !important;
}

.handle {
  position: absolute;
  width: 12px;
  height: 12px;
  border-radius: 50%;
  background: white;
  border: 2px solid var(--accent);
  box-shadow: 0 2px 4px rgba(0,0,0,.2);
  cursor: pointer;
  opacity: 0;
  transition: opacity 0.2s ease;
}

.element.selected .handle {
  opacity: 1;
}

/* Resize handles */
.handle.resize-nw {
  left: -6px;
  top: -6px;
  cursor: nwse-resize;
}

.handle.resize-ne {
  right: -6px;
  top: -6px;
  cursor: nesw-resize;
}

.handle.resize-sw {
  left: -6px;
  bottom: -6px;
  cursor: nesw-resize;
}

.handle.resize-se {
  right: -6px;
  bottom: -6px;
  cursor: nwse-resize;
}

/* Side-middle handles for width-only resizing */
.handle.resize-w {
  left: -6px;
  top: 50%;
  transform: translateY(-50%);
  cursor: ew-resize;
}

.handle.resize-e {
  right: -6px;
  top: 50%;
  transform: translateY(-50%);
  cursor: ew-resize;
}

/* Top-bottom middle handles for height-only resizing */
.handle.resize-n {
  top: -6px;
  left: 50%;
  transform: translateX(-50%);
  cursor: ns-resize;
}

.handle.resize-s {
  bottom: -6px;
  left: 50%;
  transform: translateX(-50%);
  cursor: ns-resize;
}

/* Rotation handle */
.handle.rotate {
  top: -30px;
  left: 50%;
  transform: translateX(-50%);
  width: 14px;
  height: 14px;
  background: var(--accent);
  border: 2px solid white;
  cursor: grab;
}

.handle.rotate:active {
  cursor: grabbing;
}

.handle.rotate::after {
  content: '';
  position: absolute;
  bottom: -16px;
  left: 50%;
  transform: translateX(-50%);
  width: 1px;
  height: 16px;
  background: var(--accent);
  opacity: 0.5;
}

/* Element states */
.element.dragging {
  cursor: grabbing;
}

.element.rotating {
  cursor: grabbing;
}

/* Text formatting states */
.text-bold {
  font-weight: 700;
}

.text-italic {
  font-style: italic;
}

.text-align-left {
  text-align: left;
  justify-content: flex-start;
}

.text-align-center {
  text-align: center;
  justify-content: center;
}

.text-align-right {
  text-align: right;
  justify-content: flex-end;
}

/* Ghost element for dragging */
.element-ghost {
  position: fixed;
  pointer-events: none;
  opacity: 0.5;
  z-index: 10000;
}

/* Placeholder for future content types */
.diagram-element {
  border: 2px dashed var(--accent);
  border-radius: 10px;
  background: rgba(220, 38, 38, 0.05);
  display: flex;
  align-items: center;
  justify-content: center;
  color: var(--accent);
  font-weight: 600;
  font-size: 14px;
}

.diagram-element::before {
  content: 'CLD Diagram';
}