:root {
  --blue: #3f7cac;
  --blue-dark: #2c5c85;
  --blue-tint: #eef4fa;
  --orange: #e08a2c;
  --orange-tint: #fdf3e7;
  --red: #cf3a4a;
  --red-tint: #fbedee;
  --ink: #1c2230;
  --ink-soft: #4b5468;
  --muted: #8891a3;
  --line: #e3e7ee;
  --bg: #f6f7fb;
  --card: #ffffff;
  --radius-lg: 16px;
  --radius-md: 10px;
  --radius-sm: 7px;
  --shadow: 0 1px 2px rgba(20, 30, 60, 0.04), 0 8px 24px -12px rgba(20, 30, 60, 0.12);
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  background: var(--bg);
  color: var(--ink);
  font-family: "Vazirmatn", -apple-system, "Segoe UI", sans-serif;
  font-size: 15px;
  line-height: 1.75;
}

a { color: var(--blue); text-decoration: none; }
a:hover { text-decoration: underline; }

h1, h2, h3 { margin: 0; font-weight: 700; }

.muted { color: var(--muted); }
.small { font-size: 12.5px; }
.hint { color: var(--muted); font-size: 13px; margin: 6px 0 0; }

/* ---------- topbar ---------- */

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px 28px;
  background: var(--card);
  border-bottom: 1px solid var(--line);
  position: sticky;
  top: 0;
  z-index: 20;
}

.brand { display: flex; align-items: center; gap: 10px; color: var(--ink); }
.brand:hover { text-decoration: none; }
.brand-mark {
  display: inline-flex; align-items: center; justify-content: center;
  width: 32px; height: 32px; border-radius: 9px;
  background: linear-gradient(135deg, var(--blue), var(--blue-dark));
  color: #fff; font-weight: 800; font-size: 13px; letter-spacing: -0.5px;
  font-family: "JetBrains Mono", monospace;
}
.brand-mark-lg { width: 52px; height: 52px; border-radius: 14px; font-size: 20px; margin-bottom: 14px; }
.brand-name { font-weight: 700; font-size: 16px; }

.topbar-nav { display: flex; align-items: center; gap: 12px; }
.topbar-user { color: var(--ink-soft); font-size: 13.5px; }

/* ---------- buttons ---------- */

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  border: 1px solid transparent; border-radius: var(--radius-sm);
  padding: 9px 16px; font-family: inherit; font-size: 13.5px; font-weight: 600;
  cursor: pointer; transition: background .15s, border-color .15s, transform .05s;
  white-space: nowrap;
}
.btn:active { transform: translateY(1px); }
.btn-sm { padding: 7px 12px; font-size: 12.5px; }
.btn-xs { padding: 5px 10px; font-size: 12px; }
.btn-block { width: 100%; }

.btn-primary { background: var(--blue); color: #fff; }
.btn-primary:hover { background: var(--blue-dark); text-decoration: none; }
.btn-secondary { background: var(--blue-tint); color: var(--blue-dark); }
.btn-secondary:hover { background: #e2ecf5; text-decoration: none; }
.btn-outline { background: #fff; color: var(--ink-soft); border-color: var(--line); }
.btn-outline:hover { border-color: var(--blue); color: var(--blue); text-decoration: none; }
.btn-outline.btn-optional:hover { border-color: var(--red); color: var(--red); }
.btn-ghost { background: transparent; color: var(--ink-soft); }
.btn-ghost:hover { background: var(--bg); text-decoration: none; }

.icon-btn {
  background: none; border: none; cursor: pointer; font-size: 14px;
  color: var(--muted); padding: 4px 6px; border-radius: 6px; line-height: 1;
}
.icon-btn:hover { background: var(--red-tint); color: var(--red); }

/* ---------- forms ---------- */

.field { display: flex; flex-direction: column; gap: 6px; }
.field > span { font-size: 12.5px; font-weight: 600; color: var(--ink-soft); }

input[type="text"], input[type="password"], input[type="number"], textarea {
  font-family: inherit; font-size: 14px; color: var(--ink);
  border: 1px solid var(--line); border-radius: var(--radius-sm);
  padding: 9px 12px; background: #fff; width: 100%;
  transition: border-color .15s, box-shadow .15s;
}
textarea { resize: vertical; line-height: 1.8; }
input:focus, textarea:focus {
  outline: none; border-color: var(--blue);
  box-shadow: 0 0 0 3px rgba(63, 124, 172, 0.15);
}

.stack { display: flex; flex-direction: column; gap: 16px; }
.grid-2 { display: grid; grid-template-columns: 1fr 1fr; gap: 14px 16px; }
@media (max-width: 720px) { .grid-2 { grid-template-columns: 1fr; } }

.alert { padding: 10px 14px; border-radius: var(--radius-sm); font-size: 13.5px; margin-bottom: 4px; }
.alert-error { background: var(--red-tint); color: var(--red); border: 1px solid #f2c6cc; }

/* switch */
.switch { position: relative; display: inline-block; width: 40px; height: 22px; flex-shrink: 0; }
.switch input { opacity: 0; width: 0; height: 0; }
.slider {
  position: absolute; cursor: pointer; inset: 0; background: #d8dce4;
  border-radius: 22px; transition: .15s;
}
.slider::before {
  content: ""; position: absolute; height: 16px; width: 16px; right: 3px; top: 3px;
  background: #fff; border-radius: 50%; transition: .15s; box-shadow: 0 1px 2px rgba(0,0,0,.2);
}
.switch input:checked + .slider { background: var(--blue); }
.switch input:checked + .slider::before { transform: translateX(-18px); }

.mode-toggle {
  display: flex; align-items: center; gap: 6px; font-size: 12px; color: var(--muted);
  cursor: pointer; user-select: none; margin-top: 6px;
}
.mode-toggle input { accent-color: var(--blue); }
.field-with-mode textarea.advanced-active {
  font-family: "JetBrains Mono", monospace; font-size: 13px; direction: ltr; text-align: left;
  background: #fbfbfc;
}

/* ---------- auth ---------- */

.auth-wrap { display: flex; justify-content: center; padding: 64px 20px; }
.auth-card {
  width: 100%; max-width: 380px; background: var(--card); border: 1px solid var(--line);
  border-radius: var(--radius-lg); padding: 32px; box-shadow: var(--shadow);
}
.auth-brand { text-align: center; margin-bottom: 22px; }
.auth-brand h1 { font-size: 19px; margin-bottom: 4px; }
.auth-brand p { margin: 0; font-size: 13px; }
.auth-switch { text-align: center; margin: 18px 0 0; font-size: 13px; color: var(--ink-soft); }

/* ---------- dashboard ---------- */

.container { max-width: 1080px; margin: 0 auto; padding: 32px 24px 80px; }
.page-head { margin-bottom: 24px; }
.page-head h1 { font-size: 22px; margin-bottom: 4px; }
.page-head p { margin: 0; }

.new-doc-row { display: flex; gap: 12px; margin-bottom: 32px; flex-wrap: wrap; }
.new-doc-btn {
  display: flex; align-items: center; gap: 10px; padding: 14px 20px;
  border: 1.5px dashed var(--line); border-radius: var(--radius-md); background: #fff;
  cursor: pointer; font-family: inherit; font-size: 14px; font-weight: 600; color: var(--ink-soft);
  transition: border-color .15s, color .15s, background .15s;
}
.new-doc-btn:hover { border-color: var(--blue); color: var(--blue); background: var(--blue-tint); }
.new-doc-icon {
  width: 26px; height: 26px; border-radius: 50%; background: var(--blue-tint); color: var(--blue);
  display: flex; align-items: center; justify-content: center; font-size: 16px; font-weight: 700;
}

.doc-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(230px, 1fr)); gap: 16px; }
.doc-card {
  display: block; background: var(--card); border: 1px solid var(--line); border-radius: var(--radius-md);
  padding: 18px; color: var(--ink); transition: box-shadow .15s, border-color .15s, transform .1s;
}
.doc-card:hover { box-shadow: var(--shadow); border-color: #d3dae6; text-decoration: none; transform: translateY(-1px); }
.doc-card-top { margin-bottom: 10px; }
.doc-card-title { font-size: 15px; margin-bottom: 4px; }
.doc-card-meta { margin: 0; color: var(--muted); font-size: 13px; min-height: 1.2em; }
.doc-card-foot { display: flex; align-items: center; justify-content: space-between; margin-top: 14px; }
.inline-form { margin: 0; }

.badge {
  display: inline-block; font-size: 11.5px; font-weight: 700; padding: 3px 10px;
  border-radius: 20px; background: var(--blue-tint); color: var(--blue-dark);
}
.badge-exam { background: var(--red-tint); color: var(--red); }
.badge-quiz { background: var(--orange-tint); color: #a8631a; }

.empty-state { text-align: center; padding: 60px 20px; color: var(--muted); }

/* ---------- editor ---------- */

.editor-shell { max-width: 1280px; margin: 0 auto; padding: 0 24px 60px; }
.editor-header {
  display: flex; align-items: center; justify-content: space-between; gap: 16px;
  padding: 20px 0; flex-wrap: wrap;
}
.editor-title-row { display: flex; align-items: center; gap: 10px; flex: 1; min-width: 240px; }
.title-input {
  font-size: 19px; font-weight: 700; border: none; background: transparent; padding: 4px 6px;
  border-radius: 6px; flex: 1; min-width: 160px;
}
.title-input:focus { outline: none; background: #fff; box-shadow: 0 0 0 2px var(--blue); }
.editor-actions { display: flex; align-items: center; gap: 10px; }
.save-status { font-size: 12px; color: var(--muted); min-width: 70px; text-align: center; }

.editor-body { display: grid; grid-template-columns: minmax(0, 1fr) 380px; gap: 24px; align-items: start; }
@media (max-width: 980px) { .editor-body { grid-template-columns: 1fr; } }

.editor-form { display: flex; flex-direction: column; gap: 18px; min-width: 0; }

.card {
  background: var(--card); border: 1px solid var(--line); border-radius: var(--radius-lg);
  padding: 20px 22px;
}
.card-title { font-size: 14.5px; margin-bottom: 14px; }
.card-title-row { display: flex; align-items: center; justify-content: space-between; margin-bottom: 6px; }
.card-title-row .card-title { margin-bottom: 0; }

.field-with-mode { display: flex; flex-direction: column; }

/* background picker */
.bg-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(120px, 1fr)); gap: 12px; }
.bg-option {
  position: relative; display: flex; flex-direction: column; align-items: center; gap: 6px;
  cursor: pointer; padding: 8px; border: 1.5px solid var(--line); border-radius: var(--radius-md);
  font-size: 12px; color: var(--ink-soft); transition: border-color .15s, background .15s;
}
.bg-option:hover { border-color: #c4cee0; }
.bg-option input { position: absolute; opacity: 0; }
.bg-option img { width: 100%; aspect-ratio: 210/297; object-fit: cover; border-radius: 6px; border: 1px solid var(--line); background: #fafbfc; }
.bg-option:has(input:checked) { border-color: var(--blue); background: var(--blue-tint); color: var(--blue-dark); font-weight: 600; }
.bg-upload-preview {
  width: 100%; aspect-ratio: 210/297; border-radius: 6px; border: 1.5px dashed var(--line);
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 6px;
  color: var(--muted); background: #fafbfc; overflow: hidden;
}
.bg-upload-preview img { width: 100%; height: 100%; object-fit: cover; }
.upload-plus { font-size: 20px; }

/* questions */
.question-card {
  border: 1px solid var(--line); border-radius: var(--radius-md); padding: 16px;
  margin-bottom: 14px; background: #fbfcfe;
}
.question-card[data-kind="optional"] { border-color: #f2c6cc; background: #fffaf9; }
.question-head { display: flex; align-items: center; gap: 10px; margin-bottom: 10px; }
.question-num {
  width: 26px; height: 26px; border-radius: 8px; background: var(--blue-tint); color: var(--blue-dark);
  display: flex; align-items: center; justify-content: center; font-weight: 700; font-size: 12.5px; flex-shrink: 0;
}
.question-card[data-kind="optional"] .question-num { background: var(--red-tint); color: var(--red); }
.question-kind-label { font-size: 13px; font-weight: 600; color: var(--ink-soft); flex: 1; }
.score-field { display: flex; align-items: center; gap: 6px; font-size: 12px; color: var(--muted); }
.score-field input { width: 58px; padding: 5px 8px; font-size: 13px; }

.subparts-list { margin: 10px 0 6px; display: flex; flex-direction: column; gap: 8px; }
.subpart-row { display: flex; align-items: flex-start; gap: 8px; background: #fff; border: 1px solid var(--line); border-radius: var(--radius-sm); padding: 8px 10px; }
.subpart-letter {
  width: 22px; height: 22px; border-radius: 6px; background: var(--orange-tint); color: #a8631a;
  display: flex; align-items: center; justify-content: center; font-size: 11.5px; font-weight: 700; flex-shrink: 0; margin-top: 2px;
}
.subpart-row .field-with-mode { flex: 1; }
.subpart-row textarea { font-size: 13.5px; }

.add-question-row { display: flex; gap: 10px; margin-top: 4px; }

/* preview */
.editor-preview { position: sticky; top: 76px; }
.preview-panel {
  background: var(--card); border: 1px solid var(--line); border-radius: var(--radius-lg);
  overflow: hidden; display: flex; flex-direction: column; height: calc(100vh - 110px);
}
.preview-head {
  display: flex; align-items: center; justify-content: space-between; padding: 12px 16px;
  border-bottom: 1px solid var(--line); font-size: 13px; font-weight: 700; color: var(--ink-soft);
}
.preview-body { flex: 1; overflow: auto; background: #eef0f4; display: flex; }
.preview-body iframe { width: 100%; height: 100%; border: none; }
.preview-placeholder { margin: auto; padding: 30px; text-align: center; font-size: 13px; }
.preview-log {
  margin: 16px; padding: 12px; background: #fff5f5; border: 1px solid #f2c6cc; border-radius: 8px;
  font-family: "JetBrains Mono", monospace; font-size: 11.5px; white-space: pre-wrap; color: #7a1f2b;
  overflow: auto; max-height: 100%;
}
