/* =============================================================================
 * type_3 軽量PDFビューア（PDF.js + page-flip）スタイル
 *   type_1 ベース。相違点：
 *     - ツールバーを画面下部にドッキング（大きめ・スマホ操作向け）
 *     - アイコンはインラインSVG（端末フォント依存の文字化けを排除）
 *     - サムネイル一覧は左側・常時レイヤー最前面（下部バーに重なる）／既定は開
 *     - 最初/最後のページへ移動するボタンを追加
 * ===========================================================================*/

:root {
  --tb-h: 58px;
  --thumb-w: 168px;
  --bottom-stack: 58px;   /* 検索バー＋下部ツールバーの実高さ。viewer.js が更新 */
  --bg: #1f1f22;
  --bg-bar: #2a2a2e;
  --bg-bar-2: #26262a;
  --fg: #f2f2f4;
  --fg-dim: #b9b9c0;
  --accent: #4da3ff;
  --btn-hover: #3a3a40;
  --btn-active: #4a4a52;
  --border: #3a3a40;
  --shadow: 0 6px 24px rgba(0, 0, 0, .45);
  color-scheme: dark;
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }

html, body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: var(--fg);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "Hiragino Kaku Gothic ProN",
               "Noto Sans JP", Meiryo, sans-serif;
  -webkit-text-size-adjust: 100%;
}

#app {
  position: relative;
  display: flex;
  flex-direction: column;
  height: 100%;
  overflow: hidden;
}

/* ---------------------------------------------------------------------- 本体 */
#body { flex: 1 1 auto; position: relative; min-height: 0; }

#stage {
  position: absolute; inset: 0;
  overflow: hidden;
  display: flex; align-items: center; justify-content: center;
  background: var(--bg);
  /* ページめくり・ズーム・パンはすべて JS で処理するのでブラウザには渡さない */
  touch-action: none;
}
#app[data-zoomed="1"] #stage { cursor: grab; }
#stage.grabbing { cursor: grabbing; }

#book-pan {
  transform-origin: 50% 50%;
  will-change: transform;
}
#book { width: 100%; }

/* page-flip の vendor CSS は .stf__parent に touch-action: pan-y を付けており、
   斜め／速いフリックをブラウザが取り込んでめくり判定が不安定になる。
   ジェスチャーは全て JS 側で処理するので none で上書きする。 */
.stf__parent { touch-action: none; }

.pf-page { background: #fff; overflow: hidden; }
.pf-ph {
  width: 100%; height: 100%;
  display: flex; align-items: center; justify-content: center;
  color: #c9c9c9; font-size: 15px; background: #fff;
}
.pf-canvas { display: block; width: 100%; height: 100%; }

.pf-hl {
  position: absolute;
  background: rgba(255, 210, 0, .40);
  border-radius: 1px;
  pointer-events: none;
}
.pf-hl.is-active { background: rgba(77, 163, 255, .55); }

/* ---------------------------------------------------------------- 下部ツールバー */
#toolbar {
  flex: 0 0 auto;
  min-height: var(--tb-h);
  display: flex;
  flex-wrap: nowrap;
  align-items: center;
  justify-content: space-between;
  gap: 6px;
  padding: 6px 10px;
  padding-bottom: calc(6px + env(safe-area-inset-bottom));
  background: var(--bg-bar);
  border-top: 1px solid var(--border);
  user-select: none;
  transition: padding-left .2s ease;
  z-index: 10;
}

/* グループは縮ませない（縮むとボタンが重なる）。入り切らないときは JS が
   .is-wrapped を付けて2段レイアウトへ切り替える。 */
.tb-group { display: flex; align-items: center; gap: 4px; flex: 0 0 auto; }
.tb-center { gap: 6px; }

/* 1行に収まらないとき：1段目=[一覧][ページ送り]、2段目=[ズーム・検索・全画面・PDF] */
#toolbar.is-wrapped {
  flex-wrap: wrap;
  justify-content: center;
  row-gap: 6px;
}
#toolbar.is-wrapped .tb-left   { order: 1; }
#toolbar.is-wrapped .tb-center { order: 2; }
#toolbar.is-wrapped .tb-right  { order: 3; flex: 1 1 100%; justify-content: center; }

.tb-btn {
  appearance: none;
  border: 0;
  background: transparent;
  color: var(--fg);
  min-width: 46px;
  height: 46px;
  padding: 0 10px;
  border-radius: 10px;
  font-size: 14px;
  font-weight: 600;
  line-height: 1;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  text-decoration: none;
  font-variant-numeric: tabular-nums;
  transition: background-color .12s ease;
}
.tb-btn svg { width: 24px; height: 24px; display: block; }
.tb-btn:hover { background: var(--btn-hover); }
.tb-btn:active { background: var(--btn-active); }
.tb-btn:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; }
.tb-btn[disabled] { opacity: .32; pointer-events: none; }
.tb-btn[aria-pressed="true"] { background: var(--btn-active); color: var(--accent); }
.tb-btn--wide { min-width: 58px; }

.tb-sep { width: 1px; height: 26px; background: var(--border); margin: 0 4px; }

.page-indicator {
  display: inline-flex; align-items: center; gap: 6px;
  font-size: 15px; color: var(--fg-dim); font-variant-numeric: tabular-nums;
  padding: 0 2px;
}
#pageNum {
  width: 54px; height: 38px; text-align: center;
  background: var(--bg); color: var(--fg);
  border: 1px solid var(--border); border-radius: 8px; font-size: 15px;
  -moz-appearance: textfield;
}
#pageNum::-webkit-outer-spin-button,
#pageNum::-webkit-inner-spin-button { -webkit-appearance: none; margin: 0; }
#pageCount { font-size: 15px; }

/* ------------------------------------------------------------------ 検索バー（バー直上）*/
#searchBar {
  flex: 0 0 auto;
  display: flex; align-items: center; gap: 8px;
  padding: 8px 12px;
  background: var(--bg-bar-2);
  border-top: 1px solid var(--border);
  z-index: 10;
}
#searchInput {
  flex: 1 1 auto; max-width: 420px; height: 38px; padding: 0 12px;
  background: var(--bg); color: var(--fg);
  border: 1px solid var(--border); border-radius: 8px; font-size: 15px;
}
#searchInput:focus-visible { outline: 2px solid var(--accent); }
.search-status {
  font-size: 13px; color: var(--fg-dim);
  min-width: 78px; text-align: right; font-variant-numeric: tabular-nums;
}
#searchBar .tb-btn { min-width: 40px; height: 40px; }

/* ------------------------------------------------------------ サムネイル（左サイドバー）*/
/* 下部の帯（検索バー＋ツールバー）には重ねない。--bottom-stack は viewer.js が
   その実高さを設定する（ツールバーが2段になっても隠さない）。 */
#thumbnailPanel {
  position: absolute;
  left: 0; top: 0;
  bottom: var(--bottom-stack, var(--tb-h));
  width: var(--thumb-w);
  background: var(--bg-bar-2);
  border-right: 1px solid var(--border);
  overflow-y: auto; overflow-x: hidden;
  z-index: 30;
  transform: translateX(-100%);
  visibility: hidden;
  transition: transform .2s ease, visibility 0s linear .2s;
  box-shadow: var(--shadow);
  overscroll-behavior: contain;
}
#app[data-thumbs="open"] #thumbnailPanel {
  transform: translateX(0); visibility: visible;
  transition: transform .2s ease, visibility 0s;
}
#thumbnailList {
  list-style: none; margin: 0; padding: 10px;
  display: flex; flex-direction: column; gap: 10px;
}
.thumb { margin: 0; }
.thumb button {
  display: block; width: 100%; padding: 6px;
  background: transparent; border: 2px solid transparent; border-radius: 8px;
  cursor: pointer; color: var(--fg-dim);
}
.thumb button:hover { background: var(--btn-hover); }
.thumb button:focus-visible { outline: 2px solid var(--accent); }
.thumb.is-current button { border-color: var(--accent); color: var(--fg); }
.thumb-img {
  min-height: 56px; background: #333; border-radius: 4px;
  display: flex; align-items: center; justify-content: center; overflow: hidden;
}
.thumb-img canvas { display: block; border-radius: 3px; }
.thumb-label {
  display: block; text-align: center; font-size: 11px; margin-top: 4px;
  font-variant-numeric: tabular-nums;
}

#thumbScrim {
  position: absolute;
  top: 0; left: 0; right: 0;
  bottom: var(--bottom-stack, var(--tb-h));   /* 下部の帯は覆わない＝操作可能なまま */
  z-index: 25;
  background: rgba(0, 0, 0, .45);
}

/* 広い画面：サイドバーとしてコンテンツを右へ寄せる（本がサイドバーに潜らない）。
   下部バーは全幅のまま（サイドバーが上で止まるので隠れない）。*/
@media (min-width: 1024px) {
  #app[data-thumbs="open"] #body { margin-left: var(--thumb-w); }
  #app[data-thumbs="open"] #thumbnailPanel { box-shadow: none; }
  #thumbScrim { display: none !important; }
}

/* ------------------------------------------------------------------ ローディング */
#loading {
  position: absolute; inset: 0; z-index: 50;
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  gap: 16px; background: rgba(24, 24, 27, .96); color: var(--fg); font-size: 14px;
}
.spinner {
  width: 44px; height: 44px;
  border: 5px solid rgba(255, 255, 255, .18); border-top-color: var(--accent);
  border-radius: 50%; animation: spin .8s linear infinite;
}
#loading.is-error .spinner { display: none; }
#loading.is-error #loadingText { max-width: 320px; text-align: center; line-height: 1.6; }
.progress { width: 220px; height: 4px; background: rgba(255, 255, 255, .14); border-radius: 2px; overflow: hidden; }
#progressBar { width: 0; height: 100%; background: var(--accent); transition: width .2s ease; }
@keyframes spin { to { transform: rotate(360deg); } }

@media (prefers-reduced-motion: reduce) {
  .spinner { animation-duration: 1.6s; }
  #stage, #thumbnailPanel, #toolbar { transition: none; }
}

/* ------------------------------------------------------------------ 狭い画面 */
/* JS(fitToolbar)の保険＋ボタンを少し詰めて2段に収める。 */
@media (max-width: 560px) {
  #toolbar { flex-wrap: wrap; justify-content: center; row-gap: 6px; padding: 6px 4px; gap: 3px; }
  .tb-left   { order: 1; }
  .tb-center { order: 2; gap: 3px; }
  .tb-right  { order: 3; flex: 1 1 100%; justify-content: center; }
  .tb-btn { min-width: 42px; height: 44px; padding: 0 5px; }
  .tb-btn--wide { min-width: 46px; }
  .tb-btn svg { width: 22px; height: 22px; }
  #pageNum { width: 44px; height: 36px; }
  .page-indicator { font-size: 14px; gap: 4px; }
  .tb-sep { display: none; }
}

/* 横向きスマホなど画面の低いとき：バーを薄く */
@media (orientation: landscape) and (max-height: 480px) {
  :root { --tb-h: 48px; }
  .tb-btn { height: 40px; min-width: 40px; }
  .tb-btn svg { width: 21px; height: 21px; }
  #pageNum { height: 34px; }
}
