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

body {
    background: linear-gradient(135deg, #1a1a2e 0%, #16213e 100%);
    font-family: -apple-system, BlinkMacSystemFont, 'Microsoft YaHei', 'PingFang SC', sans-serif;
    color: #fff;
    min-height: 100vh;
    min-height: -webkit-fill-available;
    overflow-x: hidden;
    /* iOS overscroll fix */
    overscroll-behavior: none;
    -webkit-overflow-scrolling: touch;
    /* Prevent text selection on game elements */
    -webkit-user-select: none;
    user-select: none;
    /* Prevent pull-to-refresh */
    overflow-y: hidden;
}

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

#game-container {
    max-width: 1200px;
    margin: 0 auto;
    padding: 10px;
}

#top-bar {
    background: rgba(0, 0, 0, 0.6);
    border-radius: 10px;
    padding: 10px 20px;
    margin-bottom: 10px;
}

#player-stats {
    display: flex;
    gap: 20px;
    flex-wrap: wrap;
    justify-content: center;
}

#player-stats span {
    background: rgba(255, 255, 255, 0.1);
    padding: 5px 15px;
    border-radius: 5px;
    font-size: 14px;
}

#hp-display { color: #ff6b6b; }
#level-display { color: #ffd93d; }
#exp-display { color: #6bcb77; }
#gold-display { color: #ffe66d; }
#floor-display { color: #4ecdc4; }
#chapter-display { color: #ff9ff3; }

/* 状态栏 */
#status-bars {
    display: flex;
    gap: 15px;
    justify-content: center;
    align-items: center;
    margin-top: 8px;
    flex-wrap: wrap;
}

#pollution-container {
    display: flex;
    align-items: center;
    gap: 8px;
    color: #9b59b6;
}

#pollution-bar {
    width: 100px;
    height: 12px;
    background: #333;
    border-radius: 6px;
    overflow: hidden;
}

#pollution-fill {
    height: 100%;
    width: 0%;
    background: linear-gradient(90deg, #8e44ad 0%, #9b59b6 50%, #e74c3c 100%);
    transition: width 0.3s;
}

#pollution-value {
    font-size: 12px;
}

#world-pollution-container {
    display: flex;
    align-items: center;
    gap: 8px;
    color: #27ae60;
}

#world-pollution-bar {
    width: 120px;
    height: 12px;
    background: #333;
    border-radius: 6px;
    overflow: hidden;
}

#world-pollution-fill {
    height: 100%;
    width: 100%;
    background: linear-gradient(90deg, #27ae60 0%, #f39c12 50%, #e74c3c 100%);
    transition: width 0.5s;
}

#world-pollution-value {
    font-size: 12px;
}

#area-name {
    background: rgba(78, 205, 196, 0.3) !important;
    color: #4ecdc4 !important;
    animation: none !important;
}

#status-effects {
    display: flex;
    gap: 10px;
}

.status-icon {
    background: rgba(255, 0, 0, 0.3);
    padding: 3px 8px;
    border-radius: 5px;
    font-size: 12px;
    animation: pulse 1s infinite;
}

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

#main-area {
    display: flex;
    gap: 10px;
}

#game-canvas {
    background: #0f0f23;
    border-radius: 10px;
    border: 3px solid #4a4a6a;
    flex: 1;
    max-width: 800px;
    touch-action: none;
}

#side-panel {
    width: 250px;
    display: flex;
    flex-direction: column;
    gap: 10px;
}

#minimap-container, #inventory-panel, #equipment-panel {
    background: rgba(0, 0, 0, 0.6);
    border-radius: 10px;
    padding: 10px;
}

#minimap-container h3, #inventory-panel h3, #equipment-panel h3 {
    font-size: 14px;
    margin-bottom: 10px;
    color: #ffd93d;
}

#minimap {
    width: 100%;
    height: 120px;
    background: #1a1a2e;
    border-radius: 5px;
}

#inventory-list {
    max-height: 150px;
    overflow-y: auto;
}

.inventory-item {
    background: rgba(255, 255, 255, 0.1);
    padding: 5px 10px;
    margin: 3px 0;
    border-radius: 5px;
    cursor: pointer;
    font-size: 12px;
    display: flex;
    justify-content: space-between;
}

.inventory-item:hover {
    background: rgba(255, 255, 255, 0.2);
}

.equip-slot {
    background: rgba(74, 74, 106, 0.5);
    padding: 8px;
    margin: 5px 0;
    border-radius: 5px;
    font-size: 12px;
}

#message-log {
    background: rgba(0, 0, 0, 0.6);
    border-radius: 10px;
    padding: 10px;
    margin-top: 10px;
    height: 100px;
    overflow-y: auto;
}

#messages {
    font-size: 12px;
    line-height: 1.6;
}

.msg-battle { color: #ff6b6b; }
.msg-item { color: #6bcb77; }
.msg-system { color: #4ecdc4; }
.msg-dialog { color: #ffd93d; }

#controls-hint {
    text-align: center;
    padding: 10px;
    font-size: 12px;
    color: #888;
}

/* Audio Controls */
#audio-controls {
    position: fixed;
    top: 10px;
    right: 10px;
    display: flex;
    gap: 8px;
    z-index: 1000;
}

#audio-controls button {
    width: 40px;
    height: 40px;
    border-radius: 50%;
    border: 2px solid rgba(255, 255, 255, 0.3);
    background: rgba(0, 0, 0, 0.5);
    color: #fff;
    font-size: 18px;
    cursor: pointer;
    transition: all 0.2s;
    display: flex;
    align-items: center;
    justify-content: center;
}

#audio-controls button:hover {
    background: rgba(78, 205, 196, 0.4);
    border-color: #4ecdc4;
    transform: scale(1.1);
}

#audio-controls button.muted {
    opacity: 0.5;
    border-color: rgba(255, 255, 255, 0.1);
}

#audio-controls button.muted::after {
    content: '';
    position: absolute;
    width: 2px;
    height: 30px;
    background: #ff6b6b;
    transform: rotate(45deg);
}

/* Dialog Overlay */
.hidden { display: none !important; }

#dialog-overlay, #battle-overlay, #shop-overlay, #gameover-overlay {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(0, 0, 0, 0.8);
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 1000;
}

#dialog-box {
    background: linear-gradient(180deg, #2a2a4a 0%, #1a1a2e 100%);
    border: 3px solid #ffd93d;
    border-radius: 15px;
    padding: 20px;
    max-width: 500px;
    width: 90%;
}

#dialog-portrait-canvas {
    width: 80px;
    height: 80px;
    background: #4a4a6a;
    border-radius: 10px;
    margin: 0 auto 15px;
    display: block;
    image-rendering: pixelated;
    image-rendering: crisp-edges;
}

#dialog-name {
    text-align: center;
    font-size: 18px;
    font-weight: bold;
    color: #ffd93d;
    margin-bottom: 10px;
}

#dialog-text {
    text-align: center;
    font-size: 14px;
    line-height: 1.8;
    margin-bottom: 20px;
}

#dialog-options {
    display: flex;
    gap: 10px;
    justify-content: center;
    flex-wrap: wrap;
}

#dialog-options button {
    background: linear-gradient(180deg, #4ecdc4 0%, #44a08d 100%);
    border: none;
    padding: 10px 25px;
    border-radius: 25px;
    color: #fff;
    font-size: 14px;
    cursor: pointer;
    transition: transform 0.2s;
}

#dialog-options button:hover {
    transform: scale(1.05);
}

/* Battle Overlay */
#battle-box {
    background: linear-gradient(180deg, #2a2a4a 0%, #1a1a2e 100%);
    border: 3px solid #ff6b6b;
    border-radius: 15px;
    padding: 20px;
    max-width: 400px;
    width: 90%;
}

#battle-enemy {
    text-align: center;
    margin-bottom: 15px;
}

#enemy-sprite-canvas {
    width: 96px;
    height: 96px;
    margin: 0 auto 10px;
    display: block;
    image-rendering: pixelated;
    image-rendering: crisp-edges;
    background: rgba(0, 0, 0, 0.3);
    border-radius: 10px;
}

#enemy-name {
    font-size: 18px;
    font-weight: bold;
    margin-bottom: 10px;
}

#enemy-hp-bar {
    background: #333;
    height: 20px;
    border-radius: 10px;
    overflow: hidden;
}

#enemy-hp-fill {
    background: linear-gradient(90deg, #ff6b6b 0%, #ee5a5a 100%);
    height: 100%;
    width: 100%;
    transition: width 0.3s;
}

#battle-log {
    background: rgba(0, 0, 0, 0.3);
    padding: 10px;
    border-radius: 10px;
    height: 100px;
    overflow-y: auto;
    margin-bottom: 15px;
    font-size: 12px;
}

#battle-actions {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 10px;
}

#battle-actions button {
    background: linear-gradient(180deg, #6b5bff 0%, #4a4aff 100%);
    border: none;
    padding: 12px;
    border-radius: 10px;
    color: #fff;
    font-size: 14px;
    cursor: pointer;
    transition: transform 0.2s;
}

#battle-actions button:hover {
    transform: scale(1.05);
}

#btn-flee {
    background: linear-gradient(180deg, #888 0%, #666 100%);
}

#btn-flee:disabled {
    background: linear-gradient(180deg, #444 0%, #333 100%);
    cursor: not-allowed;
    opacity: 0.6;
}

/* Boss Battle Styles */
#battle-box.boss-battle {
    border-color: #ffd700;
    box-shadow: 0 0 30px rgba(255, 215, 0, 0.5), inset 0 0 20px rgba(255, 0, 0, 0.1);
    animation: bossGlow 2s ease-in-out infinite;
}

@keyframes bossGlow {
    0%, 100% { box-shadow: 0 0 30px rgba(255, 215, 0, 0.5), inset 0 0 20px rgba(255, 0, 0, 0.1); }
    50% { box-shadow: 0 0 50px rgba(255, 107, 107, 0.7), inset 0 0 30px rgba(255, 0, 0, 0.2); }
}

#battle-box.boss-battle #enemy-hp-bar {
    height: 25px;
    border: 2px solid #ffd700;
}

#battle-box.boss-battle #enemy-hp-fill {
    background: linear-gradient(90deg, #ff4444 0%, #ff6b6b 50%, #ffd700 100%);
}

#battle-box.boss-battle #enemy-sprite-canvas {
    width: 110px;
    height: 110px;
    border: 3px solid #ffd700;
    box-shadow: 0 0 15px rgba(255, 215, 0, 0.5);
}

#battle-box.boss-battle #enemy-name {
    font-size: 22px;
    text-shadow: 0 0 10px #ffd700;
}

/* Shop Overlay */
#shop-box {
    background: linear-gradient(180deg, #2a4a2a 0%, #1a2e1a 100%);
    border: 3px solid #6bcb77;
    border-radius: 15px;
    padding: 20px;
    max-width: 500px;
    width: 90%;
    max-height: 80vh;
    overflow-y: auto;
}

#shop-title {
    text-align: center;
    color: #6bcb77;
    margin-bottom: 20px;
}

#shop-items {
    display: flex;
    flex-direction: column;
    gap: 10px;
    margin-bottom: 20px;
}

.shop-item {
    display: flex;
    justify-content: space-between;
    align-items: center;
    background: rgba(0, 0, 0, 0.3);
    padding: 10px 15px;
    border-radius: 10px;
}

.shop-item-info {
    flex: 1;
}

.shop-item-name {
    font-weight: bold;
    margin-bottom: 3px;
}

.shop-item-desc {
    font-size: 11px;
    color: #aaa;
}

.shop-item-price {
    color: #ffe66d;
    margin-right: 15px;
}

.shop-item button {
    background: #6bcb77;
    border: none;
    padding: 8px 15px;
    border-radius: 5px;
    color: #fff;
    cursor: pointer;
}

.shop-item button:disabled {
    background: #555;
    cursor: not-allowed;
}

#shop-close {
    width: 100%;
    background: #666;
    border: none;
    padding: 12px;
    border-radius: 10px;
    color: #fff;
    cursor: pointer;
    font-size: 14px;
}

/* Story Overlay */
#story-overlay {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(0, 0, 0, 0.95);
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 1000;
}

#story-box {
    background: linear-gradient(180deg, #1a1a2e 0%, #0f0f1a 100%);
    border: 3px solid #9b59b6;
    border-radius: 15px;
    padding: 40px;
    max-width: 600px;
    width: 90%;
    text-align: center;
}

#story-text {
    font-size: 18px;
    line-height: 2;
    margin-bottom: 30px;
    color: #e0e0e0;
    min-height: 100px;
}

#story-next {
    background: linear-gradient(180deg, #9b59b6 0%, #8e44ad 100%);
    border: none;
    padding: 12px 40px;
    border-radius: 25px;
    color: #fff;
    font-size: 16px;
    cursor: pointer;
    transition: transform 0.2s;
}

#story-next:hover {
    transform: scale(1.05);
}

/* Game Over */
#gameover-box {
    background: linear-gradient(180deg, #4a2a2a 0%, #2e1a1a 100%);
    border: 3px solid #ff6b6b;
    border-radius: 15px;
    padding: 30px;
    text-align: center;
}

#gameover-box h1 {
    color: #ff6b6b;
    margin-bottom: 15px;
}

#gameover-text {
    margin-bottom: 20px;
}

#restart-btn {
    background: linear-gradient(180deg, #6bcb77 0%, #44a08d 100%);
    border: none;
    padding: 12px 30px;
    border-radius: 25px;
    color: #fff;
    font-size: 16px;
    cursor: pointer;
}

/* Mobile Controls */
#mobile-controls {
    display: none;
    position: fixed;
    bottom: 20px;
    left: 0;
    right: 0;
    padding: 10px;
    z-index: 100;
}

.control-row {
    display: flex;
    justify-content: center;
    gap: 10px;
    margin: 5px 0;
}

.ctrl-btn {
    width: 60px;
    height: 60px;
    background: rgba(255, 255, 255, 0.2);
    border: 2px solid rgba(255, 255, 255, 0.4);
    border-radius: 15px;
    color: #fff;
    font-size: 24px;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    user-select: none;
    -webkit-user-select: none;
    touch-action: manipulation;
}

.ctrl-btn:active {
    background: rgba(255, 255, 255, 0.4);
    transform: scale(0.95);
}

.ctrl-btn.action {
    width: 50px;
    height: 50px;
    font-size: 12px;
    background: rgba(78, 205, 196, 0.4);
}

/* Tablet Controls Layout */
.tablet-controls {
    display: flex;
    justify-content: space-between;
    align-items: flex-end;
    padding: 0 20px;
    max-width: 100%;
}

.dpad-container {
    display: flex;
    flex-direction: column;
    align-items: center;
}

.action-container {
    display: flex;
    flex-direction: column;
    align-items: center;
}

/* Phone Controls Layout */
.phone-controls {
    display: flex;
    flex-direction: column;
    align-items: center;
}

.phone-controls .action-row {
    margin-top: 5px;
}

/* Touch-friendly defaults */
button, .ctrl-btn, .inventory-item, .shop-item button {
    -webkit-tap-highlight-color: transparent;
    touch-action: manipulation;
}

/* Tablet / iPad Responsive (768px - 1024px) */
@media (min-width: 601px) and (max-width: 1024px) {
    #game-container {
        padding: 10px;
        padding-bottom: 220px;
    }

    #main-area {
        flex-direction: column;
    }

    #game-canvas {
        max-width: 100%;
        min-height: 400px;
    }

    #side-panel {
        width: 100%;
        flex-direction: row;
        flex-wrap: wrap;
        gap: 10px;
    }

    #minimap-container {
        flex: 1;
        min-width: 180px;
    }

    #inventory-panel {
        flex: 2;
        min-width: 250px;
    }

    #equipment-panel {
        flex: 1;
        min-width: 180px;
    }

    #inventory-list {
        max-height: 120px;
    }

    .inventory-item {
        padding: 10px 15px;
        font-size: 14px;
        min-height: 44px;
    }

    .equip-slot {
        padding: 12px;
        font-size: 14px;
        min-height: 44px;
    }

    #controls-hint {
        display: none;
    }

    #mobile-controls {
        display: block;
    }

    .ctrl-btn {
        width: 70px;
        height: 70px;
        font-size: 28px;
        border-radius: 18px;
    }

    .ctrl-btn.action {
        width: 70px;
        height: 70px;
        font-size: 14px;
    }

    .control-row {
        gap: 15px;
        margin: 8px 0;
    }

    #player-stats span {
        font-size: 14px;
        padding: 8px 16px;
    }

    #player-stats {
        gap: 12px;
    }

    #message-log {
        height: 80px;
    }

    #messages {
        font-size: 14px;
    }

    /* Battle overlay tablet adjustments */
    #battle-box {
        max-width: 500px;
        padding: 25px;
    }

    #battle-actions button {
        padding: 16px;
        font-size: 16px;
        min-height: 50px;
    }

    #battle-log {
        font-size: 14px;
        height: 120px;
    }

    /* Dialog tablet adjustments */
    #dialog-box {
        max-width: 550px;
        padding: 25px;
    }

    #dialog-options button {
        padding: 14px 30px;
        font-size: 16px;
        min-height: 50px;
    }

    #dialog-text {
        font-size: 16px;
    }

    /* Shop tablet adjustments */
    #shop-box {
        max-width: 600px;
        padding: 25px;
    }

    .shop-item {
        padding: 15px 20px;
        min-height: 60px;
    }

    .shop-item-name {
        font-size: 16px;
    }

    .shop-item-desc {
        font-size: 13px;
    }

    .shop-item button {
        padding: 12px 20px;
        font-size: 14px;
        min-height: 44px;
    }

    #shop-close {
        padding: 16px;
        font-size: 16px;
        min-height: 50px;
    }

    /* Story overlay tablet adjustments */
    #story-box {
        max-width: 650px;
        padding: 50px;
    }

    #story-text {
        font-size: 20px;
    }

    #story-next {
        padding: 16px 50px;
        font-size: 18px;
    }

    /* Status bars */
    #pollution-bar {
        width: 120px;
        height: 16px;
    }

    #pollution-value {
        font-size: 14px;
    }

    .status-icon {
        padding: 5px 12px;
        font-size: 14px;
    }
}

/* iPad Landscape specific */
@media (min-width: 768px) and (max-width: 1024px) and (orientation: landscape) {
    #game-container {
        padding-bottom: 180px;
    }

    #main-area {
        flex-direction: row;
    }

    #game-canvas {
        flex: 1;
        max-width: calc(100% - 280px);
        min-height: 350px;
    }

    #side-panel {
        width: 260px;
        flex-direction: column;
    }

    #minimap-container, #inventory-panel, #equipment-panel {
        min-width: auto;
        flex: none;
    }

    #inventory-list {
        max-height: 100px;
    }

    #mobile-controls {
        position: fixed;
        bottom: 10px;
        left: 10px;
        right: auto;
        width: auto;
    }

    .ctrl-btn {
        width: 55px;
        height: 55px;
        font-size: 22px;
    }

    .ctrl-btn.action {
        width: 55px;
        height: 55px;
        font-size: 12px;
    }

    .control-row {
        gap: 8px;
        margin: 4px 0;
    }
}

/* Mobile Responsive (up to 600px) */
@media (max-width: 600px) {
    #controls-hint {
        display: none;
    }

    #mobile-controls {
        display: block;
    }

    #side-panel {
        display: none;
    }

    #message-log {
        height: 60px;
    }

    #player-stats span {
        font-size: 11px;
        padding: 3px 8px;
    }

    #player-stats {
        gap: 8px;
    }

    #game-container {
        padding: 5px;
        padding-top: max(5px, env(safe-area-inset-top));
        padding-bottom: calc(200px + env(safe-area-inset-bottom));
        padding-left: max(5px, env(safe-area-inset-left));
        padding-right: max(5px, env(safe-area-inset-right));
    }

    .ctrl-btn {
        width: 60px;
        height: 60px;
        font-size: 24px;
    }

    .ctrl-btn.action {
        width: 55px;
        height: 55px;
        font-size: 12px;
    }

    /* Battle mobile adjustments */
    #battle-actions button {
        padding: 14px;
        font-size: 14px;
        min-height: 48px;
    }

    /* Shop mobile adjustments */
    .shop-item button {
        padding: 10px 15px;
        min-height: 44px;
    }

    #shop-close {
        padding: 14px;
        min-height: 48px;
    }

    /* Dialog mobile adjustments */
    #dialog-options button {
        padding: 12px 25px;
        min-height: 48px;
    }

    /* Pollution bar mobile */
    #pollution-bar {
        width: 80px;
        height: 10px;
    }

    #pollution-value {
        font-size: 11px;
    }

    /* Mobile controls safe area */
    #mobile-controls {
        padding-bottom: env(safe-area-inset-bottom);
    }
}

/* iPhone SE, iPhone 8 and similar (375px width) */
@media (max-width: 414px) and (min-height: 600px) {
    #game-container {
        padding-bottom: calc(190px + env(safe-area-inset-bottom));
    }

    #top-bar {
        padding: 6px 10px;
        border-radius: 8px;
    }

    #player-stats {
        gap: 5px;
    }

    #player-stats span {
        font-size: 10px;
        padding: 3px 6px;
        border-radius: 4px;
    }

    #message-log {
        height: 50px;
        margin-bottom: 5px;
    }

    #messages {
        font-size: 11px;
    }

    .ctrl-btn {
        width: 56px;
        height: 56px;
        font-size: 22px;
        border-radius: 14px;
    }

    .ctrl-btn.action {
        width: 52px;
        height: 52px;
        font-size: 11px;
    }

    .control-row {
        gap: 8px;
        margin: 4px 0;
    }

    /* Overlays for iPhone */
    #battle-box, #shop-box, #dialog-box {
        margin: 10px;
        max-height: calc(100vh - 20px - env(safe-area-inset-top) - env(safe-area-inset-bottom));
        overflow-y: auto;
    }

    #battle-box {
        padding: 15px;
    }

    #enemy-sprite-canvas {
        width: 72px;
        height: 72px;
    }

    #battle-log {
        height: 80px;
        font-size: 12px;
    }

    #battle-actions {
        gap: 8px;
    }

    #battle-actions button {
        padding: 12px 8px;
        font-size: 13px;
        min-height: 44px;
    }

    #shop-box {
        padding: 15px;
        max-width: calc(100vw - 20px);
    }

    .shop-item {
        padding: 10px;
        flex-direction: column;
        align-items: flex-start;
        gap: 8px;
    }

    .shop-item-price {
        margin-left: 0;
    }

    .shop-item button {
        width: 100%;
        padding: 12px;
    }

    #dialog-box {
        padding: 15px;
        flex-direction: column;
    }

    #dialog-portrait-canvas {
        width: 60px;
        height: 60px;
        margin: 0 auto 10px;
    }

    #dialog-text {
        font-size: 14px;
    }

    #dialog-options {
        flex-direction: column;
        gap: 8px;
    }

    #dialog-options button {
        width: 100%;
        padding: 12px;
    }

    /* Story overlay iPhone */
    #story-box {
        padding: 30px 20px;
        margin: 20px;
    }

    #story-text {
        font-size: 16px;
    }

    #story-next {
        padding: 14px 40px;
        font-size: 16px;
    }

    /* Game over iPhone */
    #gameover-box {
        padding: 30px 20px;
    }

    #gameover-box h1 {
        font-size: 28px;
    }
}

/* iPhone Pro Max / Plus models (larger phones) */
@media (min-width: 415px) and (max-width: 480px) {
    .ctrl-btn {
        width: 65px;
        height: 65px;
        font-size: 26px;
    }

    .ctrl-btn.action {
        width: 60px;
        height: 60px;
        font-size: 13px;
    }

    .control-row {
        gap: 12px;
    }

    #player-stats span {
        font-size: 12px;
        padding: 4px 10px;
    }
}

/* Notched iPhones (iPhone X and later) - detected by safe area */
@supports (padding-top: env(safe-area-inset-top)) {
    @media (max-width: 600px) {
        body {
            /* Ensure content doesn't go under notch */
            padding-top: env(safe-area-inset-top);
        }

        #game-container {
            padding-top: max(5px, env(safe-area-inset-top));
        }

        #mobile-controls {
            bottom: 10px;
            padding-bottom: calc(10px + env(safe-area-inset-bottom));
        }

        /* Overlays respect safe areas */
        .overlay, #battle-overlay, #shop-overlay, #dialog-overlay, #story-overlay, #gameover-overlay {
            padding-top: env(safe-area-inset-top);
            padding-bottom: env(safe-area-inset-bottom);
        }
    }
}

/* iPhone landscape mode */
@media (max-width: 900px) and (max-height: 500px) and (orientation: landscape) {
    #game-container {
        padding: 5px;
        padding-left: max(5px, env(safe-area-inset-left));
        padding-right: max(5px, env(safe-area-inset-right));
        padding-bottom: calc(160px + env(safe-area-inset-bottom));
    }

    #top-bar {
        padding: 4px 10px;
    }

    #player-stats span {
        font-size: 10px;
        padding: 2px 6px;
    }

    #player-stats {
        gap: 5px;
    }

    #message-log {
        height: 40px;
    }

    #mobile-controls {
        position: fixed;
        bottom: 5px;
        left: env(safe-area-inset-left);
        right: auto;
        width: auto;
        padding: 5px;
    }

    .tablet-controls, #mobile-controls > div {
        display: flex;
        flex-direction: row;
        align-items: center;
        gap: 20px;
    }

    .dpad-container, .action-container {
        display: flex;
        flex-direction: column;
    }

    .ctrl-btn {
        width: 45px;
        height: 45px;
        font-size: 18px;
        border-radius: 10px;
    }

    .ctrl-btn.action {
        width: 42px;
        height: 42px;
        font-size: 10px;
    }

    .control-row {
        gap: 5px;
        margin: 2px 0;
    }

    /* Landscape overlays */
    #battle-box, #shop-box, #dialog-box {
        max-height: calc(100vh - 20px);
        flex-direction: row;
        max-width: 90vw;
    }

    #battle-enemy {
        flex-shrink: 0;
    }

    #battle-log {
        height: 60px;
    }

    #shop-items {
        max-height: 150px;
    }
}

/* Small phones (iPhone SE 1st gen, etc) */
@media (max-width: 380px) {
    .ctrl-btn {
        width: 50px;
        height: 50px;
        font-size: 20px;
    }

    .ctrl-btn.action {
        width: 46px;
        height: 46px;
        font-size: 10px;
    }

    .control-row {
        gap: 6px;
    }

    #player-stats span {
        font-size: 9px;
        padding: 2px 5px;
    }

    #player-stats {
        gap: 4px;
    }

    #top-bar {
        padding: 5px 8px;
    }

    #message-log {
        height: 45px;
    }

    #messages {
        font-size: 10px;
    }

    /* Smaller overlays */
    #battle-actions button {
        padding: 10px 6px;
        font-size: 12px;
    }

    #enemy-sprite-canvas {
        width: 64px;
        height: 64px;
    }
}

/* Scrollbar */
::-webkit-scrollbar {
    width: 6px;
}

::-webkit-scrollbar-track {
    background: rgba(0, 0, 0, 0.3);
    border-radius: 3px;
}

::-webkit-scrollbar-thumb {
    background: #4a4a6a;
    border-radius: 3px;
}
