@property --madness {
    syntax: '<number>';
    inherits: true;
    initial-value: 0;
}

/* 基本設定 */
*, *::before, *::after {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
    touch-action: none;
    -webkit-tap-highlight-color: transparent;
    -webkit-touch-callout: none;
    user-select: none;
    -webkit-user-select: none;
}

html, body {
    font-family: 'Hiragino Mincho ProN', 'Yu Mincho', '游明朝', serif;
    background: #000;
    color: #fff;
    overflow: hidden;
    position: fixed;
    width: 100%;
    height: 100%;
    overscroll-behavior: none;
}

.hidden {
    display: none !important;
}

/* 画面共通 */
.screen {
    position: fixed;
    top: 0;
    left: 0;
    width: 100vw;
    height: 100vh;
    opacity: 0;
    visibility: hidden;
    transition: opacity 0.8s ease-in-out, visibility 0.8s ease-in-out;
}

.screen.active {
    opacity: 1;
    visibility: visible;
}

/* タイトル画面 */
#titleScreen {
    display: flex;
    align-items: center;
    justify-content: center;
    background: linear-gradient(180deg, #1a0033 0%, #000000 100%);
}

.title-content {
    position: relative;
    text-align: center;
    width: 100%;
    height: 100%;
}

.castle-bg {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    object-fit: cover;
}

/* 既存のアニメーション */
.lightning { animation: flash 3s infinite; }
@keyframes flash { 0%, 90%, 100% { opacity: 0; } 5%, 15% { opacity: 1; } }
.fog { animation: drift 4s ease-in-out infinite alternate; }
@keyframes drift { 0% { transform: translateX(-10px); } 100% { transform: translateX(10px); } }
.game-title {
    font-size: clamp(2rem, 8vw, 4rem);
    color: #fff;
    text-shadow: 0 0 20px #ff6b6b, 0 0 40px #ff6b6b;
    margin-bottom: 1rem;
    animation: titleGlow 2s ease-in-out infinite alternate;
}
@keyframes titleGlow { 0% { text-shadow: 0 0 20px #ff6b6b; } 100% { text-shadow: 0 0 60px #ff6b6b; } }

.title-text {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    z-index: 10;
}

.subtitle {
    font-size: clamp(0.8rem, 3vw, 1.2rem);
    color: #ccc;
    margin-bottom: 2rem;
    font-style: italic;
}

.game-btn {
    min-height: 56px;
    min-width: 56px;
    cursor: pointer;
    touch-action: manipulation;
    transition: transform 0.08s ease, opacity 0.08s ease, filter 0.08s ease;
    will-change: transform;
}

.game-btn:active,
.game-btn.is-pressed {
    transform: scale(0.92);
    opacity: 0.88;
}

.start-button {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 0.5rem;
    background: rgba(255, 107, 107, 0.2);
    border: 2px solid #ff6b6b;
    color: #fff;
    padding: clamp(0.8rem, 3vw, 1.2rem) clamp(1.5rem, 5vw, 2.5rem);
    font-size: clamp(1rem, 4vw, 1.3rem);
    cursor: pointer;
    border-radius: 50px;
    transition: transform 0.08s ease, opacity 0.08s ease, box-shadow 0.3s ease, background 0.3s ease;
    font-family: inherit;
    margin: 3rem auto 0;
    transform: scale(1.2);
}

.continue-button {
    margin-top: 1rem;
    transform: scale(1);
    font-size: clamp(0.85rem, 3vw, 1rem);
    background: rgba(120, 80, 160, 0.25);
    border-color: rgba(180, 140, 220, 0.6);
}

.start-button:hover {
    background: rgba(255, 107, 107, 0.4);
    box-shadow: 0 0 30px rgba(255, 107, 107, 0.5);
    transform: scale(1.2) translateY(-2px);
}

/* ゲーム画面 */
#gameScreen { position: relative; }
.game-bg {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    z-index: 1;
    overflow: hidden;
    filter:
        hue-rotate(calc(var(--madness) * 10deg))
        contrast(calc(1 + var(--madness) * 0.07))
        saturate(calc(1 + var(--madness) * 0.12));
    transition: filter 0.6s ease;
}
#gameScreen.is-injured .game-bg::after {
    content: '';
    position: absolute;
    inset: 0;
    pointer-events: none;
    box-shadow: inset 0 0 90px rgba(90, 0, 0, 0.55);
    mix-blend-mode: multiply;
}
#gameScreen.is-mad .game-bg::before {
    content: '';
    position: absolute;
    inset: -4%;
    pointer-events: none;
    background: rgba(80, 0, 20, calc(var(--madness) * 0.08));
    mix-blend-mode: color;
}
.scene-bg {
    width: 100%;
    height: 100%;
    object-fit: cover;
    transform-origin: center center;
    will-change: transform, opacity;
    transition: opacity 0.4s ease;
}

.scene-bg.is-fading {
    opacity: 0.15;
}

.effect-canvas {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    z-index: 15;
    pointer-events: none;
}

.ending-overlay {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    z-index: 4;
    pointer-events: none;
    opacity: 0;
    transition: opacity 1.2s ease, background 0.8s ease;
}

.ending-overlay.effect-white {
    background: #fff;
    opacity: 0.92;
}

.ending-overlay.effect-white-soft {
    background: #fff;
    opacity: 0.28;
}

.ending-overlay.effect-true-glow {
    background: radial-gradient(circle at 50% 40%, rgba(255, 250, 230, 0.55), rgba(255, 255, 255, 0.12));
    opacity: 0.55;
}

.ending-overlay.effect-black {
    background: #000;
    opacity: 0.72;
}

/* テキスト演出: 震える文字 */
.shaking-text {
    display: inline-block;
    animation: shake 0.1s infinite;
    color: #ffcccc;
}
@keyframes shake {
    0% { transform: translate(0, 0); }
    25% { transform: translate(1px, 1px); }
    50% { transform: translate(-1px, -1px); }
    75% { transform: translate(-1px, 1px); }
    100% { transform: translate(1px, -1px); }
}

/* テキスト演出: 叫び */
.scream-text {
    font-size: 200%;
    color: #ff0000;
    font-weight: bold;
    display: block;
    text-align: center;
    text-shadow: 2px 2px 0 #000;
    animation: screamImpact 0.2s ease-out;
}
.text-blood {
    color: #ff4d4d;
    font-weight: bold;
    text-align: center;
    text-shadow: 0 0 12px rgba(255, 40, 40, 0.7);
}
.text-gold {
    color: #f3d27a;
    font-weight: bold;
    text-align: center;
    text-shadow: 0 0 14px rgba(230, 190, 80, 0.75);
}
@keyframes screamImpact {
    0% { transform: scale(3); opacity: 0; }
    100% { transform: scale(1); opacity: 1; }
}

/* 全画面テキストモード */
.fullscreen-text-overlay {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(0, 0, 0, 0.95);
    z-index: 20;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 2rem;
    font-size: 1.5rem;
    line-height: 2;
    text-align: center;
    color: #fff;
    pointer-events: none; /* クリックは下の要素に通す */
}

/* ノイズ演出（メタ恐怖） */
.noise-overlay {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background-image: url("data:image/svg+xml,%3Csvg viewBox='0 0 200 200' xmlns='http://www.w3.org/2000/svg'%3E%3Cfilter id='noiseFilter'%3E%3CfeTurbulence type='fractalNoise' baseFrequency='0.8' numOctaves='3' stitchTiles='stitch'/%3E%3C/filter%3E%3Crect width='100%25' height='100%25' filter='url(%23noiseFilter)' opacity='0.5'/%3E%3C/svg%3E");
    z-index: 25;
    opacity: 0.1;
    pointer-events: none;
    animation: noiseAnim 0.2s infinite;
}
@keyframes noiseAnim {
    0% { transform: translate(0,0); }
    10% { transform: translate(-5%,-5%); }
    20% { transform: translate(-10%,5%); }
    30% { transform: translate(5%,-10%); }
    40% { transform: translate(-5%,15%); }
    50% { transform: translate(-10%,5%); }
    60% { transform: translate(15%,0); }
    70% { transform: translate(0,10%); }
    80% { transform: translate(-15%,0); }
    90% { transform: translate(10%,5%); }
    100% { transform: translate(5%,0); }
}

/* インゲーム通知 */
.notification-area {
    position: absolute;
    top: 1rem;
    left: 1rem;
    z-index: 50;
    color: rgba(255, 255, 255, 0.7);
    font-family: monospace;
    font-size: 0.9rem;
    pointer-events: none;
}
.notification-msg {
    animation: fadeOut 3s forwards;
}
@keyframes fadeOut {
    0% { opacity: 1; transform: translateY(0); }
    80% { opacity: 1; }
    100% { opacity: 0; transform: translateY(-10px); }
}

/* テキストウィンドウ */
.text-window {
    position: absolute;
    bottom: 0;
    left: 0;
    width: 100%;
    height: 35%;
    background: rgba(0, 0, 0, 0.85);
    backdrop-filter: blur(10px);
    -webkit-backdrop-filter: blur(10px);
    border-top: 2px solid rgba(255, 107, 107, 0.3);
    padding: clamp(1rem, 3vw, 2rem);
    padding-bottom: calc(clamp(1rem, 3vw, 2rem) + env(safe-area-inset-bottom, 0px));
    z-index: 5;
    display: flex;
    flex-direction: column;
    transition: opacity 0.5s;
}
.text-window.faded { opacity: 0; pointer-events: none; }

.speaker-name {
    color: #ff6b6b;
    font-size: clamp(1rem, 3vw, 1.3rem);
    font-weight: bold;
    margin-bottom: 0.5rem;
    text-shadow: 0 0 10px rgba(255, 107, 107, 0.5);
}
.speaker-name.sp-son { color: #ffc9a3; text-shadow: 0 0 10px rgba(255, 160, 100, 0.5); }
.speaker-name.sp-mina { color: #b8e0ff; text-shadow: 0 0 10px rgba(140, 200, 255, 0.55); }
.speaker-name.sp-demon { color: #c9a6ff; text-shadow: 0 0 12px rgba(120, 60, 200, 0.7); }
.speaker-name.sp-clown { color: #9cff7a; text-shadow: 0 0 10px rgba(120, 255, 80, 0.45); }
.speaker-name.sp-phantom { color: #ff6b8a; text-shadow: 0 0 10px rgba(255, 40, 80, 0.6); }
.speaker-name.sp-se { color: #bbb; font-weight: normal; text-shadow: none; }
.speaker-name.sp-sys { color: #e8d48b; text-shadow: 0 0 10px rgba(220, 180, 80, 0.45); }

.game-text ruby {
    ruby-position: over;
}
.game-text rt {
    font-size: 0.55em;
    color: rgba(255, 210, 210, 0.85);
}

.game-text {
    flex: 1;
    font-size: clamp(0.9rem, 2.5vw, 1.1rem);
    line-height: 1.6;
    color: #fff;
    overflow-y: auto;
    padding-right: 1rem;
}

/* カーソル点滅効果 */
.cursor-blink {
    display: inline-block;
    width: 0.5em;
    height: 1em;
    background: #fff;
    animation: blink 1s infinite;
    vertical-align: text-bottom;
}
@keyframes blink { 50% { opacity: 0; } }

.next-button {
    position: absolute;
    bottom: 1rem;
    right: 1rem;
    color: #ff6b6b;
    cursor: pointer;
    animation: pulse 1.5s infinite;
    transition: transform 0.2s ease;
}
.next-button:hover { transform: scale(1.2); }
@keyframes pulse { 0%, 100% { opacity: 0.6; } 50% { opacity: 1; } }

/* 選択肢 */
.choices-container {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    z-index: 6;
    width: 90%;
    max-width: 600px;
}
.choices-container.hidden { opacity: 0; visibility: hidden; pointer-events: none; }
.choices-wrapper { display: flex; flex-direction: column; gap: 1rem; }
.choice-item {
    background: rgba(45, 27, 61, 0.9);
    border: 2px solid rgba(255, 107, 107, 0.5);
    color: #fff;
    padding: clamp(1rem, 3vw, 1.5rem);
    font-size: clamp(0.9rem, 2.5vw, 1.1rem);
    text-align: center;
    cursor: pointer;
    border-radius: 10px;
    transition: transform 0.08s ease, opacity 0.08s ease, background 0.3s ease, border-color 0.3s ease;
    backdrop-filter: blur(5px);
    -webkit-backdrop-filter: blur(5px);
    min-height: 48px;
}

.choice-item:active {
    transform: scale(0.96);
    opacity: 0.9;
}
.choice-item:hover {
    background: rgba(255, 107, 107, 0.2);
    border-color: #ff6b6b;
    box-shadow: 0 0 20px rgba(255, 107, 107, 0.3);
    transform: translateY(-2px);
}

/* UI controls */
.game-ui {
    position: absolute;
    top: calc(1rem + env(safe-area-inset-top, 0px));
    right: calc(1rem + env(safe-area-inset-right, 0px));
    display: flex;
    gap: 0.5rem;
    z-index: 40;
}
.ui-button {
    background: rgba(0, 0, 0, 0.7);
    border: 1px solid rgba(255, 107, 107, 0.5);
    color: #ff6b6b;
    padding: 0.7rem;
    cursor: pointer;
    border-radius: 50%;
    transition: all 0.3s ease;
    display: flex;
    align-items: center;
    justify-content: center;
}
.ui-button:hover { background: rgba(255, 107, 107, 0.2); box-shadow: 0 0 15px rgba(255, 107, 107, 0.3); }

/* メニュー画面 */
.menu-overlay {
    background: rgba(0, 0, 0, 0.9);
    display: flex;
    align-items: center;
    justify-content: center;
    backdrop-filter: blur(10px);
    -webkit-backdrop-filter: blur(10px);
}
.menu-content {
    background: rgba(45, 27, 61, 0.9);
    border: 2px solid rgba(255, 107, 107, 0.5);
    border-radius: 20px;
    padding: clamp(2rem, 5vw, 3rem);
    text-align: center;
    min-width: 300px;
}
.menu-content h2 {
    color: #ff6b6b;
    margin-bottom: 2rem;
    font-size: clamp(1.5rem, 4vw, 2rem);
    text-shadow: 0 0 10px rgba(255, 107, 107, 0.5);
}
.menu-button {
    display: block;
    width: 100%;
    background: rgba(255, 107, 107, 0.1);
    border: 1px solid rgba(255, 107, 107, 0.5);
    color: #fff;
    padding: clamp(0.8rem, 2.5vw, 1rem);
    margin-bottom: 0.8rem;
    font-size: clamp(0.9rem, 2.5vw, 1rem);
    cursor: pointer;
    border-radius: 10px;
    transition: all 0.3s ease;
    font-family: inherit;
}
.menu-button:hover { background: rgba(255, 107, 107, 0.3); box-shadow: 0 0 15px rgba(255, 107, 107, 0.2); }

.choice-appear { animation: choiceAppear 0.5s ease-out forwards; }
@keyframes choiceAppear { from { opacity: 0; transform: translateY(30px) scale(0.9); } to { opacity: 1; transform: translateY(0) scale(1); } }

.choice-item.is-remembered::after {
    content: ' 記憶あり';
    font-size: 0.7em;
    color: rgba(200, 170, 255, 0.8);
}

.backlog-panel {
    position: absolute;
    inset: calc(3.5rem + env(safe-area-inset-top, 0px)) 4% calc(2rem + env(safe-area-inset-bottom, 0px));
    z-index: 55;
    background: rgba(8, 0, 16, 0.94);
    border: 1px solid rgba(255, 107, 107, 0.35);
    border-radius: 14px;
    padding: 1rem;
    display: flex;
    flex-direction: column;
    touch-action: pan-y;
}
.backlog-head {
    color: #ffb0b0;
    margin-bottom: 0.6rem;
    font-size: 1.05rem;
}
.backlog-list {
    flex: 1;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
    touch-action: pan-y;
    padding-right: 0.4rem;
}
.backlog-entry {
    margin-bottom: 0.85rem;
    line-height: 1.5;
}
.backlog-name {
    color: #ff8a8a;
    font-size: 0.85rem;
    margin-bottom: 0.15rem;
}
.backlog-body {
    color: #eee;
    font-size: 0.92rem;
}
.backlog-close {
    margin-top: 0.8rem;
    width: 100%;
    background: rgba(255, 107, 107, 0.15);
    border: 1px solid rgba(255, 107, 107, 0.5);
    color: #fff;
    border-radius: 10px;
    font-family: inherit;
}

.ending-gate {
    position: absolute;
    inset: 0;
    z-index: 60;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: flex-end;
    padding-bottom: calc(18vh + env(safe-area-inset-bottom, 0px));
    background: linear-gradient(180deg, rgba(0, 0, 0, 0.05), rgba(0, 0, 0, 0.55));
}
.ending-gate-label {
    color: #ddd;
    margin-bottom: 1rem;
    letter-spacing: 0.08em;
}
.ui-button {
    min-width: 56px;
    min-height: 56px;
    font-size: 0.78rem;
}

@media (prefers-reduced-motion: reduce) {
    .scene-bg,
    .game-title,
    .choice-appear,
    .shaking-text,
    .noise-overlay {
        animation: none !important;
        transition: none !important;
    }
}

/* レスポンシブ */
@media (max-width: 768px) {
    .text-window { height: 40%; padding: 1rem; }
    .game-ui { top: 0.5rem; right: 0.5rem; gap: 0.3rem; }
    .ui-button { padding: 0.5rem; }
}
@media (max-width: 480px) {
    .choices-wrapper { gap: 0.8rem; }
    .choice-item { padding: 1rem 0.8rem; }
    .text-window { height: 45%; }
}
