<!DOCTYPE html>
<html lang="ja">
<head>
  <meta charset="UTF-8">
  <title>Unicodeシーザー暗号ジェネレーター</title>
  <style>
    body {
      font-family: Arial, sans-serif;
      background: #f0f4f8;
      color: #333;
      margin: 0;
      padding: 20px;
      display: flex;
      justify-content: center;
      align-items: center;
      min-height: 100vh;
    }
    .container {
      background: #fff;
      padding: 30px;
      border-radius: 8px;
      box-shadow: 0 4px 10px rgba(0,0,0,0.1);
      max-width: 600px;
      width: 100%;
    }
    h1 {
      text-align: center;
      margin-bottom: 20px;
    }
    label {
      display: block;
      margin: 15px 0 5px;
    }
    textarea, input[type="number"] {
      width: 100%;
      padding: 10px;
      border: 1px solid #ccc;
      border-radius: 4px;
      font-size: 1rem;
    }
    .radio-group {
      display: flex;
      justify-content: space-around;
      margin-top: 10px;
    }
    .radio-group label {
      margin: 0;
      font-weight: normal;
    }
    .result {
      background: #e8f0fe;
      padding: 10px;
      border-radius: 4px;
      min-height: 60px;
      margin-top: 10px;
      word-wrap: break-word;
    }
    button {
      display: block;
      width: 100%;
      padding: 12px;
      background: #4285f4;
      border: none;
      color: white;
      font-size: 1rem;
      border-radius: 4px;
      margin-top: 20px;
      cursor: pointer;
      transition: background-color 0.3s;
    }
    button:hover {
      background: #357ae8;
    }
  </style>
</head>
<body>
  <div class="container">
    <h1>Unicodeシーザー暗号ジェネレーター</h1>
    <label for="inputText">入力テキスト</label>
    <textarea id="inputText" rows="4" placeholder="ここに文章を入力してください"></textarea>
    
    <label for="shiftValue">シフト値 (例: 3 または -3)</label>
    <input type="number" id="shiftValue" value="3">

    <div class="radio-group">
      <label><input type="radio" name="mode" value="encode" checked> 暗号化</label>
      <label><input type="radio" name="mode" value="decode"> 復号化</label>
    </div>

    <button id="processBtn">シーザー暗号を生成</button>

    <label for="outputText">結果</label>
    <div id="outputText" class="result"></div>
  </div>

  <script>
    // シーザー暗号の処理（Unicodeベース）
    function caesarCipher(str, shift, mode = 'encode') {
      if(mode === 'decode') {
        shift = -shift;
      }
      let result = '';
      for(let i = 0; i < str.length; i++) {
        const code = str.charCodeAt(i);
        const newCode = code + shift;
        result += String.fromCharCode(newCode);
      }
      return result;
    }

    const inputText = document.getElementById('inputText');
    const shiftValue = document.getElementById('shiftValue');
    const processBtn = document.getElementById('processBtn');
    const outputText = document.getElementById('outputText');
    const modeRadios = document.getElementsByName('mode');

    function processText() {
      const text = inputText.value;
      const shiftParsed = parseInt(shiftValue.value, 10);
      if(isNaN(shiftParsed)) {
        outputText.textContent = "シフト値は数値で入力してください";
        return;
      }
      let mode = 'encode';
      for (const radio of modeRadios) {
        if(radio.checked) {
          mode = radio.value;
          break;
        }
      }
      const result = caesarCipher(text, shiftParsed, mode);
      outputText.textContent = result;
    }

    processBtn.addEventListener('click', processText);
    inputText.addEventListener('input', processText);
    shiftValue.addEventListener('input', processText);
    for (const radio of modeRadios) {
      radio.addEventListener('change', processText);
    }
  </script>
</body>
</html>
