:root {
  /* Palette (Tailwind-ish) */
  --bg-app: #f3f4f6;
  --bg-panel: #ffffff;
  --border: #e5e7eb;

  --primary: #4f46e5;
  /* Indigo-600 */
  --primary-hover: #4338ca;
  /* Indigo-700 */
  --primary-fg: #ffffff;

  --text-main: #111827;
  /* Gray-900 */
  --text-muted: #6b7280;
  /* Gray-500 */

  --danger: #ef4444;
  --danger-bg: #fef2f2;
  --danger-border: #fecaca;

  --font-ui: "Inter", system-ui, -apple-system, sans-serif;
  --font-mono: "JetBrains Mono", "Fira Code", ui-monospace, SFMono-Regular, Menlo, monospace;

  --header-height: 56px;
  --toolbar-height: 48px;
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  font-family: var(--font-ui);
  background: var(--bg-app);
  color: var(--text-main);
  height: 100vh;
  overflow: hidden;
  display: flex;
  flex-direction: column;
  -webkit-font-smoothing: antialiased;
}

/* --- Header --- */
header {
  height: var(--header-height);
  background: var(--bg-panel);
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 calc(16px + env(safe-area-inset-right)) 0 calc(16px + env(safe-area-inset-left));
  padding-top: env(safe-area-inset-top);
  flex-shrink: 0;
  box-shadow: 0 1px 2px 0 rgba(0, 0, 0, 0.05);
  z-index: 20;
}

.brand {
  font-weight: 600;
  font-size: 1rem;
  display: flex;
  align-items: center;
  gap: 10px;
  color: var(--text-main);
}

.brand svg {
  width: 20px;
  height: 20px;
  color: var(--primary);
}

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

/* --- Layout --- */
main {
  flex: 1;
  display: flex;
  overflow: hidden;
}

.panel {
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.editor-panel {
  width: 450px;
  background: var(--bg-panel);
  flex-shrink: 0;
  display: flex;
  flex-direction: column;
  border-right: 1px solid var(--border);
}

.preview-panel {
  flex: 1;
  background: #ffffff;
  position: relative;
  min-width: 0;
  /* Dot Grid Pattern */
  background-image: radial-gradient(#e5e7eb 1px, transparent 1px);
  background-size: 20px 20px;
}

/* --- Resizer Gutter --- */
.gutter {
  width: 8px;
  background: #f9fafb;
  border-left: 1px solid var(--border);
  border-right: 1px solid var(--border);
  cursor: col-resize;
  flex-shrink: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background 0.2s;
  z-index: 10;
  margin-left: -1px;
  /* Overlap border */
}

.gutter:hover,
.gutter.dragging {
  background: #d1d5db;
}

/* Grip handle visual */
.gutter::after {
  content: "";
  width: 2px;
  height: 16px;
  background: #9ca3af;
  border-radius: 1px;
}

/* --- Common UI Elements --- */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  padding: 6px 12px;
  border-radius: 6px;
  font-size: 0.875rem;
  font-weight: 500;
  cursor: pointer;
  transition: all 0.15s ease;
  background: transparent;
  border: 1px solid transparent;
  color: var(--text-main);
  height: 32px;
}

.btn:hover {
  background: #f3f4f6;
}

.btn:active {
  transform: translateY(1px);
}

.btn-primary {
  background: var(--primary);
  color: var(--primary-fg);
  border: 1px solid transparent;
  box-shadow: 0 1px 2px 0 rgba(0, 0, 0, 0.05);
}

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

.btn-outline {
  border: 1px solid var(--border);
  background: white;
}

.btn-outline:hover {
  border-color: #d1d5db;
  background: #f9fafb;
}

.btn-icon {
  padding: 6px;
  width: 32px;
  color: var(--text-muted);
}

.btn-icon:hover {
  color: var(--text-main);
  background: #f3f4f6;
}

select {
  padding: 0 30px 0 10px;
  height: 32px;
  border: 1px solid var(--border);
  border-radius: 6px;
  background-color: white;
  font-size: 0.85rem;
  color: var(--text-main);
  cursor: pointer;
  outline: none;
  /* Custom arrow */
  appearance: none;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' fill='none' viewBox='0 0 24 24' stroke='%236b7280' stroke-width='2'%3E%3Cpath stroke-linecap='round' stroke-linejoin='round' d='M19 9l-7 7-7-7'%3E%3C/path%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: right 8px center;
  background-size: 14px;
}

select:hover {
  border-color: #d1d5db;
}

/* --- Toolbar --- */
.toolbar {
  height: var(--toolbar-height);
  padding: 0 12px;
  border-bottom: 1px solid var(--border);
  display: flex;
  gap: 8px;
  align-items: center;
  justify-content: space-between;
  background: #ffffff;
  flex-shrink: 0;
}

/* --- Quick Actions (Snippets) --- */
.quick-actions {
  padding: 8px 12px;
  border-bottom: 1px solid var(--border);
  background: #f9fafb;
  display: flex;
  gap: 6px;
  overflow-x: auto;
  flex-shrink: 0;
  /* Hide scrollbar */
  scrollbar-width: none;
  -ms-overflow-style: none;
}

.quick-actions::-webkit-scrollbar {
  display: none;
}

.btn-snippet {
  padding: 4px 8px;
  font-size: 0.75rem;
  background: white;
  border: 1px solid #d1d5db;
  border-radius: 4px;
  cursor: pointer;
  color: var(--text-main);
  white-space: nowrap;
  font-family: var(--font-mono);
  display: flex;
  align-items: center;
  transition: all 0.1s;
}

.btn-snippet:hover {
  background: #eff6ff;
  border-color: var(--primary);
  color: var(--primary);
}

.btn-snippet code {
  font-size: 1.1em;
  margin-right: 4px;
  opacity: 0.7;
}

/* --- Editor --- */
.editor-wrapper {
  flex: 1;
  position: relative;
  background: #ffffff;
  overflow: auto;
  /* 允许内容溢出时显示滚动条 */
}

textarea {
  width: 100%;
  height: 100%;
  border: none;
  resize: none;
  padding: 16px;
  font-family: var(--font-mono);
  font-size: 13px;
  line-height: 1.6;
  outline: none;
  background: transparent;
  color: var(--text-main);
}

textarea::placeholder {
  color: #9ca3af;
}

/* --- Preview Area --- */
#preview {
  width: 100%;
  height: 100%;
  overflow: hidden;
  /* Hide scrollbars, let svg-pan-zoom handle it */
  position: relative;
  background: #ffffff;
  /* Keep background pattern */
  background-image: radial-gradient(#e5e7eb 1px, transparent 1px);
  background-size: 20px 20px;
}

/* Ensure SVG takes full space for Pan-Zoom */
#preview svg {
  width: 100%;
  height: 100%;
  display: block;
  /* Remove inline-block spacing */
}

/* Interactive Preview Hints */
#preview svg {
  cursor: default;
}

#preview .node,
#preview .actor,
#preview .task,
#preview .cluster,
#preview .label {
  cursor: pointer;
  transition: opacity 0.2s;
}

#preview .node:hover,
#preview .actor:hover {
  opacity: 0.8;
}

/* --- Feedback/Error --- */
#error-container {
  display: none;
  padding: 12px 16px;
  background: var(--danger-bg);
  border-top: 1px solid var(--danger-border);
  color: #b91c1c;
  font-size: 0.8rem;
  font-family: var(--font-mono);
  white-space: pre-wrap;
  max-height: 150px;
  overflow-y: auto;
}

#error-container.active {
  display: block;
}

/* --- Toast --- */
.toast {
  position: fixed;
  bottom: calc(24px + env(safe-area-inset-bottom));
  left: 50%;
  transform: translateX(-50%) translateY(20px);
  background: #1f2937;
  color: white;
  padding: 10px 20px;
  border-radius: 8px;
  font-size: 0.9rem;
  font-weight: 500;
  opacity: 0;
  transition: all 0.2s cubic-bezier(0.16, 1, 0.3, 1);
  z-index: 1000;
  box-shadow: 0 10px 15px -3px rgba(0, 0, 0, 0.1);
  pointer-events: none;
}

.toast.show {
  transform: translateX(-50%) translateY(0);
  opacity: 1;
}

/* --- Context Menu --- */
.ctx-menu {
  position: fixed;
  z-index: 5000;
  background: var(--bg-panel);
  border: 1px solid var(--border);
  border-radius: 8px;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.15), 0 2px 6px rgba(0, 0, 0, 0.08);
  min-width: 180px;
  padding: 4px 0;
  font-family: var(--font-ui);
  animation: ctx-fade-in 0.12s ease-out;
}

@keyframes ctx-fade-in {
  from { opacity: 0; transform: scale(0.95); }
  to { opacity: 1; transform: scale(1); }
}

.ctx-item {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 14px;
  font-size: 0.84rem;
  color: var(--text-main);
  cursor: pointer;
  border: none;
  background: none;
  width: 100%;
  text-align: left;
  font-family: var(--font-ui);
  transition: background 0.1s;
}

.ctx-item:hover {
  background: #f3f4f6;
}

.ctx-item svg {
  width: 16px;
  height: 16px;
  flex-shrink: 0;
  color: var(--text-muted);
}

.ctx-item:hover svg {
  color: var(--primary);
}

.ctx-item.danger {
  color: var(--danger);
}

.ctx-item.danger svg {
  color: var(--danger);
}

.ctx-item.danger:hover {
  background: var(--danger-bg);
}

.ctx-sep {
  height: 1px;
  background: var(--border);
  margin: 4px 0;
}

.ctx-label {
  padding: 4px 14px 2px;
  font-size: 0.7rem;
  color: var(--text-muted);
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.05em;
}

/* --- Node Dialog --- */
.node-dialog {
  position: fixed;
  z-index: 5001;
  background: var(--bg-panel);
  border: 1px solid var(--border);
  border-radius: 10px;
  box-shadow: 0 12px 32px rgba(0, 0, 0, 0.18);
  padding: 16px;
  min-width: 240px;
  font-family: var(--font-ui);
  animation: ctx-fade-in 0.15s ease-out;
}

.node-dialog-title {
  font-size: 0.85rem;
  font-weight: 600;
  color: var(--text-main);
  margin-bottom: 10px;
}

.node-dialog-input {
  width: 100%;
  padding: 8px 10px;
  border: 1px solid var(--border);
  border-radius: 6px;
  font-size: 0.9rem;
  font-family: var(--font-ui);
  outline: none;
  transition: border-color 0.15s;
  box-sizing: border-box;
}

.node-dialog-input:focus {
  border-color: var(--primary);
  box-shadow: 0 0 0 2px rgba(79, 70, 229, 0.15);
}

.node-dialog-shapes {
  display: flex;
  gap: 6px;
  margin: 10px 0;
}

.shape-btn {
  width: 40px;
  height: 32px;
  display: flex;
  align-items: center;
  justify-content: center;
  border: 1.5px solid var(--border);
  border-radius: 6px;
  background: white;
  cursor: pointer;
  color: var(--text-muted);
  transition: all 0.15s;
}

.shape-btn:hover {
  border-color: var(--primary);
  color: var(--primary);
  background: #f0f0ff;
}

.shape-btn.active {
  border-color: var(--primary);
  background: #eff0ff;
  color: var(--primary);
}

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

.btn-sm {
  height: 30px;
  padding: 4px 14px;
  font-size: 0.8rem;
}

/* --- Mobile Tab Bar (hidden on desktop) --- */
.mobile-tabs {
  display: none;
}

/* --- Mobile --- */
@media (max-width: 768px) {
  :root {
    --mobile-tab-height: 52px;
  }

  header {
    padding: 0 10px;
    height: 48px;
    gap: 4px;
  }

  .brand span {
    display: none;
  }

  /* Compact header actions */
  .actions {
    gap: 6px;
  }

  .actions .btn {
    height: 36px;
    padding: 6px 10px;
    font-size: 0.8rem;
  }

  .actions #btnPresent {
    display: none;
  }

  /* Share button: icon only on mobile */
  .actions #btnCopy span {
    display: none;
  }

  /* Mobile Tab Bar */
  .mobile-tabs {
    display: flex;
    background: var(--bg-panel);
    border-bottom: 2px solid var(--border);
    height: var(--mobile-tab-height);
    flex-shrink: 0;
    z-index: 15;
  }

  .mobile-tab {
    flex: 1;
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 6px;
    background: none;
    border: none;
    font-size: 0.85rem;
    font-weight: 500;
    color: var(--text-muted);
    cursor: pointer;
    position: relative;
    transition: color 0.2s;
    font-family: var(--font-ui);
    -webkit-tap-highlight-color: transparent;
  }

  .mobile-tab.active {
    color: var(--primary);
  }

  .mobile-tab.active::after {
    content: "";
    position: absolute;
    bottom: -2px;
    left: 20%;
    right: 20%;
    height: 2px;
    background: var(--primary);
    border-radius: 2px 2px 0 0;
  }

  /* Layout: stack vertically, tabs control visibility */
  main {
    flex-direction: column;
    flex: 1;
    overflow: hidden;
  }

  .editor-panel {
    width: 100% !important;
    flex: 1;
    border-right: none;
    border-bottom: none;
    min-height: 0;
  }

  .preview-panel {
    flex: 1;
    min-height: 0;
    padding-bottom: env(safe-area-inset-bottom);
  }

  /* Tab switching: hide inactive panel */
  body.mobile-tab-code .preview-panel {
    display: none !important;
  }

  body.mobile-tab-preview .editor-panel {
    display: none !important;
  }

  .gutter {
    display: none;
  }

  /* Toolbar: tighter on mobile */
  .toolbar {
    padding: 0 8px;
    height: 40px;
    gap: 6px;
  }

  .toolbar select {
    font-size: 0.8rem;
    height: 30px;
  }

  .toolbar #template {
    width: 110px !important;
  }

  /* Snippet bar: scrollable, compact */
  .quick-actions {
    padding: 6px 8px;
    gap: 4px;
  }

  .btn-snippet {
    padding: 6px 10px;
    font-size: 0.7rem;
    min-height: 30px;
  }

  /* Toast above safe area */
  .toast {
    bottom: calc(16px + env(safe-area-inset-bottom));
  }
}

/* --- Presentation Mode --- */
body.presentation-mode header {
  display: none !important;
}

body.presentation-mode .mobile-tabs {
  display: none !important;
}

body.presentation-mode .editor-panel {
  display: none !important;
}

body.presentation-mode .gutter {
  display: none !important;
}

body.presentation-mode .preview-panel {
  position: fixed;
  top: 0;
  left: 0;
  width: 100vw;
  height: 100vh;
  z-index: 9000;
  background: white;
  margin: 0;
  border: none;
}

body.presentation-mode #btnExitPresent {
  display: flex !important;
}

/* Hide default cursor in presentation mode */
body.presentation-mode {
  cursor: none !important;
}

/* Also hide cursor on interactive elements to ensure laser is dominant */
body.presentation-mode * {
  cursor: none !important;
}

/* Except the exit button, so user knows it's clickable (optional, but keeping cursor hidden maintains immersion if laser is tracking) */
body.presentation-mode #btnExitPresent {
  cursor: none !important;
  /* Laser acts as cursor */
}

/* Laser Pointer */
#laser-pointer {
  position: fixed;
  top: 0;
  left: 0;
  width: 16px;
  height: 16px;
  background: rgba(255, 0, 0, 0.8);
  border: 2px solid rgba(255, 255, 255, 0.8);
  border-radius: 50%;
  pointer-events: none;
  /* Let clicks pass through */
  z-index: 10000;
  transform: translate(-50%, -50%);
  display: none;
  box-shadow: 0 0 10px 2px rgba(255, 0, 0, 0.5);
  transition: transform 0.05s ease-out;
  /* Slight lag for smoothness */
}

/* AMENDMENT [start] "Enhanced Presentation Mode Hover Effects" */
body.presentation-mode #laser-pointer {
  display: block;
}

/* 1. Node/Card Hover Effect */
/* Glow and border highlight */
body.presentation-mode #preview .node:hover,
body.presentation-mode #preview .actor:hover,
body.presentation-mode #preview .label:hover,
body.presentation-mode #preview .task:hover {
  filter: drop-shadow(0 0 15px rgba(79, 70, 229, 0.6));
  /* Primary Glow */
  opacity: 1;
}

body.presentation-mode #preview .node:hover rect,
body.presentation-mode #preview .node:hover circle,
body.presentation-mode #preview .node:hover polygon,
body.presentation-mode #preview .actor:hover rect,
body.presentation-mode #preview .actor:hover circle {
  stroke: var(--primary) !important;
  stroke-width: 4px !important;
  transition: all 0.2s ease;
}

/* 2. Arrow/Edge Hover Effect */
/* Thicken and color change */
/* Need to be very specific to override inline styles */
body.presentation-mode #preview .edgePath:hover path,
body.presentation-mode #preview .flowchart-link:hover path,
body.presentation-mode #preview .relationship:hover path,
body.presentation-mode #preview .messageLine:hover {
  stroke: var(--danger) !important;
  stroke-width: 4px !important;
  opacity: 1 !important;
  filter: drop-shadow(0 0 2px var(--danger));
  cursor: none !important;
}

/* Handle arrowheads separately if possible, though they are markers */
/* Markers are hard to style via CSS as they are referenced <defs> */

/* AMENDMENT [end  ] "Enhanced Presentation Mode Hover Effects" */