:root {
  --bg: #16171b;
  --panel: #212329;
  --panel-2: #2a2c33;
  --border: #383b44;
  --text: #e7e8ec;
  --muted: #9aa0aa;
  --accent: #4f8cff;
  --accent-fg: #ffffff;
  --warn: #e0a449;
  --danger: #e0584b;
  --radius: 8px;
  --gap: 10px;
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { margin: 0; height: 100%; }
body {
  font: 14px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  color: var(--text);
  background: var(--bg);
}

/* ---------- App shell: viewport-filling grid, no outer scroll ---------- */
#app {
  height: 100dvh;
  display: grid;
  grid-template-rows: auto 1fr;
  overflow: hidden;
}

.topbar {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 8px 14px;
  background: var(--panel);
  border-bottom: 1px solid var(--border);
}
.brand { margin: 0; font-size: 16px; font-weight: 700; letter-spacing: .4px; }
.project-name {
  width: 240px;
  background: var(--panel-2);
}
.topbar-actions { margin-left: auto; display: flex; gap: 8px; align-items: center; }

.panels {
  display: grid;
  grid-template-columns: minmax(290px, 1fr) minmax(300px, 1.05fr) minmax(360px, 1.5fr);
  gap: var(--gap);
  padding: var(--gap);
  min-height: 0;        /* let panel children scroll instead of the page */
  overflow: hidden;
}

.panel {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  display: flex;
  flex-direction: column;
  min-height: 0;
  overflow: hidden;
}
.panel-head {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 9px 12px;
  border-bottom: 1px solid var(--border);
  font-weight: 600;
  background: var(--panel-2);
  border-radius: var(--radius) var(--radius) 0 0;
}
.panel-head .spacer { margin-left: auto; }
.panel-body { padding: 12px; overflow: auto; min-height: 0; flex: 1; }

/* ---------- Forms ---------- */
label.field { display: flex; flex-direction: column; gap: 4px; margin-bottom: 10px; }
label.field > span { font-size: 12px; color: var(--muted); }
.field-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.field-grid.three { grid-template-columns: 1fr 1fr 1fr; }

input, select, textarea, button {
  font: inherit;
  color: var(--text);
  background: var(--panel-2);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 6px 8px;
}
input:focus, select:focus, textarea:focus { outline: 2px solid var(--accent); outline-offset: -1px; }
input[type="color"] { padding: 2px; height: 32px; }
textarea {
  width: 100%;
  min-height: 180px;
  resize: vertical;
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  font-size: 12.5px;
  line-height: 1.5;
  white-space: pre;
}
label.field.grow, .field.grow { flex: 1; display: flex; flex-direction: column; }
.field-label-row {
  display: flex; align-items: center; justify-content: space-between; gap: 8px;
  margin-bottom: 4px; font-size: 12px; color: var(--muted);
}
.field-label-row code { background: var(--panel-2); padding: 1px 4px; border-radius: 4px; }

fieldset.group {
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 10px 12px 2px;
  margin: 0 0 12px;
}
fieldset.group legend { font-size: 12px; color: var(--muted); padding: 0 6px; }

button {
  cursor: pointer;
  background: var(--panel-2);
  transition: background .12s ease, border-color .12s ease;
}
button:hover { border-color: var(--accent); }
button.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-fg); font-weight: 600; }
button.primary:hover { filter: brightness(1.08); }
button.ghost { background: transparent; }
button.icon { padding: 4px 8px; }

.hint { font-size: 12px; color: var(--muted); margin: 6px 0 0; }
.hint code { background: var(--panel-2); padding: 1px 4px; border-radius: 4px; }
.muted { color: var(--muted); font-weight: 400; font-size: 12px; }

.banner { padding: 7px 10px; border-radius: 6px; font-size: 12.5px; margin-bottom: 10px; }
.banner.warn { background: rgba(224,164,73,.15); color: var(--warn); border: 1px solid rgba(224,164,73,.4); }

/* ---------- Data table ---------- */
.data-table { width: 100%; border-collapse: collapse; }
.data-table th, .data-table td { padding: 4px; text-align: left; }
.data-table th { font-size: 12px; color: var(--muted); font-weight: 500; }
.data-table td input { width: 100%; }
.data-table td textarea.list-cell {
  width: 100%; min-height: 36px; height: auto; resize: vertical;
  font-family: inherit; font-size: 13px; white-space: pre-wrap; line-height: 1.35;
}
.data-table td.rownum { color: var(--muted); width: 26px; text-align: right; padding-right: 8px; font-size: 12px; }
.data-table td.rowdel { width: 30px; }
.row-over td { opacity: .5; }
.row-over td.rownum::after { content: " ⚠"; }

/* ---------- Preview ---------- */
#preview-panel .panel-body { padding: 0; display: flex; flex-direction: column; }
.preview-controls {
  display: flex; align-items: center; gap: 8px;
  padding: 10px 12px; border-bottom: 1px solid var(--border);
}
.seg { display: inline-flex; border: 1px solid var(--border); border-radius: 6px; overflow: hidden; }
.seg button { border: 0; border-radius: 0; background: transparent; padding: 5px 12px; }
.seg button.active { background: var(--accent); color: var(--accent-fg); }
.preview-stage {
  flex: 1; min-height: 0; overflow: auto;
  display: flex; align-items: center; justify-content: center;
  padding: 16px; background: #0e0f12;
}
.preview-svg {
  max-width: 100%; max-height: 100%;
  background: #fff;
  box-shadow: 0 6px 28px rgba(0,0,0,.5);
}
.preview-mode-card .preview-svg { width: min(100%, 560px); height: auto; }

/* ---------- Toast ---------- */
.toast {
  position: fixed; bottom: 18px; left: 50%; transform: translateX(-50%);
  background: var(--panel-2); border: 1px solid var(--border);
  padding: 8px 16px; border-radius: 8px; box-shadow: 0 6px 24px rgba(0,0,0,.5);
  z-index: 10;
}

/* ---------- Expanded template editor (modal) ---------- */
.modal {
  position: fixed; inset: 0; z-index: 20;
  display: flex; align-items: center; justify-content: center;
  padding: 24px; background: rgba(0,0,0,.55);
}
.modal-card {
  display: flex; flex-direction: column;
  width: min(1000px, 92vw); height: min(80vh, 820px);
  background: var(--panel); border: 1px solid var(--border); border-radius: 10px;
  box-shadow: 0 20px 60px rgba(0,0,0,.55); overflow: hidden;
}
.modal-head {
  display: flex; align-items: center; gap: 8px;
  padding: 10px 14px; border-bottom: 1px solid var(--border); font-weight: 600;
}
.modal-card textarea {
  flex: 1; min-height: 0; border: 0; border-radius: 0; resize: none;
  font-size: 13.5px; padding: 14px;
}
.import-body { flex: 1; min-height: 0; display: flex; flex-direction: column; gap: 8px; padding: 12px 14px; }
.import-body .hint { margin: 0; }
.import-body textarea { flex: 1; min-height: 0; border: 1px solid var(--border); border-radius: 6px; }
.modal-foot { display: flex; align-items: center; gap: 10px; padding: 10px 14px; border-top: 1px solid var(--border); }
.file-pick {
  cursor: pointer; padding: 6px 10px; border: 1px solid var(--border);
  border-radius: 6px; background: var(--panel-2); font-size: 13px;
}
.file-pick:hover { border-color: var(--accent); }

/* ---------- Responsive fallback: stack + allow scroll on narrow screens ---------- */
@media (max-width: 1040px) {
  #app { height: auto; min-height: 100dvh; overflow: auto; }
  .panels { grid-template-columns: 1fr; overflow: visible; }
  .panel { min-height: 320px; }
  .topbar { flex-wrap: wrap; }
  .topbar-actions { margin-left: 0; }
}
