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

:root {
  --font: 'DM Sans', -apple-system, BlinkMacSystemFont, sans-serif;
  --mono: 'JetBrains Mono', monospace;

  --bg:          #ffffff;
  --bg-secondary:#f7f7f8;
  --bg-tertiary: #ececf1;
  --surface:     #ffffff;
  --border:      #e5e5e5;
  --text:        #1a1a1a;
  --text-secondary: #6b6b6b;
  --text-tertiary:  #999999;
  --accent:      #1a1a1a;
  --accent-soft: #f0f0f0;
  --user-bg:     #1a1a1a;
  --user-text:   #ffffff;
  --assistant-bg:#f7f7f8;
  --assistant-text:#1a1a1a;
  --danger:      #ef4444;
  --success:     #22c55e;
  --shadow:      0 1px 3px rgba(0,0,0,0.06), 0 1px 2px rgba(0,0,0,0.04);
  --shadow-lg:   0 10px 40px rgba(0,0,0,0.1);
  --radius:      12px;
  --radius-sm:   8px;
  --radius-full: 9999px;
  --sidebar-w:   260px;
  --transition:  0.2s cubic-bezier(0.4, 0, 0.2, 1);
}

[data-theme="dark"] {
  --bg:          #0d0d0d;
  --bg-secondary:#1a1a1a;
  --bg-tertiary: #2a2a2a;
  --surface:     #1a1a1a;
  --border:      #2a2a2a;
  --text:        #ececec;
  --text-secondary: #8e8e8e;
  --text-tertiary:  #666666;
  --accent:      #ffffff;
  --accent-soft: #2a2a2a;
  --user-bg:     #ffffff;
  --user-text:   #0d0d0d;
  --assistant-bg:#1a1a1a;
  --assistant-text:#ececec;
  --shadow:      0 1px 3px rgba(0,0,0,0.3);
  --shadow-lg:   0 10px 40px rgba(0,0,0,0.5);
}

html, body { height: 100%; }

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

#app {
  display: flex;
  height: 100vh;
  height: 100dvh;
}

/* ── Sidebar ── */
.sidebar {
  width: var(--sidebar-w);
  background: var(--bg-secondary);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  flex-shrink: 0;
  transition: transform var(--transition), opacity var(--transition);
  z-index: 30;
}

.sidebar.collapsed {
  transform: translateX(calc(-1 * var(--sidebar-w)));
  position: absolute;
  height: 100%;
  opacity: 0;
  pointer-events: none;
}

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

.logo {
  display: flex;
  align-items: center;
  gap: 10px;
}

.logo-icon {
  font-size: 22px;
  line-height: 1;
}

.logo-text {
  font-size: 17px;
  font-weight: 600;
  letter-spacing: -0.02em;
}

.logo-text small {
  font-weight: 400;
  color: var(--text-secondary);
  font-size: 13px;
  margin-left: 2px;
}

.btn-new-chat {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 34px;
  height: 34px;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  border-radius: var(--radius-sm);
  cursor: pointer;
  transition: all var(--transition);
}

.btn-new-chat:hover {
  background: var(--bg-tertiary);
}

.sidebar-conversations {
  flex: 1;
  overflow-y: auto;
  padding: 8px;
}

.conv-item {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 10px 12px;
  border-radius: var(--radius-sm);
  cursor: pointer;
  font-size: 13.5px;
  color: var(--text-secondary);
  transition: all var(--transition);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.conv-item:hover, .conv-item.active {
  background: var(--bg-tertiary);
  color: var(--text);
}

.conv-item.active {
  font-weight: 500;
}

.sidebar-footer {
  padding: 12px 16px;
  border-top: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.sidebar-footer-btn {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 10px;
  border: none;
  background: none;
  color: var(--text-secondary);
  border-radius: var(--radius-sm);
  cursor: pointer;
  font-size: 13px;
  font-family: var(--font);
  transition: all var(--transition);
}

.sidebar-footer-btn:hover {
  background: var(--bg-tertiary);
  color: var(--text);
}

.model-badge {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 11.5px;
  color: var(--text-tertiary);
  padding: 4px 0;
}

.badge-dot {
  width: 6px;
  height: 6px;
  background: var(--success);
  border-radius: 50%;
  flex-shrink: 0;
  animation: pulse 2s infinite;
}

@keyframes pulse {
  0%, 100% { opacity: 1; }
  50% { opacity: 0.4; }
}

/* ── Main ── */
#main {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-width: 0;
  position: relative;
}

/* ── Topbar ── */
#topbar {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 12px 20px;
  border-bottom: 1px solid var(--border);
  background: var(--bg);
  z-index: 10;
  min-height: 52px;
}

.topbar-title {
  flex: 1;
  font-size: 14px;
  font-weight: 500;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.topbar-actions {
  display: flex;
  gap: 4px;
}

.btn-icon {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 34px;
  height: 34px;
  border: none;
  background: none;
  color: var(--text-secondary);
  border-radius: var(--radius-sm);
  cursor: pointer;
  transition: all var(--transition);
}

.btn-icon:hover {
  background: var(--bg-tertiary);
  color: var(--text);
}

/* ── Welcome ── */
.welcome {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 40px 20px;
  overflow-y: auto;
}

.welcome-content {
  text-align: center;
  max-width: 600px;
}

.welcome-icon {
  font-size: 48px;
  margin-bottom: 16px;
  line-height: 1;
}

.welcome-content h2 {
  font-size: 28px;
  font-weight: 600;
  letter-spacing: -0.03em;
  margin-bottom: 6px;
}

.welcome-sub {
  font-size: 17px;
  color: var(--text-secondary);
  margin-bottom: 32px;
}

.welcome-chips {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 8px;
}

.chip {
  padding: 10px 18px;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  border-radius: var(--radius-full);
  font-size: 13.5px;
  font-family: var(--font);
  cursor: pointer;
  transition: all var(--transition);
  white-space: nowrap;
}

.chip:hover {
  background: var(--bg-tertiary);
  border-color: var(--text-tertiary);
}

/* ── Messages ── */
.messages {
  flex: 1;
  overflow-y: auto;
  padding: 20px 0;
  scroll-behavior: smooth;
}

.messages.hidden { display: none; }
.welcome.hidden  { display: none; }

.msg {
  display: flex;
  gap: 14px;
  padding: 12px 24px;
  max-width: 820px;
  margin: 0 auto;
  width: 100%;
  animation: fadeIn 0.25s ease-out;
}

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

.msg-avatar {
  width: 30px;
  height: 30px;
  border-radius: var(--radius-sm);
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  font-size: 14px;
  font-weight: 600;
  margin-top: 2px;
}

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

.msg-assistant .msg-avatar {
  background: var(--accent-soft);
  color: var(--text);
}

.msg-body {
  flex: 1;
  min-width: 0;
}

.msg-role {
  font-size: 13px;
  font-weight: 600;
  margin-bottom: 4px;
}

.msg-text {
  font-size: 14.5px;
  line-height: 1.7;
  word-wrap: break-word;
  overflow-wrap: break-word;
}

.msg-text p { margin-bottom: 10px; }
.msg-text p:last-child { margin-bottom: 0; }

.msg-text pre {
  background: var(--bg-tertiary);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 14px 16px;
  overflow-x: auto;
  margin: 10px 0;
  font-family: var(--mono);
  font-size: 13px;
  line-height: 1.5;
}

.msg-text code {
  font-family: var(--mono);
  font-size: 13px;
  background: var(--bg-tertiary);
  padding: 2px 6px;
  border-radius: 4px;
}

.msg-text pre code {
  background: none;
  padding: 0;
}

.msg-text ul, .msg-text ol {
  padding-left: 20px;
  margin: 8px 0;
}

.msg-text li {
  margin-bottom: 4px;
}

.msg-text strong {
  font-weight: 600;
}

.msg-text blockquote {
  border-left: 3px solid var(--border);
  padding-left: 14px;
  color: var(--text-secondary);
  margin: 10px 0;
}

.cursor-blink::after {
  content: '▊';
  animation: blink 0.7s step-end infinite;
  color: var(--text-secondary);
  font-size: 14px;
}

@keyframes blink {
  0%, 100% { opacity: 1; }
  50% { opacity: 0; }
}

.msg-meta {
  display: flex;
  gap: 12px;
  margin-top: 8px;
  font-size: 11.5px;
  color: var(--text-tertiary);
}

/* ── Input ── */
.input-area {
  padding: 0 20px 16px;
  max-width: 820px;
  margin: 0 auto;
  width: 100%;
}

.input-wrapper {
  display: flex;
  align-items: flex-end;
  gap: 8px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 16px;
  padding: 8px 12px 8px 18px;
  box-shadow: var(--shadow);
  transition: border-color var(--transition), box-shadow var(--transition);
}

.input-wrapper:focus-within {
  border-color: var(--text-tertiary);
  box-shadow: var(--shadow), 0 0 0 1px var(--border);
}

#input {
  flex: 1;
  border: none;
  background: none;
  color: var(--text);
  font-family: var(--font);
  font-size: 15px;
  line-height: 1.5;
  resize: none;
  outline: none;
  max-height: 160px;
  padding: 6px 0;
}

#input::placeholder {
  color: var(--text-tertiary);
}

.btn-send, .btn-stop {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 34px;
  height: 34px;
  border: none;
  border-radius: var(--radius-full);
  cursor: pointer;
  flex-shrink: 0;
  transition: all var(--transition);
}

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

.btn-send:disabled {
  opacity: 0.3;
  cursor: default;
}

.btn-send:not(:disabled):hover {
  opacity: 0.85;
  transform: scale(1.05);
}

.btn-stop {
  background: var(--danger);
  color: white;
}

.btn-stop:hover {
  opacity: 0.85;
}

.hidden { display: none !important; }

.input-disclaimer {
  text-align: center;
  font-size: 11.5px;
  color: var(--text-tertiary);
  margin-top: 8px;
}

/* ── Modal ── */
.modal {
  position: fixed;
  inset: 0;
  z-index: 100;
  display: flex;
  align-items: center;
  justify-content: center;
}

.modal.hidden { display: none; }

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

.modal-content {
  position: relative;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  width: 480px;
  max-width: 90vw;
  max-height: 80vh;
  overflow-y: auto;
  box-shadow: var(--shadow-lg);
  animation: modalIn 0.2s ease-out;
}

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

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

.modal-header h3 {
  font-size: 16px;
  font-weight: 600;
}

.modal-body {
  padding: 20px;
  display: flex;
  flex-direction: column;
  gap: 14px;
}

.setting-label {
  font-size: 13px;
  font-weight: 500;
  color: var(--text-secondary);
  display: flex;
  justify-content: space-between;
}

.setting-value {
  font-family: var(--mono);
  font-size: 12px;
  color: var(--text-tertiary);
}

.setting-textarea {
  width: 100%;
  padding: 10px 14px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: var(--bg-secondary);
  color: var(--text);
  font-family: var(--font);
  font-size: 13.5px;
  line-height: 1.5;
  resize: vertical;
  outline: none;
}

.setting-textarea:focus {
  border-color: var(--text-tertiary);
}

.setting-range {
  width: 100%;
  accent-color: var(--accent);
  cursor: pointer;
}

/* ── Performance Dashboard ── */
.perf-dashboard {
  border-bottom: 1px solid var(--border);
  background: var(--bg-secondary);
  padding: 12px 20px;
  overflow: hidden;
  max-height: 0;
  opacity: 0;
  transition: max-height 0.35s ease, opacity 0.25s ease, padding 0.35s ease;
  padding-top: 0;
  padding-bottom: 0;
}

.perf-dashboard.open {
  max-height: 200px;
  opacity: 1;
  padding: 12px 20px;
}

.perf-grid {
  display: grid;
  grid-template-columns: repeat(6, 1fr);
  gap: 10px;
  max-width: 820px;
  margin: 0 auto;
}

.perf-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 10px 12px;
  min-width: 0;
}

.perf-card-label {
  font-size: 10.5px;
  font-weight: 500;
  color: var(--text-tertiary);
  text-transform: uppercase;
  letter-spacing: 0.04em;
  margin-bottom: 4px;
}

.perf-card-value {
  font-family: var(--mono);
  font-size: 18px;
  font-weight: 500;
  color: var(--text);
  line-height: 1.2;
}

.perf-card-sub {
  font-size: 10.5px;
  color: var(--text-tertiary);
  margin-top: 2px;
}

.perf-bar {
  height: 4px;
  background: var(--bg-tertiary);
  border-radius: 2px;
  margin-top: 6px;
  overflow: hidden;
}

.perf-bar-fill {
  height: 100%;
  background: var(--accent);
  border-radius: 2px;
  width: 0%;
  transition: width 0.5s ease, background-color 0.3s ease;
}

.perf-bar-fill.warn  { background: #f59e0b; }
.perf-bar-fill.danger { background: #ef4444; }
.perf-bar-mem { background: #6366f1; }
.perf-bar-kv  { background: #22c55e; }

.btn-perf-toggle.active {
  background: var(--accent-soft);
  color: var(--accent);
}

/* ── Message Metrics Card ── */
.msg-metrics {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(130px, 1fr));
  gap: 6px;
  margin-top: 10px;
  padding: 10px 12px;
  background: var(--bg-secondary);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  font-family: var(--mono);
  font-size: 11.5px;
}

.msg-metric-item {
  display: flex;
  flex-direction: column;
  gap: 1px;
}

.msg-metric-label {
  font-size: 9.5px;
  color: var(--text-tertiary);
  text-transform: uppercase;
  letter-spacing: 0.04em;
  font-family: var(--font);
  font-weight: 500;
}

.msg-metric-val {
  color: var(--text);
  font-weight: 500;
}

.msg-metric-val.highlight {
  color: #22c55e;
}

.live-status {
  font-family: var(--mono);
  font-size: 11px;
}

.live-status.online  { color: #22c55e; }
.live-status.offline { color: #ef4444; }

/* ── Scrollbar ── */
::-webkit-scrollbar { width: 6px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: var(--border); border-radius: 3px; }
::-webkit-scrollbar-thumb:hover { background: var(--text-tertiary); }

/* ── Responsive ── */
@media (max-width: 768px) {
  .sidebar {
    position: fixed;
    height: 100%;
    transform: translateX(calc(-1 * var(--sidebar-w)));
    opacity: 0;
    pointer-events: none;
  }

  .sidebar.open {
    transform: translateX(0);
    opacity: 1;
    pointer-events: all;
    box-shadow: var(--shadow-lg);
  }

  .msg { padding: 10px 16px; }
  .input-area { padding: 0 12px 12px; }
  .welcome-content h2 { font-size: 22px; }
  .welcome-sub { font-size: 15px; }
  .welcome-chips { gap: 6px; }
  .chip { font-size: 12.5px; padding: 8px 14px; }
  .perf-grid { grid-template-columns: repeat(3, 1fr); }
  .perf-dashboard.open { max-height: 300px; }
  .msg-metrics { grid-template-columns: repeat(2, 1fr); }
}
