:root {
  --bg: #f5f6fb;
  --sidebar-bg: #111827;
  --sidebar-accent: #065154;
  --card-bg: #ffffff;
  --border-subtle: #e5e7eb;
  --text-main: #111827;
  --text-sub: #6b7280;
  --radius-lg: 18px;
  --radius-md: 12px;
  --shadow-soft: 0 10px 30px rgba(15, 23, 42, 0.12);
  /* カード: 高さ150px固定、縦横比 3:4 で幅を算出 */
  --card-height: 150px;
  --card-width: 112.5px; /* 150 * 3/4 */
  /* このデッキのカード・カードプール共通: 120px・縦横比維持 */
  --card-pool-height: 120px;
  --card-pool-width: 90px; /* 120 * 3/4 */
  --card-pool-row-overlap: 60px; /* カードプールのみ重なり量 */
}

*,
*::before,
*::after {
  box-sizing: border-box;
}

body {
  margin: 0;
  font-family: system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI",
    sans-serif;
  background-color: var(--bg);
  color: var(--text-main);
}

.app-shell {
  display: flex;
  min-height: 100vh;
}

.sidebar {
  width: 200px;
  background: radial-gradient(circle at top left, #0b7660, #022c22);
  color: white;
  padding: 24px 18px;
  display: flex;
  flex-direction: column;
  gap: 32px;
}

.logo {
  font-weight: 700;
  font-size: 1.3rem;
}

.nav {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.nav-item {
  border: none;
  background: transparent;
  color: rgba(255, 255, 255, 0.8);
  padding: 10px 12px;
  border-radius: 999px;
  text-align: left;
  cursor: pointer;
  font-size: 0.95rem;
}

.nav-item.active {
  background: rgba(255, 255, 255, 0.18);
  color: white;
}

.main {
  flex: 1;
  padding: 24px 32px;
  max-width: 1200px;
  margin: 0 auto;
}

.main-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
}

.main-header-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
  align-items: center;
}

.builder-save-row {
  display: flex;
  align-items: center;
  gap: 12px;
}

/* 保存完了トースト（保存ボタンのすぐ隣） */
.save-toast {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  padding: 10px 16px;
  background: rgba(34, 197, 94, 0.95);
  color: white;
  border-radius: 999px;
  font-size: 0.9rem;
  box-shadow: var(--shadow-soft);
  transition: opacity 0.2s ease;
}
.save-toast.hidden {
  display: none;
}
.save-toast-icon {
  font-weight: bold;
}

.primary-btn,
.secondary-btn {
  border-radius: 999px;
  padding: 8px 16px;
  font-size: 0.9rem;
  border: none;
  cursor: pointer;
}

.primary-btn {
  background: linear-gradient(135deg, #065154, #0b7660);
  color: white;
  box-shadow: 0 10px 20px rgba(6, 81, 84, 0.4);
}

.secondary-btn {
  background: #e5e7eb;
  color: #111827;
}

.primary-btn.outline {
  background: transparent;
  color: var(--sidebar-accent);
  box-shadow: none;
  border: 1px solid var(--sidebar-accent);
}

.view {
  display: none;
}

.view.active {
  display: block;
}

.deck-list {
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.deck-section-header {
  font-size: 0.85rem;
  color: var(--text-sub);
  margin-bottom: 4px;
}

.deck-list-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(230px, 1fr));
  gap: 16px;
}

.deck-divider {
  border: none;
  border-top: 1px solid var(--border-subtle);
  margin: 12px 0;
}

.deck-card {
  background: var(--card-bg);
  border-radius: var(--radius-lg);
  padding: 16px;
  box-shadow: var(--shadow-soft);
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  gap: 8px;
}

.deck-name {
  font-weight: 600;
  margin-bottom: 4px;
}

.deck-meta {
  font-size: 0.8rem;
  color: var(--text-sub);
}

.deck-count-label {
  font-size: 0.8rem;
  color: var(--text-sub);
  margin-left: 8px;
}

.deck-actions {
  display: flex;
  justify-content: space-between;
  margin-top: 8px;
  gap: 8px;
}

.deck-actions .primary-btn,
.deck-actions .secondary-btn {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 2px;
  padding: 6px 10px;
}

.deck-action-icon {
  font-size: clamp(1rem, 2vw, 1.3rem);
  line-height: 1;
}

.deck-action-label {
  font-size: clamp(0.6rem, 1.5vw, 0.8rem);
  line-height: 1.1;
}

.badge-active {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  font-size: 0.75rem;
  padding: 2px 8px;
  border-radius: 999px;
  background: rgba(34, 197, 94, 0.12);
  color: #16a34a;
}

.builder-header,
.switcher-form {
  display: flex;
  flex-wrap: wrap;
  gap: 16px;
  align-items: center;
  margin-bottom: 16px;
}

.pool-toggle-btn {
  margin-left: auto;
  display: none;
}

label {
  font-size: 0.9rem;
  color: var(--text-sub);
  display: flex;
  flex-direction: column;
  gap: 4px;
}

select,
input[type="text"] {
  padding: 6px 10px;
  border-radius: 999px;
  border: 1px solid var(--border-subtle);
  font-size: 0.9rem;
}

/* カード種別フィルター: カスタムドロップダウン＋ホバーでサブメニュー */
.filter-label {
  position: relative;
}
.filter-wrap {
  position: relative;
  display: inline-block;
}
.filter-trigger {
  padding: 6px 12px;
  border-radius: 999px;
  border: 1px solid var(--border-subtle);
  font-size: 0.9rem;
  background: #fff;
  color: var(--text-main);
  cursor: pointer;
  min-width: 140px;
  text-align: left;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
}
.filter-trigger::after {
  content: "▼";
  font-size: 0.7rem;
  opacity: 0.7;
}
.filter-dropdown {
  position: absolute;
  top: 100%;
  left: 0;
  margin-top: 4px;
  background: #fff;
  border: 1px solid var(--border-subtle);
  border-radius: var(--radius-md);
  box-shadow: var(--shadow-soft);
  z-index: 20;
  width: 200px;
  max-width: 90vw;
  max-height: 60vh;
  overflow-y: auto;
  overflow-x: hidden;
}
.filter-dropdown.hidden {
  display: none;
}
.filter-option {
  display: block;
  width: 100%;
  padding: 8px 12px;
  border: none;
  background: none;
  font-size: 0.9rem;
  text-align: left;
  cursor: pointer;
  color: var(--text-main);
  white-space: nowrap;
}
.filter-option:hover {
  background: rgba(99, 102, 241, 0.1);
}
.filter-option.active {
  background: rgba(99, 102, 241, 0.15);
  color: var(--sidebar-accent);
}
.filter-row {
  position: relative;
  display: flex;
  align-items: center;
}
.filter-row .filter-option {
  flex: 1;
}
.filter-row .filter-option.has-sub::after {
  content: "›";
  margin-left: auto;
  opacity: 0.7;
}
/* サブメニュー用の別ボックス（フィルタの隣に独立表示） */
.filter-submenu-panel {
  position: fixed;
  background: #fff;
  border: 1px solid var(--border-subtle);
  border-radius: var(--radius-md);
  box-shadow: var(--shadow-soft);
  min-width: 160px;
  max-height: 50vh;
  overflow-y: auto;
  z-index: 22;
  padding: 4px 0;
}
.filter-submenu-panel.hidden {
  display: none;
}
.filter-submenu-panel .filter-option {
  display: block;
  width: 100%;
  padding: 6px 12px;
  font-size: 0.85rem;
  border: none;
  background: none;
  text-align: left;
  cursor: pointer;
  color: var(--text-main);
  white-space: nowrap;
}
.filter-submenu-panel .filter-option:hover {
  background: rgba(99, 102, 241, 0.1);
}
.filter-submenu-panel .filter-option.active {
  background: rgba(99, 102, 241, 0.15);
  color: var(--sidebar-accent);
}
.filter-divider {
  height: 1px;
  background: var(--border-subtle);
  margin: 4px 0;
}

.hint {
  font-size: 0.8rem;
  color: var(--text-sub);
}

.builder-layout {
  display: grid;
  grid-template-columns: 1fr 1.4fr;
  gap: 20px;
}

/* デッキ入れ替えビューでは「抜くカード」「入れるカード」のボックス幅を常に同じにする */
#view-switcher .builder-layout {
  grid-template-columns: repeat(2, minmax(0, 1fr));
}

.card-pool,
.deck-cards,
.switcher-result {
  background: var(--card-bg);
  border-radius: var(--radius-lg);
  padding: 14px 16px;
  box-shadow: var(--shadow-soft);
  display: flex;
  flex-direction: column;
  max-height: 70vh;
  min-height: 120px;
}

.switcher-result {
  margin-top: 16px;
}

.switcher-actions {
  display: flex;
  align-items: center;
  gap: 12px;
  flex-wrap: wrap;
}

/* 旧 switcher-columns / switcher-column は builder-layout + deck-cards / card-pool に統合 */

.deck-cards {
  border: 2px solid rgba(6, 81, 84, 0.5);
}

.card-pool {
  background: #ffffff;
}

/* このデッキのカード欄（デッキ編集ビュー）は紅色の枠で囲む */
#view-builder .deck-cards {
  border-color: #d7003a;
}

.deck-cards .card-grid,
.card-pool .card-grid {
  overflow-y: auto;
  min-height: 0;
  flex: 1;
  -webkit-overflow-scrolling: touch;
  /* スクロールバーがカードに被らないよう gutter を確保 */
  scrollbar-gutter: stable;
}

.card-grid {
  display: grid;
  gap: 12px;
  margin-top: 10px;
}

/* デッキ入れ替えビュー: 抜く/入れるカードの 1 行あたりの枚数を 1〜3 枚で自動調整（左右同じルール） */
#view-switcher .card-grid {
  grid-template-columns: repeat(auto-fit, minmax(120px, 1fr));
}

/* カードプール: 120px・少し重なる演出・列数は幅に応じて減る */
#card-pool-list.card-grid {
  grid-template-columns: repeat(auto-fill, var(--card-pool-width));
  grid-auto-rows: calc(var(--card-pool-height) - var(--card-pool-row-overlap));
  justify-content: start;
  align-content: start;
  padding-bottom: var(--card-pool-row-overlap);
}

.card-pool .card {
  width: var(--card-pool-width);
  height: var(--card-pool-height);
}

/* このデッキのカード: 120px・1行4枚固定（カードプールよりスペースを抑える） */
#current-deck-cards {
  grid-template-columns: repeat(4, var(--card-pool-width));
  gap: 16px;
  align-content: start;
  justify-content: start;
}

#current-deck-cards .card {
  width: var(--card-pool-width);
  height: var(--card-pool-height);
  margin: 0;
}

.card {
  position: relative;
  border-radius: var(--radius-md);
  overflow: hidden;
  border: 2px solid transparent;
  cursor: pointer;
  background: #f9fafb;
  width: var(--card-width);
  height: var(--card-height);
  flex-shrink: 0;
}

.card img {
  width: 100%;
  height: 100%;
  display: block;
  object-fit: cover;
  object-position: 50% 65%;
}

.card.selected {
  border-color: var(--sidebar-accent);
  box-shadow: 0 0 0 2px rgba(6, 81, 84, 0.3);
}

/* カードプール: 採用=通常表示、非採用=モノクロ。青枠は使わない */
.card-pool .card.selected {
  border-color: transparent !important;
  box-shadow: none !important;
  filter: none;
}
.card-pool .card:not(.selected) {
  filter: grayscale(1);
  opacity: 0.85;
}

/* 画像レベルでのモノクロ制御（確実に効かせる） */
.card-pool .card img {
  filter: grayscale(1);
  opacity: 0.85;
}
.card-pool .card.selected img {
  filter: none;
  opacity: 1;
}

/* デッキ入れ替えビューではモノクロ化せず、左右同じサイズで表示する */
#view-switcher .deck-cards .card,
#view-switcher .card-pool .card {
  width: var(--card-width);
  height: var(--card-height);
}
#view-switcher .card-pool .card,
#view-switcher .card-pool .card img {
  filter: none;
  opacity: 1;
}

#current-deck-cards {
  position: relative;
}

#current-deck-cards .card.dragging {
  opacity: 0.5;
}

.deck-drop-indicator {
  position: absolute;
  width: 6px;
  background: #d7003a;
  border-radius: 3px;
  pointer-events: none;
  z-index: 40;
  display: none;
  top: 0;
  left: 0;
  box-shadow: 0 0 0 2px rgba(129, 140, 248, 0.35);
}

.card-badge {
  position: absolute;
  bottom: 4px;
  right: 4px;
  background: rgba(17, 24, 39, 0.7);
  color: white;
  font-size: 0.7rem;
  padding: 2px 5px;
  border-radius: 999px;
}

.card.energy-card .energy-hover-panel {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  background: rgba(15, 23, 42, 0.85);
  color: #f9fafb;
  font-size: 0.8rem;
  opacity: 0;
  transition: opacity 0.15s ease-out;
}

.card.energy-card:hover .energy-hover-panel {
  opacity: 1;
}

.energy-controls {
  display: flex;
  align-items: center;
  gap: 6px;
  margin-top: 4px;
}

.energy-controls button {
  border: none;
  border-radius: 999px;
  padding: 2px 8px;
  font-size: 0.8rem;
  cursor: pointer;
  background: #e5e7eb;
  color: #111827;
}

.energy-controls .energy-count {
  min-width: 1.5em;
  text-align: center;
}

.switcher-result h2 {
  margin-top: 0;
}

.modal {
  position: fixed;
  inset: 0;
  background: rgba(15, 23, 42, 0.45);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 50;
}

.modal.hidden {
  display: none;
}

.modal-content {
  background: white;
  border-radius: var(--radius-lg);
  padding: 20px;
  width: min(400px, 90vw);
  box-shadow: var(--shadow-soft);
}

.deck-viewer-content {
  width: min(900px, 95vw);
  max-height: 90vh;
  display: flex;
  flex-direction: column;
}

.deck-viewer-content #deck-viewer-grid {
  flex: 1;
  overflow: auto;
  grid-template-columns: repeat(auto-fit, minmax(60px, 1fr));
}

/* カードプールオーバーレイ: 枠内だけでスクロール */
#pool-modal .modal-content {
  max-height: 90vh;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.pool-modal-header {
  display: flex;
  align-items: center;
  gap: 12px;
  flex-wrap: wrap;
  margin-bottom: 8px;
}
.pool-modal-header h2 {
  margin: 0;
}
.pool-modal-filter .filter-trigger {
  min-width: 120px;
}
.pool-modal-filter .filter-dropdown {
  position: fixed;
  z-index: 60;
}
#pool-modal .filter-submenu-panel {
  z-index: 61;
}

#pool-modal-grid {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  scrollbar-gutter: stable;
  display: grid;
  grid-template-columns: repeat(auto-fill, var(--card-pool-width));
  grid-auto-rows: calc(var(--card-pool-height) - var(--card-pool-row-overlap));
  gap: 12px;
  justify-content: start;
  align-content: start;
  padding-bottom: var(--card-pool-row-overlap);
}

#pool-modal-grid .card {
  width: var(--card-pool-width);
  height: var(--card-pool-height);
}

/* オーバーレイのカードプールでもモノクロ化（スマホ幅で表示時） */
#pool-modal-grid .card.selected {
  border-color: transparent !important;
  box-shadow: none !important;
  filter: none;
}
#pool-modal-grid .card:not(.selected) {
  filter: grayscale(1);
  opacity: 0.85;
}
#pool-modal-grid .card img {
  filter: grayscale(1);
  opacity: 0.85;
}
#pool-modal-grid .card.selected img {
  filter: none;
  opacity: 1;
}

/* オーバーレイ表示中は背面をスクロールさせない */
body.pool-modal-open {
  overflow: hidden;
}

.modal-field {
  display: flex;
  flex-direction: column;
  gap: 6px;
  margin: 12px 0 20px;
}

.modal-actions {
  display: flex;
  justify-content: flex-end;
  gap: 10px;
}

.delete-confirm-btn {
  color: #d7003a;
  border-color: #d7003a;
  background: transparent;
}
.delete-confirm-btn:hover {
  background: rgba(215, 0, 58, 0.1);
}

@media (max-width: 900px) {
  .app-shell {
    flex-direction: column;
  }

  .sidebar {
    width: 100%;
    flex-direction: row;
    align-items: center;
    justify-content: space-between;
  }

  .nav {
    flex-direction: row;
  }

  .main {
    padding: 16px;
  }
}

/* カードプール欄の幅が500px以下になるタイミングで移行（1fr 1.4fr から逆算） */
@media (max-width: 1050px) {
  /* デッキ作成ビューのみ 1 カラム＋カードプールを隠してボタンでオーバーレイ */
  #view-builder .builder-layout {
    grid-template-columns: 1fr;
  }

  #view-builder .card-pool {
    display: none;
  }

  .pool-toggle-btn {
    display: inline-flex;
  }

  /* スマホ幅: このデッキのカードは列数を減らして対応（カードサイズは維持） */
  #view-builder #current-deck-cards {
    grid-template-columns: repeat(auto-fill, var(--card-pool-width));
    justify-content: start;
  }

  /* デッキ入れ替えビューでは常に左右 2 カラム表示を維持 */
  #view-switcher .builder-layout {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }
}

@media (max-width: 600px) {
  .deck-list {
    grid-template-columns: 1fr;
  }

  /* スマホ幅: デッキ入れ替えでも抜く/入れる両方のボックスを横並びで維持しつつ、カードは 1 行 1 枚に */
  #view-switcher .builder-layout {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }

  #view-switcher .card-grid {
    grid-template-columns: repeat(1, minmax(120px, 1fr));
  }
}

