:root {
  --bg: #0b141a;
  --bg-panel: #111b21;
  --bg-sidebar: #202c33;
  --bg-bubble-in: #202c33;
  --bg-bubble-out: #005c4b;
  --text: #e9edef;
  --text-muted: #8696a0;
  --accent: #00a884;
  --border: #2a3942;
  --date-bg: #182229;
  --shadow: 0 1px 3px rgba(0, 0, 0, 0.4);
  --radius: 8px;
  --font: "Segoe UI", system-ui, -apple-system, sans-serif;
}

*,
*::before,
*::after {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
  height: 100%;
  font-family: var(--font);
  background: var(--bg);
  color: var(--text);
}

.app {
  display: grid;
  grid-template-columns: 320px 1fr;
  height: 100vh;
  overflow: hidden;
}

.app > * {
  min-height: 0;
}

/* Sidebar */
.sidebar {
  background: var(--bg-sidebar);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.sidebar-header {
  padding: 20px 16px 12px;
  border-bottom: 1px solid var(--border);
}

.sidebar-header h1 {
  margin: 0 0 8px;
  font-size: 1.1rem;
  font-weight: 600;
  line-height: 1.3;
}

.stats-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
  font-size: 0.75rem;
  color: var(--text-muted);
}

.stat-box {
  background: var(--bg-panel);
  padding: 8px;
  border-radius: var(--radius);
}

.stat-box strong {
  display: block;
  color: var(--text);
  font-size: 0.95rem;
}

.sidebar-tools {
  padding: 12px 16px;
  display: flex;
  flex-direction: column;
  gap: 8px;
  border-bottom: 1px solid var(--border);
}

.search-wrap input,
.date-jump input {
  width: 100%;
  padding: 10px 12px;
  border: none;
  border-radius: var(--radius);
  background: var(--bg-panel);
  color: var(--text);
  font-size: 0.9rem;
}

.owner-tools select {
  width: 100%;
  padding: 10px 12px;
  border: none;
  border-radius: var(--radius);
  background: var(--bg-panel);
  color: var(--text);
  font-size: 0.9rem;
}

.owner-tools select:focus {
  outline: 2px solid var(--accent);
}

.search-wrap input:focus,
.date-jump input:focus {
  outline: 2px solid var(--accent);
}

.btn-row {
  display: flex;
  gap: 8px;
}

.btn {
  flex: 1;
  padding: 8px 10px;
  border: none;
  border-radius: var(--radius);
  background: var(--bg-panel);
  color: var(--text-muted);
  font-size: 0.8rem;
  cursor: pointer;
  transition: background 0.15s, color 0.15s;
}

.btn:hover {
  background: var(--border);
  color: var(--text);
}

.btn-accent {
  background: var(--accent);
  color: #fff;
}

.btn-accent:hover {
  background: #06cf9c;
}

.senders-title {
  padding: 12px 16px 6px;
  font-size: 0.7rem;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--text-muted);
}

.senders-list {
  flex: 1;
  overflow-y: auto;
  padding: 0 8px 16px;
}

.sender-item {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 8px;
  border-radius: var(--radius);
  cursor: pointer;
  font-size: 0.88rem;
  transition: background 0.12s;
}

.sender-item:hover,
.sender-item.active {
  background: var(--bg-panel);
}

.sender-dot {
  width: 10px;
  height: 10px;
  border-radius: 50%;
  flex-shrink: 0;
}

.sender-name {
  flex: 1;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.sender-badge {
  background: var(--border);
  color: var(--text-muted);
  font-size: 0.72rem;
  padding: 2px 8px;
  border-radius: 10px;
  min-width: 28px;
  text-align: center;
}

/* Main chat */
.main {
  display: flex;
  flex-direction: column;
  background: var(--bg);
  background-image: radial-gradient(circle at 20% 30%, #0d2220 0%, transparent 50%),
    radial-gradient(circle at 80% 70%, #1a1f2e 0%, transparent 45%);
  min-width: 0;
  min-height: 0;
  overflow: hidden;
}

.chat-header {
  padding: 14px 20px;
  background: var(--bg-panel);
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.chat-header span {
  color: var(--text-muted);
  font-size: 0.85rem;
}

.messages {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  overflow-x: hidden;
  padding: 16px 8%;
  overscroll-behavior: contain;
}

.load-hint {
  text-align: center;
  color: var(--text-muted);
  font-size: 0.8rem;
  padding: 12px;
}

.date-separator {
  position: sticky;
  top: 8px;
  z-index: 2;
  display: flex;
  justify-content: center;
  margin: 16px 0;
}

.date-separator span {
  background: var(--date-bg);
  color: var(--text-muted);
  font-size: 0.75rem;
  padding: 6px 14px;
  border-radius: 8px;
  box-shadow: var(--shadow);
}

.msg-row {
  display: flex;
  margin-bottom: 2px;
  padding: 1px 0;
}

.msg-row.outgoing {
  justify-content: flex-end;
  padding-left: 12%;
}

.msg-row.incoming {
  justify-content: flex-start;
  padding-right: 12%;
}

.bubble {
  max-width: min(65%, 520px);
  padding: 6px 9px 8px;
  border-radius: 7.5px;
  box-shadow: 0 1px 0.5px rgba(0, 0, 0, 0.13);
  position: relative;
}

.incoming .bubble {
  background: var(--bg-bubble-in);
  border-top-left-radius: 0;
}

.outgoing .bubble {
  background: var(--bg-bubble-out);
  border-top-right-radius: 0;
  color: #e9edef;
}

.outgoing .bubble-body {
  color: #e9edef;
}

.outgoing .bubble-time {
  color: rgba(255, 255, 255, 0.65);
}

.sender-label {
  font-size: 0.78rem;
  font-weight: 600;
  margin-bottom: 2px;
}

.bubble-body {
  font-size: 0.92rem;
  line-height: 1.45;
  white-space: pre-wrap;
  word-break: break-word;
}

.bubble-body mark {
  background: #f9d71c;
  color: #111;
  border-radius: 2px;
  padding: 0 2px;
}

.bubble-time {
  text-align: right;
  font-size: 0.68rem;
  color: rgba(255, 255, 255, 0.55);
  margin-top: 4px;
}

/* Media */
.media-image {
  max-width: 280px;
  max-height: 320px;
  border-radius: 6px;
  cursor: zoom-in;
  display: block;
  margin: 4px 0;
}

.media-video {
  max-width: 320px;
  border-radius: 6px;
  display: block;
  margin: 4px 0;
}

.audio-wrap {
  display: flex;
  align-items: center;
  gap: 10px;
  min-width: 220px;
  margin: 4px 0;
}

.waveform {
  flex: 1;
  height: 32px;
  background: repeating-linear-gradient(
    90deg,
    rgba(255, 255, 255, 0.35) 0 3px,
    transparent 3px 6px
  );
  border-radius: 4px;
  opacity: 0.7;
}

.media-doc {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px;
  background: rgba(0, 0, 0, 0.2);
  border-radius: 6px;
  margin: 4px 0;
  text-decoration: none;
  color: var(--text);
}

.media-doc:hover {
  background: rgba(0, 0, 0, 0.35);
}

.doc-icon {
  font-size: 1.8rem;
}

.media-missing {
  color: var(--text-muted);
  font-style: italic;
  font-size: 0.85rem;
  padding: 8px;
  border: 1px dashed var(--border);
  border-radius: 6px;
}

/* Lightbox */
.lightbox {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.92);
  z-index: 100;
  align-items: center;
  justify-content: center;
  cursor: zoom-out;
}

.lightbox.open {
  display: flex;
}

.lightbox img {
  max-width: 95vw;
  max-height: 95vh;
  object-fit: contain;
}

.lightbox-close {
  position: absolute;
  top: 16px;
  right: 20px;
  color: #fff;
  font-size: 2rem;
  cursor: pointer;
  background: none;
  border: none;
}

/* Print */
@media print {
  .sidebar,
  .chat-header,
  .load-hint {
    display: none !important;
  }
  .app {
    grid-template-columns: 1fr;
  }
  .messages {
    overflow: visible;
    padding: 0;
  }
  body {
    background: #fff;
    color: #000;
  }
  .bubble {
    box-shadow: none;
    border: 1px solid #ddd;
  }
  .incoming .bubble,
  .outgoing .bubble {
    background: #f5f5f5 !important;
    color: #000 !important;
  }
}

@media (max-width: 768px) {
  .app {
    grid-template-columns: 1fr;
  }
  .sidebar {
    display: none;
  }
  .messages {
    padding: 12px;
  }
}

/* Index — lista de chats */
.index-page {
  min-height: 100vh;
  background: var(--bg);
}

.index-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 24px 32px;
  background: var(--bg-panel);
  border-bottom: 1px solid var(--border);
}

.index-header h1 {
  margin: 0;
  font-size: 1.4rem;
}

.index-sub {
  margin: 6px 0 0;
  color: var(--text-muted);
  font-size: 0.9rem;
}

.index-count {
  background: var(--accent);
  color: #fff;
  padding: 6px 14px;
  border-radius: 20px;
  font-size: 0.85rem;
  font-weight: 600;
}

.index-main {
  max-width: 720px;
  margin: 0 auto;
  padding: 24px 16px 48px;
}

.chat-list {
  list-style: none;
  margin: 0;
  padding: 0;
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.chat-card {
  display: flex;
  align-items: center;
  gap: 16px;
  padding: 16px 18px;
  background: var(--bg-panel);
  border: 1px solid var(--border);
  border-radius: 12px;
  text-decoration: none;
  color: var(--text);
  transition: border-color 0.15s, background 0.15s;
}

.chat-card:hover {
  border-color: var(--accent);
  background: var(--bg-sidebar);
}

.chat-card-icon {
  font-size: 1.8rem;
  flex-shrink: 0;
}

.chat-card-body {
  flex: 1;
  min-width: 0;
}

.chat-card-body h2 {
  margin: 0 0 4px;
  font-size: 1.05rem;
  font-weight: 600;
}

.chat-card-zip {
  margin: 0 0 8px;
  font-size: 0.75rem;
  color: var(--text-muted);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.chat-card-meta {
  display: flex;
  flex-wrap: wrap;
  gap: 8px 14px;
  font-size: 0.78rem;
  color: var(--text-muted);
}

.chat-card-arrow {
  color: var(--accent);
  font-size: 1.2rem;
}

.index-empty {
  text-align: center;
  padding: 48px 24px;
  color: var(--text-muted);
}

.index-empty pre {
  background: var(--bg-panel);
  padding: 12px 16px;
  border-radius: 8px;
  text-align: left;
  overflow-x: auto;
  font-size: 0.85rem;
  color: var(--text);
}

.index-hint {
  margin-top: 20px;
  font-size: 0.85rem;
}

.back-link {
  display: inline-block;
  margin-bottom: 10px;
  font-size: 0.8rem;
  color: var(--accent);
  text-decoration: none;
}

.back-link:hover {
  text-decoration: underline;
}
