/* ======================================
   Mobile Dedicated Chat System
   Purpose-built mobile chat with quick bar and full log modes.
   ====================================== */

/* Container — hidden by default, shown via JS class.
   Must use !important to override .mobile-only { display: flex !important } */
#mobile-chat {
  display: none !important;
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  z-index: 200;
  pointer-events: none;
}

#mobile-chat.mobile-chat-visible {
  display: flex !important;
  flex-direction: column;
  pointer-events: auto;
}

/* ---- Quick Chat Bar ---- */
.mobile-chat-bar {
  display: flex;
  align-items: center;
  height: 44px;
  background: rgba(0, 0, 0, 0.75);
  padding: 0 4px;
  gap: 4px;
  flex-shrink: 0;
  border-top: 1px solid #444;
}

/* Channel pill button */
.mobile-chat-channel-pill {
  flex-shrink: 0;
  padding: 4px 10px;
  background: rgba(0, 0, 0, 0.5);
  border: 1px solid #ddb944;
  border-radius: 12px;
  color: #ddb944;
  font-size: 12px;
  font-weight: bold;
  white-space: nowrap;
  cursor: pointer;
  font-family: inherit;
  min-width: 60px;
  text-align: center;
}

.mobile-chat-channel-pill:active {
  background: rgba(255, 255, 255, 0.1);
}

/* Message display — tappable span that shows typed text + cursor */
.mobile-chat-msg {
  flex: 1;
  min-width: 0;
  height: 32px;
  line-height: 32px;
  background: rgba(0, 0, 0, 0.5);
  border: 1px solid #555;
  color: #ffff00;
  font-size: 14px;
  padding: 0 8px;
  border-radius: 4px;
  overflow: hidden;
  white-space: nowrap;
  cursor: pointer;
}

/* Placeholder via pseudo-element */
.mobile-chat-msg:empty::before {
  content: attr(data-placeholder);
  color: rgba(255, 255, 255, 0.35);
  pointer-events: none;
}

/* Disabled state (read-only channels) */
.mobile-chat-msg.mobile-chat-msg-disabled {
  opacity: 0.5;
  pointer-events: none;
}

/* Blinking cursor inside input display */
.vk-cursor {
  display: inline;
  animation: vk-blink 1s step-end infinite;
  color: #ffff00;
  font-weight: normal;
}
@keyframes vk-blink {
  50% { opacity: 0; }
}

/* ---- Message Preview (last 2 messages above bar) ---- */
.mobile-chat-preview {
  display: none;
  padding: 3px 8px;
  background: rgba(0, 0, 0, 0.6);
  border-top: 1px solid #333;
  font-size: 12px;
  line-height: 1.35;
  max-height: 90px;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
}

.mobile-chat-preview.preview-expanded {
  max-height: 40vh;
}

#mobile-chat.mobile-chat-visible .mobile-chat-preview {
  display: block;
}

/* Hide preview when full log or keyboard is open */
#mobile-chat.mobile-chat-full .mobile-chat-preview,
#mobile-chat.mobile-chat-typing .mobile-chat-preview {
  display: none;
}

.mobile-chat-preview .chat-message {
  display: block;
  white-space: normal;
  word-wrap: break-word;
  padding: 0;
  line-height: 1.4;
}

/* Send button — hidden, virtual keyboard has its own Send key */
.mobile-chat-send {
  display: none;
}

/* DM button (person icon) */
.mobile-chat-dm {
  flex-shrink: 0;
  width: 32px;
  height: 32px;
  background: none;
  border: none;
  color: #6699ff;
  font-size: 20px;
  cursor: pointer;
  font-family: 'Material Icons';
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 0;
}

.mobile-chat-dm:active {
  color: #99bbff;
}

/* DM inline bar — replaces chat bar content */
.mobile-dm-bar {
  background: rgba(0, 0, 0, 0.1);
}

.mobile-dm-input {
  flex: 1;
  min-width: 0;
  height: 32px;
  background: rgba(0, 0, 0, 0.5);
  border: 1px solid #6699ff;
  border-radius: 4px;
  color: #fff;
  font-size: 14px;
  padding: 0 8px;
  font-family: inherit;
  box-sizing: border-box;
}

.mobile-dm-confirm {
  flex-shrink: 0;
  padding: 4px 12px;
  border: 1px solid #6699ff;
  background: none;
  color: #6699ff;
  border-radius: 4px;
  font-size: 13px;
  font-family: inherit;
  cursor: pointer;
  height: 32px;
}

/* Expand/collapse button (material icon) */
.mobile-chat-expand {
  flex-shrink: 0;
  width: 32px;
  height: 32px;
  background: none;
  border: none;
  color: #ccc;
  font-size: 20px;
  cursor: pointer;
  font-family: 'Material Icons';
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 0;
}

.mobile-chat-expand:active {
  color: #fff;
}

/* ---- Full Chat Log ---- */
.mobile-chat-log {
  display: none;
  height: 40vh;
  background: rgba(0, 0, 0, 0.65);
  flex-direction: column;
  border-top: 1px solid #444;
}

#mobile-chat.mobile-chat-full .mobile-chat-log {
  display: flex;
}

/* Log header with close/collapse buttons */
.mobile-chat-log-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 2px 4px;
  border-bottom: 1px solid #333;
  flex-shrink: 0;
}

.mobile-chat-close-channel,
.mobile-chat-close {
  background: none;
  border: none;
  color: #999;
  font-size: 20px;
  font-family: 'Material Icons';
  padding: 4px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
}

.mobile-chat-close-channel:active,
.mobile-chat-close:active {
  color: #fff;
}

.mobile-chat-close-channel {
  color: #e74c3c;
  font-size: 18px;
  display: none;
}

.mobile-chat-close-channel:active {
  color: #ff6b6b;
}

.mobile-chat-log-messages {
  flex: 1;
  overflow-y: auto;
  padding: 4px 8px;
  font-size: 13px;
  -webkit-overflow-scrolling: touch;
}

/* Message styling inside mobile log */
.mobile-chat-log-messages .chat-message {
  display: block;
  padding: 2px 0;
  line-height: 1.4;
  word-wrap: break-word;
}

.mobile-chat-log-messages .channel-empty {
  color: #888;
  font-style: italic;
}

/* Scrollbar styling */
.mobile-chat-log-messages::-webkit-scrollbar {
  width: 4px;
}

.mobile-chat-log-messages::-webkit-scrollbar-track {
  background: transparent;
}

.mobile-chat-log-messages::-webkit-scrollbar-thumb {
  background: rgba(255, 255, 255, 0.2);
  border-radius: 2px;
}

/* Unread badge on expand button */
.mobile-chat-expand-badge {
  position: absolute;
  top: -4px;
  right: -4px;
  min-width: 16px;
  height: 16px;
  background: #e74c3c;
  border-radius: 8px;
  color: #fff;
  font-size: 10px;
  font-weight: bold;
  display: none;
  align-items: center;
  justify-content: center;
  padding: 0 3px;
}

/* ---- Force-mobile overrides ---- */
body.force-mobile #mobile-chat {
  display: none !important;
}

body.force-mobile #mobile-chat.mobile-chat-visible {
  display: flex !important;
  flex-direction: column;
  pointer-events: auto;
}

/* ---- Bar floating above keyboard (not inside, so keyboard scaling doesn't affect it) ---- */
.mobile-chat-bar.chat-bar-floating {
  position: fixed;
  left: 0;
  right: 0;
  z-index: 202;
  border-bottom: 1px solid #444;
  border-top: none;
}

/* ======================================
   Virtual Keyboard
   Custom on-screen QWERTY — no native keyboard, no viewport issues.
   ====================================== */

.virtual-keyboard {
  display: none;
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  background: rgba(15, 15, 15, 0.92);
  border-top: 1px solid #444;
  padding: 4px 3px;
  padding-bottom: calc(4px + env(safe-area-inset-bottom, 0px));
  z-index: 100000;
  -webkit-tap-highlight-color: transparent;
}

.virtual-keyboard.visible {
  display: block;
}

/* Key rows */
.vk-row {
  display: flex;
  justify-content: center;
  gap: 3px;
  margin-bottom: 3px;
}

.vk-row:last-child {
  margin-bottom: 0;
}

/* Utility row (Space/Send) — slightly thinner */
.vk-row-utility {
  gap: 4px;
}

/* Base key style */
.vk-key {
  flex: 1;
  max-width: 36px;
  height: 36px;
  background: #333;
  border: 1px solid #555;
  border-radius: 4px;
  color: #ddd;
  font-size: 16px;
  font-family: inherit;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
  user-select: none;
  -webkit-user-select: none;
  padding: 0;
  touch-action: manipulation;
}

/* Counter-scale key label text so letters stay original size when keyboard is resized */
.vk-key > span,
.vk-key > .material-icons {
  transform: scaleX(var(--vk-inv-x, 1)) scaleY(var(--vk-inv-y, 1));
}

.vk-key:active,
.vk-key.vk-key-pressed {
  background: #666;
  border-color: #999;
  color: #fff;
  transform: scale(1.08);
}

.vk-key-send:active,
.vk-key-send.vk-key-pressed {
  background: #3a8a3a;
  color: #bfb;
  transform: scale(1.08);
}

/* Special key sizes */
.vk-key-space {
  flex: 4;
  max-width: none;
}

.vk-key-shift,
.vk-key-backspace {
  flex: 1.5;
  max-width: 50px;
}

.vk-key-shift .material-icons,
.vk-key-backspace .material-icons {
  font-size: 18px;
}

.vk-key-mode {
  flex: 1.3;
  max-width: 46px;
  font-size: 13px;
  font-weight: bold;
}

.vk-key-send {
  flex: 1.3;
  max-width: 56px;
  background: #2a5a2a;
  border-color: #4a4;
  color: #8f8;
  font-weight: bold;
  font-size: 13px;
}

.vk-key-send:active {
  background: #3a7a3a;
}

/* Shift state indicators */
.vk-key-shift.active {
  background: #555;
  border-color: #aaa;
  color: #fff;
}

.vk-key-shift.caps-lock {
  background: #5a4a10;
  border-color: #ddb944;
  color: #ddb944;
  box-shadow: 0 0 6px rgba(221, 185, 68, 0.5);
  border-bottom: 3px solid #ddb944;
}

/* ---- Landscape: narrower and shorter ---- */
@media (orientation: landscape) {
  .virtual-keyboard {
    max-width: 60%;
    left: 20%;
    right: 20%;
    border-radius: 8px 8px 0 0;
  }
  .virtual-keyboard > .mobile-chat-bar {
    height: 30px;
    margin-bottom: 0;
    opacity: 0.7;
  }
  .vk-key {
    height: 28px;
    font-size: 14px;
  }
  .vk-row {
    margin-bottom: 2px;
  }
  .vk-key-shift .material-icons,
  .vk-key-backspace .material-icons {
    font-size: 16px;
  }
  .mobile-chat-log {
    height: 25vh;
  }
}

/* Key pop-up preview (appears above pressed key) */
.vk-key-popup {
  position: fixed;
  pointer-events: none;
  z-index: 300;
  width: 48px;
  height: 48px;
  background: #444;
  border: 1px solid #777;
  border-radius: 8px;
  color: #fff;
  font-size: 22px;
  font-weight: bold;
  font-family: inherit;
  display: flex;
  align-items: center;
  justify-content: center;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.5);
}

/* ---- Keyboard Editor Toolbar ---- */
#keyboard-editor-toolbar {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  background: rgba(0, 0, 0, 0.92);
  border-bottom: 2px solid #ffa000;
  display: none;
  flex-direction: column;
  z-index: 20000;
  color: #fff;
  font-size: 13px;
  font-family: "Martel", serif;
}

#kb-toolbar-row1 {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 10px;
  height: 36px;
}

#kb-toolbar-row2 {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  height: 34px;
  border-top: 1px solid rgba(255, 160, 0, 0.3);
}

.kb-toolbar-title {
  font-size: 14px;
  margin-right: 6px;
}

.kb-dim-label {
  font-size: 11px;
  color: #ccc;
  min-width: 36px;
  text-align: right;
}

.kb-dim-separator {
  width: 1px;
  height: 18px;
  background: rgba(255, 255, 255, 0.2);
  margin: 0 4px;
}

#keyboard-editor-toolbar button {
  padding: 4px 12px;
  border: 1px solid #666;
  background: #333;
  color: #fff;
  border-radius: 4px;
  font-size: 12px;
  cursor: pointer;
}

#keyboard-editor-toolbar #kb-save-btn {
  border-color: #ffa000;
  color: #ffa000;
}

#keyboard-editor-toolbar #kb-reset-btn {
  border-color: #ff4444;
  color: #ff4444;
}

#kb-width-minus, #kb-width-plus,
#kb-height-minus, #kb-height-plus {
  width: 26px;
  height: 26px;
  padding: 0;
  border-radius: 50%;
  border: 2px solid #ffa000;
  background: rgba(0, 0, 0, 0.8);
  color: #ffa000;
  font-size: 16px;
  font-weight: bold;
  display: flex;
  align-items: center;
  justify-content: center;
}

#kb-width-label, #kb-height-label {
  min-width: 36px;
  text-align: center;
  color: #ffa000;
  font-weight: bold;
  font-size: 12px;
}

/* Keyboard edit preview: show keyboard with dashed border */
.virtual-keyboard.keyboard-edit-preview {
  border: 2px dashed #ffa000;
}

/* Preview bar shown during keyboard editing */
.kb-preview-bar {
  pointer-events: none;
  border: 2px dashed #ffa000 !important;
}
