/* ========== 基本 ========== */
* { box-sizing: border-box; margin: 0; padding: 0; }
:root {
  --bg: #171522;
  --bg2: #221f33;
  --panel: #2a2740;
  --panel2: #353052;
  --text: #eae6f5;
  --sub: #a79fc4;
  --accent: #e8a33d;
  --accent2: #9b59d0;
  --danger: #d05959;
  --radius: 10px;
}
body {
  background: var(--bg);
  color: var(--text);
  font-family: "Hiragino Kaku Gothic ProN", "Yu Gothic", "Meiryo", sans-serif;
  min-height: 100vh;
}

.app-header {
  background: linear-gradient(90deg, #241f3a, #3a2b55);
  padding: 12px 20px;
  display: flex;
  align-items: center;
  gap: 24px;
  flex-wrap: wrap;
  border-bottom: 2px solid var(--accent);
}
.app-header h1 { font-size: 1.3rem; letter-spacing: 1px; }
.tabs { display: flex; gap: 8px; flex-wrap: wrap; }
.tab-btn {
  background: transparent;
  color: var(--sub);
  border: 1px solid #4a4468;
  border-radius: 20px;
  padding: 8px 16px;
  cursor: pointer;
  font-size: 0.95rem;
  transition: all .15s;
}
.tab-btn:hover { color: var(--text); border-color: var(--accent); }
.tab-btn.active { background: var(--accent); color: #221a08; border-color: var(--accent); font-weight: bold; }

main { padding: 20px; max-width: 1400px; margin: 0 auto; }
.tab-page { display: none; }
.tab-page.active { display: block; }

.panel {
  background: var(--panel);
  border-radius: var(--radius);
  padding: 20px;
  border: 1px solid #3d3760;
}

h2 { font-size: 1.1rem; margin-bottom: 14px; color: var(--accent); }
h3 { font-size: 0.95rem; margin-bottom: 8px; color: var(--sub); }

label { display: block; margin: 12px 0 4px; font-size: 0.85rem; color: var(--sub); }
input[type=text], textarea, select {
  width: 100%;
  background: var(--bg2);
  border: 1px solid #4a4468;
  border-radius: 6px;
  color: var(--text);
  padding: 8px 10px;
  font-size: 0.95rem;
  font-family: inherit;
}
input:focus, textarea:focus, select:focus { outline: none; border-color: var(--accent); }

.btn {
  background: var(--panel2);
  color: var(--text);
  border: 1px solid #55507a;
  border-radius: 6px;
  padding: 8px 14px;
  cursor: pointer;
  font-size: 0.9rem;
  font-family: inherit;
  transition: filter .15s;
}
.btn:hover { filter: brightness(1.2); }
.btn.primary { background: var(--accent); color: #221a08; border-color: var(--accent); font-weight: bold; }
.btn.danger { background: var(--danger); border-color: var(--danger); color: #fff; }
.btn.big { padding: 12px 22px; font-size: 1rem; }
.btn.small { padding: 4px 10px; font-size: 0.8rem; }

.msg { margin-top: 10px; font-size: 0.85rem; color: var(--accent); min-height: 1.2em; }
.badge { background: var(--accent2); color: #fff; border-radius: 10px; padding: 2px 10px; font-size: 0.8rem; }

/* ========== カード作成 ========== */
.create-layout { display: grid; grid-template-columns: minmax(300px, 480px) 1fr; gap: 20px; }
@media (max-width: 800px) { .create-layout { grid-template-columns: 1fr; } }

.mana-cost-editor { background: var(--bg2); border-radius: 6px; padding: 8px 12px; }
.mana-row { display: flex; align-items: center; gap: 8px; padding: 3px 0; }
.mana-label { width: 44px; font-size: 0.85rem; color: var(--sub); }
.step {
  width: 26px; height: 26px; border-radius: 50%;
  border: 1px solid #55507a; background: var(--panel2); color: var(--text);
  cursor: pointer; font-size: 1rem; line-height: 1;
}
.mana-count { width: 24px; text-align: center; font-weight: bold; }

.pt-inputs { display: flex; align-items: center; gap: 8px; }
.pt-inputs input { width: 70px; text-align: center; }

.img-controls { display: flex; gap: 10px; align-items: center; }
.form-actions { margin-top: 18px; display: flex; gap: 10px; flex-wrap: wrap; }

.create-preview { display: flex; flex-direction: column; align-items: center; }
.create-preview h2 { align-self: flex-start; }

/* ========== MTG風カードフレーム ========== */
.mcard {
  width: 320px;
  height: 447px;
  border-radius: 16px;
  padding: 12px;
  position: relative;
  box-shadow: 0 4px 16px rgba(0,0,0,.6);
  display: flex;
  flex-direction: column;
  color: #1a1a1a;
  font-family: "Hiragino Mincho ProN", "Yu Mincho", serif;
  flex-shrink: 0;
  user-select: none;
}
/* フレーム色 */
.mcard.frame-W { background: linear-gradient(160deg, #f5f0dc, #d8d2b4); }
.mcard.frame-U { background: linear-gradient(160deg, #b8d4ef, #7fa8d4); }
.mcard.frame-B { background: linear-gradient(160deg, #b6aeb0, #7d7378); }
.mcard.frame-R { background: linear-gradient(160deg, #eec1a4, #d68d68); }
.mcard.frame-G { background: linear-gradient(160deg, #bcd6b3, #8db183); }
.mcard.frame-gold  { background: linear-gradient(160deg, #f0dfa4, #d4b662); }
.mcard.frame-art   { background: linear-gradient(160deg, #d7dade, #a8adb5); }
.mcard.frame-land  { background: linear-gradient(160deg, #e0d4c0, #b3a284); }

.mcard-title {
  display: flex; justify-content: space-between; align-items: center;
  background: rgba(255,255,255,.75);
  border: 1px solid rgba(0,0,0,.45);
  border-radius: 8px;
  padding: 4px 8px;
  font-weight: bold;
  font-size: 0.95rem;
  gap: 4px;
}
.mcard-title .nm { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.dfc-marker { color: #6a4fa0; margin-right: 3px; font-family: sans-serif; }
.mcard-cost { display: flex; gap: 2px; flex-shrink: 0; }

.mana-symbol {
  display: inline-flex; align-items: center; justify-content: center;
  width: 20px; height: 20px; border-radius: 50%;
  font-size: 0.75rem; font-weight: bold;
  border: 1px solid rgba(0,0,0,.4);
  box-shadow: -1px 1px 0 rgba(0,0,0,.35);
  font-family: sans-serif;
  color: #1a1a1a;
}
.mana-W { background: #fffbd5; }
.mana-U { background: #aae0fa; }
.mana-B { background: #ccc2c0; }
.mana-R { background: #f9aa8f; }
.mana-G { background: #9bd3ae; }
.mana-C { background: #cac5c0; }
.mana-T { background: #cac5c0; }

.mcard-art {
  margin-top: 6px;
  height: 46%;
  border: 2px solid rgba(0,0,0,.5);
  border-radius: 4px;
  background: #202020 center/cover no-repeat;
  display: flex; align-items: center; justify-content: center;
  color: #666; font-size: 2.5rem;
  overflow: hidden;
}
.mcard-art img { width: 100%; height: 100%; object-fit: cover; }

.mcard-type {
  margin-top: 6px;
  background: rgba(255,255,255,.75);
  border: 1px solid rgba(0,0,0,.45);
  border-radius: 6px;
  padding: 3px 8px;
  font-size: 0.8rem;
  font-weight: bold;
  display: flex; justify-content: space-between; align-items: center;
  gap: 4px;
}
.mcard-type span:first-child { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.rarity { font-family: sans-serif; font-size: 0.75rem; }
.rarity-C { color: #333; }
.rarity-U { color: #6e8296; text-shadow: 0 0 2px #fff; }
.rarity-R { color: #a58e4a; text-shadow: 0 0 2px #fff; }
.rarity-M { color: #bf4427; text-shadow: 0 0 2px #fff; }

.mcard-text {
  margin-top: 6px;
  flex: 1;
  background: rgba(255,255,255,.82);
  border: 1px solid rgba(0,0,0,.45);
  border-radius: 6px;
  padding: 6px 8px;
  font-size: 0.78rem;
  line-height: 1.45;
  overflow-y: auto;
}
.mcard-text .flavor { font-style: italic; color: #444; margin-top: 6px; display: block; }
.mcard-text .mana-symbol { width: 14px; height: 14px; font-size: 0.6rem; vertical-align: -2px; }

.mcard-pt {
  position: absolute;
  bottom: 6px; right: 10px;
  background: rgba(255,255,255,.92);
  border: 1px solid rgba(0,0,0,.55);
  border-radius: 8px;
  padding: 2px 12px;
  font-weight: bold;
  font-size: 1rem;
  box-shadow: 0 1px 3px rgba(0,0,0,.4);
}

/* ========== コレクション ========== */
.collection-header { display: flex; justify-content: space-between; align-items: center; flex-wrap: wrap; gap: 10px; margin-bottom: 14px; }
.collection-tools { display: flex; gap: 10px; flex-wrap: wrap; align-items: center; }
.collection-tools input { width: 220px; }
.collection-tools select { width: auto; }

.card-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(180px, 1fr));
  gap: 16px;
}
.card-cell { display: flex; flex-direction: column; align-items: center; gap: 6px; }
.card-cell .mcard { cursor: pointer; transition: transform .15s; }
.card-cell .mcard:hover { transform: translateY(-4px); }
.card-cell-actions { display: flex; gap: 6px; }

/* 縮小カード（グリッド用・盤面用）: transformで縮小 */
.mcard-scale { transform-origin: top left; }

.empty-note { color: var(--sub); padding: 30px; text-align: center; grid-column: 1/-1; }

/* ========== デッキ ========== */
.deck-layout { display: grid; grid-template-columns: 280px 1fr; gap: 20px; }
@media (max-width: 900px) { .deck-layout { grid-template-columns: 1fr; } }

#deck-list { display: flex; flex-direction: column; gap: 8px; margin-bottom: 14px; }
.deck-item {
  background: var(--bg2);
  border: 1px solid #4a4468;
  border-radius: 6px;
  padding: 10px 12px;
  cursor: pointer;
  display: flex; justify-content: space-between; align-items: center;
}
.deck-item:hover, .deck-item.selected { border-color: var(--accent); }
.deck-item .cnt { color: var(--sub); font-size: 0.8rem; }

.deck-edit-header { display: flex; gap: 10px; align-items: center; flex-wrap: wrap; margin-bottom: 14px; }
.deck-edit-header input { max-width: 240px; }
.deck-edit-body { display: grid; grid-template-columns: 1fr 1fr; gap: 20px; }
@media (max-width: 900px) { .deck-edit-body { grid-template-columns: 1fr; } }

.deck-card-list { display: flex; flex-direction: column; gap: 4px; margin-top: 8px; max-height: 60vh; overflow-y: auto; }
.deck-card-row {
  display: flex; align-items: center; gap: 8px;
  background: var(--bg2);
  border: 1px solid #423d63;
  border-radius: 6px;
  padding: 5px 10px;
  font-size: 0.85rem;
}
.deck-card-row .nm { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; cursor: pointer; }
.deck-card-row .nm:hover { color: var(--accent); }
.deck-card-row .cost { display: flex; gap: 1px; }
.deck-card-row .cost .mana-symbol { width: 15px; height: 15px; font-size: 0.62rem; }
.deck-card-row .num { font-weight: bold; min-width: 30px; text-align: center; }

/* ========== 対戦ロビー ========== */
.battle-lobby { max-width: 760px; margin: 0 auto; }
.battle-lobby input, .battle-lobby select { max-width: 300px; }
.lobby-actions { display: grid; grid-template-columns: 1fr 1fr; gap: 20px; margin-top: 20px; }
@media (max-width: 700px) { .lobby-actions { grid-template-columns: 1fr; } }
.lobby-box {
  background: var(--bg2);
  border: 1px solid #4a4468;
  border-radius: var(--radius);
  padding: 16px;
  display: flex; flex-direction: column; gap: 10px;
}
.lobby-box p { font-size: 0.82rem; color: var(--sub); }
.room-code {
  font-size: 2rem; font-weight: bold; letter-spacing: 6px;
  color: var(--accent); text-align: center;
  background: var(--bg); border-radius: 8px; padding: 10px;
  font-family: monospace;
}

/* ========== ゲーム画面 ========== */
#battle-game { position: relative; }
.game-topbar, .game-bottombar { display: flex; align-items: center; gap: 14px; flex-wrap: wrap; }
.player-chip {
  display: flex; align-items: center; gap: 12px;
  background: var(--panel);
  border: 1px solid #3d3760;
  border-radius: 30px;
  padding: 6px 16px;
  flex-wrap: wrap;
}
.player-chip .pname { font-weight: bold; }
.player-chip.me { border-color: var(--accent); }
.life { font-size: 1.4rem; font-weight: bold; color: #7fd48a; min-width: 40px; text-align: center; }
.life-controls { display: flex; align-items: center; gap: 4px; }
.life-btn {
  width: 30px; height: 26px; border-radius: 6px;
  border: 1px solid #55507a; background: var(--panel2); color: var(--text);
  cursor: pointer; font-size: 0.75rem;
}
.zone-count { font-size: 0.82rem; color: var(--sub); }
.zone-count b { color: var(--text); font-size: 0.95rem; }
.zone-count.clickable { cursor: pointer; text-decoration: underline dotted; }
.zone-count.clickable:hover { color: var(--accent); }
.pcounter-badges { display: flex; gap: 4px; align-items: center; }
.pcounter-badges:empty { display: none; }

.turn-indicator { font-size: 0.9rem; color: var(--accent); font-weight: bold; margin-left: auto; padding-right: 200px; }

.battlefield {
  min-height: 170px;
  background: var(--bg2);
  border: 1px solid #3d3760;
  border-radius: var(--radius);
  margin: 10px 0;
  padding: 8px;
  display: flex;
  flex-direction: column;
  gap: 8px;
}
.battlefield.my-field { border-color: #5a4d27; background: #24222e; }

.field-zone {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
  align-items: flex-start;
  align-content: flex-start;
  padding: 4px;
}
.field-zone.main-zone { min-height: 165px; flex: 1; }
.field-zone.land-zone {
  position: relative;
  background: rgba(120, 104, 60, .12);
  border: 1px dashed #5a5340;
  border-radius: 8px;
  padding: 8px 8px 8px 30px;
  min-height: 60px;
}
.field-zone.land-zone::before {
  content: '土地';
  position: absolute;
  left: 8px; top: 50%;
  transform: translateY(-50%);
  writing-mode: vertical-rl;
  font-size: 0.7rem;
  color: var(--sub);
  letter-spacing: 3px;
}

/* 盤面のカード */
.field-card { position: relative; width: 112px; height: 157px; transition: transform .2s; cursor: pointer; }
.field-card .mcard { width: 320px; height: 447px; transform: scale(0.35); transform-origin: top left; }
.field-card.tapped { transform: rotate(90deg) translateY(-22px); margin-right: 40px; }
.field-card .counter-badges {
  position: absolute; top: -8px; right: -8px; z-index: 2;
  display: flex; flex-direction: column; gap: 3px;
  align-items: flex-end;
}
.counter-badge {
  background: var(--accent2); color: #fff;
  border-radius: 12px; min-width: 24px; height: 22px;
  display: flex; align-items: center; justify-content: center;
  padding: 0 6px;
  font-size: 0.72rem; font-weight: bold;
  border: 2px solid #fff;
  box-shadow: 0 1px 4px rgba(0,0,0,.4);
  white-space: nowrap;
}

/* 手札 */
.game-bottombar { flex-direction: column; align-items: stretch; }
.hand-row { display: flex; gap: 10px; align-items: stretch; margin-top: 8px; }
.hand-area {
  display: flex; gap: 8px;
  overflow-x: auto;
  padding: 10px;
  background: var(--panel);
  border-radius: var(--radius);
  border: 1px solid #3d3760;
  min-height: 180px;
  flex: 1;
}

/* 山札の山（カード裏面） */
.deck-pile {
  position: relative;
  width: 120px;
  flex-shrink: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  user-select: none;
}
.deck-back {
  width: 112px; height: 157px;
  border-radius: 10px;
  background:
    radial-gradient(circle at 50% 42%, rgba(232,163,61,.25), transparent 55%),
    repeating-linear-gradient(45deg, #3a2b55 0 10px, #332450 10px 20px);
  border: 3px solid #6a5a9c;
  box-shadow: 2px 3px 0 #1a1526, 4px 6px 0 #141020, 0 4px 12px rgba(0,0,0,.5);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 2.2rem;
  transition: transform .12s;
}
.deck-pile:hover .deck-back { transform: translateY(-4px); }
.deck-pile.empty .deck-back {
  background: var(--bg2);
  border-style: dashed;
  box-shadow: none;
  opacity: .5;
}
.deck-pile-count {
  position: absolute;
  bottom: 6px; left: 50%;
  transform: translateX(-50%);
  background: rgba(20, 16, 32, .88);
  color: var(--accent);
  font-weight: bold;
  font-size: 0.9rem;
  border: 1px solid #6a5a9c;
  border-radius: 12px;
  padding: 2px 12px;
  pointer-events: none;
}
.hand-card { position: relative; width: 112px; height: 157px; flex-shrink: 0; cursor: pointer; transition: transform .15s; }
.hand-card:hover { transform: translateY(-6px); }
.hand-card .mcard { width: 320px; height: 447px; transform: scale(0.35); transform-origin: top left; }

/* フェイズバー */
.phase-bar {
  display: flex; align-items: center; gap: 14px; flex-wrap: wrap;
  background: var(--panel);
  border-radius: 30px;
  border: 1px solid #3d3760;
  padding: 6px 14px;
}
.phases { display: flex; gap: 4px; flex-wrap: wrap; }
.phase-pill {
  font-size: 0.75rem;
  padding: 4px 10px;
  border-radius: 14px;
  color: var(--sub);
  border: 1px solid transparent;
}
.phase-pill.current { background: var(--accent); color: #221a08; font-weight: bold; }
.phase-actions { margin-left: auto; display: flex; gap: 8px; }

/* 対象指定 */
#target-banner {
  position: fixed;
  top: 64px; left: 50%;
  transform: translateX(-50%);
  z-index: 95;
  background: #c0392b;
  color: #fff;
  font-weight: bold;
  font-size: 0.9rem;
  padding: 8px 18px;
  border-radius: 24px;
  box-shadow: 0 4px 14px rgba(0,0,0,.5);
  display: flex;
  align-items: center;
  gap: 10px;
  animation: bannerBlink 1.2s ease-in-out infinite;
}
@keyframes bannerBlink { 50% { background: #e74c3c; } }

.field-card.targeted .mcard { box-shadow: 0 0 0 12px #ff5252, 0 4px 16px rgba(0,0,0,.6); }
.field-card.targeted { animation: targetPulse 0.7s ease-in-out 8; }
.player-chip.targeted {
  box-shadow: 0 0 0 3px #ff5252;
  animation: targetPulse 0.7s ease-in-out 8;
}
@keyframes targetPulse { 50% { opacity: 0.55; } }

/* 複数対象の選択中（確定前） */
.field-card.target-selected .mcard { box-shadow: 0 0 0 12px #f39c12, 0 4px 16px rgba(0,0,0,.6); }
.player-chip.target-selected { box-shadow: 0 0 0 3px #f39c12; }

/* 割り振り数値バッジ */
.player-chip { position: relative; }
.target-num {
  position: absolute;
  top: -12px; left: -12px;
  z-index: 5;
  background: #ff5252;
  color: #fff;
  font-weight: bold;
  font-size: 0.85rem;
  border-radius: 50%;
  min-width: 28px; height: 28px;
  display: flex; align-items: center; justify-content: center;
  border: 2px solid #fff;
  box-shadow: 0 2px 6px rgba(0,0,0,.5);
  pointer-events: none;
}

/* ログ */
.game-log {
  position: fixed;
  right: 12px; top: 130px;
  width: 190px;
  max-height: 50vh;
  background: rgba(30,27,48,.92);
  border: 1px solid #3d3760;
  border-radius: 8px;
  font-size: 0.75rem;
  overflow: hidden;
  display: flex; flex-direction: column;
  z-index: 50;
}
.log-title { padding: 6px 10px; color: var(--accent); font-weight: bold; border-bottom: 1px solid #3d3760; }
#log-body { overflow-y: auto; padding: 6px 10px; display: flex; flex-direction: column-reverse; }
#log-body div { padding: 2px 0; border-bottom: 1px dashed #333050; color: var(--sub); }
#log-body div b { color: var(--text); }
@media (max-width: 1100px) {
  .game-log { position: static; width: auto; max-height: 160px; margin-top: 10px; }
  .turn-indicator { padding-right: 0; }
}

/* ========== モーダル・メニュー ========== */
#modal-backdrop {
  position: fixed; inset: 0;
  background: rgba(0,0,0,.7);
  display: flex; align-items: center; justify-content: center;
  z-index: 100;
}
#modal-box {
  background: var(--panel);
  border: 1px solid #55507a;
  border-radius: var(--radius);
  padding: 24px;
  max-width: 90vw; max-height: 90vh;
  overflow-y: auto;
  position: relative;
}
#modal-close {
  position: absolute; top: 8px; right: 8px;
  background: transparent; border: none; color: var(--sub);
  font-size: 1.1rem; cursor: pointer;
}
#modal-content textarea { width: 100%; min-width: min(480px, 80vw); font-family: monospace; font-size: 0.7rem; }
.modal-actions { display: flex; gap: 10px; margin-top: 12px; flex-wrap: wrap; }
.modal-grave-list { display: flex; flex-wrap: wrap; gap: 10px; max-width: 720px; }

#ctx-menu {
  position: fixed;
  background: var(--panel2);
  border: 1px solid #6a639c;
  border-radius: 8px;
  z-index: 200;
  min-width: 160px;
  box-shadow: 0 4px 14px rgba(0,0,0,.5);
  overflow: hidden;
}
#ctx-menu button {
  display: block; width: 100%;
  background: transparent; border: none;
  color: var(--text); text-align: left;
  padding: 9px 14px; cursor: pointer; font-size: 0.85rem;
  font-family: inherit;
}
#ctx-menu button:hover { background: var(--accent); color: #221a08; }
