/* ── 라이트 모드 토큰 (기본) ── */
:root {
  color-scheme: light;
  --page:   #f5f5f5;
  --bar:    #111111;
  --ink:    #111111;
  --muted:  #666666;
  --line:   #d0d0d0;
  --field:  #ffffff;
  --soft:   #f0f0f0;
  --mine:   #e8e8e8;
  --side:   #eeeeee;
  font-family: "SF Mono", "Fira Mono", "Consolas", "Menlo", monospace;
}

/* ── 다크 모드 토큰 ── */
[data-theme="dark"] {
  color-scheme: dark;
  --page:   #0d0d0d;
  --bar:    #0a0a0a;
  --ink:    #e0e0e0;
  --muted:  #888888;
  --line:   #2a2a2a;
  --field:  #181818;
  --soft:   #141414;
  --mine:   #1e1e1e;
  --side:   #111111;
}

* { box-sizing: border-box; margin: 0; padding: 0; }

/* ── 핵심 수정: html/body 에 고정 높이를 명시해야
   하위 grid 의 overflow:auto 가 실제로 독립 스크롤로 동작함.
   height: 100% 체인이 끊기면 .messages 가 넘치는 만큼 부모 전체가 늘어나서
   페이지 자체가 스크롤되는 문제가 생긴다. ── */
html, body {
  height: 100%;
  height: 100dvh;
  overflow: hidden; /* 페이지 자체는 스크롤되지 않게 막음 */
}

body {
  background: #e5e5e5;
  color: var(--ink);
  font-size: 12.5px;
  line-height: 1.6;
  transition: background 0.2s, color 0.2s;
}

[data-theme="dark"] body {
  background: #080808;
}

button, input, textarea { font: inherit; }

/* ── Buttons ── */
button {
  min-height: 32px;
  border: 1px solid #999;
  border-radius: 0;
  background: #e8e8e8;
  color: #111;
  cursor: pointer;
  padding: 2px 10px;
  letter-spacing: 0.02em;
  transition: background 80ms;
  white-space: nowrap;
}
button:hover  { background: #fff; border-color: #444; }
button:active { background: #ccc; }
button:disabled { cursor: not-allowed; opacity: 0.45; }
button.copied { background: #2fae63; border-color: #218a4c; color: #fff; animation: copyBtnPulse 0.4s ease; }
@keyframes copyBtnPulse { 0% { transform: scale(1); } 40% { transform: scale(1.08); } 100% { transform: scale(1); } }
@media (prefers-reduced-motion: reduce) { button.copied { animation: none; } }

[data-theme="dark"] button {
  background: #1a1a1a;
  border-color: #444;
  color: #ccc;
}
[data-theme="dark"] button:hover { background: #2a2a2a; border-color: #888; }
[data-theme="dark"] button:active { background: #111; }

/* ── Layout ──
   shell 에 명시적 height: 100% 를 줘서 (html/body 의 height: 100dvh 를 그대로 상속)
   안쪽 grid 가 화면 높이를 절대 넘지 않게 고정한다.
   이게 핵심: 부모 체인의 모든 단계에 height 가 명시되어 있어야
   .messages 의 overflow-y: auto 가 "이 영역 안에서만" 스크롤된다. ── */
.shell {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 220px;
  width: min(1060px, 100vw);
  height: 100%;
  margin: 0 auto;
  background: var(--page);
  border-left: 1px solid #bbb;
  border-right: 1px solid #bbb;
  overflow: hidden;
}

[data-theme="dark"] .shell {
  border-color: #2a2a2a;
}

.room-panel {
  display: grid;
  /* 직계 자식 8개: topbar, tab-bar, loginRequiredNotice, publicSection,
     privateSection, messages, composer, footer.
     messages 한 행만 1fr(가변), 나머지 7행은 auto.
     행 개수가 자식 개수와 정확히 일치해야 마지막 footer 가
     암묵적(implicit) 행으로 밀려나 전체 높이를 초과하지 않는다. */
  grid-template-rows: auto auto auto auto auto minmax(0, 1fr) auto auto;
  height: 100%;
  min-height: 0;
  overflow: hidden;
}

.side-panel {
  background: var(--side);
  border-left: 1px solid var(--line);
  padding: 10px;
  height: 100%;
  overflow-y: auto;
}

/* ── Top bar ── */
.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  padding: 6px 8px;
  background: var(--bar);
  border-bottom: 2px solid #000;
  flex-wrap: wrap;
}
.brand-row { display: flex; align-items: center; gap: 8px; flex-shrink: 0; color: inherit; text-decoration: none; }
.mark {
  display: inline-grid;
  place-items: center;
  width: 20px; height: 20px;
  border: 1px solid #fff;
  color: #fff;
  font-weight: 700;
  font-size: 12px;
  flex-shrink: 0;
}
h1 { font-size: 13px; color: #fff; letter-spacing: 0.05em; font-weight: 700; }
h2 { font-size: 11px; padding: 0 0 8px; font-weight: 700; letter-spacing: 0.04em;
     text-transform: uppercase; border-bottom: 1px solid var(--line); margin-bottom: 8px; }
.eyebrow { color: #888; font-size: 9px; letter-spacing: 0.04em; margin-top: 1px; }

/* ── 탑바 우측 컨트롤 (INFO / DARK 토글) ── */
.topbar-controls {
  display: flex;
  align-items: center;
  gap: 6px;
  flex-shrink: 0;
}

.topbar-btn {
  min-height: unset;
  background: transparent;
  border: 1px solid #444;
  color: #888;
  font-size: 9px;
  letter-spacing: 0.08em;
  padding: 2px 6px;
  cursor: pointer;
  transition: background 80ms, color 80ms;
}
.topbar-btn:hover {
  background: #fff;
  border-color: #fff;
  color: #000;
}

[data-theme="dark"] .topbar-btn {
  border-color: #444;
  color: #666;
}
[data-theme="dark"] .topbar-btn:hover {
  background: #e0e0e0;
  border-color: #e0e0e0;
  color: #000;
}

/* ── Status badges ── */
.status-stack { display: flex; gap: 4px; align-items: center; flex-wrap: wrap; }
.status {
  display: inline-flex; align-items: center;
  min-height: 18px; padding: 1px 6px;
  border: 1px solid #555;
  color: #bbb; background: #2a2a2a;
  font-size: 10px; white-space: nowrap; letter-spacing: 0.02em;
}
.status.good { color: #bfffbf; background: #1a2e1a; border-color: #3a5a3a; }
.status.warn { color: #ffe0a0; background: #2e2010; border-color: #6a4a10; }

/* ── 탭바 (공개/개인 채팅방) ── */
.tab-bar {
  display: flex;
  border-bottom: 1px solid var(--line);
  background: var(--soft);
  flex-shrink: 0;
}
.tab-btn {
  flex: 1;
  padding: 10px;
  background: transparent;
  border: none;
  border-bottom: 2px solid transparent;
  color: var(--muted);
  cursor: pointer;
  font-size: 12px;
  letter-spacing: 0.02em;
}
.tab-btn.active {
  color: var(--ink);
  border-bottom-color: var(--ink);
  font-weight: 700;
}
[data-theme="dark"] .tab-btn.active { border-bottom-color: #e0e0e0; }

/* ── 공개방 카드 그리드 ── */
.public-rooms {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(160px, 1fr));
  gap: 8px;
  padding: 10px 8px;
}
.room-card {
  border: 1px solid var(--line);
  background: var(--field);
  padding: 14px 12px;
  cursor: pointer;
  transition: background 80ms, transform 80ms;
}
.room-card:hover { background: var(--soft); transform: translateY(-2px); }
.room-card-name { font-weight: 700; font-size: 13px; margin-bottom: 4px; }
.room-card-desc { font-size: 10.5px; color: var(--muted); margin-bottom: 8px; }
.room-card-badge {
  display: inline-block;
  font-size: 9.5px;
  padding: 2px 7px;
  border: 1px solid var(--line);
  background: var(--soft);
  color: var(--muted);
}

/* ── 공개방 메시지: 보낸이 표시 강조 ── */
.message.public .message-meta .sender { color: #2a6cd6; }
[data-theme="dark"] .message.public .message-meta .sender { color: #6ca8ff; }

.rate-limit-msg {
  text-align: center;
  color: #b56;
  font-size: 10px;
  padding: 2px 0;
}

/* ── Setup grid ── */
.setup-grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 6px 10px;
  padding: 8px;
  border-bottom: 1px solid var(--line);
  background: var(--page);
}
label { display: grid; gap: 3px; color: var(--muted); font-size: 10px; letter-spacing: 0.03em; }
input {
  width: 100%; height: 32px; padding: 2px 6px;
  border: 1px solid var(--line); border-radius: 0;
  background: var(--field); color: var(--ink);
  outline: none; font: inherit;
}
input:focus { border-color: var(--ink); box-shadow: inset 0 0 0 1px var(--ink); }
.inline-control { display: grid; grid-template-columns: minmax(0,1fr) 32px; gap: 3px; }

/* ── Actions ── */
.actions {
  display: flex; flex-wrap: wrap; gap: 5px;
  padding: 6px 8px;
  border-bottom: 1px solid var(--line);
  background: var(--page);
}
.primary { background: var(--bar); border-color: #000; color: #fff; font-weight: 700; }
.primary:hover { background: #333; }

/* ── Notice ── */
.notice {
  margin: 6px 8px;
  padding: 5px 8px;
  border: 1px solid var(--line);
  border-left: 3px solid #888;
  background: var(--soft);
  color: var(--muted); font-size: 10.5px; line-height: 1.5;
}

/* ── Messages — 독립 스크롤 + 항상 하단 기준 ──
   flex-direction: column-reverse 로 바꾸면 콘텐츠가 적어도
   메시지 영역이 "위에서부터 채워지는 느낌" 없이 항상 입력창 바로 위가
   기준점이 된다. 카카오톡/디스코드 등 메신저 UI와 동일한 패턴.
   DOM 순서는 그대로 두고 시각적 순서만 뒤집으므로 JS는 수정할 필요 없음. ── */
.messages {
  display: flex;
  flex-direction: column-reverse;
  gap: 4px;
  padding: 8px;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  min-height: 0;
  background: var(--page);
  grid-row: 6;
}
.messages.drag-over { outline: 2px dashed #555; outline-offset: -4px; background: var(--soft); }
.empty {
  margin: auto 0 0 0;
  color: var(--muted); text-align: center;
  line-height: 1.8; font-size: 11px; letter-spacing: 0.03em;
  width: 100%;
}
.system-msg {
  text-align: center; color: var(--muted);
  font-size: 10px; letter-spacing: 0.03em;
  padding: 2px 0;
}
.message {
  width: min(86%, 680px);
  padding: 5px 8px;
  border-left: 2px solid var(--line);
  background: var(--field);
  border-bottom: 1px solid var(--line);
  word-break: break-word;
}
.message.mine { align-self: flex-end; border-left: 2px solid var(--ink); background: var(--mine); }
.message-meta {
  display: flex; justify-content: space-between; gap: 10px;
  margin-bottom: 3px; color: var(--muted); font-size: 9.5px;
}
.message-meta .sender { color: var(--ink); font-weight: 700; }
.message p { white-space: pre-wrap; overflow-wrap: anywhere; line-height: 1.5; }

/* ── Image & File ── */
.msg-image {
  display: block;
  max-width: 100%;
  max-height: 240px;
  object-fit: contain;
  cursor: pointer;
  border: 1px solid var(--line);
  margin-top: 2px;
}
.msg-file {
  display: inline-flex; align-items: center; gap: 4px;
  padding: 4px 8px;
  border: 1px solid var(--line);
  background: var(--soft);
  color: var(--ink);
  text-decoration: none;
  font-size: 11px;
  margin-top: 2px;
}
.msg-file:hover { background: var(--field); }

/* ── Composer ── */
.composer {
  display: grid;
  grid-template-columns: 40px minmax(0,1fr) auto;
  align-items: stretch;
  gap: 6px;
  padding: 8px;
  border-top: 2px solid #222;
  background: var(--soft);
  flex-shrink: 0;
  grid-row: 7;
}
[data-theme="dark"] .composer { border-top-color: #333; }
.composer input { height: 38px; min-width: 0; }
.composer button {
  background: var(--bar); border-color: #000; color: #fff; font-weight: 700;
  height: 38px;
  min-height: unset;
  padding: 0 16px;
  white-space: nowrap;
  flex-shrink: 0;
}
.composer button:hover:not(:disabled) { background: #333; }
#fileButton {
  width: 40px;
  padding: 0;
  font-size: 16px;
  display: grid;
  place-items: center;
}

/* ── 푸터 ── */
.room-footer {
  border-top: 1px solid var(--line);
  background: var(--soft);
  padding: 5px 10px;
  text-align: center;
  font-size: 9px;
  color: var(--muted);
  letter-spacing: 0.05em;
  flex-shrink: 0;
  grid-row: 8;
}

/* ── Side panel ── */
dl { display: grid; gap: 10px; }
dt { font-weight: 700; font-size: 10.5px; letter-spacing: 0.03em; color: var(--ink); }
dd { margin: 2px 0 0; color: var(--muted); font-size: 10.5px; line-height: 1.5; }

/* ── Mobile ── */
@media (max-width: 720px) {
  .shell {
    grid-template-columns: 1fr;
    grid-template-rows: minmax(0, 1fr) auto;
    width: 100%;
    height: 100%;
  }
  .room-panel {
    grid-template-rows: auto auto auto auto auto minmax(0,1fr) auto auto;
    height: 100%;
    min-height: 0;
  }
  .side-panel {
    border-left: 0;
    border-top: 1px solid var(--line);
    order: 2;
    height: auto;
    max-height: 30vh;
  }
  .setup-grid { grid-template-columns: 1fr; }
  .topbar { flex-direction: column; align-items: flex-start; }
  .status-stack { flex-wrap: wrap; }
  .message { width: 90%; }
  .message.mine { width: 90%; }
  .composer { position: sticky; bottom: 0; z-index: 10; }
}

@media (max-width: 400px) {
  .actions { gap: 4px; }
  .actions button { font-size: 11px; padding: 2px 7px; }
  .status { font-size: 9px; padding: 1px 4px; }
}
