/* ── Telegram Web App Theme Integration ───────────────────── */
:root {
  --bg: var(--tg-theme-bg-color, #1a1a2e);
  --text: var(--tg-theme-text-color, #eee);
  --hint: var(--tg-theme-hint-color, #888);
  --link: var(--tg-theme-link-color, #6cacee);
  --btn: var(--tg-theme-button-color, #3b82f6);
  --btn-text: var(--tg-theme-button-text-color, #fff);
  --secondary: var(--tg-theme-secondary-bg-color, #16213e);
  --card-bg: var(--tg-theme-section-bg-color, #1e293b);
  --border: color-mix(in srgb, var(--text) 10%, transparent);
  --radius: 16px;
}

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

body {
  font-family: -apple-system, 'SF Pro Text', 'Helvetica Neue', sans-serif;
  background: var(--bg);
  color: var(--text);
  min-height: 100vh;
  -webkit-font-smoothing: antialiased;
}

#app {
  max-width: 480px;
  margin: 0 auto;
  padding: 0 12px;
}

/* ── Header ──────────────────────────────────────────────── */
#header {
  text-align: center;
  padding: 16px 0 12px;
}
#header h1 {
  font-size: 1.4em;
  font-weight: 700;
}

/* ── Tabs ────────────────────────────────────────────────── */
#tabs {
  display: flex;
  gap: 8px;
  margin-bottom: 16px;
  overflow-x: auto;
  scrollbar-width: none;
  padding: 0 2px;
}
#tabs::-webkit-scrollbar { display: none; }

.tab {
  flex: 1;
  min-width: 0;
  background: var(--secondary);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 10px 6px;
  color: var(--hint);
  font-size: 0;
  cursor: pointer;
  transition: all 0.2s;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 4px;
  position: relative;
}
.tab-icon { font-size: 18px; line-height: 1; }
.tab-label { font-size: 12px; font-weight: 500; }
.tab-badge {
  font-size: 10px;
  font-weight: 700;
  background: rgba(108,172,238,0.15);
  color: var(--link);
  padding: 1px 7px;
  border-radius: 10px;
  min-width: 20px;
  text-align: center;
  line-height: 1.4;
}
.tab.active {
  background: var(--btn);
  border-color: var(--btn);
  color: var(--btn-text);
}
.tab.active .tab-badge {
  background: color-mix(in srgb, var(--btn-text) 20%, transparent);
  color: var(--btn-text);
}
.tab.active .tab-icon { filter: none; }
.tab:not(.active) .tab-icon { opacity: 0.7; }

/* ── Tab Content ─────────────────────────────────────────── */
.tab-content { display: none; }
.tab-content.active { display: block; }

/* ── Cards ───────────────────────────────────────────────── */
.card {
  background: var(--card-bg);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 16px;
  margin-bottom: 10px;
  transition: transform 0.15s, opacity 0.15s;
  position: relative;
  overflow: hidden;
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
}
.card:active { transform: scale(0.97); opacity: 0.9; }

/* Chapter accent bar */
.card[data-type="chapter"]::before {
  content: '';
  position: absolute;
  left: 0; top: 0; bottom: 0;
  width: 3px;
  background: var(--btn);
  border-radius: 0 2px 2px 0;
}

/* Card header */
.card-header {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-bottom: 10px;
}
.card-number {
  background: var(--btn);
  color: var(--btn-text);
  font-size: 0.7em;
  font-weight: 700;
  padding: 3px 10px;
  border-radius: 12px;
  flex-shrink: 0;
  letter-spacing: 0.5px;
}
.card-title {
  font-size: 0.95em;
  font-weight: 600;
  line-height: 1.3;
  flex: 1;
  min-width: 0;
}

/* Card body sections */
.card-field {
  margin-bottom: 8px;
}
.card-field:last-child { margin-bottom: 0; }
.field-label {
  font-size: 0.7em;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.8px;
  color: var(--link);
  margin-bottom: 3px;
}
.field-value {
  font-size: 0.85em;
  color: var(--text);
  opacity: 0.8;
  line-height: 1.55;
}

/* Tags */
.card-tags {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  margin-top: 8px;
}
.tag {
  background: rgba(108,172,238,0.12);
  color: var(--link);
  font-size: 0.7em;
  padding: 3px 10px;
  border-radius: 12px;
  font-weight: 500;
}
.tag[data-type] {
  border: 1px solid rgba(108,172,238,0.2);
}

/* ── Character Cards ─────────────────────────────────────── */
.char-avatar {
  width: 36px;
  height: 36px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 1em;
  flex-shrink: 0;
}
.char-avatar[data-role="主角"] { background: rgba(239,68,68,0.2); }
.char-avatar[data-role="配角"] { background: rgba(59,130,246,0.2); }
.char-avatar[data-role="反派"] { background: rgba(168,85,247,0.2); }
.char-avatar:not([data-role]) { background: rgba(156,163,175,0.15); }

.role-badge {
  font-size: 0.65em;
  font-weight: 600;
  padding: 2px 8px;
  border-radius: 10px;
  letter-spacing: 0.3px;
}
.role-主角 { background: rgba(239,68,68,0.15); color: #f87171; }
.role-配角 { background: rgba(59,130,246,0.15); color: #60a5fa; }
.role-反派 { background: rgba(168,85,247,0.15); color: #c084fc; }
.role-默认 { background: rgba(156,163,175,0.15); color: #9ca3af; }

.char-status {
  font-size: 0.82em;
  color: var(--text);
  opacity: 0.75;
  background: color-mix(in srgb, var(--text) 3%, transparent);
  padding: 8px 12px;
  border-radius: 10px;
  line-height: 1.5;
  border-left: 2px solid rgba(108,172,238,0.3);
}

/* ── Location Cards ──────────────────────────────────────── */
.location-icon {
  width: 36px;
  height: 36px;
  border-radius: 12px;
  background: rgba(52,211,153,0.12);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 1em;
  flex-shrink: 0;
}

/* ── World Cards ─────────────────────────────────────────── */
.world-icon {
  width: 36px;
  height: 36px;
  border-radius: 12px;
  background: rgba(251,191,36,0.12);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 1em;
  flex-shrink: 0;
}

/* ── Loading ─────────────────────────────────────────────── */
.loading {
  text-align: center;
  padding: 48px 0;
  color: var(--hint);
}
.spinner {
  width: 28px;
  height: 28px;
  border: 2px solid var(--border);
  border-top-color: var(--btn);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
  margin: 0 auto 12px;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* ── Empty state ─────────────────────────────────────────── */
.empty {
  text-align: center;
  padding: 48px 0;
  color: var(--hint);
  font-size: 0.85em;
}
.empty-icon {
  font-size: 2em;
  margin-bottom: 8px;
  opacity: 0.5;
}

/* ── Detail View ─────────────────────────────────────────── */
.hidden { display: none !important; }

#detail-view {
  position: fixed;
  top: 0; left: 0; right: 0; bottom: 0;
  background: var(--bg);
  z-index: 100;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  animation: slideIn 0.25s ease-out;
}
@keyframes slideIn {
  from { transform: translateX(100%); opacity: 0; }
  to { transform: translateX(0); opacity: 1; }
}

#detail-header {
  position: sticky;
  top: 0;
  background: var(--bg);
  padding: 12px;
  display: flex;
  align-items: center;
  gap: 12px;
  border-bottom: 1px solid var(--border);
  z-index: 1;
}
.back-btn {
  background: var(--secondary);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 8px 14px;
  color: var(--link);
  font-size: 14px;
  cursor: pointer;
  font-weight: 600;
  flex-shrink: 0;
}
.back-btn:active { opacity: 0.7; }
.detail-title {
  font-size: 15px;
  font-weight: 600;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

#detail-body {
  padding: 16px 12px;
}

.detail-card {
  background: var(--card-bg);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 16px;
  margin-bottom: 12px;
}

.detail-section {
  margin-bottom: 16px;
}
.detail-section:last-child { margin-bottom: 0; }
.detail-section-title {
  font-size: 11px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 1px;
  color: var(--link);
  margin-bottom: 8px;
  padding-bottom: 6px;
  border-bottom: 1px solid var(--border);
}
.detail-section-body {
  font-size: 15px;
  line-height: 1.7;
  color: var(--text);
}

.detail-meta {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin-bottom: 12px;
}
.detail-meta-item {
  background: var(--secondary);
  padding: 6px 12px;
  border-radius: 10px;
  font-size: 13px;
  color: var(--hint);
}
.detail-meta-item strong {
  color: var(--text);
}

/* Chapter accent in detail */
.detail-card[data-accent] {
  border-left: 3px solid var(--btn);
  border-radius: 0 var(--radius) var(--radius) 0;
}
