:root {
  --wa-green: #008069;
  --wa-green-d: #00543f;
  --wa-teal: #25d366;
  --wa-panel: #f0f2f5;
  --wa-chat-bg: #efeae2;
  --wa-out: #d9fdd3;
  --wa-in: #ffffff;
  --wa-deleted: #fff5d6;
  --wa-text: #111b21;
  --wa-sub: #667781;
  --wa-line: #e9edef;
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; height: 100%; }
body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  background: var(--wa-panel); color: var(--wa-text); overflow: hidden;
  -webkit-font-smoothing: antialiased;
}
.hidden { display: none !important; }
button { font-family: inherit; }

#app { display: flex; flex-direction: column; height: 100vh; height: 100dvh; }

/* Top bar */
#topbar {
  background: var(--wa-green); color: #fff;
  padding: calc(env(safe-area-inset-top) + 9px) 12px 9px;
  display: flex; align-items: center; gap: 10px;
  box-shadow: 0 1px 4px rgba(0,0,0,.18); z-index: 5; min-height: 56px;
}
.icon-btn { background: none; border: none; color: #fff; font-size: 26px; line-height: 1; padding: 4px 6px; cursor: pointer; border-radius: 50%; }
.icon-btn:active { background: rgba(255,255,255,.15); }
#backBtn { font-size: 30px; padding-left: 0; }
.top-text { flex: 1; min-width: 0; }
#topTitle { font-size: 17px; font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.topsub { font-size: 12px; opacity: .9; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.top-avatar { width: 38px; height: 38px; flex: 0 0 auto; }

#screens { flex: 1; overflow: hidden; position: relative; }
.screen { position: absolute; inset: 0; overflow-y: auto; -webkit-overflow-scrolling: touch; }

/* Avatar (initials + lazy image overlay) */
.avatar {
  position: relative; border-radius: 50%; flex: 0 0 auto; overflow: hidden;
  width: 49px; height: 49px; display: flex; align-items: center; justify-content: center;
  color: #fff; font-weight: 600; font-size: 19px; background: #6e7b8a; user-select: none;
}
.avatar img { position: absolute; inset: 0; width: 100%; height: 100%; object-fit: cover; opacity: 0; transition: opacity .25s; }
.avatar img.on { opacity: 1; }
.avatar.sm { width: 38px; height: 38px; font-size: 15px; }

/* Accounts */
.accounts-wrap { padding: 10px 10px 24px; }
.section-label { color: var(--wa-sub); font-size: 12px; font-weight: 600; text-transform: uppercase; letter-spacing: .04em; margin: 6px 6px 8px; }
.account {
  background: #fff; border-radius: 14px; padding: 12px 14px; margin-bottom: 10px;
  display: flex; align-items: center; gap: 13px; box-shadow: 0 1px 2px rgba(0,0,0,.07);
}
.account .tap { display: flex; align-items: center; gap: 13px; flex: 1; min-width: 0; cursor: pointer; }
.account .meta { flex: 1; min-width: 0; }
.account .meta .name { font-weight: 600; font-size: 16.5px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.account .meta .sub { font-size: 13px; color: var(--wa-sub); margin-top: 2px; display: flex; align-items: center; gap: 6px; }
.dot { width: 8px; height: 8px; border-radius: 50%; display: inline-block; flex: 0 0 auto; }
.dot.on { background: var(--wa-teal); } .dot.wait { background: #f0a500; } .dot.off { background: #c4ccd2; }
.del-btn { background: none; border: none; color: #c4ccd2; font-size: 19px; padding: 8px; cursor: pointer; border-radius: 50%; flex: 0 0 auto; }
.del-btn:active { background: #f5f6f7; color: #b42318; }

.add-account {
  display: flex; align-items: center; justify-content: center; gap: 9px;
  background: var(--wa-green); color: #fff; border: none; border-radius: 14px;
  padding: 15px; width: 100%; font-size: 16px; font-weight: 600; cursor: pointer;
  box-shadow: 0 2px 8px rgba(0,128,105,.3); margin-top: 4px;
}
.add-account:active { background: var(--wa-green-d); }
.add-ic { font-size: 22px; line-height: 1; }

/* Chats */
.search-wrap { padding: 8px 10px; background: #fff; border-bottom: 1px solid var(--wa-line); position: sticky; top: 0; z-index: 2; }
.search-wrap input {
  width: 100%; border: none; background: var(--wa-panel); border-radius: 10px;
  padding: 9px 14px; font-size: 15px; outline: none; color: var(--wa-text);
}
.chat-row { display: flex; align-items: center; gap: 13px; padding: 10px 14px; background: #fff; cursor: pointer; }
.chat-row:active { background: #f5f6f7; }
.chat-row + .chat-row { border-top: 1px solid var(--wa-line); }
.chat-row .meta { flex: 1; min-width: 0; }
.chat-row .top { display: flex; align-items: baseline; gap: 8px; }
.chat-row .name { font-weight: 600; font-size: 16.5px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; flex: 1; }
.chat-row .time { font-size: 12px; color: var(--wa-sub); flex: 0 0 auto; }
.chat-row .preview { font-size: 14px; color: var(--wa-sub); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; margin-top: 2px; }
.chat-row .preview.del { color: #b42318; font-style: italic; }
.badge-group { font-size: 10px; color: var(--wa-green); border: 1px solid var(--wa-green); border-radius: 4px; padding: 0 4px; margin-left: 4px; vertical-align: middle; }

/* Feed */
#screen-feed { background-color: var(--wa-chat-bg); background-image: radial-gradient(rgba(0,0,0,.025) 1px, transparent 1px); background-size: 22px 22px; }
.feed-wrap { padding: 10px 8px 18px; display: flex; flex-direction: column; gap: 3px; }
.daysep { align-self: center; background: #fff; color: var(--wa-sub); font-size: 12px; padding: 4px 12px; border-radius: 9px; margin: 8px 0; box-shadow: 0 1px 1px rgba(0,0,0,.06); }
.bubble {
  max-width: 84%; padding: 6px 9px 5px; border-radius: 9px; position: relative;
  background: var(--wa-in); box-shadow: 0 1px .5px rgba(0,0,0,.13); font-size: 14.6px;
  line-height: 1.35; word-wrap: break-word; white-space: pre-wrap; overflow-wrap: anywhere;
}
.bubble.out { align-self: flex-end; background: var(--wa-out); border-top-right-radius: 2px; }
.bubble.in  { align-self: flex-start; border-top-left-radius: 2px; }
.bubble .author { font-size: 12.8px; font-weight: 600; margin-bottom: 1px; }
.bubble .reply { border-left: 3px solid var(--wa-green); background: rgba(0,0,0,.04); padding: 3px 7px; border-radius: 4px; font-size: 12.6px; color: var(--wa-sub); margin-bottom: 3px; }
.bubble .meta-line { font-size: 11px; color: var(--wa-sub); text-align: right; margin-top: 2px; user-select: none; }
.tag-edited { color: #8a6d00; cursor: pointer; font-style: italic; }
.bubble.deleted { background: var(--wa-deleted); }
.deleted-head { color: #8a6d00; font-style: italic; display: flex; align-items: center; gap: 5px; }
.archived { margin-top: 5px; padding: 6px 8px; border-radius: 6px; background: rgba(0,0,0,.05); border-left: 3px solid #b42318; font-size: 13.6px; color: #3a3a3a; }
.archived .lbl { font-size: 11px; font-weight: 600; color: #b42318; display: block; margin-bottom: 2px; }
.reactions { display: flex; gap: 4px; margin-top: 3px; flex-wrap: wrap; }
.react-pill { background: #fff; border: 1px solid var(--wa-line); border-radius: 12px; padding: 1px 7px; font-size: 13px; box-shadow: 0 1px 1px rgba(0,0,0,.05); }
.bubble.out .react-pill { background: #cdeec0; }
.media img { max-width: 100%; border-radius: 6px; display: block; }
.media-file { display: flex; align-items: center; gap: 8px; padding: 8px 10px; background: rgba(0,0,0,.05); border-radius: 6px; font-size: 13px; color: var(--wa-text); text-decoration: none; }
.media-missing { color: var(--wa-sub); font-style: italic; font-size: 13px; }

/* Modals */
.modal { position: fixed; inset: 0; background: rgba(0,0,0,.55); display: flex; align-items: center; justify-content: center; z-index: 50; padding: 18px; }
.modal-card { background: #fff; border-radius: 16px; padding: 22px; width: 100%; max-width: 360px; text-align: center; }
.modal-card h3 { margin: 0 0 14px; }
.qr-steps { text-align: left; font-size: 13.5px; color: #333; padding-left: 20px; margin: 0 0 14px; line-height: 1.65; }
.qr-box { width: 236px; height: 236px; margin: 0 auto 12px; display: flex; align-items: center; justify-content: center; background: #fafafa; border-radius: 12px; }
.qr-box img { width: 236px; height: 236px; border-radius: 8px; }
.qr-status { font-size: 13px; color: var(--wa-sub); margin-bottom: 16px; min-height: 18px; }
.hist-body { text-align: left; max-height: 50vh; overflow-y: auto; margin-bottom: 14px; }
.hist-item { padding: 9px 0; border-bottom: 1px solid var(--wa-line); font-size: 14px; }
.hist-item .when { font-size: 11px; color: var(--wa-sub); margin-bottom: 2px; }
.hist-item .old { color: #b42318; text-decoration: line-through; }
.hist-item .new { color: #027a48; }

.btn { border: none; border-radius: 11px; padding: 12px 16px; font-size: 15px; font-weight: 600; cursor: pointer; width: 100%; }
.btn-secondary { background: var(--wa-panel); color: #333; }

/* States */
.spinner { width: 30px; height: 30px; border: 3px solid #e0e0e0; border-top-color: var(--wa-green); border-radius: 50%; animation: spin .9s linear infinite; }
.center-spin { display: flex; justify-content: center; padding: 48px 0; }
@keyframes spin { to { transform: rotate(360deg); } }
.empty { text-align: center; color: var(--wa-sub); padding: 54px 28px; font-size: 14.5px; line-height: 1.6; }
.empty .big { font-size: 46px; margin-bottom: 12px; }
.empty .ttl { color: var(--wa-text); font-weight: 600; font-size: 16px; margin-bottom: 6px; }
.toast { position: fixed; bottom: 26px; left: 50%; transform: translateX(-50%); background: #2a2f32; color: #fff; padding: 11px 18px; border-radius: 22px; font-size: 14px; z-index: 60; max-width: 88%; box-shadow: 0 4px 14px rgba(0,0,0,.3); }
