:root {
    --bg-color: #00040a;
    --ui-color: #00f2ff;
    --accent-color: #ff00ea;
    --key-base: #222;
    --key-side: #444;
    --safe-area-bottom: env(safe-area-inset-bottom);
}

body { 
    margin: 0; 
    overflow: hidden; 
    background: #000; 
    display: flex; 
    justify-content: center; 
    align-items: center; 
    height: 100vh; /* 追加: フォールバック */
    height: 100dvh; 
    font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif; 
}

#game-container { 
    position: relative; 
    width: 100%; 
    max-width: calc(100vh * 9 / 16); /* dvhをvhに変更 */
    height: 100vh; /* 追加: フォールバック */
    height: 100dvh; 
    background: var(--bg-color); 
    display: flex; 
    flex-direction: column; 
    overflow: hidden; 
}

/* タイトル画面レイヤー */
.ui-screen {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 70%;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    z-index: 10;
    background: rgba(0, 4, 10, 0.85);
    color: white;
    pointer-events: auto;
}

.game-title {
    font-size: 2.2rem;
    margin-bottom: 2.5rem;
    color: var(--ui-color);
    text-shadow: 0 0 20px rgba(0, 242, 255, 0.5);
    letter-spacing: 4px;
    text-align: center;
    font-weight: 900;
}

.stage-select {
    display: flex;
    flex-direction: column;
    gap: 15px;
    width: 70%;
}

/* キーキャップ共通スタイル */
.stage-btn, .book-btn, .circle-btn {
    position: relative;
    border: none;
    cursor: pointer;
    font-weight: bold;
    text-transform: uppercase;
    transition: all 0.1s ease;
    display: flex;
    align-items: center;
    justify-content: center;
    outline: none;
}

/* ステージボタン：標準キーキャップ風 */
.stage-btn {
    padding: 15px;
    background: var(--key-base);
    color: var(--ui-color);
    border-radius: 8px;
    box-shadow: 
        0 6px 0 var(--key-side), 
        0 8px 15px rgba(0,0,0,0.5);
    transform: translateY(-4px);
    font-size: 14px;
    letter-spacing: 1px;
}

.stage-btn:active {
    transform: translateY(0px);
    box-shadow: 
        0 2px 0 var(--key-side), 
        0 3px 10px rgba(0,0,0,0.5);
}

#canvas-wrapper { 
    flex: 7; 
    position: relative; 
    width: 100%; 
}

#ui-panel { 
    flex: 3; 
    position: relative; 
    background: linear-gradient(180deg, #020a15 0%, #000 100%); 
    border-top: 2px solid #111; 
    display: flex; 
    justify-content: space-between; 
    align-items: center; 
    padding: 0 20px;
    padding-bottom: var(--safe-area-bottom); 
}

#ui-info { 
    position: absolute; 
    top: 10px; 
    left: 15px; 
    color: var(--ui-color); 
    font-size: 18px; 
    pointer-events: none; 
    z-index: 5; 
    opacity: 0.6;
    letter-spacing: 1px;
}

#progress-value {
    font-size: 32px;
    font-weight: bold;
}

/* ジョイスティックを中央に固定 */
.center-controls {
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);
    pointer-events: none;
}

#joystick-base { 
    width: 100px; 
    height: 100px; 
    background: rgba(0, 242, 255, 0.03); 
    border: 3px solid rgba(0, 242, 255, 0.2); 
    border-radius: 50%; 
    position: relative; 
    touch-action: none; 
    pointer-events: auto;
}

#joystick-stick { 
    width: 40px; 
    height: 40px; 
    background: var(--ui-color); 
    border-radius: 50%; 
    position: absolute; 
    top: 30px; 
    left: 30px; 
    box-shadow: 0 0 20px var(--ui-color); 
    pointer-events: none; 
}

/* サイドコントロール */
.side-controls {
    display: flex;
    gap: 12px;
}

/* 左側：縦型キーキャップ（本棚風） */
/* left-sideのレイアウトを調整 */
.left-side {
    display: flex;
    gap: 12px;
    align-items: flex-end;
    padding-bottom: 10px; /* 下側に寄せておく */
}

.book-btn {
    width: 36px;
    height: 85px;
    background: #2a2a2a;
    color: #fff;
    border-radius: 4px;
    box-shadow: 
        -4px 0 0 #111,
        0 4px 0 #111;
    writing-mode: vertical-rl;
    text-orientation: upright;
    font-size: 11px;
    transform: translateY(-2px);
}

.book-btn:active {
    transform: translateY(2px) translateX(-2px);
    box-shadow: -1px 0 0 #111, 0 1px 0 #111;
}

/* 右側：特殊キー（Shift/Enter風） */
.right-side {
    position: absolute;
    top: 15px;
    right: 20px;
    flex-direction: row;
    gap: 10px;
    justify-content: center;
}

.circle-btn {
    width: 90px;
    padding: 10px 0;
    background: #333;
    color: var(--accent-color);
    border-radius: 6px;
    font-size: 10px;
    box-shadow: 
        0 4px 0 #1a1a1a,
        0 6px 10px rgba(0,0,0,0.4);
    transform: translateY(-2px);
}

.circle-btn#btn-restart {
    position: absolute;
    top: 15px;      /* SETTINGボタンと高さを合わせる */
    left: 20px;     /* 左からの余白。SETTINGのrightと同じ値にすると対称になる */
    z-index: 10;    /* 他のUIより前面に */
    color: #ff4444; 
    margin-bottom: auto; /* 下のボタンとの距離を稼ぎ、自身を上に配置 */
}

.circle-btn:active {
    transform: translateY(2px);
    box-shadow: 0 1px 0 #1a1a1a;
}

#ui-panel, 
.ui-screen,
button {
    -webkit-user-select: none;
    user-select: none;
    -webkit-touch-callout: none;
}

/* --------------------------------------------------
   設定パネル関連
-------------------------------------------------- */
#settings-panel {
    position: absolute;
    bottom: 0;
    left: 0;
    width: 100%;
    height: 100%; 
    background: #050a10;
    z-index: 100;
    display: flex;
    flex-direction: column;
    padding: 15px;
    box-sizing: border-box;
    border-top: 2px solid var(--ui-color);
    transition: transform 0.4s cubic-bezier(0.16, 1, 0.3, 1);
}

#settings-panel.slide-down {
    transform: translateY(100%);
}

#settings-panel.slide-up {
    transform: translateY(0%);
}

.settings-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 10px;
    border-bottom: 1px solid #333;
    padding-bottom: 5px;
}

.settings-title {
    color: var(--ui-color);
    font-size: 12px;
    font-weight: bold;
    letter-spacing: 2px;
}

#btn-settings-close {
    background: none;
    border: none;
    color: #ff4444;
    font-size: 10px;
    cursor: pointer;
}

.settings-body {
    display: flex;
    flex-direction: column;
    gap: 12px;
}

.setting-row {
    display: flex;
    justify-content: space-between;
    align-items: center;
    color: #eee;
    font-size: 11px;
}

.vol-slider {
    width: 60%;
    accent-color: var(--ui-color);
}

#btn-data-reset {
    background: #441111;
    color: #ff4444;
    border: 1px solid #ff4444;
    padding: 8px;
    font-size: 11px;
    font-weight: bold;
    cursor: pointer;
    border-radius: 4px;
}

#btn-data-reset:active {
    background: #ff4444;
    color: #fff;
}

#select-lang {
    background: #222;
    color: #fff;
    border: 1px solid #444;
    padding: 4px;
    font-size: 11px;
    width: 60%;
}

/* --------------------------------------------------
   カードバインダーUI
-------------------------------------------------- */
#binder-overlay {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(0, 4, 10, 0.98);
    z-index: 500;
    display: none;
    flex-direction: column;
}

#btn-binder-close {
    position: absolute;
    top: 20px;
    right: 60px; 
    z-index: 510;
    background: #333;
    color: #ff4444;
    border: 1px solid #444;
    padding: 8px 15px;
    border-radius: 4px;
    cursor: pointer;
    font-size: 10px;
    font-weight: bold;
}

.binder-header-area {
    flex: 3;
    display: flex;
    flex-direction: column;
    justify-content: center;
    padding: 40px 20px 0 20px;
    color: var(--ui-color);
    border-bottom: 1px solid rgba(0, 242, 255, 0.1);
}

.status-line {
    font-size: 12px;
    margin: 4px 0;
    letter-spacing: 1px;
}

#binder-grid {
    flex: 7;
    overflow-y: auto;
    padding: 20px;
    display: grid;
    grid-template-columns: repeat(5, 1fr);
    grid-auto-rows: min-content;
    gap: 12px;
    justify-items: center;
    align-content: start;
}

.slot {
    width: 100%;
    max-width: 65px;
    height: 97.5px; /* 追加: 65px * 1.5 (2:3比率) を固定値で指定 */
    background: #111;
    border: 1px solid #222;
    border-radius: 4px;
    position: relative;
    overflow: hidden;
    cursor: pointer;
}

.slot.has-card {
    border-color: #444;
}

.slot img {
    width: 100%;
    height: 100%;
    object-fit: cover;
}

.badge-eq {
    position: absolute;
    top: 2px;
    left: 2px;
    background: var(--ui-color);
    color: #000;
    font-size: 8px;
    font-weight: bold;
    padding: 1px 3px;
    border-radius: 2px;
}

.stage-btn {
    position: relative; /* 子要素の基準点 */
}

.clear-badge {
    position: absolute;
    right: 10px;
    top: 50%;
    transform: translateY(-50%);
    font-size: 0.7em;
    color: #ffeb3b;
    font-weight: bold;
    pointer-events: none;
}