/* ===== Reset & Base ===== */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

:root {
  --primary: #1a5276;
  --primary-light: #2e86c1;
  --accent: #d4ac0d;
  --bg: #f4f6f8;
  --card-bg: #ffffff;
  --text: #2c3e50;
  --text-muted: #7f8c8d;
  --border: #dce1e7;
  --radius: 8px;
  --shadow: 0 2px 8px rgba(0,0,0,0.10);
  --shadow-hover: 0 4px 16px rgba(0,0,0,0.16);
  --header-h: 56px;
}

html { font-size: 16px; scroll-behavior: smooth; overflow-x: hidden; }

body {
  font-family: -apple-system, BlinkMacSystemFont, 'Noto Sans TC', 'Microsoft JhengHei', sans-serif;
  background: var(--bg);
  color: var(--text);
  line-height: 1.6;
  min-height: 100vh;
  overflow-x: hidden;
  max-width: 100vw;
}

a { color: var(--primary-light); text-decoration: none; }
a:hover { text-decoration: underline; }
img { max-width: 100%; display: block; }

/* ===== Header ===== */
.site-header {
  position: sticky;
  top: 0;
  z-index: 100;
  background: var(--primary);
  color: #fff;
  height: var(--header-h);
  display: flex;
  align-items: center;
  padding: 0 1rem;
  gap: 0.75rem;
  box-shadow: 0 2px 6px rgba(0,0,0,0.2);
}

.site-header .logo {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  color: #fff;
  font-weight: 700;
  font-size: 1rem;
  white-space: nowrap;
  text-decoration: none;
}

.site-header .logo svg { flex-shrink: 0; }

/* Inline search in header (results/detail page) */
.header-search {
  flex: 1;
  display: flex;
  gap: 0.5rem;
  max-width: 600px;
  min-width: 0;
}

.header-search input {
  flex: 1;
  min-width: 0;
  padding: 0.4rem 0.75rem;
  border: none;
  border-radius: var(--radius);
  font-size: 0.95rem;
  outline: none;
}

.header-search button {
  padding: 0.4rem 0.85rem;
  background: var(--accent);
  color: #fff;
  border: none;
  border-radius: var(--radius);
  cursor: pointer;
  font-weight: 600;
  font-size: 0.9rem;
  white-space: nowrap;
}

.header-search select {
  padding: 0.4rem 0.5rem;
  border: none;
  border-radius: var(--radius);
  font-size: 0.9rem;
  font-family: inherit;
  background: rgba(255,255,255,0.15);
  color: #fff;
  flex-shrink: 0;
}

/* ===== Hero / Search Section (index) ===== */
.hero {
  background: linear-gradient(135deg, var(--primary) 0%, var(--primary-light) 100%);
  color: #fff;
  padding: 2.5rem 1rem 3rem;
  text-align: center;
}

.hero h1 { font-size: 1.5rem; margin-bottom: 0.4rem; }
.hero p { font-size: 0.95rem; opacity: 0.85; margin-bottom: 1.75rem; }

.search-box {
  background: #fff;
  border-radius: 12px;
  padding: 1.25rem;
  max-width: 640px;
  margin: 0 auto;
  box-shadow: var(--shadow);
}

.search-row {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
  margin-bottom: 0.75rem;
}

.search-row select {
  flex: 0 0 auto;
  max-width: 110px;
}

.search-row input[type="text"] {
  flex: 1 1 0;
  min-width: 0;
}

.btn-search {
  flex-shrink: 0;
}

.search-row select {
  padding: 0.6rem 0.5rem;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  font-size: 1rem;
  font-family: inherit;
  background: var(--bg);
  color: var(--text);
  flex-shrink: 0;
}

.search-row input[type="text"] {
  flex: 1;
  padding: 0.6rem 0.85rem;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  font-size: 1rem;
  color: var(--text);
  outline: none;
  transition: border-color 0.2s;
}

.search-row input[type="text"]:focus { border-color: var(--primary-light); }

.btn-search {
  padding: 0.65rem 1.25rem;
  background: var(--primary);
  color: #fff;
  border: none;
  border-radius: var(--radius);
  font-size: 1rem;
  font-weight: 600;
  cursor: pointer;
  white-space: nowrap;
  transition: background 0.2s;
}

.btn-search:hover { background: var(--primary-light); }

.search-hint { font-size: 0.8rem; color: var(--text-muted); text-align: left; }

/* ===== Carousel ===== */
.carousel-section {
  padding: 1rem 1rem 0.5rem;
  max-width: 960px;
  margin: 0 auto;
}

.carousel-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 0.75rem;
  gap: 0.5rem;
}

.carousel-tabs {
  display: flex;
  gap: 0.35rem;
  flex-wrap: wrap;
}

.ctab {
  padding: 0.3rem 0.75rem;
  border: 1px solid var(--border);
  border-radius: 20px;
  background: var(--card-bg);
  color: var(--text);
  cursor: pointer;
  font-size: 0.82rem;
  transition: background 0.2s, color 0.2s;
}

.ctab.active { background: var(--primary); color: #fff; border-color: var(--primary); }

.carousel-nav { display: flex; gap: 0.25rem; flex-shrink: 0; }

.cnav {
  width: 32px; height: 32px;
  border: 1px solid var(--border);
  border-radius: 50%;
  background: var(--card-bg);
  cursor: pointer;
  font-size: 1.1rem;
  display: flex; align-items: center; justify-content: center;
  transition: background 0.2s;
}
.cnav:hover { background: var(--primary); color: #fff; border-color: var(--primary); }

.carousel-track-wrap {
  overflow: hidden;
  width: 100%;
}

.carousel-track {
  display: flex;
  gap: 0.75rem;
  transition: transform 0.4s ease;
  align-items: flex-start;
}

.carousel-loading {
  padding: 1.5rem;
  display: flex;
  justify-content: center;
  width: 100%;
}

.cbook {
  flex-shrink: 0;
  width: 110px;
  text-decoration: none;
  color: var(--text);
  display: flex;
  flex-direction: column;
  gap: 0.4rem;
}

.cbook:hover .cbook-title { color: var(--primary-light); }

.cbook-cover {
  width: 110px;
  height: 145px;
  background: var(--bg);
  border-radius: 4px;
  overflow: hidden;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 2rem;
  box-shadow: var(--shadow);
}

.cbook-cover img {
  width: 100%; height: 100%;
  object-fit: cover;
}

.cbook-title {
  font-size: 0.75rem;
  line-height: 1.3;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
  transition: color 0.2s;
}

/* ===== Categories ===== */
.categories {
  padding: 1.5rem 1rem;
  max-width: 800px;
  margin: 0 auto;
}

.categories h2 {
  font-size: 1.05rem;
  color: var(--text-muted);
  margin-bottom: 1rem;
  text-transform: uppercase;
  letter-spacing: 0.04em;
}

.category-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 0.75rem;
}

.category-card {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 0.4rem;
  padding: 1rem 0.5rem;
  background: var(--card-bg);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  cursor: pointer;
  border: 2px solid transparent;
  transition: border-color 0.2s, box-shadow 0.2s;
  text-decoration: none;
  color: var(--text);
}

.category-card:hover {
  border-color: var(--primary-light);
  box-shadow: var(--shadow-hover);
  text-decoration: none;
}

.category-card .icon { font-size: 1.75rem; }
.category-card span { font-size: 0.82rem; text-align: center; font-weight: 500; }

/* ===== Main Container ===== */
.container {
  max-width: 960px;
  margin: 0 auto;
  padding: 1rem;
}

/* ===== Results Page ===== */
.results-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 1rem;
  flex-wrap: wrap;
  gap: 0.5rem;
}

.results-count { font-size: 0.9rem; color: var(--text-muted); }

.book-grid {
  display: grid;
  grid-template-columns: 1fr;
  gap: 1rem;
}

/* ===== Book Card ===== */
.book-card {
  background: var(--card-bg);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  display: flex;
  gap: 0.85rem;
  padding: 0.85rem;
  transition: box-shadow 0.2s;
  cursor: pointer;
  text-decoration: none;
  color: var(--text);
}

.book-card:hover { box-shadow: var(--shadow-hover); text-decoration: none; }

.book-cover {
  width: 72px;
  flex-shrink: 0;
  aspect-ratio: 2/3;
  background: var(--bg);
  border-radius: 4px;
  overflow: hidden;
  display: flex;
  align-items: center;
  justify-content: center;
}

.book-cover img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.book-cover-placeholder {
  width: 100%;
  height: 100%;
  background: linear-gradient(135deg, #dce1e7, #b2bec3);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 1.5rem;
}

.book-info { flex: 1; min-width: 0; }

.book-title {
  font-size: 0.97rem;
  font-weight: 600;
  color: var(--primary);
  margin-bottom: 0.25rem;
  word-break: break-all;
  overflow-wrap: break-word;
  overflow: hidden;
  display: -webkit-box;
  -webkit-line-clamp: 3;
  -webkit-box-orient: vertical;
}

.book-meta {
  font-size: 0.8rem;
  color: var(--text-muted);
  display: flex;
  flex-wrap: wrap;
  gap: 0.25rem 0.75rem;
}

.book-meta span::before { content: attr(data-label) '：'; font-weight: 500; color: var(--text); }

/* ===== Pagination ===== */
.pagination {
  display: flex;
  justify-content: center;
  align-items: center;
  gap: 0.5rem;
  margin-top: 1.5rem;
  flex-wrap: wrap;
}

.btn-page {
  padding: 0.45rem 0.9rem;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--card-bg);
  color: var(--text);
  cursor: pointer;
  font-size: 0.9rem;
  transition: background 0.2s, color 0.2s;
}

.btn-page:hover, .btn-page.active {
  background: var(--primary);
  color: #fff;
  border-color: var(--primary);
}

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

/* ===== Detail Page ===== */
.detail-wrapper {
  display: flex;
  flex-direction: column;
  gap: 1.25rem;
}

.detail-cover-wrap {
  display: flex;
  gap: 1rem;
  align-items: flex-start;
}

.detail-cover {
  width: 120px;
  flex-shrink: 0;
  border-radius: var(--radius);
  overflow: hidden;
  box-shadow: var(--shadow);
}

.detail-cover img { width: 100%; }

.detail-cover-placeholder {
  width: 120px;
  aspect-ratio: 2/3;
  background: linear-gradient(135deg, #dce1e7, #b2bec3);
  border-radius: var(--radius);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 3rem;
}

.detail-title { font-size: 1.2rem; font-weight: 700; color: var(--primary); margin-bottom: 0.5rem; }

.detail-card {
  background: var(--card-bg);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  overflow: hidden;
}

.detail-card h2 {
  font-size: 0.9rem;
  font-weight: 600;
  color: var(--primary);
  padding: 0.6rem 1rem;
  background: var(--bg);
  border-bottom: 1px solid var(--border);
  text-transform: uppercase;
  letter-spacing: 0.04em;
}

.detail-table { width: 100%; border-collapse: collapse; }
.detail-table tr { border-bottom: 1px solid var(--border); }
.detail-table tr:last-child { border-bottom: none; }
.detail-table th {
  width: 30%;
  padding: 0.55rem 1rem;
  text-align: left;
  font-weight: 500;
  font-size: 0.85rem;
  color: var(--text-muted);
  background: var(--bg);
  white-space: nowrap;
}
.detail-table td { padding: 0.55rem 1rem; font-size: 0.9rem; }

/* Holdings */
.holding-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0.6rem 1rem;
  border-bottom: 1px solid var(--border);
  font-size: 0.88rem;
  flex-wrap: wrap;
  gap: 0.25rem;
}

.holding-row:last-child { border-bottom: none; }

.holding-status {
  display: inline-block;
  padding: 0.15rem 0.5rem;
  border-radius: 20px;
  font-size: 0.78rem;
  font-weight: 600;
}

.status-available { background: #d5f5e3; color: #1e8449; }
.status-borrowed { background: #fde8d8; color: #ca6f1e; }

/* ===== Back Button ===== */
.btn-back {
  display: inline-flex;
  align-items: center;
  gap: 0.4rem;
  padding: 0.5rem 1rem;
  background: var(--card-bg);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  color: var(--text);
  font-size: 0.9rem;
  cursor: pointer;
  margin-bottom: 1rem;
  text-decoration: none;
}

.btn-back:hover { background: var(--bg); text-decoration: none; }

/* ===== Loading & Error ===== */
.loading {
  text-align: center;
  padding: 3rem 1rem;
  color: var(--text-muted);
}

.spinner {
  width: 36px;
  height: 36px;
  border: 3px solid var(--border);
  border-top-color: var(--primary-light);
  border-radius: 50%;
  animation: spin 0.7s linear infinite;
  margin: 0 auto 0.75rem;
}

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

.error-box {
  background: #fdf2f8;
  border: 1px solid #f1948a;
  border-radius: var(--radius);
  padding: 1.25rem;
  text-align: center;
  color: #922b21;
}

/* ===== Footer ===== */
.site-footer {
  text-align: center;
  padding: 1.5rem 1rem;
  font-size: 0.8rem;
  color: var(--text-muted);
  border-top: 1px solid var(--border);
  margin-top: 2rem;
}

/* ===== 類型分頁標籤 ===== */
.type-tabs {
  display: flex;
  gap: 0.5rem;
  flex-wrap: wrap;
  margin-bottom: 1rem;
}

.type-tab {
  padding: 0.4rem 0.85rem;
  border: 1px solid var(--border);
  border-radius: 20px;
  background: var(--card-bg);
  color: var(--text);
  cursor: pointer;
  font-size: 0.85rem;
  transition: background 0.2s, color 0.2s, border-color 0.2s;
}

.type-tab:hover { border-color: var(--primary-light); color: var(--primary-light); }
.type-tab.active { background: var(--primary); color: #fff; border-color: var(--primary); }

.tab-count {
  display: inline-block;
  background: rgba(0,0,0,0.12);
  border-radius: 10px;
  padding: 0 0.4rem;
  font-size: 0.75rem;
  margin-left: 0.2rem;
}

/* ===== 電子書卡片 ===== */
.ebook-card { border-left: 3px solid var(--primary-light); }

.ebook-badge {
  display: inline-block;
  background: var(--primary-light);
  color: #fff;
  font-size: 0.7rem;
  padding: 0.1rem 0.4rem;
  border-radius: 4px;
  margin-bottom: 0.3rem;
}

.ebook-link-hint {
  display: block;
  font-size: 0.78rem;
  color: var(--primary-light);
  margin-top: 0.4rem;
}

/* ===== Header 個人書房連結 ===== */
.header-user {
  margin-left: auto;
  display: inline-flex;
  align-items: center;
  gap: 0.35rem;
  color: #fff;
  font-size: 0.88rem;
  padding: 0.35rem 0.65rem;
  border-radius: var(--radius);
  text-decoration: none;
  white-space: nowrap;
  transition: background 0.2s;
}
.header-user:hover { background: rgba(255,255,255,0.15); text-decoration: none; }
.header-user.active { background: rgba(255,255,255,0.18); }

/* ===== 個人書房 ===== */
.account-container { max-width: 800px; }

.login-card {
  background: var(--card-bg);
  border-radius: 12px;
  box-shadow: var(--shadow);
  padding: 1.75rem 1.5rem;
  max-width: 420px;
  margin: 1.5rem auto 0;
}

.login-card h1 { font-size: 1.25rem; color: var(--primary); margin-bottom: 0.35rem; }
.login-hint { font-size: 0.85rem; color: var(--text-muted); margin-bottom: 1.25rem; }

.login-card label {
  display: block;
  font-size: 0.85rem;
  font-weight: 600;
  margin-bottom: 0.3rem;
}

.login-card input {
  width: 100%;
  padding: 0.6rem 0.85rem;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  font-size: 1rem;
  color: var(--text);
  outline: none;
  margin-bottom: 1rem;
  transition: border-color 0.2s;
}
.login-card input:focus { border-color: var(--primary-light); }

.btn-login { width: 100%; padding: 0.7rem; }
.btn-login:disabled { opacity: 0.6; cursor: not-allowed; }

.login-error {
  background: #fdf2f8;
  border: 1px solid #f1948a;
  border-radius: var(--radius);
  color: #922b21;
  font-size: 0.85rem;
  padding: 0.5rem 0.75rem;
  margin-bottom: 1rem;
}

.login-note { font-size: 0.8rem; color: var(--text-muted); margin-top: 1rem; text-align: center; }

.account-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.75rem;
  flex-wrap: wrap;
  margin-bottom: 1rem;
}
.account-header h1 { font-size: 1.25rem; color: var(--primary); }
.account-sub { font-size: 0.8rem; color: var(--text-muted); }

.account-card { margin-bottom: 1.25rem; }

/* 摘要卡片 */
.summary-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 0.75rem;
  margin-bottom: 1.25rem;
}

.summary-card {
  background: var(--card-bg);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 0.85rem 0.5rem;
  text-align: center;
}

.summary-num { font-size: 1.5rem; font-weight: 700; color: var(--primary); line-height: 1.2; }
.summary-num small { font-size: 0.8rem; font-weight: 400; }
.summary-label { font-size: 0.8rem; color: var(--text-muted); margin-top: 0.2rem; }
.summary-warn { color: #ca6f1e; font-weight: 600; }
.summary-ok { color: #1e8449; font-weight: 600; }
.account-empty { padding: 0.85rem 1rem; font-size: 0.88rem; color: var(--text-muted); }

/* 響應式資料表：桌機為一般表格，可橫向捲動 */
.table-scroll { overflow-x: auto; }

.data-table { width: 100%; border-collapse: collapse; font-size: 0.88rem; }
.data-table th {
  background: var(--bg);
  color: var(--text-muted);
  font-weight: 600;
  text-align: left;
  padding: 0.55rem 0.85rem;
  white-space: nowrap;
  border-bottom: 1px solid var(--border);
}
.data-table td {
  padding: 0.55rem 0.85rem;
  border-bottom: 1px solid var(--border);
  vertical-align: top;
}
.data-table tbody tr:last-child td { border-bottom: none; }

/* 手機版：每列轉成堆疊卡片，欄位名稱用 data-label 顯示 */
@media (max-width: 600px) {
  .data-table thead { display: none; }
  .data-table, .data-table tbody, .data-table tr, .data-table td { display: block; width: 100%; }
  .data-table tr { border-bottom: 4px solid var(--bg); padding: 0.35rem 0; }
  .data-table tr:last-child { border-bottom: none; }
  .data-table td {
    border: none;
    padding: 0.25rem 1rem;
    display: flex;
    gap: 0.5rem;
  }
  .data-table td[data-label]::before {
    content: attr(data-label);
    flex: 0 0 30%;
    max-width: 30%;
    font-weight: 600;
    color: var(--text-muted);
  }
}

/* ===== 手機版：header select 隱藏，搜尋列更寬 ===== */
@media (max-width: 480px) {
  .header-search select { display: none; }
  .site-header .logo span { display: none; }
  .header-user span { display: none; }
}

/* ===== Tablet (≥600px) ===== */
@media (min-width: 600px) {
  .hero h1 { font-size: 1.85rem; }
  .category-grid { grid-template-columns: repeat(6, 1fr); }
  .book-grid { grid-template-columns: repeat(2, 1fr); }
  .book-cover { width: 80px; }
  .detail-cover { width: 150px; }
  .detail-cover-placeholder { width: 150px; }
  .detail-title { font-size: 1.4rem; }
}

/* ===== Desktop (≥960px) ===== */
@media (min-width: 960px) {
  .container { padding: 1.5rem; }
  .book-grid { grid-template-columns: repeat(3, 1fr); }
  .detail-wrapper { flex-direction: row; align-items: flex-start; }
  .detail-main { flex: 1; }
  .detail-cover-wrap { flex-direction: column; width: 180px; flex-shrink: 0; }
  .detail-cover { width: 180px; }
  .detail-cover-placeholder { width: 180px; }
}
