/* ── Reset & base ─────────────────────────────────────────────── */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

:root {
  --bg:        #1a1b26;
  --surface:   #1e1f2e;
  --surface2:  #24253a;
  --border:    #2f3048;
  --accent:    #7aa2f7;
  --accent2:   #bb9af7;
  --text:      #c0caf5;
  --text-dim:  #787c99;
  --green:     #9ece6a;
  --red:       #f7768e;
  --yellow:    #e0af68;
  --header-h:  56px;
  --footer-h:  100px;
  --font-ui:   -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
}

html, body {
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: var(--font-ui);
  font-size: 14px;
  overflow: hidden;
}

a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }

/* ── Layout shell ─────────────────────────────────────────────── */
#app {
  display: flex;
  flex-direction: column;
  height: 100vh;
}

/* ── Header ───────────────────────────────────────────────────── */
#header {
  height: var(--header-h);
  min-height: var(--header-h);
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 0 16px;
  flex-shrink: 0;
  overflow: hidden;
  z-index: 10;
}

#logo {
  display: flex;
  align-items: center;
  flex-shrink: 0;
}
#logo img {
  height: 30px;
  width: auto;
}

.header-sep { flex: 1; }

/* ── Toolbar ──────────────────────────────────────────────────── */
#toolbar {
  display: flex;
  align-items: center;
  gap: 6px;
}

.btn {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  padding: 5px 12px;
  border-radius: 6px;
  border: 1px solid var(--border);
  background: var(--surface2);
  color: var(--text);
  font-size: 13px;
  cursor: pointer;
  transition: background 0.15s, border-color 0.15s;
  white-space: nowrap;
}
.btn:hover { background: var(--border); border-color: var(--accent); }
.btn.primary { background: var(--accent); color: #1a1b26; border-color: var(--accent); font-weight: 600; }
.btn.primary:hover { opacity: 0.85; }
.btn svg { width: 14px; height: 14px; }

/* examples select */
#examples-select {
  padding: 5px 8px;
  border-radius: 6px;
  border: 1px solid var(--border);
  background: var(--surface2);
  color: var(--text);
  font-size: 13px;
  cursor: pointer;
  max-width: 180px;
}
#examples-select:hover { border-color: var(--accent); }

/* theme select (same styling as examples-select) */
#theme-select {
  padding: 5px 8px;
  border-radius: 6px;
  border: 1px solid var(--border);
  background: var(--surface2);
  color: var(--text);
  font-size: 13px;
  cursor: pointer;
  max-width: 160px;
}
#theme-select:hover { border-color: var(--accent); }

/* ad in header */
#ad-header {
  margin-left: 12px;
  display: flex;
  align-items: center;
  flex-shrink: 0;
}

/* ── Main two-panel area ──────────────────────────────────────── */
#main {
  display: flex;
  height: calc(100vh - var(--header-h));
  overflow: hidden;
  min-height: 0;
}

/* ── Editor panel ─────────────────────────────────────────────── */
#editor-panel {
  display: flex;
  flex-direction: column;
  width: 50%;
  min-width: 300px;
  border-right: 1px solid var(--border);
  overflow: hidden;
}

.panel-header {
  height: 36px;
  min-height: 36px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  padding: 0 12px;
  gap: 8px;
  font-size: 12px;
  color: var(--text-dim);
  text-transform: uppercase;
  letter-spacing: 0.6px;
  flex-shrink: 0;
}
.panel-header .dot {
  width: 8px; height: 8px;
  border-radius: 50%;
  background: var(--accent);
  display: inline-block;
}

#editor-container {
  flex: 1;
  overflow: hidden;
  min-height: 0;
  display: flex;
  flex-direction: column;
}

/* CodeMirror fills the container */
#editor-container .cm-editor {
  height: 100%;
  font-size: 13.5px;
}
#editor-container .cm-scroller {
  overflow: auto;
  flex: 1;
}

/* ── Preview panel ────────────────────────────────────────────── */
#preview-panel {
  display: flex;
  flex-direction: column;
  flex: 1;
  overflow: hidden;
  min-width: 0;
}

#preview-container {
  flex: 1;
  overflow: hidden;
  background: #f8f8f8;
  display: block;
  min-height: 0;
  position: relative;
  cursor: default;
  user-select: none;
}
/* Show pan cursors only when actively panning or when middle-mouse is held. */
#preview-container.panning { cursor: grabbing; }

/* ── Shape drag-to-reposition ─────────────────────────────────── */
/* Shapes emitted with data-draggable="true" show a grab cursor.  */
#preview-svg [data-draggable="true"] { cursor: grab; }
/* Applied to the <g> element while a drag is active.             */
#preview-svg .shape-dragging,
body.shape-is-dragging #preview-svg [data-draggable="true"] { cursor: grabbing; }
@media (prefers-color-scheme: dark) {
  #preview-container { background: #121212; }
}

#preview-svg {
  position: absolute;
  top: 0;
  left: 0;
  transform-origin: 0 0;
  /* transform set by JS */
}
#preview-svg svg {
  display: block;
}

#preview-empty {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  color: #aaa;
  font-family: var(--font-ui);
  font-size: 14px;
  pointer-events: none;
  cursor: default;
}

/* spinner overlay */
#spinner {
  position: absolute;
  inset: 0;
  background: rgba(255,255,255,0.6);
  display: none;
  align-items: center;
  justify-content: center;
  z-index: 5;
}
#spinner.active { display: flex; }
.spinner-ring {
  width: 36px; height: 36px;
  border: 3px solid #ddd;
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 0.7s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* ad below preview */
#ad-preview {
  background: var(--surface);
  border-top: 1px solid var(--border);
  padding: 8px;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  min-height: 116px;
}

/* ── Error bar ────────────────────────────────────────────────── */
#error-bar {
  display: none;
  background: #2d1a1a;
  border-top: 2px solid var(--red);
  color: #f7768e;
  font-family: 'SF Mono', Consolas, monospace;
  font-size: 12px;
  padding: 8px 14px;
  white-space: pre-wrap;
  word-break: break-word;
  max-height: 80px;
  overflow-y: auto;
  flex-shrink: 0;
}
#error-bar.active { display: block; }
#error-bar button.dismiss {
  float: right;
  background: none;
  border: none;
  color: var(--text-dim);
  cursor: pointer;
  font-size: 14px;
  line-height: 1;
}

/* ── Zoom controls ────────────────────────────────────────────── */
#zoom-controls {
  margin-left: auto;
  display: flex;
  align-items: center;
  gap: 4px;
}
#zoom-label {
  font-size: 11px;
  color: var(--text-dim);
  min-width: 38px;
  text-align: center;
  font-variant-numeric: tabular-nums;
}
.zoom-btn {
  width: 22px;
  height: 22px;
  border-radius: 4px;
  border: 1px solid var(--border);
  background: var(--surface2);
  color: var(--text);
  font-size: 14px;
  line-height: 1;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  padding: 0;
  transition: background 0.15s, border-color 0.15s;
}
.zoom-btn:hover { background: var(--border); border-color: var(--accent); }

/* ── Divider / resize handle ──────────────────────────────────── */
#divider {
  width: 5px;
  background: var(--border);
  cursor: col-resize;
  flex-shrink: 0;
  transition: background 0.15s;
}
#divider:hover, #divider.dragging { background: var(--accent); }

/* ── Nav links in header ──────────────────────────────────────── */
.nav-link {
  color: var(--text-dim);
  font-size: 13px;
  padding: 4px 8px;
  border-radius: 4px;
  background: none;
  border: none;
  cursor: pointer;
  font-family: var(--font-ui);
}
.nav-link:hover { color: var(--text); background: var(--surface2); text-decoration: none; }

/* ── Manual slide-in panel ────────────────────────────── */
#manual-panel {
  position: fixed;
  top: var(--header-h);
  right: 0;
  width: 640px;
  height: calc(100vh - var(--header-h));
  display: flex;
  flex-direction: column;
  background: var(--surface);
  border-left: 1px solid var(--border);
  box-shadow: -4px 0 24px rgba(0,0,0,0.4);
  transform: translateX(100%);
  transition: transform 0.28s cubic-bezier(0.4, 0, 0.2, 1);
  z-index: 100;
}
#manual-panel.open {
  transform: translateX(0);
}

#manual-panel-header {
  height: 36px;
  min-height: 36px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  padding: 0 12px;
  gap: 8px;
  font-size: 12px;
  color: var(--text-dim);
  text-transform: uppercase;
  letter-spacing: 0.6px;
  flex-shrink: 0;
}
#btn-manual-close {
  margin-left: auto;
  width: 22px;
  height: 22px;
  border-radius: 4px;
  border: 1px solid var(--border);
  background: var(--surface2);
  color: var(--text-dim);
  font-size: 13px;
  line-height: 1;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  padding: 0;
  transition: background 0.15s, border-color 0.15s, color 0.15s;
}
#btn-manual-close:hover {
  background: var(--border);
  border-color: var(--accent);
  color: var(--text);
}

/* Two-column layout inside the panel */
#manual-panel-wrap {
  flex: 1;
  display: flex;
  overflow: hidden;
  min-height: 0;
  background: #f8f9fa;
  color: #212529;
  font-family: var(--font-ui);
}

#manual-panel-nav {
  width: 180px;
  flex-shrink: 0;
  overflow-y: auto;
  border-right: 1px solid #dee2e6;
  padding: 14px 12px;
  font-size: 12px;
}
#manual-panel-nav h2 {
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.6px;
  color: #6c757d;
  margin-bottom: 10px;
  font-weight: 600;
}
#manual-panel-nav ul { list-style: none; }
#manual-panel-nav li { margin-bottom: 3px; }
#manual-panel-nav a { color: #495057; text-decoration: none; }
#manual-panel-nav a:hover { color: #0d6efd; text-decoration: none; }
#manual-panel-nav .sub { padding-left: 12px; }

#manual-panel-body {
  flex: 1;
  overflow-y: auto;
  min-width: 0;
}
/* Tighten the injected .manual-content for the narrower panel */
#manual-panel-body .manual-content {
  padding: 16px 18px 40px;
}
#manual-panel-body .manual-content h1 { font-size: 20px; margin-bottom: 6px; }
#manual-panel-body .manual-content h2 { font-size: 16px; margin: 24px 0 8px; }
#manual-panel-body .manual-content h3 { font-size: 14px; margin: 16px 0 6px; }

/* ── Manual page styles ───────────────────────────────────────── */
body.manual-page {
  overflow: auto;
  background: #f8f9fa;
  color: #212529;
  font-family: var(--font-ui);
}

.manual-wrap {
  display: flex;
  max-width: 1200px;
  margin: 0 auto;
  padding: 24px 16px;
  gap: 32px;
}

.manual-nav {
  width: 220px;
  flex-shrink: 0;
  position: sticky;
  top: 24px;
  align-self: flex-start;
  background: #fff;
  border-radius: 10px;
  border: 1px solid #dee2e6;
  padding: 16px;
  font-size: 13px;
}
.manual-nav h2 { font-size: 12px; text-transform: uppercase; letter-spacing: 0.6px; color: #6c757d; margin-bottom: 10px; }
.manual-nav ul { list-style: none; }
.manual-nav li { margin-bottom: 4px; }
.manual-nav a { color: #495057; }
.manual-nav a:hover { color: #0d6efd; }
.manual-nav .sub { padding-left: 12px; }

.manual-content { flex: 1; min-width: 0; }
.manual-content h1 { font-size: 32px; font-weight: 700; margin-bottom: 8px; }
.manual-content .subtitle { color: #6c757d; margin-bottom: 32px; font-size: 16px; }
.manual-content h2 { font-size: 22px; font-weight: 600; margin: 36px 0 12px; padding-bottom: 8px; border-bottom: 2px solid #dee2e6; }
.manual-content h3 { font-size: 17px; font-weight: 600; margin: 24px 0 8px; color: #343a40; }
.manual-content p { line-height: 1.7; margin-bottom: 12px; color: #495057; }
.manual-content ul, .manual-content ol { padding-left: 20px; margin-bottom: 12px; }
.manual-content li { line-height: 1.7; color: #495057; }

pre, code { font-family: 'SF Mono', Consolas, 'Courier New', monospace; font-size: 13px; }
.manual-content pre {
  background: #1a1b26; color: #c0caf5;
  border-radius: 8px; padding: 16px;
  overflow-x: auto; margin-bottom: 16px;
  line-height: 1.6;
}
.manual-content code { background: #f1f3f5; color: #e83e8c; padding: 1px 5px; border-radius: 4px; }
.manual-content pre code { background: none; color: inherit; padding: 0; }

table { width: 100%; border-collapse: collapse; margin-bottom: 16px; font-size: 13px; }
th { background: #e9ecef; text-align: left; padding: 8px 12px; border: 1px solid #dee2e6; font-weight: 600; }
td { padding: 7px 12px; border: 1px solid #dee2e6; vertical-align: top; }
tr:nth-child(even) td { background: #f8f9fa; }

.tag { display: inline-block; padding: 1px 7px; border-radius: 10px; font-size: 11px; font-weight: 600; }
.tag.required { background: #fff0f0; color: #c0392b; }
.tag.optional { background: #f0f8ff; color: #2980b9; }

.shape-name { font-family: 'SF Mono', Consolas, monospace; font-weight: 700; color: #0d6efd; font-size: 15px; }

.manual-content section { margin-bottom: 8px; }
.manual-top-ad { background: #f1f3f5; border-radius: 8px; padding: 8px; margin-bottom: 24px; min-height: 90px; display: flex; align-items: center; justify-content: center; }

/* ── Tab bar ──────────────────────────────────────────────────── */
#tab-bar {
  display: flex;
  align-items: stretch;
  height: 36px;
  min-height: 36px;
  background: var(--bg);
  border-bottom: 1px solid var(--border);
  overflow-x: auto;
  flex-shrink: 0;
  scrollbar-width: none; /* Firefox */
}
#tab-bar::-webkit-scrollbar { display: none; }

.tab {
  display: flex;
  align-items: center;
  gap: 4px;
  padding: 0 6px 0 12px;
  min-width: 80px;
  max-width: 180px;
  border-right: 1px solid var(--border);
  cursor: pointer;
  font-size: 12px;
  color: var(--text-dim);
  flex-shrink: 0;
  position: relative;
  user-select: none;
  transition: background 0.1s;
}
.tab:hover { background: var(--surface2); }
.tab.active {
  background: var(--surface);
  color: var(--text);
  border-top: 2px solid var(--accent);
}

.tab-title {
  flex: 1;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  line-height: 36px;
}
.tab.active .tab-title { line-height: 34px; } /* compensate for 2px top border */

.tab-close {
  background: none;
  border: none;
  color: var(--text-dim);
  cursor: pointer;
  padding: 0;
  line-height: 1;
  font-size: 13px;
  border-radius: 3px;
  opacity: 0;
  flex-shrink: 0;
  transition: opacity 0.12s, background 0.1s;
  width: 16px;
  height: 16px;
  display: flex;
  align-items: center;
  justify-content: center;
}
.tab:hover .tab-close,
.tab.active .tab-close { opacity: 1; }
.tab-close:hover { background: var(--border); color: var(--text); }

.tab-bar-actions {
  margin-left: auto;
  display: flex;
  align-items: center;
  padding: 0 4px;
  gap: 2px;
  flex-shrink: 0;
  border-left: 1px solid var(--border);
}

#btn-new-tab,
#btn-open-file {
  width: 26px;
  height: 26px;
  border-radius: 4px;
  border: none;
  background: none;
  color: var(--text-dim);
  line-height: 1;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 0;
  transition: background 0.1s, color 0.1s;
}
#btn-new-tab { font-size: 20px; }
#btn-open-file { font-size: 14px; }
#btn-new-tab:hover,
#btn-open-file:hover { background: var(--surface2); color: var(--text); }

/* ── Responsive ───────────────────────────────────────────────── */
@media (max-width: 768px) {
  html, body { overflow: auto; }
  #app { height: auto; overflow: auto; }
  #main { flex-direction: column; overflow: auto; height: auto; }
  #editor-panel { width: 100%; border-right: none; border-bottom: 1px solid var(--border); height: 50vh; }
  #divider { display: none; }
  #preview-panel { height: 50vh; overflow: auto; }
  #editor-container .cm-editor { height: 100%; }
  #ad-header { display: none; }
  .manual-wrap { flex-direction: column; }
  .manual-nav { width: 100%; position: static; }
}

/* ── Sketchitor syntax highlighting ───────────────────────────── */
/* Applied by ViewPlugin via Decoration.mark — one-dark palette   */
.sk-kw   { color: #c678dd; font-weight: 600; } /* shape keywords  purple  */
.sk-prop { color: #61afef; }                   /* param names     cyan   */
.sk-str  { color: #98c379; }                   /* strings         green  */
.sk-cmt  { color: #5c6370; font-style: italic; }/* comments        gray   */
.sk-num  { color: #d19a66; }                   /* numbers         orange */
.sk-dir  { color: #e5c07b; }                   /* @directives     yellow */

/* ── Colour picker dialog ────────────────────────────────────── */
#color-dialog-backdrop {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.55);
  z-index: 300;
  align-items: center;
  justify-content: center;
}
#color-dialog-backdrop.open { display: flex; }

#color-dialog {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 10px;
  width: 400px;
  max-width: calc(100vw - 32px);
  max-height: 80vh;
  overflow-y: auto;
  box-shadow: 0 8px 32px rgba(0,0,0,0.5);
}

#color-dialog-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 16px;
  border-bottom: 1px solid var(--border);
  font-size: 14px;
  font-weight: 600;
  color: var(--text);
}
#color-dialog-header button {
  background: none;
  border: none;
  color: var(--text-dim);
  cursor: pointer;
  font-size: 16px;
  padding: 2px 6px;
  border-radius: 4px;
  line-height: 1;
}
#color-dialog-header button:hover { background: var(--border); color: var(--text); }

#color-dialog-body { padding: 14px 16px; }

.color-picker-row {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-bottom: 16px;
}
.color-picker-row input[type="color"] {
  width: 40px;
  height: 32px;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--surface2);
  cursor: pointer;
  padding: 2px;
  flex-shrink: 0;
}
.color-picker-row input[type="text"] {
  flex: 1;
  background: var(--surface2);
  border: 1px solid var(--border);
  color: var(--text);
  border-radius: 6px;
  padding: 5px 10px;
  font-size: 13px;
  font-family: 'SF Mono', Consolas, monospace;
}
.color-picker-row input[type="text"]:focus { outline: none; border-color: var(--accent); }

.color-section-title {
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.6px;
  color: var(--text-dim);
  margin-bottom: 8px;
}

#color-swatches {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 4px;
}
.color-swatch-btn {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 6px 10px;
  background: var(--surface2);
  border: 1px solid transparent;
  border-radius: 6px;
  cursor: pointer;
  width: 100%;
  text-align: left;
  transition: border-color 0.12s, background 0.12s;
}
.color-swatch-btn:hover { border-color: var(--accent); background: var(--border); }
.swatch-dot {
  width: 18px;
  height: 18px;
  border-radius: 50%;
  flex-shrink: 0;
  border: 1px solid rgba(255,255,255,0.2);
}
.swatch-body { display: flex; flex-direction: column; min-width: 0; }
.swatch-label { font-size: 12px; color: var(--text); line-height: 1.3; }
.swatch-token {
  font-size: 10px;
  color: var(--text-dim);
  font-family: 'SF Mono', Consolas, monospace;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

/* ── Export options dialog ─────────────────────────────────────── */
#export-dialog-backdrop {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.55);
  z-index: 300;
  align-items: center;
  justify-content: center;
}
#export-dialog-backdrop.open { display: flex; }

#export-dialog {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 10px;
  width: 360px;
  max-width: calc(100vw - 32px);
  box-shadow: 0 8px 32px rgba(0,0,0,0.5);
}

#export-dialog-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 16px;
  border-bottom: 1px solid var(--border);
  font-size: 14px;
  font-weight: 600;
  color: var(--text);
}
#export-dialog-header button {
  background: none;
  border: none;
  color: var(--text-dim);
  cursor: pointer;
  font-size: 16px;
  padding: 2px 6px;
  border-radius: 4px;
  line-height: 1;
}
#export-dialog-header button:hover { background: var(--border); color: var(--text); }

#export-dialog-body { padding: 14px 16px; }

.export-section-title {
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.6px;
  color: var(--text-dim);
  margin-bottom: 8px;
}

.export-radio-group {
  display: flex;
  flex-direction: column;
  gap: 6px;
}
.export-radio-group--cols {
  display: grid;
  grid-template-columns: 1fr 1fr;
}

.export-radio-label {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 13px;
  color: var(--text);
  cursor: pointer;
  padding: 4px 8px;
  border-radius: 6px;
  transition: background 0.1s;
}
.export-radio-label:hover { background: var(--surface2); }
.export-radio-label input[type="radio"] { cursor: pointer; accent-color: var(--accent); }

.export-note {
  font-size: 11px;
  color: var(--text-muted, #888);
  margin-top: 5px;
  display: none;
}
.export-note.visible { display: block; }

.export-actions {
  display: flex;
  gap: 8px;
  margin-top: 18px;
  justify-content: flex-end;
}
