:root {
  --bg: #0b1120;
  --panel: #111827;
  --panel-2: #1f2937;
  --line: #273244;
  --text: #e5e7eb;
  --muted: #94a3b8;
  --accent: #22c55e;
  --accent-ink: #052e16;
  --out: #14532d;
  --in: #1f2937;
  --danger: #ef4444;
  --warn: #f59e0b;
  --radius: 12px;
  color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  font: 15px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
  -webkit-font-smoothing: antialiased;
}
[hidden] { display: none !important; }
button, input, textarea { font: inherit; color: inherit; }
h1, h2, h3 { margin: 0 0 .5rem; line-height: 1.2; }
h3 { font-size: .95rem; margin-top: 1.25rem; }
.muted { color: var(--muted); }
.small { font-size: .82rem; }

input, textarea {
  width: 100%;
  background: var(--bg);
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: .6rem .75rem;
  outline: none;
}
input:focus, textarea:focus { border-color: var(--accent); }

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: .4rem;
  border: 1px solid var(--line);
  background: var(--panel-2);
  border-radius: 10px;
  padding: .55rem .9rem;
  cursor: pointer;
  white-space: nowrap;
}
.btn:hover { border-color: #3b4a61; }
.btn:disabled { opacity: .5; cursor: not-allowed; }
.btn.primary { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); font-weight: 600; }
.btn.danger { border-color: var(--danger); color: #fecaca; background: transparent; }
.btn.small { padding: .3rem .6rem; font-size: .85rem; }
.btn.wide { width: 100%; }
.icon-btn {
  background: none; border: 0; cursor: pointer; font-size: 1.15rem;
  width: 2.2rem; height: 2.2rem; border-radius: 8px; color: var(--muted);
}
.icon-btn:hover { background: var(--panel-2); color: var(--text); }

/* ---------- Setup ---------- */
.setup { min-height: 100%; display: grid; place-items: center; padding: 1.5rem; }
.setup-card {
  width: min(400px, 100%);
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 18px;
  padding: 2rem 1.75rem;
  text-align: center;
}
.setup-logo { width: 64px; height: 64px; margin-bottom: .75rem; }
.setup-card p { margin: 0 0 1.5rem; }
.divider { display: flex; align-items: center; gap: .75rem; color: var(--muted); margin: 1rem 0; font-size: .85rem; }
.divider::before, .divider::after { content: ""; flex: 1; height: 1px; background: var(--line); }
.status-line { margin-top: 1.25rem !important; font-size: .85rem; }

/* ---------- Layout ---------- */
.app { display: grid; grid-template-columns: 320px 1fr; height: 100dvh; }
.sidebar {
  background: var(--panel);
  border-right: 1px solid var(--line);
  display: flex; flex-direction: column; min-height: 0;
}
.me {
  display: flex; align-items: center; justify-content: space-between;
  padding: 1rem; border-bottom: 1px solid var(--line);
}
.me .label { font-size: .75rem; color: var(--muted); text-transform: uppercase; letter-spacing: .06em; display: flex; align-items: center; gap: .4rem; }
.my-id {
  background: none; border: 0; padding: 0; cursor: pointer;
  font: 600 1.35rem/1.2 ui-monospace, "Cascadia Mono", Consolas, monospace; letter-spacing: .08em;
}
.my-id:hover { color: var(--accent); }
.dot { width: 8px; height: 8px; border-radius: 50%; display: inline-block; background: var(--danger); }
.dot.on { background: var(--accent); }
.dot.wait { background: var(--warn); }

.add-form { display: grid; gap: .5rem; padding: 1rem; border-bottom: 1px solid var(--line); }
#add-id { font-family: ui-monospace, Consolas, monospace; text-transform: uppercase; letter-spacing: .06em; }

.contacts { list-style: none; margin: 0; padding: .4rem; overflow-y: auto; flex: 1; }
.contact { padding: .65rem .75rem; border-radius: 10px; cursor: pointer; }
.contact:hover { background: var(--panel-2); }
.contact.active { background: #1e3a2b; }
.contact .row1, .contact .row2 { display: flex; justify-content: space-between; gap: .5rem; align-items: center; }
.cname { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.badge-ok { color: var(--accent); }
.ctime { font-size: .75rem; flex-shrink: 0; }
.cpreview { font-size: .85rem; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.unread {
  background: var(--accent); color: var(--accent-ink); font-size: .72rem; font-weight: 700;
  border-radius: 999px; padding: 0 .45rem; min-width: 1.3rem; text-align: center;
}
.empty-hint { padding: 1rem; font-size: .9rem; }

/* ---------- Chat ---------- */
.chat { display: flex; flex-direction: column; min-width: 0; min-height: 0; }
.chat-empty { margin: auto; }
.chat-view { display: flex; flex-direction: column; flex: 1; min-height: 0; }
.chat-head {
  display: flex; align-items: center; gap: .5rem;
  padding: .75rem 1rem; border-bottom: 1px solid var(--line); background: var(--panel);
}
.chat-title { flex: 1; min-width: 0; }
.chat-title .name { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.chat-title .sub { font-size: .8rem; }
.back { display: none; }

.banner { padding: .6rem 1rem; background: #1e293b; border-bottom: 1px solid var(--line); font-size: .9rem; display: flex; gap: .75rem; align-items: center; justify-content: space-between; }
.banner.danger { background: #450a0a; color: #fecaca; }

.messages {
  list-style: none; margin: 0; padding: 1rem;
  flex: 1; overflow-y: auto; display: flex; flex-direction: column; gap: .35rem;
}
.msg { max-width: min(70%, 560px); padding: .45rem .7rem .3rem; border-radius: var(--radius); }
.msg.in { align-self: flex-start; background: var(--in); border-bottom-left-radius: 4px; }
.msg.out { align-self: flex-end; background: var(--out); border-bottom-right-radius: 4px; }
.msg.error { border: 1px solid var(--danger); }
.msg .body { white-space: pre-wrap; overflow-wrap: anywhere; }
.msg .meta { font-size: .7rem; color: var(--muted); text-align: right; margin-top: .1rem; }
.msg .st.delivered { color: #86efac; }
.msg .st.failed { color: var(--danger); }

.composer {
  display: flex; gap: .5rem; align-items: flex-end;
  padding: .75rem 1rem calc(.75rem + env(safe-area-inset-bottom));
  border-top: 1px solid var(--line); background: var(--panel);
}
.composer textarea { resize: none; max-height: 160px; }
.composer.locked { opacity: .4; pointer-events: none; }

/* ---------- Dialoge ---------- */
.dialog {
  width: min(460px, calc(100% - 2rem));
  background: var(--panel); color: var(--text);
  border: 1px solid var(--line); border-radius: 16px; padding: 1.5rem;
}
.dialog::backdrop { background: rgb(0 0 0 / .6); }
.dialog p { margin: 0 0 1rem; }
.dialog input { margin-bottom: .5rem; }
.dialog-actions { display: flex; gap: .5rem; justify-content: flex-end; margin-top: 1.25rem; }
.row { display: flex; gap: .5rem; }
.row input { margin: 0; }
.safety {
  display: grid; grid-template-columns: repeat(4, 1fr); gap: .5rem .75rem;
  font: 600 1.15rem ui-monospace, Consolas, monospace; text-align: center;
  background: var(--bg); border-radius: 12px; padding: 1rem;
}

.toast {
  position: fixed; left: 50%; bottom: 1.5rem; transform: translateX(-50%);
  background: #0f172a; border: 1px solid var(--line); padding: .65rem 1rem; border-radius: 10px;
  box-shadow: 0 8px 30px rgb(0 0 0 / .4); max-width: calc(100% - 2rem); z-index: 10;
}

/* ---------- Mobil ---------- */
@media (max-width: 759px) {
  .app { grid-template-columns: 1fr; }
  .app.show-chat .sidebar { display: none; }
  .app:not(.show-chat) .chat { display: none; }
  .back { display: inline-block; }
  .msg { max-width: 85%; }
  #btn-safety { padding: .3rem .45rem; font-size: .75rem; }
}
#add-id::placeholder { text-transform: none; letter-spacing: normal; font-family: system-ui, sans-serif; }
