:root {
  color-scheme: dark;
  --bg: #17181b;
  --panel: #202124;
  --panel-2: #292b30;
  --input: #303238;
  --border: #454852;
  --text: #e8eaed;
  --muted: #aeb4c0;
  --accent: #8ab4f8;
  --accent-2: #5f91e8;
  --danger: #f28b82;
  --success: #81c995;
  --warning: #fbbc60;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  min-height: 100vh;
  background: var(--bg);
  color: var(--text);
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

button, select, input, textarea { font: inherit; }

button {
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--input);
  color: var(--text);
  cursor: pointer;
  padding: 10px 14px;
  transition: background .15s ease, border-color .15s ease, opacity .15s ease;
}
button:hover:not(:disabled) { background: #3a3d45; border-color: #65708a; }
button:disabled { cursor: not-allowed; opacity: .45; }
button.primary { background: var(--accent-2); border-color: var(--accent-2); color: white; }
button.primary:hover:not(:disabled) { background: #78a6f0; }
button.danger { background: transparent; border-color: var(--danger); color: var(--danger); }
button.danger:hover:not(:disabled) { background: #f28b8222; }
button.small { padding: 7px 11px; font-size: .84rem; white-space: nowrap; }

input, textarea, select {
  width: 100%;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--input);
  color: var(--text);
  outline: none;
  padding: 10px 11px;
}
input:focus, textarea:focus, select:focus { border-color: var(--accent); box-shadow: 0 0 0 2px #8ab4f822; }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
input:focus-visible, textarea:focus-visible { outline: none; }

label, .card-title { display: block; color: var(--muted); font-size: .86rem; margin-bottom: 8px; }
label.spaced { margin-top: 16px; }

.app-shell {
  width: min(1440px, calc(100vw - 32px));
  margin: 24px auto;
}

.app-header {
  display: flex;
  align-items: end;
  justify-content: space-between;
  gap: 16px;
  border-bottom: 1px solid var(--border);
  padding-bottom: 18px;
}

.eyebrow {
  margin: 0 0 4px;
  color: var(--accent);
  font-size: .85rem;
  font-weight: 700;
  letter-spacing: .04em;
  text-transform: uppercase;
}

h1, h2, h3 { margin: 0; }
h1 { font-size: 1.45rem; }
h2 { color: var(--accent); font-size: 1rem; overflow-wrap: anywhere; }
h3 { color: var(--accent); font-size: 1.1rem; }

.header-right { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; justify-content: flex-end; }
.user-email { color: var(--muted); font-size: .82rem; }

.status { color: var(--muted); font-size: .85rem; min-height: 1.3em; margin: 12px 0 0; }
.status.success { color: var(--success); }
.status.error { color: var(--danger); }

/* ---------- Layout ---------- */
.workspace {
  display: grid;
  grid-template-columns: 300px 340px minmax(0, 1fr);
  grid-template-areas: "list fields preview";
  gap: 20px;
  margin-top: 14px;
  align-items: start;
}
.list-panel { grid-area: list; }
.fields-panel { grid-area: fields; }
.preview-panel { grid-area: preview; }

.panel {
  min-width: 0;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--panel);
  padding: 16px;
}

.panel-heading { display: flex; align-items: center; justify-content: space-between; gap: 10px; margin-bottom: 14px; }
.heading-actions { display: flex; gap: 8px; flex-shrink: 0; }
.panel-actions { display: flex; gap: 10px; margin-top: 16px; }
.panel-actions button { flex: 1; }

.hint { margin: 10px 0 0; color: var(--muted); font-size: .8rem; }
.footer { color: var(--muted); font-size: .8rem; margin-top: 20px; }

/* ---------- Template list ---------- */
.list-panel { display: flex; flex-direction: column; max-height: calc(100vh - 170px); position: sticky; top: 16px; }
.list-toolbar { display: flex; gap: 8px; }
.list-toolbar input { padding: 8px 10px; }

.template-list {
  list-style: none;
  margin: 12px 0 0;
  padding: 0;
  overflow-y: auto;
  flex: 1;
  min-height: 120px;
}

.template-item {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 4px 6px 4px 2px;
  border-radius: 6px;
  border: 1px solid transparent;
  cursor: pointer;
  user-select: none;
}
.template-item:hover { background: var(--panel-2); }
.template-item.selected { background: #8ab4f81f; border-color: #8ab4f855; }
.template-item.dragging { opacity: .85; background: var(--input); border-color: var(--accent); box-shadow: 0 6px 16px #0006; }
.template-item:focus-visible { outline-offset: -2px; }

.drag-handle {
  flex-shrink: 0;
  width: 26px;
  align-self: stretch;
  display: grid;
  place-items: center;
  color: var(--muted);
  cursor: grab;
  touch-action: none;
  font-size: .9rem;
  letter-spacing: -2px;
  border-radius: 4px;
}
.drag-handle:hover { color: var(--text); background: #ffffff10; }
.template-list.filtering .drag-handle { visibility: hidden; }
body.is-dragging, body.is-dragging * { cursor: grabbing !important; }

.template-name { flex: 1; min-width: 0; font-size: .9rem; line-height: 1.3; padding: 6px 0; overflow-wrap: anywhere; }
.template-meta { flex-shrink: 0; color: var(--muted); font-size: .72rem; }
.list-empty { color: var(--muted); font-size: .88rem; padding: 10px 4px; }

/* ---------- Fields ---------- */
.fields-container { display: grid; gap: 14px; }
.field { display: grid; gap: 6px; }
.field label { margin: 0; color: var(--text); }
.required { color: var(--danger); }
.empty-state { color: var(--muted); line-height: 1.5; margin: 0; }

/* ---------- Preview ---------- */
.preview-panel { display: flex; flex-direction: column; }
#preview {
  min-height: 560px;
  resize: vertical;
  padding: 15px;
  background: var(--panel-2);
  font-family: "Cascadia Code", "SFMono-Regular", Consolas, monospace;
  font-size: .9rem;
  line-height: 1.55;
  white-space: pre-wrap;
}

/* ---------- Modals ---------- */
.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, .55);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px;
  z-index: 50;
}
.modal-overlay[hidden] { display: none; }

.modal {
  width: min(560px, 100%);
  max-height: calc(100vh - 32px);
  overflow-y: auto;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 22px;
}
.modal-wide { width: min(820px, 100%); }

.modal-header { display: flex; align-items: center; justify-content: space-between; margin-bottom: 16px; }
.modal-close { background: transparent; border: none; color: var(--muted); font-size: 1.4rem; line-height: 1; padding: 4px 8px; }
.modal-close:hover:not(:disabled) { color: var(--text); background: transparent; }

#templateBodyInput {
  min-height: 300px;
  resize: vertical;
  font-family: "Cascadia Code", "SFMono-Regular", Consolas, monospace;
  font-size: .85rem;
  line-height: 1.5;
}

.fields-list-header { display: flex; align-items: center; justify-content: space-between; gap: 10px; margin: 16px 0 8px; }
.fields-list-header label { margin: 0; }

.field-rows { display: grid; gap: 8px; }
.field-row {
  display: grid;
  grid-template-columns: minmax(0, 1fr) minmax(0, 1.3fr) auto auto;
  gap: 8px;
  align-items: center;
}
.field-row input[type="text"] { padding: 8px 10px; font-size: .88rem; }
.field-row-required { display: flex; align-items: center; gap: 5px; color: var(--muted); font-size: .78rem; white-space: nowrap; margin: 0; }
.field-row-required input { width: auto; accent-color: var(--accent-2); }
.field-row-remove { background: transparent; color: var(--muted); padding: 6px 10px; line-height: 1; }
.field-row-remove:hover:not(:disabled) { color: var(--danger); border-color: var(--danger); background: #f28b8222; }

.field-hint { color: var(--muted); font-size: .78rem; margin: 6px 0 0; line-height: 1.4; }
.form-error { color: var(--danger); font-size: .85rem; min-height: 1.1em; margin: 12px 0 0; white-space: pre-line; }

.syntax-help { margin-top: 10px; font-size: .82rem; color: var(--muted); }
.syntax-help summary { cursor: pointer; }
.syntax-help table { border-collapse: collapse; margin-top: 8px; width: 100%; }
.syntax-help td { padding: 4px 8px 4px 0; vertical-align: top; }
.syntax-help code { color: var(--accent); white-space: nowrap; }

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

.import-summary { margin: 0 0 14px; line-height: 1.5; }
.radio-option { display: flex; gap: 10px; align-items: flex-start; color: var(--text); line-height: 1.4; padding: 10px; border: 1px solid var(--border); border-radius: 6px; cursor: pointer; }
.radio-option + .radio-option { margin-top: 8px; }
.radio-option input { width: auto; margin-top: 3px; accent-color: var(--accent-2); }

/* ---------- Responsive ---------- */
@media (max-width: 1180px) {
  .workspace {
    grid-template-columns: 300px minmax(0, 1fr);
    grid-template-areas: "list fields" "list preview";
  }
  #preview { min-height: 380px; }
}

@media (max-width: 760px) {
  .app-shell { width: calc(100vw - 32px); margin: 16px auto; }
  .app-header { flex-direction: column; align-items: stretch; }
  .header-right { justify-content: flex-start; }
  .workspace { grid-template-columns: minmax(0, 1fr); grid-template-areas: "list" "fields" "preview"; }
  .list-panel { position: static; max-height: 45vh; }
  #preview { min-height: 320px; }
  .field-row { grid-template-columns: 1fr 1fr; }
  .field-row-remove { justify-self: end; }
  .fields-list-header { flex-wrap: wrap; }
}
