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

:root {
  --bg: #efeae2;
  --bg-pattern: rgba(0,0,0,0.06);
  --header-bg: #075e54;
  --header-text: #fff;
  --bubble-me: #d9fdd3;
  --bubble-other: #fff;
  --bubble-me-text: #111;
  --bubble-other-text: #111;
  --system-bg: rgba(255,255,230,0.9);
  --system-text: #54656f;
  --date-bg: rgba(255,255,255,0.9);
  --date-text: #54656f;
  --time-text: #667781;
  --sender-border: rgba(0,0,0,0.05);
  --landing-bg: #f0f2f5;
  --landing-card: #fff;
  --landing-text: #41525d;
  --lightbox-bg: rgba(0,0,0,0.9);
  --scroll-btn-bg: rgba(0,0,0,0.4);
}

.dark-theme {
  --bg: #0b141a;
  --bg-pattern: rgba(255,255,255,0.02);
  --header-bg: #1f2c34;
  --header-text: #e9edef;
  --bubble-me: #005c4b;
  --bubble-other: #202c33;
  --bubble-me-text: #e9edef;
  --bubble-other-text: #e9edef;
  --system-bg: rgba(31,44,52,0.9);
  --system-text: #8696a0;
  --date-bg: rgba(31,44,52,0.9);
  --date-text: #8696a0;
  --time-text: rgba(255,255,255,0.45);
  --sender-border: rgba(255,255,255,0.05);
  --landing-bg: #111b21;
  --landing-card: #1f2c34;
  --landing-text: #8696a0;
}

body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif;
  background: var(--landing-bg);
  min-height: 100vh;
  transition: background 0.2s;
}

/* Landing */
#landing {
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 100vh;
  padding: 20px;
}

.landing-card {
  background: var(--landing-card);
  border-radius: 12px;
  padding: 48px 40px;
  max-width: 480px;
  width: 100%;
  text-align: center;
  box-shadow: 0 2px 12px rgba(0,0,0,0.08);
}

.landing-card h1 {
  font-size: 24px;
  color: var(--header-bg);
  margin-bottom: 12px;
}

.dark-theme .landing-card h1 { color: #00a884; }

.landing-card p {
  color: var(--landing-text);
  font-size: 14px;
  line-height: 1.5;
  margin-bottom: 24px;
}

.landing-card ol {
  text-align: left;
  color: var(--landing-text);
  font-size: 13px;
  line-height: 1.8;
  margin: 0 0 28px 20px;
}

.folder-btn {
  display: inline-block;
  background: #00a884;
  color: #fff;
  border: none;
  padding: 12px 32px;
  border-radius: 24px;
  font-size: 15px;
  font-weight: 500;
  cursor: pointer;
  transition: background 0.15s;
}

.folder-btn:hover { background: #008f72; }

#folder-input { display: none; }

/* Theme toggle */
#theme-toggle {
  position: fixed;
  top: 14px;
  right: 16px;
  z-index: 100;
  background: var(--header-bg);
  color: var(--header-text);
  border: none;
  width: 36px;
  height: 36px;
  border-radius: 50%;
  font-size: 18px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  box-shadow: 0 1px 4px rgba(0,0,0,0.2);
}

/* Chat view */
#chat-view {
  display: none;
  flex-direction: column;
  height: 100vh;
  background: var(--bg);
}

#chat-view.active { display: flex; }

#chat-header {
  background: var(--header-bg);
  color: var(--header-text);
  padding: 14px 20px;
  font-size: 17px;
  font-weight: 500;
  flex-shrink: 0;
  display: flex;
  align-items: center;
  gap: 12px;
}

#chat-header .back-btn {
  background: none;
  border: none;
  color: var(--header-text);
  font-size: 20px;
  cursor: pointer;
  padding: 4px;
}

/* View Switcher Dropdown */
#view-switcher {
  position: relative;
}

#view-switcher-btn {
  background: rgba(255,255,255,0.2);
  color: var(--header-text);
  border: none;
  padding: 6px 14px;
  border-radius: 18px;
  font-size: 14px;
  cursor: pointer;
  white-space: nowrap;
  display: flex;
  align-items: center;
  gap: 4px;
}

#view-switcher-btn:hover {
  background: rgba(255,255,255,0.3);
}

#view-switcher-btn .arrow {
  font-size: 10px;
}

.dropdown-menu {
  display: none;
  position: absolute;
  top: calc(100% + 6px);
  left: 0;
  min-width: 160px;
  background: var(--landing-card);
  border-radius: 8px;
  box-shadow: 0 4px 16px rgba(0,0,0,0.15);
  overflow: hidden;
  z-index: 60;
}

.dropdown-menu.open {
  display: block;
}

.dropdown-item {
  display: block;
  width: 100%;
  background: none;
  border: none;
  padding: 10px 16px;
  font-size: 14px;
  text-align: left;
  cursor: pointer;
  color: var(--landing-text);
}

.dropdown-item:hover {
  background: rgba(0,0,0,0.05);
}

.dark-theme .dropdown-item:hover {
  background: rgba(255,255,255,0.08);
}

.dropdown-item.active {
  color: #00a884;
  font-weight: 600;
}

/* Hide search in non-all views */
#chat-view[data-view="media"] #search-box,
#chat-view[data-view="links"] #search-box,
#chat-view[data-view="docs"] #search-box {
  display: none;
}

/* Search */
#search-box {
  display: flex;
  align-items: center;
  margin-left: auto;
  gap: 6px;
}

#search-input {
  padding: 6px 12px;
  border: none;
  border-radius: 18px;
  font-size: 14px;
  width: 220px;
  outline: none;
  background: rgba(255,255,255,0.2);
  color: var(--header-text);
}

#search-input::placeholder { color: rgba(255,255,255,0.5); }

#search-count {
  font-size: 12px;
  color: rgba(255,255,255,0.7);
  min-width: 40px;
  text-align: center;
}

#search-box button {
  background: none;
  border: none;
  color: var(--header-text);
  font-size: 16px;
  cursor: pointer;
  padding: 2px 6px;
}

#search-clear,
#search-prev {
  display: none;
}

#search-box.has-query #search-clear,
#search-box.has-query #search-prev {
  display: inline;
}

/* Search highlight */
.search-highlight {
  background: rgba(255, 235, 59, 0.5);
  border-radius: 2px;
}

.search-highlight.current {
  background: rgba(255, 152, 0, 0.7);
}

#messages {
  flex: 1;
  overflow-y: auto;
  padding: 8px 5%;
  background: var(--bg);
  background-image:
    radial-gradient(circle, var(--bg-pattern) 1px, transparent 1px);
  background-size: 24px 24px;
  scroll-behavior: smooth;
}

/* Date separator */
.date-sep {
  text-align: center;
  margin: 16px 0 8px;
}

.date-sep span {
  display: inline-block;
  background: var(--date-bg);
  color: var(--date-text);
  font-size: 12px;
  padding: 5px 12px;
  border-radius: 8px;
  box-shadow: 0 1px 1px rgba(0,0,0,0.06);
}

/* System message */
.msg-system {
  text-align: center;
  margin: 4px 0;
}

.msg-system .bubble {
  display: inline-block;
  background: var(--system-bg);
  color: var(--system-text);
  font-size: 12px;
  padding: 6px 14px;
  border-radius: 8px;
  max-width: 85%;
  box-shadow: 0 1px 1px rgba(0,0,0,0.04);
}

/* Bubbles */
.msg-row {
  display: flex;
  margin: 2px 0;
}

.msg-me { justify-content: flex-end; }
.msg-other { justify-content: flex-start; }

.bubble {
  position: relative;
  min-width: 90px;
  max-width: 65%;
  padding: 6px 12px 8px;
  border-radius: 8px;
  font-size: 14px;
  line-height: 1.35;
  word-wrap: break-word;
  overflow-wrap: break-word;
  box-shadow: 0 1px 1px rgba(0,0,0,0.08);
}

.msg-me .bubble {
  background: var(--bubble-me);
  color: var(--bubble-me-text);
  border-top-right-radius: 0;
}

.msg-other .bubble {
  background: var(--bubble-other);
  color: var(--bubble-other-text);
  border-top-left-radius: 0;
}

/* Bubble tails */
.msg-me .bubble::before {
  content: '';
  position: absolute;
  top: 0;
  right: -8px;
  width: 0;
  height: 0;
  border-left: 8px solid var(--bubble-me);
  border-bottom: 8px solid transparent;
}

.msg-other .bubble::before {
  content: '';
  position: absolute;
  top: 0;
  left: -8px;
  width: 0;
  height: 0;
  border-right: 8px solid var(--bubble-other);
  border-bottom: 8px solid transparent;
}

.bubble .sender {
  font-size: 12.5px;
  font-weight: 600;
  margin-bottom: 2px;
}

.bubble .text-row {
  display: flex;
  align-items: center;
  gap: 8px;
}

.bubble .text-row .text {
  flex: 1;
}

.bubble .timestamp {
  display: flex;
  align-items: center;
  gap: 4px;
  font-size: 11px;
  color: var(--time-text);
  white-space: nowrap;
  align-self: flex-end;
  line-height: 1;
}

.bubble .edited-mark {
  font-size: 10px;
  opacity: 0.75;
  font-style: italic;
}

.bubble .text { white-space: pre-wrap; }
.bubble .text b { font-weight: 600; }
.bubble .text a { color: #027eb5; text-decoration: none; }
.dark-theme .bubble .text a { color: #53bdeb; }

/* Media */
.bubble img.media, .bubble video, .bubble .sticker {
  display: block;
  max-width: 100%;
  border-radius: 6px;
  margin: 4px 0;
  cursor: pointer;
}

.bubble img.media { max-height: 300px; object-fit: cover; }
.bubble .sticker { max-height: 160px; background: transparent; }
.bubble video { max-height: 300px; }
.bubble audio { display: block; margin: 6px 0; max-width: 100%; }

.bubble .media-placeholder {
  display: inline-block;
  background: rgba(0,0,0,0.05);
  color: var(--time-text);
  padding: 8px 14px;
  border-radius: 6px;
  font-size: 12px;
  font-style: italic;
}

/* Lightbox */
#lightbox {
  display: none;
  position: fixed;
  inset: 0;
  z-index: 200;
  background: var(--lightbox-bg);
  align-items: center;
  justify-content: center;
  cursor: zoom-out;
}

#lightbox.active { display: flex; }

#lightbox img {
  max-width: 94vw;
  max-height: 94vh;
  object-fit: contain;
  border-radius: 4px;
}

/* Sender Picker Modal */
#sender-picker {
  display: none;
  position: fixed;
  inset: 0;
  z-index: 250;
  background: rgba(0,0,0,0.5);
  align-items: center;
  justify-content: center;
}

#sender-picker.active { display: flex; }

.sender-picker-card {
  background: var(--landing-card);
  border-radius: 12px;
  padding: 36px 32px;
  max-width: 400px;
  width: 90%;
  text-align: center;
  box-shadow: 0 8px 32px rgba(0,0,0,0.2);
}

.sender-picker-card h2 {
  font-size: 20px;
  color: var(--header-bg);
  margin-bottom: 8px;
}

.dark-theme .sender-picker-card h2 { color: #00a884; }

.sender-picker-card p {
  color: var(--landing-text);
  font-size: 14px;
  line-height: 1.5;
  margin-bottom: 20px;
}

#sender-picker-options {
  display: flex;
  flex-direction: column;
  gap: 10px;
}

#sender-picker-options button {
  display: block;
  width: 100%;
  background: #00a884;
  color: #fff;
  border: none;
  padding: 12px 24px;
  border-radius: 24px;
  font-size: 15px;
  font-weight: 500;
  cursor: pointer;
  transition: background 0.15s;
}

#sender-picker-options button:hover { background: #008f72; }

/* Scroll-to-bottom */
#scroll-bottom {
  display: none;
  position: fixed;
  bottom: 24px;
  left: 50%;
  transform: translateX(-50%);
  width: 42px;
  height: 42px;
  border-radius: 50%;
  background: var(--scroll-btn-bg);
  color: #fff;
  border: none;
  font-size: 22px;
  cursor: pointer;
  align-items: center;
  justify-content: center;
  z-index: 50;
  box-shadow: 0 2px 6px rgba(0,0,0,0.2);
}

#scroll-bottom.visible { display: flex; }

/* ── View: Media Grid ────────────────────────────────────────────── */
.media-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(150px, 1fr));
  gap: 4px;
  padding: 8px;
}

.media-grid-item {
  aspect-ratio: 1;
  overflow: hidden;
  border-radius: 4px;
  background: rgba(0,0,0,0.05);
}

.media-grid-item img,
.media-grid-item video {
  width: 100%;
  height: 100%;
  object-fit: cover;
  display: block;
  cursor: pointer;
}

.audio-list {
  padding: 8px 16px;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.audio-card {
  background: var(--bubble-other);
  border-radius: 8px;
  padding: 12px;
  box-shadow: 0 1px 2px rgba(0,0,0,0.06);
}

.audio-card audio {
  width: 100%;
  margin-bottom: 6px;
}

/* ── View: Links List ────────────────────────────────────────────── */
.links-list {
  padding: 12px 16px;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.link-card {
  background: var(--bubble-other);
  color: var(--bubble-other-text);
  border-radius: 8px;
  padding: 12px 16px;
  box-shadow: 0 1px 2px rgba(0,0,0,0.06);
}

.link-url {
  display: block;
  color: #027eb5;
  text-decoration: none;
  font-size: 14px;
  word-break: break-all;
  margin-bottom: 6px;
}

.dark-theme .link-url {
  color: #53bdeb;
}

.link-context {
  font-size: 13px;
  color: var(--system-text);
  margin-bottom: 6px;
  line-height: 1.4;
  white-space: pre-wrap;
  word-break: break-word;
}

/* ── View: Docs List ─────────────────────────────────────────────── */
.docs-list {
  padding: 12px 16px;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.doc-card {
  background: var(--bubble-other);
  color: var(--bubble-other-text);
  border-radius: 8px;
  padding: 12px 16px;
  box-shadow: 0 1px 2px rgba(0,0,0,0.06);
  display: flex;
  align-items: center;
  gap: 12px;
}

.doc-icon {
  font-size: 28px;
  flex-shrink: 0;
}

.doc-info {
  flex: 1;
  min-width: 0;
}

.doc-name {
  font-size: 14px;
  font-weight: 500;
  word-break: break-all;
}

.doc-download {
  background: #00a884;
  color: #fff;
  text-decoration: none;
  padding: 6px 14px;
  border-radius: 16px;
  font-size: 13px;
  font-weight: 500;
  white-space: nowrap;
  flex-shrink: 0;
}

.doc-download:hover {
  background: #008f72;
}

/* ── Shared card meta ────────────────────────────────────────────── */
.card-meta {
  font-size: 12px;
  color: var(--time-text);
  margin-top: 2px;
}

/* ── Empty state ─────────────────────────────────────────────────── */
.view-empty {
  text-align: center;
  color: var(--system-text);
  padding: 60px 20px;
  font-size: 15px;
}

/* Responsive */
@media (max-width: 600px) {
  .bubble { max-width: 85%; }
  .landing-card { padding: 32px 24px; }
  #messages { padding: 8px 3%; }
  .media-grid { grid-template-columns: repeat(auto-fill, minmax(100px, 1fr)); }
  .doc-card { flex-wrap: wrap; }
}
