* {
    box-sizing: border-box;
}

:root {
    --primary-color: #ffb7c5;
    /* 桜色 */
    --danger-color: #ff4d4d;
    --accent-color: #70d5ff;
    /* 湯気っぽい青 */
    --glass-bg: rgba(255, 255, 255, 0.1);
    --glass-border: rgba(255, 255, 255, 0.2);
    --text-main: #ffffff;
    --text-dim: #cccccc;

    --grid-size: 50px;
    --cell-gap: 4px;
}

.hidden {
    display: none !important;
}

body {
    margin: 0;
    padding: 0;
    width: 100vw;
    height: 100vh;
    overflow: hidden;
    background-color: #121215;
    background-image: radial-gradient(circle at 50% 50%, #1e1e24 0%, #121215 100%);
    font-family: 'Zen Maru Gothic', sans-serif;
    color: var(--text-main);
}

#app {
    position: relative;
    width: 100%;
    height: 100%;
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
}

canvas {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    pointer-events: none;
}

#bg-canvas {
    z-index: 0;
}

#fx-canvas {
    z-index: 100;
}

#ui-layer {
    position: relative;
    z-index: 10;
    width: 100%;
    height: 100%;
    display: flex;
    flex-direction: column;
    pointer-events: none;
    /* UI要素以外はクリック通すなど工夫が必要だが、今回はflex配置で制御 */
}

/* Glassmorphism Panel */
.glass-panel {
    background: var(--glass-bg);
    backdrop-filter: blur(12px);
    -webkit-backdrop-filter: blur(12px);
    border: 1px solid var(--glass-border);
    border-radius: 16px;
    box-shadow: 0 4px 30px rgba(0, 0, 0, 0.3);
}

/* Status Bar */
.status-bar {
    margin: 20px auto;
    padding: 10px 20px;
    display: flex;
    gap: 30px;
    align-items: center;
    pointer-events: auto;
    width: fit-content;
    min-width: 300px;
}

.status-item {
    display: flex;
    align-items: center;
    gap: 10px;
}

.bar-container {
    width: 150px;
    height: 10px;
    background: rgba(0, 0, 0, 0.3);
    border-radius: 5px;
    overflow: hidden;
    position: relative;
}

.bar-fill {
    height: 100%;
    background: linear-gradient(90deg, #4dff88, #ffff00, #ff4d4d);
    width: 0%;
    transition: width 0.2s ease;
}

#game-container {
    flex: 1;
    display: flex;
    justify-content: center;
    align-items: center;
    pointer-events: auto;
    overflow: auto;
}

#grid-wrapper {
    display: grid;
    gap: var(--cell-gap);
    background: rgba(0, 0, 0, 0.2);
    padding: 10px;
    border-radius: 8px;
    /* Centering and Responsive Limit */
    margin: auto;
    max-width: 95vw;
    max-height: 85vh;
    overflow: hidden;
}

.cell {
    width: var(--grid-size);
    height: var(--grid-size);
    background-color: #2a2a30;
    border-radius: 4px;
    position: relative;
    cursor: pointer;
    transition: transform 0.1s, background-color 0.2s;
    display: flex;
    justify-content: center;
    align-items: center;
    font-size: calc(var(--grid-size) * 0.6);
    /* Responsive font size */
    overflow: visible;
}

.style-onsen .cell.curtain {
    background-image: url('assets/close.png') !important;
}

.style-onsen .cell.curtain {
    background-image: url('assets/close.png') !important;
    background-size: cover;
}

.cell.curtain {
    background-color: #3e3e4a;
    box-shadow: inset 0 0 10px rgba(0, 0, 0, 0.5);
    background-image: linear-gradient(90deg, rgba(255, 255, 255, 0.05) 50%, transparent 50%);
    background-size: 8px 100%;
}

.cell.curtain:hover {
    filter: brightness(1.2);
}

.cell.player-here {
    border: 2px solid var(--accent-color);
    box-shadow: 0 0 10px var(--accent-color);
}

.cell.opened {
    background-color: #1a1a1e;
}

.cell.ghost {
    color: #70d5ff;
}

.cell.human {
    background-color: #ff4d4d;
    /* Lose */
}

/* Controls */
#controls {
    position: absolute;
    bottom: 20px;
    left: 20px;
    /* Moved to left side */
    padding: 20px;
    pointer-events: auto;
    display: flex;
    flex-direction: column;
    gap: 15px;
    align-items: center;
    max-width: 150px;
    /* Limit width for mobile */
}

/* Hide crouch button when not in immersive mode */
body:not(.immersive-mode) #btn-crouch {
    display: none;
}

.d-pad {
    display: grid;
    grid-template-columns: repeat(3, 40px);
    grid-template-rows: repeat(2, 40px);
    gap: 5px;
}

.d-btn {
    width: 40px;
    height: 40px;
    border: none;
    border-radius: 8px;
    background: rgba(255, 255, 255, 0.1);
    color: white;
    font-size: 1.2rem;
    cursor: pointer;
}

.d-btn:active {
    background: rgba(255, 255, 255, 0.3);
}

.d-btn.up {
    grid-column: 2;
    grid-row: 1;
}

.d-btn.left {
    grid-column: 1;
    grid-row: 2;
}

.d-btn.down {
    grid-column: 2;
    grid-row: 2;
}

.d-btn.right {
    grid-column: 3;
    grid-row: 2;
}

.control-btn {
    padding: 10px 20px;
    border: none;
    border-radius: 8px;
    background: rgba(255, 255, 255, 0.2);
    color: white;
    font-family: inherit;
    cursor: pointer;
    font-weight: bold;
}

.control-btn.large {
    width: 100%;
    padding: 15px;
    background: var(--primary-color);
    color: #333;
    font-size: 1.1rem;
}

.control-btn:active {
    transform: scale(0.95);
}

/* Overlay & Modal */
#overlay {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(0, 0, 0, 0.6);
    z-index: 1000;
    display: flex;
    justify-content: center;
    align-items: center;
    pointer-events: auto;
    transition: opacity 0.3s;
}

#overlay.hidden {
    opacity: 0;
    pointer-events: none;
}

#modal {
    padding: 40px;
    text-align: center;
    max-width: 500px;
    width: 90%;
}

h1 {
    font-size: 2.5rem;
    margin-bottom: 20px;
    background: linear-gradient(90deg, #fff, #ffb7c5);
    -webkit-background-clip: text;
    background-clip: text;
    -webkit-text-fill-color: transparent;
}

.primary-btn {
    background: linear-gradient(135deg, var(--primary-color), #ff8fa3);
    border: none;
    padding: 15px 40px;
    border-radius: 30px;
    font-size: 1.2rem;
    color: #222;
    font-weight: bold;
    cursor: pointer;
    box-shadow: 0 4px 15px rgba(255, 183, 197, 0.4);
    transition: transform 0.2s, box-shadow 0.2s;
}

.primary-btn:hover {
    transform: translateY(-2px);
    box-shadow: 0 6px 20px rgba(255, 183, 197, 0.6);
}

/* Animations */
@keyframes heartBeat {
    0% {
        transform: scale(1);
    }

    15% {
        transform: scale(1.3);
    }

    30% {
        transform: scale(1);
    }

    45% {
        transform: scale(1.15);
    }

    60% {
        transform: scale(1);
    }
}

.pumping {
    animation: heartBeat 0.8s infinite;
}

.cell.flagged {
    color: #ff4d4d;
    text-shadow: 0 0 5px rgba(0, 0, 0, 0.5);
}

.ghost-active {
    background-color: rgba(0, 255, 255, 0.2) !important;
    box-shadow: inset 0 0 15px #0ff;
    animation: ghostFlash 0.5s infinite;
}

@keyframes ghostFlash {
    0% {
        opacity: 1;
    }

    50% {
        opacity: 0.6;
    }

    100% {
        opacity: 1;
    }
}

.ghost-cleared {
    background-color: rgba(255, 255, 255, 0.1) !important;
    filter: grayscale(1);
    opacity: 0.7;
}

.settings-group {
    background: rgba(0, 0, 0, 0.3);
    padding: 20px;
    border-radius: 12px;
    margin: 20px 0;
    display: flex;
    flex-direction: column;
    gap: 15px;
    text-align: left;
}

.settings-group label {
    display: flex;
    justify-content: space-between;
    align-items: center;
    font-size: 1rem;
}

/* Modern Form Elements */
.styled-select,
.styled-input {
    background: #2a2a30;
    color: #fff;
    border: 1px solid #444;
    border-radius: 6px;
    padding: 6px 10px;
    font-family: inherit;
    font-size: 0.9rem;
    appearance: none;
    /* Remove default arrow */
    cursor: pointer;
    min-width: 120px;
    text-align: center;
}

.styled-select {
    padding-right: 30px;
    background-image: url("data:image/svg+xml;charset=UTF-8,%3csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='white'%3e%3cpath d='M7 10l5 5 5-5z'/%3e%3c/svg%3e");
    background-repeat: no-repeat;
    background-position: right 8px center;
    background-size: 16px;
}

.styled-select:hover,
.styled-input:hover {
    border-color: var(--primary-color);
    background-color: #333;
}

.icon-btn {
    background: transparent;
    border: 1px solid #666;
    color: #ccc;
    width: 32px;
    height: 32px;
    border-radius: 50%;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: all 0.2s;
    font-size: 1.2rem;
}

.icon-btn:hover {
    background: #444;
    color: #fff;
    border-color: #999;
    transform: rotate(180deg);
}


#btn-action.active {
    background: white;
    color: black;
    transform: scale(0.95);
    box-shadow: 0 0 20px rgba(255, 255, 255, 0.5);
}

/* Immersive Mode Specific */
/* Immersive Mode Specific - High Specificity */
body.immersive-mode #grid-wrapper {
    display: grid;
    gap: 60px !important;
    /* Wide corridors */
    padding: 60px !important;
    background-color: #1a0f0a;

    /* RPG Map Floors */
    background-image:
        repeating-linear-gradient(90deg, #2a1b10 0px, #2a1b10 18px, #0f0906 18px, #0f0906 20px),
        repeating-linear-gradient(0deg, rgba(255, 255, 255, 0.03) 0px, rgba(255, 255, 255, 0.03) 1px, transparent 1px, transparent 60px);

    background-size: 20px 100%, 100% 60px;
    position: relative;
    box-shadow: inset 0 0 100px #000;

    image-rendering: pixelated;
    margin: 20px auto;
    /* Center horizontally */

    /* Overall board border (Outer Walls) */
    border: 16px solid #1a0f0a;
    outline: 2px dashed #3d2b1f;

    /* Ensure it fits in viewport if possible, or scroll */
    max-width: 95vw;
    max-height: 85vh;
    overflow: auto;

    /* Reset transform if any */
    transform: none;
}

body.immersive-mode .cell {
    width: 60px !important;
    height: 60px !important;
    border: 6px solid #5c3c2e;
    border-radius: 2px;
    background-color: #e6cea3;
    position: relative;
    box-shadow:
        0 4px 8px rgba(0, 0, 0, 0.8),
        inset 0 0 10px rgba(0, 0, 0, 0.3);
    overflow: hidden;
    background-size: cover;
    background-position: center;
    image-rendering: pixelated;
}

.immersive-mode .cell.curtain,
.style-onsen .cell.curtain {
    background-image: url('assets/close.png?v=2') !important;
    background-size: cover;
    background-position: center;
}

.immersive-mode .cell.curtain {
    border-color: #3d2b1f;
    /* Darker walls for closed rooms */
}

/* Default hidden state is handled, but ensure no styles leak */
#player-avatar {
    display: none;
    /* By default hidden */
}

body.immersive-mode #player-avatar {
    display: flex;
    /* Show in immersive */
    position: absolute;
    width: 32px;
    height: 32px;
    background: #fff;
    border: 2px solid #000;
    border-radius: 4px;
    z-index: 500;
    transition: left 0.1s linear, top 0.1s linear;
    align-items: center;
    justify-content: center;
    box-shadow: 0 0 10px rgba(255, 255, 255, 0.8);
    pointer-events: none;
    overflow: visible;
}

#player-avatar .arrow {
    position: absolute;
    top: -24px;
    width: 0;
    height: 0;
    border-left: 8px solid transparent;
    border-right: 8px solid transparent;
    border-bottom: 12px solid #fff;
    filter: drop-shadow(0 0 5px #fff);
    z-index: 501;
}

#player-avatar span {
    font-size: 1.8rem;
    line-height: 1;
    z-index: 2;
    filter: drop-shadow(0 0 2px rgba(255, 255, 255, 0.7));
}




/* New Tile Types (Immersive Backgrounds) */
.immersive-mode .cell.opened {
    background-image: url('assets/open_empty.png');
}

.immersive-mode .cell.salt-bath {
    background-image: url('assets/open_salt.png') !important;
    box-shadow: 0 0 30px #fff;
    animation: saltGlow 2s infinite;
}

.immersive-mode .cell.human {
    background-color: #000;
    /* Image will be set by JS */
}

.immersive-mode .cell.ghost-active {
    background-color: #000;
    box-shadow: inset 0 0 40px #a0f;
}

.cell.salt-bath {
    background: radial-gradient(circle, #fff 0%, #70d5ff 100%);
    box-shadow: 0 0 20px #fff;
    animation: saltGlow 2s infinite;
}

@keyframes saltGlow {

    0%,
    100% {
        transform: scale(1);
        filter: brightness(1);
    }

    50% {
        transform: scale(1.1);
        filter: brightness(1.5);
    }
}

.cell.salt-depleted {
    background: #111;
    border-color: #222;
    box-shadow: none;
    animation: none;
    opacity: 0.6;
}

.possessed-text {
    color: #a0f !important;
    text-shadow: 0 0 10px #f0f;
    animation: possessedShake 0.1s infinite;
}

@keyframes possessedShake {
    0% {
        transform: translate(0, 0);
    }

    25% {
        transform: translate(2px, -2px);
    }

    50% {
        transform: translate(-2px, 2px);
    }

    75% {
        transform: translate(2px, 2px);
    }
}

#btn-action.active {
    background: #fff;
    color: #000;
    transform: scale(0.95);
    box-shadow: 0 0 20px #fff;
}


/* Kuji-kiri Exorcism UI */
#kuji-overlay {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    display: flex;
    flex-wrap: wrap;
    align-content: center;
    justify-content: center;
    gap: 15px;
    z-index: 100;
    pointer-events: none;
    background: radial-gradient(circle, transparent 20%, rgba(0, 0, 0, 0.4) 80%);
    transition: background 0.5s;
}

#kuji-overlay.active {
    pointer-events: auto;
}

.kuji-btn {
    width: 80px;
    height: 80px;
    background: rgba(20, 0, 0, 0.8);
    border: 2px solid #600;
    border-radius: 50%;
    color: #f00;
    font-size: 2rem;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    user-select: none;
    transition: all 0.2s;
    box-shadow: 0 0 10px rgba(255, 0, 0, 0.3);
    font-family: 'Sawarabi Mincho', serif;
}

.kuji-btn:hover {
    transform: scale(1.1);
    background: rgba(60, 0, 0, 0.9);
    box-shadow: 0 0 20px #f00;
}

.kuji-btn.correct {
    background: #000;
    border-color: #0ff;
    color: #0ff;
    box-shadow: 0 0 15px #0ff;
    pointer-events: none;
    opacity: 0.5;
}

/* シャボン玉のようにゆっくりランダムに動くアニメーション（怨・冥難易度用） */
.kuji-btn.kuji-floating {
    animation: kujiFloat 3s ease-in-out infinite;
    animation-delay: calc(var(--float-delay, 0) * 0.3s);
}

@keyframes kujiFloat {

    0%,
    100% {
        transform: translate(var(--start-x, 0px), var(--start-y, 0px));
    }

    25% {
        transform: translate(calc(var(--start-x, 0px) + 15px), calc(var(--start-y, 0px) - 20px));
    }

    50% {
        transform: translate(calc(var(--start-x, 0px) - 10px), calc(var(--start-y, 0px) + 15px));
    }

    75% {
        transform: translate(calc(var(--start-x, 0px) + 20px), calc(var(--start-y, 0px) + 10px));
    }
}

/* Kuji Progress Atmosphere */
.kuji-step-1-7 {
    background: radial-gradient(circle, rgba(20, 0, 0, 0.4) 0%, rgba(0, 0, 0, 0.8) 100%) !important;
}

.kuji-step-8 {
    background: radial-gradient(circle, rgba(128, 0, 128, 0.5) 0%, rgba(30, 0, 30, 0.9) 100%) !important;
}

.kuji-step-9 {
    background: radial-gradient(circle, rgba(139, 0, 0, 0.7) 0%, rgba(50, 0, 0, 1.0) 100%) !important;
}

.purified-text {
    color: #0ff !important;
    text-shadow: 0 0 10px #fff;
    font-weight: bold;
}

#exorcism-msg {
    position: fixed;
    top: 25%;
    left: 50%;
    transform: translateX(-50%);
    font-size: 3.5rem;
    color: white;
    text-shadow: 0 0 25px red, 0 0 10px white;
    z-index: 101;
    pointer-events: none;
    font-family: 'Sawarabi Mincho', serif;
    letter-spacing: 0.8rem;
    transition: opacity 0.8s ease-out;
    opacity: 0;
}

#exorcism-msg.show {
    opacity: 1;
}

.salt-action {
    background: linear-gradient(135deg, #00d2ff 0%, #3a7bd5 100%) !important;
    color: white !important;
    box-shadow: 0 0 20px rgba(0, 210, 255, 0.6) !important;
    animation: saltButtonGlow 1.5s infinite alternate;
}

@keyframes saltButtonGlow {
    from {
        box-shadow: 0 0 20px rgba(0, 210, 255, 0.6);
    }

    to {
        box-shadow: 0 0 40px rgba(0, 210, 255, 0.9);
    }
}

/* Responsive */
@media (max-width: 600px) {
    :root {
        --grid-size: 40px;
    }

    #controls {
        bottom: 10px;
        left: 10px;
        /* Consistently on the left */
        padding: 10px;
        background: rgba(0, 0, 0, 0.4);
        /* Slightly darker for readability */
    }

    .d-pad {
        grid-template-columns: repeat(3, 35px);
        grid-template-rows: repeat(2, 35px);
    }

    .d-btn {
        width: 35px;
        height: 35px;
    }

    .status-bar {
        width: 95%;
        margin: 5px auto;
        padding: 5px 10px;
        gap: 10px;
        flex-wrap: wrap;
        /* Allow wrapping on small screens */
        justify-content: center;
    }

    .bar-container {
        width: 100px;
    }

    #modal {
        padding: 20px;
        width: 95%;
    }

    h1 {
        font-size: 1.8rem;
    }

    #exorcism-msg {
        font-size: 2rem;
        width: 90%;
        text-align: center;
    }
}

.hidden {
    display: none !important;
}