/* -----------------------------------------------------------------------------
   viewer.css — ブラウザ用電子ブックビューア
   スマートフォンの縦持ちを基準に組み、画面が広がるほど見開きへ育てていく。
   ----------------------------------------------------------------------------- */

:root {
  --accent: #4f8cff;
  --bg: #0d0f14;
  --bg-2: #161923;
  --surface: rgba(24, 27, 36, 0.92);
  --line: rgba(255, 255, 255, 0.1);
  --ink: #eef1f7;
  --ink-dim: #9ba4b6;
  --bar-h: 54px;
  --radius: 14px;
  --shadow-page: 0 18px 60px rgba(0, 0, 0, 0.55);
  --ease: cubic-bezier(0.22, 0.61, 0.36, 1);
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
}

* {
  box-sizing: border-box;
  -webkit-tap-highlight-color: transparent;
}

html,
body {
  margin: 0;
  height: 100%;
  /* clip はスクロールコンテナを作らないため、画面外に退避したパネルを
     ブラウザが「スクロールして見せよう」としてレイアウトが崩れる事故を防げる */
  overflow: hidden;
  overflow: clip;
  background: var(--bg);
  color: var(--ink);
  font-family: -apple-system, BlinkMacSystemFont, 'Hiragino Sans', 'Noto Sans JP', 'Segoe UI', sans-serif;
  font-size: 15px;
  overscroll-behavior: none;
}

body {
  /* iOS Safari のアドレスバー伸縮でガタつかないように */
  height: 100dvh;
}

#app {
  position: relative;
  width: 100%;
  height: 100dvh;
  overflow: hidden;
  overflow: clip;
  display: grid;
  /* 列の幅を画面幅に固定する。既定(auto)のままだと、上部バーのボタンが増えたときに
     バーの最小幅まで画面全体が横に広がり、スマートフォンで紙面の右端が切れる
     (2026-10 印刷ボタンを足したときに iPhone で発生)。 */
  grid-template-columns: minmax(0, 1fr);
  grid-template-rows: auto 1fr auto;
  background:
    radial-gradient(120% 90% at 50% 0%, rgba(255, 255, 255, 0.05), transparent 60%),
    linear-gradient(180deg, var(--bg-2), var(--bg));
}

button {
  font: inherit;
  color: inherit;
  border: none;
  background: none;
  cursor: pointer;
}

svg {
  width: 22px;
  height: 22px;
  fill: currentColor;
  display: block;
}

/* --- バー ----------------------------------------------------------------- */

#topbar,
#bottombar {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 0 8px;
  min-height: var(--bar-h);
  background: var(--surface);
  backdrop-filter: saturate(160%) blur(18px);
  -webkit-backdrop-filter: saturate(160%) blur(18px);
  position: relative;
  z-index: 20;
  transition: transform 260ms var(--ease), opacity 200ms ease;
}

#topbar {
  border-bottom: 1px solid var(--line);
  padding-top: var(--safe-top);
}

#bottombar {
  border-top: 1px solid var(--line);
  padding-bottom: var(--safe-bottom);
  gap: 4px;
}

#app.immersive #topbar {
  transform: translateY(-100%);
  opacity: 0;
}

#app.immersive #bottombar {
  transform: translateY(100%);
  opacity: 0;
}

/* 埋め込み時は掲載ページ側に見出しがあるので、資料名だけ省く。
   検索や一覧は残しておかないと、埋め込んだ途端に使い勝手が落ちる。 */
#app.embed #doc-title {
  display: none;
}

/* bare=1 は上部バーごと消す指定。
   行が 3 つ前提のグリッドなので、消したぶんの行定義も合わせて畳む。
   ここを直さないと #stage が高さ 0 になり紙面が消える。 */
#app.bare {
  grid-template-rows: 1fr auto;
}

#app.bare #topbar {
  display: none;
}

#doc-title {
  font-size: 15px;
  font-weight: 600;
  margin: 0;
  padding: 0 4px;
  /* 狭い画面では資料名から先に縮める(ボタンは縮めない) */
  flex: 0 1 auto;
  min-width: 0;
  max-width: min(46vw, 420px);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  letter-spacing: 0.01em;
}

.spacer {
  flex: 1;
}

.icon-btn {
  flex: 0 0 auto;
  width: 42px;
  height: 42px;
  border-radius: 11px;
  display: grid;
  place-items: center;
  color: var(--ink);
  transition: background 140ms ease, color 140ms ease;
}

.icon-btn:hover {
  background: rgba(255, 255, 255, 0.08);
}

.icon-btn:active {
  background: rgba(255, 255, 255, 0.14);
}

.icon-btn.on {
  background: var(--accent);
  color: #fff;
}

.icon-btn.compact {
  width: 40px;
  height: 40px;
}

/* --- スクラバ ------------------------------------------------------------- */

.scrubber {
  flex: 1;
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 0 6px;
  min-width: 0;
}

#scrub {
  flex: 1;
  min-width: 0;
  appearance: none;
  height: 30px;
  background: transparent;
}

#scrub::-webkit-slider-runnable-track {
  height: 4px;
  border-radius: 2px;
  background: linear-gradient(to right, var(--accent) var(--fill, 0%), rgba(255, 255, 255, 0.16) var(--fill, 0%));
}

#scrub::-webkit-slider-thumb {
  appearance: none;
  width: 18px;
  height: 18px;
  margin-top: -7px;
  border-radius: 50%;
  background: #fff;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.5);
}

#scrub::-moz-range-track {
  height: 4px;
  border-radius: 2px;
  background: rgba(255, 255, 255, 0.16);
}

#scrub::-moz-range-progress {
  height: 4px;
  border-radius: 2px;
  background: var(--accent);
}

#scrub::-moz-range-thumb {
  width: 18px;
  height: 18px;
  border: none;
  border-radius: 50%;
  background: #fff;
}

#pageno {
  font-size: 12.5px;
  color: var(--ink-dim);
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
  min-width: 62px;
  text-align: right;
}

/* --- 紙面 ----------------------------------------------------------------- */

#stage {
  position: relative;
  min-height: 0;
  overflow: hidden;
  touch-action: none;
  /* 画像はブラウザ既定でドラッグ可能。放置すると紙面をなぞった瞬間に
     ネイティブの画像ドラッグが始まり、以降の pointer イベントが届かなくなる */
  user-select: none;
  -webkit-user-select: none;
}

#stage img {
  -webkit-user-drag: none;
  user-drag: none;
  pointer-events: none;
}

#viewport {
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
  will-change: transform;
  transition: transform 260ms var(--ease);
}

#viewport.dragging {
  transition: none;
}

#spread {
  position: relative;
  display: flex;
  gap: 0;
  /* 3D は .flipper の中だけ。ここまで 3D にすると、めくり中に下の紙までぼやける */
}

/* 1枚のページ */
.leaf {
  position: relative;
  background-color: #fff;
  background-size: cover;
  background-position: center;
  box-shadow: var(--shadow-page);
  overflow: hidden;
  flex: none;
}

.leaf img {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  object-fit: fill;
  opacity: 0;
  transition: opacity 260ms ease;
  display: block;
}

.leaf img.ready {
  opacity: 1;
}

/*
  めくっている最中、まだ何も無い側の枠。
  白い紙を置くと「存在しないページがある」ように見えるので、何も描かない。
*/
.leaf.blank {
  background: none;
  box-shadow: none;
}

/*
  紙面はどこを押してもめくれる（左半分で戻り、右半分で進む）。
  ただし黙って押せるだけだと誰も気づかないので、カーソルの形で知らせる。
  指で触る端末には効かないが、そちらは元々「画面を触ってみる」ので問題ない。
*/
@media (hover: hover) {
  #stage {
    cursor: pointer;
  }
  #viewport.zoomed {
    cursor: grab;
  }
  #viewport.zoomed.dragging {
    cursor: grabbing;
  }
}

/*
  紙面に置いたクリック領域。
  発行元のデザインを塗り潰したくないので、既定はごく薄い縁だけ。
  ただし完全に透明にすると、指で触る端末では「押せる場所がある」ことに
  誰も気づけない（マウスのようにかざして探せないため）。
  そこで、ふだんは輪郭がうっすら見え、触れた瞬間にはっきりする、という程度にしている。
*/
.hotspot {
  position: absolute;
  display: block;
  border-radius: 3px;
  box-shadow: inset 0 0 0 1px color-mix(in srgb, var(--accent) 34%, transparent);
  background: color-mix(in srgb, var(--accent) 7%, transparent);
  transition:
    background-color 160ms ease,
    box-shadow 160ms ease;
  -webkit-tap-highlight-color: transparent;
}

.hotspot:hover,
.hotspot:focus-visible {
  background: color-mix(in srgb, var(--accent) 22%, transparent);
  box-shadow:
    inset 0 0 0 2px var(--accent),
    0 0 0 3px color-mix(in srgb, var(--accent) 30%, transparent);
  outline: none;
}

/* 拡大中は紙面をなぞって動かすので、リンクに邪魔をさせない */
#viewport.zoomed .hotspot {
  pointer-events: none;
}

/* 見開きの綴じ目に薄い陰を落として本らしく見せる */
.leaf.left::after,
.leaf.right::after {
  content: '';
  position: absolute;
  top: 0;
  bottom: 0;
  width: 34px;
  pointer-events: none;
}

.leaf.left::after {
  right: 0;
  background: linear-gradient(to right, transparent, rgba(0, 0, 0, 0.16));
}

.leaf.right::after {
  left: 0;
  background: linear-gradient(to left, transparent, rgba(0, 0, 0, 0.16));
}

/* めくり中の紙。細い帯の集まりで、帯ごとに遅れて返るからしなって見える */
.flipper {
  position: absolute;
  top: 0;
  z-index: 5;
  transform-style: preserve-3d;
  perspective: 2200px;
  /* filter は 3D を平らに潰してしまう(preserve-3d が効かなくなる)ので使わない */
}

/* will-change は付けない。層が増えるほど描画解像度が落ち、紙面がぼやける */
.flipper .strip {
  position: absolute;
  top: 0;
  transform-style: preserve-3d;
}

.flipper .face {
  position: absolute;
  inset: 0;
  background-color: #fff;
  background-repeat: no-repeat;
  backface-visibility: hidden;
  /* 帯の継ぎ目の線を目立たせない(回転した縁のギザギザを抑える) */
  outline: 1px solid transparent;
}

/* 先頭の帯だけに柔らかい影。紙全体が下の紙に落とす影は .turn-shadow が受け持つ */
.flipper > .strip > .face {
  box-shadow: 0 4px 18px rgba(0, 0, 0, 0.18);
}

.flipper .face.back {
  transform: rotateY(180deg);
}

/* 1ページ表示でめくる紙の裏側。印刷されていない、少し温かい紙の色 */
.flipper .face.paper {
  background: linear-gradient(90deg, #f6f4ee 0%, #eeebe3 55%, #e6e3da 100%);
}

/* めくった紙が下の紙に落とす影。進み具合に合わせて JS が濃さを動かす */
.turn-shadow {
  position: absolute;
  top: 0;
  z-index: 4;
  pointer-events: none;
  opacity: 0;
}

.flipper .face.back {
  transform: rotateY(180deg);
}

.flipper .face img {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  object-fit: fill;
}

.flipper .glare {
  position: absolute;
  inset: 0;
  pointer-events: none;
  opacity: 0;
}

/* 端の送り矢印。
   操作の判定は #stage 側でまとめて行うので、ここは見た目だけの案内にする。
   ボタンが指を横取りすると、画面端から始めたスワイプが効かなくなる。 */
.edge-btn {
  position: absolute;
  top: 0;
  bottom: 0;
  width: 84px;
  display: grid;
  place-items: center;
  color: rgba(255, 255, 255, 0.62);
  opacity: 0;
  transition: opacity 180ms ease;
  z-index: 10;
  pointer-events: none;
}

.edge-btn.prev {
  left: 0;
  background: linear-gradient(to right, rgba(0, 0, 0, 0.35), transparent);
}

.edge-btn.next {
  right: 0;
  background: linear-gradient(to left, rgba(0, 0, 0, 0.35), transparent);
}

.edge-btn svg {
  width: 30px;
  height: 30px;
}

.edge-btn:disabled {
  opacity: 0 !important;
  pointer-events: none;
}

@media (hover: hover) {
  #stage:hover .edge-btn {
    opacity: 1;
  }
}

/* タッチ端末では矢印を出さない（紙面を隠さない） */
@media (hover: none) {
  .edge-btn {
    display: none;
  }
}

/* --- パネル（一覧・検索） --------------------------------------------------- */

#scrim {
  position: absolute;
  inset: 0;
  background: rgba(0, 0, 0, 0.55);
  opacity: 0;
  pointer-events: none;
  transition: opacity 220ms ease;
  z-index: 30;
}

#app.panel-open #scrim {
  opacity: 1;
  pointer-events: auto;
}

.panel {
  position: absolute;
  z-index: 40;
  background: var(--surface);
  backdrop-filter: saturate(160%) blur(22px);
  -webkit-backdrop-filter: saturate(160%) blur(22px);
  display: flex;
  flex-direction: column;
  overscroll-behavior: contain;
  /* 閉じている間は visibility で完全に無効化し、
     読み上げやキーボード移動が画面外のパネルに入り込まないようにする */
  visibility: hidden;
  transition: transform 300ms var(--ease), visibility 0s linear 300ms;
}

.panel.open {
  visibility: visible;
  transition: transform 300ms var(--ease), visibility 0s;
}

/* スマートフォン: 下から出るシート */
@media (max-width: 767px) {
  .panel {
    left: 0;
    right: 0;
    bottom: 0;
    max-height: 76dvh;
    height: 76dvh;
    border-radius: 18px 18px 0 0;
    border-top: 1px solid var(--line);
    transform: translateY(101%);
    padding-bottom: var(--safe-bottom);
  }

  .panel.open {
    transform: translateY(0);
  }

  .panel::before {
    content: '';
    position: absolute;
    top: 7px;
    left: 50%;
    width: 38px;
    height: 4px;
    margin-left: -19px;
    border-radius: 2px;
    background: rgba(255, 255, 255, 0.24);
  }

  .panel-head {
    padding-top: 14px;
  }
}

/* タブレット以上: 横から出るパネル */
@media (min-width: 768px) {
  .panel {
    top: 0;
    bottom: 0;
    width: 340px;
    border-right: 1px solid var(--line);
    left: 0;
    transform: translateX(-101%);
  }

  .panel.open {
    transform: translateX(0);
  }

  #search-panel {
    left: auto;
    right: 0;
    border-right: none;
    border-left: 1px solid var(--line);
    transform: translateX(101%);
  }

  #search-panel.open {
    transform: translateX(0);
  }
}

.panel-head {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 10px 10px 10px 14px;
  border-bottom: 1px solid var(--line);
}

.panel-body {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  padding: 12px;
}

.tabs {
  display: flex;
  gap: 4px;
  flex: 1;
}

.tab {
  padding: 8px 14px;
  border-radius: 9px;
  color: var(--ink-dim);
  font-size: 13.5px;
}

.tab.active {
  background: rgba(255, 255, 255, 0.1);
  color: var(--ink);
}

.pane {
  display: none;
}

.pane.active {
  display: block;
}

/* サムネイル */
#thumbgrid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(94px, 1fr));
  gap: 10px;
}

.thumb {
  position: relative;
  border-radius: 8px;
  overflow: hidden;
  background: #fff;
  border: 2px solid transparent;
  cursor: pointer;
  aspect-ratio: var(--thumb-ratio, 3 / 4);
}

.thumb img {
  width: 100%;
  height: 100%;
  object-fit: contain;
  display: block;
  background: #fff;
}

.thumb.current {
  border-color: var(--accent);
}

.thumb b {
  position: absolute;
  right: 4px;
  bottom: 4px;
  background: rgba(0, 0, 0, 0.72);
  border-radius: 5px;
  padding: 1px 6px;
  font-size: 11px;
  font-weight: 500;
  color: #fff;
}

/* 目次・検索結果 */
.row {
  display: flex;
  gap: 10px;
  align-items: baseline;
  padding: 11px 10px;
  border-radius: 10px;
  cursor: pointer;
  line-height: 1.55;
}

.row:hover {
  background: rgba(255, 255, 255, 0.06);
}

.row .pg {
  flex: none;
  min-width: 30px;
  text-align: right;
  font-size: 12px;
  color: var(--accent);
  font-variant-numeric: tabular-nums;
}

.row .tx {
  font-size: 13.5px;
  min-width: 0;
}

.row mark {
  background: rgba(79, 140, 255, 0.34);
  color: inherit;
  border-radius: 3px;
  padding: 0 2px;
}

.search-field {
  flex: 1;
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 0 12px;
  background: rgba(255, 255, 255, 0.08);
  border-radius: 11px;
  height: 42px;
}

.search-field svg {
  width: 18px;
  height: 18px;
  color: var(--ink-dim);
  flex: none;
}

.search-field input {
  flex: 1;
  min-width: 0;
  background: none;
  border: none;
  outline: none;
  color: var(--ink);
  font-size: 16px; /* iOS で自動ズームさせないため 16px 以上 */
}

.hint {
  padding: 30px 16px;
  text-align: center;
  color: var(--ink-dim);
  font-size: 13px;
  line-height: 1.9;
}

/* --- 初期表示 ------------------------------------------------------------- */

#boot {
  position: absolute;
  inset: 0;
  z-index: 60;
  display: grid;
  place-items: center;
  background: var(--bg);
  transition: opacity 400ms ease;
}

#app:not(.loading) #boot {
  opacity: 0;
  pointer-events: none;
}

.boot-inner {
  display: grid;
  gap: 16px;
  justify-items: center;
  padding: 0 24px;
  text-align: center;
}

.boot-bar {
  width: 180px;
  height: 3px;
  border-radius: 2px;
  background: rgba(255, 255, 255, 0.12);
  overflow: hidden;
}

.boot-bar span {
  display: block;
  height: 100%;
  width: 30%;
  border-radius: 2px;
  background: var(--accent);
  animation: slide 1.1s var(--ease) infinite;
}

@keyframes slide {
  from {
    transform: translateX(-110%);
  }
  to {
    transform: translateX(440%);
  }
}

#boot-label {
  margin: 0;
  font-size: 13px;
  color: var(--ink-dim);
}

/* --- トースト ------------------------------------------------------------- */

#toast {
  position: absolute;
  left: 50%;
  bottom: calc(var(--bar-h) + 22px + var(--safe-bottom));
  transform: translate(-50%, 10px);
  background: rgba(12, 14, 20, 0.94);
  border: 1px solid var(--line);
  border-radius: 999px;
  padding: 9px 20px;
  font-size: 13px;
  opacity: 0;
  pointer-events: none;
  transition: opacity 200ms ease, transform 200ms var(--ease);
  z-index: 50;
  white-space: nowrap;
}

#toast.on {
  opacity: 1;
  transform: translate(-50%, 0);
}

/* --- 動きを減らす設定への配慮 ------------------------------------------------ */

@media (prefers-reduced-motion: reduce) {
  #viewport,
  .panel,
  #topbar,
  #bottombar {
    transition-duration: 1ms;
  }

  .flipper {
    display: none !important;
  }
}

/* テキスト版への導線。ページ一覧パネルの足元に置く。
   紙面は画像なので、読み上げソフトを使う方には本文が届かない。その逃げ道。 */
.panel-foot {
  border-top: 1px solid var(--line, rgba(255, 255, 255, .12));
  padding: 12px 14px;
}
.panel-foot a {
  display: flex;
  align-items: center;
  gap: 8px;
  color: inherit;
  font-size: 13px;
  text-decoration: none;
  opacity: .8;
}
.panel-foot a:hover { opacity: 1; text-decoration: underline; }
.panel-foot svg { width: 16px; height: 16px; fill: currentColor; flex: none; }

/* -------------------------------------------------------------------------- */
/* 印刷                                                                        */
/* -------------------------------------------------------------------------- */

.print-menu {
  position: absolute;
  top: 58px;
  right: 12px;
  z-index: 60;
  display: flex;
  flex-direction: column;
  gap: 6px;
  width: min(300px, calc(100vw - 24px));
  padding: 14px;
  border-radius: 14px;
  background: rgba(20, 22, 30, 0.96);
  color: #fff;
  box-shadow: 0 12px 40px rgba(0, 0, 0, 0.45);
}
.print-menu-title { margin: 0 0 4px; font-size: 14px; font-weight: 700; }
.print-menu button {
  padding: 10px 12px;
  border: 1px solid rgba(255, 255, 255, 0.18);
  border-radius: 10px;
  background: rgba(255, 255, 255, 0.08);
  color: inherit;
  font: inherit;
  font-size: 14px;
  text-align: left;
  cursor: pointer;
}
.print-menu button:hover { background: rgba(255, 255, 255, 0.16); }
.print-menu button.ghost { border-color: transparent; background: none; color: rgba(255, 255, 255, 0.7); text-align: center; }
.print-menu-note { margin: 4px 0 0; font-size: 12px; color: rgba(255, 255, 255, 0.65); }

#print-sheet { display: none; }

@media print {
  @page { margin: 8mm; }
  html, body { background: #fff !important; height: auto !important; overflow: visible !important; }
  body > *:not(#print-sheet) { display: none !important; }
  #print-sheet { display: block; }
  #print-sheet img {
    display: block;
    max-width: 100%;
    max-height: calc(100vh - 2mm);
    margin: 0 auto;
    object-fit: contain;
    break-after: page;
    page-break-after: always;
  }
  #print-sheet img:last-child { break-after: auto; page-break-after: auto; }
}

/* 幅の狭いスマートフォン(iPhone SE・小型 Android など)。
   ボタンを少しだけ詰めて、資料名を読める幅を残す */
@media (max-width: 380px) {
  #topbar,
  #bottombar {
    gap: 2px;
    padding-left: 4px;
    padding-right: 4px;
  }
  .icon-btn {
    width: 38px;
    height: 40px;
  }
  #doc-title {
    font-size: 14px;
  }
}
