:root {
  --bg: #f4f6f9;
  --surface: #ffffff;
  --border: #e2e6ec;
  --text: #1a1f2b;
  --text-muted: #6b7280;
  --primary: #3457d5;
  --primary-dark: #2941ab;
  --primary-soft: #eaefff;
  --user-bubble: #3457d5;
  --user-bubble-text: #ffffff;
  --assistant-bubble: #ffffff;
  --radius: 12px;
  --shadow: 0 1px 2px rgba(16, 24, 40, 0.06), 0 1px 3px rgba(16, 24, 40, 0.08);
}

* { box-sizing: border-box; }

html, body {
  height: 100%;
  margin: 0;
  font-family: "Segoe UI", -apple-system, BlinkMacSystemFont, Roboto, Helvetica, Arial, sans-serif;
  background: var(--bg);
  color: var(--text);
}

.app {
  height: 100%;
  display: flex;
  flex-direction: column;
}

.app-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px 28px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}

.brand {
  display: flex;
  align-items: center;
  gap: 12px;
}

.brand-mark {
  width: 38px;
  height: 38px;
  border-radius: 10px;
  background: linear-gradient(135deg, var(--primary), #6c8bff);
  color: white;
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 700;
  font-size: 14px;
  letter-spacing: 0.5px;
}

.brand-title {
  font-weight: 600;
  font-size: 15px;
  line-height: 1.3;
}

.brand-subtitle {
  font-size: 12px;
  color: var(--text-muted);
}

.tabs {
  display: flex;
  gap: 4px;
  background: var(--bg);
  padding: 4px;
  border-radius: 10px;
}

.tab-btn {
  border: none;
  background: transparent;
  padding: 8px 18px;
  border-radius: 8px;
  font-size: 13px;
  font-weight: 600;
  color: var(--text-muted);
  cursor: pointer;
  transition: all 0.15s ease;
}

.tab-btn:hover { color: var(--text); }

.tab-btn.active {
  background: var(--surface);
  color: var(--primary);
  box-shadow: var(--shadow);
}

main {
  flex: 1;
  overflow: hidden;
  display: flex;
}

.tab-panel {
  display: none;
  width: 100%;
  overflow: auto;
}

.tab-panel.active {
  display: flex;
  flex-direction: column;
}

/* ---------- Chat tab ---------- */

.chat-shell {
  max-width: 780px;
  width: 100%;
  margin: 0 auto;
  height: 100%;
  display: flex;
  flex-direction: column;
  padding: 20px 20px 0;
}

.chat-toolbar {
  display: flex;
  align-items: center;
  gap: 16px;
  padding-bottom: 14px;
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}

.field-inline {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 13px;
  color: var(--text-muted);
}

.field-inline input {
  width: 130px;
  padding: 6px 10px;
  border: 1px solid var(--border);
  border-radius: 8px;
  font-size: 13px;
}

.conv-id {
  flex: 1;
  font-size: 12px;
  color: var(--text-muted);
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  text-align: center;
}

.messages {
  flex: 1;
  overflow-y: auto;
  padding: 24px 4px;
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.empty-state {
  margin: auto;
  text-align: center;
  color: var(--text-muted);
  font-size: 14px;
  max-width: 360px;
  line-height: 1.6;
}

.empty-state-icon {
  font-size: 32px;
  margin-bottom: 10px;
}

.msg-row {
  display: flex;
  gap: 10px;
}

.msg-row.user {
  justify-content: flex-end;
}

.bubble {
  max-width: 78%;
  padding: 12px 16px;
  border-radius: var(--radius);
  font-size: 14px;
  line-height: 1.55;
  box-shadow: var(--shadow);
}

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

.msg-row.assistant .bubble {
  background: var(--assistant-bubble);
  border: 1px solid var(--border);
  border-bottom-left-radius: 4px;
}

.bubble p { margin: 0 0 8px; }
.bubble p:last-child { margin-bottom: 0; }
.bubble ol, .bubble ul { margin: 4px 0 8px; padding-left: 20px; }
.bubble strong { font-weight: 700; }

.msg-meta {
  margin-top: 8px;
  font-size: 11.5px;
  color: var(--text-muted);
  cursor: default;
}

.msg-row.assistant .msg-meta {
  border-top: 1px dashed var(--border);
  padding-top: 6px;
}

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

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

.typing span:nth-child(2) { animation-delay: 0.15s; }
.typing span:nth-child(3) { animation-delay: 0.3s; }

@keyframes bounce {
  0%, 80%, 100% { transform: translateY(0); opacity: 0.4; }
  40% { transform: translateY(-4px); opacity: 1; }
}

.chat-input-row {
  display: flex;
  gap: 10px;
  padding: 16px 0 20px;
  flex-shrink: 0;
}

.chat-input-row textarea {
  flex: 1;
  resize: none;
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 12px 14px;
  font-size: 14px;
  font-family: inherit;
  max-height: 140px;
}

.chat-input-row textarea:focus,
input:focus {
  outline: none;
  border-color: var(--primary);
  box-shadow: 0 0 0 3px var(--primary-soft);
}

.btn {
  border: none;
  border-radius: 9px;
  padding: 11px 18px;
  font-size: 13.5px;
  font-weight: 600;
  cursor: pointer;
  transition: background 0.15s ease, opacity 0.15s ease;
  white-space: nowrap;
}

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

.btn-primary {
  background: var(--primary);
  color: white;
}

.btn-primary:hover:not(:disabled) { background: var(--primary-dark); }

.btn-ghost {
  background: transparent;
  border: 1px solid var(--border);
  color: var(--text);
}

.btn-ghost:hover:not(:disabled) { background: var(--bg); }

.error-banner {
  background: #fee4e2;
  color: #a4292a;
  border: 1px solid #fecdca;
  padding: 10px 14px;
  border-radius: 9px;
  font-size: 13px;
  margin: 0 0 12px;
}

/* ---------- Admin tab ---------- */

.admin-shell {
  max-width: 900px;
  width: 100%;
  margin: 0 auto;
  padding: 28px 20px 60px;
  display: flex;
  flex-direction: column;
  gap: 24px;
}

.admin-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 22px 24px;
  box-shadow: var(--shadow);
}

.admin-card h2 {
  margin: 0 0 6px;
  font-size: 16px;
}

.admin-card-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.hint {
  color: var(--text-muted);
  font-size: 13px;
  margin: 0 0 16px;
}

.form-label {
  display: block;
  font-size: 12.5px;
  font-weight: 600;
  color: var(--text-muted);
  margin: 14px 0 6px;
}

.form-label .optional {
  font-weight: 400;
  text-transform: none;
  letter-spacing: 0;
}

.upload-form input[type="text"],
.upload-form input[type="file"],
#admin-key {
  width: 100%;
  padding: 9px 12px;
  border: 1px solid var(--border);
  border-radius: 8px;
  font-size: 13.5px;
  font-family: inherit;
}

.form-row {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 16px;
}

.upload-form .btn { margin-top: 18px; }

.upload-result {
  margin-top: 16px;
}

.result-card {
  background: #f0fdf4;
  border: 1px solid #bbf7d0;
  color: #166534;
  border-radius: 9px;
  padding: 12px 14px;
  font-size: 13px;
  line-height: 1.6;
}

.result-card.error {
  background: #fee4e2;
  border-color: #fecdca;
  color: #a4292a;
}

.guides-table {
  width: 100%;
  border-collapse: collapse;
  margin-top: 12px;
  font-size: 13px;
}

.guides-table th {
  text-align: left;
  color: var(--text-muted);
  font-weight: 600;
  font-size: 11.5px;
  text-transform: uppercase;
  letter-spacing: 0.03em;
  padding: 8px 10px;
  border-bottom: 1px solid var(--border);
}

.guides-table td {
  padding: 10px 10px;
  border-bottom: 1px solid var(--border);
  vertical-align: top;
}

.guides-table tr:last-child td { border-bottom: none; }

.muted { color: var(--text-muted); }

.status-pill {
  display: inline-block;
  padding: 2px 9px;
  border-radius: 999px;
  font-size: 11px;
  font-weight: 600;
}

.status-pill.active { background: #e6f4ea; color: #1e7b34; }
.status-pill.archived { background: #f1f1f1; color: #666; }

.link-btn {
  background: none;
  border: none;
  color: var(--primary);
  font-size: 12.5px;
  font-weight: 600;
  cursor: pointer;
  padding: 0;
}

@media (max-width: 640px) {
  .app-header { padding: 12px 16px; flex-wrap: wrap; gap: 10px; }
  .form-row { grid-template-columns: 1fr; }
  .chat-shell, .admin-shell { padding-left: 12px; padding-right: 12px; }
}
