:root{
  --bg:#f5f7fb;
  --card:#ffffff;
  --text:#1a1f2b;
  --muted:#6b7280;
  --line:rgba(0,0,0,.08);
  --accent:#4da3ff;
  --shadow: 0 10px 30px rgba(0,0,0,.08);
  --shadowStrong: 0 30px 90px rgba(0,0,0,.22);
  --radius: 18px;
  --max: 1080px;
  --photoMat:#eef2f7;

  /* iOS safe area */
  --safeT: env(safe-area-inset-top, 0px);
  --safeB: env(safe-area-inset-bottom, 0px);
}

*{ box-sizing:border-box; }
html,body{ height:100%; }

body{
  margin:0;
  font-family: ui-sans-serif, system-ui, -apple-system, "Hiragino Sans", "Noto Sans JP", "Segoe UI", Arial, sans-serif;
  background:
    radial-gradient(900px 600px at 20% 0%, rgba(77,163,255,.10), transparent 60%),
    radial-gradient(900px 600px at 90% 10%, rgba(180,200,255,.12), transparent 60%),
    var(--bg);
  color:var(--text);
}

/* =========================
   Header
========================= */
.header{
  position: sticky;
  top:0;
  z-index: 20;
  backdrop-filter: blur(10px);
  background: rgba(255,255,255,.88);
  border-bottom: 1px solid var(--line);
  padding: calc(14px + var(--safeT)) 16px 14px;

  display:flex;
  align-items:center;
  justify-content:space-between;
  gap:12px;
}

.brand{
  display:flex;
  align-items:center;
  gap:12px;
  min-width:0;
}

.logo{
  width:40px;
  height:40px;
  display:grid;
  place-items:center;
  border:1px solid var(--line);
  border-radius: 999px;
  background:#fff;
  background-image: url("logo.png");
  flex: 0 0 auto; /* 潰れ防止 */
}

.brandText{ min-width:0; }

.siteTitle{
  font-size: 18px;
  margin:0;
  letter-spacing:.02em;
  white-space: nowrap;
  overflow:hidden;
  text-overflow: ellipsis;
}
.siteSub{
  margin:2px 0 0;
  font-size:12px;
  color:var(--muted);
  white-space: nowrap;
  overflow:hidden;
  text-overflow: ellipsis;
}

.tools{
  display:flex;
  gap:10px;
  align-items:center;
  flex-wrap: wrap;
  justify-content:flex-end;
}
.tools.isHidden{ display:none; }

.search{
  width:min(420px, 52vw);
  padding:10px 12px;
  border-radius: 999px;
  border:1px solid var(--line);
  background:#fff;
  color:var(--text);
  outline:none;
}
.search::placeholder{ color: rgba(107,114,128,.8); }

.btn{
  padding:10px 12px;
  border-radius: 999px;
  border:1px solid var(--line);
  background:#fff;
  color:var(--text);
  cursor:pointer;
  transition:.18s;
  flex: 0 0 auto;
}
.btn:hover{ border-color: var(--accent); color: var(--accent); }
.btn:disabled{ opacity:.5; cursor:not-allowed; }
.btn.ghost{ background: transparent; }
.btn.primary{
  background: var(--accent);
  color:#fff;
  border-color: var(--accent);
}

/* =========================
   Layout
========================= */
.main{ padding: 22px 16px 40px; }

.book{
  max-width: var(--max);
  margin: 0 auto;
  display:grid;
  gap:16px;
}

.book.locked{
  opacity:0;
  transform: translateY(10px);
  pointer-events:none;
}
.book.unlock{ animation: bookIn .6s ease forwards; }
@keyframes bookIn{ to{ opacity:1; transform: translateY(0); } }

/* =========================
   Page（写真縦横比で高さ可変 + 右だけスクロール）
========================= */
.page{
  --mediaAR: 4 / 3;

  background: var(--card);
  border:1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  overflow:hidden;

  display:grid;
  grid-template-columns: 1.2fr 1fr;
  align-items: stretch;
}

.media{
  position:relative;
  width: 100%;
  aspect-ratio: var(--mediaAR);
  background: var(--photoMat);
  overflow:hidden;
  display:grid;
  place-items:center;
}
.media img{
  width:100%;
  height:100%;
  object-fit: contain;
  display:block;
}
.media .caption{
  position:absolute;
  left:12px;
  bottom:12px;
  font-size:12px;
  color:#fff;
  padding:8px 10px;
  border-radius: 999px;
  background: rgba(0,0,0,.4);
}

.text{
  height: 100%;
  overflow:auto;
  -webkit-overflow-scrolling: touch;
  padding: 22px;
  display:flex;
  flex-direction:column;
  gap:12px;
}

.title{ font-size: 18px; margin:0; letter-spacing:.02em; }
.meta{ display:flex; gap:10px; align-items:center; color:var(--muted); font-size: 12px; }
.poem{
  margin:0;
  line-height: 2;
  font-size: 15px;
  white-space: pre-wrap;
  overflow-wrap:anywhere;
}
.tags{
  margin-top: auto;
  padding-top: 14px;
  display:flex;
  flex-wrap:wrap;
  gap:8px;
}
.tag{
  font-size: 12px;
  padding: 6px 10px;
  border-radius: 999px;
  border:1px solid var(--line);
  background: #f3f4f6;
}

/* Nav */
.nav{
  display:flex;
  align-items:center;
  justify-content:space-between;
  gap:10px;
  flex-wrap:wrap;
}
.pager{ display:flex; align-items:center; gap:12px; color: var(--muted); }
.pageInfo{ font-size: 13px; }
.jump{ display:flex; align-items:center; gap:8px; }
.jumpLabel{ font-size:12px; color:var(--muted); }
.jumpSelect{
  padding:10px 12px;
  border-radius: 999px;
  border:1px solid var(--line);
  background:#fff;
  color:var(--text);
}

/* Results */
.results{
  border:1px solid var(--line);
  border-radius: var(--radius);
  background:#fff;
  padding: 14px;
}
.resultsTitle{ margin:0 0 10px; font-size: 13px; color: var(--muted); }
.resultList{ margin:0; padding-left: 18px; display:grid; gap:8px; }
.resultList li{
  color: rgba(26,31,43,.92);
  cursor:pointer;
  text-decoration: underline;
  text-decoration-color: rgba(77,163,255,.45);
  text-underline-offset: 3px;
}
.resultList li:hover{ color: var(--accent); }

/* Footer */
.footer{
  border-top: 1px solid var(--line);
  padding: 16px;
  text-align:center;
  color: var(--muted);
}

/* =========================
   Cover
========================= */
.cover{
  padding: 26px 16px 40px;
  min-height: calc(100vh - 74px);
  display:grid;
  place-items:center;
}
.coverCard{
  width: min(1040px, 100%);
  display:grid;
  grid-template-columns: 1.2fr 1fr;
  border-radius: 22px;
  overflow:hidden;
  border:1px solid var(--line);
  background: #fff;
  box-shadow: var(--shadow);
  position: relative;
  transform-style: preserve-3d;
  perspective: 1200px;
}
.coverMedia{ position:relative; min-height: 520px; background: #eaeef5; }
.coverMedia img{
  width:100%;
  height:100%;
  object-fit: cover;
  display:block;
  transform: scale(1.02);
  filter: contrast(1.02) saturate(1.02);
}
.coverGlow{
  position:absolute;
  inset: -40px;
  background: radial-gradient(600px 380px at 25% 10%, rgba(77,163,255,.18), transparent 60%),
              radial-gradient(600px 380px at 80% 20%, rgba(180,200,255,.16), transparent 60%);
  pointer-events:none;
}
.coverText{
  padding: 26px;
  display:flex;
  flex-direction:column;
  gap:12px;
}
.coverKicker{ margin:0; color: var(--muted); letter-spacing:.18em; text-transform: uppercase; font-size: 12px; }
.coverTitle{ margin:0; font-size: 26px; letter-spacing:.02em; }
.coverLead{ margin:0; color: rgba(55,65,81,.95); line-height: 1.9; }
.coverActions{ display:flex; gap:10px; flex-wrap:wrap; margin-top: 6px; }
.coverMeta{ margin: 10px 0 0; color: var(--muted); font-size: 12px; }

.pageFlip{ position:absolute; inset:0; pointer-events:none; overflow:hidden; opacity:0; }
.pageFlip .sheet{
  position:absolute; top:0; right:-2px; width: 55%; height: 100%;
  transform-origin: right center;
  transform: rotateY(0deg);
  border-left: 1px solid rgba(0,0,0,.06);
  background: linear-gradient(90deg, #fff, #f1f5f9 35%, #e5e7eb 70%, #d1d5db);
  box-shadow: -18px 0 40px rgba(0,0,0,.15);
}
.pageFlip .sheet2{ width: 52%; opacity: .6; filter: blur(.2px); }
.cover.flip .pageFlip{ opacity:1; }
.cover.flip .sheet{ animation: flipSheet 650ms ease forwards; }
.cover.flip .sheet2{ animation: flipSheet2 720ms ease forwards; animation-delay: 90ms; }

@keyframes flipSheet{
  0%{ transform: rotateY(0deg) translateX(0); }
  55%{ transform: rotateY(-115deg) translateX(-10px); }
  100%{ transform: rotateY(-175deg) translateX(-26px); opacity:0; }
}
@keyframes flipSheet2{
  0%{ transform: rotateY(0deg) translateX(0); }
  60%{ transform: rotateY(-110deg) translateX(-6px); }
  100%{ transform: rotateY(-170deg) translateX(-20px); opacity:0; }
}
.cover.flip .coverCard{ animation: coverOpen 650ms ease forwards; }
@keyframes coverOpen{
  0%{ transform: translateY(0) scale(1); opacity:1; }
  45%{ transform: translateY(-4px) scale(1.01); opacity:1; }
  100%{ transform: translateY(8px) scale(.985); opacity:0; }
}

/* =========================
   TOC Modal（はみ出し防止・確実にスクロール）
========================= */
body.modalLock{ overflow:hidden; }

.tocBackdrop{
  position: fixed;
  inset: 0;
  background: rgba(15, 23, 42, .35);
  backdrop-filter: blur(3px);
  z-index: 9998;
}

.tocModal{
  position: fixed;
  z-index: 9999;
  background: #fff;
  border: 1px solid var(--line);
  border-radius: 20px;
  box-shadow: var(--shadowStrong);

  opacity: 0;
  pointer-events: none;
  transition: .18s ease;

  /* デスクトップは中央 */
  left: 50%;
  top: 50%;
  transform: translate(-50%,-50%) scale(.98);
  width: min(820px, calc(100vw - 28px));
  max-height: min(82vh, 860px);
  overflow: hidden;
}

.tocModal.open{
  opacity: 1;
  transform: translate(-50%,-50%) scale(1);
  pointer-events: auto;
}

.tocModalHeader{
  display:flex;
  align-items:center;
  justify-content:space-between;
  gap: 10px;
  padding: 14px 14px 10px;
  border-bottom: 1px solid var(--line);
}
.tocModalTitle{ margin:0; font-size: 16px; }
.tocModalSub{ margin:2px 0 0; font-size: 12px; color: var(--muted); }
.tocModalTitleWrap{ display:flex; flex-direction:column; }

.tocModalBody{
  padding: 12px 14px 14px;
  overflow: auto;
  max-height: calc(min(82vh, 860px) - 64px);
  -webkit-overflow-scrolling: touch;
}

.tocControls{
  display:flex;
  gap:10px;
  align-items:center;
  margin-bottom: 12px;
}
.tocFilter{
  flex: 1;
  padding:10px 12px;
  border-radius: 999px;
  border:1px solid var(--line);
  background:#fff;
  outline:none;
}

.tocGrid{
  margin: 0;
  padding: 0;
  list-style: none;
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 12px;
}
.tocCard{
  border: 1px solid var(--line);
  border-radius: 16px;
  overflow: hidden;
  background: #fff;
  cursor: pointer;
  transition: .18s;
}
.tocCard:hover{
  border-color: rgba(77,163,255,.55);
  box-shadow: 0 12px 28px rgba(77,163,255,.14);
  transform: translateY(-1px);
}
.tocThumb{ position: relative; aspect-ratio: 4 / 3; background: #eaeef5; overflow: hidden; }
.tocThumb img{ width:100%; height:100%; object-fit: cover; display:block; }
.tocNum{
  position: absolute; left: 10px; top: 10px;
  font-size: 12px; padding: 6px 10px; border-radius: 999px;
  background: rgba(0,0,0,.45); color: #fff;
}
.tocCardBody{ padding: 10px 10px 12px; display: grid; gap: 6px; }
.tocCardTitle{ margin:0; font-size: 13px; line-height: 1.35; }
.tocCardMeta{ font-size: 12px; color: var(--muted); }
.tocCardTags{ display:flex; flex-wrap:wrap; gap:6px; margin-top:4px; }
.tocCardTag{
  font-size: 12px; padding: 5px 9px; border-radius: 999px;
  border: 1px solid var(--line); background: #f3f4f6;
}
.tocHint{ margin: 12px 0 0; font-size: 12px; color: var(--muted); }

/* =========================
   Responsive
========================= */
@media (max-width: 980px){
  .tocGrid{ grid-template-columns: repeat(3, minmax(0, 1fr)); }
}

@media (max-width: 860px){
  .page{ grid-template-columns: 1fr; }
  .text{ height:auto; overflow: visible; }

  .coverCard{ grid-template-columns: 1fr; }
  .coverMedia{ min-height: 320px; }
  .coverTitle{ font-size: 22px; }

  .tocGrid{ grid-template-columns: repeat(2, minmax(0, 1fr)); }
}

/* ★ここが重要：スマホで検索＆目次ボタンが必ず全部表示される */
@media (max-width: 640px){
  .header{
    flex-wrap: wrap;
    align-items: flex-start;
    gap: 10px;
    padding: calc(12px + var(--safeT)) 12px 12px;
  }

  .brand{ width: 100%; }

  /* toolsを2段グリッドに固定 */
  .tools{
    width: 100%;
    display: grid;
    grid-template-columns: 1fr auto auto;
    grid-template-areas:
      "search search search"
      "clear  toc   .";
    gap: 8px;
    justify-content: start;
  }

  #searchInput{ grid-area: search; width: 100%; }
  #clearSearch{ grid-area: clear; }
  #toggleToc{ grid-area: toc; }

  /* 目次モーダルを下からのシートに：100dvhで確実に収める */
  .tocModal{
    left: 12px;
    right: 12px;
    top: auto;
    bottom: calc(12px + var(--safeB));
    width: auto;
    transform: none;
    max-height: calc(100dvh - 24px - var(--safeT) - var(--safeB));
    border-radius: 18px;
  }

  .tocModal.open{ transform: none; }

  .tocModalBody{
    max-height: calc(100dvh - 24px - var(--safeT) - var(--safeB) - 64px);
  }
}

@media (max-width: 520px){
  .tocGrid{ grid-template-columns: 1fr; }
}
