/* ===== Variables & Reset ===== */
:root {
  --bg: #FAF7F2;
  --bg-card: #FFFFFF;
  --bg-chat-ai: #F5F0E8;
  --bg-chat-user: #3D3329;
  --text: #2C2418;
  --text-secondary: #8A7E72;
  --text-light: #B5AA9E;
  --accent: #C8623A;
  --accent-hover: #B5522D;
  --accent-light: #F5E6DE;
  --sage: #5B7B6F;
  --sage-light: #E3EDE8;
  --border: #E8E2DA;
  --shadow: 0 2px 12px rgba(44,36,24,0.06);
  --shadow-hover: 0 6px 24px rgba(44,36,24,0.1);
  --radius: 12px;
  --font-serif: 'Noto Serif SC', serif;
  --font-sans: 'Noto Sans SC', sans-serif;
}

* { margin: 0; padding: 0; box-sizing: border-box; }
html { scroll-behavior: smooth; }
body {
  font-family: var(--font-sans);
  background: var(--bg);
  color: var(--text);
  line-height: 1.7;
  min-height: 100vh;
}

/* ===== Animations ===== */
@keyframes fadeUp {
  from { opacity: 0; transform: translateY(20px); }
  to { opacity: 1; transform: translateY(0); }
}
@keyframes fadeIn {
  from { opacity: 0; }
  to { opacity: 1; }
}
@keyframes pulse {
  0%, 100% { opacity: 0.4; }
  50% { opacity: 1; }
}
@keyframes slideIn {
  from { opacity: 0; transform: translateX(-10px); }
  to { opacity: 1; transform: translateX(0); }
}
.fade-up { animation: fadeUp 0.5s ease-out forwards; opacity: 0; }
.fade-in { animation: fadeIn 0.3s ease-out forwards; }

/* ===== Layout ===== */
.container { max-width: 1100px; margin: 0 auto; padding: 0 24px; }

/* ===== Header ===== */
.header {
  padding: 32px 0 24px;
  border-bottom: 1px solid var(--border);
  background: var(--bg);
  position: sticky;
  top: 0;
  z-index: 100;
  backdrop-filter: blur(12px);
}
.header-inner {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 24px;
}
.logo {
  font-family: var(--font-serif);
  font-size: 22px;
  font-weight: 700;
  color: var(--text);
  cursor: pointer;
  display: flex;
  align-items: center;
  gap: 10px;
  white-space: nowrap;
}
.logo span { font-size: 28px; }
.logo small {
  font-size: 12px;
  font-weight: 400;
  color: var(--text-secondary);
  display: block;
  margin-top: 2px;
  letter-spacing: 0.5px;
}

/* ===== Search ===== */
.search-box { flex: 1; max-width: 480px; position: relative; }
.search-box input {
  width: 100%;
  padding: 12px 20px 12px 44px;
  border: 1.5px solid var(--border);
  border-radius: 40px;
  background: var(--bg-card);
  font-size: 15px;
  font-family: var(--font-sans);
  color: var(--text);
  outline: none;
  transition: all 0.25s;
}
.search-box input:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-light);
}
.search-box input::placeholder { color: var(--text-light); }
.search-icon {
  position: absolute;
  left: 16px;
  top: 50%;
  transform: translateY(-50%);
  color: var(--text-light);
  font-size: 18px;
  pointer-events: none;
}

/* ===== Categories ===== */
.categories {
  display: flex;
  gap: 8px;
  padding: 20px 0;
  overflow-x: auto;
  -ms-overflow-style: none;
  scrollbar-width: none;
}
.categories::-webkit-scrollbar { display: none; }
.cat-pill {
  padding: 8px 18px;
  border-radius: 24px;
  border: 1.5px solid var(--border);
  background: var(--bg-card);
  font-size: 14px;
  cursor: pointer;
  white-space: nowrap;
  transition: all 0.2s;
  color: var(--text-secondary);
  font-weight: 500;
}
.cat-pill:hover { border-color: var(--accent); color: var(--accent); }
.cat-pill.active { background: var(--accent); color: white; border-color: var(--accent); }

/* ===== Book Grid ===== */
.book-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(300px, 1fr));
  gap: 20px;
  padding: 8px 0 60px;
}
.book-card {
  background: var(--bg-card);
  border-radius: var(--radius);
  border: 1.5px solid var(--border);
  padding: 24px;
  cursor: pointer;
  transition: all 0.3s;
  position: relative;
  overflow: hidden;
}
.book-card:hover {
  transform: translateY(-3px);
  box-shadow: var(--shadow-hover);
  border-color: var(--accent);
}
.book-card .emoji { font-size: 40px; margin-bottom: 14px; display: block; }
.book-card h3 { font-family: var(--font-serif); font-size: 19px; font-weight: 700; margin-bottom: 4px; }
.book-card .author { font-size: 13px; color: var(--text-secondary); margin-bottom: 10px; }
.book-card .desc {
  font-size: 14px;
  color: var(--text-secondary);
  line-height: 1.6;
  margin-bottom: 14px;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
}
.book-tags { display: flex; flex-wrap: wrap; gap: 6px; }
.book-tag {
  font-size: 12px;
  padding: 3px 10px;
  border-radius: 12px;
  background: var(--sage-light);
  color: var(--sage);
  font-weight: 500;
}
.book-card .badge {
  position: absolute;
  top: 16px;
  right: 16px;
  font-size: 11px;
  padding: 4px 10px;
  border-radius: 8px;
  background: var(--accent);
  color: white;
  font-weight: 700;
}
.book-card .meta { font-size: 12px; color: var(--text-light); margin-top: 12px; display: flex; gap: 14px; }

/* ===== Detail Page ===== */
.detail-header { padding: 28px 0 20px; display: flex; align-items: flex-start; gap: 20px; }
.back-btn {
  font-size: 14px;
  color: var(--text-secondary);
  cursor: pointer;
  padding: 8px 16px;
  border-radius: 8px;
  border: 1.5px solid var(--border);
  background: var(--bg-card);
  transition: all 0.2s;
  white-space: nowrap;
  flex-shrink: 0;
}
.back-btn:hover { border-color: var(--accent); color: var(--accent); }
.detail-info { flex: 1; }
.detail-info .emoji { font-size: 36px; }
.detail-info h1 { font-family: var(--font-serif); font-size: 26px; font-weight: 700; margin: 6px 0 4px; }
.detail-info .author { color: var(--text-secondary); font-size: 15px; }

/* ===== Tabs ===== */
.tabs { display: flex; gap: 4px; border-bottom: 2px solid var(--border); margin-bottom: 0; }
.tab {
  padding: 12px 24px;
  font-size: 15px;
  font-weight: 500;
  cursor: pointer;
  border-bottom: 2.5px solid transparent;
  margin-bottom: -2px;
  color: var(--text-secondary);
  transition: all 0.2s;
}
.tab:hover { color: var(--text); }
.tab.active { color: var(--accent); border-bottom-color: var(--accent); }

/* ===== Chat ===== */
.chat-container { display: flex; flex-direction: column; height: calc(100vh - 260px); min-height: 500px; }
.chat-messages {
  flex: 1;
  overflow-y: auto;
  padding: 24px 0;
  display: flex;
  flex-direction: column;
  gap: 16px;
}
.chat-messages::-webkit-scrollbar { width: 4px; }
.chat-messages::-webkit-scrollbar-thumb { background: var(--border); border-radius: 4px; }
.msg { max-width: 85%; animation: slideIn 0.3s ease-out; }
.msg.ai { align-self: flex-start; }
.msg.user { align-self: flex-end; }
.msg-bubble { padding: 16px 20px; border-radius: 16px; font-size: 15px; line-height: 1.75; }
.msg.ai .msg-bubble { background: var(--bg-chat-ai); border-bottom-left-radius: 4px; }
.msg.user .msg-bubble { background: var(--bg-chat-user); color: #F5F0E8; border-bottom-right-radius: 4px; }
.msg-bubble p { margin-bottom: 10px; }
.msg-bubble p:last-child { margin-bottom: 0; }
.msg-bubble strong { color: var(--accent); font-weight: 600; }
.msg.user .msg-bubble strong { color: #F0C8A8; }

/* ===== Sandbox Modes ===== */
.sandbox-modes { display: flex; gap: 8px; padding: 16px 0 8px; flex-wrap: wrap; }
.mode-btn {
  padding: 8px 14px;
  border-radius: 10px;
  border: 1.5px solid var(--border);
  background: var(--bg-card);
  color: var(--text-secondary);
  font-size: 13px;
  cursor: pointer;
  transition: all 0.2s;
  font-family: var(--font-sans);
  font-weight: 500;
}
.mode-btn:hover { border-color: var(--accent); color: var(--accent); }
.mode-btn.active { background: var(--accent); color: white; border-color: var(--accent); }
.mode-btn span { margin-right: 4px; }

/* ===== Options ===== */
.options { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 12px; }
.option-btn {
  padding: 10px 18px;
  border-radius: 10px;
  border: 1.5px solid var(--accent);
  background: white;
  color: var(--accent);
  font-size: 14px;
  cursor: pointer;
  transition: all 0.2s;
  font-family: var(--font-sans);
  font-weight: 500;
  text-align: left;
  line-height: 1.5;
}
.option-btn:hover { background: var(--accent); color: white; }
.option-btn.selected { background: var(--accent); color: white; opacity: 0.7; cursor: default; }

/* ===== Chat Input ===== */
.chat-input-area { padding: 16px 0; border-top: 1px solid var(--border); display: flex; gap: 10px; align-items: center; }
.new-session-btn {
  width: 40px; height: 40px;
  border-radius: 10px;
  border: 1.5px solid var(--border);
  background: var(--bg-card);
  color: var(--text-secondary);
  font-size: 20px;
  cursor: pointer;
  transition: all 0.2s;
  flex-shrink: 0;
  display: flex; align-items: center; justify-content: center;
}
.new-session-btn:hover { border-color: var(--accent); color: var(--accent); }
.chat-input-area input {
  flex: 1;
  padding: 14px 20px;
  border: 1.5px solid var(--border);
  border-radius: 12px;
  font-size: 15px;
  font-family: var(--font-sans);
  outline: none;
  background: var(--bg-card);
  transition: border-color 0.2s;
}
.chat-input-area input:focus { border-color: var(--accent); }
.send-btn {
  padding: 14px 24px;
  border-radius: 12px;
  border: none;
  background: var(--accent);
  color: white;
  font-size: 15px;
  cursor: pointer;
  font-family: var(--font-sans);
  font-weight: 600;
  transition: background 0.2s;
  white-space: nowrap;
}
.send-btn:hover { background: var(--accent-hover); }
.send-btn:disabled { opacity: 0.5; cursor: not-allowed; }

/* ===== Typing indicator ===== */
.typing { display: flex; gap: 4px; padding: 8px 0; }
.typing span {
  width: 7px; height: 7px;
  border-radius: 50%;
  background: var(--text-light);
  animation: pulse 1.2s ease-in-out infinite;
}
.typing span:nth-child(2) { animation-delay: 0.2s; }
.typing span:nth-child(3) { animation-delay: 0.4s; }

/* ===== Content Layout with TOC ===== */
.content-layout { display: flex; gap: 24px; padding: 24px 0 60px; position: relative; }

/* Sidebar TOC */
.content-toc {
  position: sticky;
  top: 16px;
  align-self: flex-start;
  width: 200px;
  min-width: 200px;
  max-height: calc(100vh - 180px);
  overflow-y: auto;
  padding: 16px;
  background: var(--warm-bg);
  border-radius: 12px;
  border: 1px solid var(--border);
}
.content-toc::-webkit-scrollbar { width: 3px; }
.content-toc::-webkit-scrollbar-thumb { background: var(--border); border-radius: 3px; }
.toc-title {
  font-family: var(--font-serif);
  font-size: 14px;
  font-weight: 700;
  color: var(--text-secondary);
  margin-bottom: 12px;
  padding-bottom: 8px;
  border-bottom: 1px solid var(--border);
}
.toc-list { list-style: none; padding: 0; margin: 0; }
.toc-item { margin-bottom: 2px; }
.toc-item a {
  display: block;
  padding: 5px 8px;
  font-size: 13px;
  line-height: 1.4;
  color: var(--text-light);
  text-decoration: none;
  border-radius: 6px;
  transition: all 0.15s;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.toc-item a:hover { color: var(--text); background: rgba(0,0,0,0.04); }
.toc-item.active a {
  color: var(--accent);
  background: var(--accent-light);
  font-weight: 600;
}
.toc-item.toc-sub a { padding-left: 20px; font-size: 12px; }

/* Hide TOC on small screens */
@media (max-width: 768px) {
  .content-layout { flex-direction: column; }
  .content-toc {
    position: relative;
    top: 0;
    width: 100%;
    min-width: unset;
    max-height: 200px;
  }
}

/* ===== Content View ===== */
.content-view { padding: 8px 0 60px; max-width: 720px; flex: 1; min-width: 0; }
.content-view h2 {
  font-family: var(--font-serif);
  font-size: 22px;
  font-weight: 700;
  margin: 40px 0 16px;
  color: var(--text);
  padding-bottom: 8px;
  border-bottom: 2px solid var(--accent-light);
  scroll-margin-top: 20px;
}
.content-view h2:first-child { margin-top: 0; }
.content-view h3 {
  font-family: var(--font-serif);
  font-size: 18px;
  font-weight: 600;
  margin: 28px 0 12px;
  color: var(--text-secondary);
  scroll-margin-top: 20px;
}
.content-view p { margin-bottom: 16px; font-size: 15.5px; line-height: 1.85; color: #3D3329; }
.content-view blockquote {
  border-left: 3px solid var(--accent);
  padding: 12px 20px;
  margin: 20px 0;
  background: var(--accent-light);
  border-radius: 0 8px 8px 0;
  font-size: 14.5px;
  color: var(--text-secondary);
}
.content-view strong { color: var(--accent); }
.content-view hr { border: none; border-top: 1px solid var(--border); margin: 32px 0; }

/* ===== Coming Soon ===== */
.coming-soon { text-align: center; padding: 80px 20px; }
.coming-soon .emoji { font-size: 64px; margin-bottom: 20px; }
.coming-soon h2 { font-family: var(--font-serif); font-size: 24px; margin-bottom: 12px; }
.coming-soon p { color: var(--text-secondary); font-size: 15px; max-width: 400px; margin: 0 auto; }

/* ===== Empty State ===== */
.empty-state { text-align: center; padding: 60px 20px; color: var(--text-light); }

/* ===== Admin Panel ===== */
.admin-panel { padding-top: 24px; }
.admin-header h1 { font-family: var(--font-serif); font-size: 26px; margin-bottom: 4px; }
.admin-subtitle { color: var(--text-secondary); font-size: 15px; margin-bottom: 24px; }
.admin-actions { margin-bottom: 20px; }
.admin-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 14px;
}
.admin-table th {
  text-align: left;
  padding: 12px 16px;
  border-bottom: 2px solid var(--border);
  color: var(--text-secondary);
  font-weight: 600;
  font-size: 13px;
}
.admin-table td {
  padding: 12px 16px;
  border-bottom: 1px solid var(--border);
}
.admin-table tr:hover td { background: var(--accent-light); }
.status-badge {
  display: inline-block;
  padding: 3px 10px;
  border-radius: 8px;
  font-size: 12px;
  font-weight: 600;
}
.status-none { background: var(--border); color: var(--text-secondary); }
.status-running { background: #FEF3C7; color: #92400E; }
.status-queued { background: #FEF3C7; color: #92400E; }
.status-completed { background: var(--sage-light); color: var(--sage); }
.status-failed { background: #FEE2E2; color: #991B1B; }
.status-cancelled { background: var(--border); color: var(--text-secondary); }
.admin-action-btn {
  padding: 6px 14px;
  border-radius: 8px;
  border: 1.5px solid var(--accent);
  background: white;
  color: var(--accent);
  font-size: 13px;
  cursor: pointer;
  font-family: var(--font-sans);
  font-weight: 500;
  transition: all 0.2s;
}
.admin-action-btn:hover { background: var(--accent); color: white; }
.admin-cancel-btn { border-color: #991B1B; color: #991B1B; }
.admin-cancel-btn:hover { background: #991B1B; color: white; }
.job-list { display: flex; flex-direction: column; gap: 8px; margin-top: 12px; }
.job-card {
  padding: 12px 16px;
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  font-size: 13px;
}
.job-info { display: flex; gap: 16px; align-items: center; }
.job-error { margin-top: 8px; color: #991B1B; font-size: 12px; }

/* ===== Auth Modal ===== */
.modal-overlay {
  position: fixed; top: 0; left: 0; right: 0; bottom: 0;
  background: rgba(44,36,24,0.5);
  display: flex; align-items: center; justify-content: center;
  z-index: 1000;
  animation: fadeIn 0.2s ease-out;
}
.modal-card {
  background: var(--bg-card);
  border-radius: 16px;
  width: 400px; max-width: 90vw;
  box-shadow: 0 20px 60px rgba(44,36,24,0.2);
  overflow: hidden;
}
.modal-header {
  padding: 28px 28px 0;
}
.modal-header h2 {
  font-family: var(--font-serif);
  font-size: 22px; font-weight: 700;
  display: flex; justify-content: space-between; align-items: center;
}
.modal-close {
  font-size: 24px; cursor: pointer; color: var(--text-light);
  width: 32px; height: 32px; display: flex; align-items: center; justify-content: center;
  border-radius: 8px; transition: all 0.2s;
}
.modal-close:hover { background: var(--border); color: var(--text); }
.modal-subtitle { color: var(--text-secondary); font-size: 14px; margin-top: 4px; }
.modal-body {
  padding: 20px 28px 28px;
}
.modal-body label {
  display: block; font-size: 13px; font-weight: 600;
  color: var(--text-secondary); margin-bottom: 6px; margin-top: 16px;
}
.modal-body label:first-child { margin-top: 0; }
.modal-body input, .modal-body textarea {
  width: 100%; padding: 12px 16px;
  border: 1.5px solid var(--border); border-radius: 10px;
  font-size: 15px; font-family: var(--font-sans);
  outline: none; background: var(--bg);
  transition: border-color 0.2s;
}
.modal-body input:focus, .modal-body textarea:focus {
  border-color: var(--accent);
}
.modal-body textarea { resize: vertical; line-height: 1.6; }
.auth-btn {
  width: 100%; padding: 14px;
  border-radius: 12px; border: none;
  background: var(--accent); color: white;
  font-size: 16px; font-weight: 600;
  cursor: pointer; font-family: var(--font-sans);
  margin-top: 20px; transition: background 0.2s;
}
.auth-btn:hover { background: var(--accent-hover); }
.auth-btn-secondary {
  padding: 12px 16px;
  border-radius: 10px; border: 1.5px solid var(--border);
  background: var(--bg-card); color: var(--text-secondary);
  font-size: 13px; cursor: pointer; font-family: var(--font-sans);
  white-space: nowrap; transition: all 0.2s;
}
.auth-btn-secondary:hover { border-color: var(--accent); color: var(--accent); }
.code-row { display: flex; gap: 10px; }
.code-row input { flex: 1; }
.auth-hint { font-size: 12px; color: var(--sage); margin-top: 8px; }
.auth-error {
  background: #FEE2E2; color: #991B1B;
  padding: 10px 14px; border-radius: 8px;
  font-size: 13px; margin-bottom: 12px;
}
.auth-skip {
  text-align: center; margin-top: 14px;
  color: var(--text-light); font-size: 14px;
  cursor: pointer; transition: color 0.2s;
}
.auth-skip:hover { color: var(--text-secondary); }

/* ===== User Menu ===== */
.user-menu { position: relative; }
.user-avatar {
  width: 38px; height: 38px;
  border-radius: 50%;
  background: var(--accent);
  color: white;
  display: flex; align-items: center; justify-content: center;
  font-size: 14px; font-weight: 700;
  cursor: pointer;
  transition: opacity 0.2s;
}
.user-avatar:hover { opacity: 0.85; }
.user-dropdown {
  position: absolute; top: 48px; right: 0;
  background: var(--bg-card);
  border: 1.5px solid var(--border);
  border-radius: 12px;
  box-shadow: var(--shadow-hover);
  min-width: 160px;
  z-index: 200;
  overflow: hidden;
}
.dropdown-name {
  padding: 14px 16px 4px;
  font-weight: 600; font-size: 14px;
}
.dropdown-info {
  padding: 0 16px 10px;
  font-size: 12px; color: var(--text-light);
  border-bottom: 1px solid var(--border);
}
.dropdown-item {
  padding: 12px 16px;
  font-size: 14px; cursor: pointer;
  transition: background 0.15s;
}
.dropdown-item:hover { background: var(--accent-light); }
.login-btn {
  padding: 8px 20px;
  border-radius: 20px;
  border: 1.5px solid var(--accent);
  background: transparent;
  color: var(--accent);
  font-size: 14px; font-weight: 600;
  cursor: pointer; font-family: var(--font-sans);
  transition: all 0.2s;
  white-space: nowrap;
}
.login-btn:hover { background: var(--accent); color: white; }

/* ===== Responsive ===== */
@media (max-width: 768px) {
  .header-inner { flex-direction: column; align-items: stretch; gap: 14px; }
  .search-box { max-width: 100%; }
  .book-grid { grid-template-columns: 1fr; }
  .detail-header { flex-direction: column; gap: 12px; }
  .chat-container { height: calc(100vh - 300px); }
  .msg { max-width: 92%; }
}
