:root {
  --bg: #fafafa;
  --fg: #1a1a1a;
  --muted: #666;
  --border: #ddd;
  --user-bg: #e8f0fe;
  --assistant-bg: #f5f5f5;
  --error: #b00020;
  --accent: #1a73e8;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  background: var(--bg);
  color: var(--fg);
  line-height: 1.5;
}

.container {
  max-width: 900px;
  margin: 0 auto;
  padding: 1.5rem;
}

/* The IDE layout takes the full viewport. */
body:has(.ide) > .container,
body:has(.live-grid) > .container {
  max-width: none;
  padding: 0.75rem;
  height: 100vh;
  display: flex;
  flex-direction: column;
}

.ide {
  display: flex;
  flex: 1;
  gap: 0.75rem;
  min-height: 0;
}
.ide-pane {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-width: 0;
  background: white;
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 0.5rem 0.75rem;
}
.editor-pane { flex: 1.3; }
.pane-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding-bottom: 0.4rem;
  border-bottom: 1px solid var(--border);
  margin-bottom: 0.5rem;
}
.pane-subheader {
  font-size: 0.8rem;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: 0.05em;
  margin: 0.4rem 0 0.2rem;
}
.editor-toolbar { display: flex; align-items: center; gap: 0.75rem; }
.editor-toolbar label { font-size: 0.85rem; }
.editor-toolbar select { padding: 0.2rem; }
.run-controls button {
  padding: 0.3rem 1rem;
  background: var(--accent);
  color: white;
  border: none;
  border-radius: 4px;
  cursor: pointer;
  font-weight: 600;
}
.run-controls button#stop-btn { background: var(--error); }
.editor-host {
  flex: 1;
  min-height: 200px;
  border: 1px solid var(--border);
  border-radius: 4px;
  overflow: hidden;
}
.editor-host .cm-editor { height: 100%; font-size: 13px; }
.editor-host .cm-scroller { font-family: ui-monospace, SFMono-Regular, Menlo, monospace; }

/* y-codemirror.next collaborative cursor styling. The cursor color comes
   from awareness state (set in JS per client). The label hovers above. */
.cm-ySelection { background-color: var(--ySel, #1a73e833); }
.cm-yLineSelection { background-color: var(--ySel, #1a73e833); }
.cm-ySelectionCaret {
  position: relative;
  border-left: 2px solid var(--yClr, #1a73e8);
  margin-left: -1px;
  margin-right: -1px;
  box-sizing: border-box;
}
.cm-ySelectionCaretDot {
  width: 6px; height: 6px;
  border-radius: 50%;
  background-color: var(--yClr, #1a73e8);
  position: absolute;
  top: -2px; left: -3px;
}
.cm-ySelectionInfo {
  position: absolute;
  top: -1.2em;
  left: -2px;
  font-size: 0.7em;
  padding: 0 0.25em;
  background-color: var(--yClr, #1a73e8);
  color: #fff;
  border-radius: 2px;
  white-space: nowrap;
  z-index: 100;
  pointer-events: none;
}

.stdin-panel {
  margin-top: 0.4rem;
  font-size: 0.85rem;
}
.stdin-panel summary { cursor: pointer; color: var(--muted); }
.stdin-panel textarea {
  width: 100%;
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  padding: 0.4rem;
  border: 1px solid var(--border);
  border-radius: 4px;
  margin-top: 0.25rem;
}

.output-panel-wrap { margin-top: 0.4rem; display: flex; flex-direction: column; min-height: 100px; max-height: 30vh; }
.run-output {
  flex: 1;
  background: #1e1e1e;
  color: #ddd;
  border-radius: 4px;
  padding: 0.5rem 0.75rem;
  overflow: auto;
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: 0.85rem;
  white-space: pre-wrap;
  word-break: break-word;
}
.run-output .phase { color: #4ec9b0; margin: 0.2rem 0; font-weight: 600; }
.run-output .phase.error { color: #f48771; }
.run-output .stdout { color: #d4d4d4; }
.run-output .stderr { color: #f48771; }

/* Chat pane within IDE. */
.ide .chat-pane .messages { flex: 1; max-height: none; }
.ide .chat-pane #msgform { margin-top: 0.5rem; }
.ide .pane-header .end-form { margin: 0; }

/* Admin live monitor grid. */
.live-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 1rem;
}
.live-grid .live-section:nth-child(1) { grid-column: 1 / -1; }
.live-grid .live-section:nth-child(4) { grid-column: 2; grid-row: 2 / span 2; }
.live-section h2 {
  font-size: 0.9rem;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--muted);
  margin-bottom: 0.3rem;
}
.code-snapshot {
  background: #f5f5f5;
  border: 1px solid var(--border);
  border-radius: 4px;
  padding: 0.5rem 0.75rem;
  max-height: 40vh;
  overflow: auto;
  font-size: 0.85rem;
  margin: 0;
}
.messages.compact { max-height: 70vh; }
.muted { color: var(--muted); }

h1, h2 { margin-top: 0; }

.subtitle { color: var(--muted); margin-top: 0; }

.chat-header,
.admin-header {
  display: flex;
  flex-wrap: wrap;
  align-items: baseline;
  gap: 1rem;
  border-bottom: 1px solid var(--border);
  padding-bottom: 1rem;
  margin-bottom: 1rem;
}

.chat-header h1 { margin: 0; flex: 1 1 auto; }
.end-form { margin-left: auto; }

.messages {
  display: flex;
  flex-direction: column;
  gap: 0.75rem;
  max-height: 65vh;
  overflow-y: auto;
  padding: 0.5rem 0;
  margin-bottom: 1rem;
}

.msg {
  padding: 0.5rem 0.75rem;
  border-radius: 6px;
  border: 1px solid var(--border);
}
.msg.user      { background: var(--user-bg); }
.msg.assistant { background: var(--assistant-bg); }
.msg.assistant.pending { border-style: dashed; }
.msg .role {
  font-size: 0.8rem;
  text-transform: lowercase;
  color: var(--muted);
  margin-bottom: 0.2rem;
}
.msg .content {
  white-space: pre-wrap;
  word-break: break-word;
  font-size: 0.95rem;
}
/* Plain-text bubbles (user messages, streaming assistant text) use a mono
   font; rendered markdown bubbles use the default sans serif with mono code. */
.msg.user .content,
.msg .content.streaming {
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
}
.msg .content.rendered {
  white-space: normal;
}
.msg .content.rendered p {
  margin: 0 0 0.5rem 0;
}
.msg .content.rendered p:last-child { margin-bottom: 0; }
.msg .content.rendered code {
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: 0.9em;
  background: rgba(0, 0, 0, 0.06);
  padding: 0.1em 0.35em;
  border-radius: 3px;
}
.msg .content.rendered pre {
  background: #f0f0f0;
  border: 1px solid var(--border);
  border-radius: 4px;
  padding: 0.5rem 0.75rem;
  overflow-x: auto;
  font-size: 0.9em;
  margin: 0.4rem 0;
}
.msg .content.rendered pre code {
  background: none;
  padding: 0;
  font-size: 1em;
}
.msg .content.rendered ul,
.msg .content.rendered ol { margin: 0.3rem 0 0.5rem 1.2rem; padding: 0; }
.msg .content.rendered li { margin-bottom: 0.2rem; }
.msg .content.rendered strong { font-weight: 600; }
.msg .usage {
  font-size: 0.75rem;
  color: var(--muted);
  margin-top: 0.25rem;
}
.msg .error { color: var(--error); }

#msgform {
  display: flex;
  gap: 0.5rem;
}
#msgform textarea {
  flex: 1;
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  padding: 0.5rem;
  border: 1px solid var(--border);
  border-radius: 4px;
  resize: vertical;
}
#msgform button {
  padding: 0.5rem 1rem;
  background: var(--accent);
  color: white;
  border: none;
  border-radius: 4px;
  cursor: pointer;
}
#msgform button:disabled { opacity: 0.5; cursor: not-allowed; }
#msgform.disabled textarea { background: #f0f0f0; }

.link-button {
  background: none;
  border: none;
  color: var(--accent);
  cursor: pointer;
  text-decoration: underline;
  padding: 0;
  font: inherit;
}

.portal-message {
  max-width: 480px;
  margin: 4rem auto;
  text-align: center;
}

.datatable {
  width: 100%;
  border-collapse: collapse;
  margin-bottom: 1.5rem;
  font-size: 0.9rem;
}
.datatable th, .datatable td {
  text-align: left;
  padding: 0.5rem 0.75rem;
  border-bottom: 1px solid var(--border);
}
.datatable th {
  background: #f0f0f0;
  font-weight: 600;
}
.link-display {
  width: 100%;
  padding: 0.25rem;
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: 0.8rem;
  border: 1px solid var(--border);
  border-radius: 3px;
  background: white;
}

section { margin-bottom: 2rem; }
section h2 {
  font-size: 1.1rem;
  margin-bottom: 0.5rem;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: 0.05em;
}
section form { display: flex; gap: 0.75rem; align-items: end; }
section form label { display: flex; flex-direction: column; font-size: 0.85rem; }
section form input { padding: 0.4rem; border: 1px solid var(--border); border-radius: 4px; }
section form button { padding: 0.4rem 1rem; background: var(--accent); color: white; border: none; border-radius: 4px; cursor: pointer; }
