/* 色はここで決める。ダークモードは既定の藍の地のまま（机の色が基調） */
:root {
  --desk: #222c3e;
  --desk-deep: #151c2a;
  --paper: #f2ead8;
  --ink: #3a3c34;
  --accent: rgb(200, 60, 45);
  --accent-ink: #fff8f0;
  --line: rgba(255, 255, 255, 0.18);
  --gutter: 16px;
  --round: "Hiragino Maru Gothic ProN", "Zen Maru Gothic", "Kosugi Maru", "M PLUS Rounded 1c", "Yu Gothic UI", sans-serif;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --desk: #1a2232;
    --desk-deep: #0e131e;
    --paper: #e9e0cc;
    --ink: #33352d;
  }
}

:root[data-theme="dark"] {
  --desk: #1a2232;
  --desk-deep: #0e131e;
  --paper: #e9e0cc;
  --ink: #33352d;
}

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

html, body {
  height: 100%;
  margin: 0;
}

body {
  background: var(--desk);
  color: var(--paper);
  font-family: var(--round);
  overflow: hidden;
  overscroll-behavior: none;
  -webkit-user-select: none;
  user-select: none;
  -webkit-touch-callout: none;
}

[hidden] {
  display: none !important;
}

button {
  font: inherit;
  color: inherit;
  border: 0;
  background: none;
  cursor: pointer;
  touch-action: manipulation;
}

svg {
  width: 1em;
  height: 1em;
  flex: none;
}

/* ---------- 本棚 ---------- */

.shelf {
  position: fixed;
  inset: 0;
  overflow-y: auto;
  padding: calc(env(safe-area-inset-top) + 24px) var(--gutter) calc(env(safe-area-inset-bottom) + 32px);
  background:
    radial-gradient(120% 80% at 50% 0%, #2c3850 0%, var(--desk) 55%, var(--desk-deep) 100%);
}

.shelf-head h1 {
  margin: 8px 4px 20px;
  font-size: 26px;
  font-weight: 700;
  letter-spacing: 0.04em;
}

.shelf-list {
  list-style: none;
  margin: 0;
  padding: 0;
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(150px, 1fr));
  gap: 18px;
  max-width: 980px;
  margin-inline: auto;
}

/* 1冊だけのときは最大幅 360px の左寄せ */
.shelf-list.one {
  grid-template-columns: minmax(0, 360px);
  margin-inline: 0;
}

.card .sub {
  margin-top: 3px;
  font-size: 13px;
  line-height: 1.4;
  opacity: 0.75;
}

.card {
  display: flex;
  flex-direction: column;
  width: 100%;
  padding: 12px 12px 14px;
  border-radius: 18px;
  background: var(--paper);
  color: var(--ink);
  text-align: left;
  box-shadow: 0 6px 18px rgba(0, 0, 0, 0.28);
  min-height: 56px;
}

.card:active {
  transform: scale(0.98);
}

.card img {
  width: 100%;
  aspect-ratio: 1 / 1;
  object-fit: cover;
  border-radius: 12px;
  background: #e2d8c0;
  display: block;
}

.card .title {
  margin-top: 10px;
  font-size: 17px;
  font-weight: 700;
  line-height: 1.4;
}

.card .go {
  margin-top: 8px;
  align-self: flex-start;
  padding: 6px 16px;
  border-radius: 999px;
  background: var(--accent);
  color: var(--accent-ink);
  font-size: 15px;
  font-weight: 700;
}

.msg {
  margin: 24px 4px;
  font-size: 16px;
  opacity: 0.85;
}

/* ---------- 読む画面 ---------- */

.reader {
  position: fixed;
  inset: 0;
  overflow: hidden;
}

.book {
  position: absolute;
  inset: 0;
  overflow: hidden;
  touch-action: none;
  overscroll-behavior: none;
  background:
    radial-gradient(90% 70% at 50% 45%, rgba(0, 0, 0, 0) 0%, rgba(0, 0, 0, 0.35) 100%),
    var(--desk);
}

.stage {
  position: absolute;
  inset: 0;
  pointer-events: none;
}

/* 面（床・壁・ピース・ボード）は絶対配置で matrix3d を当てる */
.face {
  position: absolute;
  left: 0;
  top: 0;
  transform-origin: 0 0;
  backface-visibility: visible;
  display: none;
  pointer-events: none;
}

.face > .pic {
  position: absolute;
  left: 0;
  top: 0;
  width: 100%;
  height: 100%;
  display: block;
  pointer-events: none;
}

.face.board {
  border-radius: 0;
}

.face.ovl {
  overflow: hidden;
}

.face .sh {
  position: absolute;
  left: 0;
  top: 0;
  transform-origin: 0 0;
  pointer-events: none;
  max-width: none;
}

/* 本文 */
.face .txt {
  position: absolute;
  inset: 0;
  pointer-events: none;
}

.face .txt .line {
  position: absolute;
  left: 0;
  width: 100%;
  text-align: center;
  line-height: 1;
  white-space: nowrap;
  font-family: "Hiragino Maru Gothic ProN", "Zen Maru Gothic", "Kosugi Maru", "M PLUS Rounded 1c", "Yu Gothic UI", sans-serif;
  font-weight: 700;
}

.face .txt .line span {
  display: inline-block;
  white-space: pre;
}

/* 上部の操作 */
.top {
  position: absolute;
  left: 0;
  right: 0;
  top: 0;
  z-index: 1000;
  display: flex;
  justify-content: space-between;
  gap: 10px;
  padding: calc(env(safe-area-inset-top) + 10px) var(--gutter) 0;
  pointer-events: none;
}

.pill {
  pointer-events: auto;
  display: inline-flex;
  align-items: center;
  gap: 8px;
  min-height: 52px;
  padding: 0 18px;
  border-radius: 999px;
  background: rgba(20, 26, 40, 0.72);
  color: var(--paper);
  font-size: 17px;
  font-weight: 700;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.3);
  backdrop-filter: blur(6px);
  -webkit-backdrop-filter: blur(6px);
}

.pill[aria-pressed="false"] {
  background: rgba(20, 26, 40, 0.72);
  color: #c9bfae;
}

.pill[aria-pressed="true"] {
  background: var(--accent);
  color: var(--accent-ink);
}

.pill svg {
  width: 22px;
  height: 22px;
}

.pill b {
  font-weight: 700;
}

/* ページの点 */
.dots {
  position: absolute;
  left: 50%;
  transform: translateX(-50%);
  bottom: calc(env(safe-area-inset-bottom) + 146px);
  display: flex;
  gap: 8px;
  z-index: 1000;
  pointer-events: none;
}

.dots i {
  display: block;
  width: 9px;
  height: 9px;
  border-radius: 50%;
  background: rgba(242, 234, 216, 0.35);
  transition: background 0.2s, transform 0.2s;
}

.dots i.on {
  background: var(--paper);
  transform: scale(1.25);
}

/* 下の大きなボタン */
.bottom {
  position: absolute;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 1000;
  display: flex;
  justify-content: center;
  align-items: stretch;
  gap: 14px;
  padding: 0 var(--gutter) calc(env(safe-area-inset-bottom) + 16px);
  pointer-events: none;
}

.big {
  pointer-events: auto;
  flex: 1 1 0;
  max-width: 300px;
  min-height: 72px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  padding: 0 18px;
  border-radius: 36px;
  background: rgba(20, 26, 40, 0.78);
  color: var(--paper);
  font-size: 22px;
  font-weight: 700;
  letter-spacing: 0.06em;
  box-shadow: 0 6px 16px rgba(0, 0, 0, 0.35);
  backdrop-filter: blur(6px);
  -webkit-backdrop-filter: blur(6px);
  transition: transform 0.12s;
}

.big svg {
  width: 26px;
  height: 26px;
}

.big:active {
  transform: scale(0.96);
}

.big.primary {
  background: var(--accent);
  color: var(--accent-ink);
}

.end {
  display: flex;
  gap: 14px;
  flex: 2 1 auto;
  justify-content: center;
  max-width: 640px;
}

.end .big {
  max-width: 300px;
}

/* 読み込み中 */
.loading {
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  z-index: 1100;
  width: min(260px, 70vw);
  text-align: center;
  font-size: 17px;
  font-weight: 700;
}

.loading .bar {
  margin-top: 14px;
  height: 10px;
  border-radius: 6px;
  background: rgba(242, 234, 216, 0.2);
  overflow: hidden;
}

.loading #loading-bar {
  height: 100%;
  width: 0;
  background: var(--accent);
  transition: width 0.2s;
}

/* 狭い画面（縦持ち）・横持ちでも、ボタンが本にかぶりすぎないように */
@media (max-height: 560px) {
  .bottom {
    padding-bottom: 8px;
  }
  .big {
    min-height: 56px;
    font-size: 19px;
  }
  .dots {
    bottom: 90px;
  }
}

/* ボタンの文字は折り返さない */
.pill,
.big,
.big span,
.end .big {
  white-space: nowrap;
}

/* 幅の狭い画面（〜379px）: 文字と余白を少し小さくして、上下2つのボタンを1行に収める */
@media (max-width: 379px) {
  :root {
    --gutter: 10px;
  }
  .top {
    gap: 6px;
  }
  .pill {
    gap: 5px;
    padding: 0 12px;
    font-size: 15px;
  }
  .pill svg {
    width: 19px;
    height: 19px;
  }
  .bottom {
    gap: 8px;
  }
  .big {
    padding: 0 10px;
    font-size: 18px;
    gap: 5px;
  }
  .big svg {
    width: 22px;
    height: 22px;
  }
  .end {
    gap: 8px;
  }
}

/* 横長の画面（高さ 600px 未満）: 「もどる」「つぎ」は左右の端の丸いボタン（アイコンだけ）、
   点は下端中央、読み終わりの2つのボタンは中央下に横並び。
   JS の uiMargins() / landscape() と同じ条件にしておく。 */
@media (orientation: landscape) and (max-height: 599px) {
  /* 下部ナビの入れ物を画面全体に広げ、ボタンの top: 50% が画面の中央を指すようにする。
     入れ物は pointer-events: none のまま、ボタンだけ受ける。 */
  .bottom {
    top: 0;
    display: block;
    padding: 0;
  }
  #btn-prev,
  #btn-next {
    position: absolute;
    top: 50%;
    transform: translateY(-50%);
    width: 64px;
    height: 64px;
    min-height: 64px;
    flex: none;
    max-width: none;
    padding: 0;
    border-radius: 50%;
  }
  #btn-prev {
    left: calc(env(safe-area-inset-left) + 12px);
  }
  #btn-next {
    right: calc(env(safe-area-inset-right) + 12px);
  }
  #btn-prev:active,
  #btn-next:active {
    transform: translateY(-50%) scale(0.96);
  }
  .big .lbl {
    display: none;
  }
  .big svg {
    width: 30px;
    height: 30px;
  }
  .end {
    position: absolute;
    left: 50%;
    bottom: calc(env(safe-area-inset-bottom) + 12px);
    transform: translateX(-50%);
    flex: none;
    max-width: none;
    width: max-content;
  }
  .end .big {
    width: auto;
    height: auto;
    min-height: 64px;
    flex: none;
    max-width: none;
    padding: 0 18px;
    border-radius: 36px;
  }
  .end .big .lbl {
    display: inline;
  }
  .dots {
    bottom: calc(env(safe-area-inset-bottom) + 14px);
  }
}
