/* つなぐビット（ネットワーク編） */
/* 図の最大幅・キャラの丸の大きさ・パケットの速さは src/config.js にある */

html {
  height: 100%;
}

body.tsunagu {
  height: 100vh;
  height: 100dvh;
  min-height: 0;
  overflow: hidden;
}

[hidden] {
  display: none !important;
}

.screen {
  height: 100%;
  width: 100%;
  margin: 0 auto;
}

/* 画像（読みこめないときは、淡い色の丸に No） */
.avatar {
  display: block;
  aspect-ratio: 1;
}

.avatar img {
  display: block;
  width: 100%;
  height: 100%;
  object-fit: contain;
}

.avatar-placeholder {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 100%;
  height: 100%;
  border-radius: 50%;
  color: var(--ink-soft);
  font-size: 0.8rem;
}

/* ---------- 面えらび ---------- */
.select {
  max-width: 960px;
  padding: 16px;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 14px;
}

.select-header {
  text-align: center;
}

.select-title {
  font-size: clamp(1.7rem, 6vw, 2.4rem);
  color: var(--mint-dark);
  letter-spacing: 0.04em;
}

.select-lead {
  margin: 2px 0 0;
  color: var(--ink-soft);
  font-size: 0.9rem;
}

.stage-list {
  list-style: none;
  margin: 0;
  padding: 0;
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(150px, 1fr));
  gap: 10px;
}

.stage-card {
  display: grid;
  grid-template-columns: auto 1fr;
  grid-template-rows: 1fr auto;
  column-gap: 10px;
  row-gap: 4px;
  align-items: start;
  width: 100%;
  height: 100%;
  min-height: 92px;
  padding: 10px 12px;
  border: 2px solid var(--line);
  border-radius: var(--radius);
  background: var(--card);
  box-shadow: var(--shadow);
  color: var(--ink);
  font: inherit;
  text-align: left;
  cursor: pointer;
  transition: border-color 0.15s, transform 0.1s;
}

.stage-card:active {
  transform: scale(0.98);
}

.stage-card.is-cleared {
  border-color: var(--mint);
}

.stage-card:disabled {
  background: #f5f8f7;
  box-shadow: none;
  color: #a9b5b1;
  cursor: default;
}

.stage-card-no {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 32px;
  height: 32px;
  border-radius: 50%;
  background: var(--mint-light);
  color: var(--mint-dark);
  font-weight: 800;
}

.stage-card:disabled .stage-card-no {
  background: #e9efed;
  color: #a9b5b1;
}

.stage-card-name {
  font-size: 0.9rem;
  line-height: 1.35;
}

.stage-card-marks {
  grid-column: 1 / -1;
  display: flex;
  align-items: center;
  gap: 6px;
  min-height: 22px;
}

.stage-card-check {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 22px;
  height: 22px;
  border-radius: 50%;
  background: var(--mint);
  color: #fff;
  font-size: 0.85rem;
}

.stage-card-first {
  padding: 1px 9px;
  border-radius: 999px;
  background: #fff4d6;
  color: #9a7419;
  font-size: 0.75rem;
}

.stage-card-locked {
  font-size: 0.75rem;
}

.select-links {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 10px;
  padding-bottom: 8px;
}

@media (hover: hover) {
  .stage-card:not(:disabled):hover {
    border-color: var(--mint-dark);
  }
}

/* ---------- プレイ ---------- */
.play {
  max-width: 1400px;
  padding: 10px 16px 12px;
  display: grid;
  grid-template-columns: minmax(0, 1fr);
  grid-template-rows: auto minmax(0, 1fr) auto auto;
  grid-template-areas:
    "prompt"
    "diagram"
    "hand"
    "actions";
  gap: 10px;
}

/* お題：左に案内役クラベの絵、右に名前ラベル＋ふきだし */
.prompt {
  grid-area: prompt;
  display: flex;
  align-items: flex-end;
  gap: 8px;
  min-width: 0;
}

/* 薄いミント色の丸の中にクラベ（main.js が img を入れる。読めないときは丸だけ残る）。
   絵は丸に内接する大きさにして、はみ出さない・ゆがめない */
.kurabe-art {
  flex: none;
  display: flex;
  align-items: center;
  justify-content: center;
  width: 44px;
  height: 44px;
  border-radius: 50%;
  background: var(--mint-light);
  overflow: hidden;
  transform-origin: 50% 90%;
}

.kurabe-art img {
  display: block;
  width: 88%;
  height: 88%;
  object-fit: contain;
}

/* 送信してまちがえたとき：首をかしげる（左右に 2° ずつ1回）／クリアしたとき：小さくはねる */
.kurabe-art.is-tilt {
  animation: kurabe-tilt 0.3s ease-in-out;
}

.kurabe-art.is-hop {
  animation: kurabe-hop 0.3s ease-out;
}

@keyframes kurabe-tilt {
  25% {
    transform: rotate(-2deg);
  }
  75% {
    transform: rotate(2deg);
  }
}

@keyframes kurabe-hop {
  40% {
    transform: translateY(-4px);
  }
}

@media (prefers-reduced-motion: reduce) {
  .kurabe-art.is-tilt,
  .kurabe-art.is-hop {
    animation: none;
  }
}

.prompt-main {
  flex: 1;
  min-width: 0;
}

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

.kurabe-name {
  padding: 0 10px;
  border-radius: 999px;
  background: var(--mint);
  color: #fff;
  font-size: 0.75rem;
}

.prompt-stage {
  color: var(--ink-soft);
  font-size: 0.8rem;
}

.prompt-bubble {
  margin: 0;
  padding: 7px 10px;
  border: 2px solid var(--line);
  border-radius: 4px 14px 14px 14px;
  background: var(--card);
  font-size: clamp(0.8rem, 3.4vw, 1.05rem);
  line-height: 1.45;
  /* 2行ぶんの高さをいつも取って、面ごとに図の位置が動かないようにする */
  min-height: calc(2 * 1.45em + 18px);
}

/* 図：収まる最大の大きさで、縦横比 100:60 を保つ。
   枠（.diagram-cell）が縦長のときは、main.js が .is-vertical を付けて 60:100 の縦向きにする
   （縦向きにする縦横比は src/config.js の verticalAspect） */
.diagram-cell {
  grid-area: diagram;
  container-type: size;
  display: grid;
  place-items: center;
  min-width: 0;
  min-height: 0;
}

.diagram-box {
  position: relative;
  width: min(100cqw, 100cqh * 100 / 60, var(--diagram-max-width, 960px));
  aspect-ratio: 100 / 60;
  border: 2px solid var(--line);
  border-radius: var(--radius);
  /* 方眼のうすい模様（viewBox の 5 目盛りごと） */
  background-color: #fbfefd;
  background-image:
    linear-gradient(to right, rgba(127, 209, 185, 0.16) 1px, transparent 1px),
    linear-gradient(to bottom, rgba(127, 209, 185, 0.16) 1px, transparent 1px);
  background-size: 5% 8.3333%;
}

.diagram-box.is-vertical {
  width: min(100cqw, 100cqh * 60 / 100, var(--diagram-max-width, 960px));
  aspect-ratio: 60 / 100;
  background-size: 8.3333% 5%;
}

.diagram-svg {
  display: block;
  width: 100%;
  height: 100%;
  overflow: visible;
}

.link {
  stroke: #cfe6de;
  stroke-width: 1.8;
  stroke-linecap: round;
  transition: stroke 0.15s;
}

.link.is-active {
  stroke: #ffd666;
  filter: drop-shadow(0 0 1px #ffd666);
}

/* 名前：白いふち取りで、線の上を通っても読めるようにする。
   ふちの太さは view.js が stroke-width で入れる（文字の大きさ × src/config.js の labelStrokeRatio） */
.node-label {
  fill: var(--ink-soft);
  font-weight: 800;
  stroke: #fff;
  stroke-linejoin: round;
  paint-order: stroke;
  pointer-events: none;
}

.machine {
  fill: #f1faf7;
  stroke: #6db7a2;
  stroke-width: 0.8;
  stroke-linecap: round;
  stroke-linejoin: round;
}

.machine-paper {
  fill: #fff;
}

.machine-dot {
  fill: #6db7a2;
  stroke: none;
}

.char-base {
  fill: #fff;
  stroke: var(--line);
  stroke-width: 0.5;
}

.char-fallback-no {
  fill: var(--ink-soft);
  font-weight: 800;
}

.node--slot {
  cursor: pointer;
  outline: none;
}

.slot-ring {
  fill: #fff;
  stroke: #9ccfc0;
  stroke-width: 0.6;
  stroke-dasharray: 1.6 1.4;
  stroke-linecap: round;
}

.slot-hit {
  fill: transparent;
}

.node--slot.is-filled .slot-ring {
  stroke: transparent;
}

.node--slot:focus-visible .slot-ring,
.node--slot:focus-visible .char-base {
  stroke: var(--mint-dark);
  stroke-width: 0.9;
}

/* 手札を選んでいるあいだ、置ける枠を目立たせる */
.diagram-svg.is-choosing .slot-ring {
  stroke: var(--mint-dark);
  stroke-width: 0.8;
  animation: slot-pulse 1s ease-in-out infinite;
}

.diagram-svg.is-choosing .node--slot.is-filled .char-base {
  stroke: var(--mint-dark);
  stroke-width: 0.8;
}

.node--slot.is-wrong .char-base {
  fill: #ffe9e6;
  stroke: #e5695b;
  stroke-width: 0.9;
}

.node-inner.is-bounce {
  animation: node-bounce 0.3s ease-out;
}

.node-inner.is-shake {
  animation: node-shake 0.45s ease-in-out;
}

.node-inner.is-glow {
  animation: node-glow 0.6s ease-out;
}

.packet {
  pointer-events: none;
  will-change: transform;
}

.packet-base {
  fill: #fffbe9;
  stroke: #ffd666;
  stroke-width: 0.6;
}

/* あやしい包み：少し暗くする */
.packet--dark .packet-inner {
  filter: brightness(0.55) saturate(0.6);
}

@keyframes slot-pulse {
  50% {
    stroke-opacity: 0.35;
  }
}

@keyframes node-bounce {
  40% {
    transform: translateY(-2px);
  }
}

@keyframes node-shake {
  20%,
  60% {
    transform: translateX(-1.1px);
  }
  40%,
  80% {
    transform: translateX(1.1px);
  }
}

@keyframes node-glow {
  35% {
    transform: scale(1.22);
    filter: drop-shadow(0 0 2px #ffd666);
  }
}

/* まちがいのセリフ・「通さへんで！」のふきだし */
.diagram-overlay {
  position: absolute;
  inset: 0;
  pointer-events: none;
}

.bubble {
  position: absolute;
  z-index: 2;
  width: max-content;
  max-width: min(72%, 300px);
  padding: 7px 12px 8px;
  border: 2px solid var(--mint-dark);
  border-radius: 14px;
  background: #fff;
  box-shadow: 0 4px 12px rgba(47, 58, 55, 0.16);
  font-size: clamp(0.8rem, 3.3vw, 0.95rem);
  line-height: 1.45;
  animation: bubble-in 0.15s ease-out;
}

.bubble--above {
  transform: translate(calc(-1 * var(--bubble-shift, 50%)), -100%);
}

.bubble--below {
  transform: translate(calc(-1 * var(--bubble-shift, 50%)), 0);
}

.bubble-speaker {
  display: block;
  color: var(--mint-dark);
  font-size: 0.72rem;
}

.bubble-text {
  display: block;
}

@keyframes bubble-in {
  from {
    opacity: 0;
  }
}

/* 手札
   --hand-image        : 札の絵の大きさ（スマホ縦で1段に並ぶとき。よこ長は下の @media で変える）
   --hand-image-wrapped: スマホ縦で1段に入らず、段を分けたときの絵の大きさ
                         （390×844 の6枚の面で、図のキャラの丸が 60px を下回らない大きさ）
   --hand-count / --hand-cols は main.js が入れる（札の枚数／段を分けるときの列の数） */
.play {
  --hand-image: 64px;
  --hand-image-wrapped: 48px;
  --hand-gap: 4px; /* src/config.js の handGapPx と合わせる */
  --hand-pad-top: 5px;
  --hand-pad-bottom: 4px;
  --hand-name-gap: 2px;
  --hand-name-size: 0.64rem;
}

.hand {
  grid-area: hand;
  display: grid;
  grid-template-columns: repeat(var(--hand-count, 3), minmax(0, 1fr));
  gap: var(--hand-gap);
  padding-top: 6px; /* 選んだ札が持ち上がるぶん */
  min-width: 0;
}

.hand-card {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: flex-start;
  gap: var(--hand-name-gap);
  min-width: 0;
  min-height: 64px;
  padding: var(--hand-pad-top) 2px var(--hand-pad-bottom);
  border: 2px solid var(--line);
  border-radius: var(--radius-sm);
  background: var(--card);
  color: var(--ink);
  font: inherit;
  cursor: pointer;
  transition: transform 0.12s, border-color 0.12s, opacity 0.12s;
}

.hand-card-image {
  flex: none;
  width: var(--hand-image);
  max-width: 100%;
}

.hand-card-name {
  font-size: var(--hand-name-size);
  line-height: 1.2;
  text-align: center;
  overflow-wrap: anywhere;
}

/* 枠線が太くなるぶん、内側の余白を 2px ずつ減らして大きさを変えない */
.hand-card.is-selected {
  border-width: 4px;
  border-color: var(--mint-dark);
  padding: calc(var(--hand-pad-top) - 2px) 0 calc(var(--hand-pad-bottom) - 2px);
  background: var(--mint-light);
  transform: translateY(-6px);
}

/* スマホ縦で1段に入らないとき（main.js が .is-wrapped を付ける）：3×2 に分けて、図の場所を残すために詰める */
@media (orientation: portrait) {
  .hand.is-wrapped {
    grid-template-columns: repeat(var(--hand-cols, 3), minmax(0, 1fr));
    --hand-image: var(--hand-image-wrapped);
    --hand-pad-top: 3px;
    --hand-pad-bottom: 2px;
    --hand-name-gap: 1px;
  }
}

.hand-card:disabled {
  cursor: default;
}

/* 図に置いてある札は、うすくして場所だけ残す */
.hand-card.is-used {
  opacity: 0.25;
}

.actions {
  grid-area: actions;
  display: flex;
  gap: 10px;
}

.actions .send {
  flex: 1;
}

.btn:disabled {
  background: #e3ebe8;
  color: #a2afab;
  box-shadow: none;
  cursor: default;
  transform: none;
}

@media (hover: hover) {
  .hand-card:not(:disabled):hover {
    border-color: var(--mint);
  }
  .btn:disabled:hover {
    background: #e3ebe8;
  }
}

/* よこ長：左に図、右にお題・手札・送信 */
@media (orientation: landscape) {
  .play {
    grid-template-columns: minmax(0, 1fr) clamp(280px, 42vw, 440px);
    grid-template-rows: auto minmax(0, 1fr) auto;
    grid-template-areas:
      "diagram prompt"
      "diagram hand"
      "diagram actions";
    column-gap: 14px;
  }

  .prompt-bubble {
    font-size: clamp(0.8rem, 1.5vw, 1.05rem);
  }

  .bubble {
    font-size: clamp(0.8rem, 1.4vw, 0.95rem);
  }

  /* 手札は2〜3列のマス目（6枚なら 3×2、3枚なら 3×1）。絵は画面の高さに合わせて大きくする（1280×720 で 88px） */
  .play {
    --hand-image: clamp(56px, 12.3vh, 88px);
    --hand-gap: 6px;
    --hand-name-size: clamp(0.64rem, 1.9vh, 0.85rem);
  }

  .hand {
    align-content: center;
    grid-template-columns: repeat(var(--hand-cols, 3), minmax(0, 1fr));
  }
}

/* よこ長で背が低い（スマホ横）：詰める */
@media (orientation: landscape) and (max-height: 500px) {
  .play {
    padding: 8px 16px;
    gap: 6px;
  }

  /* 844×390 で 56px。もっと背が低い画面では、2段が入るように小さくする */
  .play {
    --hand-image: clamp(40px, 14.4vh, 56px);
    --hand-gap: 4px;
    --hand-name-size: 0.64rem;
  }

  .hand-card {
    min-height: 60px;
  }
}

/* ---------- クリア ---------- */
.clear {
  overflow-y: auto;
  display: grid;
  place-items: center;
  padding: 16px;
}

.clear-card {
  width: 100%;
  max-width: 560px;
  display: grid;
  gap: 12px;
  justify-items: center;
  text-align: center;
}

/* 見出しの左にクラベ（56px）。読めないときは薄いミント色の丸 */
.clear-head {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 10px;
  max-width: 100%;
}

.clear-kurabe {
  flex: none;
  display: block;
  width: 56px;
  height: 56px;
  animation: clear-pop 0.4s ease-out;
}

.clear-kurabe img {
  display: block;
  width: 100%;
  height: 100%;
  object-fit: contain;
}

.clear-kurabe.is-fallback {
  border-radius: 50%;
  background: var(--mint-light);
}

.clear-title {
  min-width: 0;
  font-size: clamp(1.8rem, 7vw, 2.6rem);
  color: var(--mint-dark);
  animation: clear-pop 0.4s ease-out;
}

.clear-first {
  margin: 0;
  padding: 2px 14px;
  border-radius: 999px;
  background: #fff4d6;
  color: #9a7419;
  font-size: 0.9rem;
}

.clear-explain {
  margin: 0;
  padding: 12px 16px;
  border: 2px solid var(--line);
  border-radius: var(--radius);
  text-align: left;
  font-size: 0.95rem;
}

.clear-joined {
  width: 100%;
}

.clear-joined-title {
  font-size: 1.1rem;
  color: var(--mint-dark);
}

.clear-friends {
  list-style: none;
  margin: 8px 0 0;
  padding: 0;
  display: flex;
  justify-content: center;
  gap: 20px;
}

.clear-friend {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
  animation: clear-pop 0.45s ease-out;
}

.clear-friend-link {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
  min-height: 44px;
  text-decoration: none;
}

.clear-friend-image {
  width: clamp(84px, 24vmin, 128px);
}

.clear-friend-name {
  font-size: 0.95rem;
}

.clear-all {
  margin: 0;
  padding: 10px 16px;
  border-radius: var(--radius);
  background: var(--mint-light);
  color: var(--mint-dark);
  font-size: 1.05rem;
}

.clear-actions {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 10px;
  margin-top: 4px;
}

@keyframes clear-pop {
  from {
    transform: scale(0.7);
    opacity: 0;
  }
}

@media (orientation: landscape) and (max-height: 500px) {
  .clear-card {
    max-width: 760px;
    gap: 8px;
  }

  .clear-friend-image {
    width: 72px;
  }
}
