:root {
  --bg: #fdf6ee;
  --bg2: #f5ebe0;
  --surface: #fff;
  --border: #e0cdb8;
  --text: #2c1f0e;
  --text2: #6b4c2a;
  --accent: #8b3a2a;
  --accent2: #c8855a;
  --tag-main: #7a2020;
  --tag-val: #2a5a7a;
  --tag-esc: #2a6b3a;
  --tag-palace: #6b2a6b;
  --tag-other: #5a5a2a;
  --font-size: 17px;
  --line-height: 2;
}

.dark {
  --bg: #1a1310;
  --bg2: #221a14;
  --surface: #2a2018;
  --border: #3d2e1e;
  --text: #e8d8c0;
  --text2: #b08060;
  --accent: #c87060;
  --accent2: #d09070;
}

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

html { font-size: var(--font-size); }

body {
  background: var(--bg);
  color: var(--text);
  font-family: "Hiragino Mincho ProN", "Yu Mincho", "游明朝", Georgia, serif;
  line-height: var(--line-height);
  min-height: 100dvh;
}

/* ── ナビゲーション ── */
nav {
  position: sticky;
  top: 0;
  z-index: 100;
  display: flex;
  align-items: center;
  gap: 0;
  background: var(--bg2);
  border-bottom: 1px solid var(--border);
  padding: 0 12px;
  height: 52px;
}

.nav-title {
  flex: 1;
  font-size: 0.85rem;
  color: var(--text2);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  padding: 0 8px;
}

nav button {
  background: none;
  border: none;
  color: var(--text);
  font-size: 1.5rem;
  width: 44px;
  height: 44px;
  cursor: pointer;
  border-radius: 8px;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
}

nav button:active { background: var(--border); }

/* ── タブバー ── */
.tabbar {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  z-index: 100;
  display: flex;
  background: var(--bg2);
  border-top: 1px solid var(--border);
  padding-bottom: env(safe-area-inset-bottom);
}

.tabbar button {
  flex: 1;
  background: none;
  border: none;
  color: var(--text2);
  font-size: 0.65rem;
  padding: 8px 4px 6px;
  cursor: pointer;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 3px;
  transition: color 0.15s;
}

.tabbar button .icon { font-size: 1.4rem; line-height: 1; }
.tabbar button.active { color: var(--accent); }

/* ── メインコンテンツ ── */
main {
  padding: 16px 16px calc(env(safe-area-inset-bottom) + 68px);
  max-width: 680px;
  margin: 0 auto;
}

.page { display: none; }
.page.active { display: block; }

/* ── 話一覧 ── */
.search-bar {
  display: flex;
  gap: 8px;
  margin-bottom: 16px;
}

.search-bar input {
  flex: 1;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 10px 14px;
  font-size: 1rem;
  color: var(--text);
  font-family: inherit;
  outline: none;
}

.search-bar input:focus { border-color: var(--accent2); }

.episode-list { display: flex; flex-direction: column; gap: 2px; }

.ep-item {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 14px 16px;
  cursor: pointer;
  display: flex;
  align-items: center;
  gap: 12px;
  transition: background 0.1s;
}

.ep-item:active { background: var(--bg2); }

.ep-num {
  font-size: 0.75rem;
  color: var(--text2);
  min-width: 36px;
}

.ep-title {
  flex: 1;
  font-size: 0.95rem;
}

.ep-read {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--accent);
  flex-shrink: 0;
  opacity: 0;
}

.ep-item.read .ep-read { opacity: 0.4; }
.ep-item.current .ep-read { opacity: 1; }

/* ── 本文ビュー ── */
#reader-view {
  display: none;
  position: fixed;
  inset: 0;
  z-index: 200;
  background: var(--bg);
  overflow-y: auto;
  padding: 0;
}

#reader-view.open { display: flex; flex-direction: column; }

#reader-content {
  flex: 1;
  padding: 24px 20px calc(env(safe-area-inset-bottom) + 80px);
  max-width: 680px;
  width: 100%;
  margin: 0 auto;
  white-space: pre-wrap;
  word-break: break-all;
}

#reader-content.tate {
  writing-mode: vertical-rl;
  text-orientation: mixed;
  height: calc(100dvh - 52px - env(safe-area-inset-bottom) - 80px);
  overflow-x: auto;
  overflow-y: hidden;
  padding: 24px calc(env(safe-area-inset-right) + 20px) 24px 20px;
  max-width: none;
  width: auto;
}

.reader-nav {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  z-index: 210;
  display: flex;
  align-items: center;
  gap: 8px;
  background: var(--bg2);
  border-top: 1px solid var(--border);
  padding: 8px 12px calc(env(safe-area-inset-bottom) + 8px);
}

.reader-nav button {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 8px;
  color: var(--text);
  font-size: 0.85rem;
  padding: 8px 14px;
  cursor: pointer;
  white-space: nowrap;
}

.reader-nav button:disabled { opacity: 0.3; }
.reader-nav button:active:not(:disabled) { background: var(--border); }

.reader-nav .ep-label {
  flex: 1;
  text-align: center;
  font-size: 0.8rem;
  color: var(--text2);
}

/* 設定パネル */
.settings-panel {
  display: none;
  position: fixed;
  bottom: calc(env(safe-area-inset-bottom) + 52px);
  left: 0;
  right: 0;
  z-index: 220;
  background: var(--surface);
  border-top: 1px solid var(--border);
  border-radius: 16px 16px 0 0;
  padding: 16px 20px;
  box-shadow: 0 -4px 24px rgba(0,0,0,0.15);
}

.settings-panel.open { display: block; }

.settings-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 14px;
}

.settings-row:last-child { margin-bottom: 0; }

.settings-label {
  font-size: 0.9rem;
  color: var(--text2);
}

.settings-row .btn-group {
  display: flex;
  gap: 6px;
}

.btn-group button {
  background: var(--bg2);
  border: 1px solid var(--border);
  border-radius: 8px;
  color: var(--text);
  font-size: 0.85rem;
  padding: 6px 12px;
  cursor: pointer;
}

.btn-group button.active {
  background: var(--accent);
  color: #fff;
  border-color: var(--accent);
}

/* ── 登場人物 ── */
.char-filter {
  display: flex;
  gap: 6px;
  flex-wrap: wrap;
  margin-bottom: 14px;
}

.filter-btn {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 20px;
  color: var(--text2);
  font-size: 0.78rem;
  padding: 5px 12px;
  cursor: pointer;
}

.filter-btn.active {
  background: var(--accent);
  color: #fff;
  border-color: var(--accent);
}

.char-search {
  width: 100%;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 10px 14px;
  font-size: 1rem;
  color: var(--text);
  font-family: inherit;
  outline: none;
  margin-bottom: 14px;
}

.char-search:focus { border-color: var(--accent2); }

.char-list { display: flex; flex-direction: column; gap: 2px; }

.char-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
  overflow: hidden;
}

.char-header {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 14px 16px;
  cursor: pointer;
}

.char-tag {
  font-size: 0.65rem;
  padding: 2px 7px;
  border-radius: 10px;
  color: #fff;
  white-space: nowrap;
  flex-shrink: 0;
}

.tag-主役 { background: #7a2020; }
.tag-ヴァレスティナ家 { background: #2a5a7a; }
.tag-エスカランテ家 { background: #2a6b3a; }
.tag-皇室 { background: #6b2a6b; }
.tag-その他 { background: #5a5a3a; }

.char-name { font-size: 1rem; flex: 1; }
.char-role { font-size: 0.75rem; color: var(--text2); }
.char-chevron { color: var(--text2); font-size: 1.1rem; transition: transform 0.2s; }
.char-card.open .char-chevron { transform: rotate(90deg); }

.char-body {
  display: none;
  padding: 0 16px 16px;
  border-top: 1px solid var(--border);
}

.char-card.open .char-body { display: block; }

.char-fullname {
  font-size: 0.78rem;
  color: var(--text2);
  margin: 10px 0 6px;
}

.char-summary {
  font-size: 0.88rem;
  line-height: 1.7;
  margin-bottom: 10px;
}

.char-details { margin-bottom: 10px; }
.char-details li {
  font-size: 0.82rem;
  color: var(--text2);
  line-height: 1.6;
  list-style: none;
  padding-left: 12px;
  position: relative;
}
.char-details li::before { content: "・"; position: absolute; left: 0; }

.speech-box {
  background: var(--bg2);
  border-radius: 8px;
  padding: 8px 12px;
  font-size: 0.8rem;
  color: var(--text2);
  margin-bottom: 10px;
  line-height: 1.7;
}

.relations-title {
  font-size: 0.78rem;
  color: var(--text2);
  margin-bottom: 6px;
}

.relation-chips { display: flex; flex-wrap: wrap; gap: 6px; }

.relation-chip {
  background: var(--bg2);
  border: 1px solid var(--border);
  border-radius: 20px;
  font-size: 0.75rem;
  padding: 4px 10px;
  cursor: pointer;
  color: var(--text);
  transition: background 0.1s;
}

.relation-chip:active { background: var(--border); }

/* ── 「人生」早見表 ── */
.lives-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.82rem;
  margin-top: 16px;
}

.lives-table th, .lives-table td {
  border: 1px solid var(--border);
  padding: 8px 10px;
  text-align: left;
}

.lives-table th {
  background: var(--bg2);
  color: var(--text2);
  font-weight: normal;
}

/* ── 進捗バー ── */
.progress-bar {
  height: 3px;
  background: var(--border);
  border-radius: 2px;
  margin-bottom: 16px;
  overflow: hidden;
}

.progress-fill {
  height: 100%;
  background: var(--accent);
  border-radius: 2px;
  transition: width 0.3s;
}

/* ── ホーム ── */
.home-hero {
  text-align: center;
  padding: 28px 0 20px;
}

.home-title {
  font-size: 1.15rem;
  line-height: 1.6;
  margin-bottom: 8px;
}

.home-sub {
  font-size: 0.82rem;
  color: var(--text2);
}

.home-cards { display: flex; flex-direction: column; gap: 10px; margin-top: 20px; }

.home-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 18px 20px;
  cursor: pointer;
  display: flex;
  align-items: center;
  gap: 16px;
  transition: background 0.1s;
}

.home-card:active { background: var(--bg2); }
.home-card-icon { font-size: 2rem; }
.home-card-body { flex: 1; }
.home-card-title { font-size: 1rem; margin-bottom: 3px; }
.home-card-desc { font-size: 0.78rem; color: var(--text2); }

/* ── スクロールトップボタン ── */
.scroll-top {
  position: fixed;
  right: 16px;
  bottom: calc(env(safe-area-inset-bottom) + 72px);
  width: 44px;
  height: 44px;
  border-radius: 50%;
  background: var(--accent);
  color: #fff;
  border: none;
  font-size: 1.2rem;
  cursor: pointer;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.2s;
  z-index: 90;
  display: flex;
  align-items: center;
  justify-content: center;
}

.scroll-top.show { opacity: 1; pointer-events: auto; }

/* ── お気に入り ── */
#btn-fav { font-size: 1.4rem; }
#btn-fav.fav { color: #e8a020; }

.ep-fav {
  font-size: 0.85rem;
  flex-shrink: 0;
  opacity: 0;
}
.ep-item.fav .ep-fav { opacity: 1; }

/* ── メモパネル ── */
.memo-panel {
  display: none;
  padding: 10px 16px;
  background: var(--bg2);
  border-bottom: 1px solid var(--border);
  gap: 8px;
  flex-direction: row;
  align-items: flex-start;
}
.memo-panel.open { display: flex; }
.memo-panel textarea {
  flex: 1;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 8px 10px;
  font-size: 0.88rem;
  font-family: inherit;
  color: var(--text);
  resize: none;
  outline: none;
  line-height: 1.6;
}
.memo-panel button {
  background: var(--accent);
  color: #fff;
  border: none;
  border-radius: 8px;
  padding: 8px 14px;
  font-size: 0.85rem;
  cursor: pointer;
  white-space: nowrap;
  font-family: inherit;
}

/* ── 関連話（キャラカード内） ── */
.related-eps {
  margin-top: 10px;
}
.related-eps-title {
  font-size: 0.78rem;
  color: var(--text2);
  margin-bottom: 6px;
}
.related-ep-chips { display: flex; flex-wrap: wrap; gap: 6px; }
.related-ep-chip {
  background: var(--bg2);
  border: 1px solid var(--border);
  border-radius: 20px;
  font-size: 0.75rem;
  padding: 4px 10px;
  cursor: pointer;
  color: var(--accent);
  transition: background 0.1s;
}
.related-ep-chip:active { background: var(--border); }

/* ── メモあり表示 ── */
.ep-memo-dot {
  font-size: 0.7rem;
  color: var(--accent2);
  flex-shrink: 0;
  opacity: 0;
}
.ep-item.has-memo .ep-memo-dot { opacity: 1; }

/* ── アニメーション ── */
@keyframes fadeIn {
  from { opacity: 0; transform: translateY(8px); }
  to { opacity: 1; transform: translateY(0); }
}

.page.active { animation: fadeIn 0.2s ease; }
