:root {
  --bg: #0d1117;
  --panel: #131a24;
  --panel-2: #1a2330;
  --border: #24313f;
  --text: #dbe4ee;
  --text-dim: #7c8a9a;
  --accent: #3ddc97;
  --accent-dim: #1f6b4d;
  --danger: #e5573f;
  --mono: "JetBrains Mono", "Fira Code", ui-monospace, SFMono-Regular, Menlo, monospace;
  --sans: "Inter", -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: var(--sans);
}

.hidden { display: none !important; }

/* ---------- Auth ---------- */

.auth-view {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  background:
    radial-gradient(circle at 20% 20%, rgba(61, 220, 151, 0.06), transparent 40%),
    radial-gradient(circle at 80% 80%, rgba(61, 220, 151, 0.05), transparent 40%),
    var(--bg);
}

.auth-box {
  width: 380px;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 32px;
}

.auth-box h1 {
  margin: 0 0 8px 0;
  font-size: 22px;
  letter-spacing: 0.02em;
}

.auth-box .hint {
  color: var(--text-dim);
  font-size: 12.5px;
  line-height: 1.5;
  margin-bottom: 20px;
}

.tabs {
  display: flex;
  gap: 4px;
  margin-bottom: 16px;
  background: var(--panel-2);
  border-radius: 8px;
  padding: 4px;
}

.tab-btn {
  flex: 1;
  background: transparent;
  border: none;
  color: var(--text-dim);
  padding: 8px 0;
  border-radius: 6px;
  cursor: pointer;
  font-family: var(--sans);
  font-size: 13.5px;
}

.tab-btn.active {
  background: var(--accent-dim);
  color: var(--accent);
}

.tab-panel { display: none; flex-direction: column; gap: 10px; }
.tab-panel.active { display: flex; }

input[type="text"], input[type="password"] {
  background: var(--panel-2);
  border: 1px solid var(--border);
  color: var(--text);
  padding: 10px 12px;
  border-radius: 8px;
  font-size: 14px;
  font-family: var(--sans);
  outline: none;
}

input[type="text"]:focus, input[type="password"]:focus {
  border-color: var(--accent);
}

button {
  cursor: pointer;
}

.tab-panel button[type="submit"], #create-group-form button[type="submit"] {
  background: var(--accent);
  border: none;
  color: #06231a;
  font-weight: 600;
  padding: 10px 0;
  border-radius: 8px;
  font-size: 14px;
}

.error {
  font-size: 12.5px;
  color: var(--danger);
  min-height: 16px;
}

.error.success {
  color: var(--accent);
}

.inline-link {
  color: var(--accent);
  text-decoration: underline;
  cursor: pointer;
}

/* ---------- Chat Layout ---------- */

.chat-view {
  display: grid;
  grid-template-columns: 280px 1fr;
  height: 100vh;
}

.sidebar {
  background: var(--panel);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  overflow-y: auto;
}

.sidebar-header {
  padding: 16px;
  border-bottom: 1px solid var(--border);
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-size: 13px;
}

.sidebar-section {
  padding: 14px 16px;
  border-bottom: 1px solid var(--border);
}

.sidebar-section h3 {
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--text-dim);
  margin: 0 0 10px 0;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.chat-list { display: flex; flex-direction: column; gap: 4px; }

.chat-item {
  padding: 8px 10px;
  border-radius: 6px;
  font-size: 14px;
  color: var(--text);
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 6px;
}

.chat-item.clickable {
  cursor: pointer;
}

.chat-item.clickable:hover { background: var(--panel-2); }

.relationship-tag {
  font-size: 11px;
  color: var(--text-dim);
  font-style: italic;
}

.status-dot {
  display: inline-block;
  width: 8px;
  height: 8px;
  border-radius: 50%;
  flex-shrink: 0;
}

.status-dot.online {
  background: var(--accent);
  box-shadow: 0 0 5px var(--accent);
}

.status-dot.offline {
  background: transparent;
  border: 1.5px solid var(--text-dim);
}

.member-row > span {
  display: flex;
  align-items: center;
  gap: 8px;
}

.admin-subsection {
  margin-top: 12px;
  padding-top: 10px;
  border-top: 1px solid var(--border);
}

.admin-subsection .small-btn.danger {
  width: 100%;
  text-align: center;
  padding: 8px 10px;
  white-space: normal;
  line-height: 1.4;
}

.admin-subtitle {
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--text-dim);
  margin-bottom: 8px;
}

.muted-text {
  color: var(--text-dim);
  font-size: 13px;
}

.add-member-row {
  display: flex;
  gap: 8px;
  align-items: center;
}

.add-member-row select {
  background: var(--panel-2);
  border: 1px solid var(--border);
  color: var(--text);
  padding: 6px 8px;
  border-radius: 6px;
  font-size: 13px;
  flex: 1;
}

.toast {
  position: fixed;
  bottom: 24px;
  right: 24px;
  background: var(--panel-2);
  border: 1px solid var(--accent);
  color: var(--text);
  padding: 12px 18px;
  border-radius: 8px;
  font-size: 13.5px;
  box-shadow: 0 6px 20px rgba(0, 0, 0, 0.4);
  opacity: 0;
  transform: translateY(10px);
  transition: opacity 0.3s ease, transform 0.3s ease;
  z-index: 1000;
}

.toast.show {
  opacity: 1;
  transform: translateY(0);
}

.small-btn {
  background: var(--panel-2);
  border: 1px solid var(--border);
  color: var(--accent);
  font-size: 11px;
  padding: 4px 8px;
  border-radius: 6px;
}

.small-btn.danger { color: var(--danger); }

.panel {
  background: var(--panel-2);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 10px;
  margin-bottom: 10px;
}

#create-group-form { display: flex; flex-direction: column; gap: 8px; }

.member-checks {
  display: flex;
  flex-direction: column;
  gap: 4px;
  max-height: 120px;
  overflow-y: auto;
  font-size: 13px;
}

.member-check { display: flex; align-items: center; gap: 6px; }

/* ---------- Main chat area ---------- */

.main {
  display: flex;
  flex-direction: column;
  height: 100vh;
}

.chat-header {
  padding: 16px 20px;
  border-bottom: 1px solid var(--border);
  font-size: 15px;
  font-weight: 600;
  display: flex;
  align-items: center;
  gap: 12px;
}

.chat-title-col {
  display: flex;
  flex-direction: column;
  gap: 2px;
  min-width: 0;
  flex: 1;
}

.chat-subtitle {
  font-size: 12px;
  font-weight: 400;
  color: var(--text-dim);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.group-description {
  font-size: 13px;
  color: var(--text-dim);
  line-height: 1.5;
  margin-bottom: 12px;
  padding-bottom: 12px;
  border-bottom: 1px solid var(--border);
}

.header-actions {
  display: flex;
  gap: 8px;
}

.chat-body {
  flex: 1;
  display: flex;
  flex-direction: row;
  min-height: 0;
  overflow: hidden;
}

.chat-content {
  flex: 1;
  min-width: 0;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.admin-panel {
  width: 280px;
  flex-shrink: 0;
  background: var(--panel);
  border-left: 1px solid var(--border);
  padding: 16px 20px;
  font-size: 13px;
  overflow-y: auto;
}

.member-row {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 4px 0;
}

.member-row > span { flex: 1; }

.messages {
  flex: 1;
  overflow-y: auto;
  padding: 20px;
  display: flex;
  flex-direction: column;
  gap: 14px;
}

.msg {
  max-width: 60%;
  min-width: 90px;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 12px 16px;
  align-self: flex-start;
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.msg.mine {
  align-self: flex-end;
  background: var(--accent-dim);
  border-color: var(--accent);
}

.msg-sender {
  font-size: 11.5px;
  color: var(--text-dim);
  font-family: var(--mono);
  letter-spacing: 0.02em;
}

.msg-text {
  font-size: 14.5px;
  line-height: 1.5;
  white-space: pre-wrap;
  word-break: break-word;
}

.message-form {
  display: flex;
  gap: 10px;
  padding: 16px 20px;
  border-top: 1px solid var(--border);
}

.message-form input {
  flex: 1;
}

.message-form button {
  background: var(--accent);
  border: none;
  color: #06231a;
  font-weight: 600;
  padding: 0 20px;
  border-radius: 8px;
}

/* ---------- Chat-Item Erweiterungen (Name + Ungelesen-Badge) ---------- */

.chat-item-name {
  flex: 1;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.unread-badge {
  background: var(--accent);
  color: #06231a;
  font-size: 10.5px;
  font-weight: 700;
  padding: 1px 6px;
  border-radius: 10px;
  min-width: 16px;
  text-align: center;
}

/* ---------- Link-Button (z.B. "Mit Backup wiederherstellen") ---------- */

.link-btn {
  background: transparent;
  border: none;
  color: var(--accent);
  text-decoration: underline;
  font-size: 12px;
  padding: 0;
  cursor: pointer;
}

/* ---------- Sicherheits-Panel (2FA, Geraete, Backup) ---------- */

.security-panel {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.totp-setup-result {
  display: flex;
  flex-direction: column;
  gap: 8px;
  margin-top: 8px;
  padding: 8px;
  background: var(--panel);
  border-radius: 6px;
}

.totp-setup-result code {
  background: var(--panel);
  padding: 6px;
  border-radius: 4px;
  font-family: var(--mono);
  word-break: break-all;
  font-size: 12px;
}

/* ---------- Nachrichten: Footer, Signatur, Bearbeitet-Tag ---------- */

.msg-footer {
  display: flex;
  gap: 10px;
  align-items: center;
}

.msg-time {
  font-size: 11px;
  color: var(--text-dim);
}

.msg-edited-tag {
  font-size: 11px;
  color: var(--text-dim);
  font-style: italic;
}

.sig-warning {
  color: var(--danger);
  font-weight: 700;
}

/* ---------- Reaktionen ---------- */

.reaction-bar {
  display: flex;
  gap: 6px;
  flex-wrap: wrap;
}

.reaction-chip {
  background: var(--panel-2);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 2px 9px;
  font-size: 12.5px;
  cursor: default;
}

.msg-actions {
  display: flex;
  gap: 6px;
  opacity: 0.6;
}

.msg-actions:hover {
  opacity: 1;
}

/* ---------- Datei-Anhaenge ---------- */

.attachment-box {
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.attachment-image {
  max-width: 260px;
  max-height: 260px;
  border-radius: 8px;
  border: 1px solid var(--border);
}

.attachment-link {
  color: var(--accent);
  font-size: 13px;
  text-decoration: none;
}

.attachment-link:hover {
  text-decoration: underline;
}

/* ---------- Pagination ---------- */

.load-more-btn {
  margin: 10px auto 0 auto;
  display: block;
}

/* ================================================================
   Responsive / Mobile
   ================================================================
   Auf Desktop unveraendert (Sidebar + Chat nebeneinander, Mitglieder-
   Panel als rechte Spalte). Ab 768px Breite wechselt die App in eine
   Einzelspalten-Ansicht: entweder Chat-Liste ODER offener Chat, mit
   Zurueck-Button, und das Mitglieder-Panel wird zum Vollbild-Overlay
   statt einer festen Seitenspalte. */

.mobile-only { display: none; }

@media (max-width: 768px) {
  html, body {
    height: 100vh;
    height: 100dvh;
  }

  .auth-view { padding: 16px; }

  .auth-box {
    width: 100%;
    max-width: 380px;
    padding: 24px;
  }

  .chat-view {
    grid-template-columns: 1fr;
    height: 100vh;
    height: 100dvh;
  }

  .sidebar {
    width: 100%;
    height: 100vh;
    height: 100dvh;
  }

  .main {
    display: none;
    width: 100%;
    height: 100vh;
    height: 100dvh;
  }

  /* Wird per JS umgeschaltet, sobald ein Chat geoeffnet wird */
  .chat-view.mobile-chat-open .sidebar { display: none; }
  .chat-view.mobile-chat-open .main { display: flex; }

  .mobile-only {
    display: inline-flex;
    align-items: center;
    justify-content: center;
  }

  /* Touch-freundlichere Groessen statt kleiner Desktop-Buttons */
  .small-btn {
    min-height: 40px;
    padding: 8px 12px;
    font-size: 13px;
  }

  .chat-item {
    min-height: 48px;
    font-size: 15px;
  }

  /* 16px verhindert automatisches Hineinzoomen beim Fokussieren auf iOS */
  input[type="text"],
  input[type="password"],
  .message-form input {
    font-size: 16px;
  }

  .message-form {
    padding: 10px 12px;
    gap: 8px;
  }

  .messages {
    padding: 12px;
    gap: 10px;
  }

  .msg {
    max-width: 85%;
  }

  .chat-header {
    padding: 10px 12px;
    gap: 8px;
  }

  .header-actions {
    flex-wrap: wrap;
    justify-content: flex-end;
    gap: 6px;
  }

  /* Kein Hover auf Touch-Geraeten - Reaktions-/Bearbeiten-Buttons immer voll sichtbar */
  .msg-actions { opacity: 1; }

  .sidebar-header { padding: 12px 14px; }
  .sidebar-section { padding: 12px 14px; }

  /* Mitglieder-Panel wird zum Vollbild-Overlay statt einer festen Seitenspalte */
  .admin-panel {
    position: fixed;
    top: 0;
    right: 0;
    bottom: 0;
    width: 85%;
    max-width: 340px;
    height: 100vh;
    height: 100dvh;
    z-index: 60;
    transform: translateX(100%);
    transition: transform 0.25s ease;
    box-shadow: -4px 0 24px rgba(0, 0, 0, 0.5);
  }

  .admin-panel.mobile-open {
    transform: translateX(0);
  }

  .admin-panel-overlay {
    position: fixed;
    inset: 0;
    background: rgba(0, 0, 0, 0.5);
    z-index: 55;
  }

  .close-panel-btn {
    position: fixed;
    top: 12px;
    right: 12px;
    z-index: 61;
  }
}

.break-all { word-break: break-all; }

.danger-title {
  color: var(--danger) !important;
  margin-top: 16px;
}

.discover-group-row {
  align-items: flex-start;
}

.discover-group-info {
  flex: 1;
  display: flex;
  flex-direction: column;
  gap: 2px;
  font-size: 13px;
}

.totp-status-enabled {
  color: var(--accent);
  font-weight: 600;
}
