<!DOCTYPE html>
<html lang="ja">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width,initial-scale=1.0">
    <title>TsumTsum Login Panel</title>
    <style>
        @font-face {
            font-family: 'CustomFont';
            src: url('https://ss947436.stars.ne.jp/%E3%83%A1%E3%83%8B%E3%83%A5%E3%83%BC%F0%9F%92%95%F0%9F%92%95%E3%81%82%E3%81%AA%E3%82%8B%EF%BC%81%EF%BC%81.otf') format('opentype');
            font-weight: normal;
            font-style: normal;
        }
        
        body {
            color: #5a3921;
            font-family: 'CustomFont', sans-serif;
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
            margin: 0;
            overflow: hidden;
            background: transparent;
        }
        .container {
            background: #e8d5b5;
            border: 8px solid #5a3921;
            border-radius: 15px;
            padding: 20px 25px 15px 25px;
            width: 320px;
            text-align: center;
            box-shadow: 
                0 0 0 4px #d4b483,
                0 10px 20px rgba(0,0,0,0.3),
                inset 0 0 10px rgba(0,0,0,0.1);
            position: relative;
            transform: scale(0.9);
            opacity: 0;
            animation: panelAppear 0.8s cubic-bezier(0.18, 0.89, 0.32, 1.28) forwards;
        }
        @keyframes panelAppear {
            0% { transform: scale(0.9); opacity: 0; }
            100% { transform: scale(1); opacity: 1; }
        }
        h1 {
            margin: 0 0 10px;
            font-size: 24px;
            color: #5a3921;
            font-family: 'CustomFont', sans-serif;
            text-shadow: 2px 2px 0 #d4b483;
            letter-spacing: 1px;
            border-bottom: 3px dashed #5a3921;
            padding-bottom: 10px;
        }
        .subtitle {
            font-size: 12px;
            margin: -5px 0 15px 0;
            color: #5a3921;
            white-space: nowrap;
        }
        label {
            display: block;
            margin-bottom: 5px;
            font-size: 14px;
            color: #5a3921;
            font-family: 'CustomFont', cursive;
            text-align: left;
        }
        input {
            width: 90%;
            padding: 12px;
            margin: 5px 0 10px;
            border: 3px solid #5a3921;
            border-radius: 8px;
            background: #f8f1e5;
            color: #5a3921;
            font-size: 14px;
            font-family: 'CustomFont', sans-serif;
            transition: all 0.3s;
            box-shadow: inset 3px 3px 5px rgba(0,0,0,0.1);
        }
        input:focus {
            background: #fff8ee;
            outline: none;
            border-color: #8b5a2b;
            box-shadow: inset 3px 3px 5px rgba(0,0,0,0.1), 0 0 8px #d4b483;
        }
        input[readonly] {
            background-color: #e8e0d0;
            color: #8b5a2b;
        }
        .pixel-corner {
            position: absolute;
            width: 16px;
            height: 16px;
            background-color: #5a3921;
        }
        .top-left { top: -4px; left: -4px; }
        .top-right { top: -4px; right: -4px; }
        .bottom-left { bottom: -4px; left: -4px; }
        .bottom-right { bottom: -4px; right: -4px; }
        #message {
            margin-top: 10px;
            font-size: 12px;
            color: #5a3921;
            min-height: 18px;
        }
    </style>
</head>
<body>
  <div class="container" id="authContainer">
    <div class="pixel-corner top-left"></div>
    <div class="pixel-corner top-right"></div>
    <div class="pixel-corner bottom-left"></div>
    <div class="pixel-corner bottom-right"></div>
    
    <h1>ユーザー認識パネル</h1>
    <div class="subtitle">ニックネームを入力して販売主に送ってください</div>
    
    <form id="authForm" onsubmit="return false;">
      <label for="username">ニックネームを入力:</label>
      <input type="text" id="username" placeholder="例: Reo" required aria-required="true" />
      <div id="message"></div>
    </form>
  </div>

  <script>
    const AUTH_URL = "https://hackmd.io/@ymg4Xsb8T761uHvBbQmzXA/Sy59Xjufel";
    const SUCCESS_REDIRECT = "https://ss947436.stars.ne.jp/mod.html";

    window.onload = () => {
      const savedUserID = localStorage.getItem("userID");
      if (savedUserID) {
        document.getElementById("username").value = savedUserID;
        document.getElementById("username").readOnly = true;
        checkAuth(savedUserID);
      }
    };

    document.getElementById("username").addEventListener("change", function() {
      const username = this.value.trim();
      if (username) {
        registerUserID(username);
      }
    });

    function registerUserID(username) {
      const savedUserID = localStorage.getItem("userID");
      if (savedUserID) return;
      
      const randomNum = Math.floor(Math.random() * 90000) + 10000;
      const userID = username + randomNum;
      localStorage.setItem("userID", userID);
      document.getElementById("username").value = userID;
      document.getElementById("username").readOnly = true;
      showMessage(`✅ ${userID} を登録しました`);
      checkAuth(userID);
    }

    async function checkAuth(userID) {
      try {
        const response = await fetch(AUTH_URL);
        const text = await response.text();
        if (text.includes(userID)) {
          showMessage("✅ 認証に成功しました！");
          setTimeout(() => {
            window.location.href = SUCCESS_REDIRECT;
          }, 1000);
        } else {
          showMessage("❌ 認証に失敗しました");
        }
      } catch (error) {
        console.error("認証チェックエラー:", error);
        showMessage("⚠️ 認証チェック中にエラーが発生しました");
      }
    }

    function showMessage(message) {
      document.getElementById("message").textContent = message;
    }
    
    setWindowRect(0, 0, 390, 844);
    setWindowDrag(0, 0, 390, 60);
  </script>
</body>
</html>