/* ============================
   Kyoto RDB: Detail Page – Elegant (Animal Palette)
   ============================ */
/*
  種の詳細ページ（species_detail.html）専用のスタイル
*/

/* 1) Design Tokens */
:root {
  /* メインカラー（デフォルト：野生動物） */
  --rdb-main-dark: #6e4c2e;
  --rdb-main-light: #efe4d8;

  /* 京都府パープル（アクセント用・控えめ） */
  --kp-primary: #751485;
  --kp-primary-100: #e7d3eb;
  --kp-primary-50: #f6eef8;

  /* Neutral / Ink */
  --kp-ink: #222426;
  --kp-muted: #646a6e;

  /* Surfaces */
  --kp-paper: #f8f6f2;
  /* 和紙風 */
  --kp-surface: #ffffff;
  --kp-line: #e5e1e8;
  --kp-line-darker: #beb8c2;
  /* 罫線色をさらに濃く調整 */

  /* Layout tokens */
  --radius-s: 8px;
  --radius-m: 14px;
  --radius-l: 20px;
  --shadow-s: 0 2px 6px rgba(34, 36, 38, 0.06);
  --shadow-m: 0 10px 24px rgba(34, 36, 38, 0.08);
  --maxw: 1120px;
  --gap: clamp(16px, 2vw, 24px);
}

/* カテゴリ別カラーパレット（コントラスト強化版を使用） */
body.cat-animal {
  --rdb-main-dark: var(--cat-animal-accent, #6e4c2e);
  --rdb-main-light: var(--cat-animal-weak, #efe4d8);
}

body.cat-flora {
  --rdb-main-dark: var(--cat-flora-accent, #2f6e3b);
  --rdb-main-light: var(--cat-flora-weak, #dfe9e2);
  --kp-muted: #4f6b55;
  --kp-line: #d4e3d2;
  --kp-line-darker: #b0c8af;
}

body.cat-geo {
  --rdb-main-dark: var(--cat-geo-accent, #5a6473);
  --rdb-main-light: var(--cat-geo-weak, #e4e8ee);
  --kp-muted: #525b66;
}

body.cat-eco {
  --rdb-main-dark: var(--cat-eco-accent, #2c6a7b);
  --rdb-main-light: var(--cat-eco-weak, #d8e9ee);
  --kp-muted: #2f5d66;
}

/* Typography */
body {
  color: var(--kp-ink);
  background: #ffffff;
  font-family: "Noto Sans JP", "Hiragino Kaku Gothic ProN", "Yu Gothic",
    system-ui, sans-serif;
  line-height: 1.75;
  letter-spacing: 0.01em;
}

.detail-header__name,
.detail-header__classification {
  font-family: var(--font-sans);
}

/* Page */
#rdb-detail-page {
  margin-inline: auto;
  max-width: var(--maxw);
  padding: clamp(16px, 3vw, 32px);
}

/* Header */
.detail-header {
  display: grid;
  grid-template-columns: 1.2fr 0.9fr;
  gap: var(--gap);
  align-items: end;
}

@media (max-width: 960px) {
  .detail-header {
    grid-template-columns: 1fr;
  }
}

/* Classification & Title */
.detail-header__classification {
  margin: 0 0 0.25rem;
  font-size: 0.95rem;
  color: var(--kp-ink);
  font-weight: 600;
}

.detail-header__name {
  margin: 0 0 0.5rem;
  font-size: clamp(1.8rem, 3.2vw, 2.4rem);
  font-weight: 600;
  letter-spacing: 0.02em;
  background: linear-gradient(90deg, var(--rdb-main-light), #fff0) 0 100%/100% 6px no-repeat;
  padding-bottom: 0.4rem;
  color: var(--rdb-main-dark);
}

/* 種名・名称は黒で統一（野生動物・野生植物・菌類・地域生態系） */
body.cat-animal .detail-header__name,
body.cat-flora .detail-header__name,
body.cat-eco .detail-header__name {
  color: #000;
}

.detail-header__sciname {
  margin: 0.25rem 0 0;
  font-style: normal;
  /* デフォルトは通常書体、<em>タグ内のみ斜体 */
  color: color-mix(in oklab, var(--kp-muted), #000 10%);
}

.detail-header__sciname em {
  font-style: italic;
  /* <em>タグ内のみ斜体 */
}

/* Category Table */
.category-table {
  margin-top: 1rem;
  border-collapse: separate;
  border-spacing: 0;
  width: 100%;
  background: transparent;
  border: none;
  table-layout: fixed;
}

.category-table th,
.category-table td {
  padding: 0.85rem 1rem;
  border-bottom: 1px solid var(--kp-line-darker);
  vertical-align: middle;
  font-size: 0.95rem;
}

.category-table th {
  width: 25%;
  color: #fff;
  /* 背景色に合わせて文字色を白に変更 */
  background: var(--rdb-main-dark);
  font-weight: 600;
  vertical-align: middle;
}

.category-table tr:last-child th,
.category-table tr:last-child td {
  border-bottom: none;
}

/* バッジ */
.category-badge {
  display: inline-flex;
  align-items: center;
  gap: 0.5em;
  padding: 0.35em 0.7em;
  border-radius: 999px;
  background: var(--rdb-main-light);
  border: 1px solid color-mix(in oklab, var(--rdb-main-light), #000 10%);
  color: var(--rdb-main-dark);
  font-weight: 600;
  font-size: 0.88rem;
  line-height: 1.2;
}

/* Image */
.detail-header__image figure {
  margin: 0;
  background: transparent;
  border: none;
  border-radius: 0;
  /* カードとしての角丸をしっかり見せつつ、画像もほぼフルで見せる */
  overflow: hidden;
  box-shadow: none;
}

/* 複数画像（サムネイルあり）の場合のみ、ヘッダー画像サイズをコンパクトに */
.detail-header__image figure.has-thumbs {
  max-width: 320px;
}

#rdb-detail-page .detail-header__image {
  display: flex;
  justify-content: flex-end;
  /* PCレイアウトでは右寄せでバランスを取る */
}

@media (max-width: 960px) {
  #rdb-detail-page .detail-header__image {
    justify-content: center;
    /* スマホ・タブレットでは中央寄せ */
  }
}

#species-image {
  display: block;
  width: 100%;
  height: auto;
  /* 元画像の縦横比を維持 */
  /* 画像全体を収め、トリミングしない（上下左右に余白が付くことは許容） */
  object-fit: contain;
  filter: saturate(0.95);
  background-color: #fff;
  /* レターボックス部分の背景 */
}

.detail-header__image figure.has-thumbs #species-image {
  max-height: 220px;
}

#species-image-caption {
  padding: 0.65rem 0.9rem;
  font-size: 0.88rem;
  color: var(--kp-muted);
  background: linear-gradient(90deg, var(--rdb-main-light), #fff0);
  border-top: 1px solid var(--kp-line-darker);
}

/* Body */
.detail-body {
  margin-top: clamp(16px, 3vw, 36px);
}

.detail-table {
  width: 100%;
  border-collapse: separate;
  border-spacing: 0;
  background: transparent;
  border: none;
  table-layout: fixed;
}

.detail-table tbody tr:nth-child(even) td {
  background: #fbfaf9;
}

.detail-table td {
  padding: 1rem 1rem;
  border-bottom: 1px dashed var(--kp-line-darker);
  font-size: 1rem;
  line-height: 1.5;
  vertical-align: middle;
}

.detail-table td br.compact {
  display: block;
  line-height: 0.3;
  margin: 0;
  margin-bottom: -0.7em;
  height: 0;
}

.detail-table tr:last-child td,
.detail-table tr:last-child th {
  border-bottom: none;
}

.detail-table tr.section-row td {
  background: var(--rdb-main-light);
  font-weight: 700;
  color: color-mix(in oklab, var(--rdb-main-dark), #000 10%);
}

.detail-table th {
  padding: 1rem 1rem;
  border-bottom: 1px dashed var(--kp-line-darker);
  background-color: var(--rdb-main-light);
  border-right: none;
  color: var(--kp-ink);
  font-weight: 600;
  width: 14.2%;
  vertical-align: middle;
}

.detail-table td {
  width: 70%;
}

.detail-table .detail-row--distribution th {
  width: 30%;
}

.detail-table .detail-row--distribution .detail-table__cell--distribution {
  width: 35%;
}

.detail-table .detail-row--distribution .region-map-cell {
  width: 35%;
}

/* 地域生態系：ヘッダーを1カラムにする */
.detail-header.detail-header--single-column {
  grid-template-columns: 1fr;
}

/* Footer */
/* 詳細情報テーブル下のフッター（執筆者・文献）の余白調整 */
.detail-footer {
  margin-top: clamp(16px, 3vw, 28px);
  padding: 1.1rem 1.2rem;
  border: none;
  border-top: 2px dotted var(--kp-line-darker);
  border-radius: 0;
  background: transparent;
  box-shadow: none;
  padding-top: 1.2rem;
  /* 上のテーブルとの間隔 */
  padding-bottom: 0.2rem;
  /* ページフッターとの間隔 */
  text-align: left;
  /* 下部セクションを左寄せに */
  font-size: 0.9em;
}

.detail-footer h2 {
  margin: 0 0 0.6rem;
  font-family: var(--font-sans);
  font-size: 1.15rem;
  letter-spacing: 0.02em;
  color: var(--rdb-main-dark);
}

.detail-footer .byline {
  color: var(--kp-muted);
  font-size: 0.92rem;
}

/* Meta Chips */
.meta-chip {
  display: inline-flex;
  align-items: center;
  gap: 0.4em;
  padding: 0.28rem 0.6rem;
  border-radius: 999px;
  background: var(--rdb-main-light);
  border: 1px solid color-mix(in oklab, var(--rdb-main-light), #000 10%);
  color: var(--rdb-main-dark);
  font-size: 0.86rem;
  font-weight: 600;
}

/* Links */
a {
  color: color-mix(in oklab, var(--rdb-main-dark), var(--kp-primary) 20%);
  text-decoration: none;
}

a:hover {
  text-decoration: underline;
  text-underline-offset: 2px;
}

/* ============================
     画像とマップのサイズ制限
     ============================ */

/* 画像セクション全体のサイズ制限 */
.image-section {
  max-width: 400px;
  width: 100%;
}

/* 地域生態系専用：種名の下に配置する画像セクション */
.regional-image-section {
  max-width: var(--maxw);
  margin: clamp(16px, 3vw, 32px) auto;
}

.regional-image-gallery {
  width: 100%;
}

/* メイン画像 */
.regional-image-main {
  display: flex;
  align-items: center;
  justify-content: center;
  margin: 0 0 8px;
  max-height: 420px;
  overflow: hidden;
}

.regional-image-main img {
  max-width: 100%;
  max-height: 420px;
  width: auto;
  height: auto;
  object-fit: contain;
  display: block;
  transition: opacity 0.2s ease;
}

.regional-image-main figcaption {
  display: none;
}

/* サムネイル帯: 固定サイズ・中央揃えで均一に並べる */
.regional-image-thumbs {
  display: flex;
  justify-content: center;
  align-items: flex-start;
  flex-wrap: wrap;
  gap: 8px;
  margin: 0;
}

/* サムネイルボタン: 固定 100×75px の均一ボックス */
.regional-image-thumb {
  position: relative;
  flex: 0 0 auto;
  width: 100px;
  height: 75px;
  padding: 0;
  border: none;
  background: #f0ede8;
  cursor: pointer;
  overflow: hidden;
}

.regional-image-thumb img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  display: block;
  opacity: 0.6;
  transition: opacity 0.18s ease;
}

.regional-image-thumb:hover img {
  opacity: 1;
}

.regional-image-thumb.is-active img {
  opacity: 1;
}

/* アクティブ枠を ::after で重ねる（レイアウトに影響しない） */
.regional-image-thumb.is-active::after {
  content: '';
  position: absolute;
  inset: 0;
  border: 2.5px solid var(--cat-eco-accent);
  pointer-events: none;
}

@media (max-width: 768px) {
  .regional-image-thumb {
    width: 80px;
    height: 60px;
  }
}

/* 画像ギャラリー（種の画像）のサイズ制限 */
.image-gallery {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin-bottom: 8px;
}

.image-gallery img {
  max-width: 100%;
  max-height: 300px;
  width: auto;
  height: auto;
  object-fit: contain;
  border-radius: var(--radius-s);
  box-shadow: var(--shadow-s);
}

/* 画像が1枚の場合 */
.image-gallery.count-1 img {
  max-width: 100%;
  max-height: 300px;
}

/* 画像が2枚の場合 */
.image-gallery.count-2 img {
  max-width: calc(50% - 4px);
  max-height: 300px;
}

/* 画像が3枚の場合 */
.image-gallery.count-3 img {
  max-width: calc(33.333% - 6px);
  max-height: 200px;
}

/* 画像が4枚の場合（2×2 レイアウトを想定） */
.image-gallery.count-4 img {
  max-width: calc(50% - 4px);
  max-height: 200px;
}

/* 画像が5枚の場合（2列グリッドで自動折り返し） */
.image-gallery.count-5 img {
  max-width: calc(50% - 4px);
  max-height: 180px;
}

/* 地域生態系用ギャラリー：中央寄せのフレックス配置
     （object-fit は共通の .image-gallery img の contain を利用し、画像全体を表示） */
.regional-image-section .image-gallery {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 16px;
  margin-bottom: 0;
}

/* ============================
     ヘッダー内のサブ画像ギャラリー（静的種ページ用）
     ============================ */

.detail-header__image .image-gallery--sub {
  margin-top: 8px;
  display: flex;
  justify-content: center;
  gap: 8px;
}

.detail-header__image .image-gallery--sub img {
  max-height: 140px;
  width: auto;
  object-fit: contain;
  border-radius: var(--radius-s);
  box-shadow: var(--shadow-s);
  cursor: pointer;
  opacity: 0.9;
  transition: transform 0.2s ease, box-shadow 0.2s ease, opacity 0.2s ease;
}

.detail-header__image .image-gallery--sub.count-1 img {
  max-width: 70%;
}

.detail-header__image .image-gallery--sub.count-2 img {
  max-width: calc(50% - 4px);
}

.detail-header__image .image-gallery--sub img:hover {
  transform: translateY(-1px);
  box-shadow: 0 3px 10px rgba(0, 0, 0, 0.12);
  opacity: 1;
}

.detail-header__image .image-gallery--sub img.is-active {
  outline: 2px solid var(--rdb-main-dark);
  outline-offset: 2px;
  opacity: 1;
}

/* ============================
     地域生態系専用：画像レイアウト（画像数に応じた柔軟な配置）
     ============================ */

/* 地域生態系：サブギャラリーの基本スタイル */
.detail-header__image.regional-ecosystem .image-gallery--sub {
  flex-wrap: wrap;
  justify-content: center;
  align-items: center;
}

/* 地域生態系：3枚の場合（メイン1枚 + サブ2枚） */
.detail-header__image.regional-ecosystem .image-gallery--sub.count-2 {
  justify-content: center;
}

.detail-header__image.regional-ecosystem .image-gallery--sub.count-2 img {
  max-width: calc(48% - 4px);
  max-height: 160px;
}

/* 地域生態系：4枚の場合（メイン1枚 + サブ3枚） */
.detail-header__image.regional-ecosystem .image-gallery--sub.count-3 {
  justify-content: center;
}

.detail-header__image.regional-ecosystem .image-gallery--sub.count-3 img {
  max-width: calc(32% - 6px);
  max-height: 150px;
}

/* 地域生態系：5枚の場合（メイン1枚 + サブ4枚、2×2グリッド） */
.detail-header__image.regional-ecosystem .image-gallery--sub.count-4 {
  max-width: 100%;
}

.detail-header__image.regional-ecosystem .image-gallery--sub.count-4 img {
  max-width: calc(48% - 4px);
  max-height: 140px;
}

/* 地域生態系：6枚以上の場合（メイン1枚 + サブ5枚、2×3または3×2） */
.detail-header__image.regional-ecosystem .image-gallery--sub.count-5 {
  max-width: 100%;
}

.detail-header__image.regional-ecosystem .image-gallery--sub.count-5 img {
  max-width: calc(32% - 6px);
  max-height: 130px;
}

/* 地域生態系：メイン画像のサイズ調整（サブ画像が多い場合に少し小さく） */
.detail-header__image.regional-ecosystem.has-thumbs #species-image {
  max-height: 240px;
}

/* 地域生態系：レスポンシブ対応（モバイル） */
@media (max-width: 768px) {

  .detail-header__image.regional-ecosystem .image-gallery--sub.count-2 img,
  .detail-header__image.regional-ecosystem .image-gallery--sub.count-3 img,
  .detail-header__image.regional-ecosystem .image-gallery--sub.count-4 img,
  .detail-header__image.regional-ecosystem .image-gallery--sub.count-5 img {
    max-width: calc(50% - 4px);
    max-height: 120px;
  }
}

/* 種の画像（ID指定の場合） */
#speciesImage {
  max-width: 100%;
  max-height: 300px;
  width: auto;
  height: auto;
  object-fit: contain;
  display: block;
}

/* 分布図のサイズ制限 */
.distribution-map {
  margin-top: 16px;
  max-width: 400px;
  width: 100%;
}

#distributionImage {
  max-width: 100%;
  max-height: 300px;
  width: auto;
  height: auto;
  object-fit: contain;
  display: block;
  border-radius: var(--radius-s);
  box-shadow: var(--shadow-s);
}

/* 地域マップ（SVG）のサイズ制限 */
.region-map {
  max-width: 400px;
  width: 100%;
  background: #fff;
  border: 1px solid var(--kp-line-darker);
  border-radius: var(--radius-m);
  box-shadow: var(--shadow-s);
  padding: 12px;
}

.region-map .map-content {
  max-width: 100%;
  max-height: 300px;
  overflow: hidden;
  display: flex;
  align-items: center;
  justify-content: center;
}

/* マップホバー時の吹き出しツールチップ */
.map-tooltip {
  position: fixed;
  z-index: 9999;
  display: none;
  padding: 6px 10px;
  font-size: 0.8125rem;
  font-family: var(--font-sans);
  color: #fff;
  background: rgba(34, 36, 38, 0.9);
  border-radius: 4px;
  white-space: nowrap;
  pointer-events: none;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.2);
}

.map-attribution {
  margin: 6px 0 0;
  font-size: 0.6rem;
  line-height: 1.5;
  color: var(--color-muted-text, #888);
}

.map-attribution a {
  color: inherit;
  text-decoration: underline;
}

.region-map .map-content svg {
  max-width: 100%;
  max-height: 300px;
  width: auto;
  height: auto;
  display: block;
}

/* 画像セル（テーブル内）のサイズ制限 */
.image-cell {
  max-width: 400px;
  width: 400px;
  vertical-align: top;
}

.region-map-cell {
  max-width: 400px;
  width: 400px;
  vertical-align: top;
}

/* レスポンシブ対応（モバイル） */
@media (max-width: 768px) {

  .image-section,
  .distribution-map,
  .region-map {
    max-width: 100%;
  }

  .image-cell,
  .region-map-cell {
    max-width: 100%;
    width: 100%;
  }

  .image-gallery img,
  #speciesImage,
  #distributionImage,
  .region-map .map-content svg {
    max-height: 250px;
  }

  /* 詳細情報テーブル（下部セクション）のスマホ向け縦積み化 */
  .detail-table,
  .detail-table tbody,
  .detail-table tr,
  .detail-table th,
  .detail-table td {
    display: block;
    width: 100% !important;
  }

  .detail-table th {
    border-bottom: none;
    padding-bottom: 0.2rem;
  }

  .detail-table td {
    padding-top: 0.2rem;
    border-bottom: 1px dashed var(--kp-line-darker);
  }

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

  /* ▼ No.13: 地域生態系の分布テーブルは列数が多く rowspan もあるため縦積み化を解除し横スクロールさせる */
  #regional-distribution-container {
    width: 100%;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    margin-bottom: var(--space-4);
  }

  .regional-distribution-table,
  .regional-distribution-table tbody,
  .regional-distribution-table tr,
  .regional-distribution-table th,
  .regional-distribution-table td {
    display: table-cell;
  }
  .regional-distribution-table {
    display: table;
    width: 100%;
    min-width: 800px;
  }
  .regional-distribution-table tbody {
    display: table-row-group;
  }
  .regional-distribution-table tr {
    display: table-row;
  }
  .regional-distribution-table th,
  .regional-distribution-table td {
    width: auto !important;
    border-bottom: 1px solid #dcdcdc;
    padding: 0.8rem;
  }
}

/* ============================
     マップの色分けスタイル
     ============================ */

.region-map .map-content svg path,
.region-map .map-content svg polygon {
  fill: #fff;
  stroke: #8a8174;
  stroke-width: 1.6;
  transition: fill 0.3s ease, stroke 0.3s ease;
}

.region-map .map-content svg path.highlighted,
.region-map .map-content svg polygon.highlighted,
.region-map .map-content .highlighted path,
.region-map .map-content .highlighted polygon {
  fill: #e96d5d !important;
  stroke: #6c3d33;
}

.region-map .map-content svg path:hover,
.region-map .map-content svg polygon:hover {
  fill: #ffd8d1;
  cursor: pointer;
}

/* ============================
     地域生態系：府内での分布と京都府カテゴリー（原稿レイアウト）
     ============================ */
.regional-distribution-section {
  margin-top: clamp(16px, 3vw, 28px);
}

.regional-distribution-table.detail-table {
  table-layout: auto;
}

.regional-distribution-table th,
.regional-distribution-table td {
  width: auto;
}

.regional-distribution-table .regional-table__cell--name,
.regional-distribution-table .regional-table__cell--region {
  vertical-align: top;
}