/*
 * Chat page: ported 1:1 from the Node app's web/src/styles.css (React UI), "#root" dropped.
 * Additions for this app are at the end: user menu, inline rename / delete, login page.
 */

:root {
  --bg: #f7f7f8;
  --surface: #ffffff;
  --sidebar: #f0f0f2;
  --text: #1c1c1f;
  --muted: #6b6b76;
  --border: #e2e2e7;
  --accent: #2563eb;
  --accent-text: #ffffff;
  --user-bubble: #2563eb;
  --user-text: #ffffff;
  --code-bg: #f1f1f4;
  --ok: #16a34a;
  --bad: #dc2626;
  --warn-bg: #fef9c3;
  --warn-text: #713f12;
  --error-bg: #fef2f2;
  --error-border: #fecaca;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #17171a;
    --surface: #1f1f23;
    --sidebar: #121214;
    --text: #ececf1;
    --muted: #9a9aa6;
    --border: #2e2e35;
    --accent: #3b82f6;
    --user-bubble: #2d4f9e;
    --code-bg: #2a2a31;
    --warn-bg: #3b3312;
    --warn-text: #fde68a;
    --error-bg: #3a1d1d;
    --error-border: #7f2a2a;
    color-scheme: dark;
  }
}

* {
  box-sizing: border-box;
}

html,
body {
  height: 100%;
  margin: 0;
}

body {
  background: var(--bg);
  color: var(--text);
  font:
    15px/1.55 system-ui,
    -apple-system,
    'Segoe UI',
    Roboto,
    sans-serif;
}

button {
  font: inherit;
  color: inherit;
  cursor: pointer;
}

.app {
  display: flex;
  height: 100%;
}

/* ---- Sidebar ---- */
.sidebar {
  width: 270px;
  flex-shrink: 0;
  display: flex;
  flex-direction: column;
  gap: 8px;
  padding: 12px;
  background: var(--sidebar);
  border-right: 1px solid var(--border);
}

.new-chat {
  padding: 10px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--surface);
  text-align: left;
  font-weight: 600;
}

.new-chat:hover {
  border-color: var(--accent);
}

.conversation-list {
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.conversation {
  display: flex;
  align-items: center;
  border-radius: 8px;
}

.conversation:hover,
.conversation.active {
  background: var(--surface);
}

.conversation.active {
  box-shadow: inset 3px 0 0 var(--accent);
}

.conversation-title {
  flex: 1;
  min-width: 0;
  padding: 8px 10px;
  border: 0;
  background: none;
  text-align: left;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.conversation-actions {
  display: none;
  padding-right: 4px;
}

.conversation:hover .conversation-actions,
.conversation.active .conversation-actions {
  display: flex;
}

.conversation-actions button {
  border: 0;
  background: none;
  padding: 4px 6px;
  border-radius: 6px;
  opacity: 0.7;
}

.conversation-actions button:hover {
  opacity: 1;
  background: var(--border);
}

.empty {
  color: var(--muted);
  padding: 8px 10px;
  margin: 0;
}

/* ---- Main ---- */
.main {
  flex: 1;
  min-width: 0;
  display: flex;
  flex-direction: column;
}

.header {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 10px 16px;
  border-bottom: 1px solid var(--border);
  background: var(--surface);
}

.header h1 {
  flex: 1;
  min-width: 0;
  margin: 0;
  font-size: 16px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.menu {
  display: none;
  border: 0;
  background: none;
  font-size: 20px;
}

.status-bar {
  display: flex;
  align-items: center;
  gap: 12px;
  font-size: 13px;
  color: var(--muted);
  flex-wrap: wrap;
  justify-content: flex-end;
}

.status-item {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  white-space: nowrap;
}

.status-item.model {
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
}

.dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--muted);
}

.status-item.ok .dot {
  background: var(--ok);
}

.status-item.bad {
  color: var(--bad);
}

.status-item.bad .dot {
  background: var(--bad);
}

.refresh {
  border: 0;
  background: none;
  padding: 2px 6px;
  border-radius: 6px;
}

.refresh:hover {
  background: var(--border);
}

.banner {
  display: flex;
  justify-content: space-between;
  gap: 12px;
  padding: 8px 16px;
  background: var(--error-bg);
  border-bottom: 1px solid var(--error-border);
}

.banner button {
  border: 0;
  background: none;
  font-size: 18px;
  line-height: 1;
}

/* ---- Chat ---- */
.chat {
  flex: 1;
  overflow-y: auto;
}

.chat-log {
  max-width: 860px;
  margin: 0 auto;
  padding: 24px 16px;
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.chat-empty {
  max-width: 640px;
  margin: 12vh auto 0;
  padding: 0 16px;
  text-align: center;
  color: var(--muted);
}

.chat-empty h2 {
  color: var(--text);
}

.suggestions {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  justify-content: center;
  margin-top: 16px;
}

.suggestions button {
  padding: 8px 12px;
  border: 1px solid var(--border);
  border-radius: 999px;
  background: var(--surface);
}

.suggestions button:hover {
  border-color: var(--accent);
}

.message {
  display: flex;
  flex-direction: column;
}

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

.bubble {
  max-width: 100%;
  padding: 10px 14px;
  border-radius: 12px;
  overflow-wrap: anywhere;
}

.message.user .bubble {
  max-width: 80%;
  background: var(--user-bubble);
  color: var(--user-text);
  white-space: pre-wrap;
}

.message.assistant .bubble {
  background: var(--surface);
  border: 1px solid var(--border);
}

.message.assistant.error .bubble {
  background: var(--error-bg);
  border-color: var(--error-border);
}

.message-meta {
  font-size: 12px;
  color: var(--muted);
  margin: 4px 4px 0;
}

.typing {
  color: var(--muted);
  font-style: italic;
}

.cursor {
  display: inline-block;
  width: 7px;
  height: 1em;
  margin-left: 2px;
  vertical-align: text-bottom;
  background: var(--muted);
  animation: blink 1s steps(1) infinite;
}

@keyframes blink {
  50% {
    opacity: 0;
  }
}

.notice {
  align-self: center;
  max-width: 90%;
  padding: 6px 12px;
  border-radius: 8px;
  background: var(--warn-bg);
  color: var(--warn-text);
  font-size: 13px;
}

/* Markdown inside assistant messages */
.markdown > :first-child {
  margin-top: 0;
}

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

.markdown pre,
.tool-body pre {
  background: var(--code-bg);
  padding: 10px 12px;
  border-radius: 8px;
  overflow-x: auto;
  font-size: 13px;
}

.markdown code {
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: 0.9em;
}

.markdown :not(pre) > code {
  background: var(--code-bg);
  padding: 1px 5px;
  border-radius: 4px;
}

.markdown table {
  border-collapse: collapse;
  display: block;
  overflow-x: auto;
  font-size: 14px;
}

.markdown th,
.markdown td {
  border: 1px solid var(--border);
  padding: 6px 10px;
  text-align: left;
}

.markdown th {
  background: var(--code-bg);
}

.markdown a {
  color: var(--accent);
}

/* Tool steps */
.tool-step {
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--surface);
  font-size: 14px;
}

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

.tool-step summary {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 12px;
  cursor: pointer;
  list-style: none;
}

.tool-step summary::-webkit-details-marker {
  display: none;
}

.tool-step summary::after {
  content: '▸';
  margin-left: auto;
  color: var(--muted);
}

.tool-step[open] summary::after {
  content: '▾';
}

.tool-status {
  color: var(--muted);
  font-size: 13px;
}

.tool-step.running .tool-status {
  font-style: italic;
}

.tool-body {
  padding: 0 12px 10px;
}

.tool-body pre {
  margin: 0;
  max-height: 320px;
  white-space: pre-wrap;
  overflow-wrap: anywhere;
}

.tool-label {
  margin: 8px 0 4px;
  font-size: 12px;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: 0.04em;
}

/* ---- Composer ---- */
.composer {
  display: flex;
  gap: 8px;
  align-items: flex-end;
  width: 100%;
  max-width: 860px;
  margin: 0 auto;
  padding: 12px 16px 16px;
}

.composer textarea {
  flex: 1;
  resize: none;
  padding: 10px 12px;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--surface);
  color: var(--text);
  font: inherit;
}

.composer textarea:focus {
  outline: 2px solid var(--accent);
  outline-offset: -1px;
}

.composer button {
  padding: 10px 18px;
  border: 0;
  border-radius: 10px;
  background: var(--accent);
  color: var(--accent-text);
  font-weight: 600;
}

.composer button:disabled {
  opacity: 0.5;
  cursor: default;
}

.composer button.stop {
  background: var(--bad);
}

/* ---- Small screens: sidebar becomes a drawer ---- */
.backdrop {
  display: none;
}

@media (max-width: 760px) {
  .sidebar {
    position: fixed;
    inset: 0 auto 0 0;
    z-index: 20;
    transform: translateX(-100%);
    transition: transform 0.2s;
  }

  .sidebar.open {
    transform: none;
  }

  .backdrop {
    display: block;
    position: fixed;
    inset: 0;
    z-index: 10;
    background: rgb(0 0 0 / 0.35);
  }

  .menu {
    display: block;
  }

  .header {
    flex-wrap: wrap;
  }

  .status-bar {
    width: 100%;
    justify-content: flex-start;
  }

  .conversation-actions {
    display: flex;
  }
}


[hidden] {
  display: none !important;
}

/* ---- User menu (header) ---- */
.user-menu {
  position: relative;
  font-size: 14px;
}

.user-menu summary {
  list-style: none;
  cursor: pointer;
  padding: 4px 10px;
  border: 1px solid var(--border);
  border-radius: 999px;
  white-space: nowrap;
  max-width: 180px;
  overflow: hidden;
  text-overflow: ellipsis;
}

.user-menu summary::-webkit-details-marker {
  display: none;
}

.user-menu summary::after {
  content: ' ▾';
  color: var(--muted);
}

.user-menu__panel {
  position: absolute;
  right: 0;
  top: calc(100% + 6px);
  z-index: 30;
  min-width: 220px;
  padding: 12px;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--surface);
  box-shadow: 0 8px 24px rgb(0 0 0 / 0.15);
}

.user-menu__name {
  margin: 0;
  font-weight: 600;
}

.user-menu__email {
  margin: 2px 0 10px;
  color: var(--muted);
  font-size: 13px;
  overflow-wrap: anywhere;
}

.user-menu__panel button {
  width: 100%;
  padding: 8px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: none;
}

.user-menu__panel button:hover {
  border-color: var(--bad);
  color: var(--bad);
}

/* ---- Sidebar: inline rename / delete confirmation ---- */
.conversation-edit {
  flex: 1;
  min-width: 0;
  margin: 2px;
  padding: 6px 8px;
  border: 1px solid var(--accent);
  border-radius: 6px;
  background: var(--surface);
  color: var(--text);
  font: inherit;
}

.conversation.confirming .conversation-actions {
  display: flex;
  align-items: center;
  gap: 2px;
}

.conversation-confirm {
  font-size: 12px;
  color: var(--bad);
  padding: 0 4px;
}

/* ---- Login page ---- */
.button {
  display: inline-block;
  padding: 0.6rem 1.1rem;
  border: 1px solid var(--border);
  border-radius: 0.5rem;
  background: var(--surface);
  color: var(--text);
  font: inherit;
  text-decoration: none;
  cursor: pointer;
}

.button--primary {
  border-color: var(--accent);
  background: var(--accent);
  color: var(--accent-text);
}

.login {
  max-width: 22rem;
  margin: 15vh auto 0;
  padding: 0 1rem;
  display: flex;
  flex-direction: column;
  gap: 1rem;
  text-align: center;
}

.login__logo {
  align-self: center;
}

.login__logo img {
  display: block;
  width: 100%;
  max-width: 14rem;
  height: auto;
}

.login h1 {
  margin-bottom: 0;
}

.login__lead,
.login__note {
  margin: 0;
  color: var(--muted);
}

.login__note {
  font-size: 0.85rem;
}

.login__error {
  margin: 0;
  padding: 0.6rem 0.8rem;
  border: 1px solid var(--error-border);
  border-radius: 0.5rem;
  background: var(--error-bg);
}

.login__divider {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  color: var(--muted);
  font-size: 0.85rem;
}

.login__divider::before,
.login__divider::after {
  content: '';
  flex: 1;
  border-top: 1px solid var(--border);
}

.login__form {
  display: flex;
  flex-direction: column;
  gap: 0.75rem;
  text-align: left;
}

.login__form label {
  display: flex;
  flex-direction: column;
  gap: 0.25rem;
  font-size: 0.9rem;
}

.login__form input {
  padding: 0.5rem 0.6rem;
  border: 1px solid var(--border);
  border-radius: 0.4rem;
  background: var(--surface);
  color: var(--text);
  font: inherit;
}
