/* CDD Builder - layout and look. Logical properties keep RTL and LTR right. */

:root {
  --font: Vazirmatn, 'Noto Sans Arabic', 'Noto Naskh Arabic', Inter, Tahoma, system-ui, sans-serif;
  --bg: #f4f1ea;
  --surface: #fffdf9;
  --surface-2: #f8f5ef;
  --ink: #1e2430;
  --muted: #646b78;
  --rule: #e2ddd2;
  --accent: #2e6da4;
  --accent-ink: #ffffff;
  --accent-soft: #e3eef8;
  --lever: #e9a877;
  --lever-soft: #fde6d2;
  --inter: #e08178;
  --inter-soft: #fad6d2;
  --outcome: #76aedb;
  --outcome-soft: #daebf8;
  --objective: #1f4e79;
  --external: #eb9d36;
  --external-soft: #ffe4bd;
  --ok: #2f8a5b;
  --ok-soft: #dff2e7;
  --warn: #b7791f;
  --warn-soft: #fbefd6;
  --err: #c0392b;
  --err-soft: #fbe1dd;
  --shadow: 0 1px 2px rgba(30, 36, 48, 0.06), 0 6px 20px rgba(30, 36, 48, 0.06);
  --radius: 14px;
  color-scheme: light;
}

:root[data-theme='dark'] {
  --bg: #0f131a;
  --surface: #171c25;
  --surface-2: #1d2330;
  --ink: #e6e9ef;
  --muted: #9aa3b2;
  --rule: #2a3140;
  --accent: #5ea3df;
  --accent-ink: #0f131a;
  --accent-soft: #1d3247;
  --lever-soft: #3d2a1b;
  --inter-soft: #3e2223;
  --outcome-soft: #1b3047;
  --external-soft: #3d2c14;
  --ok: #4fc185;
  --ok-soft: #173326;
  --warn: #e5ad4f;
  --warn-soft: #3a2c13;
  --err: #ef7b6c;
  --err-soft: #3d1d1a;
  --shadow: 0 1px 2px rgba(0, 0, 0, 0.3), 0 6px 20px rgba(0, 0, 0, 0.25);
  color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  font-family: var(--font);
  background: var(--bg);
  color: var(--ink);
  font-size: 15px;
  line-height: 1.7;
  -webkit-font-smoothing: antialiased;
}
button, input, select, textarea { font: inherit; color: inherit; }
:focus-visible { outline: 2.5px solid var(--accent); outline-offset: 2px; }
[hidden] { display: none !important; }

/* ---------- header ---------- */

.topbar {
  position: sticky;
  top: 0;
  z-index: 20;
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 10px 20px;
  background: color-mix(in srgb, var(--surface) 92%, transparent);
  backdrop-filter: blur(8px);
  border-block-end: 1px solid var(--rule);
}
.brand { display: flex; align-items: center; gap: 10px; min-width: 0; }
.brand-mark { width: 30px; height: 30px; flex: none; }
.brand h1 { font-size: 17px; margin: 0; white-space: nowrap; }
.brand p { margin: 0; font-size: 12.5px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.topbar .spacer { flex: 1; }
.toolbar { display: flex; flex-wrap: wrap; gap: 6px; align-items: center; justify-content: flex-end; }
.saved { font-size: 12px; color: var(--muted); margin-inline-end: 6px; white-space: nowrap; }

.btn {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  border: 1px solid var(--rule);
  background: var(--surface);
  color: var(--ink);
  border-radius: 10px;
  padding: 6px 12px;
  cursor: pointer;
  font-size: 13.5px;
  line-height: 1.5;
  white-space: nowrap;
}
.btn:hover { border-color: var(--accent); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); font-weight: 600; }
.btn.ghost { background: transparent; }
.btn.small { padding: 3px 9px; font-size: 12.5px; border-radius: 8px; }
.btn.danger:hover { border-color: var(--err); color: var(--err); }
.btn[disabled] { opacity: 0.5; cursor: default; }

/* ---------- shell ---------- */

.shell {
  display: grid;
  grid-template-columns: 250px minmax(0, 1fr) minmax(0, 0.95fr);
  gap: 20px;
  padding: 20px;
  max-width: 1720px;
  margin: 0 auto;
}
.shell.no-preview { grid-template-columns: 250px minmax(0, 1fr); }
.shell.wide { grid-template-columns: 250px minmax(0, 1fr); }

.stepper {
  position: sticky;
  top: 76px;
  align-self: start;
  background: var(--surface);
  border: 1px solid var(--rule);
  border-radius: var(--radius);
  padding: 10px;
  box-shadow: var(--shadow);
}
.stepper ol { list-style: none; margin: 0; padding: 0; }
.stepper li button {
  width: 100%;
  display: grid;
  grid-template-columns: 30px 1fr;
  gap: 8px;
  align-items: center;
  text-align: start;
  border: 0;
  background: transparent;
  padding: 7px 8px;
  border-radius: 10px;
  cursor: pointer;
}
.stepper li button:hover { background: var(--surface-2); }
.stepper .num {
  width: 28px; height: 28px;
  border-radius: 50%;
  display: grid; place-items: center;
  font-size: 13px; font-weight: 700;
  background: var(--surface-2);
  border: 1.5px solid var(--rule);
  color: var(--muted);
}
.stepper .label { display: block; font-size: 14px; font-weight: 600; line-height: 1.35; }
.stepper .sub { display: block; font-size: 11.5px; color: var(--muted); line-height: 1.35; }
.stepper li.done .num { background: var(--ok-soft); border-color: var(--ok); color: var(--ok); }
.stepper li.current button { background: var(--accent-soft); }
.stepper li.current .num { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }

.panel {
  background: var(--surface);
  border: 1px solid var(--rule);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 26px 28px 22px;
  min-width: 0;
}
.panel-head { margin-block-end: 18px; }
.panel-head .kicker { font-size: 12.5px; color: var(--muted); }
.panel-head h2 { margin: 2px 0 6px; font-size: 24px; line-height: 1.35; }
.panel-head p { margin: 0; color: var(--muted); max-width: 70ch; }

.nav {
  display: flex;
  justify-content: space-between;
  gap: 10px;
  margin-block-start: 26px;
  padding-block-start: 16px;
  border-block-start: 1px solid var(--rule);
}

/* ---------- preview ---------- */

.preview {
  position: sticky;
  top: 76px;
  align-self: start;
  background: var(--surface);
  border: 1px solid var(--rule);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  overflow: hidden;
  min-width: 0;
}
.preview header {
  display: flex; align-items: center; justify-content: space-between;
  padding: 10px 14px; border-block-end: 1px solid var(--rule);
  font-weight: 600; font-size: 14px;
}
.preview .canvas { max-height: calc(100vh - 150px); overflow: auto; direction: ltr; }
.preview .canvas svg { display: block; width: 100%; height: auto; }
.preview .empty { padding: 30px 18px; color: var(--muted); text-align: center; font-size: 14px; }

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

.field { margin-block-end: 16px; }
.field > label, .field > .label { display: block; font-weight: 600; margin-block-end: 6px; font-size: 14.5px; }
.field .hint { font-size: 12.5px; color: var(--muted); margin-block-start: 4px; }
input[type='text'], input[type='number'], select, textarea {
  width: 100%;
  border: 1px solid var(--rule);
  background: var(--surface-2);
  border-radius: 10px;
  padding: 8px 11px;
  font-size: 14.5px;
  line-height: 1.6;
}
textarea { min-height: 80px; resize: vertical; }
input:focus, select:focus, textarea:focus { outline: none; border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft); }
.row { display: grid; gap: 10px; grid-template-columns: repeat(auto-fit, minmax(140px, 1fr)); }

.choice { display: flex; flex-wrap: wrap; gap: 8px; }
.choice label {
  display: flex; align-items: center; gap: 8px;
  border: 1px solid var(--rule); border-radius: 10px; padding: 8px 12px; cursor: pointer;
  background: var(--surface-2); font-size: 14px;
}
.choice label:has(input:checked) { border-color: var(--accent); background: var(--accent-soft); }

.seg { display: inline-flex; border: 1px solid var(--rule); border-radius: 10px; overflow: hidden; flex: none; }
.seg button { border: 0; background: var(--surface-2); padding: 5px 12px; cursor: pointer; font-size: 13px; }
.seg button + button { border-inline-start: 1px solid var(--rule); }
.seg button.on { background: var(--accent); color: var(--accent-ink); font-weight: 600; }
.seg button.on.no { background: var(--muted); }

.tip {
  border-inline-start: 3px solid var(--accent);
  background: var(--accent-soft);
  border-radius: 10px;
  padding: 10px 14px;
  font-size: 13.5px;
  margin: 12px 0 18px;
}
details.why { margin: 4px 0 16px; }
details.why summary { cursor: pointer; color: var(--accent); font-size: 13px; }
details.why div { font-size: 13.5px; color: var(--muted); padding: 6px 2px 0; }

.prompts { display: flex; flex-wrap: wrap; gap: 8px; margin: 4px 0 16px; padding: 0; list-style: none; }
.prompts li {
  font-size: 13px; padding: 5px 11px; border-radius: 999px;
  background: var(--surface-2); border: 1px dashed var(--rule); color: var(--muted);
}

.adder { display: flex; gap: 8px; margin-block-end: 14px; }
.adder input { flex: 1; }

.list { display: flex; flex-direction: column; gap: 8px; }
.item {
  display: grid;
  grid-template-columns: auto minmax(0, 1fr) auto;
  gap: 10px;
  align-items: center;
  border: 1px solid var(--rule);
  border-inline-start: 5px solid var(--rule);
  border-radius: 12px;
  padding: 8px 10px;
  background: var(--surface);
}
.item.t-lever { border-inline-start-color: var(--lever); }
.item.t-outcome { border-inline-start-color: var(--outcome); }
.item.t-external { border-inline-start-color: var(--external); }
.item.t-intermediate { border-inline-start-color: var(--inter); }
.item input[type='text'] { background: transparent; border-color: transparent; padding: 4px 6px; }
.item input[type='text']:hover { border-color: var(--rule); }
.item .actions { display: flex; flex-wrap: wrap; gap: 4px; justify-content: flex-end; }
.item .warn-line { grid-column: 2 / -1; font-size: 12.5px; color: var(--warn); margin-top: -4px; }
.item .extra { grid-column: 1 / -1; }
.star {
  border: 0; background: transparent; cursor: pointer; font-size: 20px; line-height: 1;
  color: var(--rule); padding: 2px 4px;
}
.star.on { color: #e9a400; }
.empty-note { color: var(--muted); font-size: 14px; padding: 8px 2px; }
.count-note { font-size: 13px; color: var(--muted); margin: 6px 0 10px; }

.card {
  border: 1px solid var(--rule);
  border-radius: 14px;
  padding: 16px 18px;
  margin-block-end: 14px;
  background: var(--surface);
}
.card h3 { margin: 0 0 10px; font-size: 16.5px; display: flex; align-items: center; gap: 8px; }
.card.t-outcome { border-block-start: 4px solid var(--outcome); }
.card.t-lever { border-block-start: 4px solid var(--lever); }
.card.t-external { border-block-start: 4px solid var(--external); }
.card .inline { display: flex; gap: 8px; align-items: center; }
.card .inline input { flex: 1; }
.pill {
  display: inline-block; font-size: 11.5px; font-weight: 700; padding: 1px 9px; border-radius: 999px;
  background: var(--surface-2); color: var(--muted); border: 1px solid var(--rule);
}
.pill.lever { background: var(--lever-soft); border-color: var(--lever); color: inherit; }
.pill.outcome { background: var(--outcome-soft); border-color: var(--outcome); color: inherit; }
.pill.external { background: var(--external-soft); border-color: var(--external); color: inherit; }
.pill.intermediate { background: var(--inter-soft); border-color: var(--inter); color: inherit; }

.qrow {
  display: flex; justify-content: space-between; align-items: center; gap: 14px;
  padding: 10px 0; border-block-end: 1px solid var(--rule);
}
.qrow span { flex: 1; }
.group-title { font-weight: 700; margin: 18px 0 4px; font-size: 14.5px; }

.verdict { border-radius: 12px; padding: 12px 16px; margin-top: 18px; font-weight: 600; }
.verdict.fit { background: var(--ok-soft); color: var(--ok); }
.verdict.notDecision { background: var(--err-soft); color: var(--err); }
.verdict.tooSimple { background: var(--warn-soft); color: var(--warn); }
.verdict.incomplete { background: var(--surface-2); color: var(--muted); }

.pair { border: 1px solid var(--rule); border-radius: 14px; padding: 14px 16px; margin-block-end: 12px; }
.pair .q { font-weight: 600; margin-block-end: 8px; }
.pair .status { font-size: 13px; color: var(--ok); }
.pair .path { font-size: 13px; color: var(--muted); margin-top: 4px; }

table.links { width: 100%; border-collapse: collapse; font-size: 13.5px; }
table.links th { text-align: start; font-size: 12px; color: var(--muted); font-weight: 600; padding: 6px; }
table.links td { border-block-start: 1px solid var(--rule); padding: 6px; vertical-align: middle; }
table.links select { padding: 4px 6px; font-size: 13px; }
.table-wrap { overflow-x: auto; }

.chips { display: flex; flex-wrap: wrap; gap: 6px; }
.chips button {
  border: 1px solid var(--rule); background: var(--surface-2); border-radius: 999px;
  padding: 3px 11px; font-size: 13px; cursor: pointer;
}
.chips button.on { background: var(--accent-soft); border-color: var(--accent); font-weight: 600; }

.stats { display: grid; grid-template-columns: repeat(auto-fit, minmax(96px, 1fr)); gap: 10px; margin: 6px 0 20px; }
.stat { border: 1px solid var(--rule); border-radius: 12px; padding: 10px 12px; background: var(--surface-2); }
.stat b { display: block; font-size: 22px; line-height: 1.2; }
.stat span { font-size: 12.5px; color: var(--muted); }
.issue {
  display: grid; grid-template-columns: auto 1fr auto; gap: 10px; align-items: start;
  padding: 10px 12px; border-radius: 12px; margin-block-end: 8px; font-size: 14px;
}
.issue.error { background: var(--err-soft); }
.issue.warn { background: var(--warn-soft); }
.issue.info { background: var(--surface-2); }
.issue .lvl { font-size: 11.5px; font-weight: 700; padding: 1px 8px; border-radius: 999px; white-space: nowrap; margin-top: 2px; }
.issue.error .lvl { background: var(--err); color: #fff; }
.issue.warn .lvl { background: var(--warn); color: #fff; }
.issue.info .lvl { background: var(--muted); color: #fff; }

/* ---------- CDD view ---------- */

.cdd-tools { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; margin-block-end: 12px; }
.cdd-tools .sep { width: 1px; height: 22px; background: var(--rule); margin: 0 4px; }
.cdd-frame {
  direction: ltr; /* the diagram flows left to right; scrolling starts at the levers */
  border: 1px solid var(--rule);
  border-radius: 14px;
  overflow: auto;
  max-height: 78vh;
  background: var(--surface-2);
}
.cdd-frame svg { display: block; margin: 0 auto; }
.note { font-size: 12.5px; color: var(--muted); margin: 8px 2px 0; }
.summary { margin-top: 24px; }
.summary h3 { font-size: 17px; margin: 0 0 10px; }
.summary dl { margin: 0; display: grid; grid-template-columns: max-content 1fr; gap: 6px 16px; font-size: 14px; }
.summary dt { color: var(--muted); }
.summary dd { margin: 0; }
.summary ul { margin: 0; padding-inline-start: 18px; }

/* ---------- parking drawer ---------- */

.parking-fab {
  position: fixed; inset-block-end: 18px; inset-inline-end: 18px; z-index: 30;
  border-radius: 999px; padding: 10px 16px; box-shadow: var(--shadow);
}
.parking-fab .badge {
  background: var(--accent); color: var(--accent-ink); border-radius: 999px; padding: 0 7px; font-size: 12px;
}
.drawer {
  position: fixed; inset-block: 0; inset-inline-end: 0; width: min(380px, 100%); z-index: 40;
  background: var(--surface); border-inline-start: 1px solid var(--rule); box-shadow: var(--shadow);
  padding: 18px; display: flex; flex-direction: column; gap: 12px;
}
.drawer header { display: flex; justify-content: space-between; align-items: center; }
.drawer h3 { margin: 0; font-size: 17px; }
.drawer .items { overflow: auto; display: flex; flex-direction: column; gap: 8px; }
.drawer .pk { border: 1px solid var(--rule); border-radius: 12px; padding: 10px; font-size: 14px; }
.drawer .pk .actions { display: flex; flex-wrap: wrap; gap: 4px; margin-top: 8px; }
.scrim { position: fixed; inset: 0; background: rgba(0, 0, 0, 0.25); z-index: 35; }

.toast {
  position: fixed; inset-block-end: 20px; left: 50%; transform: translateX(-50%); z-index: 50;
  background: var(--ink); color: var(--bg); padding: 8px 16px; border-radius: 10px; font-size: 14px;
}

/* ---------- pilot server ---------- */

.who { font-size: 13px; color: var(--muted); white-space: nowrap; }
.picker { width: auto; max-width: 240px; padding: 5px 10px; font-size: 13.5px; }
.saved.error { color: var(--err); font-weight: 600; }
.gate { max-width: 720px; margin: 40px auto; }
.gate h2 { font-size: 26px; margin: 0 0 12px; }
.gate p, .gate li { font-size: 15.5px; }
.gate ul { padding-inline-start: 20px; }
.gate .note-box { background: var(--surface-2); border: 1px solid var(--rule); border-radius: 12px; padding: 14px 16px; margin: 16px 0; }
body.gated .stepper, body.gated .preview, body.gated .parking-fab,
body.gated #picker, body.gated #btnNew, body.gated #btnOpen, body.gated #btnSave { display: none !important; }
body.gated .shell { grid-template-columns: minmax(0, 1fr) !important; }
.feedback { margin-top: 14px; border: 1px dashed var(--rule); border-radius: 12px; padding: 12px 14px; }
.feedback textarea { min-height: 70px; margin: 8px 0; }

/* ---------- responsive ---------- */

@media (max-width: 1280px) {
  .shell { grid-template-columns: 220px minmax(0, 1fr); }
  .preview { grid-column: 1 / -1; position: static; }
  .preview .canvas { max-height: 70vh; }
}
@media (max-width: 860px) {
  .topbar { flex-wrap: wrap; padding: 10px 14px; }
  .topbar .spacer { display: none; }
  .toolbar { width: 100%; justify-content: flex-start; }
  .shell, .shell.no-preview, .shell.wide { grid-template-columns: minmax(0, 1fr); padding: 12px; gap: 12px; }
  .stepper { position: static; padding: 6px; overflow-x: auto; }
  .stepper ol { display: flex; gap: 4px; }
  .stepper li button { grid-template-columns: 28px; justify-items: center; width: auto; padding: 6px; }
  .stepper .label, .stepper .sub { display: none; }
  .stepper li.current .label { display: block; font-size: 12px; white-space: nowrap; }
  .stepper li.current button { grid-template-columns: 28px auto; }
  .panel { padding: 18px 16px; }
  .panel-head h2 { font-size: 20px; }
  .item { grid-template-columns: auto minmax(0, 1fr); }
  .item .actions { grid-column: 1 / -1; justify-content: flex-start; }
  .qrow { flex-direction: column; align-items: stretch; }
  .summary dl { grid-template-columns: 1fr; }
}

/* ---------- print: the diagram and summary only ---------- */

@media print {
  .topbar, .stepper, .preview, .nav, .parking-fab, .cdd-tools, .panel-head p, .note, .drawer, .scrim { display: none !important; }
  body { background: #fff; }
  .shell { display: block; padding: 0; }
  .panel { border: 0; box-shadow: none; padding: 0; }
  .cdd-frame { max-height: none; overflow: visible; border: 0; }
  .cdd-frame svg { width: 100% !important; height: auto !important; }
}
