* { box-sizing: border-box; font-family: system-ui, Arial; }

html, body { height: 100%; }
body { margin: 0; background: #f6f7fb; overflow: hidden; } /* prevent page scroll */

.app {
  display: grid;
  grid-template-columns: 450px 1fr;
  height: 100dvh;   /* modern */
  /*height: 100vh;*/    /* fallback */
  overflow: hidden; /* IMPORTANT */
}

/* Sidebar */
.sidebar {
  background: #fff;
  border-right: 1px solid #e5e7eb;
  display: flex;
  flex-direction: column;
  min-height: 0;     /* IMPORTANT */
}

.header {
  padding: 12px 16px;
  font-weight: 700;
  border-bottom: 1px solid #e5e7eb;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
}

.tabs{ display:flex; gap: 6px; }
.tab{
  padding: 8px 10px;
  border: 1px solid #e5e7eb;
  background: #fff;
  border-radius: 10px;
  cursor: pointer;
  font-weight: 700;
}
.tab.active{ background: #111827; color: #fff; border-color: #111827; }
.actions{ display:flex; gap: 6px; }

.search {
  margin: 12px 16px;
  padding: 10px;
  border: 1px solid #e5e7eb;
  border-radius: 10px;
  flex: 0 0 auto;
}

.list {
  flex: 1 1 auto;
  min-height: 0;     /* IMPORTANT */
  overflow-y: auto;  /* sidebar scrolls internally */
  padding: 0 8px 12px;
}

/* Conversation item */
.item { padding: 10px 12px; border-radius: 10px; cursor: pointer; margin: 6px 8px; }
.item:hover { background: #f3f4f6; }
.item.active { background: #e9f2ff; }
.item .num { font-weight: 700; }
.item .meta { font-size: 12px; color: #6b7280; margin-top: 2px; }

.row{ display:flex; align-items:flex-start; justify-content: space-between; gap: 10px; }
.item .row{ align-items:center; }
.row-actions{ display:flex; gap: 6px; flex: 0 0 auto; align-items:center; }



/* Chat */
.chat {
  display: flex;
  flex-direction: column;
  min-height: 0;    /* IMPORTANT */
  overflow: hidden; /* IMPORTANT */
}

.chat-header {
  padding: 12px 16px;
  background: #fff;
  border-bottom: 1px solid #e5e7eb;
  font-weight: 700;
  flex: 0 0 auto;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
}

.chat-title{ display:flex; flex-direction: column; gap: 2px; }
.chat-name{ font-weight: 800; }
.chat-sub{ font-weight: 500; font-size: 12px; color: #6b7280; }

.chat-actions{ display:flex; gap: 6px; align-items:center; }

.btn{ padding: 10px 14px; border: 1px solid #e5e7eb; border-radius: 10px; background: #fff; cursor: pointer; }
.btn.primary{ background:#111827; color:#fff; border-color:#111827; }
.btn.secondary{ background:#fff; }
.btn.danger{ background: #dc2626; color:#fff; border-color:#dc2626; }
.btn.small{ padding: 8px 10px; font-size: 12px; }

.messages {
  flex: 1 1 auto;
  min-height: 0;     /* IMPORTANT */
  overflow-y: auto;  /* messages scroll internally */
  padding: 16px;
}

/* Bubbles */
.bubble {
  max-width: 70%;
  padding: 10px 12px;
  margin: 8px 0;
  border-radius: 14px;          /* FIXED (was 14p) */
  line-height: 1.35;
  overflow-wrap: break-word;    /* modern */
  word-wrap: break-word;        /* legacy */
  position: relative;
}

.msg-del{
  position: absolute;
  top: 6px;
  right: 8px;
  border: none;
  background: transparent;
  cursor: pointer;
  opacity: 0;
  transition: opacity 120ms ease;
  font-size: 12px;
}

.bubble:hover .msg-del{ opacity: 0.9; }

.bubble .content { white-space: pre-wrap; }
.bubble audio{width:100%;max-width:650px;}
.inbound { background: #fff; border: 1px solid #e5e7eb; }
.outbound { background: #dbeafe; margin-left: auto; }

.time { font-size: 11px; color: #6b7280; margin-top: 6px; }

/* Composer */
.composer {
  display: flex;
  gap: 10px;
  padding: 12px;
  background: #fff;
  border-top: 1px solid #e5e7eb;
  flex: 0 0 auto; /* ALWAYS visible */
  padding-bottom: calc(12px + env(safe-area-inset-bottom, 0px));
}

#msg {
  flex: 1;
  padding: 10px;
  border: 1px solid #e5e7eb;
  border-radius: 10px;
  resize: none;
  min-height: 40px;
  max-height: 140px;
  overflow: auto;
  line-height: 1.35;
  font-family: inherit;
}

/* Variable inputs (template parameters) */
.var-row .ml-btn{
  width: 36px;
  height: 36px;
  border-radius: 10px;
  border: 1px solid #e3e3e3;
  background: #fff;
  cursor: pointer;
  flex: 0 0 auto;
}

.var-row textarea.v{
  min-height: 44px;
  resize: vertical;
}
#sendBtn { padding: 10px 14px; border: 0; border-radius: 10px; background: #111827; color: #fff; cursor: pointer; }
#sendBtn:disabled { opacity: .5; cursor: not-allowed; }

.icon-btn{
  width: 40px;
  height: 40px;
  border-radius: 10px;
  border: 1px solid #e5e7eb;
  background: #fff;
  cursor: pointer;
}

.emoji-panel{
  position: absolute;
  left: 460px;
  right: 16px;
  bottom: 70px;
  z-index: 20;
  background: #fff;
  border: 1px solid #e5e7eb;
  border-radius: 14px;
  padding: 10px;
  max-height: 220px;
  overflow: auto;
}

.emoji-panel button{
  border: 0;
  background: transparent;
  font-size: 22px;
  cursor: pointer;
  padding: 6px;
}

/* WhatsApp-style voice bubble */
.voice-bubble{
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 12px;
  border-radius: 14px;
  margin-top: 8px;
}
.inbound .voice-bubble{ background:#fff; border:1px solid #e5e7eb; }
.outbound .voice-bubble{ background:#dbeafe; }

.voice-play{
  width: 34px;
  height: 34px;
  border-radius: 999px;
  border: 0;
  background: rgba(17,24,39,0.08);
  cursor: pointer;
  font-size: 16px;
  line-height: 34px;
}

.voice-track{
  position: relative;
  flex: 1;
  height: 6px;
  border-radius: 999px;
  background: rgba(17,24,39,0.15);
  overflow: hidden;
}
.voice-progress{
  position: absolute;
  left: 0;
  top: 0;
  bottom: 0;
  width: 0%;
  background: rgba(17,24,39,0.45);
}
.voice-time{
  font-size: 12px;
  color: #6b7280;
  min-width: 44px;
  text-align: right;
}

.att { margin-top: 8px; }
.att-img { max-width: 100%; border-radius: 12px; display: block; }
.att-video { max-width: 100%; border-radius: 12px; display: block; }

@media (max-width: 900px) {
  .app {
    grid-template-columns: 1fr; /* single column */
  }

  /* default: show sidebar, hide chat */
  .chat { display: none; }

  /* when chat is open: hide sidebar, show chat */
  .app.chat-open .sidebar { display: none; }
  .app.chat-open .chat { display: flex; }

  .emoji-panel{
    left: 16px;
    right: 16px;
  }
    
}


/* --- New chat modal (templates) --- */
.header { display:flex; align-items:center; justify-content:space-between; gap:8px; }
.icon-btn.small { width:32px; height:32px; font-size:18px; }

.modal[hidden] { display:none !important; }
.modal {
  position: fixed; inset: 0;
  background: rgba(0,0,0,0.45);
  display: flex; align-items: center; justify-content: center;
  padding: 18px;
  z-index: 50;
}
.modal-card {
  width: min(520px, 100%);
  background: #fff;
  border-radius: 14px;
  padding: 16px;
  box-shadow: 0 12px 30px rgba(0,0,0,0.2);
}
.modal-title { font-weight: 700; margin-bottom: 12px; }
.lbl { display:block; margin-top: 10px; margin-bottom: 6px; font-size: 12px; opacity: 0.75; }
.input { width:100%; padding:10px; border:1px solid #e3e3e3; border-radius: 10px; }
.vars { margin-top: 8px; display:flex; flex-direction:column; gap:8px; }
.var-row { display:flex; gap:8px; align-items:center; }
.var-row .k { width: 56px; font-size: 12px; opacity: 0.7; }
.modal-actions { display:flex; justify-content:flex-end; gap:10px; margin-top: 14px; }
.modal .btn { padding: 10px 12px; border-radius: 10px; border: none; cursor: pointer; }
.modal .btn.secondary { background: #f1f1f1; }
.modal .btn.primary { background: #1f7ae0; color:#fff; }
.modal .btn.danger { background: #dc2626; color:#fff; }
.err { margin-top: 10px; color: #b00020; font-size: 12px; }


.modal-card.wide { max-width: 680px; }

/* Contacts layout: keep consistent left padding/alignment */
.contact-row{
  display: grid;
  grid-template-columns: 26px 1fr auto;
  gap: 12px;
  align-items: start;
}

.contact-sel-wrap{ display:flex; align-items:flex-start; padding-top: 4px; }
.contact-sel { width: 16px; height: 16px; margin: 0; }

.contact-info .num{ line-height: 1.1; }
.contact-info .meta{ margin-top: 4px; }

.hint { font-size: 12px; color: #666; margin: 6px 0 10px; }

.progress { margin-top: 12px; }
.progress-row { display:flex; justify-content: space-between; font-size: 12px; color:#444; margin-bottom:6px; }
.bar { height: 8px; background: rgba(0,0,0,.08); border-radius: 999px; overflow:hidden; }
.bar-fill { height: 100%; background: rgba(0,0,0,.35); }


.hidden { display: none !important; }

:root {
  --wa-bg: #efeae2;
  --wa-panel: #ffffff;
  --wa-sidebar: #f7f8fa;
  --wa-accent: #25d366;
  --wa-accent-dark: #128c7e;
  --wa-muted: #667781;
  --wa-out: #d9fdd3;
  --wa-in: #ffffff;
  --wa-border: #d8dee3;
}

body {
  background:
    radial-gradient(circle at top right, rgba(37,211,102,0.08), transparent 24%),
    linear-gradient(180deg, #f5f7fb 0%, var(--wa-bg) 100%);
}

.auth-screen {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px;
}

.auth-card {
  width: 100%;
  max-width: 420px;
  background: rgba(255,255,255,0.96);
  border: 1px solid rgba(17,24,39,0.06);
  border-radius: 24px;
  padding: 28px;
  box-shadow: 0 18px 60px rgba(17,24,39,0.14);
}

.auth-brand {
  display: flex;
  gap: 14px;
  align-items: center;
  margin-bottom: 22px;
}

.auth-logo {
  width: 54px;
  height: 54px;
  border-radius: 18px;
  background: linear-gradient(135deg, var(--wa-accent), var(--wa-accent-dark));
  display: grid;
  place-items: center;
  color: white;
  font-size: 26px;
  box-shadow: 0 10px 24px rgba(18,140,126,0.28);
}

.auth-card h1 { margin: 0; font-size: 28px; }
.auth-subtitle { margin: 6px 0 0; color: var(--wa-muted); }
.auth-form { display: flex; flex-direction: column; gap: 10px; }
.auth-form label { font-size: 14px; font-weight: 600; color: #1f2937; }
.auth-form input {
  height: 46px; border: 1px solid var(--wa-border); border-radius: 14px;
  padding: 0 14px; font-size: 15px; outline: none; background: #fff;
}
.auth-form input:focus { border-color: var(--wa-accent-dark); box-shadow: 0 0 0 4px rgba(18,140,126,0.08); }
.auth-form button {
  margin-top: 8px; height: 46px; border: 0; border-radius: 14px; cursor: pointer;
  background: linear-gradient(135deg, var(--wa-accent), var(--wa-accent-dark));
  color: #fff; font-size: 15px; font-weight: 700;
}
.auth-error { min-height: 20px; margin: 0; color: #b42318; font-size: 14px; }

.app {
  width: min(1600px, 100vw);
  margin: 0 auto;
}
.sidebar, .chat, .chat-header, .composer { background: rgba(255,255,255,0.92); backdrop-filter: blur(10px); }
.sidebar { background: rgba(247,248,250,0.96); }
.header { padding: 14px 16px; }
.search, #msg, .input, .tab, .btn, .icon-btn, #sendBtn {
  border-radius: 14px;
}
.search, #msg, .input { border-color: var(--wa-border); }
.item { border-radius: 18px; padding: 12px 14px; }
.item.active { background: #e7f7ef; }
.item .meta { color: var(--wa-muted); }
.bubble { border-radius: 18px; box-shadow: 0 1px 2px rgba(17,24,39,0.05); }
.inbound { background: var(--wa-in); }
.outbound { background: var(--wa-out); }
#sendBtn { background: linear-gradient(135deg, var(--wa-accent), var(--wa-accent-dark)); }
.current-user-label {
  font-size: 12px; color: var(--wa-muted); padding: 0 8px; max-width: 120px;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}

@media (max-width: 900px) {
  .app { grid-template-columns: 1fr; }
  .sidebar { display: flex; }
  .app.chat-open .sidebar { display: none; }
  .chat { display: none; }
  .app.chat-open .chat { display: flex; }
  .emoji-panel { left: 16px; }
}
