/* ═══════════════════════════════════════════════
   RESET & BASE
═══════════════════════════════════════════════ */
*, *::before, *::after {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

:root {
  --bg: #0f0f0f;
  --surface-1: #1a1a1a;
  --surface-2: #2a2a2a;
  --surface-3: #3a3a3a;
  --text: #e8e8e8;
  --text-muted: #888;
  --border: #3a3a3a;
  --accent: #3b82f6;
  --radius: 8px;
  --radius-lg: 12px;
}

html, body {
  height: 100%;
}

body {
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
  display: flex;
  flex-direction: column;
  min-height: 100vh;
  overflow-x: hidden;
}

/* ═══════════════════════════════════════════════
   ACCESSIBILITY
═══════════════════════════════════════════════ */
.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border: 0;
}

/* ═══════════════════════════════════════════════
   HEADER
═══════════════════════════════════════════════ */
.app-header {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 12px 20px;
  background: var(--surface-1);
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
  flex-wrap: wrap;
}

.app-title {
  font-size: 1.1rem;
  font-weight: 700;
  color: var(--text);
  white-space: nowrap;
}

.header-controls {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-wrap: wrap;
  margin-left: auto;
}

.token-counter {
  font-size: 0.75rem;
  color: var(--text-muted);
  white-space: nowrap;
  font-variant-numeric: tabular-nums;
}

/* ═══════════════════════════════════════════════
   BUTTONS
═══════════════════════════════════════════════ */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  padding: 7px 14px;
  border-radius: var(--radius);
  font-size: 0.85rem;
  font-weight: 500;
  cursor: pointer;
  border: none;
  transition: opacity 0.15s, background 0.15s;
  white-space: nowrap;
  line-height: 1.2;
}

.btn:disabled {
  opacity: 0.45;
  cursor: not-allowed;
}

.btn-primary {
  background: var(--accent);
  color: #fff;
  border: none;
}

.btn-primary:hover:not(:disabled) {
  opacity: 0.85;
}

.btn-secondary {
  background: transparent;
  color: var(--accent);
  border: 1px solid var(--accent);
}

.btn-secondary:hover:not(:disabled) {
  background: color-mix(in srgb, var(--accent) 12%, transparent);
}

.btn-danger {
  color: #e53935;
  border-color: #e53935;
}

.btn-danger:hover:not(:disabled) {
  background: rgba(229, 57, 53, 0.1);
}

.btn-send {
  align-self: flex-end;
  padding: 10px 20px;
  font-size: 0.9rem;
}

/* ═══════════════════════════════════════════════
   PERSONALITY SECTION
═══════════════════════════════════════════════ */
.personality-section {
  padding: 12px 20px 8px;
  flex-shrink: 0;
  background: var(--surface-1);
  border-bottom: 1px solid var(--border);
}

.personality-grid {
  display: flex;
  gap: 8px;
  overflow-x: auto;
  padding-bottom: 4px;
  scrollbar-width: thin;
  scrollbar-color: var(--border) transparent;
}

.personality-grid::-webkit-scrollbar {
  height: 4px;
}

.personality-grid::-webkit-scrollbar-track {
  background: transparent;
}

.personality-grid::-webkit-scrollbar-thumb {
  background: var(--border);
  border-radius: 2px;
}

.personality-card {
  --accent: #3b82f6;
  position: relative;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 4px;
  padding: 10px 14px;
  min-width: 80px;
  flex-shrink: 0;
  background: var(--surface-2);
  border: 2px solid transparent;
  border-radius: var(--radius);
  cursor: pointer;
  user-select: none;
  transition: border-color 0.15s, background 0.15s;
}

.personality-card:hover {
  background: var(--surface-3);
}

.personality-card[aria-pressed="true"] {
  border-color: var(--accent);
  background: color-mix(in srgb, var(--accent) 12%, var(--surface-2));
}

.personality-card[aria-pressed="true"]::after {
  content: "✓";
  position: absolute;
  top: 4px;
  right: 6px;
  font-size: 0.65rem;
  font-weight: 700;
  color: var(--accent);
  line-height: 1;
}

.personality-card .card-emoji {
  font-size: 1.5rem;
  line-height: 1;
}

.personality-card .card-name {
  font-size: 0.7rem;
  color: var(--text);
  text-align: center;
  max-width: 70px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

/* ═══════════════════════════════════════════════
   MESSAGES
═══════════════════════════════════════════════ */
#messages {
  flex: 1 1 auto;
  overflow-y: auto;
  padding: 20px;
  display: flex;
  flex-direction: column;
  gap: 16px;
  scroll-behavior: smooth;
  scrollbar-width: thin;
  scrollbar-color: var(--border) transparent;
}

#messages::-webkit-scrollbar {
  width: 6px;
}

#messages::-webkit-scrollbar-thumb {
  background: var(--border);
  border-radius: 3px;
}

/* Bubbles */
.bubble {
  max-width: 72%;
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.bubble-user {
  align-self: flex-end;
  align-items: flex-end;
}

.bubble-assistant {
  align-self: flex-start;
}

.bubble-error {
  align-self: flex-start;
}

.bubble-content {
  padding: 10px 14px;
  border-radius: var(--radius-lg);
  line-height: 1.55;
  font-size: 0.92rem;
  word-break: break-word;
}

/* User bubble */
.bubble-user .bubble-content {
  background: var(--surface-2);
  color: var(--text);
  border-radius: var(--radius-lg) var(--radius-lg) 4px var(--radius-lg);
}

/* Assistant bubble */
.bubble-assistant {
  --accent: #3b82f6;
}

.bubble-assistant .bubble-content {
  background: var(--surface-1);
  border-left: 4px solid var(--accent);
  border-radius: 4px var(--radius-lg) var(--radius-lg) 4px;
  color: var(--text);
}

/* Orphan bubble (deleted personality) */
.bubble-orphan .bubble-content {
  border-left-color: #666 !important;
  color: #aaa;
}

.bubble-orphan .bubble-label {
  color: #666;
}

/* Error bubble */
.bubble-error .bubble-content {
  background: #3a1f1f;
  border-left: 4px solid var(--accent, #e53935);
  color: #f48a88;
  border-radius: 4px var(--radius-lg) var(--radius-lg) 4px;
}

/* Personality label above assistant bubble */
.bubble-label {
  font-size: 0.75rem;
  color: var(--accent);
  font-weight: 600;
  padding-left: 6px;
  display: flex;
  align-items: center;
  gap: 5px;
}

/* Hover actions (copy / regenerate) */
.bubble-actions {
  display: none;
  gap: 4px;
  padding-left: 6px;
}

.bubble-assistant:hover .bubble-actions,
.bubble-assistant:focus-within .bubble-actions {
  display: flex;
}

.bubble-action-btn {
  padding: 3px 8px;
  font-size: 0.72rem;
  background: var(--surface-2);
  color: var(--text-muted);
  border: 1px solid var(--border);
  border-radius: 4px;
  cursor: pointer;
}

.bubble-action-btn:hover {
  color: var(--text);
  border-color: #666;
}

/* Markdown content */
.bubble-content p { margin-bottom: 0.6em; }
.bubble-content p:last-child { margin-bottom: 0; }
.bubble-content ul, .bubble-content ol { padding-left: 1.4em; margin-bottom: 0.6em; }
.bubble-content li { margin-bottom: 0.25em; }
.bubble-content strong { color: #fff; }
.bubble-content em { color: #ccc; }
.bubble-content blockquote {
  border-left: 3px solid var(--border);
  padding-left: 12px;
  color: var(--text-muted);
  margin: 8px 0;
}
.bubble-content h1, .bubble-content h2, .bubble-content h3 {
  margin: 0.6em 0 0.3em;
  line-height: 1.3;
}
.bubble-content a {
  color: var(--accent);
  text-decoration: underline;
}

/* Code blocks */
.bubble-content pre {
  background: #0d0d0d;
  border-radius: 6px;
  padding: 8px;
  overflow-x: auto;
  margin: 8px 0;
}

.bubble-content pre code {
  font-family: ui-monospace, "Cascadia Code", "Fira Mono", Menlo, Consolas, monospace;
  font-size: 0.83rem;
  background: none;
  padding: 0;
  color: #c9d1d9;
}

.bubble-content code:not(pre code) {
  background: #0d0d0d;
  padding: 2px 5px;
  border-radius: 4px;
  font-family: ui-monospace, monospace;
  font-size: 0.85em;
}

/* Message image thumbnails */
.bubble-content img,
.bubble-img-thumb {
  max-width: 220px;
  max-height: 160px;
  border-radius: 8px;
  display: block;
  margin-bottom: 6px;
  object-fit: cover;
}

/* Thinking / loading bubble */
.bubble-thinking .bubble-content {
  color: var(--text-muted);
  font-style: italic;
}

/* ═══════════════════════════════════════════════
   INPUT ZONE
═══════════════════════════════════════════════ */
.input-zone {
  flex-shrink: 0;
  padding: 12px 20px 16px;
  background: var(--surface-1);
  border-top: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.global-prompt-section {
  display: flex;
  flex-direction: column;
  gap: 6px;
  margin-bottom: 8px;
}
.global-prompt-section .form-meta {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 8px;
}
.modal-divider {
  border: none;
  border-top: 1px solid var(--border, #333);
  margin: 12px 0;
}

.suggestions-panel {
  display: flex;
  flex-direction: column;
  gap: 8px;
  max-height: 50vh;
  overflow-y: auto;
}
.suggestions-group {
  border-left: 4px solid var(--accent, #3b82f6);
  background: var(--surface-2);
  border-radius: 8px;
  padding: 8px 10px;
  display: flex;
  flex-direction: column;
  gap: 6px;
}
.suggestions-group-header {
  font-size: 12px;
  color: #b0b0b0;
  display: flex;
  align-items: center;
  gap: 6px;
}
.suggestion-item {
  display: flex;
  align-items: center;
  gap: 8px;
  background: #1f1f1f;
  border: 1px solid #333;
  border-radius: 6px;
  padding: 8px 10px;
}
.suggestion-text {
  flex: 1;
  font-size: 14px;
  line-height: 1.4;
  color: var(--text);
}
.suggestion-use-btn {
  flex-shrink: 0;
  background: var(--accent, #3b82f6);
  color: #fff;
  border: 0;
  border-radius: 6px;
  padding: 6px 12px;
  font-size: 13px;
  cursor: pointer;
}
.suggestion-use-btn:hover { opacity: 0.85; }
.suggestion-loading {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 10px;
  font-size: 13px;
  color: #b0b0b0;
}
.suggestion-error {
  color: #ff8a80;
  font-size: 13px;
  padding: 8px 10px;
}

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

.message-input {
  flex: 1;
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  color: var(--text);
  font-family: inherit;
  font-size: 0.92rem;
  padding: 10px 14px;
  resize: none;
  min-height: 44px;
  max-height: 200px;
  overflow-y: auto;
  line-height: 1.5;
  outline: none;
  transition: border-color 0.15s;
}

.message-input:focus {
  border-color: #3b82f6;
  outline: 2px solid rgba(59, 130, 246, 0.3);
  outline-offset: 0;
}

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

.input-meta {
  display: flex;
  align-items: center;
  gap: 10px;
}

.char-counter {
  font-size: 0.72rem;
  color: var(--text-muted);
  font-variant-numeric: tabular-nums;
  margin-left: auto;
}

.char-counter.warn {
  color: #f59e0b;
}

.char-counter.limit {
  color: #e53935;
}

/* File attach label */
.attach-label {
  display: flex;
  align-items: center;
  cursor: pointer;
  color: var(--text-muted);
  padding: 4px;
  border-radius: 4px;
  transition: color 0.15s;
}

.attach-label:hover {
  color: var(--text);
}

.file-input-hidden {
  display: none;
}

/* Image preview strip */
.image-previews {
  display: flex;
  gap: 10px;
  flex-wrap: nowrap;
  overflow-x: auto;
  padding: 4px 0 6px;
}

.image-previews[hidden] {
  display: none;
}

.preview-thumb-wrap {
  position: relative;
  flex-shrink: 0;
}

.preview-thumb-wrap img {
  max-height: 80px;
  border-radius: 6px;
  display: block;
  object-fit: cover;
}

.preview-thumb-wrap .preview-remove {
  position: absolute;
  top: -6px;
  right: -6px;
  width: 18px;
  height: 18px;
  border-radius: 50%;
  background: rgba(0,0,0,0.75);
  color: #fff;
  border: none;
  cursor: pointer;
  font-size: 0.65rem;
  display: flex;
  align-items: center;
  justify-content: center;
  line-height: 1;
}

.preview-thumb-wrap .preview-remove:hover {
  background: #e53935;
}

/* ═══════════════════════════════════════════════
   MODALS
═══════════════════════════════════════════════ */
.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.7);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 1000;
  padding: 20px;
}

.modal-overlay[hidden] {
  display: none;
}

.modal-content {
  background: var(--surface-1);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  width: 100%;
  max-width: 560px;
  max-height: 90vh;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

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

.modal-title {
  font-size: 1rem;
  font-weight: 700;
  color: var(--text);
}

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

.modal-close:hover {
  color: var(--text);
  background: var(--surface-2);
}

.modal-body {
  padding: 20px;
  overflow-y: auto;
  flex: 1;
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.modal-hint {
  font-size: 0.85rem;
  color: var(--text-muted);
  line-height: 1.5;
}

.modal-footer {
  display: flex;
  gap: 8px;
  align-items: center;
  padding: 14px 20px;
  border-top: 1px solid var(--border);
  flex-shrink: 0;
  flex-wrap: wrap;
}

/* ═══════════════════════════════════════════════
   FORM ELEMENTS
═══════════════════════════════════════════════ */
.form-group {
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.form-row {
  display: flex;
  gap: 12px;
  align-items: flex-start;
}

.form-group-emoji {
  flex: 0 0 80px;
}

.form-group-color {
  flex: 1;
}

.form-label {
  font-size: 0.82rem;
  font-weight: 600;
  color: var(--text);
}

.form-hint {
  font-weight: 400;
  color: var(--text-muted);
}

.form-input {
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  color: var(--text);
  font-family: inherit;
  font-size: 0.88rem;
  padding: 8px 10px;
  outline: none;
  transition: border-color 0.15s;
  width: 100%;
}

.form-input:focus {
  border-color: #3b82f6;
  outline: 2px solid rgba(59, 130, 246, 0.3);
  outline-offset: 0;
}

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

.form-input-emoji {
  text-align: center;
  font-size: 1.25rem;
}

.form-input-color {
  flex: 1;
  font-family: ui-monospace, monospace;
  font-size: 0.82rem;
}

.form-textarea {
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  color: var(--text);
  font-family: inherit;
  font-size: 0.88rem;
  padding: 8px 10px;
  resize: vertical;
  outline: none;
  transition: border-color 0.15s;
  width: 100%;
}

.form-textarea:focus {
  border-color: #3b82f6;
  outline: 2px solid rgba(59, 130, 246, 0.3);
  outline-offset: 0;
}

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

.textarea-counter {
  font-size: 0.72rem;
  color: var(--text-muted);
  text-align: right;
  font-variant-numeric: tabular-nums;
}

/* Color picker row */
.color-pair {
  display: flex;
  gap: 8px;
  align-items: center;
}

.color-picker {
  width: 40px;
  height: 36px;
  padding: 2px;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--surface-2);
  cursor: pointer;
  flex-shrink: 0;
}

/* Range */
.form-range {
  -webkit-appearance: none;
  appearance: none;
  width: 100%;
  height: 4px;
  background: var(--surface-3);
  border-radius: 2px;
  outline: none;
  cursor: pointer;
}

.form-range::-webkit-slider-thumb {
  -webkit-appearance: none;
  width: 16px;
  height: 16px;
  border-radius: 50%;
  background: var(--accent);
  cursor: pointer;
}

.form-range::-moz-range-thumb {
  width: 16px;
  height: 16px;
  border: none;
  border-radius: 50%;
  background: var(--accent);
  cursor: pointer;
}

.temperature-value {
  font-family: ui-monospace, monospace;
  color: var(--accent);
  font-size: 0.85rem;
}

.range-labels {
  display: flex;
  justify-content: space-between;
  font-size: 0.68rem;
  color: var(--text-muted);
  margin-top: 2px;
}

/* ═══════════════════════════════════════════════
   PERSONALITIES LIST (in modal)
═══════════════════════════════════════════════ */
.personalities-list {
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.personality-row {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 12px;
  background: var(--surface-2);
  border-radius: var(--radius);
  font-size: 0.85rem;
}

.personality-row .row-emoji {
  font-size: 1.2rem;
  flex-shrink: 0;
}

.personality-row .row-name {
  flex: 1;
  font-weight: 600;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.personality-row .row-color-badge {
  width: 14px;
  height: 14px;
  border-radius: 3px;
  flex-shrink: 0;
  border: 1px solid rgba(255,255,255,0.1);
}

.personality-row .row-temp {
  font-size: 0.75rem;
  color: var(--text-muted);
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
}

.personality-row .row-actions {
  display: flex;
  gap: 6px;
  flex-shrink: 0;
}

.personality-row .row-btn {
  padding: 4px 10px;
  font-size: 0.75rem;
  border-radius: 4px;
  cursor: pointer;
  border: 1px solid var(--border);
  background: transparent;
  color: var(--text-muted);
}

.personality-row .row-btn:hover {
  color: var(--text);
  border-color: #666;
}

.personality-row .row-btn-delete {
  color: #e57373;
  border-color: #e57373;
}

.personality-row .row-btn-delete:hover {
  background: rgba(229, 57, 53, 0.1);
  color: #e53935;
}

/* Inline delete confirmation */
.personality-row .delete-confirm {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 0.8rem;
  color: var(--text-muted);
}

.personality-row .delete-confirm .btn-confirm-yes {
  padding: 3px 8px;
  font-size: 0.75rem;
  background: #e53935;
  color: #fff;
  border: none;
  border-radius: 4px;
  cursor: pointer;
}

.personality-row .delete-confirm .btn-confirm-no {
  padding: 3px 8px;
  font-size: 0.75rem;
  background: transparent;
  color: var(--text-muted);
  border: 1px solid var(--border);
  border-radius: 4px;
  cursor: pointer;
}

/* ═══════════════════════════════════════════════
   TOASTS
═══════════════════════════════════════════════ */
#toasts {
  position: fixed;
  bottom: 20px;
  right: 20px;
  z-index: 2000;
  display: flex;
  flex-direction: column;
  align-items: flex-end;
  pointer-events: none;
}

#toasts .toast {
  background: var(--surface-2);
  border-left: 4px solid #e53935;
  padding: 12px 16px;
  border-radius: var(--radius);
  margin-top: 8px;
  font-size: 0.85rem;
  color: var(--text);
  max-width: 320px;
  pointer-events: auto;
  box-shadow: 0 4px 20px rgba(0,0,0,0.4);
}

#toasts .toast.toast-success {
  border-left-color: #4caf50;
}

#toasts .toast.toast-info {
  border-left-color: #3b82f6;
}

/* ═══════════════════════════════════════════════
   SHAKE ANIMATION
═══════════════════════════════════════════════ */
@keyframes shake {
  0%, 100% { transform: translateX(0); }
  20% { transform: translateX(-6px); }
  40% { transform: translateX(6px); }
  60% { transform: translateX(-4px); }
  80% { transform: translateX(4px); }
}

.shake {
  animation: shake 0.2s ease-in-out;
}

/* ═══════════════════════════════════════════════
   SCROLLBAR GLOBAL
═══════════════════════════════════════════════ */
::-webkit-scrollbar {
  width: 6px;
  height: 6px;
}

::-webkit-scrollbar-track {
  background: transparent;
}

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

/* ═══════════════════════════════════════════════
   RESPONSIVE ≤640px
═══════════════════════════════════════════════ */
@media (max-width: 640px) {
  .app-header {
    padding: 10px 14px;
  }

  .header-controls {
    gap: 6px;
  }

  .token-counter {
    font-size: 0.68rem;
  }

  .personality-section {
    padding: 10px 14px 6px;
  }

  /* Horizontal scroll for personality grid */
  .personality-grid {
    flex-wrap: nowrap;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
  }

  #messages {
    padding: 14px;
    gap: 12px;
  }

  .bubble {
    max-width: 90%;
  }

  .input-zone {
    padding: 10px 14px 12px;
  }

  /* Modals near full-screen */
  .modal-overlay {
    padding: 16px;
    align-items: flex-end;
  }

  .modal-content {
    max-width: 100%;
    max-height: 85vh;
    border-radius: var(--radius-lg) var(--radius-lg) 0 0;
  }

  .form-row {
    flex-direction: column;
  }

  .form-group-emoji {
    flex: none;
    width: 100%;
  }
}
