:root {
  --bg: #e5ddd5;
  --header: #075e54;
  --headerText: #fff;
  --incoming: #ffffff;
  --outgoing: #dcf8c6;
  --muted: rgba(255, 255, 255, 0.8);
  --text: #111;
  --subtext: rgba(0, 0, 0, 0.6);
  --shadow: rgba(0, 0, 0, 0.08);
  --accent: #25d366;
  --composer-bg: #f0f0f0;
}

/* Reset and base */
* {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

html, body {
  height: 100%;
  height: 100dvh;
  overflow: hidden;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}

body {
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, Arial, sans-serif;
  color: var(--text);
  background: #0b141a;
  overscroll-behavior-y: contain;
  touch-action: pan-y;
}

#app {
  height: 100%;
  height: 100dvh;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

/* Views */
.view {
  height: 100%;
  height: 100dvh;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
  overflow: auto;
  -webkit-overflow-scrolling: touch;
}

.hidden {
  display: none !important;
}

/* Login view */
.card {
  width: 100%;
  max-width: 400px;
  background: #ffffff;
  border-radius: 12px;
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.15);
  padding: 32px 24px;
  margin: auto;
}

.brand {
  font-size: 28px;
  font-weight: 700;
  letter-spacing: -0.5px;
  color: var(--header);
}

.subtitle {
  margin-top: 8px;
  color: var(--subtext);
  font-size: 14px;
}

.field {
  display: block;
  margin-top: 16px;
}

.field span {
  display: block;
  font-size: 13px;
  color: var(--subtext);
  margin-bottom: 6px;
  font-weight: 500;
}

.field input {
  width: 100%;
  padding: 12px 14px;
  border-radius: 8px;
  border: 1px solid rgba(0, 0, 0, 0.12);
  outline: none;
  font-size: 16px;
  background: #fff;
  transition: border-color 0.2s, box-shadow 0.2s;
  -webkit-appearance: none;
  appearance: none;
}

.field input:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px rgba(37, 211, 102, 0.1);
}

.btn {
  width: 100%;
  border: 0;
  border-radius: 8px;
  padding: 12px 16px;
  font-weight: 600;
  font-size: 15px;
  margin-top: 20px;
  cursor: pointer;
  transition: background-color 0.2s, transform 0.1s;
  -webkit-tap-highlight-color: transparent;
  user-select: none;
}

.btn.primary {
  background: var(--accent);
  color: #062e1a;
}

.btn.primary:hover {
  background: #20ba5a;
}

.btn.primary:active {
  transform: scale(0.98);
  background: #1da850;
}

.error {
  margin-top: 12px;
  color: #d32f2f;
  font-size: 13px;
  min-height: 18px;
  text-align: center;
}

/* Chat view */
#chatView.view {
  padding: 0;
  align-items: stretch;
  justify-content: stretch;
  display: flex;
  flex-direction: column;
  height: 100%;
  height: 100dvh;
}

/* Header */
.header {
  height: 56px;
  min-height: 56px;
  background: var(--header);
  color: var(--headerText);
  display: flex;
  align-items: center;
  padding: 0 8px;
  gap: 8px;
  flex-shrink: 0;
  position: sticky;
  top: 0;
  z-index: 100;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}

.iconBtn {
  width: 40px;
  height: 40px;
  min-width: 40px;
  min-height: 40px;
  border-radius: 50%;
  border: 0;
  background: transparent;
  color: inherit;
  font-size: 20px;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  transition: background-color 0.15s;
  -webkit-tap-highlight-color: transparent;
  user-select: none;
  flex-shrink: 0;
}

.iconBtn:hover {
  background: rgba(255, 255, 255, 0.1);
}

.iconBtn:active {
  background: rgba(255, 255, 255, 0.16);
}

.iconBtn.recording {
  background: #f44336;
  color: #fff;
  animation: pulse 1.5s ease-in-out infinite;
}

@keyframes pulse {
  0%, 100% { opacity: 1; }
  50% { opacity: 0.7; }
}

.avatar {
  width: 36px;
  height: 36px;
  min-width: 36px;
  min-height: 36px;
  border-radius: 50%;
  background: rgba(255, 255, 255, 0.25);
  flex-shrink: 0;
  overflow: hidden;
}

.titleBlock {
  display: flex;
  flex-direction: column;
  line-height: 1.2;
  min-width: 0;
  flex: 1;
}

.title {
  font-weight: 600;
  font-size: 16px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.status {
  font-size: 12px;
  color: var(--muted);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.spacer {
  flex: 1;
  min-width: 0;
}

.badge {
  margin-left: 6px;
  font-size: 12px;
  background: rgba(255, 255, 255, 0.18);
  padding: 2px 6px;
  border-radius: 10px;
  display: inline-block;
}

/* Chat area */
.chatBg {
  flex: 1;
  background: var(--bg);
  overflow-y: auto;
  overflow-x: hidden;
  padding: 8px;
  -webkit-overflow-scrolling: touch;
  overscroll-behavior: contain;
  min-height: 0;
  will-change: scroll-position;
}

.messages {
  display: flex;
  flex-direction: column;
  gap: 4px;
  min-height: min-content;
}

.bubble {
  max-width: min(75%, 320px);
  padding: 8px 12px;
  border-radius: 8px;
  box-shadow: 0 1px 0.5px rgba(0, 0, 0, 0.13);
  position: relative;
  word-wrap: break-word;
  word-break: break-word;
  white-space: pre-wrap;
  font-size: 14.2px;
  line-height: 1.4;
  text-rendering: optimizeLegibility;
  -webkit-font-smoothing: antialiased;
}

.bubble.in {
  background: var(--incoming);
  align-self: flex-start;
  border-top-left-radius: 4px;
}

.bubble.out {
  background: var(--outgoing);
  align-self: flex-end;
  border-top-right-radius: 4px;
}

.meta {
  display: flex;
  gap: 4px;
  align-items: baseline;
  justify-content: flex-end;
  margin-top: 4px;
  font-size: 11px;
  color: rgba(0, 0, 0, 0.45);
}

.ticks {
  font-size: 12px;
}

.ticks.read {
  color: #1da1f2;
}

/* Composer */
.composer {
  display: flex;
  gap: 4px;
  align-items: center;
  padding: 6px 8px;
  background: var(--composer-bg);
  flex-shrink: 0;
  position: sticky;
  bottom: 0;
  z-index: 100;
  width: 100%;
  box-sizing: border-box;
  padding-bottom: calc(6px + env(safe-area-inset-bottom));
  border-top: 1px solid rgba(0, 0, 0, 0.05);
}

.composer .iconBtn {
  flex-shrink: 0;
}

.input {
  flex: 1;
  border-radius: 20px;
  border: none;
  padding: 9px 16px;
  font-size: 15px;
  resize: none;
  outline: none;
  max-height: 100px;
  min-height: 40px;
  line-height: 1.4;
  background: #fff;
  color: var(--text);
  font-family: inherit;
  -webkit-appearance: none;
  appearance: none;
  cursor: text;
  touch-action: manipulation;
  -webkit-tap-highlight-color: transparent;
  min-width: 0;
  transition: box-shadow 0.2s;
  box-shadow: 0 0 0 1px rgba(0, 0, 0, 0.08);
  overflow-y: auto;
  overflow-x: hidden;
  position: relative;
  z-index: 1;
}

.input:focus {
  box-shadow: 0 0 0 2px var(--accent);
}

.input:disabled {
  background: #f5f5f5;
  color: rgba(0, 0, 0, 0.4);
  cursor: not-allowed;
  pointer-events: none;
  opacity: 0.6;
}

.input::placeholder {
  color: rgba(0, 0, 0, 0.4);
}

.iconBtn.send {
  background: var(--accent);
  color: #062e1a;
  width: 40px;
  height: 40px;
  min-width: 40px;
  min-height: 40px;
  font-weight: 600;
  font-size: 18px;
  flex-shrink: 0;
  display: flex !important;
  align-items: center;
  justify-content: center;
  visibility: visible;
  opacity: 1;
}

.iconBtn.send.hidden {
  display: none !important;
  visibility: hidden !important;
  opacity: 0 !important;
  pointer-events: none !important;
  width: 0 !important;
  min-width: 0 !important;
  margin: 0 !important;
  padding: 0 !important;
}

/* Reply indicator */
.replyIndicator {
  background: rgba(0, 0, 0, 0.04);
  padding: 6px 12px;
  border-top: 1px solid rgba(0, 0, 0, 0.08);
  flex-shrink: 0;
}

.replyIndicatorContent {
  display: flex;
  align-items: center;
  gap: 8px;
  max-width: 100%;
}

#replyIndicatorText {
  flex: 1;
  font-size: 13px;
  color: rgba(0, 0, 0, 0.7);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

/* Attachments */
.att {
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 6px 10px;
  align-items: center;
  padding: 10px;
  border-radius: 8px;
  background: rgba(0, 0, 0, 0.04);
  border: 1px solid rgba(0, 0, 0, 0.08);
}

.attName {
  font-weight: 600;
  font-size: 14px;
}

.attMeta {
  grid-column: 1 / 2;
  font-size: 12px;
  color: rgba(0, 0, 0, 0.55);
}

.attBtn {
  grid-row: 1 / 3;
  grid-column: 2 / 3;
  border: 0;
  padding: 8px 12px;
  border-radius: 8px;
  font-weight: 600;
  background: rgba(0, 0, 0, 0.08);
  cursor: pointer;
}

.att.audioPlaceholder {
  padding: 12px;
}

.att.audioPlaceholder .attName {
  display: flex;
  align-items: center;
  gap: 8px;
}

.attImagePreview {
  width: 100%;
  max-width: 280px;
  border-radius: 8px;
  overflow: hidden;
  cursor: pointer;
}

.attImageThumb {
  width: 100%;
  height: auto;
  display: block;
  object-fit: cover;
  transition: transform 0.2s;
}

.attImageThumb:active {
  transform: scale(0.98);
}

.attVideoPreview {
  position: relative;
  width: 100%;
  max-width: 280px;
  border-radius: 8px;
  overflow: hidden;
  cursor: pointer;
}

.attVideoThumb {
  width: 100%;
  height: auto;
  display: block;
  object-fit: cover;
}

.attPlayBtn {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  width: 48px;
  height: 48px;
  border-radius: 50%;
  background: rgba(0, 0, 0, 0.7);
  color: white;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 20px;
  pointer-events: none;
}

/* Modal */
.modal {
  position: fixed;
  inset: 0;
  display: flex;
  align-items: flex-end;
  justify-content: center;
  padding: 12px;
  z-index: 1000;
}

.backdrop {
  position: absolute;
  inset: 0;
  background: rgba(0, 0, 0, 0.5);
}

.modalCard {
  position: relative;
  width: 100%;
  max-width: 500px;
  background: #fff;
  border-radius: 16px 16px 0 0;
  box-shadow: 0 -4px 24px rgba(0, 0, 0, 0.2);
  overflow: hidden;
  z-index: 1;
  max-height: 90vh;
  overflow-y: auto;
}

.modalHeader {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 16px;
  border-bottom: 1px solid rgba(0, 0, 0, 0.08);
  position: sticky;
  top: 0;
  background: #fff;
  z-index: 10;
}

.modalTitle {
  font-weight: 700;
  font-size: 18px;
  flex: 1;
}

.modalBody {
  padding: 16px;
}

.hint {
  font-size: 13px;
  color: var(--subtext);
  margin-bottom: 10px;
}

.fingerprint {
  font-family: ui-monospace, "SF Mono", Menlo, Monaco, Consolas, monospace;
  font-size: 13px;
  padding: 12px;
  border-radius: 8px;
  background: rgba(0, 0, 0, 0.04);
  border: 1px solid rgba(0, 0, 0, 0.08);
  word-break: break-word;
  margin-bottom: 12px;
}

.sectionTitle {
  margin-top: 16px;
  margin-bottom: 8px;
  font-weight: 700;
  font-size: 12px;
  color: rgba(0, 0, 0, 0.65);
  text-transform: uppercase;
  letter-spacing: 0.5px;
}

.textField {
  width: 100%;
  padding: 10px 12px;
  border-radius: 8px;
  border: 1px solid rgba(0, 0, 0, 0.12);
  outline: none;
  font-size: 15px;
  margin-top: 8px;
}

.miniBtn {
  border: 0;
  border-radius: 8px;
  padding: 8px 12px;
  font-weight: 600;
  font-size: 14px;
  background: rgba(0, 0, 0, 0.08);
  cursor: pointer;
  transition: background-color 0.2s;
}

.miniBtn:hover {
  background: rgba(0, 0, 0, 0.12);
}

.avatarPreviewWrap {
  display: flex;
  justify-content: center;
  padding: 12px 0;
}

.avatarPreview {
  width: 80px;
  height: 80px;
  border-radius: 50%;
  object-fit: cover;
  background: rgba(0, 0, 0, 0.06);
  border: 1px solid rgba(0, 0, 0, 0.08);
}

.bgOptions {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 8px;
  margin: 12px 0;
}

.bgChip {
  height: 48px;
  border-radius: 8px;
  border: 2px solid transparent;
  background: rgba(0, 0, 0, 0.05);
  cursor: pointer;
  transition: all 0.2s;
}

.bgChip.active {
  border-color: var(--accent);
  box-shadow: 0 4px 12px rgba(37, 211, 102, 0.2);
}

.row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 12px 0;
}

.rowTitle {
  font-weight: 600;
  font-size: 15px;
}

.rowSub {
  font-size: 12px;
  color: var(--subtext);
  margin-top: 2px;
}

.rowText {
  display: flex;
  flex-direction: column;
  gap: 2px;
  flex: 1;
  min-width: 0;
}

.switch {
  position: relative;
  display: inline-block;
  width: 44px;
  height: 26px;
  flex-shrink: 0;
}

.switch input {
  opacity: 0;
  width: 0;
  height: 0;
}

.slider {
  position: absolute;
  cursor: pointer;
  inset: 0;
  background: rgba(0, 0, 0, 0.18);
  transition: 0.2s;
  border-radius: 13px;
}

.slider:before {
  position: absolute;
  content: "";
  height: 20px;
  width: 20px;
  left: 3px;
  top: 3px;
  background: white;
  transition: 0.2s;
  border-radius: 50%;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.2);
}

.switch input:checked + .slider {
  background: var(--accent);
}

.switch input:checked + .slider:before {
  transform: translateX(18px);
}

/* Emoji picker */
.emojiPanel {
  position: fixed;
  left: 0;
  right: 0;
  bottom: calc(56px + env(safe-area-inset-bottom));
  margin: 0 auto;
  max-width: 100%;
  background: #fff;
  border-top-left-radius: 16px;
  border-top-right-radius: 16px;
  box-shadow: 0 -4px 24px rgba(0, 0, 0, 0.2);
  padding: 12px;
  z-index: 50;
  max-height: 40vh;
  overflow-y: auto;
}

.emojiTop {
  display: flex;
  gap: 6px;
  overflow-x: auto;
  padding-bottom: 8px;
  -webkit-overflow-scrolling: touch;
}

.emojiTab {
  border: 0;
  background: rgba(0, 0, 0, 0.06);
  border-radius: 16px;
  padding: 6px 12px;
  font-weight: 600;
  font-size: 13px;
  white-space: nowrap;
  cursor: pointer;
  transition: background-color 0.2s;
}

.emojiTab.active {
  background: rgba(37, 211, 102, 0.22);
}

.emojiQuick {
  display: flex;
  gap: 8px;
  padding: 8px 0;
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
}

.emojiGrid {
  display: grid;
  grid-template-columns: repeat(8, 1fr);
  gap: 4px;
  padding: 8px 0;
}

.emojiBtn {
  border: 0;
  background: transparent;
  border-radius: 8px;
  padding: 8px;
  font-size: 24px;
  cursor: pointer;
  transition: background-color 0.15s;
}

.emojiBtn:hover {
  background: rgba(0, 0, 0, 0.06);
}

.replyPreview {
  border-left: 3px solid rgba(0, 0, 0, 0.2);
  padding: 6px 10px;
  margin-bottom: 6px;
  background: rgba(0, 0, 0, 0.04);
  border-radius: 4px;
}

.replyLine {
  font-size: 11px;
  font-weight: 600;
  color: rgba(0, 0, 0, 0.6);
  margin-bottom: 2px;
}

.replyText {
  font-size: 12px;
  color: rgba(0, 0, 0, 0.7);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

/* Scrollbar */
.chatBg::-webkit-scrollbar {
  width: 4px;
}

.chatBg::-webkit-scrollbar-track {
  background: transparent;
}

.chatBg::-webkit-scrollbar-thumb {
  background: rgba(0, 0, 0, 0.2);
  border-radius: 2px;
}

.chatBg::-webkit-scrollbar-thumb:hover {
  background: rgba(0, 0, 0, 0.3);
}

/* Mobile optimizations */
@media (max-width: 480px) {
  .header {
    height: 56px;
    padding: 0 6px;
    gap: 6px;
  }

  .iconBtn {
    width: 40px;
    height: 40px;
    min-width: 40px;
    min-height: 40px;
    font-size: 18px;
  }

  .avatar {
    width: 36px;
    height: 36px;
  }

  .title {
    font-size: 15px;
  }

  .status {
    font-size: 11px;
  }

  .composer {
    padding: 6px;
    gap: 4px;
  }

  .input {
    padding: 9px 12px;
    font-size: 15px;
    border-radius: 20px;
  }

  .iconBtn.send {
    width: 40px;
    height: 40px;
    min-width: 40px;
    min-height: 40px;
    font-size: 16px;
  }

  .bubble {
    max-width: min(80%, 280px);
    padding: 7px 11px;
    font-size: 14px;
  }

  .card {
    padding: 24px 20px;
  }

  .emojiGrid {
    grid-template-columns: repeat(7, 1fr);
  }

  .bgOptions {
    grid-template-columns: repeat(2, 1fr);
  }
}

/* iOS Safari fixes */
@supports (-webkit-touch-callout: none) {
  .view {
    height: -webkit-fill-available;
  }

  #chatView.view {
    height: -webkit-fill-available;
  }

  html, body {
    height: -webkit-fill-available;
  }

  #app {
    height: -webkit-fill-available;
  }
}

/* Performance optimizations */
.bubble, .iconBtn, .input {
  will-change: transform;
  transform: translateZ(0);
}

/* Ensure send button visibility */
#sendBtn:not(.hidden) {
  display: flex !important;
  visibility: visible !important;
  opacity: 1 !important;
}
