/* csc-ai-portal — frontend styles. Light/dark via prefers-color-scheme. */

:root {
  --navy: #1b3a6b;
  --navy-dark: #142c52;
  --navy-tint: #e8eef7;

  --bg: #f4f6f9;
  --surface: #ffffff;
  --surface-2: #eef1f6;
  --text: #1c2536;
  --muted: #5b6575;
  --border: #d8dde5;
  --accent: #1b3a6b;
  --accent-contrast: #ffffff;
  --focus: #2f6fd0;

  --ok: #2e7d4f;
  --warn: #b26a00;
  --danger: #b3261e;
  --danger-bg: #fdecea;
  --info-bg: #eef1f6;

  --user-bubble: #e8eef7;
  --code-bg: #f0f2f6;

  --radius: 10px;
  --sidebar-w: 280px;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #12161d;
    --surface: #1a2029;
    --surface-2: #222a36;
    --text: #e6eaf1;
    --muted: #9aa5b5;
    --border: #2c3442;
    --accent: #4f7fc4;
    --accent-contrast: #ffffff;
    --focus: #6ea2e8;

    --ok: #4caf7d;
    --warn: #e0a840;
    --danger: #ef7369;
    --danger-bg: #3a2220;
    --info-bg: #222a36;

    --navy-tint: #21304a;
    --user-bubble: #21304a;
    --code-bg: #10151c;
  }
}

* {
  box-sizing: border-box;
}

html,
body {
  height: 100%;
}

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Helvetica,
    Arial, sans-serif, 'Apple Color Emoji', 'Segoe UI Emoji';
  font-size: 15px;
  line-height: 1.5;
  display: flex;
  flex-direction: column;
}

/* --- Header -------------------------------------------------------------- */

.topbar {
  background: var(--navy);
  color: #fff;
  padding: 0.65rem 1.25rem;
  display: flex;
  align-items: baseline;
  gap: 1rem;
  flex-shrink: 0;
}

.brand {
  display: flex;
  align-items: center;
  gap: 0.6rem;
  font-size: 1.15rem;
  font-weight: 600;
  letter-spacing: 0.01em;
}

.brand-mark {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 1.9rem;
  height: 1.9rem;
  border-radius: 6px;
  background: rgba(255, 255, 255, 0.14);
  border: 1px solid rgba(255, 255, 255, 0.35);
  font-size: 0.85rem;
  font-weight: 700;
}

.topbar-sub {
  font-size: 0.8rem;
  color: rgba(255, 255, 255, 0.75);
}

/* --- Layout -------------------------------------------------------------- */

.layout {
  flex: 1;
  display: flex;
  min-height: 0;
}

.sidebar {
  width: var(--sidebar-w);
  flex-shrink: 0;
  background: var(--surface);
  border-right: 1px solid var(--border);
  padding: 1rem;
  display: flex;
  flex-direction: column;
  gap: 1.1rem;
  overflow-y: auto;
}

.sidebar-spacer {
  flex: 1;
}

.main {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-width: 0;
  min-height: 0;
}

/* --- Sidebar controls ---------------------------------------------------- */

.field {
  display: flex;
  flex-direction: column;
  gap: 0.35rem;
  border: 0;
  margin: 0;
  padding: 0;
}

.field label,
.field legend {
  font-size: 0.8rem;
  font-weight: 600;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: 0.05em;
  padding: 0;
}

select {
  width: 100%;
  padding: 0.5rem 0.6rem;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--surface);
  color: var(--text);
  font: inherit;
}

.mcp-field {
  border: 0;
  padding: 0;
  margin: 0;
}

.mcp-list {
  display: flex;
  flex-direction: column;
  gap: 0.3rem;
  margin-top: 0.35rem;
}

.mcp-item {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  padding: 0.3rem 0.4rem;
  border-radius: 6px;
  cursor: pointer;
}

.mcp-item:hover {
  background: var(--surface-2);
}

.mcp-item input {
  accent-color: var(--navy);
}

/* --- Usage meter ---------------------------------------------------------- */

.usage-head {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  font-size: 0.8rem;
  font-weight: 600;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: 0.05em;
  margin-bottom: 0.4rem;
}

.usage-text {
  color: var(--text);
  text-transform: none;
  letter-spacing: normal;
  font-variant-numeric: tabular-nums;
}

.meter {
  height: 9px;
  border-radius: 6px;
  background: var(--surface-2);
  border: 1px solid var(--border);
  overflow: hidden;
}

.meter-fill {
  height: 100%;
  width: 0;
  background: var(--ok);
  border-radius: 6px;
  transition: width 0.4s ease, background-color 0.3s ease;
}

.meter.warn .meter-fill {
  background: var(--warn);
}

.meter.over .meter-fill {
  background: var(--danger);
}

.usage-note {
  margin-top: 0.4rem;
  color: var(--warn);
}

.meter.over + .usage-note,
.usage-note.over {
  color: var(--danger);
}

/* --- User card ------------------------------------------------------------ */

.user-card {
  border-top: 1px solid var(--border);
  padding-top: 0.85rem;
  display: flex;
  flex-direction: column;
  gap: 0.35rem;
}

.user-line {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  flex-wrap: wrap;
}

.user-name {
  font-weight: 600;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  max-width: 100%;
}

.badge {
  display: inline-block;
  font-size: 0.7rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  padding: 0.12rem 0.5rem;
  border-radius: 999px;
  background: var(--navy-tint);
  color: var(--accent);
  border: 1px solid var(--border);
  text-decoration: none;
}

.badge-admin {
  background: var(--navy);
  color: #fff;
  border-color: var(--navy);
}

.logout {
  font-size: 0.85rem;
  color: var(--muted);
}

.logout:hover {
  color: var(--text);
}

a {
  color: var(--focus);
}

/* --- Buttons -------------------------------------------------------------- */

.btn {
  font: inherit;
  font-weight: 600;
  padding: 0.5rem 1rem;
  border-radius: 8px;
  border: 1px solid transparent;
  cursor: pointer;
}

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

.btn-block {
  width: 100%;
}

.btn-primary {
  background: var(--navy);
  color: var(--accent-contrast);
}

.btn-primary:not(:disabled):hover {
  background: var(--navy-dark);
}

.btn-secondary {
  background: var(--surface);
  color: var(--text);
  border-color: var(--border);
}

.btn-secondary:not(:disabled):hover {
  background: var(--surface-2);
}

/* --- Notices -------------------------------------------------------------- */

.notice {
  display: flex;
  align-items: flex-start;
  gap: 0.5rem;
  margin: 0.75rem 1.25rem 0;
}
/* `display: flex` above has equal specificity to the UA [hidden] rule and
   would otherwise keep the empty bar visible — restore hidden behavior. */
.notice[hidden] {
  display: none;
}
.notice:not([hidden]) {
  margin: 0.75rem 1.25rem 0;
  padding: 0.7rem 1rem;
  border-radius: var(--radius);
  background: var(--danger-bg);
  color: var(--danger);
  border: 1px solid var(--danger);
  font-weight: 600;
  /* long URLs / tokens must wrap, never widen the layout */
  overflow-wrap: anywhere;
  word-break: break-word;
}
.notice > span {
  flex: 1;
  min-width: 0;
}
.notice a {
  color: inherit;
  font-weight: 700;
  text-decoration: underline;
  overflow-wrap: anywhere;
}
.notice-close {
  flex: none;
  background: none;
  border: none;
  color: inherit;
  font-size: 1.1rem;
  line-height: 1;
  cursor: pointer;
  padding: 0 0.2rem;
  opacity: 0.7;
}
.notice-close:hover { opacity: 1; }

/* Informational (non-error) banner: neutral, not alarming. */
.notice.notice-info {
  background: var(--code-bg);
  color: var(--ink, inherit);
  border-color: var(--accent);
  font-weight: 500;
}
.notice.notice-info a { color: var(--accent); }

/* --- Thread --------------------------------------------------------------- */

.thread {
  position: relative;
  flex: 1;
  overflow-y: auto;
  padding: 1.25rem;
  display: flex;
  flex-direction: column;
  gap: 1rem;
  scroll-behavior: smooth;
}

.welcome {
  max-width: 44rem;
  margin: 10vh auto 0;
  text-align: center;
  color: var(--muted);
}

.welcome h2 {
  color: var(--text);
  margin-bottom: 0.5rem;
}

kbd {
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  font-size: 0.85em;
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-bottom-width: 2px;
  border-radius: 4px;
  padding: 0 0.35em;
}

.msg {
  max-width: 48rem;
  width: fit-content;
  min-width: 12rem;
  padding: 0.7rem 1rem 0.8rem;
  border-radius: var(--radius);
  border: 1px solid var(--border);
}

.msg.user {
  align-self: flex-end;
  background: var(--user-bubble);
  border-color: transparent;
}

.msg.assistant {
  align-self: flex-start;
  background: var(--surface);
}

.msg-who {
  font-size: 0.72rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--muted);
  margin-bottom: 0.25rem;
}

.msg.user .msg-content {
  white-space: pre-wrap;
  overflow-wrap: anywhere;
}

.msg-flow {
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
}

.msg-content {
  overflow-wrap: anywhere;
}

.msg-content > :first-child {
  margin-top: 0;
}

.msg-content > :last-child {
  margin-bottom: 0;
}

.msg-content p {
  margin: 0.4rem 0;
}

.msg-content ul,
.msg-content ol {
  margin: 0.4rem 0;
  padding-left: 1.4rem;
}

.msg-content h3,
.msg-content h4,
.msg-content h5,
.msg-content h6 {
  margin: 0.8rem 0 0.3rem;
  line-height: 1.3;
}

.msg-content pre {
  background: var(--code-bg);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 0.7rem 0.9rem;
  overflow-x: auto;
  font-size: 0.85rem;
  margin: 0.5rem 0;
}

.msg-content code,
.tool-chip pre {
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, 'Liberation Mono',
    monospace;
}

.msg-content :not(pre) > code {
  background: var(--code-bg);
  border: 1px solid var(--border);
  border-radius: 4px;
  padding: 0.05em 0.35em;
  font-size: 0.88em;
}

.msg-content blockquote {
  margin: 0.5rem 0;
  padding: 0.3rem 0.9rem;
  border-left: 3px solid var(--accent);
  background: var(--code-bg);
  border-radius: 0 6px 6px 0;
  color: var(--muted);
}

.msg-content hr {
  border: none;
  border-top: 1px solid var(--border);
  margin: 0.8rem 0;
}

/* Tables scroll inside their own wrapper so the chat pane never scrolls
   sideways (renderer wraps every table in .md-table). */
.msg-content .md-table {
  overflow-x: auto;
  margin: 0.5rem 0;
}

.msg-content table {
  border-collapse: collapse;
  font-size: 0.9rem;
}

.msg-content th,
.msg-content td {
  border: 1px solid var(--border);
  padding: 0.35rem 0.7rem;
  text-align: left;
  vertical-align: top;
}

.msg-content th {
  background: var(--code-bg);
  font-weight: 600;
}

/* Past chats sidebar list */
.chats-field {
  min-height: 0;
  display: flex;
  flex-direction: column;
}

.chat-list {
  overflow-y: auto;
  max-height: 14rem;
  display: flex;
  flex-direction: column;
  gap: 2px;
  margin-top: 0.3rem;
}

.chat-item {
  display: flex;
  align-items: center;
  gap: 0.25rem;
  border-radius: 6px;
}

.chat-item.active {
  background: var(--code-bg);
}

.chat-open {
  flex: 1;
  min-width: 0;
  text-align: left;
  background: none;
  border: none;
  color: inherit;
  font: inherit;
  font-size: 0.85rem;
  padding: 0.35rem 0.5rem;
  border-radius: 6px;
  cursor: pointer;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.chat-open:hover,
.chat-open:focus-visible {
  background: var(--code-bg);
}

.chat-del {
  background: none;
  border: none;
  color: var(--muted);
  font-size: 1rem;
  line-height: 1;
  padding: 0.25rem 0.45rem;
  border-radius: 6px;
  cursor: pointer;
  visibility: hidden;
}

.chat-item:hover .chat-del,
.chat-del:focus-visible {
  visibility: visible;
}

.chat-del:hover {
  color: var(--danger, #b3261e);
  background: var(--code-bg);
}

/* Compare mode */
.msg.compare-row-wrap {
  /* Comparison grids need the full thread width, not the bubble cap. */
  width: 100%;
  max-width: 100%;
}

.compare-panel {
  margin-top: 0.4rem;
  padding: 0.5rem;
  border: 1px solid var(--border);
  border-radius: 8px;
}

.compare-row {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(280px, 1fr));
  gap: 0.6rem;
}

.compare-card {
  display: flex;
  flex-direction: column;
  border: 1px solid var(--border);
  border-radius: 10px;
  overflow: hidden;
  min-width: 0;
}

.compare-card.chosen {
  border-color: var(--accent);
  box-shadow: 0 0 0 1px var(--accent);
}

.compare-card-head {
  padding: 0.4rem 0.75rem;
  font-size: 0.85rem;
  font-weight: 600;
  background: var(--code-bg);
  border-bottom: 1px solid var(--border);
}

.compare-card-body {
  flex: 1;
  padding: 0.6rem 0.75rem;
  min-height: 3rem;
}

.compare-card-foot {
  padding: 0.5rem 0.75rem;
  border-top: 1px solid var(--border);
}

/* Dialogs (share / feedback) */
.portal-dialog {
  border: 1px solid var(--border);
  border-radius: 12px;
  background: var(--surface);
  color: inherit;
  padding: 1.2rem 1.4rem;
  max-width: 26rem;
  width: 90vw;
}

.portal-dialog::backdrop {
  background: rgb(0 0 0 / 45%);
}

.portal-dialog h3 {
  margin: 0 0 0.4rem;
}

.portal-dialog textarea,
.portal-dialog select,
.portal-dialog input[type='email'] {
  width: 100%;
  margin: 0.4rem 0;
  padding: 0.5rem 0.6rem;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--bg);
  color: inherit;
  font: inherit;
}

.dialog-btns {
  display: flex;
  justify-content: flex-end;
  gap: 0.5rem;
  margin-top: 0.6rem;
}

.share-form {
  display: flex;
  gap: 0.4rem;
  align-items: center;
}

.share-form input {
  flex: 1;
}

.share-org-row {
  display: flex;
  gap: 0.4rem;
  margin-top: 0.5rem;
}

.share-current {
  margin: 0.6rem 0;
  display: flex;
  flex-direction: column;
  gap: 0.25rem;
}

.share-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  font-size: 0.9rem;
  padding: 0.2rem 0;
}

.shared-banner {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.75rem;
  padding: 0.6rem 1rem;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--code-bg);
  margin-bottom: 0.75rem;
}

.chat-share {
  font-size: 0.85rem;
}

.user-links {
  display: flex;
  gap: 0.9rem;
  align-items: center;
}

.policy-links {
  margin-top: 0.35rem;
  color: var(--muted);
}
.policy-links a {
  color: var(--muted);
  text-decoration: none;
}
.policy-links a:hover {
  text-decoration: underline;
}

.gate-links {
  padding-left: 1.1rem;
  margin: 0.5rem 0;
}
.gate-links li {
  margin: 0.35rem 0;
}
.gate-aup {
  background: var(--code-bg);
  border-radius: 8px;
  padding: 0.6rem 0.8rem;
  font-size: 0.9rem;
}

/* Drag-and-drop file attach overlay */
.thread.drag-over {
  outline: 2px dashed var(--accent);
  outline-offset: -10px;
  background: color-mix(in srgb, var(--accent) 8%, transparent);
}
.thread.drag-over::after {
  content: 'Drop files to attach';
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 600;
  color: var(--accent);
  pointer-events: none;
}

.linklike {
  background: none;
  border: none;
  padding: 0;
  color: var(--muted);
  font: inherit;
  font-size: 0.85rem;
  text-decoration: underline;
  cursor: pointer;
}

/* Attachments */
.attach-btn {
  align-self: flex-end;
  padding: 0.45rem 0.6rem;
}

.attach-chips {
  display: flex;
  flex-wrap: wrap;
  gap: 0.35rem;
  padding: 0.3rem 0;
}

.attach-chips.in-bubble {
  padding: 0.35rem 1rem 0.6rem;
}

.attach-chip {
  display: inline-flex;
  align-items: center;
  gap: 0.3rem;
  font-size: 0.8rem;
  border: 1px solid var(--border);
  border-radius: 999px;
  padding: 0.15rem 0.6rem;
  background: var(--code-bg);
}

.attach-chip button {
  background: none;
  border: none;
  color: var(--muted);
  cursor: pointer;
  font-size: 0.95rem;
  line-height: 1;
  padding: 0 0.1rem;
}

/* Artifact previews (sandboxed model-generated HTML/SVG) */
.artifact-bar {
  margin: 0.25rem 0 0.5rem;
}

.btn-small {
  font-size: 0.8rem;
  padding: 0.25rem 0.7rem;
}

.artifact-frame {
  display: block;
  width: 100%;
  height: 420px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: #fff;
  margin: 0 0 0.5rem;
  resize: vertical;
  overflow: auto;
}

/* Streaming cursor */
.msg.assistant.streaming .msg-flow::after {
  content: '▍';
  color: var(--muted);
  animation: blink 1s steps(2, start) infinite;
}

@keyframes blink {
  to {
    visibility: hidden;
  }
}

@media (prefers-reduced-motion: reduce) {
  .msg.assistant.streaming .msg-flow::after {
    animation: none;
  }
  .thread {
    scroll-behavior: auto;
  }
}

/* --- Errors / info inside a message --------------------------------------- */

.msg-error {
  background: var(--danger-bg);
  color: var(--danger);
  border: 1px solid var(--danger);
  border-radius: 8px;
  padding: 0.55rem 0.8rem;
  font-weight: 600;
  overflow-wrap: anywhere;
}

.msg-info {
  background: var(--info-bg);
  color: var(--muted);
  border-radius: 8px;
  padding: 0.45rem 0.8rem;
  font-style: italic;
}

/* --- Tool chips ------------------------------------------------------------ */

.tool-chip {
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--surface-2);
  font-size: 0.85rem;
  max-width: 100%;
}

.tool-chip summary {
  cursor: pointer;
  padding: 0.4rem 0.7rem;
  font-weight: 600;
  list-style-position: inside;
  user-select: none;
}

.tool-chip summary:hover {
  color: var(--accent);
}

.tool-chip[open] summary {
  border-bottom: 1px solid var(--border);
}

.tool-chip.tool-error summary {
  color: var(--danger);
}

.tool-chip-body {
  padding: 0.5rem 0.7rem 0.7rem;
}

.tool-chip-label {
  font-size: 0.7rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--muted);
  margin: 0.4rem 0 0.2rem;
}

.tool-chip pre {
  margin: 0;
  padding: 0.5rem 0.6rem;
  background: var(--code-bg);
  border: 1px solid var(--border);
  border-radius: 6px;
  overflow-x: auto;
  white-space: pre-wrap;
  overflow-wrap: anywhere;
  max-height: 16rem;
  overflow-y: auto;
}

.tool-result.pending {
  color: var(--muted);
  font-style: italic;
}

.tool-result.error {
  color: var(--danger);
  border-color: var(--danger);
}

/* --- Composer --------------------------------------------------------------- */

.composer {
  flex-shrink: 0;
  display: flex;
  gap: 0.6rem;
  align-items: flex-end;
  padding: 0.9rem 1.25rem 1.1rem;
  border-top: 1px solid var(--border);
  background: var(--surface);
}

.composer textarea {
  flex: 1;
  resize: none;
  min-height: 2.6rem;
  max-height: 220px;
  padding: 0.6rem 0.8rem;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--bg);
  color: var(--text);
  font: inherit;
  line-height: 1.45;
}

.composer textarea:disabled {
  opacity: 0.6;
}

.composer-btns {
  display: flex;
  gap: 0.5rem;
}

/* --- Utility ----------------------------------------------------------------- */

.muted {
  color: var(--muted);
}

.small {
  font-size: 0.8rem;
}

.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0 0 0 0);
  white-space: nowrap;
  border: 0;
}

/* Focus visibility (accessibility) */
:focus-visible {
  outline: 2px solid var(--focus);
  outline-offset: 2px;
  border-radius: 4px;
}

/* --- Small screens ------------------------------------------------------------ */

@media (max-width: 760px) {
  .layout {
    flex-direction: column;
  }

  .sidebar {
    width: 100%;
    border-right: 0;
    border-bottom: 1px solid var(--border);
    max-height: 45vh;
  }

  .sidebar-spacer {
    display: none;
  }

  .msg {
    max-width: 100%;
  }

  .topbar-sub {
    display: none;
  }
}
