:root {
  --purple-deep: #4e2a84;
  --purple-mid: #6b3fa8;
  --purple-light: #9b7fd4;
  --purple-glow: rgba(78, 42, 132, 0.18);
  --cream: #faf7f2;
  --cream-dark: #f0ebe3;
  --ink: #1a1225;
  --ink-muted: #5c5468;
  --ink-faint: #9a929f;
  --white: #ffffff;
  --assistant-bg: #f3eef8;
  --user-bg: var(--purple-deep);
  --radius-lg: 20px;
  --radius-md: 12px;
  --radius-sm: 8px;
  --shadow-soft: 0 4px 24px rgba(78, 42, 132, 0.08);
  --shadow-panel: -8px 0 40px rgba(26, 18, 37, 0.12);
  --font-display: "Fraunces", Georgia, serif;
  --font-body: "Source Serif 4", Georgia, serif;
  --panel-width: 40vw;
  --header-h: 72px; /* kept in sync with site-nav.css */
  --composer-h: 120px;
  --ease-out: cubic-bezier(0.22, 1, 0.36, 1);
}

*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

html, body {
  height: 100%;
  font-family: var(--font-body);
  font-size: 16px;
  line-height: 1.6;
  color: var(--ink);
  background: var(--cream);
  overflow: hidden;
}

body {
  display: flex;
  flex-direction: column;
}

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

/* Header / nav pills — see site-nav.css */

.site-header {
  animation: slideDown 0.6s var(--ease-out) both;
}

.session-dot {
  animation: pulse 2s ease infinite;
}

/* ── Workspace ── */
.workspace {
  flex: 1;
  display: flex;
  overflow: hidden;
  position: relative;
}

.chat-stage {
  flex: 1;
  display: flex;
  justify-content: center;
  min-width: 0;
  transition: flex 0.5s var(--ease-out);
}

.workspace.split .chat-stage {
  flex: 1;
  min-width: 0;
  justify-content: flex-start;
}

.chat-column {
  width: 100%;
  max-width: 760px;
  display: flex;
  flex-direction: column;
  min-width: 0;
  height: 100%;
  transition: max-width 0.5s var(--ease-out);
}

.workspace.split .chat-column {
  max-width: none;
  flex: 1;
}

.chat-scroll {
  flex: 1;
  overflow-y: auto;
  scroll-behavior: smooth;
  padding: 2rem 1.5rem 1rem;
}

/* ── Welcome ── */
.welcome {
  max-width: 560px;
  margin: 4vh auto 0;
  text-align: center;
  animation: fadeUp 0.8s var(--ease-out) 0.15s both;
}

.welcome-orb {
  width: 64px;
  height: 64px;
  margin: 0 auto 1.5rem;
  border-radius: 50%;
  background: radial-gradient(circle at 35% 35%, var(--purple-light), var(--purple-deep));
  box-shadow: 0 8px 32px var(--purple-glow);
  animation: orbFloat 4s ease-in-out infinite;
}

.welcome-title {
  font-family: var(--font-display);
  font-size: 1.75rem;
  font-weight: 700;
  color: var(--ink);
  margin-bottom: 0.75rem;
  letter-spacing: -0.03em;
}

.welcome-body {
  color: var(--ink-muted);
  font-size: 1.05rem;
  margin-bottom: 1.75rem;
}

.welcome-body code {
  font-size: 0.9em;
  padding: 0.15em 0.4em;
  border-radius: 4px;
  background: var(--cream-dark);
  color: var(--purple-deep);
}

.welcome-chips {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
  justify-content: center;
}

.chip {
  font-family: var(--font-body);
  font-size: 0.85rem;
  padding: 0.5rem 1rem;
  border-radius: 999px;
  border: 1px solid rgba(78, 42, 132, 0.2);
  background: var(--white);
  color: var(--purple-deep);
  cursor: pointer;
  transition: all 0.2s var(--ease-out);
}

.chip:hover {
  background: var(--purple-deep);
  color: var(--white);
  border-color: var(--purple-deep);
  transform: translateY(-1px);
  box-shadow: 0 4px 16px var(--purple-glow);
}

/* ── Messages ── */
.messages {
  max-width: 720px;
  margin: 0 auto;
  display: flex;
  flex-direction: column;
  gap: 1.25rem;
  padding-bottom: 1rem;
}

.msg {
  display: flex;
  flex-direction: column;
  gap: 0.35rem;
  animation: fadeUp 0.4s var(--ease-out) both;
}

.msg-label {
  font-size: 0.7rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--ink-faint);
  padding-left: 0.25rem;
}

.msg-bubble {
  padding: 1rem 1.25rem;
  border-radius: var(--radius-md);
  font-size: 0.975rem;
  line-height: 1.65;
  white-space: pre-wrap;
  word-break: break-word;
}

.msg.user {
  align-items: flex-end;
}

.msg.user .msg-label-row {
  align-self: flex-end;
  max-width: 92%;
}

.msg.user .msg-bubble {
  background: var(--user-bg);
  color: var(--white);
  border-bottom-right-radius: 4px;
  align-self: flex-end;
  max-width: 92%;
  width: fit-content;
}

.msg.user .msg-body {
  align-self: flex-end;
  max-width: 85%;
}

.msg.assistant .msg-body {
  max-width: 92%;
  background: var(--assistant-bg);
  color: var(--ink);
  border-radius: var(--radius-md);
  border-bottom-left-radius: 4px;
  overflow: hidden;
}

.msg.assistant .msg-body .msg-bubble {
  background: transparent;
  max-width: none;
  border-radius: 0;
}

.msg.assistant .msg-bubble {
  background: var(--assistant-bg);
  color: var(--ink);
  border-bottom-left-radius: 4px;
  max-width: 92%;
}

.msg.user .msg-body .msg-bubble {
  background: var(--user-bg);
  color: var(--white);
  border-radius: var(--radius-md);
  border-bottom-right-radius: 4px;
}

.msg-label-row {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  flex-wrap: wrap;
  position: relative;
}

.safety-badge-wrap {
  position: relative;
  display: inline-flex;
  align-items: center;
  flex-shrink: 0;
}

.safety-badge {
  display: inline-flex;
  align-items: center;
  font-family: inherit;
  font-size: 0.68rem;
  font-weight: 600;
  letter-spacing: 0.02em;
  text-transform: uppercase;
  padding: 0.15rem 0.45rem;
  border-radius: 999px;
  border: 1px solid transparent;
  cursor: pointer;
  white-space: nowrap;
}

.safety-badge-popover {
  position: fixed;
  z-index: 10000;
  max-width: min(18rem, calc(100vw - 24px));
  padding: 0.65rem 0.9rem;
  border-radius: var(--radius-md);
  border: 1px solid transparent;
  font-size: 0.8125rem;
  font-weight: 500;
  letter-spacing: normal;
  line-height: 1.45;
  text-transform: none;
  white-space: normal;
  box-shadow:
    0 12px 32px rgba(45, 24, 80, 0.16),
    0 2px 8px rgba(45, 24, 80, 0.08);
  opacity: 0;
  visibility: hidden;
  pointer-events: none;
  transform: translateY(-50%) translateY(6px);
  transition:
    opacity 0.22s var(--ease-out),
    transform 0.22s var(--ease-out),
    visibility 0.22s;
}

.safety-badge-popover.is-visible {
  opacity: 1;
  visibility: visible;
  pointer-events: auto;
  transform: translateY(-50%) translateY(0);
}

.safety-popover-medium {
  color: #5c4500;
  background: #fff9e8;
  border-color: #e8cf7a;
}

.safety-popover-low {
  color: #7a1818;
  background: #fff5f5;
  border-color: #e8aaaa;
}

.safety-popover-high {
  color: #1f6b3a;
  background: #f4fbf6;
  border-color: #b8dfc4;
}

@media (prefers-reduced-motion: reduce) {
  .safety-badge-popover {
    transition: opacity 0.12s ease, visibility 0.12s ease;
    transform: translateY(-50%);
  }

  .safety-badge-popover.is-visible {
    transform: translateY(-50%);
  }
}

.safety-badge.safety-high {
  color: #1f6b3a;
  background: #e8f5ec;
  border-color: #b8dfc4;
}

.safety-badge.safety-medium {
  color: #7a5a00;
  background: #fff6dd;
  border-color: #f0dfa0;
}

.safety-badge.safety-low {
  color: #8b1e1e;
  background: #fdeeee;
  border-color: #f0bcbc;
}

@keyframes safetyBadgePulseMedium {
  0%,
  100% {
    opacity: 1;
    box-shadow: 0 0 0 0 rgba(196, 146, 0, 0);
    filter: brightness(1);
  }
  50% {
    opacity: 1;
    box-shadow: 0 0 0 4px rgba(196, 146, 0, 0.28);
    filter: brightness(1.12);
  }
}

@keyframes safetyBadgePulseLow {
  0%,
  100% {
    opacity: 1;
    box-shadow: 0 0 0 0 rgba(180, 40, 40, 0);
    filter: brightness(1);
  }
  50% {
    opacity: 1;
    box-shadow: 0 0 0 4px rgba(180, 40, 40, 0.32);
    filter: brightness(1.15);
  }
}

.safety-badge-attention {
  cursor: help;
}

.safety-badge-attention.safety-medium {
  animation: safetyBadgePulseMedium 1.8s ease-in-out infinite;
}

.safety-badge-attention.safety-low {
  animation: safetyBadgePulseLow 1.8s ease-in-out infinite;
}

@media (prefers-reduced-motion: reduce) {
  .safety-badge-attention.safety-medium,
  .safety-badge-attention.safety-low {
    animation: none;
    box-shadow: 0 0 0 2px currentColor;
  }
}

.msg.file-note {
  font-size: 0.8rem;
  color: var(--purple-mid);
  font-style: italic;
  padding-left: 0.25rem;
}

.msg.plan-notice {
  max-width: 720px;
  margin: 0.5rem auto 0.75rem;
  padding: 0.75rem 1rem;
  border-radius: var(--radius-md);
  background: #f4f0fb;
  border: 1px solid rgba(78, 42, 132, 0.18);
  color: var(--ink);
}

.msg.plan-notice .plan-notice-text {
  margin: 0;
  font-size: 0.9rem;
  line-height: 1.5;
}

.msg.thinking .msg-bubble {
  display: flex;
  gap: 5px;
  align-items: center;
  padding: 0.875rem 1.25rem;
}

.dot-pulse span {
  display: inline-block;
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: var(--purple-light);
  animation: dotBounce 1.2s ease infinite;
}

.dot-pulse span:nth-child(2) { animation-delay: 0.15s; }
.dot-pulse span:nth-child(3) { animation-delay: 0.3s; }

/* ── Composer ── */
.composer {
  padding: 0.75rem 1.5rem 1.25rem;
  background: linear-gradient(to top, var(--cream) 60%, transparent);
  position: relative;
  z-index: 5;
}

.composer-inner {
  max-width: 720px;
  margin: 0 auto;
  display: flex;
  align-items: flex-end;
  gap: 0.5rem;
  background: var(--white);
  border: 1.5px solid rgba(78, 42, 132, 0.15);
  border-radius: var(--radius-lg);
  padding: 0.5rem 0.5rem 0.5rem 0.75rem;
  box-shadow: var(--shadow-soft);
  transition: border-color 0.2s, box-shadow 0.2s;
}

.composer-inner:focus-within {
  border-color: var(--purple-mid);
  box-shadow: 0 4px 32px var(--purple-glow);
}

.upload-btn {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 36px;
  height: 36px;
  border-radius: var(--radius-sm);
  color: var(--ink-muted);
  cursor: pointer;
  flex-shrink: 0;
  transition: all 0.2s;
}

.upload-btn:hover {
  background: var(--cream-dark);
  color: var(--purple-deep);
}

#messageInput {
  flex: 1;
  border: none;
  outline: none;
  resize: none;
  font-family: var(--font-body);
  font-size: 0.975rem;
  line-height: 1.5;
  max-height: 160px;
  padding: 0.4rem 0;
  background: transparent;
  color: var(--ink);
}

.send-btn {
  width: 36px;
  height: 36px;
  border-radius: var(--radius-sm);
  border: none;
  background: var(--purple-deep);
  color: var(--white);
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  transition: all 0.2s var(--ease-out);
}

.send-btn:disabled {
  opacity: 0.35;
  cursor: not-allowed;
}

.send-btn:not(:disabled):hover {
  background: var(--purple-mid);
  transform: scale(1.05);
}

.composer-meta {
  max-width: 720px;
  margin: 0.4rem auto 0;
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-size: 0.72rem;
  color: var(--ink-faint);
}

.file-tag {
  color: var(--purple-deep);
  font-weight: 600;
  display: flex;
  align-items: center;
  gap: 0.35rem;
}

.file-tag::before {
  content: "📄";
  font-size: 0.85rem;
}

/* ── Side Panel (constraints + source document) ── */
.side-panel {
  width: 0;
  min-width: 0;
  height: 100%;
  align-self: stretch;
  overflow: hidden;
  background: var(--white);
  border-left: 1px solid rgba(78, 42, 132, 0.12);
  box-shadow: var(--shadow-panel);
  display: flex;
  flex-direction: column;
  flex-shrink: 0;
  min-height: 0;
  transition: width 0.5s var(--ease-out), min-width 0.5s var(--ease-out);
  z-index: 8;
}

.side-panel.open {
  width: var(--panel-width);
  min-width: 280px;
  max-width: 40vw;
  flex: 0 0 var(--panel-width);
}

.panel-tabs {
  display: flex;
  gap: 0.35rem;
}

.panel-tab {
  font-family: var(--font-body);
  font-size: 0.78rem;
  font-weight: 600;
  padding: 0.35rem 0.75rem;
  border-radius: 999px;
  border: 1px solid rgba(78, 42, 132, 0.15);
  background: transparent;
  color: var(--ink-muted);
  cursor: pointer;
  transition: all 0.2s;
}

.panel-tab.active {
  background: var(--purple-deep);
  color: var(--white);
  border-color: var(--purple-deep);
}

.panel-tab:disabled {
  opacity: 0.35;
  cursor: not-allowed;
}

.panel-view {
  position: absolute;
  inset: 0;
  overflow: hidden;
  display: flex;
  flex-direction: column;
  min-height: 0;
  padding: 1rem 1rem 2rem;
}

.panel-view[hidden] {
  display: none !important;
}

.source-meta {
  font-size: 0.78rem;
  color: var(--ink-muted);
  margin-bottom: 0.75rem;
  padding-bottom: 0.75rem;
  border-bottom: 1px solid rgba(78, 42, 132, 0.08);
}

.source-meta strong {
  display: block;
  font-family: var(--font-display);
  color: var(--purple-deep);
  font-size: 0.95rem;
  margin-bottom: 0.25rem;
}

.source-doc-wrap {
  flex: 1;
  overflow: auto;
  min-width: 0;
  font-size: 0.82rem;
  line-height: 1.55;
}

.procedure-doc h1 {
  font-family: var(--font-display);
  font-size: 1rem;
  color: var(--purple-deep);
  margin-bottom: 0.75rem;
}

.procedure-doc h2 {
  font-family: var(--font-display);
  font-size: 0.88rem;
  color: var(--ink);
  margin: 1rem 0 0.5rem;
}

.procedure-doc .proc-block {
  margin-bottom: 0.65rem;
  color: var(--ink);
}

.procedure-doc .proc-list {
  margin: 0.35rem 0 0.75rem 1.25rem;
  padding-left: 1rem;
}

.procedure-doc .proc-list li {
  margin-bottom: 0.45rem;
}

.procedure-doc .proc-list ol,
.procedure-doc .proc-list ul {
  margin-top: 0.35rem;
}

.procedure-doc .source-highlight,
.source-highlight {
  background: rgba(78, 42, 132, 0.18);
  box-shadow: inset 3px 0 0 var(--purple-deep);
  padding: 0.35rem 0.5rem;
  border-radius: 4px;
}

.cite-link {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: 1.25rem;
  height: 1.25rem;
  padding: 0 0.3rem;
  margin: 0 0.1rem;
  border: none;
  border-radius: 4px;
  background: rgba(78, 42, 132, 0.15);
  color: var(--purple-deep);
  font-size: 0.72rem;
  font-weight: 700;
  cursor: pointer;
  vertical-align: super;
}

.cite-link:hover {
  background: var(--purple-deep);
  color: var(--white);
}

.constraint-kb-link {
  display: inline;
  padding: 0;
  border: none;
  background: none;
  color: var(--purple-deep);
  font: inherit;
  font-weight: 600;
  text-align: left;
  cursor: pointer;
  text-decoration: underline;
  text-decoration-color: rgba(78, 42, 132, 0.35);
  text-underline-offset: 2px;
}

.constraint-kb-link:hover {
  color: var(--purple-mid);
  text-decoration-color: var(--purple-mid);
}

.constraint-doc dl {
  margin: 0.35rem 0 0;
}

.constraint-doc dt {
  font-weight: 600;
  color: var(--ink-muted);
  font-size: 0.75rem;
  margin-top: 0.35rem;
}

.constraint-doc dd {
  margin: 0.1rem 0 0;
}

.panel-close {
  background: none;
  border: none;
  font-size: 1.5rem;
  color: var(--ink-muted);
  cursor: pointer;
  line-height: 1;
  padding: 0.25rem;
  border-radius: 4px;
  transition: color 0.2s;
}

.panel-close:hover { color: var(--purple-deep); }

.panel-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 1rem 1rem 1rem 1.25rem;
  border-bottom: 1px solid rgba(78, 42, 132, 0.08);
  gap: 0.5rem;
}

.panel-body {
  flex: 1 1 0;
  overflow: hidden;
  position: relative;
  min-width: 0;
  min-height: 0;
}

.table-wrap {
  flex: 1;
  overflow: auto;
  min-width: 0;
}

.constraints-table {
  width: max-content;
  min-width: 100%;
  table-layout: auto;
  border-collapse: collapse;
  font-size: 0.68rem;
  line-height: 1.35;
}

.constraints-table th {
  position: sticky;
  top: 0;
  background: var(--purple-deep);
  color: var(--white);
  font-family: var(--font-display);
  font-weight: 600;
  font-size: 0.65rem;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  padding: 0.55rem 0.5rem;
  text-align: left;
  white-space: nowrap;
  word-break: keep-all;
  overflow-wrap: normal;
  vertical-align: middle;
}

.constraints-table td {
  padding: 0.4rem 0.5rem;
  border-bottom: 1px solid rgba(78, 42, 132, 0.06);
  vertical-align: top;
  color: var(--ink);
  white-space: normal;
  word-break: break-word;
  overflow-wrap: anywhere;
  max-width: 14rem;
}

.constraints-table tr:nth-child(even) td {
  background: rgba(78, 42, 132, 0.03);
}

.constraints-table tr:hover td {
  background: rgba(78, 42, 132, 0.07);
}

.constraints-table td.status-within { color: #2d7a4f; font-weight: 600; }
.constraints-table td.status-out { color: #b83232; font-weight: 600; }
.constraints-table td.status-range { color: #b86e00; font-weight: 600; }

/* ── Toast ── */
.toast {
  position: fixed;
  bottom: 2rem;
  left: 50%;
  transform: translateX(-50%) translateY(1rem);
  background: var(--ink);
  color: var(--white);
  padding: 0.75rem 1.5rem;
  border-radius: var(--radius-md);
  font-size: 0.875rem;
  z-index: 10000;
  transition: transform 0.35s var(--ease-out), opacity 0.35s var(--ease-out), visibility 0.35s;
  pointer-events: none;
  opacity: 0;
  visibility: hidden;
}

.toast.show {
  transform: translateX(-50%) translateY(0);
  opacity: 1;
  visibility: visible;
}

/* ── Animations ── */
@keyframes slideDown {
  from { opacity: 0; transform: translateY(-12px); }
  to { opacity: 1; transform: translateY(0); }
}

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

@keyframes orbFloat {
  0%, 100% { transform: translateY(0); }
  50% { transform: translateY(-6px); }
}

@keyframes pulse {
  0%, 100% { opacity: 1; }
  50% { opacity: 0.4; }
}

@keyframes dotBounce {
  0%, 80%, 100% { transform: translateY(0); opacity: 0.4; }
  40% { transform: translateY(-5px); opacity: 1; }
}

/* ── Responsive ── */
@media (max-width: 900px) {
  :root { --panel-width: 100vw; }

  .workspace.split .chat-stage {
    display: none;
  }

  .side-panel.open {
    position: fixed;
    inset: var(--header-h) 0 0 0;
    width: 100% !important;
    max-width: none;
    min-width: 0;
    flex: none;
    z-index: 20;
  }

  .constraints-table {
    min-width: 100%;
  }

  .constraints-table td {
    max-width: 10rem;
  }
}
