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

:root {
  --bg: #0f1117;
  --surface: #1a1d27;
  --surface2: #22263a;
  --border: #2e3350;
  --accent: #4f8ef7;
  --accent-hover: #3a77e8;
  --green: #3ecf8e;
  --red: #f76f6f;
  --orange: #f7a14f;
  --text: #e4e6f0;
  --text-muted: #7a7f9a;
  --font: 'Segoe UI', system-ui, sans-serif;
  --mono: 'Cascadia Code', 'Fira Code', monospace;
  --radius: 8px;
  --line-color: #4f8ef7;
}

body {
  background: var(--bg);
  color: var(--text);
  font-family: var(--font);
  font-size: 14px;
  height: 100vh;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

/* ── Header ────────────────────────────────────────────────────────────── */
header {
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  padding: 0 20px;
  height: 52px;
  display: flex;
  align-items: center;
  gap: 16px;
  flex-shrink: 0;
  z-index: 100;
}

header h1 {
  font-size: 16px;
  font-weight: 600;
  color: var(--accent);
  letter-spacing: 0.5px;
  white-space: nowrap;
}

header .mapping-name {
  font-size: 14px;
  color: var(--text-muted);
  border: 1px solid var(--border);
  background: var(--surface2);
  border-radius: var(--radius);
  padding: 4px 12px;
  width: 240px;
  color: var(--text);
  outline: none;
}
header .mapping-name:focus { border-color: var(--accent); }

header .spacer { flex: 1; }

/* ── Buttons ────────────────────────────────────────────────────────────── */
.btn {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 6px 14px;
  border-radius: var(--radius);
  border: 1px solid var(--border);
  background: var(--surface2);
  color: var(--text);
  cursor: pointer;
  font-size: 13px;
  font-family: var(--font);
  transition: background 0.15s, border-color 0.15s;
  white-space: nowrap;
}
.btn:hover { background: var(--border); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn.primary:hover { background: var(--accent-hover); }
.btn.success { background: var(--green); border-color: var(--green); color: #0f1117; }
.btn.danger { background: var(--red); border-color: var(--red); color: #fff; }
.btn.sm { padding: 4px 10px; font-size: 12px; }

/* ── Layout ─────────────────────────────────────────────────────────────── */
.layout {
  display: flex;
  flex: 1;
  overflow: hidden;
}

/* ── Sidebar ────────────────────────────────────────────────────────────── */
.sidebar {
  width: 220px;
  background: var(--surface);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  flex-shrink: 0;
}

.sidebar-section {
  padding: 12px;
  border-bottom: 1px solid var(--border);
}

.sidebar-section h3 {
  font-size: 11px;
  font-weight: 600;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: 0.8px;
  margin-bottom: 8px;
}

.mapping-list {
  display: flex;
  flex-direction: column;
  gap: 2px;
  overflow-y: auto;
  flex: 1;
  padding: 8px;
}

.mapping-item {
  padding: 8px 10px;
  border-radius: 6px;
  cursor: pointer;
  border: 1px solid transparent;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 4px;
}
.mapping-item:hover { background: var(--surface2); }
.mapping-item.active { background: var(--surface2); border-color: var(--accent); }
.mapping-item .name { flex: 1; font-size: 13px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.mapping-item .del-btn { opacity: 0; font-size: 12px; color: var(--red); cursor: pointer; padding: 2px 4px; border-radius: 4px; }
.mapping-item .ren-btn { opacity: 0; font-size: 12px; color: var(--text-muted); cursor: pointer; padding: 2px 4px; border-radius: 4px; }
.mapping-item:hover .del-btn,
.mapping-item:hover .ren-btn { opacity: 1; }
.mapping-item .ren-btn:hover { color: var(--accent); }

/* ── Main mapper area ───────────────────────────────────────────────────── */
.mapper-container {
  flex: 1;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

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

.mapper-toolbar .sep {
  width: 1px;
  height: 20px;
  background: var(--border);
  margin: 0 4px;
}

.mapper-workspace {
  flex: 1;
  display: flex;
  position: relative;
  overflow: hidden;
}

/* ── Schema panels ──────────────────────────────────────────────────────── */
.schema-panel {
  width: 280px;
  background: var(--surface);
  display: flex;
  flex-direction: column;
  flex-shrink: 0;
  border-right: 1px solid var(--border);
}
.schema-panel.target { border-right: none; border-left: 1px solid var(--border); }

.schema-panel-header {
  padding: 10px 12px;
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
}
.schema-panel-header h3 { font-size: 12px; font-weight: 600; color: var(--text-muted); text-transform: uppercase; letter-spacing: 0.6px; }

.schema-tree {
  flex: 1;
  overflow-y: auto;
  padding: 8px 0;
}

.schema-node {
  display: flex;
  align-items: center;
  gap: 0;
  padding: 3px 0;
  position: relative;
  cursor: default;
  user-select: none;
}

.schema-node.leaf { cursor: grab; }
.schema-node.leaf:active { cursor: grabbing; }

.schema-node-indent {
  display: flex;
  align-items: center;
}

.schema-node-toggle {
  width: 16px;
  text-align: center;
  font-size: 10px;
  color: var(--text-muted);
  cursor: pointer;
}

.schema-node-icon {
  font-size: 11px;
  margin-right: 4px;
  color: var(--text-muted);
}

.schema-node-name {
  font-size: 13px;
  font-family: var(--mono);
  flex: 1;
  color: var(--text);
}
.schema-node.leaf .schema-node-name { color: #a8c4f5; }
.schema-node.array > .schema-node-name::after { content: '[]'; color: var(--orange); font-size: 10px; margin-left: 2px; }
.schema-node.attribute .schema-node-name { color: #f7a14f; }
.schema-node.attribute .schema-node-icon { color: #f7a14f; font-style: italic; }

.connect-point {
  width: 10px;
  height: 10px;
  border-radius: 50%;
  border: 2px solid var(--border);
  background: var(--surface2);
  margin: 0 4px;
  cursor: crosshair;
  transition: background 0.15s, border-color 0.15s;
  flex-shrink: 0;
}
.connect-point:hover, .connect-point.active { background: var(--accent); border-color: var(--accent); }
.connect-point.connected { background: var(--green); border-color: var(--green); }

/* ── SVG canvas ─────────────────────────────────────────────────────────── */
#svg-canvas {
  position: absolute;
  top: 0; left: 0;
  width: 100%; height: 100%;
  pointer-events: none;
  z-index: 10;
}

.mapping-line {
  stroke: var(--line-color);
  stroke-width: 2;
  fill: none;
  pointer-events: stroke;
  cursor: pointer;
  opacity: 0.7;
  transition: opacity 0.15s;
}
.mapping-line:hover { opacity: 1; stroke-width: 3; }
.mapping-line.selected { stroke: var(--orange); opacity: 1; stroke-width: 2.5; }
.mapping-line.lookup { stroke: var(--orange); }
.mapping-line.concat { stroke: var(--green); }
.mapping-line.split   { stroke: #a78bfa; }
.mapping-line.condition       { stroke: #f59e0b; stroke-dasharray: 6 3; }
.mapping-line.condition-else  { stroke: #f59e0b; stroke-dasharray: 2 4; opacity: 0.6; }
.mapping-line.draft { stroke: var(--text-muted); stroke-dasharray: 5 4; opacity: 0.6; }

/* ── Center rules list ──────────────────────────────────────────────────── */
.center-area {
  flex: 1;
  display: flex;
  align-items: flex-start;
  justify-content: center;
  overflow-y: auto;
  padding: 16px;
}

.rules-panel {
  width: 100%;
  max-width: 500px;
}

.rules-panel h3 {
  font-size: 12px;
  font-weight: 600;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: 0.6px;
  margin-bottom: 8px;
}

.rule-card {
  background: var(--surface2);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 10px 12px;
  margin-bottom: 6px;
  display: flex;
  align-items: center;
  gap: 8px;
  cursor: pointer;
  transition: border-color 0.15s;
}
.rule-card:hover { border-color: var(--accent); }
.rule-card.selected { border-color: var(--orange); }

.rule-path {
  font-family: var(--mono);
  font-size: 12px;
  flex: 1;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.rule-path .xpath { color: var(--text-muted); }
.rule-path .arrow { color: var(--accent); margin: 0 6px; }
.rule-badge {
  font-size: 10px;
  padding: 2px 6px;
  border-radius: 4px;
  background: var(--surface);
  border: 1px solid var(--border);
  color: var(--text-muted);
  white-space: nowrap;
}
.rule-badge.lookup { border-color: var(--orange); color: var(--orange); }
.rule-badge.concat { border-color: var(--green); color: var(--green); }
.rule-badge.split   { border-color: #a78bfa; color: #a78bfa; }
.rule-badge.condition  { border-color: #f59e0b; color: #f59e0b; }

/* ── Right panel (rule editor) ──────────────────────────────────────────── */
.right-panel {
  width: 300px;
  background: var(--surface);
  border-left: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  flex-shrink: 0;
}

.right-panel-header {
  padding: 12px;
  border-bottom: 1px solid var(--border);
  font-size: 12px;
  font-weight: 600;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: 0.6px;
}

.right-panel-content {
  flex: 1;
  overflow-y: auto;
  padding: 12px;
}

.form-group {
  margin-bottom: 14px;
}
.form-group label {
  display: block;
  font-size: 11px;
  color: var(--text-muted);
  margin-bottom: 5px;
  font-weight: 500;
}

.form-group input, .form-group select, .form-group textarea {
  width: 100%;
  background: var(--surface2);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 6px 10px;
  color: var(--text);
  font-family: var(--mono);
  font-size: 12px;
  outline: none;
  resize: vertical;
}
.form-group input:focus, .form-group select:focus, .form-group textarea:focus {
  border-color: var(--accent);
}
.form-group select option { background: var(--surface2); }

/* ── Modal ──────────────────────────────────────────────────────────────── */
.modal-overlay {
  position: fixed; inset: 0;
  background: rgba(0,0,0,0.65);
  z-index: 500;
  display: flex;
  align-items: center;
  justify-content: center;
}
.modal-overlay.hidden { display: none; }

.modal {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
  width: 560px;
  max-height: 80vh;
  display: flex;
  flex-direction: column;
  box-shadow: 0 24px 48px rgba(0,0,0,0.5);
}

.modal-header {
  padding: 16px 20px;
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: space-between;
}
.modal-header h2 { font-size: 15px; font-weight: 600; }
.modal-close { background: none; border: none; color: var(--text-muted); font-size: 18px; cursor: pointer; padding: 4px 8px; border-radius: 4px; }
.modal-close:hover { color: var(--text); background: var(--surface2); }

.modal-body {
  flex: 1;
  overflow-y: auto;
  padding: 16px 20px;
}

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

textarea.xml-input {
  width: 100%;
  height: 200px;
  background: var(--surface2);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 10px;
  color: var(--text);
  font-family: var(--mono);
  font-size: 12px;
  line-height: 1.5;
  resize: vertical;
  outline: none;
}
textarea.xml-input:focus { border-color: var(--accent); }

/* ── Test result ────────────────────────────────────────────────────────── */
.test-result {
  margin-top: 12px;
  background: var(--surface2);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 10px;
  font-family: var(--mono);
  font-size: 12px;
  white-space: pre;
  overflow: auto;
  max-height: 300px;
  color: var(--green);
}
.test-result.error { color: var(--red); }

/* ── Lookup table editor ────────────────────────────────────────────────── */
.lookup-entries { display: flex; flex-direction: column; gap: 6px; }
.lookup-entry {
  display: grid;
  grid-template-columns: 1fr 1fr auto;
  gap: 6px;
  align-items: center;
}
.lookup-entry input {
  background: var(--surface2);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 5px 8px;
  color: var(--text);
  font-family: var(--mono);
  font-size: 12px;
  outline: none;
}
.lookup-entry input:focus { border-color: var(--accent); }

/* ── Toast ──────────────────────────────────────────────────────────────── */
#toast {
  position: fixed;
  bottom: 20px;
  left: 50%;
  transform: translateX(-50%);
  background: var(--surface2);
  border: 1px solid var(--border);
  color: var(--text);
  padding: 10px 20px;
  border-radius: var(--radius);
  font-size: 13px;
  z-index: 9999;
  opacity: 0;
  transition: opacity 0.2s;
  pointer-events: none;
}
#toast.show { opacity: 1; }
#toast.success { border-color: var(--green); color: var(--green); }
#toast.error { border-color: var(--red); color: var(--red); }

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

/* ── Empty state ────────────────────────────────────────────────────────── */
.empty-state {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  height: 100%;
  color: var(--text-muted);
  gap: 12px;
  text-align: center;
  padding: 40px;
}
.empty-state .icon { font-size: 40px; opacity: 0.4; }
.empty-state p { font-size: 13px; line-height: 1.6; }

/* ── Login overlay ──────────────────────────────────────────────────────── */
.login-overlay {
  position: fixed;
  inset: 0;
  background: var(--bg);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 1000;
}
.login-overlay.hidden { display: none; }

.login-box {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 40px;
  width: 360px;
  display: flex;
  flex-direction: column;
  gap: 16px;
}
.login-box h2 {
  font-size: 20px;
  font-weight: 600;
  color: var(--accent);
  text-align: center;
  margin-bottom: 8px;
}
.login-box input {
  width: 100%;
  background: var(--surface2);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 8px 12px;
  color: var(--text);
  font-size: 14px;
  outline: none;
}
.login-box input:focus { border-color: var(--accent); }
#login-error {
  color: var(--red);
  font-size: 12px;
  min-height: 16px;
  text-align: center;
}
