:root {
  --bg: #0f1117;
  --bg-elevated: #161b26;
  --bg-hover: #1e2533;
  --border: #2a3344;
  --text: #e8ecf4;
  --text-muted: #8b95a8;
  --accent: #5b8def;
  --accent-hover: #4a7de0;
  --user-bubble: #1a3a5c;
  --assistant-bubble: #1a1f2b;
  --danger: #e85d5d;
  --radius: 12px;
  --font: "Segoe UI", system-ui, -apple-system, sans-serif;
}

* { box-sizing: border-box; }

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

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

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

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

.brand {
  display: flex;
  align-items: center;
  gap: 0.55rem;
  min-width: 0;
  flex: 1;
}

.brand-logo {
  flex-shrink: 0;
  width: 32px;
  height: 32px;
  border-radius: 8px;
  object-fit: cover;
}

.sidebar-header h1 {
  margin: 0;
  font-size: 1rem;
  font-weight: 600;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.search-form { padding: 0 0.75rem 0.5rem; }

.search-form input {
  width: 100%;
  padding: 0.5rem 0.75rem;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--bg);
  color: var(--text);
  font: inherit;
}

.conversation-list {
  flex: 1;
  overflow-y: auto;
  padding: 0.25rem 0.5rem;
}

.conversation-item {
  display: grid;
  grid-template-columns: 1fr auto;
  grid-template-rows: auto auto;
  gap: 0.15rem 0.5rem;
  width: 100%;
  padding: 0.65rem 0.75rem;
  margin-bottom: 0.25rem;
  border: none;
  border-radius: 8px;
  background: transparent;
  color: var(--text);
  text-align: left;
  cursor: pointer;
  font: inherit;
  position: relative;
}

.conversation-item:hover { background: var(--bg-hover); }

.conversation-item.active {
  background: var(--bg-hover);
  outline: 1px solid var(--border);
}

.conversation-title {
  font-weight: 600;
  font-size: 0.9rem;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  grid-column: 1;
}

.conversation-meta {
  font-size: 0.7rem;
  color: var(--text-muted);
  grid-column: 2;
  grid-row: 1;
}

.conversation-preview {
  grid-column: 1 / -1;
  font-size: 0.75rem;
  color: var(--text-muted);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.conversation-delete {
  position: absolute;
  top: 0.35rem;
  right: 0.35rem;
  width: 1.4rem;
  height: 1.4rem;
  display: none;
  align-items: center;
  justify-content: center;
  border-radius: 4px;
  color: var(--text-muted);
  font-size: 1.1rem;
  line-height: 1;
}

.conversation-item:hover .conversation-delete { display: flex; }

.conversation-delete:hover {
  background: rgba(232, 93, 93, 0.2);
  color: var(--danger);
}

.conversation-empty {
  padding: 1rem;
  color: var(--text-muted);
  font-size: 0.85rem;
  text-align: center;
}

.sidebar-footer {
  padding: 0.75rem;
  border-top: 1px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.5rem;
}

.app-version {
  font-size: 0.72rem;
  color: var(--text-muted);
  letter-spacing: 0.02em;
  flex-shrink: 0;
}

.login-version {
  margin: 1rem 0 0;
  text-align: center;
}

.chat {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-width: 0;
}

.chat-header {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  padding: 0.75rem 1rem;
  border-bottom: 1px solid var(--border);
}

.chat-title {
  flex: 1;
  font-weight: 600;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.chat-actions {
  display: flex;
  align-items: center;
  gap: 0.5rem;
}

.user-badge {
  font-size: 0.8rem;
  color: var(--text-muted);
  padding: 0.25rem 0.5rem;
  background: var(--bg-hover);
  border-radius: 6px;
}

.error-banner {
  display: none;
  margin: 0.5rem 1rem 0;
  padding: 0.6rem 0.85rem;
  border-radius: 8px;
  background: rgba(232, 93, 93, 0.15);
  color: #ffb4b4;
  font-size: 0.85rem;
}

.error-banner.visible { display: block; }

.messages {
  flex: 1;
  overflow-y: auto;
  padding: 1rem 1.25rem;
}

.welcome {
  max-width: 480px;
  margin: 2rem auto;
  text-align: center;
  color: var(--text-muted);
}

.welcome-mascot {
  display: block;
  margin: 0 auto 1rem;
  width: 120px;
  height: 120px;
  border-radius: 24px;
  object-fit: cover;
  box-shadow: 0 8px 32px rgba(91, 141, 239, 0.2);
}

.welcome h2 {
  color: var(--text);
  margin-bottom: 0.5rem;
}

.message {
  margin-bottom: 1.25rem;
  max-width: min(920px, 96%);
}

.message.user {
  margin-left: auto;
  max-width: min(520px, 78%);
}

.message.assistant {
  max-width: min(920px, 96%);
}

.message-label {
  display: block;
  font-size: 0.7rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--text-muted);
  margin-bottom: 0.35rem;
}

.message.user .message-label { text-align: right; }

.message-bubble {
  padding: 0.85rem 1rem;
  border-radius: var(--radius);
  line-height: 1.55;
  font-size: 0.95rem;
  word-break: break-word;
}

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

.message.assistant .message-bubble {
  background: var(--assistant-bubble);
  border: 1px solid var(--border);
  border-bottom-left-radius: 4px;
  padding: 1rem 1.15rem;
  line-height: 1.6;
}

.message-bubble .md-content > *:first-child {
  margin-top: 0;
}

.message-bubble .md-content > *:last-child {
  margin-bottom: 0;
}

.md-table-wrap {
  overflow-x: auto;
  margin: 0.85rem 0;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: rgba(0, 0, 0, 0.15);
}

.message-bubble table.md-table {
  width: 100%;
  min-width: 420px;
  border-collapse: collapse;
  font-size: 0.88rem;
}

.message-bubble table.md-table th,
.message-bubble table.md-table td {
  padding: 0.55rem 0.75rem;
  border-bottom: 1px solid var(--border);
  text-align: left;
  vertical-align: top;
  line-height: 1.45;
}

.message-bubble table.md-table th {
  background: var(--bg-hover);
  font-weight: 600;
  white-space: nowrap;
}

.message-bubble table.md-table tr:last-child td {
  border-bottom: none;
}

.message-bubble table.md-table td strong {
  font-weight: 600;
}

.message-bubble.streaming::after {
  content: "▋";
  animation: blink 1s step-end infinite;
  margin-left: 2px;
  opacity: 0.7;
}

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

.message-bubble p { margin: 0 0 0.6rem; }
.message-bubble p:last-child { margin-bottom: 0; }

.message-bubble .code-block {
  margin: 0.75rem 0;
  background: rgba(0, 0, 0, 0.35);
  border-radius: 8px;
  overflow: hidden;
}

.message-bubble .code-block .code-lang {
  display: block;
  padding: 0.35rem 0.75rem;
  font-size: 0.72rem;
  color: var(--text-muted);
  border-bottom: 1px solid var(--border);
  text-transform: lowercase;
}

.message-bubble .code-block.incomplete pre {
  border-bottom-left-radius: 0;
  border-bottom-right-radius: 0;
}

.message-bubble .code-block pre {
  background: transparent;
  padding: 0.75rem;
  margin: 0;
  overflow-x: auto;
  font-size: 0.85rem;
}

.message-bubble h1,
.message-bubble h2,
.message-bubble h3,
.message-bubble h4,
.message-bubble h5,
.message-bubble h6 {
  margin: 0.85rem 0 0.45rem;
  line-height: 1.3;
}

.message-bubble h1 { font-size: 1.25rem; }
.message-bubble h2 { font-size: 1.1rem; }
.message-bubble h3 { font-size: 1rem; }

.message-bubble ul,
.message-bubble ol {
  margin: 0.5rem 0;
  padding-left: 1.4rem;
}

.message-bubble li {
  margin: 0.25rem 0;
}

.message-bubble blockquote {
  margin: 0.6rem 0;
  padding: 0.35rem 0 0.35rem 0.85rem;
  border-left: 3px solid var(--accent);
  color: var(--text-muted);
}

.message-bubble hr {
  border: none;
  border-top: 1px solid var(--border);
  margin: 1rem 0;
}

.message-bubble a {
  color: var(--accent);
  text-decoration: underline;
  text-underline-offset: 2px;
}

.message-bubble .inline-code,
.message-bubble code {
  background: rgba(0, 0, 0, 0.3);
  padding: 0.1rem 0.35rem;
  border-radius: 4px;
  font-size: 0.88em;
}

.message-bubble pre code {
  background: none;
  padding: 0;
  font-size: inherit;
}

.composer {
  display: flex;
  gap: 0.5rem;
  align-items: flex-end;
  padding: 0.75rem 1rem 1rem;
  border-top: 1px solid var(--border);
  background: var(--bg-elevated);
  transition: outline-color 0.15s, background 0.15s;
}

.composer.drag-over {
  outline: 2px dashed var(--accent);
  outline-offset: -6px;
  background: rgba(91, 141, 239, 0.06);
}

.composer-main {
  flex: 1;
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
  min-width: 0;
}

.composer-input-row {
  display: flex;
  align-items: flex-end;
  gap: 0.45rem;
  min-width: 0;
}

.composer-input-row textarea {
  flex: 1;
  min-width: 0;
}

.composer-input-row .btn-attach {
  flex-shrink: 0;
  width: 40px;
  height: 40px;
  margin-bottom: 2px;
}

.attachment-preview {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
}

.attachment-preview-item {
  position: relative;
  border-radius: 8px;
  overflow: hidden;
  border: 1px solid var(--border);
  background: var(--bg);
}

.attachment-preview-item--image {
  width: 72px;
  height: 72px;
}

.attachment-preview-item--image img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  display: block;
}

.attachment-preview-item--doc {
  display: flex;
  align-items: center;
  gap: 0.4rem;
  padding: 0.45rem 0.55rem;
  min-width: 110px;
  max-width: 200px;
}

.attachment-preview-item--doc .icon-doc {
  flex-shrink: 0;
  color: var(--accent);
}

.attachment-preview-name {
  font-size: 0.78rem;
  color: var(--text);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.attachment-preview-remove {
  position: absolute;
  top: 2px;
  right: 2px;
  width: 22px;
  height: 22px;
  border: none;
  border-radius: 50%;
  background: rgba(0, 0, 0, 0.65);
  color: #fff;
  font-size: 1rem;
  line-height: 1;
  cursor: pointer;
}

.attachment-preview-item--doc .attachment-preview-remove {
  top: 4px;
  right: 4px;
}

.message-images {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
  margin-bottom: 0.5rem;
}

.message-images img {
  max-width: min(280px, 100%);
  max-height: 220px;
  border-radius: 8px;
  border: 1px solid var(--border);
  object-fit: contain;
  background: var(--bg);
}

.message-documents {
  display: flex;
  flex-wrap: wrap;
  gap: 0.4rem;
  margin-bottom: 0.5rem;
}

.message-document-chip {
  display: inline-flex;
  align-items: center;
  gap: 0.35rem;
  padding: 0.3rem 0.55rem;
  border-radius: 6px;
  border: 1px solid var(--border);
  background: var(--bg);
  font-size: 0.8rem;
  color: var(--text-muted);
}

.message-document-chip .icon-doc {
  color: var(--accent);
  flex-shrink: 0;
}

.message-text + .message-images,
.message-images + .message-text,
.message-documents + .message-text,
.message-text + .message-documents {
  margin-top: 0.35rem;
}

.btn-attach {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  color: var(--text-muted);
}

.btn-attach:hover:not(:disabled) {
  color: var(--accent);
}

.btn-attach:disabled {
  opacity: 0.45;
  cursor: not-allowed;
}

.icon-attach {
  display: block;
}

.composer textarea {
  resize: none;
  min-height: 44px;
  max-height: 160px;
  padding: 0.65rem 0.85rem;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--bg);
  color: var(--text);
  font: inherit;
  line-height: 1.4;
  width: 100%;
}

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

.composer-actions {
  display: flex;
  flex-direction: column;
  gap: 0.35rem;
  flex-shrink: 0;
}

.btn-send, .btn-stop, .btn-ghost, .btn-icon {
  font: inherit;
  cursor: pointer;
  border: none;
  border-radius: 8px;
  transition: background 0.15s;
  text-decoration: none;
}

.btn-send {
  padding: 0.55rem 1.1rem;
  background: var(--accent);
  color: #fff;
  font-weight: 600;
}

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

.btn-stop {
  padding: 0.45rem 0.85rem;
  background: rgba(232, 93, 93, 0.2);
  color: #ffb4b4;
  font-size: 0.85rem;
}

.btn-ghost {
  padding: 0.45rem 0.75rem;
  background: transparent;
  color: var(--text-muted);
  font-size: 0.85rem;
}

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

.btn-ghost:disabled { opacity: 0.4; cursor: not-allowed; }

.btn-icon {
  width: 2rem;
  height: 2rem;
  background: var(--bg-hover);
  color: var(--text);
  font-size: 1.2rem;
  line-height: 1;
}

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

.mobile-only { display: none; }

.modal-overlay {
  position: fixed;
  inset: 0;
  z-index: 200;
  align-items: center;
  justify-content: center;
  padding: 1rem;
  background: rgba(0, 0, 0, 0.6);
}

.modal-overlay:not(.is-closed) {
  display: flex;
}

.modal-overlay.is-closed,
.modal-overlay[hidden] {
  display: none !important;
  pointer-events: none !important;
  visibility: hidden !important;
}

.modal-body {
  width: 100%;
  max-width: 520px;
  max-height: calc(100vh - 2rem);
  overflow-y: auto;
  padding: 1.25rem 1.5rem 1.5rem;
  display: flex;
  flex-direction: column;
  gap: 0.75rem;
  border-radius: 12px;
  background: var(--bg-elevated);
  color: var(--text);
  box-shadow: 0 20px 60px rgba(0, 0, 0, 0.5);
}

.modal-body h2 { margin: 0; font-size: 1.1rem; }

.btn-prompt-enrich {
  align-self: flex-start;
  margin-top: 0.35rem;
  padding: 0.35rem 0.5rem;
  font-size: 0.82rem;
}

.prompt-refine-result-wrap textarea {
  background: var(--bg-hover);
}

.modal-hint {
  margin: 0;
  font-size: 0.8rem;
  color: var(--text-muted);
}

.modal-body label {
  display: flex;
  flex-direction: column;
  gap: 0.3rem;
  font-size: 0.85rem;
  color: var(--text-muted);
}

.modal-body input,
.modal-body textarea,
.modal-body select {
  padding: 0.5rem 0.65rem;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--bg);
  color: var(--text);
  font: inherit;
}

.field-note {
  font-size: 0.78rem;
  color: var(--accent);
  margin-top: 0.25rem;
}

.model-row {
  display: flex;
  gap: 0.4rem;
  align-items: center;
}

.model-row select { flex: 1; min-width: 0; }

.btn-refresh-models {
  flex-shrink: 0;
  width: 2.25rem;
  height: 2.25rem;
  font-size: 1.1rem;
}

.btn-refresh-models:disabled {
  opacity: 0.45;
  cursor: wait;
}

.modal-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
  margin-top: 0.5rem;
}

/* Login */
.login-page {
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 100vh;
  padding: 1.5rem;
}

.login-card {
  width: 100%;
  max-width: 400px;
  padding: 2rem;
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  border-radius: var(--radius);
}

.login-brand {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 0.75rem;
  margin-bottom: 0.5rem;
}

.login-mascot {
  width: 96px;
  height: 96px;
  border-radius: 20px;
  object-fit: cover;
  box-shadow: 0 8px 28px rgba(91, 141, 239, 0.25);
}

.login-card h1 {
  margin: 0;
  font-size: 1.35rem;
  text-align: center;
}

.login-subtitle {
  margin: 0 0 1.5rem;
  text-align: center;
  color: var(--text-muted);
  font-size: 0.9rem;
}

.login-form {
  display: flex;
  flex-direction: column;
  gap: 0.75rem;
}

.login-form label {
  font-size: 0.85rem;
  color: var(--text-muted);
}

.login-form input {
  padding: 0.55rem 0.75rem;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--bg);
  color: var(--text);
  font: inherit;
}

.login-submit { width: 100%; margin-top: 0.5rem; }

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

  .sidebar.open { transform: translateX(0); }
  .mobile-only { display: inline-flex; }
  .message.user { max-width: 90%; }
  .user-badge { display: none; }
}
