:root {
  --bg: #fff; --bg2: #f4f4f5; --line: #e4e4e7; --text: #18181b; --muted: #71717a;
  --accent: #2b7cd3; --mine: #dcefff; --theirs: #f1f1f3; --danger: #d33; --ok: #2a9d5c;
}
@media (prefers-color-scheme: dark) {
  :root { --bg: #17212b; --bg2: #0e1621; --line: #24303c; --text: #e8eaed; --muted: #8b98a5; --accent: #5aa9f0; --mine: #2b5278; --theirs: #182533; }
}
* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body { font: 15px/1.45 system-ui, -apple-system, "Segoe UI", "Malgun Gothic", sans-serif; background: var(--bg2); color: var(--text); }
button, input, textarea { font: inherit; color: inherit; }
button { cursor: pointer; border: 0; border-radius: 8px; padding: 8px 14px; background: var(--accent); color: #fff; }
button.ghost { background: transparent; color: var(--accent); padding: 6px 10px; }
button.danger { background: var(--danger); }
button:disabled { opacity: .5; cursor: default; }
input, textarea { background: var(--bg); border: 1px solid var(--line); border-radius: 8px; padding: 9px 12px; width: 100%; outline: none; }
input:focus, textarea:focus { border-color: var(--accent); }
.hidden { display: none !important; }

/* 인증 화면 */
.auth { min-height: 100%; display: grid; place-items: center; padding: 16px; }
.card { background: var(--bg); border-radius: 14px; padding: 24px; width: 100%; max-width: 380px; box-shadow: 0 4px 24px #0002; display: grid; gap: 12px; }
.card h1 { margin: 0; font-size: 22px; text-align: center; }
.card .sub { text-align: center; color: var(--muted); font-size: 13px; margin-top: -6px; }
.tabs { display: grid; grid-template-columns: 1fr 1fr; gap: 4px; background: var(--bg2); padding: 4px; border-radius: 10px; }
.tabs button { background: transparent; color: var(--muted); }
.tabs button.on { background: var(--bg); color: var(--text); box-shadow: 0 1px 3px #0002; }
.err { color: var(--danger); font-size: 13px; min-height: 1em; }

/* 메인 레이아웃 */
.layout { height: 100%; display: grid; grid-template-columns: 340px 1fr; background: var(--bg); }
.side { border-right: 1px solid var(--line); display: flex; flex-direction: column; min-height: 0; }
.side-head { padding: 10px 12px; display: flex; align-items: center; gap: 6px; border-bottom: 1px solid var(--line); }
.side-head .me { flex: 1; font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.side-head .me small { display: block; font-weight: 400; color: var(--ok); font-size: 11px; }
.search { padding: 8px 12px; }
.rooms { overflow-y: auto; flex: 1; }
.room { display: flex; gap: 10px; padding: 10px 12px; cursor: pointer; align-items: center; }
.room:hover { background: var(--bg2); }
.room.on { background: var(--accent); color: #fff; }
.room.on .prev, .room.on .time { color: #ffffffcc; }
.avatar { flex: none; width: 46px; height: 46px; border-radius: 50%; display: grid; place-items: center; color: #fff; font-weight: 600; font-size: 18px; }
.room .body { flex: 1; min-width: 0; }
.room .row { display: flex; justify-content: space-between; gap: 8px; }
.room .name { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.room .time, .room .prev { color: var(--muted); font-size: 13px; }
.room .prev { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.badge { background: var(--accent); color: #fff; border-radius: 10px; padding: 0 7px; font-size: 12px; min-width: 20px; text-align: center; }
.room.on .badge { background: #fff; color: var(--accent); }

.chat { display: flex; flex-direction: column; min-height: 0; background: var(--bg2); }
.chat-head { background: var(--bg); padding: 10px 14px; display: flex; align-items: center; gap: 10px; border-bottom: 1px solid var(--line); }
.chat-head .title { flex: 1; font-weight: 600; min-width: 0; }
.chat-head .title small { display: block; font-weight: 400; color: var(--muted); font-size: 12px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.back { display: none; }
.empty { margin: auto; color: var(--muted); text-align: center; padding: 20px; }
.msgs { flex: 1; overflow-y: auto; padding: 12px; display: flex; flex-direction: column; gap: 4px; }
.msg { max-width: min(75%, 560px); padding: 6px 10px; border-radius: 12px; background: var(--theirs); align-self: flex-start; word-break: break-word; white-space: pre-wrap; }
.msg.mine { background: var(--mine); align-self: flex-end; }
.msg .who { font-size: 12px; font-weight: 600; color: var(--accent); }
.msg .meta { font-size: 11px; color: var(--muted); text-align: right; margin-left: 12px; float: right; padding-top: 4px; }
.msg.bad { opacity: .6; font-style: italic; }
.daysep { align-self: center; font-size: 12px; color: var(--muted); background: var(--bg); padding: 2px 10px; border-radius: 10px; margin: 6px 0; }
.more { align-self: center; }
.warn { background: #fff3cd; color: #664d03; padding: 8px 14px; font-size: 13px; }
.typing { height: 18px; padding: 0 16px; font-size: 12px; color: var(--muted); }
.composer { display: flex; gap: 8px; padding: 10px; background: var(--bg); border-top: 1px solid var(--line); padding-bottom: calc(10px + env(safe-area-inset-bottom)); }
.composer textarea { resize: none; max-height: 140px; }

/* 모달 */
.modal-bg { position: fixed; inset: 0; background: #0008; display: grid; place-items: center; padding: 16px; z-index: 10; }
.modal { background: var(--bg); border-radius: 14px; padding: 18px; width: 100%; max-width: 420px; max-height: 85vh; overflow: auto; display: grid; gap: 12px; }
.modal h2 { margin: 0; font-size: 18px; }
.pick { display: flex; align-items: center; gap: 10px; padding: 8px; border-radius: 8px; cursor: pointer; }
.pick:hover { background: var(--bg2); }
.pick input { width: auto; }
.mono { font-family: ui-monospace, Consolas, monospace; letter-spacing: 1px; background: var(--bg2); padding: 10px; border-radius: 8px; text-align: center; font-size: 17px; user-select: all; }
.help { color: var(--muted); font-size: 13px; }

@media (max-width: 720px) {
  .layout { grid-template-columns: 1fr; }
  .layout.in-chat .side { display: none; }
  .layout:not(.in-chat) .chat { display: none; }
  .back { display: inline-block; }
  .msg { max-width: 85%; }
}

/* 답장/수정/파일/툴바 */
.msg { position: relative; }
.msg.sel { outline: 2px solid var(--accent); }
.msg .quote { border-left: 3px solid var(--accent); padding: 2px 8px; margin-bottom: 4px; font-size: 13px; background: #0000000a; border-radius: 4px; }
.msg .quote div { color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.msg .actions { clear: both; display: flex; gap: 4px; padding-top: 4px; }
.msg .actions button { padding: 3px 8px; font-size: 13px; }
.msg img.pic { display: block; max-width: min(320px, 100%); max-height: 320px; border-radius: 8px; margin-bottom: 4px; cursor: zoom-in; min-height: 40px; min-width: 80px; background: var(--bg2); }
.msg button.file { display: block; background: var(--bg); color: var(--text); margin-bottom: 4px; text-align: left; }
.danger-t { color: var(--danger); }
.bar { display: flex; align-items: center; gap: 8px; padding: 6px 12px; background: var(--bg); border-top: 1px solid var(--line); font-size: 13px; color: var(--muted); }
.bar-text { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.composer .attach { font-size: 18px; padding: 4px 8px; align-self: center; }
.toast { position: fixed; left: 50%; bottom: 80px; transform: translateX(-50%); background: #222e; color: #fff; padding: 10px 16px; border-radius: 10px; z-index: 20; max-width: 90vw; font-size: 14px; }

.msg .actions { flex-wrap: wrap; }
.msg .actions .emo { font-size: 18px; padding: 2px 6px; }
.msg .actions .emo.on { background: var(--bg2); }
.chips { clear: both; display: flex; flex-wrap: wrap; gap: 4px; padding-top: 4px; }
.chip { background: var(--bg); color: var(--text); border: 1px solid var(--line); border-radius: 12px; padding: 1px 8px; font-size: 13px; }
.chip.me { border-color: var(--accent); background: var(--bg2); }

.voice audio { max-width: 100%; height: 36px; }
.voice button { background: var(--bg); color: var(--text); }

.readonly { padding: 14px; text-align: center; color: var(--muted); font-size: 13px; background: var(--bg); border-top: 1px solid var(--line); padding-bottom: calc(14px + env(safe-area-inset-bottom)); }

.gauge { height: 8px; background: var(--bg2); border-radius: 4px; overflow: hidden; margin: 4px 0; }
.gauge > div { height: 100%; }
