:root {
  --primary: #09639e;
  --primary-dark: #074b79;
  --accent: #a04124;
  --bg: #f5f7f9;
  --surface: #ffffff;
  --border: #e0e4e8;
  --text: #1a2027;
  --text-muted: #5a6472;
  --user-bubble: #e8f4f6;
  --assistant-bubble: #ffffff;
  --shadow: 0 2px 8px rgba(0,0,0,0.08);
  --radius: 12px;
  --radius-sm: 6px;
}

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

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

/* Header */
.header {
  background: var(--primary);
  color: #fff;
  padding: 0 24px;
  height: 60px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-shrink: 0;
  box-shadow: 0 2px 6px rgba(0,0,0,0.15);
}
.header-left { display: flex; align-items: baseline; gap: 12px; }
.header-title { font-size: 1.1rem; font-weight: 700; letter-spacing: -0.2px; }
.header-subtitle { font-size: 0.8rem; opacity: 0.8; display: none; }
@media (min-width: 640px) { .header-subtitle { display: block; } }
.header-right { display: flex; align-items: center; gap: 12px; }

.session-badge {
  font-size: 0.72rem;
  padding: 2px 8px;
  border-radius: 99px;
  background: rgba(255,255,255,0.2);
  color: #fff;
}
.session-badge.saved { background: rgba(255,255,255,0.25); }

/* Buttons */
.btn {
  display: inline-flex; align-items: center; justify-content: center;
  padding: 7px 16px;
  border-radius: var(--radius-sm);
  font-size: 0.85rem; font-weight: 500;
  cursor: pointer; border: none; text-decoration: none;
  transition: opacity 0.15s, background 0.15s;
  white-space: nowrap;
}
.btn:disabled { opacity: 0.55; cursor: not-allowed; }
.btn-primary { background: var(--primary); color: #fff; }
.btn-primary:hover:not(:disabled) { background: var(--primary-dark); }
.btn-outline { background: transparent; color: #fff; border: 1.5px solid rgba(255,255,255,0.6); }
.btn-outline:hover { background: rgba(255,255,255,0.15); }
.btn-ghost { background: transparent; color: var(--text-muted); }
.btn-ghost:hover { background: var(--border); }
.btn-success { background: var(--accent); color: #fff; }
.btn-success:hover { opacity: 0.9; }
.btn-danger { background: #c0392b; color: #fff; }
.btn-send { padding: 9px 18px; font-size: 0.9rem; }

/* Main layout */
.main {
  flex: 1;
  display: flex;
  overflow: hidden;
}

/* Chat section */
.chat-section {
  flex: 1;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  max-width: 860px;
  margin: 0 auto;
  width: 100%;
  padding: 0;
}

/* Messages */
.chat-messages {
  flex: 1;
  overflow-y: auto;
  padding: 20px 24px;
  display: flex;
  flex-direction: column;
  gap: 12px;
  scroll-behavior: smooth;
}

.welcome-message {
  text-align: center;
  padding: 40px 20px;
  color: var(--text-muted);
}
.welcome-icon { font-size: 2.5rem; margin-bottom: 12px; }
.welcome-message p { line-height: 1.6; margin-bottom: 8px; }
.welcome-note { font-size: 0.8rem; opacity: 0.7; }

/* Chat bubbles */
.message {
  display: flex;
  gap: 10px;
  max-width: 100%;
  animation: fadeIn 0.2s ease;
}
@keyframes fadeIn { from { opacity: 0; transform: translateY(6px); } to { opacity: 1; transform: none; } }

.message.user { flex-direction: row-reverse; }

.message-avatar {
  width: 32px; height: 32px;
  border-radius: 50%;
  font-size: 0.75rem;
  display: flex; align-items: center; justify-content: center;
  flex-shrink: 0;
  font-weight: 600;
}
.message.user .message-avatar { background: var(--primary); color: #fff; }
.message.assistant .message-avatar { background: var(--border); color: var(--text); }

.message-bubble {
  max-width: 75%;
  padding: 10px 14px;
  border-radius: var(--radius);
  line-height: 1.55;
  font-size: 0.9rem;
  white-space: pre-wrap;
  word-break: break-word;
}
.message.user .message-bubble {
  background: var(--primary);
  color: #fff;
  border-bottom-right-radius: var(--radius-sm);
}
.message.assistant .message-bubble {
  background: var(--assistant-bubble);
  border: 1px solid var(--border);
  border-bottom-left-radius: var(--radius-sm);
  box-shadow: var(--shadow);
}

.message-bubble strong { font-weight: 600; }
.message.user .message-bubble strong { color: #fff; }

/* Thinking indicator */
.thinking-bubble {
  display: flex; align-items: center; gap: 6px;
  padding: 10px 14px;
  background: var(--assistant-bubble);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  border-bottom-left-radius: var(--radius-sm);
  box-shadow: var(--shadow);
  font-size: 0.85rem;
  color: var(--text-muted);
}
.dots { display: flex; gap: 4px; }
.dot {
  width: 6px; height: 6px;
  border-radius: 50%;
  background: var(--text-muted);
  animation: bounce 1.2s infinite;
}
.dot:nth-child(2) { animation-delay: 0.2s; }
.dot:nth-child(3) { animation-delay: 0.4s; }
@keyframes bounce {
  0%, 60%, 100% { transform: translateY(0); }
  30% { transform: translateY(-6px); }
}

/* System message */
.message.system .message-bubble {
  background: #fffbeb;
  border-color: #fde68a;
  color: #92400e;
  font-size: 0.82rem;
  max-width: 100%;
}

/* Cards panel */
.cards-panel {
  margin: 0 24px 8px;
  border: 1.5px solid var(--primary);
  border-radius: var(--radius);
  background: var(--surface);
  overflow: hidden;
  box-shadow: var(--shadow);
  max-height: 55vh;
}
.cards-panel-header {
  background: var(--primary);
  color: #fff;
  padding: 12px 16px;
}
.cards-panel-header h2 { font-size: 0.95rem; font-weight: 600; }
.cards-panel-hint { font-size: 0.78rem; opacity: 0.85; margin-top: 2px; }
.cards-list { padding: 12px; display: flex; flex-direction: column; gap: 8px; max-height: calc(50vh - 120px); overflow-y: auto; }
.cards-actions { padding: 10px 12px; border-top: 1px solid var(--border); display: flex; gap: 8px; }

/* Card item */
.card-item {
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  overflow: hidden;
  flex-shrink: 0;
}
.card-item-header {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 6px 10px;
  background: var(--bg);
  border-bottom: 1px solid var(--border);
  font-size: 0.8rem;
  font-weight: 600;
  cursor: pointer;
  user-select: none;
}
.card-item-header:hover { background: var(--surface-2, #f0f0f0); }
.card-item-toggle { margin-left: auto; font-size: 0.7rem; color: var(--text-muted); }
.card-item .card-fields { display: none; }
.card-item.expanded .card-fields { display: grid; }
.card-item.expanded .card-item-header { border-bottom: 1px solid var(--border); }
.card-badge {
  padding: 1px 6px;
  border-radius: 99px;
  font-size: 0.7rem;
  font-weight: 700;
  background: var(--primary);
  color: #fff;
}
.card-fields { display: grid; grid-template-columns: 1fr 1fr; gap: 6px; padding: 8px 10px; }
.card-field { display: flex; flex-direction: column; gap: 2px; }
.card-field.full { grid-column: 1 / -1; }
.card-field label { font-size: 0.7rem; font-weight: 600; color: var(--text-muted); text-transform: uppercase; letter-spacing: 0.4px; }
.card-field input,
.card-field textarea {
  border: 1px solid var(--border);
  border-radius: 4px;
  padding: 4px 8px;
  font-size: 0.82rem;
  font-family: inherit;
  background: #fff;
  resize: vertical;
  color: var(--text);
}
.card-field textarea { min-height: 52px; }
.card-field input:focus,
.card-field textarea:focus { outline: none; border-color: var(--primary); }
.card-remove { margin-left: auto; padding: 2px 8px; font-size: 0.75rem; background: transparent; border: 1px solid #e0e0e0; border-radius: 4px; cursor: pointer; color: #999; }
.card-remove:hover { background: #fee; border-color: #e99; color: #c00; }

/* Terms panel */
.terms-panel {
  margin: 0 24px 8px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
  overflow: hidden;
}
.terms-summary {
  display: flex; align-items: center; gap: 8px;
  padding: 10px 14px;
  cursor: pointer;
  font-size: 0.85rem; font-weight: 600;
  list-style: none;
  user-select: none;
}
.terms-summary::-webkit-details-marker { display: none; }
.terms-summary::before { content: "▶"; font-size: 0.65rem; color: var(--text-muted); transition: transform 0.2s; }
details[open] .terms-summary::before { transform: rotate(90deg); }
.terms-count { font-size: 0.75rem; font-weight: normal; color: var(--text-muted); }
.terms-list {
  padding: 8px 12px 12px;
  display: flex; flex-direction: column; gap: 4px;
  max-height: 220px; overflow-y: auto;
  border-top: 1px solid var(--border);
}
.term-row {
  display: flex; align-items: baseline; gap: 8px;
  font-size: 0.78rem; line-height: 1.4;
}
.term-chip {
  padding: 1px 7px;
  border-radius: 99px;
  font-size: 0.72rem;
  font-weight: 600;
  white-space: nowrap;
  flex-shrink: 0;
}
.term-desc {
  color: var(--text-muted);
  font-size: 0.75rem;
}
.term-robot { background: #dbeafe; color: #1e40af; }
.term-scenario { background: #dcfce7; color: #166534; }
.term-architecture { background: #fce7f3; color: #9d174d; }
.term-concept { background: #f3e8ff; color: #6b21a8; }

/* Input bar */
.input-bar {
  display: flex;
  align-items: flex-end;
  gap: 8px;
  padding: 12px 24px 8px;
  border-top: 1px solid var(--border);
  background: var(--surface);
}
.pdf-upload-btn {
  display: flex; align-items: center; justify-content: center;
  width: 38px; height: 38px;
  border-radius: var(--radius-sm);
  border: 1.5px solid var(--border);
  cursor: pointer;
  font-size: 1.1rem;
  background: var(--bg);
  flex-shrink: 0;
  transition: border-color 0.15s;
}
.pdf-upload-btn:hover { border-color: var(--primary); }
.chat-input {
  flex: 1;
  border: 1.5px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 9px 12px;
  font-size: 0.9rem;
  font-family: inherit;
  resize: none;
  max-height: 120px;
  overflow-y: auto;
  line-height: 1.4;
  transition: border-color 0.15s;
}
.chat-input:focus { outline: none; border-color: var(--primary); }

.input-bar-footer {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 4px 24px 12px;
  gap: 12px;
}
.pdf-hint { font-size: 0.75rem; color: var(--text-muted); }
.download-area { flex-shrink: 0; }

/* PDF prompt bar */
.pdf-prompt-bar {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 24px;
  background: var(--surface-2, #f5f5f5);
  border-top: 1px solid var(--border, #e0e0e0);
  flex-wrap: wrap;
}
.pdf-prompt-filename {
  font-size: 0.8rem;
  color: var(--text-muted);
  white-space: nowrap;
  flex-shrink: 0;
}
.pdf-prompt-input {
  flex: 1;
  min-width: 200px;
  padding: 6px 10px;
  border: 1px solid var(--border, #ccc);
  border-radius: 6px;
  font-size: 0.85rem;
  background: var(--surface, #fff);
  color: var(--text, #222);
}
.pdf-prompt-input:focus { outline: none; border-color: var(--primary); }

/* Modal */
.modal-overlay {
  position: fixed; inset: 0;
  background: rgba(0,0,0,0.4);
  display: flex; align-items: center; justify-content: center;
  z-index: 100;
}
.modal {
  background: var(--surface);
  border-radius: var(--radius);
  padding: 28px 32px;
  max-width: 380px;
  width: 90%;
  box-shadow: 0 8px 32px rgba(0,0,0,0.18);
}
.modal h2 { margin-bottom: 10px; font-size: 1.1rem; }
.modal p { font-size: 0.88rem; color: var(--text-muted); line-height: 1.5; margin-bottom: 16px; }
.modal-actions { display: flex; gap: 10px; margin-bottom: 12px; }
.modal-note { font-size: 0.75rem; opacity: 0.65; }

/* Loading overlay */
.loading-overlay {
  position: fixed; inset: 0;
  background: rgba(0,0,0,0.45);
  display: flex; align-items: center; justify-content: center;
  z-index: 200;
}
.loading-box {
  background: var(--surface);
  border-radius: var(--radius);
  padding: 28px 36px;
  display: flex; flex-direction: column; align-items: center; gap: 14px;
  box-shadow: 0 8px 32px rgba(0,0,0,0.18);
  font-size: 0.9rem; color: var(--text-muted);
}
.spinner {
  width: 32px; height: 32px;
  border: 3px solid var(--border);
  border-top-color: var(--primary);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

.hidden { display: none !important; }
