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

html {
  overflow-x: hidden;
  max-width: 100vw;
}

body {
  background: #1a1a2e;
  font-family: 'Courier New', Courier, monospace;
  color: #ccc;
  height: 100vh;
  height: 100dvh;
  overflow: hidden;
  max-width: 100vw;
  width: 100%;
}

/* ── Scrollbars ─────────────────────────────────────────────────────────── */
::-webkit-scrollbar { width: 4px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: #00ff9033; border-radius: 2px; }

/* ── Join screen ─────────────────────────────────────────────────────────── */
#join-screen {
  display: flex;
  align-items: center;
  justify-content: center;
  height: 100vh;
  height: 100dvh;
  background: #1a1a2e;
  overflow-x: hidden;
  width: 100%;
}

.join-card {
  background: #0f0f1a;
  border: 1px solid #00ff9044;
  padding: 40px 36px;
  width: 420px;
  box-shadow: 0 0 40px #00ff9011;
}

.join-logo {
  text-align: center;
  font-size: 32px;
  font-weight: bold;
  color: #00ff90;
  letter-spacing: 6px;
  margin-bottom: 6px;
}
.join-logo span { color: #ff4d6d; }

.join-tagline {
  text-align: center;
  color: #777;
  font-size: 13px;
  letter-spacing: 2px;
  margin-bottom: 28px;
}

.avatar-picker {
  width: 80px; height: 80px;
  border-radius: 50%;
  background: #1a1a2e;
  border: 2px dashed #00ff9055;
  display: flex; align-items: center; justify-content: center;
  margin: 0 auto 22px;
  cursor: pointer;
  font-size: 13px;
  color: #777;
  text-align: center;
  line-height: 1.4;
  overflow: hidden;
  transition: border-color 0.2s;
}
.avatar-picker:hover { border-color: #00ff9099; }
.avatar-picker img { width: 100%; height: 100%; object-fit: cover; border-radius: 50%; }

.join-field { margin-bottom: 16px; }
.join-field label {
  display: block;
  color: #aaa;
  font-size: 13px;
  letter-spacing: 2px;
  margin-bottom: 6px;
}
.join-field input {
  width: 100%;
  background: #1a1a2e;
  border: 1px solid #00ff9033;
  color: #e0e0e0;
  padding: 9px 12px;
  font-family: 'Courier New', monospace;
  font-size: 14px;
  outline: none;
  transition: border-color 0.15s;
}
.join-field input:focus { border-color: #00ff90; }
.join-field input::placeholder { color: #555; }

.join-btn {
  width: 100%;
  background: #00ff9020;
  border: 1px solid #00ff9066;
  color: #00ff90;
  padding: 11px;
  font-family: 'Courier New', monospace;
  font-size: 14px;
  letter-spacing: 3px;
  cursor: pointer;
  transition: background 0.15s;
  margin-top: 4px;
}
.join-btn:hover { background: #00ff9035; }
.join-btn:disabled { opacity: 0.5; cursor: not-allowed; }

.join-note { color: #666; font-size: 13px; text-align: center; margin-top: 12px; letter-spacing: 1px; }

.join-error {
  color: #ff4d6d;
  font-size: 13px;
  text-align: center;
  margin-top: 10px;
  min-height: 18px;
}

/* ── Chat screen ─────────────────────────────────────────────────────────── */
.chat-screen {
  display: flex;
  flex-direction: column;
  height: 100vh;
  height: 100dvh;
}

/* Header */
.nc-header {
  background: #0f0f1a;
  border-bottom: 1px solid #00ff9033;
  padding: 8px 16px;
  display: flex;
  align-items: center;
  gap: 12px;
  flex-shrink: 0;
}
.nc-logo { color: #00ff90; font-size: 18px; font-weight: bold; letter-spacing: 2px; }
.nc-logo span { color: #ff4d6d; }
.nc-header-room { color: #555; font-size: 14px; }
.nc-header-count { margin-left: auto; color: #00ff90; font-size: 13px; }
.nc-sound-btn {
  background: none;
  border: 1px solid #00ff9033;
  color: #555;
  font-size: 15px;
  padding: 2px 8px;
  cursor: pointer;
  font-family: 'Courier New', monospace;
  transition: color 0.15s, border-color 0.15s;
}
.nc-sound-btn:hover { color: #aaa; border-color: #00ff9066; }
.nc-sound-btn.active { color: #00ff90; border-color: #00ff9066; }

/* Body (three columns) */
.nc-body {
  display: flex;
  flex: 1;
  overflow: hidden;
}

/* ── Left: Rooms sidebar ──────────────────────────────────────────────────── */
.rooms-sidebar {
  width: 140px;
  background: #0a0a14;
  border-right: 1px solid #00ff9022;
  display: flex;
  flex-direction: column;
  flex-shrink: 0;
  overflow: hidden;
}
.rooms-title {
  padding: 8px 10px;
  color: #00ff90;
  font-size: 12px;
  letter-spacing: 1px;
  border-bottom: 1px solid #00ff9022;
  flex-shrink: 0;
}
.room-list { flex: 1; overflow-y: auto; }
.room-item {
  padding: 7px 10px;
  cursor: pointer;
  font-size: 14px;
  color: #555;
  border-left: 2px solid transparent;
  display: flex;
  align-items: center;
  gap: 5px;
}
.room-item:hover { color: #aaa; background: #00ff9008; }
.room-item.active { color: #00ff90; border-left-color: #00ff90; background: #00ff9010; }
.room-item .unread-badge {
  margin-left: auto;
  background: #ff4d6d;
  color: #fff;
  font-size: 11px;
  padding: 1px 5px;
  border-radius: 8px;
  display: none;
}
.room-item .unread-badge.visible { display: inline; }
.room-add {
  padding: 8px 10px;
  color: #333;
  font-size: 13px;
  cursor: pointer;
  border-top: 1px solid #00ff9011;
  flex-shrink: 0;
}
.room-add:hover { color: #00ff90; }

/* ── Center: Chat area ────────────────────────────────────────────────────── */
.nc-chat {
  flex: 1;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  min-width: 0;
}
.nc-messages {
  flex: 1;
  overflow-y: auto;
  padding: 10px 14px;
  display: flex;
  flex-direction: column;
  gap: 1px;
}

/* Messages */
.msg {
  display: flex;
  gap: 8px;
  align-items: flex-start;
  padding: 1px 0;
  position: relative;
}
.msg-time {
  color: #444;
  font-size: 13px;
  min-width: 44px;
  flex-shrink: 0;
  padding-top: 1px;
}
.msg-nick {
  min-width: 80px;
  max-width: 100px;
  text-align: right;
  font-weight: bold;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  flex-shrink: 0;
}
.msg-colon { color: #333; flex-shrink: 0; }
.msg-text { color: #d4d4d4; overflow-wrap: break-word; flex: 1; min-width: 0; }

.msg-system .msg-nick { color: #444; font-weight: normal; }
.msg-system .msg-text { color: #444; font-style: italic; }

.msg-ai .msg-nick { color: #00bfff; }
.msg-ai .msg-text { color: #aaa; }

.msg-mention {
  background: #00ff9010;
  border-left: 2px solid #00ff90;
  padding-left: 4px;
  margin-left: -4px;
}

.msg-text img {
  display: block;
  max-width: 200px;
  max-height: 120px;
  border-radius: 3px;
  margin-top: 4px;
  cursor: pointer;
}
.msg-text img:hover { opacity: 0.85; }

/* Input bar */
.nc-input-bar {
  border-top: 1px solid #00ff9022;
  padding: 8px 14px;
  display: flex;
  align-items: center;
  gap: 7px;
  background: #0f0f1a;
  flex-shrink: 0;
}
.nc-input-room-label { color: #555; font-size: 13px; flex-shrink: 0; }
.nc-input-nick-label { color: #00ff90; font-weight: bold; font-size: 14px; flex-shrink: 0; white-space: nowrap; }
.nc-input-arrow { color: #333; flex-shrink: 0; }
.nc-input {
  flex: 1;
  background: transparent;
  border: none;
  color: #e0e0e0;
  font-family: 'Courier New', monospace;
  font-size: 13px;
  outline: none;
  min-width: 0;
}
.nc-attach-btn {
  cursor: pointer;
  font-size: 16px;
  opacity: 0.5;
  flex-shrink: 0;
  line-height: 1;
}
.nc-attach-btn:hover { opacity: 1; }
.nc-send-btn {
  background: #00ff9022;
  border: 1px solid #00ff9055;
  color: #00ff90;
  padding: 5px 14px;
  font-family: 'Courier New', monospace;
  font-size: 14px;
  cursor: pointer;
  letter-spacing: 1px;
  flex-shrink: 0;
}
.nc-send-btn:hover { background: #00ff9033; }

.upload-notice {
  padding: 4px 14px;
  font-size: 13px;
  color: #ffa500;
  background: #0f0f1a;
  border-top: 1px solid #00ff9011;
}

/* ── Right: User list sidebar ─────────────────────────────────────────────── */
.nc-sidebar {
  width: 152px;
  border-left: 1px solid #00ff9022;
  background: #0f0f1a;
  display: flex;
  flex-direction: column;
  flex-shrink: 0;
  overflow: hidden;
}
.nc-sidebar-title {
  padding: 8px 10px;
  color: #00ff90;
  font-size: 12px;
  letter-spacing: 1px;
  border-bottom: 1px solid #00ff9022;
  flex-shrink: 0;
}
.nc-user-list { overflow-y: auto; flex: 1; padding: 4px 0; }

.nc-section-label {
  color: #333;
  font-size: 11px;
  letter-spacing: 1px;
  padding: 6px 10px 3px;
}
.nc-user {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 4px 10px;
  font-size: 13px;
}
.nc-avatar {
  width: 20px; height: 20px;
  border-radius: 50%;
  flex-shrink: 0;
  object-fit: cover;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 10px;
  font-weight: bold;
  color: #000;
}
.nc-nick { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.nc-online-dot { color: #00ff90; font-size: 9px; flex-shrink: 0; }
.nc-mod-star { color: #ffa500; font-size: 11px; flex-shrink: 0; }

/* Image lightbox */
#lightbox {
  display: none;
  position: fixed;
  inset: 0;
  background: #000c;
  align-items: center;
  justify-content: center;
  z-index: 1000;
  cursor: zoom-out;
}
#lightbox.open { display: flex; }
#lightbox img { max-width: 90vw; max-height: 90vh; border-radius: 4px; }

/* ── Message deletion ───────────────────────────────────────────────────── */
.msg { position: relative; }
.msg-del-btn {
  display: none; position: absolute; right: 4px; top: 1px;
  background: none; border: none; color: #ff4d6d55; cursor: pointer;
  font-size: 12px; padding: 0 3px; line-height: 1;
}
.msg:hover .msg-del-btn { display: block; }
.msg-del-btn:hover { color: #ff4d6d; }

/* ── Reactions ──────────────────────────────────────────────────────────── */
.reaction-bar { display: flex; gap: 3px; flex-wrap: wrap; margin-top: 2px; padding-left: calc(44px + 80px + 12px); }

/* ── Message deletion ───────────────────────────────────────────────────── */
.msg-del-btn {
  background: none; border: 1px solid #00ff9011; color: #444;
  font-size: 13px; padding: 2px 6px; cursor: pointer; border-radius: 10px;
}
.reaction-add-btn:hover { color: #00ff90; border-color: #00ff9033; }
.emoji-picker {
  position: fixed; z-index: 500;
  background: #0f0f1a; border: 1px solid #00ff9033;
  padding: 6px; gap: 4px; flex-wrap: wrap; width: 180px;
  box-shadow: 0 4px 16px #000a; display: none;
}
.emoji-option {
  background: none; border: none; font-size: 18px; cursor: pointer;
  padding: 2px; border-radius: 4px;
}
.emoji-option:hover { background: #00ff9015; }

/* ── Typing indicator ───────────────────────────────────────────────────── */
.nc-typing {
  padding: 2px 14px 4px;
  font-size: 12px;
  color: #555;
  min-height: 16px;
  font-style: italic;
  flex-shrink: 0;
}

/* ── @Mention dropdown ──────────────────────────────────────────────────── */
.nc-chat { position: relative; }
.mention-dropdown {
  position: absolute;
  background: #0f0f1a;
  border: 1px solid #00ff9033;
  min-width: 160px;
  max-height: 180px;
  overflow-y: auto;
  z-index: 100;
  bottom: 100px;
  left: 14px;
}
.mention-item {
  padding: 6px 12px;
  cursor: pointer;
  font-size: 14px;
  color: #aaa;
}
.mention-item:hover, .mention-item.selected { background: #00ff9015; color: #00ff90; }

/* ── Markdown ────────────────────────────────────────────────────────────── */
.msg-pre { background: #0a0a14; border: 1px solid #00ff9022; padding: 6px 10px; font-size: 13px; overflow-x: auto; margin: 2px 0; white-space: pre; }
.msg-code { background: #0a0a14; border: 1px solid #00ff9022; padding: 1px 4px; font-size: 13px; }
.msg-link { color: #00bfff; text-decoration: none; }
.msg-link:hover { text-decoration: underline; }

/* ── Profile popup ──────────────────────────────────────────────────────── */
#profile-popup {
  position: fixed; z-index: 300;
  background: #0f0f1a; border: 1px solid #00ff9033;
  padding: 16px; width: 180px; box-shadow: 0 4px 24px #000a;
}
.profile-avatar {
  width: 56px; height: 56px; border-radius: 50%;
  margin: 0 auto 10px; font-size: 22px; font-weight: bold;
  display: flex; align-items: center; justify-content: center; color: #000;
  overflow: hidden;
}
.profile-avatar img { width: 100%; height: 100%; object-fit: cover; }
.profile-nick { color: #fff; font-size: 15px; font-weight: bold; text-align: center; margin-bottom: 8px; }
.profile-close {
  position: absolute; top: 6px; right: 8px;
  background: none; border: none; color: #555; cursor: pointer; font-size: 15px;
}
.profile-close:hover { color: #aaa; }

/* ── Ignore button ──────────────────────────────────────────────────────── */
.nc-ignore-btn {
  background: none; border: none; cursor: pointer;
  font-size: 12px; opacity: 0.4; padding: 0 2px; flex-shrink: 0;
}
.nc-ignore-btn:hover { opacity: 1; }

/* ── Mobile responsive ──────────────────────────────────────────────────── */
.nc-mobile-btn {
  display: none;
  background: none;
  border: 1px solid #00ff9033;
  color: #555;
  font-size: 14px;
  padding: 2px 7px;
  cursor: pointer;
}
.nc-mobile-btn:hover { color: #aaa; }

/* ── Sidebar overlay (mobile backdrop) ──────────────────────────────────── */
#sidebar-overlay {
  display: none;
  position: fixed;
  inset: 0;
  background: #000a;
  z-index: 199;
}
#sidebar-overlay.active { display: block; }

@media (max-width: 640px) {
  .nc-mobile-btn { display: inline-block; }

  /* Fix 1: lock to viewport width; height is controlled by JS visualViewport */
  body { font-size: 13px; overflow: hidden; width: 100vw; max-width: 100vw; height: 100%; }
  /* Use position:fixed so the chat fills exactly the visible area;
     JS will update top/height when the keyboard opens or closes       */
  .chat-screen { position: fixed; top: 0; left: 0; right: 0; bottom: 0; width: 100vw; max-width: 100vw; overflow: hidden; }

  /* Header */
  .nc-header { gap: 5px; padding: 5px 8px; flex-wrap: nowrap; width: 100%; }
  .nc-logo { font-size: 14px; letter-spacing: 1px; }
  .nc-header-room { font-size: 12px; display: none; }
  .nc-header-count { font-size: 11px; margin-left: auto; white-space: nowrap; }
  .nc-sound-btn { font-size: 13px; padding: 1px 5px; }

  /* Body - single column, sidebars are fixed overlays so they're out of flow */
  .nc-body { position: relative; width: 100vw; max-width: 100vw; overflow: hidden; }

  /* Sidebars — fixed overlays; visibility:hidden when closed prevents any paint
     outside the viewport from contributing to scroll width on some browsers    */
  .rooms-sidebar {
    position: fixed; left: 0; top: 0; bottom: 0; z-index: 200;
    transform: translateX(-100%); transition: transform 0.2s ease, visibility 0s linear 0.2s;
    visibility: hidden;
    width: 75vw; max-width: 240px; box-shadow: 4px 0 20px #000a;
    overflow-x: hidden;
  }
  .rooms-sidebar.mobile-open { transform: translateX(0); visibility: visible; transition: transform 0.2s ease; }
  .rooms-title { font-size: 11px; padding: 8px 10px; }
  .room-item { font-size: 13px; padding: 7px 10px; }
  .room-add { font-size: 13px; padding: 8px 10px; }

  .nc-sidebar {
    position: fixed; right: 0; top: 0; bottom: 0; z-index: 200;
    transform: translateX(100%); transition: transform 0.2s ease, visibility 0s linear 0.2s;
    visibility: hidden;
    width: 75vw; max-width: 240px; box-shadow: -4px 0 20px #000a;
    overflow-x: hidden;
  }
  .nc-sidebar.mobile-open { transform: translateX(0); visibility: visible; transition: transform 0.2s ease; }
  .nc-sidebar-title { font-size: 11px; padding: 8px 10px; }
  .nc-user { font-size: 13px; padding: 4px 10px; gap: 6px; }
  .nc-avatar { width: 20px; height: 20px; font-size: 10px; }
  .nc-section-label { font-size: 11px; padding: 6px 10px 3px; }

  /* Chat area - fill remaining space, sidebars are out of flow */
  .nc-chat { flex: 1 !important; width: 100vw !important; min-width: 0 !important; max-width: 100vw !important; overflow-x: hidden !important; }
  .nc-messages { width: 100%; max-width: 100%; overflow-x: hidden; }

  /* Fix 2: Word wrapping — cap nick width so msg-text always has room */
  .nc-messages { padding: 8px; gap: 2px; }
  .msg { display: flex !important; flex-wrap: nowrap !important; gap: 5px; align-items: flex-start; padding: 3px 0; position: relative; width: 100%; max-width: 100%; }
  .msg-time { font-size: 11px; flex-shrink: 0; white-space: nowrap; }
  .msg-nick { font-size: 12px; flex-shrink: 0; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; max-width: 72px; }
  .msg-colon { flex-shrink: 0; }
  .msg-text { font-size: 13px; flex: 1 1 0 !important; min-width: 0 !important; word-break: break-word; overflow-wrap: anywhere; white-space: pre-wrap; line-height: 1.4; overflow-x: hidden; }
  .msg-text img { max-width: 70vw; max-height: 250px; margin-top: 3px; display: block; border-radius: 4px; }
  .reaction-bar { padding-left: 0 !important; max-width: 100%; overflow-x: hidden; }
  .reaction-btn { font-size: 12px; padding: 2px 7px; }
  .reaction-add-btn { font-size: 12px; padding: 2px 6px; }

  /* Input bar */
  .nc-input-bar { padding: 6px 8px !important; gap: 4px !important; flex-wrap: nowrap !important; }
  .nc-input-room-label { display: none; }
  .nc-input-nick-label { font-size: 12px; white-space: nowrap; }
  .nc-input-arrow { display: none; }
  .nc-input { font-size: 14px !important; flex: 1 !important; min-width: 0 !important; }
  .nc-attach-btn { font-size: 16px; }
  .nc-send-btn { font-size: 13px !important; padding: 5px 12px !important; letter-spacing: 0.5px !important; white-space: nowrap !important; }

  /* Typing */
  .nc-typing { font-size: 11px; padding: 2px 8px 3px; min-height: 14px; }

  /* Mention dropdown */
  .mention-dropdown { min-width: 150px; max-height: 160px; bottom: 70px !important; left: 8px !important; }
  .mention-item { font-size: 13px; padding: 6px 10px; }

  /* Emoji picker */
  .emoji-picker { width: 200px; padding: 5px; }
  .emoji-option { font-size: 18px; padding: 2px; }

  /* Upload notice */
  .upload-notice { font-size: 12px; padding: 4px 8px; }

  /* Join screen */
  .join-card { width: calc(100vw - 32px); max-width: 360px; padding: 24px 18px; }
  .join-logo { font-size: 26px; letter-spacing: 4px; margin-bottom: 4px; }
  .join-tagline { font-size: 12px; margin-bottom: 20px; }
  .avatar-picker { width: 70px; height: 70px; font-size: 12px; margin-bottom: 18px; }
  .join-field label { font-size: 12px; margin-bottom: 5px; }
  .join-field input { font-size: 14px; padding: 8px 10px; }
  .join-btn { font-size: 14px; padding: 10px; letter-spacing: 2px; }
  .join-note { font-size: 12px; margin-top: 10px; }

  /* Profile popup */
  #profile-popup { width: 170px; padding: 14px; }
  .profile-avatar { width: 50px; height: 50px; font-size: 20px; margin-bottom: 8px; }
  .profile-nick { font-size: 14px; margin-bottom: 7px; }
  .profile-close { font-size: 14px; top: 5px; right: 7px; }

  /* Lightbox */
  #lightbox img { max-width: 95vw; max-height: 85vh; }
}

/* Small phones (iPhone SE, etc.) */
@media (max-width: 380px) {
  body { font-size: 12px; }
  .nc-header { padding: 4px 6px; gap: 4px; }
  .nc-logo { font-size: 12px; }
  .nc-sound-btn { font-size: 12px; padding: 1px 4px; }
  .rooms-sidebar, .nc-sidebar { width: 70vw; max-width: 200px; }
  .msg-time { font-size: 10px; }
  .msg-nick { font-size: 11px; }
  .msg-text { font-size: 12px; }
  .nc-input-bar { padding: 5px 6px !important; gap: 3px !important; }
  .nc-input { font-size: 13px !important; }
  .nc-send-btn { font-size: 12px !important; padding: 4px 10px !important; }
}
