/* =========================
   styles.css（完全版・重複整理済）
   ========================= */

:root {
  /* ベース色（ライト） */
  --bg: #fcfcfd;
  --text: #0f172a;
  --muted: #64748b;
  --border: #e6e8ee;
  --card: #ffffff;
  --accent: #2563eb;
  --accent-weak: #eef2ff;
  --chip: #f4f6f8;
  --chip-border: #e5e7eb;
  --good: #16a34a;
  --warn: #d97706;
  --bad: #dc2626;
  --left: #0ea5e9;
  --right: #22c55e;

  /* コンポーネント用（ライト） */
  --switch-off-bg: #e5e7eb;
  --switch-on-bg: #c7d2fe;
  --switch-on-bd: #a5b4fc;
  --alert-bg: #fee2e2;
  --alert-fg: #991b1b;
  --alert-bd: #fecaca;

  /* 影・丸み・フォーカス */
  --radius: 12px;
  --radius-sm: 10px;
  --radius-lg: 16px;
  --shadow-sm: 0 1px 2px rgba(0, 0, 0, 0.06);
  --shadow-md: 0 6px 20px rgba(2, 6, 23, 0.08);
  --focus: #3b82f6;

  /* 配列モーダル用カラー */
  --clr-here: #4338ca;
  --bg-here: #ede9fe;
  --bd-here: #c7d2fe;
  --clr-lr: #0369a1;
  --bg-lr: #e0f2fe;
  --bd-lr: #bae6fd;
  --clr-star: #92400e;
  --bg-star: #fef3c7;
  --bd-star: #fde68a;
  --clr-typed: #065f46;
  --bg-typed: #dcfce7;
  --bd-typed: #bbf7d0;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0b1020;
    --text: #e5ecff;
    --muted: #9aa7bd;
    --border: #1f2a44;
    --card: #111833;
    --accent: #3b82f6;
    --accent-weak: #0b1733;
    --chip: #101a30;
    --chip-border: #1f2a44;
    --shadow-sm: 0 1px 2px rgba(0, 0, 0, 0.4);
    --shadow-md: 0 10px 30px rgba(0, 0, 0, 0.45);
    --focus: #60a5fa;
    /* コンポーネント用（ダーク） */
    --switch-off-bg: #1b2744;
    --switch-on-bg: #263269;
    --switch-on-bd: #3a48a0;
    --alert-bg: #2b1515;
    --alert-fg: #fecaca;
    --alert-bd: #7a2a2a;
    /* ハイライトの背景系はダークで少し沈める */
    --bg-here: #1b1d3a;
    --bd-here: #3b3f7a;
    --bg-lr: #0b2433;
    --bd-lr: #164b6b;
    --bg-star: #3a2b0b;
    --bd-star: #6b4b16;
    --bg-typed: #0f2a1b;
    --bd-typed: #1d4d2a;
    /* ダーク用の前景色（明るめにしてコントラスト確保） */
    --clr-here: #c7d2fe;
    /* indigo-200 */
    --clr-lr: #7dd3fc;
    /* sky-300 */
    --clr-star: #fde68a;
    /* amber-200 */
    --clr-typed: #86efac;
    /* green-300 */
  }

  /* 強調バッジのダーク配色（見やすく上書き） */
  .badge.hl-lr {
    background-color: #4a1e28;
    /* 暗い赤系 */
    border-color: #ff8da0;
    box-shadow: 0 0 0 2px rgba(255, 141, 160, 0.18) inset;
    color: var(--text);
  }

  .badge.hl-star {
    background-color: #3a2a00;
    /* 暗い黄土系 */
    border-color: #ffcc4d;
    box-shadow: 0 0 0 2px rgba(255, 204, 77, 0.18) inset;
    color: var(--text);
  }

  .badge.hl-lr.hl-star {
    background-image: linear-gradient(90deg, #4a1e28 0%, #3a2a00 100%);
    border-color: #ff8da0;
    color: var(--text);
  }
}

/* ===== 次のカード入力エリア ===== */
#quickInputArea {
  margin: 12px 0;
  padding: 0;
  background: transparent;
}

.quick-input-header {
  display: flex;
  align-items: center;
  gap: 6px;
  margin-bottom: 8px;
  padding: 0 2px;
}

.quick-input-header strong {
  font-size: 13px;
  font-weight: 600;
  color: var(--text);
  display: flex;
  align-items: center;
  gap: 4px;
}

.quick-input-header strong::before {
  content: "⚡";
  font-size: 14px;
}

.quick-input-grid {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 8px;
}

.quick-input-btn {
  position: relative;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: flex-start;
  gap: 8px;
  padding: 10px;
  background: var(--card);
  border: 2px solid var(--border);
  border-radius: var(--radius-sm);
  cursor: pointer;
  transition: all 0.15s cubic-bezier(0.4, 0, 0.2, 1);
  min-height: auto;
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
  box-shadow: var(--shadow-sm);
  overflow: hidden;
}

.quick-input-btn::before {
  content: "";
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  height: 3px;
  background: var(--border);
  transition: all 0.15s ease;
}

.quick-input-btn:hover {
  transform: translateY(-2px);
  box-shadow: 0 6px 16px rgba(0, 0, 0, 0.1);
  border-color: var(--accent);
}

.quick-input-btn:active {
  transform: translateY(0);
  box-shadow: var(--shadow-sm);
}

.quick-input-btn.side-left {
  border-color: color-mix(in srgb, var(--left) 30%, var(--border));
}

.quick-input-btn.side-left::before {
  background: var(--left);
}

.quick-input-btn.side-left:hover {
  background: color-mix(in srgb, var(--left) 6%, var(--card));
  border-color: var(--left);
}

.quick-input-btn.side-right {
  border-color: color-mix(in srgb, var(--right) 30%, var(--border));
}

.quick-input-btn.side-right::before {
  background: var(--right);
}

.quick-input-btn.side-right:hover {
  background: color-mix(in srgb, var(--right) 6%, var(--card));
  border-color: var(--right);
}

.quick-input-btn .card-info {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 3px;
  width: 100%;
  order: 1;
}

.quick-input-btn .card-number {
  font-size: 13px;
  font-weight: 600;
  font-family: "Courier New", Courier, monospace;
  color: var(--text);
  line-height: 1.2;
  text-align: center;
}

.quick-input-btn .card-side {
  font-size: 9px;
  font-weight: 600;
  padding: 2px 6px;
  border-radius: 3px;
  background: var(--chip);
  color: var(--muted);
  letter-spacing: 0.2px;
}

.quick-input-btn.side-left .card-side {
  background: color-mix(in srgb, var(--left) 12%, var(--card));
  color: var(--left);
}

.quick-input-btn.side-right .card-side {
  background: color-mix(in srgb, var(--right) 12%, var(--card));
  color: var(--right);
}

.quick-input-btn .card-image {
  width: 100%;
  max-width: 160px;
  height: auto;
  aspect-ratio: 63 / 88;
  border-radius: 6px;
  object-fit: cover;
  background: var(--chip);
  border: 1px solid var(--border);
  order: 2;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
}

/* ホバー時のアニメーション */
.quick-input-btn:hover .card-image {
  transform: scale(1.02);
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
  transition: all 0.2s ease;
}

/* モバイル対応 */
@media (max-width: 640px) {
  #quickInputArea {
    margin: 10px 0;
  }

  .quick-input-header {
    margin-bottom: 6px;
  }

  .quick-input-header strong {
    font-size: 12px;
  }

  .quick-input-header strong::before {
    font-size: 13px;
  }

  .quick-input-grid {
    gap: 6px;
  }

  .quick-input-btn {
    padding: 8px;
    gap: 6px;
  }

  .quick-input-btn .card-image {
    max-width: 140px;
  }

  .quick-input-btn .card-number {
    font-size: 12px;
  }

  .quick-input-btn .card-side {
    font-size: 8px;
    padding: 1px 4px;
  }
}

/* タッチデバイス用の最適化 */
@media (hover: none) and (pointer: coarse) {
  .quick-input-btn:active {
    transform: scale(0.98);
  }
}

* {
  box-sizing: border-box;
}

html,
body {
  height: 100%;
}

body {
  margin: 0;
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, Inter, Helvetica,
    Arial, "Apple Color Emoji", "Segoe UI Emoji";
  color: var(--text);
  background: var(--bg);
  -webkit-font-smoothing: antialiased;
  text-rendering: optimizeLegibility;
}

.wrap {
  max-width: 960px;
  margin: 0 auto;
  padding: 16px;
}

h1 {
  margin: 8px 0 12px;
  font-size: 20px;
}

h2 {
  margin: 0;
  font-size: 16px;
}

/* 現在の弾と入力履歴の横並びレイアウト */
.chapter-history-row {
  display: flex;
  gap: 12px;
  align-items: stretch;
  margin-bottom: 12px;
}

/* 現在の弾表示バッジ */
.current-chapter-badge {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 12px 16px;
  background: var(--accent-weak);
  border: 1px solid var(--accent);
  border-radius: var(--radius);
  color: var(--accent);
  font-size: 14px;
  font-weight: 600;
  box-shadow: var(--shadow-sm);
  white-space: nowrap;
  flex-shrink: 0;
}

.current-chapter-badge i {
  font-size: 16px;
}

.history-card {
  flex: 1;
  min-width: 0;
  margin: 0 !important;
}

.history-card h2 {
  font-size: 12px;
}

.history-card #history {
  font-size: 13px;
}

@media (prefers-color-scheme: dark) {
  .current-chapter-badge {
    background: rgba(59, 130, 246, 0.15);
    border-color: rgba(59, 130, 246, 0.4);
    color: #93c5fd;
  }
}

/* 共通 */
.mono {
  font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas,
    "Liberation Mono", "Courier New", monospace;
}

.muted {
  color: var(--muted);
}

.list {
  display: flex;
  gap: 8px;
  flex-wrap: nowrap;
}

.scroll-x {
  overflow-x: auto;
}

.card {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 10px;
  margin: 12px 0;
  box-shadow: var(--shadow-sm);
}

/* ボタン */
.btn {
  height: 44px;
  padding: 0 16px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: var(--card);
  cursor: pointer;
  box-shadow: var(--shadow-sm);
}

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

.btn.primary {
  color: #fff;
  background: var(--accent);
  border-color: var(--accent);
  box-shadow: var(--shadow-sm);
}

.btn.primary:hover {
  filter: brightness(0.97);
}

.btn.warn {
  color: #fff;
  background: #ef4444;
  border-color: #ef4444;
}

.btn.warn:hover {
  background: #dc2626;
}

.btn.ghost {
  background: var(--card);
  color: var(--text);
}

/* インタラクションの微アニメーション */
.btn,
.tab,
.gallery .tile,
#cardInput,
.badge,
.block,
.modal-backdrop .modal {
  transition: background-color 0.2s ease, border-color 0.2s ease,
    box-shadow 0.2s ease, transform 0.06s ease;
}

.btn:active {
  transform: translateY(1px);
  box-shadow: none;
}

/* キーボードフォーカスの視認性向上 */
:is(button, .btn, input, select, .tab, .close):focus-visible {
  outline: 3px solid color-mix(in oklab, var(--focus) 70%, transparent);
  outline-offset: 2px;
}

/* 入力欄・ドック */
.dock {
  display: flex;
  gap: 8px;
  align-items: center;
  flex-wrap: wrap;
}

.dock .row {
  display: flex;
  gap: 8px;
  width: 100%;
}

.dock .row.seg {
  margin-top: 6px;
}

/* 下部ボタン（2段目）は横幅を自動調整して等幅に */
.dock .row.seg > .btn {
  flex: 1 1 0;
  min-width: 0;
}

#cardInput {
  flex: 1 1 240px;
  height: 44px;
  padding: 0 12px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: var(--card);
  color: var(--text);
}

/* アラート */
.alert {
  position: fixed;
  top: calc(8px + env(safe-area-inset-top));
  left: 50%;
  transform: translateX(-50%);
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 12px;
  border-radius: 8px;
  background: var(--alert-bg);
  color: var(--alert-fg);
  border: 1px solid var(--alert-bd);
  z-index: 1000;
}

.alert .close {
  background: transparent;
  border: 0;
  color: var(--alert-fg);
  cursor: pointer;
}

/* 画像グリッド */
.gallery {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(110px, 1fr));
  gap: 8px;
}

.gallery .tile {
  border: 1px solid var(--chip-border);
  border-radius: 10px;
  background: var(--card);
  padding: 6px;
  display: flex;
  flex-direction: column;
  gap: 6px;
  cursor: pointer;
  box-shadow: var(--shadow-sm);
}

.gallery .tile:hover {
  background: var(--accent-weak);
  border-color: color-mix(in oklab, var(--accent) 40%, var(--chip-border));
}

.gallery .tile img {
  width: 100%;
  height: auto;
  border-radius: 6px;
  display: block;
}

.gallery .cap {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 6px;
  font-size: 12px;
  color: var(--muted);
}

.gallery .tag {
  padding: 1px 6px;
  border: 1px solid var(--chip-border);
  background: var(--chip);
  border-radius: 999px;
}

/* ヒント行（表/裏スイッチを縦中央に） */
.modal > .hint {
  display: flex;
  align-items: center;
  gap: 8px;
}

.modal > header + .hint {
  margin-top: 6px;
}

/* （旧ギャラリー互換） */
.grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(120px, 1fr));
  gap: 8px;
}

.grid .item {
  border: 1px solid var(--chip-border);
  background: var(--chip);
  border-radius: 10px;
  padding: 8px;
  text-align: center;
}

.grid .item img {
  max-width: 100%;
  height: auto;
  display: block;
  margin: 0 auto 6px;
}

.grid .item .title {
  font-size: 12px;
  color: var(--muted);
}

/* 配列ビュー（共通レゴンド/ブロック骨格） */
.legend {
  display: flex;
  gap: 8px;
  margin-bottom: 8px;
}

.chip {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  padding: 2px 8px;
  border: 1px solid var(--chip-border);
  background: var(--chip);
  border-radius: 999px;
  font-size: 12px;
}

/* 凡例の色（外部でも使用するため維持） */
.chip.lr {
  border-color: #bae6fd;
}

.chip.star {
  border-color: #fde68a;
}

.chip.here {
  border-color: #c7d2fe;
}

.chip.typed {
  border-color: #bbf7d0;
}

.block {
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 8px;
  background: var(--card);
  box-shadow: var(--shadow-sm);
}

.block .head {
  font-weight: 700;
  margin-bottom: 4px;
}

.block .cells {
  display: flex;
  gap: 4px;
  flex-wrap: wrap;
}

.cell {
  padding: 2px 6px;
  border-radius: 6px;
  border: 1px solid var(--chip-border);
  background: var(--chip);
  font-size: 12px;
}

/* タブUI・バッジ等 */
.tabs {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  margin: 4px 0 8px;
}

/* 結果エリアの候補タブは等幅で横幅一杯に */
#result .tabs {
  display: grid;
  grid-auto-flow: column;
  grid-auto-columns: 1fr;
  gap: 6px;
}

#result .tabs .tab {
  width: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
}

.tab {
  appearance: none;
  -webkit-appearance: none;
  cursor: pointer;
  padding: 2px 8px;
  border: 1px solid var(--border);
  background: var(--card);
  border-radius: 10px;
  font-size: 12px;
  box-shadow: var(--shadow-sm);
  font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas,
    "Liberation Mono", "Courier New", monospace;
}

.tab:hover {
  background: var(--accent-weak);
}

.tab.active {
  background: var(--accent-weak);
  border-color: var(--accent);
}

button.tab {
  appearance: none;
  -webkit-appearance: none;
}

/* OS既定外観の無効化（1度だけ） */

/* LR★含むシリンダーペア候補タブの虹色スタイル */
.tab.rainbow-tab {
  background: linear-gradient(
    90deg,
    #ffe6ea 0%,
    #fff3c4 20%,
    #e0ffef 40%,
    #e0f2fe 60%,
    #ede9fe 80%,
    #ffe6ea 100%
  );
  background-size: 300% 300%;
  animation: rainbowMove 3s ease-in-out infinite;
  border-color: color-mix(in oklab, #ffffff 60%, var(--border));
  font-weight: 700;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.15);
}

.tab.rainbow-tab:hover {
  background: linear-gradient(
    90deg,
    #ffe6ea 0%,
    #fff3c4 20%,
    #e0ffef 40%,
    #e0f2fe 60%,
    #ede9fe 80%,
    #ffe6ea 100%
  );
  background-size: 300% 300%;
  filter: brightness(1.05);
}

.tab.rainbow-tab.active {
  background: linear-gradient(
    90deg,
    #ffe6ea 0%,
    #fff3c4 20%,
    #e0ffef 40%,
    #e0f2fe 60%,
    #ede9fe 80%,
    #ffe6ea 100%
  );
  background-size: 300% 300%;
  color: var(--text);
  font-weight: 800;
  box-shadow: 0 0 0 2px rgba(255, 255, 255, 0.6), 0 2px 12px rgba(0, 0, 0, 0.2);
}

@keyframes rainbowMove {
  0% {
    background-position: 0% 50%;
  }

  50% {
    background-position: 100% 50%;
  }

  100% {
    background-position: 0% 50%;
  }
}

@media (prefers-reduced-motion: reduce) {
  .tab.rainbow-tab {
    animation: none;
  }
}

.badge {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  padding: 2px 8px;
  border: 1px solid var(--chip-border);
  background: var(--chip);
  border-radius: 999px;
  font-size: 12px;
  box-shadow: var(--shadow-sm);
}

.badge.def {
  background: var(--bg-typed);
  border-color: var(--bd-typed);
}

/* 数値のみの圧縮表示（履歴が多い時） */
.badge.cond {
  padding: 1px 6px;
  font-size: 12px;
}

/* スイッチUI（表/裏切替） */
.switch {
  position: relative;
  display: inline-flex;
  align-items: center;
  cursor: pointer;
}

.switch input {
  position: absolute;
  opacity: 0;
  width: 0;
  height: 0;
}

.switch .slider {
  position: relative;
  width: 56px;
  height: 28px;
  border-radius: 999px;
  background: var(--switch-off-bg);
  border: 1px solid var(--border);
  display: inline-block;
  transition: background 0.2s ease, border-color 0.2s ease;
}

.switch .slider,
.switch .slider::before,
.switch .slider::after {
  vertical-align: middle;
}

.switch .slider::before {
  content: "";
  position: absolute;
  top: 2px;
  left: 2px;
  width: 24px;
  height: 24px;
  border-radius: 50%;
  background: var(--card);
  border: 1px solid var(--border);
  transition: transform 0.2s ease;
  box-shadow: 0 1px 2px rgba(0, 0, 0, 0.08);
}

.switch .slider::after {
  content: attr(data-front);
  position: absolute;
  left: 30px;
  top: 50%;
  transform: translateY(-50%);
  font-size: 11px;
  color: var(--text);
  transition: opacity 0.2s ease, left 0.2s ease;
}

.switch input:checked + .slider {
  background: var(--switch-on-bg);
  border-color: var(--switch-on-bd);
}

.switch input:checked + .slider::before {
  transform: translateX(28px);
}

.switch input:checked + .slider::after {
  content: attr(data-back);
  left: 8px;
}

.kvs {
  display: flex;
  gap: 12px;
  align-items: baseline;
  color: var(--muted);
  font-size: 13px;
  margin: 4px 0 8px;
}

.lr {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 12px;
}

#result .lr {
  grid-template-columns: 1fr;
}

.lr.resolved {
  border: 2px solid var(--good);
  border-radius: 12px;
  padding: 8px;
  position: relative;
  background: color-mix(in oklab, var(--good) 8%, var(--card));
}

.lr.resolved::after {
  content: "位置確定";
  position: absolute;
  top: -10px;
  right: 12px;
  font-size: 11px;
  background: var(--good);
  color: #fff;
  padding: 2px 6px;
  border-radius: 6px;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.25);
}

.result-status {
  margin: 4px 0 8px;
  font-size: 12px;
  line-height: 1.4;
}

.result-status.warn {
  color: var(--warn);
}

.result-status.ok {
  color: var(--good);
  font-weight: 600;
}

.sidebox {
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 8px;
  background: var(--card);
}

.side-title {
  display: flex;
  align-items: center;
  gap: 6px;
  font-weight: 700;
  margin-bottom: 6px;
}

.dot {
  width: 8px;
  height: 8px;
  border-radius: 999px;
  background: var(--left);
  display: inline-block;
}

.sidebox.side-right .dot {
  background: var(--right);
}

/* 逆順シリンダーの枠 */
.sidebox.reversed {
  border: 3px solid #17a2b8;
  border-style: dashed;
  background: linear-gradient(
    135deg,
    rgba(23, 162, 184, 0.05) 0%,
    rgba(23, 162, 184, 0.1) 100%
  );
  box-shadow: 0 0 0 3px rgba(23, 162, 184, 0.15),
    0 4px 12px rgba(23, 162, 184, 0.2);
  position: relative;
}

.sidebox.reversed::before {
  content: "🔄 逆順";
  position: absolute;
  top: -12px;
  right: 8px;
  background: #17a2b8;
  color: white;
  font-size: 11px;
  font-weight: 700;
  padding: 2px 8px;
  border-radius: 4px;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.2);
}

@media (prefers-color-scheme: dark) {
  .sidebox.reversed {
    border-color: #5dade2;
    background: linear-gradient(
      135deg,
      rgba(93, 173, 226, 0.08) 0%,
      rgba(93, 173, 226, 0.15) 100%
    );
    box-shadow: 0 0 0 3px rgba(93, 173, 226, 0.2),
      0 4px 12px rgba(93, 173, 226, 0.25);
  }

  .sidebox.reversed::before {
    background: #5dade2;
    color: #0b1020;
  }
}

.stat {
  color: var(--muted);
  font-size: 12px;
  margin-top: 6px;
}

.stat.urgent {
  color: var(--text);
  background: var(--bg-star);
  border: 1px solid var(--bd-star);
  display: inline-block;
  padding: 2px 6px;
  border-radius: 6px;
  font-weight: 700;
}

.stat.next-cands {
  display: block;
  color: inherit;
  /* 1行目と同じ色に揃える */
  background: transparent;
  border: 0;
  padding: 0;
  margin-top: 2px;
  font-weight: 600;
  white-space: pre-line;
  /* JSで '\n' を改行として表示 */
  line-height: 1.3;
  /* 行間を適度に調整 */
}

/* 9枚以下の緊急時は、カード名行にもバッジ風の強調を適用（非レジェンド時） */
.stat.next-cands.urgent {
  color: var(--text);
  background: var(--bg-star);
  border: 1px solid var(--bd-star);
  display: block;
  padding: 2px 6px;
  border-radius: 6px;
  font-weight: 700;
  margin-top: 2px;
  white-space: pre-line;
  /* 改行を保持 */
}

@media (prefers-color-scheme: dark) {
  .stat.next-cands.urgent {
    text-shadow: 0 1px 2px rgba(0, 0, 0, 0.6);
  }
}

/* 次ハイライト2行まとめのグループ */
.next-hl {
  display: block;
}

.next-hl.legend .stat {
  background: transparent;
  border-color: transparent;
}

.next-hl.legend {
  background: linear-gradient(
    90deg,
    #ffe6ea 0%,
    #fff3c4 20%,
    #e0ffef 40%,
    #e0f2fe 60%,
    #ede9fe 80%,
    #ffe6ea 100%
  );
  background-size: 300% 300%;
  border: 1px solid rgba(255, 255, 255, 0.35);
  padding: 2px 6px;
  border-radius: 8px;
}

.next-hl.legend .stat.next-cands.urgent {
  background: transparent;
  border: 0;
  padding: 0;
  display: block;
  margin-top: 2px;
  white-space: pre-line;
  /* 改行を保持 */
}

@media (prefers-color-scheme: dark) {
  .next-hl.legend .stat.next-cands,
  .next-hl.legend .stat.next-cands * {
    color: #ffffff;
  }
}

.next-hl.urgent {
  animation: badgeGlow 1.8s ease-in-out infinite;
}

/* 次ハイライトの虹背景は、緊急時に穏やかに動かす */
@keyframes nextLegendMove {
  0% {
    background-position: 0% 50%;
  }

  100% {
    background-position: 100% 50%;
  }
}

.next-hl.legend.urgent {
  animation: badgeGlow 1.8s ease-in-out infinite,
    nextLegendMove 6s linear infinite;
}

@media (prefers-color-scheme: dark) {
  .next-hl.legend {
    border-color: rgba(255, 255, 255, 0.25);
    color: #ffffff;
    /* 白文字でコントラストを確保 */
    text-shadow: 0 1px 2px rgba(0, 0, 0, 0.9), 0 0 6px rgba(0, 0, 0, 0.45);
    /* 明るい帯での読みづらさ対策に、薄い内側オーバーレイを追加 */
    box-shadow: inset 0 0 0 9999px rgba(0, 0, 0, 0.12),
      0 1px 2px rgba(0, 0, 0, 0.5);
  }

  /* 子要素のテキストにも縁取りを適用（text-shadowは継承しないため個別指定） */
  .next-hl.legend .stat,
  .next-hl.legend .stat * {
    color: inherit;
    text-shadow: 0 1px 2px rgba(0, 0, 0, 0.9), 0 0 6px rgba(0, 0, 0, 0.45);
  }

  /* 緊急時は明滅が分かるように内側オーバーレイを少し弱める */
  .next-hl.legend.urgent {
    box-shadow: inset 0 0 0 9999px rgba(0, 0, 0, 0.08),
      0 1px 2px rgba(0, 0, 0, 0.5);
  }
}

@media (prefers-reduced-motion: reduce) {
  /* 動きに敏感なユーザー配慮：背景移動は止め、明滅のみ維持 */
  .next-hl.legend.urgent {
    animation: badgeGlow 1.8s ease-in-out infinite;
  }
}

/* 最高レア（★付LR）用の虹色強調 */
.stat.legend {
  background: linear-gradient(
    90deg,
    #ff6b6b,
    #f7d794,
    #1abc9c,
    #74b9ff,
    #a29bfe,
    #ff6b6b
  );
  background-size: 300% 300%;
  color: #111;
  border: 1px solid rgba(255, 255, 255, 0.35);
  display: inline-block;
  padding: 3px 8px;
  border-radius: 8px;
  font-weight: 800;
  box-shadow: 0 1px 2px rgba(0, 0, 0, 0.12);
}

@media (prefers-color-scheme: dark) {
  .stat.legend {
    color: #fff;
    border-color: rgba(255, 255, 255, 0.25);
    box-shadow: 0 1px 2px rgba(0, 0, 0, 0.5);
  }
}

/* 残り枚数が一桁の時、視認性を下げない穏やかなアニメーション */
@keyframes badgeGlow {
  0% {
    filter: brightness(1);
  }

  50% {
    filter: brightness(1.08);
  }

  100% {
    filter: brightness(1);
  }
}

.stat.urgent {
  animation: badgeGlow 1.8s ease-in-out infinite;
}

/* 最高レア＋一桁の組合せ（虹背景は動かさず、光量だけわずかに） */
/* 虹色グラデーションを横方向にゆっくり移動させる */
@keyframes legendGradientMove {
  0% {
    background-position: 0% 50%;
  }

  100% {
    background-position: 100% 50%;
  }
}

.stat.legend.urgent {
  animation: legendGradientMove 6s linear infinite;
}

@media (prefers-reduced-motion: reduce) {
  .stat.legend.urgent {
    animation: none;
  }
}

/* ===== 保存スロット ===== */
#slots {
  margin-top: 6px;
}

.slots {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 8px;
}

.slot {
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--card);
  box-shadow: var(--shadow-sm);
}

.slot-head {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px;
  border-bottom: 1px solid var(--border);
}

.slot-title {
  font-weight: 700;
}

.slot-actions {
  margin-left: auto;
  display: flex;
  gap: 6px;
}

.slot-body {
  padding: 8px;
  display: grid;
  gap: 6px;
}

.slot-summary {
  font-size: 12px;
  word-break: break-all;
}

.slot-empty {
  padding: 8px;
}

.slot .btn {
  height: 32px;
  padding: 0 10px;
}

.slot .btn.icon {
  width: 36px;
  padding: 0;
  text-align: center;
  font-size: 14px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
}

.slot .btn.icon i {
  pointer-events: none;
}

@media (max-width: 640px) {
  .slots {
    grid-template-columns: 1fr;
  }
}

/* モーダル（現在の実装：.modal-backdrop > .modal） */
.modal-backdrop {
  position: fixed;
  inset: 0;
  display: none;
  align-items: center;
  justify-content: center;
  background: rgba(0, 0, 0, 0.4);
  z-index: 999;
}

.modal-backdrop[aria-hidden="false"] {
  display: flex;
}

.modal-backdrop .modal {
  position: relative;
  display: block;
  background: var(--card);
  max-width: 90vw;
  max-height: 85vh;
  border-radius: 12px;
  padding: 12px;
  overflow: auto;
  border: 1px solid var(--border);
  box-shadow: var(--shadow-md);
}

.modal-backdrop .close {
  position: absolute;
  right: 8px;
  top: 8px;
  width: 36px;
  height: 36px;
  border-radius: 999px;
  border: 1px solid var(--chip-border);
  background: var(--chip);
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  line-height: 1;
  font-size: 16px;
}

/* ヘッダー内のクローズボタンはフレックスで縦中央に */
.modal-backdrop .modal > header {
  display: flex;
  align-items: center;
  gap: 8px;
}

.modal-backdrop .modal > header .close {
  position: static;
  margin-left: auto;
  right: auto;
  top: auto;
  width: 36px;
  height: 36px;
  border-radius: 999px;
  border: 1px solid var(--chip-border);
  background: var(--chip);
  cursor: pointer;
}

/* 拡大モーダル（画像） */
.zoom-body {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
}

.zoom-body img {
  max-height: 70vh;
  max-width: 74vw;
  border-radius: 8px;
  border: 1px solid var(--border);
}

.zoom-body .flip {
  position: absolute;
  left: 50%;
  bottom: 12px;
  transform: translateX(-50%);
  padding: 6px 10px;
  border-radius: 999px;
  border: 1px solid var(--border);
  background: var(--card);
  cursor: pointer;
}

.zoom-body .nav {
  position: absolute;
  top: 50%;
  transform: translateY(-50%);
  background: rgba(0, 0, 0, 0.5);
  color: #fff;
  border: 0;
  border-radius: 999px;
  width: 36px;
  height: 36px;
  font-size: 18px;
  cursor: pointer;
}

.zoom-body .nav.prev {
  left: 6px;
}

.zoom-body .nav.next {
  right: 6px;
}

.zoom-body .nav:hover {
  background: rgba(0, 0, 0, 0.7);
}

/* 小端末最適化 */
@media (max-width: 380px) {
  #cardInput {
    height: 40px;
  }

  .btn {
    height: 40px;
    padding: 0 12px;
    font-size: 14px;
  }
}

/* 追加: スマホ向け最適化（処理非変更・見た目のみ） */
@media (max-width: 640px) {
  /* コンテンツが固定ドックに隠れないよう下余白を確保 */
  .wrap {
    padding: 12px 12px calc(148px + env(safe-area-inset-bottom));
  }

  /* 入力操作を下部固定（セーフエリア対応） */
  .dock {
    position: fixed;
    left: 0;
    right: 0;
    bottom: 0;
    z-index: 998;
    background: var(--bg);
    border-top: 1px solid var(--border);
    padding: 10px 12px calc(10px + env(safe-area-inset-bottom));
    box-shadow: 0 -6px 18px rgba(0, 0, 0, 0.08);
  }

  .dock .row {
    flex-wrap: wrap;
  }

  /* iOS のズーム防止＆読みやすさ向上 */
  #cardInput {
    font-size: 16px;
  }

  /* タブや横スクロール要素の操作性改善 */
  .scroll-x {
    -webkit-overflow-scrolling: touch;
    overscroll-behavior-x: contain;
  }

  .tabs {
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
  }

  .legend {
    flex-wrap: wrap;
  }

  /* ギャラリーのカード幅を少し狭めて詰めやすく */
  .gallery {
    grid-template-columns: repeat(auto-fill, minmax(96px, 1fr));
  }

  /* モーダルはモバイルではフルスクリーン風に */
  .modal-backdrop .modal {
    width: 100vw;
    height: 100vh;
    max-width: none;
    max-height: none;
    border-radius: 0;
    padding: 10px;
    /* 下側はセーフエリア分を確保 */
    padding-bottom: calc(10px + env(safe-area-inset-bottom));
  }

  .modal-backdrop .modal > header {
    position: sticky;
    top: 0;
    z-index: 2;
    margin: -10px -10px 8px;
    padding: 10px;
    background: var(--card);
    border-bottom: 1px solid var(--border);
  }

  /* モーダルヘッダーは既にsticky。タップ領域だけ少し拡大 */
  .modal-backdrop .close {
    width: 40px;
    height: 40px;
  }

  /* 画像拡大の余白を端末幅に合わせる */
  .zoom-body img {
    max-width: 90vw;
    max-height: 70vh;
  }

  .zoom-body .nav {
    width: 44px;
    height: 44px;
    font-size: 20px;
  }
}

/* タップ操作の質を改善（処理非変更） */
button,
.btn,
.close {
  touch-action: manipulation;
}

input,
button {
  -webkit-tap-highlight-color: transparent;
}

/* 旧DOMの章セレクタ（未使用でも安全に残す） */
.chapter-selector {
  display: inline-flex;
  align-items: center;
  gap: 0.5rem;
  margin: 0.25rem 0.5rem 0.75rem;
}

.chapter-selector label {
  font-size: 0.9rem;
  opacity: 0.8;
}

#chapterSelect {
  padding: 0.25rem 0.5rem;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: var(--card);
  color: var(--text);
}

/* ======= 配列モーダル専用 強化レイアウト ======= */
#modalWrap .modal {
  width: min(960px, 92vw);
  border: 1px solid var(--border);
}

#modalWrap .modal > header {
  position: sticky;
  top: 0;
  z-index: 1;
  display: flex;
  align-items: center;
  gap: 8px;
  margin: -12px -12px 8px;
  padding: 10px 12px;
  background: var(--card);
  border-bottom: 1px solid var(--border);
}

#modalWrap .modal > header h3 {
  margin: 0;
  font-size: 16px;
}

#modalWrap .modal > header .close {
  margin-left: auto;
  width: 36px;
  height: 36px;
  border-radius: 999px;
  border: 1px solid var(--chip-border);
  background: var(--chip);
  cursor: pointer;
}

#modalBody {
  display: grid;
  gap: 8px;
}

#modalBody .tabs {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  margin: 4px 0 8px;
}

#modalBody .tab {
  appearance: none;
  -webkit-appearance: none;
  cursor: pointer;
  padding: 2px 8px;
  border: 1px solid var(--border);
  background: var(--card);
  border-radius: 8px;
  font-size: 12px;
  font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas,
    "Liberation Mono", "Courier New", monospace;
}

#modalBody .tab:hover {
  background: var(--accent-weak);
}

#modalBody .tab.active {
  background: var(--accent-weak);
  border-color: var(--accent);
}

#modalBody .legend {
  display: flex;
  gap: 8px;
  margin-bottom: 8px;
}

#modalBody .grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(140px, 1fr));
  gap: 8px;
}

#modalBody .block {
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 8px;
  background: var(--card);
}

#modalBody .block .head {
  font-weight: 700;
  margin-bottom: 4px;
}

#modalBody .block .cells {
  display: flex;
  gap: 4px;
  flex-wrap: wrap;
}

#modalBody .cell {
  padding: 2px 8px;
  border-radius: 999px;
  border: 1px solid var(--chip-border);
  background: var(--chip);
  font-size: 12px;
}

/* モーダル内ラベル: 基本は1行だが、幅超過時のみ半角スペースで改行 */
#modalWrap .badge,
#modalWrap .cell {
  white-space: normal;
  /* 折返しを許可 */
  word-break: keep-all;
  /* 日本語の禁則を維持（スペース以外で折返さない） */
  line-break: strict;
  /* 句読点等での不自然改行を強めに抑制 */
  display: inline-block;
  /* flex指定の上書き（バッジ内テキストの折返しを有効化） */
  max-width: 100%;
}

/* シリンダーモーダル：1ブロック内の複数カードは縦に並べる（1枚ごとに改行） */
#modalBody .block .list {
  display: flex;
  flex-direction: column;
  gap: 6px;
  overflow-x: visible;
}

#modalBody .block .list .badge {
  align-self: flex-start;
}

/* ======= 配列モーダル: ハイライト統合（重複排除版） ======= */
@keyframes _herePulse {
  0% {
    box-shadow: 0 0 0 0 rgba(67, 56, 202, 0.35);
  }

  100% {
    box-shadow: 0 0 0 8px rgba(67, 56, 202, 0);
  }
}

/* セル基底 */
#modalWrap :is(.cell, .pill, .badge, .tag) {
  position: relative;
}

/* LR */
#modalWrap :is(.cell, .pill, .badge, .tag).lr,
#modalWrap :is(.cell, .pill, .badge, .tag)[data-lr="1"] {
  color: var(--clr-lr);
  background: var(--bg-lr);
  border-color: var(--bd-lr);
  outline: 2px solid var(--bd-lr);
  outline-offset: -2px;
  font-weight: 600;
}

/* ★ */
#modalWrap :is(.cell, .pill, .badge, .tag).star,
#modalWrap :is(.cell, .pill, .badge, .tag)[data-star="1"] {
  color: var(--clr-star);
  background: var(--bg-star);
  border-color: var(--bd-star);
  outline: 2px solid var(--bd-star);
  outline-offset: -2px;
  font-weight: 600;
}

/* LR かつ ★ は虹色で強調（モーダル内） */
#modalWrap :is(.cell, .pill, .badge, .tag).lr.star {
  background-image: linear-gradient(
    90deg,
    #ffe6ea 0%,
    #fff3c4 20%,
    #e0ffef 40%,
    #e0f2fe 60%,
    #ede9fe 80%,
    #ffe6ea 100%
  );
  color: var(--text);
  border-color: color-mix(in oklab, #ffffff 60%, var(--chip-border));
  outline: 2px solid rgba(255, 255, 255, 0.6);
  outline-offset: -2px;
  font-weight: 800;
  background-size: 300% 300%;
}

@keyframes modalLegendMove {
  0% {
    background-position: 0% 50%;
  }

  100% {
    background-position: 100% 50%;
  }
}

#modalWrap :is(.cell, .pill, .badge, .tag).lr.star[aria-legend="1"] {
  animation: modalLegendMove 8s linear infinite;
}

@media (prefers-reduced-motion: reduce) {
  #modalWrap :is(.cell, .pill, .badge, .tag).lr.star[aria-legend="1"] {
    animation: none;
  }
}

@media (prefers-color-scheme: dark) {
  #modalWrap :is(.cell, .pill, .badge, .tag).lr.star {
    color: #ffffff;
    /* 明るい文字に */
    text-shadow: 0 1px 2px rgba(0, 0, 0, 0.85), 0 0 6px rgba(0, 0, 0, 0.45);
    /* 読みやすさ向上 */
    border-color: rgba(255, 255, 255, 0.28);
    outline-color: rgba(255, 255, 255, 0.38);
    /* 背景が明るいため、内側に薄い暗めオーバーレイを被せてコントラスト確保 */
    box-shadow: 0 1px 2px rgba(0, 0, 0, 0.5),
      inset 0 0 0 9999px rgba(0, 0, 0, 0.08);
  }
}

/* 現在位置（セル） */
#modalWrap :is(.cell, .pill, .badge, .tag).here,
#modalWrap :is(.cell, .pill, .badge, .tag)[data-here="1"],
#modalWrap :is(.cell, .pill, .badge, .tag)[aria-current="true"] {
  color: var(--clr-here);
  background: var(--bg-here);
  border-color: var(--bd-here);
  outline: 2px solid var(--bd-here);
  outline-offset: -2px;
  font-weight: 700;
  animation: _herePulse 1.6s ease-out infinite;
}

/* 現在位置（ブロック全体） */
#modalWrap .block.current {
  border-color: var(--bd-here);
  background: var(--bg-here);
}

#modalWrap .block.current .title {
  color: var(--clr-here);
}

/* 入力済み候補（未確定）— 常に薄いハイライト */
#modalWrap :is(.badge, .cell).typed-cand {
  background: var(--accent-weak);
  border-color: var(--accent);
  outline: 1.5px dashed var(--accent);
  outline-offset: -2px;
}

/* 入力済み（確定）— ✓ と緑系で強調 */
#modalWrap :is(.badge, .cell).typed::after,
#modalWrap :is(.badge, .cell).checked::after {
  content: "✓";
  display: inline-block;
  margin-left: 6px;
  font-weight: 700;
  font-size: 11px;
  line-height: 1;
}

/* 確定スタイルは、LR/★/here が付いていない要素のみに適用（重なり時の可読性確保） */
#modalWrap :is(.badge, .cell):not(.lr):not(.star):not(.here).typed,
#modalWrap :is(.badge, .cell):not(.lr):not(.star):not(.here).checked {
  color: var(--clr-typed);
  background: var(--bg-typed);
  border-color: var(--bd-typed);
  outline: 2px solid var(--bd-typed);
  outline-offset: -2px;
  font-weight: 600;
}

/* 組み合わせ時はベースの強調色を維持しつつチェックのみ付与（背景・文字色を保つ） */
#modalWrap :is(.badge, .cell).typed.lr,
#modalWrap :is(.badge, .cell).checked.lr {
  color: var(--clr-lr);
  background: var(--bg-lr);
  border-color: var(--bd-lr);
  outline: 2px solid var(--bd-lr);
  outline-offset: -2px;
  font-weight: 700;
}

#modalWrap :is(.badge, .cell).typed.star,
#modalWrap :is(.badge, .cell).checked.star {
  color: var(--clr-star);
  background: var(--bg-star);
  border-color: var(--bd-star);
  outline: 2px solid var(--bd-star);
  outline-offset: -2px;
  font-weight: 700;
}

#modalWrap :is(.badge, .cell).typed.here,
#modalWrap :is(.badge, .cell).checked.here {
  color: var(--clr-here);
  background: var(--bg-here);
  border-color: var(--bd-here);
  outline: 2px solid var(--bd-here);
  outline-offset: -2px;
  font-weight: 800;
}

/* LR 強調 */
.badge.hl-lr {
  background-color: #ffe6ea;
  /* 薄い赤系 */
  border-color: #ff6b81;
  box-shadow: 0 0 0 2px rgba(255, 107, 129, 0.15) inset;
  font-weight: 600;
}

/* ★ 強調 */
.badge.hl-star {
  background-color: #fff3c4;
  /* 薄い黄系 */
  border-color: #ffb300;
  box-shadow: 0 0 0 2px rgba(255, 179, 0, 0.18) inset;
  font-weight: 600;
}

/* 両方（LRかつ★）はグラデで強調 */
.badge.hl-lr.hl-star {
  background-image: linear-gradient(90deg, #ffe6ea 0%, #fff3c4 100%);
  border-color: #ff6b81;
}

/* --- 末尾のダーク上書き（ライト定義の後勝ちを防止） --- */
@media (prefers-color-scheme: dark) {
  .badge.hl-lr {
    background-color: #4a1e28;
    /* 暗い赤系 */
    border-color: #ff8da0;
    box-shadow: 0 0 0 2px rgba(255, 141, 160, 0.18) inset;
    color: var(--text);
  }

  .badge.hl-star {
    background-color: #3a2a00;
    /* 暗い黄土系 */
    border-color: #ffcc4d;
    box-shadow: 0 0 0 2px rgba(255, 204, 77, 0.18) inset;
    color: var(--text);
  }

  .badge.hl-lr.hl-star {
    background-image: linear-gradient(90deg, #4a1e28 0%, #3a2a00 100%);
    border-color: #ff8da0;
    color: var(--text);
  }
}
