/* ===== Reset & Base ===== */
*, *::before, *::after {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

:root {
  --blue: #007AFF;
  --blue-dark: #0062CC;
  --bubble-out: #007AFF;
  --bubble-in: #E5E5EA;
  --text-out: #ffffff;
  --text-in: #000000;
  --header-bg: rgba(249, 249, 249, 0.94);
  --border: #d1d1d6;
  --input-bg: #f2f2f7;
  --body-bg: #e8e8ed;
  --frame-bg: #ffffff;
  --contact-name-color: #000000;
  --input-text: #000000;
  --progress-track: #d1d1d6;
  --font: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
}

/* ===== Dark Theme ===== */
html[data-theme="dark"] {
  --blue: #0a84ff;
  --blue-dark: #0070d6;
  --bubble-out: #0a84ff;
  --bubble-in: #3a3a3c;
  --text-out: #ffffff;
  --text-in: #ffffff;
  --header-bg: rgba(28, 28, 30, 0.94);
  --border: #38383a;
  --input-bg: #2c2c2e;
  --body-bg: #000000;
  --frame-bg: #1c1c1e;
  --contact-name-color: #ffffff;
  --input-text: #ffffff;
  --progress-track: #48484a;
}

html[data-theme="dark"] .phone-frame      { background: var(--frame-bg); }
html[data-theme="dark"] .input-footer     { background: rgba(28, 28, 30, 0.94); }
html[data-theme="dark"] .message-input    { background: #2c2c2e; color: var(--input-text); border-color: var(--border); }
html[data-theme="dark"] .contact-name     { color: var(--contact-name-color); }
html[data-theme="dark"] .music-panel      { background: rgba(44, 44, 46, 0.92); border-color: rgba(255,255,255,0.1); }
html[data-theme="dark"] .music-ctrl-btn   { background: #3a3a3c; color: #ebebf5; }
html[data-theme="dark"] .music-ctrl-btn:hover { background: #48484a; }
html[data-theme="dark"] .music-ctrl-play  { background: var(--blue); color: #fff; }
html[data-theme="dark"] .music-ctrl-play:hover { background: var(--blue-dark); }
html[data-theme="dark"] .toggle-track     { background: #636366; }
html[data-theme="dark"] .toggle-flanker   { color: #ebebf5; }
html[data-theme="dark"] .now-playing-text { color: #ebebf5; }
html[data-theme="dark"] .vol-level-text   { color: #ebebf5; }
html[data-theme="dark"] .album-art-fallback { background: #3a3a3c; color: #636366; }
html[data-theme="dark"] .dot              { background: #636366; }
html[data-theme="dark"] .theme-toggle-btn { background: rgba(44,44,46,0.92); border-color: rgba(255,255,255,0.1); color: #ebebf5; }
html[data-theme="dark"] .theme-options    { background: rgba(44,44,46,0.92); border-color: rgba(255,255,255,0.1); }
html[data-theme="dark"] .theme-swatch-light { border-color: #636366; }

/* ===== Pink Theme ===== */
html[data-theme="pink"] {
  --blue: #c2185b;
  --blue-dark: #a01348;
  --bubble-out: #c2185b;
  --bubble-in: #fadadd;
  --text-out: #ffffff;
  --text-in: #3d0015;
  --header-bg: rgba(255, 244, 248, 0.94);
  --border: #f0b8cc;
  --input-bg: #fce4ed;
  --body-bg: #fce4ed;
  --frame-bg: #ffffff;
  --contact-name-color: #3d0015;
  --input-text: #3d0015;
  --progress-track: #f0b8cc;
}

html[data-theme="pink"] .phone-frame      { background: var(--frame-bg); }
html[data-theme="pink"] .input-footer     { background: rgba(255, 244, 248, 0.94); }
html[data-theme="pink"] .message-input    { background: #fff; color: var(--input-text); }
html[data-theme="pink"] .contact-name     { color: var(--contact-name-color); }
html[data-theme="pink"] .dot              { background: #d48fa0; }
html[data-theme="pink"] .music-panel      { background: rgba(255, 244, 248, 0.92); border-color: #f0b8cc; }
html[data-theme="pink"] .now-playing-text { color: #3d0015; }
html[data-theme="pink"] .vol-level-text   { color: #3d0015; }
html[data-theme="pink"] .toggle-flanker   { color: #a0506a; }
html[data-theme="pink"] .theme-toggle-btn { background: rgba(255,244,248,0.92); border-color: #f0b8cc; }
html[data-theme="pink"] .theme-options    { background: rgba(255,244,248,0.92); border-color: #f0b8cc; }

html, body {
  height: 100%;
}

body {
  font-family: var(--font);
  background: var(--body-bg);
  display: flex;
  align-items: flex-start;
  justify-content: center;
  padding: 20px 0;
  min-height: 100%;
}

/* ===== Phone Frame ===== */
.phone-frame {
  width: 100%;
  max-width: 430px;
  height: calc(100vh - 40px);
  max-height: 900px;
  background: var(--frame-bg);
  border-radius: 44px;
  box-shadow:
    0 2px 8px rgba(0, 0, 0, 0.08),
    0 16px 48px rgba(0, 0, 0, 0.22),
    0 0 0 1px rgba(0, 0, 0, 0.06);
  display: flex;
  flex-direction: column;
  overflow: hidden;
  position: relative;
}

@media (max-width: 600px) {
  body {
    background: #ffffff;
    padding: 0;
    align-items: stretch;
  }
  .phone-frame {
    max-width: 100%;
    height: 100dvh;
    max-height: none;
    border-radius: 0;
    box-shadow: none;
  }
}

/* ===== Header ===== */
.header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 8px 12px 12px;
  background: var(--header-bg);
  backdrop-filter: blur(20px);
  -webkit-backdrop-filter: blur(20px);
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
  min-height: 82px;
}

.header-btn {
  background: none;
  border: none;
  cursor: default;
  color: var(--blue);
  display: flex;
  align-items: center;
  gap: 3px;
  padding: 4px;
  border-radius: 8px;
  font-size: 17px;
  font-weight: 400;
  -webkit-user-select: none;
  user-select: none;
  opacity: 0.9;
}

.back-chevron {
  flex-shrink: 0;
}

.back-count {
  font-size: 17px;
  line-height: 1;
}

.profile-section {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 4px;
  background: none;
  border: none;
  cursor: default;
  text-decoration: none;
  flex: 1;
  padding: 0 8px;
}

.avatar-wrap {
  position: relative;
  width: 57px;
  height: 57px;
}

.profile-pic {
  width: 57px;
  height: 57px;
  object-fit: contain;
  border-radius: 50%;
  display: block;
}

.avatar-fallback {
  display: none;
  width: 57px;
  height: 57px;
  border-radius: 50%;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  color: white;
  font-size: 13px;
  font-weight: 600;
  align-items: center;
  justify-content: center;
  letter-spacing: 0.5px;
}

.contact-name {
  font-size: 12px;
  font-weight: 600;
  color: var(--contact-name-color);
  -webkit-user-select: none;
  user-select: none;
}

.video-btn {
  color: var(--blue);
  padding: 6px 8px;
}

/* ===== Messages Container ===== */
.messages-container {
  flex: 1;
  overflow-y: auto;
  overflow-x: hidden;
  padding: 16px 12px 12px;
  display: flex;
  flex-direction: column;
  gap: 3px;
  scroll-behavior: smooth;
  -webkit-overflow-scrolling: touch;
}

/* Thin custom scrollbar on desktop */
.messages-container::-webkit-scrollbar {
  width: 4px;
}
.messages-container::-webkit-scrollbar-track {
  background: transparent;
}
.messages-container::-webkit-scrollbar-thumb {
  background: rgba(0, 0, 0, 0.15);
  border-radius: 2px;
}

/* ===== Message Bubbles ===== */
.message {
  max-width: 75%;
  padding: 10px 14px;
  border-radius: 18px;
  font-size: 16px;
  line-height: 1.42;
  word-wrap: break-word;
  white-space: pre-wrap;
  animation: slideIn 0.28s ease-out both;
}

.message.outgoing {
  background: var(--bubble-out);
  color: var(--text-out);
  align-self: flex-end;
  border-bottom-right-radius: 5px;
  margin-left: 44px;
}

.message.incoming {
  background: var(--bubble-in);
  color: var(--text-in);
  align-self: flex-start;
  border-bottom-left-radius: 5px;
  margin-right: 44px;
}

/* Small gap between same-sender consecutive bubbles */
.message.outgoing + .message.outgoing,
.message.incoming + .message.incoming {
  margin-top: -1px;
}

/* ===== Typing Indicator ===== */
.typing-indicator {
  display: flex;
  align-items: center;
  gap: 5px;
  padding: 13px 16px;
  background: var(--bubble-in);
  border-radius: 18px;
  border-bottom-left-radius: 5px;
  align-self: flex-start;
  width: 56px;
  animation: slideIn 0.22s ease-out both;
  margin-right: 44px;
}

.dot {
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: #8e8e93;
  animation: dotBounce 1.2s ease-in-out infinite;
  flex-shrink: 0;
}

.dot:nth-child(1) { animation-delay: 0s; }
.dot:nth-child(2) { animation-delay: 0.18s; }
.dot:nth-child(3) { animation-delay: 0.36s; }

/* ===== Input Footer ===== */
.input-footer {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 10px 8px 14px;
  padding-bottom: calc(8px + env(safe-area-inset-bottom, 0px));
  background: rgba(249, 249, 249, 0.94);
  backdrop-filter: blur(20px);
  -webkit-backdrop-filter: blur(20px);
  border-top: 1px solid var(--border);
  flex-shrink: 0;
}

.message-input {
  flex: 1;
  height: 36px;
  padding: 0 14px;
  font-family: var(--font);
  font-size: 16px;
  color: #000;
  background: #ffffff;
  border: 1.5px solid var(--border);
  border-radius: 18px;
  outline: none;
  cursor: default;
  -webkit-user-select: none;
  user-select: none;
  /* prevent any visual indication of editability */
  caret-color: transparent;
}

.message-input:focus {
  border-color: var(--border);
  box-shadow: none;
}

/* iOS appearance reset */
.message-input {
  -webkit-appearance: none;
  appearance: none;
}

.send-button {
  width: 34px;
  height: 34px;
  border-radius: 50%;
  background: var(--blue);
  border: none;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  transition: background-color 0.15s, transform 0.1s, opacity 0.15s;
}

.send-button:hover {
  background: var(--blue-dark);
}

.send-button:active {
  transform: scale(0.92);
}

.send-button:disabled {
  opacity: 0.45;
  cursor: not-allowed;
  transform: none;
}

/* ===== Music Panel ===== */
.music-panel {
  position: fixed;
  bottom: 80px;
  left: 16px;
  z-index: 1000;
  background: rgba(255, 255, 255, 0.92);
  backdrop-filter: blur(16px);
  -webkit-backdrop-filter: blur(16px);
  border: 1px solid rgba(0, 0, 0, 0.1);
  border-radius: 14px;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.12);
  padding: 10px 12px;
  min-width: 44px;
}

@media (min-width: 601px) {
  .music-panel {
    bottom: 24px;
    left: 24px;
  }
}

/* Toggle row */
.music-top-row {
  display: flex;
  align-items: center;
  gap: 6px;
}

.toggle-flanker {
  color: #888;
  flex-shrink: 0;
}

/* iOS-style toggle switch */
.toggle-switch {
  position: relative;
  display: inline-flex;
  align-items: center;
  cursor: pointer;
}

.toggle-switch input {
  position: absolute;
  opacity: 0;
  width: 0;
  height: 0;
}

.toggle-track {
  display: block;
  width: 36px;
  height: 20px;
  background: #ccc;
  border-radius: 10px;
  transition: background 0.2s;
  position: relative;
}

.toggle-switch input:checked + .toggle-track {
  background: var(--blue);
}

.toggle-thumb {
  position: absolute;
  top: 2px;
  left: 2px;
  width: 16px;
  height: 16px;
  border-radius: 50%;
  background: #fff;
  box-shadow: 0 1px 3px rgba(0,0,0,0.25);
  transition: transform 0.2s;
}

.toggle-switch input:checked + .toggle-track .toggle-thumb {
  transform: translateX(16px);
}

/* Expanding now-playing section */
.music-details {
  margin-top: 8px;
  display: none;
  flex-direction: column;
  gap: 7px;
}

.music-details.visible {
  display: flex;
}

/* Album art + title row */
.np-info-row {
  display: flex;
  align-items: center;
  gap: 8px;
}

.album-art-wrap {
  width: 36px;
  height: 36px;
  border-radius: 6px;
  overflow: hidden;
  flex-shrink: 0;
  background: var(--input-bg);
  position: relative;
}

.album-art {
  width: 36px;
  height: 36px;
  object-fit: cover;
  display: none;
}

.album-art-fallback {
  width: 36px;
  height: 36px;
  display: flex;
  align-items: center;
  justify-content: center;
  color: #999;
  background: var(--input-bg);
}

.now-playing-text {
  font-size: 11px;
  color: #333;
  max-width: 130px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  font-weight: 500;
}

/* Progress / scrubber */
.progress-bar {
  -webkit-appearance: none;
  appearance: none;
  width: 100%;
  height: 3px;
  border-radius: 2px;
  outline: none;
  cursor: pointer;
  background: linear-gradient(to right, var(--blue) 0%, var(--progress-track) 0%);
  accent-color: var(--blue);
}

.progress-bar::-webkit-slider-thumb {
  -webkit-appearance: none;
  width: 12px;
  height: 12px;
  border-radius: 50%;
  background: var(--blue);
  cursor: pointer;
}

.progress-bar::-moz-range-thumb {
  width: 12px;
  height: 12px;
  border-radius: 50%;
  background: var(--blue);
  border: none;
  cursor: pointer;
}

/* Volume row */
.music-vol-row {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
}

.vol-level-text {
  font-size: 11px;
  font-weight: 600;
  color: #555;
  min-width: 30px;
  text-align: center;
  -webkit-user-select: none;
  user-select: none;
}

/* Controls row */
.music-ctrl-row {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
}

.music-ctrl-btn {
  width: 28px;
  height: 28px;
  border-radius: 6px;
  background: var(--input-bg);
  border: none;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  color: #333;
  transition: background 0.12s;
  flex-shrink: 0;
}

.music-ctrl-btn:hover {
  background: #dddde8;
}

.music-ctrl-btn:active {
  transform: scale(0.9);
}

.music-ctrl-play {
  width: 32px;
  height: 32px;
  border-radius: 50%;
  background: var(--blue);
  color: white;
}

.music-ctrl-play:hover {
  background: var(--blue-dark);
}

/* ===== Theme Picker ===== */
.theme-picker {
  position: fixed;
  bottom: 80px;
  right: 16px;
  z-index: 1000;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 8px;
}

@media (min-width: 601px) {
  .theme-picker { bottom: 24px; right: 24px; }
}

.theme-toggle-btn {
  width: 36px;
  height: 36px;
  border-radius: 50%;
  background: rgba(255, 255, 255, 0.92);
  backdrop-filter: blur(16px);
  -webkit-backdrop-filter: blur(16px);
  border: 1px solid rgba(0, 0, 0, 0.1);
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.12);
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  color: #555;
  transition: background 0.15s;
}

.theme-toggle-btn:hover { background: #e8e8ed; }

.theme-options {
  flex-direction: column;
  gap: 6px;
  background: rgba(255, 255, 255, 0.92);
  backdrop-filter: blur(16px);
  -webkit-backdrop-filter: blur(16px);
  border: 1px solid rgba(0, 0, 0, 0.1);
  border-radius: 14px;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.12);
  padding: 8px;
  display: none;
}

.theme-options.open { display: flex; }

.theme-swatch {
  width: 22px;
  height: 22px;
  border-radius: 50%;
  border: 2px solid transparent;
  cursor: pointer;
  transition: transform 0.1s;
  outline: none;
}

.theme-swatch:hover { transform: scale(1.15); }

.theme-swatch.active {
  box-shadow: 0 0 0 2px var(--blue);
}

.theme-swatch-light { background: #ffffff; border-color: #c8c8cc; }
.theme-swatch-dark  { background: #1c1c1e; border-color: #1c1c1e; }
.theme-swatch-pink  { background: #c2185b; border-color: #c2185b; }

/* ===== Animations ===== */
@keyframes slideIn {
  from {
    opacity: 0;
    transform: translateY(8px) scale(0.97);
  }
  to {
    opacity: 1;
    transform: translateY(0) scale(1);
  }
}

@keyframes dotBounce {
  0%, 55%, 100% {
    transform: translateY(0);
    opacity: 0.55;
  }
  28% {
    transform: translateY(-7px);
    opacity: 1;
  }
}
