/* ==========================================================================
   Haorui AI — design tokens
   ========================================================================== */
:root {
  --bg: #0a0a0c;
  --bg-soft: #0e0e11;
  --bg-elevated: #141417;

  --glass: rgba(255, 255, 255, 0.045);
  --glass-strong: rgba(255, 255, 255, 0.075);
  --glass-border: rgba(255, 255, 255, 0.09);
  --glass-border-strong: rgba(255, 255, 255, 0.14);

  --text: #ece8e4;
  --text-dim: #a29c96;
  --text-faint: #66615c;

  --accent: #e76f51;
  --accent-soft: rgba(231, 111, 81, 0.16);
  --accent-border: rgba(231, 111, 81, 0.4);
  --accent-2: #8f6fe7;
  --accent-3: #4fc6d9;

  --danger: #d9534f;

  --radius-lg: 20px;
  --radius-md: 14px;
  --radius-sm: 9px;

  --font-display: 'Space Grotesk', 'Inter', sans-serif;
  --font-body: 'Inter', -apple-system, BlinkMacSystemFont, sans-serif;
  --font-mono: 'JetBrains Mono', ui-monospace, 'SFMono-Regular', Menlo, monospace;

  --shadow-soft: 0 8px 30px rgba(0, 0, 0, 0.35);
  --shadow-deep: 0 20px 60px rgba(0, 0, 0, 0.55);
}

* { box-sizing: border-box; }

html, body {
  height: 100%;
}

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: var(--font-body);
  -webkit-font-smoothing: antialiased;
  overflow: hidden;
}

/* Ambient background: faint aurora wash, fixed, behind everything */
body::before {
  content: "";
  position: fixed;
  inset: 0;
  z-index: 0;
  pointer-events: none;
  background:
    radial-gradient(600px 380px at 8% -6%, rgba(231, 111, 81, 0.10), transparent 60%),
    radial-gradient(500px 340px at 100% 8%, rgba(143, 111, 231, 0.08), transparent 60%),
    radial-gradient(700px 460px at 50% 110%, rgba(79, 198, 217, 0.06), transparent 60%);
}

button, input, textarea, select { font-family: inherit; color: inherit; }
button { cursor: pointer; }

a { color: var(--accent); }

::selection { background: rgba(231, 111, 81, 0.35); }

/* Scrollbars */
* { scrollbar-width: thin; scrollbar-color: rgba(255,255,255,0.14) transparent; }
*::-webkit-scrollbar { width: 8px; height: 8px; }
*::-webkit-scrollbar-thumb { background: rgba(255,255,255,0.12); border-radius: 8px; }
*::-webkit-scrollbar-thumb:hover { background: rgba(255,255,255,0.2); }

/* ==========================================================================
   Layout
   ========================================================================== */
.app {
  position: relative;
  z-index: 1;
  display: grid;
  grid-template-columns: 272px 1fr;
  grid-template-rows: minmax(0, 1fr);
  height: 100vh;
  height: 100dvh;
  overflow: hidden;
}

/* ==========================================================================
   Sidebar
   ========================================================================== */
.sidebar {
  position: relative;
  z-index: 20;
  display: flex;
  flex-direction: column;
  gap: 14px;
  min-height: 0;
  height: 100%;
  padding: 18px 14px;
  background: rgba(15, 15, 18, 0.6);
  backdrop-filter: blur(24px) saturate(140%);
  -webkit-backdrop-filter: blur(24px) saturate(140%);
  border-right: 1px solid var(--glass-border);
}

.sidebar-top {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 4px 6px 2px;
}

.brand {
  display: flex;
  align-items: center;
  gap: 10px;
  font-family: var(--font-display);
  font-weight: 600;
  font-size: 17px;
  letter-spacing: -0.01em;
}

.brand-orb {
  width: 22px;
  height: 22px;
  border-radius: 50%;
  flex: none;
  background: conic-gradient(from 200deg, var(--accent), var(--accent-2), var(--accent-3), var(--accent));
  box-shadow: 0 0 14px rgba(231, 111, 81, 0.55);
  animation: orb-spin 8s linear infinite;
}

.brand-ai { color: var(--accent); }

.new-chat-btn {
  display: flex;
  align-items: center;
  gap: 8px;
  justify-content: center;
  width: 100%;
  padding: 11px 14px;
  border-radius: var(--radius-md);
  border: 1px solid var(--glass-border-strong);
  background: var(--glass-strong);
  backdrop-filter: blur(10px);
  color: var(--text);
  font-size: 13.5px;
  font-weight: 500;
  transition: border-color 0.15s ease, background 0.15s ease, transform 0.1s ease;
}
.new-chat-btn:hover {
  border-color: var(--accent-border);
  background: rgba(231, 111, 81, 0.08);
}
.new-chat-btn:active { transform: scale(0.98); }

.chat-list {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 2px;
  padding-right: 2px;
  margin-right: -2px;
}

.chat-item {
  position: relative;
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 9px 10px;
  border-radius: var(--radius-sm);
  font-size: 13.5px;
  color: var(--text-dim);
  border: 1px solid transparent;
  cursor: pointer;
  overflow: hidden;
}

.chat-item span.chat-item-title {
  flex: 1;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

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

.chat-item.active {
  background: var(--accent-soft);
  border-color: var(--accent-border);
  color: var(--text);
}

.chat-item.active::before {
  content: "";
  position: absolute;
  left: 0; top: 0; bottom: 0;
  width: 2px;
  background: linear-gradient(180deg, var(--accent), var(--accent-2));
  box-shadow: 0 0 8px var(--accent);
}

.chat-item-delete {
  opacity: 0;
  flex: none;
  width: 22px;
  height: 22px;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 6px;
  color: var(--text-faint);
  transition: opacity 0.12s ease, color 0.12s ease, background 0.12s ease;
}
.chat-item:hover .chat-item-delete { opacity: 1; }
.chat-item-delete:hover { color: var(--danger); background: rgba(217, 83, 79, 0.14); }

.chat-list-empty {
  color: var(--text-faint);
  font-size: 12.5px;
  padding: 10px;
  line-height: 1.5;
}

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

.settings-entry {
  display: flex;
  align-items: center;
  gap: 9px;
  width: 100%;
  padding: 9px 10px;
  border-radius: var(--radius-sm);
  background: transparent;
  border: 1px solid transparent;
  color: var(--text-dim);
  font-size: 13px;
}
.settings-entry:hover {
  background: var(--glass);
  color: var(--text);
}

/* ==========================================================================
   Main column
   ========================================================================== */
.main {
  position: relative;
  display: flex;
  flex-direction: column;
  min-width: 0;
  min-height: 0;
  height: 100%;
}

.topbar {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 14px 22px;
  border-bottom: 1px solid var(--glass-border);
  background: rgba(10, 10, 12, 0.45);
  backdrop-filter: blur(18px);
  -webkit-backdrop-filter: blur(18px);
  flex: none;
}

.chat-title {
  flex: 1;
  margin: 0;
  font-family: var(--font-display);
  font-size: 15px;
  font-weight: 500;
  color: var(--text-dim);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.icon-btn {
  flex: none;
  display: flex;
  align-items: center;
  justify-content: center;
  width: 34px;
  height: 34px;
  border-radius: 10px;
  border: 1px solid transparent;
  background: transparent;
  color: var(--text-dim);
  transition: background 0.12s ease, color 0.12s ease, border-color 0.12s ease;
}
.icon-btn:hover {
  background: var(--glass);
  color: var(--text);
  border-color: var(--glass-border);
}
.icon-btn.ghost { background: var(--glass); border-color: var(--glass-border); }

.mobile-only { display: none; }

.token-counter {
  flex: none;
  padding: 7px 13px;
  border-radius: 999px;
  border: 1px solid var(--glass-border);
  background: var(--glass);
  color: var(--text-dim);
  font-family: var(--font-mono);
  font-size: 12px;
  letter-spacing: 0.01em;
  white-space: nowrap;
}

/* ==========================================================================
   Messages
   ========================================================================== */
.messages {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  padding: 28px 0 12px;
}

.messages-inner,
.message-row {
  max-width: 760px;
  margin: 0 auto;
  padding: 0 24px;
}

.empty-state {
  height: 100%;
  min-height: 300px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  text-align: center;
  gap: 10px;
  color: var(--text-dim);
  padding: 0 24px;
}

.empty-orb {
  width: 52px;
  height: 52px;
  border-radius: 50%;
  margin-bottom: 6px;
  background: conic-gradient(from 200deg, var(--accent), var(--accent-2), var(--accent-3), var(--accent));
  box-shadow: 0 0 40px rgba(231, 111, 81, 0.35);
  animation: orb-spin 8s linear infinite;
}

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

.message-row {
  display: flex;
  margin-bottom: 20px;
}

.message-row.user { justify-content: flex-end; }
.message-row.model { justify-content: flex-start; }

.bubble {
  max-width: 82%;
  padding: 13px 16px;
  border-radius: var(--radius-md);
  font-size: 14.5px;
  line-height: 1.6;
  word-wrap: break-word;
}

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

.message-row.model .bubble {
  background: var(--glass);
  border: 1px solid var(--glass-border);
  backdrop-filter: blur(10px);
  border-bottom-left-radius: 4px;
}

.message-row.error .bubble {
  background: rgba(217, 83, 79, 0.1);
  border: 1px solid rgba(217, 83, 79, 0.35);
  color: #f0a8a5;
}

.bubble p { margin: 0 0 10px; }
.bubble p:last-child { margin-bottom: 0; }
.bubble ul, .bubble ol { margin: 0 0 10px; padding-left: 20px; }
.bubble li { margin-bottom: 4px; }
.bubble a { text-decoration: underline; text-underline-offset: 2px; }
.bubble h1, .bubble h2, .bubble h3 {
  font-family: var(--font-display);
  margin: 14px 0 8px;
  line-height: 1.3;
}
.bubble h1:first-child, .bubble h2:first-child, .bubble h3:first-child { margin-top: 0; }
.bubble blockquote {
  margin: 8px 0;
  padding: 4px 12px;
  border-left: 2px solid var(--accent-border);
  color: var(--text-dim);
}
.bubble code:not(pre code) {
  font-family: var(--font-mono);
  font-size: 0.86em;
  background: rgba(255, 255, 255, 0.08);
  border: 1px solid rgba(255, 255, 255, 0.08);
  padding: 1px 5px;
  border-radius: 5px;
}
.bubble table {
  border-collapse: collapse;
  width: 100%;
  margin: 8px 0;
  font-size: 13px;
}
.bubble th, .bubble td {
  border: 1px solid var(--glass-border);
  padding: 6px 10px;
  text-align: left;
}
.bubble th { background: rgba(255,255,255,0.04); }

/* typing indicator */
.typing-dots {
  display: inline-flex;
  gap: 4px;
  padding: 4px 2px;
}
.typing-dots span {
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: var(--accent);
  opacity: 0.5;
  animation: typing-bounce 1.1s ease-in-out infinite;
}
.typing-dots span:nth-child(2) { animation-delay: 0.15s; background: var(--accent-2); }
.typing-dots span:nth-child(3) { animation-delay: 0.3s; background: var(--accent-3); }

@keyframes typing-bounce {
  0%, 60%, 100% { transform: translateY(0); opacity: 0.4; }
  30% { transform: translateY(-4px); opacity: 1; }
}

/* ==========================================================================
   Code blocks
   ========================================================================== */
.code-block {
  position: relative;
  margin: 10px 0;
  border-radius: var(--radius-sm);
  overflow: hidden;
  border: 1px solid var(--glass-border);
  background: #0d0d10;
}

.code-block-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 7px 12px;
  background: rgba(255, 255, 255, 0.04);
  border-bottom: 1px solid var(--glass-border);
  font-family: var(--font-mono);
  font-size: 11.5px;
  color: var(--text-faint);
  text-transform: lowercase;
}

.copy-btn {
  display: flex;
  align-items: center;
  gap: 5px;
  padding: 4px 9px;
  border-radius: 6px;
  border: 1px solid var(--glass-border);
  background: var(--glass);
  color: var(--text-dim);
  font-size: 11.5px;
  font-family: var(--font-body);
  transition: color 0.12s ease, border-color 0.12s ease, background 0.12s ease;
}
.copy-btn:hover { color: var(--text); border-color: var(--accent-border); background: var(--accent-soft); }
.copy-btn.copied { color: var(--accent); border-color: var(--accent-border); }

.code-block pre {
  margin: 0;
  padding: 14px 16px;
  overflow-x: auto;
}
.code-block code {
  font-family: var(--font-mono);
  font-size: 13px;
  line-height: 1.55;
  color: #e3e1de;
  background: none;
  white-space: pre;
}

/* ==========================================================================
   Composer
   ========================================================================== */
.composer-region {
  flex: none;
  padding: 14px 24px 18px;
}

.composer {
  position: relative;
  max-width: 760px;
  margin: 0 auto;
  display: flex;
  align-items: flex-end;
  gap: 8px;
  padding: 10px 10px 10px 18px;
  border-radius: var(--radius-lg);
  background: #060505;
  border: 1px solid rgba(231, 111, 81, 0.4);
}

.composer-glow {
  position: absolute;
  inset: -2px;
  z-index: -1;
  border-radius: inherit;
  background: var(--accent);
  filter: blur(18px);
  opacity: 0.45;
  animation: glow-pulse 3.2s ease-in-out infinite;
  transition: opacity 0.2s ease;
}
.composer:focus-within .composer-glow { opacity: 0.8; animation-play-state: paused; }

.composer-input {
  flex: 1;
  resize: none;
  max-height: 200px;
  border: none;
  outline: none;
  background: transparent;
  color: var(--text);
  font-size: 14.5px;
  line-height: 1.5;
  padding: 6px 4px;
}
.composer-input::placeholder { color: var(--text-faint); }

.send-btn {
  flex: none;
  width: 36px;
  height: 36px;
  border-radius: 50%;
  border: none;
  display: flex;
  align-items: center;
  justify-content: center;
  background: linear-gradient(135deg, var(--accent), #d1553a);
  color: #fff;
  box-shadow: 0 4px 16px rgba(231, 111, 81, 0.35);
  transition: transform 0.1s ease, opacity 0.15s ease;
}
.send-btn:hover:not(:disabled) { transform: scale(1.05); }
.send-btn:active:not(:disabled) { transform: scale(0.95); }
.send-btn:disabled {
  background: var(--glass-strong);
  color: var(--text-faint);
  box-shadow: none;
  cursor: not-allowed;
}

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

/* ==========================================================================
   Modals
   ========================================================================== */
.modal-backdrop {
  position: fixed;
  inset: 0;
  z-index: 100;
  display: none;
  align-items: center;
  justify-content: center;
  padding: 20px;
  background: rgba(6, 6, 8, 0.65);
  backdrop-filter: blur(6px);
}
.modal-backdrop.open { display: flex; }

.modal {
  position: relative;
  width: 100%;
  max-width: 420px;
  padding: 30px 28px 26px;
  border-radius: var(--radius-lg);
  background: rgba(20, 20, 24, 0.85);
  border: 1px solid var(--glass-border-strong);
  backdrop-filter: blur(30px) saturate(160%);
  -webkit-backdrop-filter: blur(30px) saturate(160%);
  box-shadow: var(--shadow-deep);
}

.modal-orb {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  margin-bottom: 14px;
  background: conic-gradient(from 200deg, var(--accent), var(--accent-2), var(--accent-3), var(--accent));
  box-shadow: 0 0 30px rgba(231, 111, 81, 0.4);
  animation: orb-spin 8s linear infinite;
}

.modal h2 {
  font-family: var(--font-display);
  font-size: 19px;
  margin: 0 0 8px;
}

.modal-copy {
  font-size: 13.5px;
  line-height: 1.55;
  color: var(--text-dim);
  margin: 0 0 20px;
}

.modal-header-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 18px;
}
.modal-header-row h2 { margin: 0; }

.field-label {
  display: block;
  font-size: 12px;
  font-weight: 500;
  color: var(--text-dim);
  margin: 16px 0 7px;
  text-transform: uppercase;
  letter-spacing: 0.04em;
}
.field-label:first-of-type { margin-top: 0; }

.key-input-row {
  position: relative;
  display: flex;
  align-items: center;
  border-radius: var(--radius-sm);
  border: 1px solid var(--glass-border-strong);
  background: rgba(0, 0, 0, 0.25);
  overflow: hidden;
}
.key-input-row:focus-within {
  border-color: var(--accent-border);
  box-shadow: 0 0 0 3px rgba(231, 111, 81, 0.14);
}
.key-input-row input {
  flex: 1;
  border: none;
  outline: none;
  background: transparent;
  padding: 11px 12px;
  font-family: var(--font-mono);
  font-size: 13px;
  color: var(--text);
  min-width: 0;
}
.key-input-row .icon-btn { margin-right: 4px; }

select#modelSelect {
  width: 100%;
  padding: 11px 12px;
  border-radius: var(--radius-sm);
  border: 1px solid var(--glass-border-strong);
  background: rgba(0, 0, 0, 0.25);
  color: var(--text);
  font-size: 13.5px;
  outline: none;
  appearance: none;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='14' height='14' viewBox='0 0 24 24' fill='none' stroke='%23a29c96' stroke-width='2'%3E%3Cpath d='M6 9l6 6 6-6'/%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: right 12px center;
}
select#modelSelect:focus { border-color: var(--accent-border); }

.modal-link {
  display: inline-block;
  margin-top: 9px;
  font-size: 12.5px;
  color: var(--accent);
  text-decoration: none;
}
.modal-link:hover { text-decoration: underline; }

.primary-btn {
  width: 100%;
  margin-top: 22px;
  padding: 12px 16px;
  border: none;
  border-radius: var(--radius-sm);
  background: linear-gradient(135deg, var(--accent), #d1553a);
  color: #fff;
  font-size: 14px;
  font-weight: 600;
  box-shadow: 0 6px 20px rgba(231, 111, 81, 0.3);
  transition: transform 0.1s ease, box-shadow 0.15s ease;
}
.primary-btn:hover { box-shadow: 0 8px 26px rgba(231, 111, 81, 0.42); }
.primary-btn:active { transform: scale(0.99); }

.danger-btn {
  width: 100%;
  margin-top: 10px;
  padding: 11px 16px;
  border-radius: var(--radius-sm);
  border: 1px solid rgba(217, 83, 79, 0.3);
  background: rgba(217, 83, 79, 0.08);
  color: #e88985;
  font-size: 13.5px;
  font-weight: 500;
  transition: background 0.12s ease;
}
.danger-btn:hover { background: rgba(217, 83, 79, 0.16); }

.settings-actions { display: flex; flex-direction: column; }

.modal-error, .modal-success {
  margin: 12px 0 0;
  font-size: 12.5px;
  min-height: 1px;
}
.modal-error { color: #e88985; }
.modal-success { color: #7fd9b6; }

/* ==========================================================================
   Overlay (mobile sidebar backdrop)
   ========================================================================== */
.overlay {
  display: none;
  position: fixed;
  inset: 0;
  z-index: 15;
  background: rgba(0, 0, 0, 0.5);
  backdrop-filter: blur(2px);
}
.overlay.open { display: block; }

/* ==========================================================================
   Animations
   ========================================================================== */
@keyframes orb-spin {
  to { filter: hue-rotate(360deg); }
}

@keyframes glow-pulse {
  0%, 100% { opacity: 0.4; }
  50% { opacity: 0.6; }
}

@media (prefers-reduced-motion: reduce) {
  .brand-orb, .empty-orb, .modal-orb, .composer-glow, .typing-dots span {
    animation: none !important;
  }
}

/* ==========================================================================
   Responsive
   ========================================================================== */
@media (max-width: 860px) {
  .app { grid-template-columns: 1fr; }

  .sidebar {
    position: fixed;
    inset: 0 auto 0 0;
    width: 82%;
    max-width: 300px;
    transform: translateX(-100%);
    transition: transform 0.22s ease;
  }
  .sidebar.open { transform: translateX(0); }

  .mobile-only { display: flex; }

  .message-row .bubble { max-width: 88%; }
}

@media (max-width: 520px) {
  .messages-inner, .message-row { padding: 0 14px; }
  .composer-region { padding: 10px 12px 14px; }
  .empty-state h2 { font-size: 19px; }
}
