/* ========== CSS Variables ========== */
:root {
  --primary: #4F46E5;
  --primary-hover: #4338CA;
  --primary-light: #EEF2FF;
  --bg: #F7F8FC;
  --bg-card: #FFFFFF;
  --bg-input: #FFFFFF;
  --border: #E5E7EB;
  --border-light: #F3F4F6;
  --dark: #1F2329;
  --muted: #6B7280;
  --muted-light: #9CA3AF;
  --shadow-sm: 0 1px 2px rgba(0,0,0,0.05);
  --shadow-md: 0 4px 12px rgba(0,0,0,0.08);
  --shadow-lg: 0 8px 30px rgba(0,0,0,0.12);
  --radius-sm: 8px;
  --radius-md: 14px;
  --radius-lg: 20px;
  --radius-full: 999px;
  --transition: 0.2s ease;
}

[data-theme="dark"] {
  --primary: #818CF8;
  --primary-hover: #6366F1;
  --primary-light: #1E1B4B;
  --bg: #111318;
  --bg-card: #1C1F26;
  --bg-input: #1C1F26;
  --border: #2D3139;
  --border-light: #23262D;
  --dark: #F0F1F4;
  --muted: #9CA3AF;
  --muted-light: #6B7280;
  --shadow-sm: 0 1px 2px rgba(0,0,0,0.3);
  --shadow-md: 0 4px 12px rgba(0,0,0,0.4);
  --shadow-lg: 0 8px 30px rgba(0,0,0,0.5);
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --primary: #818CF8;
    --primary-hover: #6366F1;
    --primary-light: #1E1B4B;
    --bg: #111318;
    --bg-card: #1C1F26;
    --bg-input: #1C1F26;
    --border: #2D3139;
    --border-light: #23262D;
    --dark: #F0F1F4;
    --muted: #9CA3AF;
    --muted-light: #6B7280;
    --shadow-sm: 0 1px 2px rgba(0,0,0,0.3);
    --shadow-md: 0 4px 12px rgba(0,0,0,0.4);
    --shadow-lg: 0 8px 30px rgba(0,0,0,0.5);
  }
}

/* ========== Reset & Base ========== */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang SC", "Microsoft YaHei", sans-serif;
  background: var(--bg);
  color: var(--dark);
  line-height: 1.6;
  -webkit-font-smoothing: antialiased;
}

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

.container { max-width: 760px; margin: 0 auto; padding: 0 20px; }

/* ========== Header ========== */
.site-header {
  background: var(--bg-card);
  border-bottom: 1px solid var(--border);
  position: sticky;
  top: 0;
  z-index: 100;
  backdrop-filter: blur(12px);
}

.header-inner {
  display: flex;
  align-items: center;
  gap: 14px;
  padding: 14px 20px;
}

.brand {
  font-size: 18px;
  font-weight: 700;
  color: var(--dark);
  text-decoration: none;
}
.brand:hover { text-decoration: none; opacity: 0.85; }

.tagline { color: var(--muted); font-size: 13px; }

.current-project {
  color: var(--primary);
  font-weight: 600;
  font-size: 13px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  max-width: 200px;
}

/* ========== Navigation ========== */
.nav-links {
  display: flex;
  gap: 6px;
  margin-left: auto;
  align-items: center;
}

.nav-link {
  text-decoration: none;
  color: var(--muted);
  font-size: 14px;
  padding: 6px 12px;
  border-radius: var(--radius-full);
  transition: all var(--transition);
}

.nav-link:hover { color: var(--primary); background: var(--primary-light); text-decoration: none; }
.nav-link.active { color: var(--primary); font-weight: 600; background: var(--primary-light); }

#themeToggle {
  width: 36px;
  height: 36px;
  border: 1px solid var(--border);
  background: var(--bg-card);
  border-radius: var(--radius-full);
  cursor: pointer;
  font-size: 16px;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: all var(--transition);
  flex-shrink: 0;
  line-height: 1;
}

#themeToggle:hover { border-color: var(--primary); background: var(--primary-light); }

/* ========== Chat Layout ========== */
.chat-container {
  display: flex;
  flex-direction: column;
  height: calc(100vh - 57px);
  max-width: 760px;
  margin: 0 auto;
  padding: 0 20px;
  position: relative;
}

.messages {
  flex: 1;
  display: flex;
  flex-direction: column;
  gap: 14px;
  overflow-y: auto;
  padding: 20px 4px 8px;
  scroll-behavior: smooth;
}

/* Scrollbar */
.messages::-webkit-scrollbar { width: 6px; }
.messages::-webkit-scrollbar-track { background: transparent; }
.messages::-webkit-scrollbar-thumb { background: var(--border); border-radius: 3px; }
.messages::-webkit-scrollbar-thumb:hover { background: var(--muted-light); }

/* ========== Message Bubble ========== */
.msg { display: flex; animation: msgIn 0.3s ease; }
.msg.user { justify-content: flex-end; }
.msg.assistant { justify-content: flex-start; }

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

.bubble-wrap { max-width: 86%; position: relative; }

.bubble {
  padding: 12px 16px;
  border-radius: var(--radius-md);
  white-space: pre-wrap;
  word-break: break-word;
  font-size: 14px;
  line-height: 1.65;
  position: relative;
}

.msg.user .bubble {
  background: var(--primary);
  color: #fff;
  border-bottom-right-radius: 4px;
  box-shadow: 0 2px 8px rgba(79,70,229,0.25);
}

.msg.assistant .bubble {
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-bottom-left-radius: 4px;
  box-shadow: var(--shadow-sm);
}

[data-theme="dark"] .msg.user .bubble {
  box-shadow: 0 2px 8px rgba(129,140,248,0.2);
}

.msg.assistant .bubble a { color: var(--primary); }

/* Timestamp */
.msg-time {
  font-size: 11px;
  color: var(--muted-light);
  margin-top: 4px;
  padding: 0 4px;
  opacity: 0;
  transition: opacity var(--transition);
}
.msg:hover .msg-time { opacity: 1; }
.msg.user .msg-time { text-align: right; }

/* Copy button */
.msg-copy {
  position: absolute;
  top: 6px;
  right: 6px;
  width: 28px;
  height: 28px;
  border: none;
  background: var(--bg);
  color: var(--muted);
  border-radius: var(--radius-sm);
  cursor: pointer;
  font-size: 13px;
  display: flex;
  align-items: center;
  justify-content: center;
  opacity: 0;
  transition: all var(--transition);
  box-shadow: var(--shadow-sm);
}
.bubble-wrap:hover .msg-copy { opacity: 1; }
.msg-copy:hover { background: var(--primary-light); color: var(--primary); }
.msg-copy.copied { color: #059669; }

/* Retry button */
.msg-retry {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  margin-top: 8px;
  padding: 4px 12px;
  border: 1px solid var(--border);
  background: var(--bg-card);
  color: var(--muted);
  border-radius: var(--radius-full);
  cursor: pointer;
  font-size: 12px;
  transition: all var(--transition);
}
.msg-retry:hover { border-color: var(--primary); color: var(--primary); }

/* ========== Loading Spinner ========== */
.spinner {
  display: inline-flex;
  gap: 4px;
  align-items: center;
  padding: 4px 0;
}
.spinner i {
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: var(--muted-light);
  animation: dotBounce 1.2s ease-in-out infinite;
}
.spinner i:nth-child(2) { animation-delay: 0.15s; }
.spinner i:nth-child(3) { animation-delay: 0.3s; }

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

/* ========== Scroll to Bottom ========== */
.scroll-bottom {
  position: absolute;
  bottom: 130px;
  left: 50%;
  transform: translateX(-50%);
  width: 36px;
  height: 36px;
  border-radius: var(--radius-full);
  border: 1px solid var(--border);
  background: var(--bg-card);
  color: var(--muted);
  font-size: 16px;
  cursor: pointer;
  box-shadow: var(--shadow-md);
  z-index: 10;
  opacity: 0;
  pointer-events: none;
  transition: all var(--transition);
}

.scroll-bottom.visible {
  opacity: 1;
  pointer-events: auto;
}

.scroll-bottom:hover {
  border-color: var(--primary);
  color: var(--primary);
  background: var(--primary-light);
}

/* ========== Chat Form ========== */
.chat-form-area {
  flex-shrink: 0;
  padding: 12px 0 16px;
}

.chat-hint {
  text-align: center;
  font-size: 12px;
  color: var(--muted-light);
  margin-top: 8px;
}

.chat-form {
  display: flex;
  gap: 10px;
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  padding: 8px;
  transition: border-color var(--transition), box-shadow var(--transition);
}

.chat-form:focus-within {
  border-color: var(--primary);
  box-shadow: 0 0 0 3px rgba(79,70,229,0.1);
}

[data-theme="dark"] .chat-form:focus-within {
  box-shadow: 0 0 0 3px rgba(129,140,248,0.15);
}

.chat-input {
  flex: 1;
  border: none;
  outline: none;
  padding: 8px 12px;
  font-size: 14px;
  background: transparent;
  color: var(--dark);
}

.chat-input::placeholder { color: var(--muted-light); }

/* ========== Buttons ========== */
.btn {
  border: none;
  background: var(--primary);
  color: #fff;
  border-radius: var(--radius-full);
  padding: 10px 22px;
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
  transition: all var(--transition);
  white-space: nowrap;
}

.btn:hover { background: var(--primary-hover); transform: translateY(-1px); box-shadow: var(--shadow-md); }
.btn:active { transform: translateY(0); }
.btn:disabled { opacity: 0.5; cursor: default; transform: none; box-shadow: none; }

.btn.ghost {
  background: var(--bg-card);
  color: var(--muted);
  border: 1px solid var(--border);
}
.btn.ghost:hover { border-color: var(--primary); color: var(--primary); background: var(--primary-light); }

/* ========== Modal ========== */
.modal {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.45);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 200;
  animation: fadeIn 0.2s ease;
}

.modal.hidden { display: none; }

@keyframes fadeIn {
  from { opacity: 0; }
  to { opacity: 1; }
}

.modal-card {
  background: var(--bg-card);
  border-radius: var(--radius-lg);
  padding: 28px;
  width: 340px;
  box-shadow: var(--shadow-lg);
  animation: slideUp 0.25s ease;
}

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

.modal-card h3 { margin: 0 0 16px; font-size: 18px; }

.token-input {
  width: 100%;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 10px 12px;
  font-size: 14px;
  background: var(--bg-input);
  color: var(--dark);
  outline: none;
  transition: border-color var(--transition);
}
.token-input:focus { border-color: var(--primary); }

.modal-actions {
  display: flex;
  justify-content: flex-end;
  gap: 10px;
  margin-top: 18px;
}

/* ========== Apps Page ========== */
.apps-wrap { padding: 28px 0 48px; }

.apps-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 24px;
}

.apps-head h1 { font-size: 24px; font-weight: 700; }

.apps-list { display: flex; flex-direction: column; gap: 14px; }

.app-card {
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  padding: 20px;
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 16px;
  flex-wrap: wrap;
  transition: all var(--transition);
  animation: msgIn 0.3s ease;
}

.app-card:hover { box-shadow: var(--shadow-md); border-color: var(--primary); }

.app-title { font-size: 17px; font-weight: 600; }
.app-meta { color: var(--muted); font-size: 13px; margin-top: 4px; }
.app-url { margin-top: 8px; font-size: 14px; }
.app-url a { color: var(--primary); text-decoration: none; }
.app-url a:hover { text-decoration: underline; }

.app-side {
  display: flex;
  flex-direction: column;
  align-items: flex-end;
  gap: 10px;
}

.badge {
  font-size: 12px;
  padding: 4px 12px;
  border-radius: var(--radius-full);
  font-weight: 500;
}

.badge.running { background: #ECFDF5; color: #059669; }
.badge.stopped { background: var(--border-light); color: var(--muted); }
.badge.down { background: #FEF2F2; color: #DC2626; }

[data-theme="dark"] .badge.running { background: #064E3B; color: #34D399; }
[data-theme="dark"] .badge.stopped { background: #374151; color: #9CA3AF; }
[data-theme="dark"] .badge.down { background: #7F1D1D; color: #FCA5A5; }

.app-actions { display: flex; gap: 8px; flex-wrap: wrap; justify-content: flex-end; }

.act-btn {
  border: 1px solid var(--border);
  background: var(--bg-card);
  color: var(--dark);
  border-radius: var(--radius-sm);
  padding: 6px 14px;
  font-size: 13px;
  cursor: pointer;
  transition: all var(--transition);
  white-space: nowrap;
}

.act-btn:hover { border-color: var(--primary); color: var(--primary); }
.act-btn.primary { background: var(--primary); border-color: var(--primary); color: #fff; }
.act-btn.primary:hover { background: var(--primary-hover); }
.act-btn.warn { background: #FEF3C7; border-color: #F59E0B; color: #92400E; }
.act-btn.danger { background: #FEF2F2; border-color: #F87171; color: #B91C1C; }
.act-btn:disabled { opacity: 0.5; cursor: default; }

[data-theme="dark"] .act-btn.warn { background: #78350F; border-color: #D97706; color: #FDE68A; }
[data-theme="dark"] .act-btn.danger { background: #7F1D1D; border-color: #EF4444; color: #FCA5A5; }

.empty {
  color: var(--muted);
  text-align: center;
  padding: 48px 0;
  font-size: 15px;
  line-height: 1.8;
}

.empty-hint {
  margin-top: 16px;
}

/* ========== Responsive ========== */
@media (max-width: 640px) {
  .container { padding: 0 14px; }

  .header-inner { padding: 12px 14px; gap: 8px; }
  .current-project { display: none; }

  .chat-container {
    height: calc(100vh - 53px);
    padding: 0 14px;
  }

  .messages { gap: 10px; padding: 14px 2px 4px; }
  .bubble-wrap { max-width: 92%; }
  .bubble { padding: 10px 14px; font-size: 13px; }

  .chat-form-area { padding: 8px 0 12px; }
  .chat-form { padding: 6px; border-radius: var(--radius-md); }
  .chat-hint { display: none; }

  .app-card { flex-direction: column; }
  .app-side { align-items: flex-start; width: 100%; }
  .app-actions { justify-content: flex-start; }

  .apps-head { flex-direction: column; align-items: flex-start; gap: 12px; }
}

/* ========== Focus Visible ========== */
:focus-visible {
  outline: 2px solid var(--primary);
  outline-offset: 2px;
}

button:focus:not(:focus-visible),
a:focus:not(:focus-visible) {
  outline: none;
}
