:root {
    --cream: #FFF5E1;
    --pink: #FFD6E0;
    --mint: #C8E6F5;
    --text: #4A4A4A;
    --accent: #FF8FA3;
    --jrpg-bg: #1a1438;
    --jrpg-border: #ffffff;
}

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

body {
    font-family: 'Pretendard', -apple-system, 'Segoe UI', sans-serif;
    background: url('/images/bg.jpg') center / cover no-repeat fixed;
    color: var(--text);
    height: 100vh;
    overflow: hidden;
}
body::before {
    content: '';
    position: fixed;
    inset: 0;
    background: rgba(255, 255, 255, 0.12);
    pointer-events: none;
    z-index: 0;
    /* 배경 은은한 호흡 (밝기 아주 천천히 변화) */
    animation: bgBreathe 10s ease-in-out infinite;
}
@keyframes bgBreathe {
    0%, 100% { background: rgba(255, 255, 255, 0.08); }
    50%      { background: rgba(255, 255, 255, 0.18); }
}

.app {
    position: relative;
    z-index: 1;
    height: 100vh;
    display: flex;
    flex-direction: column;
    padding: 1rem 1.2rem 0.5rem;
    gap: 0.8rem;
}

/* ===== 상단 타이틀 ===== */
.topbar { text-align: center; }
.title {
    font-family: 'Galmuri11', monospace;
    font-size: clamp(1.3rem, 2.8vw, 2.2rem);
    font-weight: 400;
    color: #fff;
    /* 도트체 외곽선 — 픽셀 배경 위 또렷하게 */
    text-shadow:
        -2px -2px 0 #2a2350, 2px -2px 0 #2a2350,
        -2px 2px 0 #2a2350, 2px 2px 0 #2a2350,
        0 4px 8px rgba(0, 0, 0, 0.55);
    letter-spacing: 1px;
}
.t-emoji { font-family: initial; }

/* ===== 가운데: 아바타 + 로그 ===== */
.scene {
    flex: 1;
    position: relative;
    display: flex;
    align-items: center;
    justify-content: center;
    min-height: 0;
}
.avatar-area {
    position: relative;
    display: flex;
    align-items: center;
    justify-content: center;
    min-height: 0;
}
/* 감정 이모트 (avatar-wrap은 overflow:hidden이라 바깥에 배치 → 안 잘림) */
.emote {
    position: absolute;
    top: 2%;
    right: 4%;
    font-size: clamp(2rem, 5.5vh, 3.4rem);
    z-index: 5;
    opacity: 0;
    pointer-events: none;
    filter: drop-shadow(0 2px 6px rgba(0, 0, 0, 0.5));
}
.emote.show { animation: emotePop 1.7s ease-out forwards; }
@keyframes emotePop {
    0%   { opacity: 0; transform: translateY(12px) scale(0.4) rotate(-8deg); }
    18%  { opacity: 1; transform: translateY(-8px) scale(1.18) rotate(7deg); }
    32%  { opacity: 1; transform: translateY(-2px) scale(1)    rotate(0); }
    72%  { opacity: 1; transform: translateY(-6px) scale(1); }
    100% { opacity: 0; transform: translateY(-22px) scale(0.9); }
}
/* ===== 후광(aura) — 아바타 뒤 파스텔 빛무리 ===== */
.avatar-glow {
    position: absolute;
    top: 50%; left: 50%;
    width: clamp(360px, 66vh, 660px);
    height: clamp(360px, 66vh, 660px);
    transform: translate(-50%, -50%);
    border-radius: 50%;
    z-index: 0;
    pointer-events: none;
    background: radial-gradient(circle,
        rgba(255, 214, 224, 0.55) 0%,
        rgba(200, 230, 245, 0.35) 38%,
        rgba(232, 213, 242, 0.18) 60%,
        transparent 72%);
    filter: blur(14px);
    opacity: 0.55;
    /* 평소: 천천히 숨쉬는 빛 */
    animation: glowBreathe 5.5s ease-in-out infinite;
}
@keyframes glowBreathe {
    0%, 100% { opacity: 0.45; transform: translate(-50%, -50%) scale(1); }
    50%      { opacity: 0.7;  transform: translate(-50%, -50%) scale(1.05); }
}
/* 말하는 중: 후광 강해지고 회전하는 컬러 빛 */
.avatar-glow.active {
    background: radial-gradient(circle,
        rgba(255, 143, 163, 0.65) 0%,
        rgba(255, 214, 224, 0.5) 34%,
        rgba(200, 230, 245, 0.3) 58%,
        transparent 72%);
    animation: glowPulse 1.2s ease-in-out infinite;
}
@keyframes glowPulse {
    0%, 100% { opacity: 0.6;  transform: translate(-50%, -50%) scale(1.02); }
    50%      { opacity: 0.95; transform: translate(-50%, -50%) scale(1.12); }
}

/* ===== 아바타 주변 둥둥 떠다니는 테마 이모지 ===== */
.float-emojis {
    position: absolute;
    top: 50%; left: 50%;
    width: clamp(440px, 80vh, 800px);
    height: clamp(440px, 80vh, 800px);
    transform: translate(-50%, -50%);
    z-index: 0;
    pointer-events: none;
}
.float-emojis span {
    position: absolute;
    top: 50%; left: 50%;
    font-size: clamp(1.1rem, 2.6vh, 1.8rem);
    opacity: 0;
    filter: drop-shadow(0 2px 4px rgba(0, 0, 0, 0.35));
    animation-name: orbitFloat;
    animation-timing-function: ease-in-out;
    animation-iteration-count: infinite;
    will-change: transform, opacity;
}
/* CSS 변수 --a(시작각)로 각자 다른 위치에서 바깥으로 흘러나감 */
@keyframes orbitFloat {
    0%   { opacity: 0; transform: rotate(var(--a)) translateX(var(--r0)) rotate(calc(-1 * var(--a))) scale(0.6); }
    15%  { opacity: 0.85; }
    85%  { opacity: 0.85; }
    100% { opacity: 0; transform: rotate(calc(var(--a) + 22deg)) translateX(var(--r1)) rotate(calc(-1 * var(--a) - 22deg)) scale(1); }
}
@media (prefers-reduced-motion: reduce) {
    .avatar-glow, .float-emojis span { animation: none; }
    .float-emojis span { opacity: 0; }
}

/* JRPG 대화창과 동일한 테두리 (남색 + 흰 이중선) */
.avatar-wrap {
    position: relative;
    width: clamp(300px, 56vh, 560px);
    height: clamp(300px, 56vh, 560px);
    padding: 0;
    border: 4px solid var(--jrpg-border);
    border-radius: 10px;
    box-shadow: 0 0 0 4px var(--jrpg-bg), 0 12px 32px rgba(0, 0, 0, 0.45);
    image-rendering: pixelated;
    overflow: hidden;
}
.avatar {
    width: 100%;
    height: 100%;
    object-fit: cover;
    border-radius: 3px;
    image-rendering: pixelated;
    position: relative;
    z-index: 2;
    display: block;
    transform-origin: center center;
    will-change: transform;
    /* 평소: 천천히 숨쉬는 듯한 미세 움직임 */
    animation: gunsuBreathe 4.8s ease-in-out infinite;
}
/* 말하는 중: 살짝 더 활기차게 흔들림 (입 사진 교체와 별개로 계속 움직임) */
.avatar.talking { animation: gunsuTalk 0.5s ease-in-out infinite; }

@keyframes gunsuBreathe {
    0%, 100% { transform: scale(1.03) translateY(0); }
    50%      { transform: scale(1.045) translateY(-0.8%); }
}
@keyframes gunsuTalk {
    0%   { transform: scale(1.04) translate(-0.35%, 0); }
    50%  { transform: scale(1.05) translate(0.35%, -0.7%); }
    100% { transform: scale(1.04) translate(-0.35%, 0); }
}
@media (prefers-reduced-motion: reduce) {
    .avatar, .avatar.talking { animation: none; }
}

/* ===== 배경 떠다니는 입자 ===== */
.bg-particles {
    position: fixed;
    inset: 0;
    z-index: 0;
    overflow: hidden;
    pointer-events: none;
}
.bg-particles span {
    position: absolute;
    bottom: -20px;
    border-radius: 50%;
    background: #fff;
    box-shadow: 0 0 6px rgba(255, 255, 255, 0.7);
    animation-name: floatUp;
    animation-timing-function: linear;
    animation-iteration-count: infinite;
}
@keyframes floatUp {
    0%   { transform: translateY(0) translateX(0); opacity: 0; }
    12%  { opacity: 1; }
    88%  { opacity: 1; }
    100% { transform: translateY(-104vh) translateX(14px); opacity: 0; }
}
.ring {
    position: absolute;
    inset: -10px;
    border-radius: 12px;
    border: 5px solid var(--accent);
    opacity: 0;
    transition: opacity 0.2s;
    z-index: 1;
}
.ring.active { opacity: 1; animation: pulse 1.2s ease-in-out infinite; }
@keyframes pulse {
    0%, 100% { transform: scale(1); opacity: 0.55; box-shadow: 0 0 0 0 rgba(255, 143, 163, 0.4); }
    50% { transform: scale(1.03); opacity: 1; box-shadow: 0 0 28px 6px rgba(255, 143, 163, 0.6); }
}

/* ===== 우측 대화 로그 ===== */
.logpanel {
    position: absolute;
    right: 0;
    top: 0;
    bottom: 0;
    width: clamp(240px, 24vw, 330px);
    display: flex;
    flex-direction: column;
    background: rgba(255, 255, 255, 0.82);
    border-radius: 1.2rem;
    padding: 0.8rem;
    box-shadow: 0 8px 28px rgba(0, 0, 0, 0.18);
    min-height: 0;
}
.log-title {
    font-weight: 800;
    font-size: 1.05rem;
    padding-bottom: 0.5rem;
    border-bottom: 2px dashed var(--accent);
    margin-bottom: 0.5rem;
    flex-shrink: 0;
}
.log-list {
    flex: 1;
    overflow-y: auto;
    display: flex;
    flex-direction: column;
    gap: 0.7rem;
    padding-right: 0.3rem;
}
.log-empty { opacity: 0.5; font-size: 0.9rem; text-align: center; margin-top: 2rem; }
.log-item { display: flex; flex-direction: column; gap: 0.25rem; }
.log-q, .log-a {
    font-size: 0.92rem;
    line-height: 1.45;
    padding: 0.5rem 0.7rem;
    border-radius: 0.8rem;
}
.log-q { background: var(--mint); align-self: flex-end; max-width: 90%; }
.log-a { background: var(--pink); align-self: flex-start; max-width: 95%; }

/* ===== JRPG 하단 대화박스 ===== */
.dialogbox {
    font-family: 'Galmuri11', monospace;
    background: var(--jrpg-bg);
    color: #fff;
    border: 4px solid var(--jrpg-border);
    border-radius: 10px;
    box-shadow: 0 0 0 4px var(--jrpg-bg), 0 8px 24px rgba(0, 0, 0, 0.4);
    padding: 0.9rem 1.2rem;
    min-height: 5.5rem;
    flex-shrink: 0;
}
.speaker {
    font-family: 'Galmuri11', monospace;
    color: #ffd86b;
    font-size: 1rem;
    margin-bottom: 0.4rem;
    text-shadow: 2px 2px 0 #000;
}
.dialogtext {
    font-family: 'Galmuri11', monospace;
    font-size: clamp(1rem, 2vw, 1.4rem);
    line-height: 1.55;
    text-shadow: 2px 2px 0 #000;
    word-break: keep-all;
}

/* ===== 컨트롤바 ===== */
.controlbar {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 1rem;
    flex-shrink: 0;
    flex-wrap: wrap;
}
.status {
    font-weight: 700;
    font-size: clamp(0.9rem, 1.8vw, 1.15rem);
    color: #fff;
    background: rgba(74, 74, 74, 0.6);
    padding: 0.5rem 1rem;
    border-radius: 0.9rem;
    backdrop-filter: blur(3px);
}
.buttons { display: flex; gap: 0.7rem; }
.btn {
    font-family: inherit;
    font-size: clamp(1rem, 2vw, 1.3rem);
    font-weight: 700;
    border: none;
    border-radius: 1.2rem;
    padding: 0.8rem 1.8rem;
    cursor: pointer;
    transition: transform 0.12s, box-shadow 0.12s, background 0.2s;
    box-shadow: 0 5px 16px rgba(0, 0, 0, 0.25);
}
.btn:active { transform: scale(0.95); }
.btn-primary { background: var(--accent); color: #fff; }
.btn-sos { background: #FFB300; color: #fff; }
.btn-sos.active { background: #6A4C93; color: #fff; box-shadow: 0 0 0 3px #fff, 0 5px 16px rgba(106, 76, 147, 0.5); }
.btn-talk { background: #4CAF50; color: #fff; }
.btn-talk.recording { background: #E53935; animation: rec 1s ease-in-out infinite; }
@keyframes rec { 0%, 100% { box-shadow: 0 5px 16px rgba(229, 57, 53, 0.5); } 50% { box-shadow: 0 5px 28px rgba(229, 57, 53, 0.9); } }
.btn-secondary { background: #fff; color: var(--accent); border: 2px solid var(--accent); }
.btn.busy { opacity: 0.5; pointer-events: none; }

/* ===== 푸터 ===== */
.footer {
    text-align: center;
    padding: 0.3rem;
    font-size: 0.78rem;
    color: #fff;
    text-shadow: 0 1px 4px rgba(0, 0, 0, 0.6);
    flex-shrink: 0;
}
.footer a {
    color: #fff;
    font-weight: 700;
    text-decoration: underline;
    text-underline-offset: 2px;
}
.footer a:hover { color: var(--pink); }

/* ===== 세로 화면(태블릿 세로/모바일) ===== */
@media (max-aspect-ratio: 1/1) {
    body { overflow: auto; }
    .app { height: auto; min-height: 100vh; }
    .scene { flex-direction: column; }
    .logpanel { position: static; width: 100%; max-height: 30vh; }
    .avatar-wrap { width: clamp(160px, 40vw, 260px); height: clamp(160px, 40vw, 260px); }
}
