:root {
    --bg: #f5f6f8;
    --surface: #ffffff;
    --border: #e2e5ea;
    --text: #1a1d23;
    --muted: #6b7280;
    --primary: #1f6feb;
    --primary-hover: #1957c7;
    --danger: #b4232c;
    --success: #137a3e;
    --radius: 8px;
    --shadow: 0 1px 2px rgba(15, 23, 42, 0.04), 0 4px 12px rgba(15, 23, 42, 0.06);
}

* { box-sizing: border-box; }

html, body {
    margin: 0;
    padding: 0;
    background: var(--bg);
    color: var(--text);
    font: 14px/1.45 -apple-system, BlinkMacSystemFont, "Segoe UI", system-ui, sans-serif;
}

a { color: var(--primary); text-decoration: none; }
a:hover { text-decoration: underline; }

.muted { color: var(--muted); }
.small { font-size: 12px; }
.hidden { display: none !important; }

/* ---------------- header ---------------- */
.app-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 12px 20px;
    background: var(--surface);
    border-bottom: 1px solid var(--border);
}
.brand { font-size: 15px; }
.user { display: flex; gap: 14px; align-items: center; }
.link { color: var(--primary); }

/* ---------------- layout ---------------- */
.app-main {
    display: grid;
    grid-template-columns: 220px 1fr;
    gap: 20px;
    padding: 20px;
    min-height: calc(100vh - 56px);
}
.sidebar {
    background: var(--surface);
    border-radius: var(--radius);
    border: 1px solid var(--border);
    padding: 14px;
    align-self: start;
}
.sidebar h3 {
    margin: 0 0 10px 0;
    font-size: 12px;
    text-transform: uppercase;
    letter-spacing: 0.05em;
    color: var(--muted);
}
.sidebar ul { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 4px; }
.form-btn {
    display: block;
    width: 100%;
    text-align: left;
    background: transparent;
    border: 0;
    padding: 8px 10px;
    border-radius: 6px;
    cursor: pointer;
    color: var(--text);
    font: inherit;
}
.form-btn:hover { background: #f0f3f8; }
.form-btn.active { background: #e6efff; color: var(--primary); font-weight: 600; }

.content {
    background: var(--surface);
    border-radius: var(--radius);
    border: 1px solid var(--border);
    padding: 16px;
    display: flex;
    flex-direction: column;
    gap: 14px;
    box-shadow: var(--shadow);
}

/* ---------------- toolbar ---------------- */
.toolbar {
    display: flex;
    justify-content: space-between;
    gap: 12px;
    flex-wrap: wrap;
    align-items: end;
}
.filters, .actions { display: flex; gap: 10px; align-items: end; flex-wrap: wrap; }
.filters label { display: flex; flex-direction: column; font-size: 12px; color: var(--muted); gap: 2px; }
.filters .grow { flex: 1 1 200px; }
.filters input[type="date"], .filters input[type="search"] {
    padding: 6px 8px;
    border: 1px solid var(--border);
    border-radius: 6px;
    font: inherit;
    color: var(--text);
    background: white;
}

.btn {
    padding: 8px 14px;
    border-radius: 6px;
    border: 1px solid transparent;
    font: inherit;
    cursor: pointer;
    text-decoration: none;
    display: inline-flex;
    align-items: center;
}
.btn-primary { background: var(--primary); color: white; }
.btn-primary:hover { background: var(--primary-hover); text-decoration: none; }
.btn-secondary { background: white; color: var(--text); border-color: var(--border); }
.btn-secondary:hover { background: #f5f7fb; }

.status { min-height: 18px; font-size: 12px; }
.status.error { color: var(--danger); }
.status.success { color: var(--success); }

/* ---------------- table ---------------- */
.table-wrap { overflow-x: auto; }
table {
    width: 100%;
    border-collapse: collapse;
    font-size: 13px;
}
th, td {
    padding: 10px 8px;
    border-bottom: 1px solid var(--border);
    text-align: left;
    vertical-align: top;
}
th {
    font-size: 11px;
    text-transform: uppercase;
    letter-spacing: 0.04em;
    color: var(--muted);
    background: #fbfcfd;
    position: sticky;
    top: 0;
}
td.num { color: var(--muted); width: 32px; }
.source-badge {
    display: inline-block;
    padding: 1px 7px;
    border-radius: 999px;
    font-size: 11px;
    background: #e8eefc;
    color: var(--primary);
}
.source-badge.manual { background: #f1e9d9; color: #8a5a00; }
.source-badge.form   { background: #dcfce7; color: #137a3e; }
.empty-leads { text-align: center; padding: 40px; color: var(--muted); }
.actions-cell { width: 32px; text-align: right; }
.btn-delete {
    border: 0;
    background: transparent;
    color: var(--muted);
    width: 26px;
    height: 26px;
    border-radius: 6px;
    font-size: 18px;
    line-height: 1;
    cursor: pointer;
}
.btn-delete:hover { background: #fde8ea; color: var(--danger); }
.btn-delete:disabled { opacity: 0.4; cursor: wait; }

.btn-archive, .btn-restore {
    border: 0;
    background: transparent;
    width: 28px;
    height: 28px;
    border-radius: 6px;
    font-size: 14px;
    line-height: 1;
    cursor: pointer;
}
.btn-archive { color: var(--danger); }
.btn-archive:hover { background: #fde8ea; color: #8a1a1a; }
.btn-restore { color: var(--success); }
.btn-restore:hover { background: #dcfce7; color: #0a5a2a; }
.btn-archive:disabled, .btn-restore:disabled { opacity: 0.4; cursor: wait; }

.btn-edit-lead {
    border: 0;
    background: transparent;
    color: var(--muted);
    width: 28px;
    height: 28px;
    border-radius: 6px;
    font-size: 14px;
    line-height: 1;
    cursor: pointer;
    margin-right: 2px;
}
.btn-edit-lead:hover { background: #e8eefc; color: var(--primary); }

#edit-dialog h4 {
    margin: 14px 0 6px 0;
    font-size: 12px;
    text-transform: uppercase;
    letter-spacing: 0.05em;
    color: var(--muted);
}
#edit-dialog .grid-2 {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 10px;
    margin-bottom: 0;
}
#edit-dialog { max-width: 560px; width: 95vw; max-height: 90vh; overflow-y: auto; }
@media (max-width: 540px) {
    #edit-dialog .grid-2 { grid-template-columns: 1fr; }
}

.note-cell { max-width: 240px; }
.note-text {
    cursor: text;
    display: inline-block;
    min-width: 60px;
    padding: 2px 4px;
    border-radius: 4px;
    color: var(--text);
}
.note-text:hover { background: #f1f5f9; }
.note-edit {
    width: 100%;
    padding: 4px 6px;
    border: 1px solid var(--primary);
    border-radius: 4px;
    font: inherit;
    color: var(--text);
    background: white;
}

.toggle-archived {
    display: flex;
    align-items: center;
    gap: 6px;
    font-size: 13px;
    color: var(--muted);
    padding: 0 8px;
    cursor: pointer;
    user-select: none;
}
.toggle-archived input { width: 14px; height: 14px; cursor: pointer; }

.email-cell { white-space: nowrap; text-align: center; }
.email-sent {
    color: var(--success);
    font-weight: 600;
    margin-right: 4px;
}
.btn-send {
    border: 1px solid var(--primary);
    background: white;
    color: var(--primary);
    padding: 3px 10px;
    border-radius: 12px;
    font-size: 12px;
    cursor: pointer;
}
.btn-send:hover { background: var(--primary); color: white; }
.btn-resend {
    border: 0;
    background: transparent;
    color: var(--muted);
    width: 22px;
    height: 22px;
    border-radius: 4px;
    font-size: 13px;
    cursor: pointer;
}
.btn-resend:hover { background: #e8eefc; color: var(--primary); }
.btn-send:disabled, .btn-resend:disabled { opacity: 0.5; cursor: wait; }

.whatsapp-cell { text-align: center; white-space: nowrap; }
.btn-whatsapp {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 28px;
    height: 28px;
    border-radius: 6px;
    text-decoration: none;
    background: transparent;
    color: #25D366; /* WhatsApp green */
}
.btn-whatsapp:hover {
    background: #d9fdd3;
    text-decoration: none;
    color: #128C7E;
}

.icon-th {
    text-align: center;
    color: var(--muted);
}
.icon-th.wa {
    color: #25D366;
}
.icon-th svg {
    vertical-align: middle;
    display: inline-block;
}

.btn-edit, .btn-delete-user {
    border: 0;
    background: transparent;
    color: var(--muted);
    width: 26px;
    height: 26px;
    border-radius: 6px;
    font-size: 15px;
    line-height: 1;
    cursor: pointer;
    margin-left: 2px;
}
.btn-edit:hover { background: #e8eefc; color: var(--primary); }
.btn-delete-user:hover { background: #fde8ea; color: var(--danger); }

.role-badge {
    display: inline-block;
    padding: 1px 8px;
    border-radius: 999px;
    font-size: 11px;
    background: #e2e8f0;
    color: #334155;
}
.role-badge.admin { background: #dcfce7; color: #137a3e; }

.app-main.single { grid-template-columns: 1fr; }

dialog .checkbox {
    flex-direction: row;
    align-items: center;
    gap: 8px;
    color: var(--text);
    font-size: 14px;
}
dialog .checkbox input {
    width: 16px;
    height: 16px;
}

/* ---------------- login ---------------- */
.login-body {
    min-height: 100vh;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 20px;
}
.login-card {
    width: 100%;
    max-width: 360px;
    background: var(--surface);
    border-radius: 12px;
    border: 1px solid var(--border);
    padding: 32px;
    text-align: center;
    box-shadow: var(--shadow);
}
.login-card h1 { margin: 0 0 4px 0; font-size: 20px; }
.login-card form { display: flex; flex-direction: column; gap: 12px; margin: 20px 0 12px; text-align: left; }
.login-card label { display: flex; flex-direction: column; font-size: 12px; color: var(--muted); gap: 3px; }
.login-card input {
    padding: 9px 10px;
    border: 1px solid var(--border);
    border-radius: 6px;
    font: inherit;
    color: var(--text);
}
.login-card button { margin-top: 6px; padding: 10px; justify-content: center; }
.login-card .g_id_signin { margin: 24px auto 16px; display: inline-block; }
.error {
    background: #fde8ea;
    color: var(--danger);
    padding: 10px;
    border-radius: 6px;
    margin: 12px 0;
    font-size: 13px;
    text-align: left;
}

/* ---------------- dialog ---------------- */
dialog {
    border: 0;
    border-radius: 12px;
    padding: 24px;
    min-width: 340px;
    box-shadow: var(--shadow);
}
dialog::backdrop { background: rgba(15, 23, 42, 0.35); }
dialog h3 { margin: 0 0 16px 0; }
dialog label {
    display: flex;
    flex-direction: column;
    font-size: 12px;
    color: var(--muted);
    gap: 3px;
    margin-bottom: 10px;
}
dialog input, dialog textarea {
    padding: 7px 9px;
    border: 1px solid var(--border);
    border-radius: 6px;
    font: inherit;
    color: var(--text);
}
.dialog-actions {
    display: flex;
    justify-content: flex-end;
    gap: 8px;
    margin-top: 8px;
}

.empty-state {
    background: var(--surface);
    grid-column: 1 / -1;
    border-radius: var(--radius);
    border: 1px solid var(--border);
    padding: 32px;
    text-align: center;
}
.empty-state code {
    background: #f2f4f8;
    padding: 2px 6px;
    border-radius: 4px;
    font-size: 13px;
}

/* ---------------- responsive ---------------- */
@media (max-width: 780px) {
    .app-main { grid-template-columns: 1fr; padding: 12px; gap: 12px; }
    .sidebar ul { flex-direction: row; overflow-x: auto; }
    .form-btn { white-space: nowrap; }
    .toolbar { flex-direction: column; align-items: stretch; }
    .filters, .actions { width: 100%; }
    .filters .grow { flex: 1 1 100%; }
    th, td { font-size: 12px; padding: 8px 6px; }
}
