:root {
    /* ★★★ CSS変数を定義 ★★★ */
    --joystick-size: 120px;
}

body {
    margin: 0;
    overflow: hidden;
    font-family: sans-serif;
    touch-action: none;
    overscroll-behavior: none;
}

canvas {
    display: block;
}

/* --- UI Controls --- */
#ui-container {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    pointer-events: none;
}

#ui-container>* {
    pointer-events: auto;
}

#top-left-controls {
    position: fixed;
    top: 20px;
    left: 20px;
}

#bottom-left-controls {
    position: fixed;
    left: 30px;
    bottom: 30px;
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 15px;
}

#joystick-container {
    width: var(--joystick-size);
    height: var(--joystick-size);
    background-color: rgba(255, 255, 255, 0.2);
    border-radius: 50%;
    display: flex;
    justify-content: center;
    align-items: center;
}

#joystick-knob {
    width: calc(var(--joystick-size) / 2);
    height: calc(var(--joystick-size) / 2);
    background-color: rgba(255, 255, 255, 0.5);
    border-radius: 50%;
    transition: transform 0.1s;
}

#vertical-controls {
    display: flex;
    flex-direction: column;
    gap: 10px;
}

.vertical-button {
    width: 50px;
    height: 50px;
    background-color: rgba(255, 255, 255, 0.3);
    border: 2px solid rgba(255, 255, 255, 0.5);
    border-radius: 10px;
    color: white;
    font-size: 24px;
    font-weight: bold;
    display: flex;
    justify-content: center;
    align-items: center;
    cursor: pointer;
    user-select: none;
}

#gyro-button {
    position: fixed;
    /* ★★★ 配置を画面上部中央に変更 ★★★ */
    top: 30px;
    left: 50%;
    transform: translateX(-50%);

    /* ★★★ デザインを角丸四角形に変更 & 装飾を追加 ★★★ */
    background-color: rgba(0, 0, 0, 0.4);
    color: white;
    border: 1px solid rgba(255, 255, 255, 0.5);
    border-radius: 12px;
    /* ←角を丸める */
    padding: 12px 24px;
    font-size: 16px;
    font-weight: bold;
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.3);
    backdrop-filter: blur(8px);
    /* ガラスのようなすりガラス効果 */
    -webkit-backdrop-filter: blur(8px);
    /* Safari向け */

    /* ★★★ 操作感を向上 ★★★ */
    cursor: pointer;
    transition: background-color 0.2s, transform 0.1s;
    z-index: 201;
    /* 念のため最前面に */
}

#orientation-warning,
#loading-screen {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background-color: rgba(0, 0, 0, 0.8);
    color: white;
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    font-size: 24px;
    z-index: 200;
    transition: opacity 0.5s;
}

#orientation-warning {
    display: none;
}

#progress-bar-container {
    width: 80%;
    max-width: 400px;
    height: 20px;
    background-color: #555;
    border-radius: 10px;
    overflow: hidden;
    margin-top: 20px;
}

#progress-bar {
    width: 0%;
    height: 100%;
    background-color: #eee;
    transition: width 0.2s;
}

#version-display {
    position: fixed;
    right: 10px;
    bottom: 10px;
    font-size: 12px;
    color: rgba(255, 255, 255, 0.5);
}

#settings-button {
    width: 40px;
    height: 40px;
    background-color: rgba(0, 0, 0, 0.3);
    color: white;
    border: none;
    border-radius: 50%;
    font-size: 24px;
    cursor: pointer;
    display: flex;
    justify-content: center;
    align-items: center;
    padding: 0;
}

#settings-button svg {
    width: 24px;
    height: 24px;
}

#settings-modal-overlay {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background-color: rgba(0, 0, 0, 0.6);
    display: flex;
    justify-content: center;
    align-items: center;
    z-index: 300;
}

#settings-modal {
    background-color: #2c2c2c;
    padding: 20px;
    border-radius: 10px;
    width: 80%;
    max-width: 400px;
    color: white;
    max-height: 85vh;
    display: flex;
    flex-direction: column;
}

.modal-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    border-bottom: 1px solid #444;
    padding-bottom: 10px;
    margin-bottom: 10px;
}

.modal-header h2 {
    margin: 0;
}

#close-modal-button {
    background: none;
    border: none;
    color: white;
    font-size: 24px;
    cursor: pointer;
}

.modal-body {
    display: flex;
    flex-direction: column;
    gap: 15px;
    overflow-y: auto;
    /* ★★★ 修正(2): この行を追加 ★★★ */
}

.settings-item {
    width: 100%;
}

.settings-item label {
    display: block;
    margin-bottom: 8px;
    font-size: 14px;
    color: #ccc;
}

.modal-button {
    width: 100%;
    padding: 12px;
    background-color: #444;
    color: white;
    border: none;
    border-radius: 5px;
    font-size: 16px;
    text-align: left;
    cursor: pointer;
}

.modal-button:hover {
    background-color: #555;
}

.toggle-switch {
    display: flex;
    border: 1px solid #555;
    border-radius: 5px;
    overflow: hidden;
}

.toggle-option {
    flex: 1;
    padding: 8px;
    text-align: center;
    background-color: #444;
    cursor: pointer;
    font-size: 14px;
    transition: background-color 0.2s;
}

.toggle-option.active {
    background-color: #007aff;
    color: white;
}


.hidden {
    display: none !important;
}

/* --- タッチ操作の有効化 --- */
#gyro-button,
#settings-button,
.vertical-button,
.modal-button,
#close-modal-button,
.toggle-option,
#joystick-container {
    touch-action: auto;
}

/* --- On-Screen Debug Console --- */
#debug-console {
    /* 普段は非表示にしておく */
    display: none;
    flex-direction: column;
    /* ヘッダーとログを縦に並べる */
    position: fixed;
    bottom: 10px;
    right: 10px;
    width: 400px;
    max-width: 90vw;
    /* 画面幅の90%まで */
    height: 250px;
    max-height: 50vh;
    /* 画面の高さの50%まで */
    background-color: rgba(20, 20, 30, 0.85);
    border: 1px solid #556;
    border-radius: 8px;
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.5);
    z-index: 9999;
    font-family: monospace;
    font-size: 13px;
    overflow: hidden;
    /* 子要素の角丸を維持 */
}

#debug-stats {
    background-color: #222;
    padding: 4px 8px;
    font-family: monospace;
    color: #0f0;
    /* 見やすいように緑色に */
    border-bottom: 1px solid #444;
}

/* ★★★ bodyにdebug-modeクラスが付いたときだけ表示 ★★★ */
body.debug-mode #debug-console {
    display: flex;
}

#debug-header {
    background-color: #334;
    color: #aab;
    padding: 6px 12px;
    font-weight: bold;
    cursor: move;
    /* ドラッグできるような見た目に */
    user-select: none;
}

#debug-log-container {
    flex: 1;
    /* 残りの高さをすべて埋める */
    overflow-y: auto;
    padding: 4px;
}

#debug-log-container .log-message {
    padding: 4px 8px;
    border-bottom: 1px solid #334;
    white-space: pre-wrap;
    word-break: break-all;
    color: #ddd;
}

#debug-log-container .log-warn {
    color: #ffc;
    background-color: rgba(102, 85, 0, 0.3);
}

#debug-log-container .log-error {
    color: #fcc;
    background-color: rgba(102, 0, 0, 0.3);
}