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

:root {
  --bg: #0f0f0f;
  --surface: #1a1a1a;
  --surface-hover: #252525;
  --border: #2a2a2a;
  --text: #e0e0e0;
  --text-muted: #888;
  --primary: #4a9eff;
  --primary-hover: #3a8eef;
  --assistant-bg: #1e2a3a;
  --user-bg: #2a2a2a;
  --danger: #ff4a4a;
}

body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
  height: 100dvh;
  overflow: hidden;
}

#app {
  height: 100%;
}

.screen {
  display: none;
  height: 100%;
  flex-direction: column;
}
.screen.active {
  display: flex;
}

.hidden {
  display: none !important;
}

/* --- Session Screen --- */

.center-card {
  max-width: 400px;
  margin: auto;
  padding: 2rem;
  text-align: center;
}

.center-card h1 {
  font-size: 1.5rem;
  margin-bottom: 0.5rem;
}

.center-card p {
  color: var(--text-muted);
  margin-bottom: 1.5rem;
}

.session-buttons {
  display: flex;
  gap: 0.75rem;
  justify-content: center;
  margin-bottom: 1rem;
}

#custom-skills {
  margin-bottom: 0.5rem;
}

.skills-label {
  color: var(--text-muted);
  font-size: 0.8rem;
  margin-bottom: 0.35rem;
}

.skills-list {
  display: flex;
  flex-wrap: wrap;
  gap: 0.25rem;
  justify-content: center;
}

.skill-tag {
  display: inline-flex;
  align-items: center;
  gap: 0.25rem;
  padding: 0.2rem 0.5rem;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
  font-size: 0.75rem;
  color: var(--text-muted);
}

.skill-tag.custom {
  border-color: var(--primary);
  border-style: dashed;
}

.btn-promote {
  background: none;
  border: none;
  color: var(--primary);
  cursor: pointer;
  font-size: 0.7rem;
  padding: 0;
  line-height: 1;
}
.btn-promote:hover {
  color: #fff;
}

#existing-sessions {
  margin-top: 1rem;
}

/* --- Login Form --- */

#login-form {
  display: flex;
  flex-direction: column;
  gap: 0.75rem;
}

#login-form input {
  padding: 0.65rem 0.75rem;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 8px;
  color: var(--text);
  font-size: 0.9rem;
  font-family: inherit;
}

#login-form input:focus {
  outline: none;
  border-color: var(--primary);
}

.error-text {
  color: var(--danger);
  font-size: 0.85rem;
}

.session-item {
  display: flex;
  align-items: center;
  padding: 0.6rem 0.75rem;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 8px;
  margin-bottom: 0.35rem;
  transition: background 0.15s;
}
.session-item:hover {
  background: var(--surface-hover);
}

.session-item .session-info {
  display: flex;
  align-items: baseline;
  gap: 0.5rem;
  overflow: hidden;
  flex: 1;
  cursor: pointer;
  min-width: 0;
}

.session-item .session-name {
  font-size: 0.85rem;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.session-item .session-meta {
  font-size: 0.7rem;
  color: var(--text-muted);
  white-space: nowrap;
  flex-shrink: 0;
}

.session-item .session-actions {
  display: flex;
  align-items: center;
  gap: 0.35rem;
  flex-shrink: 0;
  margin-left: 0.5rem;
  opacity: 0;
  transition: opacity 0.15s;
}
.session-item:hover .session-actions {
  opacity: 1;
}

.btn-icon {
  background: none;
  border: none;
  color: var(--text-muted);
  cursor: pointer;
  font-size: 0.8rem;
  padding: 0.2rem;
  line-height: 1;
  border-radius: 4px;
  transition: color 0.15s, background 0.15s;
}
.btn-icon:hover {
  color: var(--text);
  background: var(--surface-hover);
}
.btn-icon.btn-kill:hover {
  color: var(--danger);
  background: rgba(255, 74, 74, 0.1);
}

.rename-input {
  background: var(--bg);
  border: 1px solid var(--primary);
  border-radius: 4px;
  color: var(--text);
  font-size: 0.85rem;
  padding: 0.15rem 0.4rem;
  font-family: inherit;
  width: 100%;
  outline: none;
}

.session-item .status {
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: var(--border);
}
.session-item .status.alive {
  background: #4caf50;
}

/* --- Buttons --- */

.btn {
  padding: 0.5rem 1rem;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--surface);
  color: var(--text);
  cursor: pointer;
  font-size: 0.875rem;
  transition: background 0.15s;
}
.btn:hover {
  background: var(--surface-hover);
}
.btn.primary {
  background: var(--primary);
  border-color: var(--primary);
  color: #fff;
}
.btn.primary:hover {
  background: var(--primary-hover);
}
.btn.small {
  padding: 0.35rem 0.75rem;
  font-size: 0.8rem;
}

/* --- Chat Screen --- */

header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0.75rem 1rem;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}

#session-label {
  font-weight: 600;
  font-size: 0.9rem;
}

.header-actions {
  display: flex;
  gap: 0.5rem;
  align-items: center;
}

.model-select {
  padding: 0.3rem 0.5rem;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 6px;
  color: var(--text-muted);
  font-size: 0.75rem;
  cursor: pointer;
}

.model-select:focus {
  outline: none;
  border-color: var(--primary);
}

/* --- Messages --- */

#messages {
  flex: 1;
  overflow-y: auto;
  padding: 1rem;
  display: flex;
  flex-direction: column;
  gap: 0.75rem;
}

.message {
  max-width: 85%;
  padding: 0.75rem 1rem;
  border-radius: 12px;
  line-height: 1.5;
  font-size: 0.9rem;
  word-wrap: break-word;
}

.message.user {
  white-space: pre-wrap;
}

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

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

.message.system {
  align-self: center;
  color: var(--text-muted);
  font-size: 0.8rem;
  font-style: italic;
}

/* --- Markdown in assistant messages --- */

.message.assistant .content h1,
.message.assistant .content h2,
.message.assistant .content h3 {
  margin-top: 0.75rem;
  margin-bottom: 0.25rem;
}
.message.assistant .content h1 { font-size: 1.1rem; }
.message.assistant .content h2 { font-size: 1rem; }
.message.assistant .content h3 { font-size: 0.95rem; }

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

.message.assistant .content ul,
.message.assistant .content ol {
  margin: 0.25rem 0 0.5rem 1.25rem;
}

.message.assistant .content li {
  margin-bottom: 0.15rem;
}

.message.assistant .content code {
  background: rgba(255, 255, 255, 0.08);
  padding: 0.15rem 0.35rem;
  border-radius: 4px;
  font-family: "SF Mono", Monaco, monospace;
  font-size: 0.85em;
}

.message.assistant .content pre {
  background: rgba(0, 0, 0, 0.3);
  padding: 0.75rem;
  border-radius: 6px;
  overflow-x: auto;
  margin: 0.5rem 0;
}

.message.assistant .content pre code {
  background: none;
  padding: 0;
  font-size: 0.85rem;
}

.message.assistant .content strong {
  color: #fff;
}

.message.assistant .content a {
  color: var(--primary);
}

.message.assistant .content blockquote {
  border-left: 3px solid var(--border);
  padding-left: 0.75rem;
  margin: 0.5rem 0;
  color: var(--text-muted);
}

.message.assistant .content table {
  border-collapse: collapse;
  margin: 0.5rem 0;
  width: 100%;
}

.message.assistant .content th,
.message.assistant .content td {
  border: 1px solid var(--border);
  padding: 0.35rem 0.5rem;
  font-size: 0.85rem;
  text-align: left;
}

.message.assistant .content th {
  background: rgba(255, 255, 255, 0.05);
}

.message .tool-call {
  font-size: 0.8rem;
  color: var(--text-muted);
  margin-top: 0.5rem;
  padding: 0.5rem;
  background: rgba(0, 0, 0, 0.2);
  border-radius: 6px;
  font-family: "SF Mono", Monaco, monospace;
  max-height: 200px;
  overflow-y: auto;
}

/* --- Input Area --- */

/* --- Working Indicator --- */

#working-indicator {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  padding: 0.4rem 0.75rem;
  font-size: 0.8rem;
  color: var(--text-muted);
  width: 100%;
}

.spinner {
  width: 14px;
  height: 14px;
  border: 2px solid var(--border);
  border-top-color: var(--primary);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}

@keyframes spin {
  to { transform: rotate(360deg); }
}

#input-area {
  display: flex;
  flex-wrap: wrap;
  align-items: flex-end;
  gap: 0.5rem;
  padding: 0.75rem 1rem;
  background: var(--surface);
  border-top: 1px solid var(--border);
}

.upload-zone {
  flex-shrink: 0;
}

.upload-label {
  cursor: pointer;
  font-size: 1.2rem;
  padding: 0.4rem;
  display: block;
}

#upload-preview {
  padding: 0.4rem 0.75rem;
  background: var(--surface-hover);
  border-radius: 6px;
  font-size: 0.8rem;
  display: flex;
  align-items: center;
  gap: 0.5rem;
}

#upload-preview .remove {
  cursor: pointer;
  color: var(--danger);
}

#message-input {
  flex: 1;
  padding: 0.6rem 0.75rem;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 8px;
  color: var(--text);
  font-size: 0.9rem;
  font-family: inherit;
  resize: none;
  min-height: 40px;
  max-height: 150px;
}

#message-input:focus {
  outline: none;
  border-color: var(--primary);
}

/* --- Files Panel --- */

#files-panel {
  position: absolute;
  bottom: 70px;
  right: 1rem;
  width: 320px;
  max-height: 400px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
  overflow: hidden;
  z-index: 10;
}

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

.panel-header h3 {
  font-size: 0.9rem;
}

#files-list {
  padding: 0.5rem;
  max-height: 340px;
  overflow-y: auto;
}

.file-item {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0.5rem 0.75rem;
  border-radius: 6px;
  cursor: pointer;
  transition: background 0.15s;
}
.file-item:hover {
  background: var(--surface-hover);
}

.file-item .name {
  font-size: 0.85rem;
  word-break: break-all;
}
.file-item .size {
  font-size: 0.75rem;
  color: var(--text-muted);
  flex-shrink: 0;
  margin-left: 0.5rem;
}

/* --- Extension UI --- */

#extension-ui {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.6);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 20;
}

#extension-ui .dialog {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 1.5rem;
  max-width: 400px;
  width: 90%;
}

#extension-ui .dialog h3 {
  margin-bottom: 0.75rem;
}

#extension-ui .dialog p {
  color: var(--text-muted);
  margin-bottom: 1rem;
  font-size: 0.9rem;
}

#extension-ui .dialog .options {
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
}

#extension-ui .dialog .options .btn {
  text-align: left;
}

#extension-ui .dialog input[type="text"] {
  width: 100%;
  padding: 0.6rem 0.75rem;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 8px;
  color: var(--text);
  font-size: 0.9rem;
  margin-bottom: 0.75rem;
}

#extension-ui .dialog .dialog-actions {
  display: flex;
  gap: 0.5rem;
  justify-content: flex-end;
  margin-top: 1rem;
}

/* --- Scrollbar --- */

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

/* --- Mobile --- */

@media (max-width: 640px) {
  .message {
    max-width: 95%;
  }

  #files-panel {
    left: 1rem;
    width: auto;
  }
}

/* Chat screen needs relative positioning for files panel */
#chat-screen {
  position: relative;
}
