/* ==============================
   共通設定
============================== */

* {
    box-sizing: border-box;
}

html {
    font-size: 16px;
}

body {
    margin: 0;
    min-height: 100vh;
    font-family:
        -apple-system,
        BlinkMacSystemFont,
        "Segoe UI",
        "Hiragino Kaku Gothic ProN",
        "Hiragino Sans",
        "Yu Gothic",
        "Meiryo",
        sans-serif;
    color: #333;
    background: #f5f7fb;
}

a {
    color: #4776e6;
    text-decoration: none;
}

a:hover {
    text-decoration: underline;
}

button,
input,
textarea {
    font: inherit;
}

button {
    cursor: pointer;
}

h1,
p {
    margin-top: 0;
}

/* 画面には表示しないが、
   読み上げソフトでは利用できる */
.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;
}

/* ==============================
   ログイン・ユーザー登録
============================== */

.auth-container {
    width: min(420px, calc(100% - 32px));
    margin: 70px auto;
    padding: 32px;
    background: #fff;
    border-radius: 18px;
    box-shadow: 0 10px 30px rgba(0, 0, 0, 0.08);
}

.auth-container h1 {
    margin-bottom: 28px;
    font-size: 1.8rem;
    text-align: center;
}

.auth-container form {
    display: flex;
    flex-direction: column;
    gap: 20px;
}

.auth-container form > div {
    display: flex;
    flex-direction: column;
    gap: 8px;
}

.auth-container label {
    font-size: 0.9rem;
    font-weight: 600;
}

.auth-container input {
    width: 100%;
    padding: 13px 14px;
    border: 1px solid #d8dce6;
    border-radius: 10px;
    background: #fff;
    outline: none;
    transition:
        border-color 0.2s,
        box-shadow 0.2s;
}

.auth-container input:focus {
    border-color: #4776e6;
    box-shadow: 0 0 0 3px rgba(71, 118, 230, 0.14);
}

.auth-container button {
    width: 100%;
    padding: 13px;
    color: #fff;
    font-weight: 700;
    background: #4776e6;
    border: none;
    border-radius: 10px;
    transition:
        background 0.2s,
        transform 0.2s;
}

.auth-container button:hover {
    background: #3765d2;
}

.auth-container button:active {
    transform: translateY(1px);
}

.auth-container > p:last-child {
    margin-top: 24px;
    margin-bottom: 0;
    font-size: 0.9rem;
    text-align: center;
}

.error,
.success {
    padding: 12px 14px;
    margin-bottom: 20px;
    border-radius: 10px;
    font-size: 0.9rem;
}

.error {
    color: #a12727;
    background: #ffe8e8;
    border: 1px solid #f6bcbc;
}

.success {
    color: #236c3b;
    background: #e7f7ed;
    border: 1px solid #b9e4c7;
}

/* ==============================
   共通ヘッダー
============================== */

.app-header {
    min-height: 68px;
    padding: 14px 24px;
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 20px;
    background: #fff;
    border-bottom: 1px solid #e5e8f0;
    position: sticky;
    top: 0;
    z-index: 10;
}

.app-header h1 {
    margin: 0;
    font-size: 1.2rem;
    text-align: center;
}

.app-header p {
    margin: 0;
    font-weight: 600;
}

.app-header a {
    flex-shrink: 0;
    font-size: 0.9rem;
}

/* ==============================
   ユーザー一覧
============================== */

.users-container {
    width: min(680px, calc(100% - 32px));
    margin: 40px auto;
    padding: 30px;
    background: #fff;
    border-radius: 18px;
    box-shadow: 0 10px 30px rgba(0, 0, 0, 0.06);
}

.users-container h1 {
    margin-bottom: 24px;
    font-size: 1.6rem;
}

.user-list {
    padding: 0;
    margin: 0;
    list-style: none;
    display: flex;
    flex-direction: column;
    gap: 12px;
}

.user-item a {
    display: block;
    padding: 16px 18px;
    color: #333;
    font-weight: 600;
    background: #f7f8fc;
    border: 1px solid #e6e9f1;
    border-radius: 12px;
    transition:
        background 0.2s,
        border-color 0.2s,
        transform 0.2s;
}

.user-item a:hover {
    text-decoration: none;
    background: #eef3ff;
    border-color: #b9c9f7;
    transform: translateY(-1px);
}

.user-item a {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 12px;
}

.user-name {
    min-width: 0;
    overflow-wrap: anywhere;
}

.unread-badge {
    min-width: 26px;
    height: 26px;
    padding: 0 8px;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
    color: #fff;
    font-size: 0.78rem;
    font-weight: 700;
    line-height: 1;
    background: #e64b5d;
    border-radius: 999px;
}

.unread-badge[hidden] {
    display: none;
}

/* ==============================
   チャット画面
============================== */

.chat-container {
    width: min(860px, 100%);
    height: calc(100vh - 68px);
    margin: 0 auto;
    display: flex;
    flex-direction: column;
    background: #fff;
}

.message-list {
    flex: 1;
    padding: 24px;
    overflow-y: auto;
    background:
        linear-gradient(
            rgba(247, 249, 253, 0.95),
            rgba(247, 249, 253, 0.95)
        );
    scroll-behavior: smooth;
}

.no-messages {
    margin-top: 40px;
    color: #8b91a1;
    text-align: center;
}

.message-row {
    width: 100%;
    margin-bottom: 14px;
    display: flex;
}

.message-row.mine {
    justify-content: flex-end;
}

.message-row.theirs {
    justify-content: flex-start;
}

.message-bubble {
    max-width: min(72%, 560px);
    padding: 11px 14px 8px;
    border-radius: 16px;
    box-shadow: 0 2px 6px rgba(0, 0, 0, 0.05);
}

.message-row.mine .message-bubble {
    background: #dce7ff;
    border-bottom-right-radius: 4px;
}

.message-row.theirs .message-bubble {
    background: #fff;
    border: 1px solid #e4e7ee;
    border-bottom-left-radius: 4px;
}

.message-text {
    margin: 0;
    line-height: 1.6;
    overflow-wrap: anywhere;
    white-space: normal;
}

.message-time {
    display: block;
    margin-top: 5px;
    color: #7f8492;
    font-size: 0.72rem;
    text-align: right;
}

.message-meta {
    margin-top: 5px;
    display: flex;
    align-items: center;
    justify-content: flex-end;
    gap: 6px;
}

.message-time {
    display: block;
    margin-top: 0;
    color: #7f8492;
    font-size: 0.72rem;
    text-align: right;
}

.read-status {
    color: #677084;
    font-size: 0.72rem;
}

/* ==============================
   メッセージ送信欄
============================== */

.message-form {
    padding: 14px 18px;
    display: flex;
    align-items: flex-end;
    gap: 12px;
    background: #fff;
    border-top: 1px solid #e5e8f0;
}

.message-form textarea {
    flex: 1;
    min-height: 48px;
    max-height: 140px;
    padding: 12px 14px;
    resize: vertical;
    border: 1px solid #d7dce7;
    border-radius: 14px;
    background: #f8f9fc;
    outline: none;
    line-height: 1.5;
    transition:
        border-color 0.2s,
        box-shadow 0.2s;
}

.message-form textarea:focus {
    border-color: #4776e6;
    box-shadow: 0 0 0 3px rgba(71, 118, 230, 0.12);
    background: #fff;
}

.message-form button {
    min-width: 76px;
    height: 48px;
    padding: 0 18px;
    color: #fff;
    font-weight: 700;
    background: #4776e6;
    border: none;
    border-radius: 14px;
    transition:
        background 0.2s,
        opacity 0.2s;
}

.message-form button:hover {
    background: #3765d2;
}

.message-form button:disabled {
    cursor: not-allowed;
    opacity: 0.6;
}

/* ==============================
   スクロールバー
============================== */

.message-list::-webkit-scrollbar {
    width: 8px;
}

.message-list::-webkit-scrollbar-track {
    background: transparent;
}

.message-list::-webkit-scrollbar-thumb {
    background: #ccd2df;
    border-radius: 10px;
}

/* ==============================
   スマホ対応
============================== */

@media (max-width: 600px) {
    .auth-container {
        margin: 30px auto;
        padding: 24px 20px;
        border-radius: 14px;
    }

    .app-header {
        min-height: 60px;
        padding: 12px 14px;
        gap: 10px;
    }

    .app-header h1 {
        font-size: 1rem;
        overflow: hidden;
        white-space: nowrap;
        text-overflow: ellipsis;
    }

    .app-header p,
    .app-header a {
        font-size: 0.8rem;
    }

    .users-container {
        width: calc(100% - 24px);
        margin: 20px auto;
        padding: 22px 16px;
    }

    .users-container h1 {
        font-size: 1.3rem;
    }

    .chat-container {
        height: calc(100dvh - 60px);
    }

    .message-list {
        padding: 16px 12px;
    }

    .message-bubble {
        max-width: 84%;
        padding: 10px 12px 7px;
    }

    .message-text {
        font-size: 0.95rem;
    }

    .message-form {
        padding:
            10px
            10px
            calc(10px + env(safe-area-inset-bottom));
        gap: 8px;
    }

    .message-form textarea {
        min-height: 44px;
        padding: 10px 12px;
        border-radius: 12px;
    }

    .message-form button {
        min-width: 64px;
        height: 44px;
        padding: 0 12px;
        border-radius: 12px;
        font-size: 0.9rem;
    }
}

.users-navigation {
    margin-bottom: 24px;
    display: flex;
    flex-wrap: wrap;
    gap: 10px;
}

.users-navigation a {
    padding: 10px 14px;
    color: #4776e6;
    font-size: 0.9rem;
    font-weight: 600;
    background: #eef3ff;
    border-radius: 10px;
}

.users-navigation a:hover {
    text-decoration: none;
    background: #e1eaff;
}

.user-search-form {
    margin-bottom: 28px;
    display: flex;
    gap: 10px;
}

.user-search-form input {
    flex: 1;
    min-width: 0;
    padding: 12px 14px;
    border: 1px solid #d8dce6;
    border-radius: 10px;
    outline: none;
}

.user-search-form input:focus {
    border-color: #4776e6;
    box-shadow: 0 0 0 3px rgba(71, 118, 230, 0.14);
}

.user-search-form button,
.friend-request-button {
    padding: 11px 16px;
    color: #fff;
    font-weight: 700;
    background: #4776e6;
    border: none;
    border-radius: 10px;
}

.search-result-title {
    margin-bottom: 16px;
    font-size: 1.2rem;
}

.search-user-item {
    padding: 14px 16px;
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 16px;
    background: #f7f8fc;
    border: 1px solid #e6e9f1;
    border-radius: 12px;
}

.search-user-info {
    min-width: 0;
    display: flex;
    flex-direction: column;
    gap: 3px;
}

.user-email {
    color: #7f8492;
    font-size: 0.82rem;
    overflow-wrap: anywhere;
}

.friend-status {
    flex-shrink: 0;
    color: #697083;
    font-size: 0.85rem;
}

.request-link {
    flex-shrink: 0;
    font-size: 0.85rem;
}

@media (max-width: 600px) {
    .user-search-form {
        flex-direction: column;
    }

    .search-user-item {
        align-items: flex-start;
        flex-direction: column;
    }
}

.friend-request-item {
    padding: 14px 16px;
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 16px;
    background: #f7f8fc;
    border: 1px solid #e6e9f1;
    border-radius: 12px;
}

.friend-request-actions {
    display: flex;
    align-items: center;
    gap: 8px;
    flex-shrink: 0;
}

.friend-request-actions form {
    margin: 0;
}

.accept-button,
.reject-button {
    padding: 9px 14px;
    font-weight: 700;
    border-radius: 9px;
    cursor: pointer;
}

.accept-button {
    color: #fff;
    background: #4776e6;
    border: none;
}

.accept-button:hover {
    background: #3765d2;
}

.reject-button {
    color: #555;
    background: #fff;
    border: 1px solid #d8dce6;
}

.reject-button:hover {
    background: #f1f3f7;
}

@media (max-width: 600px) {
    .friend-request-item {
        align-items: flex-start;
        flex-direction: column;
    }
}

.friend-request-nav-link {
    display: inline-flex;
    align-items: center;
    gap: 7px;
}

.friend-request-count {
    min-width: 22px;
    height: 22px;
    padding: 0 7px;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    color: #fff;
    font-size: 0.72rem;
    font-weight: 700;
    line-height: 1;
    background: #e64b5d;
    border-radius: 999px;
}

.friend-request-count[hidden] {
    display: none;
}

.friend-list-item {
    display: flex;
    align-items: stretch;
    gap: 10px;
}

.friend-chat-link {
    flex: 1;
    min-width: 0;
}

.delete-friend-form {
    margin: 0;
    display: flex;
}

.delete-friend-button {
    padding: 0 14px;
    color: #a33b46;
    font-weight: 700;
    background: #fff;
    border: 1px solid #efc7cc;
    border-radius: 12px;
}

.delete-friend-button:hover {
    background: #fff0f2;
}

@media (max-width: 600px) {
    .friend-list-item {
        align-items: stretch;
    }

    .delete-friend-button {
        padding: 0 12px;
        font-size: 0.82rem;
    }
}

.image-input {
    display: none;
}

.image-select-button {
    min-width: 56px;
    height: 48px;
    padding: 0 12px;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
    color: #4776e6;
    font-size: 0.85rem;
    font-weight: 700;
    background: #eef3ff;
    border-radius: 12px;
    cursor: pointer;
}

.image-select-button:hover {
    background: #e1eaff;
}

.message-image-link {
    display: block;
    margin-bottom: 8px;
    border-radius: 12px;
    overflow: hidden;
}

.message-thumbnail {
    display: block;
    width: auto;
    max-width: 300px;
    max-height: 300px;
    object-fit: cover;
    border-radius: 12px;
}

.image-modal {
    position: fixed;
    inset: 0;
    z-index: 1000;
    padding: 40px 20px;
    display: flex;
    align-items: center;
    justify-content: center;
    background: rgba(0, 0, 0, 0.82);
}

.image-modal[hidden] {
    display: none;
}

.image-modal-content {
    display: block;
    max-width: 95vw;
    max-height: 90vh;
    object-fit: contain;
}

.image-modal-close {
    position: fixed;
    top: 16px;
    right: 20px;
    width: 44px;
    height: 44px;
    color: #fff;
    font-size: 2rem;
    line-height: 1;
    background: transparent;
    border: none;
}

.modal-open {
    overflow: hidden;
}

@media (max-width: 600px) {
    .image-select-button {
        min-width: 50px;
        height: 44px;
        padding: 0 9px;
    }

    .message-thumbnail {
        max-width: min(240px, 65vw);
        max-height: 240px;
    }
}

.message-compose-area {
    flex: 1;
    min-width: 0;
    display: flex;
    flex-direction: column;
    gap: 8px;
}

.message-compose-area textarea {
    width: 100%;
}

.image-preview {
    padding: 8px;
    display: flex;
    align-items: center;
    gap: 10px;
    background: #f1f4fa;
    border: 1px solid #dce2ee;
    border-radius: 12px;
}

.image-preview[hidden] {
    display: none;
}

.image-preview-thumbnail {
    width: 58px;
    height: 58px;
    flex-shrink: 0;
    object-fit: cover;
    background: #fff;
    border-radius: 9px;
}

.image-preview-info {
    min-width: 0;
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 10px;
    flex: 1;
}

.image-preview-name {
    min-width: 0;
    color: #555e70;
    font-size: 0.8rem;
    overflow: hidden;
    white-space: nowrap;
    text-overflow: ellipsis;
}

.image-preview-remove {
    padding: 6px 9px;
    flex-shrink: 0;
    color: #a33b46;
    font-size: 0.75rem;
    font-weight: 700;
    background: #fff;
    border: 1px solid #efc7cc;
    border-radius: 8px;
}

.image-preview-remove:hover {
    background: #fff0f2;
}

.stamp-button {
    min-width: 72px;
    height: 48px;
    padding: 0 12px;
    flex-shrink: 0;
    color: #4776e6;
    font-size: 0.85rem;
    font-weight: 700;
    background: #eef3ff;
    border: none;
    border-radius: 12px;
}

.stamp-button:hover {
    background: #e1eaff;
}

.stamp-panel {
    width: min(860px, 100%);
    max-height: 360px;
    margin: 0 auto;
    padding: 14px;
    overflow-y: auto;
    background: #fff;
    border-top: 1px solid #e5e8f0;
}

.stamp-panel[hidden] {
    display: none;
}

.stamp-panel-header {
    margin-bottom: 12px;
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 12px;
    font-weight: 700;
}

.stamp-panel-close {
    width: 32px;
    height: 32px;
    color: #697083;
    font-size: 1.4rem;
    line-height: 1;
    background: transparent;
    border: none;
}

.stamp-grid {
    display: grid;
    grid-template-columns: repeat(6, minmax(0, 1fr));
    gap: 10px;
}

.stamp-item {
    padding: 6px;
    background: transparent;
    border: 1px solid transparent;
    border-radius: 12px;
}

.stamp-item:hover {
    background: #f2f5fb;
    border-color: #dce3f0;
}

.stamp-item img {
    display: block;
    width: 100%;
    height: auto;
    aspect-ratio: 370 / 320;
    object-fit: contain;
}

@media (max-width: 600px) {
    .stamp-button {
        min-width: 58px;
        height: 44px;
        padding: 0 9px;
        font-size: 0.78rem;
    }

    .stamp-panel {
        max-height: 320px;
        padding: 12px 10px;
    }

    .stamp-grid {
        grid-template-columns: repeat(4, minmax(0, 1fr));
        gap: 8px;
    }
}

.message-stamp {
    display: block;
    width: 160px;
    max-width: 45vw;
    height: auto;
    object-fit: contain;
}

.message-row.stamp-message .message-bubble {
    padding: 0;
    background: transparent;
    border: none;
    box-shadow: none;
}

.message-row.stamp-message .message-meta {
    padding: 0 4px;
}

@media (max-width: 600px) {
    .message-stamp {
        width: 140px;
        max-width: 48vw;
    }
}