:root {
  --bg: #f6f7f9;
  --surface: #ffffff;
  --surface-2: #f0f2f5;
  --border: #d8dce3;
  --border-strong: #b9c0cb;
  --text: #1a1d23;
  --text-dim: #5c6470;
  --accent: #1f5f8b;
  --accent-text: #ffffff;
  --ok: #1d7a4d;
  --warn: #a8620a;
  --err: #b3261e;
  --warn-bg: #fdf3e3;
  --err-bg: #fdeceb;
  --ok-bg: #e8f5ee;
  --radius: 6px;
  --mono: ui-monospace, "Cascadia Mono", "Segoe UI Mono", Consolas, monospace;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #14171c;
    --surface: #1c2027;
    --surface-2: #232830;
    --border: #333a45;
    --border-strong: #47505e;
    --text: #e6e9ee;
    --text-dim: #98a1af;
    --accent: #5aa9dd;
    --accent-text: #10141a;
    --ok: #5fca94;
    --warn: #e3ab5c;
    --err: #f08a82;
    --warn-bg: #2e2618;
    --err-bg: #2f1d1c;
    --ok-bg: #17281f;
  }
}

* { box-sizing: border-box; }

/**
 * The `hidden` attribute is how this app shows and hides the drop overlay, the
 * restore banner and the toast. A class selector that sets `display` outranks the
 * user-agent `[hidden] { display: none }` rule, so hidden elements would stay on
 * screen; this puts the attribute back in charge.
 */
[hidden] { display: none !important; }

html, body {
  height: 100%;
  margin: 0;
}

body {
  background: var(--bg);
  color: var(--text);
  font: 15px/1.5 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

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

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

.brand { display: flex; align-items: baseline; gap: 8px; }
.brand-mark {
  font-weight: 700;
  letter-spacing: .06em;
  font-size: 13px;
  color: var(--accent-text);
  background: var(--accent);
  padding: 3px 7px;
  border-radius: 4px;
}
.brand-name { font-weight: 600; }

.file-state {
  flex: 1;
  min-width: 0;
  display: flex;
  align-items: center;
  gap: 10px;
}
.file-name {
  color: var(--text-dim);
  font-size: 13px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.dirty-flag {
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: .05em;
  color: var(--warn);
  border: 1px solid currentColor;
  border-radius: 999px;
  padding: 1px 8px;
  flex: none;
}

.topbar-actions { display: flex; align-items: center; gap: 8px; flex: none; }
.sep { width: 1px; height: 22px; background: var(--border); }

.btn {
  font: inherit;
  font-size: 13px;
  padding: 6px 12px;
  border: 1px solid var(--border-strong);
  background: var(--surface);
  color: var(--text);
  border-radius: var(--radius);
  cursor: pointer;
}
/* <label class="btn"> is used for the file picker so it works without JS. */
label.btn {
  display: inline-block;
  line-height: 1.5;
  user-select: none;
}
.btn:hover:not(:disabled) { background: var(--surface-2); }
.btn-large { font-size: 14px; padding: 9px 18px; }

/* Keep the file input focusable and label-activatable, but out of the layout. */
.visually-hidden {
  position: absolute;
  width: 1px; height: 1px;
  margin: -1px; padding: 0;
  overflow: hidden;
  clip: rect(0 0 0 0);
  clip-path: inset(50%);
  white-space: nowrap;
  border: 0;
}
.visually-hidden:focus-visible + label.btn {
  outline: 2px solid var(--accent);
  outline-offset: 1px;
}
.btn:disabled { opacity: .45; cursor: default; }
.btn-primary {
  background: var(--accent);
  border-color: var(--accent);
  color: var(--accent-text);
  font-weight: 500;
}
.btn-primary:hover:not(:disabled) { filter: brightness(1.08); background: var(--accent); }
.btn-small { font-size: 12px; padding: 4px 9px; }
.btn-block { width: 100%; }
.btn-danger { color: var(--err); border-color: var(--border); }
.btn-danger:hover:not(:disabled) { background: var(--err-bg); }
.btn-icon {
  font: inherit;
  font-size: 13px;
  line-height: 1;
  padding: 4px 7px;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text-dim);
  border-radius: 4px;
  cursor: pointer;
}
.btn-icon:hover:not(:disabled) { background: var(--surface-2); color: var(--text); }
.btn-icon:disabled { opacity: .3; cursor: default; }

.banner {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 7px 16px;
  background: var(--warn-bg);
  border-bottom: 1px solid var(--border);
  font-size: 13px;
  flex: none;
}

/* ---------- layout ---------- */

.layout {
  flex: 1;
  display: grid;
  grid-template-columns: 290px 1fr;
  min-height: 0;
}

.sidebar {
  border-right: 1px solid var(--border);
  background: var(--surface);
  display: flex;
  flex-direction: column;
  min-height: 0;
}
.sidebar-head {
  display: flex;
  gap: 8px;
  align-items: center;
  padding: 8px 10px;
  border-bottom: 1px solid var(--border);
}
.search {
  flex: 1;
  min-width: 0;
  font: inherit;
  font-size: 13px;
  padding: 5px 8px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--bg);
  color: var(--text);
}
.q-count {
  font-size: 12px;
  color: var(--text-dim);
  font-variant-numeric: tabular-nums;
}
.sidebar-foot { padding: 8px 10px; border-top: 1px solid var(--border); }

.q-list {
  list-style: none;
  margin: 0;
  padding: 6px;
  overflow-y: auto;
  flex: 1;
}
.q-item {
  display: grid;
  grid-template-columns: auto 1fr auto;
  gap: 8px;
  align-items: start;
  padding: 8px 9px;
  border-radius: var(--radius);
  cursor: pointer;
  border: 1px solid transparent;
}
.q-item:hover { background: var(--surface-2); }
.q-item.is-active {
  background: var(--surface-2);
  border-color: var(--border-strong);
}
.q-num {
  font-size: 12px;
  color: var(--text-dim);
  font-variant-numeric: tabular-nums;
  padding-top: 1px;
}
.q-item-body { min-width: 0; }
.q-item-title {
  font-size: 13px;
  font-weight: 500;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.q-item-sub {
  font-size: 11px;
  color: var(--text-dim);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  margin-top: 1px;
}
.q-badge {
  font-size: 11px;
  line-height: 1;
  padding: 3px 5px;
  border-radius: 4px;
  font-weight: 600;
}
.q-badge.err { background: var(--err-bg); color: var(--err); }
.q-badge.warn { background: var(--warn-bg); color: var(--warn); }

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

.editor {
  overflow-y: auto;
  padding: 20px 28px 96px;
  min-height: 0;
}
.editor-inner { max-width: 900px; margin: 0 auto; }

.empty-state {
  max-width: 560px;
  margin: 12vh auto;
  text-align: center;
  color: var(--text-dim);
}
.empty-state h1 { font-size: 20px; color: var(--text); margin-bottom: 20px; }
.empty-actions {
  display: flex;
  gap: 10px;
  justify-content: center;
  flex-wrap: wrap;
  margin-bottom: 18px;
}
.empty-state code {
  font-family: var(--mono);
  font-size: .9em;
  background: var(--surface-2);
  padding: 1px 5px;
  border-radius: 3px;
}
.fineprint { font-size: 12px; margin-top: 20px; }

.q-head {
  display: flex;
  align-items: center;
  gap: 10px;
  margin-bottom: 16px;
  padding-bottom: 12px;
  border-bottom: 1px solid var(--border);
}
.q-head h2 { margin: 0; font-size: 17px; flex: 1; }
.q-head .pos { color: var(--text-dim); font-weight: 400; font-size: 14px; }

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 16px;
  margin-bottom: 16px;
}
.card > h3 {
  margin: 0 0 12px;
  font-size: 12px;
  text-transform: uppercase;
  letter-spacing: .06em;
  color: var(--text-dim);
}

.grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(220px, 1fr));
  gap: 12px 16px;
}

.field { display: flex; flex-direction: column; gap: 4px; min-width: 0; }
.field label {
  font-size: 12px;
  color: var(--text-dim);
  font-weight: 500;
}
.field .hint { font-size: 11px; color: var(--text-dim); }

input[type="text"], input[type="date"], input[type="search"], select, textarea {
  font: inherit;
  color: var(--text);
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 7px 9px;
  width: 100%;
  min-width: 0;
}
textarea { resize: vertical; line-height: 1.55; }
input:focus, select:focus, textarea:focus {
  outline: 2px solid var(--accent);
  outline-offset: -1px;
  border-color: var(--accent);
}
input[readonly] { background: var(--surface-2); color: var(--text-dim); }
.mono { font-family: var(--mono); font-size: 13px; }

/* ---------- options ---------- */

.opt {
  display: grid;
  grid-template-columns: auto 1fr;
  gap: 10px;
  padding: 12px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  margin-bottom: 10px;
  background: var(--bg);
}
.opt.is-correct {
  border-color: var(--ok);
  background: var(--ok-bg);
}
.opt-gutter {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 6px;
  padding-top: 2px;
}
.opt-label {
  font-weight: 700;
  font-size: 14px;
  width: 22px;
  text-align: center;
}
.opt-body { display: flex; flex-direction: column; gap: 8px; min-width: 0; }
.opt-row {
  display: flex;
  align-items: center;
  gap: 10px;
  font-size: 12px;
  color: var(--text-dim);
}
.opt-row label { display: flex; align-items: center; gap: 5px; cursor: pointer; }
.opt-row .spacer { flex: 1; }
.correct-tag { color: var(--ok); font-weight: 600; }

.list-row {
  display: flex;
  gap: 8px;
  align-items: center;
  margin-bottom: 8px;
}

/* ---------- authoring guidance ---------- */

.card-guidance { border-left: 3px solid var(--accent); }
.hint-guidance {
  color: var(--text-dim);
  font-size: 11.5px;
  line-height: 1.45;
  margin-bottom: 2px;
}
.checklist { list-style: none; margin: 0 0 12px; padding: 0; }
.checklist li {
  display: flex;
  gap: 8px;
  align-items: baseline;
  font-size: 13px;
  padding: 2px 0;
}
.checklist label { cursor: pointer; }
.checklist input:checked + label { color: var(--text-dim); text-decoration: line-through; }

.validation { display: flex; flex-direction: column; gap: 6px; }
.v-item {
  font-size: 13px;
  padding: 7px 10px;
  border-radius: 4px;
  border-left: 3px solid;
}
.v-item.err { background: var(--err-bg); border-color: var(--err); color: var(--err); }
.v-item.warn { background: var(--warn-bg); border-color: var(--warn); color: var(--warn); }
.v-item.ok { background: var(--ok-bg); border-color: var(--ok); color: var(--ok); }

details.raw { margin-bottom: 16px; }
details.raw > summary {
  cursor: pointer;
  font-size: 12px;
  color: var(--text-dim);
  text-transform: uppercase;
  letter-spacing: .06em;
  padding: 6px 0;
}
.raw-actions { display: flex; gap: 8px; margin-top: 8px; }
.raw-error { color: var(--err); font-size: 12px; align-self: center; }

.extra-row {
  display: grid;
  grid-template-columns: 200px 1fr auto;
  gap: 8px;
  align-items: start;
  margin-bottom: 8px;
}

/* ---------- overlays ---------- */

.drop-overlay {
  position: fixed;
  inset: 0;
  background: color-mix(in srgb, var(--accent) 22%, transparent);
  backdrop-filter: blur(2px);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 50;
  pointer-events: none;
}
.drop-overlay > div {
  background: var(--surface);
  border: 2px dashed var(--accent);
  border-radius: 10px;
  padding: 28px 48px;
  font-size: 17px;
  font-weight: 600;
}

.toast {
  position: fixed;
  bottom: 20px;
  left: 50%;
  transform: translateX(-50%);
  background: var(--text);
  color: var(--bg);
  padding: 9px 16px;
  border-radius: var(--radius);
  font-size: 13px;
  z-index: 60;
  box-shadow: 0 4px 14px rgba(0,0,0,.25);
  max-width: 80vw;
}
.toast.is-error { background: var(--err); color: #fff; }

@media (max-width: 820px) {
  .layout { grid-template-columns: 1fr; grid-template-rows: 180px 1fr; }
  .sidebar { border-right: none; border-bottom: 1px solid var(--border); }
  .editor { padding: 16px; }
  .topbar { flex-wrap: wrap; }
  .file-state { order: 3; width: 100%; }
}
