:root {
    --bg-color: #050505;
    --text-color: #d3d3d3;
    /* Light Gray instead of Green */
    --accent-color: #ff00ff;
    --border-color: #444;
    --font-main: 'Yuji Mai', serif;
}

html,
body {
    margin: 0;
    padding: 0;
    width: 100%;
    height: 100%;
    overflow: hidden;
    background-color: var(--bg-color);
}

#app-root {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background-color: var(--bg-color);
    color: var(--text-color);
    font-family: var(--font-main);
    display: flex;
    flex-direction: column;
    user-select: none;
    box-sizing: border-box;
    overflow: hidden;
    z-index: 10;
}

#game-wrapper {
    flex: 1;
    /* 広告タグ以外の余った縦スペースをすべて 차지する */
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: flex-start;
    /* 余白があれば中央寄せ、はみ出す場合は上部固定で安全にスクロール */
    width: 100%;
    min-height: 0;
    /* flex-itemが縮むために必須 */
    padding: 5px;
    box-sizing: border-box;
    overflow-y: auto;
    /* 万が一画面が小さすぎる場合は内部でスクロール */
}

#game-container {
    margin: auto;
    /* flex-startと組み合わせて安全に中央寄せ */
    width: 100%;
    max-width: 900px;
    height: 100%;
    max-height: 600px;
    /* PCでの最大高さ */
    display: grid;
    grid-template-areas:
        "view status"
        "log status"
        "controls status";
    grid-template-columns: 1fr 300px;
    /* 「view」を1frにして残りスペースを取り、ログは広がりすぎないようにする。はみ出し防止で最小値も引き下げ */
    grid-template-rows: minmax(150px, 1fr) minmax(80px, 150px) min-content;
    gap: 10px;
    padding: 10px;
    border: 2px solid var(--border-color);
    box-shadow: 0 0 20px rgba(211, 211, 211, 0.15);
    /* Match light gray */
    position: relative;
    z-index: 10000;
    background-color: var(--bg-color);
    box-sizing: border-box;
}

/* Viewport */
#viewport-container {
    grid-area: view;
    position: relative;
    background: #000;
    border: 2px solid var(--border-color);
    overflow: hidden;
}

canvas#view-canvas {
    width: 100%;
    height: 100%;
    image-rendering: pixelated;
}

#monster-overlay {
    position: absolute;
    bottom: 0px;
    left: 10px;
    right: 10px;
    height: 240px;
    pointer-events: none;
    display: none;
    justify-content: center;
    align-items: flex-end;
    gap: 10px;
}

.monster-img-container {
    position: relative;
    height: 100%;
    flex: 1;
    max-width: 240px;
    display: flex;
    justify-content: center;
    align-items: flex-end;
}

.monster-img-container img {
    max-height: 100%;
    max-width: 100%;
}

#floor-indicator {
    position: absolute;
    top: 10px;
    left: 10px;
    background: rgba(0, 0, 0, 0.7);
    padding: 5px 10px;
    border: 1px solid var(--text-color);
    font-size: 14px;
}

#timer-display {
    position: absolute;
    top: 10px;
    right: 10px;
    background: rgba(0, 0, 0, 0.7);
    color: #ffcc00;
    padding: 5px 10px;
    border: 1px solid var(--text-color);
    font-size: 14px;
    z-index: 10;
}

#btn-mute {
    position: absolute;
    top: 45px;
    right: 10px;
    background: rgba(0, 0, 0, 0.7);
    color: var(--text-color);
    padding: 5px 10px;
    border: 1px solid var(--text-color);
    font-size: 14px;
    z-index: 10;
    cursor: pointer;
    font-family: inherit;
    transition: all 0.2s;
}

#btn-mute:hover {
    color: #fff;
    border-color: #888;
}

#btn-manual {
    position: absolute;
    top: 80px;
    right: 10px;
    background: rgba(0, 0, 0, 0.7);
    color: #ffcc00;
    padding: 5px 12px;
    border: 1px solid #ffcc00;
    font-size: 16px;
    font-weight: bold;
    z-index: 10;
    cursor: pointer;
    font-family: inherit;
    transition: all 0.2s;
    border-radius: 4px;
}

#btn-manual:hover {
    background: #ffcc00;
    color: #000;
}

/* Status Area */
#status-area {
    grid-area: status;
    border: 2px solid var(--border-color);
    padding: 10px;
    display: flex;
    flex-direction: column;
    gap: 10px;
    background: rgba(10, 10, 10, 0.9);
}

.party-member {
    border: 1px solid var(--border-color);
    padding: 10px;
    font-size: 14px;
    position: relative;
    overflow: hidden;
    background: rgba(10, 10, 10, 0.9);
}

.card-portrait-bg {
    position: absolute;
    top: 0;
    left: 20%;
    width: 60%;
    height: 100%;
    background-size: cover;
    background-position: center;
    opacity: 0.25;
    z-index: 1;
    pointer-events: none;
}

.card-content {
    position: relative;
    z-index: 2;
}

.party-member.active {
    border-color: var(--text-color);
    background: rgba(211, 211, 211, 0.1);
}

.stat-bar {
    width: 100%;
    height: 14px;
    background: #222;
    margin-top: 3px;
    position: relative;
    border-radius: 2px;
    overflow: hidden;
}

.stat-fill {
    height: 100%;
    background: var(--text-color);
    transition: width 0.3s;
}

#minimap-container {
    margin-top: auto;
    text-align: center;
}

#minimap-canvas {
    width: 100%;
    aspect-ratio: 1;
    background: #000;
    border: 1px solid var(--text-color);
}

/* Log Window */
#log-window {
    grid-area: log;
    border: 2px solid var(--border-color);
    padding: 10px;
    font-size: 14px;
    overflow-y: auto;
    background: #000;
    display: flex;
    flex-direction: column;
}

.party-member.targeting {
    cursor: pointer;
    border-color: #ffcc00;
    box-shadow: 0 0 10px rgba(255, 204, 0, 0.5);
    animation: targeting-pulsate 1.5s infinite;
}

@keyframes targeting-pulsate {
    0% { border-color: #ffcc00; box-shadow: 0 0 5px rgba(255, 204, 0, 0.3); }
    50% { border-color: #fff; box-shadow: 0 0 15px rgba(255, 204, 0, 0.8); }
    100% { border-color: #ffcc00; box-shadow: 0 0 5px rgba(255, 204, 0, 0.3); }
}


/* Controls */
#controls {
    grid-area: controls;
    display: flex;
    justify-content: space-around;
    align-items: center;
    border: 2px solid var(--border-color);
}

.btn {
    background: #111;
    border: 1px solid var(--text-color);
    color: var(--text-color);
    padding: 8px 15px;
    cursor: pointer;
    font-family: inherit;
    font-size: 12px;
}

#event-screen {
    display: none;
    flex-direction: column;
    align-items: center;
    justify-content: flex-start;
    height: 100%;
    text-align: center;
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    z-index: 20000;
    background: rgba(0, 0, 0, 0.9);
    padding: 60px 20px;
    box-sizing: border-box;
    overflow-y: auto;
}

#event-screen > * {
    max-width: 600px;
    flex-shrink: 0;
}

.btn:hover:not(:disabled) {
    background: var(--text-color) !important;
    color: #000 !important;
}

.btn:disabled {
    border-color: #444;
    color: #444;
    cursor: not-allowed;
}

#battle-menu {
    display: none;
    gap: 5px;
}

/* Flash Effects */
.flash {
    animation: flash-anim 0.2s;
}

@keyframes flash-anim {
    0% {
        opacity: 1;
    }

    50% {
        opacity: 0;
    }

    100% {
        opacity: 1;
    }
}

.screen-flash {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    z-index: 100000;
    pointer-events: none;
    animation: screen-flash-anim 0.4s ease-out forwards;
}

@keyframes screen-flash-anim {
    0% {
        opacity: 0;
    }

    20% {
        opacity: 0.7;
    }

    100% {
        opacity: 0;
    }
}

#camp-menu {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(0, 0, 0, 0.95);
    border: 2px solid var(--text-color);
    display: none;
    flex-direction: column;
    padding: 0;
    /* Remove padding to handle scroll better */
    z-index: 10000;
    overflow-y: auto;
    scroll-behavior: smooth;
}

.camp-nav {
    position: sticky;
    top: 0;
    background: #111;
    border-bottom: 1px solid var(--border-color);
    padding: 5px;
    display: flex;
    flex-wrap: wrap;
    gap: 5px;
    z-index: 10001;
    box-shadow: 0 2px 10px rgba(0, 0, 0, 0.5);
}

.camp-nav .btn {
    padding: 4px 8px;
    font-size: 10px;
    flex: 1;
    min-width: 60px;
}

.camp-content {
    padding: 10px;
    display: flex;
    flex-direction: column;
    gap: 15px;
}

.camp-header {
    font-size: 18px;
    text-align: center;
    margin-bottom: 10px;
    border-bottom: 1px dotted var(--text-color);
    padding-bottom: 5px;
    color: #ffcc00;
}

.camp-character {
    display: flex;
    justify-content: space-between;
    border: 1px solid #444;
    background: rgba(20, 20, 20, 0.8);
    padding: 10px;
    border-radius: 4px;
    position: relative;
    scroll-margin-top: 50px;
}

.camp-character.low-hp {
    border-color: #f55;
    box-shadow: 0 0 5px rgba(255, 0, 0, 0.3);
}

/* Unused bars removed */

/* Layout: status area remains visible */

.camp-char-stats {
    flex: 1;
    font-size: 14px;
}

.camp-char-actions {
    flex: 1;
    display: flex;
    flex-direction: row;
    flex-wrap: wrap;
    justify-content: flex-start;
    align-items: center;
    gap: 8px;
}

@media (max-width: 900px) {
    #game-container {
        max-width: 100%;
        max-height: none;
        grid-template-areas: "view" "status" "log" "controls";
        grid-template-columns: 1fr;
        grid-template-rows: minmax(150px, 30vh) auto 1fr auto;
    }

    #status-area {
        flex-direction: row;
        justify-content: space-between;
        align-items: flex-start;
        /* Align both party-list and minimap to top */
        overflow: hidden;
        background: rgba(10, 10, 10, 0.95);
        padding: 5px 10px;
    }

    #party-list {
        flex: 0 0 220px;
        margin-right: 10px;
        margin-bottom: 0px;
        overflow-y: auto;
        overflow-x: hidden;
    }

    .party-member {
        padding: 4px 8px;
        /* Slightly tighter for better vertical fit */
        margin-bottom: 2px;
    }

    #minimap-container {
        flex: 1;
        display: flex;
        justify-content: center;
        align-items: flex-start;
        /* Move minimap gravity to top */
        background: #000;
        border: 1px solid var(--text-color);
        min-width: 0;
        height: 100%;
        /* Fill the auto-height container */
    }

    #minimap-canvas {
        width: auto !important;
        height: auto !important;
        max-width: 100%;
        max-height: 100%;
        aspect-ratio: 1;
        border: none;
    }

    #explore-menu:not([style*="display: none"]) {
        display: grid !important;
        grid-template-columns: 1fr 1.5fr 1fr 1fr;
        grid-template-rows: 1fr 1fr;
        grid-template-areas:
            "left forward right camp"
            "left backward right camp";
        gap: 5px;
        width: 100%;
        height: 120px;
        /* Give it a fixed height for mobile control area */
    }

    #explore-menu .btn {
        height: 100%;
        display: flex;
        align-items: center;
        justify-content: center;
        padding: 5px;
    }

    #btn-move-forward {
        grid-area: forward;
    }

    #btn-turn-left {
        grid-area: left;
    }

    #btn-turn-right {
        grid-area: right;
    }

    #btn-move-backward {
        grid-area: backward;
    }

    #btn-camp {
        grid-area: camp;
    }

    #battle-menu:not([style*="display: none"]) {
        display: grid !important;
        grid-template-columns: 1fr 1fr;
        gap: 5px;
        width: 100%;
    }

    .btn {
        padding: 12px 10px;
        font-size: 14px;
    }
}

/* Screens */
.overlay-screen {
    position: absolute;
    /* fixedから変更し#app-rootに追従させる */
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(0, 0, 0, 0.95);
    z-index: 20000;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    padding: 20px;
    color: var(--text-color);
    box-sizing: border-box;
    overflow-y: auto;
}

#timer-display {
    position: absolute;
    top: 10px;
    right: 10px;
    background: rgba(0, 0, 0, 0.7);
    padding: 5px 10px;
    border: 1px solid var(--text-color);
    font-size: 14px;
    font-family: inherit;
    color: #ffcc00;
    z-index: 5;
}

.ranking-list {
    margin-top: 20px;
    width: 100%;
    max-width: 400px;
    border: 1px solid var(--text-color);
    padding: 10px;
    background: #111;
}

.rank-item {
    display: flex;
    justify-content: space-between;
    border-bottom: 1px dotted #444;
    padding: 5px 0;
}

/* Stat Allocation UI */
.stat-alloc-row {
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin-bottom: 5px;
    font-size: 13px;
}

.stat-name {
    width: 40px;
    color: #aaa;
}

.stat-val-container {
    display: flex;
    align-items: center;
    gap: 10px;
}

.stat-btn {
    width: 24px;
    height: 24px;
    display: flex;
    align-items: center;
    justify-content: center;
    background: #222;
    border: 1px solid #555;
    color: #fff;
    cursor: pointer;
    border-radius: 50%;
    transition: all 0.2s;
}

.stat-btn:hover:not(:disabled) {
    background: #444;
    border-color: #ffcc00;
}

.stat-btn:disabled {
    opacity: 0.3;
    cursor: default;
}

/* Animations */
@keyframes story-fadein {
    from {
        opacity: 0;
        transform: translateY(10px);
    }

    to {
        opacity: 1;
        transform: translateY(0);
    }
}

.story-anim {
    animation: story-fadein 0.8s ease-out forwards;
}

@keyframes damage-float {
    0% {
        opacity: 1;
        transform: translate(-50%, -50%) scale(1);
    }

    100% {
        opacity: 0;
        transform: translate(-50%, calc(-50% - 50px)) scale(1.5);
    }
}

.damage-popup {
    position: absolute;
    color: #ff3333;
    font-size: 24px;
    font-weight: bold;
    text-shadow: 2px 2px 0 #000, -1px -1px 0 #fff;
    pointer-events: none;
    z-index: 1000;
    animation: damage-float 1s ease-out forwards;
}

.heal-popup {
    position: absolute;
    color: #33ff33;
    font-size: 24px;
    font-weight: bold;
    text-shadow: 2px 2px 0 #000, -1px -1px 0 #fff;
    pointer-events: none;
    z-index: 1000;
    animation: damage-float 1s ease-out forwards;
}

@keyframes shake-hit {
    0% {
        transform: translate(0, 0);
        filter: brightness(1) drop-shadow(0 0 0 red);
    }

    20% {
        transform: translate(-10px, 5px);
        filter: brightness(2) drop-shadow(0 0 10px red) sepia(1) hue-rotate(-50deg) saturate(5);
    }

    40% {
        transform: translate(10px, -5px);
        filter: brightness(2) drop-shadow(0 0 10px red) sepia(1) hue-rotate(-50deg) saturate(5);
    }

    60% {
        transform: translate(-10px, -5px);
        filter: brightness(2) drop-shadow(0 0 10px red) sepia(1) hue-rotate(-50deg) saturate(5);
    }

    80% {
        transform: translate(10px, 5px);
        filter: brightness(2) drop-shadow(0 0 10px red) sepia(1) hue-rotate(-50deg) saturate(5);
    }

    100% {
        transform: translate(0, 0);
        filter: brightness(1) drop-shadow(0 0 0 red);
    }
}

.target-hit {
    animation: shake-hit 0.4s ease-out;
}

@keyframes monster-death {
    0% {
        filter: brightness(2) saturate(0) contrast(2);
        opacity: 1;
        transform: scale(1);
    }

    100% {
        filter: brightness(5) saturate(0) contrast(2);
        opacity: 0;
        transform: scale(1.2);
    }
}

.monster-dead {
    animation: monster-death 0.5s ease-out forwards;
}

/* Blackout Overlay */
.fade-overlay {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: #000;
    z-index: 30000;
    display: none;
    align-items: center;
    justify-content: center;
    pointer-events: all;
}

.fade-text {
    color: #fff;
    font-size: 24px;
    text-align: center;
    opacity: 0;
    transition: opacity 1s;
}

@keyframes overlay-fadein {
    from {
        opacity: 0;
    }

    to {
        opacity: 1;
    }
}

@keyframes overlay-fadeout {
    from {
        opacity: 1;
    }

    to {
        opacity: 0;
    }
}

.fade-in {
    animation: overlay-fadein 1s forwards;
}

.fade-out {
    animation: overlay-fadeout 1s forwards;
}

/* Ghost Effects */
.ghost-portrait {
    filter: grayscale(100%) brightness(1.2) drop-shadow(0 0 8px #00ffff) opacity(0.6) !important;
}

.ghost-member {
    border-color: #00ffff !important;
    background: rgba(0, 30, 30, 0.6) !important;
    box-shadow: 0 0 10px rgba(0, 255, 255, 0.2);
}

.ghost-status {
    color: #00ffff;
    font-weight: bold;
    text-shadow: 0 0 5px #00ffff;
    font-size: 12px;
}