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

/* ─── Tokens ────────────────────────────────────────────────────────────── */
:root {
  --bg:         #0d0d0f;
  --surface:    #131316;
  --surface2:   #1c1c21;
  --surface3:   #242429;
  --border:     #2a2a32;
  --border2:    #38383f;
  --text:       #e8e8ee;
  --text-dim:   #7a7a8c;
  --text-muted: #44444f;
  --accent:     #e8c547;
  --accent-dim: rgba(232, 197, 71, 0.12);
  --accent-glow:rgba(232, 197, 71, 0.06);
  --danger:     #e85f5f;
  --danger-dim: rgba(232, 95, 95, 0.12);

  --font-ui:   'Syne', sans-serif;
  --font-mono: 'DM Mono', monospace;

  --topbar-h: 48px;
  --sidebar-w: 260px;
  --toolbar-h: 58px;
  --radius: 4px;
  --radius-lg: 8px;
}

/* ─── Base ──────────────────────────────────────────────────────────────── */
html, body { height: 100%; overflow: hidden; }
body {
  font-family: var(--font-ui);
  background: var(--bg);
  color: var(--text);
  font-size: 13px;
  line-height: 1.5;
}

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

/* ─── Topbar ────────────────────────────────────────────────────────────── */
.topbar {
  height: var(--topbar-h);
  min-height: var(--topbar-h);
  display: flex;
  align-items: center;
  padding: 0 16px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  gap: 16px;
  z-index: 20;
}

.topbar-brand {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-shrink: 0;
}

.brand-icon {
  font-size: 18px;
  color: var(--accent);
  line-height: 1;
}

.brand-name {
  font-weight: 700;
  font-size: 14px;
  letter-spacing: 0.02em;
  color: var(--text);
}

.topbar-center {
  flex: 1;
  display: flex;
  justify-content: center;
}

.topbar-right {
  flex-shrink: 0;
}

.template-selector-wrap {
  display: flex;
  align-items: center;
  gap: 8px;
  background: var(--surface2);
  border: 1px solid var(--border2);
  border-radius: var(--radius);
  padding: 0 10px 0 12px;
  height: 32px;
  position: relative;
}

.template-selector-wrap + .template-selector-wrap {
  margin-left: 10px;
}

.selector-label {
  font-size: 11px;
  color: var(--text-dim);
  font-family: var(--font-mono);
  letter-spacing: 0.05em;
}

.template-selector-wrap select {
  appearance: none;
  background: transparent;
  border: none;
  color: var(--text);
  font-family: var(--font-mono);
  font-size: 12px;
  cursor: pointer;
  padding-right: 4px;
  outline: none;
}

.template-selector-wrap select option {
  background: var(--surface2);
}

.selector-chevron {
  color: var(--text-dim);
  font-size: 10px;
  pointer-events: none;
}

/* ─── Body layout ───────────────────────────────────────────────────────── */
.body-layout {
  display: flex;
  flex: 1;
  overflow: hidden;
}

/* ─── Sidebar ───────────────────────────────────────────────────────────── */
.sidebar {
  width: var(--sidebar-w);
  min-width: var(--sidebar-w);
  background: var(--surface);
  border-right: 1px solid var(--border);
  overflow-y: auto;
  overflow-x: hidden;
  display: flex;
  flex-direction: column;
}

.sidebar::-webkit-scrollbar { width: 4px; }
.sidebar::-webkit-scrollbar-track { background: transparent; }
.sidebar::-webkit-scrollbar-thumb { background: var(--border2); border-radius: 2px; }

/* Section header */
.sidebar-section-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 16px 16px 8px;
  position: sticky;
  top: 0;
  background: var(--surface);
  z-index: 2;
}

.sidebar-section-header h3 {
  font-size: 10px;
  font-family: var(--font-mono);
  letter-spacing: 0.1em;
  text-transform: uppercase;
  color: var(--text-muted);
  font-weight: 500;
}

.sidebar-section-body {
  padding: 0 8px 8px;
  flex: 1;
}

/* Items */
.sidebar-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 7px 10px;
  border-radius: var(--radius);
  cursor: pointer;
  font-size: 13px;
  font-weight: 500;
  color: var(--text-dim);
  transition: background 0.1s, color 0.1s;
  gap: 6px;
  min-height: 34px;
}

.sidebar-item:hover {
  background: var(--surface2);
  color: var(--text);
}

.sidebar-item.active {
  background: var(--accent-dim);
  color: var(--accent);
  border: 1px solid rgba(232, 197, 71, 0.2);
}

.sidebar-item-label {
  flex: 1;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.item-actions {
  display: flex;
  gap: 2px;
  opacity: 0;
  transition: opacity 0.1s;
  flex-shrink: 0;
}

.sidebar-item:hover .item-actions { opacity: 1; }
.sidebar-item.active .item-actions { opacity: 1; }

.item-actions button {
  background: none;
  border: none;
  color: var(--text-muted);
  cursor: pointer;
  width: 22px;
  height: 22px;
  border-radius: 3px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 13px;
  transition: background 0.1s, color 0.1s;
}

.item-actions button:hover {
  background: var(--danger-dim);
  color: var(--danger);
}

/* Route badge */
.route-badge {
  font-family: var(--font-mono);
  font-size: 11px;
  font-weight: 500;
  background: var(--surface3);
  color: var(--text-dim);
  padding: 1px 6px;
  border-radius: 3px;
  border: 1px solid var(--border2);
  flex-shrink: 0;
}

.sidebar-item.active .route-badge {
  background: var(--accent-dim);
  color: var(--accent);
  border-color: rgba(232, 197, 71, 0.3);
}

/* Dest code + via badges */
.dest-code {
  font-family: var(--font-mono);
  font-size: 10px;
  font-weight: 600;
  background: var(--surface3);
  color: var(--text-dim);
  padding: 1px 5px;
  border-radius: 3px;
  border: 1px solid var(--border2);
  flex-shrink: 0;
}

.sidebar-item.active .dest-code {
  background: var(--accent-dim);
  color: var(--accent);
  border-color: rgba(232, 197, 71, 0.3);
}

.dest-via {
  font-size: 10px;
  color: var(--text-muted);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  flex-shrink: 1;
  min-width: 0;
}

.sidebar-item.active .dest-via {
  color: var(--accent);
}

/* Nesting */
.sidebar-nested {
  margin-left: 12px;
  border-left: 1px solid var(--border);
  padding-left: 8px;
  margin-top: 2px;
  margin-bottom: 4px;
}

/* Add buttons */
.add-btn {
  display: flex;
  align-items: center;
  gap: 6px;
  width: 100%;
  padding: 6px 10px;
  background: none;
  border: 1px dashed var(--border2);
  color: var(--text-muted);
  border-radius: var(--radius);
  cursor: pointer;
  font-size: 12px;
  font-family: var(--font-ui);
  font-weight: 500;
  margin-top: 4px;
  transition: border-color 0.15s, color 0.15s, background 0.15s;
}

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

.add-btn-plus {
  font-size: 14px;
  line-height: 1;
  font-weight: 400;
}

/* Empty state */
.empty-state {
  padding: 12px 10px;
  color: var(--text-muted);
  font-size: 12px;
  font-style: italic;
}

/* Sidebar footer */
.sidebar-footer {
  padding: 8px;
  border-top: 1px solid var(--border);
  margin-top: auto;
}

/* ─── Work area ─────────────────────────────────────────────────────────── */
.work-area {
  flex: 1;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  background: var(--bg);
}

/* ─── Canvas region ─────────────────────────────────────────────────────── */
.canvas-region {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  overflow: hidden;
  position: relative;
  background:
    radial-gradient(ellipse at 50% 50%, rgba(232, 197, 71, 0.025) 0%, transparent 70%),
    var(--bg);
  background-image:
    radial-gradient(ellipse at 50% 50%, rgba(232, 197, 71, 0.025) 0%, transparent 70%),
    linear-gradient(rgba(255,255,255,0.015) 1px, transparent 1px),
    linear-gradient(90deg, rgba(255,255,255,0.015) 1px, transparent 1px);
  background-size: 100% 100%, 32px 32px, 32px 32px;
}

/* Empty canvas state */
.empty-canvas {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 12px;
  user-select: none;
}

.empty-canvas-icon {
  font-size: 48px;
  color: var(--text-muted);
  opacity: 0.4;
}

.empty-canvas-text {
  color: var(--text-muted);
  font-size: 14px;
  font-weight: 500;
}

/* Canvas wrapper (set by JS) */
.canvas-wrapper {
  position: relative;
  flex-shrink: 0;
  box-shadow:
    0 0 0 1px var(--border2),
    0 8px 40px rgba(0, 0, 0, 0.6);
}

.canvas-wrapper img {
  display: block;
}

/* Field overlays */
.field-overlay {
  position: absolute;
  border: 1px solid rgba(232, 197, 71, 0.3);
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  overflow: hidden;
  transition: border-color 0.1s;
}

.field-overlay:hover {
  border-color: rgba(232, 197, 71, 0.65);
  background: rgba(232, 197, 71, 0.04);
}

.field-overlay.active {
  border-color: var(--accent);
  border-width: 2px;
  background: rgba(232, 197, 71, 0.06);
  box-shadow: 0 0 0 1px rgba(232, 197, 71, 0.2), inset 0 0 12px rgba(232, 197, 71, 0.04);
}

.field-text {
  pointer-events: none;
  text-align: center;
  line-height: 1.2;
  word-wrap: break-word;
  overflow: hidden;
  width: 100%;
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 2px;
}

/* ─── Field toolbar ─────────────────────────────────────────────────────── */
.field-toolbar {
  height: var(--toolbar-h);
  min-height: var(--toolbar-h);
  background: var(--surface);
  border-top: 1px solid var(--border);
  display: flex;
  align-items: center;
  gap: 0;
  padding: 0;
  overflow: hidden;
}

.toolbar-hint {
  padding: 0 20px;
  color: var(--text-muted);
  font-size: 12px;
  font-family: var(--font-mono);
}

/* Toolbar segments */
.toolbar-segment {
  display: flex;
  flex-direction: column;
  justify-content: center;
  gap: 4px;
  padding: 6px 14px;
  height: 100%;
  border-right: 1px solid var(--border);
}

.toolbar-segment:last-child { border-right: none; flex: 1; }

.toolbar-label {
  font-size: 9px;
  font-family: var(--font-mono);
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--text-muted);
  white-space: nowrap;
  line-height: 1;
}

.toolbar-controls {
  display: flex;
  align-items: center;
  gap: 6px;
}

.btn-upload-font {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  font-family: var(--font-mono);
  font-size: 10px;
  padding: 2px 8px;
  background: var(--surface2);
  color: var(--text-dim);
  border: 1px solid var(--border2);
  border-radius: var(--radius);
  cursor: pointer;
  transition: all 0.12s;
  white-space: nowrap;
  line-height: 1.6;
}

.btn-upload-font:hover {
  border-color: var(--accent);
  color: var(--accent);
  background: var(--accent-glow);
}

.toolbar-field-name {
  font-family: var(--font-mono);
  font-size: 12px;
  color: var(--accent);
  font-weight: 500;
  white-space: nowrap;
}

.toolbar-segment input[type="text"],
.toolbar-segment input[type="number"],
.toolbar-segment select {
  background: var(--surface2);
  color: var(--text);
  border: 1px solid var(--border2);
  padding: 5px 10px;
  border-radius: var(--radius);
  font-family: var(--font-mono);
  font-size: 12px;
  outline: none;
  transition: border-color 0.15s;
}

.toolbar-segment input[type="text"]:focus,
.toolbar-segment input[type="number"]:focus,
.toolbar-segment select:focus {
  border-color: var(--accent);
}

.toolbar-segment input[type="text"] { width: 220px; }
.toolbar-segment input[type="number"] { width: 72px; }
.toolbar-segment select { width: 130px; appearance: none; cursor: pointer; }

/* ─── Modal ─────────────────────────────────────────────────────────────── */
.modal-overlay {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.7);
  backdrop-filter: blur(4px);
  align-items: center;
  justify-content: center;
  z-index: 200;
}

.modal-overlay.show { display: flex; }

.modal-box {
  background: var(--surface);
  border: 1px solid var(--border2);
  border-radius: var(--radius-lg);
  padding: 28px;
  width: 440px;
  max-width: calc(100vw - 40px);
  box-shadow: 0 24px 80px rgba(0, 0, 0, 0.6);
  animation: modal-in 0.15s ease;
}

@keyframes modal-in {
  from { opacity: 0; transform: translateY(-8px) scale(0.98); }
  to   { opacity: 1; transform: translateY(0)    scale(1); }
}

.modal-box h2 {
  font-size: 16px;
  font-weight: 700;
  color: var(--text);
  margin-bottom: 20px;
  letter-spacing: 0.01em;
}

.form-group {
  margin-bottom: 14px;
}

.form-group label {
  display: block;
  font-size: 11px;
  font-family: var(--font-mono);
  letter-spacing: 0.07em;
  text-transform: uppercase;
  color: var(--text-dim);
  margin-bottom: 6px;
}

.form-group input,
.form-group select {
  width: 100%;
  background: var(--surface2);
  color: var(--text);
  border: 1px solid var(--border2);
  padding: 8px 12px;
  border-radius: var(--radius);
  font-family: var(--font-mono);
  font-size: 13px;
  outline: none;
  transition: border-color 0.15s;
}

.form-group input[type="checkbox"],
.form-group input[type="radio"] {
  width: auto;
}

.form-group input:focus,
.form-group select:focus { border-color: var(--accent); }

.form-group select[multiple] {
  height: 140px;
  padding: 6px;
  resize: vertical;
}

.modal-actions {
  display: flex;
  gap: 8px;
  justify-content: flex-end;
  margin-top: 20px;
  padding-top: 16px;
  border-top: 1px solid var(--border);
}

.btn-primary {
  background: var(--accent);
  color: #0d0d0f;
  border: none;
  padding: 8px 18px;
  border-radius: var(--radius);
  font-family: var(--font-ui);
  font-size: 13px;
  font-weight: 600;
  cursor: pointer;
  transition: opacity 0.15s;
}

.btn-primary:hover { opacity: 0.85; }

.btn-secondary {
  background: var(--surface2);
  color: var(--text-dim);
  border: 1px solid var(--border2);
  padding: 8px 18px;
  border-radius: var(--radius);
  font-family: var(--font-ui);
  font-size: 13px;
  font-weight: 500;
  cursor: pointer;
  transition: border-color 0.15s, color 0.15s;
}

.btn-secondary:hover { border-color: var(--border2); color: var(--text); }

/* ─── Save indicator ────────────────────────────────────────────────── */
.save-indicator {
  font-family: var(--font-mono);
  font-size: 10px;
  padding: 3px 10px;
  border-radius: 10px;
  white-space: nowrap;
  transition: all 0.15s;
  letter-spacing: 0.04em;
}

/* ─── Export button ─────────────────────────────────────────────────── */
.btn-export {
  font-family: var(--font-ui);
  font-size: 12px;
  font-weight: 600;
  padding: 4px 14px;
  border-radius: var(--radius);
  border: 1px solid var(--accent);
  background: var(--accent-dim);
  color: var(--accent);
  cursor: pointer;
  transition: all 0.15s;
  margin-right: 8px;
}

.btn-export:hover {
  background: var(--accent);
  color: #0d0d0f;
}

/* ─── Export destination list in modal ─────────────────────────────── */
.export-dest-list {
  max-height: 300px;
  overflow-y: auto;
  border: 1px solid var(--border2);
  border-radius: var(--radius);
  background: var(--surface2);
}

.export-dest-item {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 8px 12px;
  cursor: pointer;
  transition: background 0.1s;
  border-bottom: 1px solid var(--border);
  font-size: 12px;
}

.export-dest-item:last-child { border-bottom: none; }

.export-dest-item:hover { background: var(--surface3); }

.export-dest-item input[type="checkbox"] {
  appearance: none;
  -webkit-appearance: none;
  width: 16px;
  height: 16px;
  border: 1px solid var(--border2);
  border-radius: 3px;
  background: var(--surface);
  cursor: pointer;
  flex-shrink: 0;
  margin: 0;
}

.export-dest-item input[type="checkbox"]:checked {
  background: var(--accent);
  border-color: var(--accent);
  position: relative;
}

.export-dest-item input[type="checkbox"]:checked::after {
  content: '✓';
  position: absolute;
  top: -2px;
  left: 2px;
  font-size: 12px;
  color: #0d0d0f;
  font-weight: 700;
}

.export-dest-path {
  color: var(--text-dim);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.save-indicator.saved {
  background: rgba(61, 171, 107, 0.12);
  color: #3dab6b;
  border: 1px solid rgba(61, 171, 107, 0.25);
}

.save-indicator.unsaved {
  background: rgba(232, 197, 71, 0.12);
  color: var(--accent);
  border: 1px solid rgba(232, 197, 71, 0.3);
  animation: pulse-unsaved 1.2s ease infinite;
}

@keyframes pulse-unsaved {
  0%, 100% { opacity: 1; }
  50% { opacity: 0.6; }
}

/* ─── Scrollbar global ──────────────────────────────────────────────────── */
::-webkit-scrollbar { width: 5px; height: 5px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: var(--border2); border-radius: 3px; }
::-webkit-scrollbar-thumb:hover { background: var(--text-muted); }