:root {
  --bg: #f4f5f7;
  --panel: #ffffff;
  --border: #dfe3e8;
  --text: #1c2430;
  --muted: #6b7684;
  --accent: #2f6bd8;
  --accent-hover: #2557b3;
  --ok: #1f8a52;
  --warn: #b8860b;
  --err: #c0392b;
  --radius: 10px;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #14181d;
    --panel: #1c2128;
    --border: #2c333c;
    --text: #e6eaef;
    --muted: #939ead;
    --accent: #5b8def;
    --accent-hover: #7aa4f5;
    --ok: #47c07e;
    --warn: #e0b341;
    --err: #ef6c5a;
  }
}

* { box-sizing: border-box; }

/* Das hidden-Attribut muss staerker sein als eigene display-Regeln -
   sonst bleiben als versteckt markierte Elemente sichtbar. */
[hidden] { display: none !important; }


body {
  margin: 0;
  font: 15px/1.5 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
}

h1, h2 { margin: 0 0 .6rem; line-height: 1.25; }
h2 { font-size: 1.05rem; }

.muted { color: var(--muted); font-size: .88rem; }
.error { color: var(--err); font-size: .9rem; }
.success { color: var(--ok); font-size: .9rem; }

button {
  font: inherit;
  padding: .5rem .95rem;
  border: 1px solid transparent;
  border-radius: 8px;
  background: var(--accent);
  color: #fff;
  cursor: pointer;
}
button:hover { background: var(--accent-hover); }
button:disabled { opacity: .55; cursor: not-allowed; }
button.secondary { background: transparent; color: var(--text); border-color: var(--border); }
button.secondary:hover { background: var(--bg); }
button.danger { background: var(--err); }

.linkbtn {
  background: none;
  border: none;
  color: var(--muted);
  text-decoration: underline;
  padding: .3rem;
}
.linkbtn:hover { background: none; color: var(--text); }

input, select, textarea {
  font: inherit;
  width: 100%;
  padding: .5rem .6rem;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--panel);
  color: var(--text);
}
input:focus, select:focus, textarea:focus {
  outline: 2px solid var(--accent);
  outline-offset: -1px;
}
textarea { resize: vertical; }
label { display: block; font-size: .85rem; color: var(--muted); margin: .7rem 0 .25rem; }
label.check { display: flex; align-items: center; gap: .5rem; color: var(--text); }
label.check input { width: auto; }

/* Anmeldung */
.login-page {
  display: grid;
  place-items: center;
  min-height: 100vh;
}
.login-box {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 2rem;
  width: min(380px, 92vw);
}
.login-box button { width: 100%; margin-top: 1rem; }

/* Kopfleiste */
.topbar {
  display: flex;
  align-items: center;
  gap: 1.5rem;
  flex-wrap: wrap;
  padding: .75rem 1.25rem;
  background: var(--panel);
  border-bottom: 1px solid var(--border);
  position: sticky;
  top: 0;
  z-index: 10;
}
.brand { display: flex; align-items: center; gap: .75rem; }

.device-badge {
  display: inline-flex;
  align-items: center;
  gap: .45rem;
  font-size: .85rem;
  color: var(--muted);
  padding: .2rem .6rem;
  border: 1px solid var(--border);
  border-radius: 999px;
}
.dot {
  width: .6rem; height: .6rem;
  border-radius: 50%;
  background: var(--muted);
  flex-shrink: 0;
}
.dot.online { background: var(--ok); box-shadow: 0 0 0 3px color-mix(in srgb, var(--ok) 25%, transparent); }
.dot.offline { background: var(--err); }

.tabs { display: flex; gap: .25rem; margin-left: auto; flex-wrap: wrap; }
.tab {
  background: transparent;
  color: var(--muted);
  border: 1px solid transparent;
}
.tab:hover { background: var(--bg); color: var(--text); }
.tab.active { background: var(--bg); color: var(--text); border-color: var(--border); }

main { padding: 1.25rem; max-width: 1180px; margin: 0 auto; }

.view { display: none; }
.view.active { display: block; }

.panel {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1.1rem;
  margin-bottom: 1.1rem;
}
.panel-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
  flex-wrap: wrap;
  margin-bottom: .6rem;
}

.cards {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(170px, 1fr));
  gap: .8rem;
  margin-bottom: 1.1rem;
}
.card {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: .9rem 1rem;
  display: flex;
  flex-direction: column;
  gap: .2rem;
}
.card-label { font-size: .8rem; color: var(--muted); }
.card-value { font-size: 1.6rem; font-weight: 600; font-variant-numeric: tabular-nums; }

.grid-2 { display: grid; grid-template-columns: repeat(auto-fit, minmax(300px, 1fr)); gap: 1.1rem; }
.grid-2 .panel { margin-bottom: 0; }

.kv { margin: 0; display: grid; grid-template-columns: auto 1fr; gap: .35rem 1rem; }
.kv dt { color: var(--muted); font-size: .85rem; }
.kv dd { margin: 0; font-size: .9rem; overflow-wrap: anywhere; }

.table { width: 100%; border-collapse: collapse; font-size: .88rem; }
.table th {
  text-align: left;
  font-weight: 600;
  color: var(--muted);
  font-size: .8rem;
  padding: .45rem .5rem;
  border-bottom: 1px solid var(--border);
  white-space: nowrap;
}
.table td { padding: .5rem; border-bottom: 1px solid var(--border); vertical-align: top; }
.table tr:last-child td { border-bottom: none; }
.table .body-cell { max-width: 42ch; overflow-wrap: anywhere; }
.table-wrap { overflow-x: auto; }

.dir { font-size: 1rem; }
.dir.out { color: var(--accent); }
.dir.in { color: var(--ok); }

.badge {
  display: inline-block;
  padding: .1rem .5rem;
  border-radius: 999px;
  font-size: .75rem;
  border: 1px solid var(--border);
  white-space: nowrap;
}
.badge.queued, .badge.dispatched { color: var(--warn); border-color: currentColor; }
.badge.sent, .badge.delivered, .badge.received { color: var(--ok); border-color: currentColor; }
.badge.failed, .badge.expired { color: var(--err); border-color: currentColor; }

.filters { display: flex; gap: .5rem; flex-wrap: wrap; }
.filters select, .filters input { width: auto; min-width: 150px; }

.send-form .row { margin-bottom: .5rem; }
.send-footer {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
  margin-top: .6rem;
}

.pager { margin-top: .8rem; text-align: center; }

.token-card {
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: .9rem;
  margin-bottom: .7rem;
}
.token-card.inactive { opacity: .6; }
.token-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: .75rem;
  flex-wrap: wrap;
}
.token-name { font-weight: 600; }
.token-actions { display: flex; gap: .4rem; flex-wrap: wrap; }
.token-actions button { padding: .3rem .65rem; font-size: .82rem; }
.token-meta {
  margin-top: .5rem;
  display: flex;
  gap: 1.2rem;
  flex-wrap: wrap;
  font-size: .84rem;
  color: var(--muted);
}
code, pre, .mono { font-family: ui-monospace, SFMono-Regular, Menlo, monospace; }
.secret {
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: .7rem;
  overflow-x: auto;
  font-size: .85rem;
  user-select: all;
}

dialog {
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--panel);
  color: var(--text);
  padding: 1.4rem;
  width: min(520px, 92vw);
}
dialog::backdrop { background: rgba(0, 0, 0, .45); }
.dialog-actions { display: flex; justify-content: flex-end; gap: .5rem; margin-top: 1.2rem; }
.two-col { display: grid; grid-template-columns: 1fr 1fr; gap: .8rem; }

.toast {
  position: fixed;
  bottom: 1.2rem;
  left: 50%;
  transform: translateX(-50%);
  background: var(--text);
  color: var(--panel);
  padding: .6rem 1.1rem;
  border-radius: 999px;
  font-size: .88rem;
  z-index: 100;
}

.api-block { margin-bottom: 1.2rem; }
.api-block pre {
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: .8rem;
  overflow-x: auto;
  font-size: .82rem;
  margin: .4rem 0 0;
}
.api-method {
  display: inline-block;
  font-family: ui-monospace, monospace;
  font-size: .78rem;
  padding: .1rem .45rem;
  border-radius: 5px;
  background: var(--accent);
  color: #fff;
  margin-right: .5rem;
}
.api-method.get { background: var(--ok); }

@media (max-width: 640px) {
  .topbar { gap: .7rem; }
  .tabs { margin-left: 0; width: 100%; }
  .two-col { grid-template-columns: 1fr; }
}

/* ---------------------------------------------------------------------------
   Messenger: Unterhaltungen links, Verlauf rechts
   --------------------------------------------------------------------------- */

#view-messenger { height: calc(100vh - 4.6rem - 2.5rem); }

.messenger {
  display: grid;
  grid-template-columns: minmax(240px, 320px) 1fr;
  gap: 1rem;
  height: 100%;
  min-height: 0;
}

.conv-list,
.thread {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  min-height: 0;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.conv-head {
  display: flex;
  gap: .5rem;
  padding: .7rem;
  border-bottom: 1px solid var(--border);
}
.conv-head input { min-width: 0; }
.conv-head button { white-space: nowrap; }

.conv-items {
  list-style: none;
  margin: 0;
  padding: 0;
  overflow-y: auto;
  flex: 1;
}

.conv-item {
  display: grid;
  grid-template-columns: auto 1fr auto;
  gap: .1rem .65rem;
  padding: .65rem .75rem;
  border-bottom: 1px solid var(--border);
  cursor: pointer;
}
.conv-item:hover { background: var(--bg); }
.conv-item.active { background: color-mix(in srgb, var(--accent) 12%, transparent); }

.avatar {
  grid-row: span 2;
  width: 2.3rem;
  height: 2.3rem;
  border-radius: 50%;
  display: grid;
  place-items: center;
  background: var(--bg);
  border: 1px solid var(--border);
  font-size: .82rem;
  font-weight: 600;
  color: var(--muted);
  align-self: center;
}

.conv-name {
  font-weight: 600;
  font-size: .92rem;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.conv-time { font-size: .74rem; color: var(--muted); white-space: nowrap; }
.conv-preview {
  grid-column: 2;
  font-size: .82rem;
  color: var(--muted);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.unread-badge {
  grid-column: 3;
  justify-self: end;
  align-self: center;
  min-width: 1.25rem;
  height: 1.25rem;
  padding: 0 .35rem;
  border-radius: 999px;
  background: var(--accent);
  color: #fff;
  font-size: .72rem;
  display: grid;
  place-items: center;
}

.unread-pill {
  display: inline-grid;
  place-items: center;
  min-width: 1.1rem;
  height: 1.1rem;
  padding: 0 .3rem;
  margin-left: .4rem;
  border-radius: 999px;
  background: var(--accent);
  color: #fff;
  font-size: .7rem;
}

.thread-empty { display: grid; place-items: center; height: 100%; padding: 2rem; text-align: center; }
.thread-inner { display: flex; flex-direction: column; min-height: 0; height: 100%; }

.thread-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
  padding: .75rem 1rem;
  border-bottom: 1px solid var(--border);
}
.thread-title { font-weight: 600; }
.thread-sub { font-size: .8rem; }

.bubbles {
  flex: 1;
  overflow-y: auto;
  padding: 1rem;
  display: flex;
  flex-direction: column;
  gap: .5rem;
}

/* Bei wenigen Nachrichten sollen die Blasen unten stehen, nicht oben kleben. */
.bubbles > *:first-child { margin-top: auto; }

.bubble {
  max-width: min(72%, 46ch);
  padding: .5rem .7rem;
  border-radius: 14px;
  font-size: .9rem;
  overflow-wrap: anywhere;
  white-space: pre-wrap;
}
.bubble.in {
  align-self: flex-start;
  background: var(--bg);
  border: 1px solid var(--border);
  border-bottom-left-radius: 4px;
}
.bubble.out {
  align-self: flex-end;
  background: var(--accent);
  color: #fff;
  border-bottom-right-radius: 4px;
}
.bubble.out.pending { background: color-mix(in srgb, var(--accent) 55%, var(--panel)); }
.bubble.out.broken { background: var(--err); }

.bubble-meta {
  display: block;
  margin-top: .25rem;
  font-size: .7rem;
  opacity: .75;
}
.bubble.in .bubble-meta { color: var(--muted); opacity: 1; }

.day-sep {
  align-self: center;
  font-size: .74rem;
  color: var(--muted);
  background: var(--bg);
  border: 1px solid var(--border);
  padding: .1rem .6rem;
  border-radius: 999px;
  margin: .4rem 0;
}

.composer {
  display: flex;
  gap: .5rem;
  padding: .7rem;
  border-top: 1px solid var(--border);
  align-items: flex-end;
}
.composer textarea { resize: none; max-height: 8rem; }
.composer button { white-space: nowrap; }
.composer-meta { padding: 0 .8rem .5rem; font-size: .76rem; min-height: 1.1rem; }

.contact-row {
  display: flex;
  align-items: center;
  gap: .75rem;
  padding: .6rem .2rem;
  border-bottom: 1px solid var(--border);
}
.contact-row:last-child { border-bottom: none; }
.contact-main { flex: 1; min-width: 0; }
.contact-name { font-weight: 600; font-size: .92rem; }
.contact-numbers { font-size: .82rem; color: var(--muted); overflow-wrap: anywhere; }
.contact-actions { display: flex; gap: .4rem; flex-wrap: wrap; }
.contact-actions button { padding: .3rem .6rem; font-size: .8rem; }

.origin-tag {
  font-size: .7rem;
  color: var(--muted);
  border: 1px solid var(--border);
  border-radius: 999px;
  padding: 0 .45rem;
}

.number-row { display: flex; gap: .4rem; margin-bottom: .4rem; }
.number-row input.num-label { flex: 0 0 7rem; }
.number-row button { flex: 0 0 auto; padding: .4rem .6rem; }

@media (max-width: 780px) {
  #view-messenger { height: auto; }
  .messenger { grid-template-columns: 1fr; }
  .conv-items { max-height: 40vh; }
  .bubbles { max-height: 55vh; }
  .messenger.thread-open .conv-list { display: none; }
}

/* Fortschritt des Kontaktabgleichs */
.sync-box {
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: .8rem;
  margin-bottom: .9rem;
  background: var(--bg);
}
.sync-head {
  display: flex;
  justify-content: space-between;
  gap: 1rem;
  font-size: .88rem;
  margin-bottom: .5rem;
}
.progress {
  height: .5rem;
  border-radius: 999px;
  background: var(--border);
  overflow: hidden;
}
.progress-bar {
  height: 100%;
  width: 0;
  border-radius: 999px;
  background: var(--accent);
  transition: width .25s ease;
}
.progress-bar.done { background: var(--ok); }
.progress-bar.error { background: var(--err); }

/* Unbestimmte Phase: das Telefon liest, eine Gesamtzahl gibt es noch nicht. */
.progress-bar.indeterminate {
  width: 35% !important;
  animation: progress-slide 1.1s ease-in-out infinite;
}
@keyframes progress-slide {
  0%   { margin-left: -35%; }
  100% { margin-left: 100%; }
}
