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

body {
    font-family: 'Courier New', monospace;
    background: radial-gradient(circle at center, #0a0a1a 0%, #000008 100%);
    color: #f0f0f0;
    overflow: hidden;
    height: 100vh;
    display: flex;
    justify-content: center;
    align-items: center;
}

#game-wrapper {
    width: 100vw;
    height: calc(100vw * 16 / 9);
    max-height: 100vh;
    max-width: calc(100vh * 9 / 16);
    background: linear-gradient(135deg, #2a1a0a 0%, #1a2a1a 25%, #0a1a2a 50%, #1a0a2a 75%, #2a0a1a 100%);
    position: relative;
    overflow: hidden;
    display: flex;
    flex-direction: column;
    border: 3px solid #6bff73;
    box-shadow: 
        0 0 30px rgba(107, 255, 115, 0.4),
        inset 0 0 50px rgba(107, 255, 115, 0.1);
    border-radius: 8px;
}

#gameContainer {
    flex: 3;
    position: relative;
    background: 
        radial-gradient(circle at 20% 80%, rgba(107, 255, 115, 0.1) 0%, transparent 50%),
        radial-gradient(circle at 80% 20%, rgba(255, 170, 0, 0.1) 0%, transparent 50%),
        radial-gradient(circle at center, #1a2a1a 0%, #0a0a0a 100%);
}

#gameCanvas {
    width: 100%;
    height: 100%;
    display: block;
    filter: brightness(1.1) contrast(1.05);
}

#gameUI {
    position: absolute;
    top: 12px;
    left: 12px;
    right: 12px;
    display: flex;
    justify-content: space-between;
    z-index: 100;
    flex-wrap: wrap;
    gap: 12px;
}

.ui-panel {
    background: 
        linear-gradient(135deg, rgba(26, 42, 26, 0.95) 0%, rgba(16, 32, 16, 0.95) 100%);
    border: 2px solid #6bff73;
    border-radius: 12px;
    padding: 10px 16px;
    backdrop-filter: blur(8px);
    font-size: clamp(0.8rem, 2.5vw, 1.1rem);
    font-weight: 600;
    text-shadow: 0 1px 2px rgba(0, 0, 0, 0.8);
    box-shadow: 
        0 4px 15px rgba(0, 0, 0, 0.4),
        inset 0 1px 0 rgba(107, 255, 115, 0.2);
}

#keypadArea {
    flex: 1;
    background: 
        linear-gradient(180deg, rgba(26, 42, 30, 0.95) 0%, rgba(10, 10, 10, 0.98) 100%);
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    grid-template-rows: repeat(3, 1fr);
    gap: 8px;
    padding: 12px;
    border-top: 3px solid #6bff73;
    box-shadow: inset 0 3px 10px rgba(0, 0, 0, 0.6);
}

.keypad-btn {
    background: 
        linear-gradient(135deg, rgba(107, 255, 115, 0.15) 0%, rgba(78, 221, 157, 0.15) 50%, rgba(50, 180, 120, 0.15) 100%);
    border: 3px solid #6bff73;
    border-radius: 12px;
    color: #f0f0f0;
    font-size: clamp(1.8rem, 8vw, 3.2rem);
    font-family: 'Courier New', monospace;
    font-weight: bold;
    cursor: pointer;
    transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
    display: flex;
    align-items: center;
    justify-content: center;
    user-select: none;
    text-shadow: 0 2px 4px rgba(0, 0, 0, 0.6);
    box-shadow: 
        0 4px 15px rgba(0, 0, 0, 0.3),
        inset 0 1px 0 rgba(255, 255, 255, 0.1);
    position: relative;
    overflow: hidden;
}

.keypad-btn::before {
    content: '';
    position: absolute;
    top: 0;
    left: -100%;
    width: 100%;
    height: 100%;
    background: linear-gradient(90deg, transparent, rgba(255, 255, 255, 0.2), transparent);
    transition: left 0.5s;
}

.keypad-btn:hover {
    background: 
        linear-gradient(135deg, rgba(107, 255, 115, 0.25) 0%, rgba(78, 221, 157, 0.25) 50%, rgba(50, 180, 120, 0.25) 100%);
    transform: translateY(-3px) scale(1.02);
    box-shadow: 
        0 8px 25px rgba(107, 255, 115, 0.3),
        inset 0 1px 0 rgba(255, 255, 255, 0.2);
    border-color: #7dff8a;
}

.keypad-btn:hover::before {
    left: 100%;
}

.keypad-btn:active {
    transform: translateY(-1px) scale(0.98);
}

.keypad-btn.valid { 
    border-color: #00ff7f; 
    animation: validPulse 2s ease-in-out infinite;
    box-shadow: 
        0 4px 15px rgba(0, 255, 127, 0.4),
        inset 0 1px 0 rgba(255, 255, 255, 0.1);
}

.keypad-btn.settlement { 
    border-color: #ffaa00; 
    color: #ffcc44;
    background: 
        linear-gradient(135deg, rgba(255, 170, 0, 0.2) 0%, rgba(255, 204, 68, 0.2) 50%, rgba(255, 140, 0, 0.2) 100%);
}

.keypad-btn.event { 
    border-color: #aa00ff; 
    color: #cc44ff;
    background: 
        linear-gradient(135deg, rgba(170, 0, 255, 0.2) 0%, rgba(204, 68, 255, 0.2) 50%, rgba(136, 0, 255, 0.2) 100%);
}

.modal-screen {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: 
        radial-gradient(circle at center, rgba(10, 10, 10, 0.97) 0%, rgba(5, 5, 15, 0.99) 100%);
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    z-index: 200;
    text-align: center;
    padding: 25px;
    transition: all 0.4s ease;
    backdrop-filter: blur(5px);
}

.modal-title {
    font-size: clamp(1.5rem, 2vw, 0.5rem);
    font-weight: 900;
    margin-bottom: 3px;
    text-shadow: 
        0 0 30px #6bff73,
        0 0 60px #6bff73,
        2px 2px 4px rgba(0, 0, 0, 0.8);
    background: linear-gradient(45deg, #ffffff 0%, #6bff73 25%, #4edd9d 50%, #6bff73 75%, #ffffff 100%);
    background-size: 200% 200%;
    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;
    animation: titleShimmer 3s ease-in-out infinite;
    letter-spacing: 2px;
}

@keyframes titleShimmer {
    0%, 100% { background-position: 0% 50%; }
    50% { background-position: 100% 50%; }
}

.modal-text {
    font-size: clamp(1rem, 2.5vw, 1 rem);
    margin-bottom: 15px;
    line-height: 1.7;
    max-width: 85%;
    text-shadow: 1px 1px 2px rgba(0, 0, 0, 0.7);
    font-weight: 500;
}

.modal-button {
    background: 
        linear-gradient(135deg, rgba(107, 255, 115, 0.25) 0%, rgba(78, 221, 157, 0.25) 50%, rgba(50, 180, 120, 0.25) 100%);
    border: 3px solid #6bff73;
    border-radius: 15px;
    color: #f0f0f0;
    font-family: 'Courier New', monospace;
    font-size: clamp(1.1rem, 4vw, 1.4rem);
    font-weight: bold;
    padding: 18px 35px;
    margin: 12px;
    cursor: pointer;
    transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
    text-shadow: 0 2px 4px rgba(0, 0, 0, 0.6);
    box-shadow: 
        0 6px 20px rgba(0, 0, 0, 0.3),
        inset 0 1px 0 rgba(255, 255, 255, 0.1);
    position: relative;
    overflow: hidden;
}

.modal-button::before {
    content: '';
    position: absolute;
    top: 0;
    left: -100%;
    width: 100%;
    height: 100%;
    background: linear-gradient(90deg, transparent, rgba(255, 255, 255, 0.2), transparent);
    transition: left 0.5s;
}

.modal-button:hover { 
    background: 
        linear-gradient(135deg, rgba(107, 255, 115, 0.35) 0%, rgba(78, 221, 157, 0.35) 50%, rgba(50, 180, 120, 0.35) 100%);
    transform: translateY(-3px) scale(1.02);
    box-shadow: 
        0 10px 30px rgba(107, 255, 115, 0.3),
        inset 0 1px 0 rgba(255, 255, 255, 0.2);
    border-color: #7dff8a;
}

.modal-button:hover::before {
    left: 100%;
}

.modal-button:disabled { 
    border-color: #555; 
    color: #777; 
    background: 
        linear-gradient(135deg, rgba(50, 50, 50, 0.3) 0%, rgba(30, 30, 30, 0.3) 100%);
    cursor: not-allowed;
    transform: none;
    box-shadow: none;
}

.modal-button:disabled::before {
    display: none;
}

.narrative-content { 
    display: flex; 
    flex-direction: column; 
    justify-content: center; 
    align-items: center; 
    gap: 25px; 
    max-width: 90%; 
}

.narrative-choices { 
    display: flex; 
    flex-direction: column; 
    width: 100%; 
    gap: 8px;
}

.choice-btn { 
    width: 100%; 
    margin: 6px 0; 
    padding: 16px 25px; 
    font-size: clamp(1rem, 3.5vw, 1.2rem); 
}

@keyframes validPulse { 
    0%, 100% { 
        box-shadow: 
            0 4px 15px rgba(0, 255, 127, 0.4),
            inset 0 1px 0 rgba(255, 255, 255, 0.1);
    } 
    50% { 
        box-shadow: 
            0 8px 30px rgba(0, 255, 127, 0.6),
            inset 0 1px 0 rgba(255, 255, 255, 0.2);
    } 
}

.floating-text { 
    position: absolute; 
    font-weight: bold; 
    font-size: clamp(1.1rem, 3vw, 1.3rem); 
    pointer-events: none; 
    z-index: 150; 
    animation: floatUp 1.8s ease-out forwards;
    text-shadow: 
        0 0 10px currentColor,
        2px 2px 4px rgba(0, 0, 0, 0.8);
}

@keyframes floatUp { 
    0% { 
        opacity: 1; 
        transform: translateY(0) scale(1); 
    } 
    100% { 
        opacity: 0; 
        transform: translateY(-80px) scale(1.1); 
    } 
}

/* --- COMPACT Town Screen Styles --- */
#townScreen {
    background: 
        linear-gradient(180deg, rgba(26, 42, 26, 0.95) 0%, rgba(10, 10, 10, 0.98) 100%);
    overflow-y: visible;
    justify-content: flex-start;
    padding: 8px;
    backdrop-filter: blur(8px);
}

.town-content {
    width: 98%;
    max-width: 98%;
    display: flex;
    flex-direction: column;
    gap: 6px;
}

.town-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    flex-wrap: wrap;
    gap: 2px;
    margin-bottom: 1px;
    padding-bottom: 1px;
    border-bottom: 1px solid rgba(107, 255, 115, 0.3);
}

.town-ui-panel {
    background: 
        linear-gradient(135deg, rgba(26, 42, 26, 0.95) 0%, rgba(16, 32, 16, 0.95) 100%);
    border: 1px solid #6bff73;
    border-radius: 6px;
    padding: 4px 8px;
    font-size: clamp(0.7rem, 2.5vw, 0.9rem);
    font-weight: 600;
    white-space: nowrap;
    text-shadow: 0 1px 2px rgba(0, 0, 0, 0.8);
    box-shadow: 
        0 2px 8px rgba(0, 0, 0, 0.3),
        inset 0 1px 0 rgba(107, 255, 115, 0.2);
}

.trade-section {
    background: 
        linear-gradient(135deg, rgba(42, 42, 26, 0.9) 0%, rgba(32, 32, 20, 0.9) 100%);
    border: 1px solid #6bff73;
    border-radius: 8px;
    padding: 6px 8px;
    text-align: left;
    box-shadow: 
        0 3px 10px rgba(0, 0, 0, 0.3),
        inset 0 1px 0 rgba(107, 255, 115, 0.1);
    backdrop-filter: blur(5px);
}

.trade-section h3 {
    margin-bottom: 6px;
    font-size: clamp(0.8rem, 3vw, 1rem);
    font-weight: 700;
    color: #6bff73;
    text-shadow: 0 0 10px rgba(107, 255, 115, 0.5);
    border-bottom: 1px solid rgba(107, 255, 115, 0.3);
    padding-bottom: 3px;
}

.market-section {
    max-height: none;
    overflow-y: visible;
}

.inventory-section, .rumor-section {
    max-height: none;
    overflow-y: visible;
}

.rumor-section {
    text-align: center;
    padding: 3px;
}

#rumorBtn {
    background: 
        linear-gradient(135deg, rgba(107, 255, 115, 0.25) 0%, rgba(78, 221, 157, 0.25) 50%, rgba(50, 180, 120, 0.25) 100%);
    border: 1px solid #6bff73;
    border-radius: 6px;
    color: #f0f0f0;
    font-family: 'Courier New', monospace;
    font-size: clamp(0.6rem, 2vw, 0.75rem);
    font-weight: bold;
    padding: 4px 8px;
    cursor: pointer;
    transition: all 0.3s ease;
    text-transform: uppercase;
    letter-spacing: 0.3px;
    box-shadow: 0 1px 4px rgba(0, 0, 0, 0.3);
}

#rumorBtn:hover {
    background: 
        linear-gradient(135deg, rgba(107, 255, 115, 0.35) 0%, rgba(78, 221, 157, 0.35) 50%, rgba(50, 180, 120, 0.35) 100%);
    transform: translateY(-1px);
    border-color: #7dff8a;
}

.town-actions {
    display: flex;
    justify-content: center;
    gap: 8px;
    padding-top: 4px;
    border-top: 1px solid rgba(107, 255, 115, 0.3);
}

#exitTownBtn {
    background: 
        linear-gradient(135deg, rgba(255, 107, 115, 0.25) 0%, rgba(255, 138, 144, 0.25) 100%);
    border: 1px solid #ff6b73;
    border-radius: 6px;
    color: #f0f0f0;
    font-family: 'Courier New', monospace;
    font-size: clamp(0.6rem, 2vw, 0.75rem);
    font-weight: bold;
    padding: 4px 12px;
    cursor: pointer;
    transition: all 0.3s ease;
    text-transform: uppercase;
    letter-spacing: 0.3px;
    box-shadow: 0 1px 4px rgba(0, 0, 0, 0.3);
}

#exitTownBtn:hover {
    background: 
        linear-gradient(135deg, rgba(255, 107, 115, 0.35) 0%, rgba(255, 138, 144, 0.35) 100%);
    transform: translateY(-1px);
    border-color: #ff8a90;
}

#rumorInfo {
    margin-top: 3px;
    color: #ffcc44;
    font-weight: 600;
    text-shadow: 0 1px 2px rgba(0, 0, 0, 0.7);
    padding: 3px;
    background: rgba(255, 170, 0, 0.1);
    border-radius: 4px;
    border: 1px solid rgba(255, 170, 0, 0.3);
    font-size: clamp(0.6rem, 2.2vw, 0.8rem);
    line-height: 1.2;
}

/* --- COMPACT Trade Item Layout --- */
.trade-item { 
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 3px 0;
    border-bottom: 1px solid rgba(68, 68, 68, 0.4);
    gap: 8px;
    min-height: 32px;
}

.trade-item:last-child { 
    border-bottom: none; 
}

.item-info { 
    display: flex; 
    gap: 6px; 
    align-items: center;
    font-size: clamp(0.7rem, 2.5vw, 0.85rem);
    flex: 1;
}

.item-info span:first-child {
    font-size: clamp(0.9rem, 2.8vw, 1.1rem);
    width: 16px;
    text-align: center;
}

.item-actions {
    display: flex;
    align-items: center;
    gap: 4px;
    flex-shrink: 0;
}

.price { 
    color: #6bff73; 
    font-weight: bold;
    font-size: clamp(0.7rem, 2.5vw, 0.85rem);
    text-shadow: 0 1px 2px rgba(0, 0, 0, 0.6);
    min-width: 35px;
    text-align: right;
}

.desperate { 
    color: #ff6b73; 
    animation: blink 1.5s infinite;
    text-shadow: 0 0 8px rgba(255, 107, 115, 0.6);
}

@keyframes blink { 
    0%, 50% { opacity: 1; } 
    51%, 100% { opacity: 0.6; } 
}

.inventory-grid { 
    display: grid; 
    grid-template-columns: repeat(auto-fill, minmax(80px, 1fr)); 
    gap: 4px; 
    margin-top: 4px; 
}

.inventory-item { 
    background: 
        linear-gradient(135deg, rgba(26, 42, 26, 0.8) 0%, rgba(16, 32, 16, 0.8) 100%);
    border: 1px solid rgba(107, 255, 115, 0.6);
    border-radius: 6px;
    padding: 6px;
    text-align: center;
    font-size: 0.7rem;
    transition: all 0.3s ease;
    box-shadow: 
        0 2px 6px rgba(0, 0, 0, 0.3),
        inset 0 1px 0 rgba(107, 255, 115, 0.1);
}

.inventory-item:hover {
    transform: translateY(-1px);
    border-color: #6bff73;
    box-shadow: 
        0 3px 9px rgba(107, 255, 115, 0.2),
        inset 0 1px 0 rgba(107, 255, 115, 0.2);
}

.inventory-item div:first-child {
    font-size: 1rem;
    margin-bottom: 2px;
}

.inventory-item div:last-child {
    color: #ffcc44;
    font-weight: bold;
    text-shadow: 0 1px 2px rgba(0, 0, 0, 0.6);
}

.trade-btn { 
    border: none; 
    border-radius: 4px; 
    color: #000; 
    font-family: 'Courier New', monospace; 
    font-size: clamp(0.55rem, 1.8vw, 0.65rem);
    font-weight: 600;
    padding: 2px 4px; 
    cursor: pointer; 
    transition: all 0.3s ease;
    text-transform: uppercase;
    letter-spacing: 0.2px;
    box-shadow: 0 1px 3px rgba(0, 0, 0, 0.3);
    min-width: 35px;
}

/* Hide sell buttons when player has no items - this targets buttons that contain "(0)" */
.sell-btn:not([style*="display: none"]) {
    display: inline-block;
}

/* Alternative approach - if the button text contains "(0)", make it transparent */
.trade-btn[data-amount="0"] {
    opacity: 0;
    pointer-events: none;
}

/* Style for when inventory is empty - hide all sell buttons with 0 quantity */
.trade-item .sell-btn:contains("(0)") {
    display: none;
}

.sell-btn { 
    background: linear-gradient(135deg, #ff6b73 0%, #ff8a90 100%);
}

.sell-btn:hover { 
    background: linear-gradient(135deg, #ff8a90 0%, #ffa8a8 100%);
    transform: translateY(-1px);
    box-shadow: 0 4px 12px rgba(255, 107, 115, 0.4);
}

.buy-btn { 
    background: linear-gradient(135deg, #4edd9d 0%, #6bff73 100%);
}

.buy-btn:hover { 
    background: linear-gradient(135deg, #6bff73 0%, #7dff8a 100%);
    transform: translateY(-1px);
    box-shadow: 0 4px 12px rgba(107, 255, 115, 0.4);
}

#winScreen, #townScreen, #gameOverScreen, #narrativeScreen { 
    display: none; 
}

/* --- Enhanced Mobile Responsiveness --- */
@media (max-width: 768px) {
    .town-content {
        padding: 15px;
    }
    
    .trade-item {
        grid-template-columns: 1fr;
        grid-template-areas: 
            "info"
            "actions";
        gap: 10px;
        text-align: center;
    }
    
    .item-actions {
        justify-self: center;
        min-width: auto;
    }
    
    .inventory-grid {
        grid-template-columns: repeat(auto-fill, minmax(100px, 1fr));
    }
}

/* --- Performance Optimizations --- */
.keypad-btn,
.modal-button,
.trade-btn,
.inventory-item {
    will-change: transform;
}

@media (prefers-reduced-motion: reduce) {
    * {
        animation-duration: 0.01ms !important;
        animation-iteration-count: 1 !important;
        transition-duration: 0.01ms !important;
    }
}
