* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

:root {
  --bg-primary: #0f0f0f;
  --bg-secondary: #1a1a1a;
  --bg-tertiary: #242424;
  --bg-hover: #2a2a2a;
  --bg-active: #333;
  --text-primary: #e8e8e8;
  --text-secondary: #999;
  --text-muted: #666;
  --accent: #6C63FF;
  --accent-hover: #5a52e0;
  --border: #2a2a2a;
  --danger: #e74c3c;
  --danger-hover: #c0392b;
  --thinking-bg: #1a1a2e;
  --thinking-border: #3a3a6a;
  --tool-bg: #1a2e1a;
  --tool-border: #3a6a3a;
  --code-bg: #1e1e1e;
  --scrollbar-track: #1a1a1a;
  --scrollbar-thumb: #333;
  --sidebar-width: 280px;
}

html, body {
  height: 100%;
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', sans-serif;
  background: var(--bg-primary);
  color: var(--text-primary);
  overflow: hidden;
}

.screen {
  display: none;
  height: 100vh;
}
.screen.active {
  display: flex;
}

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

/* ===================== AUTH ===================== */

#auth-screen {
  align-items: center;
  justify-content: center;
  background: linear-gradient(135deg, #0f0f0f 0%, #1a1a2e 100%);
}

.auth-container {
  width: 380px;
  background: var(--bg-secondary);
  border-radius: 16px;
  padding: 40px 32px;
  border: 1px solid var(--border);
  box-shadow: 0 20px 60px rgba(0,0,0,0.5);
}

.auth-logo {
  text-align: center;
  margin-bottom: 32px;
}
.auth-logo h1 {
  margin-top: 12px;
  font-size: 24px;
  font-weight: 600;
}
.auth-logo p {
  color: var(--text-secondary);
  font-size: 14px;
  margin-top: 4px;
}

.auth-tabs {
  display: flex;
  gap: 0;
  margin-bottom: 24px;
  border-radius: 8px;
  overflow: hidden;
  border: 1px solid var(--border);
}
.auth-tab {
  flex: 1;
  padding: 10px;
  background: transparent;
  border: none;
  color: var(--text-secondary);
  font-size: 14px;
  cursor: pointer;
  transition: all 0.2s;
}
.auth-tab.active {
  background: var(--accent);
  color: white;
}

.auth-form {
  display: none;
  flex-direction: column;
  gap: 12px;
}
.auth-form.active {
  display: flex;
}

.auth-form input {
  width: 100%;
  padding: 12px 16px;
  background: var(--bg-tertiary);
  border: 1px solid var(--border);
  border-radius: 8px;
  color: var(--text-primary);
  font-size: 14px;
  outline: none;
  transition: border-color 0.2s;
}
.auth-form input:focus {
  border-color: var(--accent);
}

.btn-primary {
  width: 100%;
  padding: 12px;
  background: var(--accent);
  color: white;
  border: none;
  border-radius: 8px;
  font-size: 14px;
  font-weight: 500;
  cursor: pointer;
  transition: background 0.2s;
  margin-top: 4px;
}
.btn-primary:hover {
  background: var(--accent-hover);
}

.auth-error {
  color: var(--danger);
  font-size: 13px;
  text-align: center;
  min-height: 18px;
}

/* ===================== APP LAYOUT ===================== */

#app-screen {
  flex-direction: row;
  height: 100vh;
}

/* ===================== SIDEBAR ===================== */

#sidebar {
  width: var(--sidebar-width);
  min-width: var(--sidebar-width);
  background: var(--bg-secondary);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  height: 100vh;
}

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

.sidebar-logo {
  display: flex;
  align-items: center;
  gap: 10px;
  font-weight: 600;
  font-size: 15px;
}

.btn-icon {
  background: transparent;
  border: 1px solid var(--border);
  color: var(--text-secondary);
  width: 36px;
  height: 36px;
  border-radius: 8px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: all 0.2s;
}
.btn-icon:hover {
  background: var(--bg-hover);
  color: var(--text-primary);
  border-color: var(--text-muted);
}

.btn-icon-sm {
  background: transparent;
  border: none;
  color: var(--text-muted);
  width: 24px;
  height: 24px;
  border-radius: 4px;
  cursor: pointer;
  font-size: 16px;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: all 0.15s;
}
.btn-icon-sm:hover {
  background: var(--danger);
  color: white;
}

.sidebar-section-toggle {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 10px 16px;
  cursor: pointer;
  color: var(--text-secondary);
  font-size: 13px;
  transition: all 0.2s;
  border-bottom: 1px solid var(--border);
}
.sidebar-section-toggle:hover {
  background: var(--bg-hover);
  color: var(--text-primary);
}

.sidebar-section {
  padding: 8px 16px 12px;
  border-bottom: 1px solid var(--border);
}
.sidebar-section.hidden { display: none; }

.sidebar-section textarea {
  width: 100%;
  padding: 8px 10px;
  background: var(--bg-tertiary);
  border: 1px solid var(--border);
  border-radius: 6px;
  color: var(--text-primary);
  font-size: 12px;
  resize: vertical;
  outline: none;
  font-family: inherit;
}
.sidebar-section textarea:focus {
  border-color: var(--accent);
}

.btn-sm {
  margin-top: 6px;
  padding: 5px 12px;
  background: var(--accent);
  color: white;
  border: none;
  border-radius: 6px;
  font-size: 12px;
  cursor: pointer;
}
.btn-sm:hover {
  background: var(--accent-hover);
}

/* Session list */
.session-list {
  flex: 1;
  overflow-y: auto;
  padding: 8px;
}

.session-item {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 10px 12px;
  border-radius: 8px;
  cursor: pointer;
  transition: all 0.15s;
  margin-bottom: 2px;
  position: relative;
}
.session-item:hover {
  background: var(--bg-hover);
}
.session-item.active {
  background: var(--bg-tertiary);
  border: 1px solid var(--border);
}

.session-item .session-icon {
  color: var(--text-muted);
  flex-shrink: 0;
}

.session-item .session-title {
  flex: 1;
  font-size: 13px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.session-item .session-delete {
  opacity: 0;
  background: transparent;
  border: none;
  color: var(--text-muted);
  cursor: pointer;
  padding: 2px;
  border-radius: 4px;
  transition: all 0.15s;
}
.session-item:hover .session-delete {
  opacity: 1;
}
.session-item .session-delete:hover {
  color: var(--danger);
  background: rgba(231, 76, 60, 0.15);
}

.sidebar-footer {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 16px;
  border-top: 1px solid var(--border);
  font-size: 13px;
  color: var(--text-secondary);
}

/* ===================== CHAT AREA ===================== */

#chat-area {
  flex: 1;
  display: flex;
  flex-direction: column;
  height: 100vh;
  overflow: hidden;
}

.welcome {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 16px;
  color: var(--text-muted);
}
.welcome h2 {
  font-size: 22px;
  font-weight: 500;
  color: var(--text-secondary);
}
.welcome p {
  font-size: 14px;
}

/* Messages */
.messages-container {
  flex: 1;
  overflow-y: auto;
  padding: 20px 0;
}
.messages-container.hidden { display: none; }

#messages {
  max-width: 800px;
  margin: 0 auto;
  padding: 0 20px;
}

.message {
  margin-bottom: 24px;
  animation: fadeIn 0.2s ease;
}

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

.message-header {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-bottom: 6px;
}

.message-avatar {
  width: 28px;
  height: 28px;
  border-radius: 6px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 13px;
  font-weight: 600;
  flex-shrink: 0;
}
.message-avatar.user {
  background: #2a5298;
  color: #7db4ff;
}
.message-avatar.assistant {
  background: #6C63FF;
  color: white;
}
.message-avatar.tool {
  background: #2e7d32;
  color: #a5d6a7;
}
.message-avatar.system {
  background: #2a2a4a;
  color: #9e9eff;
}
.message.system .message-body {
  border-left: 2px solid #2a2a4a;
  padding-left: 8px;
}

.message-name {
  font-size: 13px;
  font-weight: 600;
  color: var(--text-secondary);
}

.message-time {
  font-size: 11px;
  color: var(--text-muted);
  margin-left: auto;
}

.message-body {
  padding-left: 36px;
  font-size: 14px;
  line-height: 1.65;
  color: var(--text-primary);
}

.message-body p { margin-bottom: 10px; }
.message-body p:last-child { margin-bottom: 0; }

.message-body ul, .message-body ol {
  margin: 8px 0;
  padding-left: 24px;
}
.message-body li { margin-bottom: 4px; }

.message-body pre {
  background: var(--code-bg);
  border-radius: 8px;
  padding: 14px 16px;
  margin: 10px 0;
  overflow-x: auto;
  font-size: 13px;
  border: 1px solid var(--border);
}
.message-body code {
  font-family: 'Cascadia Code', 'Fira Code', 'Consolas', monospace;
  font-size: 13px;
}
.message-body :not(pre) > code {
  background: var(--bg-tertiary);
  padding: 2px 6px;
  border-radius: 4px;
  font-size: 0.9em;
}

.message-body blockquote {
  border-left: 3px solid var(--accent);
  padding-left: 12px;
  margin: 10px 0;
  color: var(--text-secondary);
}

.message-body table {
  border-collapse: collapse;
  margin: 10px 0;
  width: 100%;
}
.message-body th, .message-body td {
  border: 1px solid var(--border);
  padding: 8px 12px;
  text-align: left;
}
.message-body th {
  background: var(--bg-tertiary);
  font-weight: 600;
}

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

.message-body img {
  max-width: 100%;
  border-radius: 8px;
}

.message-image {
  margin: 8px 0;
}
.message-image img {
  max-width: 320px;
  max-height: 320px;
  border-radius: 8px;
  border: 1px solid var(--border);
  cursor: pointer;
  transition: transform 0.2s;
}
.message-image img:hover {
  transform: scale(1.02);
}

@media (max-width: 768px) {
  .message-image img {
    max-width: 100%;
    max-height: 260px;
  }
}

/* Thinking block */
.thinking-block {
  margin: 8px 0;
  border: 1px solid var(--thinking-border);
  background: var(--thinking-bg);
  border-radius: 8px;
  overflow: hidden;
}

.thinking-header {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 12px;
  cursor: pointer;
  font-size: 12px;
  color: #8888cc;
  user-select: none;
}
.thinking-header:hover {
  background: rgba(58, 58, 106, 0.3);
}

.thinking-header .chevron {
  transition: transform 0.2s;
  font-size: 10px;
}
.thinking-header.open .chevron {
  transform: rotate(90deg);
}

.thinking-content {
  display: none;
  padding: 10px 12px;
  font-size: 13px;
  line-height: 1.5;
  color: #9999cc;
  max-height: 400px;
  overflow-y: auto;
}
.thinking-content.open {
  display: block;
}

/* Tool call block */
.tool-block {
  margin: 8px 0;
  border: 1px solid var(--tool-border);
  background: var(--tool-bg);
  border-radius: 8px;
  overflow: hidden;
}

.tool-header {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 12px;
  cursor: pointer;
  font-size: 12px;
  color: #88cc88;
  user-select: none;
}
.tool-header:hover {
  background: rgba(58, 106, 58, 0.3);
}

.tool-header .chevron {
  transition: transform 0.2s;
  font-size: 10px;
}
.tool-header.open .chevron {
  transform: rotate(90deg);
}

.tool-content {
  display: none;
  padding: 10px 12px;
  font-size: 13px;
  line-height: 1.5;
  color: #99cc99;
  max-height: 400px;
  overflow-y: auto;
}
.tool-content.open {
  display: block;
}
.tool-content pre {
  background: rgba(0,0,0,0.3);
  padding: 8px;
  border-radius: 4px;
  overflow-x: auto;
  white-space: pre-wrap;
}
.zip-download-btn {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  background: rgba(76, 175, 80, 0.2);
  color: #66cc66;
  border: 1px solid rgba(76, 175, 80, 0.4);
  padding: 10px 16px;
  border-radius: 6px;
  text-decoration: none;
  font-size: 13px;
  font-weight: 500;
  transition: background 0.2s, border-color 0.2s;
}
.zip-download-btn:hover {
  background: rgba(76, 175, 80, 0.35);
  border-color: rgba(76, 175, 80, 0.6);
}
.zip-download-btn svg {
  flex-shrink: 0;
}

/* Thinking animation */
.thinking-indicator {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 12px 0;
  color: var(--text-muted);
  font-size: 13px;
}

.thinking-dots {
  display: flex;
  gap: 4px;
}
.thinking-dots span {
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: var(--accent);
  animation: dotPulse 1.4s infinite ease-in-out;
}
.thinking-dots span:nth-child(2) { animation-delay: 0.2s; }
.thinking-dots span:nth-child(3) { animation-delay: 0.4s; }

@keyframes dotPulse {
  0%, 80%, 100% { opacity: 0.3; transform: scale(0.8); }
  40% { opacity: 1; transform: scale(1); }
}

/* ===================== INPUT AREA ===================== */

.input-area {
  padding: 16px 20px 20px;
  border-top: 1px solid var(--border);
}
.input-area.hidden { display: none; }

.file-preview {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 12px;
  background: var(--bg-tertiary);
  border-radius: 8px;
  margin-bottom: 8px;
  font-size: 13px;
  color: var(--text-secondary);
}
.file-preview.hidden { display: none; }

#file-preview-content {
  flex: 1;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.input-row {
  display: flex;
  align-items: flex-end;
  gap: 8px;
}

#message-input {
  flex: 1;
  padding: 12px 16px;
  background: var(--bg-tertiary);
  border: 1px solid var(--border);
  border-radius: 12px;
  color: var(--text-primary);
  font-size: 14px;
  font-family: inherit;
  resize: none;
  outline: none;
  max-height: 200px;
  line-height: 1.5;
  transition: border-color 0.2s;
}
#message-input:focus {
  border-color: var(--accent);
}

.btn-send {
  width: 42px;
  height: 42px;
  background: var(--accent);
  color: white;
  border: none;
  border-radius: 10px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: all 0.2s;
  flex-shrink: 0;
}
.btn-send:hover {
  background: var(--accent-hover);
}
.btn-send:disabled {
  opacity: 0.4;
  cursor: not-allowed;
}

.btn-stop {
  width: 42px;
  height: 42px;
  background: var(--danger);
  color: white;
  border: none;
  border-radius: 10px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: all 0.2s;
  flex-shrink: 0;
}
.btn-stop:hover {
  background: var(--danger-hover);
}
.btn-stop.hidden { display: none; }

/* ===================== RESPONSIVE ===================== */

/* Mobile header - hidden on desktop */
.mobile-header {
  display: none;
  align-items: center;
  gap: 8px;
  padding: 10px 12px;
  background: var(--bg-secondary);
  border-bottom: 1px solid var(--border);
  -webkit-user-select: none;
  user-select: none;
}
.mobile-chat-title {
  flex: 1;
  font-size: 15px;
  font-weight: 600;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

/* Sidebar backdrop */
.sidebar-backdrop {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.6);
  z-index: 99;
  -webkit-tap-highlight-color: transparent;
}
.sidebar-backdrop.visible {
  display: block;
}

@media (max-width: 768px) {
  /* Show mobile header */
  .mobile-header {
    display: flex;
  }

  /* Hide desktop-only new-chat button on sidebar header (mobile has its own) */
  .sidebar-header #new-chat-btn {
    display: none;
  }

  /* Sidebar slides in from left */
  #sidebar {
    position: fixed;
    top: 0;
    left: 0;
    height: 100%;
    height: 100dvh;
    width: min(280px, 85vw);
    min-width: unset;
    z-index: 100;
    transform: translateX(-100%);
    transition: transform 0.3s ease;
    will-change: transform;
  }
  #sidebar.open {
    transform: translateX(0);
  }

  /* Chat area full width */
  #chat-area {
    height: 100vh;
    height: 100dvh;
  }

  /* Messages container with safe areas */
  .messages-container {
    padding: 12px 0;
    padding-bottom: env(safe-area-inset-bottom, 12px);
  }

  #messages {
    padding: 0 12px;
  }

  /* Input area safe area */
  .input-area {
    padding: 10px 12px;
    padding-bottom: calc(10px + env(safe-area-inset-bottom, 0px));
  }

  /* Make input row wrap-friendly */
  .input-row {
    gap: 6px;
  }

  /* Bigger touch targets */
  .btn-send, .btn-stop {
    width: 44px;
    height: 44px;
  }

  .btn-icon {
    width: 40px;
    height: 40px;
  }

  /* Smaller auth container */
  .auth-container {
    width: calc(100vw - 32px);
    max-width: 380px;
    padding: 28px 20px;
    margin: 16px;
  }

  /* Session delete always visible on mobile (no hover) */
  .session-item .session-delete {
    opacity: 0.5;
  }

  /* Code blocks scroll, don't overflow */
  .message-body pre {
    max-width: calc(100vw - 56px);
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
  }

  /* Tables scroll */
  .message-body table {
    display: block;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
  }

  /* Message body padding */
  .message-body {
    padding-left: 0;
    margin-top: 4px;
  }

  /* Message header */
  .message-header {
    gap: 6px;
  }

  /* Welcome screen smaller */
  .welcome h2 {
    font-size: 18px;
    padding: 0 16px;
    text-align: center;
  }
  .welcome p {
    font-size: 13px;
    padding: 0 16px;
    text-align: center;
  }

  /* Thinking/tool blocks tighter */
  .thinking-content, .tool-content {
    max-height: 250px;
  }
}

/* Very small screens */
@media (max-width: 360px) {
  .auth-container {
    padding: 20px 16px;
  }
  #messages {
    padding: 0 8px;
  }
  .input-area {
    padding: 8px;
  }
}

/* Landscape phones - use more space */
@media (max-height: 500px) and (orientation: landscape) {
  .input-area {
    padding: 6px 12px;
  }
  #message-input {
    max-height: 100px;
  }
}
