:root {
  --bg: #f6f7fb;
  --card: #fff;
  --border: #e5e7eb;
  --text: #1f2937;
  --muted: #6b7280;
  --primary: #2563eb;
  --primary-h: #1d4ed8;
  --error: #dc2626;
}
* { box-sizing: border-box; }
body {
  margin: 0;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
  line-height: 1.5;
}
header { padding: 20px 24px; background: white; border-bottom: 1px solid var(--border); }
header h1 { margin: 0; font-size: 20px; font-weight: 600; }
main { max-width: 900px; margin: 20px auto; padding: 0 16px; display: flex; flex-direction: column; gap: 16px; }
.card { background: var(--card); border: 1px solid var(--border); border-radius: 8px; padding: 20px; }
.card h2 { margin: 0 0 12px; font-size: 16px; font-weight: 600; }
.hint { color: var(--muted); font-size: 13px; margin: 4px 0 12px; }
.status { margin-top: 8px; font-size: 13px; color: var(--muted); min-height: 1em; }
.status.error { color: var(--error); }
.vars-grid { display: flex; flex-direction: column; gap: 10px; }
.var-row { display: grid; grid-template-columns: minmax(160px, 280px) 1fr; gap: 12px; align-items: start; }
.var-name {
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: 13px;
  color: var(--text);
  padding-top: 7px;
  word-break: break-word;
}
.var-row-auto .var-name { color: var(--muted); }
.var-auto-preview {
  padding-top: 7px;
  font-family: "Times New Roman", Georgia, serif;
  color: var(--text);
  font-size: 14px;
}
.var-auto-preview::before {
  content: "авто";
  display: inline-block;
  background: #dbeafe;
  color: #1d4ed8;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  font-size: 11px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  padding: 2px 6px;
  border-radius: 3px;
  margin-right: 8px;
  vertical-align: 2px;
}
textarea {
  width: 100%;
  min-height: 34px;
  padding: 6px 10px;
  border: 1px solid var(--border);
  border-radius: 6px;
  font: inherit;
  resize: vertical;
  overflow: hidden;
}
textarea:focus { outline: 2px solid var(--primary); outline-offset: -1px; border-color: transparent; }
.actions { display: flex; gap: 8px; margin-top: 16px; flex-wrap: wrap; }
button {
  padding: 8px 14px;
  border: 1px solid var(--border);
  background: white;
  border-radius: 6px;
  cursor: pointer;
  font: inherit;
}
button:hover { background: #f3f4f6; }
button.primary { background: var(--primary); border-color: var(--primary); color: white; }
button.primary:hover { background: var(--primary-h); border-color: var(--primary-h); }
button.ghost { color: var(--muted); }
input[type="file"] { padding: 6px 0; }
#preview-content {
  background: #fafafa;
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 16px 20px;
  max-height: 640px;
  overflow: auto;
  font-family: "Times New Roman", Georgia, serif;
}
#preview-content table { border-collapse: collapse; width: 100%; margin: 8px 0; }
#preview-content td, #preview-content th { border: 1px solid #d1d5db; padding: 4px 8px; vertical-align: top; }
#preview-content p { margin: 4px 0; }
code { background: #f3f4f6; padding: 1px 5px; border-radius: 3px; font-size: 0.92em; }
@media (max-width: 600px) {
  .var-row { grid-template-columns: 1fr; gap: 4px; }
  .var-name { padding-top: 0; }
}

/* ---- список шаблонов ---- */
table.templates { width: 100%; border-collapse: collapse; font-size: 14px; margin-top: 8px; }
table.templates th, table.templates td { text-align: left; padding: 10px 12px; border-bottom: 1px solid var(--border); vertical-align: top; }
table.templates th { font-size: 12px; color: var(--muted); font-weight: 600; text-transform: uppercase; letter-spacing: 0.04em; background: #f8fafc; }
table.templates a { color: var(--primary); text-decoration: none; font-weight: 500; }
table.templates a:hover { text-decoration: underline; }
table.templates tr:last-child td { border-bottom: none; }

button.primary { background: var(--primary); border-color: var(--primary); color: white; }
button.danger { background: white; border-color: #fecaca; color: var(--error); }
button.danger:hover { background: #fef2f2; }

.lbl { display: block; font-size: 12px; color: var(--muted); margin-bottom: 4px; font-weight: 500; }
input[type="text"], #tpl-name { width: 100%; padding: 7px 10px; border: 1px solid var(--border); border-radius: 6px; font: inherit; }
input[type="text"]:focus { outline: 2px solid var(--primary); outline-offset: -1px; border-color: transparent; }
.row-form { display: flex; gap: 12px; flex-wrap: wrap; align-items: flex-end; }

/* ---- toast ---- */
.toast {
  position: fixed; bottom: 24px; left: 50%; transform: translateX(-50%);
  background: #1f2937; color: white;
  padding: 10px 16px; border-radius: 8px;
  font-size: 14px; opacity: 0;
  transition: opacity 0.2s; pointer-events: none;
  max-width: 90vw;
}
.toast.visible { opacity: 1; }
.toast.error { background: var(--error); }

/* ---- image placeholders ---- */
.var-kind-badge {
  display: inline-block;
  background: #fef3c7;
  color: #92400e;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  font-size: 11px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  padding: 2px 6px;
  border-radius: 3px;
  margin-left: 8px;
  vertical-align: 2px;
}
.var-image-input {
  width: 100%;
  padding: 6px;
  border: 1px solid var(--border);
  border-radius: 6px;
  font: inherit;
  background: white;
}
.var-image-hint {
  font-size: 12px;
  color: var(--muted);
  margin-top: 4px;
}

/* ---- history toggle + list ---- */
.var-input-wrap { display: flex; flex-direction: column; gap: 6px; min-width: 0; }
.var-history { display: flex; flex-direction: column; gap: 4px; }
.var-history:empty { display: none; }
.var-history-toggle {
  align-self: flex-start;
  padding: 2px 10px;
  font-size: 12px;
  border: 1px solid var(--border);
  background: #f8fafc;
  color: var(--muted);
  border-radius: 12px;
  cursor: pointer;
  font-family: inherit;
  line-height: 1.5;
  display: inline-flex;
  align-items: center;
  gap: 6px;
}
.var-history-toggle:hover { background: #eef2f7; color: var(--text); }
.var-history-toggle[data-open="1"] { background: #e0e7ff; color: var(--primary-h); border-color: #c7d2fe; }
.var-history-count {
  display: inline-block;
  background: rgba(0,0,0,0.06);
  color: inherit;
  padding: 0 6px;
  border-radius: 8px;
  font-weight: 600;
  font-size: 11px;
}
.var-history-toggle[data-open="1"] .var-history-count {
  background: rgba(37, 99, 235, 0.15);
}
.var-history-caret {
  display: inline-block;
  font-size: 9px;
  line-height: 1;
  transition: transform 0.12s ease;
}
.var-history-toggle[data-open="1"] .var-history-caret { transform: rotate(180deg); }
.var-history-list {
  display: flex;
  flex-direction: column;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: white;
  max-height: 280px;
  overflow-y: auto;
}
.var-history-list[hidden] { display: none; }
.var-history-item {
  display: flex;
  align-items: stretch;
  border-bottom: 1px solid var(--border);
}
.var-history-item:last-child { border-bottom: none; }
.var-history-item-value {
  flex: 1;
  text-align: left;
  padding: 6px 10px;
  font-size: 13px;
  border: none;
  background: transparent;
  cursor: pointer;
  font-family: inherit;
  color: var(--text);
  border-radius: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.var-history-item-value:hover { background: #f0f9ff; color: var(--primary-h); }
.var-history-item[data-multi="1"] .var-history-item-value::after {
  content: " ↵";
  opacity: 0.5;
  font-size: 11px;
}
.var-history-item-delete {
  flex: 0 0 auto;
  padding: 0 10px;
  border: none;
  border-left: 1px solid var(--border);
  background: transparent;
  color: var(--muted);
  cursor: pointer;
  font-size: 18px;
  line-height: 1;
  border-radius: 0;
}
.var-history-item-delete:hover { background: #fef2f2; color: var(--error); }
.var-history-item-delete:disabled { opacity: 0.4; cursor: wait; }

/* ---- manage variables ---- */
.manage-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 12px;
  padding: 8px 0;
  border-bottom: 1px dashed var(--border);
}
.manage-row:last-child { border-bottom: none; }
.var-name-mono {
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: 13px;
  background: #f3f4f6;
  padding: 3px 8px;
  border-radius: 4px;
  color: var(--text);
}
.manage-actions { display: flex; gap: 6px; }
.manage-actions button { padding: 4px 10px; font-size: 13px; }

h2 { margin: 0 0 6px; font-size: 16px; font-weight: 600; }

/* ---- tabs ---- */
.tabs {
  display: flex;
  gap: 2px;
  border-bottom: 1px solid var(--border);
  margin-bottom: -8px;
}
.tabs .tab {
  padding: 8px 18px;
  border: 1px solid transparent;
  border-bottom: none;
  border-radius: 6px 6px 0 0;
  background: transparent;
  color: var(--muted);
  cursor: pointer;
  font: inherit;
  font-weight: 500;
  margin-bottom: -1px;
}
.tabs .tab:hover { color: var(--text); background: rgba(0,0,0,0.03); }
.tabs .tab.active {
  background: var(--card);
  color: var(--text);
  border-color: var(--border);
  border-bottom-color: var(--card);
}

/* ---- type badge ---- */
.type-badge {
  display: inline-block;
  background: #e0e7ff;
  color: #3730a3;
  font-size: 11px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  padding: 2px 8px;
  border-radius: 10px;
  vertical-align: 1px;
}

/* ---- last render link ---- */
.last-render {
  margin-top: 12px;
  padding: 8px 12px;
  background: #ecfdf5;
  border: 1px solid #a7f3d0;
  border-radius: 6px;
  font-size: 13px;
  color: #065f46;
  display: flex;
  gap: 8px;
  align-items: center;
}
.last-render[hidden] { display: none; }
.last-render::before {
  content: "✓";
  font-weight: 700;
  color: #059669;
}
.last-render-label { color: var(--muted); }
.last-render a { color: #047857; text-decoration: underline; }
.last-render a:hover { text-decoration: none; }

/* ---- history table buttons ---- */
.btn-link {
  display: inline-block;
  padding: 5px 10px;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: white;
  color: var(--text);
  text-decoration: none;
  font-size: 13px;
}
.btn-link:hover { background: #f3f4f6; }
table.templates .ghost { padding: 5px 10px; font-size: 13px; }
table.templates .danger { font-weight: 600; }

/* ---- config_md editor ---- */
#config-md {
  width: 100%;
  padding: 10px 12px;
  border: 1px solid var(--border);
  border-radius: 6px;
  resize: vertical;
  min-height: 200px;
  line-height: 1.45;
}
#config-md:focus { outline: 2px solid var(--primary); outline-offset: -1px; border-color: transparent; }
