:root {
  --ink: #2e2a3a;
  --paper: #fff8e6;
  --paper2: #f3e1a4;
  --grass: #7cc45a;
  --hp: #ef5b6b;
  --xp: #4aa3ff;
  --wool: #fff;
  --gold: #f5c84c;
  --sat: env(safe-area-inset-top, 0px);
  --sar: env(safe-area-inset-right, 0px);
  --sab: env(safe-area-inset-bottom, 0px);
  --sal: env(safe-area-inset-left, 0px);
  --font: "Hiragino Maru Gothic ProN", "Hiragino Sans", "BIZ UDPGothic", "Yu Gothic", "Meiryo", system-ui, sans-serif;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  overflow: hidden;
  background: #5a9c43;
  color: var(--ink);
  font-family: var(--font);
  overscroll-behavior: none;
  touch-action: none;
  -webkit-user-select: none;
  user-select: none;
  -webkit-touch-callout: none;
  -webkit-tap-highlight-color: transparent;
  -webkit-text-size-adjust: 100%;
}

#game {
  position: fixed;
  inset: 0;
  width: 100%;
  height: 100%;
  display: block;
  touch-action: none;
  image-rendering: pixelated;
  image-rendering: crisp-edges;
}

.hidden { display: none !important; }

/* ---------- HUD ---------- */
#hud {
  position: fixed;
  inset: 0;
  pointer-events: none;
  padding: calc(var(--sat) + 4px) calc(var(--sar) + 8px) 0 calc(var(--sal) + 8px);
  font-weight: 700;
  text-shadow: 0 1px 0 rgba(255,255,255,.6);
}

.xpbar {
  position: relative;
  height: 14px;
  border: 2px solid var(--ink);
  border-radius: 4px;
  background: rgba(46,42,58,.55);
  overflow: hidden;
}
.xpbar .fill { background: linear-gradient(#8fd0ff, var(--xp)); }
.xpbar span {
  position: absolute; right: 6px; top: 50%; transform: translateY(-50%);
  font-size: 11px; color: #fff; text-shadow: 0 1px 0 var(--ink);
}

.fill { position: absolute; left: 0; top: 0; bottom: 0; width: 0; }

.hud-row {
  display: flex;
  align-items: flex-start;
  gap: 8px;
  margin-top: 5px;
}
.hud-left { flex: 0 1 220px; min-width: 130px; display: flex; flex-direction: column; gap: 4px; }
.hud-center { flex: 1; text-align: center; line-height: 1.1; }
.time { font-size: 20px; letter-spacing: 1px; }
.time.boss { color: #b3212f; }
.kills { font-size: 12px; }

.bar {
  position: relative;
  height: 16px;
  border: 2px solid var(--ink);
  border-radius: 4px;
  background: rgba(46,42,58,.55);
  overflow: hidden;
}
.hpbar .fill { background: linear-gradient(#ff9aa5, var(--hp)); transition: width .15s; }
.hpbar span {
  position: absolute; inset: 0; text-align: center; font-size: 11px; line-height: 12px;
  color: #fff; text-shadow: 0 1px 0 var(--ink);
}
.woolbox { display: flex; align-items: center; gap: 6px; }
.woolname {
  font-size: 12px; white-space: nowrap;
  background: var(--paper); border: 2px solid var(--ink); border-radius: 10px; padding: 0 6px;
}
.woolbar { flex: 1; height: 10px; }
.woolbar .fill { background: linear-gradient(#fff, #ffd6e4); }

.icon-btn {
  pointer-events: auto;
  width: 44px; height: 44px;
  flex: 0 0 44px;
  border: 3px solid var(--ink);
  border-radius: 10px;
  background: var(--paper);
  box-shadow: 0 3px 0 var(--ink);
  display: grid; place-items: center;
  cursor: pointer;
  touch-action: manipulation;
}
.icon-btn:active { transform: translateY(2px); box-shadow: 0 1px 0 var(--ink); }
.hud-btns { display: flex; gap: 6px; align-items: flex-start; }
.speed-btn { font: inherit; font-size: 15px; font-weight: 900; color: var(--ink); padding: 0; }
.speed-btn.fast { background: #ffd66b; }
.speed-btn[hidden] { display: none; }
.pause-icon { width: 14px; height: 16px; border-left: 5px solid var(--ink); border-right: 5px solid var(--ink); }

.weapon-list { display: flex; gap: 4px; margin-top: 4px; flex-wrap: wrap; }
.wicon {
  width: 30px; height: 30px; position: relative;
  border: 2px solid var(--ink); border-radius: 6px; background: rgba(255,248,230,.85);
}
.wicon canvas { width: 26px; height: 26px; image-rendering: pixelated; display: block; }
.wicon b {
  position: absolute; right: -4px; bottom: -5px; font-size: 10px;
  background: var(--ink); color: #fff; border-radius: 6px; padding: 0 3px; line-height: 13px;
}

.bossbar {
  position: absolute;
  left: 50%; transform: translateX(-50%);
  bottom: calc(var(--sab) + 14px);
  width: min(80%, 520px);
  text-align: center;
}
.bossname { font-size: 13px; color: #fff; text-shadow: 0 1px 0 var(--ink), 0 0 4px var(--ink); }
.bossbar .bar { height: 14px; margin-top: 2px; }
.bossbar .fill { background: linear-gradient(#b76bff, #5a1f8f); }

.banner {
  position: fixed; left: 50%; top: 32%; transform: translate(-50%, -50%);
  pointer-events: none;
  padding: 8px 18px;
  background: rgba(46,42,58,.85); color: #fff;
  border: 3px solid #fff; border-radius: 12px;
  font-weight: 800; font-size: clamp(15px, 4.2vw, 24px);
  white-space: nowrap;
  animation: bannerIn .3s ease-out;
}
.banner.danger { background: rgba(150,20,40,.9); }
.banner.wool { background: rgba(255,248,230,.95); color: var(--ink); border-color: var(--ink); }
@keyframes bannerIn { from { transform: translate(-50%, -50%) scale(.6); opacity: 0; } }

/* ---------- Screens ---------- */
.screen {
  position: fixed;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: calc(var(--sat) + 10px) calc(var(--sar) + 10px) calc(var(--sab) + 10px) calc(var(--sal) + 10px);
  background: rgba(46,42,58,.45);
  touch-action: pan-y;
}

.panel {
  width: min(100%, 460px);
  max-height: 100%;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  overscroll-behavior: contain;
  background: var(--paper);
  border: 4px solid var(--ink);
  border-radius: 16px;
  box-shadow: 0 6px 0 var(--ink);
  padding: 18px 18px 20px;
  text-align: center;
}
.panel h2 { margin: 0 0 8px; font-size: 24px; }
.panel h3 { margin: 8px 0 4px; font-size: 15px; }
.sub { margin: 0 0 12px; font-size: 14px; }

.best { font-size: 12px; line-height: 1.5; white-space: pre-line; }
.catch { margin: 0; font-size: 15px; font-weight: 800; }

/* ---------- タイトル（ドット絵アニメーション） ---------- */
.sr-only { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }
.title-screen { display: block; padding: 0; overflow: hidden; background: #6fb94f; touch-action: none; }
#titleScene { position: absolute; left: 0; top: 0; image-rendering: pixelated; image-rendering: crisp-edges; }
.title-ui {
  position: absolute; left: 50%; transform: translateX(-50%);
  bottom: calc(var(--sab) + 14px);
  width: min(92%, 420px);
  display: flex; flex-direction: column; align-items: center; gap: 8px;
  text-align: center;
}
.title-ui .catch {
  margin: 0; color: #fff; font-size: 15px; font-weight: 900; letter-spacing: .04em;
  text-shadow: 0 2px 0 #2d5a37, 2px 0 0 #2d5a37, -2px 0 0 #2d5a37, 0 -2px 0 #2d5a37;
}
.title-start { width: 100%; animation: startBob 1.6s ease-in-out infinite; }
@keyframes startBob { 50% { transform: translateY(-3px); } }
.title-start:active { animation: none; }
.title-sub { display: flex; gap: 10px; width: 100%; }
.title-sub .btn { flex: 1; }
.title-ui .best { color: #fff; font-size: 11px; text-shadow: 0 1px 0 #2d5a37, 0 0 3px #2d5a37; }
.sound-hint { margin: 0; color: #fff; font-size: 11px; font-weight: 700; text-shadow: 0 1px 0 #2d5a37, 0 0 3px #2d5a37; animation: hintBlink 1.4s ease-in-out infinite; }
@keyframes hintBlink { 50% { opacity: .45; } }
.sound-hint.gone { visibility: hidden; }
/* 横長の画面：羊が左、ボタンは右下 */
@media (orientation: landscape) {
  .title-ui { left: auto; right: calc(var(--sar) + 4%); transform: none; width: min(44%, 400px); }
}
@media (orientation: landscape) and (max-height: 520px) {
  .title-ui { bottom: calc(var(--sab) + 8px); gap: 5px; }
  .title-ui .catch { font-size: 12px; }
  .title-sub .btn { white-space: nowrap; padding: 4px 8px; }
  .title-ui .best, .sound-hint { font-size: 10px; }
}
@media (min-width: 900px) and (min-height: 700px) {
  .title-ui { zoom: 1.35; }
}

.btn-col { display: flex; flex-direction: column; gap: 10px; align-items: center; }
.btn-row { display: flex; gap: 10px; justify-content: center; flex-wrap: wrap; margin-top: 12px; }

.btn {
  min-width: 160px; min-height: 48px;
  padding: 8px 18px;
  font: inherit; font-size: 17px; font-weight: 800;
  color: var(--ink);
  background: #fff;
  border: 3px solid var(--ink);
  border-radius: 12px;
  box-shadow: 0 4px 0 var(--ink);
  cursor: pointer;
  touch-action: manipulation;
}
.btn.big { min-width: 200px; min-height: 56px; font-size: 20px; background: var(--gold); }
.btn.danger { background: #ffd9dc; }
.btn:active { transform: translateY(3px); box-shadow: 0 1px 0 var(--ink); }
.btn:focus-visible, .card:focus-visible { outline: 3px solid var(--xp); outline-offset: 2px; }

.guide-list { text-align: left; margin: 0 0 16px; padding-left: 1.2em; line-height: 1.7; font-size: 15px; }

/* level up */
.levelup-panel { width: min(100%, 520px); }
.cards { display: flex; flex-direction: column; gap: 10px; }
.card {
  display: flex; align-items: center; gap: 10px;
  text-align: left;
  min-height: 72px;
  padding: 10px 12px;
  font: inherit; color: var(--ink);
  background: #fff;
  border: 3px solid var(--ink);
  border-radius: 12px;
  box-shadow: 0 4px 0 var(--ink);
  cursor: pointer;
  touch-action: manipulation;
}
.card:active { transform: translateY(3px); box-shadow: 0 1px 0 var(--ink); }
.card canvas { width: 44px; height: 44px; flex: 0 0 44px; image-rendering: pixelated; }
.card .ct { flex: 1; min-width: 0; }
.card .cn { font-weight: 800; font-size: 16px; }
.card .tag { font-size: 11px; padding: 1px 6px; border-radius: 8px; margin-left: 4px; color: #fff; background: var(--xp); vertical-align: 2px; }
.card .tag.new { background: #e8741c; }
.card .tag.stat { background: #6b8f3a; }
.card .cd { font-size: 12px; margin-top: 2px; line-height: 1.4; }
.card .cd b { color: #d0482f; }

/* settings */
.settings { display: flex; flex-direction: column; gap: 12px; margin: 6px 0 16px; text-align: left; }
.set-row { display: flex; align-items: center; gap: 10px; min-height: 44px; font-weight: 700; }
.set-row span { width: 4.5em; }
.set-row input[type=range] { flex: 1; height: 32px; accent-color: #e8741c; }
.set-row output { width: 2.5em; text-align: right; }
.set-row.check { cursor: pointer; }
.set-row.check input { width: 26px; height: 26px; accent-color: #e8741c; }
.set-row.check span { width: auto; }

/* result */
.result-panel { width: min(100%, 560px); }
.newrec { color: #d0482f; font-weight: 800; margin: 0 0 6px; }
.res-body { display: flex; flex-direction: column; gap: 8px; }
.stats { display: grid; grid-template-columns: auto 1fr; gap: 3px 12px; margin: 0; text-align: left; font-size: 15px; }
.stats dt { font-weight: 700; }
.stats dd { margin: 0; text-align: right; }
.res-weapons ul { list-style: none; margin: 0; padding: 0; display: flex; flex-wrap: wrap; gap: 4px 10px; justify-content: center; font-size: 13px; }

/* dev */
.dev {
  position: fixed; left: calc(var(--sal) + 6px); bottom: calc(var(--sab) + 6px);
  z-index: 10;
  display: flex; flex-wrap: wrap; gap: 4px; max-width: 250px;
  background: rgba(0,0,0,.6); padding: 6px; border-radius: 8px;
}
.dev button { font-size: 11px; padding: 4px 6px; min-height: 28px; }
.dev div { color: #fff; font-size: 10px; width: 100%; font-family: monospace; }

/* ---------- 横向きの低い画面 ---------- */
@media (max-height: 520px) and (orientation: landscape) {
  .panel { padding: 10px 14px 12px; border-radius: 12px; }
  .panel h2 { font-size: 19px; margin-bottom: 4px; }
  .sub { margin-bottom: 6px; font-size: 12px; }
  .levelup-panel { width: min(100%, 780px); }
  .cards { flex-direction: row; }
  .card { flex: 1 1 0; flex-direction: column; text-align: center; min-height: 0; padding: 8px; gap: 4px; }
  .card canvas { width: 36px; height: 36px; flex-basis: 36px; }
  .card .cn { font-size: 14px; }
  .btn { min-height: 44px; font-size: 15px; min-width: 130px; }
  .btn.big { min-height: 46px; font-size: 17px; min-width: 150px; }
  .btn-col { flex-direction: row; flex-wrap: wrap; justify-content: center; }
  .result-panel { width: min(100%, 720px); }
  .res-body { flex-direction: row; align-items: flex-start; }
  .res-body > * { flex: 1; }
  .stats { font-size: 13px; }
  .settings { gap: 4px; margin-bottom: 8px; }
  .guide-list { font-size: 13px; margin-bottom: 8px; }
  .time { font-size: 17px; }
}

@media (max-width: 380px) {
  .hud-left { min-width: 120px; }
  .woolname { font-size: 11px; }
}

/* タブレット・PC：UI を少し大きく */
@media (min-width: 700px) and (min-height: 700px) {
  .panel { zoom: 1.25; }
  #hud { zoom: 1.2; }
  .title-foot { zoom: 1.25; }
}
@media (min-width: 900px) and (min-height: 700px) {
  #hud { font-size: 15px; }
  .hud-left { flex-basis: 280px; }
  .time { font-size: 26px; }
  .kills { font-size: 14px; }
  .bar { height: 20px; }
  .hpbar span { font-size: 13px; line-height: 16px; }
  .woolname { font-size: 14px; }
  .woolbar { height: 12px; }
  .wicon { width: 38px; height: 38px; }
  .wicon canvas { width: 34px; height: 34px; }
}

/* ---------- コイン・必殺技・牧場 ---------- */
.btn.small { min-width: 0; min-height: 44px; padding: 4px 16px; font-size: 15px; box-shadow: 0 3px 0 var(--ink); }
.btn.ghost { background: transparent; box-shadow: none; border-style: dashed; font-size: 13px; margin-top: 10px; }
.coin-dot {
  display: inline-block; width: 11px; height: 11px; vertical-align: -1px; margin-right: 3px;
  background: radial-gradient(circle at 35% 35%, #fffbe0 0 18%, #ffd23a 20% 70%, #e0a01a 72%);
  border: 2px solid #7a5210; border-radius: 50%;
}
.coin-dot.big { width: 22px; height: 22px; vertical-align: -4px; }
.coins { font-size: 12px; }

.ult-btn {
  --p: 0%;
  position: absolute;
  right: calc(var(--sar) + 14px); bottom: calc(var(--sab) + 18px);
  width: 84px; height: 84px; border-radius: 50%;
  pointer-events: auto; touch-action: none;
  border: 4px solid var(--ink);
  background: conic-gradient(#ff9cc2 var(--p), rgba(46,42,58,.55) var(--p));
  box-shadow: 0 4px 0 var(--ink);
  display: grid; place-items: center;
  cursor: pointer; padding: 0;
  font: inherit;
}
.ult-fill { position: absolute; inset: 7px; border-radius: 50%; background: var(--paper); }
.ult-label { position: relative; font-size: 13px; font-weight: 900; line-height: 1.1; color: #a88; }
.ult-btn.ready { animation: ultPulse .8s ease-in-out infinite; }
.ult-btn.ready .ult-fill { background: #fff; }
.ult-btn.ready .ult-label { color: #d0346a; }
@keyframes ultPulse { 50% { transform: scale(1.08); box-shadow: 0 4px 0 var(--ink), 0 0 18px 6px rgba(255,156,194,.9); } }
.ult-btn:active { transform: translateY(2px) scale(.96); }
.ult-key { position: absolute; bottom: -9px; left: 50%; transform: translateX(-50%); font-size: 10px; font-weight: 800; padding: 1px 6px; border-radius: 6px; background: var(--ink); color: #fff; white-space: nowrap; }
/* キーで選んでいるカード */
.cards .card:focus { outline: 3px solid var(--xp); outline-offset: 2px; transform: translateY(-2px); }
.bossbar.elite .fill { background: linear-gradient(#ffb36b, #d0602a); }
/* 爆発ボタンと重ならないよう、ボスの体力バーを少し左へ */
.bossbar { width: min(62%, 520px); margin-left: -40px; }

.home-panel { width: min(100%, 640px); }
.coin-balance { font-size: 26px; font-weight: 900; margin: 0 0 10px; }
.home-list { display: grid; grid-template-columns: 1fr; gap: 8px; text-align: left; }
.up-row {
  display: flex; align-items: center; gap: 10px;
  padding: 8px 10px; background: #fff; border: 3px solid var(--ink); border-radius: 12px;
}
.up-row canvas { width: 36px; height: 36px; flex: 0 0 36px; image-rendering: pixelated; }
.up-row.maxed { background: #f3ecd8; }
.up-text { flex: 1; min-width: 0; }
.up-name { font-weight: 800; font-size: 14px; }
.up-fx { font-size: 12px; line-height: 1.35; }
.buy-btn {
  flex: 0 0 auto; min-width: 76px; min-height: 44px;
  font: inherit; font-weight: 900; font-size: 15px; color: var(--ink);
  background: var(--gold); border: 3px solid var(--ink); border-radius: 10px; box-shadow: 0 3px 0 var(--ink);
  cursor: pointer; touch-action: manipulation;
}
.buy-btn:disabled { background: #e4dfd2; color: #9a93a4; box-shadow: none; cursor: default; }
.buy-btn:not(:disabled):active { transform: translateY(2px); box-shadow: 0 1px 0 var(--ink); }
.res-coins { margin: 0 0 8px; font-weight: 800; color: #9a6a10; }
.cards .reroll { align-self: center; }
@media (min-width: 560px) { .home-list { grid-template-columns: 1fr 1fr; } }
@media (max-height: 520px) and (orientation: landscape) {
  .home-panel { width: min(100%, 860px); }
  .home-list { grid-template-columns: 1fr 1fr 1fr; gap: 6px; }
  .up-row { padding: 5px 7px; gap: 6px; }
  .up-row canvas { width: 28px; height: 28px; flex-basis: 28px; }
  .up-name { font-size: 12px; } .up-fx { font-size: 11px; }
  .buy-btn { min-width: 60px; font-size: 13px; }
  .coin-balance { font-size: 20px; margin-bottom: 6px; }
  .ult-btn { width: 70px; height: 70px; }
  .cards .reroll { flex: 0 0 auto; }
}
/* プレイ記録（せってい） */
.playlog { margin: 14px 0 10px; padding-top: 10px; border-top: 2px dashed rgba(46,42,58,.2); }
.playlog .sub { font-size: 12px; margin-bottom: 6px; }
