/* ============ Base ============ */
:root {
  --bg: #131316;
  --bg-sidebar: #1a1a1f;
  --bg-elevated: #202027;
  --bg-input: #26262e;
  --bg-user-bubble: #2f2f3a;
  --bg-code: #0d0d10;
  --border: #2c2c35;
  --border-strong: #3a3a46;
  --text: #ececf1;
  --text-dim: #9d9da8;
  --text-faint: #6b6b76;
  --accent: #7c6cff;
  --accent-hover: #8f80ff;
  --danger: #ef6a6a;
  --radius: 12px;
  --sidebar-width: 272px;
  --composer-max: 768px;
}

* {
  box-sizing: border-box;
}

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

body {
  font-family:
    -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;
  background: var(--bg);
  color: var(--text);
  font-size: 15px;
  line-height: 1.6;
}

button {
  font-family: inherit;
  cursor: pointer;
  border: none;
  background: none;
  color: inherit;
}

button:focus-visible,
textarea:focus-visible,
select:focus-visible,
a:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

.app {
  display: flex;
  height: 100dvh;
  overflow: hidden;
}

/* ============ Sidebar ============ */
.sidebar {
  width: var(--sidebar-width);
  min-width: var(--sidebar-width);
  background: var(--bg-sidebar);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  transition: transform 0.25s ease;
}

.sidebar-header {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 12px;
}

.btn-new-chat {
  flex: 1;
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 10px 14px;
  border: 1px solid var(--border-strong);
  border-radius: var(--radius);
  color: var(--text);
  font-size: 14px;
  font-weight: 500;
  transition: background 0.15s ease;
}

.btn-new-chat:hover {
  background: var(--bg-elevated);
}

.btn-icon {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 36px;
  height: 36px;
  border-radius: 8px;
  color: var(--text-dim);
}

.btn-icon:hover {
  background: var(--bg-elevated);
  color: var(--text);
}

.sidebar-close {
  display: none;
}

.chat-list {
  flex: 1;
  overflow-y: auto;
  padding: 4px 8px;
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.chat-item {
  display: flex;
  align-items: center;
  gap: 6px;
  border-radius: 8px;
  padding: 0;
}

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

.chat-item-btn {
  flex: 1;
  min-width: 0;
  text-align: left;
  padding: 10px 10px;
  font-size: 13.5px;
  color: var(--text-dim);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  border-radius: 8px;
}

.chat-item.active .chat-item-btn {
  color: var(--text);
}

.chat-item-delete {
  width: 28px;
  height: 28px;
  min-width: 28px;
  margin-right: 4px;
  display: none;
  align-items: center;
  justify-content: center;
  border-radius: 6px;
  color: var(--text-faint);
}

.chat-item:hover .chat-item-delete,
.chat-item.active .chat-item-delete {
  display: inline-flex;
}

.chat-item-delete:hover {
  color: var(--danger);
  background: rgba(239, 106, 106, 0.12);
}

.chat-item-delete.confirm,
.chat-item-delete.confirm:hover {
  display: inline-flex;
  color: #fff;
  background: var(--danger);
}

.sidebar-footer {
  padding: 12px 16px;
  border-top: 1px solid var(--border);
}

.footer-note {
  font-size: 12px;
  color: var(--text-faint);
}

.sidebar-backdrop {
  display: none;
}

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

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

.menu-btn {
  display: none;
}

.topbar-title {
  font-size: 15px;
  font-weight: 600;
  margin: 0;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.topbar-spacer {
  flex: 1;
}

/* Token status banner */
.token-banner {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 16px;
  background: rgba(239, 106, 106, 0.1);
  border-bottom: 1px solid rgba(239, 106, 106, 0.3);
  color: #f0a5a5;
  font-size: 13px;
}

.token-banner[hidden] {
  display: none;
}

.token-banner.warn {
  background: rgba(224, 168, 74, 0.1);
  border-bottom-color: rgba(224, 168, 74, 0.3);
  color: #e6c07a;
}

.token-banner-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: currentColor;
  flex-shrink: 0;
}

/* ============ Messages ============ */
.messages-wrap {
  flex: 1;
  overflow-y: auto;
  scroll-behavior: auto;
  position: relative;
}

.messages {
  max-width: var(--composer-max);
  margin: 0 auto;
  padding: 24px 16px 8px;
  display: flex;
  flex-direction: column;
  gap: 20px;
}

.empty-state {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 8px;
  text-align: center;
  padding: 24px;
  pointer-events: none;
}

.empty-state[hidden] {
  display: none;
}

.empty-icon {
  font-size: 34px;
  color: var(--accent);
  margin-bottom: 4px;
}

.empty-state h2 {
  margin: 0;
  font-size: 22px;
  font-weight: 600;
}
.empty-state p {
  margin: 0;
  color: var(--text-dim);
  font-size: 14px;
  max-width: 420px;
}

.message {
  display: flex;
  flex-direction: column;
  gap: 6px;
}

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

.bubble {
  max-width: 85%;
  padding: 10px 16px;
  border-radius: 18px;
  background: var(--bg-user-bubble);
  white-space: pre-wrap;
  overflow-wrap: break-word;
}

.message.assistant .content {
  max-width: 100%;
  overflow-wrap: break-word;
}

.message .meta-row {
  display: flex;
  gap: 6px;
  align-items: center;
  min-height: 28px;
  opacity: 0;
  transition: opacity 0.15s ease;
}

.message:hover .meta-row {
  opacity: 1;
}

.meta-btn {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  font-size: 12px;
  color: var(--text-faint);
  padding: 4px 8px;
  border-radius: 6px;
}

.meta-btn:hover {
  color: var(--text);
  background: var(--bg-elevated);
}

.message.failed .content {
  opacity: 0.75;
}

.error-note {
  display: inline-block;
  margin-top: 4px;
  padding: 8px 12px;
  border: 1px solid rgba(239, 106, 106, 0.35);
  background: rgba(239, 106, 106, 0.08);
  color: var(--danger);
  border-radius: 10px;
  font-size: 13.5px;
}

.error-note.token-error {
  display: block;
  max-width: 100%;
  border-color: rgba(224, 168, 74, 0.5);
  background: rgba(224, 168, 74, 0.12);
  color: #e6c07a;
  line-height: 1.55;
}

/* Blinking cursor while streaming */
.cursor {
  display: inline-block;
  width: 8px;
  height: 16px;
  margin-left: 2px;
  vertical-align: text-bottom;
  background: var(--text);
  animation: blink 0.9s steps(2, start) infinite;
}

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

/* ============ Markdown content ============ */
.content p {
  margin: 0 0 0.75em;
}
.content p:last-child {
  margin-bottom: 0;
}
.content h1,
.content h2,
.content h3,
.content h4 {
  margin: 1em 0 0.5em;
  line-height: 1.3;
}
.content h1 {
  font-size: 1.4em;
}
.content h2 {
  font-size: 1.25em;
}
.content h3 {
  font-size: 1.1em;
}
.content ul,
.content ol {
  margin: 0 0 0.75em;
  padding-left: 1.4em;
}
.content li {
  margin: 0.25em 0;
}
.content a {
  color: var(--accent-hover);
}
.content blockquote {
  margin: 0 0 0.75em;
  padding: 2px 14px;
  border-left: 3px solid var(--border-strong);
  color: var(--text-dim);
}
.content code {
  font-family: 'SF Mono', SFMono-Regular, ui-monospace, Menlo, Consolas, monospace;
  font-size: 0.875em;
  background: var(--bg-elevated);
  padding: 2px 5px;
  border-radius: 5px;
}
.content pre {
  margin: 0 0 0.9em;
  background: var(--bg-code);
  border: 1px solid var(--border);
  border-radius: 10px;
  overflow: hidden;
}
.content pre code {
  display: block;
  background: none;
  padding: 12px 14px;
  overflow-x: auto;
  line-height: 1.5;
}

.code-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 6px 12px;
  background: var(--bg-elevated);
  border-bottom: 1px solid var(--border);
  font-size: 12px;
  color: var(--text-dim);
}

.code-copy {
  font-size: 12px;
  color: var(--text-dim);
  padding: 2px 8px;
  border-radius: 5px;
}

.code-copy:hover {
  color: var(--text);
  background: var(--bg-input);
}

/* ============ Composer ============ */
.composer-wrap {
  padding: 8px 16px 16px;
}

.composer {
  max-width: var(--composer-max);
  margin: 0 auto;
}

.composer-box {
  display: flex;
  align-items: flex-end;
  gap: 8px;
  background: var(--bg-input);
  border: 1px solid var(--border-strong);
  border-radius: 16px;
  padding: 10px 10px 10px 16px;
  transition: border-color 0.15s ease;
}

.composer-box:focus-within {
  border-color: var(--accent);
}

.composer-input {
  flex: 1;
  resize: none;
  border: none;
  background: none;
  color: var(--text);
  font-family: inherit;
  font-size: 15px;
  line-height: 1.5;
  max-height: 200px;
  padding: 4px 0;
}

.composer-input:focus {
  outline: none;
}
.composer-input::placeholder {
  color: var(--text-faint);
}

.composer-actions {
  display: flex;
  align-items: center;
  gap: 8px;
}

.model-select {
  background: var(--bg-elevated);
  color: var(--text-dim);
  border: 1px solid var(--border);
  border-radius: 8px;
  font-size: 12px;
  padding: 6px 8px;
  max-width: 180px;
}

.btn-send {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 36px;
  height: 36px;
  border-radius: 10px;
  background: var(--accent);
  color: #fff;
  transition:
    background 0.15s ease,
    opacity 0.15s ease;
}

.btn-send:hover:not(:disabled) {
  background: var(--accent-hover);
}
.btn-send:disabled {
  opacity: 0.35;
  cursor: default;
}

.btn-stop {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 8px 12px;
  border: 1px solid var(--border-strong);
  border-radius: 10px;
  font-size: 13px;
  color: var(--text);
}

.btn-stop:hover {
  border-color: var(--danger);
  color: var(--danger);
}

.btn-stop[hidden] {
  display: none;
}

.composer-hint {
  margin: 8px 4px 0;
  font-size: 11.5px;
  color: var(--text-faint);
  text-align: center;
}

/* ============ Auth gate ============ */
.auth-gate {
  position: fixed;
  inset: 0;
  z-index: 50;
  display: flex;
  align-items: center;
  justify-content: center;
  background: rgba(10, 10, 12, 0.85);
  backdrop-filter: blur(4px);
}

.auth-gate[hidden] {
  display: none;
}

.auth-card {
  width: 340px;
  max-width: 90vw;
  padding: 28px 24px;
  background: var(--bg-elevated);
  border: 1px solid var(--border-strong);
  border-radius: var(--radius);
  text-align: center;
}

.auth-card h2 {
  margin: 0 0 6px;
  font-size: 18px;
}

.auth-card p {
  margin: 0 0 16px;
  color: var(--text-dim);
  font-size: 13.5px;
}

.auth-input {
  width: 100%;
  box-sizing: border-box;
  padding: 10px 12px;
  background: var(--bg-input);
  border: 1px solid var(--border-strong);
  border-radius: 10px;
  color: var(--text);
  font-size: 14px;
  margin-bottom: 12px;
}

.auth-input:focus {
  outline: none;
  border-color: var(--accent);
}

.auth-btn {
  width: 100%;
  padding: 10px;
  background: var(--accent);
  color: #fff;
  border-radius: 10px;
  font-size: 14px;
  font-weight: 600;
}

.auth-btn:hover {
  background: var(--accent-hover);
}

.auth-btn-secondary {
  margin-top: 8px;
  background: var(--danger);
}
.auth-btn-secondary:hover {
  background: #e05555;
}

.auth-btn-ghost {
  margin-top: 8px;
  background: transparent;
  border: 1px solid var(--border-strong);
  color: var(--text-dim);
}
.auth-btn-ghost:hover {
  background: var(--bg-input);
  color: var(--text);
}

.sidebar-footer {
  display: flex;
  align-items: center;
  justify-content: space-between;
}
.settings-btn {
  width: 32px;
  height: 32px;
}

/* ============ Scrollbars ============ */
::-webkit-scrollbar {
  width: 8px;
  height: 8px;
}
::-webkit-scrollbar-thumb {
  background: var(--border-strong);
  border-radius: 4px;
}
::-webkit-scrollbar-track {
  background: transparent;
}

/* ============ Mobile ============ */
@media (max-width: 768px) {
  .sidebar {
    position: fixed;
    inset: 0 auto 0 0;
    z-index: 30;
    transform: translateX(-100%);
  }

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

  .sidebar-close {
    display: inline-flex;
  }
  .menu-btn {
    display: inline-flex;
  }

  .sidebar-backdrop {
    display: block;
    position: fixed;
    inset: 0;
    z-index: 20;
    background: rgba(0, 0, 0, 0.5);
    opacity: 0;
    pointer-events: none;
    transition: opacity 0.25s ease;
  }

  .sidebar-backdrop.visible {
    opacity: 1;
    pointer-events: auto;
  }

  .bubble {
    max-width: 92%;
  }
  .model-select {
    display: none;
  }
}
