body {
    margin: 0;
    padding: 0;
    background-color: #202020;
    color: #ffffff;
    font-family: sans-serif;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    height: 100vh;
    /* メニューバーの高さを引く */
    padding-bottom: 60px;
    box-sizing: border-box;
    overflow: hidden;
}

input[type="file"] {
    padding: 10px;
    font-size: 16px;
    cursor: pointer;
    background-color: #333;
    color: white;
    border: 1px solid #555;
    border-radius: 5px;
}

#status {
    margin-top: 10px;
    color: #aaa;
    font-size: 14px;
}

.connected {
    color: #4caf50 !important;
}

#menu-bar {
    position: fixed;
    bottom: 0;
    left: 0;
    width: 100%;
    background-color: #1a1a1a;
    border-top: 1px solid #444;
    display: flex;
    justify-content: center;
    align-items: center;
    height: 60px;
    /* 高さを固定 */
    padding: 5px 0;
    /* 上下パディングを調整 */
    gap: 15px;
    transition: transform 0.3s ease-in-out;
    z-index: 100;
}

/* メニューバーを隠す */
body.menu-hidden #menu-bar {
    transform: translateY(100%);
}

#menu-bar button {
    background-color: #007bff;
    color: white;
    border: none;
    padding: 10px;
    border-radius: 50%;
    width: 50px;
    height: 50px;
    display: flex;
    justify-content: center;
    align-items: center;
    cursor: pointer;
    transition: background-color 0.2s;
}

#menu-bar button:hover {
    background-color: #0056b3;
}

#menu-bar button:disabled {
    background-color: #555;
    cursor: not-allowed;
}

/* ゲーム画面（Canvas）のサイズ調整 */
canvas {
    position: fixed !important;
    top: 0 !important;
    left: 0 !important;
    width: 100% !important;
    height: calc(100vh - 60px) !important;
    /* メニューバーの分を空ける */
    object-fit: contain !important;
    z-index: 50 !important;
}

/* 横向きでメニューバーが隠れているときのCanvas */
body.menu-hidden canvas {
    height: 100vh !important;
}

#drop-overlay {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(0, 0, 0, 0.8);
    display: none;
    justify-content: center;
    align-items: center;
    z-index: 200;
    color: white;
    font-size: 24px;
    pointer-events: none;
}

/* --- オンスクリーンコントローラー --- */
#onscreen-controls {
    position: fixed;
    bottom: 0;
    left: 0;
    width: 100%;
    height: 100%;
    z-index: 90;
    /* ゲーム起動時にJSで表示 */
    display: none;
    color: white;
    user-select: none;
    -webkit-user-select: none;
    -webkit-touch-callout: none; /* iOSでの長押しメニューを抑制 */
    touch-action: manipulation;
    pointer-events: none;
    /* コンテナ自体はタッチを透過 */
}

.onsc-btn {
    position: absolute;
    background-color: rgba(128, 128, 128, 0.4);
    border: 2px solid rgba(255, 255, 255, 0.5);
    pointer-events: auto;
    /* ボタン自体はタッチ可能に */
    display: flex;
    justify-content: center;
    align-items: center;
    font-weight: bold;
    color: rgba(255, 255, 255, 0.8);
    font-size: 16px;
    box-sizing: border-box;
}

.onsc-btn:active {
    background-color: rgba(255, 255, 255, 0.6);
}

/* 上部ボタン */
.shoulder-buttons {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100px;
}

.shoulder-btn {
    width: 120px;
    height: 50px;
    border-radius: 10px;
    top: 15px;
    font-size: 20px;
}

#onsc-l {
    left: 15px;
}

#onsc-r {
    right: 15px;
}

/* 下部コントロール */
.main-controls {
    position: absolute;
    bottom: 0;
    left: 0;
    width: 100%;
    height: 220px;
    /* コントローラー領域の高さ */
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 0 20px 20px 20px;
    box-sizing: border-box;
}

/* メニューバーが表示されているときは、その高さ分だけコントローラーを上にずらす */
body:not(.menu-hidden) .main-controls {
    bottom: 60px;
}

/* D-Pad */
.dpad {
    position: relative;
    width: 180px;
    height: 180px;
}

.dpad-btn {
    width: 60px;
    height: 60px;
    border-radius: 15px;
    background-repeat: no-repeat;
    background-position: center;
    background-size: 60%;
}

.dpad-btn.up {
    top: 0;
    left: 60px;
    background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='white'%3E%3Cpath d='M7.41 15.41L12 10.83l4.59 4.58L18 14l-6-6-6 6z'/%3E%3C/svg%3E");
}

.dpad-btn.down {
    bottom: 0;
    left: 60px;
    background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='white'%3E%3Cpath d='M7.41 8.59L12 13.17l4.59-4.58L18 10l-6 6-6-6z'/%3E%3C/svg%3E");
}

.dpad-btn.left {
    top: 60px;
    left: 0;
    background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='white'%3E%3Cpath d='M15.41 7.41L10.83 12l4.58 4.59L14 18l-6-6 6-6z'/%3E%3C/svg%3E");
}

.dpad-btn.right {
    top: 60px;
    right: 0;
    background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='white'%3E%3Cpath d='M8.59 16.59L13.17 12 8.59 7.41 10 6l6 6-6 6z'/%3E%3C/svg%3E");
}

/* アクションボタン */
.action-buttons {
    position: relative;
    width: 180px;
    height: 180px;
}

.action-btn {
    width: 70px;
    height: 70px;
    border-radius: 50%;
    font-size: 24px;
}

/* SNES-style layout */
.action-btn.y {
    top: 35px;
    left: 0;
}

.action-btn.x {
    top: 0;
    left: 70px;
}

.action-btn.b {
    top: 105px;
    left: 35px;
}

.action-btn.a {
    top: 70px;
    right: 0;
}

/* 中央ボタン群 */
.center-buttons {
    position: absolute;
    bottom: 20px;
    left: 50%;
    transform: translateX(-50%);
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 15px;
}

.menu-toggle-btn {
    width: 80px;
    height: 40px;
    border-radius: 20px;
    font-size: 24px;
}

.system-buttons {
    display: flex;
    gap: 20px;
}

.system-btn {
    width: 90px;
    height: 40px;
    border-radius: 20px;
}

/* 中央ボタン群の中のボタンは flex レイアウトに従うように position をリセット */
.center-buttons .onsc-btn {
    position: static;
}

/* --- Core-specific Onscreen Controller Layouts --- */

/* Default (SNES) layout: All buttons visible */
.layout-snes {
    /* No changes needed, this is the default */
}

/* NES / Game Boy / PC Engine Layout (A, B buttons) */
.layout-nes #onsc-x,
.layout-nes #onsc-y,
.layout-nes #onsc-l,
.layout-nes #onsc-r,
.layout-gb #onsc-x,
.layout-gb #onsc-y,
.layout-gb #onsc-l,
.layout-gb #onsc-r,
.layout-pce #onsc-x,
.layout-pce #onsc-y,
.layout-pce #onsc-l,
.layout-pce #onsc-r {
    display: none;
}

/* GBA Layout (A, B, L, R buttons) */
.layout-gba #onsc-x,
.layout-gba #onsc-y {
    display: none;
}

/* Mega Drive / Genesis Layout (A, B, C buttons) */
.layout-md #onsc-x,
/* X is not used */
.layout-md #onsc-l,
.layout-md #onsc-r {
    display: none;
}

/* Change labels for Mega Drive */
.layout-md .action-btn {
    color: transparent;
}

/* RetroPad B -> MD A, RetroPad Y -> MD B, RetroPad A -> MD C */
.layout-md #onsc-b::after {
    content: 'A';
    color: rgba(255, 255, 255, 0.8);
}

.layout-md #onsc-y::after {
    content: 'B';
    color: rgba(255, 255, 255, 0.8);
}

.layout-md #onsc-a::after {
    content: 'C';
    color: rgba(255, 255, 255, 0.8);
}