* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  background: #fbf3e6;
  color: #4a3a30;
  font-family: -apple-system, "Hiragino Maru Gothic ProN", "Hiragino Sans", sans-serif;
  -webkit-user-select: none;
  user-select: none;
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
  overscroll-behavior: none;
}

.app {
  max-width: 480px;
  margin: 0 auto;
  padding: max(12px, env(safe-area-inset-top)) 12px max(12px, env(safe-area-inset-bottom));
  display: flex;
  flex-direction: column;
  gap: 12px;
}

/* 上の帯（戻る・やり直す）の下の部分を、残りの場所の真ん中に置く。
   盤が画面の上に寄って、下半分が空かないようにするため（T04） */
.app { min-height: 100vh; min-height: 100dvh; }
.app > .top + * { margin-top: auto; }
.app > :last-child { margin-bottom: auto; }

.top { display: flex; justify-content: space-between; align-items: center; }
.info { font-size: 15px; }
.hint { margin: 0; font-size: 14px; text-align: center; opacity: 0.75; }

.btn {
  font: inherit;
  font-size: 15px;
  padding: 8px 16px;
  border: none;
  border-radius: 999px;
  background: #e0884a;
  color: #fff;
}
.btn-sub { background: #b9a08a; }

/* 盤面。指で引っぱるので、スクロールや拡大をさせない */
.board {
  width: 100%;
  aspect-ratio: 1;
  display: block;
  border-radius: 12px;
  background: #f1e2c8;
  touch-action: none;
}

/* 毛糸 */
.yarn {
  fill: none;
  stroke-width: 1.4;
  stroke-linecap: round;
  transition: stroke 0.2s;
}
.yarn.tangled { stroke: #d65b5b !important; stroke-dasharray: 2.4 1.4; }

/* 毛玉 */
.ball .body { stroke: #3b2f2a; stroke-width: 0.6; }
.ball .thread { fill: none; stroke: rgba(255, 255, 255, 0.65); stroke-width: 0.55; stroke-linecap: round; }
.ball.grab .body { stroke-width: 1; }
.ball.grab { filter: drop-shadow(0 1px 1.2px rgba(60, 40, 30, 0.4)); }

/* ほどけたとき: 毛糸が全部同じ色になって揺れる */
.board.solved .yarn { stroke: #e0884a !important; stroke-width: 1.8; }
.board.solved .tangle-layer { animation: wobble 0.9s ease-in-out; transform-origin: 50px 50px; }
@keyframes wobble {
  0%, 100% { transform: rotate(0) scale(1); }
  20% { transform: rotate(-2.5deg) scale(1.02); }
  45% { transform: rotate(2deg) scale(1.03); }
  70% { transform: rotate(-1deg) scale(1.01); }
}

/* 結果 */
.overlay {
  position: fixed; inset: 0;
  z-index: 10; /* 盤やこたつの上に重ねる（こたつの天板は z-index: 2） */
  background: rgba(60, 40, 30, 0.45);
  display: flex; align-items: center; justify-content: center;
}
.overlay[hidden] { display: none; }
.overlay-card {
  background: #fffaf2;
  padding: 24px 32px;
  border-radius: 16px;
  text-align: center;
}
.overlay-card p { font-size: 22px; margin: 0 0 16px; }
.overlay-card .overlay-moves { font-size: 15px; margin: -4px 0 14px; opacity: 0.8; }
.overlay-buttons { display: flex; gap: 10px; justify-content: center; }
.overlay-stamp { font-size: 15px; margin: 0 0 18px; }
.overlay-paws { display: flex; justify-content: center; gap: 6px; margin-bottom: 6px; }
.overlay-paws .paw { width: 40px; height: 40px; fill: #e5728a; transform: rotate(-12deg); }
.back-link { display: block; margin-top: 8px; font-size: 15px; color: #8a6f5a; text-decoration: none; padding: 8px; }

/* クリアの絵: 猫が毛糸玉で遊ぶ */
.overlay-picture { position: relative; width: 200px; height: 130px; margin: 0 auto 12px; }
.overlay-picture .play-cat { position: absolute; left: 6px; bottom: 0; width: 110px; height: 110px; animation: bob 1.2s ease-in-out infinite; }
.overlay-picture .play-cat .cat { width: 100%; height: 100%; }
.overlay-picture .play-paw { position: absolute; left: 92px; bottom: 18px; width: 30px; height: 30px; fill: #f2c9a6; animation: swat 1.2s ease-in-out infinite; transform-origin: 0 100%; }
.overlay-picture .play-ball { position: absolute; left: 128px; bottom: 6px; width: 54px; height: 54px; animation: roll 1.2s ease-in-out infinite; }
@keyframes bob { 0%, 100% { transform: translateY(0); } 50% { transform: translateY(-4px); } }
@keyframes swat { 0%, 100% { transform: rotate(0); } 40% { transform: rotate(-25deg); } 55% { transform: rotate(8deg); } }
@keyframes roll { 0%, 35%, 100% { transform: translateX(0) rotate(0); } 70% { transform: translateX(14px) rotate(80deg); } }

/* ホーム・遊び方画面（難易度はホーム画面で選ぶ） */
.screen { background: #fbf3e6; }
.screen .overlay-card { background: transparent; width: min(320px, 100%); }
.title-name { font-size: 34px !important; font-weight: bold; margin-bottom: 8px !important; }
.title-name + .menu-buttons { margin-top: 28px; }
.home-modes { margin: 6px 0 0; }
.screen-title { font-size: 24px !important; font-weight: bold; }
.menu-buttons { display: flex; flex-direction: column; gap: 14px; }
.btn-big { font-size: 20px; padding: 14px 0; width: 100%; }
.option-group { margin-bottom: 28px; text-align: left; }
.option-label { font-size: 15px; margin-bottom: 8px; }
.modes { display: flex; gap: 8px; }
.mode-choice {
  flex: 1;
  font: inherit;
  font-size: 15px;
  padding: 12px 2px;
  white-space: nowrap;
  border-radius: 12px;
  border: 3px solid transparent;
  background: #eadfd2;
  color: #6b5a4c;
}
.mode-choice.selected { color: #fff; border-color: #4a3a30; }
.mode-easy.selected { background: #7cb97a; }
.mode-normal.selected { background: #e0884a; }
.mode-hard.selected { background: #c95a5a; }

/* 遊び方画面 */
.howto-card { width: min(400px, 100%) !important; text-align: left; }
.howto-card .screen-title { text-align: center; }
.howto-list { margin: 0 0 16px; padding-left: 1.4em; font-size: 15px; line-height: 1.6; }
.howto-list li { margin-bottom: 8px; }
.howto-tip { font-size: 14px !important; margin: 0 0 10px !important; opacity: 0.85; }
.howto-card .btn-big { margin-top: 12px; }

@media (prefers-reduced-motion: reduce) {
  .board.solved .tangle-layer,
  .overlay-picture .play-cat, .overlay-picture .play-paw, .overlay-picture .play-ball { animation: none; }
}
