<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Base64 Encoder/Decoder</title>
  <style>
    body {
      font-family: sans-serif;
      display: flex;
      justify-content: center;
      align-items: center;
      height: 100vh;
      margin: 0;
      background-color: #f0f0f0;
    }
    .container {
      display: flex;
      flex-direction: column;
      gap: 10px;
      width: 80%;
      max-width: 800px;
    }
    textarea {
      width: 100%;
      height: 150px;
      padding: 10px;
      border: 1px solid #ccc;
      border-radius: 5px;
      font-size: 16px;
      box-sizing: border-box;
    }
    .buttons {
      display: flex;
      gap: 10px;
    }
    button {
      padding: 10px 20px;
      border: none;
      border-radius: 5px;
      background-color: #007bff;
      color: white;
      font-size: 16px;
      cursor: pointer;
    }
    button:hover {
      background-color: #0056b3;
    }
  </style>
</head>
<body>
  <div class="container">
    <textarea id="text1" placeholder="テキストボックス1"></textarea>
    <div class="buttons">
      <button id="encode-btn">Encode</button>
      <button id="decode-btn">Decode</button>
    </div>
    <textarea id="text2" placeholder="テキストボックス2"></textarea>
  </div>

  <script>
    const text1 = document.getElementById('text1');
    const text2 = document.getElementById('text2');
    const encodeBtn = document.getElementById('encode-btn');
    const decodeBtn = document.getElementById('decode-btn');

    encodeBtn.addEventListener('click', () => {
      try {
        const inputText = text1.value;
        const encodedText = btoa(unescape(encodeURIComponent(inputText)));
        text2.value = encodedText;
      } catch (e) {
        alert('エンコードに失敗しました。');
      }
    });

    decodeBtn.addEventListener('click', () => {
      try {
        const inputText = text2.value;
        const decodedText = decodeURIComponent(escape(atob(inputText)));
        text1.value = decodedText;
      } catch (e) {
        alert('デコードに失敗しました。入力が有効なBase64文字列であることを確認してください。');
      }
    });
  </script>
</body>
</html>
