/* ============================================================
   ChadGPT — base stylesheet
   Zero-dependency, custom-property-driven design.
   ============================================================ */

:root {
  --bg:        #0f1117;
  --surface:   #1a1d27;
  --surface2:  #22263a;
  --border:    #2e3249;
  --accent:    #6c8af7;
  --accent-hv: #8aa2ff;
  --danger:    #e05c6e;
  --text:      #dde1f5;
  --muted:     #7a809e;
  --success:   #4caf82;
  --radius:    6px;
  --font:      system-ui, sans-serif;
  --mono:      "Fira Mono", "Cascadia Code", monospace;
}

*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }
body { background: var(--bg); color: var(--text); font-family: var(--font);
       font-size: 15px; line-height: 1.55; min-height: 100dvh; }
a { color: var(--accent); text-decoration: none; }
a:hover { color: var(--accent-hv); text-decoration: underline; }

/* ---- Navbar ---- */
.navbar {
  display: flex; align-items: center; justify-content: space-between;
  padding: 0.6rem 1.25rem;
  background: var(--surface); border-bottom: 1px solid var(--border);
  position: sticky; top: 0; z-index: 100;
}
.brand { font-weight: 700; font-size: 1.1rem; color: var(--accent); }
.nav-links { display: flex; gap: 1rem; align-items: center; }

/* ---- Toast ---- */
.toast {
  position: fixed; bottom: 1rem; right: 1rem; z-index: 500;
  padding: 0.5rem 1rem; border-radius: var(--radius);
  background: var(--surface2); border: 1px solid var(--border);
  font-size: 0.88rem; opacity: 0; transition: opacity .2s;
}
.toast:not(:empty) { opacity: 1; }
.toast.error { border-color: var(--danger); color: var(--danger); }

/* ---- WebSocket toast (idempotency / streaming errors) ---- */
.ws-toast {
  position: fixed; bottom: 1rem; left: 50%; transform: translateX(-50%);
  z-index: 600; max-width: 480px; width: max-content;
  padding: 0.55rem 1.1rem; border-radius: var(--radius);
  background: var(--surface2); border: 1px solid var(--danger);
  color: var(--danger); font-size: 0.875rem;
  opacity: 0; pointer-events: none;
  transition: opacity 0.2s ease, transform 0.2s ease;
  transform: translateX(-50%) translateY(0.4rem);
}
.ws-toast--visible {
  opacity: 1; pointer-events: auto;
  transform: translateX(-50%) translateY(0);
}

/* ---- Buttons ---- */
.btn-primary {
  background: var(--accent); color: #fff; border: none;
  padding: 0.45rem 1rem; border-radius: var(--radius);
  cursor: pointer; font-size: 0.9rem;
}
.btn-primary:hover { background: var(--accent-hv); }
.btn-primary:disabled { opacity: .5; cursor: not-allowed; }
.btn-sm { padding: 0.3rem 0.7rem; font-size: 0.82rem; border-radius: var(--radius);
          background: var(--surface2); border: 1px solid var(--border);
          color: var(--text); cursor: pointer; }
.btn-sm:hover { border-color: var(--accent); color: var(--accent); }
.btn-danger { background: transparent; border: 1px solid var(--danger);
              color: var(--danger); cursor: pointer; border-radius: var(--radius); }
.btn-danger:hover { background: var(--danger); color: #fff; }
.btn-link { background: none; border: none; color: var(--accent);
            cursor: pointer; font-size: inherit; padding: 0; }
.btn-link:hover { color: var(--accent-hv); text-decoration: underline; }

/* ---- Forms ---- */
input, textarea, select {
  background: var(--surface2); border: 1px solid var(--border);
  color: var(--text); border-radius: var(--radius);
  padding: 0.4rem 0.6rem; font-family: var(--font); font-size: 0.9rem; width: 100%;
}
input:focus, textarea:focus, select:focus {
  outline: none; border-color: var(--accent);
}
label { display: block; margin-bottom: 0.75rem; font-size: 0.88rem; color: var(--muted); }
label > input, label > textarea, label > select { margin-top: 0.2rem; }
.code-input { font-family: var(--mono); font-size: 0.82rem; }

/* ---- Auth card ---- */
.auth-card {
  max-width: 380px; margin: 6rem auto; padding: 2rem;
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
}
.auth-card h1 { margin-bottom: 1.5rem; font-size: 1.3rem; }
.auth-card form { display: flex; flex-direction: column; gap: 0.25rem; }
.auth-card button { margin-top: 0.75rem; }
.auth-card p { margin-top: 1rem; font-size: 0.88rem; color: var(--muted); }

/* ---- Conversations layout ---- */
.conversations-layout {
  display: grid; grid-template-columns: 260px 1fr; height: calc(100dvh - 48px);
}
.sidebar {
  background: var(--surface); border-right: 1px solid var(--border);
  overflow-y: auto; padding: 0;
}
.sidebar-header {
  display: flex; align-items: center; justify-content: space-between;
  padding: 0.75rem 1rem; border-bottom: 1px solid var(--border);
  position: sticky; top: 0; background: var(--surface); z-index: 10;
}
.sidebar-header h2 { font-size: 0.95rem; }
.conv-list { list-style: none; }
.conv-item {
  display: flex; flex-direction: column; padding: 0.65rem 1rem;
  border-bottom: 1px solid var(--border); color: var(--text);
  font-size: 0.9rem; cursor: pointer;
}
.conv-item:hover { background: var(--surface2); }
.conv-time { font-size: 0.75rem; color: var(--muted); margin-top: 0.15rem; }
.empty { padding: 1rem; color: var(--muted); font-size: 0.88rem; }
.main-panel { display: flex; align-items: center; justify-content: center; color: var(--muted); }
.hint { font-size: 0.92rem; }

/* ---- Chat layout ---- */
.chat-layout {
  display: grid; grid-template-columns: 240px 1fr; height: calc(100dvh - 48px);
}
.chat-sidebar {
  background: var(--surface); border-right: 1px solid var(--border);
  overflow-y: auto; padding: 1rem; display: flex; flex-direction: column; gap: 1.5rem;
}
.chat-sidebar h3 { font-size: 0.88rem; color: var(--muted); margin-bottom: 0.5rem; }
.chat-pane {
  display: flex; flex-direction: column; overflow: hidden;
}
.messages {
  flex: 1; overflow-y: auto; padding: 1rem; display: flex; flex-direction: column; gap: 0.75rem;
}
.message {
  background: var(--surface); border: 1px solid var(--border);
  border-radius: var(--radius); padding: 0.65rem 0.85rem;
}
.message-sender { font-size: 0.78rem; color: var(--accent); font-weight: 600; }
/* per-type sender label colours */
.message-sender--user  { color: var(--accent); }
.message-sender--agent { color: var(--success, #3fb950); }
/* per-type message bubble tints */
.message--sender-agent {
  background: color-mix(in srgb, var(--surface) 88%, var(--success, #3fb950));
  border-color: color-mix(in srgb, var(--border) 60%, var(--success, #3fb950));
}
.message-time   { font-size: 0.72rem; color: var(--muted); margin-left: 0.5rem; }
.message-body   { display: flex; align-items: flex-start; gap: 0.4rem; margin-top: 0.35rem; }
.message-content { flex: 1; min-width: 0; white-space: pre-wrap; word-break: break-word; }
.message--streaming .message-streaming-indicator {
  display: inline-block; animation: blink 1s step-end infinite; color: var(--accent);
}
@keyframes blink { 50% { opacity: 0; } }
.chat-input-bar {
  display: flex; gap: 0.5rem; padding: 0.75rem 1rem;
  border-top: 1px solid var(--border); background: var(--surface);
}
.chat-input-bar textarea { flex: 1; resize: none; height: 2.6rem; }

/* ---- Page content (keys / agents) ---- */
.page-content { max-width: 740px; margin: 2rem auto; padding: 0 1rem; }
.page-content h1 { font-size: 1.4rem; margin-bottom: 1.25rem; }
.page-content h2 { font-size: 1rem; margin-bottom: 0.75rem; margin-top: 1.5rem; }
.list-item {
  display: flex; align-items: center; justify-content: space-between;
  padding: 0.65rem 0.85rem; background: var(--surface);
  border: 1px solid var(--border); border-radius: var(--radius); margin-bottom: 0.5rem;
}
.item-actions { display: flex; gap: 0.5rem; align-items: center; }
.form-section { margin-top: 2rem; border-top: 1px solid var(--border); padding-top: 1.5rem; }
.form-section form { display: flex; flex-direction: column; gap: 0.25rem; max-width: 420px; }
.form-section button { margin-top: 0.5rem; align-self: flex-start; }

/* ---- Misc ---- */
.muted   { color: var(--muted); font-size: 0.8rem; margin-left: 0.4rem; }
.success-msg { color: var(--success); font-size: 0.88rem; margin-bottom: 0.75rem; }
.error-msg   { color: var(--danger);  font-size: 0.88rem; margin-bottom: 0.75rem; }

/* ---- Message terminal-state variants ---- */
.message--stopped {
  border-left: 3px solid #e6a817;
}
.message--stopped .message-content {
  opacity: 0.75;
}
.message--failed {
  border-left: 3px solid var(--danger);
}
.message--failed .message-content {
  opacity: 0.65;
}

/* ---- Status badge (stopped / failed) ---- */
.message-status-badge {
  display: inline-block;
  font-size: 0.68rem;
  padding: 1px 7px;
  border-radius: 999px;
  vertical-align: middle;
  margin-left: 0.4rem;
  font-weight: 600;
  letter-spacing: 0.03em;
  line-height: 1.6;
}
.message--stopped .message-status-badge {
  background: rgba(230, 168, 23, 0.12);
  color: #e6a817;
  border: 1px solid rgba(230, 168, 23, 0.5);
}
.message--failed .message-status-badge {
  background: var(--danger);
  color: #fff;
  border: 1px solid var(--danger);
}

/* ---- Message avatar ---- */
.message-avatar {
  display: block;
  width: 28px; height: 28px;
  border-radius: 50%;
  object-fit: cover;
  margin-bottom: 0.2rem;
  border: 1px solid var(--border);
}

/* ---- Profile page ---- */
.info-list {
  display: grid;
  grid-template-columns: max-content 1fr;
  gap: 0.35rem 1rem;
  font-size: 0.9rem;
}
.info-list dt { color: var(--muted); }
.info-list dd { font-family: var(--mono); }

.link-list {
  list-style: none;
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
}

/* ---- Attachment staging strip ---- */
.attach-strip {
  display: flex; flex-wrap: wrap; gap: 0.5rem;
  padding: 0.5rem 1rem 0;
  background: var(--surface);
}
.attach-strip.hidden { display: none; }
.attach-thumb {
  position: relative; width: 64px; height: 64px;
  border: 1px solid var(--border); border-radius: var(--radius);
  overflow: hidden; background: var(--surface2);
  display: flex; align-items: center; justify-content: center;
  font-size: 0.7rem; color: var(--muted); text-align: center;
}
.attach-thumb img {
  width: 100%; height: 100%; object-fit: cover;
}
.attach-thumb__remove {
  position: absolute; top: 2px; right: 2px;
  width: 18px; height: 18px; border-radius: 50%;
  background: rgba(0,0,0,0.65); border: none;
  color: #fff; font-size: 0.75rem; cursor: pointer;
  display: flex; align-items: center; justify-content: center;
  line-height: 1;
}
.attach-thumb__spinner {
  position: absolute; inset: 0;
  background: rgba(0,0,0,0.45);
  display: flex; align-items: center; justify-content: center;
  font-size: 1.1rem;
}
.attach-btn {
  display: flex; align-items: center; justify-content: center;
  width: 2rem; height: 2rem; cursor: pointer;
  font-size: 1.1rem; flex-shrink: 0;
}

/* ---- Attachment gallery ---- */
.attachment-gallery {
  display: grid; gap: 4px; margin-top: 0.4rem;
}
.attachment-gallery--1  { grid-template-columns: 1fr; }
.attachment-gallery--2  { grid-template-columns: 1fr 1fr; }
.attachment-gallery--3  { grid-template-columns: 1fr 1fr; }
.attachment-gallery--3 > :first-child { grid-column: 1 / -1; }
.attachment-gallery--4plus { grid-template-columns: 1fr 1fr; }
.attachment-gallery__item {
  width: 100%; border-radius: var(--radius);
  object-fit: cover; max-height: 320px; cursor: pointer;
  display: block;
}

/* ---- Attachment chip (non-image) ---- */
.attachment-chip {
  display: inline-flex; align-items: center; gap: 0.3rem;
  padding: 0.3rem 0.6rem; border-radius: var(--radius);
  background: var(--surface2); border: 1px solid var(--border);
  font-size: 0.82rem; color: var(--text); text-decoration: none;
  margin-top: 0.25rem;
}
.attachment-chip:hover { border-color: var(--accent); color: var(--accent); text-decoration: none; }
.attachment-chip__size { color: var(--muted); font-size: 0.75rem; }

/* ---- Lightbox ---- */
.lightbox {
  position: fixed; inset: 0; z-index: 900;
  background: rgba(0,0,0,0.88);
  display: flex; align-items: center; justify-content: center;
}
.lightbox.hidden { display: none; }
.lightbox__img {
  max-width: 90vw; max-height: 88vh;
  border-radius: var(--radius);
  object-fit: contain;
}
.lightbox__nav {
  position: absolute; top: 50%; transform: translateY(-50%);
  background: rgba(255,255,255,0.12); border: none;
  color: #fff; font-size: 1.5rem; padding: 0.5rem 0.8rem;
  cursor: pointer; border-radius: var(--radius);
}
.lightbox__nav:hover { background: rgba(255,255,255,0.25); }
.lightbox__nav--prev { left: 1rem; }
.lightbox__nav--next { right: 1rem; }
.lightbox__counter {
  position: absolute; bottom: 1rem; left: 50%; transform: translateX(-50%);
  color: rgba(255,255,255,0.7); font-size: 0.85rem;
}
.lightbox__close {
  position: absolute; top: 1rem; right: 1rem;
  background: rgba(255,255,255,0.12); border: none;
  color: #fff; font-size: 1.2rem; padding: 0.3rem 0.6rem;
  cursor: pointer; border-radius: var(--radius);
}
.lightbox__close:hover { background: rgba(255,255,255,0.25); }
