:root {
    --app-viewport-height: 100vh;
}

@supports (height: 100dvh) {
    :root {
        --app-viewport-height: 100dvh;
    }
}

@supports (height: 100svh) {
    :root {
        --app-viewport-height: 100svh;
    }
}

html {
    height: 100%;
}

/* Layout - Mobile Portrait */
body {
    background: var(--bg-primary);
    color: var(--text-primary);
    min-height: var(--app-viewport-height);
}

.game-container {
    display: flex;
    flex-direction: column;
    min-height: var(--app-viewport-height);
    width: min(100%, 500px);
    margin: 0 auto;
    padding: 16px clamp(12px, 4vw, 24px);
    padding-top: calc(16px + env(safe-area-inset-top));
    padding-bottom: calc(16px + env(safe-area-inset-bottom));
    gap: 16px;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
}

/* HUD (상단) */
.hud {
    display: flex;
    justify-content: space-around;
    align-items: center;
    background: var(--bg-secondary);
    padding: 16px;
    border-radius: var(--border-radius);
    box-shadow: 0 4px 8px rgba(0, 0, 0, 0.3);
}

.hud > div {
    display: flex;
    align-items: baseline;
    gap: 8px;
}

.hud .label {
    font-size: 14px;
    color: var(--text-secondary);
}

.hud #gold-value {
    font-size: 24px;
    font-weight: bold;
    color: var(--text-gold);
}

.hud #dps-value {
    font-size: 24px;
    font-weight: bold;
    color: #4CAF50;
}

.hud .unit {
    font-size: 14px;
    color: var(--text-secondary);
}

/* Action Buttons (골드 아래) */
.action-buttons {
    display: flex;
    gap: 12px;
    padding: 0;
}

.action-buttons-3col {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    gap: 8px;
}

.btn-merge-all {
    flex: 1;
    background: #9C27B0;
}

.btn-merge-all:hover:not(:disabled) {
    background: #7B1FA2;
    transform: translateY(-2px);
    box-shadow: 0 6px 12px rgba(0, 0, 0, 0.4);
}

.btn-merge-all:active:not(:disabled) {
    transform: translateY(0);
}

.btn-auto-merge {
    flex: 1;
    background: #00897B;
}

.btn-auto-merge.active {
    background: #4CAF50;
    box-shadow: 0 0 15px rgba(76, 175, 80, 0.6);
}

.btn-auto-merge:hover:not(:disabled) {
    background: #00695C;
    transform: translateY(-2px);
    box-shadow: 0 6px 12px rgba(0, 0, 0, 0.4);
}

.btn-auto-merge.active:hover:not(:disabled) {
    background: #388E3C;
}

.btn-auto-merge:active:not(:disabled) {
    transform: translateY(0);
}

.btn-sort {
    flex: 1;
    background: #2196F3;
}

.btn-sort:hover:not(:disabled) {
    background: #1976D2;
    transform: translateY(-2px);
    box-shadow: 0 6px 12px rgba(0, 0, 0, 0.4);
}

.btn-sort:active:not(:disabled) {
    transform: translateY(0);
}

.btn-sell {
    flex: 1;
    background: #FF7043;
}

.btn-sell:hover:not(:disabled) {
    background: #F4511E;
    transform: translateY(-2px);
    box-shadow: 0 6px 12px rgba(0, 0, 0, 0.4);
}

.btn-sell:active:not(:disabled) {
    transform: translateY(0);
}

.btn-duel {
    flex: 1;
    background: #E91E63;
}

.btn-duel:hover:not(:disabled) {
    background: #C2185B;
    transform: translateY(-2px);
    box-shadow: 0 6px 12px rgba(0, 0, 0, 0.4);
}

.btn-duel:active:not(:disabled) {
    transform: translateY(0);
}

.btn-donate {
    flex: 1;
    background: #607D8B;
}

.btn-donate:hover:not(:disabled) {
    background: #455A64;
    transform: translateY(-2px);
    box-shadow: 0 6px 12px rgba(0, 0, 0, 0.4);
}

.btn-donate:active:not(:disabled) {
    transform: translateY(0);
}

/* Grid Container (중앙) */
.grid-container {
    flex: 1;
    display: flex;
    align-items: center;
    justify-content: center;
    min-height: 0;
}

.grid {
    display: grid;
    grid-template-columns: repeat(4, var(--tile-size));
    grid-template-rows: repeat(4, var(--tile-size));
    gap: var(--grid-gap);
    padding: 8px;
    background: var(--bg-secondary);
    border-radius: var(--border-radius);
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.4);
    touch-action: none;
}

/* Button Container (하단) */
.button-container {
    display: flex;
    gap: 12px;
    padding: 0;
}

.button-container-3col {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    gap: 8px;
}

.btn {
    flex: 1;
    display: flex;
    flex-direction: column;
    align-items: center;
    padding: 16px;
    border-radius: var(--border-radius);
    font-size: 16px;
    font-weight: bold;
    color: white;
    transition: all var(--transition-fast);
    box-shadow: 0 4px 8px rgba(0, 0, 0, 0.3);
    touch-action: manipulation;
}

.btn-label {
    font-size: 18px;
    margin-bottom: 4px;
}

.btn-cost {
    font-size: 14px;
    opacity: 0.9;
}

.btn-buy {
    background: var(--btn-buy);
}

.btn-buy:hover:not(:disabled) {
    background: var(--btn-buy-hover);
    transform: translateY(-2px);
    box-shadow: 0 6px 12px rgba(0, 0, 0, 0.4);
}

.btn-buy:active:not(:disabled) {
    transform: translateY(0);
}

.btn-auto-buy {
    flex: 1;
    background: #FF9800;
}

.btn-auto-buy.active {
    background: #FFC107;
    box-shadow: 0 0 15px rgba(255, 193, 7, 0.6);
}

.btn-auto-buy:hover:not(:disabled) {
    background: #F57C00;
    transform: translateY(-2px);
    box-shadow: 0 6px 12px rgba(0, 0, 0, 0.4);
}

.btn-auto-buy.active:hover:not(:disabled) {
    background: #FFA000;
}

.btn-auto-buy:active:not(:disabled) {
    transform: translateY(0);
}

.btn-upgrade {
    background: var(--btn-upgrade);
}

.btn-upgrade:hover:not(:disabled) {
    background: var(--btn-upgrade-hover);
    transform: translateY(-2px);
    box-shadow: 0 6px 12px rgba(0, 0, 0, 0.4);
}

.btn-upgrade:active:not(:disabled) {
    transform: translateY(0);
}

/* Message Container */
.message-container {
    position: fixed;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    pointer-events: none;
    z-index: 1000;
}

/* Responsive */
@media (max-height: 700px) {
    .game-container {
        padding: 12px;
        gap: 12px;
    }

    .hud {
        padding: 12px;
    }

    .btn {
        padding: 12px;
    }
}
