/* ============================================================
   Design Tokens
   ============================================================ */
:root {
  --color-bg:      #f5f3f7;
  --color-surface: #ffffff;
  --color-primary: #7c3aed;
  --color-primary-light: #a78bfa;
  --color-border:  #e9e5f0;
  --color-text:    #1e1b2e;
  --color-muted:   #6b6180;
  --shadow-card:   0 1px 3px rgba(0,0,0,.12), 0 1px 2px rgba(0,0,0,.06);
}

/* ============================================================
   Global Reset
   ============================================================ */
* { box-sizing: border-box; margin: 0; padding: 0; }

a, button, select, input, [role="tab"] {
  touch-action: manipulation;
}

html, body {
  overflow-x: hidden;
  width: 100%;
  max-width: 100%;
}

body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  font-size: 15px;
  background: var(--color-bg);
  color: var(--color-text);
  min-height: 100vh;
}

/* ============================================================
   Layout
   ============================================================ */
.layout {
  display: flex;
  flex-direction: column;
  min-height: 100vh;
}

@media (min-width: 769px) {
  .layout {
    height: 100vh;
    overflow: hidden;
  }
}

/* ============================================================
   App Header
   ============================================================ */
.app-header {
  display: flex;
  align-items: center;
  gap: 16px;
  padding: 10px 16px;
  background: linear-gradient(135deg, #7c3aed 0%, #9333ea 50%, #6d28d9 100%);
  flex-shrink: 0;
}

.app-header h1 {
  font-size: 16px;
  font-weight: 700;
  color: #ffffff;
  line-height: 1.3;
}

.app-header h1 span {
  font-weight: 400;
  font-size: 12px;
  color: rgba(255,255,255,0.7);
}

.app-header .header-spacer {
  flex: 1;
}

.app-header .header-menu-wrapper {
  position: relative;
}

.header-menu-btn {
  font-size: 18px;
  color: rgba(255,255,255,0.9);
  background: rgba(255,255,255,0.15);
  border: 1px solid rgba(255,255,255,0.3);
  border-radius: 4px;
  padding: 4px 8px;
  cursor: pointer;
  line-height: 1;
}
.header-menu-btn:hover { background: rgba(255,255,255,0.25); }

.header-menu-dropdown {
  display: none;
  position: absolute;
  top: calc(100% + 6px);
  right: 0;
  background: white;
  border: 1px solid var(--color-border);
  border-radius: 4px;
  box-shadow: 0 4px 12px rgba(0,0,0,.15);
  z-index: 200;
  min-width: 120px;
  padding: 4px 0;
}
.header-menu-dropdown.open { display: block; }

.header-menu-item {
  display: block;
  width: 100%;
  padding: 8px 14px;
  font-size: 13px;
  color: var(--color-text);
  background: none;
  border: none;
  text-align: left;
  cursor: pointer;
}
.header-menu-item:hover { background: var(--color-bg); }

/* ============================================================
   Status Tabs
   ============================================================ */
.status-tabs {
  display: flex;
  gap: 4px;
  padding: 10px 16px;
  background: var(--color-surface);
  border-bottom: 1px solid var(--color-border);
  overflow-x: auto;
  flex-shrink: 0;
}

.tab {
  padding: 6px 12px;
  font-size: 13px;
  border: 1px solid var(--color-border);
  border-radius: 4px;
  background: white;
  color: var(--color-muted);
  cursor: pointer;
  white-space: nowrap;
  transition: all 0.15s;
}
.tab:hover {
  border-color: var(--color-primary);
  color: var(--color-primary);
}
.tab.active {
  background: var(--color-primary);
  color: white;
  border-color: var(--color-primary);
}

.tabs-spacer {
  flex: 1;
}

.btn-new-lead {
  padding: 6px 14px;
  font-size: 13px;
  font-weight: 500;
  border: none;
  border-radius: 4px;
  background: var(--color-primary);
  color: white;
  cursor: pointer;
  white-space: nowrap;
  transition: opacity 0.15s;
}
.btn-new-lead:hover { opacity: 0.88; }

/* ============================================================
   Content Area
   ============================================================ */
.content-area {
  display: flex;
  flex: 1;
  overflow: hidden;
}

/* ============================================================
   Lead List
   ============================================================ */
.lead-list {
  flex: 1;
  overflow-y: auto;
  padding: 8px 16px;
}

.lead-card {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 12px 14px;
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  border-radius: 6px;
  margin-bottom: 6px;
  cursor: pointer;
  transition: background 0.15s, border-color 0.15s;
  box-shadow: var(--shadow-card);
}
.lead-card:hover {
  border-color: var(--color-primary-light);
  background: #faf8fc;
}

.lead-card-left {
  display: flex;
  flex-direction: column;
  gap: 2px;
  min-width: 0;
}

.lead-card-name {
  font-weight: 600;
  font-size: 14px;
  color: var(--color-text);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.lead-card-email {
  font-size: 12px;
  color: var(--color-muted);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.lead-card-right {
  display: flex;
  align-items: center;
  gap: 10px;
  flex-shrink: 0;
}

.lead-card-time {
  font-size: 11px;
  color: var(--color-muted);
}

.status-badge {
  font-size: 11px;
  padding: 2px 8px;
  border-radius: 12px;
  font-weight: 500;
  text-transform: capitalize;
}

.status-new       { background: #ede9fe; color: #7c3aed; }
.status-contacted { background: #fef3c7; color: #d97706; }
.status-quoted    { background: #d1fae5; color: #059669; }
.status-won       { background: #bbf7d0; color: #16a34a; }
.status-lost      { background: #fee2e2; color: #dc2626; }

.empty, .loading {
  text-align: center;
  color: var(--color-muted);
  padding: 48px 16px;
  font-size: 14px;
}

/* ============================================================
   Lead Detail Panel
   ============================================================ */
.detail-panel {
  width: 420px;
  max-width: 100%;
  border-left: 1px solid var(--color-border);
  background: var(--color-surface);
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.detail-panel[hidden] { display: none; }

.detail-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 16px;
  border-bottom: 1px solid var(--color-border);
  flex-shrink: 0;
}

.btn-back {
  background: none;
  border: none;
  color: var(--color-primary);
  font-size: 13px;
  cursor: pointer;
  padding: 4px 8px;
  border-radius: 4px;
}
.btn-back:hover { background: var(--color-bg); }

.detail-status-select {
  padding: 4px 8px;
  font-size: 12px;
  border: 1px solid var(--color-border);
  border-radius: 4px;
  background: white;
  color: var(--color-text);
  cursor: pointer;
}
.detail-status-select:focus {
  outline: none;
  border-color: var(--color-primary);
}

/* Lead info */
.detail-info {
  padding: 16px;
  border-bottom: 1px solid var(--color-border);
  flex-shrink: 0;
}

.detail-info h2 {
  font-size: 18px;
  font-weight: 600;
  margin-bottom: 6px;
}

.detail-info p {
  font-size: 13px;
  color: var(--color-muted);
  margin-bottom: 2px;
}

/* Conversation */
.conversation {
  flex: 1;
  overflow-y: auto;
  padding: 16px;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.conversation-title {
  font-size: 12px;
  font-weight: 600;
  color: var(--color-muted);
  text-transform: uppercase;
  letter-spacing: 0.05em;
  margin-bottom: 4px;
}

.message {
  padding: 8px 12px;
  border-radius: 6px;
  font-size: 13px;
  line-height: 1.5;
  max-width: 85%;
}

.message-in {
  background: var(--color-bg);
  border: 1px solid var(--color-border);
  align-self: flex-start;
}

.message-out {
  background: var(--color-primary);
  color: white;
  align-self: flex-end;
}

.message-meta {
  font-size: 11px;
  color: var(--color-muted);
  margin-bottom: 3px;
}

.message-out .message-meta {
  color: rgba(255,255,255,0.7);
}

.message-body {
  white-space: pre-wrap;
  word-break: break-word;
}

/* Reply form */
.reply-form {
  border-top: 1px solid var(--color-border);
  padding: 12px 16px;
  display: flex;
  flex-direction: column;
  gap: 8px;
  flex-shrink: 0;
}

.reply-form input,
.reply-form textarea {
  width: 100%;
  padding: 8px 12px;
  font-size: 13px;
  border: 1px solid var(--color-border);
  border-radius: 6px;
  background: var(--color-bg);
  color: var(--color-text);
  font-family: inherit;
  resize: vertical;
}
.reply-form input:focus,
.reply-form textarea:focus {
  outline: none;
  border-color: var(--color-primary);
  box-shadow: 0 0 0 2px rgba(124,58,237,0.1);
}
.reply-form input::placeholder,
.reply-form textarea::placeholder {
  color: var(--color-muted);
}

.btn-send {
  align-self: flex-end;
  padding: 8px 20px;
  font-size: 13px;
  font-weight: 500;
  background: var(--color-primary);
  color: white;
  border: none;
  border-radius: 6px;
  cursor: pointer;
  transition: opacity 0.15s;
}
.btn-send:hover { opacity: 0.88; }
.btn-send:disabled { opacity: 0.5; cursor: not-allowed; }

/* ============================================================
   Compose Form
   ============================================================ */
.compose-title {
  font-size: 14px;
  font-weight: 600;
  color: var(--color-text);
}

.compose-form {
  flex: 1;
  overflow-y: auto;
  padding: 16px;
  display: flex;
  flex-direction: column;
  gap: 14px;
}

.compose-field {
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.compose-field label {
  font-size: 12px;
  font-weight: 500;
  color: var(--color-muted);
}

.compose-field input,
.compose-field textarea {
  width: 100%;
  padding: 8px 12px;
  font-size: 13px;
  border: 1px solid var(--color-border);
  border-radius: 6px;
  background: var(--color-bg);
  color: var(--color-text);
  font-family: inherit;
  resize: vertical;
}
.compose-field input:focus,
.compose-field textarea:focus {
  outline: none;
  border-color: var(--color-primary);
  box-shadow: 0 0 0 2px rgba(124,58,237,0.1);
}
.compose-field input::placeholder,
.compose-field textarea::placeholder {
  color: var(--color-muted);
}

.compose-form .btn-send {
  align-self: flex-end;
  margin-top: 4px;
}

/* ============================================================
   Mobile Responsive
   ============================================================ */
@media (max-width: 768px) {
  .content-area {
    flex-direction: column;
  }

  .lead-list {
    padding: 8px 10px;
  }

  .detail-panel {
    position: fixed;
    inset: 0;
    width: 100%;
    border-left: none;
    z-index: 100;
  }

  .lead-card {
    flex-direction: column;
    align-items: flex-start;
    gap: 6px;
  }

  .lead-card-right {
    width: 100%;
    justify-content: space-between;
  }

  .status-tabs {
    padding: 8px 10px;
  }
}
