/* Character Chat — dark-mode-default, responsive, chat bubbles. */

:root {
  --bg: #0f1115;
  --surface: #1a1d24;
  --surface-2: #232733;
  --border: #2e3340;
  --text: #e8eaf0;
  --text-muted: #9aa3b2;
  --accent: #6c8cff;
  --accent-text: #ffffff;
  --danger: #ff6b6b;
  --danger-bg: #3a1f24;
  --radius: 12px;
}

* {
  box-sizing: border-box;
}

[hidden] {
  display: none !important;
}

html,
body {
  margin: 0;
  height: 100%;
}

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

/* ---------------------------------------------------------------- header */

.app-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  flex-wrap: wrap;
  padding: 10px 16px;
  border-bottom: 1px solid var(--border);
  background: var(--surface);
}

.app-title {
  font-size: 18px;
  margin: 0;
  font-weight: 650;
}

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

.model-label {
  font-size: 13px;
  color: var(--text-muted);
}

.model-picker,
.header-btn,
.send-btn,
.stop-btn,
.back-btn {
  background: var(--surface-2);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 6px 12px;
  font-size: 14px;
  cursor: pointer;
}

.model-picker:hover,
.header-btn:hover,
.back-btn:hover {
  border-color: var(--accent);
}

.send-btn {
  background: var(--accent);
  color: var(--accent-text);
  border-color: var(--accent);
  font-weight: 600;
}

.send-btn:hover:not(:disabled) {
  filter: brightness(1.1);
}

.send-btn:disabled {
  opacity: 0.6;
  cursor: default;
}

.stop-btn {
  background: var(--danger-bg);
  border-color: var(--danger);
  color: var(--danger);
}

/* ---------------------------------------------------------------- layout */

.app-main {
  flex: 1;
  min-height: 0;
  display: flex;
  flex-direction: column;
  width: 100%;
  max-width: 800px;
  margin: 0 auto;
  padding: 12px 16px 0;
}

/* ---------------------------------------------------------------- grid */

.character-grid {
  display: flex;
  flex-wrap: wrap;
  gap: 12px;
  padding: 8px 0;
  overflow-y: auto;
}

.character-card-wrap {
  flex: 1 1 220px;
  max-width: 320px;
  display: flex;
  position: relative;
}

.character-card {
  flex: 1;
  text-align: left;
  background: var(--surface);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 16px;
  cursor: pointer;
  transition: border-color 0.15s ease, transform 0.1s ease;
}

.character-card-wrap .character-card {
  padding-right: 56px; /* room for the Edit button */
}

.card-edit-btn {
  position: absolute;
  top: 8px;
  right: 8px;
  background: var(--surface-2);
  color: var(--text-muted);
  border: 1px solid var(--border);
  border-radius: 7px;
  padding: 2px 8px;
  font-size: 12px;
  cursor: pointer;
}

.card-edit-btn:hover {
  border-color: var(--accent);
  color: var(--text);
}

.new-agent-card {
  flex: 1 1 220px;
  max-width: 320px;
  min-height: 84px;
  background: transparent;
  color: var(--text-muted);
  border: 1px dashed var(--border);
  border-radius: var(--radius);
  padding: 16px;
  font-size: 15px;
  cursor: pointer;
  transition: border-color 0.15s ease, color 0.15s ease;
}

.new-agent-card:hover {
  border-color: var(--accent);
  color: var(--text);
}

.character-card:hover {
  border-color: var(--accent);
  transform: translateY(-1px);
}

.character-name {
  font-size: 16px;
  font-weight: 650;
  margin-bottom: 4px;
}

.character-tagline {
  font-size: 13px;
  color: var(--text-muted);
}

/* ---------------------------------------------------------------- chat */

.chat-view {
  flex: 1;
  min-height: 0;
  display: flex;
  flex-direction: column;
}

.chat-header {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 4px 0 10px;
  border-bottom: 1px solid var(--border);
  flex-wrap: wrap; /* narrow phones: action buttons drop below the title */
}

.chat-character-name {
  font-weight: 650;
}

.chat-header-actions {
  margin-left: auto;
  display: flex;
  gap: 8px;
}

.chat-header-btn {
  font-size: 13px;
  padding: 4px 10px;
}

.messages {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 10px;
  padding: 14px 2px;
  scroll-behavior: smooth;
}

.bubble {
  max-width: 85%;
  padding: 10px 14px;
  border-radius: var(--radius);
  line-height: 1.5;
  font-size: 15px;
  word-wrap: break-word;
  overflow-wrap: anywhere;
}

.user-bubble {
  align-self: flex-end;
  background: var(--accent);
  color: var(--accent-text);
  border-bottom-right-radius: 4px;
  white-space: pre-wrap;
}

.assistant-bubble {
  align-self: flex-start;
  background: var(--surface);
  border: 1px solid var(--border);
  border-bottom-left-radius: 4px;
}

/* markdown content inside assistant bubbles */
.assistant-bubble .bubble-content > *:first-child { margin-top: 0; }
.assistant-bubble .bubble-content > *:last-child { margin-bottom: 0; }
.assistant-bubble p { margin: 0.5em 0; }
.assistant-bubble ul,
.assistant-bubble ol { margin: 0.5em 0; padding-left: 1.3em; }
.assistant-bubble code {
  background: var(--surface-2);
  border-radius: 4px;
  padding: 1px 5px;
  font-size: 0.9em;
}
.assistant-bubble pre {
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 10px;
  overflow-x: auto;
}
.assistant-bubble pre code { background: none; padding: 0; }
.assistant-bubble blockquote {
  margin: 0.5em 0;
  padding-left: 10px;
  border-left: 3px solid var(--border);
  color: var(--text-muted);
}

/* ---------------------------------------------------------------- error */

.error-bubble {
  align-self: flex-start;
  background: var(--danger-bg);
  border: 1px solid var(--danger);
  color: var(--text);
  max-width: 85%;
}

.error-message {
  font-weight: 600;
  margin-bottom: 6px;
}

.error-preview {
  font-size: 13px;
  color: var(--text-muted);
  margin-bottom: 8px;
  word-break: break-word;
}

.retry-btn {
  background: var(--surface-2);
  color: var(--text);
  border: 1px solid var(--danger);
  border-radius: 8px;
  padding: 4px 12px;
  font-size: 13px;
  cursor: pointer;
}

.retry-btn:hover {
  filter: brightness(1.15);
}

.interrupted-note {
  color: var(--text-muted);
  font-style: italic;
  font-size: 0.85em;
}

/* ---------------------------------------------------------------- input */

.input-form {
  display: flex;
  gap: 10px;
  align-items: flex-end;
  padding: 10px 0 14px;
  border-top: 1px solid var(--border);
}

.input-text {
  flex: 1;
  resize: none;
  background: var(--surface);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 10px 12px;
  font-family: inherit;
  font-size: 15px;
  line-height: 1.4;
}

.input-text:focus,
.model-picker:focus-visible,
button:focus-visible,
.field-input:focus {
  outline: 2px solid var(--accent);
  outline-offset: 1px;
}

.input-buttons {
  display: flex;
  gap: 8px;
}

.save-btn,
.send-btn,
.stop-btn {
  white-space: nowrap;
}

/* ---------------------------------------------------------------- modal */

.modal {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.55);
  backdrop-filter: blur(3px);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px;
  z-index: 50;
}

.modal-dialog {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 18px;
  width: 100%;
  max-width: 420px;
  max-height: 100%; /* of .modal's content box (viewport - padding) */
  overflow-y: auto; /* tall dialogs (soul editor) scroll on short screens */
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.modal-title {
  margin: 0 0 6px;
  font-size: 17px;
}

.field-label {
  font-size: 13px;
  color: var(--text-muted);
}

.field-input {
  background: var(--surface-2);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 8px 10px;
  font-family: inherit;
  font-size: 14px;
  resize: vertical;
}

.modal-actions {
  display: flex;
  justify-content: flex-end;
  gap: 8px;
  margin-top: 8px;
}

.modal-dialog-wide {
  max-width: 560px;
}

.modal-subtitle {
  margin: -2px 0 4px;
  font-size: 13px;
  color: var(--text-muted);
}

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

.agent-soul-input {
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  font-size: 13px;
  line-height: 1.45;
  min-height: 180px;
}

.modal-actions-split {
  justify-content: space-between;
  align-items: center;
}

.modal-actions-left,
.modal-actions-right {
  display: flex;
  gap: 8px;
}

.modal-status {
  font-size: 13px;
  padding: 6px 10px;
  border-radius: 8px;
  background: var(--surface-2);
  color: var(--text-muted);
}

.modal-status-error {
  color: var(--danger);
  border: 1px solid var(--danger);
  background: var(--danger-bg);
}

.modal-status-ok {
  color: var(--text);
  border: 1px solid var(--accent);
}

.checkbox-row {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 13px;
  color: var(--text-muted);
  cursor: pointer;
}

.danger-btn {
  color: var(--danger);
  border-color: var(--danger);
}

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