:root {
  color-scheme: dark;
  --bg: #050505;
  --bg-soft: #0a0a0a;
  --panel: rgba(15, 15, 15, 0.95);
  --panel-strong: #121212;
  --border: rgba(212, 175, 55, 0.2);
  --text: #ffffff;
  --muted: #a0a0a0;
  --muted-strong: #cccccc;
  --accent: #d4af37;
  --accent-strong: #b5952f;
  --danger: #e74c3c;
  --warning: #f1c40f;
  --shadow: 0 20px 50px rgba(0, 0, 0, 0.7);
  --radius: 8px;
  --radius-sm: 4px;
  --mono: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, "Liberation Mono", monospace;
  --sans: 'Inter', ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  --serif: 'Playfair Display', ui-serif, Georgia, serif;
}

* {
  box-sizing: border-box;
}

html {
  min-height: 100%;
  background: var(--bg);
}

body {
  min-height: 100%;
  margin: 0;
  color: var(--text);
  font-family: var(--sans);
  line-height: 1.5;
}

/* Logo Styles */
.title-container {
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  gap: 0.5rem;
  margin-bottom: 0.7rem;
}

.kxn-logo-img {
  height: clamp(6rem, 15vw, 10rem);
  width: auto;
  object-fit: contain;
  filter: drop-shadow(0 0 15px rgba(212, 175, 55, 0.4));
  transition: transform 0.3s ease;
}

.kxn-logo-img:hover {
  transform: scale(1.05);
}

.gold-key {
  font-size: 0.85em;
  filter: drop-shadow(0 0 10px rgba(212, 175, 55, 0.5));
}

h1, h2, h3 {
  font-family: var(--serif);
}

button,
input,
textarea {
  font: inherit;
}

button {
  min-height: 44px;
  border: 0;
  border-radius: 999px;
  padding: 0.72rem 1.05rem;
  background: linear-gradient(135deg, var(--accent), var(--accent-strong));
  color: #04110d;
  cursor: pointer;
  font-weight: 800;
  letter-spacing: -0.01em;
  transition: transform 160ms ease, filter 160ms ease, border-color 160ms ease, background 160ms ease;
}

button:hover:not(:disabled) {
  transform: translateY(-1px);
  filter: brightness(1.06);
}

button:active:not(:disabled) {
  transform: translateY(0);
}

button:disabled {
  cursor: not-allowed;
  opacity: 0.55;
}

input,
textarea {
  width: 100%;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: rgba(7, 10, 16, 0.74);
  color: var(--text);
  outline: none;
  padding: 0.9rem 1rem;
  transition: border-color 160ms ease, box-shadow 160ms ease, background 160ms ease;
}

textarea {
  min-height: 54px;
  max-height: 170px;
  resize: vertical;
}

input::placeholder,
textarea::placeholder {
  color: #64748b;
}

input:focus,
textarea:focus,
button:focus-visible {
  border-color: rgba(33, 230, 161, 0.78);
  box-shadow: 0 0 0 4px rgba(33, 230, 161, 0.15);
}

h1,
h2,
h3,
p {
  margin-top: 0;
}

h1 {
  margin-bottom: 0;
  font-size: clamp(1.8rem, 4vw, 2.5rem);
  font-weight: 500;
  line-height: 0.9;
  letter-spacing: -0.075em;
}

h2 {
  margin-bottom: 0.35rem;
  font-size: clamp(1.45rem, 3vw, 2.1rem);
  letter-spacing: -0.04em;
}

h3 {
  margin-bottom: 0.65rem;
  font-size: 1rem;
}

.shell {
  width: min(1180px, calc(100% - 32px));
  margin: 0 auto;
  padding: 48px 0;
  display: flex;
  flex-direction: column;
  flex: 1;
}

.split-layout {
  display: grid;
  grid-template-columns: 320px 1fr;
  gap: 1.5rem;
  height: calc(100vh - 250px);
  min-height: 500px;
}

.sidebar-contacts {
  display: flex;
  flex-direction: column;
  overflow-y: auto;
  padding: 1.25rem;
}

.sidebar-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 1.25rem;
}

.main-chat {
  display: flex;
  flex-direction: column;
  padding: 0;
  overflow: hidden;
  border-left: 1px solid var(--border);
}

.hero {
  max-width: 820px;
  margin: 0 auto 28px;
  text-align: center;
}

.hero p {
  margin: 0 auto;
  max-width: 660px;
  color: var(--muted-strong);
  font-size: 1.08rem;
}

.hero-badge,
.eyebrow {
  display: inline-flex;
  align-items: center;
  gap: 0.4rem;
  margin-bottom: 0.75rem;
  color: var(--accent);
  font-size: 0.78rem;
  font-weight: 800;
  letter-spacing: 0.14em;
  text-transform: uppercase;
}

.panel {
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: linear-gradient(180deg, rgba(23, 29, 42, 0.96), rgba(14, 18, 27, 0.94));
  box-shadow: var(--shadow);
  backdrop-filter: blur(22px);
}

.join-panel {
  max-width: 640px;
  margin: 0 auto;
  padding: clamp(1.35rem, 4vw, 2.1rem);
}

.panel-heading {
  margin-bottom: 1.2rem;
}

.stack {
  display: grid;
  gap: 1rem;
}

.field {
  display: grid;
  gap: 0.42rem;
  color: var(--muted-strong);
  font-weight: 700;
}

.field small,
.muted,
.status {
  color: var(--muted);
}

.field small {
  font-weight: 500;
}

.status {
  min-height: 1.5rem;
  margin: 0.2rem 0 0;
}

.status.ok {
  color: var(--accent);
}

.status.warn {
  color: var(--warning);
}

.status.error {
  color: var(--danger);
}

.security-callout,
.security-note {
  margin-top: 1.25rem;
  border: 1px solid rgba(33, 230, 161, 0.22);
  border-radius: var(--radius-sm);
  background: rgba(33, 230, 161, 0.07);
  color: var(--muted-strong);
  padding: 1rem;
}

.security-note {
  border-color: rgba(251, 191, 36, 0.28);
  background: rgba(251, 191, 36, 0.07);
}

.hidden {
  display: none !important;
}

.topbar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 1rem;
  margin-bottom: 1rem;
  padding: 1.2rem;
}

.topbar h2 {
  margin-bottom: 0.1rem;
}

.topbar-actions,
.button-row {
  display: flex;
  flex-wrap: wrap;
  gap: 0.7rem;
}

.secondary-button {
  border: 1px solid var(--border);
  background: rgba(255, 255, 255, 0.04);
  color: var(--text);
}

.danger-button {
  border: 1px solid rgba(251, 113, 133, 0.38);
  background: rgba(251, 113, 133, 0.1);
  color: #fecdd3;
}

.app-grid {
  display: grid;
  grid-template-columns: minmax(280px, 340px) minmax(0, 1fr);
  gap: 1rem;
  align-items: stretch;
}

.sidebar,
.chat-panel {
  padding: 1rem;
}

.sidebar {
  display: grid;
  align-content: start;
  gap: 1rem;
}

.fingerprint {
  overflow-wrap: anywhere;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: rgba(0, 0, 0, 0.22);
  color: var(--accent);
  font-family: var(--mono);
  font-size: 0.85rem;
  line-height: 1.8;
  padding: 0.85rem;
}

.peer-list {
  display: grid;
  gap: 0.75rem;
}

.peer-card {
  display: grid;
  gap: 0.6rem;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: rgba(255, 255, 255, 0.035);
  padding: 0.85rem;
}

.peer-card-header {
  display: flex;
  justify-content: space-between;
  gap: 0.75rem;
  align-items: start;
}

.peer-name {
  margin: 0;
  font-weight: 850;
  word-break: break-word;
}

.peer-meta {
  margin: 0.2rem 0 0;
  color: var(--muted);
  font-size: 0.8rem;
  font-family: var(--mono);
}

.badge {
  display: inline-flex;
  align-items: center;
  border: 1px solid var(--border);
  border-radius: 999px;
  padding: 0.25rem 0.5rem;
  white-space: nowrap;
  font-size: 0.75rem;
  font-weight: 800;
}

.badge.verified {
  border-color: rgba(33, 230, 161, 0.34);
  background: rgba(33, 230, 161, 0.09);
  color: var(--accent);
}

.badge.unverified {
  border-color: rgba(251, 191, 36, 0.36);
  background: rgba(251, 191, 36, 0.1);
  color: #fde68a;
}

.peer-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
}

.peer-actions button,
.button-row button {
  min-height: 36px;
  padding: 0.5rem 0.75rem;
  font-size: 0.82rem;
}

.messages {
  display: flex;
  min-height: 520px;
  max-height: 62vh;
  overflow-y: auto;
  flex-direction: column;
  gap: 0.35rem; /* Tighter baseline spacing, custom gap per message row type */
  padding: 0.5rem 0.5rem 1rem;
  scroll-behavior: smooth;
}

.message-row {
  display: flex;
  width: 100%;
  margin-top: 0.15rem;
}

.message-row.new-burst {
  margin-top: 1rem; /* Clear burst grouping between different users/times */
}

.message-row.consecutive {
  margin-top: 0.15rem;
}

.message-row.own {
  justify-content: flex-end;
}

.message-bubble {
  max-width: min(72%, 760px);
  border: 1px solid var(--border);
  border-radius: 16px;
  background: rgba(255, 255, 255, 0.04);
  padding: 0.75rem 1rem;
  box-shadow: 0 3px 12px rgba(0, 0, 0, 0.2);
  transition: transform 0.2s ease, background 0.2s ease;
  position: relative;
}

.message-row.own .message-bubble {
  border-color: rgba(212, 175, 55, 0.22);
  background: linear-gradient(135deg, rgba(212, 175, 55, 0.08), rgba(181, 149, 47, 0.12));
  border-bottom-right-radius: 4px; /* Signal style speech bubble corners */
}

.message-row:not(.own) .message-bubble {
  border-color: rgba(255, 255, 255, 0.06);
  background: rgba(255, 255, 255, 0.045);
  border-bottom-left-radius: 4px;
}

.message-row.consecutive.own .message-bubble {
  border-bottom-right-radius: 16px; /* Smooth secondary bubbles */
}

.message-row.consecutive:not(.own) .message-bubble {
  border-bottom-left-radius: 16px;
}

.message-meta {
  margin-bottom: 0.35rem;
  color: var(--accent);
  font-size: 0.72rem;
  font-weight: 800;
  text-transform: uppercase;
  letter-spacing: 0.04em;
}

.message-text {
  white-space: pre-wrap;
  word-break: break-word;
  font-size: 0.95rem;
  line-height: 1.45;
}

.micro-time {
  font-size: 0.65rem;
  opacity: 0.55;
  margin-left: 10px;
  font-family: var(--mono);
  float: right;
  margin-top: 5px;
}

/* Animations */
@keyframes popIn {
  from {
    opacity: 0;
    transform: translateY(8px) scale(0.98);
  }
  to {
    opacity: 1;
    transform: translateY(0) scale(1);
  }
}

.animate-pop-in {
  animation: popIn 280ms cubic-bezier(0.16, 1, 0.3, 1) forwards;
}

.system-message {
  align-self: center;
  border: 1px solid var(--border);
  border-radius: 999px;
  background: rgba(255, 255, 255, 0.045);
  color: var(--muted-strong);
  padding: 0.55rem 0.9rem;
  text-align: center;
}

.composer {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  gap: 0.75rem;
  border-top: 1px solid var(--border);
  padding-top: 1rem;
}

.empty-state {
  margin: 0;
  color: var(--muted);
  font-style: italic;
}

.noscript {
  position: fixed;
  right: 1rem;
  bottom: 1rem;
  z-index: 10;
  border: 1px solid var(--danger);
  border-radius: var(--radius-sm);
  background: #190a0f;
  color: #fecdd3;
  padding: 1rem;
}

.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border: 0;
}

/* Contact Styling in Sidebar */
.contact-item {
  display: flex;
  align-items: center;
  gap: 0.85rem;
  padding: 0.85rem 1rem;
  border-bottom: 1px solid rgba(212, 175, 55, 0.08);
  cursor: pointer;
  transition: background 180ms ease, border-color 180ms ease;
  position: relative;
}

.contact-item:hover {
  background: rgba(212, 175, 55, 0.03);
}

.contact-item.active {
  background: rgba(212, 175, 55, 0.08);
  border-left: 3px solid var(--accent);
}

.contact-avatar-wrapper {
  position: relative;
  display: inline-block;
  flex-shrink: 0;
}

.contact-avatar {
  width: 44px;
  height: 44px;
  border-radius: 50%;
  background: linear-gradient(135deg, rgba(212, 175, 55, 0.2), rgba(181, 149, 47, 0.15));
  color: var(--accent);
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 700;
  font-size: 0.95rem;
  border: 1px solid rgba(212, 175, 55, 0.25);
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.3);
}

.status-indicator {
  width: 12px;
  height: 12px;
  border-radius: 50%;
  border: 2px solid var(--bg-soft);
  position: absolute;
  bottom: 0;
  right: 0;
  z-index: 2;
}

.status-indicator.online {
  background: #10b981;
  box-shadow: 0 0 8px rgba(16, 185, 129, 0.6);
}

.status-indicator.offline {
  background: #64748b;
}

.contact-info {
  display: flex;
  flex-direction: column;
  flex: 1;
  min-width: 0;
}

.contact-name {
  font-weight: 600;
  color: var(--text);
  font-size: 0.95rem;
  margin-bottom: 0.15rem;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.contact-id-text {
  font-size: 0.75rem;
  color: var(--muted);
  font-family: var(--mono);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.online-badge {
  font-size: 0.65rem;
  font-weight: 800;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: #10b981;
  background: rgba(16, 185, 129, 0.12);
  border: 1px solid rgba(16, 185, 129, 0.2);
  padding: 0.15rem 0.35rem;
  border-radius: 4px;
  position: absolute;
  right: 1rem;
  top: 0.85rem;
}

/* Chat bubble enhancements */
.message-bubble {
  max-width: min(72%, 760px);
  border: 1px solid var(--border);
  border-radius: 14px;
  background: rgba(255, 255, 255, 0.04);
  padding: 0.72rem 0.85rem;
  box-shadow: 0 4px 12px rgba(0,0,0,0.25);
}

.message-row.own .message-bubble {
  border-color: rgba(212, 175, 55, 0.25);
  background: linear-gradient(135deg, rgba(212, 175, 55, 0.08), rgba(181, 149, 47, 0.12));
}

.mobile-only-btn {
  display: none;
}

@media (max-width: 820px) {
  .shell {
    width: 100%;
    padding: 12px;
    transition: padding 0.3s ease;
  }

  body:has(#chat-view:not(.hidden)) {
    overflow: hidden;
    position: fixed;
    width: 100%;
    height: 100%;
  }

  body:has(#chat-view:not(.hidden)) .shell {
    height: 100vh;
    overflow: hidden; /* Prevent horizontal scrollbars during slides */
    padding: 12px 12px 0 12px;
  }

  body:has(#chat-view:not(.hidden)) .hero {
    display: none; /* Hide hero header on mobile to maximize chat area */
  }

  /* Make branding more compact on mobile login screen */
  .kxn-logo-img {
    height: 4.5rem;
  }
  h1 {
    font-size: 1.6rem;
  }
  .hero p {
    font-size: 0.9rem;
    padding: 0 10px;
  }

  .split-layout {
    display: flex !important;
    flex-direction: row;
    grid-template-columns: none !important;
    width: 200%; /* Side by side on double width */
    height: calc(100vh - 24px) !important;
    gap: 0;
    transition: transform 320ms cubic-bezier(0.16, 1, 0.3, 1);
  }

  .split-layout.chat-open {
    transform: translateX(-50%); /* Slide to the chat pane */
  }

  .sidebar-contacts {
    display: flex !important;
    width: 50% !important;
    height: 100%;
    flex-shrink: 0;
    border-right: none;
  }

  .main-chat {
    display: flex !important;
    width: 50% !important;
    height: 100%;
    flex-shrink: 0;
    border-left: none;
  }

  .mobile-only-btn {
    display: inline-block !important;
  }

  .messages {
    min-height: auto;
    flex: 1;
    max-height: calc(100vh - 180px);
  }

  .message-bubble {
    max-width: 85%;
  }
}

/* Tab Buttons */
.tab-btn {
  background: transparent;
  color: var(--muted);
  border: none;
  font-weight: 600;
  transition: color 180ms ease, background 180ms ease;
  cursor: pointer;
}
.tab-btn:hover {
  color: var(--text);
  background: rgba(255, 255, 255, 0.03);
}
.tab-btn.active {
  background: rgba(212, 175, 55, 0.12) !important;
  color: var(--accent) !important;
  border: 1px solid var(--border) !important;
}
