* {
  box-sizing: border-box;
}

html, body {
  margin: 0;
  padding: 0;
  height: 100%;
  width: 100%;
  background: #2a2a2a;
  color: #fff;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "Hiragino Kaku Gothic ProN",
    "Hiragino Sans", "Meiryo", sans-serif;
  overflow: hidden;
  overscroll-behavior: none;
  -webkit-tap-highlight-color: transparent;
}

#viewer-root {
  position: fixed;
  inset: 0;
  display: flex;
  flex-direction: column;
}

/* === Stage / Book === */

#flipbook-stage {
  flex: 1 1 auto;
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 12px;
  min-height: 0;
  overflow: hidden;
  touch-action: none;
}

#zoom-wrap {
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
  transform-origin: 50% 50%;
  transform: translate(0, 0) scale(1);
  transition: transform 0.18s ease-out;
  will-change: transform;
}

#zoom-wrap.no-anim {
  transition: none;
}

#zoom-wrap.zoomed {
  cursor: grab;
}

#zoom-wrap.panning {
  cursor: grabbing;
}

#flipbook {
  display: block;
  margin: 0 auto;
  max-width: 100%;
  max-height: 100%;
}

#flipbook .page {
  background: #fff;
  overflow: hidden;
}

#flipbook .page img {
  width: 100%;
  height: 100%;
  display: block;
  user-select: none;
  -webkit-user-drag: none;
  pointer-events: none;
}

#loading {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 14px;
  color: #ccc;
  pointer-events: none;
  background: #2a2a2a;
  transition: opacity 0.3s ease;
}

#loading.hidden {
  opacity: 0;
  pointer-events: none;
}

/* === Controls bar === */

#controls {
  flex: 0 0 auto;
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: center;
  gap: 8px 16px;
  padding: 10px 16px;
  background: rgba(0, 0, 0, 0.55);
  backdrop-filter: blur(4px);
}

.ctrl-group {
  display: flex;
  align-items: center;
  gap: 6px;
}

.ctrl-group.slider {
  flex: 1 1 200px;
  min-width: 120px;
  max-width: 380px;
}

#controls button {
  appearance: none;
  background: rgba(255, 255, 255, 0.12);
  color: #fff;
  border: 1px solid rgba(255, 255, 255, 0.2);
  border-radius: 6px;
  padding: 6px 12px;
  font-size: 16px;
  line-height: 1;
  cursor: pointer;
  min-width: 40px;
  min-height: 36px;
  transition: background 0.15s;
  font-family: inherit;
}

#controls button:hover {
  background: rgba(255, 255, 255, 0.22);
}

#controls button:active {
  background: rgba(255, 255, 255, 0.32);
}

#controls button:disabled {
  opacity: 0.35;
  cursor: not-allowed;
}

/* === Download PDF menu === */

.download-wrap {
  position: relative;
  display: inline-flex;
}

#download-menu {
  display: none;
  position: absolute;
  bottom: calc(100% + 8px);
  left: 50%;
  transform: translateX(-50%);
  flex-direction: column;
  gap: 4px;
  min-width: 120px;
  padding: 6px;
  background: rgba(20, 20, 20, 0.95);
  border: 1px solid rgba(255, 255, 255, 0.2);
  border-radius: 8px;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.45);
  z-index: 50;
}

#download-menu.open {
  display: flex;
}

#download-menu button {
  appearance: none;
  width: 100%;
  min-width: 0;
  text-align: left;
  background: rgba(255, 255, 255, 0.1);
  color: #fff;
  border: 1px solid transparent;
  border-radius: 6px;
  padding: 8px 10px;
  font-size: 13px;
  line-height: 1.2;
  cursor: pointer;
  font-family: inherit;
  white-space: nowrap;
}

#download-menu button:hover:not(:disabled) {
  background: rgba(255, 255, 255, 0.22);
}

#download-menu button:disabled {
  opacity: 0.35;
  cursor: not-allowed;
}

#page-indicator {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  font-size: 14px;
  font-variant-numeric: tabular-nums;
  padding: 4px 8px;
  background: rgba(255, 255, 255, 0.06);
  border: 1px solid rgba(255, 255, 255, 0.12);
  border-radius: 6px;
}

#page-input {
  width: 3.4em;
  background: transparent;
  border: none;
  color: #fff;
  font: inherit;
  font-variant-numeric: tabular-nums;
  text-align: right;
  padding: 2px 0;
  outline: none;
  -moz-appearance: textfield;
}

#page-input::-webkit-outer-spin-button,
#page-input::-webkit-inner-spin-button {
  -webkit-appearance: none;
  margin: 0;
}

#page-input:focus {
  background: rgba(255, 255, 255, 0.12);
  border-radius: 3px;
}

#page-total {
  opacity: 0.85;
}

#page-slider {
  width: 100%;
  appearance: none;
  background: transparent;
  cursor: pointer;
  height: 28px;
}

#page-slider::-webkit-slider-runnable-track {
  height: 4px;
  background: rgba(255, 255, 255, 0.25);
  border-radius: 2px;
}

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

#page-slider::-webkit-slider-thumb {
  appearance: none;
  width: 16px;
  height: 16px;
  border-radius: 50%;
  background: #fff;
  margin-top: -6px;
  border: 1px solid rgba(0, 0, 0, 0.25);
}

#page-slider::-moz-range-thumb {
  width: 16px;
  height: 16px;
  border-radius: 50%;
  background: #fff;
  border: 1px solid rgba(0, 0, 0, 0.25);
}

#zoom-level {
  display: inline-block;
  min-width: 3.6em;
  text-align: center;
  font-size: 13px;
  font-variant-numeric: tabular-nums;
}

/* === Side toggle (thumbnails) floating button === */

.side-toggle {
  position: absolute;
  top: 12px;
  right: 12px;
  z-index: 10;
  appearance: none;
  background: rgba(0, 0, 0, 0.55);
  color: #fff;
  border: 1px solid rgba(255, 255, 255, 0.18);
  border-radius: 6px;
  padding: 8px 12px;
  font-size: 18px;
  line-height: 1;
  cursor: pointer;
  backdrop-filter: blur(4px);
}

.side-toggle:hover {
  background: rgba(0, 0, 0, 0.75);
}

/* === Thumbnail panel === */

#thumb-panel {
  position: fixed;
  top: 0;
  right: 0;
  width: 280px;
  max-width: 88vw;
  height: 100%;
  background: #1e1e1e;
  border-left: 1px solid rgba(255, 255, 255, 0.1);
  transform: translateX(100%);
  transition: transform 0.25s ease;
  z-index: 30;
  display: flex;
  flex-direction: column;
}

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

.thumb-panel-head {
  flex: 0 0 auto;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 14px;
  border-bottom: 1px solid rgba(255, 255, 255, 0.08);
  font-size: 14px;
  font-weight: 600;
}

.thumb-panel-head button {
  appearance: none;
  background: transparent;
  border: none;
  color: #fff;
  font-size: 22px;
  line-height: 1;
  cursor: pointer;
  padding: 4px 8px;
}

.thumb-list {
  flex: 1 1 auto;
  overflow-y: auto;
  padding: 14px 14px 18px;
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 18px 14px;
  align-content: start;
}

.thumb {
  background: transparent;
  border: none;
  cursor: pointer;
  position: relative;
  display: flex;
  flex-direction: column;
  align-items: stretch;
  padding: 0;
  transition: transform 0.1s ease;
}

.thumb:hover {
  transform: translateY(-1px);
}

.thumb-img {
  position: relative;
  width: 100%;
  aspect-ratio: var(--thumb-aspect, 0.71);
  background: #fff;
  border-radius: 3px;
  overflow: hidden;
  box-shadow: 0 2px 6px rgba(0, 0, 0, 0.5),
    0 0 0 1px rgba(0, 0, 0, 0.35);
  transition: box-shadow 0.15s ease;
}

.thumb:hover .thumb-img {
  box-shadow: 0 5px 12px rgba(0, 0, 0, 0.6),
    0 0 0 1px rgba(0, 0, 0, 0.35);
}

.thumb.current .thumb-img {
  box-shadow: 0 0 0 2px #4ea1ff,
    0 4px 10px rgba(0, 0, 0, 0.55);
}

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

.thumb-num {
  display: block;
  margin-top: 6px;
  font-size: 11px;
  line-height: 1;
  text-align: center;
  color: #bdbdbd;
  font-variant-numeric: tabular-nums;
  letter-spacing: 0.02em;
  pointer-events: none;
}

.thumb.current .thumb-num {
  color: #4ea1ff;
  font-weight: 600;
}

#thumb-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.45);
  z-index: 20;
}

/* === Toast === */

#toast {
  position: absolute;
  bottom: 14px;
  left: 50%;
  transform: translate(-50%, 30px);
  background: rgba(0, 0, 0, 0.8);
  color: #fff;
  font-size: 13px;
  padding: 8px 14px;
  border-radius: 6px;
  pointer-events: none;
  opacity: 0;
  transition: opacity 0.2s ease, transform 0.2s ease;
  z-index: 40;
  max-width: 80vw;
  text-align: center;
}

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

/* === Mobile responsive === */

@media (max-width: 600px) {
  #flipbook-stage {
    padding: 6px;
  }

  #controls {
    gap: 6px 10px;
    padding: 8px 8px;
  }

  #controls button {
    padding: 6px 10px;
    font-size: 15px;
    min-width: 36px;
    min-height: 34px;
  }

  .ctrl-group.slider {
    flex-basis: 100%;
    order: 99;
    max-width: none;
  }

  #page-indicator {
    font-size: 13px;
    padding: 3px 6px;
  }

  #page-input {
    width: 2.8em;
  }

  #zoom-level {
    min-width: 3em;
    font-size: 12px;
  }

  .side-toggle {
    top: 8px;
    right: 8px;
    padding: 6px 10px;
  }
}
