/*
 * The surface must be visually distinct from ANY host app's chrome
 * (GrokVision.md §3.3): "It must not spoof A, and A must not spoof it."
 *
 * So: dark header, one accent hue drawn from the user's own anti-spoof mark, a
 * typeface stack no product ships as its brand face, and a permanent footer that
 * says whose panel this is. It should read as "a different thing, attached to
 * this window" at a glance, from across a room.
 *
 * KimiPlan.md: color is a language. Five semantic colors, each with one
 * meaning, everywhere: blue = read/from-an-app, green = you-typed/success,
 * violet = mapper-proposed, red = write-risk/missing/failure, amber =
 * untrusted/paused. Semantic hues are fixed and never follow the mark's
 * --hue; the chrome accent still does.
 */

:root {
  --hue: 212;
  --ink: #0f1115;
  --ink-2: #3d4450;
  --ink-3: #5b6470;
  --line: #e3e6ea;
  --bg: #ffffff;
  --bg-2: #f6f7f9;
  --accent: hsl(var(--hue) 78% 44%);
  --accent-soft: hsl(var(--hue) 78% 96%);
  --ok: #0d7a45;
  --bad: #b42318;
  --warn: #8a5a00;

  /* Semantic layer — meaning, not brand (KimiPlan §3.2) */
  --read: #2563eb;
  --read-soft: #eaf1fe;
  --typed: #0d7a45;
  --typed-soft: #edf9f2;
  --mapped: #7c3aed;
  --mapped-soft: #f3effe;
  --risk: #b42318;
  --risk-soft: #fef3f2;
  --warn2: #8a5a00;
  --warn-soft: #fff6e6;

  /* Spacing scale (4px grid) */
  --s1: 4px;
  --s2: 8px;
  --s3: 12px;
  --s4: 16px;
  --s5: 24px;

  --radius: 8px;
  --radius-lg: 12px;
  --radius-pill: 999px;

  /* Elevation — whisper-quiet; this is a consent surface */
  --elev-1: 0 1px 2px rgb(15 17 21 / 0.06);

  /* Motion */
  --t-fast: 120ms;
  --t-view: 150ms;
  --ease: cubic-bezier(0.2, 0.7, 0.3, 1);

  color-scheme: light;
}

* {
  box-sizing: border-box;
}

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

body {
  display: flex;
  flex-direction: column;
  background: var(--bg);
  color: var(--ink);
  font: 14px/1.5 ui-sans-serif, system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  -webkit-font-smoothing: antialiased;
}

/* Universal, unmistakable focus. Accent = the user's mark hue. */
:is(
  .member-card,
  .member-chip,
  .cap,
  .btn,
  .ghost,
  .brand-name,
  .back,
  .search,
  .add-app-input,
  .field-input,
  .scope-select
):focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

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

.chrome {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  padding: 10px 12px;
  background: var(--ink);
  color: #fff;
  flex: none;
}

.brand {
  display: flex;
  align-items: center;
  gap: 8px;
  min-width: 0;
}

.mark {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: 34px;
  height: 24px;
  padding: 0 6px;
  border-radius: 5px;
  background: var(--accent);
  color: #fff;
  font-size: 12px;
  letter-spacing: 1px;
  user-select: none;
  cursor: help;
}

.brand-name {
  background: none;
  border: 0;
  padding: 4px 2px;
  color: #fff;
  font: 600 13px/1 inherit;
  letter-spacing: 0.02em;
  cursor: pointer;
}

.brand-name:hover {
  text-decoration: underline;
  text-underline-offset: 3px;
}

.chrome-actions {
  display: flex;
  align-items: center;
  gap: 4px;
}

.ghost {
  background: rgba(255, 255, 255, 0.1);
  border: 0;
  color: #fff;
  padding: 7px 11px;
  border-radius: 5px;
  font: 500 11.5px/1 inherit;
  cursor: pointer;
}

.ghost:hover {
  background: rgba(255, 255, 255, 0.18);
}

.ghost.danger {
  background: var(--bad);
}

.ghost:focus-visible {
  outline-color: #fff;
}

.close {
  font-size: 15px;
  line-height: 1;
  padding: 6px 8px;
  min-width: 28px;
  min-height: 28px;
}

/* ---------- rooted-in bar ---------- */

.rooted {
  display: flex;
  align-items: baseline;
  gap: 6px;
  flex-wrap: wrap;
  padding: 8px 12px;
  background: var(--bg-2);
  border-bottom: 1px solid var(--line);
  flex: none;
}

.rooted-label {
  color: var(--ink-3);
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.08em;
}

.rooted-name {
  font-weight: 650;
}

.rooted-origin {
  color: var(--ink-3);
  font: 11px/1.4 ui-monospace, SFMono-Regular, Menlo, monospace;
  word-break: break-all;
}

.rooted-transport {
  margin-left: auto;
  color: var(--ink-3);
  font-size: 10.5px;
  border: 1px solid var(--line);
  background: #fff;
  border-radius: 999px;
  padding: 3px 8px;
}

.banner {
  padding: 10px 12px;
  background: var(--warn-soft);
  border-bottom: 1px solid #ecd9b0;
  color: var(--warn2);
  font-size: 12.5px;
  font-weight: 550;
  flex: none;
}

/* ---------- search ---------- */

.searchbar {
  padding: 10px 12px 4px;
  flex: none;
}

.search {
  width: 100%;
  min-height: 36px;
  padding: 8px 10px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  font: inherit;
  background: #fff;
}

.search:focus {
  outline: 2px solid var(--accent);
  outline-offset: -1px;
  border-color: var(--accent);
}

/* ---------- body ---------- */

.body {
  flex: 1;
  overflow-y: auto;
  padding: 8px 12px 16px;
  scroll-padding-bottom: 64px;
}

.stack {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.muted {
  color: var(--ink-3);
  font-size: 12.5px;
  margin: 6px 0 0;
}

/* Quiet textual "‹ Back" — orientation aid, not a state machine change. */
.back {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  width: fit-content;
  background: none;
  border: 0;
  padding: 4px 8px 4px 0;
  color: var(--ink-3);
  font: 600 12px inherit;
  cursor: pointer;
}

.back:hover {
  color: var(--accent);
}

/* Step chips: pure orientation, never navigation. */
.steps {
  display: flex;
  gap: 6px;
  flex-wrap: wrap;
  margin-bottom: 2px;
}

.step {
  font: 650 10.5px/1 inherit;
  padding: 5px 10px;
  border-radius: var(--radius-pill);
  border: 1px solid var(--line);
  background: var(--bg-2);
  color: var(--ink-3);
}

.step.done {
  color: var(--ok);
  border-color: #bfe6ce;
  background: var(--typed-soft);
}

.step.now {
  color: #fff;
  background: var(--accent);
  border-color: var(--accent);
}

.crumb {
  padding: 4px 0 8px;
  border-bottom: 1px solid var(--line);
  margin-bottom: 4px;
}

.crumb-title {
  font-weight: 650;
  font-size: 16px;
}

.crumb-sub {
  color: var(--ink-3);
  font: 11px/1.4 ui-monospace, SFMono-Regular, Menlo, monospace;
  word-break: break-all;
}

.strong {
  font-weight: 650;
  margin-bottom: 2px;
}

.p {
  margin: 0 0 6px;
  color: var(--ink-2);
  font-size: 13px;
}

.p:last-child {
  margin-bottom: 0;
}

.empty {
  padding: 24px 14px;
  text-align: center;
  border: 1px dashed var(--line);
  border-radius: var(--radius);
  color: var(--ink-2);
}

/* Dashed-circle pictogram; CSS-drawn, no assets, not an app's content. */
.empty::before {
  content: "";
  display: block;
  width: 28px;
  height: 28px;
  margin: 0 auto 10px;
  border-radius: 50%;
  border: 2px dashed var(--line);
}

.notice {
  padding: 11px 12px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--bg-2);
}

.notice.ok {
  background: #f0faf4;
  border-color: #bfe6ce;
}

.notice.bad {
  background: var(--risk-soft);
  border-color: #f2c9c4;
}

/* ---------- member cards ---------- */

.member-card {
  display: flex;
  align-items: flex-start;
  gap: 10px;
  width: 100%;
  min-height: 52px;
  padding: 10px 12px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: #fff;
  color: inherit;
  text-align: left;
  cursor: pointer;
  font: inherit;
}

.member-card:hover {
  border-color: var(--accent);
  background: var(--accent-soft);
}

/* Every app gets a face: deterministic per-origin hue, set inline from JS. */
.member-icon {
  display: grid;
  place-items: center;
  width: 36px;
  height: 36px;
  flex: none;
  border-radius: 8px;
  overflow: hidden;
  background: var(--ink);
  background: hsl(var(--app-hue, 212) 55% 38%);
  color: #fff;
  font-weight: 650;
  font-size: 15px;
}

.member-icon-img {
  display: block;
  width: 36px;
  height: 36px;
  object-fit: cover;
}

.member-text {
  display: flex;
  flex-direction: column;
  min-width: 0;
  flex: 1;
}

.member-name {
  font-weight: 600;
  font-size: 14px;
}

/* The origin is never optional. It is the identity; the name is only a label. */
.member-origin {
  color: var(--ink-3);
  font: 10.5px/1.4 ui-monospace, SFMono-Regular, Menlo, monospace;
  word-break: break-all;
}

.member-origin.unattested::after {
  content: " · unnamed";
  color: var(--warn);
}

.member-meta {
  color: var(--ink-3);
  font-size: 11px;
}

/* S1: jump to startEdge. Real <button>s; the card is no longer a <button>. */
.member-chips {
  display: flex;
  flex-wrap: wrap;
  gap: 4px;
  margin-top: 6px;
}

.member-chip {
  min-height: 30px;
  padding: 5px 8px;
  border: 1px solid var(--line);
  border-left-width: 3px;
  border-radius: 6px;
  background: #fff;
  color: var(--ink);
  font: 600 10.5px/1.2 ui-monospace, SFMono-Regular, Menlo, monospace;
  cursor: pointer;
}

.member-chip.read {
  border-left-color: var(--read);
}

.member-chip.write {
  border-left-color: var(--risk);
}

.member-chip:hover:not(:disabled) {
  background: var(--bg-2);
}

.member-chip.read:hover:not(:disabled) {
  background: var(--read-soft);
}

.member-chip.write:hover:not(:disabled) {
  background: var(--risk-soft);
}

.member-chip:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

.presence-wrap {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 3px;
  flex: none;
}

.presence {
  width: 10px;
  height: 10px;
  flex: none;
  border-radius: 50%;
  box-shadow: 0 0 0 2px var(--bg);
}

.presence.on {
  background: var(--ok);
}

.presence.off {
  background: #cbd1d9;
}

/* Presence is readable without hovering the dot. */
.presence-label {
  font-size: 10.5px;
  color: var(--ink-3);
  white-space: nowrap;
}

.presence-wrap:has(.presence.on) .presence-label {
  color: var(--ok);
}

.add-app {
  display: grid;
  gap: 8px;
  margin-top: 8px;
  padding: 12px;
  border: 1px dashed var(--line);
  border-radius: var(--radius);
  background: var(--bg-2);
}

.add-app-label {
  font-weight: 650;
  font-size: 12px;
}

.add-app-row {
  display: flex;
  gap: 8px;
}

.add-app-input {
  flex: 1;
  min-width: 0;
  min-height: 36px;
  padding: 8px 10px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  font: inherit;
}

.add-app-msg {
  color: var(--bad);
  font-size: 12px;
  min-height: 1em;
}

/* ---------- capabilities ---------- */

.cap {
  display: flex;
  flex-direction: column;
  gap: 3px;
  width: 100%;
  min-height: 48px;
  padding: 10px 12px;
  border: 1px solid var(--line);
  border-left: 3px solid var(--read);
  border-radius: var(--radius);
  background: #fff;
  text-align: left;
  cursor: pointer;
  font: inherit;
}

/* Writes are visually hotter on purpose (Frontend-handoff §7). */
.cap.write {
  border-left-color: var(--risk);
}

article.cap {
  cursor: default;
}

.cap:hover:not(:disabled) {
  border-color: var(--read);
}

.cap.read:hover:not(:disabled) {
  background: var(--read-soft);
  border-color: var(--read);
}

.cap.write:hover:not(:disabled) {
  background: var(--risk-soft);
  border-color: var(--risk);
}

.cap:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

.cap-head {
  display: flex;
  align-items: center;
  gap: 6px;
  flex-wrap: wrap;
}

.cap-name {
  font: 600 12.5px/1.4 ui-monospace, SFMono-Regular, Menlo, monospace;
}

.cap-desc {
  color: var(--ink-2);
  font-size: 13px;
}

.badge {
  font: 700 10px/1 inherit;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  padding: 3px 7px;
  border-radius: 4px;
}

.badge.read {
  background: var(--read-soft);
  color: var(--read);
}

.badge.write {
  background: var(--risk-soft);
  color: var(--risk);
}

.badge.untrusted {
  background: var(--warn-soft);
  color: var(--warn2);
}

.badge.req {
  background: var(--accent-soft);
  color: var(--accent);
}

.badge.draft {
  background: var(--warn-soft);
  color: var(--warn2);
}

.badge.changes {
  background: var(--risk-soft);
  color: var(--risk);
}

.badge.destructive,
.badge.financial {
  background: var(--risk);
  color: #fff;
}

.cap-schema {
  display: flex;
  flex-direction: column;
  gap: 2px;
  margin-top: 6px;
}

.schema-row {
  display: flex;
  align-items: center;
  gap: 6px;
  flex-wrap: wrap;
  font: 12px/1.4 ui-monospace, SFMono-Regular, Menlo, monospace;
}

.schema-name {
  color: var(--ink);
}

.schema-type {
  color: var(--ink-3);
}

.cap-schema-empty,
.cap-meta {
  font-size: 12px;
  color: var(--ink-3);
}

.cap-inspect {
  display: flex;
  flex-direction: column;
  gap: 2px;
  margin-top: 6px;
}

.cap-inspect .p {
  margin: 0;
  font-size: 12px;
  color: var(--ink-2);
}

.cap-use {
  margin-top: 8px;
  align-self: flex-start;
}

.notice.loud {
  background: var(--risk-soft);
  border: 1px solid var(--risk);
}

/* ---------- confirm card ---------- */

.fields {
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.field {
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.field-label {
  font-weight: 600;
  font-size: 14px;
}

.field-head {
  display: flex;
  align-items: center;
  gap: 6px;
}

.field-name {
  font: 600 12px/1.4 ui-monospace, SFMono-Regular, Menlo, monospace;
  color: var(--ink-3);
}

.field-type {
  margin-left: auto;
  color: var(--ink-3);
  font-size: 10.5px;
}

.field-input {
  padding: 8px 10px;
  min-height: 36px;
  border: 1px solid var(--line);
  border-radius: 6px;
  font: 13px/1.4 ui-monospace, SFMono-Regular, Menlo, monospace;
  background: #fff;
}

.field-input:focus {
  outline: 2px solid var(--accent);
  outline-offset: -1px;
}

/* Validation points AT the field, not just at a list far away. */
.field-input[aria-invalid="true"] {
  border-color: var(--risk);
  background: var(--risk-soft);
}

/* Provenance is the point, so it is never subtle. */
.prov {
  display: flex;
  gap: 4px;
  flex-wrap: wrap;
}

.prov-tag {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  font-size: 10.5px;
  font-weight: 550;
  padding: 3px 7px;
  border-radius: 4px;
  background: #eef2f7;
  color: var(--ink-2);
}

/* Values trace back to their source app by colour: dot hue = app's avatar hue. */
.prov-tag.read {
  background: var(--read-soft);
  color: var(--read);
}

.prov-tag.read::before {
  content: "";
  width: 6px;
  height: 6px;
  flex: none;
  border-radius: 50%;
  background: hsl(var(--app-hue, 212) 55% 42%);
}

.prov-tag.typed {
  background: var(--typed-soft);
  color: var(--typed);
}

.prov-tag.mapped {
  background: var(--mapped-soft);
  color: var(--mapped);
}

.prov-tag.constant {
  background: var(--bg-2);
  color: var(--ink-2);
}

.prov-tag.missing {
  background: var(--risk-soft);
  color: var(--risk);
  border: 1px dashed rgb(180 35 24 / 0.5);
}

.prov-tag.untrusted {
  background: var(--warn-soft);
  color: var(--warn2);
  font-weight: 700;
}

/* Our constant glyph, CSS-painted — tag textContent stays byte-identical. */
.prov-tag.untrusted::before {
  content: "⚠";
}

/* ---------- json ---------- */

/* Machine truth looks like machine truth: the panel's own dark material. */
.json {
  border: 1px solid #262c36;
  border-radius: var(--radius);
  overflow: hidden;
  background: var(--ink);
}

.json-head {
  padding: 7px 12px;
  background: #171b21;
  border-bottom: 1px solid #262c36;
  font: 700 10px/1 inherit;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: #9aa4b2;
}

.json-body {
  margin: 0;
  padding: 12px;
  font: 12px/1.6 ui-monospace, SFMono-Regular, Menlo, monospace;
  white-space: pre-wrap;
  word-break: break-word;
  color: #dbe2ec;
  max-height: 260px;
  overflow: auto;
}

/* Syntax tokens — same characters, colored. textContent is byte-identical. */
.tok-key {
  color: hsl(var(--hue) 80% 74%);
}

.tok-str {
  color: #8fd9a8;
}

.tok-num {
  color: #f0b45f;
}

.tok-bool,
.tok-null {
  color: #c9a8f5;
}

.problems {
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.problem {
  padding: 6px 9px;
  border-radius: 6px;
  background: var(--risk-soft);
  color: var(--risk);
  font-size: 12px;
}

/* The decision never scrolls away on long schemas. */
.confirm-actions {
  position: sticky;
  bottom: 0;
  display: flex;
  gap: 8px;
  align-items: center;
  padding: 10px 0 4px;
  margin-top: 4px;
  background: var(--bg);
  border-top: 1px solid var(--line);
}

.confirm-actions .btn.primary {
  flex: 1 1 auto;
  min-height: 42px;
  font-size: 13.5px;
}

/* ---------- buttons, rows ---------- */

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

.row.wrap {
  flex-wrap: wrap;
}

.btn {
  min-height: 36px;
  padding: 8px 12px;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: #fff;
  font: 500 12.5px/1 inherit;
  cursor: pointer;
}

.btn:hover:not(:disabled) {
  border-color: var(--ink-3);
}

.btn.primary {
  background: var(--accent);
  border-color: var(--accent);
  color: #fff;
  font-weight: 600;
}

.btn.primary:disabled {
  background: #c9ced6;
  border-color: #c9ced6;
  cursor: not-allowed;
}

.btn.danger {
  color: var(--bad);
  border-color: #f2c9c4;
}

.btn.small {
  min-height: 30px;
  padding: 5px 9px;
  font-size: 11.5px;
}

.scope {
  padding: 10px 12px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--bg-2);
}

.scope-select {
  width: 100%;
  min-height: 36px;
  padding: 7px 8px;
  border: 1px solid var(--line);
  border-radius: 6px;
  font: inherit;
  background: #fff;
}

/* ---------- result / failure marks ---------- */

/* CSS-drawn shapes; no images, no app-supplied content. */
.result-mark {
  position: relative;
  width: 40px;
  height: 40px;
  margin: 2px 0 6px;
  border-radius: 50%;
  background: var(--typed-soft);
  border: 2px solid var(--typed);
}

.result-mark::after {
  content: "";
  position: absolute;
  left: 13px;
  top: 9px;
  width: 8px;
  height: 14px;
  border: solid var(--typed);
  border-width: 0 3px 3px 0;
  transform: rotate(45deg);
}

.result-mark.bad {
  background: var(--risk-soft);
  border-color: var(--risk);
}

.result-mark.bad::after {
  left: 50%;
  top: 7px;
  width: 3px;
  height: 15px;
  border: 0;
  border-radius: 2px;
  background: var(--risk);
  transform: translateX(-50%);
}

.result-mark.bad::before {
  content: "";
  position: absolute;
  left: 50%;
  bottom: 6px;
  width: 3px;
  height: 3px;
  border-radius: 50%;
  background: var(--risk);
  transform: translateX(-50%);
}

/* The product promise, visible: you never left your app. */
.locus {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  width: fit-content;
  padding: 5px 10px;
  border-radius: var(--radius-pill);
  border: 1px solid var(--line);
  background: var(--bg-2);
  color: var(--ink-2);
  font-size: 11.5px;
  word-break: break-all;
}

.locus::before {
  content: "";
  width: 7px;
  height: 7px;
  flex: none;
  border-radius: 50%;
  background: hsl(var(--app-hue, 212) 55% 42%);
}

/* ---------- grants ---------- */

.grant {
  display: flex;
  align-items: center;
  gap: 8px;
  min-height: 44px;
  padding: 9px 10px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: #fff;
}

.grant-text {
  flex: 1;
  min-width: 0;
}

.grant-line {
  font: 11.5px/1.5 ui-monospace, SFMono-Regular, Menlo, monospace;
  word-break: break-all;
}

.grant-meta {
  color: var(--ink-3);
  font-size: 11px;
}

/* ---------- footer ---------- */

.foot {
  flex: none;
  padding: 8px 12px;
  border-top: 1px solid var(--line);
  background: var(--bg-2);
  color: var(--ink-3);
  font-size: 10.5px;
}

/* ---------- motion ----------
 * Small and guarded (KimiPlan §4.4 / Frontend-handoff §11, §15.5).
 * Nothing here ever delays the confirm JSON or the Approve button: the
 * animation wraps the whole view once for 150ms and then it is done.
 */

@media (prefers-reduced-motion: no-preference) {
  .body > * {
    animation: view-in var(--t-view) var(--ease);
  }

  @keyframes view-in {
    from {
      opacity: 0;
      transform: translateY(6px);
    }
    to {
      opacity: 1;
      transform: none;
    }
  }

  .presence.on {
    animation: presence-pulse 2.4s ease-out infinite;
  }

  @keyframes presence-pulse {
    0%,
    100% {
      box-shadow: 0 0 0 2px var(--bg), 0 0 0 2px rgb(13 122 69 / 0.35);
    }
    50% {
      box-shadow: 0 0 0 2px var(--bg), 0 0 0 6px rgb(13 122 69 / 0);
    }
  }

  .member-card,
  .member-chip,
  .cap,
  .btn,
  .ghost {
    transition:
      border-color var(--t-fast) var(--ease),
      background var(--t-fast) var(--ease),
      transform var(--t-fast) var(--ease),
      box-shadow var(--t-fast) var(--ease);
  }

  .member-card:hover,
  .cap:hover:not(:disabled) {
    transform: translateY(-1px);
    box-shadow: var(--elev-1);
  }
}
