:root {
    --background: #eef1f5;
    --header-background: #ffffff;
    --chat-background: #e9edf2;
    --received-background: #ffffff;
    --sent-background: #2f80ed;
    --sent-text: #ffffff;
    --text: #222222;
    --sub-text: #777777;
    --border: #d8dde5;
    --online: #28b463;
    --input-background: #ffffff;
}

* {
    box-sizing: border-box;
}

html,
body {
    width: 100%;
    height: 100%;
    margin: 0;
}

body {
    overflow: hidden;
    color: var(--text);
    background: var(--background);

    font-family:
        -apple-system,
        BlinkMacSystemFont,
        "Hiragino Sans",
        "Yu Gothic",
        "Meiryo",
        sans-serif;
}

button,
textarea,
input,
select {
    font: inherit;
}

.app {
    width: 100%;
    max-width: 900px;
    height: 100dvh;
    margin: 0 auto;

    display: flex;
    flex-direction: column;

    overflow: hidden;

    background: var(--chat-background);

    box-shadow:
        0 0 30px rgb(0 0 0 / 8%);
}


/* =========================
   ヘッダー
========================= */

.chat-header {
    flex-shrink: 0;

    min-height: 74px;

    padding:
        max(12px, env(safe-area-inset-top)) 18px 12px;

    display: flex;
    align-items: center;

    gap: 12px;

    background: var(--header-background);

    border-bottom:
        1px solid var(--border);

    z-index: 10;
}

.header-avatar {
    width: 46px;
    height: 46px;

    display: grid;
    place-items: center;

    flex-shrink: 0;

    color: #ffffff;

    background:
        linear-gradient(135deg,
            #2f80ed,
            #7f56d9);

    border-radius: 50%;

    font-size: 18px;
    font-weight: 700;
    line-height: 1;

    user-select: none;
}

.header-info {
    min-width: 0;
}

.header-info h1 {
    margin: 0 0 3px;

    overflow: hidden;

    font-size: 17px;
    line-height: 1.35;

    white-space: nowrap;
    text-overflow: ellipsis;
}

.online-status {
    display: flex;
    align-items: center;

    gap: 6px;

    color: var(--sub-text);

    font-size: 12px;
}

.online-dot {
    width: 8px;
    height: 8px;

    background: var(--online);

    border-radius: 50%;
}


/* =========================
   チャット表示エリア
========================= */

.chat-area {
    flex: 1;
    min-height: 0;

    padding:
        12px 16px 16px;

    overflow-x: hidden;
    overflow-y: auto;

    scroll-behavior: smooth;
}

.date-label {
    width: fit-content;

    margin:
        0 auto 14px;

    padding:
        5px 12px;

    color: #68707c;

    background:
        rgb(255 255 255 / 70%);

    border-radius: 999px;

    font-size: 12px;
    line-height: 1.3;

    backdrop-filter: blur(8px);
}


/* =========================
   メッセージ行
========================= */

.message-row {
    width: 100%;

    margin:
        0 0 12px;

    display: flex;

    gap: 8px;
}

.message-row.received {
    align-items: flex-start;
    justify-content: flex-start;
}

.message-row.sent {
    align-items: flex-end;
    justify-content: flex-end;
}


/* =========================
   相手アイコン
========================= */

.message-avatar {
    width: 34px;
    height: 34px;

    display: grid;
    place-items: center;

    flex:
        0 0 34px;

    color: #ffffff;

    background:
        linear-gradient(135deg,
            #2f80ed,
            #7157e8);

    border-radius: 50%;

    font-size: 13px;
    font-weight: 700;
    line-height: 1;

    user-select: none;
}

.received .message-avatar {
    margin-top: 18px;
}


/* =========================
   メッセージ内容
========================= */

.message-content {
    width: fit-content;

    max-width:
        min(70%,
            560px);

    display: flex;
    flex-direction: column;
}

.sent .message-content {
    align-items: flex-end;
}

.received .message-content {
    align-items: flex-start;
}


/* =========================
   表示名
========================= */

.message-name {
    margin:
        0 0 4px 4px;

    padding: 0;

    color: #707782;

    font-size: 12px;
    line-height: 14px;
}


/* =========================
   吹き出し共通
========================= */

.message-bubble {
    width: fit-content;

    min-width: 0;
    min-height: 0;

    max-width: 100%;

    margin: 0;

    padding:
        9px 13px;

    display: block;

    overflow-wrap: anywhere;
    word-break: break-word;

    /*
    HTML内のインデントや改行を
    余計な空白として表示しない
    */
    white-space: normal;

    text-align: left;

    line-height: 1.5;

    box-shadow:
        0 2px 6px rgb(0 0 0 / 8%);
}


/* 相手側 */

.received .message-bubble {
    color: #222222;

    background:
        var(--received-background);

    border-radius:
        16px 16px 16px 4px;
}


/* 自分側 */

.sent .message-bubble {
    color:
        var(--sent-text);

    background:
        var(--sent-background);

    border-radius:
        16px 16px 4px 16px;
}


/* =========================
   時刻
========================= */

.message-time {
    margin-top: 3px;

    color: #818792;

    font-size: 10px;
    line-height: 1;
}

.sent .message-time {
    margin-right: 3px;

    text-align: right;
}

.received .message-time {
    margin-left: 3px;

    text-align: left;
}


/* =========================
   入力中表示
========================= */

.typing-wrapper {
    flex-shrink: 0;

    padding:
        0 16px 7px;

    background:
        var(--chat-background);
}

.typing-wrapper .message-row {
    margin: 0;
}

.typing-wrapper .message-avatar {
    margin-top: 0;
}

.typing-bubble {
    min-width: 58px;

    padding:
        11px 14px;

    display: flex;
    align-items: center;

    gap: 5px;

    background: #ffffff;

    border-radius:
        16px 16px 16px 4px;

    box-shadow:
        0 2px 6px rgb(0 0 0 / 8%);
}

.typing-bubble span {
    width: 7px;
    height: 7px;

    background: #9ca3ad;

    border-radius: 50%;

    animation:
        typingDot 1.2s infinite ease-in-out;
}

.typing-bubble span:nth-child(2) {
    animation-delay: 0.15s;
}

.typing-bubble span:nth-child(3) {
    animation-delay: 0.3s;
}


/* =========================
   入力欄
========================= */

.input-area {
    flex-shrink: 0;

    padding:
        11px 13px max(11px,
            env(safe-area-inset-bottom));

    background:
        var(--input-background);

    border-top:
        1px solid var(--border);
}

.input-area form {
    display: flex;
    align-items: flex-end;

    gap: 9px;
}

#messageInput {
    width: 100%;

    min-height: 43px;
    max-height: 120px;

    padding:
        10px 14px;

    resize: none;

    outline: none;

    color: var(--text);

    background: #f4f6f8;

    border:
        1px solid transparent;

    border-radius: 22px;

    line-height: 1.45;

    overflow-y: auto;

    transition:
        border-color 0.2s,
        background 0.2s;
}

#messageInput:focus {
    background: #ffffff;

    border-color: #9ac2f7;
}

#sendButton {
    width: 43px;
    height: 43px;

    padding: 10px;

    display: grid;
    place-items: center;

    flex-shrink: 0;

    cursor: pointer;

    color: #ffffff;

    background:
        var(--sent-background);

    border: none;

    border-radius: 50%;

    transition:
        transform 0.15s,
        opacity 0.15s;
}

#sendButton:hover {
    transform: scale(1.05);
}

#sendButton:active {
    transform: scale(0.94);
}

#sendButton:disabled {
    cursor: not-allowed;

    opacity: 0.5;
}

#sendButton svg {
    width: 22px;
    height: 22px;

    fill: currentColor;
}


/* =========================
   エラー表示
========================= */

.system-message {
    width: fit-content;

    max-width: 90%;

    margin:
        8px auto 12px;

    padding:
        8px 12px;

    color: #a52828;

    background: #ffe3e3;

    border-radius: 8px;

    font-size: 12px;
    line-height: 1.4;
}

.hidden {
    display: none !important;
}


/* =========================
   アニメーション
========================= */

.animation-fade {
    animation:
        fadeIn 0.35s ease both;
}

.animation-slide {
    animation:
        slideIn 0.4s ease both;
}

.animation-pop {
    animation:
        popIn 0.4s ease both;
}

.animation-shake {
    animation:
        fadeIn 0.25s ease both,

        shake 0.55s 0.25s ease;
}

@keyframes typingDot {

    0%,
    60%,
    100% {
        transform: translateY(0);
        opacity: 0.45;
    }

    30% {
        transform: translateY(-5px);
        opacity: 1;
    }
}

@keyframes fadeIn {
    from {
        opacity: 0;
    }

    to {
        opacity: 1;
    }
}

@keyframes slideIn {
    from {
        opacity: 0;
        transform: translateX(-20px);
    }

    to {
        opacity: 1;
        transform: translateX(0);
    }
}

@keyframes popIn {
    0% {
        opacity: 0;
        transform: scale(0.75);
    }

    70% {
        transform: scale(1.04);
    }

    100% {
        opacity: 1;
        transform: scale(1);
    }
}

@keyframes shake {

    0%,
    100% {
        transform: translateX(0);
    }

    20% {
        transform: translateX(-7px);
    }

    40% {
        transform: translateX(7px);
    }

    60% {
        transform: translateX(-5px);
    }

    80% {
        transform: translateX(5px);
    }
}


/* =========================
   スマホ表示
========================= */

@media (max-width: 600px) {

    .chat-header {
        min-height: 67px;

        padding-left: 13px;
        padding-right: 13px;
    }

    .header-avatar {
        width: 42px;
        height: 42px;
    }

    .chat-area {
        padding:
            10px 11px 14px;
    }

    .message-content {
        max-width: 78%;
    }

    .message-avatar {
        width: 32px;
        height: 32px;

        flex-basis: 32px;

        font-size: 12px;
    }

    .received .message-avatar {
        margin-top: 18px;
    }

    .typing-wrapper .message-avatar {
        margin-top: 0;
    }

    .message-bubble {
        padding:
            8px 12px;

        font-size: 15px;
    }

    .message-row {
        margin-bottom: 10px;
    }
}