:root {
  color-scheme: dark;
  accent-color: #fee500;
  --bg-outer: #07080a;
  --bg-room: #0c0d10;
  --bg-header: rgba(16, 18, 24, 0.92);
  --bg-composer: #13151b;
  --bg-input: #1b1e26;
  --bg-other-bubble: #1e212b;
  --fg-other-bubble: #f1f3f7;
  --bg-my-bubble: #fee500;
  --fg-my-bubble: #191919;
  --bg-date-pill: rgba(255, 255, 255, 0.08);
  --fg-date-pill: #b5bcc9;
  --fg-primary: #f3f4f6;
  --fg-secondary: #9ca3af;
  --fg-muted: #6b7280;
  --unread-color: #fee500;
  --border-subtle: rgba(255, 255, 255, 0.08);
  --bg-dialog: #151821;
  --bg-notice: rgba(254, 229, 0, 0.1);
  --border-notice: rgba(254, 229, 0, 0.25);
  --scroll-thumb: #3b4152;
  --scroll-track: #0c0d10;
  --reply-other-bg: rgba(255, 255, 255, 0.07);
  --reply-my-bg: rgba(0, 0, 0, 0.09);
}

[data-theme="kakao"] {
  color-scheme: light;
  accent-color: #3c1e1e;
  --bg-outer: #9bbbd4;
  --bg-room: #b2c7da;
  --bg-header: rgba(169, 192, 214, 0.95);
  --bg-composer: #ffffff;
  --bg-input: #f4f5f7;
  --bg-other-bubble: #ffffff;
  --fg-other-bubble: #191919;
  --bg-my-bubble: #fee500;
  --fg-my-bubble: #191919;
  --bg-date-pill: rgba(0, 0, 0, 0.16);
  --fg-date-pill: #ffffff;
  --fg-primary: #191919;
  --fg-secondary: #425161;
  --fg-muted: #516275;
  --unread-color: #ffeb33;
  --border-subtle: rgba(0, 0, 0, 0.09);
  --bg-dialog: #ffffff;
  --bg-notice: rgba(255, 255, 255, 0.92);
  --border-notice: rgba(0, 0, 0, 0.08);
  --scroll-thumb: #7b93a8;
  --scroll-track: #b2c7da;
  --reply-other-bg: rgba(0, 0, 0, 0.05);
  --reply-my-bg: rgba(0, 0, 0, 0.08);
}

*, *::before, *::after {
  box-sizing: border-box;
}

html, body {
  margin: 0;
  padding: 0;
  height: 100%;
  background: var(--bg-outer);
  color: var(--fg-primary);
  font-family: -apple-system, BlinkMacSystemFont, "Apple SD Gothic Neo", "Pretendard", "Malgun Gothic", sans-serif;
  -webkit-font-smoothing: antialiased;
}

::selection {
  background: rgba(254, 229, 0, 0.35);
  color: inherit;
}

.app-shell {
  display: flex;
  flex-direction: column;
  width: 100%;
  max-width: 820px;
  height: 100vh;
  height: 100dvh;
  margin: 0 auto;
  background: var(--bg-room);
  border-left: 1px solid var(--border-subtle);
  border-right: 1px solid var(--border-subtle);
  position: relative;
  overflow: hidden;
}

/* Header */
.room-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  padding: 10px 14px;
  background: var(--bg-header);
  backdrop-filter: blur(12px);
  border-bottom: 1px solid var(--border-subtle);
  z-index: 20;
  user-select: none;
}

.room-brand {
  display: flex;
  align-items: center;
  gap: 10px;
  min-width: 0;
}

.brand-logo {
  width: 34px;
  height: 34px;
  border-radius: 10px;
  flex-shrink: 0;
}

.room-title-wrap {
  min-width: 0;
}

.room-title-row {
  display: flex;
  align-items: center;
  gap: 6px;
}

.room-title {
  font-size: 0.98rem;
  font-weight: 700;
  margin: 0;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.online-pill {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  font-size: 0.78rem;
  font-weight: 600;
  padding: 2px 8px;
  border-radius: 999px;
  background: rgba(32, 201, 151, 0.15);
  color: #20c997;
  border: 1px solid rgba(32, 201, 151, 0.3);
  cursor: pointer;
}

[data-theme="kakao"] .online-pill {
  background: rgba(0, 0, 0, 0.08);
  color: #1f2937;
  border-color: rgba(0, 0, 0, 0.12);
}

.conn-dot {
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: #20c997;
  display: inline-block;
}

.conn-dot.offline {
  background: #ef4444;
}

.room-subtitle {
  font-size: 0.72rem;
  color: var(--fg-secondary);
  margin-top: 1px;
}

.header-actions {
  display: flex;
  align-items: center;
  gap: 6px;
  flex-shrink: 0;
}

.icon-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 5px;
  height: 34px;
  padding: 0 10px;
  border-radius: 9px;
  border: 1px solid var(--border-subtle);
  background: rgba(255, 255, 255, 0.04);
  color: var(--fg-primary);
  font-size: 0.8rem;
  font-weight: 600;
  cursor: pointer;
  transition: background 0.15s ease, transform 0.1s ease;
  user-select: none;
}

[data-theme="kakao"] .icon-btn {
  background: rgba(255, 255, 255, 0.55);
}

.icon-btn:hover {
  background: rgba(255, 255, 255, 0.11);
}

.icon-btn:active {
  transform: scale(0.97);
}

.profile-btn {
  max-width: 155px;
}

.profile-btn-name {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

/* Pinned Notice Banner */
.notice-banner {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 10px;
  margin: 8px 12px 0;
  padding: 9px 12px;
  border-radius: 11px;
  background: var(--bg-notice);
  border: 1px solid var(--border-notice);
  font-size: 0.83rem;
  z-index: 15;
}

.notice-banner[hidden] {
  display: none;
}

.notice-text {
  flex: 1;
  line-height: 1.4;
  word-break: break-word;
}

/* Scrollable Message Stream */
.message-stream {
  flex: 1;
  overflow-y: auto;
  overflow-x: hidden;
  padding: 14px 14px 10px;
  display: flex;
  flex-direction: column;
  gap: 2px;
  scrollbar-width: thin;
  scrollbar-color: var(--scroll-thumb) var(--scroll-track);
  overscroll-behavior-y: contain;
}

.date-divider {
  align-self: center;
  margin: 12px 0 8px;
  padding: 4px 13px;
  border-radius: 999px;
  background: var(--bg-date-pill);
  color: var(--fg-date-pill);
  font-size: 0.74rem;
  font-weight: 500;
  user-select: none;
}

.msg-row {
  display: flex;
  align-items: flex-start;
  gap: 8px;
  margin-top: 8px;
  max-width: 100%;
  position: relative;
}

.msg-row.grouped {
  margin-top: 3px;
}

.msg-row.mine {
  flex-direction: row-reverse;
}

.msg-avatar {
  width: 36px;
  height: 36px;
  border-radius: 13px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 700;
  font-size: 0.92rem;
  color: #ffffff;
  flex-shrink: 0;
  user-select: none;
  box-shadow: 0 2px 5px rgba(0, 0, 0, 0.15);
}

.msg-row.grouped .msg-avatar {
  visibility: hidden;
  height: 0;
}

.msg-row.mine .msg-avatar {
  display: none;
}

.msg-body {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  max-width: min(76%, 520px);
  min-width: 0;
}

.msg-row.mine .msg-body {
  align-items: flex-end;
}

.msg-sender-line {
  display: flex;
  align-items: center;
  gap: 5px;
  margin: 0 0 4px 2px;
  font-size: 0.78rem;
  font-weight: 600;
  color: var(--fg-secondary);
  user-select: none;
}

.msg-row.grouped .msg-sender-line,
.msg-row.mine .msg-sender-line {
  display: none;
}

.role-badge {
  font-size: 0.66rem;
  padding: 1px 5px;
  border-radius: 5px;
  font-weight: 700;
}

.role-badge.admin {
  background: rgba(254, 229, 0, 0.2);
  color: #f59f00;
}

[data-theme="kakao"] .role-badge.admin {
  background: #191919;
  color: #fee500;
}

.role-badge.member {
  background: rgba(51, 154, 240, 0.18);
  color: #4dabf7;
}

[data-theme="kakao"] .role-badge.member {
  background: rgba(25, 113, 194, 0.14);
  color: #1864ab;
}

.role-badge.pin {
  background: rgba(132, 94, 247, 0.18);
  color: #9775fa;
}

[data-theme="kakao"] .role-badge.pin {
  background: rgba(95, 61, 196, 0.14);
  color: #5f3dc4;
}

.bubble-line {
  display: flex;
  align-items: flex-end;
  gap: 6px;
  max-width: 100%;
}

.msg-row.mine .bubble-line {
  flex-direction: row-reverse;
}

.msg-bubble {
  padding: 9px 12px;
  border-radius: 15px;
  background: var(--bg-other-bubble);
  color: var(--fg-other-bubble);
  font-size: 0.92rem;
  line-height: 1.46;
  word-break: break-word;
  overflow-wrap: anywhere;
  white-space: pre-wrap;
  box-shadow: 0 1px 2px rgba(0, 0, 0, 0.12);
  position: relative;
}

.msg-row:not(.mine):not(.grouped) .msg-bubble {
  border-top-left-radius: 4px;
}

.msg-row.mine .msg-bubble {
  background: var(--bg-my-bubble);
  color: var(--fg-my-bubble);
}

.msg-row.mine:not(.grouped) .msg-bubble {
  border-top-right-radius: 4px;
}

.msg-bubble.deleted {
  opacity: 0.62;
  font-style: italic;
  background: rgba(128, 128, 128, 0.14);
  color: var(--fg-secondary);
}

.msg-bubble a {
  color: inherit;
  text-decoration: underline;
  text-underline-offset: 2px;
}

.reply-quote {
  display: block;
  padding: 5px 8px;
  margin-bottom: 6px;
  border-radius: 8px;
  background: var(--reply-other-bg);
  border-left: 3px solid #fee500;
  font-size: 0.77rem;
  cursor: pointer;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  max-width: 280px;
}

.msg-row.mine .reply-quote {
  background: var(--reply-my-bg);
  border-left-color: #191919;
}

.reply-quote-author {
  font-weight: 700;
  display: block;
  margin-bottom: 1px;
}

.msg-image-wrap {
  margin: -3px -5px 2px;
  border-radius: 11px;
  overflow: hidden;
  cursor: zoom-in;
  max-width: 280px;
}

.msg-image {
  display: block;
  max-width: 100%;
  max-height: 300px;
  width: auto;
  height: auto;
  object-fit: cover;
  border-radius: 10px;
}

.msg-caption {
  margin-top: 6px;
}

.msg-file-card {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 4px 2px;
  min-width: 190px;
  text-decoration: none !important;
}

.msg-file-icon {
  width: 36px;
  height: 36px;
  border-radius: 9px;
  background: rgba(0, 0, 0, 0.12);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 1.1rem;
  flex-shrink: 0;
}

.msg-file-meta {
  min-width: 0;
  flex: 1;
}

.msg-file-name {
  font-size: 0.84rem;
  font-weight: 600;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.msg-file-size {
  font-size: 0.72rem;
  opacity: 0.75;
}

.msg-meta {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  gap: 1px;
  flex-shrink: 0;
  user-select: none;
  padding-bottom: 1px;
}

.msg-row.mine .msg-meta {
  align-items: flex-end;
}

.msg-unread {
  font-size: 0.71rem;
  font-weight: 800;
  color: var(--unread-color);
  line-height: 1.1;
}

[data-theme="kakao"] .msg-unread {
  text-shadow: 0 1px 1px rgba(0, 0, 0, 0.25);
}

.msg-time {
  font-size: 0.69rem;
  color: var(--fg-muted);
  white-space: nowrap;
}

.msg-row.hide-time .msg-time {
  display: none;
}

/* Quick Hover Actions (Reply / Delete) */
.msg-actions {
  display: inline-flex;
  gap: 3px;
  opacity: 0;
  transition: opacity 0.12s ease;
  user-select: none;
}

.msg-row:hover .msg-actions,
.msg-row:focus-within .msg-actions {
  opacity: 1;
}

@media (hover: none) {
  .msg-actions {
    opacity: 0.75;
  }
}

.msg-act-btn {
  border: none;
  background: rgba(128, 128, 128, 0.16);
  color: var(--fg-secondary);
  border-radius: 6px;
  padding: 2px 6px;
  font-size: 0.7rem;
  cursor: pointer;
}

.msg-act-btn:hover {
  background: rgba(128, 128, 128, 0.3);
  color: var(--fg-primary);
}

.msg-highlight .msg-bubble {
  outline: 2px solid #fee500;
  outline-offset: 2px;
}

/* Floating New Message / Scroll Bottom Button */
.scroll-bottom-btn {
  position: absolute;
  right: 16px;
  bottom: 84px;
  padding: 7px 13px;
  border-radius: 999px;
  border: 1px solid var(--border-subtle);
  background: var(--bg-dialog);
  color: var(--fg-primary);
  font-size: 0.78rem;
  font-weight: 600;
  box-shadow: 0 4px 14px rgba(0, 0, 0, 0.32);
  cursor: pointer;
  z-index: 18;
}

.scroll-bottom-btn[hidden] {
  display: none;
}

/* Typing & Reply Bar */
.typing-bar {
  padding: 3px 16px;
  font-size: 0.74rem;
  color: var(--fg-secondary);
  min-height: 20px;
  user-select: none;
}

.typing-bar:empty {
  display: none;
}

.reply-composer-bar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  padding: 7px 14px;
  background: var(--bg-composer);
  border-top: 1px solid var(--border-subtle);
  font-size: 0.8rem;
}

.reply-composer-bar[hidden] {
  display: none;
}

.reply-composer-text {
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  color: var(--fg-secondary);
}

/* Quick Entry Bar (Shown before first login/guest entry) */
.quick-join-bar {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 8px;
  padding: 10px 14px;
  background: var(--bg-composer);
  border-top: 1px solid var(--border-subtle);
}

.quick-join-bar[hidden] {
  display: none;
}

.quick-nick-input {
  flex: 1;
  min-width: 140px;
  height: 38px;
  padding: 0 12px;
  border-radius: 10px;
  border: 1px solid var(--border-subtle);
  background: var(--bg-input);
  color: var(--fg-primary);
  font-size: 0.9rem;
}

.btn-kakao {
  height: 38px;
  padding: 0 15px;
  border-radius: 10px;
  border: none;
  background: #fee500;
  color: #191919;
  font-weight: 700;
  font-size: 0.86rem;
  cursor: pointer;
  user-select: none;
}

.btn-kakao:disabled {
  opacity: 0.45;
  cursor: not-allowed;
}

.btn-secondary {
  height: 38px;
  padding: 0 11px;
  border-radius: 10px;
  border: 1px solid var(--border-subtle);
  background: var(--bg-input);
  color: var(--fg-primary);
  font-weight: 600;
  font-size: 0.81rem;
  cursor: pointer;
  user-select: none;
}

/* Composer Area */
.composer-wrap {
  display: flex;
  align-items: flex-end;
  gap: 8px;
  padding: 9px 12px calc(9px + env(safe-area-inset-bottom, 0px));
  background: var(--bg-composer);
  border-top: 1px solid var(--border-subtle);
}

.composer-wrap[hidden] {
  display: none;
}

.attach-btn {
  width: 38px;
  height: 38px;
  border-radius: 11px;
  border: 1px solid var(--border-subtle);
  background: var(--bg-input);
  color: var(--fg-primary);
  font-size: 1.25rem;
  line-height: 1;
  cursor: pointer;
  flex-shrink: 0;
  user-select: none;
}

.growable-textarea {
  flex: 1;
  min-width: 0;
}

.chat-textarea {
  field-sizing: content;
  height: auto;
  width: 100%;
  min-block-size: 1.5lh;
  max-block-size: 6lh;
  padding: 9px 12px;
  border-radius: 12px;
  border: 1px solid var(--border-subtle);
  background: var(--bg-input);
  color: var(--fg-primary);
  font: inherit;
  font-size: 0.93rem;
  line-height: 1.42;
  resize: none;
  outline: none;
}

.chat-textarea:focus {
  border-color: rgba(254, 229, 0, 0.55);
}

@supports not (field-sizing: content) {
  .growable-textarea {
    display: grid;
  }
  .growable-textarea::after,
  .growable-textarea > .chat-textarea {
    grid-area: 1 / 1 / 2 / 2;
    font: inherit;
    font-size: 0.93rem;
    line-height: 1.42;
    padding: 9px 12px;
    max-height: 8.6rem;
  }
  .growable-textarea::after {
    content: attr(data-replicated-value) " ";
    white-space: pre-wrap;
    word-break: break-word;
    visibility: hidden;
  }
  .growable-textarea > .chat-textarea {
    overflow-y: auto;
  }
}

.send-btn {
  height: 38px;
  padding: 0 16px;
  border-radius: 11px;
  border: none;
  background: #fee500;
  color: #191919;
  font-weight: 700;
  font-size: 0.88rem;
  cursor: pointer;
  flex-shrink: 0;
  user-select: none;
}

/* Drag-and-drop overlay */
.drop-overlay {
  position: absolute;
  inset: 0;
  background: rgba(12, 13, 16, 0.82);
  border: 3px dashed #fee500;
  border-radius: 12px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 1.1rem;
  font-weight: 700;
  color: #fee500;
  z-index: 40;
  pointer-events: none;
}

.drop-overlay[hidden] {
  display: none;
}

/* Native <dialog> Modals */
dialog.modal-card {
  width: min(92vw, 430px);
  border: 1px solid var(--border-subtle);
  border-radius: 18px;
  padding: 20px;
  background: var(--bg-dialog);
  color: var(--fg-primary);
  box-shadow: 0 20px 50px rgba(0, 0, 0, 0.5);
}

dialog::backdrop {
  background-color: rgba(0, 0, 0, 0.58);
  backdrop-filter: blur(3px);
}

.modal-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 14px;
}

.modal-title {
  margin: 0;
  font-size: 1.05rem;
  font-weight: 700;
}

.modal-close {
  border: none;
  background: transparent;
  color: var(--fg-secondary);
  font-size: 1.25rem;
  cursor: pointer;
  padding: 2px 6px;
}

.auth-tabs {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 6px;
  padding: 4px;
  border-radius: 11px;
  background: var(--bg-input);
  margin-bottom: 14px;
}

.auth-tab {
  border: none;
  border-radius: 8px;
  padding: 8px 4px;
  background: transparent;
  color: var(--fg-secondary);
  font-size: 0.8rem;
  font-weight: 700;
  cursor: pointer;
}

.auth-tab.active {
  background: #fee500;
  color: #191919;
}

.form-stack {
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.form-stack[hidden] {
  display: none;
}

.field-label {
  display: flex;
  flex-direction: column;
  gap: 5px;
  font-size: 0.8rem;
  font-weight: 600;
  color: var(--fg-secondary);
}

.modal-input {
  height: 40px;
  padding: 0 12px;
  border-radius: 10px;
  border: 1px solid var(--border-subtle);
  background: var(--bg-input);
  color: var(--fg-primary);
  font-size: 0.9rem;
}

input:not(input[type="range"]):user-invalid {
  border-color: #ef4444;
}

input:not(input[type="range"]):user-valid {
  border-color: rgba(32, 201, 151, 0.45);
}

.help-note {
  margin: 0;
  font-size: 0.76rem;
  color: var(--fg-secondary);
  line-height: 1.45;
}

.user-list {
  list-style: none;
  padding: 0;
  margin: 0;
  max-height: 320px;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.user-list-item {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 6px 8px;
  border-radius: 10px;
  background: var(--bg-input);
}

.stat-grid {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 8px;
  margin-bottom: 12px;
}

.stat-box {
  padding: 10px;
  border-radius: 10px;
  background: var(--bg-input);
  border: 1px solid var(--border-subtle);
}

.stat-label {
  font-size: 0.73rem;
  color: var(--fg-secondary);
}

.stat-value {
  font-size: 0.96rem;
  font-weight: 700;
  margin-top: 3px;
}

dialog.lightbox-dialog {
  max-width: 94vw;
  max-height: 92vh;
  padding: 10px;
  border: none;
  border-radius: 14px;
  background: rgba(12, 13, 16, 0.95);
  color: #fff;
}

.lightbox-img {
  display: block;
  max-width: 88vw;
  max-height: 78vh;
  object-fit: contain;
  border-radius: 8px;
  margin: 0 auto;
}

.lightbox-bar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-top: 8px;
  gap: 10px;
}

.toast {
  position: fixed;
  bottom: 76px;
  left: 50%;
  transform: translateX(-50%);
  background: rgba(20, 22, 29, 0.94);
  color: #fff;
  padding: 9px 16px;
  border-radius: 999px;
  font-size: 0.83rem;
  font-weight: 600;
  border: 1px solid rgba(255, 255, 255, 0.14);
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.4);
  z-index: 9999;
}

.toast[hidden] {
  display: none;
}

@media (max-width: 600px) {
  .app-shell {
    border-left: none;
    border-right: none;
  }
  .hide-mobile {
    display: none;
  }
  .msg-body {
    max-width: 80%;
  }
}
