:root {
    --primary: #4f46e5;
    --primary-hover: #4338ca;
    --danger: #ef4444;
    --gold: #fbbf24;
    --bg-dark: #111827;
    --panel-bg: rgba(31, 41, 55, 0.95);
    --text-light: #f3f4f6;
}
::-webkit-scrollbar { width: 6px; height: 6px; }
::-webkit-scrollbar-track { background: rgba(0, 0, 0, 0.2); border-radius: 4px; }
::-webkit-scrollbar-thumb { background: #4b5563; border-radius: 4px; }

body {
    margin: 0; padding: 0;
    font-family: 'Malgun Gothic', 'Apple SD Gothic Neo', sans-serif;
    background-color: #000; color: var(--text-light);
    display: flex; justify-content: center; align-items: center;
    height: 100dvh; overflow: hidden; box-sizing: border-box;
}

#game-wrapper {
    position: relative; width: 100%; max-width: 900px; height: 100%;
    background: linear-gradient(to bottom, #0f172a 0%, #1e1b4b 70%, #064e3b 100%);
    overflow: hidden; box-shadow: 0 0 20px rgba(0,0,0,0.8);
}

/* 상단 UI 바 */
#top-bar {
    position: absolute; top: 0; left: 0; width: 100%; padding: 8px 10px;
    display: flex; justify-content: space-between; align-items: center;
    box-sizing: border-box; background: rgba(0,0,0,0.85);
    z-index: 20; font-weight: bold; font-size: 0.9rem; text-shadow: 1px 1px 2px black;
}

.top-bar-left { display: flex; gap: 8px; align-items: center; }
.top-bar-right { display: flex; gap: 6px; align-items: center; flex-wrap: wrap; justify-content: flex-end; }

.stat-group { display: flex; align-items: center; gap: 4px; }
.hp-container {
    width: 90px; height: 16px; background-color: rgba(0,0,0,0.5);
    border: 2px solid white; border-radius: 8px; overflow: hidden; position: relative;
}
#hp-bar { width: 100%; height: 100%; background: linear-gradient(90deg, #ef4444, #f87171); transition: width 0.3s ease-out; }

#game-area { position: absolute; top: 0; left: 0; width: 100%; height: calc(100% - 170px); }

#time-slow-overlay {
    position: absolute; top: 0; left: 0; width: 100%; height: 100%;
    background: rgba(59, 130, 246, 0.2); z-index: 1; pointer-events: none; display: none;
    box-shadow: inset 0 0 50px rgba(59, 130, 246, 0.5);
}

#pause-overlay {
    position: absolute; top: 0; left: 0; width: 100%; height: 100%;
    background: rgba(0,0,0,0.6); z-index: 45; display: none;
    justify-content: center; align-items: center; flex-direction: column;
    color: white; backdrop-filter: blur(4px);
}
#pause-overlay h2 { font-size: 2.5rem; color: var(--gold); margin: 0 0 20px 0; text-shadow: 2px 2px 4px #000; }

.boss-warning {
    position: absolute; top: 30%; left: 50%; transform: translate(-50%, -50%);
    color: red; font-size: 3.5rem; font-weight: 900;
    text-shadow: 0 0 20px rgba(255,0,0,0.8), 2px 2px 5px black;
    z-index: 100; pointer-events: none; display: none;
    animation: flashWarning 0.8s infinite; white-space: nowrap;
}
@keyframes flashWarning { 0%, 100% { opacity: 1; transform: translate(-50%, -50%) scale(1); } 50% { opacity: 0.3; transform: translate(-50%, -50%) scale(1.1); } }

.castle { position: absolute; left: 20px; bottom: 20px; font-size: 70px; z-index: 10; filter: drop-shadow(2px 4px 6px rgba(0,0,0,0.5)); transition: transform 0.1s; }
.archer { position: absolute; left: 70px; bottom: 80px; font-size: 35px; z-index: 11; transition: transform 0.1s; }

.equipped-char {
    position: absolute; z-index: 12;
    background-size: contain; background-repeat: no-repeat; background-position: center bottom;
    filter: drop-shadow(2px 2px 4px rgba(0,0,0,0.5)); transition: transform 0.2s; pointer-events: none;
}

.monster { 
    position: absolute; bottom: 20px; z-index: 5; width: 80px; height: 80px;
    background-size: contain; background-repeat: no-repeat; background-position: center bottom;
    transition: filter 0.2s; filter: drop-shadow(2px 2px 4px rgba(0,0,0,0.5));
}
.monster.boss { width: 130px; height: 130px; z-index: 6; filter: drop-shadow(0 0 10px red); }
.monster-hp-bg {
    position: absolute; top: -10px; left: 50%; transform: translateX(-50%);
    width: 80%; height: 6px; background: rgba(0,0,0,0.7);
    border: 1px solid #fff; border-radius: 3px; display: none; z-index: 20;
}
.monster-hp-fill { width: 100%; height: 100%; background: #ef4444; border-radius: 2px; transition: width 0.1s; }
.arrow { position: absolute; font-size: 24px; z-index: 12; pointer-events: none; }

#skill-bar {
    position: absolute; bottom: 170px; left: 0; width: 100%;
    display: flex; justify-content: center; gap: 12px; padding: 10px 0; box-sizing: border-box;
    background: linear-gradient(to top, rgba(0,0,0,0.8) 0%, transparent 100%); z-index: 15; display: none;
}
.skill-btn {
    background: #374151; border: 2px solid #4b5563; border-radius: 50%; width: 45px; height: 45px; font-size: 1.3rem;
    display: flex; justify-content: center; align-items: center; position: relative; cursor: pointer; color: white;
    box-shadow: 0 4px 6px rgba(0,0,0,0.5); user-select: none; -webkit-tap-highlight-color: transparent;
}
.skill-btn:active { transform: scale(0.9); }
.skill-badge { position: absolute; top: -5px; right: -5px; background: #ef4444; color: white; font-size: 0.7rem; font-weight: bold; padding: 2px 6px; border-radius: 10px; border: 1px solid #111; }
#btn-skill-grace { border-color: var(--gold); overflow: hidden; }
#grace-fill { position: absolute; bottom: 0; left: 0; width: 100%; background: rgba(251, 191, 36, 0.5); height: 0%; transition: height 0.3s; z-index: 0; }
#btn-skill-grace span { position: relative; z-index: 1; }

#quiz-panel {
    position: absolute; bottom: 0; left: 0; width: 100%; height: 170px; background: var(--panel-bg); border-top: 3px solid #374151;
    display: flex; flex-direction: column; align-items: center; justify-content: center; padding: 10px; box-sizing: border-box; z-index: 20; backdrop-filter: blur(5px); transition: border-color 0.3s;
}
#verse-display { font-size: 1rem; line-height: 1.4; text-align: center; margin-bottom: 8px; padding: 0 10px; width: 100%; box-sizing: border-box; min-height: 45px; max-height: 65px; overflow-y: auto; color: var(--gold); font-weight: 500; word-break: keep-all; }
.hint-text { color: #10b981; font-weight: bold; font-size: 1rem; margin-bottom: 8px; text-shadow: 1px 1px 2px black; }
.controls { display: flex; gap: 8px; align-items: center; justify-content: center; width: 100%; flex-wrap: wrap; }
.chapter-badge { background-color: var(--primary); color: white; padding: 8px 12px; border-radius: 8px; font-weight: bold; font-size: 1.1rem; border: 2px solid #6366f1; box-shadow: 0 0 10px rgba(79, 70, 229, 0.4); white-space: nowrap; }
.number-input { width: 120px; padding: 8px 5px; font-size: 1.1rem; font-weight: bold; background-color: #374151; color: white; border: 2px solid #4b5563; border-radius: 8px; text-align: center; outline: none; transition: all 0.2s; }
.number-input:focus { border-color: var(--primary); box-shadow: 0 0 10px rgba(79, 70, 229, 0.6); background-color: #1f2937; }
input::-webkit-outer-spin-button, input::-webkit-inner-spin-button { -webkit-appearance: none; margin: 0; }
.controls span { font-size: 1rem; font-weight: bold; }

button { background-color: var(--primary); color: white; border: none; padding: 8px 15px; font-size: 1rem; font-weight: bold; border-radius: 8px; cursor: pointer; transition: background-color 0.2s, transform 0.1s; box-shadow: 0 4px 6px rgba(0,0,0,0.3); white-space: nowrap; }
button:hover { background-color: var(--primary-hover); filter: brightness(1.1); }
button:active { transform: translateY(2px); box-shadow: 0 1px 2px rgba(0,0,0,0.3); }
.btn-pause, .btn-quit { padding: 4px 8px; font-size: 0.8rem; margin-left: 2px; }
.btn-pause { background-color: #f59e0b; } .btn-pause:hover { background-color: #d97706; }
.btn-quit { background-color: #ef4444; }

.speed-btn { background-color: #374151; color: #fff; padding: 4px 8px; font-size: 0.85rem; border-radius: 4px; margin-left: 2px; }
.speed-btn.active { background-color: var(--primary); font-weight: bold; border: 1px solid #fff; }

.screen { 
    position: absolute; top: 0; left: 0; width: 100%; height: 100%; 
    background: rgba(0,0,0,0.9); 
    display: flex; flex-direction: column; 
    justify-content: flex-start; 
    align-items: center; z-index: 50; backdrop-filter: blur(4px); 
    padding: 40px 15px; box-sizing: border-box; overflow-y: auto; overflow-x: hidden; 
}
.screen::before, .screen::after { content: ''; margin: auto; }

.screen h1 { font-size: 2.2rem; margin-top: 0; margin-bottom: 15px; text-align: center; background: linear-gradient(to right, #fbbf24, #f59e0b); -webkit-background-clip: text; -webkit-text-fill-color: transparent; line-height: 1.2; }
.stage-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(70px, 1fr)); gap: 8px; margin: 15px 0; width: 100%; max-width: 450px; }
.stage-btn { background: #1f2937; border: 2px solid #4b5563; color: white; padding: 12px 0; font-size: 1rem; font-weight: bold; border-radius: 10px; cursor: pointer; transition: all 0.2s; }
.stage-btn:hover:not(.locked) { background: var(--primary); transform: scale(1.05); }
.stage-btn.locked { background: #111; color: #555; border-color: #333; cursor: not-allowed; }

.stage-btn.infinity { background: linear-gradient(45deg, #b91c1c, #ef4444); border-color: #fca5a5; color: white; animation: pulseRed 2s infinite; }
@keyframes pulseRed { 0% { box-shadow: 0 0 0 0 rgba(239, 68, 68, 0.7); } 70% { box-shadow: 0 0 0 10px rgba(239, 68, 68, 0); } 100% { box-shadow: 0 0 0 0 rgba(239, 68, 68, 0); } }

.settings-panel { background: rgba(0, 0, 0, 0.5); padding: 15px; border-radius: 12px; width: 100%; max-width: 450px; border: 1px solid #4b5563; }
.setting-group { margin-bottom: 12px; } .setting-group:last-child { margin-bottom: 0; }
.setting-group h3 { margin: 0 0 8px 0; color: var(--primary); font-size: 0.95rem; text-align: center; }
.radio-group { display: flex; justify-content: center; background: #1f2937; padding: 8px; border-radius: 8px; flex-wrap: wrap; gap: 8px; }
.radio-group label { cursor: pointer; font-size: 0.85rem; display: flex; align-items: center; gap: 4px; }
select.custom-sel { width: 100%; padding: 8px; font-size: 0.9rem; background-color: #1f2937; color: white; border: 1px solid #4b5563; border-radius: 8px; }
select.custom-sel:disabled { opacity: 0.5; cursor: not-allowed; background-color: #111; }

.combo-text { position: absolute; color: var(--gold); font-weight: 900; font-size: 35px; text-shadow: 2px 2px 0 #000, -2px -2px 0 #000, 2px -2px 0 #000, -2px 2px 0 #000; pointer-events: none; z-index: 40; animation: comboPop 0.8s ease-out forwards; }
@keyframes comboPop { 0% { transform: scale(0.3) rotate(-15deg); opacity: 0; } 40% { transform: scale(1.3) rotate(5deg); opacity: 1; } 100% { transform: scale(1) rotate(0deg); opacity: 0; top: -30px; } }
.floating-text { position: absolute; color: red; font-weight: bold; font-size: 20px; pointer-events: none; animation: floatUp 1s ease-out forwards; z-index: 30; text-shadow: 1px 1px 2px black;}
@keyframes floatUp { 0% { opacity: 1; transform: translateY(0) scale(1); } 100% { opacity: 0; transform: translateY(-40px) scale(1.5); } }
@keyframes shake { 0%, 100% { transform: translateX(0); } 25%, 75% { transform: translateX(-5px); } 50% { transform: translateX(5px); } }
.shake { animation: shake 0.3s; }
.hidden { display: none !important; }

.shop-content { background: #1f2937; padding: 20px; border-radius: 12px; border: 2px solid var(--gold); width: 100%; max-width: 450px; text-align: center; box-sizing: border-box; }
.shop-item { background: #111827; border: 1px solid #4b5563; padding: 12px; border-radius: 8px; margin-bottom: 10px; display: flex; justify-content: space-between; align-items: center; gap: 10px; }
.shop-item-info { text-align: left; flex: 1; }
.shop-item-title { font-weight: bold; font-size: 0.95rem; color: #fff; margin-bottom: 4px; }
.shop-item-desc { font-size: 0.75rem; color: #9ca3af; line-height: 1.3; }
.btn-buy { background: var(--gold); color: #000; padding: 6px 10px; border-radius: 5px; font-weight: bold; font-size: 0.85rem; min-width: 60px; flex-shrink: 0;}
.btn-buy.energy-buy { background: #60a5fa; color: #fff; }
.btn-buy:disabled { background: #4b5563; color: #9ca3af; cursor: not-allowed; }

.dex-stats { display: flex; justify-content: center; gap: 20px; margin-bottom: 15px; color: #fff; font-weight: bold; background: #111; padding: 10px; border-radius: 8px; border: 1px solid #4b5563;}
.dex-list { display: flex; flex-direction: column; gap: 8px; text-align: left; max-height: 50vh; overflow-y: auto; padding-right: 5px; }
.dex-item { background: #111; border: 1px solid #374151; padding: 10px; border-radius: 8px; font-size: 0.85rem; position: relative;}
.dex-item.unlocked { border-color: var(--gold); }
.dex-item.unlocked .dex-chapter { color: var(--gold); font-weight: bold; margin-bottom: 4px; display: inline-block; }
.dex-item.unlocked .dex-text { color: #f3f4f6; line-height: 1.4; word-break: keep-all; display: block;}
.dex-count-badge { float: right; background: var(--gold); color: #000; padding: 2px 6px; border-radius: 4px; font-size: 0.7rem; font-weight: bold; }
.dex-item.locked { background: #000; filter: grayscale(100%); }
.dex-item.locked .dex-chapter { color: #4b5563; font-weight: bold; margin-bottom: 4px; display: block; }
.dex-item.locked .dex-text { color: #374151; text-shadow: none; font-style: italic; }

.wrong-notes-container { margin-top: 15px; text-align: left; background: rgba(0,0,0,0.5); padding: 10px; border-radius: 8px; border: 1px solid #ef4444; max-height: 150px; overflow-y: auto; width: 100%; box-sizing: border-box;}
.wrong-note-item { color: #fca5a5; font-size: 0.85rem; margin-bottom: 8px; padding-bottom: 8px; border-bottom: 1px solid rgba(239, 68, 68, 0.3); line-height: 1.3;}
.wrong-note-item:last-child { margin-bottom: 0; padding-bottom: 0; border-bottom: none; }
.wrong-note-badge { display: inline-block; background: #ef4444; color: white; padding: 2px 6px; border-radius: 4px; font-size: 0.75rem; font-weight: bold; margin-right: 5px; }

.save-load-container { display: flex; gap: 10px; justify-content: center; margin-top: 15px; }
.btn-save { background: #10b981; } .btn-load { background: #3b82f6; }

.hero-card { background: #111827; border: 2px solid #4b5563; padding: 10px; border-radius: 8px; display: flex; flex-direction: column; align-items: center; text-align: center; }
.hero-card.owned { border-color: var(--gold); }
.hero-card.equipped { border-color: #10b981; box-shadow: 0 0 10px rgba(16, 185, 129, 0.5); }
.hero-icon { width: 45px; height: 45px; background-color: #374151; border-radius: 50%; margin-bottom: 8px; background-size: cover; background-position: center; }
.hero-card.unowned .hero-icon { filter: grayscale(100%) opacity(0.5); }

#auto-save-notice { position: absolute; top: 50px; right: 10px; background: rgba(16, 185, 129, 0.8); color: white; padding: 5px 10px; border-radius: 5px; font-size: 0.8rem; font-weight: bold; opacity: 0; transition: opacity 0.5s; z-index: 100; pointer-events: none; }

/* 모바일 최적화 */
@media (min-width: 901px) { body { padding: 20px 0; } #game-wrapper { border-radius: 12px; height: 90vh; } }

@media (max-width: 480px) {
    #top-bar { flex-direction: column; align-items: stretch; padding: 6px 10px; gap: 6px; }
    .top-bar-left, .top-bar-right { width: 100%; justify-content: space-between; }
    
    .hp-container { width: 55px; height: 12px; }
    .speed-btn { padding: 4px 6px; font-size: 0.75rem; }
    .btn-pause, .btn-quit { padding: 4px 6px; font-size: 0.75rem; margin-left: 2px; }
    #in-game-stats { font-size: 0.75rem; }
    
    .castle { font-size: 55px; left: 10px; bottom: 20px; } 
    .archer { font-size: 30px; left: 55px; bottom: 70px; }
    .equipped-char { transform: scale(0.8); transform-origin: bottom left; left: 15px !important; bottom: 100px !important;}
    .monster { width: 65px; height: 65px; } 
    .monster.boss { width: 100px; height: 100px; }
    
    .chapter-badge { padding: 6px 10px; font-size: 1rem; } 
    .number-input { width: 90px; padding: 6px; }
    #quiz-panel { height: 150px; }
    #skill-bar { bottom: 150px; gap: 8px; }
    #game-area { height: calc(100% - 150px); }
    .skill-btn { width: 40px; height: 40px; font-size: 1.1rem; }
    
    .hero-grid { grid-template-columns: repeat(2, 1fr); }
    .screen { padding: 30px 10px; }
}

@media screen and (max-height: 600px) and (orientation: landscape) {
    #top-bar { flex-direction: row; padding: 5px 10px; flex-wrap: nowrap; }
    .top-bar-left, .top-bar-right { width: auto; justify-content: flex-start; gap: 6px; }
    .hp-container { width: 60px; height: 10px; }
    
    #quiz-panel { height: 60px; flex-direction: row; padding: 4px 10px; justify-content: space-between; }
    #verse-display { flex: 1; text-align: left; max-height: 50px; margin-bottom: 0; padding: 0 10px 0 0; font-size: 0.85rem; }
    .hint-text { margin-bottom: 0; font-size: 0.8rem; margin-right: 10px; }
    .controls { flex: 0 0 auto; width: auto; gap: 5px; }
    .chapter-badge { padding: 4px 8px; font-size: 0.9rem; }
    .number-input { width: 90px; padding: 4px; font-size: 0.9rem;}
    button { padding: 4px 10px; font-size: 0.85rem; }
    
    #game-area { height: calc(100% - 60px); }
    
    #skill-bar { bottom: 60px; height: 45px; padding: 4px 8px; justify-content: flex-start; background: none; }
    .skill-btn { width: 32px; height: 32px; font-size: 1rem; }
    .skill-badge { padding: 1px 3px; font-size: 0.6rem; }
    
    .castle { font-size: 45px; left: 10px; bottom: 10px; }
    .archer { font-size: 25px; left: 45px; bottom: 40px; }
    .equipped-char { transform: scale(0.6); transform-origin: bottom left; left: 10px !important; bottom: 65px !important;}
    .monster { width: 50px; height: 50px; bottom: 10px; }
    .monster.boss { width: 80px; height: 80px; bottom: 10px; }
    .boss-warning { font-size: 2rem; top: 40%; }
    
    .screen { padding: 20px 10px; }
    .screen h1 { font-size: 1.3rem; margin-bottom: 8px; }
    .stage-grid { grid-template-columns: repeat(6, 1fr); max-width: 600px; margin: 8px 0; gap: 5px; }
    .stage-btn { padding: 6px 0; font-size: 0.85rem; }
    .settings-panel { display: flex; gap: 10px; flex-wrap: nowrap; padding: 8px; max-width: 700px; align-items: center;}
    .setting-group { flex: 1; min-width: 100px; margin-bottom: 0; }
    .setting-group h3 { font-size: 0.85rem; margin-bottom: 4px; }
    .radio-group label { font-size: 0.75rem; }
    
    .shop-content { padding: 10px 15px; max-height: 95vh; max-width: 600px; }
    .shop-item { padding: 6px; margin-bottom: 5px; }
    #hero-grid { max-height: 60vh; grid-template-columns: repeat(4, 1fr); gap: 6px; }
    .hero-icon { width: 30px; height: 30px; }
    .dex-list { max-height: 55vh; }
}
