/* =========================================================================
   EVA Agent — compact sync console
   ========================================================================= */

:root {
  --bg:        #F1EFE9;
  --surface:   #FCFBF8;
  --surface-2: #F6F4EE;
  --line:      #E3DFD4;
  --line-soft: #ECE8DE;
  --ink:       #1C1E22;
  --ink-soft:  #595D66;
  --ink-faint: #9B9A92;
  --source:    #21508C;
  --source-soft: #E4ECF6;
  --target:    #B05418;
  --target-soft: #F6E8D9;
  --new:       #2F7D43;
  --new-soft:  #E4F0E5;
  --modified:  #B45309;
  --modified-soft: #FEF3C7;
  --exists:    #6E7079;
  --exists-soft: #ECE9E1;
  --blocked:   #B23B2E;
  --blocked-soft: #F6E2DD;
  --radius:    14px;
  --radius-sm: 9px;
  --shadow-sm: 0 1px 2px rgba(28,30,34,.05), 0 2px 6px rgba(28,30,34,.04);
  --shadow:    0 4px 12px rgba(28,30,34,.06), 0 18px 40px -22px rgba(28,30,34,.22);
  --shadow-lg: 0 30px 80px -30px rgba(28,30,34,.40);
  --mono: "IBM Plex Mono", ui-monospace, "SF Mono", Menlo, monospace;
  --sans: "IBM Plex Sans", system-ui, sans-serif;
  --display: "Bricolage Grotesque", var(--sans);
  --ease: cubic-bezier(.2,.7,.2,1);
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html { -webkit-text-size-adjust: 100%; }

body {
  margin: 0;
  font-family: var(--sans);
  font-weight: 450;
  color: var(--ink);
  background: var(--bg);
  line-height: 1.55;
  -webkit-font-smoothing: antialiased;
  text-rendering: optimizeLegibility;
}

.grain {
  position: fixed; inset: 0; z-index: 0; pointer-events: none; opacity: .5;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='160' height='160'%3E%3Cfilter id='n'%3E%3CfeTurbulence type='fractalNoise' baseFrequency='.85' numOctaves='2' stitchTiles='stitch'/%3E%3C/filter%3E%3Crect width='100%25' height='100%25' filter='url(%23n)' opacity='.035'/%3E%3C/svg%3E");
}

/* ── Masthead ─────────────────────────────────────────────────────── */
.masthead { position: relative; z-index: 2; padding: 26px 0 10px; }
.masthead-inner {
  max-width: 1120px; margin: 0 auto; padding: 0 32px;
  display: flex; align-items: center; justify-content: space-between; gap: 20px;
}
.brand { display: flex; align-items: center; gap: 13px; }
.brand-mark {
  width: 26px; height: 26px; border-radius: 8px; flex: none;
  background: linear-gradient(135deg, var(--source) 0%, var(--target) 100%);
  box-shadow: var(--shadow-sm); transform: rotate(-8deg);
}
.brand-text { font-family: var(--display); font-weight: 600; font-size: 1.18rem; letter-spacing: -.01em; }
.brand-text em { font-style: normal; color: var(--ink-faint); font-weight: 500; }
.tag {
  font-family: var(--mono); font-size: .72rem;
  background: var(--surface-2); border: 1px solid var(--line);
  padding: 4px 12px; border-radius: 999px; color: var(--ink-soft);
}

/* ── Stage / panels ───────────────────────────────────────────────── */
.stage { position: relative; z-index: 1; max-width: 1120px; margin: 0 auto; padding: 26px 32px 90px; }
.panel {
  background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius);
  box-shadow: var(--shadow); padding: 34px 34px 30px; margin-bottom: 26px;
  animation: rise .5s var(--ease) both;
}
@keyframes rise { from { opacity: 0; transform: translateY(14px); } to { opacity: 1; transform: none; } }
.panel-head { margin-bottom: 26px; }
.panel-head--row { display: flex; align-items: flex-start; justify-content: space-between; gap: 24px; }
.panel-title { font-family: var(--display); font-weight: 600; font-size: 1.5rem; letter-spacing: -.02em; margin: 0 0 7px; }
.panel-sub { margin: 0; color: var(--ink-soft); font-size: .92rem; max-width: 64ch; }
.panel-sub strong { color: var(--ink); font-weight: 600; }

/* ── Environments ─────────────────────────────────────────────────── */
.env-grid {
  display: grid; grid-template-columns: 1fr 60px 1fr; align-items: stretch; gap: 0; margin-bottom: 24px;
}
.env-card {
  background: var(--surface-2); border: 1px solid var(--line); border-radius: var(--radius);
  padding: 18px 20px 16px; position: relative; overflow: hidden;
}
.env-card::before { content: ""; position: absolute; inset: 0 0 auto 0; height: 3px; }
.env-card--target::before { background: linear-gradient(90deg, transparent, var(--target)); }
.env-card.is-connected.env-card--target { border-color: color-mix(in srgb, var(--target) 40%, var(--line)); }
.env-card-top { display: flex; align-items: center; justify-content: space-between; margin-bottom: 14px; }
.env-tag {
  font-family: var(--display); font-weight: 600; font-size: .78rem; letter-spacing: .08em;
  text-transform: uppercase; padding: 3px 10px; border-radius: 7px;
}
.env-card--target .env-tag { color: var(--target); background: var(--target-soft); }
.env-status { font-family: var(--mono); font-size: .66rem; color: var(--ink-faint); display: flex; align-items: center; gap: 6px; }
.env-status::before { content: ""; width: 7px; height: 7px; border-radius: 50%; background: var(--ink-faint); }
.env-card.is-connected .env-status { color: var(--new); }
.env-card.is-connected .env-status::before { background: var(--new); box-shadow: 0 0 0 3px var(--new-soft); }
/* ── Fields ───────────────────────────────────────────────────────── */
.field { display: flex; flex-direction: column; gap: 5px; margin-bottom: 12px; }
.field-label { font-size: .74rem; font-weight: 600; color: var(--ink-soft); letter-spacing: .01em; }
.field-hint { font-weight: 450; color: var(--ink-faint); }
.input {
  font-family: var(--sans); font-size: .9rem; color: var(--ink);
  background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius-sm);
  padding: 9px 12px; width: 100%; transition: border-color .18s, box-shadow .18s, background .18s;
}
.input.mono { font-family: var(--mono); font-size: .82rem; }
.input::placeholder { color: var(--ink-faint); }
.input:focus { outline: none; border-color: var(--source); box-shadow: 0 0 0 4px var(--source-soft); background: #fff; }

/* ── Entity row ───────────────────────────────────────────────────── */
.entity-row { display: flex; gap: 14px; align-items: flex-end; }
.field--type { flex: 1; }
.field--ids { flex: 2; }
.btn { cursor: pointer; }
.btn:disabled { opacity: .5; cursor: default; }

.btn-primary {
  font-family: var(--sans); font-weight: 500; font-size: .9rem;
  background: var(--source); color: #fff; border: none; border-radius: var(--radius-sm);
  padding: 10px 20px; display: inline-flex; align-items: center; gap: 8px;
  transition: background .18s, box-shadow .18s, transform .12s;
  white-space: nowrap;
}
.btn-primary:hover:not(:disabled) { background: #1A4070; transform: translateY(-1px); box-shadow: var(--shadow-sm); }
.btn-primary.is-busy { pointer-events: none; }
.btn-primary.is-busy .btn-label::after { content: "…"; }

.btn-ghost {
  font-family: var(--sans); font-weight: 500; font-size: .84rem;
  background: transparent; color: var(--ink-soft); border: 1px solid var(--line);
  border-radius: var(--radius-sm); padding: 8px 16px;
  transition: background .18s, color .18s, border-color .18s;
}
.btn-ghost:hover:not(:disabled) { background: var(--surface-2); color: var(--ink); border-color: var(--ink-faint); }

.btn-commit {
  font-family: var(--sans); font-weight: 500; font-size: .9rem;
  background: var(--new); color: #fff; border: none; border-radius: var(--radius-sm);
  padding: 10px 24px; transition: background .18s, box-shadow .18s, transform .12s;
}
.btn-commit:hover:not(:disabled) { background: #236836; transform: translateY(-1px); box-shadow: var(--shadow-sm); }

.btn-sm { font-size: .8rem; padding: 6px 13px; }
.btn-arrow { width: 14px; height: 12px; flex: none; }

.form-error { color: var(--blocked); font-size: .84rem; margin: 10px 0 0; font-weight: 500; }

/* ── Agent status indicator ───────────────────────────────────────── */
.agent-status {
  display: flex; align-items: center; gap: 9px;
  font-family: var(--mono); font-size: .72rem;
  padding: 6px 14px; border-radius: 999px;
  background: var(--surface-2); border: 1px solid var(--line);
  white-space: nowrap;
}
.agent-dot {
  width: 8px; height: 8px; border-radius: 50%;
  background: var(--ink-faint); transition: background .3s;
}
.agent-status[data-state="running"] .agent-dot { background: var(--source); animation: pulse 1.2s ease infinite; }
.agent-status[data-state="awaiting_approval"] .agent-dot { background: var(--modified); }
.agent-status[data-state="done"] .agent-dot { background: var(--new); }
.agent-status[data-state="error"] .agent-dot { background: var(--blocked); }

@keyframes pulse {
  0%, 100% { box-shadow: 0 0 0 0 color-mix(in srgb, var(--source) 40%, transparent); }
  50% { box-shadow: 0 0 0 6px color-mix(in srgb, var(--source) 0%, transparent); }
}

/* ── Conversation ─────────────────────────────────────────────────── */
.conversation {
  max-height: 460px; overflow-y: auto; margin-bottom: 16px;
  display: flex; flex-direction: column; gap: 10px;
  scroll-behavior: smooth;
}
.conversation::-webkit-scrollbar { width: 6px; }
.conversation::-webkit-scrollbar-track { background: transparent; }
.conversation::-webkit-scrollbar-thumb { background: var(--line); border-radius: 3px; }

.msg--placeholder {
  color: var(--ink-faint); font-size: .88rem; text-align: center; padding: 30px 0;
}

.msg { animation: pop .3s var(--ease) both; }
@keyframes pop { from { opacity: 0; transform: translateY(8px) scale(.98); } to { opacity: 1; transform: none; } }

.msg--agent .msg-body {
  background: var(--surface-2); border: 1px solid var(--line-soft);
  border-radius: 0 var(--radius) var(--radius) var(--radius);
  padding: 14px 18px; font-size: .9rem; line-height: 1.6;
  max-width: 90%;
}
.msg--agent .msg-body p { margin: 0 0 8px; }
.msg--agent .msg-body p:last-child { margin-bottom: 0; }
.msg--agent .msg-body code {
  font-family: var(--mono); font-size: .82rem;
  background: var(--exists-soft); padding: 1px 5px; border-radius: 4px;
}
.msg--agent .msg-body ul { margin: 6px 0; padding-left: 20px; }
.msg--agent .msg-body li { margin-bottom: 3px; }
.msg--agent .msg-body h4 { margin: 12px 0 4px; font-family: var(--display); font-weight: 600; }

/* Reasoning blocks — distinct from output */
.msg--reasoning .msg-body {
  background: #F0EEF5; border-color: #D6D0E8;
  font-size: .86rem; line-height: 1.55; opacity: .88;
}
.msg--reasoning .msg-body p { margin: 0 0 6px; }
.msg--reasoning .msg-body p:last-child { margin-bottom: 0; }
.msg--reasoning .msg-body code {
  background: #E2DCF0; font-size: .8rem;
}

.msg--user {
  display: flex; flex-direction: column; align-items: flex-end;
}
.msg--user .msg-label {
  font-size: .7rem; font-weight: 600; color: var(--ink-faint);
  text-transform: uppercase; letter-spacing: .06em; margin-bottom: 3px;
}
.msg--user .msg-body {
  background: var(--source-soft); border: 1px solid color-mix(in srgb, var(--source) 20%, transparent);
  border-radius: var(--radius) 0 var(--radius) var(--radius);
  padding: 10px 16px; font-size: .9rem; max-width: 80%;
}

/* ── Tool call display ────────────────────────────────────────────── */
.tool-call {
  display: flex; align-items: center; gap: 10px;
  font-family: var(--mono); font-size: .78rem;
  padding: 7px 14px; border-radius: var(--radius-sm);
  background: var(--exists-soft); border: 1px solid var(--line-soft);
  animation: pop .25s var(--ease) both;
}
.tool-name { font-weight: 500; color: var(--ink); }
.tool-status {
  font-size: .7rem; padding: 1px 8px; border-radius: 999px;
  text-transform: uppercase; letter-spacing: .04em;
}
.tool-call[data-status="running"] .tool-status { color: var(--source); background: var(--source-soft); }
.tool-call[data-status="done"] .tool-status { color: var(--new); background: var(--new-soft); }
.tool-call[data-status="error"] .tool-status { color: var(--blocked); background: var(--blocked-soft); }
.tool-detail-area { margin-left: auto; }
.tool-detail { color: var(--ink-faint); font-size: .72rem; }
.tool-detail.ok { color: var(--new); }
.tool-detail.err { color: var(--blocked); }

/* ── Plan summary ─────────────────────────────────────────────────── */
.plan-summary {
  background: #fff; border: 2px solid var(--new-soft);
  border-radius: var(--radius-sm); padding: 16px 20px; font-size: .88rem; line-height: 1.6;
  animation: pop .35s var(--ease) both;
}
.plan-summary p { margin: 0 0 8px; }
.plan-summary p:last-child { margin-bottom: 0; }
.plan-summary ul { margin: 6px 0; padding-left: 20px; }
.plan-summary code {
  font-family: var(--mono); font-size: .8rem;
  background: var(--exists-soft); padding: 1px 5px; border-radius: 4px;
}

/* ── Tool call details (clickable) ──────────────────────────────────── */
.tool-call { cursor: pointer; user-select: none; }
.tool-call.is-expanded {
  border-color: var(--source);
  background: #EAF0F8;
}
.tool-details {
  margin-top: 6px; padding: 8px 12px;
  background: #FCFBF8; border: 1px solid var(--line-soft);
  border-radius: var(--radius-sm); overflow-x: auto;
  font-size: .74rem; line-height: 1.45; max-height: 300px; overflow-y: auto;
}
.tool-details pre {
  margin: 0; font-family: var(--mono); white-space: pre-wrap;
  word-break: break-all;
}

/* ── Approval bar ─────────────────────────────────────────────────── */
.approval-bar {
  display: flex; align-items: center; justify-content: space-between; gap: 16px;
  background: var(--modified-soft); border: 1px solid color-mix(in srgb, var(--modified) 30%, transparent);
  border-radius: var(--radius-sm); padding: 14px 20px; margin-bottom: 14px;
  animation: pop .35s var(--ease) both;
}
.approval-text { font-size: .88rem; color: var(--ink); }
.approval-actions { display: flex; gap: 10px; flex: none; }

/* ── Input bar ────────────────────────────────────────────────────── */
.input-bar {
  display: flex; gap: 10px; align-items: center;
  border-top: 1px solid var(--line-soft); padding-top: 16px;
}
.input-bar .input { flex: 1; }

/* ── Result panel ─────────────────────────────────────────────────── */
.result-panel {
  border-top: 1px solid var(--line-soft); padding-top: 20px; margin-top: 14px;
  animation: pop .4s var(--ease) both;
}
.result-title { font-family: var(--display); font-weight: 600; font-size: 1.1rem; margin: 0 0 8px; }
.result-stats { font-size: .9rem; color: var(--ink-soft); margin-bottom: 18px; }

/* ── Toast ────────────────────────────────────────────────────────── */
.toast {
  position: fixed; bottom: 28px; left: 50%; transform: translateX(-50%) translateY(10px);
  background: var(--ink); color: #fff; padding: 10px 24px; border-radius: 999px;
  font-size: .84rem; box-shadow: var(--shadow-lg); z-index: 100;
  opacity: 0; transition: opacity .3s var(--ease), transform .3s var(--ease);
  pointer-events: none;
}
.toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }
.toast.err { background: var(--blocked); }

/* ── Scope nav ────────────────────────────────────────────────────── */
.scope-nav { display: flex; gap: 6px; align-items: center; }
.scope-btn {
  font-family: var(--mono); font-size: .77rem; font-weight: 500;
  padding: 6px 16px; border-radius: 999px; border: 1px solid var(--line);
  background: var(--surface); color: var(--ink-soft); cursor: pointer;
  transition: background .18s, color .18s, border-color .18s, box-shadow .18s;
}
.scope-btn:hover { background: var(--surface-2); color: var(--ink); }
.scope-btn.is-active {
  background: var(--source); color: #fff; border-color: var(--source);
  box-shadow: 0 0 0 4px var(--source-soft);
}

/* ── Details toggle ──────────────────────────────────────────────── */
.toggle-btn {
  font-family: var(--mono); font-size: .72rem; font-weight: 500;
  display: inline-flex; align-items: center; gap: 6px;
  padding: 5px 12px; border-radius: 999px; border: 1px solid var(--line);
  background: var(--surface); color: var(--ink-soft); cursor: pointer;
  transition: all .18s; margin-left: 8px;
}
.toggle-btn:hover { background: var(--surface-2); color: var(--ink); }
.toggle-btn.is-on { background: var(--source-soft); color: var(--source); border-color: var(--source); }
.toggle-icon {
  width: 14px; height: 14px; border-radius: 3px; border: 2px solid currentColor;
  position: relative; flex: none; opacity: .5; transition: opacity .18s;
}
.toggle-btn.is-on .toggle-icon { opacity: 1; }
.toggle-icon::after {
  content: ""; position: absolute; inset: 2px; border-radius: 1px;
  background: currentColor; opacity: 0; transition: opacity .18s;
}
.toggle-btn.is-on .toggle-icon::after { opacity: 1; }

/* ── Hide details mode ────────────────────────────────────────────── */
.hide-details .msg--reasoning,
.hide-details .tool-call { display: none; }

/* ── Thinking indicator ────────────────────────────────────────────── */
.thinking-indicator {
  display: flex; align-items: center; gap: 10px;
  padding: 10px 0; animation: pop .25s var(--ease) both;
}
.thinking-dots { display: flex; gap: 4px; }
.thinking-dots span {
  width: 6px; height: 6px; border-radius: 50%;
  background: var(--source); opacity: .35;
  animation: thinkPulse 1.2s ease infinite;
}
.thinking-dots span:nth-child(2) { animation-delay: .2s; }
.thinking-dots span:nth-child(3) { animation-delay: .4s; }
@keyframes thinkPulse {
  0%, 100% { opacity: .35; transform: scale(1); }
  50% { opacity: 1; transform: scale(1.3); }
}
.thinking-label { font-family: var(--mono); font-size: .78rem; color: var(--ink-faint); }

/* ── Scripting connect row ────────────────────────────────────────── */
.env-row { display: flex; gap: 14px; align-items: flex-end; }
.field--endpoint { flex: 3; }
.field--token { flex: 3; }

/* ── Script layout (split panel) ──────────────────────────────────── */
.script-layout { display: grid; grid-template-columns: 1fr 1fr; gap: 20px; align-items: start; }
.script-panel {
  background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius);
  box-shadow: var(--shadow); overflow: hidden; position: sticky; top: 20px;
}
.script-toolbar {
  display: flex; align-items: center; justify-content: space-between; gap: 10px;
  padding: 12px 16px; border-bottom: 1px solid var(--line-soft);
  background: var(--surface-2);
}
.script-toolbar-title { font-family: var(--display); font-weight: 600; font-size: .85rem; color: var(--ink-soft); }
.script-toolbar-actions { display: flex; gap: 8px; align-items: center; flex: 1; justify-content: flex-end; }
.script-select {
  font-family: var(--sans); font-size: .82rem; padding: 5px 10px;
  border: 1px solid var(--line); border-radius: var(--radius-sm);
  background: var(--surface); color: var(--ink); max-width: 200px;
}
.script-textarea {
  width: 100%; min-height: 400px; max-height: 70vh;
  font-family: var(--mono); font-size: .82rem; line-height: 1.55;
  padding: 16px; border: none; resize: vertical; color: var(--ink);
  background: #FBF9F4; tab-size: 2;
}
.script-textarea:focus { outline: none; background: #FEFDF8; }
.script-textarea::placeholder { color: var(--ink-faint); }

/* ── Context menu ─────────────────────────────────────────────────── */
.ctx-menu {
  position: fixed; z-index: 100;
  background: var(--surface); border: 1px solid var(--line);
  border-radius: var(--radius-sm); box-shadow: var(--shadow-lg);
  padding: 4px; min-width: 140px;
}
.ctx-btn {
  display: block; width: 100%; text-align: left;
  font-family: var(--sans); font-size: .82rem; color: var(--ink);
  background: none; border: none; padding: 7px 12px; border-radius: 5px;
  cursor: pointer;
}
.ctx-btn:hover { background: var(--source-soft); color: var(--source); }

/* ── Chat panel (right side in scripting) ─────────────────────────── */
.chat-panel .panel--compact { margin: 0; padding: 20px; }

/* ── Responsive ───────────────────────────────────────────────────── */
@media (max-width: 620px) {
  .env-grid { grid-template-columns: 1fr; gap: 14px; }
  .env-flow { display: none; }
  .entity-row { flex-direction: column; gap: 10px; }
  .env-row { flex-direction: column; gap: 10px; }
  .script-layout { grid-template-columns: 1fr; }
  .panel { padding: 24px 20px 24px; }
  .stage { padding: 16px 16px 70px; }
  .masthead-inner { padding: 0 20px; }
}