/* Story Builder - Main Layout Styles */

:root {
  --bg: #f5f5f7;
  --panel: #ffffff;
  --muted: #eef0f3;
  --ink: #0f172a;
  --ink-2: #64748b;
  --accent: #dc2626;
  --accent-dark: #b91c1c;
  --accent-light: #ef4444;
  --shadow: 0 8px 20px rgba(0,0,0,.08);
  --radius: 12px;
  --stage-shadow: 0 10px 26px rgba(0,0,0,.12), 0 2px 6px rgba(0,0,0,.06);
  --stage-border: 1px solid rgba(0,0,0,.08);
  --handle: #0f172a;
}

* {
  box-sizing: border-box;
}

html, body {
  height: 100%;
  margin: 0;
  padding: 0;
}

body {
  font-family: -apple-system, system-ui, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  background: var(--bg);
  color: var(--ink);
  overflow: hidden;
}

/* Main App Grid Layout */
.app {
  display: grid;
  grid-template-columns: 240px 1fr;
  grid-template-rows: minmax(64px, 64px) minmax(200px, 1fr) minmax(60px, 60px);
  gap: 0;
  width: 100vw;
  height: 100vh;
  min-height: 400px; /* Ensure minimum usable height */
}

/* Back to Modules Button */
.back-to-birdseye {
  position: fixed;
  top: 20px;
  left: 20px;
  background: var(--panel);
  border: 2px solid var(--accent);
  color: var(--accent);
  padding: 8px 16px;
  border-radius: 24px;
  font-weight: 600;
  font-size: 14px;
  text-decoration: none;
  transition: all 0.3s ease;
  z-index: 1000;
}

.back-to-birdseye:hover {
  background: var(--accent);
  color: var(--panel);
  transform: translateY(-2px);
  box-shadow: var(--shadow);
}

/* Sidebar */
.sidebar {
  grid-row: 1 / span 3;
  background: var(--muted);
  padding: 80px 12px 16px;
  border-right: 1px solid #e5e7eb;
  display: flex;
  flex-direction: column;
  min-width: 240px;
}

.thumbs {
  display: flex;
  flex-direction: column;
  gap: 12px;
  flex: 1;
  overflow-y: auto;
  overflow-x: hidden;
  padding: 4px;
}

.thumbs::-webkit-scrollbar {
  width: 6px;
}

.thumbs::-webkit-scrollbar-track {
  background: transparent;
}

.thumbs::-webkit-scrollbar-thumb {
  background: rgba(0,0,0,.2);
  border-radius: 3px;
}

.add-slide {
  margin-top: 12px;
  height: 44px;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: var(--radius);
  background: var(--panel);
  border: 2px dashed #c9ccd3;
  color: var(--ink-2);
  cursor: pointer;
  font-weight: 600;
  font-size: 14px;
  transition: all 0.2s ease;
}

.add-slide:hover {
  border-color: var(--accent);
  color: var(--accent);
  transform: translateY(-1px);
  box-shadow: 0 4px 12px rgba(220, 38, 38, 0.15);
}

/* Top Toolbar */
.topbar {
  grid-column: 2;
  grid-row: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 0 20px;
  background: var(--panel);
  border-bottom: 1px solid #e5e7eb;
  position: relative;
}

.topbar-inner {
  display: flex;
  align-items: center;
  justify-content: space-between;
  width: 100%;
  max-width: var(--stage-width, 1200px);
  position: relative;
}

.topbar > .toolbar-group {
  position: absolute;
  left: 20px;
}

.topbar > .toolbar-group:last-child {
  position: absolute;
  right: 20px;
  left: auto;
}

.toolbar-group {
  display: flex;
  align-items: center;
  gap: 8px;
}

.tool-btn {
  width: 40px;
  height: 40px;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 10px;
  background: var(--panel);
  border: 1px solid #e5e7eb;
  cursor: pointer;
  transition: all 0.2s ease;
}

.tool-btn:hover {
  background: var(--muted);
  transform: translateY(-1px);
  box-shadow: 0 2px 8px rgba(0,0,0,.08);
}

.icon-text {
  font-size: 20px;
  font-weight: 700;
  color: var(--ink);
}

.icon-image {
  width: 20px;
  height: 20px;
  fill: var(--ink);
}

.story-title {
  flex: 0 0 auto;
  width: 300px;
  max-width: 100%;
  margin: 0 20px;
}

.story-title input {
  width: 100%;
  padding: 8px 12px;
  border: 1px solid transparent;
  border-radius: 8px;
  background: var(--muted);
  font-size: 16px;
  font-weight: 600;
  text-align: center;
  transition: all 0.2s ease;
}

.story-title input:focus {
  outline: none;
  border-color: var(--accent);
  background: var(--panel);
}

.control-btn {
  padding: 8px 16px;
  border-radius: 8px;
  background: var(--panel);
  border: 1px solid #e5e7eb;
  color: var(--ink);
  font-weight: 600;
  font-size: 14px;
  cursor: pointer;
  transition: all 0.2s ease;
}

.control-btn:hover {
  background: var(--muted);
  transform: translateY(-1px);
  box-shadow: 0 2px 8px rgba(0,0,0,.08);
}

.control-btn.preview {
  display: flex;
  align-items: center;
  position: relative;
  padding: 8px 4px 8px 16px;
}

.preview-text {
  /* Natural width */
}

.preview-separator {
  width: 1px;
  height: 20px;
  background: rgba(0, 0, 0, 0.15);
  margin: 0 4px 0 12px;
}

.preview-dropdown {
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  padding: 4px 2px 4px 4px;
  border-radius: 4px;
  transition: background-color 0.2s;
}

.preview-dropdown:hover {
  background: rgba(0, 0, 0, 0.08);
}

.control-btn.publish {
  background: var(--accent);
  color: var(--panel);
  border-color: var(--accent);
  display: flex;
  align-items: center;
  position: relative;
  padding: 8px 4px 8px 16px;
}

.control-btn.publish:hover {
  background: var(--accent-dark);
  border-color: var(--accent-dark);
}

.publish-text {
  /* Remove flex: 1 to allow natural width */
}

.publish-separator {
  width: 1px;
  height: 20px;
  background: rgba(255, 255, 255, 0.3);
  margin: 0 4px 0 12px;
}

.publish-dropdown {
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  padding: 4px 2px 4px 4px;
  border-radius: 4px;
  transition: background-color 0.2s;
}

.publish-dropdown:hover {
  background: rgba(255, 255, 255, 0.1);
}

/* Main Stage Area */
.stage-wrap {
  grid-column: 2;
  grid-row: 2;
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
  min-width: 300px;
  background: var(--bg);
}

/* Bottom Bar */
.bottombar {
  grid-column: 2;
  grid-row: 3;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 20px;
  background: var(--panel);
  border-top: 1px solid #e5e7eb;
  position: relative;
}

.ar-controls {
  display: flex;
  gap: 12px;
}

.ar-btn {
  padding: 8px 12px;
  border-radius: 8px;
  background: var(--panel);
  border: 2px solid #d1d5db;
  cursor: pointer;
  transition: all 0.2s ease;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 4px;
}

.ar-btn:hover {
  border-color: var(--ink-2);
  transform: translateY(-1px);
}

.ar-btn.active {
  border-color: var(--accent);
  background: var(--muted);
}

.ar-icon {
  display: inline-block;
  background: #d1d5db;
  border-radius: 2px;
}

.ar-921 {
  width: 16px;
  height: 38px;
}

.ar-916 {
  width: 18px;
  height: 32px;
}

.ar-11 {
  width: 24px;
  height: 24px;
}

.ar-169 {
  width: 32px;
  height: 18px;
}

.ar-219 {
  width: 38px;
  height: 16px;
}

.ar-label {
  font-size: 9px;
  color: var(--ink-2);
  font-weight: 500;
  line-height: 1;
}

.ar-btn.active .ar-icon {
  background: var(--accent);
}

.ar-btn.active .ar-label {
  color: var(--accent);
}

.slide-nav {
  position: absolute;
  left: 50%;
  transform: translateX(-50%);
  display: flex;
  align-items: center;
  gap: 16px;
  background: var(--panel);
  padding: 4px 12px;
  border-radius: 20px;
  box-shadow: 0 2px 8px rgba(0,0,0,.08);
}

.nav-btn {
  width: 32px;
  height: 32px;
  border-radius: 6px;
  background: var(--panel);
  border: 1px solid #d1d5db;
  color: var(--ink);
  cursor: pointer;
  transition: all 0.2s ease;
  font-size: 16px;
  font-weight: 600;
}

.nav-btn:not(:disabled):hover {
  background: var(--muted);
  border-color: var(--ink-2);
}

.nav-btn:disabled {
  opacity: 0.4;
  cursor: not-allowed;
}

#slideIndicator {
  font-size: 14px;
  font-weight: 600;
  color: var(--ink-2);
  min-width: 80px;
  text-align: center;
}

/* Responsive adjustments */
@media (max-width: 1024px) {
  .app {
    grid-template-columns: 200px 1fr;
  }
  
  .sidebar {
    min-width: 200px;
  }
}

@media (max-width: 820px) {
  .app {
    grid-template-columns: 80px 1fr;
  }
  
  .sidebar {
    min-width: 80px;
    padding: 80px 8px 16px;
  }
  
  .add-slide {
    font-size: 0;
  }
  
  .add-slide::before {
    content: '+';
    font-size: 20px;
  }
}

/* Handle very small viewport heights */
@media (max-height: 500px) {
  .app {
    grid-template-rows: minmax(50px, 50px) minmax(150px, 1fr) minmax(50px, 50px);
    min-height: 300px;
  }

  .topbar {
    padding: 0 12px;
  }

  .bottombar {
    padding: 0 12px;
  }

  .story-title {
    width: 240px;
  }
}

/* Preview mode styles */
.app.preview-mode .sidebar {
  display: none;
}

.app.preview-mode .topbar {
  display: none;
}

.app.preview-mode .main {
  grid-column: 1 / -1;
  margin-left: 0;
  padding: 0;
}

.app.preview-mode .stage-wrap {
  padding: 20px;
  background: #1a1a1a;
}

.app.preview-mode .footer {
  grid-column: 1 / -1;
}

/* Exit preview button */
.exit-preview-btn {
  position: fixed;
  top: 20px;
  right: 20px;
  z-index: 10001;
  padding: 10px 20px;
  background: rgba(0, 0, 0, 0.8);
  color: white;
  border: 1px solid rgba(255, 255, 255, 0.2);
  border-radius: 8px;
  cursor: pointer;
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 14px;
  font-weight: 500;
  -webkit-backdrop-filter: blur(10px);
  backdrop-filter: blur(10px);
  transition: all 0.2s;
}

.exit-preview-btn:hover {
  background: rgba(0, 0, 0, 0.9);
  border-color: rgba(255, 255, 255, 0.3);
  transform: translateY(-1px);
}

/* Disable element interactions in preview mode */
.app.preview-mode .element {
  cursor: default !important;
  pointer-events: none;
}

.app.preview-mode .element-content {
  pointer-events: auto;
}

.app.preview-mode .element::before {
  display: none !important;
}

.app.preview-mode .handles {
  display: none !important;
}

/* Enable embed interaction in preview mode */
.app.preview-mode .embed-inner {
  pointer-events: auto !important;
}

/* Embed dialog */
.embed-dialog {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,.5);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 10000;
  -webkit-backdrop-filter: blur(4px);
  backdrop-filter: blur(4px);
}

.embed-dialog .dialog-content {
  background: white;
  border-radius: 12px;
  padding: 24px;
  max-width: 600px;
  width: 90%;
  box-shadow: 0 10px 40px rgba(0,0,0,.2);
}

.embed-dialog h3 {
  margin: 0 0 12px 0;
  font-size: 20px;
  font-weight: 600;
}

.embed-dialog p {
  margin: 0 0 16px 0;
  color: #666;
  font-size: 14px;
}

.embed-dialog textarea {
  width: 100%;
  min-height: 120px;
  padding: 12px;
  border: 1px solid #ddd;
  border-radius: 8px;
  font-family: 'Monaco', 'Menlo', monospace;
  font-size: 12px;
  resize: vertical;
}

.embed-dialog textarea:focus {
  outline: none;
  border-color: var(--accent);
}

.embed-dialog .dialog-actions {
  display: flex;
  gap: 12px;
  justify-content: flex-end;
  margin-top: 20px;
}

.embed-dialog button {
  padding: 8px 16px;
  border-radius: 8px;
  border: none;
  font-size: 14px;
  font-weight: 500;
  cursor: pointer;
  transition: all 0.2s;
}

.embed-dialog .btn-secondary {
  background: #f0f0f0;
  color: #666;
}

.embed-dialog .btn-secondary:hover {
  background: #e0e0e0;
}

.embed-dialog .btn-primary {
  background: var(--accent);
  color: white;
}

.embed-dialog .btn-primary:hover {
  background: #b91c1c;
}

/* Publish Overlay Styles */
.publish-overlay {
  position: fixed;
  top: 0;
  left: 0;
  width: 100vw;
  height: 100vh;
  background: rgba(0, 0, 0, 0.5);
  display: flex;
  justify-content: center;
  align-items: center;
  z-index: 10000;
  overflow-y: auto;
  padding: 20px;
}

.publish-container {
  background: white;
  padding: 30px;
  border-radius: 12px;
  box-shadow: 0 10px 40px rgba(0, 0, 0, 0.2);
  max-width: 650px;
  width: 100%;
  max-height: 90vh;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.publish-container h2 {
  margin: 0 0 10px 0;
  color: #333;
  font-size: 24px;
}

.publish-container h3 {
  margin: 20px 0 10px 0;
  color: #333;
  font-size: 16px;
  font-weight: 600;
  border-bottom: 1px solid #eee;
  padding-bottom: 5px;
}

.publish-container > p {
  margin: 0 0 25px 0;
  color: #666;
  font-size: 14px;
}

.publish-form {
  display: flex;
  flex-direction: column;
  gap: 15px;
}

.scrollable-form {
  overflow-y: auto;
  flex: 1;
  margin-bottom: 20px;
  padding-right: 10px;
}

.form-section {
  margin-bottom: 10px;
}

.form-group {
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.form-row {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 15px;
}

.form-group label {
  font-weight: 600;
  color: #333;
  font-size: 14px;
}

.form-group input[type="text"],
.form-group input[type="number"],
.form-group select {
  padding: 10px 12px;
  border: 1px solid #ddd;
  border-radius: 6px;
  font-size: 14px;
  transition: border-color 0.2s;
}

.form-group input[type="color"] {
  height: 40px;
  border: 1px solid #ddd;
  border-radius: 6px;
  cursor: pointer;
}

.form-group input:focus,
.form-group select:focus {
  outline: none;
  border-color: var(--accent);
  box-shadow: 0 0 0 3px rgba(220, 38, 38, 0.1);
}

.form-group small {
  color: #888;
  font-size: 12px;
}

.radio-group {
  display: flex;
  gap: 20px;
}

.radio-label {
  display: flex;
  align-items: center;
  gap: 8px;
  cursor: pointer;
  font-weight: normal;
}

.radio-label input[type="radio"] {
  cursor: pointer;
}

.checkbox-label {
  display: flex;
  align-items: center;
  gap: 8px;
  cursor: pointer;
  font-weight: normal;
}

.checkbox-label input[type="checkbox"] {
  cursor: pointer;
}

.button-group {
  display: flex;
  gap: 12px;
  margin-top: 0;
  padding-top: 20px;
  border-top: 1px solid #eee;
  flex-shrink: 0;
}

.button-group button {
  flex: 1;
  padding: 12px 20px;
  border: none;
  border-radius: 6px;
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.2s;
}

.cancel-btn {
  background: #f0f0f0;
  color: #333;
}

.cancel-btn:hover {
  background: #e0e0e0;
}

.publish-btn {
  background: var(--accent);
  color: white;
}

.publish-btn:hover {
  background: var(--accent-dark);
}

.publish-btn:disabled {
  background: #ccc;
  cursor: not-allowed;
}

.secondary-btn {
  background: #6c757d;
  color: white;
  border: none;
  padding: 10px 20px;
  border-radius: 4px;
  cursor: pointer;
  font-size: 14px;
}

.secondary-btn:hover {
  background: #5a6268;
}

@media (max-height: 400px) {
  .app {
    overflow-y: auto;
    height: auto;
    min-height: 300px;
  }

  body {
    overflow: auto;
  }
}