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

:root {
  --bg-primary: #171717;
  --bg-secondary: #212121;
  --bg-hover: #2a2a2a;
  --bg-input: #2f2f2f;
  --text-primary: #ececec;
  --text-secondary: #b4b4b4;
  --text-muted: #777;
  --accent: #10a37f;
  --accent-hover: #0e8f6f;
  --user-bg: transparent;
  --assistant-bg: var(--bg-secondary);
  --danger: #ef4444;
  --success: #10a37f;
  --warning: #f59e0b;
  --border: #333;
  --radius: 12px;
  --radius-sm: 8px;
  --header-height: 48px;
  --max-width: 768px;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
}

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

body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', sans-serif;
  background: var(--bg-primary);
  color: var(--text-primary);
  font-size: 15px;
  line-height: 1.6;
  -webkit-text-size-adjust: 100%;
  -webkit-tap-highlight-color: transparent;
}

#app {
  display: flex;
  flex-direction: column;
  height: 100%;
  height: 100dvh;
  padding-top: var(--safe-top);
  padding-bottom: var(--safe-bottom);
}

/* === Setup Screen === */
.setup {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px;
}

.setup.hidden { display: none; }

.setup-card {
  max-width: 440px;
  width: 100%;
  text-align: center;
}

.setup-card h1 {
  font-size: 1.8rem;
  font-weight: 700;
  margin-bottom: 6px;
}

.setup-sub {
  color: var(--text-muted);
  margin-bottom: 32px;
}

.setup-field {
  text-align: left;
  margin-bottom: 20px;
}

.setup-field label {
  display: block;
  font-size: 0.82rem;
  color: var(--text-secondary);
  margin-bottom: 6px;
}

.setup-field input[type="text"] {
  width: 100%;
  background: var(--bg-input);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  color: var(--text-primary);
  font-size: 15px;
  padding: 12px 14px;
  outline: none;
  font-family: inherit;
}

.setup-field input:focus { border-color: var(--accent); }

.setup-hint {
  font-size: 0.8rem;
  color: var(--text-muted);
  margin-top: 8px;
  line-height: 1.5;
}

.link-btn {
  background: none;
  border: none;
  color: var(--accent);
  font-size: inherit;
  cursor: pointer;
  padding: 0;
  text-decoration: underline;
  font-family: inherit;
}

.link-btn:hover { color: var(--accent-hover); }

.btn-connect {
  width: 100%;
  background: var(--accent);
  color: #fff;
  border: none;
  border-radius: var(--radius-sm);
  padding: 12px;
  font-size: 1rem;
  font-weight: 600;
  cursor: pointer;
  transition: background 0.15s;
}

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

.setup-error {
  color: #fca5a5;
  font-size: 0.85rem;
  margin-top: 12px;
  text-align: left;
}

.setup-error.hidden { display: none; }

/* === Header === */
header {
  height: var(--header-height);
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 12px;
  background: var(--bg-primary);
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}

.header-left, .header-right {
  display: flex;
  align-items: center;
  gap: 8px;
}

.status {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  flex-shrink: 0;
}

.status.connected { background: var(--success); box-shadow: 0 0 6px var(--success); }
.status.disconnected { background: var(--danger); }
.status.connecting { background: var(--warning); animation: pulse 1s infinite; }

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

.model-dropdown {
  background: transparent;
  border: 1px solid var(--border);
  border-radius: 6px;
  color: var(--text-primary);
  font-size: 0.82rem;
  padding: 4px 8px;
  max-width: 200px;
  outline: none;
}

.model-dropdown:focus { border-color: var(--accent); }

/* === Buttons === */
.icon-btn {
  background: none;
  border: none;
  color: var(--text-secondary);
  cursor: pointer;
  padding: 6px;
  border-radius: 6px;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background 0.15s, color 0.15s;
}

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

.btn-danger {
  background: transparent;
  color: var(--danger);
  border: 1px solid rgba(239,68,68,0.3);
  border-radius: var(--radius-sm);
  padding: 8px 16px;
  font-size: 0.85rem;
  cursor: pointer;
  width: 100%;
  transition: background 0.15s;
}

.btn-danger:hover { background: rgba(239,68,68,0.1); }

/* === Sidebar === */
.overlay {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.5);
  z-index: 49;
}

.overlay.hidden { display: none; }

#sidebar {
  position: fixed;
  top: 0;
  left: 0;
  bottom: 0;
  width: 280px;
  max-width: 80vw;
  background: var(--bg-secondary);
  border-right: 1px solid var(--border);
  z-index: 50;
  display: flex;
  flex-direction: column;
  transform: translateX(0);
  transition: transform 0.2s ease;
}

#sidebar.hidden {
  transform: translateX(-100%);
  pointer-events: none;
}

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

.sidebar-header h3 { font-size: 1rem; font-weight: 600; }

.sidebar-content {
  padding: 16px;
  overflow-y: auto;
  flex: 1;
  display: flex;
  flex-direction: column;
  gap: 20px;
}

.setting-group label {
  display: block;
  font-size: 0.82rem;
  color: var(--text-secondary);
  margin-bottom: 6px;
}

.setting-group input[type="text"] {
  width: 100%;
  background: var(--bg-input);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  color: var(--text-primary);
  font-size: 13px;
  padding: 8px 10px;
  outline: none;
  font-family: 'SF Mono', 'Fira Code', Consolas, monospace;
  margin-bottom: 6px;
}

.setting-group input[type="text"]:focus { border-color: var(--accent); }

.btn-sm {
  background: var(--accent);
  color: #fff;
  border: none;
  border-radius: 6px;
  padding: 6px 14px;
  font-size: 0.8rem;
  font-weight: 600;
  cursor: pointer;
  transition: background 0.15s;
}

.btn-sm:hover { background: var(--accent-hover); }

.setting-group textarea {
  width: 100%;
  background: var(--bg-input);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  color: var(--text-primary);
  font-size: 14px;
  padding: 8px 10px;
  resize: vertical;
  min-height: 70px;
  outline: none;
  font-family: inherit;
}

.setting-group textarea:focus { border-color: var(--accent); }

.setting-group input[type="range"] {
  width: 100%;
  accent-color: var(--accent);
}

.checkbox-row {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 0.85rem;
  color: var(--text-secondary);
  cursor: pointer;
}

.checkbox-row input[type="checkbox"] {
  accent-color: var(--accent);
  width: 15px;
  height: 15px;
}

/* === Chat === */
#chat-container {
  flex: 1;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
}

#messages {
  max-width: var(--max-width);
  margin: 0 auto;
  padding: 16px 16px 8px;
  display: flex;
  flex-direction: column;
  min-height: 100%;
}

/* === Welcome (in-chat) === */
.welcome-chat {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  text-align: center;
  padding: 40px 20px;
}

.welcome-chat p {
  color: var(--text-muted);
  font-size: 1.1rem;
}

/* === Messages === */
.message {
  display: flex;
  gap: 12px;
  padding: 20px 0;
  animation: fadeIn 0.2s ease;
}

.message + .message {
  border-top: 1px solid rgba(255,255,255,0.04);
}

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

.avatar {
  width: 30px;
  height: 30px;
  border-radius: 6px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 0.65rem;
  font-weight: 700;
  flex-shrink: 0;
  text-transform: uppercase;
  letter-spacing: 0.02em;
}

.message.user .avatar {
  background: #5b5fc7;
  color: #fff;
}

.message.assistant .avatar {
  background: var(--accent);
  color: #fff;
}

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

.message-content {
  line-height: 1.65;
  word-wrap: break-word;
  overflow-wrap: break-word;
}

.message-content.error {
  color: #fca5a5;
  background: rgba(239,68,68,0.1);
  padding: 10px 14px;
  border-radius: var(--radius-sm);
  border: 1px solid rgba(239,68,68,0.2);
  font-size: 0.9rem;
}

/* Markdown */
.message-content p { margin: 0.5em 0; }
.message-content p:first-child { margin-top: 0; }
.message-content p:last-child { margin-bottom: 0; }
.message-content ul, .message-content ol { margin: 0.5em 0; padding-left: 1.5em; }
.message-content li { margin: 0.25em 0; }

.message-content h1, .message-content h2, .message-content h3,
.message-content h4, .message-content h5, .message-content h6 {
  margin: 0.8em 0 0.3em;
  line-height: 1.3;
}
.message-content h1 { font-size: 1.25em; }
.message-content h2 { font-size: 1.1em; }
.message-content h3 { font-size: 1em; }

.message-content code {
  background: rgba(255,255,255,0.08);
  padding: 2px 5px;
  border-radius: 4px;
  font-size: 0.87em;
  font-family: 'SF Mono', 'Fira Code', Consolas, monospace;
}

.message-content pre {
  background: #0d0d0d;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 14px;
  overflow-x: auto;
  margin: 0.6em 0;
  position: relative;
}

.message-content pre code {
  background: none;
  padding: 0;
  font-size: 0.85em;
  line-height: 1.55;
  color: #d4d4d4;
}

.message-content blockquote {
  border-left: 3px solid var(--accent);
  padding-left: 14px;
  margin: 0.5em 0;
  color: var(--text-secondary);
}

.message-content a { color: var(--accent); text-decoration: none; }
.message-content a:hover { text-decoration: underline; }

.message-content table { border-collapse: collapse; margin: 0.6em 0; width: 100%; font-size: 0.9em; }
.message-content th, .message-content td { border: 1px solid var(--border); padding: 6px 10px; text-align: left; }
.message-content th { background: rgba(255,255,255,0.04); }

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

/* Copy button */
.copy-btn {
  position: absolute;
  top: 6px;
  right: 6px;
  background: rgba(255,255,255,0.08);
  border: none;
  color: var(--text-muted);
  border-radius: 4px;
  padding: 3px 8px;
  font-size: 0.7rem;
  cursor: pointer;
  opacity: 0;
  transition: opacity 0.15s;
}

.message-content pre:hover .copy-btn { opacity: 1; }
.copy-btn:hover { background: rgba(255,255,255,0.15); color: var(--text-primary); }

/* Typing indicator */
.typing {
  display: flex;
  gap: 4px;
  padding: 4px 0;
}

.typing span {
  width: 6px;
  height: 6px;
  background: var(--text-muted);
  border-radius: 50%;
  animation: typingDot 1.4s infinite;
}

.typing span:nth-child(2) { animation-delay: 0.2s; }
.typing span:nth-child(3) { animation-delay: 0.4s; }

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

/* === Input Area === */
#input-area {
  padding: 0 16px 12px;
  flex-shrink: 0;
  max-width: var(--max-width);
  margin: 0 auto;
  width: 100%;
}

.input-wrapper {
  display: flex;
  align-items: flex-end;
  gap: 8px;
  background: var(--bg-input);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 8px 10px 8px 16px;
  transition: border-color 0.15s;
}

.input-wrapper:focus-within {
  border-color: rgba(255,255,255,0.2);
}

#user-input {
  flex: 1;
  background: transparent;
  border: none;
  color: var(--text-primary);
  font-size: 16px;
  line-height: 1.5;
  resize: none;
  max-height: 150px;
  outline: none;
  padding: 4px 0;
  font-family: inherit;
}

.send-button, .stop-button {
  width: 32px;
  height: 32px;
  border: none;
  border-radius: 8px;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  flex-shrink: 0;
  transition: background 0.15s, transform 0.1s;
}

.send-button {
  background: var(--text-primary);
  color: var(--bg-primary);
}

.send-button:disabled {
  background: var(--bg-hover);
  color: var(--text-muted);
  cursor: not-allowed;
}

.send-button:not(:disabled):hover { background: #fff; }
.send-button:not(:disabled):active { transform: scale(0.92); }

.stop-button { background: var(--text-primary); color: var(--bg-primary); }
.stop-button:hover { background: #fff; }

.input-footer {
  text-align: center;
  font-size: 0.72rem;
  color: var(--text-muted);
  margin-top: 8px;
}

.hidden { display: none !important; }

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

/* === Desktop === */
@media (min-width: 768px) {
  #messages { padding: 24px 24px 12px; }
}
