:root {
  --bg: #f4f7f5;
  --surface: #ffffff;
  --surface-alt: #f8faf9;
  --text: #1b262c;
  --muted: #5b6973;
  --primary: #2d6a4f;
  --primary-hover: #245740;
  --primary-soft: #e6f4ea;
  --accent: #e09f3e;
  --accent-soft: #fff6e5;
  --info: #1d4ed8;
  --info-soft: #e0ecff;
  --danger: #c1121f;
  --danger-soft: #fde8e8;
  --border: #d9e2ec;
  --radius: 14px;
  --shadow: 0 2px 10px rgba(27, 38, 44, 0.06);
  --tab-height: 64px;
}

* {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
  padding: 0;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Hiragino Kaku Gothic ProN", "Yu Gothic UI", "Meiryo", sans-serif;
  line-height: 1.5;
  -webkit-tap-highlight-color: transparent;
  overflow-x: hidden;
}

:focus-visible {
  outline: 2.5px solid var(--primary);
  outline-offset: 2px;
}

button,
input,
select,
textarea {
  font: inherit;
}

button {
  cursor: pointer;
}

button:disabled {
  opacity: 0.55;
  cursor: not-allowed;
}

.app-shell {
  max-width: 720px;
  margin: 0 auto;
  min-height: 100vh;
  background: var(--surface);
  display: flex;
  flex-direction: column;
  box-shadow: 0 0 24px rgba(0, 0, 0, 0.04);
}

/* 認証・権限エラー画面 */
.auth-screen {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px 16px;
}

.auth-card {
  width: 100%;
  max-width: 420px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 28px 22px;
  box-shadow: var(--shadow);
  text-align: center;
}

.auth-card h1 {
  margin: 0 0 8px;
  font-size: 1.5rem;
  color: var(--primary);
}

.auth-card p {
  margin: 0 0 18px;
  color: var(--muted);
  font-size: 0.94rem;
}

.auth-denied {
  border-color: #f3b5b9;
  background: #fffafa;
}

.auth-denied h1 {
  color: var(--danger);
}

/* 上部固定エリア */
.top-sticky {
  position: sticky;
  top: 0;
  z-index: 20;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  padding: 10px 14px 12px;
}

.header-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-wrap: wrap;
  gap: 8px;
  margin-bottom: 10px;
}

.app-title {
  margin: 0;
  font-size: 1.12rem;
  color: var(--primary);
  font-weight: 800;
  white-space: nowrap;
}

.header-actions {
  display: flex;
  align-items: center;
  gap: 6px;
  flex-wrap: wrap;
  max-width: 100%;
}

.user-chip {
  font-size: 0.76rem;
  color: var(--muted);
  background: var(--surface-alt);
  border: 1px solid var(--border);
  padding: 3px 8px;
  border-radius: 999px;
  max-width: 120px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

/* 子ども切替バー */
.child-bar {
  display: flex;
  gap: 8px;
  overflow-x: auto;
  padding-bottom: 4px;
  margin-bottom: 8px;
  scrollbar-width: none;
}

.child-bar::-webkit-scrollbar {
  display: none;
}

.child-pill {
  flex: 0 0 auto;
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 6px 12px;
  border-radius: 999px;
  border: 1.5px solid var(--border);
  background: var(--surface);
  color: var(--text);
  font-size: 0.88rem;
  font-weight: 600;
  min-height: 36px;
}

.child-pill.active {
  background: var(--primary);
  border-color: var(--primary);
  color: #fff;
}

.child-dot {
  width: 10px;
  height: 10px;
  border-radius: 50%;
  display: inline-block;
  border: 1px solid rgba(0, 0, 0, 0.15);
}

/* 検索バー＋新規登録ボタン */
.search-action-row {
  display: flex;
  gap: 8px;
  align-items: center;
}

.search-box {
  flex: 1;
  position: relative;
  display: flex;
  align-items: center;
}

.search-box input {
  width: 100%;
  min-height: 42px;
  padding: 8px 34px 8px 12px;
  border: 1.5px solid var(--border);
  border-radius: 10px;
  background: var(--surface-alt);
  font-size: 0.95rem;
}

.search-box input:focus {
  outline: none;
  border-color: var(--primary);
  background: #fff;
}

.search-clear-btn {
  position: absolute;
  right: 6px;
  background: transparent;
  border: none;
  color: var(--muted);
  font-size: 1rem;
  padding: 4px 6px;
}

/* ボタン共通 */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  min-height: 42px;
  padding: 8px 14px;
  border-radius: 10px;
  border: 1px solid transparent;
  font-weight: 700;
  font-size: 0.9rem;
  transition: background 0.15s ease;
}

.btn-primary {
  background: var(--primary);
  color: #fff;
}

.btn-primary:hover {
  background: var(--primary-hover);
}

.btn-secondary {
  background: var(--surface);
  color: var(--text);
  border-color: var(--border);
}

.btn-danger {
  background: var(--danger);
  color: #fff;
}

.btn-danger-outline {
  background: var(--danger-soft);
  color: var(--danger);
  border-color: #f3b5b9;
}

.btn-sm {
  min-height: 34px;
  padding: 5px 10px;
  font-size: 0.82rem;
}

.btn-block {
  width: 100%;
}

/* メイン領域 */
.main-content {
  flex: 1;
  padding: 12px 14px calc(var(--tab-height) + env(safe-area-inset-bottom, 0px) + 24px);
}

/* フィルタバー */
.filter-row {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  align-items: center;
  margin-bottom: 8px;
}

.filter-chip {
  padding: 6px 11px;
  border-radius: 999px;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--muted);
  font-size: 0.84rem;
  font-weight: 600;
  min-height: 34px;
}

.filter-chip.active {
  background: var(--primary-soft);
  border-color: var(--primary);
  color: var(--primary);
}

.filter-chip.fav-active {
  background: var(--accent-soft);
  border-color: var(--accent);
  color: #9a5b00;
}

.meta-bar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-size: 0.8rem;
  color: var(--muted);
  margin-bottom: 10px;
}

/* 書籍カード */
.book-list {
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.book-card {
  display: flex;
  gap: 12px;
  padding: 12px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  text-align: left;
  width: 100%;
  transition: border-color 0.15s ease;
}

.book-card:hover {
  border-color: var(--primary);
}

.book-card.is-overdue {
  border-color: #f3b5b9;
  background: #fffdfd;
}

.book-cover {
  width: 64px;
  height: 86px;
  flex: 0 0 64px;
  border-radius: 8px;
  overflow: hidden;
  background: var(--surface-alt);
  border: 1px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: center;
  color: var(--muted);
  font-size: 1.5rem;
}

.book-cover img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.book-body {
  flex: 1;
  min-width: 0;
}

.book-title-row {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 6px;
}

.book-title {
  margin: 0 0 3px;
  font-size: 0.98rem;
  font-weight: 700;
  color: var(--text);
  word-break: break-word;
}

.fav-star {
  color: #d97706;
  font-size: 1.05rem;
  flex: 0 0 auto;
}

.book-authors,
.book-series {
  font-size: 0.8rem;
  color: var(--muted);
  margin: 0 0 3px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.badge-row {
  display: flex;
  flex-wrap: wrap;
  gap: 5px;
  margin-top: 6px;
  align-items: center;
}

.badge {
  display: inline-flex;
  align-items: center;
  padding: 2px 8px;
  border-radius: 999px;
  font-size: 0.74rem;
  font-weight: 700;
}

.badge-borrowing {
  background: var(--info-soft);
  color: var(--info);
}

.badge-read {
  background: var(--primary-soft);
  color: var(--primary);
}

.badge-want {
  background: var(--accent-soft);
  color: #9a5b00;
}

.badge-unread {
  background: #edf2f7;
  color: #4a5568;
}

.badge-overdue {
  background: var(--danger-soft);
  color: var(--danger);
  border: 1px solid #f3b5b9;
}

.badge-count {
  background: #f1f5f9;
  color: #334155;
}

.due-text {
  font-size: 0.76rem;
  color: var(--muted);
}

.due-text.overdue-text {
  color: var(--danger);
  font-weight: 700;
}

.empty-state {
  text-align: center;
  padding: 36px 16px;
  color: var(--muted);
  background: var(--surface-alt);
  border: 1px dashed var(--border);
  border-radius: var(--radius);
}

/* 下部4タブナビゲーション */
.bottom-nav {
  position: fixed;
  bottom: 0;
  left: 50%;
  transform: translateX(-50%);
  width: 100%;
  max-width: 720px;
  min-height: var(--tab-height);
  background: var(--surface);
  border-top: 1px solid var(--border);
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  z-index: 30;
  padding-bottom: env(safe-area-inset-bottom, 0px);
  box-shadow: 0 -2px 12px rgba(27, 38, 44, 0.06);
}

.nav-tab {
  border: none;
  border-top: 2.5px solid transparent;
  background: transparent;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 2px;
  min-height: 56px;
  padding: 6px 4px;
  color: var(--muted);
  font-size: 0.72rem;
  font-weight: 600;
}

.nav-tab .nav-icon {
  font-size: 1.2rem;
  line-height: 1;
}

.nav-tab.active {
  color: var(--primary);
  border-top-color: var(--primary);
  font-weight: 800;
}

/* モーダル・シート */
.modal-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(15, 23, 42, 0.48);
  z-index: 50;
  display: flex;
  align-items: flex-end;
  justify-content: center;
}

@media (min-width: 640px) {
  .modal-backdrop {
    align-items: center;
    padding: 20px;
  }
}

.modal-sheet {
  width: 100%;
  max-width: 640px;
  max-height: 92vh;
  overflow-y: auto;
  background: var(--surface);
  border-radius: 18px 18px 0 0;
  padding: 14px 16px calc(28px + env(safe-area-inset-bottom, 0px));
  box-shadow: 0 -4px 24px rgba(0, 0, 0, 0.16);
}

.modal-sheet::before {
  content: "";
  display: block;
  width: 40px;
  height: 4px;
  border-radius: 999px;
  background: var(--border);
  margin: 0 auto 10px;
}

@media (min-width: 640px) {
  .modal-sheet {
    border-radius: 16px;
    padding-top: 18px;
  }

  .modal-sheet::before {
    display: none;
  }
}

.modal-header {
  position: sticky;
  top: -14px;
  z-index: 5;
  background: var(--surface);
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  margin: 0 -16px 14px;
  padding: 10px 16px;
  border-bottom: 1px solid var(--border);
}

.modal-header h2 {
  margin: 0;
  font-size: 1.1rem;
}

.section-card {
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 12px;
  margin-bottom: 12px;
  background: var(--surface-alt);
}

.section-card h3 {
  margin: 0 0 8px;
  font-size: 0.94rem;
}

.form-group {
  margin-bottom: 12px;
}

.form-group label {
  display: block;
  font-size: 0.84rem;
  font-weight: 700;
  margin-bottom: 4px;
}

.form-group input,
.form-group select,
.form-group textarea {
  width: 100%;
  padding: 9px 11px;
  border: 1.5px solid var(--border);
  border-radius: 9px;
  background: #fff;
}

.form-row-2 {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
}

.field-error {
  color: var(--danger);
  font-size: 0.8rem;
  font-weight: 700;
  margin-top: 4px;
}

.alert-box {
  padding: 10px 12px;
  border-radius: 10px;
  font-size: 0.86rem;
  margin-bottom: 12px;
}

.alert-error {
  background: var(--danger-soft);
  color: var(--danger);
  border: 1px solid #f3b5b9;
}

.alert-info {
  background: var(--info-soft);
  color: var(--info);
  border: 1px solid #bfdbfe;
}

.alert-warn {
  background: var(--accent-soft);
  color: #92400e;
  border: 1px solid #fcd34d;
}

.child-reaction-card {
  background: #fff;
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 10px 12px;
  margin-bottom: 8px;
}

.child-reaction-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  margin-bottom: 8px;
}

.toggle-group {
  display: flex;
  gap: 6px;
}

/* 新規登録モーダル内の切替タブ・カメラ枠・検索候補 */
.subtab-bar {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 6px;
  margin-bottom: 12px;
}

.subtab-btn {
  padding: 8px 6px;
  border-radius: 10px;
  border: 1.5px solid var(--border);
  background: var(--surface-alt);
  color: var(--muted);
  font-size: 0.82rem;
  font-weight: 700;
}

.subtab-btn.active {
  background: var(--primary-soft);
  border-color: var(--primary);
  color: var(--primary);
}

.camera-viewport {
  width: 100%;
  min-height: 200px;
  max-height: 280px;
  background: #111827;
  border-radius: 12px;
  overflow: hidden;
  display: flex;
  align-items: center;
  justify-content: center;
  color: #cbd5e1;
  margin-bottom: 10px;
}

.camera-viewport video {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.candidate-list {
  display: flex;
  flex-direction: column;
  gap: 8px;
  margin-top: 10px;
}

.candidate-card {
  display: flex;
  gap: 10px;
  padding: 10px;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: #fff;
  text-align: left;
  width: 100%;
}

.candidate-card:hover {
  border-color: var(--primary);
}

/* シリーズ・作者・おすすめタブ用カード */
.group-list {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.group-card {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  padding: 12px 14px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
  box-shadow: var(--shadow);
  width: 100%;
  text-align: left;
}

.group-card:hover {
  border-color: var(--primary);
}

.work-card {
  display: flex;
  gap: 12px;
  padding: 12px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
}

.work-card-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  margin-top: 8px;
}

.back-bar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  margin-bottom: 10px;
}

/* 設定・ゴミ箱・図書館照会 */
.settings-tabs {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 5px;
  margin-bottom: 12px;
}

.item-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-wrap: wrap;
  gap: 8px;
  padding: 10px 12px;
  background: #fff;
  border: 1px solid var(--border);
  border-radius: 10px;
  margin-bottom: 8px;
}

.item-row > div:first-child {
  min-width: 0;
  flex: 1 1 170px;
  word-break: break-word;
}

.item-row-actions {
  display: flex;
  gap: 6px;
  flex-wrap: wrap;
}

.library-result-card {
  background: #fff;
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 10px 12px;
  margin-bottom: 8px;
}

.badge-available {
  background: var(--primary-soft);
  color: var(--primary);
}

.badge-checked_out {
  background: var(--accent-soft);
  color: #92400e;
}

.badge-none,
.badge-fallback {
  background: #edf2f7;
  color: #4a5568;
}

/* トースト */
.toast {
  position: fixed;
  left: 50%;
  bottom: calc(var(--tab-height) + 14px);
  transform: translateX(-50%) translateY(20px);
  background: #1f2933;
  color: #fff;
  padding: 10px 16px;
  border-radius: 999px;
  font-size: 0.86rem;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.2s ease, transform 0.2s ease;
  z-index: 80;
  max-width: 90vw;
  text-align: center;
}

.toast.show {
  opacity: 1;
  transform: translateX(-50%) translateY(0);
}

/* スマホ片手操作（375px 幅含む狭幅画面）の最適化 */
@media (max-width: 440px) {
  .top-sticky {
    padding: 8px 12px 10px;
  }

  .main-content {
    padding: 10px 12px calc(var(--tab-height) + env(safe-area-inset-bottom, 0px) + 24px);
  }

  .form-row-2 {
    grid-template-columns: 1fr;
  }

  .settings-tabs {
    grid-template-columns: repeat(2, 1fr);
  }

  .subtab-btn {
    min-height: 40px;
    font-size: 0.78rem;
    padding: 7px 6px;
  }

  .user-chip {
    max-width: 96px;
  }

  .book-card {
    padding: 10px;
    gap: 10px;
  }

  .book-cover {
    width: 56px;
    height: 76px;
    flex: 0 0 56px;
  }
}

/* PC・タブレット幅（1280×800 等）での見やすい中央レイアウト */
@media (min-width: 900px) {
  body {
    padding: 0 16px;
  }

  .app-shell {
    max-width: 760px;
    border-left: 1px solid var(--border);
    border-right: 1px solid var(--border);
  }

  .bottom-nav {
    max-width: 760px;
  }

  .user-chip {
    max-width: 200px;
  }
}
