/* SamAlgo Chat — polished dark interface */

:root {
  --chat-bg: #080c16;
  --chat-surface: #0f172a;
  --chat-surface-2: #111c32;
  --chat-surface-3: #18243d;
  --chat-border: rgba(148, 163, 184, 0.18);
  --chat-border-strong: rgba(148, 163, 184, 0.28);
  --chat-blue: #3b82f6;
  --chat-blue-2: #60a5fa;
  --chat-cyan: #22d3ee;
  --chat-green: #34d399;
  --chat-amber: #fbbf24;
  --chat-red: #fb7185;
  --chat-text: #f8fafc;
  --chat-text-soft: #cbd5e1;
  --chat-text-muted: #94a3b8;
  --chat-text-dim: #64748b;
  --chat-shadow: 0 18px 60px rgba(0, 0, 0, 0.38);
  --chat-radius: 8px;
  --chat-sidebar: 300px;
  --chat-header: 58px;
  --chat-input: 112px;
  --font-body: "Manrope", ui-sans-serif, system-ui, sans-serif;
  --font-mono: "IBM Plex Mono", ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, monospace;
}

* {
  box-sizing: border-box;
}

html,
body {
  height: 100%;
  margin: 0;
  overflow: hidden;
  background: var(--chat-bg);
  color: var(--chat-text);
  font-family: var(--font-body);
}

body::before {
  content: "";
  position: fixed;
  inset: 0;
  pointer-events: none;
  background:
    radial-gradient(circle at 18% 18%, rgba(59, 130, 246, 0.16), transparent 24rem),
    linear-gradient(135deg, rgba(34, 211, 238, 0.08), transparent 38%),
    linear-gradient(180deg, rgba(8, 12, 22, 0), #080c16 78%);
}

button,
select,
textarea {
  font: inherit;
}

button {
  cursor: pointer;
}

button:disabled {
  cursor: not-allowed;
  opacity: 0.55;
}

.chat-shell {
  position: relative;
  z-index: 1;
  display: grid;
  grid-template-columns: var(--chat-sidebar) 1fr;
  height: 100vh;
  min-height: 0;
}

.chat-sidebar {
  min-height: 0;
  border-right: 1px solid var(--chat-border);
  background: rgba(8, 12, 22, 0.72);
  backdrop-filter: blur(18px);
  display: flex;
  flex-direction: column;
}

.chat-brand {
  height: var(--chat-header);
  padding: 0 18px;
  border-bottom: 1px solid var(--chat-border);
  display: flex;
  align-items: center;
  gap: 10px;
  text-decoration: none;
  color: var(--chat-text);
}

.chat-brand img {
  width: 28px;
  height: 28px;
  border-radius: 6px;
}

.chat-brand-name {
  font-weight: 800;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  color: var(--chat-text);
}

.chat-brand-tag {
  border: 1px solid var(--chat-border);
  border-radius: 999px;
  padding: 2px 7px;
  color: var(--chat-text-muted);
  font-size: 0.64rem;
  line-height: 1;
  text-transform: uppercase;
}

.chat-sidebar-body {
  padding: 14px;
  overflow: auto;
}

.chat-sidebar-actions {
  display: grid;
  gap: 10px;
}

.chat-new-btn,
.chat-login-btn {
  width: 100%;
  border: 1px solid var(--chat-border-strong);
  border-radius: var(--chat-radius);
  padding: 10px 12px;
  color: var(--chat-text);
  background: rgba(15, 23, 42, 0.78);
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  transition: transform 140ms ease, border-color 140ms ease, background 140ms ease;
}

.chat-new-btn:hover,
.chat-login-btn:hover {
  transform: translateY(-1px);
  border-color: rgba(96, 165, 250, 0.48);
  background: rgba(30, 41, 59, 0.78);
}

.chat-auth-note {
  margin: 12px 0 0;
  padding: 12px;
  border: 1px solid rgba(251, 191, 36, 0.22);
  border-radius: var(--chat-radius);
  background: rgba(251, 191, 36, 0.07);
  color: var(--chat-text-soft);
  font-size: 0.82rem;
  line-height: 1.45;
}

.chat-history-title-row {
  margin: 20px 0 10px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
}

.chat-history-title {
  margin: 0;
  color: var(--chat-text-dim);
  font-size: 0.72rem;
  font-weight: 800;
  letter-spacing: 0.08em;
  text-transform: uppercase;
}

.chat-history-refresh {
  width: 28px;
  height: 28px;
  border: 1px solid var(--chat-border);
  border-radius: 7px;
  color: var(--chat-text-muted);
  background: rgba(15, 23, 42, 0.72);
  display: grid;
  place-items: center;
}

.chat-history-refresh:hover {
  color: var(--chat-text);
  border-color: rgba(96, 165, 250, 0.42);
}

.chat-history-state,
.chat-history-error {
  border: 1px solid var(--chat-border);
  border-radius: var(--chat-radius);
  padding: 9px 10px;
  color: var(--chat-text-muted);
  background: rgba(148, 163, 184, 0.06);
  font-size: 0.8rem;
}

.chat-history-error {
  border-color: rgba(251, 113, 133, 0.3);
  color: #ffe4e6;
  background: rgba(251, 113, 133, 0.08);
}

.chat-history-list {
  display: grid;
  gap: 6px;
}

.chat-history-row {
  width: 100%;
  border: 1px solid transparent;
  border-radius: var(--chat-radius);
  background: transparent;
  display: grid;
  grid-template-columns: minmax(0, 1fr) 30px;
  align-items: center;
  gap: 4px;
  transition: border-color 140ms ease, background 140ms ease;
}

.chat-history-row:hover,
.chat-history-row.active {
  border-color: var(--chat-border);
  background: rgba(148, 163, 184, 0.08);
}

.chat-history-item {
  min-width: 0;
  border: 0;
  border-radius: calc(var(--chat-radius) - 1px);
  padding: 9px 10px;
  background: transparent;
  color: var(--chat-text-soft);
  text-align: left;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.chat-history-item:hover {
  color: var(--chat-text);
}

.chat-history-delete {
  width: 26px;
  height: 26px;
  margin-right: 3px;
  border: 1px solid transparent;
  border-radius: 7px;
  color: var(--chat-text-dim);
  background: transparent;
  display: grid;
  place-items: center;
  opacity: 0;
  transition: opacity 140ms ease, color 140ms ease, border-color 140ms ease, background 140ms ease;
}

.chat-history-row:hover .chat-history-delete,
.chat-history-row.active .chat-history-delete,
.chat-history-delete:focus-visible {
  opacity: 1;
}

.chat-history-delete:hover:not(:disabled) {
  border-color: rgba(251, 113, 133, 0.32);
  color: #ffe4e6;
  background: rgba(251, 113, 133, 0.1);
}

.chat-main {
  min-width: 0;
  min-height: 0;
  display: grid;
  grid-template-rows: var(--chat-header) 1fr var(--chat-input);
}

.chat-header {
  border-bottom: 1px solid var(--chat-border);
  background: rgba(8, 12, 22, 0.62);
  backdrop-filter: blur(18px);
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  padding: 0 22px;
}

.chat-title-wrap {
  min-width: 0;
}

.chat-title {
  margin: 0;
  font-size: 0.95rem;
  font-weight: 800;
}

.chat-subtitle {
  margin: 2px 0 0;
  color: var(--chat-text-muted);
  font-size: 0.76rem;
}

.chat-header-controls {
  display: flex;
  align-items: center;
  gap: 10px;
}

.model-select {
  min-width: 220px;
  max-width: 330px;
  border: 1px solid var(--chat-border-strong);
  border-radius: var(--chat-radius);
  padding: 9px 34px 9px 10px;
  color: var(--chat-text);
  background: rgba(15, 23, 42, 0.92);
  outline: none;
}

.chat-status-pill {
  border: 1px solid var(--chat-border);
  border-radius: 999px;
  padding: 6px 10px;
  color: var(--chat-text-muted);
  font-size: 0.75rem;
  white-space: nowrap;
}

.chat-messages-scroll {
  min-height: 0;
  overflow-y: auto;
  padding: 28px 22px 18px;
  scroll-behavior: smooth;
}

.chat-messages {
  width: min(920px, 100%);
  margin: 0 auto;
  display: grid;
  gap: 22px;
}

.chat-empty {
  min-height: calc(100vh - var(--chat-header) - var(--chat-input) - 80px);
  display: grid;
  place-items: center;
  text-align: center;
}

.chat-empty-inner {
  width: min(620px, 100%);
}

.chat-empty-kicker {
  color: var(--chat-blue-2);
  font-family: var(--font-mono);
  font-size: 0.78rem;
  text-transform: uppercase;
  letter-spacing: 0.08em;
}

.chat-empty h2 {
  margin: 12px 0;
  font-size: clamp(2rem, 6vw, 4.4rem);
  line-height: 0.95;
  letter-spacing: 0;
}

.chat-empty p {
  margin: 0 auto;
  max-width: 560px;
  color: var(--chat-text-soft);
  line-height: 1.6;
}

.message {
  display: grid;
  grid-template-columns: 34px minmax(0, 1fr);
  gap: 12px;
  align-items: start;
  animation: message-in 180ms ease both;
}

.message.user {
  grid-template-columns: minmax(0, 1fr);
  justify-items: end;
}

.message-avatar {
  width: 34px;
  height: 34px;
  border: 1px solid rgba(96, 165, 250, 0.36);
  border-radius: 8px;
  background: rgba(59, 130, 246, 0.12);
  display: grid;
  place-items: center;
  overflow: hidden;
}

.message-avatar img {
  width: 24px;
  height: 24px;
  object-fit: contain;
}

.message-card {
  position: relative;
  min-width: 0;
  width: fit-content;
  max-width: min(780px, 100%);
  border: 1px solid var(--chat-border);
  border-radius: 8px;
  padding: 14px 16px;
  background: rgba(15, 23, 42, 0.78);
  box-shadow: 0 14px 40px rgba(0, 0, 0, 0.22);
}

.message.user .message-card {
  border-color: rgba(96, 165, 250, 0.34);
  background: linear-gradient(135deg, rgba(59, 130, 246, 0.26), rgba(34, 211, 238, 0.12));
}

.message-actions {
  position: absolute;
  top: 8px;
  right: 8px;
  opacity: 0;
  transition: opacity 140ms ease;
}

.message-card:hover .message-actions {
  opacity: 1;
}

.icon-btn {
  width: 30px;
  height: 30px;
  border: 1px solid var(--chat-border);
  border-radius: 7px;
  color: var(--chat-text-muted);
  background: rgba(8, 12, 22, 0.65);
  display: inline-grid;
  place-items: center;
}

.icon-btn:hover {
  color: var(--chat-text);
  border-color: rgba(96, 165, 250, 0.42);
}

.message-content {
  color: var(--chat-text);
  line-height: 1.65;
  overflow-wrap: anywhere;
}

.message-content p {
  margin: 0 0 0.85rem;
}

.message-content p:last-child {
  margin-bottom: 0;
}

.message-content a {
  color: var(--chat-blue-2);
}

.message-content code {
  border: 1px solid var(--chat-border);
  border-radius: 6px;
  padding: 0.12rem 0.35rem;
  background: rgba(8, 12, 22, 0.72);
  color: #dbeafe;
  font-family: var(--font-mono);
  font-size: 0.9em;
}

.message-content pre {
  overflow: auto;
  margin: 12px 0;
  border: 1px solid var(--chat-border);
  border-radius: 8px;
  padding: 14px;
  background: #0b1120;
}

.message-content pre code {
  border: 0;
  padding: 0;
  background: transparent;
}

.typing-indicator {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  height: 20px;
}

.typing-indicator span {
  width: 6px;
  height: 6px;
  border-radius: 999px;
  background: var(--chat-blue-2);
  animation: typing 920ms ease-in-out infinite;
}

.typing-indicator span:nth-child(2) {
  animation-delay: 120ms;
}

.typing-indicator span:nth-child(3) {
  animation-delay: 240ms;
}

.tool-stack {
  display: grid;
  gap: 10px;
  margin: 0 0 14px;
}

.tool-card {
  border: 1px solid rgba(96, 165, 250, 0.24);
  border-radius: 8px;
  background: linear-gradient(180deg, rgba(30, 41, 59, 0.86), rgba(15, 23, 42, 0.86));
  overflow: hidden;
}

.tool-header {
  width: 100%;
  border: 0;
  padding: 11px 12px;
  color: var(--chat-text);
  background: transparent;
  display: grid;
  grid-template-columns: 30px 1fr auto 18px;
  gap: 10px;
  align-items: center;
  text-align: left;
}

.tool-icon {
  width: 30px;
  height: 30px;
  border: 1px solid rgba(96, 165, 250, 0.28);
  border-radius: 7px;
  color: var(--chat-blue-2);
  background: rgba(59, 130, 246, 0.12);
  display: grid;
  place-items: center;
}

.tool-title {
  min-width: 0;
}

.tool-name {
  font-weight: 800;
  font-size: 0.84rem;
}

.tool-caption {
  color: var(--chat-text-muted);
  font-size: 0.72rem;
}

.tool-status {
  border-radius: 999px;
  padding: 4px 8px;
  font-size: 0.7rem;
  color: var(--chat-text);
  background: rgba(148, 163, 184, 0.14);
}

.tool-status.running {
  color: #dbeafe;
  background: rgba(59, 130, 246, 0.18);
}

.tool-status.done {
  color: #d1fae5;
  background: rgba(52, 211, 153, 0.14);
}

.tool-status.error {
  color: #ffe4e6;
  background: rgba(251, 113, 133, 0.14);
}

.tool-chevron {
  color: var(--chat-text-muted);
  transition: transform 160ms ease;
}

.tool-card.expanded .tool-chevron {
  transform: rotate(90deg);
}

.tool-details {
  border-top: 1px solid rgba(148, 163, 184, 0.14);
  padding: 12px;
  display: grid;
  gap: 12px;
}

.tool-section-label {
  display: block;
  margin-bottom: 6px;
  color: var(--chat-text-dim);
  font-family: var(--font-mono);
  font-size: 0.68rem;
  letter-spacing: 0.08em;
  text-transform: uppercase;
}

.tool-code {
  max-height: 260px;
  overflow: auto;
  margin: 0;
  border: 1px solid rgba(148, 163, 184, 0.14);
  border-radius: 7px;
  padding: 10px;
  color: #dbeafe;
  background: rgba(8, 12, 22, 0.84);
  font-family: var(--font-mono);
  font-size: 0.78rem;
  line-height: 1.5;
  white-space: pre-wrap;
}

.tool-result-preview {
  max-height: 190px;
}

.thinking-block {
  margin: 0 0 12px;
  border: 1px solid rgba(34, 211, 238, 0.2);
  border-radius: 8px;
  background: rgba(34, 211, 238, 0.06);
  overflow: hidden;
}

.thinking-toggle {
  width: 100%;
  border: 0;
  padding: 10px 12px;
  color: var(--chat-text-soft);
  background: transparent;
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.thinking-label {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  font-size: 0.82rem;
  font-weight: 700;
}

.thinking-dots {
  display: inline-flex;
  gap: 4px;
}

.thinking-dots span {
  width: 4px;
  height: 4px;
  border-radius: 999px;
  background: var(--chat-cyan);
  animation: typing 920ms ease-in-out infinite;
}

.thinking-dots span:nth-child(2) { animation-delay: 120ms; }
.thinking-dots span:nth-child(3) { animation-delay: 240ms; }

.thinking-body {
  border-top: 1px solid rgba(34, 211, 238, 0.14);
  padding: 12px;
  color: var(--chat-text-soft);
  font-family: var(--font-mono);
  font-size: 0.78rem;
  line-height: 1.6;
  white-space: pre-wrap;
}

.message-error {
  margin-top: 10px;
  border: 1px solid rgba(251, 113, 133, 0.3);
  border-radius: 8px;
  padding: 10px 12px;
  color: #ffe4e6;
  background: rgba(251, 113, 133, 0.1);
}

.chat-input-wrap {
  border-top: 1px solid var(--chat-border);
  background: rgba(8, 12, 22, 0.62);
  backdrop-filter: blur(18px);
  padding: 16px 22px 18px;
}

.chat-form {
  width: min(920px, 100%);
  margin: 0 auto;
}

.public-block-banner {
  width: 100%;
  margin: 0;
  border: 1px solid rgba(251, 113, 133, 0.48);
  border-radius: 8px;
  padding: 11px 13px;
  color: #ffe4e6;
  background: rgba(127, 29, 29, 0.36);
  box-shadow: 0 12px 32px rgba(127, 29, 29, 0.22);
  display: grid;
  gap: 4px;
  font-size: 0.82rem;
  line-height: 1.45;
}

.public-block-banner strong {
  color: #fecdd3;
  font-size: 0.78rem;
  letter-spacing: 0.06em;
  text-transform: uppercase;
}

.input-shell {
  min-height: 58px;
  border: 1px solid var(--chat-border-strong);
  border-radius: 8px;
  padding: 10px 10px 10px 14px;
  background: rgba(15, 23, 42, 0.92);
  box-shadow: var(--chat-shadow);
  display: grid;
  grid-template-columns: 1fr 42px;
  gap: 10px;
  align-items: end;
}

.input-shell:focus-within {
  border-color: rgba(96, 165, 250, 0.58);
}

.chat-input {
  width: 100%;
  max-height: 180px;
  border: 0;
  outline: 0;
  resize: none;
  padding: 8px 0;
  color: var(--chat-text);
  background: transparent;
  line-height: 1.55;
}

.chat-input::placeholder {
  color: var(--chat-text-dim);
}

.send-btn {
  width: 42px;
  height: 42px;
  border: 0;
  border-radius: 8px;
  color: #fff;
  background: linear-gradient(135deg, var(--chat-blue), var(--chat-cyan));
  display: grid;
  place-items: center;
  transition: transform 130ms ease, filter 130ms ease;
}

.send-btn:hover:not(:disabled) {
  transform: translateY(-1px);
  filter: brightness(1.08);
}

.send-btn:active:not(:disabled) {
  transform: translateY(0) scale(0.98);
}

.input-footer {
  width: min(920px, 100%);
  margin: 8px auto 0;
  color: var(--chat-text-dim);
  font-size: 0.72rem;
  text-align: center;
}

.mobile-sidebar-toggle {
  display: none;
}

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

@keyframes typing {
  0%, 80%, 100% {
    transform: translateY(0);
    opacity: 0.45;
  }
  40% {
    transform: translateY(-4px);
    opacity: 1;
  }
}

@media (max-width: 900px) {
  .chat-shell {
    grid-template-columns: 1fr;
  }

  .chat-sidebar {
    position: fixed;
    z-index: 40;
    inset: 0 auto 0 0;
    width: min(320px, 86vw);
    transform: translateX(-102%);
    transition: transform 180ms ease;
  }

  .chat-shell.sidebar-open .chat-sidebar {
    transform: translateX(0);
  }

  .mobile-sidebar-toggle {
    display: inline-grid;
  }

  .chat-header {
    padding: 0 14px;
  }

  .model-select {
    min-width: 150px;
    max-width: 52vw;
  }

  .chat-status-pill {
    display: none;
  }

  .chat-messages-scroll {
    padding: 20px 14px 14px;
  }

  .chat-input-wrap {
    padding: 12px 14px 14px;
  }

  .message-card {
    max-width: 100%;
  }
}
