:root {
  --bg: #0b0f1a;
  --bg2: #121826;
  --panel: #161d2e;
  --border: #243049;
  --text: #e8eefc;
  --muted: #8b9bb8;
  --primary: #6c8cff;
  --primary-hover: #8aa3ff;
  --ai: #1a2338;
  --user: #243056;
  --danger: #ff6b8a;
  --ok: #3ddc97;
  --warn: #ffc857;
  --shadow: 0 20px 50px rgba(0, 0, 0, 0.45);
  --radius: 16px;
  --font: "Segoe UI", system-ui, -apple-system, sans-serif;
}

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

html, body {
  height: 100%;
  overflow: hidden;
}

body {
  font-family: var(--font);
  background: var(--bg);
  color: var(--text);
  line-height: 1.5;
}

a {
  color: var(--primary);
  text-decoration: none;
}
a:hover {
  color: var(--primary-hover);
  text-decoration: underline;
}

/* ===== Auth ===== */
.auth-page {
  min-height: 100%;
  height: 100%;
  overflow: auto;
  display: grid;
  place-items: center;
  padding: 24px;
  background:
    radial-gradient(ellipse at top, rgba(108, 140, 255, 0.18), transparent 50%),
    radial-gradient(ellipse at bottom right, rgba(61, 220, 151, 0.08), transparent 40%),
    var(--bg);
}

.auth-card {
  width: 100%;
  max-width: 400px;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 24px;
  padding: 36px 32px;
  box-shadow: var(--shadow);
}

.settings-card {
  max-width: 560px;
}

.logo {
  text-align: center;
  margin-bottom: 28px;
}

.logo-icon {
  width: 56px;
  height: 56px;
  margin: 0 auto 12px;
  display: grid;
  place-items: center;
  border-radius: 16px;
  background: linear-gradient(135deg, #6c8cff, #3ddc97);
  color: #0b0f1a;
  font-size: 26px;
  font-weight: 700;
}

.logo h1 {
  font-size: 28px;
  letter-spacing: -0.03em;
}

.logo p {
  color: var(--muted);
  margin-top: 6px;
  font-size: 14px;
}

.alert {
  background: rgba(255, 107, 138, 0.12);
  border: 1px solid rgba(255, 107, 138, 0.35);
  color: #ffb3c2;
  padding: 10px 14px;
  border-radius: 12px;
  margin-bottom: 16px;
  font-size: 14px;
}

.alert-ok {
  background: rgba(61, 220, 151, 0.12);
  border-color: rgba(61, 220, 151, 0.35);
  color: #9af0c8;
}

.status-box {
  border-radius: 12px;
  padding: 12px 14px;
  margin-bottom: 16px;
  font-size: 14px;
  border: 1px solid var(--border);
}

.status-box.ok {
  background: rgba(61, 220, 151, 0.1);
  border-color: rgba(61, 220, 151, 0.3);
  color: #9af0c8;
}

.status-box.warn {
  background: rgba(255, 200, 87, 0.1);
  border-color: rgba(255, 200, 87, 0.3);
  color: #ffe0a0;
}

.guide {
  background: var(--bg2);
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 14px 16px;
  margin-bottom: 18px;
  font-size: 14px;
}

.guide h3 {
  font-size: 15px;
  margin-bottom: 10px;
}

.guide ol {
  padding-left: 18px;
  color: var(--text);
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.guide .muted {
  margin-top: 12px;
  color: var(--muted);
  font-size: 13px;
}

.guide code {
  background: rgba(108, 140, 255, 0.15);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 1px 6px;
  font-size: 12px;
}

.tag {
  display: inline-block;
  margin-left: 6px;
  font-size: 11px;
  color: #0b0f1a;
  background: var(--ok);
  border-radius: 999px;
  padding: 1px 8px;
  font-weight: 700;
}

.auth-form {
  display: flex;
  flex-direction: column;
  gap: 14px;
}

.auth-form label {
  display: flex;
  flex-direction: column;
  gap: 6px;
  font-size: 13px;
  color: var(--muted);
}

.auth-form input,
.auth-form select {
  background: var(--bg2);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 12px 14px;
  color: var(--text);
  font-size: 15px;
  outline: none;
  transition: border-color 0.15s, box-shadow 0.15s;
  font-family: inherit;
}

.auth-form select {
  cursor: pointer;
  appearance: none;
  background-image: linear-gradient(45deg, transparent 50%, var(--muted) 50%),
    linear-gradient(135deg, var(--muted) 50%, transparent 50%);
  background-position: calc(100% - 18px) calc(50% - 3px), calc(100% - 12px) calc(50% - 3px);
  background-size: 6px 6px, 6px 6px;
  background-repeat: no-repeat;
  padding-right: 36px;
}

.auth-form select option {
  background: var(--bg2);
  color: var(--text);
}

.auth-form input:focus,
.auth-form select:focus {
  border-color: var(--primary);
  box-shadow: 0 0 0 3px rgba(108, 140, 255, 0.2);
}

.extra-keys {
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 10px 12px;
  background: var(--bg2);
}

.extra-keys summary {
  cursor: pointer;
  color: var(--muted);
  font-size: 13px;
  margin-bottom: 0;
  list-style: none;
}

.extra-keys summary::-webkit-details-marker {
  display: none;
}

.extra-keys[open] summary {
  margin-bottom: 12px;
  color: var(--text);
}

.extra-keys label {
  margin-bottom: 10px;
}

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  border: none;
  border-radius: 12px;
  padding: 12px 16px;
  font-size: 15px;
  font-weight: 600;
  cursor: pointer;
  transition: 0.15s ease;
  text-decoration: none;
}

.btn-primary {
  background: linear-gradient(135deg, #6c8cff, #5a7af0);
  color: white;
  margin-top: 6px;
}
.btn-primary:hover {
  filter: brightness(1.08);
  text-decoration: none;
  color: white;
}
.btn-primary:disabled {
  opacity: 0.6;
  cursor: wait;
}

.btn-ghost {
  background: transparent;
  color: var(--muted);
  border: 1px solid var(--border);
  width: 100%;
}
.btn-ghost:hover {
  background: var(--bg2);
  color: var(--text);
  text-decoration: none;
}

.auth-switch {
  text-align: center;
  margin-top: 22px;
  color: var(--muted);
  font-size: 14px;
}

/* ===== Chat layout ===== */
.chat-page {
  display: grid;
  grid-template-columns: 260px 1fr;
  height: 100%;
  max-height: 100%;
  overflow: hidden;
}

.sidebar {
  background: var(--panel);
  border-right: 1px solid var(--border);
  padding: 20px 16px;
  display: flex;
  flex-direction: column;
  gap: 20px;
  min-height: 0;
  overflow: auto;
}

.logo-mini {
  display: flex;
  align-items: center;
  gap: 10px;
  font-size: 18px;
}
.logo-mini .logo-icon {
  width: 36px;
  height: 36px;
  margin: 0;
  font-size: 16px;
  border-radius: 10px;
}

.user-badge {
  margin-top: 10px;
  font-size: 13px;
  color: var(--muted);
  background: var(--bg2);
  border: 1px solid var(--border);
  border-radius: 999px;
  padding: 6px 12px;
  display: inline-block;
}

.sidebar-actions {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.sidebar-hint {
  margin-top: auto;
  font-size: 12px;
  color: var(--muted);
  line-height: 1.45;
  background: var(--bg2);
  border-radius: 12px;
  padding: 12px;
  border: 1px solid var(--border);
}
.sidebar-hint p + p {
  margin-top: 8px;
}

.chat-main {
  display: grid;
  grid-template-rows: auto minmax(0, 1fr) auto;
  min-width: 0;
  min-height: 0;
  height: 100%;
  overflow: hidden;
  background:
    radial-gradient(ellipse at top right, rgba(108, 140, 255, 0.08), transparent 40%),
    var(--bg);
}

.chat-header {
  padding: 16px 24px;
  border-bottom: 1px solid var(--border);
  background: rgba(18, 24, 38, 0.8);
  backdrop-filter: blur(8px);
  flex-shrink: 0;
}

.chat-header h2 {
  font-size: 18px;
  font-weight: 650;
}

.status {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  color: var(--muted);
  font-size: 13px;
  margin-top: 2px;
}

.dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--ok);
  box-shadow: 0 0 0 3px rgba(61, 220, 151, 0.2);
  display: inline-block;
}

.dot-warn {
  background: var(--warn);
  box-shadow: 0 0 0 3px rgba(255, 200, 87, 0.2);
}

.messages {
  overflow-y: auto !important;
  overflow-x: hidden;
  padding: 24px;
  display: flex;
  flex-direction: column;
  gap: 14px;
  min-height: 0;
  height: 100%;
  -webkit-overflow-scrolling: touch;
  overscroll-behavior: contain;
  scroll-behavior: smooth;
}

.msg {
  display: flex;
  gap: 12px;
  max-width: 780px;
  width: 100%;
  animation: fadeIn 0.2s ease;
  flex-shrink: 0;
}

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

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

.avatar {
  width: 36px;
  height: 36px;
  border-radius: 12px;
  display: grid;
  place-items: center;
  flex-shrink: 0;
  font-weight: 700;
  font-size: 14px;
  background: var(--border);
  color: var(--text);
}

.msg.ai .avatar {
  background: linear-gradient(135deg, #6c8cff, #3ddc97);
  color: #0b0f1a;
}

.msg.user .avatar {
  background: #3a4d7a;
}

.bubble {
  background: var(--ai);
  border: 1px solid var(--border);
  border-radius: 16px;
  padding: 12px 14px;
  max-width: min(100%, 640px);
}

.msg.user .bubble {
  background: var(--user);
}

.bubble p {
  white-space: pre-wrap;
  word-break: break-word;
  font-size: 15px;
}

.bubble time {
  display: block;
  margin-top: 6px;
  font-size: 11px;
  color: var(--muted);
}

.msg.user .bubble time {
  text-align: right;
}

/* typing dots */
.typing .bubble p {
  display: flex;
  gap: 5px;
  padding: 4px 2px;
}
.typing .bubble span {
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: var(--muted);
  animation: bounce 1.2s infinite ease-in-out;
}
.typing .bubble span:nth-child(2) { animation-delay: 0.15s; }
.typing .bubble span:nth-child(3) { animation-delay: 0.3s; }

@keyframes bounce {
  0%, 80%, 100% { transform: translateY(0); opacity: 0.4; }
  40% { transform: translateY(-5px); opacity: 1; }
}

.composer {
  display: flex;
  gap: 12px;
  padding: 16px 24px 20px;
  border-top: 1px solid var(--border);
  background: rgba(18, 24, 38, 0.92);
  backdrop-filter: blur(8px);
  flex-shrink: 0;
}

.composer textarea {
  flex: 1;
  resize: none;
  min-height: 48px;
  max-height: 140px;
  background: var(--bg2);
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 12px 14px;
  color: var(--text);
  font: inherit;
  outline: none;
  transition: border-color 0.15s, box-shadow 0.15s;
}

.composer textarea:focus {
  border-color: var(--primary);
  box-shadow: 0 0 0 3px rgba(108, 140, 255, 0.18);
}

.composer .btn-primary {
  margin-top: 0;
  min-width: 120px;
  height: 48px;
  align-self: flex-end;
}

/* Mobile */
@media (max-width: 800px) {
  .chat-page {
    grid-template-columns: 1fr;
    grid-template-rows: auto minmax(0, 1fr);
  }
  .sidebar {
    flex-direction: row;
    align-items: center;
    justify-content: space-between;
    gap: 12px;
    padding: 12px 14px;
    border-right: none;
    border-bottom: 1px solid var(--border);
    overflow: visible;
  }
  .sidebar-top {
    display: flex;
    align-items: center;
    gap: 10px;
  }
  .user-badge {
    margin-top: 0;
  }
  .sidebar-actions {
    flex-direction: row;
  }
  .sidebar-actions .btn-ghost {
    width: auto;
    padding: 8px 12px;
    font-size: 13px;
  }
  .sidebar-hint {
    display: none;
  }
  .chat-main {
    min-height: 0;
    height: auto;
  }
  .messages {
    padding: 16px;
  }
  .composer {
    padding: 12px;
  }
  .settings-card {
    max-width: 100%;
  }
}
