/* Cortex — a modern, minimal chat UI.
 *
 * This stylesheet is loaded only by Cortex, so the :root overrides below
 * re-theme the shared penguina kit (menu, buttons, status pill) for this
 * app alone — penguina.css itself is untouched, so the rest of the suite
 * is unaffected.
 */

/* --- design tokens ------------------------------------------------------ */

:root {
  --font-body: 'DM Sans', system-ui, -apple-system, 'Segoe UI', sans-serif;
  --cx-code: ui-monospace, SFMono-Regular, 'SF Mono', Menlo, Consolas, monospace;

  --bg: #f5f6f8;
  --panel: #ffffff;
  --panel-strong: #ffffff;
  --panel-soft: #f1f3f5;
  --border: rgba(15, 23, 42, 0.09);
  --border-strong: rgba(15, 23, 42, 0.14);
  --text: #1a1d24;
  --muted: #6b7383;
  --accent: #2f7cff;
  --accent-ink: #2152c9;
  --pill: rgba(47, 124, 255, 0.10);
  --pill-border: rgba(47, 124, 255, 0.22);
  --shadow: 0 1px 2px rgba(15, 23, 42, 0.04), 0 12px 32px rgba(15, 23, 42, 0.07);
  --radius: 14px;
  --menu-bg: rgba(255, 255, 255, 0.82);
  --menu-text: #3a4253;
  --menu-sep: #aab2c0;
  --menu-right: #4b5563;
  --title-text: #1a1d24;
  --label-text: #1a1d24;
  --brand-text: #5b647a;
  --mono-text: #3a4253;
  --btn-secondary-bg: rgba(15, 23, 42, 0.05);
  --pill-danger-bg: #fdecec;
  --pill-danger-border: rgba(220, 38, 38, 0.22);
  --pill-danger-text: #b42318;

  --cx-user: #eef0f3;
  --cx-hover: rgba(15, 23, 42, 0.05);
  --cx-ring: rgba(47, 124, 255, 0.16);

  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0e0f13;
    --panel: #16181d;
    --panel-strong: #16181d;
    --panel-soft: #1e2128;
    --border: rgba(255, 255, 255, 0.09);
    --border-strong: rgba(255, 255, 255, 0.15);
    --text: #e7e9ee;
    --muted: #9aa1af;
    --accent: #5e9dff;
    --accent-ink: #bcd4ff;
    --pill: rgba(94, 157, 255, 0.15);
    --pill-border: rgba(94, 157, 255, 0.30);
    --shadow: 0 1px 2px rgba(0, 0, 0, 0.4), 0 14px 36px rgba(0, 0, 0, 0.4);
    --menu-bg: rgba(20, 22, 27, 0.82);
    --menu-text: #d4d8e0;
    --menu-sep: #6b7280;
    --menu-right: #c2c7d2;
    --title-text: #e7e9ee;
    --label-text: #e7e9ee;
    --brand-text: #aab1bf;
    --mono-text: #c8cdd7;
    --btn-secondary-bg: rgba(255, 255, 255, 0.06);
    --pill-danger-bg: rgba(248, 113, 113, 0.13);
    --pill-danger-border: rgba(248, 113, 113, 0.30);
    --pill-danger-text: #fca5a5;

    --cx-user: #262932;
    --cx-hover: rgba(255, 255, 255, 0.06);
    --cx-ring: rgba(94, 157, 255, 0.22);

    color-scheme: dark;
  }
}

/* --- base --------------------------------------------------------------- */

body { background: var(--bg); }
::selection { background: var(--cx-ring); }

/* --- shared kit: calmer menu, buttons, status pill ---------------------- */

.pg-menu {
  box-shadow: none;
  border-bottom: 1px solid var(--border);
}

.btn {
  min-height: 0;
  padding: 7px 13px;
  border-radius: 9px;
  font-size: 13px;
  font-weight: 600;
  letter-spacing: 0;
  box-shadow: none;
}
.btn:hover { transform: none; box-shadow: none; filter: none; }
.btn:active { transform: none; }
.btn.secondary {
  background: var(--btn-secondary-bg);
  color: var(--text);
  border: 1px solid var(--border);
}
.btn.secondary:hover { background: var(--cx-hover); }

.pg-pill {
  gap: 6px;
  padding: 5px 10px 5px 9px;
  border-radius: 999px;
  font-size: 12px;
  font-weight: 600;
}
.pill-dot { width: 7px; height: 7px; box-shadow: none; }
.pill-online .pill-dot,
.pill-starting .pill-dot,
.pill-offline .pill-dot { box-shadow: none; animation: none; }

/* --- login -------------------------------------------------------------- */

.cortex-login {
  width: 100%;
  max-width: 400px;
  margin: 0 auto;
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  gap: 13px;
  padding: 36px 32px;
  background: var(--panel-strong);
  border: 1px solid var(--border);
  border-radius: 18px;
  box-shadow: var(--shadow);
  animation: cortexIn 280ms ease;
}
.cortex-mark { display: block; }
.cortex-login-title {
  margin: 4px 0 0;
  font-size: 26px;
  font-weight: 600;
  letter-spacing: -0.02em;
  color: var(--text);
}
.cortex-login-sub {
  margin: 0;
  font-size: 15px;
  line-height: 1.6;
  color: var(--muted);
}
.cortex-btn-primary {
  width: 100%;
  margin-top: 6px;
  padding: 12px 16px;
  border: none;
  border-radius: 11px;
  background: var(--accent);
  color: #fff;
  font: inherit;
  font-size: 15px;
  font-weight: 600;
  cursor: pointer;
  transition: filter 140ms ease;
}
.cortex-btn-primary:hover { filter: brightness(1.07); }
.cortex-btn-primary:active { filter: brightness(0.95); }
.cortex-error {
  margin: 0;
  width: 100%;
  padding: 9px 12px;
  border-radius: 10px;
  font-size: 13.5px;
  background: var(--pill-danger-bg);
  border: 1px solid var(--pill-danger-border);
  color: var(--pill-danger-text);
}

/* --- chat shell --------------------------------------------------------- */

.cortex-app {
  width: 100%;
  max-width: 1020px;
  margin: 0 auto;
  height: min(80vh, 840px);
  display: flex;
  flex-direction: row;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 18px;
  box-shadow: var(--shadow);
  overflow: hidden;
  animation: cortexIn 280ms ease;
}
.cortex-main {
  flex: 1;
  min-width: 0;
  display: flex;
  flex-direction: column;
}

/* --- conversation sidebar ---------------------------------------------- */

.cortex-sidebar {
  flex: 0 0 240px;
  display: flex;
  flex-direction: column;
  gap: 8px;
  padding: 14px 10px 10px;
  background: var(--panel-sub, rgba(127,127,127,0.04));
  border-right: 1px solid var(--border);
  overflow: hidden;
}
.cortex-new-chat-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  padding: 8px 10px;
  font: inherit;
  font-size: 13px;
  font-weight: 500;
  color: var(--text);
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 8px;
  cursor: pointer;
  transition: background 120ms ease, border-color 120ms ease;
}
.cortex-new-chat-btn:hover { background: var(--panel-hover, rgba(127,127,127,0.08)); border-color: var(--border-strong, var(--border)); }
.cortex-new-chat-btn svg { flex: none; }
.conv-list-label {
  margin: 6px 4px 2px;
  font-size: 11px;
  font-weight: 600;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  color: var(--muted);
}
.conv-list {
  list-style: none;
  margin: 0;
  padding: 0;
  overflow-y: auto;
  flex: 1;
  min-height: 0;
  scrollbar-width: thin;
  scrollbar-color: var(--border-strong) transparent;
}
.conv-list li {
  position: relative;
  display: flex;
  align-items: center;
}
.conv-item {
  flex: 1;
  min-width: 0;
  display: block;
  padding: 7px 9px;
  margin: 1px 0;
  font-size: 13px;
  line-height: 1.35;
  color: var(--text);
  background: transparent;
  border: 0;
  border-radius: 7px;
  text-align: left;
  cursor: pointer;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.conv-item:hover { background: var(--panel-hover, rgba(127,127,127,0.10)); }
.conv-item.is-active {
  background: var(--panel-hover, rgba(127,127,127,0.14));
  font-weight: 500;
}
.conv-list li .conv-del {
  position: absolute;
  right: 4px;
  top: 50%;
  transform: translateY(-50%);
  display: none;
  padding: 4px;
  font: inherit;
  font-size: 11px;
  color: var(--muted);
  background: transparent;
  border: 0;
  border-radius: 5px;
  cursor: pointer;
}
.conv-list li:hover .conv-del { display: inline-flex; }
.conv-del:hover { color: var(--danger, #c53030); background: rgba(127,127,127,0.15); }
.conv-list-empty {
  margin: 6px 4px;
  font-size: 12px;
  color: var(--muted);
  font-style: italic;
}

@media (max-width: 720px) {
  .cortex-sidebar { display: none; }
}

.cortex-head {
  flex-shrink: 0;
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 11px 13px 11px 14px;
  border-bottom: 1px solid var(--border);
}
.cortex-sidebar-toggle {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 28px;
  height: 28px;
  padding: 0;
  color: var(--muted);
  background: transparent;
  border: 0;
  border-radius: 6px;
  cursor: pointer;
  opacity: 0;
  transition: background 120ms ease, color 120ms ease, opacity 200ms ease;
}
/* At rest the penguina mark slides over the (invisible) toggle button so the
   header reads as one icon. pointer-events:none lets hover pass through to the
   button beneath, which then fades the button in and slides the mark back to
   its natural slot. */
.cortex-head .cortex-id {
  transform: translateX(-38px);
  pointer-events: none;
  transition: transform 240ms cubic-bezier(0.4, 0, 0.2, 1);
}
.cortex-sidebar-toggle:hover,
.cortex-sidebar-toggle:focus-visible {
  opacity: 1;
  color: var(--text);
  background: rgba(127,127,127,0.10);
}
.cortex-sidebar-toggle:hover ~ .cortex-id,
.cortex-sidebar-toggle:focus-visible ~ .cortex-id {
  transform: translateX(0);
}
.cortex-app.sidebar-collapsed .cortex-sidebar { display: none; }
.cortex-id {
  display: inline-flex;
  align-items: center;
  gap: 9px;
  font-size: 14px;
  font-weight: 600;
  color: var(--text);
}
.cortex-id img { display: block; }

/* --- message list ------------------------------------------------------- */

.msg-area {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  padding: 24px;
  display: flex;
  flex-direction: column;
  gap: 22px;
  scrollbar-width: thin;
  scrollbar-color: var(--border-strong) transparent;
}
.msg-area::-webkit-scrollbar { width: 10px; }
.msg-area::-webkit-scrollbar-thumb {
  background: var(--border-strong);
  background-clip: padding-box;
  border: 3px solid transparent;
  border-radius: 999px;
}
.msg-area::-webkit-scrollbar-thumb:hover {
  background: var(--muted);
  background-clip: padding-box;
}

.msg { display: flex; flex-direction: column; max-width: 100%; }
.msg.user { align-items: flex-end; }
.msg.assistant { align-items: flex-start; }

.msg-bubble { max-width: 100%; }
.msg.user .msg-bubble {
  max-width: 78%;
  padding: 10px 14px;
  border-radius: 16px 16px 5px 16px;
  background: var(--cx-user);
}
.msg.assistant .msg-bubble { padding: 0; background: transparent; }
.msg.assistant.is-error .msg-bubble {
  padding: 11px 14px;
  border-radius: 12px;
  background: var(--pill-danger-bg);
  border: 1px solid var(--pill-danger-border);
  color: var(--pill-danger-text);
}

.msg-text {
  font-size: 15px;
  line-height: 1.7;
  overflow-wrap: break-word;
}
.msg-text:empty { display: none; }

/* User messages: plain text, but preserve newlines the user typed. */
.msg.user .msg-text { white-space: pre-wrap; }

/* Assistant messages: rendered markdown. Tighten default block spacing so the
   bubble doesn't get airy, scale headings down (they're inside chat, not docs),
   and give code blocks/blockquotes their own contained look. */
.msg.assistant .msg-text > :first-child { margin-top: 0; }
.msg.assistant .msg-text > :last-child { margin-bottom: 0; }
.msg.assistant .msg-text p { margin: 0.4em 0; }
.msg.assistant .msg-text h1,
.msg.assistant .msg-text h2,
.msg.assistant .msg-text h3,
.msg.assistant .msg-text h4,
.msg.assistant .msg-text h5,
.msg.assistant .msg-text h6 {
  margin: 0.8em 0 0.3em;
  line-height: 1.3;
  font-weight: 600;
}
.msg.assistant .msg-text h1 { font-size: 1.25em; }
.msg.assistant .msg-text h2 { font-size: 1.18em; }
.msg.assistant .msg-text h3 { font-size: 1.10em; }
.msg.assistant .msg-text h4,
.msg.assistant .msg-text h5,
.msg.assistant .msg-text h6 { font-size: 1em; }
.msg.assistant .msg-text ul,
.msg.assistant .msg-text ol {
  margin: 0.4em 0;
  padding-left: 1.4em;
}
.msg.assistant .msg-text li { margin: 0.15em 0; }
.msg.assistant .msg-text li > p { margin: 0.2em 0; }
.msg.assistant .msg-text a { color: var(--accent, #2a6df4); text-decoration: underline; }
.msg.assistant .msg-text strong { font-weight: 600; }
.msg.assistant .msg-text em { font-style: italic; }
.msg.assistant .msg-text code {
  font-family: var(--cx-code);
  font-size: 0.92em;
  padding: 0.1em 0.35em;
  background: var(--code-bg, rgba(127,127,127,0.14));
  border-radius: 4px;
}
.msg.assistant .msg-text pre {
  margin: 0.5em 0;
  padding: 10px 12px;
  background: var(--code-bg, rgba(127,127,127,0.14));
  border-radius: 8px;
  overflow-x: auto;
  white-space: pre;
  font-size: 0.92em;
  line-height: 1.5;
}
.msg.assistant .msg-text pre code {
  font-size: 1em;
  padding: 0;
  background: transparent;
  border-radius: 0;
}
.msg.assistant .msg-text blockquote {
  margin: 0.5em 0;
  padding: 0.1em 0 0.1em 12px;
  border-left: 3px solid var(--muted);
  color: var(--muted);
}
.msg.assistant .msg-text hr {
  border: 0;
  border-top: 1px solid var(--muted);
  margin: 0.8em 0;
  opacity: 0.4;
}
.msg.assistant .msg-text table {
  border-collapse: collapse;
  margin: 0.5em 0;
  font-size: 0.95em;
}
.msg.assistant .msg-text th,
.msg.assistant .msg-text td {
  padding: 4px 8px;
  border: 1px solid var(--muted);
}
.msg.assistant .msg-text th { font-weight: 600; text-align: left; }

/* --- tool calls -------------------------------------------------------- */

.msg-tools { display: flex; flex-direction: column; gap: 6px; }
.msg-tools:not(:empty) { margin-bottom: 12px; }

.tool-chip {
  font-size: 13px;
}

.tool-summary {
  display: flex;
  align-items: center;
  gap: 7px;
  padding: 2px 0;
  line-height: 1.5;
}
.tool-summary::before {
  content: '';
  flex: none;
  width: 12px;
  height: 12px;
  background-color: var(--muted);
  -webkit-mask: url("data:image/svg+xml;utf8,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='black' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpath d='M14.7 6.3a1 1 0 0 0 0 1.4l1.6 1.6a1 1 0 0 0 1.4 0l3.77-3.77a6 6 0 0 1-7.94 7.94l-6.91 6.91a2.12 2.12 0 0 1-3-3l6.91-6.91a6 6 0 0 1 7.94-7.94l-3.76 3.76z'/%3E%3C/svg%3E") center / contain no-repeat;
          mask: url("data:image/svg+xml;utf8,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='black' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpath d='M14.7 6.3a1 1 0 0 0 0 1.4l1.6 1.6a1 1 0 0 0 1.4 0l3.77-3.77a6 6 0 0 1-7.94 7.94l-6.91 6.91a2.12 2.12 0 0 1-3-3l6.91-6.91a6 6 0 0 1 7.94-7.94l-3.76 3.76z'/%3E%3C/svg%3E") center / contain no-repeat;
}

.tool-name { font-weight: 500; color: var(--muted); }
.tool-args {
  min-width: 0;
  font-family: var(--cx-code);
  font-size: 12px;
  color: var(--muted);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.tool-status { margin-left: auto; flex: none; font-size: 12px; color: var(--muted); }

/* --- action confirmation card ------------------------------------------ */

.action-card {
  margin: 4px 0 2px;
  padding: 12px 13px;
  display: flex;
  flex-direction: column;
  gap: 8px;
  background: var(--panel-soft);
  border: 1px solid var(--border-strong);
  border-radius: 12px;
}
.action-card.resolved { opacity: 0.9; }
.action-card.resolved.approved { border-color: var(--pill-border); }

.action-card-head { display: flex; align-items: center; gap: 8px; }
.action-card-badge {
  flex: none;
  padding: 2px 7px;
  font-size: 11px;
  font-weight: 700;
  letter-spacing: 0.03em;
  text-transform: uppercase;
  color: var(--accent-ink);
  background: var(--pill);
  border: 1px solid var(--pill-border);
  border-radius: 6px;
}
.action-card-title {
  font-size: 14px;
  font-weight: 600;
  line-height: 1.4;
  color: var(--text);
}
.action-card-meta {
  font-family: var(--cx-code);
  font-size: 12px;
  color: var(--muted);
}

.action-card-params {
  display: flex;
  flex-direction: column;
  gap: 3px;
  padding: 7px 9px;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 8px;
}
.action-card-param {
  display: flex;
  gap: 8px;
  font-family: var(--cx-code);
  font-size: 12px;
  line-height: 1.5;
}
.param-k { flex: none; min-width: 84px; color: var(--muted); }
.param-v { color: var(--text); overflow-wrap: anywhere; }

.action-card-btns {
  display: flex;
  justify-content: flex-end;
  gap: 8px;
  margin-top: 2px;
}
.action-btn {
  padding: 7px 14px;
  font: inherit;
  font-size: 13px;
  font-weight: 600;
  border-radius: 9px;
  border: 1px solid var(--border-strong);
  background: var(--panel);
  color: var(--text);
  cursor: pointer;
  transition: filter 130ms ease, background 130ms ease;
}
.action-btn:hover { background: var(--cx-hover); }
.action-btn.primary {
  border-color: transparent;
  background: var(--accent);
  color: #fff;
}
.action-btn.primary:hover { filter: brightness(1.08); background: var(--accent); }
.action-btn:disabled { opacity: 0.5; pointer-events: none; }

.action-card-verdict {
  font-size: 12.5px;
  font-weight: 600;
  color: var(--muted);
}
.action-card.approved .action-card-verdict { color: var(--accent-ink); }

/* --- typing indicator --------------------------------------------------- */

.dots { display: inline-flex; gap: 4px; padding: 4px 0; }
.dots i {
  width: 6px; height: 6px;
  border-radius: 50%;
  background: var(--muted);
  animation: dotPulse 1.2s infinite ease-in-out;
}
.dots i:nth-child(2) { animation-delay: 0.18s; }
.dots i:nth-child(3) { animation-delay: 0.36s; }
@keyframes dotPulse {
  0%, 60%, 100% { opacity: 0.25; transform: translateY(0); }
  30% { opacity: 1; transform: translateY(-3px); }
}

/* --- welcome / empty state --------------------------------------------- */

.welcome {
  margin: auto;
  max-width: 440px;
  padding: 16px 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 13px;
  text-align: center;
}
.welcome-mark { width: 46px; height: 46px; }
.welcome h2 {
  margin: 0;
  font-size: 21px;
  font-weight: 600;
  letter-spacing: -0.01em;
  color: var(--text);
}
.welcome p {
  margin: 0;
  font-size: 14px;
  line-height: 1.65;
  color: var(--muted);
}
.suggest-row {
  margin-top: 4px;
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  justify-content: center;
}
.suggest-chip {
  padding: 8px 13px;
  font: inherit;
  font-size: 13px;
  color: var(--text);
  background: var(--panel-soft);
  border: 1px solid var(--border);
  border-radius: 999px;
  cursor: pointer;
  transition: background 130ms ease, border-color 130ms ease;
}
.suggest-chip:hover { background: var(--cx-hover); border-color: var(--border-strong); }

/* --- composer ----------------------------------------------------------- */

.composer-area { flex-shrink: 0; padding: 12px 16px 16px; }
.composer {
  display: flex;
  align-items: flex-end;
  gap: 8px;
  padding: 8px 8px 8px 15px;
  background: var(--panel-soft);
  border: 1px solid var(--border-strong);
  border-radius: 16px;
  transition: border-color 140ms ease, box-shadow 140ms ease;
}
.composer:focus-within {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--cx-ring);
}
.composer-input {
  flex: 1;
  min-width: 0;
  max-height: 160px;
  padding: 7px 0;
  resize: none;
  border: none;
  background: transparent;
  color: var(--text);
  font: inherit;
  font-size: 15px;
  line-height: 1.5;
}
.composer-input:focus { outline: none; }
.composer-input::placeholder { color: var(--muted); }
.composer-send {
  flex-shrink: 0;
  display: grid;
  place-items: center;
  width: 34px;
  height: 34px;
  border: none;
  border-radius: 999px;
  background: var(--accent);
  color: #fff;
  cursor: pointer;
  transition: filter 130ms ease, opacity 130ms ease;
}
.composer-send:hover { filter: brightness(1.08); }
.composer-send:active { filter: brightness(0.95); }
.composer-input:placeholder-shown + .composer-send { opacity: 0.4; pointer-events: none; }
.composer.is-busy .composer-send { opacity: 0.5; pointer-events: none; }

/* --- motion ------------------------------------------------------------- */

@keyframes cortexIn {
  from { opacity: 0; transform: translateY(8px); }
  to { opacity: 1; transform: translateY(0); }
}

/* --- responsive --------------------------------------------------------- */

@media (max-width: 720px) {
  .cortex-app { height: min(82vh, 720px); border-radius: 14px; }
  .msg-area { padding: 18px 16px; gap: 18px; }
  .msg.user .msg-bubble { max-width: 88%; }
  .composer-area { padding: 10px 12px 12px; }
}

@media (prefers-reduced-motion: reduce) {
  .cortex-app, .cortex-login { animation: none; }
  .dots i { animation: none; opacity: 0.55; }
}
