/* CSS Variables - Dark Theme */
:root {
  --bg-primary: #1a1a2e;
  --bg-secondary: #16213e;
  --bg-tertiary: #0f3460;
  --bg-input: #1e2a4a;
  --bg-hover: #243554;
  --bg-message-user: #3B82F6;
  --bg-message-assistant: #2a2a4a;
  --bg-code: #0d1117;
  --text-primary: #eaeaea;
  --text-secondary: #a0a0a0;
  --text-muted: #6b7280;
  --accent: #3B82F6;
  --accent-hover: #2563eb;
  --error: #EF4444;
  --error-bg: #7f1d1d;
  --warning: #F59E0B;
  --warning-bg: #78350f;
  --success: #22C55E;
  --success-bg: #14532d;
  --border: #374151;
  --border-light: #4b5563;
  --shadow: rgba(0, 0, 0, 0.3);
  --context-normal: #22C55E;
  --context-warning: #F59E0B;
  --context-limit: #EF4444;
  --scrollbar-track: #1a1a2e;
  --scrollbar-thumb: #374151;
  --modal-backdrop: rgba(0, 0, 0, 0.7);
}

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

/* Base */
html, body {
  height: 100%;
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, sans-serif;
  font-size: 16px;
  line-height: 1.5;
  color: var(--text-primary);
  background-color: var(--bg-primary);
}

/* Scrollbar */
::-webkit-scrollbar {
  width: 8px;
  height: 8px;
}

::-webkit-scrollbar-track {
  background: var(--scrollbar-track);
}

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

::-webkit-scrollbar-thumb:hover {
  background: var(--border-light);
}

/* Login Screen */
#login-screen {
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 100vh;
  padding: 1rem;
}

#login-form {
  display: flex;
  flex-direction: column;
  gap: 1rem;
  width: 100%;
  max-width: 320px;
  padding: 2rem;
  background: var(--bg-secondary);
  border-radius: 8px;
  box-shadow: 0 4px 20px var(--shadow);
}

#login-form h1 {
  text-align: center;
  color: var(--accent);
  margin-bottom: 0.5rem;
}

#login-form input {
  padding: 0.75rem 1rem;
  background: var(--bg-input);
  border: 1px solid var(--border);
  border-radius: 4px;
  color: var(--text-primary);
  font-size: 1rem;
}

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

#login-form button {
  padding: 0.75rem 1rem;
  background: var(--accent);
  border: none;
  border-radius: 4px;
  color: white;
  font-size: 1rem;
  font-weight: 600;
  cursor: pointer;
  transition: background 0.2s;
}

#login-form button:hover:not(:disabled) {
  background: var(--accent-hover);
}

#login-form button:disabled {
  opacity: 0.6;
  cursor: not-allowed;
}

#login-error {
  color: var(--error);
  font-size: 0.875rem;
  text-align: center;
  min-height: 1.25rem;
}

/* App Layout */
#app {
  display: flex;
  height: 100vh;
  overflow: hidden;
}

/* Sidebar */
#sidebar {
  display: flex;
  flex-direction: column;
  width: 280px;
  min-width: 280px;
  background: var(--bg-secondary);
  border-right: 1px solid var(--border);
}

#new-chat-btn {
  margin: 1rem;
  padding: 0.75rem 1rem;
  background: var(--accent);
  border: none;
  border-radius: 4px;
  color: white;
  font-size: 0.9rem;
  font-weight: 600;
  cursor: pointer;
  transition: background 0.2s;
}

#new-chat-btn:hover {
  background: var(--accent-hover);
}

#chat-list {
  flex: 1;
  overflow-y: auto;
  padding: 0 0.5rem;
}

.chat-item {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  padding: 0.75rem;
  margin-bottom: 0.25rem;
  border-radius: 4px;
  cursor: pointer;
  transition: background 0.2s;
  border-left: 3px solid transparent;
}

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

.chat-item.active {
  background: var(--bg-tertiary);
}

.chat-item-star {
  color: var(--warning);
  font-size: 0.9rem;
  flex-shrink: 0;
  margin-right: 0.25rem;
}

.chat-item-icon {
  font-size: 1.25rem;
  flex-shrink: 0;
}

.chat-item-info {
  flex: 1;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  min-width: 0;
}

.chat-item-title {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  font-size: 0.9rem;
}

.chat-item-template {
  font-size: 0.7rem;
  color: var(--text-muted);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.chat-list-empty {
  padding: 1rem;
  text-align: center;
  color: var(--text-muted);
  font-size: 0.9rem;
}

#sidebar-footer {
  display: flex;
  gap: 0.5rem;
  padding: 1rem;
  border-top: 1px solid var(--border);
}

#sidebar-footer button {
  flex: 1;
  padding: 0.5rem;
  background: var(--bg-tertiary);
  border: 1px solid var(--border);
  border-radius: 4px;
  color: var(--text-primary);
  font-size: 0.85rem;
  cursor: pointer;
  transition: background 0.2s;
}

#sidebar-footer button:hover {
  background: var(--bg-hover);
}

/* Main Content */
#main {
  flex: 1;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  background: var(--bg-primary);
}

/* Empty State */
#empty-state {
  display: flex;
  align-items: center;
  justify-content: center;
  flex: 1;
  color: var(--text-secondary);
  font-size: 1.1rem;
}

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

#chat-header {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  padding: 1rem 1.5rem;
  background: var(--bg-secondary);
  border-bottom: 1px solid var(--border);
}

#chat-icon {
  font-size: 1.5rem;
}

#chat-header-info {
  flex: 1;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  min-width: 0;
}

#chat-title {
  font-size: 1.1rem;
  font-weight: 600;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

#chat-template-name {
  font-size: 0.75rem;
  color: var(--text-muted);
}

#chat-shared-with {
  font-size: 0.7rem;
  color: var(--text-muted);
  opacity: 0.8;
}

#chat-favorite-btn {
  padding: 0.5rem;
  background: transparent;
  border: none;
  color: var(--text-secondary);
  font-size: 1.25rem;
  cursor: pointer;
  transition: color 0.2s;
}

#chat-favorite-btn:hover {
  color: var(--warning);
}

#chat-settings-btn {
  padding: 0.5rem 1rem;
  background: var(--bg-tertiary);
  border: 1px solid var(--border);
  border-radius: 4px;
  color: var(--text-primary);
  font-size: 0.85rem;
  cursor: pointer;
}

#chat-settings-btn:hover {
  background: var(--bg-hover);
}

/* Messages Container */
#messages-container {
  flex: 1;
  overflow-y: auto;
  padding: 1rem 1.5rem;
  display: flex;
  flex-direction: column;
  gap: 1rem;
}

/* Context Boundary Banner */
#context-boundary-banner {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  padding: 0.5rem 1.5rem;
  background: var(--warning-bg);
  border-bottom: 1px solid var(--warning);
  color: var(--warning);
  font-size: 0.875rem;
  cursor: pointer;
  flex-shrink: 0;
}

#context-boundary-banner:hover {
  opacity: 0.85;
}

.banner-text {
  flex: 1;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.banner-time {
  flex-shrink: 0;
  color: var(--text-muted);
  font-size: 0.8rem;
}

/* Message */
.message {
  max-width: 80%;
  padding: 0.75rem 1rem;
  border-radius: 8px;
  position: relative;
}

.message.user {
  align-self: flex-end;
  background: var(--bg-message-user);
  color: white;
}

.message.assistant {
  align-self: flex-start;
  background: var(--bg-message-assistant);
}

.message.out-of-context {
  opacity: 0.4;
}

.message.in-context {
  border-left: 2px solid var(--context-normal);
}

.message.pinned {
  border-left: 2px solid var(--warning);
  background: rgba(34, 197, 94, 0.07) !important;
}

.pin-icon {
  position: absolute;
  top: 0.25rem;
  left: 0.25rem;
  font-size: 0.7rem;
  opacity: 0.7;
}

.message-actions .pin-btn.active {
  color: var(--warning);
}

.message-content {
  white-space: pre-wrap;
  word-wrap: break-word;
}

.message-content pre {
  background: var(--bg-code);
  padding: 0.75rem;
  border-radius: 4px;
  overflow-x: auto;
  margin: 0.5rem 0;
}

.message-content code {
  font-family: 'Monaco', 'Menlo', 'Ubuntu Mono', monospace;
  font-size: 0.9em;
}

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

.message-meta {
  margin-top: 0.5rem;
  font-size: 0.75rem;
  color: var(--text-muted);
  opacity: 0.8;
}

.message.user .message-meta {
  text-align: right;
}

.message-content code:not(pre code) {
  background: var(--bg-code);
  padding: 0.1rem 0.3rem;
  border-radius: 3px;
}

.message-actions {
  display: none;
  position: absolute;
  top: 0.25rem;
  right: 0.25rem;
  gap: 0.25rem;
}

.message:hover .message-actions {
  display: flex;
}

.message-actions button {
  padding: 0.25rem 0.5rem;
  background: var(--bg-tertiary);
  border: 1px solid var(--border);
  border-radius: 3px;
  color: var(--text-secondary);
  font-size: 0.75rem;
  cursor: pointer;
}

.message-actions button:hover {
  background: var(--bg-hover);
  color: var(--text-primary);
}

.message[data-editing="true"] {
  max-width: 90%;
  width: 90%;
}

.message-edit-textarea {
  width: 100%;
  min-height: 60px;
  max-height: 400px;
  padding: 0.5rem;
  background: var(--bg-input);
  border: 1px solid var(--border);
  border-radius: 4px;
  color: var(--text-primary);
  font-family: inherit;
  font-size: inherit;
  resize: vertical;
  overflow-y: auto;
  line-height: 1.5;
}

/* Collapsed Messages (tool results, sensors) */
.message.collapsed-message {
  max-width: 80%;
  padding: 0;
}

.message.collapsed-message.ttl-stale {
  border-left-color: var(--warning);
  background: var(--warning-bg);
}

.collapsed-header {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 12px;
  cursor: pointer;
  transition: background 0.15s;
}
.collapsed-header:hover { background: var(--bg-tertiary); }

.collapsed-expand,
.collapsed-age {
  user-select: none;
}

.collapsed-expand {
  font-size: 0.7rem;
  color: var(--text-secondary);
}

.collapsed-title {
  flex-shrink: 0;
  font-size: 0.9rem;
  color: var(--text-primary);
  font-weight: 500;
}

.collapsed-preview {
  flex: 1;
  min-width: 0;
  font-size: 0.82rem;
  color: var(--text-secondary);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  padding-left: 4px;
}

.collapsed-age {
  flex-shrink: 0;
  font-size: 0.75rem;
  color: var(--text-secondary);
  white-space: nowrap;
}

.collapsed-modal-content {
  max-height: 60vh;
  overflow-y: auto;
}

/* Edit mode */
.collapsed-message.editing {
  padding: 8px;
  width: 90%;
  max-width: 90%;
  align-self: flex-start;
}

.collapsed-edit-textarea {
  width: 100%;
  min-height: 200px;
  background: var(--bg-primary);
  color: var(--text-primary);
  border: 1px solid var(--bg-tertiary);
  border-radius: 4px;
  padding: 8px;
  font-family: monospace;
  font-size: 0.85rem;
  box-sizing: border-box;
  resize: vertical;
}

/* Tool Progress (during execution) */
.tool-progress {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  padding: 0.5rem 0.75rem;
  color: var(--text-secondary);
  font-size: 0.9rem;
  font-style: italic;
}

/* Buffering progress indicator (shown in placeholder when response is JSON-buffered) */
.buffering-count {
  color: var(--text-secondary);
  font-size: 0.85rem;
  font-style: italic;
  opacity: 0.75;
}

/* Tool Settings in Chat Settings */
.tools-list {
  border: 1px solid var(--border);
  border-radius: 4px;
  padding: 0.5rem;
  background: var(--bg-secondary);
}

.tool-item {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  padding: 0.25rem;
  cursor: pointer;
  border-radius: 2px;
}

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

.tool-item-desc {
  font-size: 0.8rem;
  color: var(--text-muted);
  padding-left: 1.5rem;
  margin-bottom: 0.25rem;
}

.tool-checkbox {
  accent-color: var(--accent);
}

.tool-usage {
  font-size: 0.8rem;
  color: var(--text-muted);
  margin-left: 0.25rem;
}

.tool-usage.warning {
  color: var(--warning);
}

/* Context Indicator */
#context-indicator {
  padding: 0.5rem 1.5rem;
  background: var(--bg-secondary);
  border-top: 1px solid var(--border);
}

#context-bar {
  height: 6px;
  background: var(--bg-tertiary);
  border-radius: 3px;
  overflow: hidden;
}

#context-fill {
  height: 100%;
  background: var(--context-normal);
  transition: width 0.3s, background 0.3s;
}

#context-fill.warning {
  background: var(--context-warning);
}

#context-fill.limit {
  background: var(--context-limit);
}

#context-text {
  display: block;
  margin-top: 0.25rem;
  font-size: 0.75rem;
  color: var(--text-secondary);
}

/* Memory Panel */
#memory-panel {
  background: var(--bg-secondary);
  border-top: 1px solid var(--border);
}

#memory-panel-bar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0.35rem 1.5rem;
  cursor: pointer;
  user-select: none;
  font-size: 0.75rem;
  color: var(--text-secondary);
}

#memory-panel-bar:hover {
  background: var(--bg-tertiary);
}

#memory-panel-toggle {
  font-size: 0.65rem;
  color: var(--text-muted);
}

#memory-panel-content {
  padding: 0.5rem 1.5rem 0.75rem;
}

.memory-block-row {
  display: flex;
  gap: 0.4rem;
  margin-bottom: 0.4rem;
  align-items: flex-start;
}

.memory-block-textarea {
  flex: 1;
  resize: none;
  overflow-y: auto;
  font-size: 0.82rem;
  font-family: inherit;
  background: var(--bg-primary);
  border: 1px solid var(--border);
  border-radius: 4px;
  padding: 0.3rem 0.5rem;
  color: var(--text-primary);
  min-height: 2rem;
  max-height: 8rem;
}

.memory-block-textarea:focus {
  outline: none;
  border-color: var(--accent, #3B82F6);
}

.memory-block-delete {
  flex-shrink: 0;
  background: none;
  border: none;
  color: var(--text-muted);
  cursor: pointer;
  padding: 0.2rem 0.35rem;
  border-radius: 4px;
  font-size: 0.85rem;
  line-height: 1;
  margin-top: 0.3rem;
}

.memory-block-delete:hover {
  background: var(--bg-tertiary);
  color: var(--danger, #ef4444);
}

#memory-add-btn {
  display: block;
  width: 100%;
  background: none;
  border: 1px dashed var(--border);
  color: var(--text-muted);
  cursor: pointer;
  padding: 0.25rem 0.5rem;
  border-radius: 4px;
  font-size: 0.78rem;
  text-align: left;
  margin-top: 0.25rem;
}

#memory-add-btn:hover {
  background: var(--bg-tertiary);
  border-color: var(--text-secondary);
  color: var(--text-secondary);
}

/* Input Container */
#input-container {
  display: flex;
  flex-direction: column;
  padding: 1rem 1.5rem;
  background: var(--bg-secondary);
  gap: 0;
}

#input-image-preview {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 6px 8px;
  background: var(--bg-tertiary);
  border-radius: 6px 6px 0 0;
  border-bottom: 1px solid var(--border);
  margin-bottom: 0;
}

#input-image-preview .pending-img-thumb {
  max-height: 56px;
  max-width: 100px;
  border-radius: 4px;
  object-fit: cover;
}

#input-image-preview .img-spinner {
  display: inline-block;
  width: 16px;
  height: 16px;
  border: 2px solid var(--border);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
  flex-shrink: 0;
}

#input-image-preview .img-status {
  font-size: 0.8rem;
  color: var(--text-secondary);
}

#input-image-preview .pending-img-remove {
  margin-left: auto;
  background: none;
  border: none;
  cursor: pointer;
  color: var(--text-secondary);
  font-size: 1rem;
  padding: 2px 6px;
  border-radius: 3px;
  line-height: 1;
}

#input-image-preview .pending-img-remove:hover {
  background: var(--bg-hover);
  color: var(--text-primary);
}

#input-row {
  display: flex;
  gap: 0.75rem;
  align-items: flex-end;
}

#input-buttons {
  display: flex;
  flex-direction: column;
  gap: 0.4rem;
}

#message-input {
  flex: 1;
  padding: 0.75rem 1rem;
  background: var(--bg-input);
  border: 1px solid var(--border);
  border-radius: 4px;
  color: var(--text-primary);
  font-family: inherit;
  font-size: 1rem;
  resize: none;
  max-height: 50dvh;
  overflow-y: auto;
}

#input-image-preview:not([hidden]) + #input-row #message-input {
  border-top-left-radius: 0;
  border-top-right-radius: 0;
}

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

#message-input:disabled {
  opacity: 0.6;
  cursor: not-allowed;
}

#send-btn {
  padding: 0.75rem 1.5rem;
  background: var(--accent);
  border: none;
  border-radius: 4px;
  color: white;
  font-size: 1rem;
  font-weight: 600;
  cursor: pointer;
  transition: background 0.2s;
  white-space: nowrap;
}

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

#send-btn:disabled {
  opacity: 0.6;
  cursor: not-allowed;
}

#save-btn {
  padding: 0.5rem 1rem;
  background: var(--bg-tertiary);
  border: 1px solid var(--border);
  border-radius: 4px;
  color: var(--text-secondary);
  font-size: 1rem;
  cursor: pointer;
  transition: background 0.2s, color 0.2s;
  white-space: nowrap;
}

#save-btn:hover {
  background: var(--bg-hover);
  color: var(--text-primary);
}

.btn-hint {
  display: block;
  font-size: 0.65rem;
  font-weight: 400;
  color: rgba(0, 0, 0, 0.5);
  line-height: 1;
  margin-top: 0.15rem;
}

#send-btn.interrupt {
  background: var(--error);
}

#send-btn.interrupt:hover:not(:disabled) {
  background: #dc2626;
}

#input-warning {
  padding: 0 1.5rem 0.5rem;
  font-size: 0.85rem;
  min-height: 1.5rem;
}

#input-warning.warning {
  color: var(--warning);
}

#input-warning.error {
  color: var(--error);
}

/* Template Editor */
#template-editor {
  display: flex;
  flex: 1;
  overflow: hidden;
  position: relative;
}

#template-list-panel {
  width: 280px;
  min-width: 280px;
  background: var(--bg-secondary);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  padding: 1rem;
}

#template-list-panel h2 {
  margin-bottom: 1rem;
  font-size: 1.1rem;
}

#new-template-btn {
  margin-bottom: 1rem;
  padding: 0.5rem 1rem;
  background: var(--accent);
  border: none;
  border-radius: 4px;
  color: white;
  font-size: 0.9rem;
  cursor: pointer;
}

#new-template-btn:hover {
  background: var(--accent-hover);
}

#template-list {
  flex: 1;
  overflow-y: auto;
}

.template-item {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  padding: 0.75rem;
  border-radius: 4px;
  border-left: 3px solid var(--accent);
  cursor: pointer;
  transition: background 0.2s;
  margin-bottom: 0.25rem;
}

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

.template-item.active {
  background: var(--bg-tertiary);
}

.template-item.default {
  opacity: 0.7;
}

.template-item-icon {
  font-size: 1.25rem;
}

.template-item-name {
  flex: 1;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

#template-form-panel {
  flex: 1;
  padding: 1.5rem;
  overflow-y: auto;
}

#template-form-placeholder {
  display: flex;
  align-items: center;
  justify-content: center;
  height: 100%;
  color: var(--text-secondary);
}

#template-form {
  display: flex;
  flex-direction: column;
  gap: 1rem;
  max-width: 500px;
}

#template-form label {
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
  font-size: 0.9rem;
  color: var(--text-secondary);
}

#template-form input[type="text"],
#template-form input[type="number"],
#template-form textarea,
#template-form select {
  padding: 0.75rem;
  background: var(--bg-input);
  border: 1px solid var(--border);
  border-radius: 4px;
  color: var(--text-primary);
  font-size: 1rem;
}

#template-form textarea {
  resize: none;
  overflow-y: auto;
  max-height: 50dvh;
}

#template-form input:focus,
#template-form textarea:focus,
#template-form select:focus {
  outline: none;
  border-color: var(--accent);
}

#template-form input:invalid {
  border-color: var(--error);
}

.icon-ai-group {
  display: flex;
  align-items: center;
  gap: 0.5rem;
}

.icon-ai-group input {
  flex: 1;
  padding: 0.5rem;
}

.icon-ai-group button {
  padding: 0.5rem 1rem;
  background: var(--bg-tertiary);
  border: 1px solid var(--border);
  border-radius: 4px;
  color: var(--text-primary);
  cursor: pointer;
  transition: background 0.2s;
}

.icon-ai-group button:hover:not(:disabled) {
  background: var(--bg-hover);
}

.icon-ai-group button:disabled {
  opacity: 0.6;
  cursor: not-allowed;
}

#tpl-icon-preview {
  font-size: 1.5rem;
  min-width: 2rem;
  text-align: center;
}

.model-input-group {
  display: flex;
  gap: 0.5rem;
}

.model-input-group input {
  flex: 1;
}

.model-input-group select {
  flex: 1;
}

.model-input-group button {
  padding: 0.5rem 1rem;
  background: var(--bg-tertiary);
  border: 1px solid var(--border);
  border-radius: 4px;
  color: var(--text-primary);
  cursor: pointer;
  white-space: nowrap;
}

.model-input-group button:hover:not(:disabled) {
  background: var(--bg-hover);
}

.model-input-group button:disabled {
  opacity: 0.6;
  cursor: not-allowed;
}

.floating-message {
  margin-top: 0.5rem;
  padding: 0.75rem;
  padding-right: 2rem;
  background: var(--bg-tertiary);
  border: 1px solid var(--border);
  border-radius: 4px;
  font-size: 0.9rem;
  color: var(--text-secondary);
  white-space: pre-wrap;
  max-height: 200px;
  overflow-y: auto;
  position: relative;
  user-select: text;
}

.ai-message-text {
  user-select: text;
}

.ai-message-close {
  position: absolute;
  top: 0.25rem;
  right: 0.25rem;
  background: transparent;
  border: none;
  color: var(--text-muted);
  font-size: 1.2rem;
  cursor: pointer;
  padding: 0.25rem 0.5rem;
  line-height: 1;
}

.ai-message-close:hover {
  color: var(--text-primary);
}

#color-picker {
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
}

#color-presets {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
}

.color-swatch {
  width: 28px;
  height: 28px;
  border-radius: 4px;
  cursor: pointer;
  border: 2px solid transparent;
  transition: transform 0.1s;
}

.color-swatch:hover {
  transform: scale(1.1);
}

.color-swatch.selected {
  border-color: white;
}

#tpl-color-custom {
  width: 60px;
  height: 32px;
  padding: 0;
  border: 1px solid var(--border);
  border-radius: 4px;
  cursor: pointer;
}

.range-group {
  display: flex;
  align-items: center;
  gap: 1rem;
}

.range-group input[type="range"] {
  flex: 1;
}

.range-group input[type="number"] {
  width: 70px;
}

.checkbox-label {
  flex-direction: row !important;
  align-items: center !important;
}

.checkbox-label input {
  width: auto;
  margin-right: 0.5rem;
}

#template-form-errors {
  color: var(--error);
  font-size: 0.85rem;
  min-height: 1.25rem;
}

#template-form-actions {
  display: flex;
  gap: 1rem;
  margin-top: 1rem;
}

#template-form-actions button {
  padding: 0.75rem 1.5rem;
  border: none;
  border-radius: 4px;
  font-size: 1rem;
  cursor: pointer;
}

#tpl-save-btn {
  background: var(--accent);
  color: white;
}

#tpl-save-btn:hover:not(:disabled) {
  background: var(--accent-hover);
}

#tpl-save-btn:disabled {
  opacity: 0.6;
  cursor: not-allowed;
}

#tpl-delete-btn {
  background: var(--error);
  color: white;
}

#tpl-delete-btn:hover {
  background: #dc2626;
}

#template-editor-close {
  position: absolute;
  top: 1rem;
  right: 1rem;
  padding: 0.5rem 1rem;
  background: var(--bg-tertiary);
  border: 1px solid var(--border);
  border-radius: 4px;
  color: var(--text-primary);
  cursor: pointer;
}

#template-editor-close:hover {
  background: var(--bg-hover);
}

/* Modal */
#modal-overlay {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: var(--modal-backdrop);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 1000;
  padding: 1rem;
}

#modal-content {
  background: var(--bg-secondary);
  border-radius: 8px;
  max-width: 500px;
  width: 100%;
  max-height: 80vh;
  overflow: hidden;
  display: flex;
  flex-direction: column;
  box-shadow: 0 4px 30px var(--shadow);
}

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

#modal-title {
  font-size: 1.1rem;
  font-weight: 600;
}

#modal-close {
  background: none;
  border: none;
  color: var(--text-secondary);
  font-size: 1.5rem;
  cursor: pointer;
  padding: 0;
  line-height: 1;
}

#modal-close:hover {
  color: var(--text-primary);
}

#modal-body {
  flex: 1;
  padding: 1.5rem;
  overflow-y: auto;
}

#modal-footer {
  padding: 1rem 1.5rem;
  border-top: 1px solid var(--border);
  display: flex;
  gap: 0.75rem;
  justify-content: flex-end;
}

#modal-footer:empty {
  display: none;
}

#modal-footer button {
  padding: 0.5rem 1rem;
  border: none;
  border-radius: 4px;
  font-size: 0.9rem;
  cursor: pointer;
}

.modal-btn-primary {
  background: var(--accent);
  color: white;
}

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

.modal-btn-secondary {
  background: var(--bg-tertiary);
  color: var(--text-primary);
  border: 1px solid var(--border) !important;
}

.modal-btn-secondary:hover {
  background: var(--bg-hover);
}

.modal-btn-danger {
  background: var(--error);
  color: white;
}

.modal-btn-danger:hover {
  background: #dc2626;
}

/* Auto-grow textareas in settings modal */
#modal-body textarea {
  resize: none;
  overflow-y: auto;
  max-height: 50dvh;
  width: 100%;
  padding: 0.75rem;
  background: var(--bg-input);
  border: 1px solid var(--border);
  border-radius: 4px;
  color: var(--text-primary);
  font-family: inherit;
  font-size: 1rem;
}

#modal-body textarea:focus {
  outline: none;
  border-color: var(--accent);
}

/* Settings Section */
.settings-section {
  margin-top: 1.5rem;
  padding-top: 1.5rem;
  border-top: 1px solid var(--border);
}

.settings-label {
  font-weight: 500;
  margin-bottom: 0.5rem;
  color: var(--text-secondary);
}

/* Sharing List */
.share-list {
  max-height: 120px;
  overflow-y: auto;
  border: 1px solid var(--border);
  border-radius: 4px;
  padding: 0.5rem;
  background: var(--bg-secondary);
}

.share-item {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  padding: 0.25rem;
  cursor: pointer;
  border-radius: 2px;
}

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

.share-checkbox {
  accent-color: var(--accent);
}

/* Shared Indicator in Chat List */
.chat-item-shared {
  font-size: 0.75rem;
  margin-right: 0.25rem;
}

/* Template Sync Section */
.sync-section {
  margin-top: 1.5rem;
  padding-top: 1.5rem;
  border-top: 1px solid var(--border);
}

.sync-label {
  font-weight: 500;
  margin-bottom: 0.5rem;
  color: var(--text-secondary);
}

.sync-info {
  margin-bottom: 0.75rem;
  color: var(--text-muted);
  font-style: italic;
}

.sync-buttons {
  display: flex;
  gap: 0.5rem;
}

.sync-buttons button {
  flex: 1;
}

/* Template Selection Modal */
.template-select-list {
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
}

.template-select-item {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  padding: 0.75rem 1rem;
  background: var(--bg-tertiary);
  border: 2px solid transparent;
  border-radius: 4px;
  cursor: pointer;
  transition: border-color 0.2s;
}

.template-select-item:hover {
  border-color: var(--border-light);
}

.template-select-item.selected {
  border-color: var(--accent);
}

.template-select-icon {
  font-size: 1.5rem;
}

.template-select-name {
  flex: 1;
  font-weight: 500;
}

/* Toast Notifications */
#toast-container {
  position: fixed;
  bottom: 1rem;
  right: 1rem;
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
  z-index: 2000;
}

.toast {
  padding: 1rem 1.5rem;
  background: var(--bg-secondary);
  border-radius: 4px;
  box-shadow: 0 4px 20px var(--shadow);
  max-width: 400px;
  animation: slideIn 0.3s ease;
  border-left: 4px solid var(--accent);
}

.toast.error {
  border-left-color: var(--error);
}

.toast.warning {
  border-left-color: var(--warning);
}

.toast.success {
  border-left-color: var(--success);
}

.toast-title {
  font-weight: 600;
  margin-bottom: 0.25rem;
}

.toast-message {
  font-size: 0.9rem;
  color: var(--text-secondary);
}

.toast-details {
  margin-top: 0.5rem;
  padding: 0.5rem;
  background: var(--bg-code);
  border-radius: 4px;
  font-size: 0.8rem;
  font-family: monospace;
  max-height: 100px;
  overflow-y: auto;
}

@keyframes slideIn {
  from {
    transform: translateX(100%);
    opacity: 0;
  }
  to {
    transform: translateX(0);
    opacity: 1;
  }
}

/* Loading States */
.loading {
  opacity: 0.6;
  pointer-events: none;
}

.spinner {
  display: inline-block;
  width: 16px;
  height: 16px;
  border: 2px solid var(--text-secondary);
  border-top-color: transparent;
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}

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

.skeleton {
  background: linear-gradient(90deg, var(--bg-tertiary) 25%, var(--bg-hover) 50%, var(--bg-tertiary) 75%);
  background-size: 200% 100%;
  animation: shimmer 1.5s infinite;
  border-radius: 4px;
}

@keyframes shimmer {
  0% {
    background-position: 200% 0;
  }
  100% {
    background-position: -200% 0;
  }
}

/* Mobile Styles */
@media (max-width: 768px) {
  #sidebar {
    position: fixed;
    left: -280px;
    top: 0;
    bottom: 0;
    z-index: 100;
    transition: left 0.3s ease;
  }

  #sidebar.open {
    left: 0;
  }

  .sidebar-backdrop {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: var(--modal-backdrop);
    z-index: 99;
    display: none;
  }

  .sidebar-backdrop.visible {
    display: block;
  }

  #app {
    position: relative;
  }

  .hamburger-btn {
    position: fixed;
    top: 1rem;
    left: 1rem;
    z-index: 50;
    padding: 0.5rem;
    background: var(--bg-secondary);
    border: 1px solid var(--border);
    border-radius: 4px;
    color: var(--text-primary);
    cursor: pointer;
    font-size: 1.25rem;
  }

  #chat-header {
    padding-left: 4rem;
  }

  #template-editor {
    flex-direction: column;
  }

  #template-list-panel {
    width: 100%;
    min-width: 100%;
    max-height: 40%;
    border-right: none;
    border-bottom: 1px solid var(--border);
  }

  .message {
    max-width: 90%;
  }
}

/* Utilities */
[hidden] {
  display: none !important;
}

.visually-hidden {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border: 0;
}

/* Drop Overlay */
#chat-view {
  position: relative;
}

#drop-overlay {
  position: absolute;
  inset: 0;
  background: rgba(59, 130, 246, 0.15);
  border: 3px dashed var(--accent);
  border-radius: 8px;
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 100;
  pointer-events: none;
}

.drop-zone-content {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 12px;
  color: var(--accent);
  font-size: 1.2rem;
}

.drop-icon { font-size: 3rem; }

/* Message Image */
.message-image {
  margin-bottom: 8px;
}

.message-thumbnail {
  max-width: 300px;
  max-height: 200px;
  border-radius: 8px;
  cursor: pointer;
  transition: opacity 0.2s;
  display: block;
}

.message-thumbnail:hover {
  opacity: 0.8;
}

/* Full-size image in modal */
.image-full {
  max-width: 90vw;
  max-height: 75vh;
  object-fit: contain;
  display: block;
  margin: 0 auto;
}

/* Spinner animation */
@keyframes spin {
  to { transform: rotate(360deg); }
}

/* File Content marker */
.message.file-content-marked {
  outline: 2px solid var(--accent, #3B82F6);
  outline-offset: 2px;
  background: rgba(34, 197, 94, 0.18) !important;
}

.message-actions .file-btn.active {
  color: var(--accent, #3B82F6);
}

/* Wide modal variant */
#modal-content.modal-wide {
  max-width: 90vw;
}

/* File Replace diff modal */
.file-replace-diff {
  max-height: 65vh;
  overflow-y: auto;
  font-family: 'Monaco', 'Menlo', 'Ubuntu Mono', monospace;
  font-size: 0.85rem;
  line-height: 1.5;
  white-space: pre-wrap;
  word-wrap: break-word;
}

.diff-segment-text {
  color: var(--text-primary);
  padding: 0 0.25rem;
}

.diff-chunk {
  display: inline;
}

.diff-old {
  background: rgba(239, 68, 68, 0.15);
  color: #ef4444;
  padding: 0 0.1rem;
  border-radius: 2px;
  transition: background 0.15s, color 0.15s;
}

.diff-old.hidden {
  display: none;
}

.diff-new {
  background: rgba(34, 197, 94, 0.15);
  color: #22c55e;
  padding: 0 0.1rem;
  border-radius: 2px;
}

.diff-new.hidden {
  display: none;
}

.diff-btns {
  display: inline-flex;
  gap: 0.3rem;
  margin-left: 0.4rem;
  vertical-align: middle;
}

.diff-accept-btn,
.diff-decline-btn {
  padding: 0.1rem 0.5rem;
  border-radius: 3px;
  border: 1px solid var(--border);
  background: var(--bg-tertiary);
  color: var(--text-secondary);
  font-size: 0.75rem;
  font-family: inherit;
  cursor: pointer;
  vertical-align: middle;
}

.diff-accept-btn.active {
  background: rgba(34, 197, 94, 0.2);
  border-color: #22c55e;
  color: #22c55e;
}

.diff-decline-btn.active {
  background: rgba(239, 68, 68, 0.2);
  border-color: #ef4444;
  color: #ef4444;
}

/* Pinned message navigation */
#pinned-nav {
  position: absolute;
  right: 1rem;
  bottom: 5rem;
  display: flex;
  flex-direction: column;
  gap: 0.25rem;
  z-index: 10;
}

#pinned-nav button {
  width: 2rem;
  height: 2rem;
  border: 1px solid var(--border, #374151);
  background: var(--bg-secondary, #1f2937);
  color: var(--text-secondary, #9ca3af);
  border-radius: 4px;
  cursor: pointer;
  font-size: 0.75rem;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background 0.15s, color 0.15s;
}

#pinned-nav button:hover {
  background: var(--bg-hover, #374151);
  color: var(--text-primary, #f9fafb);
}

/* CSS Tooltips for message action buttons */
[data-tooltip] {
  position: relative;
}

[data-tooltip]::after {
  content: attr(data-tooltip);
  position: absolute;
  bottom: calc(100% + 6px);
  left: 50%;
  transform: translateX(-50%);
  background: var(--bg-primary, #111827);
  color: var(--text-primary, #f9fafb);
  border: 1px solid var(--border, #374151);
  border-radius: 4px;
  padding: 0.25rem 0.5rem;
  font-size: 0.72rem;
  white-space: nowrap;
  pointer-events: none;
  opacity: 0;
  transition: opacity 0.15s;
  z-index: 100;
}

[data-tooltip]:hover::after {
  opacity: 1;
}
