* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
  -webkit-tap-highlight-color: transparent;
}

:root {
  --bg: #0d1117;
  --bg-secondary: #161b22;
  --bg-tertiary: #1c2128;
  --surface: #21262d;
  --border: #30363d;
  --text: #e6edf3;
  --text-muted: #8b949e;
  --accent: #2f81f7;
  --accent-dark: #1a4fa0;
  --user-bubble: #2f81f7;
  --assistant-bubble: #1c2128;
  --tool-bg: #161b22;
  --danger: #f85149;
  --success: #3fb950;
  --safe-top: env(safe-area-inset-top, 20px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
}

html, body {
  height: 100%;
  overflow: hidden;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, 'SF Pro Text', 'Helvetica Neue', sans-serif;
  font-size: 16px;
  -webkit-font-smoothing: antialiased;
}

.screen {
  position: fixed;
  inset: 0;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.view {
  flex: 1;
  min-width: 0;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
}

/* ===== Sidebar ===== */
#sidebar-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.5);
  z-index: 200;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.25s;
}
#sidebar-overlay.open {
  opacity: 1;
  pointer-events: all;
}

#sidebar {
  position: fixed;
  top: 0;
  left: 0;
  bottom: 0;
  width: 280px;
  max-width: 85vw;
  background: var(--bg-secondary);
  border-right: 1px solid var(--border);
  z-index: 201;
  transform: translateX(-100%);
  transition: transform 0.28s cubic-bezier(0.4, 0, 0.2, 1);
  display: flex;
  flex-direction: column;
  padding-top: env(safe-area-inset-top, 20px);
}
#sidebar.open { transform: translateX(0); }

.sidebar-header {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 20px 16px 16px;
  border-bottom: 1px solid var(--border);
}
.sidebar-avatar {
  width: 46px; height: 46px; border-radius: 50%;
  background: var(--accent);
  display: flex; align-items: center; justify-content: center;
  font-size: 20px; font-weight: 700; color: white; flex-shrink: 0;
}
.sidebar-app-name { font-weight: 700; font-size: 1.05rem; }
.sidebar-app-sub { font-size: 0.72rem; color: var(--text-muted); margin-top: 2px; }

.sidebar-section-title {
  font-size: 0.7rem;
  font-weight: 600;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: 0.06em;
  padding: 16px 16px 6px;
}
.sidebar-profiles { overflow-y: auto; }

.sidebar-profile-item {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 12px 16px;
  cursor: pointer;
  transition: background 0.15s;
}
.sidebar-profile-item:active { background: var(--surface); }
.sidebar-profile-item.active { background: rgba(47,129,247,0.1); }
.sidebar-profile-item.active .sidebar-profile-name { color: var(--accent); }
.sidebar-profile-name { font-weight: 600; font-size: 0.95rem; }
.sidebar-profile-desc { font-size: 0.72rem; color: var(--text-muted); margin-top: 1px; }

.sidebar-footer {
  border-top: 1px solid var(--border);
  padding-bottom: env(safe-area-inset-bottom, 0px);
}
.sidebar-new-chat {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 16px;
  cursor: pointer;
  font-weight: 600;
  font-size: 0.95rem;
  color: var(--accent);
  transition: background 0.15s;
}
.sidebar-new-chat:active { background: var(--surface); }

.hidden { display: none !important; }

/* ===== Auth Screen ===== */
#auth-screen {
  justify-content: center;
  align-items: center;
}

.auth-container {
  text-align: center;
  padding: 2rem;
  width: 100%;
  max-width: 340px;
}

.auth-logo {
  width: 80px;
  height: 80px;
  border-radius: 20px;
  background: linear-gradient(135deg, var(--accent), #a371f7);
  color: white;
  font-size: 42px;
  font-weight: 700;
  display: flex;
  align-items: center;
  justify-content: center;
  margin: 0 auto 1.5rem;
}

.auth-container h1 { font-size: 1.5rem; font-weight: 600; margin-bottom: 0.5rem; }
.auth-container p { color: var(--text-muted); font-size: 0.9rem; margin-bottom: 1.5rem; }

#token-input, #server-input {
  width: 100%;
  padding: 12px 16px;
  border-radius: 12px;
  border: 1px solid var(--border);
  background: var(--bg-secondary);
  color: var(--text);
  font-size: 16px;
  outline: none;
  margin-bottom: 0.75rem;
}

#token-input { text-align: center; letter-spacing: 2px; }
#server-input { font-size: 14px; text-align: center; }
#token-input:focus, #server-input:focus { border-color: var(--accent); }

#connect-btn {
  width: 100%;
  padding: 12px;
  border-radius: 12px;
  border: none;
  background: var(--accent);
  color: white;
  font-size: 16px;
  font-weight: 600;
  cursor: pointer;
  margin-top: 0.5rem;
}

#connect-btn:active { background: var(--accent-dark); }

/* ===== Header ===== */
#header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 8px 12px;
  padding-top: calc(var(--safe-top) + 8px);
  background: var(--bg-secondary);
  border-bottom: 1px solid var(--border);
  min-height: 44px;
  z-index: 10;
  flex-shrink: 0;
}

.header-left, .header-right { display: flex; align-items: center; gap: 6px; min-width: 80px; }
.header-right { justify-content: flex-end; }
.header-center { font-weight: 600; font-size: 1rem; cursor: pointer; }

#header button {
  width: 34px; height: 34px;
  border-radius: 8px; border: none;
  background: var(--surface); color: var(--text);
  font-size: 18px; cursor: pointer;
  display: flex; align-items: center; justify-content: center;
}

.status-dot {
  width: 8px; height: 8px; border-radius: 50%;
  background: var(--danger); transition: background 0.3s;
}
.status-dot.connected { background: var(--success); }

#cost-display {
  font-size: 0.75rem; color: var(--text-muted);
  font-variant-numeric: tabular-nums;
}

/* ===== Dashboard ===== */
#dashboard {
  padding: 20px 16px;
  padding-bottom: calc(var(--safe-bottom) + 20px);
  overflow-x: hidden;
}

.dash-greeting {
  font-size: 1.4rem;
  font-weight: 700;
  margin-bottom: 20px;
  line-height: 1.3;
}

.dash-greeting span { color: var(--text-muted); font-weight: 400; font-size: 0.9rem; display: block; margin-top: 4px; }

.actions-grid {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 10px;
  margin-bottom: 28px;
}

.action-card {
  background: var(--bg-secondary);
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 14px;
  cursor: pointer;
  transition: transform 0.15s, background 0.15s;
  position: relative;
  overflow: hidden;
}

.action-card:active {
  transform: scale(0.97);
  background: var(--surface);
}

.action-icon {
  width: 36px; height: 36px;
  border-radius: 10px;
  display: flex; align-items: center; justify-content: center;
  font-size: 18px;
  margin-bottom: 10px;
  color: white;
}

.action-label {
  font-weight: 600;
  font-size: 0.9rem;
  margin-bottom: 3px;
}

.action-desc {
  font-size: 0.75rem;
  color: var(--text-muted);
  line-height: 1.3;
}

/* ===== Profiles ===== */
.profiles-row {
  display: flex;
  gap: 10px;
}

.profile-card {
  flex: 1;
  display: flex;
  align-items: center;
  gap: 10px;
  background: var(--bg-secondary);
  border: 2px solid var(--border);
  border-radius: 14px;
  padding: 12px 14px;
  cursor: pointer;
  transition: transform 0.15s, border-color 0.2s;
}

.profile-card:active { transform: scale(0.97); }
.profile-card.active { border-color: var(--accent); }

.profile-icon {
  width: 40px; height: 40px; border-radius: 12px;
  display: flex; align-items: center; justify-content: center;
  font-weight: 700; font-size: 18px; color: white; flex-shrink: 0;
}

.profile-name { font-weight: 600; font-size: 0.9rem; }
.profile-desc { font-size: 0.7rem; color: var(--text-muted); }

/* ===== Agent Status Bar ===== */
#agent-status {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 16px;
  background: linear-gradient(90deg, rgba(47,129,247,0.15), rgba(163,113,247,0.1));
  border-bottom: 1px solid var(--border);
  font-size: 0.8rem;
  color: var(--accent);
  flex-shrink: 0;
}

.status-pulse {
  width: 8px; height: 8px;
  border-radius: 50%;
  background: var(--accent);
  animation: pulse 1.5s infinite;
}

#agent-status.idle {
  background: rgba(63,185,80,0.1);
  color: var(--success);
}

#agent-status.idle .status-pulse {
  background: var(--success);
  animation: none;
}

/* ===== Metrics ===== */
.metrics-row {
  display: flex;
  gap: 10px;
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
}

.metric-card {
  flex: 1;
  min-width: 100px;
  background: var(--bg-secondary);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 14px 12px;
  text-align: center;
}

.metric-value {
  font-size: 1.5rem;
  font-weight: 700;
  color: var(--accent);
  font-variant-numeric: tabular-nums;
}

.metric-label {
  font-size: 0.7rem;
  color: var(--text-muted);
  margin-top: 4px;
}

/* ===== Channels ===== */
.channels-row {
  display: flex;
  gap: 10px;
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
  padding-bottom: 4px;
}

.channel-card {
  display: flex;
  align-items: center;
  gap: 10px;
  background: var(--bg-secondary);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 12px 16px;
  cursor: pointer;
  min-width: 150px;
  flex-shrink: 0;
  transition: transform 0.15s, background 0.15s;
}

.channel-card:active { transform: scale(0.97); background: var(--surface); }

.channel-icon {
  width: 36px; height: 36px; border-radius: 10px;
  display: flex; align-items: center; justify-content: center;
  font-weight: 700; font-size: 16px; color: white; flex-shrink: 0;
}

.channel-name { font-weight: 600; font-size: 0.9rem; }
.channel-desc { font-size: 0.75rem; color: var(--text-muted); }

.dash-section { margin-bottom: 20px; }
.dash-section h3 {
  font-size: 0.85rem;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: 0.5px;
  margin-bottom: 10px;
}

.dash-recent h3 {
  font-size: 0.85rem;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: 0.5px;
  margin-bottom: 10px;
}

.recent-item {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 12px 14px;
  background: var(--bg-secondary);
  border: 1px solid var(--border);
  border-radius: 10px;
  margin-bottom: 8px;
  cursor: pointer;
  transition: background 0.15s;
}

.recent-item:active { background: var(--surface); }

.recent-preview {
  font-size: 0.88rem;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  flex: 1;
  margin-right: 10px;
}

.recent-meta {
  font-size: 0.75rem;
  color: var(--text-muted);
  white-space: nowrap;
}

/* ===== Sessions Panel ===== */
#sessions-panel {
  position: absolute;
  top: 0; right: 0;
  width: 300px; height: 100%;
  background: var(--bg-secondary);
  border-left: 1px solid var(--border);
  z-index: 20;
  padding-top: calc(var(--safe-top) + 8px);
  display: flex; flex-direction: column;
}

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

.sessions-header button {
  background: none; border: none; color: var(--text-muted);
  font-size: 18px; cursor: pointer;
}

#sessions-list {
  flex: 1; overflow-y: auto; -webkit-overflow-scrolling: touch;
}

.session-item {
  padding: 12px 16px;
  border-bottom: 1px solid var(--border);
  cursor: pointer;
}

.session-item:active { background: var(--surface); }
.session-item .session-date { font-size: 0.75rem; color: var(--text-muted); }
.session-item .session-preview {
  font-size: 0.85rem; margin-top: 4px;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.session-item .session-count { font-size: 0.7rem; color: var(--text-muted); }
.session-item.active { background: var(--surface); border-left: 3px solid var(--accent); }

/* ===== Chat View ===== */
#chat-view {
  display: flex;
  flex-direction: column;
}

#messages {
  flex: 1;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  padding: 16px;
  display: flex;
  flex-direction: column;
  gap: 10px;
  scroll-behavior: smooth;
}

.message {
  max-width: 85%;
  padding: 10px 14px;
  border-radius: 16px;
  line-height: 1.5;
  word-wrap: break-word;
  overflow-wrap: break-word;
  animation: fadeIn 0.2s ease;
}

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

.message.user {
  align-self: flex-end;
  background: var(--user-bubble);
  color: white;
  border-bottom-right-radius: 4px;
}

.message.assistant {
  align-self: flex-start;
  background: var(--assistant-bubble);
  border-bottom-left-radius: 4px;
}

/* Markdown */
.message.assistant h1, .message.assistant h2, .message.assistant h3 {
  margin: 0.6em 0 0.3em; font-weight: 600;
}
.message.assistant h1 { font-size: 1.15em; }
.message.assistant h2 { font-size: 1.05em; }
.message.assistant h3 { font-size: 1em; }
.message.assistant p { margin: 0.3em 0; }
.message.assistant ul, .message.assistant ol { padding-left: 1.5em; margin: 0.3em 0; }

.message.assistant code {
  background: rgba(255,255,255,0.1);
  padding: 1px 5px; border-radius: 4px;
  font-family: 'SF Mono', 'Menlo', monospace; font-size: 0.88em;
}

.message.assistant pre {
  background: var(--bg); border: 1px solid var(--border);
  border-radius: 8px; padding: 10px 12px;
  overflow-x: auto; margin: 0.5em 0;
  -webkit-overflow-scrolling: touch;
}

.message.assistant pre code {
  background: none; padding: 0; font-size: 0.85em; line-height: 1.4;
}

.message.assistant a { color: var(--accent); text-decoration: none; }
.message.assistant blockquote {
  border-left: 3px solid var(--accent); padding-left: 10px;
  color: var(--text-muted); margin: 0.4em 0;
}

.message.assistant table { border-collapse: collapse; margin: 0.4em 0; font-size: 0.9em; width: 100%; }
.message.assistant th, .message.assistant td { border: 1px solid var(--border); padding: 5px 8px; text-align: left; }
.message.assistant th { background: var(--surface); }

/* Tool indicator */
.tool-indicator {
  display: flex; align-items: flex-start; gap: 8px;
  padding: 8px 12px;
  background: var(--tool-bg); border: 1px solid var(--border);
  border-radius: 8px; font-size: 0.82rem; color: var(--text-muted);
  max-width: 92%; align-self: flex-start;
  animation: fadeIn 0.15s ease;
  flex-wrap: wrap;
}

.tool-indicator .tool-icon { color: var(--accent); flex-shrink: 0; }
.tool-indicator .tool-name { font-weight: 600; color: var(--text); flex-shrink: 0; }
.tool-indicator .drop-badge {
  font-size: 0.6rem;
  font-weight: 700;
  padding: 2px 6px;
  border-radius: 4px;
  color: #000;
  letter-spacing: 0.5px;
  flex-shrink: 0;
}

.tool-indicator .tool-detail {
  font-style: italic;
  line-height: 1.3;
}

.tool-indicator .tool-progress {
  margin-left: auto;
  font-size: 0.7rem;
  color: var(--accent);
  font-weight: 600;
  white-space: nowrap;
  flex-shrink: 0;
}

/* Typing */
.typing {
  align-self: flex-start;
  padding: 12px 18px;
  background: var(--assistant-bubble);
  border-radius: 16px; border-bottom-left-radius: 4px;
  display: flex; gap: 4px;
}

.typing span {
  width: 7px; height: 7px; background: var(--text-muted);
  border-radius: 50%; animation: bounce 1.2s infinite;
}
.typing span:nth-child(2) { animation-delay: 0.2s; }
.typing span:nth-child(3) { animation-delay: 0.4s; }

@keyframes bounce {
  0%, 60%, 100% { transform: translateY(0); }
  30% { transform: translateY(-6px); }
}

/* ===== Input Area ===== */
#input-area {
  padding: 8px 12px;
  padding-bottom: calc(var(--safe-bottom) + 8px);
  background: var(--bg-secondary);
  border-top: 1px solid var(--border);
  flex-shrink: 0;
}

.input-row {
  display: flex; align-items: flex-end; gap: 6px;
  background: var(--surface);
  border-radius: 22px;
  padding: 4px 4px 4px 14px;
}

#msg-input {
  flex: 1; border: none; background: transparent;
  color: var(--text); font-size: 16px; font-family: inherit;
  resize: none; outline: none; max-height: 120px;
  line-height: 1.4; padding: 8px 0;
}

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

#mic-btn, #send-btn, #cancel-btn, #photo-btn {
  width: 36px; height: 36px; min-width: 36px;
  border-radius: 50%; border: none;
  cursor: pointer; display: flex;
  align-items: center; justify-content: center;
  transition: background 0.2s;
}

#photo-btn { background: transparent; color: var(--text-muted); }
#mic-btn { background: transparent; color: var(--text-muted); }
#mic-btn.recording { background: var(--danger); color: white; animation: pulse 1.5s infinite; }

#cancel-btn { background: var(--danger); color: white; }

@keyframes pulse {
  0%, 100% { box-shadow: 0 0 0 0 rgba(248,81,73,0.4); }
  50% { box-shadow: 0 0 0 8px rgba(248,81,73,0); }
}

#send-btn { background: var(--accent); color: white; }
#send-btn:active { background: var(--accent-dark); }
#send-btn:disabled { opacity: 0.4; }

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

/* ===== Responsive ===== */
@media (min-width: 768px) {
  .message { max-width: 65%; }
  .actions-grid { grid-template-columns: repeat(4, 1fr); }
  #messages, #input-area, #dashboard { max-width: 800px; margin-left: auto; margin-right: auto; width: 100%; }
}
