:root {
  --bg: #24305e;
  --paper: #f6f1e7;
  --panel: #ffffff;
  --ink: #1b1b2f;
  --muted: #6b6b7b;
  --accent: #f76c6c;
  --accent2: #ffd166;
  --hp-good: #2ecc71;
  --hp-mid: #f5c518;
  --hp-low: #e74c3c;
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body {
  background: var(--bg);
  font-family: 'M PLUS Rounded 1c', 'Hiragino Maru Gothic ProN', 'Meiryo', sans-serif;
  color: var(--ink);
  display: flex;
  justify-content: center;
}

#app {
  width: 100%;
  max-width: 480px;
  height: 100dvh;
  background: var(--paper);
  display: flex;
  flex-direction: column;
  box-shadow: 0 0 40px rgba(0, 0, 0, .35);
}

h1, h2 { margin: 0; }
h2 { font-size: 1.4rem; }
.hint { color: var(--muted); margin: 0; font-size: .9rem; }

/* ===== 画面 ===== */
/* 画面の高さは固定し、入りきらない分は画面内でスクロール */
.screen { display: none; flex: 1; min-height: 0; overflow-y: auto; flex-direction: column; padding: 16px; gap: 12px; }
.screen.active { display: flex; }
.content {
  flex: 1;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  gap: 16px;
  text-align: center;
}
.screen-title { font-size: 1.2rem; }
.screen-title small { color: var(--muted); font-size: .85rem; }

/* ===== タイトル ===== */
.logo {
  background: var(--accent);
  color: #fff;
  border: 4px solid var(--ink);
  border-radius: 18px;
  padding: 20px 24px;
  box-shadow: 0 8px 0 var(--ink);
  transform: rotate(-2deg);
}
.logo h1 { font-size: 2rem; line-height: 1.25; text-shadow: 3px 3px 0 var(--ink); }
.logo-sub { display: block; letter-spacing: .3em; font-size: .75rem; color: var(--accent2); }

/* ===== メニューとボタン ===== */
.menu {
  display: grid;
  gap: 10px;
  background: var(--panel);
  border: 3px solid var(--ink);
  border-radius: 14px;
  padding: 12px;
}
.menu.two { grid-template-columns: 1fr 1fr; }
.right { display: flex; justify-content: flex-end; }

.btn {
  font: inherit;
  font-weight: 800;
  font-size: 1rem;
  padding: 13px 12px;
  border: 3px solid var(--ink);
  border-radius: 10px;
  background: #fff;
  color: var(--ink);
  cursor: pointer;
  box-shadow: 0 4px 0 var(--ink);
}
.btn:active:not(:disabled) { transform: translateY(3px); box-shadow: 0 1px 0 var(--ink); }
.btn.primary { background: var(--accent); color: #fff; }
.btn.yellow { background: var(--accent2); }
.btn.small { padding: 6px 10px; font-size: .8rem; box-shadow: 0 3px 0 var(--ink); }
.btn:disabled { opacity: .4; cursor: not-allowed; }
.btn .sub { display: block; font-size: .72rem; font-weight: 500; opacity: .85; }

.name-input {
  font: inherit;
  font-size: 1.2rem;
  padding: 12px;
  border: 3px solid var(--ink);
  border-radius: 10px;
  width: 100%;
}
.big-name {
  font-size: 1.8rem;
  font-weight: 800;
  padding: 8px 16px;
  word-break: break-all;
}

/* ===== 性能結果 ===== */
.result-box {
  background: var(--panel);
  border: 3px double var(--ink);
  border-width: 6px;
  border-radius: 12px;
  padding: 16px;
  width: 100%;
}
.result-box p { margin: 0; }
.stats { display: grid; gap: 8px; margin-top: 8px; text-align: left; }
.stat-row { display: grid; grid-template-columns: 4em 1fr 3em; align-items: center; gap: 8px; }
.stat-label { font-weight: 800; }
.stat-bar { height: 10px; background: #e5e1d8; border-radius: 5px; overflow: hidden; }
.stat-bar i { display: block; height: 100%; width: 0; background: var(--accent); border-radius: 5px; transition: width .8s ease-out; }
.stat-val { text-align: right; font-weight: 800; font-variant-numeric: tabular-nums; }

/* ===== アバター ===== */
.avatar {
  width: 64px;
  height: 64px;
  flex: none;
  border: 3px solid var(--ink);
  border-radius: 50%;
  overflow: hidden;
  display: grid;
  place-items: center;
  color: #fff;
  font-weight: 800;
  font-size: 1.6rem;
  text-shadow: 2px 2px 0 rgba(0, 0, 0, .3);
  background: #999;
}
.avatar img { width: 100%; height: 100%; object-fit: cover; }

/* ===== カード一覧（コレクション・選択） ===== */
.card-list {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  display: grid;
  gap: 10px;
  align-content: start;
  padding: 10px 8px 4px 0; /* 選択番号のバッジが切れないように */
}
.empty { text-align: center; color: var(--muted); padding: 40px 0; }
.card {
  display: flex;
  gap: 12px;
  align-items: center;
  background: var(--panel);
  border: 3px solid var(--ink);
  border-radius: 12px;
  padding: 10px;
  text-align: left;
  font: inherit;
  color: inherit;
  width: 100%;
  position: relative;
}
.card-body { flex: 1; min-width: 0; }
.card-name { font-weight: 800; font-size: 1.05rem; word-break: break-all; }
.card-stats { display: grid; grid-template-columns: repeat(3, 1fr); gap: 2px 8px; font-size: .8rem; margin-top: 4px; }
.card-stats b { font-variant-numeric: tabular-nums; }
.card-actions { display: flex; gap: 6px; margin-top: 8px; flex-wrap: wrap; }
.card.selectable { cursor: pointer; }
.card.selected { background: #fff4d6; border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent) inset; }
.order {
  position: absolute;
  top: -8px;
  right: -6px;
  width: 30px;
  height: 30px;
  border-radius: 50%;
  background: var(--accent);
  color: #fff;
  border: 3px solid var(--ink);
  display: grid;
  place-items: center;
  font-weight: 800;
}

/* ===== バトル ===== */
.battle { padding: 0; gap: 0; }
.battle-top {
  display: flex;
  justify-content: space-between;
  padding: 8px 14px;
  background: var(--ink);
  color: #fff;
  font-size: .85rem;
  font-weight: 800;
}
.field {
  flex: 1;
  display: grid;
  grid-template-rows: 1fr 1fr;
  padding: 12px 16px;
  background: linear-gradient(#bfe6ff 0%, #e8f7ff 45%, #b7e4a8 45%, #8fd17c 100%);
  min-height: 300px;
}
.side { display: flex; align-items: center; justify-content: space-between; gap: 12px; }
.status {
  background: var(--panel);
  border: 3px solid var(--ink);
  border-radius: 10px;
  padding: 8px 10px;
  width: 58%;
}
.status-name { display: flex; justify-content: space-between; gap: 6px; font-weight: 800; word-break: break-all; }
.dots { white-space: nowrap; letter-spacing: 2px; font-size: .8rem; }
.dot-alive { color: var(--accent); }
.dot-dead { color: #bbb; }
.hpbar { height: 10px; margin-top: 6px; background: #ddd; border: 2px solid var(--ink); border-radius: 6px; overflow: hidden; }
.hpbar i { display: block; height: 100%; background: var(--hp-good); transition: width .5s ease-out, background-color .5s; }
.hpbar i.mid { background: var(--hp-mid); }
.hpbar i.low { background: var(--hp-low); }
.hp-text { text-align: right; font-size: .85rem; font-weight: 800; margin-top: 2px; font-variant-numeric: tabular-nums; }
.mon .avatar { width: 110px; height: 110px; font-size: 3rem; box-shadow: 0 10px 0 rgba(0, 0, 0, .15); }
.mon.fainted { opacity: .25; filter: grayscale(1); transition: opacity .5s; }
.mon.shake { animation: shake .4s; }
@keyframes shake {
  0%, 100% { transform: translateX(0); }
  20% { transform: translateX(-10px); }
  40% { transform: translateX(10px); }
  60% { transform: translateX(-6px); }
  80% { transform: translateX(6px); }
}

.bottom { padding: 12px; display: grid; gap: 10px; background: var(--paper); border-top: 4px solid var(--ink); }
.msg {
  min-height: 3.6em;
  background: var(--panel);
  border: 3px solid var(--ink);
  border-radius: 10px;
  padding: 10px 12px;
  font-weight: 800;
  line-height: 1.5;
}
.cmd { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; min-height: 52px; }
.full { grid-column: 1 / -1; }

/* 技 */
.moves-title { margin-top: 12px !important; font-weight: 800; text-align: left; }
.moves { display: grid; grid-template-columns: 1fr 1fr; gap: 4px; margin-top: 6px; }
.move {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 2px 4px;
  font-size: .75rem;
  font-weight: 800;
  text-align: left;
  border: 2px solid var(--ink);
  border-radius: 6px;
  padding: 3px 6px;
  word-break: break-all;
}
.move b { margin-left: auto; font-weight: 500; white-space: nowrap; }
.move-tag {
  font-size: .65rem;
  color: #fff;
  border-radius: 4px;
  padding: 0 4px;
  white-space: nowrap;
}
.move-phys { background: #ffe3dc; }
.move-mag { background: #e4e0ff; }
.move-special { background: #d8f5e3; }
.move-phys .move-tag { background: #d9480f; }
.move-mag .move-tag { background: #6741d9; }
.move-special .move-tag { background: #2b8a3e; }

/* 状態異常・能力変化のバッジ */
.tags { display: flex; flex-wrap: wrap; gap: 3px; margin-top: 4px; }
.tags:empty { display: none; }
.tag { font-size: .65rem; font-weight: 800; color: #fff; border-radius: 4px; padding: 1px 5px; }
.tag.bad { background: #862e9c; }
.tag.up { background: #e8590c; }
.tag.down { background: #1971c2; }
.move-btn { text-align: left; font-size: .85rem; padding: 8px 10px; }
.move-btn .move-tag { margin-right: 4px; vertical-align: 2px; }

/* 交代画面のキャラ性能 */
.switch-card { display: flex; align-items: center; gap: 10px; text-align: left; padding: 8px 10px; }
.switch-card .avatar { width: 44px; height: 44px; font-size: 1.1rem; border-width: 2px; }
.switch-body { flex: 1; min-width: 0; }
.switch-name { display: block; word-break: break-all; }
.switch-stats { display: grid; grid-template-columns: repeat(3, 1fr); gap: 0 8px; font-size: .75rem; font-weight: 500; margin-top: 2px; }
.switch-stats b { font-variant-numeric: tabular-nums; }

/* ===== モーダル（画像調整・キャラコード） ===== */
.modal {
  position: fixed;
  inset: 0;
  z-index: 10;
  display: grid;
  place-items: center;
  padding: 16px;
  background: rgba(27, 27, 47, .6);
}
.modal[hidden] { display: none; }
.modal-box {
  width: 100%;
  max-width: 420px;
  max-height: 100%;
  overflow-y: auto;
  display: grid;
  gap: 10px;
  background: var(--paper);
  border: 4px solid var(--ink);
  border-radius: 16px;
  padding: 16px;
}
.modal-box h3 { margin: 0; font-size: 1.1rem; word-break: break-all; }
.row2 { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }

.crop-canvas {
  justify-self: center;
  width: 240px;
  max-width: 100%;
  aspect-ratio: 1;
  border: 4px solid var(--ink);
  border-radius: 50%;
  cursor: grab;
  touch-action: none; /* スマホでドラッグしても画面がスクロールしないように */
}
.crop-canvas:active { cursor: grabbing; }
.zoom-row { display: flex; align-items: center; gap: 8px; font-weight: 800; font-size: .85rem; }
.zoom-row input { flex: 1; accent-color: var(--accent); }

.code-area {
  width: 100%;
  font: .8rem/1.4 ui-monospace, Consolas, monospace;
  padding: 8px;
  border: 3px solid var(--ink);
  border-radius: 10px;
  resize: vertical;
  word-break: break-all;
}
.status-msg { margin: 0; font-weight: 800; color: var(--hp-good); min-height: 1.2em; }
.status-msg:empty { display: none; }
.status-msg.error { color: var(--hp-low); }

/* ===== フレンド ===== */
.player-label { margin: 0; font-weight: 800; }
.player-label:empty { display: none; }
.friend-row { display: grid; grid-template-columns: 1fr 1fr; margin-bottom: -4px; }
/* タイトルの「コレクション」ボタンの上に置く丸いボタン */
.friend-btn {
  grid-column: 2;
  justify-self: center;
  position: relative;
  width: 76px;
  height: 76px;
  border-radius: 50%;
  border: 3px solid var(--ink);
  background: #8ce0c9;
  color: var(--ink);
  font: inherit;
  font-weight: 800;
  font-size: .9rem;
  cursor: pointer;
  box-shadow: 0 4px 0 var(--ink);
}
.friend-btn:active { transform: translateY(3px); box-shadow: 0 1px 0 var(--ink); }
.badge {
  position: absolute;
  top: -6px;
  right: -6px;
  min-width: 26px;
  height: 26px;
  padding: 0 6px;
  border-radius: 13px;
  background: var(--accent);
  color: #fff;
  border: 2px solid var(--ink);
  display: grid;
  place-items: center;
  font-size: .8rem;
}
.badge[hidden] { display: none; }

.friend-box {
  display: grid;
  gap: 8px;
  background: var(--panel);
  border: 3px solid var(--ink);
  border-radius: 12px;
  padding: 10px 12px;
}
.friend-box h3 { margin: 0; font-size: .95rem; }
.friend-box h3::before { content: '〇 '; color: var(--accent); }
.my-name { display: flex; align-items: center; gap: 6px; word-break: break-all; }
.my-name .btn { margin-left: auto; }
.code-row { display: flex; align-items: center; gap: 8px; }
.friend-code {
  flex: 1;
  font: 800 1.4rem/1.2 ui-monospace, Consolas, monospace;
  letter-spacing: .08em;
}
.code-input {
  flex: 1;
  min-width: 0;
  font: 800 1.1rem ui-monospace, Consolas, monospace;
  text-transform: uppercase;
  padding: 8px 10px;
  border: 3px solid var(--ink);
  border-radius: 10px;
}
.code-input::placeholder { text-transform: none; font-weight: 500; }

.tabs { display: grid; grid-template-columns: repeat(3, 1fr); border: 3px solid var(--ink); border-radius: 10px; overflow: hidden; }
.tab {
  font: inherit;
  font-weight: 800;
  font-size: .85rem;
  padding: 8px 2px;
  border: 0;
  background: #fff;
  color: var(--ink);
  cursor: pointer;
}
.tab + .tab { border-left: 3px solid var(--ink); }
.tab.active { background: var(--ink); color: #fff; }
.tab span { font-size: .75rem; }
.friend-list { padding-right: 0; }
.friend-list .empty { padding: 24px 0; }
.friend-card { padding: 8px 10px; }
.friend-card .avatar, .friend-confirm .avatar { width: 44px; height: 44px; font-size: 1.1rem; border-width: 2px; }
.friend-actions { display: flex; gap: 6px; flex: none; }
.friend-confirm { display: flex; align-items: center; gap: 12px; background: var(--panel); border: 3px solid var(--ink); border-radius: 12px; padding: 10px; }
#player-back[hidden] + .btn { grid-column: 1 / -1; }
.ask { margin: 0; font-weight: 800; text-align: center; font-size: 1.1rem; }

/* ===== ゲームオーバー ===== */
.score-box { text-align: center; display: grid; gap: 6px; }
.score-label { font-weight: 800; }
.score { font-size: 4rem; font-weight: 800; color: var(--accent); line-height: 1; }
.best { font-weight: 800; }
.new-record { color: var(--accent); }
.score-line { font-weight: 800; font-size: 1.2rem; }
.score-line .score { margin-right: 4px; }
.prev-score { font-weight: 800; font-size: .9rem; }
.prev-score:empty { display: none; }

/* ランキング（長くなったら枠の中でスクロール） */
.ranking-box {
  width: 100%;
  display: grid;
  gap: 6px;
  background: var(--panel);
  border: 3px solid var(--ink);
  border-radius: 12px;
  padding: 10px 12px;
}
.ranking-box h3 { margin: 0; font-size: 1.15rem; }
.ranking {
  list-style: none;
  margin: 0;
  padding: 0;
  max-height: 36dvh;
  overflow-y: auto;
  display: grid;
  gap: 4px;
  text-align: left;
}
.rank-row {
  display: grid;
  grid-template-columns: 2.2em 1fr auto;
  align-items: center;
  gap: 6px;
  padding: 6px 8px;
  border: 2px solid var(--ink);
  border-radius: 8px;
  font-weight: 800;
  font-size: .9rem;
}
.rank-row.top1 { background: #ffe9a8; }
.rank-row.top2 { background: #eceff3; }
.rank-row.top3 { background: #f6dcc4; }
.rank-row.me { box-shadow: 0 0 0 3px var(--accent) inset; }
.rank { text-align: right; font-variant-numeric: tabular-nums; }
.rank-name { min-width: 0; word-break: break-all; }
.rank-score { white-space: nowrap; font-variant-numeric: tabular-nums; }
.rank-note { color: var(--muted); font-size: .85rem; text-align: center; padding: 6px 0; }
