* {
    -webkit-user-select: none;
    user-select: none;
    -webkit-touch-callout: none;
    -webkit-tap-highlight-color: transparent;
    box-sizing: border-box;
}

html, body {
    margin: 0; padding: 0; height: 100%; overflow: hidden; background: #05070c;
    font-family: 'Segoe UI', system-ui, -apple-system, "Hiragino Sans", "Noto Sans JP", sans-serif;
    touch-action: none; overscroll-behavior: none;
    -webkit-text-size-adjust: 100%;
}
canvas { display: block; position: fixed; top: 0; left: 0; touch-action: none; }

/* ---------- ロビー ---------- */
#lobby-modal {
    position: fixed; inset: 0;
    background: rgba(5, 7, 12, 0.85);
    display: flex; overflow-y: auto; padding: 16px;
    z-index: 100; backdrop-filter: blur(10px); -webkit-backdrop-filter: blur(10px);
}
.lobby-card {
    margin: auto;
    background: #0f1422; border: 1px solid rgba(0, 229, 255, 0.3); border-radius: 12px;
    padding: 30px; width: 100%; max-width: 320px;
    box-shadow: 0 12px 40px rgba(0,0,0,0.8); text-align: center; color: #fff;
}
.lobby-title { font-size: 34px; font-weight: bold; color: #00e5ff; margin-bottom: 20px; letter-spacing: 2px; }
.lobby-sub { display: block; font-size: 12px; color: #8892b0; letter-spacing: 6px; margin-top: 2px; }

.tab-container { display: flex; margin-bottom: 20px; border-bottom: 2px solid #334166; }
.tab-btn { flex: 1; padding: 10px; background: transparent; border: none; color: #8892b0; font-weight: bold; cursor: pointer; transition: 0.2s; font-size: 13px; }
.tab-btn.active { color: #00e5ff; border-bottom: 3px solid #00e5ff; }
.mode-panel { display: none; }
.mode-panel.active { display: block; }

.input-group { margin-bottom: 16px; text-align: left; }
.input-group label { font-size: 12px; color: #8892b0; display: block; margin-bottom: 6px; }
.input-group input, .input-group select {
    -webkit-user-select: auto; user-select: auto;
    width: 100%; background: #1a2236; border: 1px solid #334166;
    color: #fff; padding: 10px; border-radius: 6px; font-size: 16px; outline: none;
}
.input-group input:focus, .input-group select:focus { border-color: #00e5ff; }
.input-group input:disabled { opacity: 0.6; }

.btn-group { display: flex; gap: 10px; margin-top: 20px; }
.btn { flex: 1; padding: 12px; border: none; border-radius: 6px; font-weight: bold; font-size: 14px; cursor: pointer; transition: 0.2s; touch-action: manipulation; }
.btn:disabled { opacity: 0.4; cursor: default; }
.btn-host { background: #ff1744; color: #fff; }
.btn-join { background: #00e5ff; color: #05070c; }
.btn-single { background: #2ed573; color: #111; width: 100%; margin-top: 10px; }
.btn-ghost { background: rgba(255,255,255,0.15); color: #fff; width: 100%; }
.btn:not(:disabled):hover { opacity: 0.85; }

#lobby-players-list { margin-top: 15px; font-size: 14px; text-align: left; color: #ccd6f6; line-height: 1.6; }
#btn-start { display: none; width: 100%; margin-top: 15px; background: #ffea00; color: #111; }
#status-text { margin-top: 15px; font-size: 13px; color: #ffea00; min-height: 18px; }

/* ---------- プレイ中のUI ---------- */
#ui-container {
    position: fixed;
    top: calc(20px + env(safe-area-inset-top)); left: calc(20px + env(safe-area-inset-left));
    display: flex; flex-direction: column; gap: 8px; pointer-events: none; z-index: 10;
}
.score-box {
    background: rgba(15, 20, 34, 0.65); border-radius: 6px; padding: 8px 16px;
    box-shadow: 0 4px 16px rgba(0,0,0,0.5); backdrop-filter: blur(6px); -webkit-backdrop-filter: blur(6px);
    min-width: 100px; text-align: left;
}
.player-title { font-size: 11px; font-weight: bold; letter-spacing: 0.5px; margin-bottom: 2px; }
.score-val { font-size: 22px; font-weight: bold; color: #fff; text-shadow: 0 0 8px rgba(255,255,255,0.4); }

#timer-box {
    position: fixed; top: calc(20px + env(safe-area-inset-top)); left: 50%; transform: translateX(-50%);
    background: rgba(15, 20, 34, 0.65); border: 2px solid #ffea00; border-radius: 8px;
    padding: 4px 28px; box-shadow: 0 4px 16px rgba(0,0,0,0.5); text-align: center; color: #fff;
    z-index: 10; pointer-events: none; backdrop-filter: blur(6px); -webkit-backdrop-filter: blur(6px);
}
#timer-title { font-size: 12px; font-weight: bold; color: #ffea00; letter-spacing: 2px; margin-bottom: -2px; }
#timer-val { font-size: 32px; font-weight: bold; }

#log-container {
    position: fixed; bottom: calc(80px + env(safe-area-inset-bottom)); left: calc(20px + env(safe-area-inset-left));
    display: flex; flex-direction: column; gap: 8px; pointer-events: none; z-index: 20;
    max-width: calc(100vw - 200px);
}
.log-msg {
    background: rgba(15, 20, 34, 0.75); border-left: 4px solid #fff;
    padding: 8px 14px; border-radius: 4px; font-size: 13px; font-weight: bold; color: #fff;
    box-shadow: 0 2px 10px rgba(0,0,0,0.5); backdrop-filter: blur(4px); -webkit-backdrop-filter: blur(4px);
    animation: slideIn 0.3s ease-out forwards, fadeOut 0.5s ease-in 3.5s forwards;
    opacity: 0; transform: translateX(-20px);
}
@keyframes slideIn { to { transform: translateX(0); opacity: 1; } }
@keyframes fadeOut { to { opacity: 0; } }

#countdown-modal {
    display: none; position: fixed; inset: 0;
    justify-content: center; align-items: center; z-index: 150; pointer-events: none;
    background: rgba(0, 0, 0, 0.3);
}
#countdown-text {
    font-size: 140px; font-weight: bold; color: #ffea00;
    text-shadow: 0 0 40px rgba(255, 234, 0, 0.8), 0 0 10px #ff1744;
    font-style: italic;
}
@keyframes popAnim {
    0% { transform: scale(0.5); opacity: 0; }
    50% { transform: scale(1.2); opacity: 1; }
    100% { transform: scale(1); opacity: 1; }
}

#guide-panel {
    position: fixed; bottom: calc(20px + env(safe-area-inset-bottom)); left: 50%; transform: translateX(-50%);
    background: rgba(15, 20, 34, 0.7); border: 1px solid rgba(255, 255, 255, 0.2);
    border-radius: 6px; padding: 8px 20px; color: #ccd6f6; font-size: 12px; display: flex; gap: 16px;
    pointer-events: none; backdrop-filter: blur(4px); -webkit-backdrop-filter: blur(4px); z-index: 10; white-space: nowrap;
}
.key-badge { background: #222b45; color: #fff; padding: 2px 6px; border-radius: 4px; font-weight: bold; }

/* ---------- 十字ボタン ---------- */
#virtual-pad {
    position: fixed;
    bottom: calc(20px + env(safe-area-inset-bottom)); right: calc(20px + env(safe-area-inset-right));
    width: 140px; height: 140px; display: none; z-index: 50;
}
.d-btn {
    position: absolute; width: 45px; height: 45px;
    background: rgba(15, 20, 34, 0.6); border: 2px solid rgba(0, 229, 255, 0.6);
    border-radius: 8px; color: #00e5ff; font-size: 20px; font-weight: bold;
    display: flex; justify-content: center; align-items: center;
    touch-action: manipulation; box-shadow: 0 4px 12px rgba(0,0,0,0.5);
    backdrop-filter: blur(4px); -webkit-backdrop-filter: blur(4px);
}
.d-btn:active { background: rgba(0, 229, 255, 0.5); color: #fff; border-color: #fff; }
.d-btn.up { top: 0; left: 47.5px; }
.d-btn.down { bottom: 0; left: 47.5px; }
.d-btn.left { top: 47.5px; left: 0; }
.d-btn.right { top: 47.5px; right: 0; }

/* タッチ操作の端末なら、縦でも横でも十字ボタンを出す */
@media (hover: none) and (pointer: coarse) {
    #virtual-pad { display: block; }
    #guide-panel { display: none; }
    #log-container { bottom: calc(20px + env(safe-area-inset-bottom)); }
}

/* 小さい画面ではタイマーを少し小さく */
@media (max-width: 420px) {
    #timer-box { padding: 2px 14px; }
    #timer-val { font-size: 26px; }
    #countdown-text { font-size: 110px; }
}

/* ---------- リザルト ---------- */
#result-modal {
    display: none; position: fixed; inset: 0;
    background: rgba(5, 7, 12, 0.95); overflow-y: auto; padding: 16px;
    z-index: 200; backdrop-filter: blur(12px); -webkit-backdrop-filter: blur(12px);
}
.result-card {
    margin: auto;
    background: #0f1422; border: 2px solid #ffea00; border-radius: 16px;
    padding: 32px 26px; box-shadow: 0 12px 60px rgba(255, 234, 0, 0.2);
    text-align: center; color: #fff; width: 100%; max-width: 360px;
}
.result-title { font-size: 24px; font-weight: bold; color: #ffea00; margin-bottom: 24px; letter-spacing: 4px; }
#result-list { font-size: 17px; margin-bottom: 16px; text-align: left; }
.result-row { display: flex; justify-content: space-between; gap: 8px; margin-bottom: 10px; padding-bottom: 10px; border-bottom: 1px solid rgba(255,255,255,0.1); }
#result-message { font-size: 32px; font-weight: bold; margin-top: 16px; text-shadow: 0 0 20px rgba(255,255,255,0.5); }
.result-buttons { display: flex; flex-direction: column; gap: 10px; margin-top: 24px; }
.result-buttons .btn { margin-top: 0; }
.time-bonus {
  position: fixed;
  top: 80px;
  left: 50%;
  transform: translateX(-50%);
  color: #7fffb0;
  font-size: 28px;
  font-weight: bold;
  text-shadow: 0 0 8px rgba(0,0,0,.8);
  pointer-events: none;
  z-index: 50;
  animation: timeBonusUp 1.2s ease-out forwards;
}
.time-bonus.big { color: #ffd84a; font-size: 40px; }
@keyframes timeBonusUp {
  0%   { opacity: 0; transform: translate(-50%, 10px) scale(.8); }
  15%  { opacity: 1; transform: translate(-50%, 0) scale(1.1); }
  100% { opacity: 0; transform: translate(-50%, -30px) scale(1); }
}
#result-extra {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 12px;
  margin: 14px 0;
}
.result-best {
  color: #ccd6f6;
  font-size: 16px;
}
.result-best.new {
  color: #ffea00;
  font-weight: bold;
  text-shadow: 0 0 10px rgba(255, 234, 0, .6);
  animation: popAnim 0.5s ease-out;
}
.btn-share {
  display: inline-block;
  padding: 10px 24px;
  background: #000;
  color: #fff;
  border: 1px solid #555;
  border-radius: 999px;
  font-weight: bold;
  text-decoration: none;
}
.btn-share:hover { background: #222; }
/* PC：左右の広告 */
.ad-side {
  position: fixed;
  top: 50%;
  transform: translateY(-50%);
  width: 160px;
  height: 600px;
  z-index: 1000;
}
.ad-side.left  { left: 10px; }
.ad-side.right { right: 10px; }
.ad-side.hidden { display: none; }
.ad-side iframe { display: block; border: 0; }

/* スマホ：画面の中の広告（ボタンから28px離す） */
.ad-slot { margin-top: 28px; display: flex; justify-content: center; width: 100%; }
.ad-slot:empty { display: none; }
.ad-slot iframe { display: block; border: 0; }

/* 広告で縦に長くなっても、ロビーと結果画面をスクロールできるようにする */
#lobby-modal, #result-modal {
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  touch-action: pan-y;
}
#lobby-modal > *, #result-modal > * { margin: auto; }
.lobby-card .ad-slot  { margin-left: -30px; margin-right: -30px; width: auto; }
.result-card .ad-slot { margin-left: -26px; margin-right: -26px; width: auto; }
