* { box-sizing: border-box; }

:root {
  --app-height: 100vh;
}

html, body {
  margin: 0;
  padding: 0;
  width: 100%;
  height: 100%;
  overflow: hidden;
  font-family: Arial, sans-serif;
  background: #e5ddd5;
  color: #111b21;
}

body {
  height: var(--app-height);
}

.app {
  display: flex;
  width: 100%;
  height: var(--app-height);
  overflow: hidden;
}

.sidebar {
  width: 340px;
  min-width: 340px;
  background: #ffffff;
  border-right: 1px solid #d1d7db;
  display: flex;
  flex-direction: column;
  height: 100%;
}

.sidebar-header {
  padding: 16px;
  background: #f0f2f5;
  border-bottom: 1px solid #d1d7db;
  font-weight: bold;
  font-size: 18px;
  min-height: 64px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}

.logout-button {
  border: none;
  background: transparent;
  color: #54656f;
  cursor: pointer;
  font-size: 14px;
}

.chat-list {
  flex: 1;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  min-height: 0;
}

.chat-item {
  padding: 14px 16px;
  border-bottom: 1px solid #f0f2f5;
  cursor: pointer;
  transition: background 0.2s ease;
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.chat-item:hover {
  background: #f5f6f6;
}

.chat-item.active {
  background: #f0f2f5;
}

.chat-item.has-unread .chat-phone {
  font-weight: 800;
}

.chat-item.has-unread .chat-preview {
  font-weight: 700;
  color: #111b21;
}

.chat-top {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
}

.chat-bottom {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
}

.chat-phone {
  font-weight: 600;
  font-size: 15px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.chat-time {
  font-size: 12px;
  color: #667781;
  white-space: nowrap;
}

.chat-preview {
  font-size: 13px;
  color: #667781;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  flex: 1;
}

.unread-badge {
  min-width: 20px;
  height: 20px;
  border-radius: 999px;
  background: #00a884;
  color: white;
  font-size: 11px;
  font-weight: 700;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  padding: 0 6px;
}

.template-panel {
  padding: 12px;
  border-top: 1px solid #e5e5e5;
  background: #fafafa;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.template-panel h3 {
  margin: 0 0 4px;
  font-size: 14px;
}

.template-panel input,
.template-panel textarea {
  width: 100%;
  padding: 10px 12px;
  border: 1px solid #d1d7db;
  border-radius: 10px;
  font-size: 14px;
  font-family: Arial, sans-serif;
}

.template-panel textarea {
  min-height: 90px;
  resize: vertical;
}

.template-send-button {
  border: none;
  background: #00a884;
  color: white;
  padding: 10px 12px;
  border-radius: 10px;
  cursor: pointer;
  font-weight: 700;
}

.main {
  flex: 1;
  display: flex;
  flex-direction: column;
  background: #efeae2;
  height: 100%;
  min-width: 0;
  min-height: 0;
}

.main-header {
  padding: 16px 20px;
  background: #f0f2f5;
  border-bottom: 1px solid #d1d7db;
  font-weight: 600;
  min-height: 64px;
  display: flex;
  align-items: center;
  gap: 12px;
}

.back-button {
  display: none;
  border: none;
  background: transparent;
  font-size: 24px;
  cursor: pointer;
  color: #111b21;
  padding: 0;
  margin: 0;
  line-height: 1;
}

.messages {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  padding: 16px 16px 8px;
  display: flex;
  flex-direction: column;
  gap: 8px;
  background: #efeae2;
}

.message-row {
  display: flex;
  width: 100%;
  align-items: flex-end;
}

.message-row.inbound {
  justify-content: flex-start;
}

.message-row.outbound {
  justify-content: flex-end;
}

.bubble {
  max-width: 72%;
  width: fit-content;
  min-width: 100px;
  padding: 8px 10px;
  border-radius: 10px;
  box-shadow: 0 1px 0 rgba(11, 20, 26, 0.13);
  word-wrap: break-word;
  white-space: pre-wrap;
  line-height: 1.35;
  font-size: 14px;
  display: inline-flex;
  flex-direction: column;
  position: relative;
}

.bubble.inbound {
  background: #ffffff;
  border-top-left-radius: 4px;
}

.bubble.outbound {
  background: #d9fdd3;
  border-top-right-radius: 4px;
}

.bubble.inbound::before {
  content: "";
  position: absolute;
  top: 0;
  left: -8px;
  width: 0;
  height: 0;
  border-top: 10px solid #ffffff;
  border-left: 10px solid transparent;
}

.bubble.outbound::before {
  content: "";
  position: absolute;
  top: 0;
  right: -8px;
  width: 0;
  height: 0;
  border-top: 10px solid #d9fdd3;
  border-right: 10px solid transparent;
}

.channel-label {
  display: inline-block;
  font-size: 11px;
  font-weight: 700;
  color: #54656f;
  background: rgba(0, 0, 0, 0.06);
  border-radius: 999px;
  padding: 3px 8px;
  margin-bottom: 6px;
  align-self: flex-start;
}

.audio-box {
  margin-top: 8px;
}

.audio-box audio {
  width: 100%;
  max-width: 320px;
}

.message-footer {
  display: flex;
  justify-content: flex-end;
  margin-top: 4px;
}

.message-time {
  font-size: 11px;
  color: #667781;
}

.composer {
  display: flex;
  gap: 10px;
  align-items: center;
  padding: 10px 12px calc(10px + env(safe-area-inset-bottom));
  background: #f0f2f5;
  border-top: 1px solid #d1d7db;
  flex-shrink: 0;
}

.composer input {
  flex: 1;
  min-width: 0;
  padding: 13px 16px;
  border: 1px solid #d1d7db;
  border-radius: 999px;
  font-size: 16px;
  outline: none;
  background: white;
}

.composer button {
  border: none;
  background: #00a884;
  color: white;
  width: 44px;
  height: 44px;
  border-radius: 999px;
  font-size: 14px;
  font-weight: 700;
  cursor: pointer;
  flex-shrink: 0;
}

.empty-state {
  margin: auto;
  text-align: center;
  color: #667781;
  padding: 24px;
}

@media (max-width: 700px) {
  .app {
    position: relative;
    width: 100%;
    height: var(--app-height);
    overflow: hidden;
  }

  .sidebar,
  .main {
    width: 100%;
    min-width: 100%;
    height: 100%;
  }

  .sidebar {
    position: absolute;
    inset: 0;
    z-index: 1;
    border-right: none;
    background: #fff;
  }

  .main {
    position: absolute;
    inset: 0;
    z-index: 2;
    background: #efeae2;
    transform: translateX(100%);
    transition: transform 0.2s ease;
  }

  .app.chat-open .main {
    transform: translateX(0);
  }

  .app.chat-open .sidebar {
    display: none;
  }

  .back-button {
    display: inline-block;
  }

  .sidebar-header,
  .main-header {
    min-height: 60px;
    padding: 14px 16px;
  }

  .messages {
    padding: 12px;
    gap: 8px;
  }

  .bubble {
    max-width: 82%;
    min-width: 0;
    font-size: 15px;
    padding: 8px 10px;
  }

  .composer {
    padding: 10px 12px calc(10px + env(safe-area-inset-bottom));
  }

  .composer input {
    font-size: 16px;
    padding: 12px 14px;
  }

  .composer button {
    width: 42px;
    height: 42px;
  }

  .logout-button {
    font-size: 13px;
  }

  .template-panel {
    padding-bottom: calc(12px + env(safe-area-inset-bottom));
  }
}
