* {
    box-sizing: border-box;
}

html {
    font-size: 16px;
    overflow-x: hidden;
}

body {
    margin: 0;
    min-height: 100vh;
    overflow-x: hidden;
    color: #17202a;
    background: #eef2f6;
    font-family: system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
    line-height: 1.6;
}

button,
textarea {
    font: inherit;
}

.character-page {
    display: flex;
    flex-direction: column;
    align-items: center;
    min-height: 100vh;
    padding: 18px 16px 22px;
    background: linear-gradient(180deg, #ffffff 0%, #edf2f7 100%);
}

.character-stage {
    width: min(100%, 760px);
    margin: 0 auto;
}

.character-visual {
    position: relative;
    display: grid;
    min-height: min(60vh, 540px);
    place-items: center;
}

.character-video {
    display: block;
    width: min(100%, 500px);
    height: min(60vh, 540px);
    object-fit: contain;
    background: transparent;
}

.video-fallback {
    display: grid;
    width: min(100%, 420px);
    min-height: min(50vh, 430px);
    place-items: center;
    padding: 24px;
    border: 1px dashed #b8c6d8;
    border-radius: 8px;
    color: #52606d;
    background: rgba(255, 255, 255, 0.68);
    text-align: center;
}

.video-fallback[hidden] {
    display: none;
}

.character-dialogue-overlay {
    position: absolute;
    left: 50%;
    bottom: 18%;
    z-index: 10;
    width: min(90vw, 560px);
    max-height: min(26vh, 190px);
    transform: translateX(-50%);
    overflow-y: auto;
    padding: 18px 22px;
    border: 1px solid rgba(185, 210, 239, 0.42);
    border-radius: 5px;
    color: #fff;
    background: rgba(52, 100, 165, 0.70);
    box-shadow: 0 6px 16px rgba(15, 45, 84, 0.14);
    font-size: 18px;
    font-weight: 600;
    line-height: 1.7;
    overflow-wrap: anywhere;
    white-space: pre-wrap;
}

.interaction-area {
    width: min(100%, 700px);
    margin: 8px auto 0;
}

.character-status {
    margin: 0 0 6px;
    color: #225481;
    font-size: 0.9rem;
    font-weight: 700;
    text-align: center;
}

.chat-form {
    width: 100%;
}

.input-row {
    display: grid;
    grid-template-columns: minmax(0, 1fr) 48px 100px;
    gap: 10px;
    align-items: center;
}

textarea {
    width: 100%;
    height: 56px;
    min-height: 48px;
    max-height: 120px;
    resize: vertical;
    padding: 12px 13px;
    border: 1px solid #c3ccd8;
    border-radius: 7px;
    color: #17202a;
    background: rgba(255, 255, 255, 0.96);
}

textarea:focus {
    border-color: #23639c;
    outline: 3px solid rgba(35, 99, 156, 0.16);
}

button {
    min-height: 48px;
    border: 0;
    cursor: pointer;
}

.mic-button {
    width: 48px;
    height: 48px;
    padding: 0;
    border-radius: 50%;
    color: #fff;
    background: #2f6fb0;
    font-size: 1.18rem;
}

.send-button {
    width: 100px;
    min-width: 100px;
    padding: 10px 18px;
    border-radius: 7px;
    color: #fff;
    background: #238553;
    font-weight: 700;
}

.send-button:disabled,
textarea:disabled {
    cursor: wait;
    opacity: 0.64;
}

.error-message {
    min-height: 1.5em;
    width: min(100%, 700px);
    margin: 8px 0 0;
    color: #9b1c1c;
    font-size: 0.94rem;
    text-align: center;
}

.chat-history {
    width: min(100%, 700px);
    margin-top: 6px;
    color: #52606d;
    font-size: 0.92rem;
}

.chat-history summary {
    width: fit-content;
    margin: 0 auto;
    cursor: pointer;
}

.chat-log {
    display: flex;
    flex-direction: column;
    gap: 8px;
    max-height: 24vh;
    margin-top: 8px;
    padding: 6px;
    overflow-y: auto;
}

.chat-message {
    width: fit-content;
    max-width: min(88%, 640px);
    padding: 8px 11px;
    border: 1px solid #d9e2ec;
    border-radius: 7px;
    color: #26323f;
    background: rgba(255, 255, 255, 0.9);
    overflow-wrap: anywhere;
}

.chat-message p {
    margin: 0;
}

.chat-message p + p {
    margin-top: 6px;
}

.user-message {
    align-self: flex-end;
    color: #fff;
    border-color: #225f9e;
    background: #225f9e;
}

.ai-message {
    align-self: flex-start;
}

.visually-hidden {
    position: absolute;
    width: 1px;
    height: 1px;
    padding: 0;
    margin: -1px;
    overflow: hidden;
    clip: rect(0, 0, 0, 0);
    white-space: nowrap;
    border: 0;
}

@media (max-width: 640px) {
    .character-page {
        padding: 10px 12px 16px;
    }

    .character-stage {
        width: 100%;
    }

    .character-visual {
        min-height: min(55vh, 430px);
    }

    .character-video {
        width: min(100%, 350px);
        height: min(52vh, 390px);
    }

    .video-fallback {
        min-height: min(42vh, 320px);
    }

    .character-dialogue-overlay {
        bottom: 17%;
        width: min(90vw, 420px);
        max-height: 24vh;
        padding: 14px 16px;
        font-size: 1rem;
    }

    .interaction-area {
        width: 100%;
        margin-top: 8px;
    }

    .input-row {
        grid-template-columns: 1fr 1fr;
        gap: 8px;
    }

    textarea {
        grid-column: 1 / -1;
        height: 58px;
        min-height: 58px;
    }

    .mic-button,
    .send-button {
        width: 100%;
        min-height: 46px;
    }

    .send-button {
        min-width: 0;
    }

    .chat-message {
        max-width: 94%;
    }
}