* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
    user-select: none;
}

html, body {
    height: 100%;
    overflow: hidden;
}

body {
    background: linear-gradient(135deg, #1a237e, #283593, #3949ab);
    min-height: 100vh;
    display: flex;
    justify-content: center;
    align-items: center;
    font-family: 'Segoe UI', Arial, sans-serif;
    touch-action: manipulation;
    overscroll-behavior: none;
    position: fixed;
    inset: 0;
}

#game-wrapper {
    background: #ffecb3;
    border-radius: 24px;
    padding: 14px;
    box-shadow: 0 10px 40px rgba(0,0,0,0.4);
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 10px;
    max-height: 100vh;
    box-sizing: border-box;
    overflow: hidden;
}

#header {
    width: 100%;
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 0 8px;
}

#header h1 {
    font-size: 28px;
    color: #4e342e;
    font-weight: 900;
    letter-spacing: 2px;
    text-shadow: 2px 2px 0 #d7ccc8;
}

#header-controls {
    display: flex;
    align-items: center;
    gap: 16px;
}

#block-count, #zombie-count {
    font-size: 18px;
    font-weight: 700;
    color: #4e342e;
    background: #fff8e1;
    padding: 6px 16px;
    border-radius: 20px;
    border: 2px solid #bcaaa4;
}

#zombie-count {
    background: #f3e5f5;
    border-color: #ce93d8;
    color: #4a148c;
}

#clear-btn, #save-btn, #load-btn, #zombie-toggle, #char-btn {
    color: white;
    border: none;
    padding: 8px 16px;
    border-radius: 20px;
    font-size: 14px;
    font-weight: 700;
    cursor: pointer;
    transition: all 0.2s;
}

#clear-btn {
    background: #ef5350;
    box-shadow: 0 3px 0 #c62828;
}

#save-btn {
    background: #42a5f5;
    box-shadow: 0 3px 0 #1565c0;
}

#load-btn {
    background: #66bb6a;
    box-shadow: 0 3px 0 #2e7d32;
}

#zombie-toggle {
    background: #ff7043;
    box-shadow: 0 3px 0 #d84315;
}

#char-btn {
    background: #8e24aa;
    box-shadow: 0 3px 0 #6a1b9a;
}

#clear-btn:hover, #save-btn:hover, #load-btn:hover, #zombie-toggle:hover, #char-btn:hover {
    transform: translateY(-2px);
}

#clear-btn:active, #save-btn:active, #load-btn:active, #zombie-toggle:active, #char-btn:active {
    transform: translateY(3px);
    box-shadow: 0 0 0;
}

#canvas-container {
    position: relative;
    border-radius: 16px;
    overflow: hidden;
    border: 4px solid #8d6e63;
    box-shadow: inset 0 0 0 2px #d7ccc8;
    background: #87ceeb;
}

#gameCanvas {
    display: block;
    width: min(800px, calc((100vh - 240px) * 4 / 3), 90vw);
    height: auto;
    aspect-ratio: 800 / 600;
    cursor: pointer;
}

#start-screen {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(0,0,0,0.6);
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    gap: 12px;
    z-index: 10;
}

#start-screen h2 {
    font-size: 52px;
    color: #ffd54f;
    font-weight: 900;
    text-shadow: 3px 3px 0 #4e342e;
    letter-spacing: 4px;
}

#start-screen p {
    color: white;
    font-size: 18px;
    text-align: center;
    line-height: 1.5;
}

.controls-hint {
    background: rgba(0,0,0,0.4);
    padding: 12px 24px;
    border-radius: 12px;
    font-size: 14px !important;
    line-height: 1.8 !important;
}

#start-btn {
    background: #66bb6a;
    color: white;
    border: none;
    padding: 16px 60px;
    border-radius: 30px;
    font-size: 28px;
    font-weight: 900;
    cursor: pointer;
    transition: all 0.2s;
    box-shadow: 0 5px 0 #388e3c;
    letter-spacing: 2px;
    margin-top: 8px;
}

#start-btn:hover {
    transform: translateY(-3px);
    box-shadow: 0 8px 0 #388e3c;
}

#start-btn:active {
    transform: translateY(5px);
    box-shadow: 0 0 0 #388e3c;
}

#char-select {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(0,0,0,0.7);
    display: none;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    gap: 12px;
    z-index: 10;
    padding: 20px;
}

#char-select h2 {
    font-size: 44px;
    color: #ffd54f;
    font-weight: 900;
    text-shadow: 3px 3px 0 #4e342e;
    letter-spacing: 3px;
    text-align: center;
}

.char-subtitle {
    color: white;
    font-size: 20px;
    margin-bottom: 4px;
}

#char-grid {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    gap: 14px;
    max-width: 480px;
    width: 100%;
}

.char-item {
    border-radius: 16px;
    border: 3px solid rgba(255,255,255,0.6);
    padding: 18px 8px 12px;
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 10px;
    cursor: pointer;
    transition: all 0.2s;
    box-shadow: 0 4px 12px rgba(0,0,0,0.3);
}

.char-item:hover {
    transform: scale(1.08);
    border-color: #fff;
    box-shadow: 0 6px 20px rgba(255,255,255,0.25);
}

.char-item:active {
    transform: scale(0.95);
}

.char-dot {
    width: 56px;
    height: 56px;
    border-radius: 50%;
    border: 3px solid rgba(0,0,0,0.25);
    display: block;
}

.char-rainbow {
    background: linear-gradient(135deg, #ff0000, #ff8800, #ffff00, #00cc00, #0088ff, #a822ff);
}

.rainbow-dot {
    background: linear-gradient(135deg, #ff0000, #ff8800, #ffff00, #00cc00, #0088ff, #a822ff);
}

.char-name {
    font-size: 18px;
    font-weight: 900;
    color: #4e342e;
    text-shadow: 1px 1px 0 rgba(255,255,255,0.6);
}

#char-back-btn {
    background: #ef5350;
    color: white;
    border: none;
    padding: 12px 40px;
    border-radius: 24px;
    font-size: 18px;
    font-weight: 700;
    cursor: pointer;
    transition: all 0.2s;
    box-shadow: 0 4px 0 #c62828;
    margin-top: 6px;
}

#char-back-btn:hover {
    transform: translateY(-2px);
}

#char-back-btn:active {
    transform: translateY(4px);
    box-shadow: 0 0 0 #c62828;
}

#palette {
    display: flex;
    gap: 6px;
    padding: 10px 16px;
    background: #fff8e1;
    border-radius: 16px;
    border: 3px solid #bcaaa4;
    flex-wrap: wrap;
    justify-content: center;
    align-items: center;
}

.palette-items-section {
    display: flex;
    gap: 6px;
    align-items: center;
    margin-left: 10px;
    padding-left: 10px;
    border-left: 2px solid #bcaaa4;
}

.palette-label {
    font-size: 12px;
    font-weight: 700;
    color: #4e342e;
}

.palette-item-special {
    background: #546E7A !important;
    display: flex;
    align-items: center;
    justify-content: center;
}

.item-icon {
    font-size: 20px;
    line-height: 1;
}

.palette-item {
    width: 44px;
    height: 44px;
    border-radius: 8px;
    border: 3px solid transparent;
    cursor: pointer;
    transition: all 0.2s;
    position: relative;
}

.palette-item:hover {
    transform: scale(1.15);
}

.palette-item.selected {
    border-color: #1a237e;
    transform: scale(1.15);
    box-shadow: 0 0 12px rgba(26,35,126,0.4);
}

.palette-item .tooltip {
    position: absolute;
    bottom: -28px;
    left: 50%;
    transform: translateX(-50%);
    background: rgba(0,0,0,0.8);
    color: white;
    padding: 2px 8px;
    border-radius: 6px;
    font-size: 11px;
    white-space: nowrap;
    display: none;
    pointer-events: none;
}

.palette-item:hover .tooltip {
    display: block;
}

#mobile-controls {
    display: none;
    gap: 14px;
    margin-top: 4px;
    padding-bottom: env(safe-area-inset-bottom);
}

.m-mode {
    width: 72px;
    background: #43a047;
    box-shadow: 0 4px 0 #2e7d32;
    font-size: 26px;
}

.m-mode.mode-walk {
    background: #fb8c00;
    box-shadow: 0 4px 0 #e65100;
}

#rotate {
    display: none;
    position: fixed;
    inset: 0;
    z-index: 9999;
    align-items: center;
    justify-content: center;
    background: linear-gradient(180deg, #4a90d9, #2c6bb8);
    color: #fff;
    text-align: center;
    flex-direction: column;
}

.rotate-card {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 12px;
}

.rotate-icon {
    font-size: 56px;
}

.rotate-card strong {
    font-size: 28px;
    letter-spacing: 1px;
}

.rotate-card span {
    font-size: 16px;
    color: rgba(255,255,255,0.85);
}

@media (max-width: 700px) and (orientation: portrait) {
    #rotate { display: flex; }
}

#tablet-bar {
    display: flex;
    justify-content: center;
    gap: 10px;
}

#tablet-btn {
    display: flex;
    align-items: center;
    gap: 8px;
    background: #37474F;
    color: white;
    border: none;
    padding: 10px 20px;
    border-radius: 20px;
    font-size: 14px;
    font-weight: 700;
    cursor: pointer;
    transition: all 0.2s;
    box-shadow: 0 4px 0 #263238;
}

#tablet-btn:hover {
    transform: translateY(-2px);
    box-shadow: 0 6px 0 #263238;
}

#tablet-btn:active {
    transform: translateY(4px);
    box-shadow: 0 0 0 #263238;
}

.tablet-icon {
    font-size: 18px;
}

.tablet-label {
    color: #4FC3F7;
}

#camera-count {
    background: #263238;
    padding: 2px 8px;
    border-radius: 10px;
    font-size: 12px;
    color: #90A4AE;
}

.m-btn {
    width: 60px;
    height: 60px;
    border-radius: 50%;
    background: #5c6bc0;
    color: white;
    border: none;
    font-size: 24px;
    cursor: pointer;
    box-shadow: 0 4px 0 #3949ab;
    transition: all 0.05s;
    touch-action: none;
}

.m-btn:active {
    transform: translateY(4px);
    box-shadow: 0 0 0 #3949ab;
}

@media (max-width: 860px) {
    #game-wrapper {
        padding: 8px;
        border-radius: 0;
        width: 100%;
        height: 100vh;
        height: 100dvh;
        max-height: none;
        gap: 6px;
    }

    #canvas-container {
        flex: 1;
        min-height: 0;
        width: 100%;
        border-width: 3px;
    }

    #gameCanvas {
        width: 100%;
        height: 100%;
        max-width: 100%;
        max-height: 100%;
        aspect-ratio: 800 / 600;
        object-fit: contain;
        cursor: pointer;
    }

    #header h1 {
        font-size: 20px;
    }

    #header {
        flex-wrap: wrap;
        gap: 6px;
    }

    #header-controls {
        flex-wrap: wrap;
        justify-content: center;
        gap: 6px;
    }

    #block-count, #zombie-count {
        font-size: 13px;
        padding: 4px 10px;
    }

    #clear-btn, #save-btn, #load-btn, #zombie-toggle, #char-btn {
        font-size: 12px;
        padding: 6px 9px;
    }

    #palette {
        flex-wrap: nowrap;
        overflow-x: auto;
        -webkit-overflow-scrolling: touch;
        scrollbar-width: none;
        padding: 6px 10px;
        gap: 5px;
        justify-content: flex-start;
    }

    #palette::-webkit-scrollbar {
        display: none;
    }

    .palette-items-section {
        margin-left: 6px;
        padding-left: 6px;
        flex-shrink: 0;
    }

    .palette-item {
        width: 38px;
        height: 38px;
        flex-shrink: 0;
    }

    #tablet-btn {
        padding: 7px 14px;
    }

    #mobile-controls {
        display: flex;
        justify-content: center;
        margin-top: 2px;
    }

    .m-btn {
        width: 64px;
        height: 64px;
        font-size: 26px;
    }

    .m-mode {
        width: 76px;
        font-size: 28px;
    }

    #char-select h2 {
        font-size: 30px;
    }

    #char-grid {
        grid-template-columns: repeat(2, 1fr);
        max-width: 320px;
    }

    .char-dot {
        width: 44px;
        height: 44px;
    }

    #start-screen h2 {
        font-size: 38px;
    }

    .controls-hint {
        font-size: 13px !important;
        padding: 10px 16px;
        max-width: 92%;
        max-height: 55%;
        overflow-y: auto;
    }

    #start-btn {
        font-size: 22px;
        padding: 14px 48px;
    }

    .tile .tooltip {
        display: none !important;
    }
}

@media (max-width: 860px) and (max-height: 480px) {
    #game-wrapper {
        gap: 4px;
        padding: 6px;
    }

    #tablet-bar {
        display: none;
    }

    #header h1 {
        font-size: 17px;
    }

    #block-count, #zombie-count {
        font-size: 12px;
        padding: 3px 8px;
    }

    .m-btn {
        width: 58px;
        height: 58px;
        font-size: 22px;
    }

    .m-mode {
        width: 70px;
        font-size: 24px;
    }

    #palette {
        padding: 4px 8px;
    }

    .palette-item {
        width: 32px;
        height: 32px;
    }
}
