*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

body {
  font-family: 'Segoe UI', sans-serif;
  background: #f4f6f9;
  color: #333;
  height: 100vh;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

/* ─── ヘッダー（共通） ───────────────────────────────────────────────────────── */
.app-header {
  position: sticky; top: 0; z-index: 100;
  background: #232f3e; color: #fff;
  height: 54px; padding: 0 24px;
  display: flex; align-items: center; justify-content: space-between;
  box-shadow: 0 2px 8px rgba(0,0,0,0.3);
  flex-shrink: 0;
}
.header-left { display: flex; align-items: center; gap: 10px; }
.header-logo { font-size: 0.85rem; color: #ff9900; font-weight: 700; text-decoration: none; letter-spacing: 0.5px; }
.header-sep { color: #4a5568; font-size: 0.9rem; }
.header-title { font-size: 0.95rem; font-weight: 600; color: #e2e8f0; }
.header-right { display: flex; align-items: center; gap: 8px; }

/* API設定バー */
.api-bar {
  background: #fffbe6;
  border-bottom: 1px solid #ffe58f;
  padding: 8px 20px;
  display: flex;
  align-items: center;
  gap: 10px;
  font-size: 0.82rem;
  flex-shrink: 0;
}
.api-bar.ok { display: none; }
.api-bar label { color: #7a4f00; font-weight: 600; white-space: nowrap; }
.api-key-input {
  flex: 1;
  max-width: 380px;
  padding: 5px 10px;
  border: 1px solid #d9d9d9;
  border-radius: 4px;
  font-size: 0.82rem;
  font-family: monospace;
}
.save-key-btn {
  padding: 5px 14px;
  background: #ff9900;
  color: #fff;
  border: none;
  border-radius: 4px;
  font-size: 0.82rem;
  cursor: pointer;
  font-weight: 600;
}
.save-key-btn:hover { background: #e68a00; }

/* ─── メインレイアウト ───────────────────────────────────────────────────────── */
.layout {
  display: flex;
  flex: 1;
  overflow: hidden;
}

/* ─── 左サイドバー（会話履歴） ───────────────────────────────────────────────── */
.sidebar {
  width: 240px;
  background: #1a2332;
  display: flex;
  flex-direction: column;
  flex-shrink: 0;
  overflow: hidden;
}

.new-chat-btn {
  margin: 12px;
  padding: 10px;
  background: transparent;
  border: 1px solid #4a5568;
  border-radius: 8px;
  color: #e2e8f0;
  font-size: 0.85rem;
  font-weight: 600;
  cursor: pointer;
  text-align: left;
  transition: background 0.15s;
}
.new-chat-btn:hover { background: #2d3748; }

.sidebar-label {
  padding: 4px 16px 8px;
  font-size: 0.72rem;
  font-weight: 700;
  color: #718096;
  text-transform: uppercase;
  letter-spacing: 0.5px;
}

.conv-list {
  flex: 1;
  overflow-y: auto;
  padding: 0 8px 12px;
}
.conv-list::-webkit-scrollbar { width: 4px; }
.conv-list::-webkit-scrollbar-thumb { background: #4a5568; border-radius: 4px; }

.conv-empty {
  padding: 12px;
  font-size: 0.8rem;
  color: #718096;
  text-align: center;
}

.conv-item {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 8px 10px;
  border-radius: 6px;
  cursor: pointer;
  color: #a0aec0;
  font-size: 0.83rem;
  transition: background 0.15s;
  position: relative;
}
.conv-item:hover { background: #2d3748; color: #e2e8f0; }
.conv-item.active { background: #2d3748; color: #fff; }

.conv-body {
  flex: 1;
  overflow: hidden;
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.conv-title {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.conv-desc {
  font-size: 0.72rem;
  color: #4a5568;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  line-height: 1.3;
}
.conv-item.active .conv-desc { color: #718096; }

.conv-delete {
  display: none;
  background: none;
  border: none;
  color: #718096;
  font-size: 0.75rem;
  cursor: pointer;
  padding: 2px 4px;
  border-radius: 3px;
  flex-shrink: 0;
  line-height: 1;
}
.conv-delete:hover { color: #fc8181; background: rgba(255,255,255,0.1); }
.conv-item:hover .conv-delete,
.conv-item.active .conv-delete { display: block; }

/* ─── チャットエリア ─────────────────────────────────────────────────────────── */
.chat-area {
  flex: 1;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  background: #f4f6f9;
}

/* メッセージ一覧 */
.messages {
  flex: 1;
  overflow-y: auto;
  padding: 24px;
  display: flex;
  flex-direction: column;
  gap: 20px;
  max-width: 860px;
  width: 100%;
  margin: 0 auto;
  align-self: center;
  box-sizing: border-box;
}

/* ウェルカム画面 */
.welcome {
  max-width: 520px;
  margin: 32px auto;
  text-align: center;
}
.welcome .icon { font-size: 3rem; margin-bottom: 16px; }
.welcome h2 { font-size: 1.2rem; color: #232f3e; margin-bottom: 8px; }
.welcome p { font-size: 0.87rem; color: #777; line-height: 1.7; margin-bottom: 24px; }

.example-questions { display: flex; flex-direction: column; gap: 8px; }
.example-label { font-size: 0.75rem; color: #aaa; font-weight: 600; margin-bottom: 2px; }
.example-btn {
  background: #fff;
  border: 1px solid #dce1e7;
  border-radius: 8px;
  padding: 10px 16px;
  font-size: 0.85rem;
  color: #444;
  cursor: pointer;
  text-align: left;
  transition: border-color 0.15s, background 0.15s;
}
.example-btn:hover { border-color: #ff9900; background: #fffaf0; color: #232f3e; }

/* メッセージ */
.msg {
  display: flex;
  gap: 12px;
}
.msg.user { flex-direction: row-reverse; }

.msg-avatar {
  width: 34px;
  height: 34px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 1rem;
  flex-shrink: 0;
  margin-top: 2px;
}
.msg.user .msg-avatar { background: #ff9900; }
.msg.assistant .msg-avatar { background: #232f3e; }

.msg-bubble {
  padding: 12px 16px;
  border-radius: 12px;
  font-size: 0.88rem;
  line-height: 1.7;
  max-width: 680px;
  word-break: break-word;
}
.msg.user .msg-bubble {
  background: #ff9900;
  color: #fff;
  border-top-right-radius: 4px;
}
.msg.assistant .msg-bubble {
  background: #fff;
  border: 1px solid #dce1e7;
  border-top-left-radius: 4px;
}

.msg-sources {
  margin-top: 5px;
  font-size: 0.74rem;
  color: #aaa;
  padding-left: 2px;
}
.pdf-link {
  color: #146eb4;
  text-decoration: none;
  border-bottom: 1px dashed #146eb4;
}
.pdf-link:hover { color: #ff9900; border-color: #ff9900; }

/* ローディング */
.typing-label { font-size: 0.8rem; color: #aaa; margin-bottom: 6px; }
.typing { display: flex; gap: 4px; align-items: center; }
.typing span {
  width: 6px; height: 6px;
  background: #ccc; border-radius: 50%;
  animation: bounce 1.2s infinite;
}
.typing span:nth-child(2) { animation-delay: 0.2s; }
.typing span:nth-child(3) { animation-delay: 0.4s; }
@keyframes bounce {
  0%, 60%, 100% { transform: translateY(0); }
  30% { transform: translateY(-6px); }
}

/* 入力エリア */
.input-area {
  padding: 12px 24px 16px;
  border-top: 1px solid #dce1e7;
  background: #f4f6f9;
  flex-shrink: 0;
}
.input-row {
  display: flex;
  gap: 10px;
  align-items: flex-end;
  max-width: 812px;
  margin: 0 auto;
}
.question-input {
  flex: 1;
  resize: none;
  border: 1px solid #dce1e7;
  border-radius: 8px;
  padding: 10px 14px;
  font-size: 0.9rem;
  font-family: 'Segoe UI', sans-serif;
  line-height: 1.5;
  max-height: 160px;
  outline: none;
  background: #fff;
  transition: border-color 0.15s;
}
.question-input:focus { border-color: #ff9900; }
.send-btn {
  width: 44px; height: 44px;
  background: #ff9900; color: #fff;
  border: none; border-radius: 8px;
  font-size: 1.1rem; cursor: pointer;
  display: flex; align-items: center; justify-content: center;
  transition: background 0.15s;
  flex-shrink: 0;
}
.send-btn:hover { background: #e68a00; }
.send-btn:disabled { background: #ccc; cursor: not-allowed; }
.input-hint {
  font-size: 0.74rem; color: #bbb;
  text-align: center; margin-top: 6px;
}

/* エラー表示 */
.error-msg {
  background: #fff2f0;
  border: 1px solid #ffccc7;
  color: #cf1322;
  border-radius: 8px;
  padding: 10px 14px;
  font-size: 0.85rem;
  max-width: 680px;
}
