/* リセットCSS */
* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

/* メインカラー */
:root {
    --primary-color: #1e3a8a;
    --primary-light: #3b82f6;
    --secondary-color: #f8fafc;
    --text-color: #1f2937;
    --text-light: #6b7280;
    --border-color: #e5e7eb;
    --message-bg: #ffffff;
    --user-message-bg: #dbeafe;
    --hover-bg: #f3f4f6;
    --success-color: #10b981;
    --error-color: #ef4444;
    --warning-color: #f59e0b;
}

body {
    font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
    background-color: #f9fafb;
    color: var(--text-color);
    line-height: 1.6;
}

/* 汎用ユーティリティ */
.hidden { display: none !important; }

/* ヘッダー */
.app-header {
    position: sticky;
    top: 0;
    z-index: 1100;
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 16px;
    padding: 10px 16px;
    background: #ffffff;
    border-bottom: 1px solid var(--border-color);
}
.app-title { font-size: 16px; font-weight: 700; color: var(--text-color); }
.header-right { position: relative; }
.user-button {
    display: inline-flex;
    align-items: center;
    gap: 8px;
    padding: 6px 10px;
    border: 1px solid var(--border-color);
    border-radius: 20px;
    background: #fff;
    cursor: pointer;
}
.user-button:hover { background: var(--hover-bg); }
.user-avatar { width: 28px; height: 28px; border-radius: 50%; object-fit: cover; }
.user-button-caret {
    display: inline-block;
    width: 0;
    height: 0;
    margin-left: 2px;
    border-left: 4px solid transparent;
    border-right: 4px solid transparent;
    border-top: 5px solid var(--text-light);
    transition: transform 0.2s ease, border-top-color 0.2s ease;
}
.user-button:hover .user-button-caret {
    border-top-color: var(--primary-color);
}
.user-button[aria-expanded="true"] .user-button-caret {
    transform: rotate(180deg);
}

.dropdown.hidden { display: none; }
.dropdown {
    position: absolute;
    right: 0;
    top: calc(100% + 8px);
    width: 200px;
    background: #fff;
    border: 1px solid var(--border-color);
    border-radius: 8px;
    box-shadow: 0 10px 20px rgba(0,0,0,0.1);
    padding: 8px;
}
.dropdown-item { width: 100%; text-align: left; padding: 8px 10px; border: none; background: transparent; cursor: pointer; border-radius: 6px; }
.dropdown-item:hover { background: var(--hover-bg); }
.dropdown-separator { border: none; border-top: 1px solid var(--border-color); margin: 6px 0; }

/* プロフィールモーダル */
.profile-summary { display: flex; align-items: center; gap: 12px; }
.profile-avatar { width: 64px; height: 64px; border-radius: 50%; object-fit: cover; }
.profile-name { font-weight: 700; font-size: 16px; }
.profile-email, .profile-id { font-size: 13px; color: var(--text-light); }

/* プロフィール編集 */
#profile-edit.hidden, #profile-edit-actions.hidden { display: none; }
.profile-edit-header { display: flex; align-items: center; gap: 12px; margin-bottom: 12px; }
.change-avatar-link { font-size: 13px; color: var(--primary-light); text-decoration: underline; cursor: pointer; }
.change-avatar-link:hover { color: var(--primary-color); }
.hidden-file-input { display: none; }

/* メインレイアウト */
.chat-container {
    display: flex;
    height: 100vh;
    max-width: 1400px;
    margin: 0 auto;
    background: white;
    box-shadow: 0 0 20px rgba(0, 0, 0, 0.1);
}

/* サイドバー */
.sidebar {
    width: 280px;
    background: var(--secondary-color);
    border-right: 1px solid var(--border-color);
    display: flex;
    flex-direction: column;
    overflow-y: auto;
}

.user-info {
    padding: 20px;
    border-bottom: 1px solid var(--border-color);
    display: flex;
    align-items: center;
    gap: 12px;
}

.user-avatar img {
    width: 40px;
    height: 40px;
    border-radius: 50%;
    object-fit: cover;
}

.user-details {
    flex: 1;
}

.username {
    font-weight: 600;
    font-size: 14px;
    color: var(--text-color);
}

.user-status {
    font-size: 12px;
    color: var(--text-light);
}

.user-status.online {
    color: var(--success-color);
}

.user-status.offline {
    color: var(--text-light);
}

/* チャンネル・ユーザーセクション */
.channels-section,
.users-section {
    display: flex;
    flex-direction: column;
    min-height: 0;
}

.channels-section {
    padding: 16px 20px;
    border-bottom: 1px solid var(--border-color);
    flex: 1.5 1 0;
}

.channels-section h3,
.users-section h3 {
    font-size: 14px;
    font-weight: 600;
    color: var(--text-light);
    margin-bottom: 12px;
    letter-spacing: 0.5px;
}

/* 見出し内の追加ボタン */
.channels-section h3 {
    display: flex;
    align-items: center;
    justify-content: space-between;
}

.icon-button {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 24px;
    height: 24px;
    border-radius: 50%;
    border: 1px solid var(--border-color);
    background: #fff;
    color: var(--text-color);
    cursor: pointer;
    font-weight: 700;
    line-height: 1;
}

.icon-button:hover,
.icon-button:focus {
    background: var(--hover-bg);
    outline: none;
}

.channels-list,
.users-list {
    list-style: none;
    margin: 0;
    padding: 0;
    display: flex;
    flex-direction: column;
    gap: 4px;
    flex: 1 1 auto;
    overflow-y: auto;
}

.users-section {
    padding: 16px 20px;
    flex: 1 1 0;
}

.channel-item,
.user-item {
    border-radius: 6px;
    cursor: pointer;
    transition: background-color 0.2s ease, color 0.2s ease;
    display: flex;
    align-items: center;
    gap: 10px;
    position: relative;
    padding: 8px 10px;
}

.channel-item:hover,
.user-item:hover {
    background-color: var(--hover-bg);
}

.channel-item.active {
    background-color: var(--primary-color);
    color: #fff;
}

.channel-item.active::before {
    content: '';
    position: absolute;
    left: 0;
    top: 8px;
    bottom: 8px;
    width: 4px;
    border-radius: 2px;
    background: var(--primary-light);
}

.channel-item .icon {
    width: 24px;
    height: 24px;
    border-radius: 6px;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    background: rgba(30, 58, 138, 0.1);
    color: var(--primary-color);
    font-size: 14px;
    flex-shrink: 0;
}

.channel-item.active .icon {
    background: rgba(255, 255, 255, 0.2);
    color: #fff;
}

.channel-item .channel-content {
    flex: 1;
    display: flex;
    flex-direction: column;
    line-height: 1.2;
}

.channel-item .channel-description {
    font-size: 11px;
    color: var(--text-light);
}

.channel-item.active .channel-description {
    color: rgba(255, 255, 255, 0.7);
}

.channel-item .message-count {
    min-width: 20px;
    padding: 2px 6px;
    border-radius: 999px;
    background: var(--primary-light);
    color: #fff;
    font-size: 11px;
    font-weight: 600;
    text-align: center;
}

.channel-item.active .message-count {
    background: #fff;
    color: var(--primary-color);
}

.user-item {
    padding: 6px 8px;
}

.user-item .user-avatar {
    width: 32px;
    height: 32px;
    border-radius: 50%;
    object-fit: cover;
    flex-shrink: 0;
}

.user-item .user-meta {
    display: flex;
    flex-direction: column;
    justify-content: center;
    line-height: 1.2;
}

.user-item .user-meta .name {
    font-size: 13px;
    font-weight: 600;
    color: var(--text-color);
}

.user-item .user-meta .status {
    font-size: 11px;
    color: var(--text-light);
}

.user-item .user-meta .status.online {
    color: var(--success-color);
}

.user-item .user-meta .status.offline {
    color: var(--text-light);
}

/* メインエリア */
.main-area {
    flex: 1;
    display: flex;
    flex-direction: column;
    overflow: hidden;
}

.chat-header {
    padding: 20px;
    border-bottom: 1px solid var(--border-color);
    background: white;
}

.channel-info h2 {
    font-size: 18px;
    font-weight: 600;
    color: var(--text-color);
    margin-bottom: 4px;
}

.channel-description {
    font-size: 14px;
    color: var(--text-light);
}

/* メッセージコンテナ */
.messages-container {
    flex: 1;
    overflow-y: auto;
    padding: 20px;
    background: #fafafa;
}

.message {
    margin-bottom: 16px;
    display: flex;
    gap: 12px;
}

.message.own {
    flex-direction: row-reverse;
}

.message-avatar {
    width: 32px;
    height: 32px;
    border-radius: 50%;
    object-fit: cover;
    flex-shrink: 0;
}

.message-content {
    max-width: 70%;
    min-width: 100px;
}

.message.own .message-content {
    text-align: right;
}

.message-header {
    display: flex;
    align-items: center;
    gap: 8px;
    margin-bottom: 4px;
}

.message.own .message-header {
    justify-content: flex-end;
}

.message-username {
    font-weight: 600;
    font-size: 13px;
    color: var(--text-color);
}

.message-timestamp {
    font-size: 11px;
    color: var(--text-light);
}

.message-bubble {
    background: var(--message-bg);
    border: 1px solid var(--border-color);
    border-radius: 12px;
    padding: 12px 16px;
    word-wrap: break-word;
    box-shadow: 0 1px 2px rgba(0, 0, 0, 0.05);
}

.message.own .message-bubble {
    background: var(--user-message-bg);
    border-color: var(--primary-light);
}

.message-text {
    font-size: 14px;
    line-height: 1.4;
    color: var(--text-color);
    white-space: pre-wrap;
}

.message.own .message-text {
    text-align: left;
}

/* メッセージ入力 */
.message-input-container {
    padding: 20px;
    border-top: 1px solid var(--border-color);
    background: white;
}

.message-input-wrapper {
    display: flex;
    gap: 12px;
    align-items: flex-end;
}

#message-input {
    flex: 1;
    min-height: 40px;
    max-height: 120px;
    padding: 12px 16px;
    border: 1px solid var(--border-color);
    border-radius: 20px;
    resize: none;
    font-family: inherit;
    font-size: 14px;
    line-height: 1.4;
    outline: none;
    transition: border-color 0.2s;
}

#message-input:focus {
    border-color: var(--primary-color);
}

.send-button {
    padding: 12px 20px;
    background: var(--primary-color);
    color: white;
    border: none;
    border-radius: 20px;
    font-size: 14px;
    font-weight: 600;
    cursor: pointer;
    transition: background-color 0.2s;
    min-width: 80px;
}

.send-button:hover {
    background: var(--primary-light);
}

.send-button:disabled {
    background: var(--text-light);
    cursor: not-allowed;
}

/* モーダル */
.modal.hidden { display: none; }
.modal {
    position: fixed;
    inset: 0;
    z-index: 1500;
}
.modal-backdrop {
    position: absolute;
    inset: 0;
    background: rgba(0,0,0,0.4);
}
.modal-dialog {
    position: relative;
    width: 96%;
    max-width: 520px;
    margin: 10vh auto;
    background: #fff;
    border-radius: 12px;
    box-shadow: 0 10px 30px rgba(0,0,0,0.2);
    overflow: hidden;
}
.modal-header,
.modal-footer {
    padding: 16px 20px;
    border-bottom: 1px solid var(--border-color);
}
.modal-footer { border-bottom: none; border-top: 1px solid var(--border-color); }
.modal-body { padding: 16px 20px; }
.modal-close {
    border: none;
    background: transparent;
    font-size: 18px;
    cursor: pointer;
}
.form-group { margin-bottom: 12px; }
.form-group.checkbox { margin-top: 8px; }
.form-group label { display: block; font-size: 13px; color: var(--text-color); margin-bottom: 6px; }
.form-group input[type="text"],
.form-group textarea {
    width: 100%;
    border: 1px solid var(--border-color);
    border-radius: 8px;
    padding: 10px 12px;
    font-size: 14px;
}
.field-error { color: var(--error-color); font-size: 12px; margin-top: 4px; min-height: 16px; }
.required { color: var(--error-color); }
.modal-footer .primary,
.modal-footer .secondary {
    padding: 10px 16px;
    border-radius: 8px;
    border: 1px solid var(--border-color);
    cursor: pointer;
}
.modal-footer .primary { background: var(--primary-color); color: #fff; border-color: var(--primary-color); }
.modal-footer .secondary { background: #fff; color: var(--text-color); }
.modal-footer .primary:disabled { opacity: 0.6; cursor: not-allowed; }

/* ローディング */
.loading {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: rgba(255, 255, 255, 0.8);
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 1000;
}

.loading.hidden {
    display: none;
}

.loading-spinner {
    width: 40px;
    height: 40px;
    border: 4px solid var(--border-color);
    border-top: 4px solid var(--primary-color);
    border-radius: 50%;
    animation: spin 1s linear infinite;
}

@keyframes spin {
    0% { transform: rotate(0deg); }
    100% { transform: rotate(360deg); }
}

/* レスポンシブデザイン */
@media (max-width: 768px) {
    .chat-container {
        flex-direction: column;
    }
    
    .sidebar {
        width: 100%;
        height: auto;
        max-height: 200px;
    }
    
    .message-content {
        max-width: 85%;
    }
}

/* スクロールバー */
.messages-container::-webkit-scrollbar,
.sidebar::-webkit-scrollbar {
    width: 6px;
}

.messages-container::-webkit-scrollbar-track,
.sidebar::-webkit-scrollbar-track {
    background: var(--secondary-color);
}

.messages-container::-webkit-scrollbar-thumb,
.sidebar::-webkit-scrollbar-thumb {
    background: var(--border-color);
    border-radius: 3px;
}

.messages-container::-webkit-scrollbar-thumb:hover,
.sidebar::-webkit-scrollbar-thumb:hover {
    background: var(--text-light);
}

/* アニメーション */
.message {
    animation: fadeInUp 0.3s ease-out;
}

@keyframes fadeInUp {
    from {
        opacity: 0;
        transform: translateY(20px);
    }
    to {
        opacity: 1;
        transform: translateY(0);
    }
}
