* { margin: 0; padding: 0; box-sizing: border-box; }
:root {
  --wa-green: #00a884;
  --wa-green-dark: #008069;
  --wa-bubble-out: #d9fdd3;
  --wa-bubble-in: #ffffff;
  --wa-bg: #efeae2;
  --wa-panel: #f0f2f5;
  --wa-text: #111b21;
  --wa-text-2: #667781;
  --wa-blue: #53bdeb;
}
html, body { height: 100%; }
body {
  font-family: "Segoe UI", Helvetica, Arial, sans-serif;
  color: var(--wa-text);
  background: #dadbd3;
}

/* ============ login ============ */
.login-wrap {
  min-height: 100vh; display: flex; align-items: center; justify-content: center;
  background: linear-gradient(180deg, var(--wa-green-dark) 0 220px, #dadbd3 220px);
}
.login-card {
  background: #fff; border-radius: 8px; box-shadow: 0 6px 18px rgba(0,0,0,.25);
  padding: 40px; width: 360px; max-width: 92vw;
}
.login-card h1 { font-size: 20px; margin-bottom: 4px; color: var(--wa-green-dark); display:flex; align-items:center; gap:10px;}
.login-card p { color: var(--wa-text-2); font-size: 13px; margin-bottom: 24px; }
.login-card label { display: block; font-size: 13px; color: var(--wa-text-2); margin: 12px 0 4px; }
.login-card input {
  width: 100%; padding: 10px 12px; border: 1px solid #d1d7db; border-radius: 6px; font-size: 15px; outline: none;
}
.login-card input:focus { border-color: var(--wa-green); }
.login-card button {
  margin-top: 22px; width: 100%; padding: 11px; background: var(--wa-green-dark); border: none;
  border-radius: 24px; color: #fff; font-size: 15px; font-weight: 600; cursor: pointer;
}
.login-card button:hover { background: var(--wa-green); }
.login-error { color: #d32f2f; font-size: 13px; margin-top: 12px; min-height: 18px; }

/* ============ app layout ============ */
.app { display: flex; height: 100vh; height: 100dvh; max-width: 1600px; margin: 0 auto; box-shadow: 0 0 20px rgba(0,0,0,.15); }
.sidebar { width: 380px; min-width: 300px; background: #fff; border-right: 1px solid #e9edef; display: flex; flex-direction: column; }
.sidebar-header {
  background: var(--wa-panel); padding: 10px 16px; height: 60px;
  display: flex; align-items: center; justify-content: space-between;
}
.sidebar-header .me { font-weight: 600; font-size: 15px; display:flex; align-items:center; gap:10px; }
.avatar {
  width: 40px; height: 40px; border-radius: 50%; background: var(--wa-green);
  color: #fff; display: inline-flex; align-items: center; justify-content: center;
  font-size: 18px; font-weight: 600; flex-shrink: 0;
}
.icon-btn {
  border: none; background: none; color: #54656f; cursor: pointer; font-size: 13px;
  padding: 6px 10px; border-radius: 6px;
}
.icon-btn:hover { background: rgba(0,0,0,.06); }
.chat-list { flex: 1; overflow-y: auto; }
.chat-item {
  display: flex; gap: 13px; padding: 10px 14px; cursor: pointer; align-items: center;
  border-bottom: 1px solid #f5f6f6;
}
.chat-item:hover { background: #f5f6f6; }
.chat-item.active { background: #f0f2f5; }
.chat-item .info { flex: 1; min-width: 0; }
.chat-item .row1 { display: flex; justify-content: space-between; align-items: baseline; }
.chat-item .name { font-size: 16px; }
.chat-item .date { font-size: 12px; color: var(--wa-text-2); }
.chat-item .preview {
  font-size: 13.5px; color: var(--wa-text-2); white-space: nowrap;
  overflow: hidden; text-overflow: ellipsis; margin-top: 2px;
}

/* ============ chat pane ============ */
.chat-pane { flex: 1; display: flex; flex-direction: column; min-width: 0; background: var(--wa-bg); }
.chat-header {
  height: 60px; background: var(--wa-panel); display: flex; align-items: center;
  gap: 14px; padding: 0 16px; border-left: 1px solid #d1d7db; flex-shrink: 0;
}
.chat-header .title { font-size: 16px; font-weight: 600; }
.chat-header .sub { font-size: 12.5px; color: var(--wa-text-2); }
.back-btn { display: none; }

.messages {
  flex: 1; overflow-y: auto; padding: 12px 6%;
  background:
    url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='260' height='260' viewBox='0 0 260 260'%3E%3Cg fill='none' stroke='%23d6d0c4' stroke-width='1.2' opacity='.55'%3E%3Ccircle cx='40' cy='50' r='10'/%3E%3Cpath d='M180 30l14 14m0-14l-14 14'/%3E%3Cpath d='M100 120q10-16 20 0'/%3E%3Ccircle cx='220' cy='150' r='7'/%3E%3Cpath d='M30 200l12 12m0-12l-12 12'/%3E%3Cpath d='M150 220q12-14 24 0'/%3E%3C/g%3E%3C/svg%3E"),
    var(--wa-bg);
}
.empty-state {
  flex:1; display:flex; flex-direction:column; align-items:center; justify-content:center;
  color: var(--wa-text-2); background: var(--wa-panel); border-left: 1px solid #d1d7db; gap: 8px;
}
.empty-state .big { font-size: 30px; color: #41525d; font-weight: 300; }

.date-chip, .system-msg {
  align-self: center; margin: 10px auto; width: fit-content; max-width: 80%;
  background: #ffffff; color: #54656f; font-size: 12.5px; padding: 5px 12px;
  border-radius: 8px; box-shadow: 0 1px .5px rgba(11,20,26,.13); text-align: center;
}
.system-msg { background: #ffeecd; }

.msg-row {
  display: flex; margin: 2px 0;
  /* skip layout/paint of offscreen messages — keeps 2000+ msg chats smooth */
  content-visibility: auto;
  contain-intrinsic-size: auto 48px;
}
.msg-row.out { justify-content: flex-end; }
.bubble {
  position: relative; max-width: min(65%, 560px); padding: 6px 9px 8px 9px;
  border-radius: 8px; box-shadow: 0 1px .5px rgba(11,20,26,.13); font-size: 14.2px; line-height: 19px;
}
.msg-row.in .bubble { background: var(--wa-bubble-in); border-top-left-radius: 0; }
.msg-row.out .bubble { background: var(--wa-bubble-out); border-top-right-radius: 0; }
.msg-row.in .bubble::before, .msg-row.out .bubble::before {
  content: ""; position: absolute; top: 0; width: 0; height: 0; border: 6px solid transparent;
}
.msg-row.in .bubble::before { left: -6px; border-top-color: var(--wa-bubble-in); border-right-color: var(--wa-bubble-in); }
.msg-row.out .bubble::before { right: -6px; border-top-color: var(--wa-bubble-out); border-left-color: var(--wa-bubble-out); }

.msg-text { white-space: pre-wrap; word-wrap: break-word; unicode-bidi: plaintext; }
.msg-meta {
  float: left; /* flipped for rtl bubbles via plaintext; we place below instead */
}
.meta {
  display: flex; justify-content: flex-end; gap: 4px; align-items: center;
  font-size: 11px; color: var(--wa-text-2); margin-top: 2px; user-select: none;
}
.meta .edited { font-style: italic; }
.deleted, .placeholder { color: var(--wa-text-2); font-style: italic; display: flex; gap: 6px; align-items: center; }

/* media */
.bubble img.photo {
  max-width: 100%; width: 330px; border-radius: 6px; display: block; cursor: pointer; background:#eee;
  min-height: 60px;
}
.bubble video { max-width: 100%; width: 330px; border-radius: 6px; display: block; background: #000; }
.sticker-row .bubble { background: none !important; box-shadow: none; padding: 0; }
.sticker-row .bubble::before { display: none; }
.sticker-row img { width: 150px; height: 150px; object-fit: contain; display: block; }
.sticker-row .meta { justify-content: flex-end; filter: drop-shadow(0 0 2px #fff); }

.doc-card {
  display: flex; align-items: center; gap: 10px; background: rgba(0,0,0,.04);
  padding: 10px 12px; border-radius: 6px; cursor: pointer; text-decoration: none; color: inherit;
  max-width: 330px;
}
.doc-card .doc-icon { font-size: 26px; }
.doc-card .doc-name { font-size: 13.5px; word-break: break-all; unicode-bidi: plaintext; }
.doc-card .doc-sub { font-size: 11.5px; color: var(--wa-text-2); }

/* voice player */
.bubble:has(.voice) { width: 320px; max-width: min(78%, 560px); }
.voice { display: flex; align-items: center; gap: 10px; width: 100%; padding: 4px 0; }
.voice .play-btn {
  border: none; background: none; cursor: pointer; font-size: 24px; color: #54656f;
  width: 34px; height: 34px; display: flex; align-items: center; justify-content: center; flex-shrink: 0;
}
.voice .mic { color: var(--wa-green); font-size: 18px; flex-shrink: 0; }
.voice .track { flex: 1; display: flex; flex-direction: column; gap: 5px; }
.voice input[type=range] {
  width: 100%; accent-color: var(--wa-green); height: 4px; cursor: pointer;
}
.voice .times { display: flex; justify-content: space-between; font-size: 11px; color: var(--wa-text-2); }
.voice .speed {
  border: none; border-radius: 12px; background: rgba(0,0,0,.08); font-size: 11px; font-weight: 600;
  padding: 3px 8px; cursor: pointer; color: #54656f; display: none;
}
.voice.playing .speed { display: inline-block; }

/* lightbox */
.lightbox {
  position: fixed; inset: 0; background: rgba(17,27,33,.95); display: none;
  align-items: center; justify-content: center; z-index: 100; flex-direction: column;
}
.lightbox.open { display: flex; }
.lightbox img { max-width: 92vw; max-height: 88vh; object-fit: contain; }
.lightbox .close {
  position: absolute; top: 14px; right: 20px; background: none; border: none;
  color: #fff; font-size: 30px; cursor: pointer;
}

/* scroll to top/bottom */
.jump-btns { position: absolute; right: 22px; bottom: 22px; display: flex; flex-direction: column; gap: 8px; }
.jump-btns button {
  width: 40px; height: 40px; border-radius: 50%; border: none; background: #fff;
  box-shadow: 0 2px 6px rgba(0,0,0,.25); cursor: pointer; font-size: 16px; color: #54656f;
}
.chat-pane { position: relative; }

/* ============ admin ============ */
.admin-wrap { max-width: 980px; margin: 0 auto; padding: 24px 16px; }
.admin-topbar {
  background: var(--wa-green-dark); color: #fff; padding: 14px 20px; border-radius: 10px;
  display: flex; justify-content: space-between; align-items: center; margin-bottom: 24px;
}
.admin-topbar h1 { font-size: 18px; }
.admin-topbar a, .admin-topbar button {
  color: #fff; background: rgba(255,255,255,.15); border: none; padding: 8px 14px;
  border-radius: 18px; font-size: 13px; cursor: pointer; text-decoration: none; margin-left: 8px;
}
.card { background: #fff; border-radius: 10px; padding: 20px; box-shadow: 0 1px 4px rgba(0,0,0,.08); margin-bottom: 20px; }
.card h2 { font-size: 16px; margin-bottom: 14px; color: var(--wa-green-dark); }
.form-grid { display: flex; gap: 12px; flex-wrap: wrap; align-items: flex-end; }
.form-grid label { display: block; font-size: 12px; color: var(--wa-text-2); margin-bottom: 4px; }
.form-grid input[type=text], .form-grid input[type=password] {
  padding: 9px 12px; border: 1px solid #d1d7db; border-radius: 6px; font-size: 14px; width: 200px;
}
.chat-checks { display: flex; gap: 14px; flex-wrap: wrap; padding: 6px 0; }
.chat-checks label { display: flex; align-items: center; gap: 6px; font-size: 13.5px; cursor: pointer; margin: 0; color: var(--wa-text); }
.btn-primary {
  background: var(--wa-green-dark); color: #fff; border: none; padding: 10px 22px;
  border-radius: 20px; font-size: 14px; font-weight: 600; cursor: pointer;
}
.btn-primary:hover { background: var(--wa-green); }
.btn-small { background: #f0f2f5; border: 1px solid #d1d7db; padding: 5px 12px; border-radius: 14px; font-size: 12.5px; cursor: pointer; margin-right: 6px; }
.btn-danger { color: #d32f2f; border-color: #f0c0c0; }
table.users { width: 100%; border-collapse: collapse; }
table.users th { text-align: left; font-size: 12px; color: var(--wa-text-2); padding: 8px 10px; border-bottom: 1px solid #e9edef; }
table.users td { padding: 10px; border-bottom: 1px solid #f5f6f6; font-size: 14px; vertical-align: middle; }
.tag { background: #e7f8f2; color: var(--wa-green-dark); font-size: 11.5px; padding: 3px 9px; border-radius: 10px; margin-right: 4px; display: inline-block; margin-bottom: 3px;}
.tag.role { background: #fff3cd; color: #8a6d1a; }
.msg-note { font-size: 13px; margin-top: 10px; min-height: 16px; }
.msg-note.err { color: #d32f2f; }
.msg-note.ok { color: var(--wa-green-dark); }

/* admin on small screens: table becomes stacked cards */
@media (max-width: 760px) {
  .admin-wrap { padding: 14px 10px; }
  .admin-topbar { flex-wrap: wrap; gap: 10px; padding: 12px 14px; }
  .admin-topbar h1 { font-size: 16px; }
  .card { padding: 14px; }
  .form-grid { flex-direction: column; align-items: stretch; }
  .form-grid input[type=text], .form-grid input[type=password] { width: 100%; }
  .chat-checks { flex-direction: column; gap: 8px; align-items: flex-start; }
  table.users, table.users tbody, table.users tr, table.users td { display: block; width: 100%; }
  table.users thead { display: none; }
  table.users tr {
    border: 1px solid #e9edef; border-radius: 10px; padding: 12px; margin-bottom: 14px;
    box-shadow: 0 1px 3px rgba(0,0,0,.05);
  }
  table.users td { border: none; padding: 6px 0; }
  table.users td::before {
    content: attr(data-label); display: block; font-size: 11px; color: var(--wa-text-2);
    text-transform: uppercase; letter-spacing: .4px; margin-bottom: 4px;
  }
  table.users td:first-child::before { display: none; }
  table.users td:first-child { font-size: 16px; }
  .actions-cell { display: flex !important; flex-wrap: wrap; gap: 8px; }
  .actions-cell::before { width: 100%; }
  .btn-small { margin: 0; padding: 8px 14px; font-size: 13px; }
}

@media (max-width: 760px) {
  .app { flex-direction: column; box-shadow: none; }
  .sidebar { width: 100%; min-width: 0; height: 100%; border-right: none; }
  .app.chat-open .sidebar { display: none; }
  .app:not(.chat-open) .chat-pane { display: none !important; }
  .app:not(.chat-open) .empty-state { display: none !important; }
  .back-btn { display: inline-flex; font-size: 20px; padding: 4px 8px; }
  .chat-header { gap: 8px; padding: 0 10px; border-left: none; }
  .messages { padding: 10px 3%; }
  .bubble { max-width: 85%; }
  .bubble:has(.voice) { width: 300px; max-width: 88%; }
  .bubble img.photo, .bubble video { width: min(280px, 68vw); }
  .jump-btns { right: 12px; bottom: 12px; }
  .login-card { padding: 28px 22px; }
}
