/* MO OS Control Center — single theme source (tokens), then layout, then components. RTL-first; logical properties only. */
:root {
  --font: -apple-system, "SF Arabic", "Segoe UI", "Noto Sans Arabic", "Noto Sans", Tahoma, sans-serif;
  --mono: ui-monospace, "SF Mono", Menlo, Consolas, monospace;
  --s1: 4px; --s2: 8px; --s3: 12px; --s4: 16px; --s5: 24px; --s6: 32px; --s7: 48px;
  --r1: 6px; --r2: 10px; --r3: 14px;
  --t-xs: 12px; --t-sm: 13px; --t-md: 15px; --t-lg: 18px; --t-xl: 24px;
  --bg: #f6f5f2; --surface: #ffffff; --surface-2: #f0efeb; --ink: #1d1f22; --muted: #63676e; --line: #e2e0da; --line-2: #d2cfc7;
  --accent: #2f5d50; --accent-ink: #ffffff; --accent-weak: #e2ede8;
  --ok: #2f6b3a; --ok-weak: #e4f1e6; --warn: #8a5a00; --warn-weak: #fbf0d4; --danger: #a4262c; --danger-weak: #fbe6e6;
  --ai: #4b4fa8; --ai-weak: #ecedfb; --test: #7a3e9d; --test-weak: #f3e8fa;
  --shadow: 0 1px 2px rgba(0,0,0,.05), 0 4px 16px rgba(0,0,0,.06);
  --focus: 0 0 0 3px color-mix(in srgb, var(--accent) 45%, transparent);
  color-scheme: light;
}
:root[data-theme="dark"] {
  --bg: #111315; --surface: #191c1f; --surface-2: #202428; --ink: #e9e7e2; --muted: #9ba1a8; --line: #2b3035; --line-2: #3a4047;
  --accent: #7cc0a8; --accent-ink: #0d1f19; --accent-weak: #1d2e28;
  --ok: #8fd19e; --ok-weak: #1b2b1f; --warn: #e7c06a; --warn-weak: #2e2716; --danger: #f08a8f; --danger-weak: #331c1e;
  --ai: #a9acf5; --ai-weak: #23244a; --test: #d6a6f0; --test-weak: #2c2036;
  --shadow: 0 1px 2px rgba(0,0,0,.3), 0 6px 20px rgba(0,0,0,.35);
  color-scheme: dark;
}
@media (prefers-reduced-motion: reduce) { * { animation: none !important; transition: none !important; scroll-behavior: auto !important; } }

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body { font: var(--t-md)/1.55 var(--font); background: var(--bg); color: var(--ink); -webkit-font-smoothing: antialiased; }
a { color: var(--accent); }
button, input, select, textarea { font: inherit; color: inherit; }
:focus-visible { outline: none; box-shadow: var(--focus); border-radius: var(--r1); }
.sr-only { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }
.mono { font-family: var(--mono); font-size: var(--t-xs); direction: ltr; unicode-bidi: isolate; }
.muted { color: var(--muted); }
.small { font-size: var(--t-sm); }
.xs { font-size: var(--t-xs); }
.row { display: flex; align-items: center; gap: var(--s2); flex-wrap: wrap; }
.spread { justify-content: space-between; }
.stack > * + * { margin-block-start: var(--s3); }
.grow { flex: 1; min-width: 0; }
.hidden { display: none !important; }

/* ---------- shell */
.app { display: grid; grid-template-columns: 232px minmax(0, 1fr); grid-template-rows: 56px 1fr; height: 100vh; }
.top { grid-column: 1 / -1; min-width: 0; overflow: hidden; display: flex; align-items: center; gap: var(--s3); padding-inline: var(--s4); border-block-end: 1px solid var(--line); background: var(--surface); }
.brand { font-weight: 700; display: flex; align-items: center; gap: var(--s2); min-width: 200px; }
.brand-dot { width: 10px; height: 10px; border-radius: 50%; background: var(--accent); }
.nav { border-inline-end: 1px solid var(--line); background: var(--surface); padding: var(--s3); overflow-y: auto; }
.nav a { display: flex; align-items: center; gap: var(--s3); padding: var(--s2) var(--s3); border-radius: var(--r2); color: var(--ink); text-decoration: none; font-size: var(--t-md); }
.nav a:hover { background: var(--surface-2); }
.nav a[aria-current="page"] { background: var(--accent-weak); color: var(--accent); font-weight: 600; }
.nav .ico { width: 20px; text-align: center; }
.nav .kbd { margin-inline-start: auto; }
.nav-foot { margin-block-start: var(--s5); padding: var(--s3); border-block-start: 1px solid var(--line); }
main { overflow-y: auto; overflow-x: hidden; min-width: 0; padding: var(--s5) var(--s6); }
main:focus, main:focus-visible { outline: none; box-shadow: none; }
.page-head { display: flex; align-items: flex-end; justify-content: space-between; gap: var(--s4); margin-block-end: var(--s5); flex-wrap: wrap; }
.page-head h1 { margin: 0; font-size: var(--t-xl); line-height: 1.2; }

/* ---------- primitives */
.btn { display: inline-flex; align-items: center; gap: var(--s2); padding: 7px 14px; border-radius: var(--r2); border: 1px solid var(--line-2); background: var(--surface); cursor: pointer; font-size: var(--t-sm); white-space: nowrap; }
.btn:hover { background: var(--surface-2); }
.btn.primary { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); }
.btn.primary:hover { filter: brightness(1.06); }
.btn.ghost { border-color: transparent; background: transparent; }
.btn.danger { color: var(--danger); border-color: color-mix(in srgb, var(--danger) 40%, var(--line)); }
.btn[disabled], .btn[aria-disabled="true"] { opacity: .55; cursor: not-allowed; }
.btn.sm { padding: 4px 10px; font-size: var(--t-xs); }
.kbd { font: var(--t-xs) var(--mono); padding: 1px 6px; border: 1px solid var(--line-2); border-block-end-width: 2px; border-radius: 5px; color: var(--muted); direction: ltr; }
.input, .select, textarea.input { width: 100%; padding: 8px 12px; border: 1px solid var(--line-2); border-radius: var(--r2); background: var(--surface); }
textarea.input { resize: vertical; min-height: 44px; }
.field { display: grid; gap: var(--s1); }
.field label { font-size: var(--t-sm); color: var(--muted); }
.card { background: var(--surface); border: 1px solid var(--line); border-radius: var(--r3); padding: var(--s4) var(--s5); }
.card h2 { margin: 0 0 var(--s3); font-size: var(--t-md); display: flex; align-items: center; gap: var(--s2); }
.card h2 .count { color: var(--muted); font-weight: 400; }
.grid { display: grid; gap: var(--s4); }
.g2 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
.g3 { grid-template-columns: repeat(3, minmax(0, 1fr)); }
.span2 { grid-column: span 2; }
.chip { display: inline-flex; align-items: center; gap: 4px; padding: 1px 8px; border-radius: 999px; font-size: var(--t-xs); background: var(--surface-2); color: var(--muted); border: 1px solid var(--line); white-space: nowrap; }
.chip.ok { background: var(--ok-weak); color: var(--ok); border-color: transparent; }
.chip.warn { background: var(--warn-weak); color: var(--warn); border-color: transparent; }
.chip.danger { background: var(--danger-weak); color: var(--danger); border-color: transparent; }
.chip.ai { background: var(--ai-weak); color: var(--ai); border-color: transparent; }
.chip.test { background: var(--test-weak); color: var(--test); border-color: transparent; }
.chip.accent { background: var(--accent-weak); color: var(--accent); border-color: transparent; }
.list { list-style: none; margin: 0; padding: 0; }
.list.card { padding: var(--s1) var(--s5); }
.list > li { padding: var(--s3) 0; border-block-end: 1px solid var(--line); }
.list > li:last-child { border-block-end: 0; }
.item-btn { all: unset; display: block; width: 100%; cursor: pointer; border-radius: var(--r1); }
.item-btn:hover .item-title { color: var(--accent); }
.item-btn:focus-visible { box-shadow: var(--focus); }
.item-title { font-weight: 600; }
.excerpt { color: var(--muted); font-size: var(--t-sm); display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden; }
.fresh { font-size: var(--t-xs); color: var(--muted); display: inline-flex; gap: var(--s2); align-items: center; flex-wrap: wrap; }
.notice { padding: var(--s3) var(--s4); border-radius: var(--r2); background: var(--warn-weak); color: var(--warn); font-size: var(--t-sm); }
.notice.danger { background: var(--danger-weak); color: var(--danger); }
.notice.info { background: var(--accent-weak); color: var(--accent); }
.empty { padding: var(--s5); text-align: center; color: var(--muted); font-size: var(--t-sm); }
.skel { height: 14px; border-radius: 6px; background: linear-gradient(90deg, var(--surface-2), var(--line), var(--surface-2)); background-size: 200% 100%; animation: sk 1.2s infinite; margin-block: 8px; }
@keyframes sk { to { background-position: -200% 0; } }
table.tbl { width: 100%; border-collapse: collapse; font-size: var(--t-sm); }
.tbl th, .tbl td { text-align: start; padding: 8px 10px; border-block-end: 1px solid var(--line); vertical-align: top; }
.tbl th { color: var(--muted); font-weight: 500; font-size: var(--t-xs); }
.live-dot { width: 8px; height: 8px; border-radius: 50%; background: var(--muted); display: inline-block; }
.live-dot.on { background: var(--ok); box-shadow: 0 0 0 3px var(--ok-weak); }
.live-dot.off { background: var(--danger); }
.live-dot.wait { background: var(--warn); }
.pill-group { display: inline-flex; border: 1px solid var(--line-2); border-radius: var(--r2); overflow: hidden; }
.pill-group button { border: 0; background: var(--surface); padding: 6px 12px; cursor: pointer; font-size: var(--t-sm); }
.pill-group button[aria-pressed="true"] { background: var(--accent); color: var(--accent-ink); }
details > summary { cursor: pointer; color: var(--muted); font-size: var(--t-sm); }
pre.body { white-space: pre-wrap; font: inherit; font-size: var(--t-sm); background: var(--surface-2); padding: var(--s3); border-radius: var(--r2); margin: 0; max-height: 360px; overflow: auto; }

/* ---------- drawer, palette, toasts, dialog */
.scrim { position: fixed; inset: 0; background: rgba(0,0,0,.25); z-index: 40; }
.drawer { position: fixed; inset-block: 0; inset-inline-end: 0; width: min(560px, 100vw); background: var(--surface); border-inline-start: 1px solid var(--line); box-shadow: var(--shadow); z-index: 41; display: flex; flex-direction: column; }
.drawer-head { display: flex; align-items: center; gap: var(--s2); padding: var(--s4) var(--s5); border-block-end: 1px solid var(--line); }
.drawer-head h2 { margin: 0; font-size: var(--t-lg); flex: 1; }
.drawer-body { padding: var(--s4) var(--s5); overflow-y: auto; flex: 1; }
.palette { position: fixed; inset-inline: 0; inset-block-start: 12vh; margin-inline: auto; width: min(640px, 94vw); background: var(--surface); border: 1px solid var(--line); border-radius: var(--r3); box-shadow: var(--shadow); z-index: 51; overflow: hidden; }
.palette input { border: 0; border-block-end: 1px solid var(--line); border-radius: 0; padding: var(--s4); font-size: var(--t-lg); }
.palette ul { list-style: none; margin: 0; padding: var(--s2); max-height: 50vh; overflow: auto; }
.palette li { padding: 10px 12px; border-radius: var(--r2); cursor: pointer; display: flex; gap: var(--s3); align-items: center; }
.palette li[aria-selected="true"] { background: var(--accent-weak); color: var(--accent); }
.palette li[aria-disabled="true"] { opacity: .55; cursor: not-allowed; }
.palette .hint { margin-inline-start: auto; font-size: var(--t-xs); color: var(--muted); }
.toasts { position: fixed; inset-block-end: var(--s4); inset-inline-start: var(--s4); display: grid; gap: var(--s2); z-index: 60; }
.toast { background: var(--ink); color: var(--bg); padding: 10px 14px; border-radius: var(--r2); font-size: var(--t-sm); max-width: 420px; box-shadow: var(--shadow); }
.toast.danger { background: var(--danger); color: #fff; }
dialog { border: 1px solid var(--line); border-radius: var(--r3); background: var(--surface); color: var(--ink); padding: 0; width: min(520px, 94vw); box-shadow: var(--shadow); }
dialog::backdrop { background: rgba(0,0,0,.3); }
dialog .d-head { padding: var(--s4) var(--s5); border-block-end: 1px solid var(--line); font-weight: 700; }
dialog .d-body { padding: var(--s4) var(--s5); }
dialog .d-foot { padding: var(--s3) var(--s5); border-block-start: 1px solid var(--line); display: flex; gap: var(--s2); justify-content: flex-end; }

/* ---------- chat */
.chat { display: grid; grid-template-rows: 1fr auto; height: calc(100vh - 56px - 2 * var(--s5) - 80px); min-height: 420px; }
.msgs { overflow-y: auto; padding: var(--s2); display: grid; gap: var(--s3); align-content: start; }
.msg { max-width: 78%; padding: var(--s3) var(--s4); border-radius: var(--r3); white-space: pre-wrap; font-size: var(--t-md); }
.msg.me { background: var(--accent); color: var(--accent-ink); justify-self: start; }
.msg.brain { background: var(--surface); border: 1px solid var(--line); justify-self: end; }
.msg .meta { margin-block-start: var(--s2); font-size: var(--t-xs); opacity: .8; display: flex; gap: var(--s2); flex-wrap: wrap; }
.composer { display: flex; gap: var(--s2); padding-block-start: var(--s3); align-items: flex-end; }

/* ---------- studio */
.studio { display: grid; grid-template-columns: 280px 1fr 280px; gap: 0; height: calc(100vh - 56px); margin: calc(-1 * var(--s5)) calc(-1 * var(--s6)); }
.st-pane { background: var(--surface); border-inline-end: 1px solid var(--line); display: flex; flex-direction: column; min-height: 0; }
.st-pane.st-inspector { border-inline-end: 0; border-inline-start: 1px solid var(--line); }
.st-pane > header { padding: var(--s3) var(--s4); border-block-end: 1px solid var(--line); display: grid; gap: var(--s2); }
.st-pane > .body { padding: var(--s3) var(--s4); overflow-y: auto; overflow-x: hidden; flex: 1; }
.st-pane .btn { max-width: 100%; white-space: normal; overflow-wrap: anywhere; text-align: start; }
.st-center { display: flex; flex-direction: column; min-width: 0; min-height: 0; }
.st-toolbar { display: flex; gap: var(--s2); align-items: center; padding: var(--s2) var(--s3); border-block-end: 1px solid var(--line); background: var(--surface); flex-wrap: wrap; }
.canvas-wrap { position: relative; flex: 1; overflow: hidden; background-color: var(--bg); background-image: radial-gradient(var(--line) 1px, transparent 1px); background-size: 22px 22px; touch-action: none; }
.canvas-wrap:focus-visible { box-shadow: inset var(--focus); }
.world { position: absolute; top: 0; left: 0; transform-origin: 0 0; direction: ltr; }
svg.edges { position: absolute; left: 0; top: 0; overflow: visible; pointer-events: none; }
svg.edges path { fill: none; stroke: var(--line-2); stroke-width: 2; }
svg.edges path.proposed_relation { stroke: var(--accent); stroke-dasharray: 6 4; }
svg.edges path.proposed_dependency { stroke: var(--warn); stroke-dasharray: 2 4; stroke-width: 2.5; }
svg.edges text { font-size: 11px; fill: var(--muted); }
.node { position: absolute; width: 220px; background: var(--surface); border: 1px solid var(--line-2); border-radius: var(--r2); padding: 8px 10px 10px; box-shadow: var(--shadow); cursor: grab; direction: rtl; user-select: none; }
.node:focus-visible { box-shadow: var(--focus); }
.node.sel { border-color: var(--accent); box-shadow: 0 0 0 2px var(--accent); }
.node.ai { border-style: dashed; border-color: var(--ai); background: var(--ai-weak); }
.node.rejected { opacity: .45; }
.node .n-type { font-size: 11px; font-weight: 700; color: var(--muted); display: flex; gap: 6px; align-items: center; }
.node .n-text { font-size: var(--t-sm); margin-block-start: 4px; white-space: pre-wrap; word-break: break-word; max-height: 140px; overflow: hidden; }
.node[data-type="problem"] { border-inline-start: 4px solid #b5484e; }
.node[data-type="idea"] { border-inline-start: 4px solid #2f7d68; }
.node[data-type="option"] { border-inline-start: 4px solid #3466a8; }
.node[data-type="risk"] { border-inline-start: 4px solid #b06d00; }
.node[data-type="evidence"] { border-inline-start: 4px solid #5d6b2f; }
.node[data-type="question"] { border-inline-start: 4px solid #7a3e9d; }
.node[data-type="decision"] { border-inline-start: 4px solid #555a60; }
.node[data-type="action"] { border-inline-start: 4px solid #2f5d50; }
.node[data-type="note"] { border-inline-start: 4px solid #8a8f96; }
.group-box { position: absolute; border: 1.5px dashed var(--line-2); border-radius: var(--r3); background: color-mix(in srgb, var(--surface-2) 50%, transparent); }
.group-box .g-title { position: absolute; top: -22px; right: 6px; font-size: 12px; color: var(--muted); direction: rtl; white-space: nowrap; }
.zoom-ctl { position: absolute; inset-block-end: var(--s3); inset-inline-start: var(--s3); display: flex; gap: 4px; }
.outline { list-style: none; margin: 0; padding: 0; }
.outline li { padding: 4px 0; }
.outline ul { list-style: none; padding-inline-start: var(--s5); border-inline-start: 1px dashed var(--line-2); margin: 0; }
.outline .o-row { display: flex; gap: var(--s2); align-items: flex-start; }
.outline .o-row .item-btn { flex: 1; padding: 4px 6px; overflow-wrap: anywhere; }
.outline .btn { max-width: 100%; white-space: normal; overflow-wrap: anywhere; text-align: start; }
.outline .o-row .item-btn[aria-current="true"] { background: var(--accent-weak); }
.sugg { border: 1px dashed var(--ai); background: var(--ai-weak); border-radius: var(--r2); padding: var(--s2) var(--s3); margin-block-end: var(--s2); }
.st-tabs { display: none; }
.prov { font-size: var(--t-xs); color: var(--muted); }

/* ---------- login */
.login { min-height: 100vh; display: grid; place-items: center; padding: var(--s5); }
.login .card { width: min(420px, 100%); }
.code-input { font-size: 28px; letter-spacing: 10px; text-align: center; direction: ltr; }

/* ---------- responsive */
@media (max-width: 1360px) {
  .studio { grid-template-columns: 270px 1fr; }
  .studio .st-inspector { display: none; }
  .studio.show-inspector .st-inspector { display: flex; position: fixed; inset-block: 56px 0; inset-inline-start: 0; width: 320px; z-index: 30; box-shadow: var(--shadow); }
}
@media (max-width: 760px) {
  .app { grid-template-columns: minmax(0, 1fr); grid-template-rows: 52px minmax(0, 1fr) 58px; }
  .top { gap: var(--s2); padding-inline: var(--s2); }
  .top .btn.grow { min-width: 0; }
  .top .btn.grow .muted { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
  .top .chip.test { max-width: 96px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
  .top .row { flex-wrap: nowrap; gap: var(--s1); }
  .tbl { display: block; overflow-x: auto; max-width: 100%; }
  .st-toolbar input[type=search] { width: 120px !important; }
  .st-toolbar .hide-sm { display: none; }
  .brand { min-width: 0; }
  .brand .name { display: none; }
  .nav { grid-row: 3; border: 0; border-block-start: 1px solid var(--line); display: flex; padding: 4px; overflow-x: auto; }
  .nav a { flex-direction: column; gap: 0; font-size: 11px; padding: 4px 8px; min-width: 64px; justify-content: center; }
  .nav .kbd, .nav-foot { display: none; }
  main { padding: var(--s4); }
  .g2, .g3 { grid-template-columns: 1fr; }
  .span2 { grid-column: auto; }
  .top .hide-sm { display: none; }
  .studio { grid-template-columns: 1fr; margin: calc(-1 * var(--s4)); height: calc(100vh - 52px - 58px); }
  .studio .st-pane, .studio .st-center { display: none; }
  .studio[data-tab="talk"] .st-talk, .studio[data-tab="board"] .st-center, .studio[data-tab="inspect"] .st-inspector { display: flex; position: static; width: auto; box-shadow: none; }
  .st-tabs { display: flex; }
  .msg { max-width: 92%; }
  .chat { height: calc(100vh - 52px - 58px - 120px); }
}
