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

:root {
  --bg-primary: #f7f7f8;
  --bg-secondary: #ffffff;
  --bg-user-msg: #2563eb;
  --bg-ai-msg: #ffffff;
  --text-primary: #1a1a2e;
  --text-secondary: #6b7280;
  --text-user-msg: #ffffff;
  --text-ai-msg: #1a1a2e;
  --border-color: #e5e7eb;
  --sidebar-bg: #1e293b;
  --sidebar-text: #cbd5e1;
  --sidebar-hover: #334155;
  --sidebar-active: #3b82f6;
  --shadow-sm: 0 1px 2px rgba(0, 0, 0, 0.05);
  --shadow-md: 0 4px 6px -1px rgba(0, 0, 0, 0.1);
  --radius-sm: 8px;
  --radius-md: 12px;
  --radius-lg: 16px;
  --sidebar-width: 260px;
}

body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  background: var(--bg-primary);
  color: var(--text-primary);
  height: 100vh;
  overflow: hidden;
}

#app {
  display: flex;
  height: 100vh;
}

.sidebar {
  width: var(--sidebar-width);
  background: var(--sidebar-bg);
  display: flex;
  flex-direction: column;
  flex-shrink: 0;
  transition: margin-left 0.3s ease, opacity 0.3s ease;
  overflow: hidden;
}

.sidebar.collapsed {
  margin-left: calc(-1 * var(--sidebar-width));
  opacity: 0;
  pointer-events: none;
}

.sidebar-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 16px;
  border-bottom: 1px solid rgba(255, 255, 255, 0.08);
  flex-shrink: 0;
}

.sidebar-header h2 {
  font-size: 15px;
  font-weight: 600;
  color: #f1f5f9;
}

.sidebar .icon-btn {
  color: var(--sidebar-text);
  border-radius: 6px;
}

.sidebar .icon-btn:hover {
  background: var(--sidebar-hover);
  color: #f1f5f9;
}

.conversation-list {
  list-style: none;
  flex: 1;
  overflow-y: auto;
  padding: 8px;
}

.conversation-list::-webkit-scrollbar {
  width: 4px;
}

.conversation-list::-webkit-scrollbar-thumb {
  background: rgba(255, 255, 255, 0.15);
  border-radius: 4px;
}

.conversation-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 10px 12px;
  border-radius: var(--radius-sm);
  cursor: pointer;
  transition: background 0.15s;
  margin-bottom: 2px;
}

.conversation-item:hover {
  background: var(--sidebar-hover);
}

.conversation-item.active {
  background: var(--sidebar-active);
}

.conversation-item.active .conversation-title {
  color: #ffffff;
}

.conversation-item.active .conversation-delete {
  color: rgba(255, 255, 255, 0.7);
}

.conversation-title {
  font-size: 13px;
  color: var(--sidebar-text);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  flex: 1;
  line-height: 1.4;
}

.conversation-delete {
  background: none;
  border: none;
  cursor: pointer;
  color: rgba(255, 255, 255, 0.3);
  padding: 2px;
  border-radius: 4px;
  display: flex;
  align-items: center;
  justify-content: center;
  opacity: 0;
  transition: opacity 0.15s, color 0.15s;
  flex-shrink: 0;
  margin-left: 6px;
}

.conversation-item:hover .conversation-delete {
  opacity: 1;
}

.conversation-delete:hover {
  color: #f87171;
}

.main-area {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-width: 0;
  height: 100vh;
}

.header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 20px;
  background: var(--bg-secondary);
  border-bottom: 1px solid var(--border-color);
  flex-shrink: 0;
}

.header-left {
  display: flex;
  align-items: center;
  gap: 10px;
}

.header h1 {
  font-size: 18px;
  font-weight: 600;
  color: var(--text-primary);
}

.icon-btn {
  background: none;
  border: none;
  cursor: pointer;
  color: var(--text-secondary);
  padding: 6px;
  border-radius: var(--radius-sm);
  transition: background 0.2s, color 0.2s;
  display: flex;
  align-items: center;
  justify-content: center;
}

.icon-btn:hover {
  background: var(--bg-primary);
  color: var(--text-primary);
}

.settings-panel {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.4);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 100;
  backdrop-filter: blur(4px);
}

.settings-panel.hidden {
  display: none;
}

.settings-content {
  background: var(--bg-secondary);
  border-radius: var(--radius-lg);
  padding: 28px;
  width: 90%;
  max-width: 440px;
  box-shadow: var(--shadow-md);
}

.settings-content h2 {
  font-size: 18px;
  font-weight: 600;
  margin-bottom: 20px;
}

.form-group {
  margin-bottom: 16px;
}

.form-group label {
  display: block;
  font-size: 13px;
  font-weight: 500;
  color: var(--text-secondary);
  margin-bottom: 6px;
}

.form-group input {
  width: 100%;
  padding: 10px 12px;
  border: 1px solid var(--border-color);
  border-radius: var(--radius-sm);
  font-size: 14px;
  outline: none;
  transition: border-color 0.2s;
  background: var(--bg-primary);
}

.form-group input:focus {
  border-color: var(--bg-user-msg);
}

.settings-actions {
  display: flex;
  gap: 10px;
  margin-top: 24px;
}

.btn {
  padding: 10px 20px;
  border-radius: var(--radius-sm);
  font-size: 14px;
  font-weight: 500;
  cursor: pointer;
  border: none;
  transition: opacity 0.2s;
}

.btn:hover {
  opacity: 0.85;
}

.btn-primary {
  background: var(--bg-user-msg);
  color: var(--text-user-msg);
}

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

.chat-container {
  flex: 1;
  overflow-y: auto;
  padding: 20px;
  scroll-behavior: smooth;
}

.messages {
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.message {
  display: flex;
  gap: 10px;
  max-width: 85%;
  animation: fadeIn 0.3s ease;
}

@keyframes fadeIn {
  from { opacity: 0; transform: translateY(8px); }
  to { opacity: 1; transform: translateY(0); }
}

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

.message-avatar {
  width: 32px;
  height: 32px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 14px;
  font-weight: 600;
  flex-shrink: 0;
}

.message.user .message-avatar {
  background: var(--bg-user-msg);
  color: var(--text-user-msg);
}

.message.assistant .message-avatar {
  background: #e0e7ff;
  color: #4338ca;
}

.message-bubble {
  padding: 12px 16px;
  border-radius: var(--radius-md);
  line-height: 1.6;
  font-size: 14px;
  word-break: break-word;
  white-space: pre-wrap;
}

.message.user .message-bubble {
  background: var(--bg-user-msg);
  color: var(--text-user-msg);
  border-bottom-right-radius: 4px;
}

.message.assistant .message-bubble {
  background: var(--bg-ai-msg);
  color: var(--text-ai-msg);
  border-bottom-left-radius: 4px;
  box-shadow: var(--shadow-sm);
}

.typing-indicator {
  display: inline-flex;
  gap: 4px;
  padding: 4px 0;
}

.typing-indicator span {
  width: 6px;
  height: 6px;
  background: var(--text-secondary);
  border-radius: 50%;
  animation: bounce 1.4s infinite ease-in-out;
}

.typing-indicator span:nth-child(1) { animation-delay: 0s; }
.typing-indicator span:nth-child(2) { animation-delay: 0.2s; }
.typing-indicator span:nth-child(3) { animation-delay: 0.4s; }

@keyframes bounce {
  0%, 80%, 100% { transform: scale(0.6); opacity: 0.4; }
  40% { transform: scale(1); opacity: 1; }
}

.input-area {
  padding: 16px 20px;
  background: var(--bg-secondary);
  border-top: 1px solid var(--border-color);
  flex-shrink: 0;
}

.input-wrapper {
  display: flex;
  align-items: center;
  gap: 10px;
  background: var(--bg-primary);
  border: 1px solid var(--border-color);
  border-radius: var(--radius-md);
  padding: 8px 12px;
  transition: border-color 0.2s;
}

.input-wrapper:focus-within {
  border-color: var(--bg-user-msg);
}

#user-input {
  flex: 1;
  border: none;
  outline: none;
  resize: none;
  font-size: 14px;
  line-height: 1.5;
  background: transparent;
  color: var(--text-primary);
  max-height: 120px;
  font-family: inherit;
}

#user-input::placeholder {
  color: var(--text-secondary);
}

.send-btn {
  background: var(--bg-user-msg);
  color: var(--text-user-msg);
  border: none;
  border-radius: var(--radius-sm);
  width: 36px;
  height: 36px;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  flex-shrink: 0;
  transition: opacity 0.2s, transform 0.1s;
}

.send-btn:hover {
  opacity: 0.85;
}

.send-btn:active {
  transform: scale(0.95);
}

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

.error-bubble {
  background: #fef2f2 !important;
  color: #dc2626 !important;
  border: 1px solid #fecaca;
}

.questionnaire-header {
  font-size: 14px;
  font-weight: 600;
  color: var(--text-primary);
  margin-bottom: 16px;
  padding-bottom: 10px;
  border-bottom: 1px solid var(--border-color);
}

.loading-hint {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 4px 0;
}

.loading-spinner {
  width: 18px;
  height: 18px;
  border: 2px solid var(--border-color);
  border-top-color: var(--bg-user-msg);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
  flex-shrink: 0;
}

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

.loading-text {
  font-size: 14px;
  color: var(--text-secondary);
}

.questionnaire-form {
  display: flex;
  flex-direction: column;
  gap: 14px;
}

.questionnaire-field {
  display: flex;
  flex-direction: column;
  gap: 5px;
}

.questionnaire-label {
  font-size: 13px;
  font-weight: 500;
  color: var(--text-primary);
  line-height: 1.4;
}

.questionnaire-input {
  width: 100%;
  padding: 8px 10px;
  border: 1px solid var(--border-color);
  border-radius: 6px;
  font-size: 13px;
  outline: none;
  transition: border-color 0.2s;
  background: var(--bg-primary);
  color: var(--text-primary);
  font-family: inherit;
}

.questionnaire-input:focus {
  border-color: var(--bg-user-msg);
}

.questionnaire-actions {
  display: flex;
  justify-content: flex-end;
  margin-top: 6px;
  padding-top: 12px;
  border-top: 1px solid var(--border-color);
}

.message.assistant .message-bubble:has(.questionnaire-form) {
  max-width: 520px;
  width: 100%;
}

.risk-result {
  display: flex;
  flex-direction: column;
  gap: 14px;
}

.risk-level {
  font-size: 16px;
  font-weight: 700;
  color: #d97706;
  padding: 10px 14px;
  background: #fffbeb;
  border-left: 4px solid #f59e0b;
  border-radius: 6px;
}

.risk-section {
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.risk-label {
  font-size: 13px;
  font-weight: 600;
  color: var(--text-primary);
}

.risk-text {
  font-size: 13px;
  line-height: 1.7;
  color: var(--text-ai-msg);
}

.followup-field {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.followup-question {
  font-size: 13px;
  font-weight: 500;
  color: var(--text-primary);
  line-height: 1.4;
}

.followup-options {
  display: flex;
  gap: 12px;
}

.followup-radio {
  display: flex;
  align-items: center;
  gap: 5px;
  cursor: pointer;
  font-size: 13px;
  color: var(--text-primary);
  padding: 5px 12px;
  border: 1px solid var(--border-color);
  border-radius: 6px;
  transition: border-color 0.2s, background 0.2s;
}

.followup-radio:hover {
  border-color: var(--bg-user-msg);
}

.followup-radio input[type="radio"] {
  accent-color: var(--bg-user-msg);
  margin: 0;
  width: 14px;
  height: 14px;
  cursor: pointer;
}

.followup-radio:has(input:checked) {
  border-color: var(--bg-user-msg);
  background: #eff6ff;
}

.followup-summary {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.followup-summary-header {
  font-size: 14px;
  font-weight: 600;
  color: var(--text-primary);
  padding-bottom: 8px;
  border-bottom: 1px solid var(--border-color);
}

.followup-summary-line {
  font-size: 13px;
  line-height: 1.6;
  color: var(--text-ai-msg);
}

.analysis-result {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.analysis-header {
  font-size: 15px;
  font-weight: 700;
  color: var(--text-primary);
  padding-bottom: 10px;
  border-bottom: 1px solid var(--border-color);
}

.analysis-section {
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.analysis-label {
  font-size: 13px;
  font-weight: 600;
  color: var(--text-primary);
}

.analysis-text {
  font-size: 13px;
  line-height: 1.7;
  color: var(--text-ai-msg);
  padding-left: 12px;
  border-left: 3px solid #e0e7ff;
}

.message.assistant .message-bubble:has(.followup-form) {
  max-width: 520px;
  width: 100%;
}

.confirm-text {
  font-size: 14px;
  line-height: 1.7;
  color: var(--text-ai-msg);
  margin-bottom: 14px;
}

.confirm-actions {
  display: flex;
  gap: 10px;
}

.confirm-actions .btn {
  padding: 8px 20px;
  font-size: 13px;
}

.message-bubble img {
  max-width: 100%;
  max-height: 180px;
  width: auto;
  height: auto;
  object-fit: contain;
  border-radius: 6px;
  margin: 6px 0;
  display: block;
}

.message-bubble a {
  color: #2563eb;
  text-decoration: none;
}

.message-bubble a:hover {
  text-decoration: underline;
}

.message-bubble strong {
  font-weight: 600;
}

.message-bubble code {
  background: #f3f4f6;
  padding: 2px 6px;
  border-radius: 4px;
  font-size: 13px;
  font-family: "SF Mono", Monaco, monospace;
}

.message-bubble ul {
  padding-left: 20px;
  margin: 8px 0;
}

.message-bubble li {
  margin: 4px 0;
}

.message-bubble h4 {
  font-size: 15px;
  font-weight: 600;
  margin: 12px 0 8px;
  color: var(--text-primary);
}

@media (max-width: 768px) {
  .sidebar {
    position: fixed;
    left: 0;
    top: 0;
    bottom: 0;
    z-index: 50;
    box-shadow: 4px 0 12px rgba(0, 0, 0, 0.15);
  }

  .sidebar.collapsed {
    margin-left: calc(-1 * var(--sidebar-width));
  }

  .message {
    max-width: 92%;
  }

  .settings-content {
    width: 95%;
    padding: 20px;
  }
}
