* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

html {
    height: 100%;
    height: -webkit-fill-available;
}

body {
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
    background-color: #000;
    color: #fff;
    overflow: hidden;
    /* iOS Safariのアドレスバー対策 */
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    min-height: 100vh;
    min-height: -webkit-fill-available;
    -webkit-user-select: none;
    user-select: none;
    -webkit-touch-callout: none;
    /* スクロールバウンスを無効化 */
    overscroll-behavior: none;
}

#gameContainer {
    position: relative;
    width: 100vw;
    height: 100vh;
    height: 100dvh; /* Dynamic viewport height for mobile */
    display: flex;
    justify-content: center;
    align-items: center;
    /* セーフエリア対応 */
    padding: env(safe-area-inset-top) env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left);
}

#gameCanvas {
    position: relative;
    z-index: 1;
}

#gameCanvas {
    background-color: #87CEEB;
    border: 2px solid #333;
    image-rendering: auto;
    max-width: 100%;
    height: auto;
}

#gameUI {
    position: absolute;
    top: 0;
    left: 0;
    width: 360px;
    height: 640px;
    pointer-events: none;
    z-index: 10;
    transform-origin: top left;
}

/* バッテリーコンテナ */
#batteryContainer {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 40px;
    background: rgba(0, 0, 0, 0.8);
    border-bottom: 2px solid rgba(255, 255, 255, 0.2);
    pointer-events: none;
    box-shadow: 0 2px 10px rgba(0, 0, 0, 0.5);
}

.battery-bar-full {
    position: relative;
    width: 100%;
    height: 100%;
    background: rgba(26, 26, 26, 0.8);
    overflow: hidden;
}

.battery-fill {
    position: absolute;
    top: 0;
    left: 0;
    height: 100%;
    width: 100%;
    background: linear-gradient(90deg, 
        #ff0000 0%, 
        #ff3333 10%, 
        #ff6600 20%, 
        #ffaa00 30%, 
        #ffcc00 40%, 
        #ffff00 50%, 
        #ccff00 60%, 
        #99ff00 70%, 
        #66ff00 80%, 
        #33ff00 90%, 
        #00ff00 100%);
    transition: width 0.3s ease;
    box-shadow: 0 0 20px rgba(0, 255, 0, 0.5);
}

.battery-info {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    display: flex;
    gap: 20px;
    align-items: center;
    font-size: 20px;
    font-weight: bold;
    text-shadow: 2px 2px 4px rgba(0, 0, 0, 0.8);
    z-index: 10;
}

#batteryPercent {
    color: #fff;
    font-size: 24px;
}

.battery-drain {
    color: #ffd700;
    font-size: 18px;
    animation: pulse 1s ease-in-out infinite;
}

@keyframes pulse {
    0%, 100% { opacity: 0.8; }
    50% { opacity: 1; }
}

#topBar {
    position: absolute;
    top: 50px;
    left: 10px;
    right: 10px;
    display: flex;
    gap: 15px;
    background: rgba(0, 0, 0, 0.7);
    padding: 8px 15px;
    border-radius: 10px;
    font-size: 12px;
    justify-content: center;
    z-index: 50;
    max-width: calc(100% - 20px);
    box-sizing: border-box;
}

.status-item {
    display: flex;
    align-items: center;
    gap: 5px;
    white-space: nowrap;
}

.label {
    color: #ccc;
}

/* 古いバッテリー表示は削除 */

.screen {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    text-align: center;
    background: rgba(0, 0, 0, 0.95);
    display: flex;
    flex-direction: column;
    justify-content: flex-start;
    align-items: center;
    pointer-events: all;
    z-index: 100;
    overflow-y: auto;
    padding: 20px 10px;
    box-sizing: border-box;
}

/* タイトル画面専用スタイル */
#startScreen {
    background: none;
    padding: 0;
}

.title-canvas {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    z-index: 0;
}

.title-content {
    position: relative;
    z-index: 1;
    width: 100%;
    height: 100%;
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    background: linear-gradient(to bottom, rgba(0, 0, 0, 0.3), rgba(0, 0, 0, 0.8));
    padding: 20px;
    box-sizing: border-box;
}

.title-header {
    margin-bottom: 30px;
    animation: titleFadeIn 1s ease-out;
}

.title-main {
    font-size: 56px;
    font-weight: bold;
    color: #FFF;
    text-shadow: 3px 3px 6px rgba(0, 0, 0, 0.8), 0 0 20px rgba(76, 175, 80, 0.5);
    margin: 0;
    letter-spacing: 2px;
    animation: titleGlow 2s ease-in-out infinite alternate;
}

.title-sub {
    font-size: 28px;
    color: #4CAF50;
    margin: 5px 0;
    font-weight: 300;
    letter-spacing: 4px;
    text-transform: uppercase;
    text-shadow: 2px 2px 4px rgba(0, 0, 0, 0.8);
}

.title-version {
    font-size: 14px;
    color: #888;
    margin-top: 10px;
}

.button-container {
    margin: 20px 0;
    animation: buttonSlideUp 1s ease-out 0.5s both;
}

.button-primary {
    background: linear-gradient(45deg, #4CAF50, #66BB6A);
    font-size: 24px;
    padding: 18px 40px;
    box-shadow: 0 4px 15px rgba(76, 175, 80, 0.3);
    transform: scale(1);
    transition: all 0.3s ease;
}

.button-primary:hover {
    background: linear-gradient(45deg, #66BB6A, #81C784);
    transform: scale(1.05);
    box-shadow: 0 6px 20px rgba(76, 175, 80, 0.5);
}

.button-secondary {
    background: linear-gradient(45deg, #2196F3, #42A5F5);
    font-size: 20px;
    padding: 15px 35px;
    box-shadow: 0 4px 15px rgba(33, 150, 243, 0.3);
}

.button-secondary:hover {
    background: linear-gradient(45deg, #42A5F5, #64B5F6);
    box-shadow: 0 6px 20px rgba(33, 150, 243, 0.5);
}

@keyframes titleFadeIn {
    from {
        opacity: 0;
        transform: translateY(-30px);
    }
    to {
        opacity: 1;
        transform: translateY(0);
    }
}

@keyframes titleGlow {
    from {
        text-shadow: 3px 3px 6px rgba(0, 0, 0, 0.8), 0 0 20px rgba(76, 175, 80, 0.3);
    }
    to {
        text-shadow: 3px 3px 6px rgba(0, 0, 0, 0.8), 0 0 30px rgba(76, 175, 80, 0.8);
    }
}

@keyframes buttonSlideUp {
    from {
        opacity: 0;
        transform: translateY(20px);
    }
    to {
        opacity: 1;
        transform: translateY(0);
    }
}

.screen.hidden {
    display: none;
}

h1 {
    font-size: 48px;
    margin: 10px 0;
    color: #4CAF50;
    flex-shrink: 0;
}

h2 {
    font-size: 36px;
    margin: 10px 0 20px 0;
    color: #4CAF50;
    flex-shrink: 0;
}

h3 {
    font-size: 20px;
    margin-bottom: 10px;
    color: #fff;
}

.button {
    background: #4CAF50;
    color: white;
    border: none;
    padding: 15px 30px;
    font-size: 20px;
    border-radius: 10px;
    cursor: pointer;
    margin: 10px 5px;
    transition: background 0.3s;
}

.button:hover {
    background: #45a049;
}

.instructions {
    margin-top: 30px;
    text-align: left;
}

.instructions ul {
    list-style: none;
    padding-left: 20px;
}

.instructions li {
    margin: 5px 0;
    color: #ccc;
}

#finalScore, #failedScore {
    font-size: 24px;
    margin: 20px 0;
    color: #ffd700;
    line-height: 1.4;
    max-width: 90%;
    word-wrap: break-word;
}

#failedReason {
    font-size: 20px;
    margin: 20px 0;
    color: #ff6b6b;
}

.button-group {
    display: flex;
    gap: 20px;
    justify-content: center;
    margin: 30px 0;
    flex-wrap: wrap;
    flex-shrink: 0;
}

.button:disabled {
    background: #666;
    cursor: not-allowed;
    opacity: 0.6;
}

.button:disabled:hover {
    background: #666;
}

/* Upgrade Icon */
.upgrade-icon {
    cursor: pointer;
    background: rgba(255, 255, 255, 0.1);
    border-radius: 5px;
    padding: 5px 8px;
    transition: background 0.3s;
    pointer-events: all;
    font-size: 16px;
}

.upgrade-icon:hover {
    background: rgba(255, 255, 255, 0.2);
}

.upgrade-icon span {
    font-size: 20px;
}

/* Modal Styles */
.modal {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(0, 0, 0, 0.95);
    z-index: 1000;
    pointer-events: all;
    display: flex;
    align-items: center;
    justify-content: center;
}

.modal.hidden {
    display: none;
}

.modal-content {
    width: 100%;
    max-width: 360px;
    height: 100%;
    background: #1a1a1a;
    padding: 15px;
    overflow: hidden;
    display: flex;
    flex-direction: column;
    border-radius: 10px;
}

.modal-content h2 {
    text-align: center;
    margin: 15px 0 10px 0;
    color: #f39c12;
    font-size: 24px;
}

.reward-info {
    text-align: center;
    margin-bottom: 15px;
    padding: 12px;
    background: rgba(255, 255, 255, 0.1);
    border-radius: 10px;
}

.reward-info p {
    font-size: 18px;
    margin: 8px 0;
}

.reward-info span {
    color: #4CAF50;
    font-weight: bold;
}

/* Upgrade Cards */
.upgrade-choices {
    display: flex;
    flex-direction: column;
    gap: 10px;
    flex: 1;
    overflow-y: auto;
    padding: 8px;
    max-height: calc(100vh - 200px);
}

.upgrade-card {
    background: #2a2a2a;
    border: 2px solid #444;
    border-radius: 10px;
    padding: 12px;
    cursor: pointer;
    transition: all 0.3s;
    min-height: auto;
}

.upgrade-card:hover {
    background: #3a3a3a;
    border-color: #4CAF50;
    transform: translateY(-2px);
}

.upgrade-card.special {
    border-color: #ffd700;
    background: linear-gradient(135deg, #2a2a2a, #3a3a3a);
    position: relative;
}

.upgrade-card.special::before {
    content: "SPECIAL DEAL!";
    position: absolute;
    top: -10px;
    right: 15px;
    background: #ffd700;
    color: #000;
    padding: 3px 10px;
    border-radius: 5px;
    font-size: 12px;
    font-weight: bold;
}

.upgrade-card.disabled {
    opacity: 0.5;
    background: #1a1a1a;
    border-color: #333;
    cursor: not-allowed;
}

.upgrade-card.disabled:hover {
    background: #1a1a1a;
    border-color: #333;
    transform: none;
}

.upgrade-name {
    font-size: 16px;
    font-weight: bold;
    margin-bottom: 6px;
    color: #fff;
}

.upgrade-details {
    font-size: 13px;
    color: #ccc;
    margin-bottom: 10px;
    line-height: 1.3;
}

.upgrade-price {
    font-size: 17px;
    color: #4CAF50;
    font-weight: bold;
}

/* スキップボタン */
.skip-upgrade-btn {
    background: #d32f2f;
    color: white;
    border: none;
    border-radius: 8px;
    padding: 10px 20px;
    font-size: 16px;
    font-weight: bold;
    cursor: pointer;
    margin-bottom: 15px;
    transition: background 0.3s;
    width: 100%;
}

.skip-upgrade-btn:hover {
    background: #b71c1c;
}

/* シャッフルボタン */
.shuffle-btn {
    background: #1976d2;
    color: white;
    border: none;
    border-radius: 8px;
    padding: 10px 20px;
    font-size: 16px;
    font-weight: bold;
    cursor: pointer;
    margin-top: 15px;
    transition: background 0.3s;
    width: 100%;
}

.shuffle-btn:hover {
    background: #115293;
}

.shuffle-btn:disabled {
    background: #666;
    cursor: not-allowed;
    opacity: 0.6;
}

.upgrade-price.original {
    text-decoration: line-through;
    color: #666;
    margin-right: 10px;
}

/* Upgrade Progress List */
.upgrade-list {
    display: flex;
    flex-direction: column;
    gap: 8px;
    margin-bottom: 10px;
    flex: 1;
    overflow-y: auto;
    padding: 5px;
    max-height: calc(100vh - 150px);
}

.upgrade-item {
    background: #2a2a2a;
    padding: 10px;
    border-radius: 8px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: 10px;
    width: 100%;
}

.upgrade-item-info {
    display: flex;
    flex-direction: column;
    flex: 1;
}

.upgrade-item-name {
    font-weight: bold;
    color: #fff;
    margin-bottom: 3px;
    font-size: 14px;
}

.upgrade-item-graph {
    display: flex;
    align-items: center;
    gap: 10px;
    width: 100%;
}

.upgrade-item-bar {
    flex: 1;
    height: 10px;
    background: rgba(255, 255, 255, 0.1);
    border-radius: 5px;
    overflow: hidden;
    border: 1px solid rgba(255, 255, 255, 0.2);
    margin-right: 8px;
}

.upgrade-item-bar-fill {
    height: 100%;
    background: linear-gradient(90deg, #4CAF50, #66BB6A);
    transition: width 0.3s ease;
}

.upgrade-item-level {
    color: #4CAF50;
    font-weight: bold;
    font-size: 12px;
    min-width: 40px;
    text-align: right;
}

/* アナログスティック */
.analog-stick {
    position: fixed;
    pointer-events: none;
    z-index: 100;
}

.analog-stick.hidden {
    display: none;
}

.stick-base {
    position: relative;
    width: 150px;
    height: 150px;
    border-radius: 50%;
    background: rgba(0, 0, 0, 0.3);
    border: 3px solid rgba(255, 255, 255, 0.3);
    display: flex;
    align-items: center;
    justify-content: center;
}

.stick-knob {
    position: absolute;
    width: 60px;
    height: 60px;
    border-radius: 50%;
    background: rgba(255, 255, 255, 0.5);
    border: 2px solid rgba(255, 255, 255, 0.8);
    transition: none;
}

/* デバッグウインドウ */
.debug-window {
    position: fixed;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    width: 600px;
    max-width: 90vw;
    max-height: 80vh;
    background: rgba(0, 0, 0, 0.95);
    border: 2px solid #00ff00;
    border-radius: 8px;
    z-index: 10000;
    overflow: hidden;
    display: flex;
    flex-direction: column;
    font-family: 'Courier New', monospace;
    pointer-events: auto;
}

.debug-window.hidden {
    display: none;
}

.debug-header {
    background: #00ff00;
    color: #000;
    padding: 10px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    flex-shrink: 0;
}

.debug-header h3 {
    margin: 0;
    font-size: 16px;
}

.debug-close {
    background: transparent;
    border: none;
    color: #000;
    font-size: 24px;
    cursor: pointer;
    padding: 0;
    width: 30px;
    height: 30px;
    display: flex;
    align-items: center;
    justify-content: center;
}

.debug-close:hover {
    background: rgba(0, 0, 0, 0.2);
}

.debug-content {
    padding: 20px;
    overflow-y: auto;
    flex: 1;
}

.debug-section {
    margin-bottom: 20px;
    padding-bottom: 20px;
    border-bottom: 1px solid #00ff00;
}

.debug-section:last-child {
    border-bottom: none;
}

.debug-section h4 {
    color: #00ff00;
    margin: 0 0 15px 0;
    font-size: 14px;
}

.debug-control {
    margin-bottom: 12px;
    display: flex;
    align-items: center;
    gap: 10px;
}

.debug-control label {
    color: #00ff00;
    font-size: 12px;
    min-width: 200px;
}

.debug-control input[type="range"] {
    flex: 1;
    height: 20px;
    -webkit-appearance: none;
    appearance: none;
    background: transparent;
}

.debug-control input[type="range"]::-webkit-slider-track {
    background: #333;
    height: 4px;
    border-radius: 2px;
}

.debug-control input[type="range"]::-webkit-slider-thumb {
    -webkit-appearance: none;
    appearance: none;
    width: 16px;
    height: 16px;
    background: #00ff00;
    border-radius: 50%;
    cursor: pointer;
    margin-top: -6px;
}

.debug-control input[type="range"]::-moz-range-track {
    background: #333;
    height: 4px;
    border-radius: 2px;
}

.debug-control input[type="range"]::-moz-range-thumb {
    width: 16px;
    height: 16px;
    background: #00ff00;
    border-radius: 50%;
    border: none;
    cursor: pointer;
}

.debug-control input[type="number"] {
    background: #333;
    border: 1px solid #00ff00;
    color: #00ff00;
    padding: 4px 8px;
    font-family: 'Courier New', monospace;
    font-size: 12px;
    width: 80px;
    outline: none;
}

.debug-control input[type="number"]:focus {
    border-color: #00ff00;
    box-shadow: 0 0 5px #00ff00;
}

.debug-btn {
    background: #00ff00;
    color: #000;
    border: none;
    padding: 4px 12px;
    font-family: 'Courier New', monospace;
    font-size: 12px;
    cursor: pointer;
    border-radius: 4px;
}

.debug-btn:hover {
    background: #00dd00;
}

/* タッチデバイス判定 */
@media (pointer: coarse) {
    #gameContainer {
        height: 100vh;
        width: 100vw;
    }
    
    #gameCanvas {
        width: 100%;
        height: 100%;
        object-fit: contain;
    }
    
    /* モバイルでのアップグレード画面最適化 */
    .modal-content {
        max-height: 100dvh;
        padding: 8px;
        height: 100%;
    }
    
    .modal-content h2 {
        font-size: 18px;
        margin: 5px 0;
    }
    
    .reward-info {
        padding: 5px;
        margin-bottom: 5px;
    }
    
    .reward-info p {
        font-size: 14px;
        margin: 3px 0;
    }
    
    .upgrade-choices {
        gap: 5px;
        padding: 3px;
        max-height: calc(100dvh - 160px);
    }
    
    .upgrade-card {
        padding: 8px;
    }
    
    .upgrade-name {
        font-size: 13px;
    }
    
    .upgrade-details {
        font-size: 11px;
        margin-bottom: 5px;
    }
    
    .upgrade-price {
        font-size: 14px;
    }
    
    .upgrade-list {
        max-height: calc(100dvh - 120px);
    }
    
    /* モバイルでのスクリーン最適化 */
    .screen {
        padding: 10px 5px;
    }
    
    /* タイトル画面のモバイル最適化 */
    .title-main {
        font-size: 38px;
    }
    
    .title-sub {
        font-size: 20px;
        letter-spacing: 2px;
    }
    
    .button-primary {
        font-size: 20px;
        padding: 15px 35px;
    }
    
    .button-secondary {
        font-size: 18px;
        padding: 12px 30px;
    }
    
    h1 {
        font-size: 32px;
        margin: 5px 0;
    }
    
    h2 {
        font-size: 24px;
        margin: 5px 0 15px 0;
    }
    
    #finalScore, #failedScore {
        font-size: 18px;
        margin: 15px 0;
        max-width: 95%;
    }
    
    .button-group {
        gap: 10px;
        margin: 20px 0;
    }
    
    .button {
        padding: 12px 20px;
        font-size: 16px;
        margin: 5px 3px;
    }
}

/* チュートリアルスタイル */
.tutorial-overlay {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    z-index: 200;
    pointer-events: none;
    background: rgba(0, 0, 0, 0.1);
}

.tutorial-overlay.hidden {
    display: none;
}

.tutorial-box {
    position: absolute;
    background: rgba(0, 0, 0, 0.75);
    border: 2px solid #4CAF50;
    border-radius: 10px;
    padding: 15px;
    min-width: 280px;
    max-width: 320px;
    box-shadow: 0 4px 20px rgba(0, 255, 0, 0.3);
    pointer-events: all;
}

.tutorial-box.tutorial-top {
    top: 10px;
    right: 10px;
    transform: none;
}

.tutorial-box.tutorial-center {
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
}

.tutorial-box.tutorial-bottom {
    bottom: 10px;
    left: 10px;
    transform: none;
}

.tutorial-box.tutorial-left {
    top: 50%;
    left: 10px;
    transform: translateY(-50%);
}

.tutorial-box.tutorial-right {
    top: 50%;
    right: 10px;
    transform: translateY(-50%);
}

.tutorial-title {
    color: #4CAF50;
    font-size: 20px;
    margin-bottom: 10px;
}

.tutorial-text {
    color: #fff;
    font-size: 14px;
    line-height: 1.6;
    margin-bottom: 15px;
    white-space: pre-line;
}

.tutorial-button {
    background: #4CAF50;
    color: white;
    border: none;
    padding: 10px 20px;
    font-size: 16px;
    border-radius: 5px;
    cursor: pointer;
    margin: 5px;
}

.tutorial-button:hover {
    background: #45a049;
}

.tutorial-skip {
    background: #666;
    color: white;
    border: none;
    padding: 8px 16px;
    font-size: 14px;
    border-radius: 5px;
    cursor: pointer;
    margin: 5px;
}

.tutorial-skip:hover {
    background: #777;
}

.tutorial-highlight {
    position: absolute;
    border: 3px solid #FFD700;
    background: rgba(255, 215, 0, 0.2);
    border-radius: 10px;
    pointer-events: none;
    animation: tutorial-pulse 2s ease-in-out infinite;
}

.tutorial-highlight.hidden {
    display: none;
}

@keyframes tutorial-pulse {
    0% { 
        box-shadow: 0 0 10px rgba(255, 215, 0, 0.5);
        transform: scale(1);
    }
    50% { 
        box-shadow: 0 0 30px rgba(255, 215, 0, 0.8);
        transform: scale(1.05);
    }
    100% { 
        box-shadow: 0 0 10px rgba(255, 215, 0, 0.5);
        transform: scale(1);
    }
}

.tutorial-arrow {
    position: absolute;
    width: 0;
    height: 0;
    border-left: 20px solid transparent;
    border-right: 20px solid transparent;
    border-bottom: 30px solid #FFD700;
    pointer-events: none;
    animation: tutorial-bounce 1s ease-in-out infinite;
}

.tutorial-arrow.hidden {
    display: none;
}

@keyframes tutorial-bounce {
    0%, 100% { transform: translateY(0); }
    50% { transform: translateY(-10px); }
}

.close-btn {
    display: block;
    margin: 0 auto;
    padding: 10px 25px;
    background: #4CAF50;
    color: white;
    border: none;
    border-radius: 8px;
    cursor: pointer;
    font-size: 16px;
    transition: background 0.3s;
}

.close-btn:hover {
    background: #45a049;
}

.title-btn {
    background: #d32f2f;
    margin-left: 10px;
}

.title-btn:hover {
    background: #f44336;
}

.modal .button-group {
    display: flex;
    justify-content: center;
    gap: 10px;
    margin-top: 20px;
}