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

:root {
  --sidebar-width: 260px;
  --bg-primary: #212121;
  --bg-secondary: #171717;
  --bg-tertiary: #2f2f2f;
  --bg-hover: #3a3a3a;
  --text-primary: #ececec;
  --text-secondary: #b4b4b4;
  --text-muted: #8e8e8e;
  --border-color: #3a3a3a;
  --accent: #10a37f;
  --accent-hover: #0d8c6d;
  --user-bubble: #2f2f2f;
  --code-bg: #1e1e1e;
  --scrollbar-thumb: #4a4a4a;
  --danger: #ef4444;
}

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

.app {
  display: flex;
  height: 100vh;
  height: 100dvh;
  min-height: 0;
}

/* Sidebar */
.sidebar {
  width: var(--sidebar-width);
  background: var(--bg-secondary);
  display: flex;
  flex-direction: column;
  border-right: 1px solid var(--border-color);
  transition: transform 0.3s ease;
  flex-shrink: 0;
}

.sidebar.collapsed {
  transform: translateX(calc(-1 * var(--sidebar-width)));
  position: absolute;
  z-index: 10;
  height: 100%;
}

.sidebar-header {
  padding: 12px;
}

.new-chat-btn {
  width: 100%;
  padding: 10px 16px;
  background: transparent;
  border: 1px solid var(--border-color);
  border-radius: 8px;
  color: var(--text-primary);
  font-size: 14px;
  cursor: pointer;
  display: flex;
  align-items: center;
  gap: 8px;
  transition: background 0.2s;
}

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

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

.sidebar-conversations::-webkit-scrollbar {
  width: 6px;
}

.sidebar-conversations::-webkit-scrollbar-thumb {
  background: var(--scrollbar-thumb);
  border-radius: 3px;
}

.conversation-item {
  padding: 10px 12px;
  border-radius: 8px;
  cursor: pointer;
  font-size: 14px;
  color: var(--text-secondary);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  display: flex;
  align-items: center;
  justify-content: space-between;
  transition: background 0.2s;
}

.conversation-item:hover {
  background: var(--bg-tertiary);
}

.conversation-item.active {
  background: var(--bg-tertiary);
  color: var(--text-primary);
}

.conversation-item .conv-title {
  overflow: hidden;
  text-overflow: ellipsis;
  flex: 1;
}

.conversation-item .delete-conv {
  display: none;
  background: none;
  border: none;
  color: var(--text-muted);
  cursor: pointer;
  padding: 2px 4px;
  font-size: 16px;
  line-height: 1;
  border-radius: 4px;
  flex-shrink: 0;
}

.conversation-item:hover .delete-conv {
  display: block;
}

.conversation-item .delete-conv:hover {
  color: var(--danger);
  background: rgba(239, 68, 68, 0.1);
}

.sidebar-footer {
  padding: 12px;
  border-top: 1px solid var(--border-color);
}

.settings-btn {
  width: 100%;
  padding: 10px 16px;
  background: transparent;
  border: none;
  border-radius: 8px;
  color: var(--text-secondary);
  font-size: 14px;
  cursor: pointer;
  display: flex;
  align-items: center;
  gap: 8px;
  transition: background 0.2s;
}

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

/* Main */
.main {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-width: 0;
  min-height: 0;
}

.main-header {
  display: flex;
  align-items: center;
  min-height: 56px;
  padding: max(12px, env(safe-area-inset-top)) 16px 12px;
  gap: 12px;
  border-bottom: 1px solid transparent;
  flex-shrink: 0;
}

.sidebar-toggle {
  background: none;
  border: none;
  color: var(--text-secondary);
  cursor: pointer;
  padding: 4px;
  border-radius: 6px;
  display: flex;
  align-items: center;
}

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

.model-selector {
  position: relative;
}

.model-name {
  font-weight: 600;
  font-size: 16px;
}

.model-selector select {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  opacity: 0;
  cursor: pointer;
}

/* Chat */
.chat-container {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  scroll-behavior: smooth;
  -webkit-overflow-scrolling: touch;
}

.chat-container::-webkit-scrollbar {
  width: 8px;
}

.chat-container::-webkit-scrollbar-thumb {
  background: var(--scrollbar-thumb);
  border-radius: 4px;
}

/* Welcome */
.welcome-screen {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  height: 100%;
  padding: 20px;
  text-align: center;
}

.welcome-screen.hidden {
  display: none;
}

.welcome-icon {
  margin-bottom: 20px;
  color: var(--text-secondary);
}

.welcome-screen h1 {
  font-size: 28px;
  font-weight: 600;
  margin-bottom: 32px;
  color: var(--text-primary);
}

.welcome-suggestions {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 12px;
  max-width: 600px;
  width: 100%;
}

.suggestion {
  padding: 16px;
  background: var(--bg-tertiary);
  border: 1px solid var(--border-color);
  border-radius: 12px;
  color: var(--text-secondary);
  font-size: 14px;
  cursor: pointer;
  text-align: left;
  transition: background 0.2s, border-color 0.2s;
}

.suggestion:hover {
  background: var(--bg-hover);
  border-color: var(--text-muted);
}

/* Messages */
.messages {
  max-width: 768px;
  margin: 0 auto;
  padding: 16px 24px 24px;
  width: 100%;
}

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

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

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

.message-avatar {
  width: 28px;
  height: 28px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 14px;
  font-weight: 600;
  flex-shrink: 0;
}

.message-avatar.user {
  background: #5436da;
  color: white;
}

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

.message-role {
  font-weight: 600;
  font-size: 14px;
}

.message-content {
  padding-left: 36px;
  line-height: 1.7;
  font-size: 15px;
  word-wrap: break-word;
  overflow-wrap: break-word;
}

.message-content p {
  margin-bottom: 12px;
}

.message-content p:last-child {
  margin-bottom: 0;
}

.message-content ul, .message-content ol {
  margin: 8px 0 12px 20px;
}

.message-content li {
  margin-bottom: 4px;
}

.message-content strong {
  font-weight: 600;
}

.message-content h1, .message-content h2, .message-content h3 {
  margin: 16px 0 8px;
  font-weight: 600;
}

.message-content h1 { font-size: 1.4em; }
.message-content h2 { font-size: 1.2em; }
.message-content h3 { font-size: 1.1em; }

.message-content pre {
  background: var(--code-bg);
  border-radius: 8px;
  margin: 12px 0;
  overflow: hidden;
  max-width: 100%;
}

.message-content .code-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 8px 16px;
  background: #2d2d2d;
  font-size: 12px;
  color: var(--text-muted);
}

.message-content .copy-code-btn {
  background: none;
  border: none;
  color: var(--text-muted);
  cursor: pointer;
  font-size: 12px;
  padding: 2px 8px;
  border-radius: 4px;
  transition: color 0.2s, background 0.2s;
}

.message-content .copy-code-btn:hover {
  color: var(--text-primary);
  background: var(--bg-hover);
}

.message-content pre code {
  display: block;
  padding: 16px;
  overflow-x: auto;
  max-width: 100%;
  font-family: 'SF Mono', 'Fira Code', 'Cascadia Code', monospace;
  font-size: 13px;
  line-height: 1.5;
  -webkit-overflow-scrolling: touch;
}

.message-content code {
  background: var(--bg-tertiary);
  padding: 2px 6px;
  border-radius: 4px;
  font-family: 'SF Mono', 'Fira Code', monospace;
  font-size: 13px;
}

.message-content pre code {
  background: transparent;
  padding: 0;
}

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

.message-content table {
  border-collapse: collapse;
  margin: 12px 0;
  display: block;
  width: max-content;
  max-width: 100%;
  overflow-x: auto;
  font-size: 14px;
  -webkit-overflow-scrolling: touch;
}

.message-content th, .message-content td {
  border: 1px solid var(--border-color);
  padding: 8px 12px;
  text-align: left;
}

.message-content th {
  background: var(--bg-tertiary);
  font-weight: 600;
}

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

.message-images {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
  padding-left: 36px;
  margin-bottom: 8px;
}

.message-images img {
  max-width: 200px;
  max-height: 200px;
  border-radius: 8px;
  object-fit: cover;
  cursor: pointer;
  transition: opacity 0.2s;
}

.message-images img:hover {
  opacity: 0.85;
}

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

.typing-indicator span {
  width: 8px;
  height: 8px;
  background: var(--text-muted);
  border-radius: 50%;
  animation: bounce 1.4s infinite ease-in-out;
}

.typing-indicator span:nth-child(2) { animation-delay: 0.16s; }
.typing-indicator span:nth-child(3) { animation-delay: 0.32s; }

@keyframes bounce {
  0%, 80%, 100% { transform: scale(0.6); opacity: 0.4; }
  40% { transform: scale(1); opacity: 1; }
}

/* Input */
.input-area {
  padding: 0 24px calc(16px + env(safe-area-inset-bottom));
  max-width: 768px;
  margin: 0 auto;
  width: 100%;
  flex-shrink: 0;
}

.attachments-preview {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
  margin-bottom: 8px;
}

.attachment-thumb {
  position: relative;
  width: 64px;
  height: 64px;
  border-radius: 8px;
  overflow: hidden;
  border: 1px solid var(--border-color);
}

.attachment-thumb img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.attachment-thumb .remove-attachment {
  position: absolute;
  top: 2px;
  right: 2px;
  width: 20px;
  height: 20px;
  background: rgba(0,0,0,0.7);
  border: none;
  border-radius: 50%;
  color: white;
  font-size: 12px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
}

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

.input-wrapper:focus-within {
  border-color: var(--text-muted);
}

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

.input-wrapper textarea::placeholder {
  color: var(--text-muted);
}

.attach-btn, .send-btn, .stop-btn {
  background: none;
  border: none;
  color: var(--text-muted);
  cursor: pointer;
  width: 36px;
  min-width: 36px;
  height: 36px;
  padding: 4px;
  border-radius: 6px;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: color 0.2s, background 0.2s;
  flex-shrink: 0;
}

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

.send-btn {
  background: var(--accent);
  color: white;
  border-radius: 8px;
  padding: 6px;
  opacity: 0.5;
  pointer-events: none;
}

.send-btn:not(:disabled) {
  opacity: 1;
  pointer-events: auto;
}

.send-btn:not(:disabled):hover {
  background: var(--accent-hover);
}

.stop-btn {
  background: var(--text-secondary);
  color: var(--bg-primary);
  border-radius: 8px;
  padding: 6px;
}

.stop-btn:hover {
  background: var(--text-primary);
}

.hidden {
  display: none !important;
}

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

/* Modal */
.modal {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  z-index: 100;
  display: flex;
  align-items: center;
  justify-content: center;
}

.modal-overlay {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: rgba(0,0,0,0.6);
}

.modal-content {
  position: relative;
  background: var(--bg-secondary);
  border-radius: 16px;
  width: 90%;
  max-width: 560px;
  max-height: 80vh;
  max-height: min(80vh, calc(100dvh - 32px));
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  border: 1px solid var(--border-color);
}

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

.modal-header h2 {
  font-size: 18px;
  font-weight: 600;
}

.modal-close {
  background: none;
  border: none;
  color: var(--text-secondary);
  font-size: 24px;
  cursor: pointer;
  padding: 4px;
  line-height: 1;
}

.modal-body {
  padding: 24px;
}

.setting-section {
  padding: 16px 0 4px;
  border-top: 1px solid var(--border-color);
}

.setting-section:first-child {
  padding-top: 0;
  border-top: none;
}

.setting-section h3 {
  margin-bottom: 14px;
  color: var(--text-primary);
  font-size: 15px;
  font-weight: 600;
}

.setting-group {
  margin-bottom: 20px;
}

.setting-group label {
  display: block;
  font-size: 14px;
  font-weight: 500;
  margin-bottom: 8px;
  color: var(--text-secondary);
}

.setting-group input[type="text"],
.setting-group input[type="password"],
.setting-group input[type="number"],
.setting-group select,
.setting-group textarea {
  width: 100%;
  padding: 10px 12px;
  background: var(--bg-tertiary);
  border: 1px solid var(--border-color);
  border-radius: 8px;
  color: var(--text-primary);
  font-size: 14px;
  font-family: inherit;
  outline: none;
  transition: border-color 0.2s;
}

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

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

.setting-grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 12px;
}

.checkbox-row {
  display: flex;
  align-items: center;
  gap: 10px;
  margin: 0 0 16px;
  color: var(--text-secondary);
  font-size: 14px;
  line-height: 1.4;
  cursor: pointer;
}

.checkbox-row input {
  width: 16px;
  height: 16px;
  accent-color: var(--accent);
  flex-shrink: 0;
}

.api-key-wrapper {
  display: flex;
  gap: 8px;
}

.api-key-wrapper input {
  flex: 1;
}

.toggle-visibility {
  background: var(--bg-tertiary);
  border: 1px solid var(--border-color);
  border-radius: 8px;
  color: var(--text-secondary);
  cursor: pointer;
  padding: 8px;
  display: flex;
  align-items: center;
}

.save-settings-btn {
  width: 100%;
  padding: 12px;
  background: var(--accent);
  border: none;
  border-radius: 8px;
  color: white;
  font-size: 15px;
  font-weight: 500;
  cursor: pointer;
  transition: background 0.2s;
}

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

/* Image lightbox */
.lightbox {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: rgba(0,0,0,0.9);
  z-index: 200;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: zoom-out;
}

.lightbox img {
  max-width: 90%;
  max-height: 90%;
  object-fit: contain;
  border-radius: 8px;
}

/* Error toast */
.error-toast {
  position: fixed;
  bottom: 80px;
  left: 50%;
  transform: translateX(-50%);
  background: var(--danger);
  color: white;
  padding: 12px 24px;
  border-radius: 8px;
  font-size: 14px;
  z-index: 300;
  animation: slideUp 0.3s ease, fadeOut 0.3s ease 3.7s;
  max-width: 90%;
}

@keyframes slideUp {
  from { transform: translateX(-50%) translateY(20px); opacity: 0; }
  to { transform: translateX(-50%) translateY(0); opacity: 1; }
}

@keyframes fadeOut {
  to { opacity: 0; }
}

/* Sidebar overlay */
.sidebar-overlay {
  display: none;
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: rgba(0,0,0,0.5);
  z-index: 9;
}

.sidebar-overlay.active {
  display: block;
}

/* Responsive */
@media (max-width: 768px) {
  :root {
    --sidebar-width: min(84vw, 320px);
  }

  .app {
    width: 100vw;
  }

  .sidebar {
    position: fixed;
    z-index: 10;
    top: 0;
    left: 0;
    height: 100vh;
    height: 100dvh;
    padding-top: env(safe-area-inset-top);
    padding-bottom: env(safe-area-inset-bottom);
    transform: translateX(calc(-1 * var(--sidebar-width)));
    box-shadow: 16px 0 40px rgba(0, 0, 0, 0.35);
  }

  .sidebar.open {
    transform: translateX(0);
  }

  .sidebar-header {
    padding: 12px;
  }

  .sidebar-footer {
    padding: 12px;
  }

  .new-chat-btn,
  .settings-btn,
  .conversation-item {
    min-height: 44px;
  }

  .main-header {
    min-height: calc(52px + env(safe-area-inset-top));
    padding: env(safe-area-inset-top) 12px 8px;
    background: var(--bg-primary);
    border-bottom-color: var(--border-color);
  }

  .sidebar-toggle {
    width: 40px;
    height: 40px;
    justify-content: center;
    flex-shrink: 0;
  }

  .model-selector {
    min-width: 0;
  }

  .model-name {
    display: block;
    max-width: calc(100vw - 84px);
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    font-size: 15px;
  }

  .welcome-screen {
    justify-content: flex-start;
    min-height: 100%;
    padding: 44px 16px 24px;
  }

  .welcome-icon {
    margin-bottom: 16px;
  }

  .welcome-screen h1 {
    margin-bottom: 24px;
    font-size: 24px;
    line-height: 1.3;
  }

  .welcome-suggestions {
    grid-template-columns: 1fr;
    gap: 10px;
  }

  .suggestion {
    min-height: 48px;
    padding: 14px;
  }

  .messages {
    padding: 16px 14px 20px;
  }

  .message {
    margin-bottom: 22px;
  }

  .message-content {
    padding-left: 0;
    font-size: 15px;
    line-height: 1.65;
  }

  .message-images {
    padding-left: 0;
  }

  .message-images img {
    max-width: min(100%, 220px);
    max-height: 220px;
  }

  .input-area {
    padding: 8px 10px calc(10px + env(safe-area-inset-bottom));
    background: var(--bg-primary);
    border-top: 1px solid var(--border-color);
  }

  .input-wrapper {
    align-items: flex-end;
    gap: 6px;
    border-radius: 14px;
    padding: 6px 8px;
  }

  .input-wrapper textarea {
    max-height: 140px;
    padding: 7px 0;
  }

  .attach-btn,
  .send-btn,
  .stop-btn {
    width: 40px;
    min-width: 40px;
    height: 40px;
  }

  .disclaimer {
    margin-top: 6px;
    font-size: 11px;
    line-height: 1.35;
  }

  .modal {
    align-items: flex-end;
  }

  .modal-content {
    width: 100%;
    max-width: none;
    max-height: calc(100dvh - env(safe-area-inset-top));
    border-right: none;
    border-bottom: none;
    border-left: none;
    border-radius: 16px 16px 0 0;
  }

  .modal-header {
    position: sticky;
    top: 0;
    z-index: 1;
    padding: 16px;
    background: var(--bg-secondary);
  }

  .modal-close {
    min-width: 40px;
    min-height: 40px;
  }

  .modal-body {
    padding: 18px 16px calc(24px + env(safe-area-inset-bottom));
  }

  .api-key-wrapper {
    align-items: stretch;
  }

  .toggle-visibility {
    min-width: 44px;
    justify-content: center;
  }

  .setting-grid {
    grid-template-columns: 1fr;
    gap: 0;
  }

  .error-toast {
    bottom: calc(96px + env(safe-area-inset-bottom));
    width: calc(100% - 24px);
    padding: 12px 16px;
    text-align: center;
  }
}

@media (max-width: 380px) {
  .welcome-screen h1 {
    font-size: 22px;
  }

  .messages {
    padding-right: 12px;
    padding-left: 12px;
  }

  .suggestion {
    font-size: 13px;
  }
}
