/* Mobile-First RPG Styles */
* {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
    -webkit-touch-callout: none;
    -webkit-user-select: none;
    -khtml-user-select: none;
    -moz-user-select: none;
    -ms-user-select: none;
    user-select: none;
}

html, body {
    height: 100%;
    overflow: hidden;
    font-family: 'Georgia', serif;
    background: linear-gradient(135deg, #1a1a2e, #16213e, #0f4c75);
    color: #f3f4f6;
}

/* Screen Management */
#game-container {
    position: relative;
    width: 100vw;
    height: 100vh;
    overflow: hidden;
}

.screen {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    display: none;
}

.screen.active {
    display: flex;
}

/* Loading Screen */
#loading-screen {
    background: linear-gradient(135deg, #2d1b69, #1a1a2e);
    justify-content: center;
    align-items: center;
    flex-direction: column;
}

.loading-content {
    text-align: center;
    max-width: 300px;
    width: 90%;
}

.game-title {
    font-size: clamp(2rem, 8vw, 4rem);
    color: #d4af37;
    text-shadow: 2px 2px 4px rgba(0,0,0,0.8);
    margin-bottom: 10px;
    font-weight: bold;
}

.subtitle {
    font-size: clamp(1rem, 4vw, 1.5rem);
    color: #f3f4f6;
    margin-bottom: 30px;
    opacity: 0.9;
}

.loading-bar {
    width: 100%;
    height: 8px;
    background: rgba(255,255,255,0.2);
    border-radius: 4px;
    overflow: hidden;
    margin-bottom: 20px;
}

.loading-fill {
    height: 100%;
    background: linear-gradient(90deg, #d4af37, #ffd700);
    width: 0%;
    transition: width 0.3s ease;
}

.loading-text {
    font-size: 1rem;
    color: #ccc;
}

/* Main Menu */
#main-menu {
    background: linear-gradient(rgba(0,0,0,0.7), rgba(0,0,0,0.7));
    justify-content: center;
    align-items: center;
}

.menu-background {
    background: rgba(0, 0, 0, 0.9);
    border: 3px solid #d4af37;
    border-radius: 15px;
    padding: clamp(20px, 5vw, 40px);
    text-align: center;
    box-shadow: 0 0 30px rgba(212, 175, 55, 0.3);
    max-width: 400px;
    width: 90%;
}

.menu-buttons {
    display: flex;
    flex-direction: column;
    gap: 15px;
    margin-top: 30px;
}

.menu-btn {
    background: linear-gradient(45deg, #8b4513, #d4af37);
    color: #fff;
    border: none;
    padding: 15px 25px;
    font-size: clamp(1rem, 3vw, 1.2rem);
    font-family: inherit;
    border-radius: 8px;
    cursor: pointer;
    transition: all 0.3s ease;
    text-shadow: 1px 1px 2px #000;
    min-height: 50px;
    touch-action: manipulation;
}

.menu-btn:hover, .menu-btn:active {
    background: linear-gradient(45deg, #d4af37, #ffd700);
    transform: translateY(-2px);
    box-shadow: 0 4px 12px rgba(0,0,0,0.4);
}

.menu-btn.secondary {
    background: linear-gradient(45deg, #4b5563, #6b7280);
}

.menu-btn.secondary:hover {
    background: linear-gradient(45deg, #6b7280, #9ca3af);
}

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

.small-btn {
    background: #6b7280;
    color: white;
    border: none;
    padding: 8px 12px;
    border-radius: 4px;
    font-size: 0.9rem;
    cursor: pointer;
    margin-left: 10px;
}

/* World Seed Screen */
#world-seed-screen {
    justify-content: center;
    align-items: center;
    background: rgba(0,0,0,0.8);
}

.creation-panel {
    background: rgba(0, 0, 0, 0.95);
    border: 3px solid #d4af37;
    border-radius: 15px;
    padding: clamp(20px, 4vw, 30px);
    text-align: center;
    max-width: 500px;
    width: 90%;
    max-height: 90vh;
    overflow-y: auto;
}

.creation-panel h2 {
    color: #d4af37;
    font-size: clamp(1.5rem, 5vw, 2rem);
    margin-bottom: 20px;
}

.seed-input-group {
    margin: 20px 0;
    text-align: left;
}

.seed-input-group label {
    display: block;
    margin-bottom: 10px;
    color: #f3f4f6;
}

.seed-input-group input {
    width: calc(100% - 80px);
    padding: 10px;
    border: 2px solid #666;
    border-radius: 4px;
    background: #333;
    color: white;
    font-size: 1rem;
}

.algorithm-preview {
    margin: 25px 0;
    text-align: left;
}

.algorithm-preview h3 {
    color: #d4af37;
    margin-bottom: 15px;
}

.algorithm-item {
    background: rgba(255,255,255,0.1);
    padding: 10px;
    margin-bottom: 8px;
    border-radius: 5px;
    border-left: 3px solid #d4af37;
}

/* Character Creation */
#character-creation {
    justify-content: center;
    align-items: center;
    background: rgba(0,0,0,0.8);
}

.race-selection {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(140px, 1fr));
    gap: 15px;
    margin: 25px 0;
}

.race-option {
    background: rgba(255, 255, 255, 0.1);
    border: 2px solid #666;
    border-radius: 10px;
    padding: 15px;
    cursor: pointer;
    transition: all 0.3s ease;
    touch-action: manipulation;
}

.race-option:hover, .race-option:active {
    border-color: #d4af37;
    background: rgba(212, 175, 55, 0.2);
}

.race-option.selected {
    border-color: #d4af37;
    background: rgba(212, 175, 55, 0.3);
    box-shadow: 0 0 15px rgba(212, 175, 55, 0.5);
}

.race-option img {
    width: 64px;
    height: 64px;
    margin-bottom: 10px;
    border-radius: 5px;
}

.race-option h3 {
    color: #d4af37;
    margin-bottom: 5px;
    font-size: 1.1rem;
}

.race-option p {
    font-size: 0.85rem;
    color: #ccc;
    line-height: 1.3;
}

/* Game Screen */
#game-screen {
    background: #000;
}

#canvas {
    display: block;
    width: 100%;
    height: 100%;
    image-rendering: pixelated;
    image-rendering: -moz-crisp-edges;
    image-rendering: crisp-edges;
}

/* Mobile UI Overlay */
#mobile-ui {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    pointer-events: none;
    z-index: 10;
}

.ui-panel, .control-area {
    pointer-events: auto;
}

.ui-panel {
    position: absolute;
    background: rgba(0, 0, 0, 0.8);
    border: 2px solid #d4af37;
    border-radius: 8px;
    backdrop-filter: blur(5px);
}

/* Character Panel (Top-Left) */
.top-left {
    top: 10px;
    left: 10px;
}

#character-panel {
    padding: 10px;
    display: flex;
    align-items: center;
    gap: 10px;
    min-width: 200px;
}

#character-portrait {
    width: 48px;
    height: 48px;
    border-radius: 6px;
    overflow: hidden;
    flex-shrink: 0;
}

#character-portrait img {
    width: 100%;
    height: 100%;
    object-fit: cover;
}

#stats-container {
    flex: 1;
}

.stat-bar {
    display: flex;
    align-items: center;
    margin-bottom: 5px;
    gap: 8px;
}

.bar-bg {
    flex: 1;
    height: 12px;
    background: #333;
    border: 1px solid #666;
    border-radius: 6px;
    overflow: hidden;
}

.bar-fill {
    height: 100%;
    transition: width 0.3s ease;
    border-radius: 5px;
}

.health-bar {
    background: linear-gradient(90deg, #dc2626, #ef4444);
}

.mana-bar {
    background: linear-gradient(90deg, #2563eb, #3b82f6);
}

.stat-text {
    color: #fff;
    font-size: 0.8rem;
    font-weight: bold;
    min-width: 24px;
}

.level-display {
    color: #d4af37;
    font-weight: bold;
    font-size: 0.9rem;
    margin-top: 2px;
}

/* Quest Panel (Top-Right) */
.top-right {
    top: 10px;
    right: 10px;
}

#quest-panel {
    padding: 0;
    min-width: 180px;
    max-width: 250px;
}

.panel-header {
    padding: 8px 12px;
    background: rgba(212, 175, 55, 0.2);
    border-bottom: 1px solid #d4af37;
    display: flex;
    justify-content: space-between;
    align-items: center;
    cursor: pointer;
}

.expand-btn {
    background: none;
    border: none;
    color: #d4af37;
    font-size: 0.8rem;
    cursor: pointer;
    transition: transform 0.3s;
}

#quest-panel.collapsed .expand-btn {
    transform: rotate(-90deg);
}

.panel-content {
    padding: 10px;
    max-height: 150px;
    overflow-y: auto;
}

#quest-panel.collapsed .panel-content {
    display: none;
}

.quest-item {
    background: rgba(255, 255, 255, 0.1);
    padding: 8px;
    border-radius: 4px;
    margin-bottom: 6px;
    font-size: 0.8rem;
    position: relative;
}

.quest-item.active {
    border-left: 3px solid #d4af37;
}

/* Control Areas */
.control-area {
    position: absolute;
}

.bottom-left {
    bottom: 20px;
    left: 20px;
}

.bottom-right {
    bottom: 20px;
    right: 20px;
}

.bottom-center {
    bottom: 20px;
    left: 50%;
    transform: translateX(-50%);
}

.top-center {
    top: 15px;
    left: 50%;
    transform: translateX(-50%);
    display: flex;
    gap: 10px;
}

/* Virtual D-Pad */
.dpad {
    width: 120px;
    height: 120px;
    position: relative;
}

.dpad-button {
    position: absolute;
    width: 40px;
    height: 40px;
    background: rgba(255, 255, 255, 0.2);
    border: 2px solid rgba(255, 255, 255, 0.4);
    border-radius: 6px;
    display: flex;
    align-items: center;
    justify-content: center;
    color: white;
    font-size: 1.2rem;
    cursor: pointer;
    touch-action: manipulation;
    transition: all 0.1s;
}

.dpad-up {
    top: 0;
    left: 40px;
}

.dpad-down {
    bottom: 0;
    left: 40px;
}

.dpad-left {
    top: 40px;
    left: 0;
}

.dpad-right {
    top: 40px;
    right: 0;
}

.dpad-center {
    position: absolute;
    top: 40px;
    left: 40px;
    right: 40px;
    height: 40px;
    display: flex;
    align-items: center;
}

.dpad-inner {
    width: 40px;
    height: 40px;
    background: rgba(0, 0, 0, 0.5);
    border-radius: 50%;
    margin: 0 auto;
}

.dpad-button:active, .dpad-button.pressed {
    background: rgba(212, 175, 55, 0.6);
    border-color: #d4af37;
    transform: scale(0.95);
}

/* Action Buttons */
#action-buttons {
    display: flex;
    flex-direction: column;
    gap: 10px;
}

.action-btn {
    width: 60px;
    height: 60px;
    border: none;
    border-radius: 50%;
    font-size: 1.5rem;
    cursor: pointer;
    touch-action: manipulation;
    transition: all 0.1s;
    display: flex;
    align-items: center;
    justify-content: center;
}

.action-btn.primary {
    background: linear-gradient(45deg, #059669, #10b981);
    color: white;
}

.action-btn.secondary {
    background: linear-gradient(45deg, #dc2626, #ef4444);
    color: white;
}

.action-btn.tertiary {
    background: linear-gradient(45deg, #4f46e5, #6366f1);
    color: white;
}

.action-btn:active, .action-btn.pressed {
    transform: scale(0.9);
    box-shadow: inset 0 2px 4px rgba(0,0,0,0.3);
}

/* Quick Inventory */
#quick-inventory {
    display: flex;
    gap: 5px;
}

.quick-slot {
    width: 40px;
    height: 40px;
    background: rgba(0, 0, 0, 0.7);
    border: 2px solid rgba(255, 255, 255, 0.3);
    border-radius: 6px;
    display: flex;
    align-items: center;
    justify-content: center;
    color: white;
    font-size: 0.8rem;
    cursor: pointer;
    position: relative;
    touch-action: manipulation;
}

.quick-slot:hover, .quick-slot:active {
    border-color: #d4af37;
}

/* Map Toggle Buttons */
.ui-btn {
    background: rgba(0, 0, 0, 0.8);
    border: 2px solid rgba(255, 255, 255, 0.3);
    border-radius: 8px;
    color: white;
    font-size: 1.2rem;
    width: 44px;
    height: 44px;
    cursor: pointer;
    touch-action: manipulation;
}

.ui-btn:hover, .ui-btn:active {
    border-color: #d4af37;
    background: rgba(212, 175, 55, 0.3);
}

/* Touch Overlay */
#touch-overlay {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    z-index: 5;
    touch-action: pan-x pan-y;
}

/* Overlay Panels */
.overlay-panel {
    position: absolute;
    background: rgba(0, 0, 0, 0.95);
    border: 3px solid #d4af37;
    border-radius: 10px;
    z-index: 20;
    max-width: 90vw;
    max-height: 90vh;
    overflow: hidden;
    display: flex;
    flex-direction: column;
}

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

.overlay-panel .panel-header {
    background: rgba(212, 175, 55, 0.2);
    padding: 15px;
    border-bottom: 1px solid #d4af37;
    display: flex;
    justify-content: space-between;
    align-items: center;
}

.overlay-panel .panel-header h3 {
    color: #d4af37;
    margin: 0;
}

.close-btn {
    background: none;
    border: none;
    color: #d4af37;
    font-size: 1.5rem;
    cursor: pointer;
    padding: 0;
    width: 30px;
    height: 30px;
    display: flex;
    align-items: center;
    justify-content: center;
}

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

/* World Info Panel */
#world-info-panel {
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    min-width: 300px;
}

.info-section {
    margin-bottom: 15px;
    padding: 10px;
    background: rgba(255, 255, 255, 0.05);
    border-radius: 5px;
}

.info-section strong {
    color: #d4af37;
}

/* Inventory Panel */
#inventory-panel {
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    width: min(400px, 90vw);
}

#inventory-grid {
    display: grid;
    grid-template-columns: repeat(5, 1fr);
    gap: 8px;
    margin-bottom: 20px;
}

.inventory-slot {
    aspect-ratio: 1;
    background: rgba(255, 255, 255, 0.1);
    border: 2px solid #666;
    border-radius: 6px;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    position: relative;
    overflow: hidden;
}

.inventory-slot:hover, .inventory-slot:active {
    border-color: #d4af37;
    background: rgba(212, 175, 55, 0.2);
}

.inventory-slot img {
    max-width: 90%;
    max-height: 90%;
}

.item-quantity {
    position: absolute;
    bottom: 2px;
    right: 2px;
    background: rgba(0, 0, 0, 0.8);
    color: white;
    font-size: 0.7rem;
    padding: 1px 4px;
    border-radius: 3px;
}

#equipment-slots {
    display: flex;
    justify-content: space-around;
    gap: 10px;
}

.equipment-slot {
    flex: 1;
    aspect-ratio: 1;
    max-width: 80px;
    background: rgba(255, 255, 255, 0.1);
    border: 2px solid #666;
    border-radius: 8px;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    text-align: center;
    padding: 5px;
}

.equipment-slot:hover {
    border-color: #d4af37;
}

.equipment-slot span {
    font-size: 0.7rem;
    color: #ccc;
    margin-bottom: 5px;
}

.equipment-icon {
    width: 32px;
    height: 32px;
    background: rgba(255, 255, 255, 0.1);
    border-radius: 4px;
}

/* Mini-Map */
.mini-map {
    top: 20%;
    right: 10px;
    width: 220px;
    height: 240px;
}

#mini-map-canvas {
    width: 100%;
    height: 200px;
    border: 1px solid #666;
    border-radius: 5px;
    margin-top: 10px;
}

/* Dialogue Panel */
#dialogue-panel {
    bottom: 0;
    left: 0;
    right: 0;
    height: 200px;
    border-radius: 0;
    border-left: none;
    border-right: none;
    border-bottom: none;
}

.dialogue-content {
    display: flex;
    height: 100%;
    gap: 15px;
    padding: 20px;
}

#npc-portrait-container {
    flex-shrink: 0;
}

#npc-portrait {
    width: 80px;
    height: 80px;
    border: 2px solid #d4af37;
    border-radius: 8px;
}

#dialogue-text-container {
    flex: 1;
    display: flex;
    flex-direction: column;
    gap: 10px;
}

#npc-name {
    color: #d4af37;
    font-weight: bold;
    font-size: 1.1rem;
}

#dialogue-message {
    color: #f3f4f6;
    line-height: 1.4;
    flex: 1;
}

#dialogue-choices {
    display: flex;
    gap: 10px;
    flex-wrap: wrap;
}

.dialogue-choice {
    background: rgba(212, 175, 55, 0.2);
    border: 1px solid #d4af37;
    color: #fff;
    padding: 8px 16px;
    border-radius: 6px;
    cursor: pointer;
    transition: all 0.3s ease;
    touch-action: manipulation;
}

.dialogue-choice:hover, .dialogue-choice:active {
    background: rgba(212, 175, 55, 0.4);
}

/* Settings Screen */
#settings-screen {
    justify-content: center;
    align-items: center;
    background: rgba(0, 0, 0, 0.9);
}

.settings-panel {
    background: rgba(0, 0, 0, 0.95);
    border: 3px solid #d4af37;
    border-radius: 15px;
    padding: clamp(20px, 4vw, 30px);
    width: min(500px, 90vw);
    max-height: 90vh;
    overflow-y: auto;
}

.settings-panel h2 {
    color: #d4af37;
    margin-bottom: 25px;
    text-align: center;
    font-size: clamp(1.5rem, 4vw, 2rem);
}

.setting-group {
    margin-bottom: 25px;
}

.setting-group h3 {
    color: #d4af37;
    margin-bottom: 15px;
    font-size: 1.2rem;
}

.setting-item {
    margin-bottom: 15px;
}

.setting-item label {
    display: block;
    color: #f3f4f6;
    margin-bottom: 8px;
    font-weight: 500;
}

.setting-item input[type="range"] {
    width: 100%;
    height: 6px;
    border-radius: 3px;
    background: #333;
    outline: none;
    -webkit-appearance: none;
}

.setting-item input[type="range"]::-webkit-slider-thumb {
    appearance: none;
    width: 20px;
    height: 20px;
    border-radius: 50%;
    background: #d4af37;
    cursor: pointer;
}

.setting-item input[type="range"]::-moz-range-thumb {
    width: 20px;
    height: 20px;
    border-radius: 50%;
    background: #d4af37;
    cursor: pointer;
    border: none;
}

.setting-item select {
    width: 100%;
    background: #333;
    color: #fff;
    border: 1px solid #666;
    border-radius: 6px;
    padding: 10px;
    font-size: 1rem;
}

.setting-item input[type="checkbox"] {
    transform: scale(1.5);
    margin-right: 10px;
    accent-color: #d4af37;
}

/* Debug Panel */
.debug-panel {
    position: absolute;
    top: 10px;
    right: 10px;
    background: rgba(0, 0, 0, 0.8);
    padding: 10px;
    border-radius: 5px;
    font-family: monospace;
    font-size: 0.8rem;
    color: #00ff00;
    z-index: 30;
    display: none; /* Hidden by default */
}

.debug-panel div {
    margin-bottom: 2px;
}

/* Responsive Design */
@media (max-width: 768px) {
    .race-selection {
        grid-template-columns: repeat(2, 1fr);
    }
    
    .game-title {
        font-size: 2rem;
    }
    
    .subtitle {
        font-size: 1rem;
    }
    
    #character-panel {
        min-width: 160px;
    }
    
    #quest-panel {
        min-width: 140px;
    }
    
    .dpad {
        width: 100px;
        height: 100px;
    }
    
    .dpad-button {
        width: 34px;
        height: 34px;
        font-size: 1rem;
    }
    
    .dpad-up, .dpad-down {
        left: 33px;
    }
    
    .dpad-left, .dpad-right {
        top: 33px;
    }
    
    .dpad-center {
        top: 33px;
        left: 33px;
    }
    
    .dpad-inner {
        width: 34px;
        height: 34px;
    }
    
    .action-btn {
        width: 50px;
        height: 50px;
        font-size: 1.3rem;
    }
    
    .inventory-slot {
        min-height: 50px;
    }
    
    #dialogue-panel {
        height: 180px;
    }
    
    .dialogue-content {
        padding: 15px;
        gap: 10px;
    }
    
    #npc-portrait {
        width: 60px;
        height: 60px;
    }
}

@media (max-width: 480px) {
    .creation-panel, .settings-panel {
        padding: 15px;
    }
    
    .race-selection {
        grid-template-columns: 1fr;
    }
    
    .menu-btn {
        padding: 12px 20px;
        font-size: 1rem;
    }
    
    #inventory-grid {
        grid-template-columns: repeat(4, 1fr);
    }
    
    #equipment-slots {
        flex-direction: column;
        max-width: 100px;
        margin: 0 auto;
    }
    
    .dialogue-content {
        flex-direction: column;
    }
    
    #npc-portrait-container {
        align-self: center;
    }
}

@media (orientation: landscape) and (max-height: 600px) {
    .control-area.bottom-left,
    .control-area.bottom-right,
    .control-area.bottom-center {
        bottom: 10px;
    }
    
    .ui-panel.top-left,
    .ui-panel.top-right {
        top: 5px;
    }
    
    .dpad {
        width: 90px;
        height: 90px;
    }
    
    .dpad-button {
        width: 30px;
        height: 30px;
    }
    
    .action-btn {
        width: 45px;
        height: 45px;
    }
}

/* Safe area support for modern phones */
@supports (padding: max(0px)) {
    .ui-panel.top-left,
    .ui-panel.top-right {
        top: max(10px, env(safe-area-inset-top));
    }
    
    .control-area.bottom-left,
    .control-area.bottom-right,
    .control-area.bottom-center {
        bottom: max(20px, env(safe-area-inset-bottom) + 10px);
    }
    
    .overlay-panel:not(.mini-map) {
        margin: max(20px, env(safe-area-inset-top)) max(20px, env(safe-area-inset-right)) max(20px, env(safe-area-inset-bottom)) max(20px, env(safe-area-inset-left));
    }
}

/* High DPI displays */
@media (-webkit-min-device-pixel-ratio: 2), (min-resolution: 192dpi) {
    #canvas {
        image-rendering: -webkit-optimize-contrast;
    }
}

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

/* Dark mode support */
@media (prefers-color-scheme: dark) {
    /* Already using dark theme */
}

/* Focus styles for keyboard navigation */
button:focus,
.race-option:focus,
.inventory-slot:focus,
.equipment-slot:focus {
    outline: 2px solid #d4af37;
    outline-offset: 2px;
}

/* Print styles */
@media print {
    #game-container {
        display: none;
    }
    
    body::after {
        content: "Middle Earth Mobile RPG - Visit the web app to play!";
        display: block;
        text-align: center;
        padding: 50px;
        font-size: 18px;
    }
}