:root {
  --bg-app: #0B0F19;
  --bg-sidebar: #131A26;
  --bg-surface: #131A26;
  --bg-elevated: #1A2332;
  --accent: #6366F1;
  --accent-hover: #4F46E5;
  --user-bubble: #1E293B;
  --text-primary: #F1F5F9;
  --text-muted: #94A3B8;
  --border-subtle: rgba(255, 255, 255, 0.06);
  --success: #22c55e;
  --error: #ef4444;
  --radius: 12px;
  --stream-max: 800px;
  --sidebar-width: 280px;
}

* {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  background: var(--bg-app);
  color: var(--text-primary);
  min-height: 100dvh;
  line-height: 1.5;
  overflow: hidden;
}

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

.sidebar {
  width: var(--sidebar-width);
  flex-shrink: 0;
  background: var(--bg-sidebar);
  border-right: 1px solid var(--border-subtle);
  display: flex;
  flex-direction: column;
  z-index: 40;
}

.sidebar-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 1rem 1rem 0.75rem;
}

.logo {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  font-weight: 600;
  font-size: 1.0625rem;
  color: var(--text-primary);
}

.logo svg {
  color: var(--accent);
}

.sidebar-close {
  display: none;
}

.btn-new-chat {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  margin: 0 0.75rem 0.75rem;
  padding: 0.625rem 0.875rem;
  background: transparent;
  border: 1px solid var(--border-subtle);
  border-radius: var(--radius);
  color: var(--text-primary);
  font-size: 0.875rem;
  font-weight: 500;
  cursor: pointer;
  transition: background 0.15s, border-color 0.15s;
}

.btn-new-chat:hover {
  background: var(--bg-elevated);
  border-color: rgba(255, 255, 255, 0.12);
}

.conversation-list {
  flex: 1;
  overflow-y: auto;
  padding: 0 0.5rem;
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.conversation-item {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  padding: 0.625rem 0.75rem;
  border-radius: var(--radius);
  cursor: pointer;
  border: none;
  background: transparent;
  color: var(--text-muted);
  font-size: 0.875rem;
  text-align: left;
  width: 100%;
  transition: background 0.15s, color 0.15s;
  border-left: 3px solid transparent;
}

.conversation-item:hover {
  background: var(--bg-elevated);
  color: var(--text-primary);
}

.conversation-item.active {
  background: var(--bg-elevated);
  color: var(--text-primary);
  border-left-color: var(--accent);
}

.conversation-item-content {
  flex: 1;
  min-width: 0;
}

.conversation-title {
  display: block;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  font-weight: 500;
}

.conversation-time {
  display: block;
  font-size: 0.75rem;
  color: var(--text-muted);
  margin-top: 2px;
}

.conversation-delete {
  opacity: 0;
  flex-shrink: 0;
  padding: 0.25rem;
  background: transparent;
  border: none;
  color: var(--text-muted);
  cursor: pointer;
  border-radius: 6px;
  transition: opacity 0.15s, color 0.15s, background 0.15s;
}

.conversation-item:hover .conversation-delete,
.conversation-delete:focus {
  opacity: 1;
}

.conversation-delete:hover {
  color: var(--error);
  background: rgba(239, 68, 68, 0.1);
}

.sidebar-footer {
  padding: 0.75rem;
  border-top: 1px solid var(--border-subtle);
}

.user-strip {
  display: flex;
  align-items: center;
  gap: 0.625rem;
  padding: 0.5rem 0.625rem;
  border-radius: var(--radius);
}

.user-label {
  font-size: 0.8125rem;
  color: var(--text-muted);
}

.sidebar-overlay {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.5);
  z-index: 30;
}

.sidebar-overlay.visible {
  display: block;
}

.main {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-width: 0;
  background: var(--bg-app);
}

.header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0.875rem 1.25rem;
  border-bottom: 1px solid var(--border-subtle);
  flex-shrink: 0;
}

.header-left {
  display: flex;
  align-items: center;
  gap: 0.75rem;
}

.header h1 {
  font-size: 1rem;
  font-weight: 600;
}

.header-right {
  display: flex;
  align-items: center;
  gap: 0.625rem;
}

.sidebar-open {
  display: none;
}

.status-badge {
  font-size: 0.75rem;
  padding: 0.25rem 0.75rem;
  border-radius: 999px;
  font-weight: 500;
}

.status-loading {
  background: var(--bg-elevated);
  color: var(--text-muted);
}

.status-ok {
  background: rgba(99, 102, 241, 0.15);
  color: var(--accent);
}

.status-error {
  background: rgba(239, 68, 68, 0.15);
  color: var(--error);
}

.chat-area {
  flex: 1;
  overflow: hidden;
  display: flex;
  flex-direction: column;
}

.messages {
  flex: 1;
  overflow-y: auto;
  padding: 1.5rem 1.25rem;
  display: flex;
  flex-direction: column;
  gap: 1.25rem;
  max-width: var(--stream-max);
  width: 100%;
  margin: 0 auto;
}

.message-row {
  display: flex;
  align-items: flex-start;
  gap: 0.75rem;
  max-width: 70%;
  width: fit-content;
  animation: fadeIn 0.2s ease;
}

.message-row.user {
  align-self: flex-end;
  flex-direction: row-reverse;
}

.message-row.assistant {
  align-self: flex-start;
}

.avatar {
  width: 32px;
  height: 32px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
}

.avatar-sm {
  width: 28px;
  height: 28px;
}

.avatar-assistant {
  background: rgba(99, 102, 241, 0.2);
  color: var(--accent);
}

.avatar-user {
  background: var(--bg-elevated);
  color: var(--text-muted);
}

.message-bubble {
  padding: 0.75rem 1rem;
  border-radius: var(--radius);
  white-space: pre-wrap;
  word-break: break-word;
  font-size: 0.9375rem;
  line-height: 1.55;
}

.message-row.user .message-bubble {
  background: var(--user-bubble);
  color: var(--text-primary);
  border-bottom-right-radius: 4px;
}

.message-row.assistant .message-bubble {
  background: var(--bg-sidebar);
  color: var(--text-primary);
  border: 1px solid var(--border-subtle);
  border-bottom-left-radius: 4px;
}

.message-bubble.streaming::after {
  content: "▋";
  animation: blink 0.8s step-end infinite;
  margin-left: 2px;
  color: var(--accent);
}

.empty-state {
  text-align: center;
  color: var(--text-muted);
  margin: auto;
  padding: 3rem 1.5rem;
}

.empty-state h2 {
  font-size: 1.375rem;
  font-weight: 600;
  color: var(--text-primary);
  margin-bottom: 0.5rem;
}

.empty-state p {
  font-size: 0.9375rem;
}

.error-banner {
  background: rgba(239, 68, 68, 0.12);
  color: var(--error);
  padding: 0.75rem 1rem;
  border-radius: var(--radius);
  font-size: 0.875rem;
  align-self: stretch;
  max-width: var(--stream-max);
  width: 100%;
  margin: 0 auto;
}

.composer-wrap {
  flex-shrink: 0;
  padding: 0 1.25rem 1.25rem;
  display: flex;
  justify-content: center;
}

.composer {
  display: flex;
  align-items: flex-end;
  gap: 0.5rem;
  width: 100%;
  max-width: var(--stream-max);
  padding: 0.625rem 0.75rem;
  background: var(--bg-surface);
  border: 1px solid var(--border-subtle);
  border-radius: var(--radius);
  box-shadow: 0 4px 24px rgba(0, 0, 0, 0.25);
  backdrop-filter: blur(12px);
  transition: border-color 0.15s, box-shadow 0.15s;
}

.composer:focus-within {
  border-color: rgba(99, 102, 241, 0.4);
  box-shadow: 0 4px 24px rgba(0, 0, 0, 0.25), 0 0 0 3px rgba(99, 102, 241, 0.12);
}

.composer textarea {
  flex: 1;
  resize: none;
  min-height: 24px;
  max-height: 160px;
  padding: 0.375rem 0.25rem;
  border: none;
  background: transparent;
  color: var(--text-primary);
  font-family: inherit;
  font-size: 0.9375rem;
  line-height: 1.4;
}

.composer textarea:focus {
  outline: none;
}

.composer textarea::placeholder {
  color: var(--text-muted);
}

.icon-btn {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 40px;
  height: 40px;
  padding: 0;
  border: none;
  border-radius: var(--radius);
  background: transparent;
  color: var(--text-muted);
  cursor: pointer;
  transition: background 0.15s, color 0.15s, transform 0.15s;
  flex-shrink: 0;
}

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

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

.send-btn {
  background: var(--accent);
  color: #fff;
}

.send-btn:hover:not(:disabled) {
  background: var(--accent-hover);
  color: #fff;
  transform: scale(1.05);
}

.send-btn:disabled {
  opacity: 0.5;
  transform: none;
}

.modal {
  position: fixed;
  inset: 0;
  z-index: 50;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 1rem;
}

.modal.hidden {
  display: none;
}

.modal-backdrop {
  position: absolute;
  inset: 0;
  background: rgba(0, 0, 0, 0.6);
  backdrop-filter: blur(4px);
}

.modal-panel {
  position: relative;
  width: 100%;
  max-width: 400px;
  background: var(--bg-sidebar);
  border: 1px solid var(--border-subtle);
  border-radius: var(--radius);
  box-shadow: 0 16px 48px rgba(0, 0, 0, 0.4);
}

.modal-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 1rem 1.25rem;
  border-bottom: 1px solid var(--border-subtle);
}

.modal-header h2 {
  font-size: 1rem;
  font-weight: 600;
}

.modal-body {
  padding: 1.25rem;
  display: flex;
  flex-direction: column;
  gap: 1.25rem;
}

.setting-group {
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
}

.setting-group label {
  font-size: 0.8125rem;
  color: var(--text-muted);
  font-weight: 500;
}

.setting-row {
  display: flex;
  align-items: center;
  gap: 0.75rem;
}

.setting-row input[type="range"] {
  flex: 1;
  accent-color: var(--accent);
}

.setting-group input[type="number"],
.modal-body input[type="number"] {
  width: 100%;
  padding: 0.5rem 0.75rem;
  border: 1px solid var(--border-subtle);
  border-radius: var(--radius);
  background: var(--bg-app);
  color: var(--text-primary);
  font-size: 0.875rem;
}

.setting-group input:focus,
.modal-body input:focus {
  outline: none;
  border-color: rgba(99, 102, 241, 0.4);
}

.btn-danger {
  padding: 0.625rem 1rem;
  background: transparent;
  border: 1px solid rgba(239, 68, 68, 0.4);
  border-radius: var(--radius);
  color: var(--error);
  font-size: 0.875rem;
  font-weight: 500;
  cursor: pointer;
  transition: background 0.15s;
}

.btn-danger:hover {
  background: rgba(239, 68, 68, 0.1);
}

@keyframes fadeIn {
  from { opacity: 0; transform: translateY(4px); }
  to { opacity: 1; transform: translateY(0); }
}

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

@media (max-width: 767px) {
  .sidebar {
    position: fixed;
    top: 0;
    left: 0;
    bottom: 0;
    transform: translateX(-100%);
    transition: transform 0.25s ease;
  }

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

  .sidebar-close {
    display: flex;
  }

  .sidebar-open {
    display: flex;
  }

  .message-row {
    max-width: 90%;
  }

  .header h1 {
    font-size: 0.9375rem;
  }
}

@media (min-width: 768px) {
  .sidebar-overlay.visible {
    display: none !important;
  }
}
