:root {
    --bg: #f4f6f9;
    --surface: #ffffff;
    --text: #1f2933;
    --muted: #6b7785;
    --border: #dde3ea;
    --primary: #2563eb;
    --primary-dark: #1d4ed8;
    --mine: #2563eb;
    --mine-text: #ffffff;
    --theirs: #ffffff;
    --alert: #dc2626;
    --alert-bg: #fef2f2;
    --unread-bg: #eff6ff;
}

* { box-sizing: border-box; }

body {
    margin: 0;
    font-family: -apple-system, BlinkMacSystemFont, "Hiragino Sans", "Hiragino Kaku Gothic ProN", "Yu Gothic UI", Meiryo, sans-serif;
    background: var(--bg);
    color: var(--text);
    line-height: 1.6;
}

a { color: var(--primary); }

.muted { color: var(--muted); }
.text-alert { color: var(--alert); }
.empty { color: var(--muted); text-align: center; padding: 48px 16px; }

/* ---------- ボタン ---------- */
.btn-primary {
    background: var(--primary);
    color: #fff;
    border: none;
    border-radius: 8px;
    padding: 10px 20px;
    font-size: 15px;
    font-weight: 600;
    cursor: pointer;
}
.btn-primary:hover { background: var(--primary-dark); }
.btn-primary:disabled { opacity: .6; cursor: default; }

.btn-link {
    background: none;
    border: none;
    color: var(--muted);
    font-size: 14px;
    cursor: pointer;
    padding: 4px 6px;
    text-decoration: underline;
}
form.inline { display: inline; margin: 0; }

/* ---------- ログイン画面 ---------- */
.auth-page {
    min-height: 100vh;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 24px 16px;
}
.auth-card {
    width: 100%;
    max-width: 420px;
    background: var(--surface);
    border-radius: 14px;
    box-shadow: 0 4px 24px rgba(0, 0, 0, .06);
    padding: 28px 24px;
}
.brand { font-size: 22px; text-align: center; margin: 0 0 16px; }
.auth-card h2 { font-size: 17px; margin: 20px 0 8px; }
.tabs {
    display: flex;
    border-bottom: 1px solid var(--border);
}
.tabs a {
    flex: 1;
    text-align: center;
    padding: 8px 4px;
    font-size: 14px;
    color: var(--muted);
    text-decoration: none;
    border-bottom: 2px solid transparent;
}
.tabs a.active { color: var(--primary); border-bottom-color: var(--primary); font-weight: 600; }

.auth-card form { display: flex; flex-direction: column; }
.auth-card label { font-size: 14px; font-weight: 600; margin-top: 14px; }
.auth-card input {
    margin-top: 4px;
    padding: 10px 12px;
    border: 1px solid var(--border);
    border-radius: 8px;
    font-size: 16px;
}
.auth-card input:focus { outline: 2px solid var(--primary); border-color: transparent; }
.auth-card small { color: var(--muted); font-size: 12px; margin-top: 2px; }
.auth-card small.field-note { color: #b45309; font-size: 13px; font-weight: 600; }
.auth-card .btn-primary { margin-top: 22px; }
.info { font-size: 13px; color: var(--muted); background: var(--bg); padding: 10px 12px; border-radius: 8px; }
.alert { background: var(--alert-bg); color: var(--alert); border-radius: 8px; padding: 8px 12px; font-size: 14px; }
.alert ul { margin: 0; padding-left: 18px; }
.switch { text-align: center; font-size: 14px; margin: 18px 0 0; }

/* ---------- ヘッダー ---------- */
.topbar {
    position: sticky;
    top: 0;
    z-index: 10;
    display: flex;
    align-items: center;
    gap: 12px;
    background: var(--surface);
    border-bottom: 1px solid var(--border);
    padding: 10px 16px;
}
.topbar h1 {
    font-size: 17px;
    margin: 0;
    flex: 1;
    min-width: 0;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}
.topbar .back { text-decoration: none; font-size: 14px; white-space: nowrap; }
.topbar-right { display: flex; align-items: center; gap: 6px; font-size: 14px; }
.topbar .me { color: var(--muted); white-space: nowrap; }

/* ---------- 教員ダッシュボード ---------- */
.dashboard { max-width: 760px; margin: 0 auto; padding: 16px; }
.summary {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    gap: 8px 18px;
    font-size: 14px;
    margin-bottom: 12px;
}
.summary input {
    margin-left: auto;
    padding: 6px 10px;
    border: 1px solid var(--border);
    border-radius: 8px;
    font-size: 14px;
    min-width: 0;
    width: 200px;
    max-width: 100%;
}
.student-list {
    list-style: none;
    margin: 0;
    padding: 0;
    background: var(--surface);
    border-radius: 12px;
    overflow: hidden;
    border: 1px solid var(--border);
}
.student-list li + li { border-top: 1px solid var(--border); }
.student-list a {
    display: block;
    padding: 12px 16px;
    color: inherit;
    text-decoration: none;
}
.student-list a:hover { background: var(--bg); }
.student-list li.has-unread { background: var(--unread-bg); }
.student-list li.has-unread .nickname { font-weight: 700; }
.row-main { display: flex; align-items: center; gap: 8px; }
.nickname { font-size: 16px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.badge {
    background: var(--alert);
    color: #fff;
    font-size: 12px;
    font-weight: 700;
    border-radius: 999px;
    min-width: 22px;
    height: 22px;
    padding: 0 7px;
    display: inline-flex;
    align-items: center;
    justify-content: center;
}
.time { margin-left: auto; font-size: 12px; color: var(--muted); white-space: nowrap; }
.preview {
    font-size: 14px;
    color: var(--muted);
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    margin-top: 2px;
}
.has-unread .preview { color: var(--text); }

/* ---------- チャット ---------- */
.chat-page { padding-bottom: 180px; }
.chat {
    max-width: 760px;
    margin: 0 auto;
    padding: 16px;
    display: flex;
    flex-direction: column;
    gap: 10px;
}
.msg { display: flex; flex-direction: column; max-width: 78%; }
.msg.mine { align-self: flex-end; align-items: flex-end; }
.msg.theirs { align-self: flex-start; align-items: flex-start; }
.bubble {
    padding: 9px 13px;
    border-radius: 16px;
    white-space: pre-wrap;       /* 改行をそのまま表示（HTMLはエスケープ済み） */
    overflow-wrap: anywhere;
    font-size: 15px;
}
.mine .bubble { background: var(--mine); color: var(--mine-text); border-bottom-right-radius: 4px; }
.theirs .bubble { background: var(--theirs); border: 1px solid var(--border); border-bottom-left-radius: 4px; }
.meta { font-size: 11px; color: var(--muted); display: flex; gap: 6px; margin-top: 2px; }

.composer {
    position: fixed;
    left: 0;
    right: 0;
    bottom: 0;
    background: var(--surface);
    border-top: 1px solid var(--border);
    padding: 8px 16px 12px;
}
.composer-row { max-width: 760px; margin: 0 auto; display: flex; gap: 8px; align-items: flex-end; }
.composer textarea {
    flex: 1;
    min-width: 0;
    resize: vertical;
    max-height: 40vh;
    padding: 10px 12px;
    border: 1px solid var(--border);
    border-radius: 10px;
    font: inherit;
    font-size: 16px;
}
.composer-error {
    max-width: 760px;
    margin: 0 auto 4px;
    color: var(--alert);
    font-size: 13px;
    min-height: 0;
}
.composer-error:empty { display: none; }

/* ---------- 添付ファイル ---------- */
/* 入力欄は1段目に横幅いっぱい、添付・送信ボタンは2段目に並べる（スマホでも入力欄が狭くならない） */
.composer-row { flex-direction: column; align-items: stretch; gap: 6px; }
.composer-row textarea { width: 100%; }
.composer-actions { display: flex; align-items: center; gap: 8px; }
.composer-actions .btn-primary { margin-left: auto; padding: 8px 22px; }
.attach-btn {
    position: relative;
    overflow: hidden;
    flex: none;
    display: inline-flex;
    align-items: center;
    gap: 4px;
    height: 36px;
    padding: 0 12px;
    border: 1px solid var(--border);
    border-radius: 8px;
    background: var(--bg);
    color: var(--text);
    font-size: 14px;
    line-height: 1;
    cursor: pointer;
}
.attach-btn:hover { background: var(--unread-bg); }
.attach-btn:focus-within { outline: 2px solid var(--primary); outline-offset: 1px; }
.attach-btn input[type="file"] {
    position: absolute;
    left: 0;
    top: 0;
    width: 1px;
    height: 1px;
    opacity: 0;
}
.attach-chip {
    min-width: 0;
    flex: 1;
    display: flex;
    align-items: center;
    gap: 4px;
    font-size: 13px;
    color: var(--muted);
}
.attach-chip[hidden] { display: none; }
.attach-chip .attach-name {
    min-width: 0;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}
.attach-img {
    display: block;
    margin-bottom: 4px;
}
.attach-img img {
    display: block;
    max-width: min(260px, 100%);
    max-height: 320px;
    border-radius: 12px;
    border: 1px solid var(--border);
    background: var(--surface);
}
.attach-file {
    display: flex;
    align-items: center;
    gap: 6px;
    max-width: 100%;
    margin-bottom: 4px;
    padding: 8px 12px;
    border: 1px solid var(--border);
    border-radius: 12px;
    background: var(--surface);
    color: var(--text);
    text-decoration: none;
    font-size: 14px;
}
.attach-file:hover { background: var(--unread-bg); }
.attach-file .attach-name {
    min-width: 0;
    overflow-wrap: anywhere;
    color: var(--primary);
    text-decoration: underline;
}
.attach-file .attach-size { flex: none; color: var(--muted); font-size: 12px; }
.msg.selected .attach-img img,
.msg.selected .attach-file { outline: 3px solid var(--alert); outline-offset: 1px; }

/* ---------- メッセージ選択削除（教員） ---------- */
.btn-danger {
    background: var(--alert);
    color: #fff;
    border: none;
    border-radius: 8px;
    padding: 8px 18px;
    font-size: 15px;
    font-weight: 600;
    cursor: pointer;
}
.btn-danger:disabled { opacity: .5; cursor: default; }
.select-toggle { color: var(--primary); font-weight: 600; text-decoration: none; }
.msg { position: relative; }
.select-box { display: none; }
.selecting .msg { cursor: pointer; }
.selecting .select-box {
    display: flex;
    align-items: center;
    position: absolute;
    top: 6px;
}
.selecting .msg.mine { padding-left: 30px; }
.selecting .msg.theirs { padding-right: 30px; }
.selecting .msg.mine .select-box { left: 0; }
.selecting .msg.theirs .select-box { right: 0; }
.select-box input { width: 20px; height: 20px; margin: 0; cursor: pointer; }
.msg.selected .bubble { outline: 3px solid var(--alert); outline-offset: 1px; }
.delete-bar { display: none; }
.selecting .delete-bar {
    display: block;
    position: fixed;
    left: 0;
    right: 0;
    bottom: 0;
    z-index: 20;
    background: var(--alert-bg);
    border-top: 1px solid #fecaca;
    padding: 12px 16px;
}
.selecting .composer { display: none; }
.delete-bar-row {
    max-width: 760px;
    margin: 0 auto;
    display: flex;
    align-items: center;
    gap: 10px;
    font-size: 14px;
}
.delete-bar-row .btn-danger { margin-left: auto; }
.flash-notice {
    max-width: 760px;
    margin: 12px auto 0;
    background: #ecfdf5;
    color: #047857;
    border-radius: 8px;
    padding: 8px 12px;
    font-size: 14px;
}

/* ---------- 学籍番号検索 ---------- */
.lookup {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: 12px;
    padding: 12px 16px;
    margin-bottom: 14px;
}
.lookup-form label { font-size: 14px; font-weight: 600; }
.lookup-row { display: flex; gap: 8px; margin-top: 4px; }
.lookup-row input {
    flex: 1;
    min-width: 0;
    max-width: 240px;
    padding: 8px 12px;
    border: 1px solid var(--border);
    border-radius: 8px;
    font-size: 16px;
}
.lookup-row .btn-primary { padding: 8px 16px; }
.lookup-result { margin-top: 10px; font-size: 14px; background: #ecfdf5; border-radius: 8px; padding: 8px 12px; }
.lookup-result .muted { margin-left: 8px; font-size: 13px; }
.lookup-alert { margin-top: 10px; }
.student-list li.found { outline: 2px solid #10b981; outline-offset: -2px; }

/* ---------- 通知設定 ---------- */
.nav-link { font-size: 14px; white-space: nowrap; padding: 4px 6px; }
.settings { max-width: 640px; margin: 0 auto; padding: 16px; }
.settings-form {
    display: flex;
    flex-direction: column;
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: 12px;
    padding: 20px;
    margin-top: 12px;
}
.settings-form label { font-size: 14px; font-weight: 600; margin-top: 16px; }
.settings-form label.check { display: flex; align-items: center; gap: 8px; margin-top: 0; font-size: 15px; }
.settings-form label.check input { width: 18px; height: 18px; }
.settings-form input[type="email"],
.settings-form input[type="url"],
.settings-form input[type="text"],
.settings-form input[type="password"],
.settings-form select {
    margin-top: 4px;
    padding: 10px 12px;
    border: 1px solid var(--border);
    border-radius: 8px;
    font-size: 16px;
}
.settings-form small { color: var(--muted); font-size: 12px; margin-top: 2px; }
.settings-form select { background: var(--surface); }
.form-section { font-size: 15px; margin: 24px 0 0; padding-top: 14px; border-top: 1px solid var(--border); }
.form-section.first { margin-top: 0; padding-top: 0; border-top: none; }
.preset-row { display: flex; align-items: center; gap: 4px; font-size: 13px; color: var(--muted); margin-top: 8px; }
.preset-row .btn-link { color: var(--primary); font-weight: 600; }
.field-pair { display: grid; grid-template-columns: 110px 1fr; gap: 12px; }
.field-pair > div { display: flex; flex-direction: column; min-width: 0; }
.settings-actions { display: flex; flex-wrap: wrap; gap: 10px; margin-top: 22px; }
.btn-secondary {
    background: var(--surface);
    color: var(--primary);
    border: 1px solid var(--primary);
    border-radius: 8px;
    padding: 10px 20px;
    font-size: 15px;
    font-weight: 600;
    cursor: pointer;
}
.btn-secondary:hover { background: var(--unread-bg); }
.success { background: #ecfdf5; color: #047857; border-radius: 8px; padding: 8px 12px; font-size: 14px; margin-top: 12px; }
.settings .alert { margin-top: 12px; }
.warning { background: #fffbeb; color: #92400e; border: 1px solid #fcd34d; border-radius: 8px; padding: 10px 12px; font-size: 14px; margin-top: 12px; }
.warning ul { margin: 4px 0 0; padding-left: 18px; }
.notify-status h2 + dl { margin-bottom: 8px; }
.notify-status h2:not(:first-child) { margin-top: 18px; }
.small { font-size: 13px; }
.notify-status { margin-top: 20px; font-size: 14px; }
.notify-status h2 { font-size: 15px; margin: 0 0 6px; }
.notify-status dl { display: grid; grid-template-columns: auto 1fr; gap: 4px 16px; margin: 0; }
.notify-status dt { color: var(--muted); }
.notify-status dd { margin: 0; overflow-wrap: anywhere; }

@media (max-width: 480px) {
    .topbar .me { display: none; }
    .summary input { margin-left: 0; width: 100%; }
    .msg { max-width: 88%; }
}
