*,
*::before,
*::after {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

:root {
  --bg: #0f1117;
  --surface: #1a1d27;
  --surface-2: #242836;
  --border: #2e3344;
  --text: #e2e8f0;
  --text-muted: #94a3b8;
  --accent: #6366f1;
  --accent-hover: #818cf8;
  --danger: #f87171;
  --canvas-bg: #12141c;
  --shadow: 0 4px 24px rgba(0, 0, 0, 0.4);
  --radius: 10px;
  --font: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  --mono: "SF Mono", "Fira Code", "Cascadia Code", monospace;
}

[data-theme="light"] {
  --bg: #f1f5f9;
  --surface: #ffffff;
  --surface-2: #f8fafc;
  --border: #e2e8f0;
  --text: #1e293b;
  --text-muted: #64748b;
  --canvas-bg: #e2e8f0;
  --shadow: 0 4px 24px rgba(0, 0, 0, 0.08);
}

html, body {
  height: 100%;
}

body {
  font-family: var(--font);
  background: var(--bg);
  color: var(--text);
  line-height: 1.5;
}

.app {
  display: flex;
  flex-direction: column;
  height: 100vh;
  max-width: 100%;
}

.header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 1rem 1.5rem;
  border-bottom: 1px solid var(--border);
  background: var(--surface);
  flex-shrink: 0;
}

.header h1 {
  font-size: 1.25rem;
  font-weight: 700;
  letter-spacing: -0.02em;
}

.subtitle {
  font-size: 0.85rem;
  color: var(--text-muted);
  margin-top: 0.15rem;
}

.subtitle code {
  font-family: var(--mono);
  font-size: 0.8rem;
  background: var(--surface-2);
  padding: 0.1em 0.35em;
  border-radius: 4px;
}

.header-actions {
  display: flex;
  gap: 0.5rem;
  align-items: flex-end;
}

.ai-cta-wrap {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 0.15rem;
}

.btn {
  font-family: var(--font);
  font-size: 0.875rem;
  font-weight: 500;
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 0.45rem 0.85rem;
  cursor: pointer;
  background: var(--surface-2);
  color: var(--text);
  transition: background 0.15s, border-color 0.15s, transform 0.1s;
}

.btn:hover {
  background: var(--border);
}

.btn:active {
  transform: scale(0.97);
}

.btn-ghost {
  background: transparent;
}

.btn-ghost:hover {
  background: var(--surface-2);
}

a.btn {
  text-decoration: none;
  display: inline-flex;
  align-items: center;
}

.btn-primary {
  background: var(--accent);
  border-color: var(--accent);
  color: #fff;
  padding: 0.45rem 1rem;
}

.btn-primary:hover {
  background: var(--accent-hover);
  border-color: var(--accent-hover);
}

/* AI CTA — must come after .btn so red styles are not overridden */
.btn.btn-ai-cta {
  background: #dc2626;
  border-color: #dc2626;
  color: #fff;
  font-weight: 600;
  font-size: 0.78rem;
  padding: 0.5rem 0.7rem;
  line-height: 1.2;
  text-align: center;
  white-space: nowrap;
}

.btn.btn-ai-cta:hover {
  background: #b91c1c;
  border-color: #b91c1c;
  color: #fff;
}

.header-actions .ai-cta-partner {
  display: block;
  font-size: 0.625rem;
  font-weight: 400;
  color: var(--text-muted);
  line-height: 1.15;
  text-align: center;
  letter-spacing: 0.01em;
  opacity: 0.85;
}

.btn-sm {
  font-size: 0.8rem;
  padding: 0.35rem 0.65rem;
}

.btn-icon {
  padding: 0.45rem;
  display: inline-flex;
  align-items: center;
  justify-content: center;
}

.btn-icon svg {
  width: 18px;
  height: 18px;
}

.hidden {
  display: none !important;
}

.btn:disabled {
  opacity: 0.4;
  cursor: not-allowed;
  pointer-events: none;
}

.canvas-empty {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  text-align: center;
  padding: 2rem;
  pointer-events: none;
  z-index: 1;
}

.canvas-empty.hidden {
  display: none;
}

.canvas.drag-over {
  outline: 2px dashed var(--accent);
  outline-offset: -4px;
  background: color-mix(in srgb, var(--accent) 6%, var(--canvas-bg));
}

.empty-icon {
  width: 56px;
  height: 56px;
  color: var(--text-muted);
  margin-bottom: 1rem;
  opacity: 0.6;
}

.empty-title {
  font-size: 1.05rem;
  font-weight: 600;
  margin-bottom: 0.4rem;
}

.empty-hint {
  font-size: 0.85rem;
  color: var(--text-muted);
  max-width: 420px;
}

.empty-hint code {
  font-family: var(--mono);
  font-size: 0.78rem;
  background: var(--surface);
  padding: 0.1em 0.35em;
  border-radius: 4px;
}

.diagram-container {
  width: 100%;
  height: 100%;
  position: relative;
  z-index: 2;
}

.editor-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 0.4rem;
  flex-shrink: 0;
}

.format-select {
  font-family: var(--font);
  font-size: 0.8rem;
  padding: 0.35rem 0.5rem;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--surface-2);
  color: var(--text);
  cursor: pointer;
  outline: none;
}

.format-select:focus {
  border-color: var(--accent);
}

.editor-actions {
  display: flex;
  align-items: center;
  gap: 0.5rem;
}

.file-name {
  font-size: 0.8rem;
  font-weight: 600;
  color: var(--text-muted);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  flex: 1;
  min-width: 0;
  margin-right: 1rem;
}

#source-editor {
  flex: 1;
  min-height: 100px;
  font-family: var(--mono);
  font-size: 0.78rem;
  line-height: 1.5;
  background: var(--canvas-bg);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 0.6rem 0.75rem;
  resize: vertical;
  outline: none;
}

#source-editor:focus {
  border-color: var(--accent);
}

.main-content {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-height: 0;
  overflow: hidden;
}

.main-content.editor-collapsed .viewer-panel {
  flex: 1 1 auto;
}

.editor-panel {
  display: flex;
  flex-direction: column;
  width: 100%;
  flex: 0 0 auto;
  max-height: min(240px, 35vh);
  border-top: 1px solid var(--border);
  background: var(--surface);
  padding: 0.6rem 1rem 0.75rem;
  min-height: 0;
  transition: max-height 0.2s ease, padding 0.2s ease, opacity 0.2s ease;
}

.editor-panel.collapsed {
  display: none;
  max-height: 0;
  padding: 0;
  border: none;
  overflow: hidden;
}

.viewer-panel {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-height: 0;
  min-width: 0;
  width: 100%;
  overflow: hidden;
  position: relative;
}

.toolbar {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  padding: 0.5rem 1rem;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}

.toolbar-group {
  display: flex;
  align-items: center;
  gap: 0.35rem;
}

.toolbar-right {
  margin-left: auto;
}

.zoom-level {
  font-size: 0.8rem;
  font-family: var(--mono);
  color: var(--text-muted);
  min-width: 48px;
  text-align: center;
}

.canvas {
  flex: 1;
  position: relative;
  overflow: hidden;
  background: var(--canvas-bg);
  background-image:
    radial-gradient(circle, var(--border) 1px, transparent 1px);
  background-size: 20px 20px;
  cursor: grab;
}

.canvas:active {
  cursor: grabbing;
}

.diagram-container svg {
  max-width: none !important;
  display: block;
}

.has-diagram .diagram-container {
  z-index: 2;
}

.has-diagram .canvas-empty {
  display: none;
}

.error-banner {
  position: absolute;
  bottom: 1rem;
  left: 50%;
  transform: translateX(-50%);
  background: #7f1d1d;
  color: #fecaca;
  padding: 0.65rem 1.25rem;
  border-radius: 8px;
  font-size: 0.85rem;
  max-width: 80%;
  box-shadow: var(--shadow);
  z-index: 10;
}

[data-theme="light"] .error-banner {
  background: #fef2f2;
  color: #b91c1c;
  border: 1px solid #fecaca;
}

.seo-footer {
  border-top: 1px solid var(--border);
  background: var(--surface);
  padding: 0 1.5rem 1rem;
}

.seo-details {
  max-width: 1200px;
  margin: 0 auto;
}

.seo-details summary {
  padding: 0.75rem 0;
  font-size: 0.8rem;
  font-weight: 500;
  color: var(--text-muted);
  cursor: pointer;
  list-style: none;
  user-select: none;
}

.seo-details summary::-webkit-details-marker {
  display: none;
}

.seo-details summary::before {
  content: "▸ ";
  display: inline-block;
  transition: transform 0.15s;
}

.seo-details[open] summary::before {
  transform: rotate(90deg);
}

.seo-details[open] summary {
  margin-bottom: 0.75rem;
  color: var(--text);
}

.seo-content {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 1.25rem;
  padding-bottom: 0.5rem;
}

.seo-footer h2 {
  font-size: 0.85rem;
  font-weight: 600;
  color: var(--text);
  margin-bottom: 0.35rem;
  letter-spacing: -0.01em;
}

.seo-footer p {
  font-size: 0.75rem;
  color: var(--text-muted);
  line-height: 1.55;
}

.seo-footer strong {
  color: var(--text);
  font-weight: 500;
}

@media (max-width: 768px) {
  .editor-panel {
    max-height: 160px;
  }

  .seo-content {
    grid-template-columns: 1fr;
  }

  .header {
    flex-direction: column;
    align-items: flex-start;
    gap: 0.75rem;
  }
}

/* ── Template gallery ─────────────────────────────────────────────────────── */

.templates-app {
  height: auto;
  min-height: 100vh;
}

.templates-toolbar {
  padding: 1rem 1.5rem;
  border-bottom: 1px solid var(--border);
  background: var(--surface);
  display: flex;
  flex-direction: column;
  gap: 0.75rem;
}

.template-search {
  width: 100%;
  max-width: 480px;
  font-family: var(--font);
  font-size: 0.9rem;
  padding: 0.55rem 0.85rem;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--surface-2);
  color: var(--text);
  outline: none;
}

.template-search:focus {
  border-color: var(--accent);
}

.category-filters {
  display: flex;
  flex-wrap: wrap;
  gap: 0.4rem;
}

.filter-pill {
  font-family: var(--font);
  font-size: 0.78rem;
  padding: 0.35rem 0.7rem;
  border: 1px solid var(--border);
  border-radius: 999px;
  background: var(--surface-2);
  color: var(--text-muted);
  cursor: pointer;
  transition: background 0.15s, border-color 0.15s, color 0.15s;
}

.filter-pill:hover {
  border-color: var(--accent);
  color: var(--text);
}

.filter-pill.active {
  background: var(--accent);
  border-color: var(--accent);
  color: #fff;
}

.templates-main {
  flex: 1;
  padding: 1.25rem 1.5rem 2rem;
  max-width: 1200px;
  width: 100%;
  margin: 0 auto;
}

.template-section {
  margin-bottom: 2rem;
}

.section-header h2 {
  font-size: 1rem;
  font-weight: 700;
  margin-bottom: 0.25rem;
}

.section-header p {
  font-size: 0.8rem;
  color: var(--text-muted);
  margin-bottom: 0.85rem;
}

.template-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(260px, 1fr));
  gap: 0.75rem;
}

.template-card {
  text-align: left;
  font-family: var(--font);
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1rem;
  cursor: pointer;
  transition: border-color 0.15s, transform 0.1s, box-shadow 0.15s;
  color: var(--text);
}

.template-card:hover {
  border-color: var(--accent);
  transform: translateY(-2px);
  box-shadow: var(--shadow);
}

.template-card h3 {
  font-size: 0.9rem;
  font-weight: 600;
  margin: 0.5rem 0 0.35rem;
  line-height: 1.3;
}

.template-card p {
  font-size: 0.78rem;
  color: var(--text-muted);
  line-height: 1.45;
  display: -webkit-box;
  -webkit-line-clamp: 3;
  -webkit-box-orient: vertical;
  overflow: hidden;
}

.template-card-cta {
  display: inline-block;
  margin-top: 0.65rem;
  font-size: 0.75rem;
  color: var(--accent);
  font-weight: 500;
}

.template-type-badge {
  display: inline-block;
  font-size: 0.68rem;
  font-family: var(--mono);
  padding: 0.15rem 0.45rem;
  border-radius: 4px;
  background: var(--surface-2);
  color: var(--text-muted);
  border: 1px solid var(--border);
}

.templates-empty {
  text-align: center;
  color: var(--text-muted);
  padding: 3rem 1rem;
}

.template-modal {
  position: fixed;
  inset: 0;
  z-index: 100;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 1rem;
}

.template-modal.hidden {
  display: none;
}

.template-modal-backdrop {
  position: absolute;
  inset: 0;
  background: rgba(0, 0, 0, 0.6);
}

.template-modal-panel {
  position: relative;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  width: 100%;
  max-width: 720px;
  max-height: 90vh;
  overflow-y: auto;
  padding: 1.25rem;
  box-shadow: var(--shadow);
}

.modal-topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.75rem;
  margin: -0.25rem 0 0.5rem;
  position: sticky;
  top: 0;
  z-index: 2;
  background: var(--surface);
  padding: 0.25rem 0 0.5rem;
}

.modal-header {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  flex: 1;
  min-width: 0;
}

.modal-header h2 {
  font-size: 1rem;
  font-weight: 700;
  margin: 0;
  line-height: 1.3;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.modal-desc {
  font-size: 0.85rem;
  color: var(--text-muted);
  margin-bottom: 1rem;
  line-height: 1.45;
}

.modal-close {
  flex-shrink: 0;
  background: none;
  border: none;
  font-size: 1.5rem;
  color: var(--text-muted);
  cursor: pointer;
  line-height: 1;
  padding: 0.25rem 0.35rem;
}

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

.modal-preview {
  background: var(--canvas-bg);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 1rem;
  margin-bottom: 1rem;
  overflow-x: auto;
  min-height: 120px;
  display: flex;
  align-items: center;
  justify-content: center;
}

.modal-preview svg {
  max-width: 100%;
  height: auto;
}

.preview-error {
  font-size: 0.8rem;
  color: var(--text-muted);
}

.modal-code {
  font-family: var(--mono);
  font-size: 0.72rem;
  line-height: 1.5;
  background: var(--canvas-bg);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 0.75rem;
  overflow-x: auto;
  max-height: 200px;
  margin-bottom: 1rem;
  white-space: pre-wrap;
  word-break: break-word;
}

.modal-actions {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  flex-shrink: 0;
}

.ai-search-panel {
  max-width: 520px;
}

.ai-search-input {
  width: 100%;
  font-family: var(--font);
  font-size: 0.9rem;
  padding: 0.65rem 0.85rem;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--surface-2);
  color: var(--text);
  outline: none;
  margin-bottom: 0.25rem;
}

.ai-search-input:focus {
  border-color: var(--accent);
}

.ai-search-actions {
  display: flex;
  justify-content: flex-end;
  gap: 0.5rem;
  margin-top: 1rem;
}

.modal-actions .btn-sm {
  white-space: nowrap;
}

@media (max-width: 768px) {
  .template-grid {
    grid-template-columns: 1fr;
  }

  .templates-toolbar {
    padding: 0.75rem 1rem;
  }

  .templates-main {
    padding: 1rem;
  }
}
