/* ── Reset & Base ─────────────────────────────────────────── */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

:root {
  --bg:          #0d1117;
  --bg-surface:  #161b22;
  --bg-elevated: #1c2333;
  --bg-input:    #0d1117;
  --border:      #30363d;
  --border-soft: #21262d;
  --text:        #e6edf3;
  --text-muted:  #8b949e;
  --text-dim:    #484f58;
  --accent:      #a371f7;
  --accent-dim:  #3d1f6b;
  --accent-glow: rgba(163, 113, 247, 0.15);
  --gold:        #d4a857;
  --gold-dim:    #5a3e1b;
  --green:       #3fb950;
  --red:         #f85149;
  --blue:        #58a6ff;
  --radius:      8px;
  --radius-lg:   12px;
  --transition:  160ms ease;
  --shadow:      0 4px 24px rgba(0,0,0,0.5);
}

html, body {
  height: 100%;
  font-family: 'Segoe UI', system-ui, -apple-system, sans-serif;
  background: var(--bg);
  color: var(--text);
  font-size: 14px;
  line-height: 1.6;
}

/* ── Layout ───────────────────────────────────────────────── */
.app {
  display: flex;
  flex-direction: column;
  height: 100vh;
  overflow: hidden;
}

.app-header {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 12px 20px;
  background: var(--bg-surface);
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}

.app-header h1 {
  font-size: 18px;
  font-weight: 600;
  color: var(--accent);
  letter-spacing: 0.5px;
}

.header-subtitle {
  color: var(--text-muted);
  font-size: 12px;
}

.header-actions {
  margin-left: auto;
  display: flex;
  gap: 8px;
  align-items: center;
}

.app-body {
  display: flex;
  flex: 1;
  overflow: hidden;
}

/* ── Setup Panel ──────────────────────────────────────────── */
.setup-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.7);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 100;
  backdrop-filter: blur(4px);
}

.setup-box {
  background: var(--bg-surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  padding: 32px;
  width: 520px;
  max-width: calc(100vw - 40px);
  box-shadow: var(--shadow);
}

.setup-box h2 {
  font-size: 20px;
  font-weight: 600;
  margin-bottom: 6px;
  color: var(--text);
}

.setup-box p {
  color: var(--text-muted);
  margin-bottom: 24px;
  font-size: 13px;
}

.card-type-grid {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 10px;
  margin-bottom: 20px;
}

.type-btn {
  background: var(--bg-elevated);
  border: 2px solid var(--border);
  border-radius: var(--radius);
  padding: 16px 12px;
  cursor: pointer;
  color: var(--text-muted);
  transition: var(--transition);
  text-align: center;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 8px;
}

.type-btn:hover {
  border-color: var(--accent);
  color: var(--text);
  background: var(--accent-glow);
}

.type-btn.active {
  border-color: var(--accent);
  color: var(--text);
  background: var(--accent-glow);
}

.type-icon { font-size: 24px; }
.type-label { font-size: 12px; font-weight: 600; letter-spacing: 0.3px; }
.type-desc { font-size: 11px; color: var(--text-dim); }

.api-key-row {
  display: flex;
  flex-direction: column;
  gap: 6px;
  margin-bottom: 20px;
}

.api-key-row label {
  font-size: 12px;
  color: var(--text-muted);
  font-weight: 500;
}

.api-key-row input {
  background: var(--bg-input);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 10px 12px;
  color: var(--text);
  font-size: 13px;
  font-family: 'Consolas', monospace;
  outline: none;
  transition: var(--transition);
}

.api-key-row input:focus {
  border-color: var(--accent);
}

.api-key-note {
  font-size: 11px;
  color: var(--text-dim);
}

/* ── Import area ──────────────────────────────────────────── */
.import-row {
  display: flex;
  flex-direction: column;
  gap: 8px;
  margin-bottom: 20px;
}

.import-row label {
  font-size: 12px;
  color: var(--text-muted);
  font-weight: 500;
}

.import-row label code {
  font-family: 'Consolas', monospace;
  font-size: 11px;
  color: var(--accent);
}

.import-row textarea {
  background: var(--bg-input);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 10px 12px;
  color: var(--text);
  font-size: 12px;
  font-family: 'Consolas', monospace;
  outline: none;
  resize: vertical;
  min-height: 120px;
  transition: var(--transition);
}

.import-row textarea:focus {
  border-color: var(--accent);
}

.import-file-row {
  display: flex;
  align-items: center;
  gap: 10px;
}

.import-file-name {
  font-size: 11px;
  color: var(--text-dim);
  font-family: 'Consolas', monospace;
}

/* ── Chat Panel ───────────────────────────────────────────── */
.chat-panel {
  display: flex;
  flex-direction: column;
  flex: 1;
  min-width: 0;
  border-right: 1px solid var(--border);
}

.chat-topbar {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 16px;
  background: var(--bg-surface);
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}

.chat-type-badge {
  font-size: 11px;
  font-weight: 600;
  letter-spacing: 0.5px;
  padding: 3px 8px;
  border-radius: 20px;
  background: var(--accent-dim);
  color: var(--accent);
  text-transform: uppercase;
}

.chat-topbar-actions {
  margin-left: auto;
  display: flex;
  gap: 8px;
}

.chat-messages {
  flex: 1;
  overflow-y: auto;
  padding: 16px;
  display: flex;
  flex-direction: column;
  gap: 16px;
  scroll-behavior: smooth;
}

/* Scrollbar */
.chat-messages::-webkit-scrollbar { width: 4px; }
.chat-messages::-webkit-scrollbar-track { background: transparent; }
.chat-messages::-webkit-scrollbar-thumb { background: var(--border); border-radius: 2px; }

.message {
  display: flex;
  gap: 10px;
  max-width: 85%;
}

.message.user {
  align-self: flex-end;
  flex-direction: row-reverse;
}

.message.assistant {
  align-self: flex-start;
}

.msg-avatar {
  width: 32px;
  height: 32px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 14px;
  flex-shrink: 0;
}

.message.assistant .msg-avatar {
  background: var(--accent-dim);
}

.message.user .msg-avatar {
  background: var(--gold-dim);
}

.msg-bubble {
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 10px 14px;
  color: var(--text);
  line-height: 1.6;
  white-space: pre-wrap;
  word-break: break-word;
}

.message.user .msg-bubble {
  background: var(--accent-dim);
  border-color: var(--accent);
}

.msg-bubble strong { color: var(--accent); }
.msg-bubble em { color: var(--text-muted); }
.msg-bubble code {
  background: var(--bg);
  padding: 1px 4px;
  border-radius: 3px;
  font-family: monospace;
  font-size: 12px;
}

/* ── Chat Input ───────────────────────────────────────────── */
.chat-input-area {
  padding: 12px 16px;
  border-top: 1px solid var(--border);
  background: var(--bg-surface);
  flex-shrink: 0;
}

.chat-input-row {
  display: flex;
  gap: 8px;
  align-items: flex-end;
}

.chat-input-row textarea {
  flex: 1;
  background: var(--bg-input);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 10px 12px;
  color: var(--text);
  font-size: 14px;
  font-family: inherit;
  resize: none;
  outline: none;
  transition: var(--transition);
  min-height: 42px;
  max-height: 120px;
  line-height: 1.5;
}

.chat-input-row textarea:focus {
  border-color: var(--accent);
}

.chat-input-row textarea::placeholder {
  color: var(--text-dim);
}

/* ── Card Panel ───────────────────────────────────────────── */
.card-panel {
  width: 420px;
  flex-shrink: 0;
  display: flex;
  flex-direction: column;
  background: var(--bg-surface);
  overflow: hidden;
}

.card-panel-header {
  padding: 12px 16px;
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  gap: 8px;
  flex-shrink: 0;
}

.card-panel-header h3 {
  font-size: 14px;
  font-weight: 600;
  color: var(--gold);
  flex: 1;
}

.card-tabs {
  display: flex;
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}

.tab-btn {
  flex: 1;
  padding: 8px;
  background: none;
  border: none;
  color: var(--text-muted);
  font-size: 12px;
  font-weight: 500;
  cursor: pointer;
  border-bottom: 2px solid transparent;
  transition: var(--transition);
}

.tab-btn:hover { color: var(--text); }
.tab-btn.active {
  color: var(--accent);
  border-bottom-color: var(--accent);
}

.card-content {
  flex: 1;
  overflow-y: auto;
}

.card-content::-webkit-scrollbar { width: 4px; }
.card-content::-webkit-scrollbar-thumb { background: var(--border); border-radius: 2px; }

.tab-pane { display: none; padding: 16px; }
.tab-pane.active { display: block; }

/* Preview fields */
.preview-field {
  margin-bottom: 16px;
}

.preview-label {
  font-size: 10px;
  font-weight: 700;
  letter-spacing: 1px;
  color: var(--text-dim);
  text-transform: uppercase;
  margin-bottom: 4px;
}

.preview-value {
  font-size: 13px;
  color: var(--text);
  line-height: 1.6;
  white-space: pre-wrap;
  word-break: break-word;
}

.preview-tags {
  display: flex;
  flex-wrap: wrap;
  gap: 4px;
  margin-top: 4px;
}

.preview-tag {
  font-size: 11px;
  padding: 2px 8px;
  border-radius: 20px;
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  color: var(--text-muted);
}

.preview-greeting {
  background: var(--bg-elevated);
  border: 1px solid var(--border-soft);
  border-radius: var(--radius);
  padding: 10px 12px;
  font-size: 12px;
  color: var(--text-muted);
  margin-bottom: 6px;
  white-space: pre-wrap;
  word-break: break-word;
  line-height: 1.5;
}

.preview-greeting-num {
  font-size: 10px;
  color: var(--accent);
  font-weight: 600;
  margin-bottom: 4px;
}

/* JSON tab */
.json-pane {
  padding: 0;
}

.json-pane pre {
  padding: 16px;
  font-family: 'Consolas', 'Fira Code', monospace;
  font-size: 11px;
  color: var(--text-muted);
  white-space: pre-wrap;
  word-break: break-all;
  line-height: 1.6;
}

/* ── Empty state ─────────────────────────────────────────── */
.card-empty {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  height: 100%;
  gap: 12px;
  color: var(--text-dim);
  text-align: center;
  padding: 32px;
}

.card-empty-icon { font-size: 40px; }
.card-empty-text { font-size: 13px; line-height: 1.6; }

/* ── Buttons ──────────────────────────────────────────────── */
.btn {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 8px 14px;
  border-radius: var(--radius);
  font-size: 13px;
  font-weight: 500;
  cursor: pointer;
  border: none;
  outline: none;
  transition: var(--transition);
  white-space: nowrap;
}

.btn:disabled {
  opacity: 0.45;
  cursor: not-allowed;
}

.btn-primary {
  background: var(--accent);
  color: #fff;
}
.btn-primary:hover:not(:disabled) { filter: brightness(1.1); }

.btn-secondary {
  background: var(--accent-dim);
  color: var(--accent);
  border: 1px solid var(--accent);
}
.btn-secondary:hover:not(:disabled) { background: var(--accent-glow); }

.btn-ghost {
  background: transparent;
  color: var(--text-muted);
  border: 1px solid var(--border);
}
.btn-ghost:hover:not(:disabled) { color: var(--text); border-color: var(--text-muted); }

.btn-gold {
  background: var(--gold-dim);
  color: var(--gold);
  border: 1px solid var(--gold);
}
.btn-gold:hover:not(:disabled) { filter: brightness(1.1); }

.btn-send {
  background: var(--accent);
  color: #fff;
  border: none;
  border-radius: var(--radius);
  padding: 10px 16px;
  font-size: 14px;
  cursor: pointer;
  transition: var(--transition);
  flex-shrink: 0;
  align-self: flex-end;
}
.btn-send:hover:not(:disabled) { filter: brightness(1.1); }
.btn-send:disabled { opacity: 0.4; cursor: not-allowed; }

.btn-icon {
  background: none;
  border: none;
  color: var(--text-muted);
  cursor: pointer;
  padding: 4px 6px;
  border-radius: 4px;
  font-size: 14px;
  transition: var(--transition);
}
.btn-icon:hover { color: var(--text); background: var(--bg-elevated); }

/* ── Loading ──────────────────────────────────────────────── */
.loading-bar {
  height: 2px;
  background: linear-gradient(90deg, var(--accent), var(--gold), var(--accent));
  background-size: 200% 100%;
  animation: loading-sweep 1.5s linear infinite;
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  z-index: 999;
}

@keyframes loading-sweep {
  0%   { background-position: 200% 0; }
  100% { background-position: -200% 0; }
}

.typing-dots {
  display: inline-flex;
  gap: 4px;
  padding: 10px 14px;
}
.typing-dots span {
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: var(--accent);
  animation: dot-bounce 1.2s ease-in-out infinite;
}
.typing-dots span:nth-child(2) { animation-delay: 0.2s; }
.typing-dots span:nth-child(3) { animation-delay: 0.4s; }

@keyframes dot-bounce {
  0%, 80%, 100% { transform: translateY(0); opacity: 0.4; }
  40%            { transform: translateY(-6px); opacity: 1; }
}

/* ── Settings drawer ─────────────────────────────────────── */
.settings-drawer {
  position: fixed;
  top: 0;
  right: -360px;
  width: 360px;
  height: 100vh;
  background: var(--bg-surface);
  border-left: 1px solid var(--border);
  z-index: 200;
  transition: right 250ms ease;
  display: flex;
  flex-direction: column;
  box-shadow: var(--shadow);
}

.settings-drawer.open { right: 0; }

.settings-header {
  padding: 16px;
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.settings-header h3 { font-size: 15px; font-weight: 600; }

.settings-body { padding: 16px; display: flex; flex-direction: column; gap: 16px; }

.settings-field { display: flex; flex-direction: column; gap: 6px; }
.settings-field label { font-size: 12px; color: var(--text-muted); font-weight: 500; }
.settings-field input {
  background: var(--bg-input);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 9px 12px;
  color: var(--text);
  font-size: 13px;
  font-family: 'Consolas', monospace;
  outline: none;
}
.settings-field input:focus { border-color: var(--accent); }
.settings-field .field-note { font-size: 11px; color: var(--text-dim); }

/* ── Regen dialog ─────────────────────────────────────────── */
.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.6);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 300;
  backdrop-filter: blur(3px);
}

.modal-box {
  background: var(--bg-surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  padding: 24px;
  width: 460px;
  max-width: calc(100vw - 40px);
  box-shadow: var(--shadow);
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.modal-box h3 { font-size: 15px; font-weight: 600; }
.modal-box p { font-size: 13px; color: var(--text-muted); }
.modal-box textarea {
  background: var(--bg-input);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 10px 12px;
  color: var(--text);
  font-size: 13px;
  font-family: inherit;
  resize: vertical;
  outline: none;
  min-height: 80px;
}
.modal-box textarea:focus { border-color: var(--accent); }
.modal-actions { display: flex; justify-content: flex-end; gap: 8px; }

/* ── Responsive ───────────────────────────────────────────── */
@media (max-width: 768px) {
  .card-panel { width: 100%; border-right: none; border-top: 1px solid var(--border); }
  .app-body { flex-direction: column; }
}

/* ── Toast ───────────────────────────────────────────────── */
.toast-container {
  position: fixed;
  bottom: 20px;
  right: 20px;
  z-index: 999;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.toast {
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 10px 14px;
  font-size: 13px;
  color: var(--text);
  box-shadow: var(--shadow);
  animation: toast-in 200ms ease;
}

.toast.success { border-color: var(--green); }
.toast.error   { border-color: var(--red); }

@keyframes toast-in {
  from { opacity: 0; transform: translateY(8px); }
  to   { opacity: 1; transform: translateY(0); }
}
