:root {
  --bg: #0b0f14;
  --panel: #121821;
  --muted: #9bb0c0;
  --text: #e6eef5;
  --accent: #3aa6ff;
  --warn: #ffb86b;
}

* { box-sizing: border-box; }
body {
  margin: 0;
  font-family: ui-sans-serif, system-ui, -apple-system, Segoe UI, Roboto, Helvetica, Arial, Noto Sans, Apple Color Emoji, Segoe UI Emoji;
  color: var(--text);
  background: linear-gradient(180deg, #0b0f14 0%, #0e141c 100%);
}

.toggle-headless { display: none; }
.toggle-thorough { display: none; }

.container { max-width: 1100px; margin: 0 auto; padding: 24px; }

header { display: flex; align-items: center; justify-content: space-between; gap: 16px; }
h1 { margin: 0; font-size: 20px; font-weight: 600; color: var(--text); }

form { display: flex; align-items: center; gap: 8px; }
input[type="url"] {
  width: 420px; padding: 10px 12px; border-radius: 8px; border: 1px solid #1f2937; background: #0d131b; color: var(--text);
}
label.toggle { display: inline-flex; align-items: center; gap: 6px; color: var(--muted); font-size: 13px; }
label.toggle input { accent-color: var(--accent); }
button { padding: 10px 14px; border-radius: 8px; border: none; background: var(--accent); color: #00121c; font-weight: 600; cursor: pointer; }
button:hover { filter: brightness(1.1); }
button#cancel { background: #ff6b6b; color: #190000; }
button#completeEarly { background: #38b2ac; color: #001a19; white-space: nowrap;}

#modeAgentic { white-space: nowrap; }

.main { display: grid; grid-template-columns: 1fr 420px; gap: 16px; margin-top: 16px; }
.preview { background: var(--panel); border: 1px solid #1f2937; border-radius: 12px; overflow: hidden; }
.preview-header { padding: 8px 12px; font-size: 12px; color: var(--muted); border-bottom: 1px solid #1f2937; }
.preview-canvas { position: relative; }
#frame { display: block; width: 100%; aspect-ratio: 16/10; object-fit: contain; background: #0a0f15; }
.preview-placeholder { position: absolute; inset: 0; display: flex; align-items: center; justify-content: center; text-align: center; color: var(--muted); padding: 12px; pointer-events: none; }

.controls { display: flex; align-items: center; gap: 8px; padding: 8px 12px; border-top: 1px solid #1f2937; background: #0d131b; }
#timeline { flex: 1; }
.muted { color: var(--muted); font-size: 12px; }
.markers { display: flex; flex-wrap: wrap; gap: 6px; padding: 8px 12px 12px; border-top: 1px solid #1f2937; }
.marker { font-size: 11px; padding: 2px 8px; border-radius: 999px; background: #1a2431; border: 1px solid #2a3545; color: var(--muted); cursor: pointer; }
.marker.warn { background: #402c12; border-color: #5c3b17; color: #ffb86b; }
.marker.info { background: #112436; border-color: #16314a; color: #7ec3ff; }
.hidden { display: none; }
.rec { margin-left: 8px; color: #ff4d4f; font-weight: 700; letter-spacing: .5px; }
#download { background: #9ae6b4; color: #00331f; }

.panel { display: grid; grid-template-rows: auto; gap: 12px; }
.logs { background: var(--panel); border: 1px solid #1f2937; border-radius: 12px; padding: 12px; height: 60vh; overflow: auto; }
.results { background: var(--panel); border: 1px solid #1f2937; border-radius: 12px; padding: 12px; }
.final { background: var(--panel); border: 1px solid #1f2937; border-radius: 12px; padding: 12px; margin: 0; color: var(--muted); max-height: 30vh; overflow: auto; }

.log { margin: 0 0 8px; font-size: 13px; color: var(--muted); }
.log .tag { display: inline-block; font-size: 11px; padding: 2px 6px; border-radius: 6px; margin-right: 6px; color: #00121c; }
.log.warn .tag { background: var(--warn); }
.log.info .tag { background: var(--accent); }
.log.bot .tag { background: #bfb3ff; color: #1a1333; }
.log span { white-space: pre-wrap; }
/* Clear, readable error block */
.log.error { background: rgba(239, 68, 68, 0.08); border: 1px solid rgba(127, 29, 29, 0.9); color: #fecaca; padding: 10px; border-radius: 8px; }
.log.error .tag { background: #ef4444; color: #fff; }
.log.error span { color: #ffe4e6; font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, "Liberation Mono", "Courier New", monospace; line-height: 1.4; }
/* Slightly emphasize warnings too */
.log.warn { background: rgba(245, 158, 11, 0.06); border: 1px solid rgba(146, 64, 14, 0.7); color: #fde68a; padding: 10px; border-radius: 8px; }
.status { color: var(--muted); font-style: italic; margin-bottom: 8px; }

.summary { display: flex; flex-wrap: wrap; gap: 10px; margin-bottom: 12px; }
.chip { background: #0d131b; border: 1px solid #1f2937; color: var(--muted); padding: 6px 10px; border-radius: 999px; font-size: 12px; }
.issues { display: grid; gap: 20px; grid-template-columns: repeat(auto-fill, minmax(360px, 1fr)); align-items: stretch; }
.issues > * { min-width: 0; }
.issue { position: relative; border: 1px solid #1f2937; border-radius: 12px; padding: 14px; background: #0d131b; transition: border-color .2s ease, transform .12s ease; overflow: hidden; display: flex; flex-direction: column; height: 100%; }
.issue:hover { border-color: #2b3a4f; transform: translateY(-1px); }
.issue * { max-width: 100%; }
.issue .head { margin-bottom: 8px; align-items: center; }
.issue .head { padding-bottom: 6px; border-bottom: 1px solid #1f2937; }
.issue .comp { font-weight: 700; color: var(--text); font-size: 14px; line-height: 1.35; word-break: break-word; overflow-wrap: anywhere; }
.issue .msg, .issue .suggested-alt, .issue .ai-note, .issue .meta { word-break: break-word; overflow-wrap: anywhere; }
.issue .msg { margin-top: 8px; padding: 10px; border-radius: 8px; background: rgba(59, 130, 246, 0.06); border: 1px solid rgba(59, 130, 246, 0.18); color: var(--text); line-height: 1.45; }
.issue .msg::before { content: 'Violation'; display: block; font-size: 11px; letter-spacing: .3px; color: var(--muted); text-transform: uppercase; margin-bottom: 4px; }
/* Severity-coded highlight for violation text */
.issue:has(.pill.high) .msg { background: rgba(239, 68, 68, 0.12); border-color: rgba(239, 68, 68, 0.45); }
.issue:has(.pill.medium) .msg { background: rgba(245, 158, 11, 0.12); border-color: rgba(245, 158, 11, 0.45); }
.issue:has(.pill.low) .msg { background: rgba(16, 185, 129, 0.12); border-color: rgba(16, 185, 129, 0.45); }
.issue:has(.pill.info) .msg { background: rgba(58, 166, 255, 0.10); border-color: rgba(58, 166, 255, 0.35); }
/* Separate user-facing message below violation */
.issue .user-msg { margin-top: 8px; padding: 10px; border-radius: 8px; background: rgba(55, 65, 81, 0.35); border: 1px solid #253041; color: var(--text); line-height: 1.45; }
.issue .user-msg::before { content: 'Message'; display: block; font-size: 11px; letter-spacing: .3px; color: var(--muted); text-transform: uppercase; margin-bottom: 4px; }
.issue .meta.ai-note { font-style: normal; padding: 8px 10px; border-radius: 8px; border: 1px solid #2d3a4d; background: rgba(186, 104, 200, 0.08); color: #e9d5ff; }
.issue .meta.ai-note::before { content: 'AI Check'; display: block; font-size: 11px; letter-spacing: .3px; color: #c4b5fd; text-transform: uppercase; margin-bottom: 4px; }
.issue a { color: var(--accent); word-break: break-all; }
.kbd-pill { display: inline-block; margin-left: 6px; font-size: 10px; padding: 2px 6px; border-radius: 999px; color: #2e1065; background: #c4b5fd; border: 1px solid #8b5cf6; vertical-align: middle; }
.issue > * + * { margin-top: 10px; }
.issue > .chip { margin-top: auto; align-self: flex-start; }
.issue .head { display: flex; justify-content: space-between; align-items: start; gap: 10px; margin-bottom: 6px; }
.pill { font-size: 11px; padding: 2px 8px; border-radius: 999px; color: #00121c; background: var(--warn); }
.pill.high { background: #ef4444; color: #fee2e2; }
.pill.medium { background: #f59e0b; color: #111827; }
.pill.low { background: #10b981; color: #052e2b; }
.pill.info { background: var(--accent); color: #00121c; }
/* Accented border by severity when supported */
.issue:has(.pill.high) { border-color: rgba(239, 68, 68, 0.6); box-shadow: 0 0 0 1px rgba(239,68,68,.15) inset; }
.issue:has(.pill.medium) { border-color: rgba(245, 158, 11, 0.6); box-shadow: 0 0 0 1px rgba(245,158,11,.12) inset; }
.issue:has(.pill.low) { border-color: rgba(16, 185, 129, 0.5); box-shadow: 0 0 0 1px rgba(16,185,129,.1) inset; }
.issue:has(.pill.info) { border-color: rgba(58, 166, 255, 0.5); box-shadow: 0 0 0 1px rgba(58,166,255,.08) inset; }
/* Severity left bar for quick scanning */
.issue::before { content: ''; position: absolute; left: 0; top: 0; bottom: 0; width: 3px; background: transparent; }
.issue:has(.pill.high)::before { background: #ef4444; }
.issue:has(.pill.medium)::before { background: #f59e0b; }
.issue:has(.pill.low)::before { background: #10b981; }
.issue:has(.pill.info)::before { background: var(--accent); }
.comp { font-weight: 600; color: var(--text); }
.meta { color: var(--muted); font-size: 12px; }
.meta.flow { margin-bottom: 8px; line-height: 1.4; }
.flow-label { color: var(--muted); margin-right: 4px; }
.flow-arrow { margin: 0 6px; color: var(--accent); }
.issue-img-wrap { margin: 8px 0; }
.issue-img { display: block; max-height: 160px; max-width: 100%; border-radius: 8px; border: 1px solid #1f2937; object-fit: contain; background: #0a0f15; }
.issue-img-fallback { display: none; font-size: 11px; margin-top: 6px; color: var(--muted); }
.issue-img-broken { display: none; }
.issue-img-broken + .issue-img-fallback { display: block; }
.suggested-alt { margin: 8px 0; padding: 8px 10px; border-radius: 8px; background: #112436; border: 1px solid #16314a; color: var(--text); font-size: 13px; line-height: 1.4; }
.suggested-alt::before { content: 'Suggested Alt'; display: block; font-size: 11px; letter-spacing: .3px; color: #7ec3ff; text-transform: uppercase; margin-bottom: 4px; }
.ai-note { margin-top: 6px; font-style: italic; font-size: 12px; }
.ai-live { margin-top: 8px; padding: 8px 10px; border-radius: 8px; border: 1px solid #2d3a4d; background: rgba(58, 166, 255, 0.08); font-size: 13px; line-height: 1.45; }
.ai-live strong { color: var(--accent); font-style: normal; }
.issue .meta a { color: var(--accent); word-break: break-all; }
.issue .meta { line-height: 1.4; }
.msg { color: var(--muted); }

/* Clear, readable human/AI message block within an issue */
.user-msg { margin-top: 8px; padding-top: 6px; border-top: 1px solid #1f2937; }
.user-msg-intro { margin: 0 0 8px; line-height: 1.5; color: #e2e8f0; }
.user-msg-details { margin: 0; padding-left: 1.2rem; color: #e2e8f0; }
.user-msg-details li { margin: 6px 0; line-height: 1.45; }

@media (max-width: 980px) {
  .main { grid-template-columns: 1fr; }
}
/* Layout */
.layout { display: grid; grid-template-columns: 280px 1fr; min-height: 100vh; background: #0b0d10; color: #e6edf3; }
.sidebar { border-right: 1px solid #1f2a37; padding: 16px; display: flex; flex-direction: column; gap: 12px; }
.brand { font-weight: 700; letter-spacing: .5px; font-size: 18px; color: #9ecbff; }
.history-header { display: flex; align-items: center; justify-content: space-between; margin-top: 4px; color: #9aa6b2; }
.history-header button { background: #111827; color: #cbd5e1; border: 1px solid #253041; border-radius: 6px; padding: 2px 8px; cursor: pointer; }
.history-list { display: flex; flex-direction: column; gap: 6px; overflow:auto; }
.history-item { border: 1px solid #1f2a37; background: #0f1318; border-radius: 8px; padding: 8px; cursor: pointer; }
.history-item:hover { border-color: #334155; }
.history-item .url { color: #e2e8f0; font-size: 12px; word-break: break-all; display: flex; align-items: center; gap: 8px; }
.history-item .meta { color: #94a3b8; font-size: 11px; display: flex; gap: 10px; margin-top: 4px; }

/* Compact delete button in history rows */
.history-item .del-history {
  margin-left: auto;
  padding: 2px 6px;
  border-radius: 6px;
  border: 1px solid #253041;
  background: #111827;
  color: #cbd5e1;
  line-height: 1;
  font-size: 12px;
  cursor: pointer;
}
.history-item .del-history:hover {
  background: #1b2330;
  color: #fca5a5;
  border-color: #334155;
}
.history-item .del-history:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

.content { display: flex; flex-direction: column; min-width: 0; }
.toolbar { position: sticky; top: 0; z-index: 10; backdrop-filter: blur(6px); background: rgba(15,19,24,.7); border-bottom: 1px solid #1f2a37; }
.toolbar .bar { display: flex; gap: 8px; align-items: center; padding: 12px 16px; }
.toolbar input.url { flex: 1; min-width: 240px; }
.toolbar input.min { width: 120px; }
.toolbar .primary { background: #2563eb; border: 1px solid #1d4ed8; color: white; border-radius: 8px; padding: 8px 12px; }

/* Reuse existing preview/panel styles; ensure they flex within content */
.main { display: grid; grid-template-columns: 1fr 420px; gap: 16px; padding: 16px; align-items: start; }
/* Make the conclusion report span full width under the two columns */
.results { grid-column: 1 / -1; }
.preview { background: #0f1318; border: 1px solid #1f2a37; border-radius: 8px; padding: 12px; }
.panel { background: #0f1318; border: 1px solid #1f2a37; border-radius: 8px; padding: 12px; }

/* Issue image box + overlay */
.issue-img-box { position: relative; display: inline-block; }
.issue-bbox { position: absolute; pointer-events: none; }

/* Strong focus ring for keyboard accessibility */
button:focus-visible, .chip:focus-visible, .marker:focus-visible, a:focus-visible, input:focus-visible, select:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

/* Agentic live trace + plan approval */
.agent-trace {
  grid-column: 1 / -1;
  background: #0c1018;
  border: 1px solid #243044;
  border-radius: 10px;
  padding: 12px 14px;
  max-height: 42vh;
  overflow: auto;
  font-size: 13px;
  color: var(--text);
}
.agent-trace.hidden { display: none; }
.agent-trace h3 { margin: 0 0 8px; font-size: 13px; color: var(--accent); font-weight: 600; }
.agent-plan-block {
  margin-bottom: 14px;
  padding: 10px 12px;
  background: #0f1620;
  border: 1px solid #1e2a3a;
  border-radius: 8px;
  border-left: 3px solid #7c3aed;
}
.agent-plan-block .plan-meta { font-size: 11px; color: var(--muted); margin-bottom: 6px; }
/* Readable plan text (headings, lists, paragraphs) — same in trace, log, and modal */
.plan-body-readable {
  font-size: 14px;
  line-height: 1.55;
  color: #e2e8f0;
}
.plan-body-readable .plan-heading {
  margin: 12px 0 8px;
  font-size: 15px;
  font-weight: 600;
  color: #c4b5fd;
  line-height: 1.35;
}
.plan-body-readable .plan-heading:first-child {
  margin-top: 0;
}
.plan-body-readable .plan-para {
  margin: 0 0 10px;
  color: #e2e8f0;
}
.plan-body-readable .plan-muted {
  color: #94a3b8;
  font-style: italic;
}
.plan-body-readable .plan-list {
  margin: 0 0 12px;
  padding-left: 1.35rem;
  color: #e2e8f0;
}
.plan-body-readable .plan-list li {
  margin-bottom: 6px;
}

.plan-approval-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.65);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 100000;
  padding: 16px;
  box-sizing: border-box;
}
.plan-approval-modal {
  background: #0f172a;
  color: #f1f5f9;
  border: 1px solid #334155;
  border-radius: 16px;
  max-width: min(720px, 100%);
  width: 100%;
  max-height: min(88vh, 900px);
  display: flex;
  flex-direction: column;
  box-shadow: 0 20px 50px rgba(0, 0, 0, 0.55);
  overflow: hidden;
}
.plan-approval-intro {
  margin: 0;
  padding: 16px 20px 0;
  font-size: 15px;
  line-height: 1.5;
  color: #cbd5e1;
}
.plan-approval-h2 {
  margin: 0;
  padding: 8px 20px 0;
  font-size: 18px;
  font-weight: 700;
  color: #f8fafc;
  letter-spacing: -0.02em;
}
.plan-approval-hint {
  margin: 0;
  padding: 10px 20px 12px;
  font-size: 13px;
  line-height: 1.45;
  color: #94a3b8;
  border-bottom: 1px solid #1e293b;
}
.plan-approval-scroll {
  flex: 1;
  min-height: 120px;
  max-height: 50vh;
  overflow: auto;
  padding: 16px 20px;
  background: #0c1222;
  -webkit-overflow-scrolling: touch;
}
.plan-approval-scroll .plan-body-readable {
  font-size: 15px;
  line-height: 1.6;
}
.plan-approval-actions {
  display: flex;
  gap: 12px;
  justify-content: flex-end;
  flex-wrap: wrap;
  padding: 16px 20px;
  border-top: 1px solid #1e293b;
  background: #111827;
}
.plan-btn {
  padding: 10px 18px;
  border-radius: 10px;
  font-weight: 600;
  font-size: 15px;
  cursor: pointer;
  border: none;
}
.plan-btn-approve {
  background: #22c55e;
  color: #052e16;
}
.plan-btn-decline {
  background: #1e293b;
  color: #f8fafc;
  border: 1px solid #64748b;
}

/* Full plan blocks in the log column */
.log.plan-log {
  display: flex;
  gap: 10px;
  align-items: flex-start;
  background: rgba(124, 58, 237, 0.08);
  border: 1px solid rgba(139, 92, 246, 0.35);
  border-radius: 10px;
  padding: 12px 14px;
}
.log.plan-log .plan-log-wrap {
  flex: 1;
  min-width: 0;
}
.log.plan-log .plan-log-title {
  font-weight: 600;
  font-size: 14px;
  color: #ddd6fe;
  margin-bottom: 8px;
}
.log.plan-log .plan-log-body {
  max-height: 280px;
  overflow: auto;
  padding: 10px 12px;
  background: #0a0f18;
  border-radius: 8px;
  border: 1px solid #1e293b;
}
.agent-action-block {
  margin-bottom: 12px;
  padding: 10px 12px;
  background: #0a1219;
  border: 1px solid #1a2835;
  border-radius: 8px;
  border-left: 3px solid #0ea5e9;
}
.agent-action-block .step-meta { font-size: 11px; color: var(--muted); margin-bottom: 6px; }
.agent-action-block pre {
  margin: 0 0 8px;
  white-space: pre-wrap;
  word-break: break-word;
  font-family: ui-monospace, Menlo, Consolas, monospace;
  font-size: 11px;
  max-height: 200px;
  overflow: auto;
  color: #bae6fd;
}
.agent-action-block pre.obs { color: #a7f3d0; max-height: 280px; }
.ux-review {
  margin-bottom: 16px;
  padding: 12px 14px;
  background: #0f1620;
  border: 1px solid #243044;
  border-radius: 10px;
}
.ux-review h3 { margin: 0 0 10px; font-size: 14px; color: #a78bfa; }
.ux-block { margin-bottom: 12px; }
.ux-block strong { display: block; font-size: 12px; color: var(--muted); margin-bottom: 4px; }
.ux-block pre {
  margin: 0;
  white-space: pre-wrap;
  word-break: break-word;
  font-size: 12px;
  line-height: 1.45;
  color: #e2e8f0;
}
.log.agent .tag { background: #7c3aed; color: #fff; }
