/* ========================================================================
   Design tokens
   ======================================================================== */
:root {
  --bg: #0f1115;
  --bg-2: #171a21;
  --bg-3: #222632;
  --bg-4: #2b3040;
  --border: #2a2f3d;
  --text: #e9ebf1;
  --text-2: #c3c8d6;
  --muted: #8b92a6;
  --accent: #4f8cff;
  --accent-2: #3d78e6;
  --accent-soft: rgba(79, 140, 255, .14);
  --mine: #3672e8;
  --danger: #ef5350;
  --online: #34d399;

  --font: "Pretendard Variable", Pretendard, -apple-system, BlinkMacSystemFont, system-ui,
          "Apple SD Gothic Neo", "Noto Sans KR", "Segoe UI", Roboto, sans-serif;
  --fs-xs: 12px;
  --fs-sm: 13px;
  --fs-md: 15px;
  --fs-lg: 17px;
  --fs-xl: 22px;

  --r-sm: 10px;
  --r-md: 14px;
  --r-lg: 20px;
  --sidebar-w: 320px;
  --control-h: 44px;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --shadow-lg: 0 24px 64px rgba(0, 0, 0, .55);
}

/* ========================================================================
   Base
   ======================================================================== */
* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
html { -webkit-text-size-adjust: 100%; }
body {
  font-family: var(--font);
  font-size: var(--fs-md);
  line-height: 1.5;
  color: var(--text);
  background: var(--bg);
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  overscroll-behavior: none;
  letter-spacing: -0.01em;
}
button, input, textarea { font: inherit; letter-spacing: inherit; color: inherit; }
button { cursor: pointer; }
a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }
img { display: block; }
.hidden { display: none !important; }
.muted { color: var(--muted); font-size: var(--fs-sm); }
.error { color: var(--danger); font-size: var(--fs-sm); margin-top: 14px; }
.mobile-only { display: none; }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

/* ========================================================================
   Screens / login
   ======================================================================== */
.screen { position: fixed; inset: 0; }
.center { display: flex; align-items: center; justify-content: center; padding: 24px; }
.card {
  width: 100%; max-width: 400px;
  padding: 44px 32px 36px;
  background: var(--bg-2);
  border: 1px solid var(--border);
  border-radius: 24px;
  text-align: center;
  box-shadow: var(--shadow-lg);
}
.card .logo { font-size: 56px; line-height: 1; margin-bottom: 18px; }
.card h1 { margin: 0 0 8px; font-size: var(--fs-xl); font-weight: 700; letter-spacing: -0.02em; }
.card p { margin: 0 0 28px; font-size: var(--fs-md); color: var(--muted); }
.card .btn-lg { margin-top: 4px; }

.spinner {
  width: 36px; height: 36px;
  border: 3px solid var(--border);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin .8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* ========================================================================
   Buttons
   ======================================================================== */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 10px;
  min-height: var(--control-h);
  padding: 0 20px;
  font-size: var(--fs-md); font-weight: 600;
  background: var(--bg-3);
  border: 1px solid var(--border);
  border-radius: 12px;
  transition: background .15s, border-color .15s, transform .05s;
}
.btn:hover { background: var(--bg-4); }
.btn:active { transform: translateY(1px); }
.btn:disabled { opacity: .5; cursor: default; transform: none; }
.btn-primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn-primary:hover { background: var(--accent-2); }
.btn-lg { width: 100%; min-height: 50px; font-size: 16px; }

.icon-btn {
  position: relative;
  display: inline-flex; align-items: center; justify-content: center;
  width: 40px; height: 40px; flex-shrink: 0;
  background: none; border: none; border-radius: var(--r-sm);
  font-size: 18px; color: var(--muted);
  transition: background .15s, color .15s;
}
.icon-btn:hover { background: var(--bg-3); color: var(--text); }
.icon-btn.danger:hover { color: var(--danger); }

/* ========================================================================
   App layout
   ======================================================================== */
.app { display: flex; }

.sidebar {
  width: var(--sidebar-w); flex-shrink: 0;
  display: flex; flex-direction: column;
  background: var(--bg-2);
  border-right: 1px solid var(--border);
  padding-top: var(--safe-top);
}
.sidebar-header {
  display: flex; align-items: center; gap: 12px;
  padding: 16px 14px 16px 18px;
  border-bottom: 1px solid var(--border);
}
.me-info { flex: 1; min-width: 0; }
.me-name { font-weight: 700; font-size: var(--fs-md); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.me-email { font-size: var(--fs-xs); color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

.avatar {
  width: 40px; height: 40px; border-radius: 50%; flex-shrink: 0;
  background: var(--bg-3); object-fit: cover;
}
.avatar.sm { width: 32px; height: 32px; }

.sidebar-section { padding: 10px 10px 6px; overflow-y: auto; }
.sidebar-section + .sidebar-section { border-top: 1px solid var(--border); }
.sidebar-section:first-of-type { flex: 0 1 auto; max-height: 55%; }
.sidebar-section:last-of-type { flex: 1; }
.section-title {
  display: flex; align-items: center; justify-content: space-between;
  padding: 8px 10px 6px;
  font-size: var(--fs-xs); font-weight: 700;
  text-transform: uppercase; letter-spacing: .08em;
  color: var(--muted);
}
.section-title .icon-btn {
  width: 26px; height: 26px; border-radius: 50%;
  background: #f5c542; color: #1b1d24;
  font-size: 18px; font-weight: 700; line-height: 1;
}
.section-title .icon-btn:hover { background: #ffd35c; color: #000; }

.list { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 2px; }
.list li {
  display: flex; align-items: center; gap: 12px;
  min-height: 56px; padding: 8px 10px;
  border-radius: 12px;
  cursor: pointer; user-select: none;
  -webkit-tap-highlight-color: transparent;
  transition: background .12s;
}
.list li:hover { background: var(--bg-3); }
.list li.active { background: var(--accent-soft); }
.list li.active .item-name { color: #dbe7ff; }
.list .room-icon {
  width: 40px; height: 40px; border-radius: 50%; flex-shrink: 0;
  display: flex; align-items: center; justify-content: center;
  background: var(--bg-3); font-size: 18px;
}
.list .item-body { flex: 1; min-width: 0; }
.list .item-name { font-size: var(--fs-md); font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.list .item-sub { font-size: var(--fs-sm); color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; margin-top: 1px; }
.badge {
  min-width: 22px; height: 22px; padding: 0 7px;
  display: inline-flex; align-items: center; justify-content: center;
  background: var(--accent); color: #fff;
  font-size: 11px; font-weight: 700; border-radius: 11px;
}
.dot {
  width: 11px; height: 11px; border-radius: 50%; flex-shrink: 0;
  background: #4b5163;
  border: 2px solid var(--bg-2);
  margin-left: -20px; margin-top: 26px;
}
.dot.online { background: var(--online); }

/* ========================================================================
   Chat
   ======================================================================== */
.chat { flex: 1; min-width: 0; position: relative; display: flex; flex-direction: column; }
.chat-header {
  display: flex; align-items: center; gap: 8px;
  min-height: 64px;
  padding: calc(10px + var(--safe-top)) 14px 10px 18px;
  background: var(--bg-2);
  border-bottom: 1px solid var(--border);
}
.chat-title { flex: 1; min-width: 0; }
#room-name { font-size: var(--fs-lg); font-weight: 700; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
#room-sub { font-size: var(--fs-sm); color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

.messages {
  flex: 1; overflow-y: auto;
  padding: 20px 20px 12px;
  display: flex; flex-direction: column; gap: 2px;
  -webkit-overflow-scrolling: touch;
}
.empty-state {
  margin: auto; max-width: 300px;
  text-align: center; color: var(--muted); font-size: var(--fs-md); line-height: 1.6;
}
.date-sep {
  align-self: center;
  margin: 18px 0 10px;
  padding: 4px 12px;
  font-size: var(--fs-xs); font-weight: 600; color: var(--muted);
  background: var(--bg-3); border-radius: 999px;
}

.msg { display: flex; gap: 10px; max-width: 78%; align-self: flex-start; margin-top: 12px; }
.msg.cont { margin-top: 3px; }
.msg.cont .avatar { visibility: hidden; height: 0; }
.msg.cont .msg-name { display: none; }
.msg.mine { align-self: flex-end; flex-direction: row-reverse; }
.msg.mine .avatar { display: none; }
.msg-body { min-width: 0; display: flex; flex-direction: column; }
.msg.mine .msg-body { align-items: flex-end; }
.msg-name { margin: 0 0 4px 4px; font-size: var(--fs-sm); font-weight: 600; color: var(--text-2); }
.msg-row { display: flex; align-items: flex-end; gap: 6px; max-width: 100%; }
.msg.mine .msg-row { flex-direction: row-reverse; }
.msg-content { min-width: 0; flex: 0 1 auto; display: flex; flex-direction: column; gap: 4px; }
.msg.mine .msg-content { align-items: flex-end; }
.msg-meta { flex-shrink: 0; display: flex; flex-direction: column; align-items: flex-start; gap: 2px; padding-bottom: 2px; }
.msg.mine .msg-meta { align-items: flex-end; }
.msg-time { font-size: 11px; color: var(--muted); white-space: nowrap; line-height: 1.2; }
.msg-del {
  width: 22px; height: 22px; padding: 0;
  background: none; border: none; border-radius: 6px;
  font-size: 13px; color: var(--muted);
  opacity: 0; transition: opacity .12s, background .12s, color .12s;
}
.msg:hover .msg-del { opacity: 1; }
.msg-del:hover { background: var(--bg-3); color: var(--danger); }

.bubble {
  padding: 10px 14px;
  background: var(--bg-3);
  border-radius: 18px; border-top-left-radius: 6px;
  font-size: var(--fs-md); line-height: 1.55;
  white-space: pre-wrap; word-break: break-word; overflow-wrap: anywhere;
  max-width: 100%;
}
.msg.mine .bubble { background: var(--mine); color: #fff; border-top-left-radius: 18px; border-top-right-radius: 6px; }
.msg.mine .bubble a { color: #dfe9ff; }
/* 이미지 말풍선: 배경 없이 이미지 크기만큼만. (% max-width 는 순환 참조로 무시되므로 px/vw 만 사용) */
.bubble.img-bubble, .msg.mine .bubble.img-bubble {
  padding: 0; background: none; border-radius: 14px; overflow: hidden;
  width: fit-content; max-width: none;
}
.bubble.img-bubble img {
  max-width: min(360px, 60vw); max-height: 400px;
  width: auto; height: auto;
  border-radius: 14px; cursor: zoom-in; background: var(--bg-3);
}
.file-card { display: flex; align-items: center; gap: 12px; color: inherit; text-decoration: none; }
.file-card:hover { text-decoration: none; }
.file-card .file-icon { font-size: 30px; line-height: 1; }
.file-card .file-name { font-weight: 600; word-break: break-all; }
.file-card .file-size { font-size: var(--fs-xs); opacity: .75; margin-top: 2px; }
.bubble.pending { opacity: .65; }
.progress { width: 180px; height: 4px; margin-top: 8px; background: rgba(255,255,255,.2); border-radius: 2px; overflow: hidden; }
.progress > div { width: 0; height: 100%; background: #fff; transition: width .2s; }

/* ========================================================================
   Composer
   ======================================================================== */
.composer {
  display: flex; align-items: flex-end; gap: 8px;
  padding: 12px 12px calc(12px + var(--safe-bottom));
  background: var(--bg-2);
  border-top: 1px solid var(--border);
}
.composer .icon-btn { width: var(--control-h); height: var(--control-h); font-size: 20px; }
.composer textarea {
  flex: 1; min-height: var(--control-h); max-height: 160px;
  padding: 11px 16px;
  font-size: var(--fs-md); line-height: 1.45;
  background: var(--bg-3);
  border: 1px solid var(--border);
  border-radius: 22px;
  resize: none; outline: none;
  transition: border-color .15s;
}
.composer textarea::placeholder { color: var(--muted); }
.composer textarea:focus { border-color: var(--accent); }
.send-btn { color: var(--accent); }
.send-btn:hover { background: var(--accent-soft); color: var(--accent); }

.drop-overlay {
  position: absolute; inset: 0; z-index: 50; pointer-events: none;
  display: flex; align-items: center; justify-content: center;
  background: rgba(79,140,255,.14);
  border: 3px dashed var(--accent);
  font-size: 20px; font-weight: 700;
}

/* ========================================================================
   Modals
   ======================================================================== */
.modal {
  position: fixed; inset: 0; z-index: 100;
  display: flex; align-items: center; justify-content: center;
  padding: 24px;
  background: rgba(0,0,0,.6);
  backdrop-filter: blur(4px); -webkit-backdrop-filter: blur(4px);
}
.modal-box {
  width: 100%; max-width: 540px; max-height: min(88vh, 88dvh);
  overflow-y: auto;
  padding: 28px 28px 24px;
  background: var(--bg-2);
  border: 1px solid var(--border);
  border-radius: var(--r-lg);
  box-shadow: var(--shadow-lg);
  animation: modal-in .18s ease-out;
}
@keyframes modal-in { from { opacity: 0; transform: translateY(8px) scale(.98); } to { opacity: 1; transform: none; } }
.modal-box h2 { margin: 0; font-size: 20px; font-weight: 700; letter-spacing: -0.02em; line-height: 1.3; }
.modal-head { display: flex; align-items: flex-start; justify-content: space-between; gap: 12px; margin-bottom: 22px; }
.modal-head .icon-btn { margin: -6px -8px 0 0; }
.modal-sub { margin: 6px 0 0; font-size: var(--fs-sm); color: var(--muted); line-height: 1.5; }
.modal-box label { display: block; margin-bottom: 18px; }
.modal-box label span, .field-label { display: block; margin-bottom: 8px; font-size: var(--fs-sm); font-weight: 600; color: var(--muted); }
.modal-box input[type=text], .modal-box input[type=email], .select {
  width: 100%; height: var(--control-h);
  padding: 0 14px;
  font-size: 16px;
  background: var(--bg-3);
  border: 1px solid var(--border);
  border-radius: 12px;
  outline: none;
  transition: border-color .15s;
}
.modal-box input:focus, .select:focus { border-color: var(--accent); outline: none; }
.select {
  -webkit-appearance: none; appearance: none;
  padding-right: 36px; cursor: pointer;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='14' height='14' viewBox='0 0 24 24' fill='none' stroke='%238b92a6' stroke-width='2.5' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpath d='m6 9 6 6 6-6'/%3E%3C/svg%3E");
  background-repeat: no-repeat; background-position: right 12px center;
}
.btn-ghost { min-height: 36px; padding: 0 10px; background: none; border-color: transparent; color: var(--accent); font-size: var(--fs-sm); }
.btn-ghost:hover { background: var(--accent-soft); }
.modal-box input::placeholder { color: var(--muted); }
.modal-actions { display: flex; justify-content: flex-end; gap: 10px; margin-top: 24px; }

.picker { display: flex; flex-direction: column; gap: 2px; max-height: 320px; overflow-y: auto; }
.picker label {
  display: flex; align-items: center; gap: 12px;
  min-height: 48px; margin: 0; padding: 6px 10px;
  border-radius: 12px; cursor: pointer;
  font-size: var(--fs-md);
  transition: background .12s;
}
.picker label:hover { background: var(--bg-3); }
.picker input[type=checkbox] { width: 20px; height: 20px; accent-color: var(--accent); flex-shrink: 0; }
.picker .empty { padding: 10px; color: var(--muted); }

.invite-form { display: grid; grid-template-columns: 1fr 116px 84px; gap: 10px; align-items: stretch; }
.invite-form .btn { padding: 0; }
.invite-toolbar { display: flex; align-items: center; justify-content: space-between; margin: 20px 0 4px; }
.invite-toolbar b { color: var(--text); font-weight: 700; }
.invite-list { list-style: none; margin: 0; padding: 0; max-height: 360px; overflow-y: auto; }
.invite-list li {
  display: grid; grid-template-columns: 40px 1fr auto 36px; align-items: center; gap: 12px;
  min-height: 64px; padding: 10px 0;
  border-bottom: 1px solid var(--border);
}
.invite-list li:last-child { border-bottom: none; }
.invite-list li.empty { display: block; padding: 24px 0; text-align: center; color: var(--muted); }
.invite-list .item-body { min-width: 0; }
.invite-list .item-name { font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.invite-list .item-sub { font-size: var(--fs-sm); color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.invite-list .placeholder-icon {
  width: 40px; height: 40px; border-radius: 50%;
  display: flex; align-items: center; justify-content: center;
  background: var(--bg-3); color: var(--muted); font-size: 18px;
}
.invite-list .icon-btn { width: 36px; height: 36px; font-size: 15px; }
.tag {
  height: 30px; min-width: 64px; padding: 0 12px;
  display: inline-flex; align-items: center; justify-content: center;
  font-size: var(--fs-sm); font-weight: 600;
  background: var(--bg-3); color: var(--muted);
  border: none; border-radius: 999px;
  white-space: nowrap; cursor: pointer;
  transition: background .12s;
}
.tag:hover { background: var(--bg-4); }
.tag.admin { background: #3b2f66; color: #d3c4ff; }
.tag:disabled { cursor: default; opacity: .8; }

/* 링크 미리보기: 화면의 90% */
.link-modal { padding: 0; }
.link-modal .modal-box {
  width: 90vw; height: 90vh; height: 90dvh;
  max-width: none; max-height: none;
  padding: 0; overflow: hidden;
  display: flex; flex-direction: column;
}
.link-head {
  display: flex; align-items: center; gap: 10px;
  padding: 12px 12px 12px 18px;
  border-bottom: 1px solid var(--border);
  background: var(--bg-2);
}
.link-url {
  flex: 1; min-width: 0;
  font-size: var(--fs-sm); color: var(--muted);
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.link-head .btn { min-height: 38px; padding: 0 14px; font-size: var(--fs-sm); white-space: nowrap; }
.link-head .tag { flex-shrink: 0; }
#link-end { border-color: rgba(239,83,80,.5); color: #ff8a87; }
#link-end:hover { background: rgba(239,83,80,.15); }
.icon-btn.live { color: #f5c542; }
.icon-btn.live::after {
  content: ""; position: absolute; top: 6px; right: 6px;
  width: 8px; height: 8px; border-radius: 50%;
  background: var(--online); box-shadow: 0 0 0 2px var(--bg-2);
}
.link-head .btn:hover { text-decoration: none; }
.link-frame { flex: 1; width: 100%; border: none; background: #fff; }
.link-foot { padding: 8px 18px; border-top: 1px solid var(--border); font-size: var(--fs-xs); }

/* ========================================================================
   Misc
   ======================================================================== */
.lightbox {
  position: fixed; inset: 0; z-index: 200;
  display: flex; align-items: center; justify-content: center;
  background: rgba(0,0,0,.94); cursor: zoom-out;
}
.lightbox img { max-width: 100vw; max-height: 100vh; object-fit: contain; }

.toast {
  position: fixed; left: 50%; bottom: calc(28px + var(--safe-bottom));
  transform: translateX(-50%);
  z-index: 300; max-width: min(90vw, 420px);
  padding: 12px 18px;
  background: #2e3341; color: var(--text);
  font-size: var(--fs-md); font-weight: 500;
  border-radius: 12px;
  box-shadow: 0 10px 30px rgba(0,0,0,.45);
}
.backdrop { position: fixed; inset: 0; z-index: 40; background: rgba(0,0,0,.55); }

/* ========================================================================
   Mobile
   ======================================================================== */
@media (max-width: 760px) {
  :root { --fs-md: 16px; --fs-sm: 14px; --fs-xs: 12px; --fs-lg: 18px; }
  .mobile-only { display: inline-flex; }

  .sidebar {
    position: fixed; top: 0; bottom: 0; left: 0; z-index: 45;
    width: min(86vw, 340px);
    transform: translateX(-100%);
    transition: transform .22s ease;
    box-shadow: 0 0 48px rgba(0,0,0,.6);
  }
  .sidebar.open { transform: translateX(0); }

  .chat-header { min-height: 60px; padding-left: 8px; }
  .messages { padding: 16px 12px 8px; }
  .msg { max-width: 90%; gap: 8px; }
  .msg-del { opacity: .55; }
  .bubble.img-bubble img { max-width: min(280px, 68vw); max-height: 320px; }
  .composer { padding: 10px 10px calc(10px + var(--safe-bottom)); }

  /* 바텀시트 스타일 모달 */
  .modal { align-items: flex-end; padding: 0; }
  .modal-box {
    max-width: none; max-height: min(92vh, 92dvh);
    padding: 24px 20px calc(20px + var(--safe-bottom));
    border-radius: var(--r-lg) var(--r-lg) 0 0;
    border-bottom: none;
    animation: sheet-in .22s ease-out;
  }
  @keyframes sheet-in { from { transform: translateY(24px); opacity: 0; } to { transform: none; opacity: 1; } }
  .modal-actions .btn { flex: 1; }
  .invite-form { grid-template-columns: 1fr 96px; }
  .invite-form input { grid-column: 1 / -1; }
  .card { padding: 40px 24px 32px; }

  .link-modal { align-items: center; }
  .link-modal .modal-box {
    width: 92vw; height: 90vh; height: 90dvh;
    padding: 0; border-radius: var(--r-md); border-bottom: 1px solid var(--border);
    animation: modal-in .18s ease-out;
  }
  .link-head { padding: 10px 8px 10px 14px; }
  .link-head .btn { padding: 0 10px; }
  .link-foot { display: none; }
}
@media (hover: none) {
  .msg-del { opacity: .55; }
  .list li:hover { background: none; }
  .list li.active { background: var(--accent-soft); }
}
