:root {
  --bg: #fbfaf7;
  --fg: #1d1d1f;
  --sub: #6b6b70;
  --line: #e4e1da;
  --card: #ffffff;
  --accent: #3a6ea5;
  --bar: rgba(251, 250, 247, .94);
  --scrim: rgba(0, 0, 0, .32);
  --page: #fbfaf7;
}
[data-theme="sepia"] {
  --bg: #f3ead7;
  --fg: #4a3a28;
  --sub: #85735c;
  --line: #e0d3b8;
  --card: #f8f1e2;
  --accent: #8a5a2b;
  --bar: rgba(243, 234, 215, .95);
  --page: #f3ead7;
}
[data-theme="dark"] {
  --bg: #141414;
  --fg: #e3e3e3;
  --sub: #9a9a9f;
  --line: #2c2c2e;
  --card: #1e1e20;
  --accent: #8ab4f8;
  --bar: rgba(28, 28, 30, .94);
  --scrim: rgba(0, 0, 0, .55);
  --page: #141414;
}

* { box-sizing: border-box; }
html, body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: var(--fg);
  font-family: -apple-system, BlinkMacSystemFont, "Hiragino Sans", "Noto Sans JP", "Yu Gothic UI", sans-serif;
  -webkit-text-size-adjust: 100%;
  touch-action: manipulation;
  -webkit-tap-highlight-color: transparent;
}
body { overflow: hidden; }
button { font: inherit; color: inherit; cursor: pointer; }
[hidden] { display: none !important; }

/* ---------- 本棚 ---------- */
#home {
  height: 100dvh;
  overflow-y: auto;
  padding: calc(env(safe-area-inset-top) + 16px) 20px calc(env(safe-area-inset-bottom) + 24px);
  max-width: 960px;
  margin: 0 auto;
  overscroll-behavior-y: contain; /* ブラウザ自体の引っぱり更新は止めて、アプリの更新を使う */
}
.home-head { display: flex; align-items: center; justify-content: space-between; }
.home-head h1 { font-size: 22px; margin: 8px 0; font-weight: 700; letter-spacing: .02em; }
.primary {
  display: block;
  width: 100%;
  margin: 20px 0 8px;
  padding: 16px;
  border: 0;
  border-radius: 14px;
  background: var(--accent);
  color: #fff;
  font-size: 17px;
  font-weight: 600;
}
[data-theme="dark"] .primary { color: #10223a; }

/* 本を開くボタン：控えめなタイル2つ */
.open-row { display: flex; gap: 10px; margin: 14px 0 18px; }
.open-tile {
  flex: 1 1 0;
  min-width: 0;
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 12px 14px;
  border: 1px solid var(--line);
  border-radius: 14px;
  background: var(--card);
  color: var(--fg);
  text-align: left;
  font-size: 14px;
  font-weight: 600;
  line-height: 1.35;
  transition: background .15s, transform .1s;
}
.open-tile:active { transform: scale(.98); background: color-mix(in srgb, var(--accent) 8%, var(--card)); }
.open-tile .ico {
  flex: none;
  width: 36px;
  height: 36px;
  border-radius: 10px;
  display: grid;
  place-items: center;
  background: color-mix(in srgb, var(--accent) 14%, transparent);
  color: var(--accent);
}
.open-tile svg { width: 20px; height: 20px; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; }
.open-tile .lbl { min-width: 0; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
/* スマホ幅：アイコンを上、文字を下にして1行に収める */
@media (max-width: 520px) {
  .open-tile { flex-direction: column; justify-content: center; gap: 7px; padding: 12px 8px 11px; text-align: center; font-size: 13px; }
  .open-tile .ico { width: 34px; height: 34px; }
  .open-tile .lbl { max-width: 100%; }
}

/* 本棚の検索・並べ替え */
#shelf-tools { position: relative; }
.shelf-search {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 0 12px;
  height: 40px;
  border-radius: 12px;
  background: color-mix(in srgb, var(--fg) 6%, transparent);
  color: var(--sub);
}
.shelf-search svg { width: 18px; height: 18px; flex: none; fill: none; stroke: currentColor; stroke-width: 1.9; stroke-linecap: round; }
.shelf-search input {
  flex: 1;
  min-width: 0;
  border: 0;
  outline: 0;
  background: none;
  color: var(--fg);
  font: inherit;
  font-size: 16px; /* iOSで勝手に拡大されないよう16px以上 */
}
.shelf-search input::placeholder { color: var(--sub); }
.shelf-bar { display: flex; align-items: center; gap: 8px; margin: 10px 0 14px; }
.shelf-bar .text-btn { margin-left: auto; padding: 6px 4px 6px 10px; }
.chip {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  height: 32px;
  padding: 0 12px;
  border: 1px solid var(--line);
  border-radius: 999px;
  background: transparent;
  color: var(--fg);
  font-size: 13px;
  white-space: nowrap;
}
.chip svg { width: 15px; height: 15px; flex: none; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }
#saved-only svg { display: none; }
#saved-only[aria-pressed="true"] { background: var(--accent); border-color: var(--accent); color: #fff; }
[data-theme="dark"] #saved-only[aria-pressed="true"] { color: #10223a; }
#saved-only[aria-pressed="true"] svg { display: block; }
.sort-wrap { position: relative; }
.menu {
  position: absolute;
  z-index: 5;
  top: calc(100% + 6px);
  left: 0;
  min-width: 200px;
  padding: 6px;
  border-radius: 14px;
  background: var(--card);
  border: 1px solid var(--line);
  box-shadow: 0 10px 30px rgba(0, 0, 0, .16);
}
.menu button {
  display: flex;
  align-items: center;
  width: 100%;
  border: 0;
  background: none;
  padding: 11px 12px 11px 34px;
  border-radius: 9px;
  font-size: 15px;
  text-align: left;
  position: relative;
}
.menu button:active { background: color-mix(in srgb, var(--accent) 10%, transparent); }
.menu button[aria-checked="true"] { color: var(--accent); font-weight: 600; }
.menu button[aria-checked="true"]::before {
  content: "";
  position: absolute;
  left: 13px;
  top: 50%;
  width: 10px;
  height: 5px;
  border-left: 2px solid currentColor;
  border-bottom: 2px solid currentColor;
  transform: translateY(-70%) rotate(-45deg);
}

/* 本棚：表紙の一覧（スマホ3列・iPad5列ほど） */
.shelf-grid {
  list-style: none;
  margin: 0;
  padding: 0;
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(96px, 1fr));
  gap: 18px 14px;
}
@media (min-width: 600px) { .shelf-grid { grid-template-columns: repeat(auto-fill, minmax(120px, 1fr)); gap: 22px 18px; } }
.shelf-grid li { cursor: pointer; min-width: 0; }
.shelf-grid .cover {
  position: relative;
  aspect-ratio: 2 / 3;
  border-radius: 4px;
  background: var(--line);
  box-shadow: 0 1px 2px rgba(0, 0, 0, .12), 0 4px 12px rgba(0, 0, 0, .08);
  transition: transform .12s;
}
.shelf-grid li:active .cover { transform: scale(.97); }
.shelf-grid img { display: block; width: 100%; height: 100%; object-fit: cover; border-radius: 4px; }
.shelf-grid .ph {
  position: absolute;
  inset: 0;
  border-radius: 4px;
  padding: 12px 10px;
  display: flex;
  flex-direction: column;
  justify-content: center;
  gap: 6px;
  text-align: center;
  background: linear-gradient(160deg, color-mix(in srgb, var(--accent) 20%, var(--card)), color-mix(in srgb, var(--accent) 8%, var(--card)));
  overflow: hidden;
}
.ph-t { font-size: 13px; font-weight: 700; line-height: 1.4; overflow: hidden; display: -webkit-box; -webkit-line-clamp: 5; -webkit-box-orient: vertical; overflow-wrap: anywhere; }
.ph-a { font-size: 10.5px; color: var(--sub); overflow: hidden; white-space: nowrap; text-overflow: ellipsis; }
/* 保存済み（この端末で電波なしでも読める）の印 */
.shelf-grid .ok {
  position: absolute;
  right: -5px;
  bottom: -5px;
  width: 22px;
  height: 22px;
  border-radius: 50%;
  display: grid;
  place-items: center;
  background: var(--accent);
  color: #fff;
  box-shadow: 0 0 0 2px var(--bg);
}
[data-theme="dark"] .shelf-grid .ok { color: #10223a; }
.shelf-grid .ok svg { width: 14px; height: 14px; fill: none; stroke: currentColor; stroke-width: 3; stroke-linecap: round; stroke-linejoin: round; }
/* 編集中 */
.shelf-grid .del, .shelf-grid .confirm { display: none; }
#home.editing .shelf-grid .del {
  display: grid;
  place-items: center;
  position: absolute;
  top: -7px;
  left: -7px;
  width: 24px;
  height: 24px;
  border-radius: 50%;
  background: #3a3a3c;
  color: #fff;
  font-size: 16px;
  line-height: 1;
  box-shadow: 0 0 0 2px var(--bg);
}
#home.editing .shelf-grid li:not(.armed) .cover { animation: wiggle .28s ease-in-out infinite alternate; }
@keyframes wiggle { from { transform: rotate(-.8deg); } to { transform: rotate(.8deg); } }
#home.editing .shelf-grid .del { z-index: 2; }
#home.editing .shelf-grid li.armed .del { background: #c0392b; }
#home.editing .shelf-grid li.armed .confirm {
  display: grid;
  place-items: center;
  position: absolute;
  inset: 0;
  padding: 8px;
  border-radius: 4px;
  background: rgba(192, 57, 43, .88);
  color: #fff;
  font-size: 12.5px;
  font-weight: 700;
  line-height: 1.5;
  text-align: center;
}
.shelf-empty { color: var(--sub); font-size: 14px; line-height: 1.7; text-align: center; margin: 36px 12px; }
.version { text-align: center; color: var(--sub); font-size: 11px; margin-top: 36px; }
@media (prefers-reduced-motion: reduce) { #home.editing .shelf-grid li .cover { animation: none !important; } }

/* ---------- 読書画面 ---------- */
#reader { position: fixed; inset: 0; background: var(--page); }
#viewer { position: absolute; inset: 0; will-change: opacity, transform; }
foliate-view { display: block; width: 100%; height: 100%; }

.bar {
  position: fixed;
  left: 0;
  right: 0;
  z-index: 10;
  background: var(--bar);
  -webkit-backdrop-filter: blur(12px);
  backdrop-filter: blur(12px);
  transition: transform .2s ease, opacity .2s ease;
  opacity: 0;
  pointer-events: none;
}
#topbar {
  top: 0;
  display: flex;
  align-items: center;
  gap: 4px;
  padding: calc(env(safe-area-inset-top) + 6px) 8px 6px;
  border-bottom: 1px solid var(--line);
  transform: translateY(-100%);
}
#bottombar {
  bottom: 0;
  padding: 12px 20px calc(env(safe-area-inset-bottom) + 12px);
  border-top: 1px solid var(--line);
  transform: translateY(100%);
}
#reader.menu .bar { opacity: 1; transform: none; pointer-events: auto; }
#book-title {
  flex: 1;
  min-width: 0;
  text-align: center;
  font-size: 14px;
  font-weight: 600;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.icon-btn {
  width: 44px;
  height: 44px;
  border: 0;
  background: none;
  display: grid;
  place-items: center;
  border-radius: 10px;
}
.icon-btn svg { width: 24px; height: 24px; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; }
#progress { width: 100%; accent-color: var(--accent); }
#progress-label { text-align: center; font-size: 12px; color: var(--sub); margin-top: 4px; }

/* ---------- パネル ---------- */
.scrim { position: fixed; inset: 0; background: var(--scrim); z-index: 20; }
.sheet {
  position: fixed;
  z-index: 21;
  background: var(--card);
  color: var(--fg);
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
}
.sheet.side {
  top: 0;
  bottom: 0;
  right: 0;
  width: min(86vw, 420px);
  padding: calc(env(safe-area-inset-top) + 8px) 0 calc(env(safe-area-inset-bottom) + 16px);
  box-shadow: -8px 0 24px rgba(0, 0, 0, .12);
}
.sheet.bottom {
  left: 0;
  right: 0;
  bottom: 0;
  max-height: 80dvh;
  padding: 8px 20px calc(env(safe-area-inset-bottom) + 20px);
  border-radius: 16px 16px 0 0;
  box-shadow: 0 -8px 24px rgba(0, 0, 0, .12);
  max-width: 640px;
  margin: 0 auto;
}
.sheet-head { display: flex; align-items: center; justify-content: space-between; padding: 6px 16px 10px; }
.sheet.bottom .sheet-head { padding: 6px 0 10px; }
.sheet-head h3 { margin: 0; font-size: 16px; }
.text-btn { border: 0; background: none; color: var(--accent); font-size: 15px; padding: 8px; }

#toc-list ul { list-style: none; margin: 0; padding: 0; }
#toc-list ul ul { padding-left: 16px; }
#toc-list button {
  display: block;
  width: 100%;
  text-align: left;
  border: 0;
  background: none;
  padding: 12px 16px;
  font-size: 15px;
  line-height: 1.5;
  border-bottom: 1px solid var(--line);
}
#toc-list button.current { color: var(--accent); font-weight: 600; }

.setting { display: flex; align-items: center; justify-content: space-between; gap: 12px; padding: 12px 0; border-bottom: 1px solid var(--line); }
.setting > span { font-size: 14px; flex: none; }
.seg { display: flex; background: var(--bg); border: 1px solid var(--line); border-radius: 10px; padding: 2px; flex-wrap: wrap; justify-content: flex-end; }
.seg button { border: 0; background: none; padding: 8px 12px; font-size: 13px; border-radius: 8px; }
.seg button.on { background: var(--accent); color: #fff; }
[data-theme="dark"] .seg button.on { color: #10223a; }
.stepper { display: flex; align-items: center; gap: 8px; }
.stepper button { border: 1px solid var(--line); background: var(--bg); border-radius: 10px; padding: 8px 14px; font-size: 14px; }
.stepper output { min-width: 48px; text-align: center; font-size: 14px; }
.note { color: var(--sub); font-size: 12px; margin: 12px 0 0; }
#settings-panel.fxl .reflow-only { display: none; }
#settings-panel.vertical .horizontal-only { display: none; }
#settings-panel:not(.vertical) .vertical-only,
#settings-panel.fxl .vertical-only { display: none; }
#settings-panel:not(.fxl) .fxl-only { display: none; }
#settings-panel:not(.in-book) .book-only,
#settings-panel:not(.in-book) .reflow-only,
#settings-panel:not(.in-book) .fxl-only,
#settings-panel:not(.in-book) .vertical-only { display: none; }

/* ---------- 読み込み中・お知らせ ---------- */
#loading {
  position: fixed;
  inset: 0;
  z-index: 30;
  display: grid;
  place-content: center;
  justify-items: center;
  background: var(--scrim);
  color: #fff;
}
.spinner { width: 36px; height: 36px; border: 3px solid rgba(255, 255, 255, .35); border-top-color: #fff; border-radius: 50%; animation: spin 1s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }
#toast {
  position: fixed;
  left: 50%;
  bottom: calc(env(safe-area-inset-bottom) + 24px);
  transform: translateX(-50%);
  z-index: 40;
  max-width: 90vw;
  padding: 10px 16px;
  border-radius: 12px;
  background: rgba(30, 30, 32, .92);
  color: #fff;
  font-size: 13px;
  line-height: 1.5;
}
@media (prefers-reduced-motion: reduce) {
  .bar { transition: none; }
  .spinner { animation-duration: 3s; }
}

/* ---------- 試作1.3で追加 ---------- */
#settings-panel.in-book .home-only { display: none; }

/* 検索 */
.search-form { display: flex; gap: 8px; padding: 0 16px 8px; }
.search-form input {
  flex: 1;
  min-width: 0;
  font: inherit;
  font-size: 16px; /* iOSで勝手に拡大されないよう16px以上 */
  padding: 10px 12px;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: var(--bg);
  color: var(--fg);
}
.search-go { border: 0; border-radius: 10px; padding: 0 16px; background: var(--accent); color: #fff; font-size: 15px; }
[data-theme="dark"] .search-go { color: #10223a; }
.search-status { color: var(--sub); font-size: 12px; margin: 0; padding: 4px 16px 8px; }
#search-results { list-style: none; margin: 0; padding: 0; }
#search-results .grp { font-size: 12px; color: var(--sub); padding: 10px 16px 4px; font-weight: 600; }
#search-results button {
  display: block;
  width: 100%;
  text-align: left;
  border: 0;
  border-bottom: 1px solid var(--line);
  background: none;
  padding: 10px 16px;
  font-size: 14px;
  line-height: 1.6;
}
#search-results mark { background: color-mix(in srgb, var(--accent) 28%, transparent); color: inherit; border-radius: 3px; padding: 0 1px; }

/* 他の端末の読書位置の案内 */
#sync-banner {
  position: fixed;
  z-index: 15;
  left: 50%;
  top: calc(env(safe-area-inset-top) + 64px);
  transform: translateX(-50%);
  width: min(92vw, 520px);
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 10px 10px 10px 14px;
  border-radius: 14px;
  background: var(--card);
  color: var(--fg);
  border: 1px solid var(--line);
  box-shadow: 0 6px 20px rgba(0, 0, 0, .15);
  font-size: 13px;
  line-height: 1.5;
}
#sync-text { flex: 1; }
.banner-btn { border: 0; border-radius: 10px; padding: 8px 12px; background: var(--accent); color: #fff; font-size: 13px; flex: none; }
[data-theme="dark"] .banner-btn { color: #10223a; }
.banner-x { border: 0; background: none; color: var(--sub); font-size: 18px; padding: 4px 8px; flex: none; }

/* 読み込み・ダウンロードの進み具合 */
#loading p { margin: 14px 0 0; font-size: 14px; text-align: center; padding: 0 24px; line-height: 1.6; }
#loading-bar { width: min(70vw, 320px); height: 6px; border-radius: 3px; background: rgba(255, 255, 255, .3); margin-top: 12px; overflow: hidden; }
#loading-bar i { display: block; height: 100%; width: 0; background: #fff; transition: width .2s; }

/* アカウント */
.account-box { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; justify-content: flex-end; min-width: 0; }
#account-text { font-size: 12px; color: var(--sub); overflow-wrap: anywhere; }
.small-btn { border: 1px solid var(--line); background: var(--bg); border-radius: 10px; padding: 8px 12px; font-size: 13px; }

/* スマホなど幅の狭い画面 */
@media (max-width: 480px) {
  .setting { flex-wrap: wrap; }
  .setting > span { width: 100%; }
  .seg { width: 100%; justify-content: stretch; }
  .seg button { flex: 1; padding: 8px 6px; }
  #home { padding-left: 16px; padding-right: 16px; }
}

/* ---------- Googleドライブの選択画面 ---------- */
.sheet.full {
  inset: 0;
  display: flex;
  flex-direction: column;
  padding: env(safe-area-inset-top) 0 0;
  overflow: hidden;
}
.drive-head { display: flex; align-items: center; gap: 4px; padding: 6px 8px; border-bottom: 1px solid var(--line); }
.drive-head h3 { flex: 1; margin: 0; font-size: 16px; text-align: center; }
#drive-crumbs {
  display: flex;
  gap: 2px;
  align-items: center;
  overflow-x: auto;
  white-space: nowrap;
  padding: 8px 12px;
  font-size: 13px;
  border-bottom: 1px solid var(--line);
  scrollbar-width: none;
}
#drive-crumbs button { border: 0; background: none; color: var(--accent); padding: 6px 4px; font-size: 13px; flex: none; }
#drive-crumbs button:last-child { color: var(--fg); font-weight: 600; }
#drive-crumbs .sep { color: var(--sub); flex: none; }
.drive-tools { display: flex; gap: 8px; padding: 8px 12px; }
.drive-tools input {
  flex: 1;
  min-width: 0;
  font: inherit;
  font-size: 16px;
  padding: 8px 10px;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: var(--bg);
  color: var(--fg);
}
#drive-list { flex: 1; overflow-y: auto; -webkit-overflow-scrolling: touch; overscroll-behavior: contain; }
.drow {
  display: flex;
  align-items: center;
  gap: 12px;
  width: 100%;
  text-align: left;
  border: 0;
  border-bottom: 1px solid var(--line);
  background: none;
  padding: 12px 14px;
  font-size: 15px;
  line-height: 1.4;
  min-height: 52px;
}
.drow svg { width: 22px; height: 22px; flex: none; fill: none; stroke: currentColor; stroke-width: 1.7; stroke-linejoin: round; }
.drow.folder svg { color: var(--accent); }
.drow .nm { flex: 1; min-width: 0; overflow-wrap: anywhere; }
.drow .sz { font-size: 12px; color: var(--sub); flex: none; }
.drow .chk {
  width: 22px;
  height: 22px;
  border-radius: 50%;
  border: 1.5px solid var(--sub);
  flex: none;
  display: grid;
  place-items: center;
}
.drow.sel { background: color-mix(in srgb, var(--accent) 10%, transparent); }
.drow.sel .chk { background: var(--accent); border-color: var(--accent); }
.drow.sel .chk::after { content: ''; width: 9px; height: 5px; border: 2px solid #fff; border-top: 0; border-right: 0; transform: translateY(-1px) rotate(-45deg); }
.drow .saved { font-size: 11px; padding: 1px 7px; border-radius: 999px; background: color-mix(in srgb, var(--accent) 16%, transparent); color: var(--accent); flex: none; }
.drive-msg { padding: 24px 16px; color: var(--sub); font-size: 14px; text-align: center; line-height: 1.7; }
.drive-foot {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 10px 14px calc(env(safe-area-inset-bottom) + 10px);
  border-top: 1px solid var(--line);
  background: var(--card);
}
#drive-count { flex: 1; font-size: 13px; color: var(--sub); }
.drive-foot .primary { width: auto; margin: 0; padding: 12px 22px; font-size: 15px; }
.drive-foot .primary:disabled { opacity: .4; }

/* ---------- 引っぱって更新 ---------- */
#ptr {
  position: fixed;
  z-index: 8;
  left: 50%;
  top: calc(env(safe-area-inset-top) + 6px);
  width: 40px;
  height: 40px;
  margin-left: -20px;
  border-radius: 50%;
  display: grid;
  place-items: center;
  background: var(--card);
  color: var(--accent);
  border: 1px solid var(--line);
  box-shadow: 0 4px 14px rgba(0, 0, 0, .14);
  transform: translateY(-70px);
  opacity: 0;
  pointer-events: none;
}
#ptr.back { transition: transform .25s, opacity .25s; }
#ptr svg { width: 20px; height: 20px; fill: none; stroke: currentColor; stroke-width: 2.2; stroke-linecap: round; stroke-linejoin: round; transition: transform .2s; }
#ptr.ready svg { transform: rotate(180deg); }
#ptr .ptr-spin { display: none; width: 20px; height: 20px; border-radius: 50%; border: 2.5px solid color-mix(in srgb, var(--accent) 25%, transparent); border-top-color: var(--accent); animation: spin .8s linear infinite; }
#ptr.busy svg { display: none; }
#ptr.busy .ptr-spin { display: block; }
@keyframes spin { to { transform: rotate(360deg); } }
