/* ---------- token ---------- */
:root {
  --bg: #f5f7fa;
  --bg-soft: #ffffff;
  --panel: #ffffff;
  --panel-2: #eef1f5;
  --border: #d9dee5;
  --border-soft: #e9edf2;
  --text: #1b2733;
  --muted: #667283;
  --accent: #2563eb;
  --accent-ink: #ffffff;
  --danger: #c0392f;
  --success: #067647;
  --warn: #b54708;
  --radius: 10px;
  --radius-sm: 7px;
  color-scheme: light;
}

* { box-sizing: border-box; }
[x-cloak] { display: none !important; }

html, body { height: 100%; }

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

h1, h2, h3 { margin: 0; font-weight: 650; line-height: 1.25; }
h1 { font-size: 1.4rem; }
h2 { font-size: 1.05rem; }
h3 { font-size: .9rem; }
p { margin: 0; }
code { font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: .85em; }

.muted { color: var(--muted); }
.small { font-size: .8125rem; }
.error { color: var(--danger); }
.stack { display: grid; gap: .75rem; align-content: start; }
.row { display: flex; align-items: center; gap: .5rem; }
.spacer { flex: 1; }
.truncate { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

/* ---------- kontrol ---------- */
input, select, textarea, button {
  font: inherit;
  color: inherit;
  border-radius: var(--radius-sm);
  border: 1px solid var(--border);
  padding: .5rem .7rem;
  background: var(--bg-soft);
}

input:focus-visible, select:focus-visible, textarea:focus-visible, button:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 1px;
}

button { cursor: pointer; background: var(--panel-2); }
button:hover:not(:disabled) { border-color: #b6bfcb; }
button:disabled { opacity: .5; cursor: not-allowed; }

button.primary { background: var(--accent); border-color: transparent; color: var(--accent-ink); font-weight: 600; }
button.ghost { background: transparent; }
button.danger { background: transparent; color: var(--danger); }

label { display: grid; gap: .3rem; font-size: .8125rem; color: var(--muted); }

.card {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1rem;
  box-shadow: 0 1px 2px rgb(16 24 40 / 6%);
}

.badge {
  display: inline-flex;
  align-items: center;
  gap: .35rem;
  font-size: .7rem;
  font-weight: 600;
  letter-spacing: .02em;
  padding: .15rem .45rem;
  border-radius: 999px;
  border: 1px solid var(--border);
  color: var(--muted);
  white-space: nowrap;
}
.badge .dot { width: .45rem; height: .45rem; border-radius: 50%; background: currentColor; }
.badge.ok { color: var(--success); border-color: color-mix(in srgb, var(--success) 40%, transparent); }
.badge.warn { color: var(--warn); border-color: color-mix(in srgb, var(--warn) 40%, transparent); }
.badge.bad { color: var(--danger); border-color: color-mix(in srgb, var(--danger) 40%, transparent); }

/* ---------- login ---------- */
.login-wrap { min-height: 100dvh; display: grid; place-items: center; padding: 1.5rem; }
.login-card { width: min(23rem, 100%); }
.login-card form { display: grid; gap: .8rem; margin-top: 1rem; }
.login-hint { margin-top: 1rem; border-top: 1px solid var(--border-soft); padding-top: .8rem; }
.login-hint ul { margin: .4rem 0 0; padding-left: 1.1rem; }

/* ---------- shell aplikasi ---------- */
.app { min-height: 100dvh; display: flex; flex-direction: column; }

/*
 * Halaman conversations mengunci tinggi shell ke viewport supaya yang bergulir
 * adalah panel di dalamnya (daftar chat dan area pesan), bukan body. Halaman
 * lain tetap memakai `.app` biasa dan bergulir seperti halaman umumnya.
 */
.app-fixed { height: 100dvh; min-height: 0; overflow: hidden; }

.topbar {
  display: flex;
  align-items: center;
  gap: 1rem;
  padding: .7rem 1rem;
  border-bottom: 1px solid var(--border);
  background: var(--panel);
}
.topbar .brand { font-weight: 700; letter-spacing: -.01em; }
.topbar nav { display: flex; gap: .25rem; }
.topbar nav a {
  color: var(--muted);
  text-decoration: none;
  padding: .35rem .6rem;
  border-radius: var(--radius-sm);
  font-size: .875rem;
}
.topbar nav a:hover { background: var(--panel-2); color: var(--text); }
.topbar nav a[aria-current="page"] { background: var(--panel-2); color: var(--text); }

/*
 * `.app-fixed` memotong luapan, jadi topbar tidak boleh melebar melewati layar —
 * kalau tidak, tombol Keluar jadi tak terjangkau di layar sempit. Nama user
 * disembunyikan lebih dulu karena perannya sudah terwakili badge di sebelahnya.
 */
@media (max-width: 34rem) {
  .topbar { gap: .4rem; padding: .6rem .7rem; }
  .topbar nav a { padding: .3rem .45rem; }
  .topbar > .small.muted { display: none; }
  /* Sisakan titik warnanya saja; teks status tidak muat dan malah tergencet. */
  .topbar .badge { flex: none; padding: .25rem; }
  .topbar .badge > span:not(.dot) { display: none; }
}

.page { padding: 1.25rem; max-width: 60rem; width: 100%; margin: 0 auto; display: grid; gap: 1rem; }

/* ---------- dashboard / WA ---------- */
.wa-grid { display: grid; gap: 1rem; grid-template-columns: minmax(0, 1fr); }
@media (min-width: 52rem) { .wa-grid { grid-template-columns: 20rem minmax(0, 1fr); } }

.qr-box {
  display: grid;
  place-items: center;
  gap: .75rem;
  min-height: 17rem;
  border: 1px dashed var(--border);
  border-radius: var(--radius);
  padding: 1rem;
  text-align: center;
}
.qr-box img { width: 15rem; height: 15rem; border-radius: 6px; background: #fff; padding: .4rem; }

.spinner {
  width: 1.6rem; height: 1.6rem;
  border: 2px solid var(--border);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin .8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

dl.kv { display: grid; grid-template-columns: auto 1fr; gap: .35rem .75rem; margin: 0; font-size: .875rem; }
dl.kv dt { color: var(--muted); }
dl.kv dd { margin: 0; }

/* ---------- conversations (tiga kolom ala WhatsApp Web) ---------- */
.chat-shell {
  flex: 1;
  min-height: 0;
  display: grid;
  grid-template-columns: 13.5rem 20rem minmax(0, 1fr);
}
@media (max-width: 60rem) {
  .chat-shell { grid-template-columns: 11rem minmax(0, 1fr); }
  .chat-shell .thread { display: none; }
  .chat-shell.has-active .list { display: none; }
  .chat-shell.has-active .thread { display: flex; }
}

.chat-shell > * { min-width: 0; min-height: 0; }

/* kolom 1: pembagian tugas */
.scopes {
  border-right: 1px solid var(--border);
  background: var(--panel);
  overflow-y: auto;
  padding: .6rem;
  display: grid;
  gap: .15rem;
  align-content: start;
}
.scopes .group-label {
  font-size: .68rem;
  text-transform: uppercase;
  letter-spacing: .06em;
  color: var(--muted);
  padding: .7rem .5rem .3rem;
}
.scope-btn {
  display: flex;
  align-items: center;
  gap: .5rem;
  width: 100%;
  text-align: left;
  background: transparent;
  border: 1px solid transparent;
  padding: .4rem .5rem;
  border-radius: var(--radius-sm);
  font-size: .875rem;
  color: var(--muted);
}
.scope-btn:hover { background: var(--panel-2); color: var(--text); }
.scope-btn.active { background: var(--panel-2); color: var(--text); border-color: var(--border); }
.scope-btn .swatch { width: .55rem; height: .55rem; border-radius: 2px; flex: none; }

/* kolom 2: daftar percakapan */
.list {
  border-right: 1px solid var(--border);
  background: var(--bg-soft);
  box-shadow: none;
  display: flex;
  flex-direction: column;
}
.list-head { padding: .6rem; border-bottom: 1px solid var(--border); }
.list-head input { width: 100%; }
.list-scroll { overflow-y: auto; flex: 1; min-height: 0; }

.conv {
  display: grid;
  grid-template-columns: 1fr auto;
  gap: .15rem .5rem;
  width: 100%;
  text-align: left;
  background: transparent;
  border: none;
  border-bottom: 1px solid var(--border-soft);
  border-radius: 0;
  padding: .6rem .75rem;
}
.conv:hover { background: var(--panel-2); }
.conv.active { background: var(--panel-2); }
.conv .name { font-weight: 600; font-size: .875rem; }
.conv .time { font-size: .7rem; color: var(--muted); }
.conv .preview { grid-column: 1 / -1; font-size: .8rem; color: var(--muted); }
.conv .tags { grid-column: 1 / -1; display: flex; gap: .3rem; flex-wrap: wrap; margin-top: .2rem; }

.tag {
  font-size: .65rem;
  padding: .1rem .35rem;
  border-radius: 4px;
  border: 1px solid var(--border);
  color: var(--muted);
}
.tag.team { border-color: transparent; }
.tag.unassigned { color: var(--warn); border-color: color-mix(in srgb, var(--warn) 35%, transparent); }
.tag.session-on { color: var(--success); border-color: color-mix(in srgb, var(--success) 35%, transparent); }

.pill-count {
  background: var(--accent);
  color: var(--accent-ink);
  font-size: .68rem;
  font-weight: 700;
  border-radius: 999px;
  padding: 0 .35rem;
  min-width: 1.15rem;
  text-align: center;
}

/* kolom 3: thread */
.thread { display: flex; flex-direction: column; background: var(--bg); }
.thread-head {
  display: flex;
  align-items: center;
  gap: .6rem;
  flex-wrap: wrap;
  padding: .6rem .9rem;
  border-bottom: 1px solid var(--border);
  background: var(--panel);
}
.thread-head select { padding: .3rem .4rem; font-size: .8rem; }
.close-thread { font-size: 1.1rem; line-height: 1; padding: .2rem .5rem; color: var(--muted); }
.close-thread:hover { color: var(--text); }

.bubbles {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  padding: 1rem;
  display: flex;
  flex-direction: column;
  gap: .4rem;
}
.bubble {
  max-width: min(32rem, 78%);
  padding: .45rem .7rem;
  border-radius: 10px;
  background: var(--panel-2);
  border: 1px solid var(--border);
  font-size: .875rem;
}
/* `pre-wrap` hanya untuk teks pesan. Kalau dipasang di `.bubble`, indentasi
   HTML di antara tag ikut dirender sebagai spasi di awal baris. */
.bubble .text { white-space: pre-wrap; word-break: break-word; }
.day-sep {
  align-self: center;
  padding: .15rem 0;
}
.day-sep span {
  display: inline-block;
  font-size: .7rem;
  color: var(--muted);
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 999px;
  padding: .15rem .6rem;
}

.bubble.me { align-self: flex-end; background: #dceafd; border-color: #b9d3fb; }
.bubble .meta { display: block; margin-top: .2rem; font-size: .65rem; color: var(--muted); text-align: right; }

.composer {
  display: flex;
  align-items: flex-end;
  gap: .5rem;
  padding: .7rem;
  border-top: 1px solid var(--border);
  background: var(--panel);
}

.composer textarea {
  flex: 1;
  resize: none;
  /* Tinggi diatur JS mengikuti isi; max-height yang membatasi lalu menggulir. */
  max-height: 9rem;
  overflow-y: auto;
  line-height: 1.45;
  font-family: inherit;
}

.composer button { flex: none; }

.empty { flex: 1; display: grid; place-items: center; padding: 2rem; text-align: center; color: var(--muted); }

.list-loading {
  display: flex;
  align-items: center;
  gap: .5rem;
  padding: .9rem;
}
.list-loading .spinner { width: 1rem; height: 1rem; }

.confirm {
  display: grid;
  gap: .5rem;
  padding: .7rem;
  border: 1px solid color-mix(in srgb, var(--danger) 35%, transparent);
  background: color-mix(in srgb, var(--danger) 6%, transparent);
  border-radius: var(--radius-sm);
}
