:root {
    --primary-color: #e0e0e0;
    --accent-color: #81d4fa;
    --dark-bg: #121212;
    --dialog-bg: rgba(0, 0, 0, 0.85);
    /* Safe areas for iOS notch */
    --safe-top: max(env(safe-area-inset-top), 10px);
    --safe-bottom: max(env(safe-area-inset-bottom), 10px);
    --safe-left: max(env(safe-area-inset-left), 10px);
    --safe-right: max(env(safe-area-inset-right), 10px);
}

/* Font */
:root {
    --font-main: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
    --color-bg: #121212;
    --color-text: #e0e0e0;
    --color-accent: #81d4fa;
}

/* Avatar Styles */
#char-avatar {
    position: absolute;
    top: -50px;
    left: 10px;
    width: 60px;
    height: 60px;
    border-radius: 50%;
    border: 3px solid var(--color-accent);
    background-color: #333;
    background-size: cover;
    background-repeat: no-repeat;
    background-position: top center;
    box-shadow: 0 0 15px rgba(0, 0, 0, 0.8);
    z-index: 20;
    transition: background-image 0.3s;
    overflow: hidden;
    background-image: url('assets/images/chars/unknown.png');
    pointer-events: none;
    /* fallback */
}

#char-avatar.hidden {
    display: none;
}

* {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
    -webkit-touch-callout: none !important;
    -webkit-user-select: none !important;
    -khtml-user-select: none !important;
    -moz-user-select: none !important;
    -ms-user-select: none !important;
    user-select: none !important;
    -webkit-tap-highlight-color: transparent !important;
    -webkit-user-drag: none !important;
    touch-action: pan-y;
    /* Optionally restricts touch to vertical scroll only, but let's keep it normal */
}

/* Hard block for dragging universally */
img,
canvas,
div {
    -webkit-user-drag: none;
}

body {
    background-color: #000;
    color: var(--color-text);
    font-family: var(--font-main);
    overflow: hidden;
    width: 100vw;
    height: 100vh;
    /* Mobile Scroll Fixes */
    position: fixed;
    overscroll-behavior: none;
    -webkit-overflow-scrolling: auto;
}

html {
    overflow: hidden;
    position: fixed;
    width: 100%;
    height: 100%;
    overscroll-behavior: none;
}

#game-container {
    position: relative;
    width: 100%;
    height: 100%;
    overflow: hidden;
    background: #1a1a1a;
}

/* Screens */
.screen {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    opacity: 0;
    pointer-events: none;
    transition: opacity 0.5s ease;
    padding: var(--safe-top) var(--safe-right) var(--safe-bottom) var(--safe-left);
}

.screen.active {
    opacity: 1;
    pointer-events: all;
}

.hidden {
    display: none !important;
}

/* Menu */
.title {
    font-size: 3rem;
    margin-bottom: 2rem;
    text-shadow: 0 0 10px var(--color-accent);
    text-align: center;
}

.menu-buttons {
    display: flex;
    flex-direction: column;
    gap: 1rem;
    width: 200px;
}

.btn {
    padding: 1rem;
    background: rgba(255, 255, 255, 0.1);
    border: 1px solid var(--color-accent);
    color: white;
    font-size: 1.2rem;
    cursor: pointer;
    transition: background 0.2s, transform 0.1s;
}

.btn:hover {
    background: rgba(129, 212, 250, 0.2);
}

.btn:active {
    transform: scale(0.95);
}

/* Gameplay */
#screen-game {
    justify-content: flex-end;
    /* Align dialog to bottom */
}

.layer {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background-size: cover;
    background-position: center;
    pointer-events: none;
    /* Prevents interaction with background layers */
}

#so-character {
    background-size: auto 70%;
    /* VN-style: character takes most of screen above dialog */
    background-repeat: no-repeat;
    background-position: center bottom;
    /* Centered, aligned to bottom of character layer */
    pointer-events: none;
    transition: background-image 0.3s ease-in-out;
    /* Smooth transition */
}

#ui-overlay {
    position: absolute;
    width: 100%;
    height: 100%;
    pointer-events: none;
}

#stats-panel {
    position: absolute;
    top: calc(10px + var(--safe-top));
    left: calc(10px + var(--safe-left));
    background: rgba(0, 0, 0, 0.5);
    padding: 5px 10px;
    border-radius: 5px;
    font-size: 1.2rem;
    color: #ffeb3b;
    /* Energy color */
}

#dialog-box {
    position: absolute;
    bottom: calc(20px + var(--safe-bottom));
    left: calc(5% + var(--safe-left));
    right: calc(5% + var(--safe-right));
    width: auto;
    /* Auto width based on left/right */
    min-height: 120px;
    max-height: 180px;
    background: rgba(0, 0, 0, 0.85);
    border: 2px solid var(--primary-color);
    border-top: 2px solid var(--color-accent);
    border-radius: 10px;
    padding: 15px 20px;
    pointer-events: all;
    display: flex;
    flex-direction: column;
    justify-content: flex-start;
    z-index: 5;
}

#char-name {
    color: var(--color-accent);
    font-weight: bold;
    margin-bottom: 5px;
    font-size: 1.1rem;
    margin-left: 40px;
    /* Shift name to right to clear avatar */
}

#dialog-text {
    font-size: 1.1rem;
    line-height: 1.4;
    flex-grow: 1;
}

.btn-next {
    align-self: flex-end;
    background: none;
    border: none;
    color: var(--accent-color);
    font-size: 1.5rem;
    cursor: pointer;
    animation: bounce 1s infinite;
}

@keyframes bounce {

    0%,
    100% {
        transform: translateY(0);
    }

    50% {
        transform: translateY(5px);
    }
}

/* Choices */
#choices-container {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(0, 0, 0, 0.7);
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 1.5rem;
    pointer-events: all;
    z-index: 10;
}

#choices-container .btn {
    width: 80%;
    max-width: 400px;
    background: #2a2a2a;
    border-color: #fff;
}

/* Minigame */
#screen-minigame {
    background: #111;
    z-index: 20;
}

#minigame-content {
    width: 100%;
    height: 100%;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    overflow-y: auto;
    overflow-x: hidden;
    -webkit-overflow-scrolling: touch;
    padding: 10px;
    box-sizing: border-box;
}

/* Mobile Adjustments - Phones Portrait */
@media (max-width: 480px) {
    .title {
        font-size: 1.8rem;
        padding: 0 10px;
    }

    #dialog-box {
        bottom: calc(5px + var(--safe-bottom));
        left: calc(3% + var(--safe-left));
        right: calc(3% + var(--safe-right));
        width: auto;
        min-height: 100px;
        max-height: 30vh;
        /* Dynamic height */
        padding: 12px 15px;
        font-size: 0.95rem;
    }

    #char-name {
        font-size: 1rem;
        margin-left: 0;
    }

    #dialog-text {
        font-size: 0.95rem;
        line-height: 1.3;
    }

    .btn {
        font-size: 1rem;
        padding: 0.8rem;
        min-height: 44px;
        /* iOS touch target */
    }

    #so-character {
        background-size: auto 60%;
    }

    #char-avatar {
        width: 50px;
        height: 50px;
        top: -40px;
    }

    #choices-container .btn {
        width: 90%;
        padding: 1rem;
    }

    .minigame-modal h2 {
        font-size: 1.8rem;
    }

    .minigame-modal button {
        padding: 0.8rem 2rem;
        font-size: 1.2rem;
    }
}

/* Tablets */
@media (min-width: 481px) and (max-width: 768px) {
    .title {
        font-size: 2.5rem;
    }

    #dialog-box {
        bottom: calc(15px + var(--safe-bottom));
        min-height: 130px;
        max-height: 170px;
    }

    #so-character {
        background-size: auto 65%;
    }
}

/* Landscape Mode - Phones */
@media (max-height: 500px) and (orientation: landscape) {
    #dialog-box {
        bottom: calc(5px + var(--safe-bottom));
        left: calc(15% + var(--safe-left));
        right: calc(15% + var(--safe-right));
        width: auto;
        min-height: 80px;
        max-height: 120px;
        padding: 10px 15px;
    }

    #so-character {
        background-size: auto 85%;
        background-position: center bottom;
    }

    .title {
        font-size: 1.5rem;
        margin-bottom: 1rem;
    }

    .menu-buttons {
        flex-direction: row;
        flex-wrap: wrap;
        width: auto;
        gap: 0.5rem;
    }

    .btn {
        padding: 0.6rem 1rem;
    }

    #choices-container {
        flex-direction: row;
        flex-wrap: wrap;
        gap: 0.8rem;
    }

    #choices-container .btn {
        width: 45%;
    }
}

/* Touch improvements for all mobile */
@media (pointer: coarse) {
    .btn {
        min-height: 44px;
        min-width: 44px;
    }

    .btn-next {
        padding: 10px;
        font-size: 2rem;
    }
}

/* Special Effects & Minigame Modal */
#so-character.center {
    background-position: center center !important;
    background-size: auto 60% !important;
    filter: drop-shadow(0 0 15px rgba(0, 255, 255, 0.7));
}

.minigame-modal {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(0, 0, 0, 0.9);
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    z-index: 9999;
    animation: fadeIn 0.5s ease-out;
}

.minigame-modal h2 {
    color: #81d4fa;
    font-size: 2.5rem;
    text-shadow: 0 0 20px #81d4fa;
    margin-bottom: 2rem;
    text-align: center;
}

.minigame-modal button {
    padding: 1rem 3rem;
    font-size: 1.5rem;
    color: white;
    background: transparent;
    border: 2px solid #81d4fa;
    cursor: pointer;
    transition: all 0.3s ease;
    box-shadow: 0 0 15px rgba(129, 212, 250, 0.3);
}

.minigame-modal button:hover {
    background: #81d4fa;
    color: #000;
    box-shadow: 0 0 30px #81d4fa;
}

@keyframes fadeIn {
    from {
        opacity: 0;
        transform: scale(0.9);
    }

    to {
        opacity: 1;
        transform: scale(1);
    }
}

/* Settings Panel */
#screen-settings {
    background: rgba(0, 0, 0, 0.95);
    z-index: 100;
}

.settings-panel {
    background: rgba(30, 30, 40, 0.95);
    border: 2px solid var(--color-accent);
    border-radius: 15px;
    padding: 2rem;
    min-width: 300px;
    max-width: 400px;
    width: 90%;
    box-shadow: 0 0 30px rgba(129, 212, 250, 0.3);
    /* Mobile Fit Fixes */
    max-height: 85vh;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
}

.settings-panel h2 {
    color: var(--color-accent);
    text-align: center;
    margin-bottom: 1.5rem;
    font-size: 1.8rem;
    text-shadow: 0 0 10px var(--color-accent);
}

.setting-row {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 1rem;
    margin-bottom: 1.2rem;
    padding: 0.8rem;
    background: rgba(255, 255, 255, 0.05);
    border-radius: 8px;
}

.setting-row label {
    flex: 1;
    color: var(--color-text);
    font-size: 1rem;
}

.setting-row input[type="range"] {
    flex: 1.5;
    -webkit-appearance: none;
    appearance: none;
    height: 8px;
    background: rgba(255, 255, 255, 0.2);
    border-radius: 4px;
    outline: none;
}

.setting-row input[type="range"]::-webkit-slider-thumb {
    -webkit-appearance: none;
    appearance: none;
    width: 20px;
    height: 20px;
    background: var(--color-accent);
    border-radius: 50%;
    cursor: pointer;
    box-shadow: 0 0 10px var(--color-accent);
}

.setting-row input[type="range"]::-moz-range-thumb {
    width: 20px;
    height: 20px;
    background: var(--color-accent);
    border-radius: 50%;
    cursor: pointer;
    border: none;
}

.setting-row input[type="checkbox"] {
    width: 24px;
    height: 24px;
    accent-color: var(--color-accent);
    cursor: pointer;
}

.setting-row span {
    min-width: 45px;
    text-align: right;
    color: var(--color-accent);
    font-weight: bold;
}

.setting-row select {
    flex: 1;
    padding: 0.5rem;
    background: rgba(255, 255, 255, 0.1);
    border: 1px solid var(--color-accent);
    border-radius: 5px;
    color: var(--color-text);
    font-size: 1rem;
    cursor: pointer;
}

.setting-row select option {
    background: #1a1a2e;
    color: var(--color-text);
}

.settings-panel .btn {
    width: 100%;
    margin-top: 1rem;
}

/* Mobile adjustments for settings */
@media (max-width: 480px) {
    .settings-panel {
        padding: 1.5rem;
        min-width: auto;
    }

    .settings-panel h2 {
        font-size: 1.4rem;
    }

    .setting-row {
        flex-wrap: wrap;
        gap: 0.5rem;
    }

    .setting-row label {
        width: 100%;
        margin-bottom: 0.3rem;
    }
}

.version {
    position: absolute;
    bottom: 10px;
    right: 15px;
    color: rgba(255, 255, 255, 0.3);
    font-size: 0.8rem;
}